@@ -6569,6 +6569,31 @@ SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) {
6569
6569
return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap;
6570
6570
}
6571
6571
6572
+ const Instruction *ScalarEvolution::getDefinedScopeRoot(const SCEV *S) {
6573
+ if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S))
6574
+ return &*AddRec->getLoop()->getHeader()->begin();
6575
+ // TODO: add SCEVConstant and SCEVUnknown caxes here
6576
+ return nullptr;
6577
+ }
6578
+
6579
+ static bool
6580
+ isGuaranteedToTransferExecutionToSuccessor(BasicBlock::const_iterator Begin,
6581
+ BasicBlock::const_iterator End) {
6582
+ return llvm::all_of( make_range(Begin, End), [](const Instruction &I) {
6583
+ return isGuaranteedToTransferExecutionToSuccessor(&I);
6584
+ });
6585
+ }
6586
+
6587
+ bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A,
6588
+ const Instruction *B) {
6589
+ if (A->getParent() == B->getParent() &&
6590
+ ::isGuaranteedToTransferExecutionToSuccessor(A->getIterator(),
6591
+ B->getIterator()))
6592
+ return true;
6593
+ return false;
6594
+ }
6595
+
6596
+
6572
6597
bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
6573
6598
// Here we check that I is in the header of the innermost loop containing I,
6574
6599
// since we only deal with instructions in the loop header. The actual loop we
@@ -6600,11 +6625,9 @@ bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
6600
6625
// TODO: We can do better here in some cases.
6601
6626
if (!isSCEVable(Op->getType()))
6602
6627
return false;
6603
- const SCEV *OpS = getSCEV(Op);
6604
- if (auto *AddRecS = dyn_cast<SCEVAddRecExpr>(OpS)) {
6605
- if (isGuaranteedToExecuteForEveryIteration(I, AddRecS->getLoop()))
6628
+ if (auto *DefI = getDefinedScopeRoot(getSCEV(Op)))
6629
+ if (isGuaranteedToTransferExecutionTo(DefI, I))
6606
6630
return true;
6607
- }
6608
6631
}
6609
6632
return false;
6610
6633
}
0 commit comments