MachineCSE.cpp 20 KB

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