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