[llvm] [IndVarSimplify] Keep additional nuw and nsw flags during LinearFunctionTestReplace (PR #177433)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 14:00:40 PDT 2026
================
@@ -1110,10 +1110,46 @@ linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
// dead, it could not be poison on the first iteration in the first place.)
if (auto *BO = dyn_cast<BinaryOperator>(IncVar)) {
const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IncVar));
+
+ // However, if we successfully computed an ExitCount, we can infer nowrap
+ // properties from the original loop exit condition. If the original
+ // comparison was signed and SCEV computed an exit count, then NSW is safe.
+ // Similarly, if it was unsigned, NUW is safe. This is because SCEV's exit
+ // count analysis proves the loop exits before overflow occurs for that
+ // signedness.
+ bool KeepNSW = false;
+ bool KeepNUW = false;
+
+ CondBrInst *BI = cast<CondBrInst>(ExitingBB->getTerminator());
+ if (auto *OrigCmp = dyn_cast<ICmpInst>(BI->getCondition())) {
+ // Check if the chosen IV is actually used in the original comparison.
+ bool IVUsedInOriginalCmp = (OrigCmp->getOperand(0) == IndVar ||
+ OrigCmp->getOperand(0) == IncVar ||
+ OrigCmp->getOperand(1) == IndVar ||
+ OrigCmp->getOperand(1) == IncVar);
+
+ if (IVUsedInOriginalCmp) {
+ ICmpInst::Predicate Pred = OrigCmp->getPredicate();
+
+ // If the original comparison was signed, SCEV proved NSW is safe.
+ KeepNSW = ICmpInst::isSigned(Pred);
----------------
yasmincs wrote:
I'm not sure I understand what you want me to change here. In the case of samesign ult, the code will not preserve nsw anyway because it only preserves signed wrap flags when the comparison is a signed comparison.
https://github.com/llvm/llvm-project/pull/177433
More information about the llvm-commits
mailing list