MachineSink.cpp 8.2 KB

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