Skip to content

Commit ed1cb01

Browse files
committed
[IRBuilder] Add IsInBounds parameter to CreateGEP()
We commonly want to create either an inbounds or non-inbounds GEP based on a boolean value, e.g. when preserving inbounds from existing GEPs. Directly accept such a boolean in the API, rather than requiring a ternary between CreateGEP and CreateInBoundsGEP. This change is not entirely NFC, because we now preserve an inbounds flag in a constant expression edge-case in InstCombine.
1 parent 8e6d481 commit ed1cb01

File tree

8 files changed

+48
-97
lines changed

8 files changed

+48
-97
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,17 +1723,18 @@ class IRBuilderBase {
17231723
}
17241724

17251725
Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1726-
const Twine &Name = "") {
1727-
if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, /*IsInBounds=*/false))
1726+
const Twine &Name = "", bool IsInBounds = false) {
1727+
if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, IsInBounds))
17281728
return V;
1729-
return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1729+
return Insert(IsInBounds
1730+
? GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList)
1731+
: GetElementPtrInst::Create(Ty, Ptr, IdxList),
1732+
Name);
17301733
}
17311734

17321735
Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
17331736
const Twine &Name = "") {
1734-
if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, /*IsInBounds=*/true))
1735-
return V;
1736-
return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1737+
return CreateGEP(Ty, Ptr, IdxList, Name, /* IsInBounds */ true);
17371738
}
17381739

17391740
Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5324,11 +5324,8 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
53245324
// SDAG consecutive load/store merging.
53255325
if (ResultPtr->getType() != I8PtrTy)
53265326
ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
5327-
ResultPtr =
5328-
AddrMode.InBounds
5329-
? Builder.CreateInBoundsGEP(I8Ty, ResultPtr, ResultIndex,
5330-
"sunkaddr")
5331-
: Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
5327+
ResultPtr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex,
5328+
"sunkaddr", AddrMode.InBounds);
53325329
}
53335330

53345331
ResultIndex = V;
@@ -5339,11 +5336,8 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
53395336
} else {
53405337
if (ResultPtr->getType() != I8PtrTy)
53415338
ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
5342-
SunkAddr =
5343-
AddrMode.InBounds
5344-
? Builder.CreateInBoundsGEP(I8Ty, ResultPtr, ResultIndex,
5345-
"sunkaddr")
5346-
: Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
5339+
SunkAddr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr",
5340+
AddrMode.InBounds);
53475341
}
53485342

53495343
if (SunkAddr->getType() != Addr->getType())

llvm/lib/Target/NVPTX/NVPTXGenericToNVVM.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,10 @@ Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
278278
C->getIndices());
279279
case Instruction::GetElementPtr:
280280
// GetElementPtrConstantExpr
281-
return cast<GEPOperator>(C)->isInBounds()
282-
? Builder.CreateGEP(
283-
cast<GEPOperator>(C)->getSourceElementType(),
284-
NewOperands[0],
285-
makeArrayRef(&NewOperands[1], NumOperands - 1))
286-
: Builder.CreateInBoundsGEP(
287-
cast<GEPOperator>(C)->getSourceElementType(),
288-
NewOperands[0],
289-
makeArrayRef(&NewOperands[1], NumOperands - 1));
281+
return Builder.CreateGEP(cast<GEPOperator>(C)->getSourceElementType(),
282+
NewOperands[0],
283+
makeArrayRef(&NewOperands[1], NumOperands - 1), "",
284+
cast<GEPOperator>(C)->isInBounds());
290285
case Instruction::Select:
291286
// SelectConstantExpr
292287
return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,10 +1943,8 @@ static Instruction *foldSelectGEP(GetElementPtrInst &GEP,
19431943
SmallVector<Value *, 4> IndexC(GEP.indices());
19441944
bool IsInBounds = GEP.isInBounds();
19451945
Type *Ty = GEP.getSourceElementType();
1946-
Value *NewTrueC = IsInBounds ? Builder.CreateInBoundsGEP(Ty, TrueC, IndexC)
1947-
: Builder.CreateGEP(Ty, TrueC, IndexC);
1948-
Value *NewFalseC = IsInBounds ? Builder.CreateInBoundsGEP(Ty, FalseC, IndexC)
1949-
: Builder.CreateGEP(Ty, FalseC, IndexC);
1946+
Value *NewTrueC = Builder.CreateGEP(Ty, TrueC, IndexC, "", IsInBounds);
1947+
Value *NewFalseC = Builder.CreateGEP(Ty, FalseC, IndexC, "", IsInBounds);
19501948
return SelectInst::Create(Cond, NewTrueC, NewFalseC, "", nullptr, Sel);
19511949
}
19521950

@@ -2000,11 +1998,9 @@ Instruction *InstCombinerImpl::visitGEPOfGEP(GetElementPtrInst &GEP,
20001998
// -- have to recreate %src & %gep
20011999
// put NewSrc at same location as %src
20022000
Builder.SetInsertPoint(cast<Instruction>(Src));
2003-
Value *NewSrc = Builder.CreateGEP(
2004-
GEP.getSourceElementType(), SO0, GO1, Src->getName());
2005-
// Propagate 'inbounds' if the new source was not constant-folded.
2006-
if (auto *NewSrcGEPI = dyn_cast<GetElementPtrInst>(NewSrc))
2007-
NewSrcGEPI->setIsInBounds(Src->isInBounds());
2001+
Value *NewSrc =
2002+
Builder.CreateGEP(GEP.getSourceElementType(), SO0, GO1,
2003+
Src->getName(), Src->isInBounds());
20082004
GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
20092005
GEP.getSourceElementType(), NewSrc, {SO1});
20102006
NewGEP->setIsInBounds(GEP.isInBounds());
@@ -2178,9 +2174,8 @@ Instruction *InstCombinerImpl::visitGEPOfBitcast(BitCastInst *BCI,
21782174
// existing GEP Value. Causing issues if this Value is accessed when
21792175
// constructing an AddrSpaceCastInst
21802176
SmallVector<Value *, 8> Indices(GEP.indices());
2181-
Value *NGEP = GEP.isInBounds()
2182-
? Builder.CreateInBoundsGEP(SrcEltType, SrcOp, Indices)
2183-
: Builder.CreateGEP(SrcEltType, SrcOp, Indices);
2177+
Value *NGEP =
2178+
Builder.CreateGEP(SrcEltType, SrcOp, Indices, "", GEP.isInBounds());
21842179
NGEP->takeName(&GEP);
21852180

21862181
// Preserve GEP address space to satisfy users
@@ -2231,12 +2226,10 @@ Instruction *InstCombinerImpl::visitGEPOfBitcast(BitCastInst *BCI,
22312226
// Otherwise, if the offset is non-zero, we need to find out if there is a
22322227
// field at Offset in 'A's type. If so, we can pull the cast through the
22332228
// GEP.
2234-
SmallVector<Value*, 8> NewIndices;
2229+
SmallVector<Value *, 8> NewIndices;
22352230
if (findElementAtOffset(SrcType, Offset.getSExtValue(), NewIndices, DL)) {
2236-
Value *NGEP =
2237-
GEP.isInBounds()
2238-
? Builder.CreateInBoundsGEP(SrcEltType, SrcOp, NewIndices)
2239-
: Builder.CreateGEP(SrcEltType, SrcOp, NewIndices);
2231+
Value *NGEP = Builder.CreateGEP(SrcEltType, SrcOp, NewIndices, "",
2232+
GEP.isInBounds());
22402233

22412234
if (NGEP->getType() == GEP.getType())
22422235
return replaceInstUsesWith(GEP, NGEP);
@@ -2539,11 +2532,8 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
25392532
// addrspacecast i8 addrspace(1)* %0 to i8*
25402533
SmallVector<Value *, 8> Idx(GEP.indices());
25412534
Value *NewGEP =
2542-
GEP.isInBounds()
2543-
? Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr,
2544-
Idx, GEP.getName())
2545-
: Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2546-
GEP.getName());
2535+
Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2536+
GEP.getName(), GEP.isInBounds());
25472537
return new AddrSpaceCastInst(NewGEP, GEPType);
25482538
}
25492539
}
@@ -2558,13 +2548,9 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
25582548
DL.getTypeAllocSize(StrippedPtrEltTy->getArrayElementType()) ==
25592549
DL.getTypeAllocSize(GEPEltType)) {
25602550
Type *IdxType = DL.getIndexType(GEPType);
2561-
Value *Idx[2] = { Constant::getNullValue(IdxType), GEP.getOperand(1) };
2562-
Value *NewGEP =
2563-
GEP.isInBounds()
2564-
? Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2565-
GEP.getName())
2566-
: Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2567-
GEP.getName());
2551+
Value *Idx[2] = {Constant::getNullValue(IdxType), GEP.getOperand(1)};
2552+
Value *NewGEP = Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2553+
GEP.getName(), GEP.isInBounds());
25682554

25692555
// V and GEP are both pointer types --> BitCast
25702556
return CastInst::CreatePointerBitCastOrAddrSpaceCast(NewGEP, GEPType);
@@ -2596,11 +2582,8 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
25962582
// If the multiplication NewIdx * Scale may overflow then the new
25972583
// GEP may not be "inbounds".
25982584
Value *NewGEP =
2599-
GEP.isInBounds() && NSW
2600-
? Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr,
2601-
NewIdx, GEP.getName())
2602-
: Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, NewIdx,
2603-
GEP.getName());
2585+
Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, NewIdx,
2586+
GEP.getName(), GEP.isInBounds() && NSW);
26042587

26052588
// The NewGEP must be pointer typed, so must the old one -> BitCast
26062589
return CastInst::CreatePointerBitCastOrAddrSpaceCast(NewGEP,
@@ -2641,11 +2624,8 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
26412624
Value *Off[2] = {Constant::getNullValue(IndTy), NewIdx};
26422625

26432626
Value *NewGEP =
2644-
GEP.isInBounds() && NSW
2645-
? Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr,
2646-
Off, GEP.getName())
2647-
: Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Off,
2648-
GEP.getName());
2627+
Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Off,
2628+
GEP.getName(), GEP.isInBounds() && NSW);
26492629
// The NewGEP must be pointer typed, so must the old one -> BitCast
26502630
return CastInst::CreatePointerBitCastOrAddrSpaceCast(NewGEP,
26512631
GEPType);

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,19 +3474,13 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
34743474

34753475
Type *Ty = GEPI.getSourceElementType();
34763476
Value *True = Sel->getTrueValue();
3477-
Value *NTrue =
3478-
IsInBounds
3479-
? IRB.CreateInBoundsGEP(Ty, True, Index,
3480-
True->getName() + ".sroa.gep")
3481-
: IRB.CreateGEP(Ty, True, Index, True->getName() + ".sroa.gep");
3477+
Value *NTrue = IRB.CreateGEP(Ty, True, Index, True->getName() + ".sroa.gep",
3478+
IsInBounds);
34823479

34833480
Value *False = Sel->getFalseValue();
34843481

3485-
Value *NFalse =
3486-
IsInBounds
3487-
? IRB.CreateInBoundsGEP(Ty, False, Index,
3488-
False->getName() + ".sroa.gep")
3489-
: IRB.CreateGEP(Ty, False, Index, False->getName() + ".sroa.gep");
3482+
Value *NFalse = IRB.CreateGEP(Ty, False, Index,
3483+
False->getName() + ".sroa.gep", IsInBounds);
34903484

34913485
Value *NSel = IRB.CreateSelect(Sel->getCondition(), NTrue, NFalse,
34923486
Sel->getName() + ".sroa.sel");
@@ -3540,10 +3534,8 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
35403534

35413535
IRB.SetInsertPoint(In->getParent(), std::next(In->getIterator()));
35423536
Type *Ty = GEPI.getSourceElementType();
3543-
NewVal = IsInBounds ? IRB.CreateInBoundsGEP(Ty, In, Index,
3544-
In->getName() + ".sroa.gep")
3545-
: IRB.CreateGEP(Ty, In, Index,
3546-
In->getName() + ".sroa.gep");
3537+
NewVal = IRB.CreateGEP(Ty, In, Index, In->getName() + ".sroa.gep",
3538+
IsInBounds);
35473539
}
35483540
NewPN->addIncoming(NewVal, B);
35493541
}

llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -682,24 +682,16 @@ void StraightLineStrengthReduce::rewriteCandidateWithBasis(
682682
unsigned AS = Basis.Ins->getType()->getPointerAddressSpace();
683683
Type *CharTy = Type::getInt8PtrTy(Basis.Ins->getContext(), AS);
684684
Reduced = Builder.CreateBitCast(Basis.Ins, CharTy);
685-
if (InBounds)
686-
Reduced =
687-
Builder.CreateInBoundsGEP(Builder.getInt8Ty(), Reduced, Bump);
688-
else
689-
Reduced = Builder.CreateGEP(Builder.getInt8Ty(), Reduced, Bump);
685+
Reduced =
686+
Builder.CreateGEP(Builder.getInt8Ty(), Reduced, Bump, "", InBounds);
690687
Reduced = Builder.CreateBitCast(Reduced, C.Ins->getType());
691688
} else {
692689
// C = gep Basis, Bump
693690
// Canonicalize bump to pointer size.
694691
Bump = Builder.CreateSExtOrTrunc(Bump, IntPtrTy);
695-
if (InBounds)
696-
Reduced = Builder.CreateInBoundsGEP(
697-
cast<GetElementPtrInst>(Basis.Ins)->getResultElementType(),
698-
Basis.Ins, Bump);
699-
else
700-
Reduced = Builder.CreateGEP(
701-
cast<GetElementPtrInst>(Basis.Ins)->getResultElementType(),
702-
Basis.Ins, Bump);
692+
Reduced = Builder.CreateGEP(
693+
cast<GetElementPtrInst>(Basis.Ins)->getResultElementType(),
694+
Basis.Ins, Bump, "", InBounds);
703695
}
704696
break;
705697
}

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9469,11 +9469,8 @@ void VPWidenGEPRecipe::execute(VPTransformState &State) {
94699469

94709470
// Create the new GEP. Note that this GEP may be a scalar if VF == 1,
94719471
// but it should be a vector, otherwise.
9472-
auto *NewGEP = IsInBounds
9473-
? State.Builder.CreateInBoundsGEP(
9474-
GEP->getSourceElementType(), Ptr, Indices)
9475-
: State.Builder.CreateGEP(GEP->getSourceElementType(),
9476-
Ptr, Indices);
9472+
auto *NewGEP = State.Builder.CreateGEP(GEP->getSourceElementType(), Ptr,
9473+
Indices, "", IsInBounds);
94779474
assert((State.VF.isScalar() || NewGEP->getType()->isVectorTy()) &&
94789475
"NewGEP is not a pointer vector");
94799476
State.set(this, NewGEP, Part);

llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ define void @PR51485(<2 x i64> %v) {
195195
; CHECK-NEXT: br label [[LOOP:%.*]]
196196
; CHECK: loop:
197197
; CHECK-NEXT: [[SL1:%.*]] = shl nuw nsw <2 x i64> [[V:%.*]], <i64 7, i64 7>
198-
; CHECK-NEXT: [[E6:%.*]] = getelementptr inbounds i8, i8* getelementptr (i8, i8* bitcast (void (<2 x i64>)* @PR51485 to i8*), i64 80), <2 x i64> [[SL1]]
198+
; CHECK-NEXT: [[E6:%.*]] = getelementptr inbounds i8, i8* getelementptr inbounds (i8, i8* bitcast (void (<2 x i64>)* @PR51485 to i8*), i64 80), <2 x i64> [[SL1]]
199199
; CHECK-NEXT: call void @blackhole(<2 x i8*> [[E6]])
200200
; CHECK-NEXT: br label [[LOOP]]
201201
;

0 commit comments

Comments
 (0)