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 | // |
Dan Gohman | a5225ad | 2009-08-05 01:19:01 +0000 | [diff] [blame] | 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. |
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" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Bill Wendling | 1e973aa | 2009-08-22 20:26:23 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | STATISTIC(NumSunk, "Number of machine instructions sunk"); |
| 33 | |
| 34 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 35 | class MachineSinking : public MachineFunctionPass { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 36 | const TargetInstrInfo *TII; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 37 | const TargetRegisterInfo *TRI; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 38 | MachineRegisterInfo *RegInfo; // Machine register information |
Dan Gohman | a5225ad | 2009-08-05 01:19:01 +0000 | [diff] [blame] | 39 | MachineDominatorTree *DT; // Machine dominator tree |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 40 | AliasAnalysis *AA; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 41 | BitVector AllocatableSet; // Which physregs are allocatable? |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 42 | |
| 43 | public: |
| 44 | static char ID; // Pass identification |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 45 | MachineSinking() : MachineFunctionPass(&ID) {} |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 46 | |
| 47 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 48 | |
| 49 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 50 | AU.setPreservesCFG(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 51 | MachineFunctionPass::getAnalysisUsage(AU); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 52 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 53 | AU.addRequired<MachineDominatorTree>(); |
| 54 | AU.addPreserved<MachineDominatorTree>(); |
| 55 | } |
| 56 | private: |
| 57 | bool ProcessBlock(MachineBasicBlock &MBB); |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 58 | bool SinkInstruction(MachineInstr *MI, bool &SawStore); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 59 | bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const; |
| 60 | }; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 61 | } // end anonymous namespace |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 62 | |
| 63 | char MachineSinking::ID = 0; |
| 64 | static RegisterPass<MachineSinking> |
| 65 | X("machine-sink", "Machine code sinking"); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 66 | |
| 67 | FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); } |
| 68 | |
| 69 | /// AllUsesDominatedByBlock - Return true if all uses of the specified register |
| 70 | /// occur in blocks dominated by the specified block. |
| 71 | bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg, |
| 72 | MachineBasicBlock *MBB) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 73 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 74 | "Only makes sense for vregs"); |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 75 | // Ignoring debug uses is necessary so debug info doesn't affect the code. |
| 76 | // This may leave a referencing dbg_value in the original block, before |
| 77 | // the definition of the vreg. Dwarf generator handles this although the |
| 78 | // user might not get the right info at runtime. |
| 79 | for (MachineRegisterInfo::use_nodbg_iterator I = |
| 80 | RegInfo->use_nodbg_begin(Reg), |
| 81 | E = RegInfo->use_nodbg_end(); I != E; ++I) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 82 | // Determine the block of the use. |
| 83 | MachineInstr *UseInst = &*I; |
| 84 | MachineBasicBlock *UseBlock = UseInst->getParent(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 85 | if (UseInst->isPHI()) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 86 | // PHI nodes use the operand in the predecessor block, not the block with |
| 87 | // the PHI. |
| 88 | UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB(); |
| 89 | } |
| 90 | // Check that it dominates. |
| 91 | if (!DT->dominates(MBB, UseBlock)) |
| 92 | return false; |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 97 | bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { |
David Greene | c19a9cd | 2010-01-05 01:26:00 +0000 | [diff] [blame] | 98 | DEBUG(dbgs() << "******** Machine Sinking ********\n"); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 99 | |
Dan Gohman | 4e9785e | 2009-10-19 14:52:05 +0000 | [diff] [blame] | 100 | const TargetMachine &TM = MF.getTarget(); |
| 101 | TII = TM.getInstrInfo(); |
| 102 | TRI = TM.getRegisterInfo(); |
| 103 | RegInfo = &MF.getRegInfo(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 104 | DT = &getAnalysis<MachineDominatorTree>(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 105 | AA = &getAnalysis<AliasAnalysis>(); |
Dan Gohman | 4e9785e | 2009-10-19 14:52:05 +0000 | [diff] [blame] | 106 | AllocatableSet = TRI->getAllocatableSet(MF); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 107 | |
| 108 | bool EverMadeChange = false; |
| 109 | |
| 110 | while (1) { |
| 111 | bool MadeChange = false; |
| 112 | |
| 113 | // Process all basic blocks. |
Dan Gohman | 4e9785e | 2009-10-19 14:52:05 +0000 | [diff] [blame] | 114 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 115 | I != E; ++I) |
| 116 | MadeChange |= ProcessBlock(*I); |
| 117 | |
| 118 | // If this iteration over the code changed anything, keep iterating. |
| 119 | if (!MadeChange) break; |
| 120 | EverMadeChange = true; |
| 121 | } |
| 122 | return EverMadeChange; |
| 123 | } |
| 124 | |
| 125 | bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 126 | // 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] | 127 | if (MBB.succ_size() <= 1 || MBB.empty()) return false; |
| 128 | |
| 129 | bool MadeChange = false; |
| 130 | |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 131 | // Walk the basic block bottom-up. Remember if we saw a store. |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 132 | MachineBasicBlock::iterator I = MBB.end(); |
| 133 | --I; |
| 134 | bool ProcessedBegin, SawStore = false; |
| 135 | do { |
| 136 | MachineInstr *MI = I; // The instruction to sink. |
| 137 | |
| 138 | // Predecrement I (if it's not begin) so that it isn't invalidated by |
| 139 | // sinking. |
| 140 | ProcessedBegin = I == MBB.begin(); |
| 141 | if (!ProcessedBegin) |
| 142 | --I; |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 143 | |
| 144 | if (MI->isDebugValue()) |
| 145 | continue; |
| 146 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 147 | if (SinkInstruction(MI, SawStore)) |
| 148 | ++NumSunk, MadeChange = true; |
| 149 | |
| 150 | // If we just processed the first instruction in the block, we're done. |
| 151 | } while (!ProcessedBegin); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 152 | |
| 153 | return MadeChange; |
| 154 | } |
| 155 | |
| 156 | /// SinkInstruction - Determine whether it is safe to sink the specified machine |
| 157 | /// instruction out of its current block into a successor. |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 158 | bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) { |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 159 | // Check if it's safe to move the instruction. |
Evan Cheng | ac1abde | 2010-03-02 19:03:01 +0000 | [diff] [blame] | 160 | if (!MI->isSafeToMove(TII, AA, SawStore)) |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 161 | return false; |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 162 | |
| 163 | // FIXME: This should include support for sinking instructions within the |
| 164 | // block they are currently in to shorten the live ranges. We often get |
| 165 | // instructions sunk into the top of a large block, but it would be better to |
| 166 | // also sink them down before their first use in the block. This xform has to |
| 167 | // be careful not to *increase* register pressure though, e.g. sinking |
| 168 | // "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] | 169 | // and z and only shrink the live range of x. |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 170 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 171 | // Loop over all the operands of the specified instruction. If there is |
| 172 | // anything we can't handle, bail out. |
| 173 | MachineBasicBlock *ParentBlock = MI->getParent(); |
| 174 | |
| 175 | // SuccToSinkTo - This is the successor to sink this instruction to, once we |
| 176 | // decide. |
| 177 | MachineBasicBlock *SuccToSinkTo = 0; |
| 178 | |
| 179 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 180 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 181 | if (!MO.isReg()) continue; // Ignore non-register operands. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 182 | |
| 183 | unsigned Reg = MO.getReg(); |
| 184 | if (Reg == 0) continue; |
| 185 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 186 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 187 | if (MO.isUse()) { |
| 188 | // 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] | 189 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 190 | // it could get allocated to something with a def during allocation. |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 191 | if (!RegInfo->def_empty(Reg)) |
| 192 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 193 | if (AllocatableSet.test(Reg)) |
| 194 | return false; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 195 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 196 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 197 | unsigned AliasReg = *Alias; |
| 198 | if (!RegInfo->def_empty(AliasReg)) |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 199 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 200 | if (AllocatableSet.test(AliasReg)) |
| 201 | return false; |
| 202 | } |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 203 | } else if (!MO.isDead()) { |
| 204 | // A def that isn't dead. We can't move it. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 205 | return false; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 207 | } else { |
| 208 | // Virtual register uses are always safe to sink. |
| 209 | if (MO.isUse()) continue; |
Evan Cheng | b6f5417 | 2009-02-07 01:21:47 +0000 | [diff] [blame] | 210 | |
| 211 | // If it's not safe to move defs of the register class, then abort. |
| 212 | if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg))) |
| 213 | return false; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 214 | |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 215 | // FIXME: This picks a successor to sink into based on having one |
| 216 | // successor that dominates all the uses. However, there are cases where |
| 217 | // sinking can happen but where the sink point isn't a successor. For |
| 218 | // example: |
| 219 | // x = computation |
| 220 | // if () {} else {} |
| 221 | // use x |
| 222 | // the instruction could be sunk over the whole diamond for the |
| 223 | // if/then/else (or loop, etc), allowing it to be sunk into other blocks |
| 224 | // after that. |
| 225 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 226 | // Virtual register defs can only be sunk if all their uses are in blocks |
| 227 | // dominated by one of the successors. |
| 228 | if (SuccToSinkTo) { |
| 229 | // If a previous operand picked a block to sink to, then this operand |
| 230 | // must be sinkable to the same block. |
| 231 | if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo)) |
| 232 | return false; |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | // Otherwise, we should look at all the successors and decide which one |
| 237 | // we should sink to. |
| 238 | for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(), |
| 239 | E = ParentBlock->succ_end(); SI != E; ++SI) { |
| 240 | if (AllUsesDominatedByBlock(Reg, *SI)) { |
| 241 | SuccToSinkTo = *SI; |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // If we couldn't find a block to sink to, ignore this instruction. |
| 247 | if (SuccToSinkTo == 0) |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
Chris Lattner | 9bb459b | 2008-01-05 01:39:17 +0000 | [diff] [blame] | 252 | // If there are no outputs, it must have side-effects. |
| 253 | if (SuccToSinkTo == 0) |
| 254 | return false; |
Evan Cheng | b599979 | 2009-02-15 08:36:12 +0000 | [diff] [blame] | 255 | |
| 256 | // It's not safe to sink instructions to EH landing pad. Control flow into |
| 257 | // landing pad is implicitly defined. |
| 258 | if (SuccToSinkTo->isLandingPad()) |
| 259 | return false; |
Chris Lattner | 9bb459b | 2008-01-05 01:39:17 +0000 | [diff] [blame] | 260 | |
Dan Gohman | dfffba6 | 2009-10-19 14:56:05 +0000 | [diff] [blame] | 261 | // 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] | 262 | // happen with loops. |
| 263 | if (MI->getParent() == SuccToSinkTo) |
| 264 | return false; |
| 265 | |
David Greene | c19a9cd | 2010-01-05 01:26:00 +0000 | [diff] [blame] | 266 | DEBUG(dbgs() << "Sink instr " << *MI); |
| 267 | DEBUG(dbgs() << "to block " << *SuccToSinkTo); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 268 | |
| 269 | // If the block has multiple predecessors, this would introduce computation on |
| 270 | // a path that it doesn't already exist. We could split the critical edge, |
| 271 | // but for now we just punt. |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 272 | // FIXME: Split critical edges if not backedges. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 273 | if (SuccToSinkTo->pred_size() > 1) { |
David Greene | c19a9cd | 2010-01-05 01:26:00 +0000 | [diff] [blame] | 274 | DEBUG(dbgs() << " *** PUNTING: Critical edge found\n"); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
| 278 | // Determine where to insert into. Skip phi nodes. |
| 279 | MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 280 | while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI()) |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 281 | ++InsertPos; |
| 282 | |
| 283 | // Move the instruction. |
| 284 | SuccToSinkTo->splice(InsertPos, ParentBlock, MI, |
| 285 | ++MachineBasicBlock::iterator(MI)); |
| 286 | return true; |
| 287 | } |