[llvm] [SeparateConstOffsetFromGEP] Preserve inbounds flag based on ValueTracking and NUW (PR #130617)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 10 02:23:49 PDT 2025


================
@@ -676,6 +685,30 @@ Value *ConstantOffsetExtractor::applyExts(Value *V) {
   return Current;
 }
 
+bool ConstantOffsetExtractor::checkRebuildingPreservesNUW() const {
+  auto AllowsPreservingNUW = [](User *U) {
+    if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
+      auto Opcode = BO->getOpcode();
+      if (Opcode == BinaryOperator::Or) {
+        // Ors are only considered here if they are disjoint. The addition that
+        // they represent in this case is NUW.
+        assert(cast<PossiblyDisjointInst>(BO)->isDisjoint());
+        return true;
+      }
+      return Opcode == BinaryOperator::Add && BO->hasNoUnsignedWrap();
+    }
+    // UserChain can only contain ConstantInt, CastInst, or BinaryOperator.
+    // Among the possible CastInsts, only trunc without nuw is a problem: If it
+    // is distributed through an add nuw, wrapping may occur:
+    // "add nuw trunc(a), trunc(b)" is more poisonous than "trunc(add nuw a, b)"
+    if (TruncInst *TI = dyn_cast<TruncInst>(U))
+      return TI->hasNoUnsignedWrap();
+    return true;
+  };
+
+  return all_of(UserChain, AllowsPreservingNUW);
+}
----------------
arsenm wrote:

I'd rework this to make the lambda be the function implemented here, and the use site does the all_of 

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


More information about the llvm-commits mailing list