1 //===- LoopFlatten.cpp - Loop flattening pass------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass flattens pairs nested loops into a single loop.
11 // The intention is to optimise loop nests like this, which together access an
13 // for (int i = 0; i < N; ++i)
14 // for (int j = 0; j < M; ++j)
17 // for (int i = 0; i < (N*M); ++i)
20 // It can also flatten loops where the induction variables are not used in the
21 // loop. This is only worth doing if the induction variables are only used in an
22 // expression like i*M+j. If they had any other uses, we would have to insert a
23 // div/mod to reconstruct the original values, so this wouldn't be profitable.
25 // We also need to prove that N*M will not overflow.
27 //===----------------------------------------------------------------------===//
29 #include "llvm/Transforms/Scalar/LoopFlatten.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Analysis/AssumptionCache.h"
33 #include "llvm/Analysis/LoopInfo.h"
34 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
35 #include "llvm/Analysis/ScalarEvolution.h"
36 #include "llvm/Analysis/TargetTransformInfo.h"
37 #include "llvm/Analysis/ValueTracking.h"
38 #include "llvm/IR/Dominators.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/IRBuilder.h"
41 #include "llvm/IR/Module.h"
42 #include "llvm/IR/PatternMatch.h"
43 #include "llvm/IR/Verifier.h"
44 #include "llvm/InitializePasses.h"
45 #include "llvm/Pass.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include "llvm/Transforms/Scalar.h"
49 #include "llvm/Transforms/Utils/Local.h"
50 #include "llvm/Transforms/Utils/LoopUtils.h"
51 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
52 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
55 using namespace llvm::PatternMatch;
57 #define DEBUG_TYPE "loop-flatten"
59 STATISTIC(NumFlattened, "Number of loops flattened");
61 static cl::opt<unsigned> RepeatedInstructionThreshold(
62 "loop-flatten-cost-threshold", cl::Hidden, cl::init(2),
63 cl::desc("Limit on the cost of instructions that can be repeated due to "
67 AssumeNoOverflow("loop-flatten-assume-no-overflow", cl::Hidden,
69 cl::desc("Assume that the product of the two iteration "
70 "trip counts will never overflow"));
73 WidenIV("loop-flatten-widen-iv", cl::Hidden,
75 cl::desc("Widen the loop induction variables, if possible, so "
76 "overflow checks won't reject flattening"));
79 Loop *OuterLoop = nullptr;
80 Loop *InnerLoop = nullptr;
81 // These PHINodes correspond to loop induction variables, which are expected
82 // to start at zero and increment by one on each loop.
83 PHINode *InnerInductionPHI = nullptr;
84 PHINode *OuterInductionPHI = nullptr;
85 Value *InnerTripCount = nullptr;
86 Value *OuterTripCount = nullptr;
87 BinaryOperator *InnerIncrement = nullptr;
88 BinaryOperator *OuterIncrement = nullptr;
89 BranchInst *InnerBranch = nullptr;
90 BranchInst *OuterBranch = nullptr;
91 SmallPtrSet<Value *, 4> LinearIVUses;
92 SmallPtrSet<PHINode *, 4> InnerPHIsToTransform;
94 // Whether this holds the flatten info before or after widening.
97 // Holds the old/narrow induction phis, i.e. the Phis before IV widening has
98 // been applied. This bookkeeping is used so we can skip some checks on these
100 PHINode *NarrowInnerInductionPHI = nullptr;
101 PHINode *NarrowOuterInductionPHI = nullptr;
103 FlattenInfo(Loop *OL, Loop *IL) : OuterLoop(OL), InnerLoop(IL) {};
105 bool isNarrowInductionPhi(PHINode *Phi) {
106 // This can't be the narrow phi if we haven't widened the IV first.
109 return NarrowInnerInductionPHI == Phi || NarrowOuterInductionPHI == Phi;
114 setLoopComponents(Value *&TC, Value *&TripCount, BinaryOperator *&Increment,
115 SmallPtrSetImpl<Instruction *> &IterationInstructions) {
117 IterationInstructions.insert(Increment);
118 LLVM_DEBUG(dbgs() << "Found Increment: "; Increment->dump());
119 LLVM_DEBUG(dbgs() << "Found trip count: "; TripCount->dump());
120 LLVM_DEBUG(dbgs() << "Successfully found all loop components\n");
124 // Finds the induction variable, increment and trip count for a simple loop that
126 static bool findLoopComponents(
127 Loop *L, SmallPtrSetImpl<Instruction *> &IterationInstructions,
128 PHINode *&InductionPHI, Value *&TripCount, BinaryOperator *&Increment,
129 BranchInst *&BackBranch, ScalarEvolution *SE, bool IsWidened) {
130 LLVM_DEBUG(dbgs() << "Finding components of loop: " << L->getName() << "\n");
132 if (!L->isLoopSimplifyForm()) {
133 LLVM_DEBUG(dbgs() << "Loop is not in normal form\n");
137 // Currently, to simplify the implementation, the Loop induction variable must
138 // start at zero and increment with a step size of one.
139 if (!L->isCanonical(*SE)) {
140 LLVM_DEBUG(dbgs() << "Loop is not canonical\n");
144 // There must be exactly one exiting block, and it must be the same at the
146 BasicBlock *Latch = L->getLoopLatch();
147 if (L->getExitingBlock() != Latch) {
148 LLVM_DEBUG(dbgs() << "Exiting and latch block are different\n");
152 // Find the induction PHI. If there is no induction PHI, we can't do the
153 // transformation. TODO: could other variables trigger this? Do we have to
154 // search for the best one?
155 InductionPHI = L->getInductionVariable(*SE);
157 LLVM_DEBUG(dbgs() << "Could not find induction PHI\n");
160 LLVM_DEBUG(dbgs() << "Found induction PHI: "; InductionPHI->dump());
162 bool ContinueOnTrue = L->contains(Latch->getTerminator()->getSuccessor(0));
163 auto IsValidPredicate = [&](ICmpInst::Predicate Pred) {
165 return Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT;
167 return Pred == CmpInst::ICMP_EQ;
170 // Find Compare and make sure it is valid. getLatchCmpInst checks that the
171 // back branch of the latch is conditional.
172 ICmpInst *Compare = L->getLatchCmpInst();
173 if (!Compare || !IsValidPredicate(Compare->getUnsignedPredicate()) ||
174 Compare->hasNUsesOrMore(2)) {
175 LLVM_DEBUG(dbgs() << "Could not find valid comparison\n");
178 BackBranch = cast<BranchInst>(Latch->getTerminator());
179 IterationInstructions.insert(BackBranch);
180 LLVM_DEBUG(dbgs() << "Found back branch: "; BackBranch->dump());
181 IterationInstructions.insert(Compare);
182 LLVM_DEBUG(dbgs() << "Found comparison: "; Compare->dump());
184 // Find increment and trip count.
185 // There are exactly 2 incoming values to the induction phi; one from the
186 // pre-header and one from the latch. The incoming latch value is the
187 // increment variable.
189 dyn_cast<BinaryOperator>(InductionPHI->getIncomingValueForBlock(Latch));
190 if (Increment->hasNUsesOrMore(3)) {
191 LLVM_DEBUG(dbgs() << "Could not find valid increment\n");
194 // The trip count is the RHS of the compare. If this doesn't match the trip
195 // count computed by SCEV then this is because the trip count variable
196 // has been widened so the types don't match, or because it is a constant and
197 // another transformation has changed the compare (e.g. icmp ult %inc,
198 // tripcount -> icmp ult %j, tripcount-1), or both.
199 Value *RHS = Compare->getOperand(1);
200 const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
201 if (isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
202 LLVM_DEBUG(dbgs() << "Backedge-taken count is not predictable\n");
205 // The use of the Extend=false flag on getTripCountFromExitCount was added
206 // during a refactoring to preserve existing behavior. However, there's
207 // nothing obvious in the surrounding code when handles the overflow case.
208 // FIXME: audit code to establish whether there's a latent bug here.
209 const SCEV *SCEVTripCount =
210 SE->getTripCountFromExitCount(BackedgeTakenCount, false);
211 const SCEV *SCEVRHS = SE->getSCEV(RHS);
212 if (SCEVRHS == SCEVTripCount)
213 return setLoopComponents(RHS, TripCount, Increment, IterationInstructions);
214 ConstantInt *ConstantRHS = dyn_cast<ConstantInt>(RHS);
216 const SCEV *BackedgeTCExt = nullptr;
218 const SCEV *SCEVTripCountExt;
219 // Find the extended backedge taken count and extended trip count using
220 // SCEV. One of these should now match the RHS of the compare.
221 BackedgeTCExt = SE->getZeroExtendExpr(BackedgeTakenCount, RHS->getType());
222 SCEVTripCountExt = SE->getTripCountFromExitCount(BackedgeTCExt, false);
223 if (SCEVRHS != BackedgeTCExt && SCEVRHS != SCEVTripCountExt) {
224 LLVM_DEBUG(dbgs() << "Could not find valid trip count\n");
228 // If the RHS of the compare is equal to the backedge taken count we need
229 // to add one to get the trip count.
230 if (SCEVRHS == BackedgeTCExt || SCEVRHS == BackedgeTakenCount) {
231 ConstantInt *One = ConstantInt::get(ConstantRHS->getType(), 1);
232 Value *NewRHS = ConstantInt::get(
233 ConstantRHS->getContext(), ConstantRHS->getValue() + One->getValue());
234 return setLoopComponents(NewRHS, TripCount, Increment,
235 IterationInstructions);
237 return setLoopComponents(RHS, TripCount, Increment, IterationInstructions);
239 // If the RHS isn't a constant then check that the reason it doesn't match
240 // the SCEV trip count is because the RHS is a ZExt or SExt instruction
241 // (and take the trip count to be the RHS).
243 LLVM_DEBUG(dbgs() << "Could not find valid trip count\n");
246 auto *TripCountInst = dyn_cast<Instruction>(RHS);
247 if (!TripCountInst) {
248 LLVM_DEBUG(dbgs() << "Could not find valid trip count\n");
251 if ((!isa<ZExtInst>(TripCountInst) && !isa<SExtInst>(TripCountInst)) ||
252 SE->getSCEV(TripCountInst->getOperand(0)) != SCEVTripCount) {
253 LLVM_DEBUG(dbgs() << "Could not find valid extended trip count\n");
256 return setLoopComponents(RHS, TripCount, Increment, IterationInstructions);
259 static bool checkPHIs(FlattenInfo &FI, const TargetTransformInfo *TTI) {
260 // All PHIs in the inner and outer headers must either be:
261 // - The induction PHI, which we are going to rewrite as one induction in
262 // the new loop. This is already checked by findLoopComponents.
263 // - An outer header PHI with all incoming values from outside the loop.
264 // LoopSimplify guarantees we have a pre-header, so we don't need to
265 // worry about that here.
266 // - Pairs of PHIs in the inner and outer headers, which implement a
267 // loop-carried dependency that will still be valid in the new loop. To
268 // be valid, this variable must be modified only in the inner loop.
270 // The set of PHI nodes in the outer loop header that we know will still be
271 // valid after the transformation. These will not need to be modified (with
272 // the exception of the induction variable), but we do need to check that
273 // there are no unsafe PHI nodes.
274 SmallPtrSet<PHINode *, 4> SafeOuterPHIs;
275 SafeOuterPHIs.insert(FI.OuterInductionPHI);
277 // Check that all PHI nodes in the inner loop header match one of the valid
279 for (PHINode &InnerPHI : FI.InnerLoop->getHeader()->phis()) {
280 // The induction PHIs break these rules, and that's OK because we treat
281 // them specially when doing the transformation.
282 if (&InnerPHI == FI.InnerInductionPHI)
284 if (FI.isNarrowInductionPhi(&InnerPHI))
287 // Each inner loop PHI node must have two incoming values/blocks - one
288 // from the pre-header, and one from the latch.
289 assert(InnerPHI.getNumIncomingValues() == 2);
290 Value *PreHeaderValue =
291 InnerPHI.getIncomingValueForBlock(FI.InnerLoop->getLoopPreheader());
293 InnerPHI.getIncomingValueForBlock(FI.InnerLoop->getLoopLatch());
295 // The incoming value from the outer loop must be the PHI node in the
296 // outer loop header, with no modifications made in the top of the outer
298 PHINode *OuterPHI = dyn_cast<PHINode>(PreHeaderValue);
299 if (!OuterPHI || OuterPHI->getParent() != FI.OuterLoop->getHeader()) {
300 LLVM_DEBUG(dbgs() << "value modified in top of outer loop\n");
304 // The other incoming value must come from the inner loop, without any
305 // modifications in the tail end of the outer loop. We are in LCSSA form,
306 // so this will actually be a PHI in the inner loop's exit block, which
307 // only uses values from inside the inner loop.
308 PHINode *LCSSAPHI = dyn_cast<PHINode>(
309 OuterPHI->getIncomingValueForBlock(FI.OuterLoop->getLoopLatch()));
311 LLVM_DEBUG(dbgs() << "could not find LCSSA PHI\n");
315 // The value used by the LCSSA PHI must be the same one that the inner
317 if (LCSSAPHI->hasConstantValue() != LatchValue) {
319 dbgs() << "LCSSA PHI incoming value does not match latch value\n");
323 LLVM_DEBUG(dbgs() << "PHI pair is safe:\n");
324 LLVM_DEBUG(dbgs() << " Inner: "; InnerPHI.dump());
325 LLVM_DEBUG(dbgs() << " Outer: "; OuterPHI->dump());
326 SafeOuterPHIs.insert(OuterPHI);
327 FI.InnerPHIsToTransform.insert(&InnerPHI);
330 for (PHINode &OuterPHI : FI.OuterLoop->getHeader()->phis()) {
331 if (FI.isNarrowInductionPhi(&OuterPHI))
333 if (!SafeOuterPHIs.count(&OuterPHI)) {
334 LLVM_DEBUG(dbgs() << "found unsafe PHI in outer loop: "; OuterPHI.dump());
339 LLVM_DEBUG(dbgs() << "checkPHIs: OK\n");
344 checkOuterLoopInsts(FlattenInfo &FI,
345 SmallPtrSetImpl<Instruction *> &IterationInstructions,
346 const TargetTransformInfo *TTI) {
347 // Check for instructions in the outer but not inner loop. If any of these
348 // have side-effects then this transformation is not legal, and if there is
349 // a significant amount of code here which can't be optimised out that it's
350 // not profitable (as these instructions would get executed for each
351 // iteration of the inner loop).
352 InstructionCost RepeatedInstrCost = 0;
353 for (auto *B : FI.OuterLoop->getBlocks()) {
354 if (FI.InnerLoop->contains(B))
358 if (!isa<PHINode>(&I) && !I.isTerminator() &&
359 !isSafeToSpeculativelyExecute(&I)) {
360 LLVM_DEBUG(dbgs() << "Cannot flatten because instruction may have "
365 // The execution count of the outer loop's iteration instructions
366 // (increment, compare and branch) will be increased, but the
367 // equivalent instructions will be removed from the inner loop, so
368 // they make a net difference of zero.
369 if (IterationInstructions.count(&I))
371 // The uncoditional branch to the inner loop's header will turn into
372 // a fall-through, so adds no cost.
373 BranchInst *Br = dyn_cast<BranchInst>(&I);
374 if (Br && Br->isUnconditional() &&
375 Br->getSuccessor(0) == FI.InnerLoop->getHeader())
377 // Multiplies of the outer iteration variable and inner iteration
378 // count will be optimised out.
379 if (match(&I, m_c_Mul(m_Specific(FI.OuterInductionPHI),
380 m_Specific(FI.InnerTripCount))))
382 InstructionCost Cost =
383 TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency);
384 LLVM_DEBUG(dbgs() << "Cost " << Cost << ": "; I.dump());
385 RepeatedInstrCost += Cost;
389 LLVM_DEBUG(dbgs() << "Cost of instructions that will be repeated: "
390 << RepeatedInstrCost << "\n");
391 // Bail out if flattening the loops would cause instructions in the outer
392 // loop but not in the inner loop to be executed extra times.
393 if (RepeatedInstrCost > RepeatedInstructionThreshold) {
394 LLVM_DEBUG(dbgs() << "checkOuterLoopInsts: not profitable, bailing.\n");
398 LLVM_DEBUG(dbgs() << "checkOuterLoopInsts: OK\n");
402 static bool checkIVUsers(FlattenInfo &FI) {
403 // We require all uses of both induction variables to match this pattern:
405 // (OuterPHI * InnerTripCount) + InnerPHI
407 // Any uses of the induction variables not matching that pattern would
408 // require a div/mod to reconstruct in the flattened loop, so the
409 // transformation wouldn't be profitable.
411 Value *InnerTripCount = FI.InnerTripCount;
413 (isa<SExtInst>(InnerTripCount) || isa<ZExtInst>(InnerTripCount)))
414 InnerTripCount = cast<Instruction>(InnerTripCount)->getOperand(0);
416 // Check that all uses of the inner loop's induction variable match the
417 // expected pattern, recording the uses of the outer IV.
418 SmallPtrSet<Value *, 4> ValidOuterPHIUses;
419 for (User *U : FI.InnerInductionPHI->users()) {
420 if (U == FI.InnerIncrement)
423 // After widening the IVs, a trunc instruction might have been introduced,
424 // so look through truncs.
425 if (isa<TruncInst>(U)) {
428 U = *U->user_begin();
431 // If the use is in the compare (which is also the condition of the inner
432 // branch) then the compare has been altered by another transformation e.g
433 // icmp ult %inc, tripcount -> icmp ult %j, tripcount-1, where tripcount is
434 // a constant. Ignore this use as the compare gets removed later anyway.
435 if (U == FI.InnerBranch->getCondition())
438 LLVM_DEBUG(dbgs() << "Found use of inner induction variable: "; U->dump());
440 Value *MatchedMul = nullptr;
441 Value *MatchedItCount = nullptr;
442 bool IsAdd = match(U, m_c_Add(m_Specific(FI.InnerInductionPHI),
443 m_Value(MatchedMul))) &&
444 match(MatchedMul, m_c_Mul(m_Specific(FI.OuterInductionPHI),
445 m_Value(MatchedItCount)));
447 // Matches the same pattern as above, except it also looks for truncs
448 // on the phi, which can be the result of widening the induction variables.
450 match(U, m_c_Add(m_Trunc(m_Specific(FI.InnerInductionPHI)),
451 m_Value(MatchedMul))) &&
452 match(MatchedMul, m_c_Mul(m_Trunc(m_Specific(FI.OuterInductionPHI)),
453 m_Value(MatchedItCount)));
457 // Look through extends if the IV has been widened.
459 (isa<SExtInst>(MatchedItCount) || isa<ZExtInst>(MatchedItCount))) {
460 assert(MatchedItCount->getType() == FI.InnerInductionPHI->getType() &&
461 "Unexpected type mismatch in types after widening");
462 MatchedItCount = isa<SExtInst>(MatchedItCount)
463 ? dyn_cast<SExtInst>(MatchedItCount)->getOperand(0)
464 : dyn_cast<ZExtInst>(MatchedItCount)->getOperand(0);
467 if ((IsAdd || IsAddTrunc) && MatchedItCount == InnerTripCount) {
468 LLVM_DEBUG(dbgs() << "Use is optimisable\n");
469 ValidOuterPHIUses.insert(MatchedMul);
470 FI.LinearIVUses.insert(U);
472 LLVM_DEBUG(dbgs() << "Did not match expected pattern, bailing\n");
477 // Check that there are no uses of the outer IV other than the ones found
478 // as part of the pattern above.
479 for (User *U : FI.OuterInductionPHI->users()) {
480 if (U == FI.OuterIncrement)
483 auto IsValidOuterPHIUses = [&] (User *U) -> bool {
484 LLVM_DEBUG(dbgs() << "Found use of outer induction variable: "; U->dump());
485 if (!ValidOuterPHIUses.count(U)) {
486 LLVM_DEBUG(dbgs() << "Did not match expected pattern, bailing\n");
489 LLVM_DEBUG(dbgs() << "Use is optimisable\n");
493 if (auto *V = dyn_cast<TruncInst>(U)) {
494 for (auto *K : V->users()) {
495 if (!IsValidOuterPHIUses(K))
501 if (!IsValidOuterPHIUses(U))
505 LLVM_DEBUG(dbgs() << "checkIVUsers: OK\n";
506 dbgs() << "Found " << FI.LinearIVUses.size()
507 << " value(s) that can be replaced:\n";
508 for (Value *V : FI.LinearIVUses) {
515 // Return an OverflowResult dependant on if overflow of the multiplication of
516 // InnerTripCount and OuterTripCount can be assumed not to happen.
517 static OverflowResult checkOverflow(FlattenInfo &FI, DominatorTree *DT,
518 AssumptionCache *AC) {
519 Function *F = FI.OuterLoop->getHeader()->getParent();
520 const DataLayout &DL = F->getParent()->getDataLayout();
522 // For debugging/testing.
523 if (AssumeNoOverflow)
524 return OverflowResult::NeverOverflows;
526 // Check if the multiply could not overflow due to known ranges of the
528 OverflowResult OR = computeOverflowForUnsignedMul(
529 FI.InnerTripCount, FI.OuterTripCount, DL, AC,
530 FI.OuterLoop->getLoopPreheader()->getTerminator(), DT);
531 if (OR != OverflowResult::MayOverflow)
534 for (Value *V : FI.LinearIVUses) {
535 for (Value *U : V->users()) {
536 if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) {
537 for (Value *GEPUser : U->users()) {
538 Instruction *GEPUserInst = dyn_cast<Instruction>(GEPUser);
539 if (!isa<LoadInst>(GEPUserInst) &&
540 !(isa<StoreInst>(GEPUserInst) &&
541 GEP == GEPUserInst->getOperand(1)))
543 if (!isGuaranteedToExecuteForEveryIteration(GEPUserInst,
546 // The IV is used as the operand of a GEP which dominates the loop
547 // latch, and the IV is at least as wide as the address space of the
548 // GEP. In this case, the GEP would wrap around the address space
549 // before the IV increment wraps, which would be UB.
550 if (GEP->isInBounds() &&
551 V->getType()->getIntegerBitWidth() >=
552 DL.getPointerTypeSizeInBits(GEP->getType())) {
554 dbgs() << "use of linear IV would be UB if overflow occurred: ";
556 return OverflowResult::NeverOverflows;
563 return OverflowResult::MayOverflow;
566 static bool CanFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
567 ScalarEvolution *SE, AssumptionCache *AC,
568 const TargetTransformInfo *TTI) {
569 SmallPtrSet<Instruction *, 8> IterationInstructions;
570 if (!findLoopComponents(FI.InnerLoop, IterationInstructions,
571 FI.InnerInductionPHI, FI.InnerTripCount,
572 FI.InnerIncrement, FI.InnerBranch, SE, FI.Widened))
574 if (!findLoopComponents(FI.OuterLoop, IterationInstructions,
575 FI.OuterInductionPHI, FI.OuterTripCount,
576 FI.OuterIncrement, FI.OuterBranch, SE, FI.Widened))
579 // Both of the loop trip count values must be invariant in the outer loop
580 // (non-instructions are all inherently invariant).
581 if (!FI.OuterLoop->isLoopInvariant(FI.InnerTripCount)) {
582 LLVM_DEBUG(dbgs() << "inner loop trip count not invariant\n");
585 if (!FI.OuterLoop->isLoopInvariant(FI.OuterTripCount)) {
586 LLVM_DEBUG(dbgs() << "outer loop trip count not invariant\n");
590 if (!checkPHIs(FI, TTI))
593 // FIXME: it should be possible to handle different types correctly.
594 if (FI.InnerInductionPHI->getType() != FI.OuterInductionPHI->getType())
597 if (!checkOuterLoopInsts(FI, IterationInstructions, TTI))
600 // Find the values in the loop that can be replaced with the linearized
601 // induction variable, and check that there are no other uses of the inner
602 // or outer induction variable. If there were, we could still do this
603 // transformation, but we'd have to insert a div/mod to calculate the
604 // original IVs, so it wouldn't be profitable.
605 if (!checkIVUsers(FI))
608 LLVM_DEBUG(dbgs() << "CanFlattenLoopPair: OK\n");
612 static bool DoFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
613 ScalarEvolution *SE, AssumptionCache *AC,
614 const TargetTransformInfo *TTI, LPMUpdater *U) {
615 Function *F = FI.OuterLoop->getHeader()->getParent();
616 LLVM_DEBUG(dbgs() << "Checks all passed, doing the transformation\n");
619 OptimizationRemark Remark(DEBUG_TYPE, "Flattened", FI.InnerLoop->getStartLoc(),
620 FI.InnerLoop->getHeader());
621 OptimizationRemarkEmitter ORE(F);
622 Remark << "Flattened into outer loop";
626 Value *NewTripCount = BinaryOperator::CreateMul(
627 FI.InnerTripCount, FI.OuterTripCount, "flatten.tripcount",
628 FI.OuterLoop->getLoopPreheader()->getTerminator());
629 LLVM_DEBUG(dbgs() << "Created new trip count in preheader: ";
630 NewTripCount->dump());
632 // Fix up PHI nodes that take values from the inner loop back-edge, which
633 // we are about to remove.
634 FI.InnerInductionPHI->removeIncomingValue(FI.InnerLoop->getLoopLatch());
636 // The old Phi will be optimised away later, but for now we can't leave
637 // leave it in an invalid state, so are updating them too.
638 for (PHINode *PHI : FI.InnerPHIsToTransform)
639 PHI->removeIncomingValue(FI.InnerLoop->getLoopLatch());
641 // Modify the trip count of the outer loop to be the product of the two
643 cast<User>(FI.OuterBranch->getCondition())->setOperand(1, NewTripCount);
645 // Replace the inner loop backedge with an unconditional branch to the exit.
646 BasicBlock *InnerExitBlock = FI.InnerLoop->getExitBlock();
647 BasicBlock *InnerExitingBlock = FI.InnerLoop->getExitingBlock();
648 InnerExitingBlock->getTerminator()->eraseFromParent();
649 BranchInst::Create(InnerExitBlock, InnerExitingBlock);
650 DT->deleteEdge(InnerExitingBlock, FI.InnerLoop->getHeader());
652 // Replace all uses of the polynomial calculated from the two induction
653 // variables with the one new one.
654 IRBuilder<> Builder(FI.OuterInductionPHI->getParent()->getTerminator());
655 for (Value *V : FI.LinearIVUses) {
656 Value *OuterValue = FI.OuterInductionPHI;
658 OuterValue = Builder.CreateTrunc(FI.OuterInductionPHI, V->getType(),
661 LLVM_DEBUG(dbgs() << "Replacing: "; V->dump();
662 dbgs() << "with: "; OuterValue->dump());
663 V->replaceAllUsesWith(OuterValue);
666 // Tell LoopInfo, SCEV and the pass manager that the inner loop has been
667 // deleted, and any information that have about the outer loop invalidated.
668 SE->forgetLoop(FI.OuterLoop);
669 SE->forgetLoop(FI.InnerLoop);
671 U->markLoopAsDeleted(*FI.InnerLoop, FI.InnerLoop->getName());
672 LI->erase(FI.InnerLoop);
674 // Increment statistic value.
680 static bool CanWidenIV(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
681 ScalarEvolution *SE, AssumptionCache *AC,
682 const TargetTransformInfo *TTI) {
684 LLVM_DEBUG(dbgs() << "Widening the IVs is disabled\n");
688 LLVM_DEBUG(dbgs() << "Try widening the IVs\n");
689 Module *M = FI.InnerLoop->getHeader()->getParent()->getParent();
690 auto &DL = M->getDataLayout();
691 auto *InnerType = FI.InnerInductionPHI->getType();
692 auto *OuterType = FI.OuterInductionPHI->getType();
693 unsigned MaxLegalSize = DL.getLargestLegalIntTypeSizeInBits();
694 auto *MaxLegalType = DL.getLargestLegalIntType(M->getContext());
696 // If both induction types are less than the maximum legal integer width,
697 // promote both to the widest type available so we know calculating
698 // (OuterTripCount * InnerTripCount) as the new trip count is safe.
699 if (InnerType != OuterType ||
700 InnerType->getScalarSizeInBits() >= MaxLegalSize ||
701 MaxLegalType->getScalarSizeInBits() < InnerType->getScalarSizeInBits() * 2) {
702 LLVM_DEBUG(dbgs() << "Can't widen the IV\n");
706 SCEVExpander Rewriter(*SE, DL, "loopflatten");
707 SmallVector<WeakTrackingVH, 4> DeadInsts;
708 unsigned ElimExt = 0;
709 unsigned Widened = 0;
711 auto CreateWideIV = [&] (WideIVInfo WideIV, bool &Deleted) -> bool {
712 PHINode *WidePhi = createWideIV(WideIV, LI, SE, Rewriter, DT, DeadInsts,
713 ElimExt, Widened, true /* HasGuards */,
714 true /* UsePostIncrementRanges */);
717 LLVM_DEBUG(dbgs() << "Created wide phi: "; WidePhi->dump());
718 LLVM_DEBUG(dbgs() << "Deleting old phi: "; WideIV.NarrowIV->dump());
719 Deleted = RecursivelyDeleteDeadPHINode(WideIV.NarrowIV);
724 if (!CreateWideIV({FI.InnerInductionPHI, MaxLegalType, false }, Deleted))
726 // Add the narrow phi to list, so that it will be adjusted later when the
727 // the transformation is performed.
729 FI.InnerPHIsToTransform.insert(FI.InnerInductionPHI);
731 if (!CreateWideIV({FI.OuterInductionPHI, MaxLegalType, false }, Deleted))
734 assert(Widened && "Widened IV expected");
737 // Save the old/narrow induction phis, which we need to ignore in CheckPHIs.
738 FI.NarrowInnerInductionPHI = FI.InnerInductionPHI;
739 FI.NarrowOuterInductionPHI = FI.OuterInductionPHI;
741 // After widening, rediscover all the loop components.
742 return CanFlattenLoopPair(FI, DT, LI, SE, AC, TTI);
745 static bool FlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
746 ScalarEvolution *SE, AssumptionCache *AC,
747 const TargetTransformInfo *TTI, LPMUpdater *U) {
749 dbgs() << "Loop flattening running on outer loop "
750 << FI.OuterLoop->getHeader()->getName() << " and inner loop "
751 << FI.InnerLoop->getHeader()->getName() << " in "
752 << FI.OuterLoop->getHeader()->getParent()->getName() << "\n");
754 if (!CanFlattenLoopPair(FI, DT, LI, SE, AC, TTI))
757 // Check if we can widen the induction variables to avoid overflow checks.
758 bool CanFlatten = CanWidenIV(FI, DT, LI, SE, AC, TTI);
760 // It can happen that after widening of the IV, flattening may not be
761 // possible/happening, e.g. when it is deemed unprofitable. So bail here if
763 // TODO: IV widening without performing the actual flattening transformation
764 // is not ideal. While this codegen change should not matter much, it is an
765 // unnecessary change which is better to avoid. It's unlikely this happens
766 // often, because if it's unprofitibale after widening, it should be
767 // unprofitabe before widening as checked in the first round of checks. But
768 // 'RepeatedInstructionThreshold' is set to only 2, which can probably be
769 // relaxed. Because this is making a code change (the IV widening, but not
770 // the flattening), we return true here.
771 if (FI.Widened && !CanFlatten)
774 // If we have widened and can perform the transformation, do that here.
776 return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI, U);
778 // Otherwise, if we haven't widened the IV, check if the new iteration
779 // variable might overflow. In this case, we need to version the loop, and
780 // select the original version at runtime if the iteration space is too
782 // TODO: We currently don't version the loop.
783 OverflowResult OR = checkOverflow(FI, DT, AC);
784 if (OR == OverflowResult::AlwaysOverflowsHigh ||
785 OR == OverflowResult::AlwaysOverflowsLow) {
786 LLVM_DEBUG(dbgs() << "Multiply would always overflow, so not profitable\n");
788 } else if (OR == OverflowResult::MayOverflow) {
789 LLVM_DEBUG(dbgs() << "Multiply might overflow, not flattening\n");
793 LLVM_DEBUG(dbgs() << "Multiply cannot overflow, modifying loop in-place\n");
794 return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI, U);
797 bool Flatten(LoopNest &LN, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE,
798 AssumptionCache *AC, TargetTransformInfo *TTI, LPMUpdater *U) {
799 bool Changed = false;
800 for (Loop *InnerLoop : LN.getLoops()) {
801 auto *OuterLoop = InnerLoop->getParentLoop();
804 FlattenInfo FI(OuterLoop, InnerLoop);
805 Changed |= FlattenLoopPair(FI, DT, LI, SE, AC, TTI, U);
810 PreservedAnalyses LoopFlattenPass::run(LoopNest &LN, LoopAnalysisManager &LAM,
811 LoopStandardAnalysisResults &AR,
814 bool Changed = false;
816 // The loop flattening pass requires loops to be
817 // in simplified form, and also needs LCSSA. Running
818 // this pass will simplify all loops that contain inner loops,
819 // regardless of whether anything ends up being flattened.
820 Changed |= Flatten(LN, &AR.DT, &AR.LI, &AR.SE, &AR.AC, &AR.TTI, &U);
823 return PreservedAnalyses::all();
825 return getLoopPassPreservedAnalyses();
829 class LoopFlattenLegacyPass : public FunctionPass {
831 static char ID; // Pass ID, replacement for typeid
832 LoopFlattenLegacyPass() : FunctionPass(ID) {
833 initializeLoopFlattenLegacyPassPass(*PassRegistry::getPassRegistry());
836 // Possibly flatten loop L into its child.
837 bool runOnFunction(Function &F) override;
839 void getAnalysisUsage(AnalysisUsage &AU) const override {
840 getLoopAnalysisUsage(AU);
841 AU.addRequired<TargetTransformInfoWrapperPass>();
842 AU.addPreserved<TargetTransformInfoWrapperPass>();
843 AU.addRequired<AssumptionCacheTracker>();
844 AU.addPreserved<AssumptionCacheTracker>();
849 char LoopFlattenLegacyPass::ID = 0;
850 INITIALIZE_PASS_BEGIN(LoopFlattenLegacyPass, "loop-flatten", "Flattens loops",
852 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
853 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
854 INITIALIZE_PASS_END(LoopFlattenLegacyPass, "loop-flatten", "Flattens loops",
857 FunctionPass *llvm::createLoopFlattenPass() { return new LoopFlattenLegacyPass(); }
859 bool LoopFlattenLegacyPass::runOnFunction(Function &F) {
860 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
861 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
862 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
863 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
864 auto &TTIP = getAnalysis<TargetTransformInfoWrapperPass>();
865 auto *TTI = &TTIP.getTTI(F);
866 auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
867 bool Changed = false;
868 for (Loop *L : *LI) {
869 auto LN = LoopNest::getLoopNest(*L, *SE);
870 Changed |= Flatten(*LN, DT, LI, SE, AC, TTI, nullptr);