Skip to content

Commit 8ed0e6b

Browse files
committed
[SelectionDAG] Replace error prone index check in BaseIndexOffset::computeAliasing
Deriving NoAlias based on having the same index in two BaseIndexOffset expressions seemed weird (and as shown in the added unittest the correctness of doing so depended on undocumented pre-conditions that the user of BaseIndexOffset::computeAliasing would need to take care of. This patch removes the code that dereived NoAlias based on indices being the same. As a compensation, to avoid regressions/diffs in various lit test, we also add a new check. The new check derives NoAlias in case the two base pointers are based on two different GlobalValue:s (neither of them being a GlobalAlias). Reviewed By: niravd Differential Revision: https://reviews.llvm.org/D110256
1 parent 1896fb2 commit 8ed0e6b

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,20 @@ bool BaseIndexOffset::computeAliasing(const SDNode *Op0,
150150
IsAlias = false;
151151
return true;
152152
}
153-
// We cannot safely determine whether the pointers alias if we compare two
154-
// global values and at least one is a GlobalAlias.
155-
if (IsGV0 && IsGV1 &&
156-
(isa<GlobalAlias>(
157-
cast<GlobalAddressSDNode>(BasePtr0.getBase())->getGlobal()) ||
158-
isa<GlobalAlias>(
159-
cast<GlobalAddressSDNode>(BasePtr1.getBase())->getGlobal())))
160-
return false;
161-
// If checkable indices we can check they do not alias.
162-
// FIXME: Please describe this a bit better. Looks weird to say that there
163-
// is no alias if the indices are the same. Is this code assuming that
164-
// someone has checked that the base isn't the same as a precondition? And
165-
// what about offsets? And what about NumBytes0 and NumBytest1 (can we
166-
// really derive NoAlias here if we do not even know how many bytes that are
167-
// dereferenced)?
168-
if (BasePtr0.getIndex() == BasePtr1.getIndex()) {
169-
IsAlias = false;
170-
return true;
153+
if (IsGV0 && IsGV1) {
154+
auto *GV0 = cast<GlobalAddressSDNode>(BasePtr0.getBase())->getGlobal();
155+
auto *GV1 = cast<GlobalAddressSDNode>(BasePtr1.getBase())->getGlobal();
156+
// It doesn't make sense to access one global value using another globals
157+
// values address, so we can assume that there is no aliasing in case of
158+
// two different globals (unless we have symbols that may indirectly point
159+
// to each other).
160+
// FIXME: This is perhaps a bit too defensive. We could try to follow the
161+
// chain with aliasee information for GlobalAlias variables to find out if
162+
// we indirect symbols may alias or not.
163+
if (GV0 != GV1 && !isa<GlobalAlias>(GV0) && !isa<GlobalAlias>(GV1)) {
164+
IsAlias = false;
165+
return true;
166+
}
171167
}
172168
}
173169
return false; // Cannot determine whether the pointers alias.

llvm/unittests/CodeGen/SelectionDAGAddressAnalysisTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ TEST_F(SelectionDAGAddressAnalysisTest, sameFrameObject) {
121121
EXPECT_TRUE(IsAlias);
122122
}
123123

124+
TEST_F(SelectionDAGAddressAnalysisTest, sameFrameObjectUnknownSize) {
125+
SDLoc Loc;
126+
auto Int8VT = EVT::getIntegerVT(Context, 8);
127+
auto VecVT = EVT::getVectorVT(Context, Int8VT, 4);
128+
SDValue FIPtr = DAG->CreateStackTemporary(VecVT);
129+
int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
130+
MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(*MF, FI);
131+
TypeSize Offset = TypeSize::Fixed(0);
132+
SDValue Value = DAG->getConstant(0, Loc, VecVT);
133+
SDValue Index = DAG->getMemBasePlusOffset(FIPtr, Offset, Loc);
134+
SDValue Store = DAG->getStore(DAG->getEntryNode(), Loc, Value, Index,
135+
PtrInfo.getWithOffset(Offset));
136+
137+
// Maybe unlikely that BaseIndexOffset::computeAliasing is used with the
138+
// optional NumBytes being unset like in this test, but it would be confusing
139+
// if that function determined IsAlias=false here.
140+
Optional<int64_t> NumBytes;
141+
142+
bool IsAlias;
143+
bool IsValid = BaseIndexOffset::computeAliasing(
144+
Store.getNode(), NumBytes, Store.getNode(), NumBytes, *DAG, IsAlias);
145+
146+
EXPECT_FALSE(IsValid);
147+
}
148+
124149
TEST_F(SelectionDAGAddressAnalysisTest, noAliasingFrameObjects) {
125150
SDLoc Loc;
126151
auto Int8VT = EVT::getIntegerVT(Context, 8);

0 commit comments

Comments
 (0)