ProcessImplicitDefs.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
  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. #define DEBUG_TYPE "processimplicitdefs"
  10. #include "llvm/CodeGen/ProcessImplicitDefs.h"
  11. #include "llvm/ADT/DepthFirstIterator.h"
  12. #include "llvm/ADT/SmallSet.h"
  13. #include "llvm/Analysis/AliasAnalysis.h"
  14. #include "llvm/CodeGen/LiveVariables.h"
  15. #include "llvm/CodeGen/MachineInstr.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Target/TargetInstrInfo.h"
  20. #include "llvm/Target/TargetRegisterInfo.h"
  21. using namespace llvm;
  22. char ProcessImplicitDefs::ID = 0;
  23. char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
  24. INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
  25. "Process Implicit Definitions", false, false)
  26. INITIALIZE_PASS_DEPENDENCY(LiveVariables)
  27. INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
  28. "Process Implicit Definitions", false, false)
  29. void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
  30. AU.setPreservesCFG();
  31. AU.addPreserved<AliasAnalysis>();
  32. AU.addPreserved<LiveVariables>();
  33. AU.addPreservedID(MachineLoopInfoID);
  34. AU.addPreservedID(MachineDominatorsID);
  35. AU.addPreservedID(TwoAddressInstructionPassID);
  36. AU.addPreservedID(PHIEliminationID);
  37. MachineFunctionPass::getAnalysisUsage(AU);
  38. }
  39. bool
  40. ProcessImplicitDefs::CanTurnIntoImplicitDef(MachineInstr *MI,
  41. unsigned Reg, unsigned OpIdx,
  42. SmallSet<unsigned, 8> &ImpDefRegs) {
  43. switch(OpIdx) {
  44. case 1:
  45. return MI->isCopy() && (!MI->getOperand(0).readsReg() ||
  46. ImpDefRegs.count(MI->getOperand(0).getReg()));
  47. case 2:
  48. return MI->isSubregToReg() && (!MI->getOperand(0).readsReg() ||
  49. ImpDefRegs.count(MI->getOperand(0).getReg()));
  50. default: return false;
  51. }
  52. }
  53. static bool isUndefCopy(MachineInstr *MI, unsigned Reg,
  54. SmallSet<unsigned, 8> &ImpDefRegs) {
  55. if (MI->isCopy()) {
  56. MachineOperand &MO0 = MI->getOperand(0);
  57. MachineOperand &MO1 = MI->getOperand(1);
  58. if (MO1.getReg() != Reg)
  59. return false;
  60. if (!MO0.readsReg() || ImpDefRegs.count(MO0.getReg()))
  61. return true;
  62. return false;
  63. }
  64. return false;
  65. }
  66. /// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
  67. /// there is one implicit_def for each use. Add isUndef marker to
  68. /// implicit_def defs and their uses.
  69. bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &fn) {
  70. DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
  71. << "********** Function: "
  72. << ((Value*)fn.getFunction())->getName() << '\n');
  73. bool Changed = false;
  74. TII = fn.getTarget().getInstrInfo();
  75. TRI = fn.getTarget().getRegisterInfo();
  76. MRI = &fn.getRegInfo();
  77. LV = getAnalysisIfAvailable<LiveVariables>();
  78. SmallSet<unsigned, 8> ImpDefRegs;
  79. SmallVector<MachineInstr*, 8> ImpDefMIs;
  80. SmallVector<MachineInstr*, 4> RUses;
  81. SmallPtrSet<MachineBasicBlock*,16> Visited;
  82. SmallPtrSet<MachineInstr*, 8> ModInsts;
  83. MachineBasicBlock *Entry = fn.begin();
  84. for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
  85. DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
  86. DFI != E; ++DFI) {
  87. MachineBasicBlock *MBB = *DFI;
  88. for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
  89. I != E; ) {
  90. MachineInstr *MI = &*I;
  91. ++I;
  92. if (MI->isImplicitDef()) {
  93. ImpDefMIs.push_back(MI);
  94. // Is this a sub-register read-modify-write?
  95. if (MI->getOperand(0).readsReg())
  96. continue;
  97. unsigned Reg = MI->getOperand(0).getReg();
  98. ImpDefRegs.insert(Reg);
  99. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  100. for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
  101. ImpDefRegs.insert(*SubRegs);
  102. }
  103. continue;
  104. }
  105. // Eliminate %reg1032:sub<def> = COPY undef.
  106. if (MI->isCopy() && MI->getOperand(0).readsReg()) {
  107. MachineOperand &MO = MI->getOperand(1);
  108. if (MO.isUndef() || ImpDefRegs.count(MO.getReg())) {
  109. if (LV && MO.isKill()) {
  110. LiveVariables::VarInfo& vi = LV->getVarInfo(MO.getReg());
  111. vi.removeKill(MI);
  112. }
  113. unsigned Reg = MI->getOperand(0).getReg();
  114. MI->eraseFromParent();
  115. Changed = true;
  116. // A REG_SEQUENCE may have been expanded into partial definitions.
  117. // If this was the last one, mark Reg as implicitly defined.
  118. if (TargetRegisterInfo::isVirtualRegister(Reg) && MRI->def_empty(Reg))
  119. ImpDefRegs.insert(Reg);
  120. continue;
  121. }
  122. }
  123. bool ChangedToImpDef = false;
  124. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  125. MachineOperand& MO = MI->getOperand(i);
  126. if (!MO.isReg() || !MO.readsReg())
  127. continue;
  128. unsigned Reg = MO.getReg();
  129. if (!Reg)
  130. continue;
  131. if (!ImpDefRegs.count(Reg))
  132. continue;
  133. // Use is a copy, just turn it into an implicit_def.
  134. if (CanTurnIntoImplicitDef(MI, Reg, i, ImpDefRegs)) {
  135. bool isKill = MO.isKill();
  136. MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  137. for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
  138. MI->RemoveOperand(j);
  139. if (isKill) {
  140. ImpDefRegs.erase(Reg);
  141. if (LV) {
  142. LiveVariables::VarInfo& vi = LV->getVarInfo(Reg);
  143. vi.removeKill(MI);
  144. }
  145. }
  146. ChangedToImpDef = true;
  147. Changed = true;
  148. break;
  149. }
  150. Changed = true;
  151. MO.setIsUndef();
  152. // This is a partial register redef of an implicit def.
  153. // Make sure the whole register is defined by the instruction.
  154. if (MO.isDef()) {
  155. MI->addRegisterDefined(Reg);
  156. continue;
  157. }
  158. if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
  159. // Make sure other reads of Reg are also marked <undef>.
  160. for (unsigned j = i+1; j != e; ++j) {
  161. MachineOperand &MOJ = MI->getOperand(j);
  162. if (MOJ.isReg() && MOJ.getReg() == Reg && MOJ.readsReg())
  163. MOJ.setIsUndef();
  164. }
  165. ImpDefRegs.erase(Reg);
  166. }
  167. }
  168. if (ChangedToImpDef) {
  169. // Backtrack to process this new implicit_def.
  170. --I;
  171. } else {
  172. for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
  173. MachineOperand& MO = MI->getOperand(i);
  174. if (!MO.isReg() || !MO.isDef())
  175. continue;
  176. ImpDefRegs.erase(MO.getReg());
  177. }
  178. }
  179. }
  180. // Any outstanding liveout implicit_def's?
  181. for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
  182. MachineInstr *MI = ImpDefMIs[i];
  183. unsigned Reg = MI->getOperand(0).getReg();
  184. if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
  185. !ImpDefRegs.count(Reg)) {
  186. // Delete all "local" implicit_def's. That include those which define
  187. // physical registers since they cannot be liveout.
  188. MI->eraseFromParent();
  189. Changed = true;
  190. continue;
  191. }
  192. // If there are multiple defs of the same register and at least one
  193. // is not an implicit_def, do not insert implicit_def's before the
  194. // uses.
  195. bool Skip = false;
  196. SmallVector<MachineInstr*, 4> DeadImpDefs;
  197. for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(Reg),
  198. DE = MRI->def_end(); DI != DE; ++DI) {
  199. MachineInstr *DeadImpDef = &*DI;
  200. if (!DeadImpDef->isImplicitDef()) {
  201. Skip = true;
  202. break;
  203. }
  204. DeadImpDefs.push_back(DeadImpDef);
  205. }
  206. if (Skip)
  207. continue;
  208. // The only implicit_def which we want to keep are those that are live
  209. // out of its block.
  210. for (unsigned j = 0, ee = DeadImpDefs.size(); j != ee; ++j)
  211. DeadImpDefs[j]->eraseFromParent();
  212. Changed = true;
  213. // Process each use instruction once.
  214. for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
  215. UE = MRI->use_end(); UI != UE; ++UI) {
  216. if (UI.getOperand().isUndef())
  217. continue;
  218. MachineInstr *RMI = &*UI;
  219. if (ModInsts.insert(RMI))
  220. RUses.push_back(RMI);
  221. }
  222. for (unsigned i = 0, e = RUses.size(); i != e; ++i) {
  223. MachineInstr *RMI = RUses[i];
  224. // Turn a copy use into an implicit_def.
  225. if (isUndefCopy(RMI, Reg, ImpDefRegs)) {
  226. RMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  227. bool isKill = false;
  228. SmallVector<unsigned, 4> Ops;
  229. for (unsigned j = 0, ee = RMI->getNumOperands(); j != ee; ++j) {
  230. MachineOperand &RRMO = RMI->getOperand(j);
  231. if (RRMO.isReg() && RRMO.getReg() == Reg) {
  232. Ops.push_back(j);
  233. if (RRMO.isKill())
  234. isKill = true;
  235. }
  236. }
  237. // Leave the other operands along.
  238. for (unsigned j = 0, ee = Ops.size(); j != ee; ++j) {
  239. unsigned OpIdx = Ops[j];
  240. RMI->RemoveOperand(OpIdx-j);
  241. }
  242. // Update LiveVariables varinfo if the instruction is a kill.
  243. if (LV && isKill) {
  244. LiveVariables::VarInfo& vi = LV->getVarInfo(Reg);
  245. vi.removeKill(RMI);
  246. }
  247. continue;
  248. }
  249. // Replace Reg with a new vreg that's marked implicit.
  250. const TargetRegisterClass* RC = MRI->getRegClass(Reg);
  251. unsigned NewVReg = MRI->createVirtualRegister(RC);
  252. bool isKill = true;
  253. for (unsigned j = 0, ee = RMI->getNumOperands(); j != ee; ++j) {
  254. MachineOperand &RRMO = RMI->getOperand(j);
  255. if (RRMO.isReg() && RRMO.getReg() == Reg) {
  256. RRMO.setReg(NewVReg);
  257. RRMO.setIsUndef();
  258. if (isKill) {
  259. // Only the first operand of NewVReg is marked kill.
  260. RRMO.setIsKill();
  261. isKill = false;
  262. }
  263. }
  264. }
  265. }
  266. RUses.clear();
  267. ModInsts.clear();
  268. }
  269. ImpDefRegs.clear();
  270. ImpDefMIs.clear();
  271. }
  272. return Changed;
  273. }