blob: f975ad344ade007da7dc342ae88f0e5903fac10c [file] [log] [blame]
Chris Lattnerc4ce73f2008-01-04 07:36:53 +00001//===-- 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 Gohmana5225ad2009-08-05 01:19:01 +000010// 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 Lattnerc4ce73f2008-01-04 07:36:53 +000016//
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 Gohman6f0d0242008-02-10 18:45:23 +000023#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000024#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/Debug.h"
Bill Wendling1e973aa2009-08-22 20:26:23 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000030using namespace llvm;
31
32STATISTIC(NumSunk, "Number of machine instructions sunk");
33
34namespace {
35 class VISIBILITY_HIDDEN MachineSinking : public MachineFunctionPass {
36 const TargetMachine *TM;
37 const TargetInstrInfo *TII;
38 MachineFunction *CurMF; // Current MachineFunction
39 MachineRegisterInfo *RegInfo; // Machine register information
Dan Gohmana5225ad2009-08-05 01:19:01 +000040 MachineDominatorTree *DT; // Machine dominator tree
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000041
42 public:
43 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +000044 MachineSinking() : MachineFunctionPass(&ID) {}
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000045
46 virtual bool runOnMachineFunction(MachineFunction &MF);
47
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000049 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000050 MachineFunctionPass::getAnalysisUsage(AU);
51 AU.addRequired<MachineDominatorTree>();
52 AU.addPreserved<MachineDominatorTree>();
53 }
54 private:
55 bool ProcessBlock(MachineBasicBlock &MBB);
Chris Lattneraad193a2008-01-12 00:17:41 +000056 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000057 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
58 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000059} // end anonymous namespace
Dan Gohman844731a2008-05-13 00:00:25 +000060
61char MachineSinking::ID = 0;
62static RegisterPass<MachineSinking>
63X("machine-sink", "Machine code sinking");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000064
65FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
66
67/// AllUsesDominatedByBlock - Return true if all uses of the specified register
68/// occur in blocks dominated by the specified block.
69bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
70 MachineBasicBlock *MBB) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +000071 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
72 "Only makes sense for vregs");
Dan Gohman29438d12009-09-25 22:24:52 +000073 for (MachineRegisterInfo::use_iterator I = RegInfo->use_begin(Reg),
74 E = RegInfo->use_end(); I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000075 // Determine the block of the use.
76 MachineInstr *UseInst = &*I;
77 MachineBasicBlock *UseBlock = UseInst->getParent();
78 if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
79 // PHI nodes use the operand in the predecessor block, not the block with
80 // the PHI.
81 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
82 }
83 // Check that it dominates.
84 if (!DT->dominates(MBB, UseBlock))
85 return false;
86 }
87 return true;
88}
89
90
91
92bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling1e973aa2009-08-22 20:26:23 +000093 DEBUG(errs() << "******** Machine Sinking ********\n");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000094
95 CurMF = &MF;
96 TM = &CurMF->getTarget();
97 TII = TM->getInstrInfo();
98 RegInfo = &CurMF->getRegInfo();
99 DT = &getAnalysis<MachineDominatorTree>();
100
101 bool EverMadeChange = false;
102
103 while (1) {
104 bool MadeChange = false;
105
106 // Process all basic blocks.
107 for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end();
108 I != E; ++I)
109 MadeChange |= ProcessBlock(*I);
110
111 // If this iteration over the code changed anything, keep iterating.
112 if (!MadeChange) break;
113 EverMadeChange = true;
114 }
115 return EverMadeChange;
116}
117
118bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000119 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000120 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
121
122 bool MadeChange = false;
123
Chris Lattneraad193a2008-01-12 00:17:41 +0000124 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000125 MachineBasicBlock::iterator I = MBB.end();
126 --I;
127 bool ProcessedBegin, SawStore = false;
128 do {
129 MachineInstr *MI = I; // The instruction to sink.
130
131 // Predecrement I (if it's not begin) so that it isn't invalidated by
132 // sinking.
133 ProcessedBegin = I == MBB.begin();
134 if (!ProcessedBegin)
135 --I;
136
137 if (SinkInstruction(MI, SawStore))
138 ++NumSunk, MadeChange = true;
139
140 // If we just processed the first instruction in the block, we're done.
141 } while (!ProcessedBegin);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000142
143 return MadeChange;
144}
145
146/// SinkInstruction - Determine whether it is safe to sink the specified machine
147/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000148bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000149 // Check if it's safe to move the instruction.
150 if (!MI->isSafeToMove(TII, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000151 return false;
Chris Lattnere430e1c2008-01-05 06:47:58 +0000152
153 // FIXME: This should include support for sinking instructions within the
154 // block they are currently in to shorten the live ranges. We often get
155 // instructions sunk into the top of a large block, but it would be better to
156 // also sink them down before their first use in the block. This xform has to
157 // be careful not to *increase* register pressure though, e.g. sinking
158 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000159 // and z and only shrink the live range of x.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000160
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000161 // Loop over all the operands of the specified instruction. If there is
162 // anything we can't handle, bail out.
163 MachineBasicBlock *ParentBlock = MI->getParent();
164
165 // SuccToSinkTo - This is the successor to sink this instruction to, once we
166 // decide.
167 MachineBasicBlock *SuccToSinkTo = 0;
168
169 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
170 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000171 if (!MO.isReg()) continue; // Ignore non-register operands.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000172
173 unsigned Reg = MO.getReg();
174 if (Reg == 0) continue;
175
Dan Gohman6f0d0242008-02-10 18:45:23 +0000176 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000177 // If this is a physical register use, we can't move it. If it is a def,
178 // we can move it, but only if the def is dead.
179 if (MO.isUse() || !MO.isDead())
180 return false;
181 } else {
182 // Virtual register uses are always safe to sink.
183 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000184
185 // If it's not safe to move defs of the register class, then abort.
186 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
187 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000188
Chris Lattnere430e1c2008-01-05 06:47:58 +0000189 // FIXME: This picks a successor to sink into based on having one
190 // successor that dominates all the uses. However, there are cases where
191 // sinking can happen but where the sink point isn't a successor. For
192 // example:
193 // x = computation
194 // if () {} else {}
195 // use x
196 // the instruction could be sunk over the whole diamond for the
197 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
198 // after that.
199
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000200 // Virtual register defs can only be sunk if all their uses are in blocks
201 // dominated by one of the successors.
202 if (SuccToSinkTo) {
203 // If a previous operand picked a block to sink to, then this operand
204 // must be sinkable to the same block.
205 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
206 return false;
207 continue;
208 }
209
210 // Otherwise, we should look at all the successors and decide which one
211 // we should sink to.
212 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
213 E = ParentBlock->succ_end(); SI != E; ++SI) {
214 if (AllUsesDominatedByBlock(Reg, *SI)) {
215 SuccToSinkTo = *SI;
216 break;
217 }
218 }
219
220 // If we couldn't find a block to sink to, ignore this instruction.
221 if (SuccToSinkTo == 0)
222 return false;
223 }
224 }
225
Chris Lattner9bb459b2008-01-05 01:39:17 +0000226 // If there are no outputs, it must have side-effects.
227 if (SuccToSinkTo == 0)
228 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000229
230 // It's not safe to sink instructions to EH landing pad. Control flow into
231 // landing pad is implicitly defined.
232 if (SuccToSinkTo->isLandingPad())
233 return false;
Chris Lattner9bb459b2008-01-05 01:39:17 +0000234
Chris Lattner296185c2009-04-10 16:38:36 +0000235 // If is not possible to sink an instruction into its own block. This can
236 // happen with loops.
237 if (MI->getParent() == SuccToSinkTo)
238 return false;
239
Chris Lattnercf143a42009-08-23 03:13:20 +0000240 DEBUG(errs() << "Sink instr " << *MI);
241 DEBUG(errs() << "to block " << *SuccToSinkTo);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000242
243 // If the block has multiple predecessors, this would introduce computation on
244 // a path that it doesn't already exist. We could split the critical edge,
245 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000246 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000247 if (SuccToSinkTo->pred_size() > 1) {
Chris Lattnercf143a42009-08-23 03:13:20 +0000248 DEBUG(errs() << " *** PUNTING: Critical edge found\n");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000249 return false;
250 }
251
252 // Determine where to insert into. Skip phi nodes.
253 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
254 while (InsertPos != SuccToSinkTo->end() &&
255 InsertPos->getOpcode() == TargetInstrInfo::PHI)
256 ++InsertPos;
257
258 // Move the instruction.
259 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
260 ++MachineBasicBlock::iterator(MI));
261 return true;
262}