blob: 7717fc88cfed109d34b2350bb7f0369d6aedcb5b [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//
10// This pass
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "machine-sink"
15#include "llvm/CodeGen/Passes.h"
16#include "llvm/CodeGen/MachineRegisterInfo.h"
17#include "llvm/CodeGen/MachineDominators.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000018#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000019#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/TargetMachine.h"
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/Debug.h"
24using namespace llvm;
25
26STATISTIC(NumSunk, "Number of machine instructions sunk");
27
28namespace {
29 class VISIBILITY_HIDDEN MachineSinking : public MachineFunctionPass {
30 const TargetMachine *TM;
31 const TargetInstrInfo *TII;
32 MachineFunction *CurMF; // Current MachineFunction
33 MachineRegisterInfo *RegInfo; // Machine register information
34 MachineDominatorTree *DT; // Machine dominator tree for the current Loop
35
36 public:
37 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +000038 MachineSinking() : MachineFunctionPass(&ID) {}
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000039
40 virtual bool runOnMachineFunction(MachineFunction &MF);
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000043 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000044 MachineFunctionPass::getAnalysisUsage(AU);
45 AU.addRequired<MachineDominatorTree>();
46 AU.addPreserved<MachineDominatorTree>();
47 }
48 private:
49 bool ProcessBlock(MachineBasicBlock &MBB);
Chris Lattneraad193a2008-01-12 00:17:41 +000050 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000051 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
52 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000053} // end anonymous namespace
Dan Gohman844731a2008-05-13 00:00:25 +000054
55char MachineSinking::ID = 0;
56static RegisterPass<MachineSinking>
57X("machine-sink", "Machine code sinking");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000058
59FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
60
61/// AllUsesDominatedByBlock - Return true if all uses of the specified register
62/// occur in blocks dominated by the specified block.
63bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
64 MachineBasicBlock *MBB) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +000065 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
66 "Only makes sense for vregs");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000067 for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg),
68 E = RegInfo->reg_end(); I != E; ++I) {
69 if (I.getOperand().isDef()) continue; // ignore def.
70
71 // Determine the block of the use.
72 MachineInstr *UseInst = &*I;
73 MachineBasicBlock *UseBlock = UseInst->getParent();
74 if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
75 // PHI nodes use the operand in the predecessor block, not the block with
76 // the PHI.
77 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
78 }
79 // Check that it dominates.
80 if (!DT->dominates(MBB, UseBlock))
81 return false;
82 }
83 return true;
84}
85
86
87
88bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
89 DOUT << "******** Machine Sinking ********\n";
90
91 CurMF = &MF;
92 TM = &CurMF->getTarget();
93 TII = TM->getInstrInfo();
94 RegInfo = &CurMF->getRegInfo();
95 DT = &getAnalysis<MachineDominatorTree>();
96
97 bool EverMadeChange = false;
98
99 while (1) {
100 bool MadeChange = false;
101
102 // Process all basic blocks.
103 for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end();
104 I != E; ++I)
105 MadeChange |= ProcessBlock(*I);
106
107 // If this iteration over the code changed anything, keep iterating.
108 if (!MadeChange) break;
109 EverMadeChange = true;
110 }
111 return EverMadeChange;
112}
113
114bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000115 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000116 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
117
118 bool MadeChange = false;
119
Chris Lattneraad193a2008-01-12 00:17:41 +0000120 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000121 MachineBasicBlock::iterator I = MBB.end();
122 --I;
123 bool ProcessedBegin, SawStore = false;
124 do {
125 MachineInstr *MI = I; // The instruction to sink.
126
127 // Predecrement I (if it's not begin) so that it isn't invalidated by
128 // sinking.
129 ProcessedBegin = I == MBB.begin();
130 if (!ProcessedBegin)
131 --I;
132
133 if (SinkInstruction(MI, SawStore))
134 ++NumSunk, MadeChange = true;
135
136 // If we just processed the first instruction in the block, we're done.
137 } while (!ProcessedBegin);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000138
139 return MadeChange;
140}
141
142/// SinkInstruction - Determine whether it is safe to sink the specified machine
143/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000144bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000145 // Check if it's safe to move the instruction.
146 if (!MI->isSafeToMove(TII, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000147 return false;
Chris Lattnere430e1c2008-01-05 06:47:58 +0000148
149 // FIXME: This should include support for sinking instructions within the
150 // block they are currently in to shorten the live ranges. We often get
151 // instructions sunk into the top of a large block, but it would be better to
152 // also sink them down before their first use in the block. This xform has to
153 // be careful not to *increase* register pressure though, e.g. sinking
154 // "x = y + z" down if it kills y and z would increase the live ranges of y
155 // and z only the shrink the live range of x.
156
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000157 // Loop over all the operands of the specified instruction. If there is
158 // anything we can't handle, bail out.
159 MachineBasicBlock *ParentBlock = MI->getParent();
160
161 // SuccToSinkTo - This is the successor to sink this instruction to, once we
162 // decide.
163 MachineBasicBlock *SuccToSinkTo = 0;
164
165 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
166 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000167 if (!MO.isReg()) continue; // Ignore non-register operands.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000168
169 unsigned Reg = MO.getReg();
170 if (Reg == 0) continue;
171
Dan Gohman6f0d0242008-02-10 18:45:23 +0000172 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000173 // If this is a physical register use, we can't move it. If it is a def,
174 // we can move it, but only if the def is dead.
175 if (MO.isUse() || !MO.isDead())
176 return false;
177 } else {
178 // Virtual register uses are always safe to sink.
179 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000180
181 // If it's not safe to move defs of the register class, then abort.
182 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
183 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000184
Chris Lattnere430e1c2008-01-05 06:47:58 +0000185 // FIXME: This picks a successor to sink into based on having one
186 // successor that dominates all the uses. However, there are cases where
187 // sinking can happen but where the sink point isn't a successor. For
188 // example:
189 // x = computation
190 // if () {} else {}
191 // use x
192 // the instruction could be sunk over the whole diamond for the
193 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
194 // after that.
195
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000196 // Virtual register defs can only be sunk if all their uses are in blocks
197 // dominated by one of the successors.
198 if (SuccToSinkTo) {
199 // If a previous operand picked a block to sink to, then this operand
200 // must be sinkable to the same block.
201 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
202 return false;
203 continue;
204 }
205
206 // Otherwise, we should look at all the successors and decide which one
207 // we should sink to.
208 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
209 E = ParentBlock->succ_end(); SI != E; ++SI) {
210 if (AllUsesDominatedByBlock(Reg, *SI)) {
211 SuccToSinkTo = *SI;
212 break;
213 }
214 }
215
216 // If we couldn't find a block to sink to, ignore this instruction.
217 if (SuccToSinkTo == 0)
218 return false;
219 }
220 }
221
Chris Lattner9bb459b2008-01-05 01:39:17 +0000222 // If there are no outputs, it must have side-effects.
223 if (SuccToSinkTo == 0)
224 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000225
226 // It's not safe to sink instructions to EH landing pad. Control flow into
227 // landing pad is implicitly defined.
228 if (SuccToSinkTo->isLandingPad())
229 return false;
Chris Lattner9bb459b2008-01-05 01:39:17 +0000230
Chris Lattner296185c2009-04-10 16:38:36 +0000231 // If is not possible to sink an instruction into its own block. This can
232 // happen with loops.
233 if (MI->getParent() == SuccToSinkTo)
234 return false;
235
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000236 DEBUG(cerr << "Sink instr " << *MI);
237 DEBUG(cerr << "to block " << *SuccToSinkTo);
238
239 // If the block has multiple predecessors, this would introduce computation on
240 // a path that it doesn't already exist. We could split the critical edge,
241 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000242 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000243 if (SuccToSinkTo->pred_size() > 1) {
244 DEBUG(cerr << " *** PUNTING: Critical edge found\n");
245 return false;
246 }
247
248 // Determine where to insert into. Skip phi nodes.
249 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
250 while (InsertPos != SuccToSinkTo->end() &&
251 InsertPos->getOpcode() == TargetInstrInfo::PHI)
252 ++InsertPos;
253
254 // Move the instruction.
255 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
256 ++MachineBasicBlock::iterator(MI));
257 return true;
258}