Make some intrinsics safe to speculatively execute.
authorNick Lewycky <nicholas@mxc.ca>
Wed, 21 Dec 2011 05:52:02 +0000 (05:52 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Wed, 21 Dec 2011 05:52:02 +0000 (05:52 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@147036 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ValueTracking.cpp
lib/Transforms/Utils/SimplifyCFG.cpp
test/Transforms/SimplifyCFG/SpeculativeExec.ll

index ef19e065b70769f7b164e7fcfe64d699c31fd019..4739d1b881adce0ba2a3535fdb4726e327a355d8 100644 (file)
@@ -1912,11 +1912,31 @@ bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,
       return false;
     return LI->getPointerOperand()->isDereferenceablePointer();
   }
-  case Instruction::Call:
+  case Instruction::Call: {
+   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
+     switch (II->getIntrinsicID()) {
+       case Intrinsic::bswap:
+       case Intrinsic::ctlz:
+       case Intrinsic::ctpop:
+       case Intrinsic::cttz:
+       case Intrinsic::objectsize:
+       case Intrinsic::sadd_with_overflow:
+       case Intrinsic::smul_with_overflow:
+       case Intrinsic::ssub_with_overflow:
+       case Intrinsic::uadd_with_overflow:
+       case Intrinsic::umul_with_overflow:
+       case Intrinsic::usub_with_overflow:
+         return true;
+       // TODO: some fp intrinsics are marked as having the same error handling
+       // as libm. They're safe to speculate when they won't error.
+       // TODO: are convert_{from,to}_fp16 safe?
+       // TODO: can we list target-specific intrinsics here?
+       default: break;
+     }
+   }
     return false; // The called function could have undefined behavior or
-                  // side-effects.
-                  // FIXME: We should special-case some intrinsics (bswap,
-                  // overflow-checking arithmetic, etc.)
+                  // side-effects, even if marked readnone nounwind.
+  }
   case Instruction::VAArg:
   case Instruction::Alloca:
   case Instruction::Invoke:
index bf2cb49bdcde66c985f5614afa629ea56e60cb11..4fdbaaef78f0e8a0e88e79a11d8ba502374a4dae 100644 (file)
@@ -293,6 +293,7 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
     Cost = 1;
     break;   // These are all cheap and non-trapping instructions.
 
+  case Instruction::Call:
   case Instruction::Select:
     Cost = 2;
     break;
index 5cfc77ce08a5b1b51ab7520e2dd1228d7ddfd101..4fbb64a4582b25b24b2b2168628f8013d3cb0579 100644 (file)
@@ -1,7 +1,10 @@
-; RUN: opt < %s -simplifycfg  -S | grep select
-; RUN: opt < %s -simplifycfg  -S | grep br | count 2
+; RUN: opt < %s -simplifycfg -phi-node-folding-threshold=2 -S | FileCheck %s
 
-define i32 @t2(i32 %a, i32 %b, i32 %c) nounwind  {
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @test1(i32 %a, i32 %b, i32 %c) nounwind  {
+; CHECK: @test1
 entry:
         %tmp1 = icmp eq i32 %b, 0
         br i1 %tmp1, label %bb1, label %bb3
@@ -9,6 +12,11 @@ entry:
 bb1:            ; preds = %entry
        %tmp2 = icmp sgt i32 %c, 1
        br i1 %tmp2, label %bb2, label %bb3
+; CHECK: bb1:
+; CHECK-NEXT: add i32 %a, 1
+; CHECK-NEXT: icmp sgt i32 %c, 1
+; CHECK-NEXT: select i1 %tmp2, i32 %tmp3, i32 %a
+; CHECK-NEXT: br label %bb3
 
 bb2:           ; preds = bb1
        %tmp3 = add i32 %a, 1
@@ -19,3 +27,20 @@ bb3:         ; preds = %bb2, %entry
         %tmp5 = sub i32 %tmp4, 1
        ret i32 %tmp5
 }
+
+declare i8 @llvm.cttz.i8(i8, i1)
+
+define i8 @test2(i8 %a) {
+; CHECK: @test2
+  br i1 undef, label %bb_true, label %bb_false
+bb_true:
+  %b = tail call i8 @llvm.cttz.i8(i8 %a, i1 false)
+  br label %join
+bb_false:
+  br label %join
+join:
+  %c = phi i8 [%b, %bb_true], [%a, %bb_false]
+; CHECK: select
+  ret i8 %c
+}
+