CodePlacementOpt.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //===-- CodePlacementOpt.cpp - Code Placement pass. -----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the pass that optimizes code placement and aligns loop
  11. // headers to target-specific alignment boundaries.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "code-placement"
  15. #include "llvm/CodeGen/MachineLoopInfo.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/Target/TargetInstrInfo.h"
  19. #include "llvm/Target/TargetLowering.h"
  20. #include "llvm/Target/TargetMachine.h"
  21. #include "llvm/Support/Compiler.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/ADT/Statistic.h"
  24. using namespace llvm;
  25. STATISTIC(NumLoopsAligned, "Number of loops aligned");
  26. STATISTIC(NumIntraElim, "Number of intra loop branches eliminated");
  27. STATISTIC(NumIntraMoved, "Number of intra loop branches moved");
  28. namespace {
  29. class CodePlacementOpt : public MachineFunctionPass {
  30. const MachineLoopInfo *MLI;
  31. const TargetInstrInfo *TII;
  32. const TargetLowering *TLI;
  33. public:
  34. static char ID;
  35. CodePlacementOpt() : MachineFunctionPass(ID) {}
  36. virtual bool runOnMachineFunction(MachineFunction &MF);
  37. virtual const char *getPassName() const {
  38. return "Code Placement Optimizer";
  39. }
  40. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  41. AU.addRequired<MachineLoopInfo>();
  42. AU.addPreservedID(MachineDominatorsID);
  43. MachineFunctionPass::getAnalysisUsage(AU);
  44. }
  45. private:
  46. bool HasFallthrough(MachineBasicBlock *MBB);
  47. bool HasAnalyzableTerminator(MachineBasicBlock *MBB);
  48. void Splice(MachineFunction &MF,
  49. MachineFunction::iterator InsertPt,
  50. MachineFunction::iterator Begin,
  51. MachineFunction::iterator End);
  52. bool EliminateUnconditionalJumpsToTop(MachineFunction &MF,
  53. MachineLoop *L);
  54. bool MoveDiscontiguousLoopBlocks(MachineFunction &MF,
  55. MachineLoop *L);
  56. bool OptimizeIntraLoopEdgesInLoopNest(MachineFunction &MF, MachineLoop *L);
  57. bool OptimizeIntraLoopEdges(MachineFunction &MF);
  58. bool AlignLoops(MachineFunction &MF);
  59. bool AlignLoop(MachineFunction &MF, MachineLoop *L, unsigned Align);
  60. };
  61. char CodePlacementOpt::ID = 0;
  62. } // end anonymous namespace
  63. FunctionPass *llvm::createCodePlacementOptPass() {
  64. return new CodePlacementOpt();
  65. }
  66. /// HasFallthrough - Test whether the given branch has a fallthrough, either as
  67. /// a plain fallthrough or as a fallthrough case of a conditional branch.
  68. ///
  69. bool CodePlacementOpt::HasFallthrough(MachineBasicBlock *MBB) {
  70. MachineBasicBlock *TBB = 0, *FBB = 0;
  71. SmallVector<MachineOperand, 4> Cond;
  72. if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
  73. return false;
  74. // This conditional branch has no fallthrough.
  75. if (FBB)
  76. return false;
  77. // An unconditional branch has no fallthrough.
  78. if (Cond.empty() && TBB)
  79. return false;
  80. // It has a fallthrough.
  81. return true;
  82. }
  83. /// HasAnalyzableTerminator - Test whether AnalyzeBranch will succeed on MBB.
  84. /// This is called before major changes are begun to test whether it will be
  85. /// possible to complete the changes.
  86. ///
  87. /// Target-specific code is hereby encouraged to make AnalyzeBranch succeed
  88. /// whenever possible.
  89. ///
  90. bool CodePlacementOpt::HasAnalyzableTerminator(MachineBasicBlock *MBB) {
  91. // Conservatively ignore EH landing pads.
  92. if (MBB->isLandingPad()) return false;
  93. // Aggressively handle return blocks and similar constructs.
  94. if (MBB->succ_empty()) return true;
  95. // Ask the target's AnalyzeBranch if it can handle this block.
  96. MachineBasicBlock *TBB = 0, *FBB = 0;
  97. SmallVector<MachineOperand, 4> Cond;
  98. // Make sure the terminator is understood.
  99. if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
  100. return false;
  101. // Ignore blocks which look like they might have EH-related control flow.
  102. // AnalyzeBranch thinks it knows how to analyze such things, but it doesn't
  103. // recognize the possibility of a control transfer through an unwind.
  104. // Such blocks contain EH_LABEL instructions, however they may be in the
  105. // middle of the block. Instead of searching for them, just check to see
  106. // if the CFG disagrees with AnalyzeBranch.
  107. if (1u + !Cond.empty() != MBB->succ_size())
  108. return false;
  109. // Make sure we have the option of reversing the condition.
  110. if (!Cond.empty() && TII->ReverseBranchCondition(Cond))
  111. return false;
  112. return true;
  113. }
  114. /// Splice - Move the sequence of instructions [Begin,End) to just before
  115. /// InsertPt. Update branch instructions as needed to account for broken
  116. /// fallthrough edges and to take advantage of newly exposed fallthrough
  117. /// opportunities.
  118. ///
  119. void CodePlacementOpt::Splice(MachineFunction &MF,
  120. MachineFunction::iterator InsertPt,
  121. MachineFunction::iterator Begin,
  122. MachineFunction::iterator End) {
  123. assert(Begin != MF.begin() && End != MF.begin() && InsertPt != MF.begin() &&
  124. "Splice can't change the entry block!");
  125. MachineFunction::iterator OldBeginPrior = prior(Begin);
  126. MachineFunction::iterator OldEndPrior = prior(End);
  127. MF.splice(InsertPt, Begin, End);
  128. prior(Begin)->updateTerminator();
  129. OldBeginPrior->updateTerminator();
  130. OldEndPrior->updateTerminator();
  131. }
  132. /// EliminateUnconditionalJumpsToTop - Move blocks which unconditionally jump
  133. /// to the loop top to the top of the loop so that they have a fall through.
  134. /// This can introduce a branch on entry to the loop, but it can eliminate a
  135. /// branch within the loop. See the @simple case in
  136. /// test/CodeGen/X86/loop_blocks.ll for an example of this.
  137. bool CodePlacementOpt::EliminateUnconditionalJumpsToTop(MachineFunction &MF,
  138. MachineLoop *L) {
  139. bool Changed = false;
  140. MachineBasicBlock *TopMBB = L->getTopBlock();
  141. bool BotHasFallthrough = HasFallthrough(L->getBottomBlock());
  142. if (TopMBB == MF.begin() ||
  143. HasAnalyzableTerminator(prior(MachineFunction::iterator(TopMBB)))) {
  144. new_top:
  145. for (MachineBasicBlock::pred_iterator PI = TopMBB->pred_begin(),
  146. PE = TopMBB->pred_end(); PI != PE; ++PI) {
  147. MachineBasicBlock *Pred = *PI;
  148. if (Pred == TopMBB) continue;
  149. if (HasFallthrough(Pred)) continue;
  150. if (!L->contains(Pred)) continue;
  151. // Verify that we can analyze all the loop entry edges before beginning
  152. // any changes which will require us to be able to analyze them.
  153. if (Pred == MF.begin())
  154. continue;
  155. if (!HasAnalyzableTerminator(Pred))
  156. continue;
  157. if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Pred))))
  158. continue;
  159. // Move the block.
  160. DEBUG(dbgs() << "CGP: Moving blocks starting at BB#" << Pred->getNumber()
  161. << " to top of loop.\n");
  162. Changed = true;
  163. // Move it and all the blocks that can reach it via fallthrough edges
  164. // exclusively, to keep existing fallthrough edges intact.
  165. MachineFunction::iterator Begin = Pred;
  166. MachineFunction::iterator End = llvm::next(Begin);
  167. while (Begin != MF.begin()) {
  168. MachineFunction::iterator Prior = prior(Begin);
  169. if (Prior == MF.begin())
  170. break;
  171. // Stop when a non-fallthrough edge is found.
  172. if (!HasFallthrough(Prior))
  173. break;
  174. // Stop if a block which could fall-through out of the loop is found.
  175. if (Prior->isSuccessor(End))
  176. break;
  177. // If we've reached the top, stop scanning.
  178. if (Prior == MachineFunction::iterator(TopMBB)) {
  179. // We know top currently has a fall through (because we just checked
  180. // it) which would be lost if we do the transformation, so it isn't
  181. // worthwhile to do the transformation unless it would expose a new
  182. // fallthrough edge.
  183. if (!Prior->isSuccessor(End))
  184. goto next_pred;
  185. // Otherwise we can stop scanning and procede to move the blocks.
  186. break;
  187. }
  188. // If we hit a switch or something complicated, don't move anything
  189. // for this predecessor.
  190. if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Prior))))
  191. break;
  192. // Ok, the block prior to Begin will be moved along with the rest.
  193. // Extend the range to include it.
  194. Begin = Prior;
  195. ++NumIntraMoved;
  196. }
  197. // Move the blocks.
  198. Splice(MF, TopMBB, Begin, End);
  199. // Update TopMBB.
  200. TopMBB = L->getTopBlock();
  201. // We have a new loop top. Iterate on it. We shouldn't have to do this
  202. // too many times if BranchFolding has done a reasonable job.
  203. goto new_top;
  204. next_pred:;
  205. }
  206. }
  207. // If the loop previously didn't exit with a fall-through and it now does,
  208. // we eliminated a branch.
  209. if (Changed &&
  210. !BotHasFallthrough &&
  211. HasFallthrough(L->getBottomBlock())) {
  212. ++NumIntraElim;
  213. }
  214. return Changed;
  215. }
  216. /// MoveDiscontiguousLoopBlocks - Move any loop blocks that are not in the
  217. /// portion of the loop contiguous with the header. This usually makes the loop
  218. /// contiguous, provided that AnalyzeBranch can handle all the relevant
  219. /// branching. See the @cfg_islands case in test/CodeGen/X86/loop_blocks.ll
  220. /// for an example of this.
  221. bool CodePlacementOpt::MoveDiscontiguousLoopBlocks(MachineFunction &MF,
  222. MachineLoop *L) {
  223. bool Changed = false;
  224. MachineBasicBlock *TopMBB = L->getTopBlock();
  225. MachineBasicBlock *BotMBB = L->getBottomBlock();
  226. // Determine a position to move orphaned loop blocks to. If TopMBB is not
  227. // entered via fallthrough and BotMBB is exited via fallthrough, prepend them
  228. // to the top of the loop to avoid losing that fallthrough. Otherwise append
  229. // them to the bottom, even if it previously had a fallthrough, on the theory
  230. // that it's worth an extra branch to keep the loop contiguous.
  231. MachineFunction::iterator InsertPt =
  232. llvm::next(MachineFunction::iterator(BotMBB));
  233. bool InsertAtTop = false;
  234. if (TopMBB != MF.begin() &&
  235. !HasFallthrough(prior(MachineFunction::iterator(TopMBB))) &&
  236. HasFallthrough(BotMBB)) {
  237. InsertPt = TopMBB;
  238. InsertAtTop = true;
  239. }
  240. // Keep a record of which blocks are in the portion of the loop contiguous
  241. // with the loop header.
  242. SmallPtrSet<MachineBasicBlock *, 8> ContiguousBlocks;
  243. for (MachineFunction::iterator I = TopMBB,
  244. E = llvm::next(MachineFunction::iterator(BotMBB)); I != E; ++I)
  245. ContiguousBlocks.insert(I);
  246. // Find non-contigous blocks and fix them.
  247. if (InsertPt != MF.begin() && HasAnalyzableTerminator(prior(InsertPt)))
  248. for (MachineLoop::block_iterator BI = L->block_begin(), BE = L->block_end();
  249. BI != BE; ++BI) {
  250. MachineBasicBlock *BB = *BI;
  251. // Verify that we can analyze all the loop entry edges before beginning
  252. // any changes which will require us to be able to analyze them.
  253. if (!HasAnalyzableTerminator(BB))
  254. continue;
  255. if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(BB))))
  256. continue;
  257. // If the layout predecessor is part of the loop, this block will be
  258. // processed along with it. This keeps them in their relative order.
  259. if (BB != MF.begin() &&
  260. L->contains(prior(MachineFunction::iterator(BB))))
  261. continue;
  262. // Check to see if this block is already contiguous with the main
  263. // portion of the loop.
  264. if (!ContiguousBlocks.insert(BB))
  265. continue;
  266. // Move the block.
  267. DEBUG(dbgs() << "CGP: Moving blocks starting at BB#" << BB->getNumber()
  268. << " to be contiguous with loop.\n");
  269. Changed = true;
  270. // Process this block and all loop blocks contiguous with it, to keep
  271. // them in their relative order.
  272. MachineFunction::iterator Begin = BB;
  273. MachineFunction::iterator End = llvm::next(MachineFunction::iterator(BB));
  274. for (; End != MF.end(); ++End) {
  275. if (!L->contains(End)) break;
  276. if (!HasAnalyzableTerminator(End)) break;
  277. ContiguousBlocks.insert(End);
  278. ++NumIntraMoved;
  279. }
  280. // If we're inserting at the bottom of the loop, and the code we're
  281. // moving originally had fall-through successors, bring the sucessors
  282. // up with the loop blocks to preserve the fall-through edges.
  283. if (!InsertAtTop)
  284. for (; End != MF.end(); ++End) {
  285. if (L->contains(End)) break;
  286. if (!HasAnalyzableTerminator(End)) break;
  287. if (!HasFallthrough(prior(End))) break;
  288. }
  289. // Move the blocks. This may invalidate TopMBB and/or BotMBB, but
  290. // we don't need them anymore at this point.
  291. Splice(MF, InsertPt, Begin, End);
  292. }
  293. return Changed;
  294. }
  295. /// OptimizeIntraLoopEdgesInLoopNest - Reposition loop blocks to minimize
  296. /// intra-loop branching and to form contiguous loops.
  297. ///
  298. /// This code takes the approach of making minor changes to the existing
  299. /// layout to fix specific loop-oriented problems. Also, it depends on
  300. /// AnalyzeBranch, which can't understand complex control instructions.
  301. ///
  302. bool CodePlacementOpt::OptimizeIntraLoopEdgesInLoopNest(MachineFunction &MF,
  303. MachineLoop *L) {
  304. bool Changed = false;
  305. // Do optimization for nested loops.
  306. for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
  307. Changed |= OptimizeIntraLoopEdgesInLoopNest(MF, *I);
  308. // Do optimization for this loop.
  309. Changed |= EliminateUnconditionalJumpsToTop(MF, L);
  310. Changed |= MoveDiscontiguousLoopBlocks(MF, L);
  311. return Changed;
  312. }
  313. /// OptimizeIntraLoopEdges - Reposition loop blocks to minimize
  314. /// intra-loop branching and to form contiguous loops.
  315. ///
  316. bool CodePlacementOpt::OptimizeIntraLoopEdges(MachineFunction &MF) {
  317. bool Changed = false;
  318. if (!TLI->shouldOptimizeCodePlacement())
  319. return Changed;
  320. // Do optimization for each loop in the function.
  321. for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
  322. I != E; ++I)
  323. if (!(*I)->getParentLoop())
  324. Changed |= OptimizeIntraLoopEdgesInLoopNest(MF, *I);
  325. return Changed;
  326. }
  327. /// AlignLoops - Align loop headers to target preferred alignments.
  328. ///
  329. bool CodePlacementOpt::AlignLoops(MachineFunction &MF) {
  330. const Function *F = MF.getFunction();
  331. if (F->hasFnAttr(Attribute::OptimizeForSize))
  332. return false;
  333. unsigned Align = TLI->getPrefLoopAlignment();
  334. if (!Align)
  335. return false; // Don't care about loop alignment.
  336. bool Changed = false;
  337. for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
  338. I != E; ++I)
  339. Changed |= AlignLoop(MF, *I, Align);
  340. return Changed;
  341. }
  342. /// AlignLoop - Align loop headers to target preferred alignments.
  343. ///
  344. bool CodePlacementOpt::AlignLoop(MachineFunction &MF, MachineLoop *L,
  345. unsigned Align) {
  346. bool Changed = false;
  347. // Do alignment for nested loops.
  348. for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
  349. Changed |= AlignLoop(MF, *I, Align);
  350. L->getTopBlock()->setAlignment(Align);
  351. Changed = true;
  352. ++NumLoopsAligned;
  353. return Changed;
  354. }
  355. bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
  356. MLI = &getAnalysis<MachineLoopInfo>();
  357. if (MLI->empty())
  358. return false; // No loops.
  359. TLI = MF.getTarget().getTargetLowering();
  360. TII = MF.getTarget().getInstrInfo();
  361. bool Changed = OptimizeIntraLoopEdges(MF);
  362. Changed |= AlignLoops(MF);
  363. return Changed;
  364. }