SeparateConstOffsetFromGEP: Fix creating pointless bitcasts

This was directly creating new BitCastInsts, so under opaque pointers,
would end up producing bitcast from ptr to ptr.
This commit is contained in:
Matt Arsenault 2022-11-26 14:53:36 -05:00
parent e8d4550813
commit d1c0092163
2 changed files with 11 additions and 13 deletions

View File

@ -1122,18 +1122,17 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
// sizeof(int64).
//
// Emit an uglygep in this case.
Type *I8PtrTy = Type::getInt8PtrTy(GEP->getContext(),
GEP->getPointerAddressSpace());
NewGEP = new BitCastInst(NewGEP, I8PtrTy, "", GEP);
NewGEP = GetElementPtrInst::Create(
Type::getInt8Ty(GEP->getContext()), NewGEP,
ConstantInt::get(IntPtrTy, AccumulativeByteOffset, true), "uglygep",
GEP);
IRBuilder<> Builder(GEP);
Type *I8PtrTy =
Builder.getInt8Ty()->getPointerTo(GEP->getPointerAddressSpace());
NewGEP = cast<Instruction>(Builder.CreateGEP(
Builder.getInt8Ty(), Builder.CreateBitCast(NewGEP, I8PtrTy),
{ConstantInt::get(IntPtrTy, AccumulativeByteOffset, true)}, "uglygep",
GEPWasInBounds));
NewGEP->copyMetadata(*GEP);
// Inherit the inbounds attribute of the original GEP.
cast<GetElementPtrInst>(NewGEP)->setIsInBounds(GEPWasInBounds);
if (GEP->getType() != I8PtrTy)
NewGEP = new BitCastInst(NewGEP, GEP->getType(), GEP->getName(), GEP);
NewGEP = cast<Instruction>(Builder.CreateBitCast(NewGEP, GEP->getType()));
}
GEP->replaceAllUsesWith(NewGEP);

View File

@ -15,8 +15,7 @@ define ptr addrspace(3) @packed_struct(i32 %i, i32 %j) {
; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[I:%.*]] to i64
; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[J:%.*]] to i64
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr [1024 x %struct.Packed], ptr addrspace(3) @packed_struct_array, i64 0, i64 [[TMP0]], i32 1, i64 [[TMP1]]
; CHECK-NEXT: [[TMP3:%.*]] = bitcast ptr addrspace(3) [[TMP2]] to ptr addrspace(3)
; CHECK-NEXT: [[UGLYGEP:%.*]] = getelementptr inbounds i8, ptr addrspace(3) [[TMP3]], i64 100
; CHECK-NEXT: [[UGLYGEP:%.*]] = getelementptr inbounds i8, ptr addrspace(3) [[TMP2]], i64 100
; CHECK-NEXT: ret ptr addrspace(3) [[UGLYGEP]]
;
entry: