blob: c5f555b02cc47e08c8b85c67160f6ccf21665b9e [file] [log] [blame]
Evan Chengc6fe3332010-03-02 02:38:24 +00001//===-- MachineCSE.cpp - Machine Common Subexpression Elimination Pass ----===//
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 performs global common subexpression elimination on machine
Evan Chengc5bbba12010-03-02 19:02:27 +000011// instructions using a scoped hash table based value numbering scheme. It
Evan Chengc6fe3332010-03-02 02:38:24 +000012// must be run while the machine function is still in SSA form.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "machine-cse"
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/CodeGen/MachineDominators.h"
19#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng6ba95542010-03-03 02:48:20 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
21#include "llvm/Target/TargetInstrInfo.h"
Evan Chengc6fe3332010-03-02 02:38:24 +000022#include "llvm/ADT/ScopedHashTable.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/Support/Debug.h"
25
26using namespace llvm;
27
Evan Cheng16b48b82010-03-03 21:20:05 +000028STATISTIC(NumCoalesces, "Number of copies coalesced");
29STATISTIC(NumCSEs, "Number of common subexpression eliminated");
30
Evan Chengc6fe3332010-03-02 02:38:24 +000031namespace llvm {
32 template<> struct DenseMapInfo<MachineInstr*> {
33 static inline MachineInstr *getEmptyKey() {
34 return 0;
35 }
36
37 static inline MachineInstr *getTombstoneKey() {
38 return reinterpret_cast<MachineInstr*>(-1);
39 }
40
41 static unsigned getHashValue(const MachineInstr* const &MI) {
42 unsigned Hash = MI->getOpcode() * 37;
43 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
44 const MachineOperand &MO = MI->getOperand(i);
45 uint64_t Key = (uint64_t)MO.getType() << 32;
46 switch (MO.getType()) {
47 default: break;
48 case MachineOperand::MO_Register:
Evan Cheng6ba95542010-03-03 02:48:20 +000049 if (MO.isDef() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
50 continue; // Skip virtual register defs.
Evan Chengc6fe3332010-03-02 02:38:24 +000051 Key |= MO.getReg();
52 break;
53 case MachineOperand::MO_Immediate:
54 Key |= MO.getImm();
55 break;
56 case MachineOperand::MO_FrameIndex:
57 case MachineOperand::MO_ConstantPoolIndex:
58 case MachineOperand::MO_JumpTableIndex:
59 Key |= MO.getIndex();
60 break;
61 case MachineOperand::MO_MachineBasicBlock:
62 Key |= DenseMapInfo<void*>::getHashValue(MO.getMBB());
63 break;
64 case MachineOperand::MO_GlobalAddress:
65 Key |= DenseMapInfo<void*>::getHashValue(MO.getGlobal());
66 break;
67 case MachineOperand::MO_BlockAddress:
68 Key |= DenseMapInfo<void*>::getHashValue(MO.getBlockAddress());
69 break;
70 }
71 Key += ~(Key << 32);
72 Key ^= (Key >> 22);
73 Key += ~(Key << 13);
74 Key ^= (Key >> 8);
75 Key += (Key << 3);
76 Key ^= (Key >> 15);
77 Key += ~(Key << 27);
78 Key ^= (Key >> 31);
79 Hash = (unsigned)Key + Hash * 37;
80 }
81 return Hash;
82 }
83
84 static bool isEqual(const MachineInstr* const &LHS,
85 const MachineInstr* const &RHS) {
Evan Cheng6ba95542010-03-03 02:48:20 +000086 if (RHS == getEmptyKey() || RHS == getTombstoneKey() ||
87 LHS == getEmptyKey() || LHS == getTombstoneKey())
88 return LHS == RHS;
89 return LHS->isIdenticalTo(RHS, MachineInstr::IgnoreVRegDefs);
Evan Chengc6fe3332010-03-02 02:38:24 +000090 }
91 };
92} // end llvm namespace
93
94namespace {
95 class MachineCSE : public MachineFunctionPass {
Evan Cheng6ba95542010-03-03 02:48:20 +000096 const TargetInstrInfo *TII;
97 MachineRegisterInfo *MRI;
Evan Chengc6fe3332010-03-02 02:38:24 +000098 MachineDominatorTree *DT;
99 public:
100 static char ID; // Pass identification
Evan Cheng6ba95542010-03-03 02:48:20 +0000101 MachineCSE() : MachineFunctionPass(&ID), CurrVN(0) {}
Evan Chengc6fe3332010-03-02 02:38:24 +0000102
103 virtual bool runOnMachineFunction(MachineFunction &MF);
104
105 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
106 AU.setPreservesCFG();
107 MachineFunctionPass::getAnalysisUsage(AU);
108 AU.addRequired<MachineDominatorTree>();
109 AU.addPreserved<MachineDominatorTree>();
110 }
111
112 private:
Evan Cheng16b48b82010-03-03 21:20:05 +0000113 unsigned CurrVN;
114 ScopedHashTable<MachineInstr*, unsigned> VNT;
115 SmallVector<MachineInstr*, 64> Exps;
116
Evan Cheng6ba95542010-03-03 02:48:20 +0000117 bool PerformTrivialCoalescing(MachineInstr *MI, MachineBasicBlock *MBB);
Evan Chengc6fe3332010-03-02 02:38:24 +0000118 bool ProcessBlock(MachineDomTreeNode *Node);
119 };
120} // end anonymous namespace
121
122char MachineCSE::ID = 0;
123static RegisterPass<MachineCSE>
124X("machine-cse", "Machine Common Subexpression Elimination");
125
126FunctionPass *llvm::createMachineCSEPass() { return new MachineCSE(); }
127
Evan Cheng6ba95542010-03-03 02:48:20 +0000128bool MachineCSE::PerformTrivialCoalescing(MachineInstr *MI,
129 MachineBasicBlock *MBB) {
130 bool Changed = false;
131 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
132 MachineOperand &MO = MI->getOperand(i);
Evan Cheng16b48b82010-03-03 21:20:05 +0000133 if (!MO.isReg() || !MO.isUse())
134 continue;
135 unsigned Reg = MO.getReg();
136 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg))
137 continue;
138 if (!MRI->hasOneUse(Reg))
139 // Only coalesce single use copies. This ensure the copy will be
140 // deleted.
141 continue;
142 MachineInstr *DefMI = MRI->getVRegDef(Reg);
143 if (DefMI->getParent() != MBB)
144 continue;
145 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
146 if (TII->isMoveInstr(*DefMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
147 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
148 !SrcSubIdx && !DstSubIdx) {
149 MO.setReg(SrcReg);
150 DefMI->eraseFromParent();
151 ++NumCoalesces;
152 Changed = true;
Evan Cheng6ba95542010-03-03 02:48:20 +0000153 }
154 }
155
156 return Changed;
157}
158
159static bool hasLivePhysRegDefUse(MachineInstr *MI) {
160 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
161 MachineOperand &MO = MI->getOperand(i);
162 if (!MO.isReg())
163 continue;
164 unsigned Reg = MO.getReg();
165 if (!Reg)
166 continue;
Evan Cheng16b48b82010-03-03 21:20:05 +0000167 // FIXME: This is obviously overly conservative. On x86 lots of instructions
168 // will def EFLAGS and they are not marked dead at this point.
Evan Cheng6ba95542010-03-03 02:48:20 +0000169 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
170 !(MO.isDef() && MO.isDead()))
171 return true;
Evan Chengc6fe3332010-03-02 02:38:24 +0000172 }
173 return false;
174}
175
Evan Cheng6ba95542010-03-03 02:48:20 +0000176bool MachineCSE::ProcessBlock(MachineDomTreeNode *Node) {
177 bool Changed = false;
178
179 ScopedHashTableScope<MachineInstr*, unsigned> VNTS(VNT);
180 MachineBasicBlock *MBB = Node->getBlock();
Evan Cheng16b48b82010-03-03 21:20:05 +0000181 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) {
Evan Cheng6ba95542010-03-03 02:48:20 +0000182 MachineInstr *MI = &*I;
Evan Cheng16b48b82010-03-03 21:20:05 +0000183 ++I;
Evan Cheng6ba95542010-03-03 02:48:20 +0000184 bool SawStore = false;
185 if (!MI->isSafeToMove(TII, 0, SawStore))
186 continue;
187 // Ignore copies or instructions that read / write physical registers
188 // (except for dead defs of physical registers).
189 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
Evan Cheng16b48b82010-03-03 21:20:05 +0000190 if (TII->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) ||
191 MI->isExtractSubreg() || MI->isInsertSubreg() || MI->isSubregToReg())
192 continue;
Evan Cheng6ba95542010-03-03 02:48:20 +0000193 if (hasLivePhysRegDefUse(MI))
194 continue;
195
196 bool FoundCSE = VNT.count(MI);
197 if (!FoundCSE) {
198 // Look for trivial copy coalescing opportunities.
199 if (PerformTrivialCoalescing(MI, MBB))
200 FoundCSE = VNT.count(MI);
201 }
202
Evan Cheng16b48b82010-03-03 21:20:05 +0000203 if (!FoundCSE) {
204 VNT.insert(MI, CurrVN++);
205 Exps.push_back(MI);
206 continue;
207 }
208
209 // Found a common subexpression, eliminate it.
210 unsigned CSVN = VNT.lookup(MI);
211 MachineInstr *CSMI = Exps[CSVN];
212 DEBUG(dbgs() << "Examining: " << *MI);
213 DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI);
214 unsigned NumDefs = MI->getDesc().getNumDefs();
215 for (unsigned i = 0, e = MI->getNumOperands(); NumDefs && i != e; ++i) {
216 MachineOperand &MO = MI->getOperand(i);
217 if (!MO.isReg() || !MO.isDef())
218 continue;
219 unsigned OldReg = MO.getReg();
220 unsigned NewReg = CSMI->getOperand(i).getReg();
221 assert(OldReg != NewReg &&
222 TargetRegisterInfo::isVirtualRegister(OldReg) &&
223 TargetRegisterInfo::isVirtualRegister(NewReg) &&
224 "Do not CSE physical register defs!");
225 MRI->replaceRegWith(OldReg, NewReg);
226 --NumDefs;
227 }
228 MI->eraseFromParent();
229 ++NumCSEs;
Evan Cheng6ba95542010-03-03 02:48:20 +0000230 }
231
232 // Recursively call ProcessBlock with childred.
233 const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
234 for (unsigned i = 0, e = Children.size(); i != e; ++i)
235 Changed |= ProcessBlock(Children[i]);
236
237 return Changed;
238}
239
Evan Chengc6fe3332010-03-02 02:38:24 +0000240bool MachineCSE::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng6ba95542010-03-03 02:48:20 +0000241 TII = MF.getTarget().getInstrInfo();
242 MRI = &MF.getRegInfo();
Evan Chengc6fe3332010-03-02 02:38:24 +0000243 DT = &getAnalysis<MachineDominatorTree>();
244 return ProcessBlock(DT->getRootNode());
245}