llvm.org GIT mirror llvm / 47a3de7
MachineLoop: add methods findLoopControlBlock and findLoopPreheader This adds two new utility functions findLoopControlBlock and findLoopPreheader to MachineLoop and MachineLoopInfo. These functions are refactored and taken from the Hexagon target as they are target independent; thus this is intendend to be a non-functional change. Differential Revision: https://reviews.llvm.org/D22959 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278661 91177308-0d34-0410-b5e6-96231b3b80d8 Sjoerd Meijer 3 years ago
3 changed file(s) with 70 addition(s) and 62 deletion(s). Raw diff Collapse all Expand all
5353 /// that contains the header.
5454 MachineBasicBlock *getBottomBlock();
5555
56 /// \brief Find the block that contains the loop control variable and the
57 /// loop test. This will return the latch block if it's one of the exiting
58 /// blocks. Otherwise, return the exiting block. Return 'null' when
59 /// multiple exiting blocks are present.
60 MachineBasicBlock *findLoopControlBlock();
61
5662 void dump() const;
5763
5864 private:
7985 }
8086
8187 LoopInfoBase& getBase() { return LI; }
88
89 /// \brief Find the block that either is the loop preheader, or could
90 /// speculatively be used as the preheader. This is e.g. useful to place
91 /// loop setup code. Code that cannot be speculated should not be placed
92 /// here. SpeculativePreheader is controlling whether it also tries to
93 /// find the speculative preheader if the regular preheader is not present.
94 MachineBasicBlock *findLoopPreheader(MachineLoop *L,
95 bool SpeculativePreheader = false) const;
8296
8397 /// The iterator interface to the top-level loops in the current function.
8498 typedef LoopInfoBase::iterator iterator;
7676 return BotMBB;
7777 }
7878
79 MachineBasicBlock *MachineLoop::findLoopControlBlock() {
80 if (MachineBasicBlock *Latch = getLoopLatch()) {
81 if (isLoopExiting(Latch))
82 return Latch;
83 else
84 return getExitingBlock();
85 }
86 return nullptr;
87 }
88
89 MachineBasicBlock *
90 MachineLoopInfo::findLoopPreheader(MachineLoop *L,
91 bool SpeculativePreheader) const {
92 if (MachineBasicBlock *PB = L->getLoopPreheader())
93 return PB;
94
95 if (!SpeculativePreheader)
96 return nullptr;
97
98 MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
99 if (HB->pred_size() != 2 || HB->hasAddressTaken())
100 return nullptr;
101 // Find the predecessor of the header that is not the latch block.
102 MachineBasicBlock *Preheader = nullptr;
103 for (MachineBasicBlock *P : HB->predecessors()) {
104 if (P == LB)
105 continue;
106 // Sanity.
107 if (Preheader)
108 return nullptr;
109 Preheader = P;
110 }
111
112 // Check if the preheader candidate is a successor of any other loop
113 // headers. We want to avoid having two loop setups in the same block.
114 for (MachineBasicBlock *S : Preheader->successors()) {
115 if (S == HB)
116 continue;
117 MachineLoop *T = getLoopFor(S);
118 if (T && T->getHeader() == S)
119 return nullptr;
120 }
121 return Preheader;
122 }
123
79124 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
80125 LLVM_DUMP_METHOD void MachineLoop::dump() const {
81126 print(dbgs());
276276 /// cannot be adjusted to reflect the post-bump value.
277277 bool fixupInductionVariable(MachineLoop *L);
278278
279 /// \brief Find the block that either is the loop preheader, or could
280 /// speculatively be used as the preheader.
281 MachineBasicBlock *findLoopPreheader(MachineLoop *L) const;
282
283279 /// \brief Given a loop, if it does not have a preheader, create one.
284280 /// Return the block that is the preheader.
285281 MachineBasicBlock *createPreheaderForLoop(MachineLoop *L);
376372 return Changed;
377373 }
378374
379 /// \brief Return the latch block if it's one of the exiting blocks. Otherwise,
380 /// return the exiting block. Return 'null' when multiple exiting blocks are
381 /// present.
382 static MachineBasicBlock* getExitingBlock(MachineLoop *L) {
383 if (MachineBasicBlock *Latch = L->getLoopLatch()) {
384 if (L->isLoopExiting(Latch))
385 return Latch;
386 else
387 return L->getExitingBlock();
388 }
389 return nullptr;
390 }
391
392375 bool HexagonHardwareLoops::findInductionRegister(MachineLoop *L,
393376 unsigned &Reg,
394377 int64_t &IVBump,
395378 MachineInstr *&IVOp
396379 ) const {
397380 MachineBasicBlock *Header = L->getHeader();
398 MachineBasicBlock *Preheader = findLoopPreheader(L);
381 MachineBasicBlock *Preheader = MLI->findLoopPreheader(L, SpecPreheader);
399382 MachineBasicBlock *Latch = L->getLoopLatch();
400 MachineBasicBlock *ExitingBlock = getExitingBlock(L);
383 MachineBasicBlock *ExitingBlock = L->findLoopControlBlock();
401384 if (!Header || !Preheader || !Latch || !ExitingBlock)
402385 return false;
403386
565548 // Look for the cmp instruction to determine if we can get a useful trip
566549 // count. The trip count can be either a register or an immediate. The
567550 // location of the value depends upon the type (reg or imm).
568 MachineBasicBlock *ExitingBlock = getExitingBlock(L);
551 MachineBasicBlock *ExitingBlock = L->findLoopControlBlock();
569552 if (!ExitingBlock)
570553 return nullptr;
571554
576559 if (!FoundIV)
577560 return nullptr;
578561
579 MachineBasicBlock *Preheader = findLoopPreheader(L);
562 MachineBasicBlock *Preheader = MLI->findLoopPreheader(L, SpecPreheader);
580563
581564 MachineOperand *InitialValue = nullptr;
582565 MachineInstr *IV_Phi = MRI->getVRegDef(IVReg);
797780 if (!isPowerOf2_64(std::abs(IVBump)))
798781 return nullptr;
799782
800 MachineBasicBlock *PH = findLoopPreheader(Loop);
783 MachineBasicBlock *PH = MLI->findLoopPreheader(Loop, SpecPreheader);
801784 assert (PH && "Should have a preheader by now");
802785 MachineBasicBlock::iterator InsertPos = PH->getFirstTerminator();
803786 DebugLoc DL;
11481131 if (containsInvalidInstruction(L, IsInnerHWLoop))
11491132 return false;
11501133
1151 MachineBasicBlock *LastMBB = getExitingBlock(L);
1134 MachineBasicBlock *LastMBB = L->findLoopControlBlock();
11521135 // Don't generate hw loop if the loop has more than one exit.
11531136 if (!LastMBB)
11541137 return false;
11631146
11641147 // Ensure the loop has a preheader: the loop instruction will be
11651148 // placed there.
1166 MachineBasicBlock *Preheader = findLoopPreheader(L);
1149 MachineBasicBlock *Preheader = MLI->findLoopPreheader(L, SpecPreheader);
11671150 if (!Preheader) {
11681151 Preheader = createPreheaderForLoop(L);
11691152 if (!Preheader)
11901173
11911174 // Determine the loop start.
11921175 MachineBasicBlock *TopBlock = L->getTopBlock();
1193 MachineBasicBlock *ExitingBlock = getExitingBlock(L);
1176 MachineBasicBlock *ExitingBlock = L->findLoopControlBlock();
11941177 MachineBasicBlock *LoopStart = 0;
11951178 if (ExitingBlock != L->getLoopLatch()) {
11961179 MachineBasicBlock *TB = 0, *FB = 0;
15791562 bool HexagonHardwareLoops::fixupInductionVariable(MachineLoop *L) {
15801563 MachineBasicBlock *Header = L->getHeader();
15811564 MachineBasicBlock *Latch = L->getLoopLatch();
1582 MachineBasicBlock *ExitingBlock = getExitingBlock(L);
1565 MachineBasicBlock *ExitingBlock = L->findLoopControlBlock();
15831566
15841567 if (!(Header && Latch && ExitingBlock))
15851568 return false;
18171800 return false;
18181801 }
18191802
1820 /// Find a preaheader of the given loop.
1821 MachineBasicBlock *HexagonHardwareLoops::findLoopPreheader(MachineLoop *L)
1822 const {
1823 if (MachineBasicBlock *PB = L->getLoopPreheader())
1824 return PB;
1825 if (!SpecPreheader)
1826 return nullptr;
1827 MachineBasicBlock *HB = L->getHeader(), *LB = L->getLoopLatch();
1828 if (HB->pred_size() != 2 || HB->hasAddressTaken())
1829 return nullptr;
1830 // Find the predecessor of the header that is not the latch block.
1831 MachineBasicBlock *Preheader = nullptr;
1832 for (MachineBasicBlock *P : HB->predecessors()) {
1833 if (P == LB)
1834 continue;
1835 // Sanity.
1836 if (Preheader)
1837 return nullptr;
1838 Preheader = P;
1839 }
1840
1841 // Check if the preheader candidate is a successor of any other loop
1842 // headers. We want to avoid having two loop setups in the same block.
1843 for (MachineBasicBlock *S : Preheader->successors()) {
1844 if (S == HB)
1845 continue;
1846 MachineLoop *T = MLI->getLoopFor(S);
1847 if (T && T->getHeader() == S)
1848 return nullptr;
1849 }
1850 return Preheader;
1851 }
1852
1853
18541803 /// createPreheaderForLoop - Create a preheader for a given loop.
18551804 MachineBasicBlock *HexagonHardwareLoops::createPreheaderForLoop(
18561805 MachineLoop *L) {
1857 if (MachineBasicBlock *TmpPH = findLoopPreheader(L))
1806 if (MachineBasicBlock *TmpPH = MLI->findLoopPreheader(L, SpecPreheader))
18581807 return TmpPH;
18591808 if (!HWCreatePreheader)
18601809 return nullptr;
18611810
18621811 MachineBasicBlock *Header = L->getHeader();
18631812 MachineBasicBlock *Latch = L->getLoopLatch();
1864 MachineBasicBlock *ExitingBlock = getExitingBlock(L);
1813 MachineBasicBlock *ExitingBlock = L->findLoopControlBlock();
18651814 MachineFunction *MF = Header->getParent();
18661815 DebugLoc DL;
18671816