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

0 commit comments

Comments
 (0)