Skip to content

Commit 636aafb

Browse files
krystian-andrzejewskiigcbot
authored andcommitted
Support simplification of phi instruction in ScalarEvolution
This change is to extend supported patterns for creating SCEVs iteratively in ScalarEvolution (to reflect the recursive algorithm exactly). These patterns are related to one common source value of the phi instruction and this can be resolved from this value.
1 parent 2e18a61 commit 636aafb

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

external/llvm/releases/14.0.0/patches_external/0003-ScalarEvolutionStackOverflowWindows.patch

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SPDX-License-Identifier: Apache-2.0 with LLVM-exception
1717
# TODO: Once upstreamed, update with LLORG revision & adjust per community review
1818

1919
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
20-
index e765a4d54..518683c29 100644
20+
index e765a4d54..8712103b5 100644
2121
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
2222
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
2323
@@ -5576,12 +5576,14 @@ const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
@@ -73,37 +73,54 @@ index e765a4d54..518683c29 100644
7373
// Queue CurV for SCEV creation, followed by its's operands which need to
7474
// be constructed first.
7575
Stack.emplace_back(CurV, true);
76-
@@ -7248,21 +7251,19 @@ ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops) {
77-
76+
@@ -7249,20 +7252,43 @@ ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops) {
7877
case Instruction::PHI: {
7978
PHINode *PN = cast<PHINode>(U);
80-
-
81-
- if (PN->getNumIncomingValues() == 2) {
82-
- std::pair<Value *, Value *> StartAndBEValueV =
83-
- getStartAndBackEdgeValues(PN);
79+
80+
+ // This phi's operands must be attached due to range computations even if it is redundant to determine its SCEV expression.
81+
+ for (auto &Op : PN->operands())
82+
+ Ops.push_back(Op);
83+
+
84+
+ // Check if it can be classified as a special scalar evolution expression like:
85+
+ // * addrec,
86+
+ // * phi-like-select.
87+
if (PN->getNumIncomingValues() == 2) {
88+
std::pair<Value *, Value *> StartAndBEValueV =
89+
getStartAndBackEdgeValues(PN);
8490
- if (StartAndBEValueV.first && StartAndBEValueV.second)
8591
- // resolve recursively - may create cycles
86-
- return nullptr;
92+
+ Value *BEValueV = StartAndBEValueV.second,
93+
+ *StartValueV = StartAndBEValueV.first;
94+
+ if (BEValueV && StartValueV)
95+
+ // This phi instruction is initially defined as undefined and can be
96+
+ // replaced with an addrec expression if it meets requirements.
97+
+ // This may create cycles.
98+
+ return getUnknown(V);
99+
+
100+
+ if (BrPHIToSelect(DT, PN))
101+
+ // This phi instruction might be classified as a phi-like-select
102+
+ // expression. Any initial definition cannot be applied since this
103+
+ // pattern doesn't support such a path as dedicated to addrec
104+
+ // expressions.
105+
return nullptr;
87106
- else if (BrPHIToSelect(DT, PN))
88107
- // resolve iteratively - does not create cycles
89108
- for (auto &Op : PN->operands())
90109
- Ops.push_back(Op);
91-
- }
92-
-
110+
}
111+
93112
- // resolve iteratively - does not create SCEV cycles (trivially resolved)
94113
- return nullptr;
95-
+ // This phi's operands must be attached due to range computations even if it is redundant
96-
+ // to determine its SCEV expression.
97-
+ for (auto &Op : PN->operands())
98-
+ Ops.push_back(Op);
99-
+ if (BrPHIToSelect(DT, PN))
100-
+ // This phi instruction might be classified as a phi-like-select expression.
101-
+ // Any initial definition cannot be applied since this pattern doesn't support
102-
+ // such a path as dedicated to addrec expressions.
103-
+ return nullptr;
104-
+ // This phi instruction is initially defined as undefined and can be replaced
105-
+ // with an addrec expression if it meets requirements. Otherwise,
106-
+ // this will be preserved.
114+
+ // This means that this phi instruction has one common input value.
115+
+ // This cannot create cycles due to SSA properties (an usage of the value
116+
+ // cannot be before its definition and this leads to a conclusion that it must
117+
+ // dominate over this phi instruction to meet the initial assumptions).
118+
+ if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC}))
119+
+ if (LI.replacementPreservesLCSSAForm(PN, V))
120+
+ return nullptr;
121+
+
122+
+ // This phi instruction is classified as undefined due to
123+
+ // an unsupported pattern. This may create cycles.
107124
+ return getUnknown(V);
108125
}
109126

0 commit comments

Comments
 (0)