MachineSink.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/MRegisterInfo.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((intptr_t)&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);
  45. bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
  46. };
  47. char MachineSinking::ID = 0;
  48. RegisterPass<MachineSinking> X("machine-sink", "Machine code sinking");
  49. } // end anonymous namespace
  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(MRegisterInfo::isVirtualRegister(Reg) && "Only makes sense for vregs");
  56. for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg),
  57. E = RegInfo->reg_end(); I != E; ++I) {
  58. if (I.getOperand().isDef()) continue; // ignore def.
  59. // Determine the block of the use.
  60. MachineInstr *UseInst = &*I;
  61. MachineBasicBlock *UseBlock = UseInst->getParent();
  62. if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
  63. // PHI nodes use the operand in the predecessor block, not the block with
  64. // the PHI.
  65. UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
  66. }
  67. // Check that it dominates.
  68. if (!DT->dominates(MBB, UseBlock))
  69. return false;
  70. }
  71. return true;
  72. }
  73. bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
  74. DOUT << "******** Machine Sinking ********\n";
  75. CurMF = &MF;
  76. TM = &CurMF->getTarget();
  77. TII = TM->getInstrInfo();
  78. RegInfo = &CurMF->getRegInfo();
  79. DT = &getAnalysis<MachineDominatorTree>();
  80. bool EverMadeChange = false;
  81. while (1) {
  82. bool MadeChange = false;
  83. // Process all basic blocks.
  84. for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end();
  85. I != E; ++I)
  86. MadeChange |= ProcessBlock(*I);
  87. // If this iteration over the code changed anything, keep iterating.
  88. if (!MadeChange) break;
  89. EverMadeChange = true;
  90. }
  91. return EverMadeChange;
  92. }
  93. bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
  94. bool MadeChange = false;
  95. // Can't sink anything out of a block that has less than two successors.
  96. if (MBB.succ_size() <= 1) return false;
  97. // Walk the basic block bottom-up
  98. for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){
  99. MachineBasicBlock::iterator LastIt = I;
  100. if (SinkInstruction(--I)) {
  101. I = LastIt;
  102. ++NumSunk;
  103. }
  104. }
  105. return MadeChange;
  106. }
  107. /// SinkInstruction - Determine whether it is safe to sink the specified machine
  108. /// instruction out of its current block into a successor.
  109. bool MachineSinking::SinkInstruction(MachineInstr *MI) {
  110. // Loop over all the operands of the specified instruction. If there is
  111. // anything we can't handle, bail out.
  112. MachineBasicBlock *ParentBlock = MI->getParent();
  113. // SuccToSinkTo - This is the successor to sink this instruction to, once we
  114. // decide.
  115. MachineBasicBlock *SuccToSinkTo = 0;
  116. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  117. const MachineOperand &MO = MI->getOperand(i);
  118. if (!MO.isReg()) continue; // Ignore non-register operands.
  119. unsigned Reg = MO.getReg();
  120. if (Reg == 0) continue;
  121. if (MRegisterInfo::isPhysicalRegister(Reg)) {
  122. // If this is a physical register use, we can't move it. If it is a def,
  123. // we can move it, but only if the def is dead.
  124. if (MO.isUse() || !MO.isDead())
  125. return false;
  126. } else {
  127. // Virtual register uses are always safe to sink.
  128. if (MO.isUse()) continue;
  129. // Virtual register defs can only be sunk if all their uses are in blocks
  130. // dominated by one of the successors.
  131. if (SuccToSinkTo) {
  132. // If a previous operand picked a block to sink to, then this operand
  133. // must be sinkable to the same block.
  134. if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
  135. return false;
  136. continue;
  137. }
  138. // Otherwise, we should look at all the successors and decide which one
  139. // we should sink to.
  140. for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
  141. E = ParentBlock->succ_end(); SI != E; ++SI) {
  142. if (AllUsesDominatedByBlock(Reg, *SI)) {
  143. SuccToSinkTo = *SI;
  144. break;
  145. }
  146. }
  147. // If we couldn't find a block to sink to, ignore this instruction.
  148. if (SuccToSinkTo == 0)
  149. return false;
  150. }
  151. }
  152. // FIXME: Check that the instr doesn't have side effects etc.
  153. DEBUG(cerr << "Sink instr " << *MI);
  154. DEBUG(cerr << "to block " << *SuccToSinkTo);
  155. // If the block has multiple predecessors, this would introduce computation on
  156. // a path that it doesn't already exist. We could split the critical edge,
  157. // but for now we just punt.
  158. if (SuccToSinkTo->pred_size() > 1) {
  159. DEBUG(cerr << " *** PUNTING: Critical edge found\n");
  160. return false;
  161. }
  162. // Determine where to insert into. Skip phi nodes.
  163. MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
  164. while (InsertPos != SuccToSinkTo->end() &&
  165. InsertPos->getOpcode() == TargetInstrInfo::PHI)
  166. ++InsertPos;
  167. // Move the instruction.
  168. SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
  169. ++MachineBasicBlock::iterator(MI));
  170. return true;
  171. }