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