MachineSink.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 moves instructions into successor blocks when possible, so that
  11. // they aren't executed on paths where their results aren't needed.
  12. //
  13. // This pass is not intended to be a replacement or a complete alternative
  14. // for an LLVM-IR-level sinking pass. It is only designed to sink simple
  15. // constructs that are not exposed before lowering and instruction selection.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #define DEBUG_TYPE "machine-sink"
  19. #include "llvm/CodeGen/Passes.h"
  20. #include "llvm/CodeGen/MachineRegisterInfo.h"
  21. #include "llvm/CodeGen/MachineDominators.h"
  22. #include "llvm/CodeGen/MachineLoopInfo.h"
  23. #include "llvm/Analysis/AliasAnalysis.h"
  24. #include "llvm/Target/TargetRegisterInfo.h"
  25. #include "llvm/Target/TargetInstrInfo.h"
  26. #include "llvm/Target/TargetMachine.h"
  27. #include "llvm/ADT/SmallSet.h"
  28. #include "llvm/ADT/Statistic.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. using namespace llvm;
  33. static cl::opt<bool>
  34. SplitEdges("machine-sink-split",
  35. cl::desc("Split critical edges during machine sinking"),
  36. cl::init(true), cl::Hidden);
  37. STATISTIC(NumSunk, "Number of machine instructions sunk");
  38. STATISTIC(NumSplit, "Number of critical edges split");
  39. STATISTIC(NumCoalesces, "Number of copies coalesced");
  40. namespace {
  41. class MachineSinking : public MachineFunctionPass {
  42. const TargetInstrInfo *TII;
  43. const TargetRegisterInfo *TRI;
  44. MachineRegisterInfo *MRI; // Machine register information
  45. MachineDominatorTree *DT; // Machine dominator tree
  46. MachineLoopInfo *LI;
  47. AliasAnalysis *AA;
  48. BitVector AllocatableSet; // Which physregs are allocatable?
  49. // Remember which edges have been considered for breaking.
  50. SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8>
  51. CEBCandidates;
  52. public:
  53. static char ID; // Pass identification
  54. MachineSinking() : MachineFunctionPass(ID) {}
  55. virtual bool runOnMachineFunction(MachineFunction &MF);
  56. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  57. AU.setPreservesCFG();
  58. MachineFunctionPass::getAnalysisUsage(AU);
  59. AU.addRequired<AliasAnalysis>();
  60. AU.addRequired<MachineDominatorTree>();
  61. AU.addRequired<MachineLoopInfo>();
  62. AU.addPreserved<MachineDominatorTree>();
  63. AU.addPreserved<MachineLoopInfo>();
  64. }
  65. virtual void releaseMemory() {
  66. CEBCandidates.clear();
  67. }
  68. private:
  69. bool ProcessBlock(MachineBasicBlock &MBB);
  70. bool isWorthBreakingCriticalEdge(MachineInstr *MI,
  71. MachineBasicBlock *From,
  72. MachineBasicBlock *To);
  73. MachineBasicBlock *SplitCriticalEdge(MachineInstr *MI,
  74. MachineBasicBlock *From,
  75. MachineBasicBlock *To,
  76. bool BreakPHIEdge);
  77. bool SinkInstruction(MachineInstr *MI, bool &SawStore);
  78. bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
  79. MachineBasicBlock *DefMBB,
  80. bool &BreakPHIEdge, bool &LocalUse) const;
  81. bool PerformTrivialForwardCoalescing(MachineInstr *MI,
  82. MachineBasicBlock *MBB);
  83. };
  84. } // end anonymous namespace
  85. char MachineSinking::ID = 0;
  86. INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink",
  87. "Machine code sinking", false, false)
  88. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  89. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  90. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  91. INITIALIZE_PASS_END(MachineSinking, "machine-sink",
  92. "Machine code sinking", false, false)
  93. FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
  94. bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI,
  95. MachineBasicBlock *MBB) {
  96. if (!MI->isCopy())
  97. return false;
  98. unsigned SrcReg = MI->getOperand(1).getReg();
  99. unsigned DstReg = MI->getOperand(0).getReg();
  100. if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
  101. !TargetRegisterInfo::isVirtualRegister(DstReg) ||
  102. !MRI->hasOneNonDBGUse(SrcReg))
  103. return false;
  104. const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
  105. const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
  106. if (SRC != DRC)
  107. return false;
  108. MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
  109. if (DefMI->isCopyLike())
  110. return false;
  111. DEBUG(dbgs() << "Coalescing: " << *DefMI);
  112. DEBUG(dbgs() << "*** to: " << *MI);
  113. MRI->replaceRegWith(DstReg, SrcReg);
  114. MI->eraseFromParent();
  115. ++NumCoalesces;
  116. return true;
  117. }
  118. /// AllUsesDominatedByBlock - Return true if all uses of the specified register
  119. /// occur in blocks dominated by the specified block. If any use is in the
  120. /// definition block, then return false since it is never legal to move def
  121. /// after uses.
  122. bool
  123. MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
  124. MachineBasicBlock *MBB,
  125. MachineBasicBlock *DefMBB,
  126. bool &BreakPHIEdge,
  127. bool &LocalUse) const {
  128. assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
  129. "Only makes sense for vregs");
  130. if (MRI->use_nodbg_empty(Reg))
  131. return true;
  132. // Ignoring debug uses is necessary so debug info doesn't affect the code.
  133. // This may leave a referencing dbg_value in the original block, before
  134. // the definition of the vreg. Dwarf generator handles this although the
  135. // user might not get the right info at runtime.
  136. // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
  137. // into and they are all PHI nodes. In this case, machine-sink must break
  138. // the critical edge first. e.g.
  139. //
  140. // BB#1: derived from LLVM BB %bb4.preheader
  141. // Predecessors according to CFG: BB#0
  142. // ...
  143. // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead>
  144. // ...
  145. // JE_4 <BB#37>, %EFLAGS<imp-use>
  146. // Successors according to CFG: BB#37 BB#2
  147. //
  148. // BB#2: derived from LLVM BB %bb.nph
  149. // Predecessors according to CFG: BB#0 BB#1
  150. // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1>
  151. BreakPHIEdge = true;
  152. for (MachineRegisterInfo::use_nodbg_iterator
  153. I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
  154. I != E; ++I) {
  155. MachineInstr *UseInst = &*I;
  156. MachineBasicBlock *UseBlock = UseInst->getParent();
  157. if (!(UseBlock == MBB && UseInst->isPHI() &&
  158. UseInst->getOperand(I.getOperandNo()+1).getMBB() == DefMBB)) {
  159. BreakPHIEdge = false;
  160. break;
  161. }
  162. }
  163. if (BreakPHIEdge)
  164. return true;
  165. for (MachineRegisterInfo::use_nodbg_iterator
  166. I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end();
  167. I != E; ++I) {
  168. // Determine the block of the use.
  169. MachineInstr *UseInst = &*I;
  170. MachineBasicBlock *UseBlock = UseInst->getParent();
  171. if (UseInst->isPHI()) {
  172. // PHI nodes use the operand in the predecessor block, not the block with
  173. // the PHI.
  174. UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
  175. } else if (UseBlock == DefMBB) {
  176. LocalUse = true;
  177. return false;
  178. }
  179. // Check that it dominates.
  180. if (!DT->dominates(MBB, UseBlock))
  181. return false;
  182. }
  183. return true;
  184. }
  185. bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
  186. DEBUG(dbgs() << "******** Machine Sinking ********\n");
  187. const TargetMachine &TM = MF.getTarget();
  188. TII = TM.getInstrInfo();
  189. TRI = TM.getRegisterInfo();
  190. MRI = &MF.getRegInfo();
  191. DT = &getAnalysis<MachineDominatorTree>();
  192. LI = &getAnalysis<MachineLoopInfo>();
  193. AA = &getAnalysis<AliasAnalysis>();
  194. AllocatableSet = TRI->getAllocatableSet(MF);
  195. bool EverMadeChange = false;
  196. while (1) {
  197. bool MadeChange = false;
  198. // Process all basic blocks.
  199. CEBCandidates.clear();
  200. for (MachineFunction::iterator I = MF.begin(), E = MF.end();
  201. I != E; ++I)
  202. MadeChange |= ProcessBlock(*I);
  203. // If this iteration over the code changed anything, keep iterating.
  204. if (!MadeChange) break;
  205. EverMadeChange = true;
  206. }
  207. return EverMadeChange;
  208. }
  209. bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
  210. // Can't sink anything out of a block that has less than two successors.
  211. if (MBB.succ_size() <= 1 || MBB.empty()) return false;
  212. // Don't bother sinking code out of unreachable blocks. In addition to being
  213. // unprofitable, it can also lead to infinite looping, because in an
  214. // unreachable loop there may be nowhere to stop.
  215. if (!DT->isReachableFromEntry(&MBB)) return false;
  216. bool MadeChange = false;
  217. // Walk the basic block bottom-up. Remember if we saw a store.
  218. MachineBasicBlock::iterator I = MBB.end();
  219. --I;
  220. bool ProcessedBegin, SawStore = false;
  221. do {
  222. MachineInstr *MI = I; // The instruction to sink.
  223. // Predecrement I (if it's not begin) so that it isn't invalidated by
  224. // sinking.
  225. ProcessedBegin = I == MBB.begin();
  226. if (!ProcessedBegin)
  227. --I;
  228. if (MI->isDebugValue())
  229. continue;
  230. if (PerformTrivialForwardCoalescing(MI, &MBB))
  231. continue;
  232. if (SinkInstruction(MI, SawStore))
  233. ++NumSunk, MadeChange = true;
  234. // If we just processed the first instruction in the block, we're done.
  235. } while (!ProcessedBegin);
  236. return MadeChange;
  237. }
  238. bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI,
  239. MachineBasicBlock *From,
  240. MachineBasicBlock *To) {
  241. // FIXME: Need much better heuristics.
  242. // If the pass has already considered breaking this edge (during this pass
  243. // through the function), then let's go ahead and break it. This means
  244. // sinking multiple "cheap" instructions into the same block.
  245. if (!CEBCandidates.insert(std::make_pair(From, To)))
  246. return true;
  247. if (!MI->isCopy() && !MI->getDesc().isAsCheapAsAMove())
  248. return true;
  249. // MI is cheap, we probably don't want to break the critical edge for it.
  250. // However, if this would allow some definitions of its source operands
  251. // to be sunk then it's probably worth it.
  252. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  253. const MachineOperand &MO = MI->getOperand(i);
  254. if (!MO.isReg()) continue;
  255. unsigned Reg = MO.getReg();
  256. if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg))
  257. continue;
  258. if (MRI->hasOneNonDBGUse(Reg))
  259. return true;
  260. }
  261. return false;
  262. }
  263. MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineInstr *MI,
  264. MachineBasicBlock *FromBB,
  265. MachineBasicBlock *ToBB,
  266. bool BreakPHIEdge) {
  267. if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
  268. return 0;
  269. // Avoid breaking back edge. From == To means backedge for single BB loop.
  270. if (!SplitEdges || FromBB == ToBB)
  271. return 0;
  272. // Check for backedges of more "complex" loops.
  273. if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
  274. LI->isLoopHeader(ToBB))
  275. return 0;
  276. // It's not always legal to break critical edges and sink the computation
  277. // to the edge.
  278. //
  279. // BB#1:
  280. // v1024
  281. // Beq BB#3
  282. // <fallthrough>
  283. // BB#2:
  284. // ... no uses of v1024
  285. // <fallthrough>
  286. // BB#3:
  287. // ...
  288. // = v1024
  289. //
  290. // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted:
  291. //
  292. // BB#1:
  293. // ...
  294. // Bne BB#2
  295. // BB#4:
  296. // v1024 =
  297. // B BB#3
  298. // BB#2:
  299. // ... no uses of v1024
  300. // <fallthrough>
  301. // BB#3:
  302. // ...
  303. // = v1024
  304. //
  305. // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3
  306. // flow. We need to ensure the new basic block where the computation is
  307. // sunk to dominates all the uses.
  308. // It's only legal to break critical edge and sink the computation to the
  309. // new block if all the predecessors of "To", except for "From", are
  310. // not dominated by "From". Given SSA property, this means these
  311. // predecessors are dominated by "To".
  312. //
  313. // There is no need to do this check if all the uses are PHI nodes. PHI
  314. // sources are only defined on the specific predecessor edges.
  315. if (!BreakPHIEdge) {
  316. for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
  317. E = ToBB->pred_end(); PI != E; ++PI) {
  318. if (*PI == FromBB)
  319. continue;
  320. if (!DT->dominates(ToBB, *PI))
  321. return 0;
  322. }
  323. }
  324. return FromBB->SplitCriticalEdge(ToBB, this);
  325. }
  326. static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) {
  327. return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence();
  328. }
  329. /// SinkInstruction - Determine whether it is safe to sink the specified machine
  330. /// instruction out of its current block into a successor.
  331. bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
  332. // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to
  333. // be close to the source to make it easier to coalesce.
  334. if (AvoidsSinking(MI, MRI))
  335. return false;
  336. // Check if it's safe to move the instruction.
  337. if (!MI->isSafeToMove(TII, AA, SawStore))
  338. return false;
  339. // FIXME: This should include support for sinking instructions within the
  340. // block they are currently in to shorten the live ranges. We often get
  341. // instructions sunk into the top of a large block, but it would be better to
  342. // also sink them down before their first use in the block. This xform has to
  343. // be careful not to *increase* register pressure though, e.g. sinking
  344. // "x = y + z" down if it kills y and z would increase the live ranges of y
  345. // and z and only shrink the live range of x.
  346. // Loop over all the operands of the specified instruction. If there is
  347. // anything we can't handle, bail out.
  348. MachineBasicBlock *ParentBlock = MI->getParent();
  349. // SuccToSinkTo - This is the successor to sink this instruction to, once we
  350. // decide.
  351. MachineBasicBlock *SuccToSinkTo = 0;
  352. bool BreakPHIEdge = false;
  353. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  354. const MachineOperand &MO = MI->getOperand(i);
  355. if (!MO.isReg()) continue; // Ignore non-register operands.
  356. unsigned Reg = MO.getReg();
  357. if (Reg == 0) continue;
  358. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  359. if (MO.isUse()) {
  360. // If the physreg has no defs anywhere, it's just an ambient register
  361. // and we can freely move its uses. Alternatively, if it's allocatable,
  362. // it could get allocated to something with a def during allocation.
  363. if (!MRI->def_empty(Reg))
  364. return false;
  365. if (AllocatableSet.test(Reg))
  366. return false;
  367. // Check for a def among the register's aliases too.
  368. for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
  369. unsigned AliasReg = *Alias;
  370. if (!MRI->def_empty(AliasReg))
  371. return false;
  372. if (AllocatableSet.test(AliasReg))
  373. return false;
  374. }
  375. } else if (!MO.isDead()) {
  376. // A def that isn't dead. We can't move it.
  377. return false;
  378. }
  379. } else {
  380. // Virtual register uses are always safe to sink.
  381. if (MO.isUse()) continue;
  382. // If it's not safe to move defs of the register class, then abort.
  383. if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
  384. return false;
  385. // FIXME: This picks a successor to sink into based on having one
  386. // successor that dominates all the uses. However, there are cases where
  387. // sinking can happen but where the sink point isn't a successor. For
  388. // example:
  389. //
  390. // x = computation
  391. // if () {} else {}
  392. // use x
  393. //
  394. // the instruction could be sunk over the whole diamond for the
  395. // if/then/else (or loop, etc), allowing it to be sunk into other blocks
  396. // after that.
  397. // Virtual register defs can only be sunk if all their uses are in blocks
  398. // dominated by one of the successors.
  399. if (SuccToSinkTo) {
  400. // If a previous operand picked a block to sink to, then this operand
  401. // must be sinkable to the same block.
  402. bool LocalUse = false;
  403. if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock,
  404. BreakPHIEdge, LocalUse))
  405. return false;
  406. continue;
  407. }
  408. // Otherwise, we should look at all the successors and decide which one
  409. // we should sink to.
  410. for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
  411. E = ParentBlock->succ_end(); SI != E; ++SI) {
  412. bool LocalUse = false;
  413. if (AllUsesDominatedByBlock(Reg, *SI, ParentBlock,
  414. BreakPHIEdge, LocalUse)) {
  415. SuccToSinkTo = *SI;
  416. break;
  417. }
  418. if (LocalUse)
  419. // Def is used locally, it's never safe to move this def.
  420. return false;
  421. }
  422. // If we couldn't find a block to sink to, ignore this instruction.
  423. if (SuccToSinkTo == 0)
  424. return false;
  425. }
  426. }
  427. // If there are no outputs, it must have side-effects.
  428. if (SuccToSinkTo == 0)
  429. return false;
  430. // It's not safe to sink instructions to EH landing pad. Control flow into
  431. // landing pad is implicitly defined.
  432. if (SuccToSinkTo->isLandingPad())
  433. return false;
  434. // It is not possible to sink an instruction into its own block. This can
  435. // happen with loops.
  436. if (MI->getParent() == SuccToSinkTo)
  437. return false;
  438. // If the instruction to move defines a dead physical register which is live
  439. // when leaving the basic block, don't move it because it could turn into a
  440. // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
  441. for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
  442. const MachineOperand &MO = MI->getOperand(I);
  443. if (!MO.isReg()) continue;
  444. unsigned Reg = MO.getReg();
  445. if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
  446. if (SuccToSinkTo->isLiveIn(Reg))
  447. return false;
  448. }
  449. DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo);
  450. // If the block has multiple predecessors, this would introduce computation on
  451. // a path that it doesn't already exist. We could split the critical edge,
  452. // but for now we just punt.
  453. if (SuccToSinkTo->pred_size() > 1) {
  454. // We cannot sink a load across a critical edge - there may be stores in
  455. // other code paths.
  456. bool TryBreak = false;
  457. bool store = true;
  458. if (!MI->isSafeToMove(TII, AA, store)) {
  459. DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
  460. TryBreak = true;
  461. }
  462. // We don't want to sink across a critical edge if we don't dominate the
  463. // successor. We could be introducing calculations to new code paths.
  464. if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
  465. DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
  466. TryBreak = true;
  467. }
  468. // Don't sink instructions into a loop.
  469. if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
  470. DEBUG(dbgs() << " *** NOTE: Loop header found\n");
  471. TryBreak = true;
  472. }
  473. // Otherwise we are OK with sinking along a critical edge.
  474. if (!TryBreak)
  475. DEBUG(dbgs() << "Sinking along critical edge.\n");
  476. else {
  477. MachineBasicBlock *NewSucc =
  478. SplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
  479. if (!NewSucc) {
  480. DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
  481. "break critical edge\n");
  482. return false;
  483. } else {
  484. DEBUG(dbgs() << " *** Splitting critical edge:"
  485. " BB#" << ParentBlock->getNumber()
  486. << " -- BB#" << NewSucc->getNumber()
  487. << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
  488. SuccToSinkTo = NewSucc;
  489. ++NumSplit;
  490. BreakPHIEdge = false;
  491. }
  492. }
  493. }
  494. if (BreakPHIEdge) {
  495. // BreakPHIEdge is true if all the uses are in the successor MBB being
  496. // sunken into and they are all PHI nodes. In this case, machine-sink must
  497. // break the critical edge first.
  498. MachineBasicBlock *NewSucc = SplitCriticalEdge(MI, ParentBlock,
  499. SuccToSinkTo, BreakPHIEdge);
  500. if (!NewSucc) {
  501. DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
  502. "break critical edge\n");
  503. return false;
  504. }
  505. DEBUG(dbgs() << " *** Splitting critical edge:"
  506. " BB#" << ParentBlock->getNumber()
  507. << " -- BB#" << NewSucc->getNumber()
  508. << " -- BB#" << SuccToSinkTo->getNumber() << '\n');
  509. SuccToSinkTo = NewSucc;
  510. ++NumSplit;
  511. }
  512. // Determine where to insert into. Skip phi nodes.
  513. MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
  514. while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
  515. ++InsertPos;
  516. // Move the instruction.
  517. SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
  518. ++MachineBasicBlock::iterator(MI));
  519. // Conservatively, clear any kill flags, since it's possible that they are no
  520. // longer correct.
  521. MI->clearKillInfo();
  522. return true;
  523. }