@@ -122,6 +122,10 @@ static cl::opt<bool>
122122AllowIVWidening (" indvars-widen-indvars" , cl::Hidden, cl::init(true ),
123123 cl::desc(" Allow widening of indvars to eliminate s/zext" ));
124124
125+ static cl::opt<bool > EnableAlmostDeadCounterCheck (
126+ " enable-almost-dead-counter-check" , cl::Hidden, cl::init(false ),
127+ cl::desc(" Enable almost dead LoopCounter check." ));
128+
125129namespace {
126130
127131class IndVarSimplify {
@@ -160,6 +164,13 @@ class IndVarSimplify {
160164
161165 bool sinkUnusedInvariants (Loop *L);
162166
167+ PHINode *findCandidateLoopCounter (Loop *L, PHINode *LoopCounter,
168+ const SCEV *ExitCount, ICmpInst *Cond,
169+ ScalarEvolution *SE);
170+
171+ bool tryToReplacePureLoopCounter (Loop *L, ScalarEvolution *SE,
172+ SCEVExpander &Rewriter, LoopInfo *LI);
173+
163174public:
164175 IndVarSimplify (LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
165176 const DataLayout &DL, TargetLibraryInfo *TLI,
@@ -808,7 +819,13 @@ static bool isLoopCounter(PHINode* Phi, Loop *L,
808819 return false ;
809820
810821 const SCEV *S = SE->getSCEV (Phi);
811- if (!match (S, m_scev_AffineAddRec (m_SCEV (), m_scev_One (), m_SpecificLoop (L))))
822+ const SCEVConstant *Step;
823+ if (!match (S, m_scev_AffineAddRec (m_SCEV (), m_SCEVConstant (Step),
824+ m_SpecificLoop (L))))
825+ return false ;
826+ int64_t StepVal = Step->getValue ()->getSExtValue ();
827+ // Require that the loop counter stride can only be 1 or -1
828+ if (StepVal != 1 && StepVal != -1 )
812829 return false ;
813830
814831 int LatchIdx = Phi->getBasicBlockIndex (L->getLoopLatch ());
@@ -910,7 +927,8 @@ static Value *genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB,
910927 assert (isLoopCounter (IndVar, L, SE));
911928 assert (ExitCount->getType ()->isIntegerTy () && " exit count must be integer" );
912929 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV (IndVar));
913- assert (AR->getStepRecurrence (*SE)->isOne () && " only handles unit stride" );
930+ const SCEV *StepAbs = SE->getAbsExpr (AR->getStepRecurrence (*SE), true );
931+ assert (StepAbs->isOne () && " only handles unit stride" );
914932
915933 // For integer IVs, truncate the IV before computing the limit unless we
916934 // know apriori that the limit must be a constant when evaluated in the
@@ -1870,6 +1888,133 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
18701888 return Changed;
18711889}
18721890
1891+
1892+ // / Look for a PHI node from the loop header that
1893+ // / 1. is a candidate inductive variable and passes the isLoopCounter() check;
1894+ // / 2. has an integer type; has a width that is not less than ExitCount;
1895+ // / 3. is a live node.
1896+ PHINode *IndVarSimplify::findCandidateLoopCounter (Loop *L, PHINode *LoopCounter,
1897+ const SCEV *ExitCount,
1898+ ICmpInst *Cond,
1899+ ScalarEvolution *SE) {
1900+
1901+ PHINode *CandidateCounter = nullptr ;
1902+ unsigned ExitCountWidth = SE->getTypeSizeInBits (ExitCount->getType ());
1903+
1904+ // Look for another IV that can serve as a LoopCounter.
1905+ for (PHINode &AuxPHI : L->getHeader ()->phis ()) {
1906+
1907+ unsigned PhiWidth;
1908+
1909+ // Require that the candidate IV is of integer type
1910+ if (AuxPHI.getType ()->isIntegerTy ())
1911+ PhiWidth = SE->getTypeSizeInBits (AuxPHI.getType ());
1912+ else
1913+ continue ;
1914+
1915+ if (L->isAuxiliaryInductionVariable (AuxPHI, *SE) &&
1916+ &AuxPHI != LoopCounter && isLoopCounter (&AuxPHI, L, SE) &&
1917+ // Do not worry; if the width of the LoopCounter is
1918+ // greater than ExitCount, LTFR will sign/zero extend ExitCount.
1919+ PhiWidth >= ExitCountWidth && DL.isLegalInteger (PhiWidth) &&
1920+ !isAlmostDeadIV (&AuxPHI, L->getLoopLatch (), Cond)) {
1921+
1922+ CandidateCounter = &AuxPHI;
1923+ break ;
1924+ }
1925+ }
1926+
1927+ return CandidateCounter;
1928+ }
1929+
1930+
1931+ // / We define a PHI node as the Pure LoopCounter if it meets these two conditions:
1932+ // / 1. it is used for loop termination testing;
1933+ // / 2. it has at most two uses, PostAdd and CMP, with the latter being optional.
1934+ // /
1935+ // / When the Pure loop counter is selected as the loop termination condition,
1936+ // / we will try to find a candidate loop counter to take over its counting function
1937+ // / such as:
1938+ // loop:
1939+ // / %3 = phi i64 [ %10, %loop ], [ %2, %entry ]
1940+ // / %4 = phi i64 [ %9, %loop ], [ 1, %entry ]
1941+ // / ... ; Here, %4 is used to calculate array indices.
1942+ // / %9 = add i64 %4, 1
1943+ // / %10 = add nsw i64 %3, -1
1944+ // / %11 = icmp ugt i64 %3, 1
1945+ // / br i1 %11, label %loop, label %end
1946+ // / After replacing the Pure LoopCounter:
1947+ // / loop:
1948+ // / %4 = phi i64 [ %9, %loop ], [ 1, %entry ]
1949+ // / ...
1950+ // / %9 = add i64 %4, 1
1951+ // / %11 = icmp ne i64 %9, 11
1952+ // / br i1 %11, label %loop, label %end
1953+ // /
1954+ bool IndVarSimplify::tryToReplacePureLoopCounter (Loop *L, ScalarEvolution *SE,
1955+ SCEVExpander &Rewriter,
1956+ LoopInfo *LI) {
1957+ bool Changed = false ;
1958+
1959+ SmallVector<BasicBlock *, 16 > ExitingBlocks;
1960+ L->getExitingBlocks (ExitingBlocks);
1961+ if (ExitingBlocks.empty ())
1962+ return false ;
1963+
1964+ for (BasicBlock *ExitingBB : ExitingBlocks) {
1965+ // Can't handle non-branch yet.
1966+ if (!isa<BranchInst>(ExitingBB->getTerminator ()))
1967+ continue ;
1968+ BranchInst *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator ());
1969+ if (!BI || BI->isUnconditional ())
1970+ continue ;
1971+
1972+ // Check the Compare 检查整数比较 输出信息 LLVM_Ddg()
1973+ ICmpInst *Cond = dyn_cast<ICmpInst>(BI->getCondition ());
1974+ if (!Cond)
1975+ continue ;
1976+
1977+ // If our exitting block exits multiple loops, we can only rewrite the
1978+ // innermost one. Otherwise, we're changing how many times the innermost
1979+ // loop runs before it exits.
1980+ if (LI->getLoopFor (ExitingBB) != L)
1981+ continue ;
1982+
1983+ // Get the number of times the backedge executes before the given
1984+ // exit would be taken;
1985+ const SCEV *ExitCount = SE->getExitCount (L, ExitingBB);
1986+ if (isa<SCEVCouldNotCompute>(ExitCount) || ExitCount->isZero () ||
1987+ !Rewriter.isSafeToExpand (ExitCount))
1988+ continue ;
1989+
1990+ // Let FindLoopCounter() select the loop counter first.
1991+ PHINode *FoundCounter = FindLoopCounter (L, ExitingBB, ExitCount, SE, DT);
1992+ if (!FoundCounter)
1993+ continue ;
1994+
1995+ Value *PostAdd = FoundCounter->getIncomingValueForBlock (L->getLoopLatch ());
1996+ // If it selects the Pure LoopCounter
1997+ if ((isLoopExitTestBasedOn (FoundCounter, ExitingBB) ||
1998+ isLoopExitTestBasedOn (PostAdd, ExitingBB)) &&
1999+ !hasConcreteDef (FoundCounter) &&
2000+ // isAlmostDeadIV()
2001+ isAlmostDeadIV (FoundCounter, L->getLoopLatch (), Cond)) {
2002+
2003+ // Try to find other suitable IV as the new LoopCounter
2004+ PHINode *CandidateCounter =
2005+ findCandidateLoopCounter (L, FoundCounter, ExitCount, Cond, SE);
2006+ if (!CandidateCounter)
2007+ continue ;
2008+
2009+ // Let this newly found candidate LoopCounter perform LFTR
2010+ Changed |= linearFunctionTestReplace (L, ExitingBB, ExitCount,
2011+ CandidateCounter, Rewriter);
2012+ }
2013+ }
2014+
2015+ return Changed;
2016+ }
2017+
18732018// ===----------------------------------------------------------------------===//
18742019// IndVarSimplify driver. Manage several subpasses of IV simplification.
18752020// ===----------------------------------------------------------------------===//
@@ -1983,18 +2128,23 @@ bool IndVarSimplify::run(Loop *L) {
19832128 if (!IndVar)
19842129 continue ;
19852130
1986- // Avoid high cost expansions. Note: This heuristic is questionable in
1987- // that our definition of "high cost" is not exactly principled.
1988- if (Rewriter.isHighCostExpansion (ExitCount, L, SCEVCheapExpansionBudget,
1989- TTI, PreHeader->getTerminator ()))
1990- continue ;
2131+ if (EnableAlmostDeadCounterCheck)
2132+ Changed |= tryToReplacePureLoopCounter (L, SE, Rewriter, LI);
2133+ else {
2134+ // Avoid high cost expansions. Note: This heuristic is questionable in
2135+ // that our definition of "high cost" is not exactly principled.
2136+ if (Rewriter.isHighCostExpansion (ExitCount, L, SCEVCheapExpansionBudget,
2137+ TTI, PreHeader->getTerminator ()))
2138+ continue ;
19912139
1992- if (!Rewriter.isSafeToExpand (ExitCount))
1993- continue ;
2140+ if (!Rewriter.isSafeToExpand (ExitCount))
2141+ continue ;
19942142
1995- Changed |= linearFunctionTestReplace (L, ExitingBB,
1996- ExitCount, IndVar,
1997- Rewriter);
2143+ else
2144+ Changed |= linearFunctionTestReplace (L, ExitingBB,
2145+ ExitCount, IndVar,
2146+ Rewriter);
2147+ }
19982148 }
19992149 }
20002150 // Clear the rewriter cache, because values that are in the rewriter's cache
0 commit comments