[llvm] 475f30d - [ConstantFolding] Preserve inbounds when casting GEP indices

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 9 04:09:11 PST 2023


Author: Nikita Popov
Date: 2023-03-09T13:08:19+01:00
New Revision: 475f30dfc83cda05b2e80edeb7b7b057e9f8ba38

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

LOG: [ConstantFolding] Preserve inbounds when casting GEP indices

This canonicalization just makes the implicit sext/trunc explicit,
and does not affect the inbounds-ness of the GEP.

Added: 
    

Modified: 
    llvm/lib/Analysis/ConstantFolding.cpp
    llvm/test/Other/constant-fold-gep.ll
    llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash-inseltpoison.ll
    llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash.ll
    llvm/test/Transforms/InstSimplify/ConstProp/cast-vector.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index d6ed643765653..76223f353288c 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -822,7 +822,8 @@ Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
 /// If array indices are not pointer-sized integers, explicitly cast them so
 /// that they aren't implicitly casted by the getelementptr.
 Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
-                         Type *ResultTy, std::optional<unsigned> InRangeIndex,
+                         Type *ResultTy, bool InBounds,
+                         std::optional<unsigned> InRangeIndex,
                          const DataLayout &DL, const TargetLibraryInfo *TLI) {
   Type *IntIdxTy = DL.getIndexType(ResultTy);
   Type *IntIdxScalarTy = IntIdxTy->getScalarType();
@@ -851,7 +852,7 @@ Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
     return nullptr;
 
   Constant *C = ConstantExpr::getGetElementPtr(
-      SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex);
+      SrcElemTy, Ops[0], NewIdxs, InBounds, InRangeIndex);
   return ConstantFoldConstant(C, DL, TLI);
 }
 
@@ -886,7 +887,8 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
     return nullptr;
 
   if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy,
-                                   GEP->getInRangeIndex(), DL, TLI))
+                                   GEP->isInBounds(), GEP->getInRangeIndex(),
+                                   DL, TLI))
     return C;
 
   Constant *Ptr = Ops[0];

diff  --git a/llvm/test/Other/constant-fold-gep.ll b/llvm/test/Other/constant-fold-gep.ll
index 4bc9d29553df3..0c1ca129bdb38 100644
--- a/llvm/test/Other/constant-fold-gep.ll
+++ b/llvm/test/Other/constant-fold-gep.ll
@@ -457,7 +457,7 @@ define ptr @
diff erent_addrspace() nounwind noinline {
   %p = getelementptr inbounds i8, ptr addrspacecast (ptr addrspace(12) @p12 to ptr),
                                   i32 2
   ret ptr %p
-; OPT: ret ptr getelementptr (i8, ptr addrspacecast (ptr addrspace(12) @p12 to ptr), i64 2)
+; OPT: ret ptr getelementptr inbounds (i8, ptr addrspacecast (ptr addrspace(12) @p12 to ptr), i64 2)
 }
 
 define ptr @same_addrspace() nounwind noinline {

diff  --git a/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash-inseltpoison.ll b/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash-inseltpoison.ll
index fb2133182503a..a40811aa1dde1 100644
--- a/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash-inseltpoison.ll
+++ b/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash-inseltpoison.ll
@@ -9,7 +9,7 @@ define <4 x ptr> @test(ptr %ptr) {
 ; CHECK-LABEL: @test(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[L3:%.*]] = load i64, ptr [[PTR:%.*]], align 4
-; CHECK-NEXT:    [[I6:%.*]] = insertelement <4 x ptr> getelementptr (i64, ptr null, <4 x i64> <i64 poison, i64 poison, i64 poison, i64 -128>), ptr undef, i64 [[L3]]
+; CHECK-NEXT:    [[I6:%.*]] = insertelement <4 x ptr> getelementptr inbounds (i64, ptr null, <4 x i64> <i64 poison, i64 poison, i64 poison, i64 -128>), ptr undef, i64 [[L3]]
 ; CHECK-NEXT:    ret <4 x ptr> [[I6]]
 ;
 entry:

diff  --git a/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash.ll b/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash.ll
index 276008f6348a3..ddb223fffab5e 100644
--- a/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash.ll
+++ b/llvm/test/Transforms/GVN/constexpr-vector-constainsundef-crash.ll
@@ -8,7 +8,9 @@
 define <4 x ptr> @test(ptr %ptr) {
 ; CHECK-LABEL: @test(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    ret <4 x ptr> getelementptr (i64, ptr null, <4 x i64> <i64 0, i64 0, i64 0, i64 -128>)
+; CHECK-NEXT:    [[L3:%.*]] = load i64, ptr [[PTR:%.*]], align 4
+; CHECK-NEXT:    [[I6:%.*]] = insertelement <4 x ptr> getelementptr inbounds (i64, ptr null, <4 x i64> <i64 0, i64 0, i64 0, i64 -128>), ptr undef, i64 [[L3]]
+; CHECK-NEXT:    ret <4 x ptr> [[I6]]
 ;
 entry:
   %B9 = sdiv i16 -32768, 256

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/cast-vector.ll b/llvm/test/Transforms/InstSimplify/ConstProp/cast-vector.ll
index 83ef0c14592cb..3e4504a166366 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/cast-vector.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/cast-vector.ll
@@ -8,7 +8,7 @@
 define <2 x i16> @test1() {
 ; CHECK-LABEL: @test1(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    ret <2 x i16> ptrtoint (<2 x ptr> getelementptr ([10 x i32], ptr null, <2 x i64> zeroinitializer, <2 x i64> <i64 5, i64 7>) to <2 x i16>)
+; CHECK-NEXT:    ret <2 x i16> ptrtoint (<2 x ptr> getelementptr inbounds ([10 x i32], ptr null, <2 x i64> zeroinitializer, <2 x i64> <i64 5, i64 7>) to <2 x i16>)
 ;
 entry:
   %gep = getelementptr inbounds [10 x i32], ptr null, i16 0, <2 x i16> <i16 5, i16 7>


        


More information about the llvm-commits mailing list