blob: 468bd01548c244dacfaa675c275d4116941cb90c [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 {
43 MachineFunctionPass::getAnalysisUsage(AU);
44 AU.addRequired<MachineDominatorTree>();
45 AU.addPreserved<MachineDominatorTree>();
46 }
47 private:
48 bool ProcessBlock(MachineBasicBlock &MBB);
Chris Lattneraad193a2008-01-12 00:17:41 +000049 bool SinkInstruction(MachineInstr *MI, bool &SawStore);
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000050 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
51 };
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000052} // end anonymous namespace
Dan Gohman844731a2008-05-13 00:00:25 +000053
54char MachineSinking::ID = 0;
55static RegisterPass<MachineSinking>
56X("machine-sink", "Machine code sinking");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000057
58FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
59
60/// AllUsesDominatedByBlock - Return true if all uses of the specified register
61/// occur in blocks dominated by the specified block.
62bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
63 MachineBasicBlock *MBB) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +000064 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
65 "Only makes sense for vregs");
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000066 for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg),
67 E = RegInfo->reg_end(); I != E; ++I) {
68 if (I.getOperand().isDef()) continue; // ignore def.
69
70 // Determine the block of the use.
71 MachineInstr *UseInst = &*I;
72 MachineBasicBlock *UseBlock = UseInst->getParent();
73 if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
74 // PHI nodes use the operand in the predecessor block, not the block with
75 // the PHI.
76 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
77 }
78 // Check that it dominates.
79 if (!DT->dominates(MBB, UseBlock))
80 return false;
81 }
82 return true;
83}
84
85
86
87bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
88 DOUT << "******** Machine Sinking ********\n";
89
90 CurMF = &MF;
91 TM = &CurMF->getTarget();
92 TII = TM->getInstrInfo();
93 RegInfo = &CurMF->getRegInfo();
94 DT = &getAnalysis<MachineDominatorTree>();
95
96 bool EverMadeChange = false;
97
98 while (1) {
99 bool MadeChange = false;
100
101 // Process all basic blocks.
102 for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end();
103 I != E; ++I)
104 MadeChange |= ProcessBlock(*I);
105
106 // If this iteration over the code changed anything, keep iterating.
107 if (!MadeChange) break;
108 EverMadeChange = true;
109 }
110 return EverMadeChange;
111}
112
113bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
114 bool MadeChange = false;
115
116 // Can't sink anything out of a block that has less than two successors.
117 if (MBB.succ_size() <= 1) return false;
118
Chris Lattneraad193a2008-01-12 00:17:41 +0000119 // Walk the basic block bottom-up. Remember if we saw a store.
120 bool SawStore = false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000121 for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){
122 MachineBasicBlock::iterator LastIt = I;
Chris Lattneraad193a2008-01-12 00:17:41 +0000123 if (SinkInstruction(--I, SawStore)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000124 I = LastIt;
125 ++NumSunk;
126 }
127 }
128
129 return MadeChange;
130}
131
132/// SinkInstruction - Determine whether it is safe to sink the specified machine
133/// instruction out of its current block into a successor.
Chris Lattneraad193a2008-01-12 00:17:41 +0000134bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) {
Evan Chengb27087f2008-03-13 00:44:09 +0000135 // Check if it's safe to move the instruction.
136 if (!MI->isSafeToMove(TII, SawStore))
Chris Lattneraad193a2008-01-12 00:17:41 +0000137 return false;
Chris Lattnere430e1c2008-01-05 06:47:58 +0000138
139 // FIXME: This should include support for sinking instructions within the
140 // block they are currently in to shorten the live ranges. We often get
141 // instructions sunk into the top of a large block, but it would be better to
142 // also sink them down before their first use in the block. This xform has to
143 // be careful not to *increase* register pressure though, e.g. sinking
144 // "x = y + z" down if it kills y and z would increase the live ranges of y
145 // and z only the shrink the live range of x.
146
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000147 // Loop over all the operands of the specified instruction. If there is
148 // anything we can't handle, bail out.
149 MachineBasicBlock *ParentBlock = MI->getParent();
150
151 // SuccToSinkTo - This is the successor to sink this instruction to, once we
152 // decide.
153 MachineBasicBlock *SuccToSinkTo = 0;
154
155 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
156 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000157 if (!MO.isReg()) continue; // Ignore non-register operands.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000158
159 unsigned Reg = MO.getReg();
160 if (Reg == 0) continue;
161
Dan Gohman6f0d0242008-02-10 18:45:23 +0000162 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000163 // If this is a physical register use, we can't move it. If it is a def,
164 // we can move it, but only if the def is dead.
165 if (MO.isUse() || !MO.isDead())
166 return false;
167 } else {
168 // Virtual register uses are always safe to sink.
169 if (MO.isUse()) continue;
Evan Chengb6f54172009-02-07 01:21:47 +0000170
171 // If it's not safe to move defs of the register class, then abort.
172 if (!TII->isSafeToMoveRegClassDefs(RegInfo->getRegClass(Reg)))
173 return false;
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000174
Chris Lattnere430e1c2008-01-05 06:47:58 +0000175 // FIXME: This picks a successor to sink into based on having one
176 // successor that dominates all the uses. However, there are cases where
177 // sinking can happen but where the sink point isn't a successor. For
178 // example:
179 // x = computation
180 // if () {} else {}
181 // use x
182 // the instruction could be sunk over the whole diamond for the
183 // if/then/else (or loop, etc), allowing it to be sunk into other blocks
184 // after that.
185
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000186 // Virtual register defs can only be sunk if all their uses are in blocks
187 // dominated by one of the successors.
188 if (SuccToSinkTo) {
189 // If a previous operand picked a block to sink to, then this operand
190 // must be sinkable to the same block.
191 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
192 return false;
193 continue;
194 }
195
196 // Otherwise, we should look at all the successors and decide which one
197 // we should sink to.
198 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
199 E = ParentBlock->succ_end(); SI != E; ++SI) {
200 if (AllUsesDominatedByBlock(Reg, *SI)) {
201 SuccToSinkTo = *SI;
202 break;
203 }
204 }
205
206 // If we couldn't find a block to sink to, ignore this instruction.
207 if (SuccToSinkTo == 0)
208 return false;
209 }
210 }
211
Chris Lattner9bb459b2008-01-05 01:39:17 +0000212 // If there are no outputs, it must have side-effects.
213 if (SuccToSinkTo == 0)
214 return false;
Evan Chengb5999792009-02-15 08:36:12 +0000215
216 // It's not safe to sink instructions to EH landing pad. Control flow into
217 // landing pad is implicitly defined.
218 if (SuccToSinkTo->isLandingPad())
219 return false;
Chris Lattner9bb459b2008-01-05 01:39:17 +0000220
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000221 DEBUG(cerr << "Sink instr " << *MI);
222 DEBUG(cerr << "to block " << *SuccToSinkTo);
223
224 // If the block has multiple predecessors, this would introduce computation on
225 // a path that it doesn't already exist. We could split the critical edge,
226 // but for now we just punt.
Chris Lattnere430e1c2008-01-05 06:47:58 +0000227 // FIXME: Split critical edges if not backedges.
Chris Lattnerc4ce73f2008-01-04 07:36:53 +0000228 if (SuccToSinkTo->pred_size() > 1) {
229 DEBUG(cerr << " *** PUNTING: Critical edge found\n");
230 return false;
231 }
232
233 // Determine where to insert into. Skip phi nodes.
234 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
235 while (InsertPos != SuccToSinkTo->end() &&
236 InsertPos->getOpcode() == TargetInstrInfo::PHI)
237 ++InsertPos;
238
239 // Move the instruction.
240 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
241 ++MachineBasicBlock::iterator(MI));
242 return true;
243}