]> granicus.if.org Git - clang/commitdiff
Introduce __builtin_expect() intrinsic support.
authorJakub Staszak <jstaszak@apple.com>
Fri, 8 Jul 2011 22:45:14 +0000 (22:45 +0000)
committerJakub Staszak <jstaszak@apple.com>
Fri, 8 Jul 2011 22:45:14 +0000 (22:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134761 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBuiltin.cpp
test/CodeGen/builtin-expect.c

index 269917b4427ba7988aae5b2c88a8eb3c748b811a..8f29663613da51d49b1c3b3f8ac0944f391d9c3c 100644 (file)
@@ -22,6 +22,7 @@
 #include "clang/Basic/TargetBuiltins.h"
 #include "llvm/Intrinsics.h"
 #include "llvm/Target/TargetData.h"
+
 using namespace clang;
 using namespace CodeGen;
 using namespace llvm;
@@ -311,11 +312,16 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
     return RValue::get(Result);
   }
   case Builtin::BI__builtin_expect: {
-    // FIXME: pass expect through to LLVM
     Value *ArgValue = EmitScalarExpr(E->getArg(0));
-    if (E->getArg(1)->HasSideEffects(getContext()))
-      (void)EmitScalarExpr(E->getArg(1));
-    return RValue::get(ArgValue);
+    const llvm::Type *ArgType = ArgValue->getType();
+
+    Value *FnExpect = CGM.getIntrinsic(Intrinsic::expect, &ArgType, 1);
+    Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
+
+    Value *Result = Builder.CreateCall2(FnExpect, ArgValue, ExpectedValue,
+                                        "expval");
+    return RValue::get(Result);
+
   }
   case Builtin::BI__builtin_bswap32:
   case Builtin::BI__builtin_bswap64: {
index 88479d90a0926730552bf2c0bb444962d659cbf5..664c6b6a470ecb45c8bf458927d1886431460d0e 100644 (file)
@@ -19,3 +19,29 @@ int main() {
 
 // CHECK: call void @isigprocmask()
 // CHECK: [[C:%.*]] = call i64 (...)* @bar()
+
+
+// CHECK: @test1
+int test1(int x) {
+// CHECK: @llvm.expect
+  if (__builtin_expect (x, 1))
+    return 0;
+  return x;
+}
+
+// CHECK: @test2
+int test2(int x) {
+// CHECK: @llvm.expect
+  switch(__builtin_expect(x, 5)) {
+  default:
+    return 0;
+  case 0:
+  case 1:
+  case 2:
+    return 1;
+  case 5:
+    return 5;
+  };
+
+  return 0;
+}