Skip to content

Commit 2931faa

Browse files
committed
Emit getelementptr nuw for adding offset to pointer
1 parent 2a438dd commit 2931faa

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

gen/binops.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ DValue *emitPointerOffset(Loc loc, DValue *base, Expression *offset,
116116
if (!llResult) {
117117
if (negateOffset)
118118
llOffset = gIR->ir->CreateNeg(llOffset);
119-
llResult = DtoGEP1(llBaseTy, llBase, llOffset);
119+
#if LDC_LLVM_VER >= 2000
120+
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
121+
if (!negateOffset)
122+
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
123+
#endif
124+
llResult = DtoGEP1(llBaseTy, llBase, llOffset
125+
#if LDC_LLVM_VER >= 2000
126+
, "", nullptr, nw
127+
#endif
128+
);
120129
}
121130

122131
return new DImValue(resultType, llResult);

tests/codegen/inbounds.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ int foo4(int* p) {
3737
// PreExp in pointer
3838
// CHECK-LABEL: @foo5
3939
int foo5(int* p) {
40-
// CHECK: getelementptr inbounds
40+
// CHECK: getelementptr inbounds{{( nuw)?}}
4141
return *++p;
4242
}
4343

4444
// Add offset to pointer
4545
// CHECK-LABEL: @foo6
4646
int foo6(int* p, int i) {
47-
// CHECK: getelementptr inbounds
47+
// CHECK: getelementptr inbounds{{( nuw)?}}
4848
return *(p + i);
4949
}
5050

0 commit comments

Comments
 (0)