Skip to content

Commit c60cdb4

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
1 parent 12ffa9c commit c60cdb4

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,19 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
413413
continue;
414414
WorkList.emplace_back(DT.getNode(&BB));
415415

416+
// Returns true if we can add a known condition from BB to its successor
417+
// block Succ. Each predecessor of Succ can either be BB or be dominated by
418+
// Succ (e.g. the case when adding a condition from a pre-header to a loop
419+
// header).
420+
auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
421+
assert(isa<BranchInst>(BB.getTerminator()));
422+
return any_of(successors(&BB),
423+
[Succ](const BasicBlock *S) { return S != Succ; }) &&
424+
all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
425+
return Pred == &BB || DT.dominates(Succ, Pred);
426+
});
427+
};
428+
416429
// True as long as long as the current instruction is guaranteed to execute.
417430
bool GuaranteedToExecute = true;
418431
// Scan BB for assume calls.
@@ -431,9 +444,12 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
431444
WorkList.emplace_back(DT.getNode(&BB), cast<ICmpInst>(Cond), false);
432445
} else {
433446
// Otherwise the condition only holds in the successors.
434-
for (BasicBlock *Succ : successors(&BB))
447+
for (BasicBlock *Succ : successors(&BB)) {
448+
if (!CanAdd(Succ))
449+
continue;
435450
WorkList.emplace_back(DT.getNode(Succ), cast<ICmpInst>(Cond),
436451
false);
452+
}
437453
}
438454
}
439455
GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
@@ -443,18 +459,6 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
443459
if (!Br || !Br->isConditional())
444460
continue;
445461

446-
// Returns true if we can add a known condition from BB to its successor
447-
// block Succ. Each predecessor of Succ can either be BB or be dominated by
448-
// Succ (e.g. the case when adding a condition from a pre-header to a loop
449-
// header).
450-
auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
451-
assert(isa<BranchInst>(BB.getTerminator()));
452-
return any_of(successors(&BB),
453-
[Succ](const BasicBlock *S) { return S != Succ; }) &&
454-
all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
455-
return Pred == &BB || DT.dominates(Succ, Pred);
456-
});
457-
};
458462
// If the condition is an OR of 2 compares and the false successor only has
459463
// the current block as predecessor, queue both negated conditions for the
460464
// 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)