blob: 636dad8ffb9c0f94002aacd1fb3005859f4447c6 [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;
Dan Gohman19778e72009-09-25 22:53:29 +000038 const TargetRegisterInfo *TRI;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000039 MachineFunction *CurMF; // Current MachineFunction
40 MachineRegisterInfo *RegInfo; // Machine register information
Dan Gohmana5225ad2009-08-05 01:19:01 +000041 MachineDominatorTree *DT; // Machine dominator tree
Dan Gohman45094e32009-09-26 02:34:00 +000042 BitVector AllocatableSet; // Which physregs are allocatable?
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000043
44 public:
45 static char ID; // Pass identification
Dan Gohmanae73dc12008-09-04 17:05:41 +000046 MachineSinking() : MachineFunctionPass(&ID) {}
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000047
48 virtual bool runOnMachineFunction(MachineFunction &MF);
49
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000051 AU.setPreservesCFG();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000052 MachineFunctionPass::getAnalysisUsage(AU);
53 AU.addRequired<MachineDominatorTree>();
54 AU.addPreserved<MachineDominatorTree>();
55 }
56 private:
57 bool ProcessBlock(MachineBasicBlock &MBB);
Chris Lattneraad193a2008-01-12 00:17:41 +000058 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000059 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
60 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000061} // end anonymous namespace
Dan Gohman844731a2008-05-13 00:00:25 +000062
63char MachineSinking::ID = 0;
64static RegisterPass<MachineSinking>
65X("machine-sink", "Machine code sinking");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000066
67FunctionPass *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.
71bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
72 MachineBasicBlock *MBB) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +000073 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
74 "Only makes sense for vregs");
Dan Gohman29438d12009-09-25 22:24:52 +000075 for (MachineRegisterInfo::use_iterator I = RegInfo->use_begin(Reg),
76 E = RegInfo->use_end(); I != E; ++I) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000077 // Determine the block of the use.
78 MachineInstr *UseInst = &*I;
79 MachineBasicBlock *UseBlock = UseInst->getParent();
80 if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
81 // PHI nodes use the operand in the predecessor block, not the block with
82 // the PHI.
83 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
84 }
85 // Check that it dominates.
86 if (!DT->dominates(MBB, UseBlock))
87 return false;
88 }
89 return true;
90}
91
92
93
94bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling1e973aa2009-08-22 20:26:23 +000095 DEBUG(errs() << "******** Machine Sinking ********\n");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000096
97 CurMF = &MF;
98 TM = &CurMF->getTarget();
99 TII = TM->getInstrInfo();
Dan Gohman19778e72009-09-25 22:53:29 +0000100 TRI = TM->getRegisterInfo();
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000101 RegInfo = &CurMF->getRegInfo();
102 DT = &getAnalysis<MachineDominatorTree>();
Dan Gohman45094e32009-09-26 02:34:00 +0000103 AllocatableSet = TRI->getAllocatableSet(*CurMF);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000104
105 bool EverMadeChange = false;
106
107 while (1) {
108 bool MadeChange = false;
109
110 // Process all basic blocks.
111 for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end();
112 I != E; ++I)
113 MadeChange |= ProcessBlock(*I);
114
115 // If this iteration over the code changed anything, keep iterating.
116 if (!MadeChange) break;
117 EverMadeChange = true;
118 }
119 return EverMadeChange;
120}
121
122bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000123 // Can't sink anything out of a block that has less than two successors.
Chris Lattner296185c2009-04-10 16:38:36 +0000124 if (MBB.succ_size() <= 1 || MBB.empty()) return false;
125
126 bool MadeChange = false;
127
Chris Lattneraad193a2008-01-12 00:17:41 +0000128 // Walk the basic block bottom-up. Remember if we saw a store.
Chris Lattner296185c2009-04-10 16:38:36 +0000129 MachineBasicBlock::iterator I = MBB.end();
130 --I;
131 bool ProcessedBegin, SawStore = false;
132 do {
133 MachineInstr *MI = I; // The instruction to sink.
134
135 // Predecrement I (if it's not begin) so that it isn't invalidated by
136 // sinking.
137 ProcessedBegin = I == MBB.begin();
138 if (!ProcessedBegin)
139 --I;
140
141 if (SinkInstruction(MI, SawStore))
142 ++NumSunk, MadeChange = true;
143
144 // If we just processed the first instruction in the block, we're done.
145 } while (!ProcessedBegin);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000146
147 return MadeChange;
148}
149
150/// SinkInstruction - Determine whether it is safe to sink the specified machine
151/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000152bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000153 // Check if it's safe to move the instruction.
154 if (!MI->isSafeToMove(TII, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000155 return false;
Chris Lattnere430e1c2008-01-05 06:47:58 +0000156
157 // FIXME: This should include support for sinking instructions within the
158 // block they are currently in to shorten the live ranges. We often get
159 // instructions sunk into the top of a large block, but it would be better to
160 // also sink them down before their first use in the block. This xform has to
161 // be careful not to *increase* register pressure though, e.g. sinking
162 // "x = y + z" down if it kills y and z would increase the live ranges of y
Dan Gohmana5225ad2009-08-05 01:19:01 +0000163 // and z and only shrink the live range of x.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000164
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000165 // Loop over all the operands of the specified instruction. If there is
166 // anything we can't handle, bail out.
167 MachineBasicBlock *ParentBlock = MI->getParent();
168
169 // SuccToSinkTo - This is the successor to sink this instruction to, once we
170 // decide.
171 MachineBasicBlock *SuccToSinkTo = 0;
172
173 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
174 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000175 if (!MO.isReg()) continue; // Ignore non-register operands.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000176
177 unsigned Reg = MO.getReg();
178 if (Reg == 0) continue;
179
Dan Gohman6f0d0242008-02-10 18:45:23 +0000180 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000181 // If this is a physical register use, we can't move it. If it is a def,
182 // we can move it, but only if the def is dead.
Dan Gohman19778e72009-09-25 22:53:29 +0000183 if (MO.isUse()) {
184 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000185 // and we can freely move its uses. Alternatively, if it's allocatable,
186 // it could get allocated to something with a def during allocation.
Dan Gohman19778e72009-09-25 22:53:29 +0000187 if (!RegInfo->def_empty(Reg))
188 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000189 if (AllocatableSet.test(Reg))
190 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000191 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000192 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
193 unsigned AliasReg = *Alias;
194 if (!RegInfo->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000195 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000196 if (AllocatableSet.test(AliasReg))
197 return false;
198 }
Dan Gohman19778e72009-09-25 22:53:29 +0000199 } else if (!MO.isDead()) {
200 // A def that isn't dead. We can't move it.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000201 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000202 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000203 } else {
204 // Virtual register uses are always safe to sink.
205 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000206
207 // If it's not safe to move defs of the register class, then abort.
208 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
209 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000210
Chris Lattnere430e1c2008-01-05 06:47:58 +0000211 // FIXME: This picks a successor to sink into based on having one
212 // successor that dominates all the uses. However, there are cases where
213 // sinking can happen but where the sink point isn't a successor. For
214 // example:
215 // x = computation
216 // if () {} else {}
217 // use x
218 // the instruction could be sunk over the whole diamond for the
219 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
220 // after that.
221
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000222 // Virtual register defs can only be sunk if all their uses are in blocks
223 // dominated by one of the successors.
224 if (SuccToSinkTo) {
225 // If a previous operand picked a block to sink to, then this operand
226 // must be sinkable to the same block.
227 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
228 return false;
229 continue;
230 }
231
232 // Otherwise, we should look at all the successors and decide which one
233 // we should sink to.
234 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
235 E = ParentBlock->succ_end(); SI != E; ++SI) {
236 if (AllUsesDominatedByBlock(Reg, *SI)) {
237 SuccToSinkTo = *SI;
238 break;
239 }
240 }
241
242 // If we couldn't find a block to sink to, ignore this instruction.
243 if (SuccToSinkTo == 0)
244 return false;
245 }
246 }
247
Chris Lattner9bb459b2008-01-05 01:39:17 +0000248 // If there are no outputs, it must have side-effects.
249 if (SuccToSinkTo == 0)
250 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000251
252 // It's not safe to sink instructions to EH landing pad. Control flow into
253 // landing pad is implicitly defined.
254 if (SuccToSinkTo->isLandingPad())
255 return false;
Chris Lattner9bb459b2008-01-05 01:39:17 +0000256
Chris Lattner296185c2009-04-10 16:38:36 +0000257 // If is not possible to sink an instruction into its own block. This can
258 // happen with loops.
259 if (MI->getParent() == SuccToSinkTo)
260 return false;
261
Chris Lattnercf143a42009-08-23 03:13:20 +0000262 DEBUG(errs() << "Sink instr " << *MI);
263 DEBUG(errs() << "to block " << *SuccToSinkTo);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000264
265 // If the block has multiple predecessors, this would introduce computation on
266 // a path that it doesn't already exist. We could split the critical edge,
267 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000268 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000269 if (SuccToSinkTo->pred_size() > 1) {
Chris Lattnercf143a42009-08-23 03:13:20 +0000270 DEBUG(errs() << " *** PUNTING: Critical edge found\n");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000271 return false;
272 }
273
274 // Determine where to insert into. Skip phi nodes.
275 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
276 while (InsertPos != SuccToSinkTo->end() &&
277 InsertPos->getOpcode() == TargetInstrInfo::PHI)
278 ++InsertPos;
279
280 // Move the instruction.
281 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
282 ++MachineBasicBlock::iterator(MI));
283 return true;
284}