[llvm] 896f344 - [ConstantFolding] Use getGetElementPtr() overload accepting DataLayout (#226104)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 03:51:11 PDT 2026


Author: Nikita Popov
Date: 2026-09-24T10:51:03Z
New Revision: 896f344605665ad74f34521ecd11ef84c7617987

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

LOG: [ConstantFolding] Use getGetElementPtr() overload accepting DataLayout (#226104)

To directly create the constant GEP in canonical form. The practical
effect is that we a) canonicalize vector GEPs and b) decline to
create a GEP which can't be represented in ptradd form (in the
constant_fold_custom_dl test due to a non-canonical index type).

Added: 
    

Modified: 
    llvm/lib/Analysis/ConstantFolding.cpp
    llvm/test/Transforms/InstCombine/gep-custom-dl.ll
    llvm/test/Transforms/InstCombine/gep-vector.ll
    llvm/test/Transforms/InstCombine/vectorgep-crash.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 18921240799bd..f8dc90a95abfe 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1004,8 +1004,10 @@ Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
   if (!Any)
     return nullptr;
 
-  Constant *C =
-      ConstantExpr::getGetElementPtr(SrcElemTy, Ops[0], NewIdxs, NW, InRange);
+  Constant *C = ConstantExpr::getGetElementPtr(DL, SrcElemTy, Ops[0], NewIdxs,
+                                               NW, InRange);
+  if (!C)
+    return nullptr;
   return ConstantFoldConstant(C, DL, TLI);
 }
 
@@ -1167,7 +1169,7 @@ Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
     if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
       return C;
 
-    return ConstantExpr::getGetElementPtr(SrcElemTy, Ops[0], Ops.slice(1),
+    return ConstantExpr::getGetElementPtr(DL, SrcElemTy, Ops[0], Ops.slice(1),
                                           GEP->getNoWrapFlags(),
                                           GEP->getInRange());
   }

diff  --git a/llvm/test/Transforms/InstCombine/gep-custom-dl.ll b/llvm/test/Transforms/InstCombine/gep-custom-dl.ll
index 5027af6941f3b..fe374c2efd163 100644
--- a/llvm/test/Transforms/InstCombine/gep-custom-dl.ll
+++ b/llvm/test/Transforms/InstCombine/gep-custom-dl.ll
@@ -168,7 +168,11 @@ define i32 @test10() {
 define i16 @constant_fold_custom_dl() {
 ; CHECK-LABEL: @constant_fold_custom_dl(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    ret i16 ptrtoint (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) getelementptr inbounds nuw (i8, ptr addrspace(1) @X_as1, i32 1000), i16 sub (i16 0, i16 ptrtoint (ptr addrspace(1) @X_as1 to i16))) to i16)
+; CHECK-NEXT:    [[TMP0:%.*]] = sext i16 sub (i16 0, i16 ptrtoint (ptr addrspace(1) @X_as1 to i16)) to i32
+; CHECK-NEXT:    [[D:%.*]] = getelementptr i8, ptr addrspace(1) getelementptr inbounds nuw (i8, ptr addrspace(1) @X_as1, i32 1000), i32 [[TMP0]]
+; CHECK-NEXT:    [[TMP1:%.*]] = ptrtoint ptr addrspace(1) [[D]] to i40
+; CHECK-NEXT:    [[E:%.*]] = trunc i40 [[TMP1]] to i16
+; CHECK-NEXT:    ret i16 [[E]]
 ;
 
 entry:

diff  --git a/llvm/test/Transforms/InstCombine/gep-vector.ll b/llvm/test/Transforms/InstCombine/gep-vector.ll
index 50c5d734fc9ff..352bc8254beb3 100644
--- a/llvm/test/Transforms/InstCombine/gep-vector.ll
+++ b/llvm/test/Transforms/InstCombine/gep-vector.ll
@@ -5,7 +5,7 @@
 
 define <2 x ptr> @vectorindex1() {
 ; CHECK-LABEL: @vectorindex1(
-; CHECK-NEXT:    ret <2 x ptr> getelementptr inbounds ([64 x [8192 x i8]], ptr @block, <2 x i64> zeroinitializer, <2 x i64> <i64 0, i64 1>, <2 x i64> splat (i64 8192))
+; CHECK-NEXT:    ret <2 x ptr> getelementptr inbounds (i8, ptr @block, <2 x i64> <i64 8192, i64 16384>)
 ;
   %1 = getelementptr inbounds [64 x [8192 x i8]], ptr @block, i64 0, <2 x i64> <i64 0, i64 1>, i64 8192
   ret <2 x ptr> %1
@@ -13,7 +13,7 @@ define <2 x ptr> @vectorindex1() {
 
 define <2 x ptr> @vectorindex2() {
 ; CHECK-LABEL: @vectorindex2(
-; CHECK-NEXT:    ret <2 x ptr> getelementptr inbounds ([64 x [8192 x i8]], ptr @block, <2 x i64> zeroinitializer, <2 x i64> splat (i64 1), <2 x i64> <i64 8191, i64 8193>)
+; CHECK-NEXT:    ret <2 x ptr> getelementptr inbounds (i8, ptr @block, <2 x i64> <i64 16383, i64 16385>)
 ;
   %1 = getelementptr inbounds [64 x [8192 x i8]], ptr @block, i64 0, i64 1, <2 x i64> <i64 8191, i64 8193>
   ret <2 x ptr> %1
@@ -21,7 +21,7 @@ define <2 x ptr> @vectorindex2() {
 
 define <2 x ptr> @vectorindex3() {
 ; CHECK-LABEL: @vectorindex3(
-; CHECK-NEXT:    ret <2 x ptr> getelementptr inbounds ([64 x [8192 x i8]], ptr @block, <2 x i64> zeroinitializer, <2 x i64> <i64 0, i64 1>, <2 x i64> <i64 8191, i64 8193>)
+; CHECK-NEXT:    ret <2 x ptr> getelementptr inbounds (i8, ptr @block, <2 x i64> <i64 8191, i64 16385>)
 ;
   %1 = getelementptr inbounds [64 x [8192 x i8]], ptr @block, i64 0, <2 x i64> <i64 0, i64 1>, <2 x i64> <i64 8191, i64 8193>
   ret <2 x ptr> %1

diff  --git a/llvm/test/Transforms/InstCombine/vectorgep-crash.ll b/llvm/test/Transforms/InstCombine/vectorgep-crash.ll
index 46bfeba3020ad..129a38ef34431 100644
--- a/llvm/test/Transforms/InstCombine/vectorgep-crash.ll
+++ b/llvm/test/Transforms/InstCombine/vectorgep-crash.ll
@@ -32,7 +32,7 @@ top:
 define <16 x ptr> @test() {
 ; CHECK-LABEL: define <16 x ptr> @test() {
 ; CHECK-NEXT:  [[VECTOR_BODY:.*:]]
-; CHECK-NEXT:    ret <16 x ptr> getelementptr ([65 x [[STRUCT_A:%.*]]], ptr @G, <16 x i64> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, i32 0)
+; CHECK-NEXT:    ret <16 x ptr> getelementptr (i8, ptr @G, <16 x i64> <i64 16, i64 32, i64 48, i64 64, i64 80, i64 96, i64 112, i64 128, i64 144, i64 160, i64 176, i64 192, i64 208, i64 224, i64 240, i64 256>)
 ;
 vector.body:
   %VectorGep = getelementptr [65 x %struct.A], ptr @G, <16 x i64> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, <16 x i32> zeroinitializer
@@ -42,7 +42,7 @@ vector.body:
 define <16 x ptr> @test2() {
 ; CHECK-LABEL: define <16 x ptr> @test2() {
 ; CHECK-NEXT:  [[VECTOR_BODY:.*:]]
-; CHECK-NEXT:    ret <16 x ptr> getelementptr ([65 x [[STRUCT_A:%.*]]], ptr @G, <16 x i64> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, i32 0)
+; CHECK-NEXT:    ret <16 x ptr> getelementptr (i8, ptr @G, <16 x i64> <i64 16, i64 32, i64 48, i64 64, i64 80, i64 96, i64 112, i64 128, i64 144, i64 160, i64 176, i64 192, i64 208, i64 224, i64 240, i64 256>)
 ;
 vector.body:
   %VectorGep = getelementptr [65 x %struct.A], ptr @G, <16 x i32> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, <16 x i32> zeroinitializer


        


More information about the llvm-commits mailing list