/*========================== begin_copyright_notice ============================ Copyright (C) 2023-2024 Intel Corporation SPDX-License-Identifier: MIT ============================= end_copyright_notice ===========================*/ /*========================== begin_copyright_notice ============================ Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information. SPDX-License-Identifier: Apache-2.0 with LLVM-exception ============================= end_copyright_notice ===========================*/ # TODO: Once upstreamed, update with LLORG revision & adjust per community review diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index e765a4d54..8712103b5 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -5576,12 +5576,14 @@ const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) { // First, try to find AddRec expression without creating a fictituos symbolic // value for PN. - if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV)) - return S; + // if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV)) + // return S; // Handle PHI node value symbolically. - const SCEV *SymbolicName = getUnknown(PN); - insertValueToMap(PN, SymbolicName); + // const SCEV *SymbolicName = getUnknown(PN); + // insertValueToMap(PN, SymbolicName); + const SCEV *SymbolicName = getExistingSCEV(PN); + assert(SymbolicName); // Using this symbolic name for the PHI, analyze the value coming around // the back-edge. @@ -5789,6 +5791,8 @@ static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S, // match. static bool BrPHIToSelect(DominatorTree &DT, PHINode *Merge, Value *&C, Value *&LHS, Value *&RHS) { + if (Merge->getNumIncomingValues() != 2) + return false; BasicBlock *IDom = DT[Merge->getParent()]->getIDom()->getBlock(); assert(IDom && "At least the entry block should dominate PN"); @@ -7094,14 +7098,13 @@ const SCEV *ScalarEvolution::createSCEVIter(Value *V) { using PointerTy = PointerIntPair; SmallVector Stack; - Stack.emplace_back(V, true); Stack.emplace_back(V, false); while (!Stack.empty()) { auto E = Stack.pop_back_val(); Value *CurV = E.getPointer(); - if (getExistingSCEV(CurV)) + if (getExistingSCEV(CurV) && !E.getInt()) continue; SmallVector Ops; @@ -7118,7 +7121,7 @@ const SCEV *ScalarEvolution::createSCEVIter(Value *V) { if (CreatedSCEV) insertValueToMap(CurV, CreatedSCEV); - else { + if (!Ops.empty() || !CreatedSCEV) { // Queue CurV for SCEV creation, followed by its's operands which need to // be constructed first. Stack.emplace_back(CurV, true); @@ -7249,20 +7252,43 @@ ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl &Ops) { case Instruction::PHI: { PHINode *PN = cast(U); + // This phi's operands must be attached due to range computations even if it is redundant to determine its SCEV expression. + for (auto &Op : PN->operands()) + Ops.push_back(Op); + + // Check if it can be classified as a special scalar evolution expression like: + // * addrec, + // * phi-like-select. if (PN->getNumIncomingValues() == 2) { std::pair StartAndBEValueV = getStartAndBackEdgeValues(PN); - if (StartAndBEValueV.first && StartAndBEValueV.second) - // resolve recursively - may create cycles + Value *BEValueV = StartAndBEValueV.second, + *StartValueV = StartAndBEValueV.first; + if (BEValueV && StartValueV) + // This phi instruction is initially defined as undefined and can be + // replaced with an addrec expression if it meets requirements. + // This may create cycles. + return getUnknown(V); + + if (BrPHIToSelect(DT, PN)) + // This phi instruction might be classified as a phi-like-select + // expression. Any initial definition cannot be applied since this + // pattern doesn't support such a path as dedicated to addrec + // expressions. return nullptr; - else if (BrPHIToSelect(DT, PN)) - // resolve iteratively - does not create cycles - for (auto &Op : PN->operands()) - Ops.push_back(Op); } - // resolve iteratively - does not create SCEV cycles (trivially resolved) - return nullptr; + // This means that this phi instruction has one common input value. + // This cannot create cycles due to SSA properties (an usage of the value + // cannot be before its definition and this leads to a conclusion that it must + // dominate over this phi instruction to meet the initial assumptions). + if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC})) + if (LI.replacementPreservesLCSSAForm(PN, V)) + return nullptr; + + // This phi instruction is classified as undefined due to + // an unsupported pattern. This may create cycles. + return getUnknown(V); } case Instruction::Select: {