Skip to content

Commit 141e845

Browse files
committed
[SCEV] Make SCEVAddExpr actually always return pointer type if there is pointer operand (PR46457)
Summary: The added assertion fails on the added test without the fix. Reduced from test-suite/MultiSource/Benchmarks/MiBench/office-ispell/correct.c In IR, getelementptr, obviously, takes pointer as it's base, and returns a pointer. When creating an SCEV expression, SCEV operands are sorted in hope that it increases folding potential, and at the same time SCEVAddExpr's type is the type of the last(!) operand. Which means, in some exceedingly rare cases, pointer operand may happen to end up not being the last operand, and as a result SCEV for GEP will suddenly have a non-pointer return type. We should ensure that does not happen. In the end, actually storing the `Type *`, at the cost of increasing memory footprint of `SCEVAddExpr`, appears to be the solution. We can't just store a 'is a pointer' bit and create pointer type on the fly since we don't have data layout in getType(). Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=46457 | PR46457 ]] Reviewers: efriedma, mkazantsev, reames, nikic Reviewed By: efriedma Subscribers: hiraditya, javed.absar, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D82633
1 parent f9f52c8 commit 141e845

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

llvm/include/llvm/Analysis/ScalarEvolutionExpressions.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,22 @@ class Type;
222222
class SCEVAddExpr : public SCEVCommutativeExpr {
223223
friend class ScalarEvolution;
224224

225-
SCEVAddExpr(const FoldingSetNodeIDRef ID,
226-
const SCEV *const *O, size_t N)
227-
: SCEVCommutativeExpr(ID, scAddExpr, O, N) {}
225+
Type *Ty;
228226

229-
public:
230-
Type *getType() const {
231-
// Use the type of the last operand, which is likely to be a pointer
232-
// type, if there is one. This doesn't usually matter, but it can help
233-
// reduce casts when the expressions are expanded.
234-
return getOperand(getNumOperands() - 1)->getType();
227+
SCEVAddExpr(const FoldingSetNodeIDRef ID, const SCEV *const *O, size_t N)
228+
: SCEVCommutativeExpr(ID, scAddExpr, O, N) {
229+
auto *FirstPointerTypedOp = find_if(operands(), [](const SCEV *Op) {
230+
return Op->getType()->isPointerTy();
231+
});
232+
if (FirstPointerTypedOp != operands().end())
233+
Ty = (*FirstPointerTypedOp)->getType();
234+
else
235+
Ty = getOperand(0)->getType();
235236
}
236237

238+
public:
239+
Type *getType() const { return Ty; }
240+
237241
/// Methods for support type inquiry through isa, cast, and dyn_cast:
238242
static bool classof(const SCEV *S) {
239243
return S->getSCEVType() == scAddExpr;

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3317,7 +3317,10 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
33173317
}
33183318

33193319
// Add the total offset from all the GEP indices to the base.
3320-
return getAddExpr(BaseExpr, TotalOffset, Wrap);
3320+
auto *GEPExpr = getAddExpr(BaseExpr, TotalOffset, Wrap);
3321+
assert(BaseExpr->getType() == GEPExpr->getType() &&
3322+
"GEP should not change type mid-flight.");
3323+
return GEPExpr;
33213324
}
33223325

33233326
std::tuple<SCEV *, FoldingSetNodeID, void *>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2+
; RUN: opt < %s -S -analyze -scalar-evolution | FileCheck %s
3+
4+
; Reduced from test-suite/MultiSource/Benchmarks/MiBench/office-ispell/correct.c
5+
; getelementptr, obviously, takes pointer as it's base, and returns a pointer.
6+
; SCEV operands are sorted in hope that it increases folding potential,
7+
; and at the same time SCEVAddExpr's type is the type of the last(!) operand.
8+
; Which means, in some exceedingly rare cases, pointer operand may happen to
9+
; end up not being the last operand, and as a result SCEV for GEP will suddenly
10+
; have a non-pointer return type. We should ensure that does not happen.
11+
12+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
13+
target triple = "x86_64-unknown-linux-gnu"
14+
15+
@c = dso_local local_unnamed_addr global i32* null, align 8
16+
@a = dso_local local_unnamed_addr global i32 0, align 4
17+
@b = dso_local global [1 x i32] zeroinitializer, align 4
18+
19+
define i32 @d(i32 %base) {
20+
; CHECK-LABEL: 'd'
21+
; CHECK-NEXT: Classifying expressions for: @d
22+
; CHECK-NEXT: %e = alloca [1 x [1 x i8]], align 1
23+
; CHECK-NEXT: --> %e U: full-set S: full-set
24+
; CHECK-NEXT: %0 = bitcast [1 x [1 x i8]]* %e to i8*
25+
; CHECK-NEXT: --> %e U: full-set S: full-set
26+
; CHECK-NEXT: %f.0 = phi i32 [ %base, %entry ], [ %inc, %for.cond ]
27+
; CHECK-NEXT: --> {%base,+,1}<nsw><%for.cond> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Computable }
28+
; CHECK-NEXT: %idxprom = sext i32 %f.0 to i64
29+
; CHECK-NEXT: --> {(sext i32 %base to i64),+,1}<nsw><%for.cond> U: [-2147483648,-9223372036854775808) S: [-2147483648,-9223372036854775808) Exits: <<Unknown>> LoopDispositions: { %for.cond: Computable }
30+
; CHECK-NEXT: %arrayidx = getelementptr inbounds [1 x [1 x i8]], [1 x [1 x i8]]* %e, i64 0, i64 %idxprom
31+
; CHECK-NEXT: --> {((sext i32 %base to i64) + %e)<nsw>,+,1}<nsw><%for.cond> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Computable }
32+
; CHECK-NEXT: %1 = load i32*, i32** @c, align 8
33+
; CHECK-NEXT: --> %1 U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Variant }
34+
; CHECK-NEXT: %sub.ptr.lhs.cast = ptrtoint i32* %1 to i64
35+
; CHECK-NEXT: --> %sub.ptr.lhs.cast U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Variant }
36+
; CHECK-NEXT: %sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, ptrtoint ([1 x i32]* @b to i64)
37+
; CHECK-NEXT: --> ((-1 * ptrtoint ([1 x i32]* @b to i64)) + %sub.ptr.lhs.cast) U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Variant }
38+
; CHECK-NEXT: %sub.ptr.div = sdiv exact i64 %sub.ptr.sub, 4
39+
; CHECK-NEXT: --> %sub.ptr.div U: full-set S: [-2305843009213693952,2305843009213693952) Exits: <<Unknown>> LoopDispositions: { %for.cond: Variant }
40+
; CHECK-NEXT: %arrayidx1 = getelementptr inbounds [1 x i8], [1 x i8]* %arrayidx, i64 0, i64 %sub.ptr.div
41+
; CHECK-NEXT: --> ({((sext i32 %base to i64) + %e)<nsw>,+,1}<nsw><%for.cond> + %sub.ptr.div)<nsw> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Variant }
42+
; CHECK-NEXT: %2 = load i8, i8* %arrayidx1, align 1
43+
; CHECK-NEXT: --> %2 U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Variant }
44+
; CHECK-NEXT: %conv = sext i8 %2 to i32
45+
; CHECK-NEXT: --> (sext i8 %2 to i32) U: [-128,128) S: [-128,128) Exits: <<Unknown>> LoopDispositions: { %for.cond: Variant }
46+
; CHECK-NEXT: %inc = add nsw i32 %f.0, 1
47+
; CHECK-NEXT: --> {(1 + %base),+,1}<nw><%for.cond> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %for.cond: Computable }
48+
; CHECK-NEXT: Determining loop execution counts for: @d
49+
; CHECK-NEXT: Loop %for.cond: <multiple exits> Unpredictable backedge-taken count.
50+
; CHECK-NEXT: Loop %for.cond: Unpredictable max backedge-taken count.
51+
; CHECK-NEXT: Loop %for.cond: Unpredictable predicated backedge-taken count.
52+
;
53+
entry:
54+
%e = alloca [1 x [1 x i8]], align 1
55+
%0 = bitcast [1 x [1 x i8]]* %e to i8*
56+
call void @llvm.lifetime.start.p0i8(i64 1, i8* %0) #2
57+
br label %for.cond
58+
59+
for.cond: ; preds = %for.cond, %entry
60+
%f.0 = phi i32 [ %base, %entry ], [ %inc, %for.cond ]
61+
%idxprom = sext i32 %f.0 to i64
62+
%arrayidx = getelementptr inbounds [1 x [1 x i8]], [1 x [1 x i8]]* %e, i64 0, i64 %idxprom
63+
%1 = load i32*, i32** @c, align 8
64+
%sub.ptr.lhs.cast = ptrtoint i32* %1 to i64
65+
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, ptrtoint ([1 x i32]* @b to i64)
66+
%sub.ptr.div = sdiv exact i64 %sub.ptr.sub, 4
67+
%arrayidx1 = getelementptr inbounds [1 x i8], [1 x i8]* %arrayidx, i64 0, i64 %sub.ptr.div
68+
%2 = load i8, i8* %arrayidx1, align 1
69+
%conv = sext i8 %2 to i32
70+
store i32 %conv, i32* @a, align 4
71+
%inc = add nsw i32 %f.0, 1
72+
br label %for.cond
73+
}
74+
75+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)

0 commit comments

Comments
 (0)