Skip to content

Commit ced15cd

Browse files
authored
DAG: Preserve more flags when expanding gep (#110815)
This allows selecting the addressing mode for stack instructions in cases where we need to prove the sign bit is zero.
1 parent 671cbcf commit ced15cd

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4386,34 +4386,50 @@ void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
43864386
// it.
43874387
IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
43884388

4389+
SDNodeFlags ScaleFlags;
4390+
// The multiplication of an index by the type size does not wrap the
4391+
// pointer index type in a signed sense (mul nsw).
4392+
ScaleFlags.setNoSignedWrap(NW.hasNoUnsignedSignedWrap());
4393+
4394+
// The multiplication of an index by the type size does not wrap the
4395+
// pointer index type in an unsigned sense (mul nuw).
4396+
ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4397+
43894398
if (ElementScalable) {
43904399
EVT VScaleTy = N.getValueType().getScalarType();
43914400
SDValue VScale = DAG.getNode(
43924401
ISD::VSCALE, dl, VScaleTy,
43934402
DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
43944403
if (IsVectorGEP)
43954404
VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4396-
IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale);
4405+
IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4406+
ScaleFlags);
43974407
} else {
43984408
// If this is a multiply by a power of two, turn it into a shl
43994409
// immediately. This is a very common case.
44004410
if (ElementMul != 1) {
44014411
if (ElementMul.isPowerOf2()) {
44024412
unsigned Amt = ElementMul.logBase2();
4403-
IdxN = DAG.getNode(ISD::SHL, dl,
4404-
N.getValueType(), IdxN,
4405-
DAG.getConstant(Amt, dl, IdxN.getValueType()));
4413+
IdxN = DAG.getNode(ISD::SHL, dl, N.getValueType(), IdxN,
4414+
DAG.getConstant(Amt, dl, IdxN.getValueType()),
4415+
ScaleFlags);
44064416
} else {
44074417
SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
44084418
IdxN.getValueType());
4409-
IdxN = DAG.getNode(ISD::MUL, dl,
4410-
N.getValueType(), IdxN, Scale);
4419+
IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4420+
ScaleFlags);
44114421
}
44124422
}
44134423
}
44144424

4415-
N = DAG.getNode(ISD::ADD, dl,
4416-
N.getValueType(), N, IdxN);
4425+
// The successive addition of the current address, truncated to the
4426+
// pointer index type and interpreted as an unsigned number, and each
4427+
// offset, also interpreted as an unsigned number, does not wrap the
4428+
// pointer index type (add nuw).
4429+
SDNodeFlags AddFlags;
4430+
AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4431+
4432+
N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N, IdxN, AddFlags);
44174433
}
44184434
}
44194435

llvm/test/DebugInfo/Sparc/pointer-add-unknown-offset-debug-info.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ define void @pointer_add_unknown_offset(ptr %base, i32 %offset) !dbg !7 {
1212
; CHECK-NEXT: [[COPY:%[0-9]+]]:i64regs = COPY $i1
1313
; CHECK-NEXT: [[COPY1:%[0-9]+]]:i64regs = COPY $i0
1414
; CHECK-NEXT: [[SRAri:%[0-9]+]]:i64regs = SRAri [[COPY]], 0
15-
; CHECK-NEXT: [[SLLXri:%[0-9]+]]:i64regs = SLLXri killed [[SRAri]], 2
15+
; CHECK-NEXT: [[SLLXri:%[0-9]+]]:i64regs = nsw SLLXri killed [[SRAri]], 2
1616
; CHECK-NEXT: DBG_VALUE_LIST !13, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value), [[COPY1]], [[SLLXri]], debug-location !16
1717
; CHECK-NEXT: DBG_VALUE_LIST !14, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_plus_uconst, 3, DW_OP_stack_value), [[COPY1]], [[SLLXri]], debug-location !16
1818
; CHECK-NEXT: DBG_VALUE_LIST !15, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 2, DW_OP_plus, DW_OP_LLVM_arg, 1, DW_OP_LLVM_arg, 3, DW_OP_plus, DW_OP_plus, DW_OP_stack_value), [[COPY1]], [[COPY1]], [[SLLXri]], [[SLLXri]], debug-location !16

0 commit comments

Comments
 (0)