MachineSink.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //===-- MachineSink.cpp - Sinking for machine instructions ----------------===//
  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 pass
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "machine-sink"
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "llvm/CodeGen/MachineRegisterInfo.h"
  16. #include "llvm/CodeGen/MachineDominators.h"
  17. #include "llvm/Target/TargetRegisterInfo.h"
  18. #include "llvm/Target/TargetInstrInfo.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/Support/Compiler.h"
  23. #include "llvm/Support/Debug.h"
  24. using namespace llvm;
  25. STATISTIC(NumSunk, "Number of machine instructions sunk");
  26. namespace {
  27. class VISIBILITY_HIDDEN MachineSinking : public MachineFunctionPass {
  28. const TargetMachine *TM;
  29. const TargetInstrInfo *TII;
  30. MachineFunction *CurMF; // Current MachineFunction
  31. MachineRegisterInfo *RegInfo; // Machine register information
  32. MachineDominatorTree *DT; // Machine dominator tree for the current Loop
  33. public:
  34. static char ID; // Pass identification
  35. MachineSinking() : MachineFunctionPass(&ID) {}
  36. virtual bool runOnMachineFunction(MachineFunction &MF);
  37. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  38. MachineFunctionPass::getAnalysisUsage(AU);
  39. AU.addRequired<MachineDominatorTree>();
  40. AU.addPreserved<MachineDominatorTree>();
  41. }
  42. private:
  43. bool ProcessBlock(MachineBasicBlock &MBB);
  44. bool SinkInstruction(MachineInstr *MI, bool &SawStore);
  45. bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
  46. };
  47. } // end anonymous namespace
  48. char MachineSinking::ID = 0;
  49. static RegisterPass<MachineSinking>
  50. X("machine-sink", "Machine code sinking");
  51. FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
  52. /// AllUsesDominatedByBlock - Return true if all uses of the specified register
  53. /// occur in blocks dominated by the specified block.
  54. bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
  55. MachineBasicBlock *MBB) const {
  56. assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
  57. "Only makes sense for vregs");
  58. for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg),
  59. E = RegInfo->reg_end(); I != E; ++I) {
  60. if (I.getOperand().isDef()) continue; // ignore def.
  61. // Determine the block of the use.
  62. MachineInstr *UseInst = &*I;
  63. MachineBasicBlock *UseBlock = UseInst->getParent();
  64. if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
  65. // PHI nodes use the operand in the predecessor block, not the block with
  66. // the PHI.
  67. UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
  68. }
  69. // Check that it dominates.
  70. if (!DT->dominates(MBB, UseBlock))
  71. return false;
  72. }
  73. return true;
  74. }
  75. bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
  76. DOUT << "******** Machine Sinking ********\n";
  77. CurMF = &MF;
  78. TM = &CurMF->getTarget();
  79. TII = TM->getInstrInfo();
  80. RegInfo = &CurMF->getRegInfo();
  81. DT = &getAnalysis<MachineDominatorTree>();
  82. bool EverMadeChange = false;
  83. while (1) {
  84. bool MadeChange = false;
  85. // Process all basic blocks.
  86. for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end();
  87. I != E; ++I)
  88. MadeChange |= ProcessBlock(*I);
  89. // If this iteration over the code changed anything, keep iterating.
  90. if (!MadeChange) break;
  91. EverMadeChange = true;
  92. }
  93. return EverMadeChange;
  94. }
  95. bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
  96. bool MadeChange = false;
  97. // Can't sink anything out of a block that has less than two successors.
  98. if (MBB.succ_size() <= 1) return false;
  99. // Walk the basic block bottom-up. Remember if we saw a store.
  100. bool SawStore = false;
  101. for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){
  102. MachineBasicBlock::iterator LastIt = I;
  103. if (SinkInstruction(--I, SawStore)) {
  104. I = LastIt;
  105. ++NumSunk;
  106. }
  107. }
  108. return MadeChange;
  109. }
  110. /// SinkInstruction - Determine whether it is safe to sink the specified machine
  111. /// instruction out of its current block into a successor.
  112. bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
  113. // Check if it's safe to move the instruction.
  114. if (!MI->isSafeToMove(TII, SawStore))
  115. return false;
  116. // FIXME: This should include support for sinking instructions within the
  117. // block they are currently in to shorten the live ranges. We often get
  118. // instructions sunk into the top of a large block, but it would be better to
  119. // also sink them down before their first use in the block. This xform has to
  120. // be careful not to *increase* register pressure though, e.g. sinking
  121. // "x = y + z" down if it kills y and z would increase the live ranges of y
  122. // and z only the shrink the live range of x.
  123. // Loop over all the operands of the specified instruction. If there is
  124. // anything we can't handle, bail out.
  125. MachineBasicBlock *ParentBlock = MI->getParent();
  126. // SuccToSinkTo - This is the successor to sink this instruction to, once we
  127. // decide.
  128. MachineBasicBlock *SuccToSinkTo = 0;
  129. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  130. const MachineOperand &MO = MI->getOperand(i);
  131. if (!MO.isReg()) continue; // Ignore non-register operands.
  132. unsigned Reg = MO.getReg();
  133. if (Reg == 0) continue;
  134. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  135. // If this is a physical register use, we can't move it. If it is a def,
  136. // we can move it, but only if the def is dead.
  137. if (MO.isUse() || !MO.isDead())
  138. return false;
  139. } else {
  140. // Virtual register uses are always safe to sink.
  141. if (MO.isUse()) continue;
  142. // FIXME: This picks a successor to sink into based on having one
  143. // successor that dominates all the uses. However, there are cases where
  144. // sinking can happen but where the sink point isn't a successor. For
  145. // example:
  146. // x = computation
  147. // if () {} else {}
  148. // use x
  149. // the instruction could be sunk over the whole diamond for the
  150. // if/then/else (or loop, etc), allowing it to be sunk into other blocks
  151. // after that.
  152. // Virtual register defs can only be sunk if all their uses are in blocks
  153. // dominated by one of the successors.
  154. if (SuccToSinkTo) {
  155. // If a previous operand picked a block to sink to, then this operand
  156. // must be sinkable to the same block.
  157. if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
  158. return false;
  159. continue;
  160. }
  161. // Otherwise, we should look at all the successors and decide which one
  162. // we should sink to.
  163. for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
  164. E = ParentBlock->succ_end(); SI != E; ++SI) {
  165. if (AllUsesDominatedByBlock(Reg, *SI)) {
  166. SuccToSinkTo = *SI;
  167. break;
  168. }
  169. }
  170. // If we couldn't find a block to sink to, ignore this instruction.
  171. if (SuccToSinkTo == 0)
  172. return false;
  173. }
  174. }
  175. // If there are no outputs, it must have side-effects.
  176. if (SuccToSinkTo == 0)
  177. return false;
  178. DEBUG(cerr << "Sink instr " << *MI);
  179. DEBUG(cerr << "to block " << *SuccToSinkTo);
  180. // If the block has multiple predecessors, this would introduce computation on
  181. // a path that it doesn't already exist. We could split the critical edge,
  182. // but for now we just punt.
  183. // FIXME: Split critical edges if not backedges.
  184. if (SuccToSinkTo->pred_size() > 1) {
  185. DEBUG(cerr << " *** PUNTING: Critical edge found\n");
  186. return false;
  187. }
  188. // Determine where to insert into. Skip phi nodes.
  189. MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
  190. while (InsertPos != SuccToSinkTo->end() &&
  191. InsertPos->getOpcode() == TargetInstrInfo::PHI)
  192. ++InsertPos;
  193. // Move the instruction.
  194. SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
  195. ++MachineBasicBlock::iterator(MI));
  196. return true;
  197. }