Skip to content

Commit

Permalink
[ConstantFold] Fix GEP of GEP fold with opaque pointers
Browse files Browse the repository at this point in the history
This was previously combining indices even though they operate on
different types. For non-opaque pointers, the condition is
automatically satisfied based on the pointer types being equal.
  • Loading branch information
nikic committed Jul 23, 2021
1 parent 923727e commit f623b3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions llvm/lib/IR/ConstantFold.cpp
Expand Up @@ -2347,8 +2347,11 @@ static bool isIndexInRangeOfArrayType(uint64_t NumElements,
// Combine Indices - If the source pointer to this getelementptr instruction
// is a getelementptr instruction, combine the indices of the two
// getelementptr instructions into a single instruction.
static Constant *foldGEPOfGEP(GEPOperator *GEP, bool InBounds,
static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
ArrayRef<Value *> Idxs) {
if (PointeeTy != GEP->getResultElementType())
return nullptr;

Constant *Idx0 = cast<Constant>(Idxs[0]);
if (Idx0->isNullValue()) {
// Handle the simple case of a zero index.
Expand Down Expand Up @@ -2491,7 +2494,7 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,

if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
if (auto *GEP = dyn_cast<GEPOperator>(CE))
if (Constant *C = foldGEPOfGEP(GEP, InBounds, Idxs))
if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
return C;

// Attempt to fold casts to the same type away. For example, folding:
Expand Down
7 changes: 7 additions & 0 deletions llvm/test/Other/force-opaque-ptrs.ll
Expand Up @@ -64,6 +64,13 @@ define void @remangle_intrinsic() {
ret void
}

define i32* @constexpr_gep() {
; CHECK-LABEL: define {{[^@]+}}@constexpr_gep() {
; CHECK-NEXT: ret ptr getelementptr (i32, ptr getelementptr (i8, ptr null, i64 4), i64 1)
;
ret i32* getelementptr(i32, i32* bitcast (i8* getelementptr (i8, i8* null, i64 4) to i32*), i64 1)
}

declare i8* @llvm.stacksave()
declare void @llvm.stackprotector(i8*, i8**)
declare <2 x i64> @llvm.masked.expandload.v2i64(i64*, <2 x i1>, <2 x i64>)

0 comments on commit f623b3a

Please sign in to comment.