Skip to content

Commit f98be87

Browse files
committed
[InstSimplify] Accept GEPNoWrapFlags instead of only InBounds flag
This preserves the flags if a constexpr GEP is created (at least as long as they don't get dropped later -- the test cases uses a constexpr index to avoid that).
1 parent 99873b3 commit f98be87

File tree

6 files changed

+27
-15
lines changed

6 files changed

+27
-15
lines changed

llvm/include/llvm/Analysis/InstructionSimplify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
166166

167167
/// Given operands for a GetElementPtrInst, fold the result or return null.
168168
Value *simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
169-
bool InBounds, const SimplifyQuery &Q);
169+
GEPNoWrapFlags NW, const SimplifyQuery &Q);
170170

171171
/// Given operands for an InsertValueInst, fold the result or return null.
172172
Value *simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ static Value *simplifyXorInst(Value *, Value *, const SimplifyQuery &,
7171
unsigned);
7272
static Value *simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &,
7373
unsigned);
74-
static Value *simplifyGEPInst(Type *, Value *, ArrayRef<Value *>, bool,
75-
const SimplifyQuery &, unsigned);
74+
static Value *simplifyGEPInst(Type *, Value *, ArrayRef<Value *>,
75+
GEPNoWrapFlags, const SimplifyQuery &, unsigned);
7676
static Value *simplifySelectInst(Value *, Value *, Value *,
7777
const SimplifyQuery &, unsigned);
7878
static Value *simplifyInstructionWithOperands(Instruction *I,
@@ -4943,7 +4943,7 @@ Value *llvm::simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
49434943
/// Given operands for an GetElementPtrInst, see if we can fold the result.
49444944
/// If not, this returns null.
49454945
static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
4946-
ArrayRef<Value *> Indices, bool InBounds,
4946+
ArrayRef<Value *> Indices, GEPNoWrapFlags NW,
49474947
const SimplifyQuery &Q, unsigned) {
49484948
// The type of the GEP pointer operand.
49494949
unsigned AS =
@@ -5068,17 +5068,18 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
50685068
return nullptr;
50695069

50705070
if (!ConstantExpr::isSupportedGetElementPtr(SrcTy))
5071-
return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr), InBounds,
5072-
std::nullopt, Indices);
5071+
// TODO(gep_nowrap): Pass on the whole GEPNoWrapFlags.
5072+
return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr),
5073+
NW.isInBounds(), std::nullopt, Indices);
50735074

5074-
auto *CE = ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices,
5075-
InBounds);
5075+
auto *CE =
5076+
ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices, NW);
50765077
return ConstantFoldConstant(CE, Q.DL);
50775078
}
50785079

50795080
Value *llvm::simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
5080-
bool InBounds, const SimplifyQuery &Q) {
5081-
return ::simplifyGEPInst(SrcTy, Ptr, Indices, InBounds, Q, RecursionLimit);
5081+
GEPNoWrapFlags NW, const SimplifyQuery &Q) {
5082+
return ::simplifyGEPInst(SrcTy, Ptr, Indices, NW, Q, RecursionLimit);
50825083
}
50835084

50845085
/// Given operands for an InsertValueInst, see if we can fold the result.
@@ -7077,7 +7078,7 @@ static Value *simplifyInstructionWithOperands(Instruction *I,
70777078
case Instruction::GetElementPtr: {
70787079
auto *GEPI = cast<GetElementPtrInst>(I);
70797080
return simplifyGEPInst(GEPI->getSourceElementType(), NewOps[0],
7080-
ArrayRef(NewOps).slice(1), GEPI->isInBounds(), Q,
7081+
ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q,
70817082
MaxRecurse);
70827083
}
70837084
case Instruction::InsertValue: {

llvm/lib/Analysis/PHITransAddr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ Value *PHITransAddr::translateSubExpr(Value *V, BasicBlock *CurBB,
214214
// Simplify the GEP to handle 'gep x, 0' -> x etc.
215215
if (Value *V = simplifyGEPInst(GEP->getSourceElementType(), GEPOps[0],
216216
ArrayRef<Value *>(GEPOps).slice(1),
217-
GEP->isInBounds(), {DL, TLI, DT, AC})) {
217+
GEP->getNoWrapFlags(), {DL, TLI, DT, AC})) {
218218
for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
219219
RemoveInstInputs(GEPOps[i], InstInputs);
220220

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,8 +2709,9 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
27092709
SmallVector<Value *, 8> Indices(GEP.indices());
27102710
Type *GEPType = GEP.getType();
27112711
Type *GEPEltType = GEP.getSourceElementType();
2712-
if (Value *V = simplifyGEPInst(GEPEltType, PtrOp, Indices, GEP.isInBounds(),
2713-
SQ.getWithInstruction(&GEP)))
2712+
if (Value *V =
2713+
simplifyGEPInst(GEPEltType, PtrOp, Indices, GEP.getNoWrapFlags(),
2714+
SQ.getWithInstruction(&GEP)))
27142715
return replaceInstUsesWith(GEP, V);
27152716

27162717
// For vector geps, use the generic demanded vector support.

llvm/lib/Transforms/Scalar/NewGVN.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ NewGVN::ExprResult NewGVN::createExpression(Instruction *I) const {
11991199
} else if (auto *GEPI = dyn_cast<GetElementPtrInst>(I)) {
12001200
Value *V = simplifyGEPInst(GEPI->getSourceElementType(), *E->op_begin(),
12011201
ArrayRef(std::next(E->op_begin()), E->op_end()),
1202-
GEPI->isInBounds(), Q);
1202+
GEPI->getNoWrapFlags(), Q);
12031203
if (auto Simplified = checkExprResults(E, I, V))
12041204
return Simplified;
12051205
} else if (AllConstant) {

llvm/test/Transforms/InstSimplify/gep.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
55

66
%struct.A = type { [7 x i8] }
77

8+
@g = external global i8
9+
810
define ptr @test1(ptr %b, ptr %e) {
911
; CHECK-LABEL: @test1(
1012
; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
@@ -271,6 +273,14 @@ define <vscale x 2 x ptr> @ptr_idx_mix_scalar_scalable_vector() {
271273
ret <vscale x 2 x ptr> %v
272274
}
273275

276+
define ptr @constexpr_gep_nusw_nuw() {
277+
; CHECK-LABEL: @constexpr_gep_nusw_nuw(
278+
; CHECK-NEXT: ret ptr getelementptr nusw nuw (i8, ptr @g, i64 ptrtoint (ptr @g to i64))
279+
;
280+
%v = getelementptr nusw nuw i8, ptr @g, i64 ptrtoint (ptr @g to i64)
281+
ret ptr %v
282+
}
283+
274284
; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - end.
275285

276286
define ptr @poison() {

0 commit comments

Comments
 (0)