TwoAddressInstructionPass.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //===-- TwoAddressInstructionPass.cpp - Two-Address instruction 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 TwoAddress instruction pass which is used
  11. // by most register allocators. Two-Address instructions are rewritten
  12. // from:
  13. //
  14. // A = B op C
  15. //
  16. // to:
  17. //
  18. // A = B
  19. // A op= C
  20. //
  21. // Note that if a register allocator chooses to use this pass, that it
  22. // has to be capable of handling the non-SSA nature of these rewritten
  23. // virtual registers.
  24. //
  25. // It is also worth noting that the duplicate operand of the two
  26. // address instruction is removed.
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #define DEBUG_TYPE "twoaddrinstr"
  30. #include "llvm/CodeGen/Passes.h"
  31. #include "llvm/Function.h"
  32. #include "llvm/CodeGen/LiveVariables.h"
  33. #include "llvm/CodeGen/MachineFunctionPass.h"
  34. #include "llvm/CodeGen/MachineInstr.h"
  35. #include "llvm/CodeGen/MachineRegisterInfo.h"
  36. #include "llvm/Target/MRegisterInfo.h"
  37. #include "llvm/Target/TargetInstrInfo.h"
  38. #include "llvm/Target/TargetMachine.h"
  39. #include "llvm/Support/Debug.h"
  40. #include "llvm/Support/Compiler.h"
  41. #include "llvm/ADT/Statistic.h"
  42. #include "llvm/ADT/STLExtras.h"
  43. using namespace llvm;
  44. STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
  45. STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
  46. STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
  47. namespace {
  48. struct VISIBILITY_HIDDEN TwoAddressInstructionPass
  49. : public MachineFunctionPass {
  50. static char ID; // Pass identification, replacement for typeid
  51. TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
  52. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  53. /// runOnMachineFunction - pass entry point
  54. bool runOnMachineFunction(MachineFunction&);
  55. };
  56. char TwoAddressInstructionPass::ID = 0;
  57. RegisterPass<TwoAddressInstructionPass>
  58. X("twoaddressinstruction", "Two-Address instruction pass");
  59. }
  60. const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
  61. void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const {
  62. AU.addRequired<LiveVariables>();
  63. AU.addPreserved<LiveVariables>();
  64. AU.addPreservedID(MachineLoopInfoID);
  65. AU.addPreservedID(MachineDominatorsID);
  66. AU.addPreservedID(PHIEliminationID);
  67. MachineFunctionPass::getAnalysisUsage(AU);
  68. }
  69. /// runOnMachineFunction - Reduce two-address instructions to two
  70. /// operands.
  71. ///
  72. bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
  73. DOUT << "Machine Function\n";
  74. const TargetMachine &TM = MF.getTarget();
  75. const TargetInstrInfo &TII = *TM.getInstrInfo();
  76. LiveVariables &LV = getAnalysis<LiveVariables>();
  77. bool MadeChange = false;
  78. DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
  79. DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
  80. for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
  81. mbbi != mbbe; ++mbbi) {
  82. for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
  83. mi != me; ++mi) {
  84. const TargetInstrDescriptor *TID = mi->getInstrDescriptor();
  85. bool FirstTied = true;
  86. for (unsigned si = 1, e = TID->numOperands; si < e; ++si) {
  87. int ti = TID->getOperandConstraint(si, TOI::TIED_TO);
  88. if (ti == -1)
  89. continue;
  90. if (FirstTied) {
  91. ++NumTwoAddressInstrs;
  92. DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
  93. }
  94. FirstTied = false;
  95. assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
  96. mi->getOperand(si).isUse() && "two address instruction invalid");
  97. // if the two operands are the same we just remove the use
  98. // and mark the def as def&use, otherwise we have to insert a copy.
  99. if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
  100. // rewrite:
  101. // a = b op c
  102. // to:
  103. // a = b
  104. // a = a op c
  105. unsigned regA = mi->getOperand(ti).getReg();
  106. unsigned regB = mi->getOperand(si).getReg();
  107. assert(MRegisterInfo::isVirtualRegister(regA) &&
  108. MRegisterInfo::isVirtualRegister(regB) &&
  109. "cannot update physical register live information");
  110. #ifndef NDEBUG
  111. // First, verify that we don't have a use of a in the instruction (a =
  112. // b + a for example) because our transformation will not work. This
  113. // should never occur because we are in SSA form.
  114. for (unsigned i = 0; i != mi->getNumOperands(); ++i)
  115. assert((int)i == ti ||
  116. !mi->getOperand(i).isRegister() ||
  117. mi->getOperand(i).getReg() != regA);
  118. #endif
  119. // If this instruction is not the killing user of B, see if we can
  120. // rearrange the code to make it so. Making it the killing user will
  121. // allow us to coalesce A and B together, eliminating the copy we are
  122. // about to insert.
  123. if (!LV.KillsRegister(mi, regB)) {
  124. // If this instruction is commutative, check to see if C dies. If
  125. // so, swap the B and C operands. This makes the live ranges of A
  126. // and C joinable.
  127. // FIXME: This code also works for A := B op C instructions.
  128. if ((TID->Flags & M_COMMUTABLE) && mi->getNumOperands() >= 3) {
  129. assert(mi->getOperand(3-si).isRegister() &&
  130. "Not a proper commutative instruction!");
  131. unsigned regC = mi->getOperand(3-si).getReg();
  132. if (LV.KillsRegister(mi, regC)) {
  133. DOUT << "2addr: COMMUTING : " << *mi;
  134. MachineInstr *NewMI = TII.commuteInstruction(mi);
  135. if (NewMI == 0) {
  136. DOUT << "2addr: COMMUTING FAILED!\n";
  137. } else {
  138. DOUT << "2addr: COMMUTED TO: " << *NewMI;
  139. // If the instruction changed to commute it, update livevar.
  140. if (NewMI != mi) {
  141. LV.instructionChanged(mi, NewMI); // Update live variables
  142. mbbi->insert(mi, NewMI); // Insert the new inst
  143. mbbi->erase(mi); // Nuke the old inst.
  144. mi = NewMI;
  145. }
  146. ++NumCommuted;
  147. regB = regC;
  148. goto InstructionRearranged;
  149. }
  150. }
  151. }
  152. // If this instruction is potentially convertible to a true
  153. // three-address instruction,
  154. if (TID->Flags & M_CONVERTIBLE_TO_3_ADDR) {
  155. // FIXME: This assumes there are no more operands which are tied
  156. // to another register.
  157. #ifndef NDEBUG
  158. for (unsigned i = si+1, e = TID->numOperands; i < e; ++i)
  159. assert(TID->getOperandConstraint(i, TOI::TIED_TO) == -1);
  160. #endif
  161. if (MachineInstr *New = TII.convertToThreeAddress(mbbi, mi, LV)) {
  162. DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
  163. DOUT << "2addr: TO 3-ADDR: " << *New;
  164. mbbi->erase(mi); // Nuke the old inst.
  165. mi = New;
  166. ++NumConvertedTo3Addr;
  167. // Done with this instruction.
  168. break;
  169. }
  170. }
  171. }
  172. InstructionRearranged:
  173. const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA);
  174. TII.copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
  175. MachineBasicBlock::iterator prevMi = prior(mi);
  176. DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
  177. // Update live variables for regA
  178. LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
  179. varInfo.DefInst = prevMi;
  180. // update live variables for regB
  181. LiveVariables::VarInfo& varInfoB = LV.getVarInfo(regB);
  182. // regB is used in this BB.
  183. varInfoB.UsedBlocks[mbbi->getNumber()] = true;
  184. if (LV.removeVirtualRegisterKilled(regB, mbbi, mi))
  185. LV.addVirtualRegisterKilled(regB, prevMi);
  186. if (LV.removeVirtualRegisterDead(regB, mbbi, mi))
  187. LV.addVirtualRegisterDead(regB, prevMi);
  188. // replace all occurences of regB with regA
  189. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  190. if (mi->getOperand(i).isRegister() &&
  191. mi->getOperand(i).getReg() == regB)
  192. mi->getOperand(i).setReg(regA);
  193. }
  194. }
  195. assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
  196. mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
  197. MadeChange = true;
  198. DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
  199. }
  200. }
  201. }
  202. return MadeChange;
  203. }