Skip to content

Commit 6c2fbc3

Browse files
authored
[IRBuilder] Add CreatePtrAdd() method (NFC) (#77582)
This abstracts over the common pattern of creating a gep with i8 element type.
1 parent ae5d639 commit 6c2fbc3

22 files changed

+77
-90
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,16 @@ class IRBuilderBase {
19741974
return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
19751975
}
19761976

1977+
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
1978+
bool IsInBounds = false) {
1979+
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, IsInBounds);
1980+
}
1981+
1982+
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
1983+
const Twine &Name = "") {
1984+
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, /*IsInBounds*/ true);
1985+
}
1986+
19771987
/// Same as CreateGlobalString, but return a pointer with "i8*" type
19781988
/// instead of a pointer to array of i8.
19791989
///

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5553,7 +5553,6 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
55535553
} else {
55545554
Type *I8PtrTy =
55555555
Builder.getPtrTy(Addr->getType()->getPointerAddressSpace());
5556-
Type *I8Ty = Builder.getInt8Ty();
55575556

55585557
// Start with the base register. Do this first so that subsequent address
55595558
// matching finds it last, which will prevent it from trying to match it
@@ -5597,8 +5596,8 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
55975596
// SDAG consecutive load/store merging.
55985597
if (ResultPtr->getType() != I8PtrTy)
55995598
ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
5600-
ResultPtr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex,
5601-
"sunkaddr", AddrMode.InBounds);
5599+
ResultPtr = Builder.CreatePtrAdd(ResultPtr, ResultIndex, "sunkaddr",
5600+
AddrMode.InBounds);
56025601
}
56035602

56045603
ResultIndex = V;
@@ -5609,8 +5608,8 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
56095608
} else {
56105609
if (ResultPtr->getType() != I8PtrTy)
56115610
ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
5612-
SunkAddr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr",
5613-
AddrMode.InBounds);
5611+
SunkAddr = Builder.CreatePtrAdd(ResultPtr, ResultIndex, "sunkaddr",
5612+
AddrMode.InBounds);
56145613
}
56155614

56165615
if (SunkAddr->getType() != Addr->getType()) {
@@ -6169,7 +6168,6 @@ bool CodeGenPrepare::splitLargeGEPOffsets() {
61696168
Type *PtrIdxTy = DL->getIndexType(GEP->getType());
61706169
Type *I8PtrTy =
61716170
PointerType::get(Ctx, GEP->getType()->getPointerAddressSpace());
6172-
Type *I8Ty = Type::getInt8Ty(Ctx);
61736171

61746172
BasicBlock::iterator NewBaseInsertPt;
61756173
BasicBlock *NewBaseInsertBB;
@@ -6198,7 +6196,7 @@ bool CodeGenPrepare::splitLargeGEPOffsets() {
61986196
if (NewBaseGEP->getType() != I8PtrTy)
61996197
NewBaseGEP = NewBaseBuilder.CreatePointerCast(NewBaseGEP, I8PtrTy);
62006198
NewBaseGEP =
6201-
NewBaseBuilder.CreateGEP(I8Ty, NewBaseGEP, BaseIndex, "splitgep");
6199+
NewBaseBuilder.CreatePtrAdd(NewBaseGEP, BaseIndex, "splitgep");
62026200
NewGEPBases.insert(NewBaseGEP);
62036201
return;
62046202
};
@@ -6235,9 +6233,7 @@ bool CodeGenPrepare::splitLargeGEPOffsets() {
62356233
}
62366234

62376235
// Generate a new GEP to replace the current one.
6238-
LLVMContext &Ctx = GEP->getContext();
62396236
Type *PtrIdxTy = DL->getIndexType(GEP->getType());
6240-
Type *I8Ty = Type::getInt8Ty(Ctx);
62416237

62426238
if (!NewBaseGEP) {
62436239
// Create a new base if we don't have one yet. Find the insertion
@@ -6250,7 +6246,7 @@ bool CodeGenPrepare::splitLargeGEPOffsets() {
62506246
if (Offset != BaseOffset) {
62516247
// Calculate the new offset for the new GEP.
62526248
Value *Index = ConstantInt::get(PtrIdxTy, Offset - BaseOffset);
6253-
NewGEP = Builder.CreateGEP(I8Ty, NewBaseGEP, Index);
6249+
NewGEP = Builder.CreatePtrAdd(NewBaseGEP, Index);
62546250
}
62556251
replaceAllUsesWith(GEP, NewGEP, FreshBBs, IsHugeFunc);
62566252
LargeOffsetGEPID.erase(GEP);

llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ static bool lowerLoadRelative(Function &F) {
7272

7373
bool Changed = false;
7474
Type *Int32Ty = Type::getInt32Ty(F.getContext());
75-
Type *Int8Ty = Type::getInt8Ty(F.getContext());
7675

7776
for (Use &U : llvm::make_early_inc_range(F.uses())) {
7877
auto CI = dyn_cast<CallInst>(U.getUser());
@@ -81,10 +80,10 @@ static bool lowerLoadRelative(Function &F) {
8180

8281
IRBuilder<> B(CI);
8382
Value *OffsetPtr =
84-
B.CreateGEP(Int8Ty, CI->getArgOperand(0), CI->getArgOperand(1));
83+
B.CreatePtrAdd(CI->getArgOperand(0), CI->getArgOperand(1));
8584
Value *OffsetI32 = B.CreateAlignedLoad(Int32Ty, OffsetPtr, Align(4));
8685

87-
Value *ResultPtr = B.CreateGEP(Int8Ty, CI->getArgOperand(0), OffsetI32);
86+
Value *ResultPtr = B.CreatePtrAdd(CI->getArgOperand(0), OffsetI32);
8887

8988
CI->replaceAllUsesWith(ResultPtr);
9089
CI->eraseFromParent();

llvm/lib/CodeGen/SafeStack.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class SafeStack {
119119
Type *StackPtrTy;
120120
Type *IntPtrTy;
121121
Type *Int32Ty;
122-
Type *Int8Ty;
123122

124123
Value *UnsafeStackPtr = nullptr;
125124

@@ -195,8 +194,7 @@ class SafeStack {
195194
: F(F), TL(TL), DL(DL), DTU(DTU), SE(SE),
196195
StackPtrTy(PointerType::getUnqual(F.getContext())),
197196
IntPtrTy(DL.getIntPtrType(F.getContext())),
198-
Int32Ty(Type::getInt32Ty(F.getContext())),
199-
Int8Ty(Type::getInt8Ty(F.getContext())) {}
197+
Int32Ty(Type::getInt32Ty(F.getContext())) {}
200198

201199
// Run the transformation on the associated function.
202200
// Returns whether the function was changed.
@@ -562,8 +560,8 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
562560

563561
if (StackGuardSlot) {
564562
unsigned Offset = SSL.getObjectOffset(StackGuardSlot);
565-
Value *Off = IRB.CreateGEP(Int8Ty, BasePointer, // BasePointer is i8*
566-
ConstantInt::get(Int32Ty, -Offset));
563+
Value *Off =
564+
IRB.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -Offset));
567565
Value *NewAI =
568566
IRB.CreateBitCast(Off, StackGuardSlot->getType(), "StackGuardSlot");
569567

@@ -581,10 +579,10 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
581579
if (Size == 0)
582580
Size = 1; // Don't create zero-sized stack objects.
583581

584-
Value *Off = IRB.CreateGEP(Int8Ty, BasePointer, // BasePointer is i8*
585-
ConstantInt::get(Int32Ty, -Offset));
582+
Value *Off =
583+
IRB.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -Offset));
586584
Value *NewArg = IRB.CreateBitCast(Off, Arg->getType(),
587-
Arg->getName() + ".unsafe-byval");
585+
Arg->getName() + ".unsafe-byval");
588586

589587
// Replace alloc with the new location.
590588
replaceDbgDeclare(Arg, BasePointer, DIB, DIExpression::ApplyOffset,
@@ -616,8 +614,8 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
616614
InsertBefore = User;
617615

618616
IRBuilder<> IRBUser(InsertBefore);
619-
Value *Off = IRBUser.CreateGEP(Int8Ty, BasePointer, // BasePointer is i8*
620-
ConstantInt::get(Int32Ty, -Offset));
617+
Value *Off =
618+
IRBUser.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -Offset));
621619
Value *Replacement = IRBUser.CreateBitCast(Off, AI->getType(), Name);
622620

623621
if (auto *PHI = dyn_cast<PHINode>(User))
@@ -647,8 +645,8 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
647645
IRB.SetInsertPoint(BasePointer->getNextNode());
648646

649647
Value *StaticTop =
650-
IRB.CreateGEP(Int8Ty, BasePointer, ConstantInt::get(Int32Ty, -FrameSize),
651-
"unsafe_stack_static_top");
648+
IRB.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -FrameSize),
649+
"unsafe_stack_static_top");
652650
IRB.CreateStore(StaticTop, UnsafeStackPtr);
653651
return StaticTop;
654652
}

llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,7 @@ auto AlignVectors::createAdjustedPointer(IRBuilderBase &Builder, Value *Ptr,
687687
if (auto *I = dyn_cast<Instruction>(Ptr))
688688
if (Instruction *New = CloneMap.lookup(I))
689689
Ptr = New;
690-
return Builder.CreateGEP(Type::getInt8Ty(HVC.F.getContext()), Ptr,
691-
HVC.getConstInt(Adjust), "gep");
690+
return Builder.CreatePtrAdd(Ptr, HVC.getConstInt(Adjust), "gep");
692691
}
693692

694693
auto AlignVectors::createAlignedPointer(IRBuilderBase &Builder, Value *Ptr,

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,8 @@ static bool foldConsecutiveLoads(Instruction &I, const DataLayout &DL,
808808
APInt Offset1(DL.getIndexTypeSizeInBits(Load1Ptr->getType()), 0);
809809
Load1Ptr = Load1Ptr->stripAndAccumulateConstantOffsets(
810810
DL, Offset1, /* AllowNonInbounds */ true);
811-
Load1Ptr = Builder.CreateGEP(Builder.getInt8Ty(), Load1Ptr,
812-
Builder.getInt32(Offset1.getZExtValue()));
811+
Load1Ptr = Builder.CreatePtrAdd(Load1Ptr,
812+
Builder.getInt32(Offset1.getZExtValue()));
813813
}
814814
// Generate wider load.
815815
NewLoad = Builder.CreateAlignedLoad(WiderType, Load1Ptr, LI1->getAlign(),

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,8 +2022,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
20222022
auto *FramePtr = GetFramePointer(Alloca);
20232023
auto &Value = *Alias.second;
20242024
auto ITy = IntegerType::get(C, Value.getBitWidth());
2025-
auto *AliasPtr = Builder.CreateGEP(Type::getInt8Ty(C), FramePtr,
2026-
ConstantInt::get(ITy, Value));
2025+
auto *AliasPtr =
2026+
Builder.CreatePtrAdd(FramePtr, ConstantInt::get(ITy, Value));
20272027
Alias.first->replaceUsesWithIf(
20282028
AliasPtr, [&](Use &U) { return DT.dominates(CB, U); });
20292029
}

llvm/lib/Transforms/IPO/ArgumentPromotion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static Value *createByteGEP(IRBuilderBase &IRB, const DataLayout &DL,
100100
Value *Ptr, Type *ResElemTy, int64_t Offset) {
101101
if (Offset != 0) {
102102
APInt APOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), Offset);
103-
Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(APOffset));
103+
Ptr = IRB.CreatePtrAdd(Ptr, IRB.getInt(APOffset));
104104
}
105105
return Ptr;
106106
}

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ static Value *constructPointer(Value *Ptr, int64_t Offset,
298298
<< "-bytes\n");
299299

300300
if (Offset)
301-
Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt64(Offset),
302-
Ptr->getName() + ".b" + Twine(Offset));
301+
Ptr = IRB.CreatePtrAdd(Ptr, IRB.getInt64(Offset),
302+
Ptr->getName() + ".b" + Twine(Offset));
303303
return Ptr;
304304
}
305305

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ void DevirtModule::applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
17691769
continue;
17701770
auto *RetType = cast<IntegerType>(Call.CB.getType());
17711771
IRBuilder<> B(&Call.CB);
1772-
Value *Addr = B.CreateGEP(Int8Ty, Call.VTable, Byte);
1772+
Value *Addr = B.CreatePtrAdd(Call.VTable, Byte);
17731773
if (RetType->getBitWidth() == 1) {
17741774
Value *Bits = B.CreateLoad(Int8Ty, Addr);
17751775
Value *BitsAndBit = B.CreateAnd(Bits, Bit);
@@ -2066,14 +2066,14 @@ void DevirtModule::scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc) {
20662066
Value *LoadedValue = nullptr;
20672067
if (TypeCheckedLoadFunc->getIntrinsicID() ==
20682068
Intrinsic::type_checked_load_relative) {
2069-
Value *GEP = LoadB.CreateGEP(Int8Ty, Ptr, Offset);
2069+
Value *GEP = LoadB.CreatePtrAdd(Ptr, Offset);
20702070
LoadedValue = LoadB.CreateLoad(Int32Ty, GEP);
20712071
LoadedValue = LoadB.CreateSExt(LoadedValue, IntPtrTy);
20722072
GEP = LoadB.CreatePtrToInt(GEP, IntPtrTy);
20732073
LoadedValue = LoadB.CreateAdd(GEP, LoadedValue);
20742074
LoadedValue = LoadB.CreateIntToPtr(LoadedValue, Int8PtrTy);
20752075
} else {
2076-
Value *GEP = LoadB.CreateGEP(Int8Ty, Ptr, Offset);
2076+
Value *GEP = LoadB.CreatePtrAdd(Ptr, Offset);
20772077
LoadedValue = LoadB.CreateLoad(Int8PtrTy, GEP);
20782078
}
20792079

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,8 +1590,7 @@ void AddressSanitizer::instrumentMaskedLoadOrStore(
15901590
InstrumentedAddress = IRB.CreateExtractElement(Addr, Index);
15911591
} else if (Stride) {
15921592
Index = IRB.CreateMul(Index, Stride);
1593-
Addr = IRB.CreateBitCast(Addr, PointerType::getUnqual(*C));
1594-
InstrumentedAddress = IRB.CreateGEP(Type::getInt8Ty(*C), Addr, {Index});
1593+
InstrumentedAddress = IRB.CreatePtrAdd(Addr, Index);
15951594
} else {
15961595
InstrumentedAddress = IRB.CreateGEP(VTy, Addr, {Zero, Index});
15971596
}

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ Value *HWAddressSanitizer::memToShadow(Value *Mem, IRBuilder<> &IRB) {
862862
if (Mapping.Offset == 0)
863863
return IRB.CreateIntToPtr(Shadow, PtrTy);
864864
// (Mem >> Scale) + Offset
865-
return IRB.CreateGEP(Int8Ty, ShadowBase, Shadow);
865+
return IRB.CreatePtrAdd(ShadowBase, Shadow);
866866
}
867867

868868
int64_t HWAddressSanitizer::getAccessInfo(bool IsWrite,

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5253,8 +5253,8 @@ struct VarArgAArch64Helper : public VarArgHelperBase {
52535253
Align(8), /*isStore*/ true)
52545254
.first;
52555255

5256-
Value *GrSrcPtr = IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
5257-
GrRegSaveAreaShadowPtrOff);
5256+
Value *GrSrcPtr =
5257+
IRB.CreateInBoundsPtrAdd(VAArgTLSCopy, GrRegSaveAreaShadowPtrOff);
52585258
Value *GrCopySize = IRB.CreateSub(GrArgSize, GrRegSaveAreaShadowPtrOff);
52595259

52605260
IRB.CreateMemCpy(GrRegSaveAreaShadowPtr, Align(8), GrSrcPtr, Align(8),
@@ -5269,10 +5269,9 @@ struct VarArgAArch64Helper : public VarArgHelperBase {
52695269
Align(8), /*isStore*/ true)
52705270
.first;
52715271

5272-
Value *VrSrcPtr = IRB.CreateInBoundsGEP(
5273-
IRB.getInt8Ty(),
5274-
IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
5275-
IRB.getInt32(AArch64VrBegOffset)),
5272+
Value *VrSrcPtr = IRB.CreateInBoundsPtrAdd(
5273+
IRB.CreateInBoundsPtrAdd(VAArgTLSCopy,
5274+
IRB.getInt32(AArch64VrBegOffset)),
52765275
VrRegSaveAreaShadowPtrOff);
52775276
Value *VrCopySize = IRB.CreateSub(VrArgSize, VrRegSaveAreaShadowPtrOff);
52785277

@@ -5285,8 +5284,8 @@ struct VarArgAArch64Helper : public VarArgHelperBase {
52855284
Align(16), /*isStore*/ true)
52865285
.first;
52875286

5288-
Value *StackSrcPtr = IRB.CreateInBoundsGEP(
5289-
IRB.getInt8Ty(), VAArgTLSCopy, IRB.getInt32(AArch64VAEndOffset));
5287+
Value *StackSrcPtr = IRB.CreateInBoundsPtrAdd(
5288+
VAArgTLSCopy, IRB.getInt32(AArch64VAEndOffset));
52905289

52915290
IRB.CreateMemCpy(StackSaveAreaShadowPtr, Align(16), StackSrcPtr,
52925291
Align(16), VAArgOverflowSize);

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
329329

330330
// Account for the fact that on windows-msvc __start_* symbols actually
331331
// point to a uint64_t before the start of the array.
332-
auto GEP = IRB.CreateGEP(Int8Ty, SecStart,
333-
ConstantInt::get(IntptrTy, sizeof(uint64_t)));
332+
auto GEP =
333+
IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
334334
return std::make_pair(GEP, SecEnd);
335335
}
336336

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,9 +1297,9 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
12971297
Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
12981298
Value *MemsetLen = Builder.CreateSelect(
12991299
Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff);
1300-
Instruction *NewMemSet = Builder.CreateMemSet(
1301-
Builder.CreateGEP(Builder.getInt8Ty(), Dest, SrcSize),
1302-
MemSet->getOperand(1), MemsetLen, Alignment);
1300+
Instruction *NewMemSet =
1301+
Builder.CreateMemSet(Builder.CreatePtrAdd(Dest, SrcSize),
1302+
MemSet->getOperand(1), MemsetLen, Alignment);
13031303

13041304
assert(isa<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(MemCpy)) &&
13051305
"MemCpy must be a MemoryDef");

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,8 +1903,8 @@ static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr,
19031903
APInt Offset, Type *PointerTy,
19041904
const Twine &NamePrefix) {
19051905
if (Offset != 0)
1906-
Ptr = IRB.CreateInBoundsGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(Offset),
1907-
NamePrefix + "sroa_idx");
1906+
Ptr = IRB.CreateInBoundsPtrAdd(Ptr, IRB.getInt(Offset),
1907+
NamePrefix + "sroa_idx");
19081908
return IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, PointerTy,
19091909
NamePrefix + "sroa_cast");
19101910
}

llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,7 @@ void SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs(
896896
}
897897
}
898898
// Create an ugly GEP with a single index for each index.
899-
ResultPtr =
900-
Builder.CreateGEP(Builder.getInt8Ty(), ResultPtr, Idx, "uglygep");
899+
ResultPtr = Builder.CreatePtrAdd(ResultPtr, Idx, "uglygep");
901900
if (FirstResult == nullptr)
902901
FirstResult = ResultPtr;
903902
}
@@ -906,8 +905,7 @@ void SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs(
906905
// Create a GEP with the constant offset index.
907906
if (AccumulativeByteOffset != 0) {
908907
Value *Offset = ConstantInt::get(PtrIndexTy, AccumulativeByteOffset);
909-
ResultPtr =
910-
Builder.CreateGEP(Builder.getInt8Ty(), ResultPtr, Offset, "uglygep");
908+
ResultPtr = Builder.CreatePtrAdd(ResultPtr, Offset, "uglygep");
911909
} else
912910
isSwapCandidate = false;
913911

@@ -1107,9 +1105,8 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
11071105

11081106
Type *PtrIdxTy = DL->getIndexType(GEP->getType());
11091107
IRBuilder<> Builder(GEP);
1110-
NewGEP = cast<Instruction>(Builder.CreateGEP(
1111-
Builder.getInt8Ty(), NewGEP,
1112-
{ConstantInt::get(PtrIdxTy, AccumulativeByteOffset, true)},
1108+
NewGEP = cast<Instruction>(Builder.CreatePtrAdd(
1109+
NewGEP, ConstantInt::get(PtrIdxTy, AccumulativeByteOffset, true),
11131110
GEP->getName(), GEPWasInBounds));
11141111
NewGEP->copyMetadata(*GEP);
11151112

llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,7 @@ void StraightLineStrengthReduce::rewriteCandidateWithBasis(
656656
case Candidate::GEP: {
657657
bool InBounds = cast<GetElementPtrInst>(C.Ins)->isInBounds();
658658
// C = (char *)Basis + Bump
659-
Reduced =
660-
Builder.CreateGEP(Builder.getInt8Ty(), Basis.Ins, Bump, "", InBounds);
659+
Reduced = Builder.CreatePtrAdd(Basis.Ins, Bump, "", InBounds);
661660
break;
662661
}
663662
default:

0 commit comments

Comments
 (0)