diff --git a/include/llvm/Support/Compiler.h b/include/llvm/Support/Compiler.h index 7186b22a403..90292df3819 100644 --- a/include/llvm/Support/Compiler.h +++ b/include/llvm/Support/Compiler.h @@ -29,6 +29,12 @@ #define ATTRIBUTE_USED #endif +#if (__GNUC__ >= 4) +#define BUILTIN_EXPECT(EXPR, VALUE) __builtin_expect((EXPR), (VALUE)) +#else +#define BUILTIN_EXPECT(EXPR, VALUE) (EXPR) +#endif + // C++ doesn't support 'extern template' of template specializations. GCC does, // but requires __extension__ before it. In the header, use this: // EXTERN_TEMPLATE_INSTANTIATION(class foo); diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index 639d6fa570f..f62a31d1990 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -16,6 +16,7 @@ #include "llvm/System/Program.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Config/config.h" +#include "llvm/Support/Compiler.h" #include #if defined(HAVE_UNISTD_H) @@ -142,7 +143,7 @@ raw_ostream &raw_ostream::write(unsigned char C) { raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { // Group exceptional cases into a single branch. - if (OutBufCur+Size > OutBufEnd) { + if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) { if (Unbuffered) { write_impl(Ptr, Size); return *this;