[llvm] 2672719 - [InstCombine] Don't handle non-canonical index type in icmp of load fold (#151346)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 30 08:52:12 PDT 2025


Author: Nikita Popov
Date: 2025-07-30T17:52:08+02:00
New Revision: 2672719a09cf9fd279e66cd3582b7cc8331c0b31

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

LOG: [InstCombine] Don't handle non-canonical index type in icmp of load fold (#151346)

We should just bail out and wait for it to be canonicalized. The current
implementation could emit a trunc without actually performing the
transform.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index da9b12686a8f1..b268fea85ab07 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -163,6 +163,11 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
     LaterIndices.push_back(IdxVal);
   }
 
+  Value *Idx = GEP->getOperand(2);
+  // If the index type is non-canonical, wait for it to be canonicalized.
+  if (Idx->getType() != DL.getIndexType(GEP->getType()))
+    return nullptr;
+
   enum { Overdefined = -3, Undefined = -2 };
 
   // Variables for our state machines.
@@ -290,17 +295,6 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
 
   // Now that we've scanned the entire array, emit our new comparison(s).  We
   // order the state machines in complexity of the generated code.
-  Value *Idx = GEP->getOperand(2);
-
-  // If the index is larger than the pointer offset size of the target, truncate
-  // the index down like the GEP would do implicitly.  We don't have to do this
-  // for an inbounds GEP because the index can't be out of range.
-  if (!GEP->isInBounds()) {
-    Type *PtrIdxTy = DL.getIndexType(GEP->getType());
-    unsigned OffsetSize = PtrIdxTy->getIntegerBitWidth();
-    if (Idx->getType()->getPrimitiveSizeInBits().getFixedValue() > OffsetSize)
-      Idx = Builder.CreateTrunc(Idx, PtrIdxTy);
-  }
 
   // If inbounds keyword is not present, Idx * ElementSize can overflow.
   // Let's assume that ElementSize is 2 and the wanted value is at offset 0.


        


More information about the llvm-commits mailing list