@@ -17,7 +17,7 @@ SPDX-License-Identifier: Apache-2.0 with LLVM-exception
17
17
# TODO: Once upstreamed, update with LLORG revision & adjust per community review
18
18
19
19
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
20
- index e765a4d54..518683c29 100644
20
+ index e765a4d54..8712103b5 100644
21
21
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
22
22
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
23
23
@@ -5576,12 +5576,14 @@ const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
@@ -73,37 +73,54 @@ index e765a4d54..518683c29 100644
73
73
// Queue CurV for SCEV creation, followed by its's operands which need to
74
74
// be constructed first.
75
75
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) {
78
77
case Instruction::PHI: {
79
78
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);
84
90
- if (StartAndBEValueV.first && StartAndBEValueV.second)
85
91
- // 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;
87
106
- else if (BrPHIToSelect(DT, PN))
88
107
- // resolve iteratively - does not create cycles
89
108
- for (auto &Op : PN->operands())
90
109
- Ops.push_back(Op);
91
- - }
92
- -
110
+ }
111
+
93
112
- // resolve iteratively - does not create SCEV cycles (trivially resolved)
94
113
- 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.
107
124
+ return getUnknown(V);
108
125
}
109
126
0 commit comments