Skip to content

Commit 69c4346

Browse files
committed
[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 69437a3 commit 69c4346

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
@@ -155,14 +155,19 @@ bool UnrolledInstAnalyzer::visitCmpInst(CmpInst &I) {
155155
if (Value *SimpleRHS = SimplifiedValues.lookup(RHS))
156156
RHS = SimpleRHS;
157157

158-
if (!isa<Constant>(LHS) && !isa<Constant>(RHS)) {
158+
if (!isa<Constant>(LHS) && !isa<Constant>(RHS) && !I.isSigned()) {
159159
auto SimplifiedLHS = SimplifiedAddresses.find(LHS);
160160
if (SimplifiedLHS != SimplifiedAddresses.end()) {
161161
auto SimplifiedRHS = SimplifiedAddresses.find(RHS);
162162
if (SimplifiedRHS != SimplifiedAddresses.end()) {
163163
SimplifiedAddress &LHSAddr = SimplifiedLHS->second;
164164
SimplifiedAddress &RHSAddr = SimplifiedRHS->second;
165165
if (LHSAddr.Base == RHSAddr.Base) {
166+
// FIXME: This is only correct for equality predicates. For
167+
// unsigned predicates, this only holds if we have nowrap flags,
168+
// which we don't track (for nuw it's valid as-is, for nusw it
169+
// requires converting the predicated to signed). As this is used only
170+
// for cost modelling, this is not a correctness issue.
166171
bool Res = ICmpInst::compare(LHSAddr.Offset, RHSAddr.Offset,
167172
I.getPredicate());
168173
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)