| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2023-2024 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
/*========================== begin_copyright_notice ============================ |
| 10 |
|
| 11 |
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
See https://llvm.org/LICENSE.txt for license information. |
| 13 |
SPDX-License-Identifier: Apache-2.0 with LLVM-exception |
| 14 |
|
| 15 |
============================= end_copyright_notice ===========================*/ |
| 16 |
|
| 17 |
# TODO: Once upstreamed, update with LLORG revision & adjust per community review |
| 18 |
|
| 19 |
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp |
| 20 |
index e765a4d54..8712103b5 100644 |
| 21 |
--- a/llvm/lib/Analysis/ScalarEvolution.cpp |
| 22 |
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp |
| 23 |
@@ -5576,12 +5576,14 @@ const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) { |
| 24 |
|
| 25 |
// First, try to find AddRec expression without creating a fictituos symbolic |
| 26 |
// value for PN. |
| 27 |
- if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV)) |
| 28 |
- return S; |
| 29 |
+ // if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV)) |
| 30 |
+ // return S; |
| 31 |
|
| 32 |
// Handle PHI node value symbolically. |
| 33 |
- const SCEV *SymbolicName = getUnknown(PN); |
| 34 |
- insertValueToMap(PN, SymbolicName); |
| 35 |
+ // const SCEV *SymbolicName = getUnknown(PN); |
| 36 |
+ // insertValueToMap(PN, SymbolicName); |
| 37 |
+ const SCEV *SymbolicName = getExistingSCEV(PN); |
| 38 |
+ assert(SymbolicName); |
| 39 |
|
| 40 |
// Using this symbolic name for the PHI, analyze the value coming around |
| 41 |
// the back-edge. |
| 42 |
@@ -5789,6 +5791,8 @@ static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S, |
| 43 |
// match. |
| 44 |
static bool BrPHIToSelect(DominatorTree &DT, PHINode *Merge, Value *&C, |
| 45 |
Value *&LHS, Value *&RHS) { |
| 46 |
+ if (Merge->getNumIncomingValues() != 2) |
| 47 |
+ return false; |
| 48 |
|
| 49 |
BasicBlock *IDom = DT[Merge->getParent()]->getIDom()->getBlock(); |
| 50 |
assert(IDom && "At least the entry block should dominate PN"); |
| 51 |
@@ -7094,14 +7098,13 @@ const SCEV *ScalarEvolution::createSCEVIter(Value *V) { |
| 52 |
using PointerTy = PointerIntPair<Value *, 1, bool>; |
| 53 |
SmallVector<PointerTy> Stack; |
| 54 |
|
| 55 |
- Stack.emplace_back(V, true); |
| 56 |
Stack.emplace_back(V, false); |
| 57 |
|
| 58 |
while (!Stack.empty()) { |
| 59 |
auto E = Stack.pop_back_val(); |
| 60 |
Value *CurV = E.getPointer(); |
| 61 |
|
| 62 |
- if (getExistingSCEV(CurV)) |
| 63 |
+ if (getExistingSCEV(CurV) && !E.getInt()) |
| 64 |
continue; |
| 65 |
|
| 66 |
SmallVector<Value *> Ops; |
| 67 |
@@ -7118,7 +7121,7 @@ const SCEV *ScalarEvolution::createSCEVIter(Value *V) { |
| 68 |
|
| 69 |
if (CreatedSCEV) |
| 70 |
insertValueToMap(CurV, CreatedSCEV); |
| 71 |
- else { |
| 72 |
+ if (!Ops.empty() || !CreatedSCEV) { |
| 73 |
// Queue CurV for SCEV creation, followed by its's operands which need to |
| 74 |
// be constructed first. |
| 75 |
Stack.emplace_back(CurV, true); |
| 76 |
@@ -7249,20 +7252,43 @@ ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops) { |
| 77 |
case Instruction::PHI: { |
| 78 |
PHINode *PN = cast<PHINode>(U); |
| 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); |
| 90 |
- if (StartAndBEValueV.first && StartAndBEValueV.second) |
| 91 |
- // resolve recursively - may create cycles |
| 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; |
| 106 |
- else if (BrPHIToSelect(DT, PN)) |
| 107 |
- // resolve iteratively - does not create cycles |
| 108 |
- for (auto &Op : PN->operands()) |
| 109 |
- Ops.push_back(Op); |
| 110 |
} |
| 111 |
|
| 112 |
- // resolve iteratively - does not create SCEV cycles (trivially resolved) |
| 113 |
- return nullptr; |
| 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. |
| 124 |
+ return getUnknown(V); |
| 125 |
} |
| 126 |
|
| 127 |
case Instruction::Select: { |