Skip to content

Commit ad730d3

Browse files
committed
Emit getelementptr nuw for IndexExp in static array with const exp
1 parent 330712f commit ad730d3

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

gen/toir.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,16 @@ class ToElemVisitor : public Visitor {
11981198
}
11991199
LLType *elt = DtoMemType(e1type->nextOf());
12001200
LLType *arrty = llvm::ArrayType::get(elt, e1type->isTypeSArray()->dim->isIntegerExp()->getInteger());
1201-
arrptr = DtoGEP(arrty, DtoLVal(l), DtoConstUint(0), DtoRVal(r));
1201+
#if LDC_LLVM_VER >= 2000
1202+
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
1203+
if (e->indexIsInBounds)
1204+
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
1205+
#endif
1206+
arrptr = DtoGEP(arrty, DtoLVal(l), DtoConstUint(0), DtoRVal(r)
1207+
#if LDC_LLVM_VER >= 2000
1208+
, "", nullptr, nw
1209+
#endif
1210+
);
12021211
} else if (e1type->ty == TY::Tarray) {
12031212
if (p->emitArrayBoundsChecks() && !e->indexIsInBounds) {
12041213
DtoIndexBoundsCheck(e->loc, l, r);

gen/tollvm.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,19 @@ LLValue *DtoGEP(LLType *pointeeTy, LLValue *ptr, unsigned i0, unsigned i1,
428428
}
429429

430430
LLConstant *DtoGEP(LLType *pointeeTy, LLConstant *ptr, unsigned i0,
431-
unsigned i1) {
431+
unsigned i1
432+
#if LDC_LLVM_VER >= 2000
433+
, llvm::GEPNoWrapFlags nw
434+
#endif
435+
) {
432436
LLValue *indices[] = {DtoConstUint(i0), DtoConstUint(i1)};
433437
return llvm::ConstantExpr::getGetElementPtr(pointeeTy, ptr, indices,
434-
/* InBounds = */ true);
438+
#if LDC_LLVM_VER >= 2000
439+
nw
440+
#else
441+
/* InBounds = */ true
442+
#endif
443+
);
435444
}
436445

437446
LLValue *DtoGEP1i64(LLType *pointeeTy, LLValue *ptr, uint64_t i0, const char *name,

gen/tollvm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ LLValue *DtoGEP(LLType *pointeeTy, LLValue *ptr, unsigned i0, unsigned i1,
102102
#endif
103103
);
104104
LLConstant *DtoGEP(LLType *pointeeTy, LLConstant *ptr, unsigned i0,
105-
unsigned i1);
105+
unsigned i1
106+
#if LDC_LLVM_VER >= 2000
107+
, llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds()
108+
#endif
109+
);
106110

107111
LLValue *DtoGEP1i64(LLType *pointeeTy, LLValue *ptr, uint64_t i0,
108112
const char *name = "", llvm::BasicBlock *bb = nullptr

tests/codegen/inbounds.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern(C): // Avoid name mangling
99
// IndexExp in static array with const exp
1010
// CHECK-LABEL: @foo1
1111
int foo1(int[3] a) {
12-
// CHECK: getelementptr inbounds [3 x i32]
12+
// CHECK: getelementptr inbounds{{( nuw)?}} [3 x i32]
1313
return a[1];
1414
}
1515

0 commit comments

Comments
 (0)