llvm.org GIT mirror llvm / c4ce73f
Add a really quick hack at a machine code sinking pass, enabled with --enable-sinking. It is missing validity checks, so it is known broken. However, it is powerful enough to compile this contrived code: void test1(int C, double A, double B, double *P) { double Tmp = A*A+B*B; *P = C ? Tmp : A; } into: _test1: movsd 8(%esp), %xmm0 cmpl $0, 4(%esp) je LBB1_2 # entry LBB1_1: # entry movsd 16(%esp), %xmm1 mulsd %xmm1, %xmm1 mulsd %xmm0, %xmm0 addsd %xmm1, %xmm0 LBB1_2: # entry movl 24(%esp), %eax movsd %xmm0, (%eax) ret instead of: _test1: movsd 16(%esp), %xmm0 mulsd %xmm0, %xmm0 movsd 8(%esp), %xmm1 movapd %xmm1, %xmm2 mulsd %xmm2, %xmm2 addsd %xmm0, %xmm2 cmpl $0, 4(%esp) je LBB1_2 # entry LBB1_1: # entry movapd %xmm2, %xmm1 LBB1_2: # entry movl 24(%esp), %eax movsd %xmm1, (%eax) ret woo. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45570 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
3 changed file(s) with 219 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
156156 ///
157157 FunctionPass *createMachineLICMPass();
158158
159 /// createMachineSinkingPass - This pass performs sinking on machine
160 /// instructions.
161 FunctionPass *createMachineSinkingPass();
162
159163 } // End llvm namespace
160164
161165 #endif
2828 static cl::opt PrintEmittedAsm("print-emitted-asm", cl::Hidden,
2929 cl::desc("Dump emitter generated instructions as assembly"));
3030
31 // Hidden options to help debugging
32 static cl::opt
33 EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
34 cl::desc("Perform sinking on machine code"));
35
36
3137 FileModel::Model
3238 LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
3339 std::ostream &Out,
6773 PM.add(createMachineFunctionPrinterPass(cerr));
6874
6975 PM.add(createMachineLICMPass());
76
77 if (EnableSinking)
78 PM.add(createMachineSinkingPass());
7079
7180 // Perform register allocation to convert to a concrete x86 representation
7281 PM.add(createRegisterAllocator());
0 //===-- MachineSink.cpp - Sinking for machine instructions ----------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "machine-sink"
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/CodeGen/MachineRegisterInfo.h"
16 #include "llvm/CodeGen/MachineDominators.h"
17 #include "llvm/Target/MRegisterInfo.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 using namespace llvm;
25
26 STATISTIC(NumSunk, "Number of machine instructions sunk");
27
28 namespace {
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
38 MachineSinking() : MachineFunctionPass((intptr_t)&ID) {}
39
40 virtual bool runOnMachineFunction(MachineFunction &MF);
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 MachineFunctionPass::getAnalysisUsage(AU);
44 AU.addRequired();
45 AU.addPreserved();
46 }
47 private:
48 bool ProcessBlock(MachineBasicBlock &MBB);
49 bool SinkInstruction(MachineInstr *MI);
50 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB) const;
51 };
52
53 char MachineSinking::ID = 0;
54 RegisterPass X("machine-sink", "Machine code sinking");
55 } // end anonymous namespace
56
57 FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); }
58
59 /// AllUsesDominatedByBlock - Return true if all uses of the specified register
60 /// occur in blocks dominated by the specified block.
61 bool MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
62 MachineBasicBlock *MBB) const {
63 assert(MRegisterInfo::isVirtualRegister(Reg) && "Only makes sense for vregs");
64 for (MachineRegisterInfo::reg_iterator I = RegInfo->reg_begin(Reg),
65 E = RegInfo->reg_end(); I != E; ++I) {
66 if (I.getOperand().isDef()) continue; // ignore def.
67
68 // Determine the block of the use.
69 MachineInstr *UseInst = &*I;
70 MachineBasicBlock *UseBlock = UseInst->getParent();
71 if (UseInst->getOpcode() == TargetInstrInfo::PHI) {
72 // PHI nodes use the operand in the predecessor block, not the block with
73 // the PHI.
74 UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB();
75 }
76 // Check that it dominates.
77 if (!DT->dominates(MBB, UseBlock))
78 return false;
79 }
80 return true;
81 }
82
83
84
85 bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
86 DOUT << "******** Machine Sinking ********\n";
87
88 CurMF = &MF;
89 TM = &CurMF->getTarget();
90 TII = TM->getInstrInfo();
91 RegInfo = &CurMF->getRegInfo();
92 DT = &getAnalysis();
93
94 bool EverMadeChange = false;
95
96 while (1) {
97 bool MadeChange = false;
98
99 // Process all basic blocks.
100 for (MachineFunction::iterator I = CurMF->begin(), E = CurMF->end();
101 I != E; ++I)
102 MadeChange |= ProcessBlock(*I);
103
104 // If this iteration over the code changed anything, keep iterating.
105 if (!MadeChange) break;
106 EverMadeChange = true;
107 }
108 return EverMadeChange;
109 }
110
111 bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
112 bool MadeChange = false;
113
114 // Can't sink anything out of a block that has less than two successors.
115 if (MBB.succ_size() <= 1) return false;
116
117 // Walk the basic block bottom-up
118 for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){
119 MachineBasicBlock::iterator LastIt = I;
120 if (SinkInstruction(--I)) {
121 I = LastIt;
122 ++NumSunk;
123 }
124 }
125
126 return MadeChange;
127 }
128
129 /// SinkInstruction - Determine whether it is safe to sink the specified machine
130 /// instruction out of its current block into a successor.
131 bool MachineSinking::SinkInstruction(MachineInstr *MI) {
132 // Loop over all the operands of the specified instruction. If there is
133 // anything we can't handle, bail out.
134 MachineBasicBlock *ParentBlock = MI->getParent();
135
136 // SuccToSinkTo - This is the successor to sink this instruction to, once we
137 // decide.
138 MachineBasicBlock *SuccToSinkTo = 0;
139
140 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
141 const MachineOperand &MO = MI->getOperand(i);
142 if (!MO.isReg()) continue; // Ignore non-register operands.
143
144 unsigned Reg = MO.getReg();
145 if (Reg == 0) continue;
146
147 if (MRegisterInfo::isPhysicalRegister(Reg)) {
148 // If this is a physical register use, we can't move it. If it is a def,
149 // we can move it, but only if the def is dead.
150 if (MO.isUse() || !MO.isDead())
151 return false;
152 } else {
153 // Virtual register uses are always safe to sink.
154 if (MO.isUse()) continue;
155
156 // Virtual register defs can only be sunk if all their uses are in blocks
157 // dominated by one of the successors.
158 if (SuccToSinkTo) {
159 // If a previous operand picked a block to sink to, then this operand
160 // must be sinkable to the same block.
161 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo))
162 return false;
163 continue;
164 }
165
166 // Otherwise, we should look at all the successors and decide which one
167 // we should sink to.
168 for (MachineBasicBlock::succ_iterator SI = ParentBlock->succ_begin(),
169 E = ParentBlock->succ_end(); SI != E; ++SI) {
170 if (AllUsesDominatedByBlock(Reg, *SI)) {
171 SuccToSinkTo = *SI;
172 break;
173 }
174 }
175
176 // If we couldn't find a block to sink to, ignore this instruction.
177 if (SuccToSinkTo == 0)
178 return false;
179 }
180 }
181
182 // FIXME: Check that the instr doesn't have side effects etc.
183
184 DEBUG(cerr << "Sink instr " << *MI);
185 DEBUG(cerr << "to block " << *SuccToSinkTo);
186
187 // If the block has multiple predecessors, this would introduce computation on
188 // a path that it doesn't already exist. We could split the critical edge,
189 // but for now we just punt.
190 if (SuccToSinkTo->pred_size() > 1) {
191 DEBUG(cerr << " *** PUNTING: Critical edge found\n");
192 return false;
193 }
194
195 // Determine where to insert into. Skip phi nodes.
196 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
197 while (InsertPos != SuccToSinkTo->end() &&
198 InsertPos->getOpcode() == TargetInstrInfo::PHI)
199 ++InsertPos;
200
201 // Move the instruction.
202 SuccToSinkTo->splice(InsertPos, ParentBlock, MI,
203 ++MachineBasicBlock::iterator(MI));
204 return true;
205 }