MachineCSE.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //===-- MachineCSE.cpp - Machine Common Subexpression Elimination 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 pass performs global common subexpression elimination on machine
  11. // instructions using a scoped hash table based value numbering scheme. It
  12. // must be run while the machine function is still in SSA form.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #define DEBUG_TYPE "machine-cse"
  16. #include "llvm/CodeGen/Passes.h"
  17. #include "llvm/CodeGen/MachineDominators.h"
  18. #include "llvm/CodeGen/MachineInstr.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.h"
  20. #include "llvm/Analysis/AliasAnalysis.h"
  21. #include "llvm/Target/TargetInstrInfo.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/ScopedHashTable.h"
  24. #include "llvm/ADT/SmallSet.h"
  25. #include "llvm/ADT/Statistic.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/RecyclingAllocator.h"
  28. using namespace llvm;
  29. STATISTIC(NumCoalesces, "Number of copies coalesced");
  30. STATISTIC(NumCSEs, "Number of common subexpression eliminated");
  31. STATISTIC(NumPhysCSEs,
  32. "Number of physreg referencing common subexpr eliminated");
  33. STATISTIC(NumCrossBBCSEs,
  34. "Number of cross-MBB physreg referencing CS eliminated");
  35. STATISTIC(NumCommutes, "Number of copies coalesced after commuting");
  36. namespace {
  37. class MachineCSE : public MachineFunctionPass {
  38. const TargetInstrInfo *TII;
  39. const TargetRegisterInfo *TRI;
  40. AliasAnalysis *AA;
  41. MachineDominatorTree *DT;
  42. MachineRegisterInfo *MRI;
  43. public:
  44. static char ID; // Pass identification
  45. MachineCSE() : MachineFunctionPass(ID), LookAheadLimit(5), CurrVN(0) {
  46. initializeMachineCSEPass(*PassRegistry::getPassRegistry());
  47. }
  48. virtual bool runOnMachineFunction(MachineFunction &MF);
  49. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  50. AU.setPreservesCFG();
  51. MachineFunctionPass::getAnalysisUsage(AU);
  52. AU.addRequired<AliasAnalysis>();
  53. AU.addPreservedID(MachineLoopInfoID);
  54. AU.addRequired<MachineDominatorTree>();
  55. AU.addPreserved<MachineDominatorTree>();
  56. }
  57. virtual void releaseMemory() {
  58. ScopeMap.clear();
  59. Exps.clear();
  60. }
  61. private:
  62. const unsigned LookAheadLimit;
  63. typedef RecyclingAllocator<BumpPtrAllocator,
  64. ScopedHashTableVal<MachineInstr*, unsigned> > AllocatorTy;
  65. typedef ScopedHashTable<MachineInstr*, unsigned,
  66. MachineInstrExpressionTrait, AllocatorTy> ScopedHTType;
  67. typedef ScopedHTType::ScopeTy ScopeType;
  68. DenseMap<MachineBasicBlock*, ScopeType*> ScopeMap;
  69. ScopedHTType VNT;
  70. SmallVector<MachineInstr*, 64> Exps;
  71. unsigned CurrVN;
  72. bool PerformTrivialCoalescing(MachineInstr *MI, MachineBasicBlock *MBB);
  73. bool isPhysDefTriviallyDead(unsigned Reg,
  74. MachineBasicBlock::const_iterator I,
  75. MachineBasicBlock::const_iterator E) const ;
  76. bool hasLivePhysRegDefUses(const MachineInstr *MI,
  77. const MachineBasicBlock *MBB,
  78. SmallSet<unsigned,8> &PhysRefs,
  79. SmallVector<unsigned,2> &PhysDefs) const;
  80. bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
  81. SmallSet<unsigned,8> &PhysRefs,
  82. SmallVector<unsigned,2> &PhysDefs,
  83. bool &NonLocal) const;
  84. bool isCSECandidate(MachineInstr *MI);
  85. bool isProfitableToCSE(unsigned CSReg, unsigned Reg,
  86. MachineInstr *CSMI, MachineInstr *MI);
  87. void EnterScope(MachineBasicBlock *MBB);
  88. void ExitScope(MachineBasicBlock *MBB);
  89. bool ProcessBlock(MachineBasicBlock *MBB);
  90. void ExitScopeIfDone(MachineDomTreeNode *Node,
  91. DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
  92. DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap);
  93. bool PerformCSE(MachineDomTreeNode *Node);
  94. };
  95. } // end anonymous namespace
  96. char MachineCSE::ID = 0;
  97. INITIALIZE_PASS_BEGIN(MachineCSE, "machine-cse",
  98. "Machine Common Subexpression Elimination", false, false)
  99. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  100. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  101. INITIALIZE_PASS_END(MachineCSE, "machine-cse",
  102. "Machine Common Subexpression Elimination", false, false)
  103. FunctionPass *llvm::createMachineCSEPass() { return new MachineCSE(); }
  104. bool MachineCSE::PerformTrivialCoalescing(MachineInstr *MI,
  105. MachineBasicBlock *MBB) {
  106. bool Changed = false;
  107. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  108. MachineOperand &MO = MI->getOperand(i);
  109. if (!MO.isReg() || !MO.isUse())
  110. continue;
  111. unsigned Reg = MO.getReg();
  112. if (!TargetRegisterInfo::isVirtualRegister(Reg))
  113. continue;
  114. if (!MRI->hasOneNonDBGUse(Reg))
  115. // Only coalesce single use copies. This ensure the copy will be
  116. // deleted.
  117. continue;
  118. MachineInstr *DefMI = MRI->getVRegDef(Reg);
  119. if (DefMI->getParent() != MBB)
  120. continue;
  121. if (!DefMI->isCopy())
  122. continue;
  123. unsigned SrcReg = DefMI->getOperand(1).getReg();
  124. if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
  125. continue;
  126. if (DefMI->getOperand(0).getSubReg() || DefMI->getOperand(1).getSubReg())
  127. continue;
  128. if (!MRI->constrainRegClass(SrcReg, MRI->getRegClass(Reg)))
  129. continue;
  130. DEBUG(dbgs() << "Coalescing: " << *DefMI);
  131. DEBUG(dbgs() << "*** to: " << *MI);
  132. MO.setReg(SrcReg);
  133. MRI->clearKillFlags(SrcReg);
  134. DefMI->eraseFromParent();
  135. ++NumCoalesces;
  136. Changed = true;
  137. }
  138. return Changed;
  139. }
  140. bool
  141. MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
  142. MachineBasicBlock::const_iterator I,
  143. MachineBasicBlock::const_iterator E) const {
  144. unsigned LookAheadLeft = LookAheadLimit;
  145. while (LookAheadLeft) {
  146. // Skip over dbg_value's.
  147. while (I != E && I->isDebugValue())
  148. ++I;
  149. if (I == E)
  150. // Reached end of block, register is obviously dead.
  151. return true;
  152. bool SeenDef = false;
  153. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
  154. const MachineOperand &MO = I->getOperand(i);
  155. if (!MO.isReg() || !MO.getReg())
  156. continue;
  157. if (!TRI->regsOverlap(MO.getReg(), Reg))
  158. continue;
  159. if (MO.isUse())
  160. // Found a use!
  161. return false;
  162. SeenDef = true;
  163. }
  164. if (SeenDef)
  165. // See a def of Reg (or an alias) before encountering any use, it's
  166. // trivially dead.
  167. return true;
  168. --LookAheadLeft;
  169. ++I;
  170. }
  171. return false;
  172. }
  173. /// hasLivePhysRegDefUses - Return true if the specified instruction read/write
  174. /// physical registers (except for dead defs of physical registers). It also
  175. /// returns the physical register def by reference if it's the only one and the
  176. /// instruction does not uses a physical register.
  177. bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
  178. const MachineBasicBlock *MBB,
  179. SmallSet<unsigned,8> &PhysRefs,
  180. SmallVector<unsigned,2> &PhysDefs) const{
  181. MachineBasicBlock::const_iterator I = MI; I = llvm::next(I);
  182. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  183. const MachineOperand &MO = MI->getOperand(i);
  184. if (!MO.isReg())
  185. continue;
  186. unsigned Reg = MO.getReg();
  187. if (!Reg)
  188. continue;
  189. if (TargetRegisterInfo::isVirtualRegister(Reg))
  190. continue;
  191. // If the def is dead, it's ok. But the def may not marked "dead". That's
  192. // common since this pass is run before livevariables. We can scan
  193. // forward a few instructions and check if it is obviously dead.
  194. if (MO.isDef() &&
  195. (MO.isDead() || isPhysDefTriviallyDead(Reg, I, MBB->end())))
  196. continue;
  197. PhysRefs.insert(Reg);
  198. if (MO.isDef())
  199. PhysDefs.push_back(Reg);
  200. for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
  201. PhysRefs.insert(*Alias);
  202. }
  203. return !PhysRefs.empty();
  204. }
  205. bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
  206. SmallSet<unsigned,8> &PhysRefs,
  207. SmallVector<unsigned,2> &PhysDefs,
  208. bool &NonLocal) const {
  209. // For now conservatively returns false if the common subexpression is
  210. // not in the same basic block as the given instruction. The only exception
  211. // is if the common subexpression is in the sole predecessor block.
  212. const MachineBasicBlock *MBB = MI->getParent();
  213. const MachineBasicBlock *CSMBB = CSMI->getParent();
  214. bool CrossMBB = false;
  215. if (CSMBB != MBB) {
  216. if (MBB->pred_size() != 1 || *MBB->pred_begin() != CSMBB)
  217. return false;
  218. for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
  219. if (TRI->isInAllocatableClass(PhysDefs[i]))
  220. // Avoid extending live range of physical registers unless
  221. // they are unallocatable.
  222. return false;
  223. }
  224. CrossMBB = true;
  225. }
  226. MachineBasicBlock::const_iterator I = CSMI; I = llvm::next(I);
  227. MachineBasicBlock::const_iterator E = MI;
  228. MachineBasicBlock::const_iterator EE = CSMBB->end();
  229. unsigned LookAheadLeft = LookAheadLimit;
  230. while (LookAheadLeft) {
  231. // Skip over dbg_value's.
  232. while (I != E && I != EE && I->isDebugValue())
  233. ++I;
  234. if (I == EE) {
  235. assert(CrossMBB && "Reaching end-of-MBB without finding MI?");
  236. CrossMBB = false;
  237. NonLocal = true;
  238. I = MBB->begin();
  239. EE = MBB->end();
  240. continue;
  241. }
  242. if (I == E)
  243. return true;
  244. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
  245. const MachineOperand &MO = I->getOperand(i);
  246. if (!MO.isReg() || !MO.isDef())
  247. continue;
  248. unsigned MOReg = MO.getReg();
  249. if (TargetRegisterInfo::isVirtualRegister(MOReg))
  250. continue;
  251. if (PhysRefs.count(MOReg))
  252. return false;
  253. }
  254. --LookAheadLeft;
  255. ++I;
  256. }
  257. return false;
  258. }
  259. bool MachineCSE::isCSECandidate(MachineInstr *MI) {
  260. if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() ||
  261. MI->isKill() || MI->isInlineAsm() || MI->isDebugValue())
  262. return false;
  263. // Ignore copies.
  264. if (MI->isCopyLike())
  265. return false;
  266. // Ignore stuff that we obviously can't move.
  267. if (MI->mayStore() || MI->isCall() || MI->isTerminator() ||
  268. MI->hasUnmodeledSideEffects())
  269. return false;
  270. if (MI->mayLoad()) {
  271. // Okay, this instruction does a load. As a refinement, we allow the target
  272. // to decide whether the loaded value is actually a constant. If so, we can
  273. // actually use it as a load.
  274. if (!MI->isInvariantLoad(AA))
  275. // FIXME: we should be able to hoist loads with no other side effects if
  276. // there are no other instructions which can change memory in this loop.
  277. // This is a trivial form of alias analysis.
  278. return false;
  279. }
  280. return true;
  281. }
  282. /// isProfitableToCSE - Return true if it's profitable to eliminate MI with a
  283. /// common expression that defines Reg.
  284. bool MachineCSE::isProfitableToCSE(unsigned CSReg, unsigned Reg,
  285. MachineInstr *CSMI, MachineInstr *MI) {
  286. // FIXME: Heuristics that works around the lack the live range splitting.
  287. // Heuristics #1: Don't CSE "cheap" computation if the def is not local or in
  288. // an immediate predecessor. We don't want to increase register pressure and
  289. // end up causing other computation to be spilled.
  290. if (MI->isAsCheapAsAMove()) {
  291. MachineBasicBlock *CSBB = CSMI->getParent();
  292. MachineBasicBlock *BB = MI->getParent();
  293. if (CSBB != BB && !CSBB->isSuccessor(BB))
  294. return false;
  295. }
  296. // Heuristics #2: If the expression doesn't not use a vr and the only use
  297. // of the redundant computation are copies, do not cse.
  298. bool HasVRegUse = false;
  299. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  300. const MachineOperand &MO = MI->getOperand(i);
  301. if (MO.isReg() && MO.isUse() &&
  302. TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  303. HasVRegUse = true;
  304. break;
  305. }
  306. }
  307. if (!HasVRegUse) {
  308. bool HasNonCopyUse = false;
  309. for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(Reg),
  310. E = MRI->use_nodbg_end(); I != E; ++I) {
  311. MachineInstr *Use = &*I;
  312. // Ignore copies.
  313. if (!Use->isCopyLike()) {
  314. HasNonCopyUse = true;
  315. break;
  316. }
  317. }
  318. if (!HasNonCopyUse)
  319. return false;
  320. }
  321. // Heuristics #3: If the common subexpression is used by PHIs, do not reuse
  322. // it unless the defined value is already used in the BB of the new use.
  323. bool HasPHI = false;
  324. SmallPtrSet<MachineBasicBlock*, 4> CSBBs;
  325. for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(CSReg),
  326. E = MRI->use_nodbg_end(); I != E; ++I) {
  327. MachineInstr *Use = &*I;
  328. HasPHI |= Use->isPHI();
  329. CSBBs.insert(Use->getParent());
  330. }
  331. if (!HasPHI)
  332. return true;
  333. return CSBBs.count(MI->getParent());
  334. }
  335. void MachineCSE::EnterScope(MachineBasicBlock *MBB) {
  336. DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n');
  337. ScopeType *Scope = new ScopeType(VNT);
  338. ScopeMap[MBB] = Scope;
  339. }
  340. void MachineCSE::ExitScope(MachineBasicBlock *MBB) {
  341. DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n');
  342. DenseMap<MachineBasicBlock*, ScopeType*>::iterator SI = ScopeMap.find(MBB);
  343. assert(SI != ScopeMap.end());
  344. ScopeMap.erase(SI);
  345. delete SI->second;
  346. }
  347. bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
  348. bool Changed = false;
  349. SmallVector<std::pair<unsigned, unsigned>, 8> CSEPairs;
  350. for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) {
  351. MachineInstr *MI = &*I;
  352. ++I;
  353. if (!isCSECandidate(MI))
  354. continue;
  355. bool FoundCSE = VNT.count(MI);
  356. if (!FoundCSE) {
  357. // Look for trivial copy coalescing opportunities.
  358. if (PerformTrivialCoalescing(MI, MBB)) {
  359. Changed = true;
  360. // After coalescing MI itself may become a copy.
  361. if (MI->isCopyLike())
  362. continue;
  363. FoundCSE = VNT.count(MI);
  364. }
  365. }
  366. // Commute commutable instructions.
  367. bool Commuted = false;
  368. if (!FoundCSE && MI->isCommutable()) {
  369. MachineInstr *NewMI = TII->commuteInstruction(MI);
  370. if (NewMI) {
  371. Commuted = true;
  372. FoundCSE = VNT.count(NewMI);
  373. if (NewMI != MI) {
  374. // New instruction. It doesn't need to be kept.
  375. NewMI->eraseFromParent();
  376. Changed = true;
  377. } else if (!FoundCSE)
  378. // MI was changed but it didn't help, commute it back!
  379. (void)TII->commuteInstruction(MI);
  380. }
  381. }
  382. // If the instruction defines physical registers and the values *may* be
  383. // used, then it's not safe to replace it with a common subexpression.
  384. // It's also not safe if the instruction uses physical registers.
  385. bool CrossMBBPhysDef = false;
  386. SmallSet<unsigned,8> PhysRefs;
  387. SmallVector<unsigned, 2> PhysDefs;
  388. if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs, PhysDefs)) {
  389. FoundCSE = false;
  390. // ... Unless the CS is local or is in the sole predecessor block
  391. // and it also defines the physical register which is not clobbered
  392. // in between and the physical register uses were not clobbered.
  393. unsigned CSVN = VNT.lookup(MI);
  394. MachineInstr *CSMI = Exps[CSVN];
  395. if (PhysRegDefsReach(CSMI, MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
  396. FoundCSE = true;
  397. }
  398. if (!FoundCSE) {
  399. VNT.insert(MI, CurrVN++);
  400. Exps.push_back(MI);
  401. continue;
  402. }
  403. // Found a common subexpression, eliminate it.
  404. unsigned CSVN = VNT.lookup(MI);
  405. MachineInstr *CSMI = Exps[CSVN];
  406. DEBUG(dbgs() << "Examining: " << *MI);
  407. DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI);
  408. // Check if it's profitable to perform this CSE.
  409. bool DoCSE = true;
  410. unsigned NumDefs = MI->getDesc().getNumDefs();
  411. for (unsigned i = 0, e = MI->getNumOperands(); NumDefs && i != e; ++i) {
  412. MachineOperand &MO = MI->getOperand(i);
  413. if (!MO.isReg() || !MO.isDef())
  414. continue;
  415. unsigned OldReg = MO.getReg();
  416. unsigned NewReg = CSMI->getOperand(i).getReg();
  417. if (OldReg == NewReg)
  418. continue;
  419. assert(TargetRegisterInfo::isVirtualRegister(OldReg) &&
  420. TargetRegisterInfo::isVirtualRegister(NewReg) &&
  421. "Do not CSE physical register defs!");
  422. if (!isProfitableToCSE(NewReg, OldReg, CSMI, MI)) {
  423. DoCSE = false;
  424. break;
  425. }
  426. // Don't perform CSE if the result of the old instruction cannot exist
  427. // within the register class of the new instruction.
  428. const TargetRegisterClass *OldRC = MRI->getRegClass(OldReg);
  429. if (!MRI->constrainRegClass(NewReg, OldRC)) {
  430. DoCSE = false;
  431. break;
  432. }
  433. CSEPairs.push_back(std::make_pair(OldReg, NewReg));
  434. --NumDefs;
  435. }
  436. // Actually perform the elimination.
  437. if (DoCSE) {
  438. for (unsigned i = 0, e = CSEPairs.size(); i != e; ++i) {
  439. MRI->replaceRegWith(CSEPairs[i].first, CSEPairs[i].second);
  440. MRI->clearKillFlags(CSEPairs[i].second);
  441. }
  442. if (CrossMBBPhysDef) {
  443. // Add physical register defs now coming in from a predecessor to MBB
  444. // livein list.
  445. while (!PhysDefs.empty()) {
  446. unsigned LiveIn = PhysDefs.pop_back_val();
  447. if (!MBB->isLiveIn(LiveIn))
  448. MBB->addLiveIn(LiveIn);
  449. }
  450. ++NumCrossBBCSEs;
  451. }
  452. MI->eraseFromParent();
  453. ++NumCSEs;
  454. if (!PhysRefs.empty())
  455. ++NumPhysCSEs;
  456. if (Commuted)
  457. ++NumCommutes;
  458. Changed = true;
  459. } else {
  460. DEBUG(dbgs() << "*** Not profitable, avoid CSE!\n");
  461. VNT.insert(MI, CurrVN++);
  462. Exps.push_back(MI);
  463. }
  464. CSEPairs.clear();
  465. }
  466. return Changed;
  467. }
  468. /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given
  469. /// dominator tree node if its a leaf or all of its children are done. Walk
  470. /// up the dominator tree to destroy ancestors which are now done.
  471. void
  472. MachineCSE::ExitScopeIfDone(MachineDomTreeNode *Node,
  473. DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
  474. DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) {
  475. if (OpenChildren[Node])
  476. return;
  477. // Pop scope.
  478. ExitScope(Node->getBlock());
  479. // Now traverse upwards to pop ancestors whose offsprings are all done.
  480. while (MachineDomTreeNode *Parent = ParentMap[Node]) {
  481. unsigned Left = --OpenChildren[Parent];
  482. if (Left != 0)
  483. break;
  484. ExitScope(Parent->getBlock());
  485. Node = Parent;
  486. }
  487. }
  488. bool MachineCSE::PerformCSE(MachineDomTreeNode *Node) {
  489. SmallVector<MachineDomTreeNode*, 32> Scopes;
  490. SmallVector<MachineDomTreeNode*, 8> WorkList;
  491. DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
  492. DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
  493. CurrVN = 0;
  494. // Perform a DFS walk to determine the order of visit.
  495. WorkList.push_back(Node);
  496. do {
  497. Node = WorkList.pop_back_val();
  498. Scopes.push_back(Node);
  499. const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
  500. unsigned NumChildren = Children.size();
  501. OpenChildren[Node] = NumChildren;
  502. for (unsigned i = 0; i != NumChildren; ++i) {
  503. MachineDomTreeNode *Child = Children[i];
  504. ParentMap[Child] = Node;
  505. WorkList.push_back(Child);
  506. }
  507. } while (!WorkList.empty());
  508. // Now perform CSE.
  509. bool Changed = false;
  510. for (unsigned i = 0, e = Scopes.size(); i != e; ++i) {
  511. MachineDomTreeNode *Node = Scopes[i];
  512. MachineBasicBlock *MBB = Node->getBlock();
  513. EnterScope(MBB);
  514. Changed |= ProcessBlock(MBB);
  515. // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
  516. ExitScopeIfDone(Node, OpenChildren, ParentMap);
  517. }
  518. return Changed;
  519. }
  520. bool MachineCSE::runOnMachineFunction(MachineFunction &MF) {
  521. TII = MF.getTarget().getInstrInfo();
  522. TRI = MF.getTarget().getRegisterInfo();
  523. MRI = &MF.getRegInfo();
  524. AA = &getAnalysis<AliasAnalysis>();
  525. DT = &getAnalysis<MachineDominatorTree>();
  526. return PerformCSE(DT->getRootNode());
  527. }