[ConstantFold] Don't drop zero index gep with inrange attribute
authorNikita Popov <npopov@redhat.com>
Fri, 6 Jan 2023 11:02:52 +0000 (12:02 +0100)
committerNikita Popov <npopov@redhat.com>
Fri, 6 Jan 2023 11:06:42 +0000 (12:06 +0100)
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.

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

index 2566617..f84fe79 100644 (file)
@@ -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);
index 99b738b..50695a6 100644 (file)
 ; 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.