[llvm] e565a4f - [IR] Extract helper for GEPNoWrapFlags intersection (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 07:58:36 PDT 2024


Author: Nikita Popov
Date: 2024-10-01T16:58:23+02:00
New Revision: e565a4fa0b09456265e28b017054b20ff4315c58

URL: https://github.com/llvm/llvm-project/commit/e565a4fa0b09456265e28b017054b20ff4315c58
DIFF: https://github.com/llvm/llvm-project/commit/e565a4fa0b09456265e28b017054b20ff4315c58.diff

LOG: [IR] Extract helper for GEPNoWrapFlags intersection (NFC)

When combining two geps into one by adding the offsets, we have
to take some care when intersecting the flags, because nusw flags
cannot be straightforwardly preserved.

Add a helper for this on GEPNoWrapFlags so we won't have to repeat
this logic in various places.

Added: 
    

Modified: 
    llvm/include/llvm/IR/GEPNoWrapFlags.h
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/GEPNoWrapFlags.h b/llvm/include/llvm/IR/GEPNoWrapFlags.h
index 55a25c69193f2c..4e6ab0d88bfcf8 100644
--- a/llvm/include/llvm/IR/GEPNoWrapFlags.h
+++ b/llvm/include/llvm/IR/GEPNoWrapFlags.h
@@ -74,6 +74,16 @@ class GEPNoWrapFlags {
     return GEPNoWrapFlags(Flags & ~NUWFlag);
   }
 
+  /// Given (gep (gep p, x), y), determine the nowrap flags for (gep p, x+y).
+  GEPNoWrapFlags intersectForOffsetAdd(GEPNoWrapFlags Other) const {
+    GEPNoWrapFlags Res = *this & Other;
+    // Without inbounds, we could only preserve nusw if we know that x + y does
+    // not wrap.
+    if (!Res.isInBounds() && Res.hasNoUnsignedSignedWrap())
+      Res = Res.withoutNoUnsignedSignedWrap();
+    return Res;
+  }
+
   bool operator==(GEPNoWrapFlags Other) const { return Flags == Other.Flags; }
   bool operator!=(GEPNoWrapFlags Other) const { return !(*this == Other); }
 

diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 5740285675eba8..916a14e13ff2af 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2349,12 +2349,7 @@ Instruction *InstCombinerImpl::narrowMathIfNoOverflow(BinaryOperator &BO) {
 /// transform.
 static GEPNoWrapFlags getMergedGEPNoWrapFlags(GEPOperator &GEP1,
                                               GEPOperator &GEP2) {
-  GEPNoWrapFlags NW = GEP1.getNoWrapFlags() & GEP2.getNoWrapFlags();
-  // Without inbounds, we could only preserve nusw if we know that x + y does
-  // not wrap.
-  if (!NW.isInBounds())
-    NW = NW.withoutNoUnsignedSignedWrap();
-  return NW;
+  return GEP1.getNoWrapFlags().intersectForOffsetAdd(GEP2.getNoWrapFlags());
 }
 
 /// Thread a GEP operation with constant indices through the constant true/false


        


More information about the llvm-commits mailing list