Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -1690,19 +1690,6 @@ 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(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(BB->getTerminator()); @@ -1747,7 +1734,12 @@ // 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(&I); + return CI && (CI->cannotDuplicate() || CI->isConvergent()); + })) + return false; // Okay, this is a simple enough basic block. See if any phi values are // constants. Index: test/Transforms/SimplifyCFG/attr-convergent.ll =================================================================== --- /dev/null +++ test/Transforms/SimplifyCFG/attr-convergent.ll @@ -0,0 +1,33 @@ +; 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(i32 %cond, i32* %out) { +entry: + %out1 = getelementptr i32, i32* %out, i32 1 + %out2 = getelementptr i32, i32* %out, i32 2 + %cmp = icmp eq i32 %cond, 0 + br i1 %cmp, label %if.then, label %if.end + +if.then: + store i32 5, i32* %out + br label %if.end + +if.end: + call void @barrier() + br i1 %cmp, label %cond.end, label %cond.false + +cond.false: + store i32 5, i32* %out1 + br label %cond.end + +cond.end: + %value = phi i32 [ 1, %cond.false ], [ 0, %if.end ] + store i32 %value, i32* %out2 + ret void +} + +declare void @barrier() convergent