[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 https://github.com/llvm/llvm-project/issues/54217
This commit is contained in:
Florian Hahn
2022-03-07 12:01:15 +00:00
parent 12ffa9c2aa
commit c60cdb44f7
2 changed files with 18 additions and 14 deletions

View File

@@ -413,6 +413,19 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
continue;
WorkList.emplace_back(DT.getNode(&BB));
// Returns true if we can add a known condition from BB to its successor
// block Succ. Each predecessor of Succ can either be BB or be dominated by
// Succ (e.g. the case when adding a condition from a pre-header to a loop
// header).
auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
assert(isa<BranchInst>(BB.getTerminator()));
return any_of(successors(&BB),
[Succ](const BasicBlock *S) { return S != Succ; }) &&
all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
return Pred == &BB || DT.dominates(Succ, Pred);
});
};
// True as long as long as the current instruction is guaranteed to execute.
bool GuaranteedToExecute = true;
// Scan BB for assume calls.
@@ -431,9 +444,12 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
WorkList.emplace_back(DT.getNode(&BB), cast<ICmpInst>(Cond), false);
} else {
// Otherwise the condition only holds in the successors.
for (BasicBlock *Succ : successors(&BB))
for (BasicBlock *Succ : successors(&BB)) {
if (!CanAdd(Succ))
continue;
WorkList.emplace_back(DT.getNode(Succ), cast<ICmpInst>(Cond),
false);
}
}
}
GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
@@ -443,18 +459,6 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
if (!Br || !Br->isConditional())
continue;
// Returns true if we can add a known condition from BB to its successor
// block Succ. Each predecessor of Succ can either be BB or be dominated by
// Succ (e.g. the case when adding a condition from a pre-header to a loop
// header).
auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
assert(isa<BranchInst>(BB.getTerminator()));
return any_of(successors(&BB),
[Succ](const BasicBlock *S) { return S != Succ; }) &&
all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
return Pred == &BB || DT.dominates(Succ, Pred);
});
};
// If the condition is an OR of 2 compares and the false successor only has
// the current block as predecessor, queue both negated conditions for the
// false successor.

View File

@@ -164,7 +164,7 @@ define i1 @assume_does_not_dominates_successor_with_may_unwind_call_before_assum
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[C_2:%.*]] = icmp eq i16 [[A]], 0
; CHECK-NEXT: ret i1 true
; CHECK-NEXT: ret i1 [[C_2]]
;
entry:
br i1 %i.0, label %exit, label %if.then