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" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Bill Wendling | 1e973aa | 2009-08-22 20:26:23 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 34 | static cl::opt<bool> |
| 35 | SplitEdges("machine-sink-split", |
| 36 | cl::desc("Split critical edges during machine sinking"), |
| 37 | cl::init(false), cl::Hidden); |
| 38 | static cl::opt<unsigned> |
| 39 | SplitLimit("split-limit", |
| 40 | cl::init(~0u), cl::Hidden); |
| 41 | |
| 42 | STATISTIC(NumSunk, "Number of machine instructions sunk"); |
| 43 | STATISTIC(NumSplit, "Number of critical edges split"); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 46 | class MachineSinking : public MachineFunctionPass { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 47 | const TargetInstrInfo *TII; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 48 | const TargetRegisterInfo *TRI; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 49 | MachineRegisterInfo *RegInfo; // Machine register information |
Dan Gohman | a5225ad | 2009-08-05 01:19:01 +0000 | [diff] [blame] | 50 | MachineDominatorTree *DT; // Machine dominator tree |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 51 | MachineLoopInfo *LI; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 52 | AliasAnalysis *AA; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 53 | BitVector AllocatableSet; // Which physregs are allocatable? |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 54 | |
| 55 | public: |
| 56 | static char ID; // Pass identification |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 57 | MachineSinking() : MachineFunctionPass(ID) {} |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 58 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 59 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 60 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 61 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 62 | AU.setPreservesCFG(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 63 | MachineFunctionPass::getAnalysisUsage(AU); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 64 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 65 | AU.addRequired<MachineDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 66 | AU.addRequired<MachineLoopInfo>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 67 | AU.addPreserved<MachineDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 68 | AU.addPreserved<MachineLoopInfo>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 69 | } |
| 70 | private: |
| 71 | bool ProcessBlock(MachineBasicBlock &MBB); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 72 | MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *From, |
| 73 | MachineBasicBlock *To); |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 74 | bool SinkInstruction(MachineInstr *MI, bool &SawStore); |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 75 | bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB, |
| 76 | MachineBasicBlock *DefMBB, bool &LocalUse) const; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 77 | }; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 78 | } // end anonymous namespace |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 79 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 80 | char MachineSinking::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 81 | INITIALIZE_PASS(MachineSinking, "machine-sink", |
| 82 | "Machine code sinking", false, false); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 83 | |
| 84 | FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); } |
| 85 | |
| 86 | /// AllUsesDominatedByBlock - Return true if all uses of the specified register |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 87 | /// occur in blocks dominated by the specified block. If any use is in the |
| 88 | /// definition block, then return false since it is never legal to move def |
| 89 | /// after uses. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 90 | bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg, |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 91 | MachineBasicBlock *MBB, |
| 92 | MachineBasicBlock *DefMBB, |
| 93 | bool &LocalUse) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 94 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 95 | "Only makes sense for vregs"); |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 96 | // Ignoring debug uses is necessary so debug info doesn't affect the code. |
| 97 | // This may leave a referencing dbg_value in the original block, before |
| 98 | // the definition of the vreg. Dwarf generator handles this although the |
| 99 | // user might not get the right info at runtime. |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 100 | for (MachineRegisterInfo::use_nodbg_iterator |
| 101 | I = RegInfo->use_nodbg_begin(Reg), E = RegInfo->use_nodbg_end(); |
| 102 | I != E; ++I) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 103 | // Determine the block of the use. |
| 104 | MachineInstr *UseInst = &*I; |
| 105 | MachineBasicBlock *UseBlock = UseInst->getParent(); |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 107 | if (UseInst->isPHI()) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 108 | // PHI nodes use the operand in the predecessor block, not the block with |
| 109 | // the PHI. |
| 110 | UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB(); |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame^] | 111 | } else if (UseBlock == DefMBB) { |
| 112 | LocalUse = true; |
| 113 | return false; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 114 | } |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 115 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 116 | // Check that it dominates. |
| 117 | if (!DT->dominates(MBB, UseBlock)) |
| 118 | return false; |
| 119 | } |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 120 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 124 | bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { |
David Greene | c19a9cd | 2010-01-05 01:26:00 +0000 | [diff] [blame] | 125 | DEBUG(dbgs() << "******** Machine Sinking ********\n"); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 4e9785e | 2009-10-19 14:52:05 +0000 | [diff] [blame] | 127 | const TargetMachine &TM = MF.getTarget(); |
| 128 | TII = TM.getInstrInfo(); |
| 129 | TRI = TM.getRegisterInfo(); |
| 130 | RegInfo = &MF.getRegInfo(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 131 | DT = &getAnalysis<MachineDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 132 | LI = &getAnalysis<MachineLoopInfo>(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 133 | AA = &getAnalysis<AliasAnalysis>(); |
Dan Gohman | 4e9785e | 2009-10-19 14:52:05 +0000 | [diff] [blame] | 134 | AllocatableSet = TRI->getAllocatableSet(MF); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 135 | |
| 136 | bool EverMadeChange = false; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 137 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 138 | while (1) { |
| 139 | bool MadeChange = false; |
| 140 | |
| 141 | // Process all basic blocks. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 142 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 143 | I != E; ++I) |
| 144 | MadeChange |= ProcessBlock(*I); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 145 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 146 | // If this iteration over the code changed anything, keep iterating. |
| 147 | if (!MadeChange) break; |
| 148 | EverMadeChange = true; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 150 | return EverMadeChange; |
| 151 | } |
| 152 | |
| 153 | bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 154 | // 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] | 155 | if (MBB.succ_size() <= 1 || MBB.empty()) return false; |
| 156 | |
Dan Gohman | c4ae94d | 2010-04-05 19:17:22 +0000 | [diff] [blame] | 157 | // 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] | 158 | // unprofitable, it can also lead to infinite looping, because in an |
| 159 | // unreachable loop there may be nowhere to stop. |
Dan Gohman | c4ae94d | 2010-04-05 19:17:22 +0000 | [diff] [blame] | 160 | if (!DT->isReachableFromEntry(&MBB)) return false; |
| 161 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 162 | bool MadeChange = false; |
| 163 | |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 164 | // Walk the basic block bottom-up. Remember if we saw a store. |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 165 | MachineBasicBlock::iterator I = MBB.end(); |
| 166 | --I; |
| 167 | bool ProcessedBegin, SawStore = false; |
| 168 | do { |
| 169 | MachineInstr *MI = I; // The instruction to sink. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 171 | // Predecrement I (if it's not begin) so that it isn't invalidated by |
| 172 | // sinking. |
| 173 | ProcessedBegin = I == MBB.begin(); |
| 174 | if (!ProcessedBegin) |
| 175 | --I; |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 176 | |
| 177 | if (MI->isDebugValue()) |
| 178 | continue; |
| 179 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 180 | if (SinkInstruction(MI, SawStore)) |
| 181 | ++NumSunk, MadeChange = true; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 183 | // If we just processed the first instruction in the block, we're done. |
| 184 | } while (!ProcessedBegin); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 185 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 186 | return MadeChange; |
| 187 | } |
| 188 | |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 189 | MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineBasicBlock *FromBB, |
| 190 | MachineBasicBlock *ToBB) { |
| 191 | // Avoid breaking back edge. From == To means backedge for single BB loop. |
| 192 | if (!SplitEdges || NumSplit == SplitLimit || FromBB == ToBB) |
| 193 | return 0; |
| 194 | |
| 195 | // Check for more "complex" loops. |
| 196 | if (LI->getLoopFor(FromBB) != LI->getLoopFor(ToBB) || |
| 197 | !LI->isLoopHeader(ToBB)) { |
| 198 | // It's not always legal to break critical edges and sink the computation |
| 199 | // to the edge. |
| 200 | // |
| 201 | // BB#1: |
| 202 | // v1024 |
| 203 | // Beq BB#3 |
| 204 | // <fallthrough> |
| 205 | // BB#2: |
| 206 | // ... no uses of v1024 |
| 207 | // <fallthrough> |
| 208 | // BB#3: |
| 209 | // ... |
| 210 | // = v1024 |
| 211 | // |
| 212 | // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted: |
| 213 | // |
| 214 | // BB#1: |
| 215 | // ... |
| 216 | // Bne BB#2 |
| 217 | // BB#4: |
| 218 | // v1024 = |
| 219 | // B BB#3 |
| 220 | // BB#2: |
| 221 | // ... no uses of v1024 |
| 222 | // <fallthrough> |
| 223 | // BB#3: |
| 224 | // ... |
| 225 | // = v1024 |
| 226 | // |
| 227 | // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3 |
| 228 | // flow. We need to ensure the new basic block where the computation is |
| 229 | // sunk to dominates all the uses. |
| 230 | // It's only legal to break critical edge and sink the computation to the |
| 231 | // new block if all the predecessors of "To", except for "From", are |
| 232 | // not dominated by "From". Given SSA property, this means these |
| 233 | // predecessors are dominated by "To". |
| 234 | for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(), |
| 235 | E = ToBB->pred_end(); PI != E; ++PI) { |
| 236 | if (*PI == FromBB) |
| 237 | continue; |
| 238 | if (!DT->dominates(ToBB, *PI)) |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | // FIXME: Determine if it's cost effective to break this edge. |
| 243 | return FromBB->SplitCriticalEdge(ToBB, this); |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 249 | /// SinkInstruction - Determine whether it is safe to sink the specified machine |
| 250 | /// instruction out of its current block into a successor. |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 251 | bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) { |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 252 | // Check if it's safe to move the instruction. |
Evan Cheng | ac1abde | 2010-03-02 19:03:01 +0000 | [diff] [blame] | 253 | if (!MI->isSafeToMove(TII, AA, SawStore)) |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 254 | return false; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 255 | |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 256 | // FIXME: This should include support for sinking instructions within the |
| 257 | // block they are currently in to shorten the live ranges. We often get |
| 258 | // instructions sunk into the top of a large block, but it would be better to |
| 259 | // also sink them down before their first use in the block. This xform has to |
| 260 | // be careful not to *increase* register pressure though, e.g. sinking |
| 261 | // "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] | 262 | // and z and only shrink the live range of x. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 263 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 264 | // Loop over all the operands of the specified instruction. If there is |
| 265 | // anything we can't handle, bail out. |
| 266 | MachineBasicBlock *ParentBlock = MI->getParent(); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 267 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 268 | // SuccToSinkTo - This is the successor to sink this instruction to, once we |
| 269 | // decide. |
| 270 | MachineBasicBlock *SuccToSinkTo = 0; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 271 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 272 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 273 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 274 | if (!MO.isReg()) continue; // Ignore non-register operands. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 275 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 276 | unsigned Reg = MO.getReg(); |
| 277 | if (Reg == 0) continue; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 278 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 279 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 280 | if (MO.isUse()) { |
| 281 | // 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] | 282 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 283 | // it could get allocated to something with a def during allocation. |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 284 | if (!RegInfo->def_empty(Reg)) |
| 285 | return false; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 286 | |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 287 | if (AllocatableSet.test(Reg)) |
| 288 | return false; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 289 | |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 290 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 291 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 292 | unsigned AliasReg = *Alias; |
| 293 | if (!RegInfo->def_empty(AliasReg)) |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 294 | return false; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 295 | |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 296 | if (AllocatableSet.test(AliasReg)) |
| 297 | return false; |
| 298 | } |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 299 | } else if (!MO.isDead()) { |
| 300 | // A def that isn't dead. We can't move it. |
| 301 | return false; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 302 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 303 | } else { |
| 304 | // Virtual register uses are always safe to sink. |
| 305 | if (MO.isUse()) continue; |
Evan Cheng | b6f5417 | 2009-02-07 01:21:47 +0000 | [diff] [blame] | 306 | |
| 307 | // If it's not safe to move defs of the register class, then abort. |
| 308 | if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg))) |
| 309 | return false; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 310 | |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 311 | // FIXME: This picks a successor to sink into based on having one |
| 312 | // successor that dominates all the uses. However, there are cases where |
| 313 | // sinking can happen but where the sink point isn't a successor. For |
| 314 | // example: |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 315 | // |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 316 | // x = computation |
| 317 | // if () {} else {} |
| 318 | // use x |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 319 | // |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 320 | // the instruction could be sunk over the whole diamond for the |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 321 | // if/then/else (or loop, etc), allowing it to be sunk into other blocks |
| 322 | // after that. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 323 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 324 | // Virtual register defs can only be sunk if all their uses are in blocks |
| 325 | // dominated by one of the successors. |
| 326 | if (SuccToSinkTo) { |
| 327 | // If a previous operand picked a block to sink to, then this operand |
| 328 | // must be sinkable to the same block. |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame^] | 329 | bool LocalUse = false; |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 330 | if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, ParentBlock, LocalUse)) |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 331 | return false; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 332 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 333 | continue; |
| 334 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 335 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 336 | // Otherwise, we should look at all the successors and decide which one |
| 337 | // we should sink to. |
| 338 | for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(), |
| 339 | E = ParentBlock->succ_end(); SI != E; ++SI) { |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame^] | 340 | bool LocalUse = false; |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 341 | if (AllUsesDominatedByBlock(Reg, *SI, ParentBlock, LocalUse)) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 342 | SuccToSinkTo = *SI; |
| 343 | break; |
| 344 | } |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 345 | if (LocalUse) |
| 346 | // Def is used locally, it's never safe to move this def. |
| 347 | return false; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 348 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 349 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 350 | // If we couldn't find a block to sink to, ignore this instruction. |
| 351 | if (SuccToSinkTo == 0) |
| 352 | return false; |
| 353 | } |
| 354 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 9bb459b | 2008-01-05 01:39:17 +0000 | [diff] [blame] | 356 | // If there are no outputs, it must have side-effects. |
| 357 | if (SuccToSinkTo == 0) |
| 358 | return false; |
Evan Cheng | b599979 | 2009-02-15 08:36:12 +0000 | [diff] [blame] | 359 | |
| 360 | // It's not safe to sink instructions to EH landing pad. Control flow into |
| 361 | // landing pad is implicitly defined. |
| 362 | if (SuccToSinkTo->isLandingPad()) |
| 363 | return false; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 364 | |
Dan Gohman | dfffba6 | 2009-10-19 14:56:05 +0000 | [diff] [blame] | 365 | // 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] | 366 | // happen with loops. |
| 367 | if (MI->getParent() == SuccToSinkTo) |
| 368 | return false; |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 369 | |
Daniel Dunbar | d24c9d5 | 2010-06-23 00:48:25 +0000 | [diff] [blame] | 370 | // If the instruction to move defines a dead physical register which is live |
| 371 | // when leaving the basic block, don't move it because it could turn into a |
| 372 | // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>) |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 373 | for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { |
| 374 | const MachineOperand &MO = MI->getOperand(I); |
| 375 | if (!MO.isReg()) continue; |
| 376 | unsigned Reg = MO.getReg(); |
| 377 | if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
| 378 | if (SuccToSinkTo->isLiveIn(Reg)) |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 379 | return false; |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 380 | } |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 381 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 382 | DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo); |
| 383 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 384 | // If the block has multiple predecessors, this would introduce computation on |
| 385 | // a path that it doesn't already exist. We could split the critical edge, |
| 386 | // but for now we just punt. |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 387 | // FIXME: Split critical edges if not backedges. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 388 | if (SuccToSinkTo->pred_size() > 1) { |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 389 | // We cannot sink a load across a critical edge - there may be stores in |
| 390 | // other code paths. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 391 | bool TryBreak = false; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 392 | bool store = true; |
| 393 | if (!MI->isSafeToMove(TII, AA, store)) { |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 394 | DEBUG(dbgs() << " *** PUNTING: Won't sink load along critical edge.\n"); |
| 395 | TryBreak = true; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // We don't want to sink across a critical edge if we don't dominate the |
| 399 | // successor. We could be introducing calculations to new code paths. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 400 | if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) { |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 401 | DEBUG(dbgs() << " *** PUNTING: Critical edge found\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 402 | TryBreak = true; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 405 | // Don't sink instructions into a loop. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 406 | if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) { |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 407 | DEBUG(dbgs() << " *** PUNTING: Loop header found\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 408 | TryBreak = true; |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 411 | // Otherwise we are OK with sinking along a critical edge. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 412 | if (!TryBreak) |
| 413 | DEBUG(dbgs() << "Sinking along critical edge.\n"); |
| 414 | else { |
| 415 | MachineBasicBlock *NewSucc = SplitCriticalEdge(ParentBlock, SuccToSinkTo); |
| 416 | if (!NewSucc) { |
| 417 | DEBUG(dbgs() << |
| 418 | " *** PUNTING: Not legal or profitable to break critical edge\n"); |
| 419 | return false; |
| 420 | } else { |
| 421 | DEBUG(dbgs() << "*** Splitting critical edge:" |
| 422 | " BB#" << ParentBlock->getNumber() |
| 423 | << " -- BB#" << NewSucc->getNumber() |
| 424 | << " -- BB#" << SuccToSinkTo->getNumber() << '\n'); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 425 | SuccToSinkTo = NewSucc; |
| 426 | ++NumSplit; |
| 427 | } |
| 428 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 429 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 430 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 431 | // Determine where to insert into. Skip phi nodes. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 432 | MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 433 | while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI()) |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 434 | ++InsertPos; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 435 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 436 | // Move the instruction. |
| 437 | SuccToSinkTo->splice(InsertPos, ParentBlock, MI, |
| 438 | ++MachineBasicBlock::iterator(MI)); |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 439 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 440 | // Conservatively, clear any kill flags, since it's possible that they are no |
| 441 | // longer correct. |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 442 | MI->clearKillInfo(); |
| 443 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 444 | return true; |
| 445 | } |