blob: e5c34201ef974cbe8cc57c982cd9c43edfcf291c [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)) {
Dan Gohman19778e72009-09-25 22:53:29 +0000181 if (MO.isUse()) {
182 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000183 // and we can freely move its uses. Alternatively, if it's allocatable,
184 // it could get allocated to something with a def during allocation.
Dan Gohman19778e72009-09-25 22:53:29 +0000185 if (!RegInfo->def_empty(Reg))
186 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000187 if (AllocatableSet.test(Reg))
188 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000189 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000190 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
191 unsigned AliasReg = *Alias;
192 if (!RegInfo->def_empty(AliasReg))
Dan Gohman19778e72009-09-25 22:53:29 +0000193 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000194 if (AllocatableSet.test(AliasReg))
195 return false;
196 }
Dan Gohman19778e72009-09-25 22:53:29 +0000197 } else if (!MO.isDead()) {
198 // A def that isn't dead. We can't move it.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000199 return false;
Dan Gohman19778e72009-09-25 22:53:29 +0000200 }
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000201 } else {
202 // Virtual register uses are always safe to sink.
203 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000204
205 // If it's not safe to move defs of the register class, then abort.
206 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
207 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000208
Chris Lattnere430e1c2008-01-05 06:47:58 +0000209 // FIXME: This picks a successor to sink into based on having one
210 // successor that dominates all the uses. However, there are cases where
211 // sinking can happen but where the sink point isn't a successor. For
212 // example:
213 // x = computation
214 // if () {} else {}
215 // use x
216 // the instruction could be sunk over the whole diamond for the
217 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
218 // after that.
219
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000220 // Virtual register defs can only be sunk if all their uses are in blocks
221 // dominated by one of the successors.
222 if (SuccToSinkTo) {
223 // If a previous operand picked a block to sink to, then this operand
224 // must be sinkable to the same block.
225 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
226 return false;
227 continue;
228 }
229
230 // Otherwise, we should look at all the successors and decide which one
231 // we should sink to.
232 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
233 E = ParentBlock->succ_end(); SI != E; ++SI) {
234 if (AllUsesDominatedByBlock(Reg, *SI)) {
235 SuccToSinkTo = *SI;
236 break;
237 }
238 }
239
240 // If we couldn't find a block to sink to, ignore this instruction.
241 if (SuccToSinkTo == 0)
242 return false;
243 }
244 }
245
Chris Lattner9bb459b2008-01-05 01:39:17 +0000246 // If there are no outputs, it must have side-effects.
247 if (SuccToSinkTo == 0)
248 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000249
250 // It's not safe to sink instructions to EH landing pad. Control flow into
251 // landing pad is implicitly defined.
252 if (SuccToSinkTo->isLandingPad())
253 return false;
Chris Lattner9bb459b2008-01-05 01:39:17 +0000254
Chris Lattner296185c2009-04-10 16:38:36 +0000255 // If is not possible to sink an instruction into its own block. This can
256 // happen with loops.
257 if (MI->getParent() == SuccToSinkTo)
258 return false;
259
Chris Lattnercf143a42009-08-23 03:13:20 +0000260 DEBUG(errs() << "Sink instr " << *MI);
261 DEBUG(errs() << "to block " << *SuccToSinkTo);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000262
263 // If the block has multiple predecessors, this would introduce computation on
264 // a path that it doesn't already exist. We could split the critical edge,
265 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000266 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000267 if (SuccToSinkTo->pred_size() > 1) {
Chris Lattnercf143a42009-08-23 03:13:20 +0000268 DEBUG(errs() << " *** PUNTING: Critical edge found\n");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000269 return false;
270 }
271
272 // Determine where to insert into. Skip phi nodes.
273 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
274 while (InsertPos != SuccToSinkTo->end() &&
275 InsertPos->getOpcode() == TargetInstrInfo::PHI)
276 ++InsertPos;
277
278 // Move the instruction.
279 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
280 ++MachineBasicBlock::iterator(MI));
281 return true;
282}