[llvm] r260730 - [SimplifyCFG] Don't fold conditional branches that contain calls to convergent functions.

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 12 13:01:36 PST 2016


Author: jlebar
Date: Fri Feb 12 15:01:36 2016
New Revision: 260730

URL: http://llvm.org/viewvc/llvm-project?rev=260730&view=rev
Log:
[SimplifyCFG] Don't fold conditional branches that contain calls to convergent functions.

Summary:
Performing this optimization duplicates the call to the convergent
function and adds new control-flow dependencies, which is a no-no.

Reviewers: jingyue

Subscribers: broune, hfinkel, tra, resistor, joker.eph, arsenm, llvm-commits, mzolotukhin

Differential Revision: http://reviews.llvm.org/D17128

Added:
    llvm/trunk/test/Transforms/SimplifyCFG/attr-convergent.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=260730&r1=260729&r2=260730&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Fri Feb 12 15:01:36 2016
@@ -1690,19 +1690,6 @@ static bool SpeculativelyExecuteBB(Branc
   return true;
 }
 
-/// \returns True if this block contains a CallInst with the NoDuplicate
-/// attribute.
-static bool HasNoDuplicateCall(const BasicBlock *BB) {
-  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
-    const CallInst *CI = dyn_cast<CallInst>(I);
-    if (!CI)
-      continue;
-    if (CI->cannotDuplicate())
-      return true;
-  }
-  return false;
-}
-
 /// Return true if we can thread a branch across this block.
 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
@@ -1747,7 +1734,12 @@ static bool FoldCondBranchOnPHI(BranchIn
   // Now we know that this block has multiple preds and two succs.
   if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false;
 
-  if (HasNoDuplicateCall(BB)) return false;
+  // Can't fold blocks that contain noduplicate or convergent calls.
+  if (llvm::any_of(*BB, [](const Instruction &I) {
+        const CallInst *CI = dyn_cast<CallInst>(&I);
+        return CI && (CI->cannotDuplicate() || CI->isConvergent());
+      }))
+    return false;
 
   // Okay, this is a simple enough basic block.  See if any phi values are
   // constants.

Added: llvm/trunk/test/Transforms/SimplifyCFG/attr-convergent.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/attr-convergent.ll?rev=260730&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/attr-convergent.ll (added)
+++ llvm/trunk/test/Transforms/SimplifyCFG/attr-convergent.ll Fri Feb 12 15:01:36 2016
@@ -0,0 +1,28 @@
+; RUN: opt < %s -simplifycfg -S | FileCheck %s
+
+; Checks that the SimplifyCFG pass won't duplicate a call to a function marked
+; convergent.
+;
+; CHECK: call void @barrier
+; CHECK-NOT: call void @barrier
+define void @check(i1 %cond, i32* %out) {
+entry:
+  br i1 %cond, label %if.then, label %if.end
+
+if.then:
+  store i32 5, i32* %out
+  br label %if.end
+
+if.end:
+  %x = phi i1 [ true, %entry ], [ false, %if.then ]
+  call void @barrier()
+  br i1 %x, label %cond.end, label %cond.false
+
+cond.false:
+  br label %cond.end
+
+cond.end:
+  ret void
+}
+
+declare void @barrier() convergent




More information about the llvm-commits mailing list