[llvm] f09d32d - [ConstantFold] Don't drop zero index gep with inrange attribute

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 6 03:06:50 PST 2023


Author: Nikita Popov
Date: 2023-01-06T12:06:42+01:00
New Revision: f09d32d121cdb77603b677c9666cfffb2186f86c

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

LOG: [ConstantFold] Don't drop zero index gep with inrange attribute

This may cause GlobalSplit to fail if opaque pointers are used.

inrange really needs a new representation, but for now restore the
pre-opaque pointers status.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Assembler/getelementptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 2566617012d91..f84fe79b21be2 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -2053,6 +2053,10 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
     if (!C->getType()->getScalarType()->isOpaquePointerTy() && Idxs.size() != 1)
       return false;
 
+    // Avoid losing inrange information.
+    if (InRangeIndex)
+      return false;
+
     return all_of(Idxs, [](Value *Idx) {
       Constant *IdxC = cast<Constant>(Idx);
       return IdxC->isNullValue() || isa<UndefValue>(IdxC);

diff  --git a/llvm/test/Assembler/getelementptr.ll b/llvm/test/Assembler/getelementptr.ll
index 99b738b1413c5..50695a65abb33 100644
--- a/llvm/test/Assembler/getelementptr.ll
+++ b/llvm/test/Assembler/getelementptr.ll
@@ -33,13 +33,13 @@
 ; CHECK: @nestedarray.2 = alias ptr, getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, i32 0, inrange i32 0, i32 4)
 @nestedarray.2 = alias ptr, getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, i32 0, inrange i32 0, i32 4)
 
-; CHECK: @nestedarray.3 = alias ptr, ptr @nestedarray
+; CHECK: @nestedarray.3 = alias ptr, getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, i32 0, inrange i32 0)
 @nestedarray.3 = alias ptr, getelementptr inbounds ([4 x ptr], ptr getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, i32 0, inrange i32 0), i32 0, i32 0)
 
-; CHECK: @nestedarray.4 = alias ptr, getelementptr inbounds ([4 x ptr], ptr @nestedarray, i32 1, i32 0)
+; CHECK: @nestedarray.4 = alias ptr, getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, i32 0, i32 1, i32 0)
 @nestedarray.4 = alias ptr, getelementptr inbounds ([4 x ptr], ptr getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, i32 0, inrange i32 0), i32 1, i32 0)
 
-; CHECK: @nestedarray.5 = alias ptr, getelementptr inbounds ([4 x ptr], ptr @nestedarray, i32 1, i32 0)
+; CHECK: @nestedarray.5 = alias ptr, getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, inrange i32 0, i32 1, i32 0)
 @nestedarray.5 = alias ptr, getelementptr inbounds ([4 x ptr], ptr getelementptr inbounds ([2 x [4 x ptr]], ptr @nestedarray, inrange i32 0, i32 0), i32 1, i32 0)
 
 ; See if i92 indices work too.


        


More information about the llvm-commits mailing list