Skip to content

Commit a8e8d6d

Browse files
committed
[ConstraintElimination] Only add cond from assume to succs if valid.
Add missing CanAdd check before adding a condition from an assume to the successor blocks. When adding information from assume to successor blocks we need to perform the same CanAdd as we do for adding a condition from a branch. Fixes llvm#54217 (cherry-picked from c60cdb4)
1 parent 25cb9bc commit a8e8d6d

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
269269
continue;
270270
WorkList.emplace_back(DT.getNode(&BB));
271271

272+
// Returns true if we can add a known condition from BB to its successor
273+
// block Succ. Each predecessor of Succ can either be BB or be dominated by
274+
// Succ (e.g. the case when adding a condition from a pre-header to a loop
275+
// header).
276+
auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
277+
assert(isa<BranchInst>(BB.getTerminator()));
278+
return any_of(successors(&BB),
279+
[Succ](const BasicBlock *S) { return S != Succ; }) &&
280+
all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
281+
return Pred == &BB || DT.dominates(Succ, Pred);
282+
});
283+
};
284+
272285
// True as long as long as the current instruction is guaranteed to execute.
273286
bool GuaranteedToExecute = true;
274287
// Scan BB for assume calls.
@@ -287,8 +300,12 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
287300
WorkList.emplace_back(DT.getNode(&BB), cast<CmpInst>(Cond), false);
288301
} else {
289302
// Otherwise the condition only holds in the successors.
290-
for (BasicBlock *Succ : successors(&BB))
291-
WorkList.emplace_back(DT.getNode(Succ), cast<CmpInst>(Cond), false);
303+
for (BasicBlock *Succ : successors(&BB)) {
304+
if (!CanAdd(Succ))
305+
continue;
306+
WorkList.emplace_back(DT.getNode(Succ), cast<CmpInst>(Cond),
307+
false);
308+
}
292309
}
293310
}
294311
GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
@@ -298,15 +315,6 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
298315
if (!Br || !Br->isConditional())
299316
continue;
300317

301-
// Returns true if we can add a known condition from BB to its successor
302-
// block Succ. Each predecessor of Succ can either be BB or be dominated by
303-
// Succ (e.g. the case when adding a condition from a pre-header to a loop
304-
// header).
305-
auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
306-
return all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
307-
return Pred == &BB || DT.dominates(Succ, Pred);
308-
});
309-
};
310318
// If the condition is an OR of 2 compares and the false successor only has
311319
// the current block as predecessor, queue both negated conditions for the
312320
// false successor.

llvm/test/Transforms/ConstraintElimination/assumes.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ define i1 @assume_does_not_dominates_successor_with_may_unwind_call_before_assum
164164
; CHECK-NEXT: br label [[EXIT]]
165165
; CHECK: exit:
166166
; CHECK-NEXT: [[C_2:%.*]] = icmp eq i16 [[A]], 0
167-
; CHECK-NEXT: ret i1 true
167+
; CHECK-NEXT: ret i1 [[C_2]]
168168
;
169169
entry:
170170
br i1 %i.0, label %exit, label %if.then

0 commit comments

Comments
 (0)