Skip to content

Commit ca99994

Browse files
committed
[SDAG] Preserve InBounds in DAGCombines
This PR preserves the InBounds flag (llvm#162477) where possible in PTRADD-related DAGCombines. We can't preserve them in all the cases that we could in the analogous GISel change (llvm#152495) because SDAG usually represents pointers as integers, which means that pointer provenance is not preserved between PTRADD operations (see the discussion at PR llvm#162477 for more details). This PR marks the places in the DAGCombiner where this is relevant explicitly. For SWDEV-516125.
1 parent e588c7f commit ca99994

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,12 @@ SDValue DAGCombiner::visitPTRADD(SDNode *N) {
27152715
(N->getFlags() & N0->getFlags()) & SDNodeFlags::NoUnsignedWrap;
27162716
SDValue Add = DAG.getNode(ISD::ADD, DL, IntVT, {Y, Z}, Flags);
27172717
AddToWorklist(Add.getNode());
2718+
// We can't set InBounds even if both original ptradds were InBounds and
2719+
// NUW: SDAG usually represents pointers as integers, therefore, the
2720+
// matched pattern behaves as if it had implicit casts:
2721+
// (ptradd inbounds (inttoptr (ptrtoint (ptradd inbounds x, y))), z)
2722+
// The outer inbounds ptradd might therefore rely on a provenance that x
2723+
// does not have.
27182724
return DAG.getMemBasePlusOffset(X, Add, DL, Flags);
27192725
}
27202726
}
@@ -2740,6 +2746,12 @@ SDValue DAGCombiner::visitPTRADD(SDNode *N) {
27402746
// that.
27412747
SDNodeFlags Flags =
27422748
(N->getFlags() & N0->getFlags()) & SDNodeFlags::NoUnsignedWrap;
2749+
// We can't set InBounds even if both original ptradds were InBounds and
2750+
// NUW: SDAG usually represents pointers as integers, therefore, the
2751+
// matched pattern behaves as if it had implicit casts:
2752+
// (ptradd inbounds (inttoptr (ptrtoint (ptradd inbounds GA, v))), c)
2753+
// The outer inbounds ptradd might therefore rely on a provenance that
2754+
// GA does not have.
27432755
SDValue Inner = DAG.getMemBasePlusOffset(GAValue, N1, DL, Flags);
27442756
AddToWorklist(Inner.getNode());
27452757
return DAG.getMemBasePlusOffset(Inner, N0.getOperand(1), DL, Flags);
@@ -2763,8 +2775,13 @@ SDValue DAGCombiner::visitPTRADD(SDNode *N) {
27632775
bool ZIsConstant = DAG.isConstantIntBuildVectorOrConstantInt(Z);
27642776

27652777
// If both additions in the original were NUW, reassociation preserves that.
2766-
SDNodeFlags ReassocFlags =
2767-
(N->getFlags() & N1->getFlags()) & SDNodeFlags::NoUnsignedWrap;
2778+
SDNodeFlags CommonFlags = N->getFlags() & N1->getFlags();
2779+
SDNodeFlags ReassocFlags = CommonFlags & SDNodeFlags::NoUnsignedWrap;
2780+
if (CommonFlags.hasNoUnsignedWrap()) {
2781+
// If both operations are NUW and the PTRADD is inbounds, the offests are
2782+
// both non-negative, so the reassociated PTRADDs are also inbounds.
2783+
ReassocFlags |= N->getFlags() & SDNodeFlags::InBounds;
2784+
}
27682785

27692786
if (ZIsConstant != YIsConstant) {
27702787
if (YIsConstant)

0 commit comments

Comments
 (0)