[llvm] [WIP][IndVarSimplify] Fix Masking Issue by Adding nsw/nuw Flags to Trunc Instruction (PR #150179)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 24 01:53:22 PDT 2025


================
@@ -1049,9 +1049,28 @@ linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
     if (Extended) {
       bool Discard;
       L->makeLoopInvariant(ExitCnt, Discard);
-    } else
+    } else{
       CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->getType(),
                                       "lftr.wideiv");
+
+      // Set the correct wrap flag to avoid the masking issue.
+      Instruction *TruncInst = dyn_cast<Instruction>(CmpIndVar);
----------------
lukel97 wrote:

It's probably easier to add the flags via IRBuilder

```suggestion
CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->getType(),
"lftr.wideiv", TruncAR->hasNoUnsignedWrap(), TruncAR->hasSignedWrap());
```

https://github.com/llvm/llvm-project/pull/150179


More information about the llvm-commits mailing list