Skip to content

Commit 82c93b6

Browse files
[SCEV] Simplify SCEVExpr for PHI to SCEV for operand if operands are identical (#115945)
Helps SCEV analyze some special phi nodes, allowing the computation of loop trip count in cases like the following: https://godbolt.org/z/xGs1d81TW
1 parent 49abcd2 commit 82c93b6

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,10 @@ class ScalarEvolution {
17801780
/// V.
17811781
const SCEV *getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops);
17821782

1783+
/// Returns SCEV for the first operand of a phi if all phi operands have
1784+
/// identical opcodes and operands.
1785+
const SCEV *createNodeForPHIWithIdenticalOperands(PHINode *PN);
1786+
17831787
/// Provide the special handling we need to analyze PHI SCEVs.
17841788
const SCEV *createNodeForPHI(PHINode *PN);
17851789

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6019,6 +6019,42 @@ const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
60196019
return nullptr;
60206020
}
60216021

6022+
/// Returns SCEV for the first operand of a phi if all phi operands have
6023+
/// identical opcodes and operands
6024+
/// eg.
6025+
/// a: %add = %a + %b
6026+
/// br %c
6027+
/// b: %add1 = %a + %b
6028+
/// br %c
6029+
/// c: %phi = phi [%add, a], [%add1, b]
6030+
/// scev(%phi) => scev(%add)
6031+
const SCEV *
6032+
ScalarEvolution::createNodeForPHIWithIdenticalOperands(PHINode *PN) {
6033+
BinaryOperator *CommonInst = nullptr;
6034+
// Check if instructions are identical.
6035+
for (Value *Incoming : PN->incoming_values()) {
6036+
auto *IncomingInst = dyn_cast<BinaryOperator>(Incoming);
6037+
if (!IncomingInst)
6038+
return nullptr;
6039+
if (CommonInst) {
6040+
if (!CommonInst->isIdenticalToWhenDefined(IncomingInst))
6041+
return nullptr; // Not identical, give up
6042+
} else {
6043+
// Remember binary operator
6044+
CommonInst = IncomingInst;
6045+
}
6046+
}
6047+
if (!CommonInst)
6048+
return nullptr;
6049+
6050+
// Check if SCEV exprs for instructions are identical.
6051+
const SCEV *CommonSCEV = getSCEV(CommonInst);
6052+
bool SCEVExprsIdentical =
6053+
all_of(drop_begin(PN->incoming_values()),
6054+
[this, CommonSCEV](Value *V) { return CommonSCEV == getSCEV(V); });
6055+
return SCEVExprsIdentical ? CommonSCEV : nullptr;
6056+
}
6057+
60226058
const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
60236059
if (const SCEV *S = createAddRecFromPHI(PN))
60246060
return S;
@@ -6030,6 +6066,9 @@ const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
60306066
/*UseInstrInfo=*/true, /*CanUseUndef=*/false}))
60316067
return getSCEV(V);
60326068

6069+
if (const SCEV *S = createNodeForPHIWithIdenticalOperands(PN))
6070+
return S;
6071+
60336072
if (const SCEV *S = createNodeFromSelectLikePHI(PN))
60346073
return S;
60356074

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" 2>&1 | FileCheck %s
3+
define void @test1(ptr %x, ptr %y) {
4+
; CHECK-LABEL: 'test1'
5+
; CHECK-NEXT: Classifying expressions for: @test1
6+
; CHECK-NEXT: %v1.0 = phi i32 [ 0, %entry ], [ %k.0, %if.end ]
7+
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%for.cond> U: [0,7) S: [0,7) Exits: 6 LoopDispositions: { %for.cond: Computable }
8+
; CHECK-NEXT: %add = add nsw i32 %v1.0, 1
9+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%for.cond> U: [1,8) S: [1,8) Exits: 7 LoopDispositions: { %for.cond: Computable }
10+
; CHECK-NEXT: %add6 = add nsw i32 %v1.0, 1
11+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%for.cond> U: [1,8) S: [1,8) Exits: 7 LoopDispositions: { %for.cond: Computable }
12+
; CHECK-NEXT: %k.0 = phi i32 [ %add, %if.then ], [ %add6, %if.else ]
13+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%for.cond> U: [1,8) S: [1,8) Exits: 7 LoopDispositions: { %for.cond: Computable }
14+
; CHECK-NEXT: Determining loop execution counts for: @test1
15+
; CHECK-NEXT: Loop %for.cond: backedge-taken count is i32 6
16+
; CHECK-NEXT: Loop %for.cond: constant max backedge-taken count is i32 6
17+
; CHECK-NEXT: Loop %for.cond: symbolic max backedge-taken count is i32 6
18+
; CHECK-NEXT: Loop %for.cond: Trip multiple is 7
19+
;
20+
entry:
21+
br label %for.cond
22+
23+
for.cond: ; preds = %6, %0
24+
%v1.0 = phi i32 [ 0, %entry ], [ %k.0, %if.end ]
25+
%cmp = icmp slt i32 %v1.0, 6
26+
br i1 %cmp, label %for.body, label %exit
27+
28+
for.body: ; preds = %1
29+
%cmp3 = icmp slt i32 %v1.0, 2
30+
br i1 %cmp3, label %if.then, label %if.else
31+
32+
if.then: ; preds = %2
33+
%add = add nsw i32 %v1.0, 1
34+
br label %if.end
35+
36+
if.else: ; preds = %2
37+
%add6 = add nsw i32 %v1.0, 1
38+
br label %if.end
39+
40+
if.end: ; preds = %4, %3
41+
%k.0 = phi i32 [ %add, %if.then ], [ %add6, %if.else ]
42+
br label %for.cond
43+
44+
exit: ; preds = %5
45+
ret void
46+
}
47+
48+
define void @test2(ptr %x, ptr %y) {
49+
; CHECK-LABEL: 'test2'
50+
; CHECK-NEXT: Classifying expressions for: @test2
51+
; CHECK-NEXT: %v1.0 = phi i32 [ 0, %entry ], [ %k.0, %if.end ]
52+
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%for.cond> U: [0,7) S: [0,7) Exits: 6 LoopDispositions: { %for.cond: Computable }
53+
; CHECK-NEXT: %add = add nuw i32 %v1.0, 1
54+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%for.cond> U: [1,8) S: [1,8) Exits: 7 LoopDispositions: { %for.cond: Computable }
55+
; CHECK-NEXT: %add6 = add nsw i32 %v1.0, 1
56+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%for.cond> U: [1,8) S: [1,8) Exits: 7 LoopDispositions: { %for.cond: Computable }
57+
; CHECK-NEXT: %k.0 = phi i32 [ %add, %if.then ], [ %add6, %if.else ]
58+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%for.cond> U: [1,8) S: [1,8) Exits: 7 LoopDispositions: { %for.cond: Computable }
59+
; CHECK-NEXT: Determining loop execution counts for: @test2
60+
; CHECK-NEXT: Loop %for.cond: backedge-taken count is i32 6
61+
; CHECK-NEXT: Loop %for.cond: constant max backedge-taken count is i32 6
62+
; CHECK-NEXT: Loop %for.cond: symbolic max backedge-taken count is i32 6
63+
; CHECK-NEXT: Loop %for.cond: Trip multiple is 7
64+
;
65+
entry:
66+
br label %for.cond
67+
68+
for.cond: ; preds = %6, %0
69+
%v1.0 = phi i32 [ 0, %entry ], [ %k.0, %if.end ]
70+
%cmp = icmp slt i32 %v1.0, 6
71+
br i1 %cmp, label %for.body, label %exit
72+
73+
for.body: ; preds = %1
74+
%cmp3 = icmp slt i32 %v1.0, 2
75+
br i1 %cmp3, label %if.then, label %if.else
76+
77+
if.then: ; preds = %2
78+
%add = add nuw i32 %v1.0, 1
79+
br label %if.end
80+
81+
if.else: ; preds = %2
82+
%add6 = add nsw i32 %v1.0, 1
83+
br label %if.end
84+
85+
if.end: ; preds = %4, %3
86+
%k.0 = phi i32 [ %add, %if.then ], [ %add6, %if.else ]
87+
br label %for.cond
88+
89+
exit: ; preds = %5
90+
ret void
91+
}
92+

0 commit comments

Comments
 (0)