llvm.org GIT mirror llvm / c6fe333
Add skeleton of a machine level cse pass. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97543 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 10 years ago
2 changed file(s) with 128 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
161161 ///
162162 FunctionPass *createGCInfoPrinter(raw_ostream &OS);
163163
164 /// createMachineCSEPass - This pass performs global CSE on machine
165 /// instructions.
166 FunctionPass *createMachineCSEPass();
167
164168 /// createMachineLICMPass - This pass performs LICM on machine instructions.
165169 ///
166170 FunctionPass *createMachineLICMPass();
0 //===-- MachineCSE.cpp - Machine Common Subexpression Elimination Pass ----===//
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 performs global common subexpression elimination on machine
10 // instructions using a scoped hash table based value numbering schemem. IT
11 // must be run while the machine function is still in SSA form.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "machine-cse"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/MachineDominators.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/ADT/ScopedHashTable.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Support/Debug.h"
22
23 using namespace llvm;
24
25 namespace llvm {
26 template<> struct DenseMapInfo {
27 static inline MachineInstr *getEmptyKey() {
28 return 0;
29 }
30
31 static inline MachineInstr *getTombstoneKey() {
32 return reinterpret_cast(-1);
33 }
34
35 static unsigned getHashValue(const MachineInstr* const &MI) {
36 unsigned Hash = MI->getOpcode() * 37;
37 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
38 const MachineOperand &MO = MI->getOperand(i);
39 uint64_t Key = (uint64_t)MO.getType() << 32;
40 switch (MO.getType()) {
41 default: break;
42 case MachineOperand::MO_Register:
43 Key |= MO.getReg();
44 break;
45 case MachineOperand::MO_Immediate:
46 Key |= MO.getImm();
47 break;
48 case MachineOperand::MO_FrameIndex:
49 case MachineOperand::MO_ConstantPoolIndex:
50 case MachineOperand::MO_JumpTableIndex:
51 Key |= MO.getIndex();
52 break;
53 case MachineOperand::MO_MachineBasicBlock:
54 Key |= DenseMapInfo::getHashValue(MO.getMBB());
55 break;
56 case MachineOperand::MO_GlobalAddress:
57 Key |= DenseMapInfo::getHashValue(MO.getGlobal());
58 break;
59 case MachineOperand::MO_BlockAddress:
60 Key |= DenseMapInfo::getHashValue(MO.getBlockAddress());
61 break;
62 }
63 Key += ~(Key << 32);
64 Key ^= (Key >> 22);
65 Key += ~(Key << 13);
66 Key ^= (Key >> 8);
67 Key += (Key << 3);
68 Key ^= (Key >> 15);
69 Key += ~(Key << 27);
70 Key ^= (Key >> 31);
71 Hash = (unsigned)Key + Hash * 37;
72 }
73 return Hash;
74 }
75
76 static bool isEqual(const MachineInstr* const &LHS,
77 const MachineInstr* const &RHS) {
78 return LHS->isIdenticalTo(RHS);
79 }
80 };
81 } // end llvm namespace
82
83 namespace {
84 class MachineCSE : public MachineFunctionPass {
85 ScopedHashTable VNT;
86 MachineDominatorTree *DT;
87 public:
88 static char ID; // Pass identification
89 MachineCSE() : MachineFunctionPass(&ID) {}
90
91 virtual bool runOnMachineFunction(MachineFunction &MF);
92
93 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
94 AU.setPreservesCFG();
95 MachineFunctionPass::getAnalysisUsage(AU);
96 AU.addRequired();
97 AU.addPreserved();
98 }
99
100 private:
101 bool ProcessBlock(MachineDomTreeNode *Node);
102 };
103 } // end anonymous namespace
104
105 char MachineCSE::ID = 0;
106 static RegisterPass
107 X("machine-cse", "Machine Common Subexpression Elimination");
108
109 FunctionPass *llvm::createMachineCSEPass() { return new MachineCSE(); }
110
111 bool MachineCSE::ProcessBlock(MachineDomTreeNode *Node) {
112 ScopedHashTableScope VNTS(VNT);
113 MachineBasicBlock *MBB = Node->getBlock();
114 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
115 ++I) {
116 }
117 return false;
118 }
119
120 bool MachineCSE::runOnMachineFunction(MachineFunction &MF) {
121 DT = &getAnalysis();
122 return ProcessBlock(DT->getRootNode());
123 }