Skip to content

Commit 2454241

Browse files
nikicakiramenai
authored andcommitted
[LoopUnrollAnalyzer] Don't simplify signed pointer comparison
We're generally not able to simplify signed pointer comparisons (because we don't have no-wrap flags that would permit it), so we shouldn't pretend that we can in the cost model. The unsigned comparison case is also not modelled correctly, as explained in the added comment. As this is a cost model inaccuracy at worst, I'm leaving it alone for now.
1 parent db62d45 commit 2454241

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

llvm/lib/Analysis/LoopUnrollAnalyzer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,19 @@ bool UnrolledInstAnalyzer::visitCmpInst(CmpInst &I) {
179179
if (Value *SimpleRHS = SimplifiedValues.lookup(RHS))
180180
RHS = SimpleRHS;
181181

182-
if (!isa<Constant>(LHS) && !isa<Constant>(RHS)) {
182+
if (!isa<Constant>(LHS) && !isa<Constant>(RHS) && !I.isSigned()) {
183183
auto SimplifiedLHS = SimplifiedAddresses.find(LHS);
184184
if (SimplifiedLHS != SimplifiedAddresses.end()) {
185185
auto SimplifiedRHS = SimplifiedAddresses.find(RHS);
186186
if (SimplifiedRHS != SimplifiedAddresses.end()) {
187187
SimplifiedAddress &LHSAddr = SimplifiedLHS->second;
188188
SimplifiedAddress &RHSAddr = SimplifiedRHS->second;
189189
if (LHSAddr.Base == RHSAddr.Base) {
190+
// FIXME: This is only correct for equality predicates. For
191+
// unsigned predicates, this only holds if we have nowrap flags,
192+
// which we don't track (for nuw it's valid as-is, for nusw it
193+
// requires converting the predicated to signed). As this is used only
194+
// for cost modelling, this is not a correctness issue.
190195
bool Res = ICmpInst::compare(LHSAddr.Offset, RHSAddr.Offset,
191196
I.getPredicate());
192197
SimplifiedValues[&I] = ConstantInt::getBool(I.getType(), Res);

llvm/unittests/Analysis/UnrollAnalyzerTest.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
221221
" %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ]\n"
222222
" %iv2.0 = phi i8* [ %start.iv2, %entry ], [ %iv2.1, %loop.body ]\n"
223223
" %cmp = icmp eq i8* %iv2.0, %iv.0\n"
224+
" %cmp2 = icmp slt i8* %iv2.0, %iv.0\n"
225+
" %cmp3 = icmp ult i8* %iv2.0, %iv.0\n"
224226
" %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1\n"
225227
" %iv2.1 = getelementptr inbounds i8, i8* %iv2.0, i64 1\n"
226228
" %exitcond = icmp ne i8* %iv.1, %limit\n"
@@ -242,12 +244,21 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
242244

243245
BasicBlock::iterator BBI = Header->begin();
244246
std::advance(BBI, 2);
245-
Instruction *Y1 = &*BBI;
247+
Instruction *Cmp1 = &*BBI++;
248+
Instruction *Cmp2 = &*BBI++;
249+
Instruction *Cmp3 = &*BBI++;
246250
// Check simplification expected on the 5th iteration.
247251
// Check that "%cmp = icmp eq i8* %iv2.0, %iv.0" is simplified to 0.
248-
auto I1 = SimplifiedValuesVector[5].find(Y1);
252+
auto I1 = SimplifiedValuesVector[5].find(Cmp1);
249253
EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
250254
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
255+
// Check that "%cmp2 = icmp slt i8* %iv2.0, %iv.0" does not simplify
256+
auto I2 = SimplifiedValuesVector[5].find(Cmp2);
257+
EXPECT_TRUE(I2 == SimplifiedValuesVector[5].end());
258+
// Check that "%cmp3 = icmp ult i8* %iv2.0, %iv.0" is simplified to 0.
259+
auto I3 = SimplifiedValuesVector[5].find(Cmp3);
260+
EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end());
261+
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
251262
}
252263

253264
TEST(UnrollAnalyzerTest, CastSimplifications) {

0 commit comments

Comments
 (0)