Remove a ton of extraneous #includes
[oota-llvm.git] / include / llvm / Support / InstVisitor.h
1 //===- llvm/Support/InstVisitor.h - Define instruction visitors --*- C++ -*--=//
2 //
3 // This template class is used to define instruction visitors in a typesafe
4 // manner without having to use lots of casts and a big switch statement (in
5 // your code that is).  The win here is that if instructions are added in the
6 // future, they will be added to the InstVisitor<T> class, allowing you to
7 // automatically support them (if you handle on of their superclasses).
8 //
9 // Note that this library is specifically designed as a template to avoid
10 // virtual function call overhead.  Defining and using an InstVisitor is just as
11 // efficient as having your own switch statement over the instruction opcode.
12 //
13 // InstVisitor Usage:
14 //   You define InstVisitors from inheriting from the InstVisitor base class
15 // and "overriding" functions in your class.  I say "overriding" because this
16 // class is defined in terms of statically resolved overloading, not virtual
17 // functions.  As an example, here is a visitor that counts the number of malloc
18 // instructions processed:
19 //
20 //  // Declare the class.  Note that we derive from InstVisitor instantiated
21 //  // with _our new subclasses_ type.
22 //  //
23 //  struct CountMallocVisitor : public InstVisitor<CountMallocVisitor> {
24 //    unsigned Count;
25 //    CountMallocVisitor() : Count(0) {}
26 //
27 //    void visitMallocInst(MallocInst *MI) { ++Count; }
28 //  };
29 //
30 //  And this class would be used like this:
31 //    CountMallocVistor CMV;
32 //    CMV.visit(function);
33 //    NumMallocs = CMV.Count;
34 //
35 // Returning a value from the visitation function:
36 //   The InstVisitor class takes an optional second template argument that
37 // specifies what type the instruction visitation functions should return.  If
38 // you specify this, you *MUST* provide an implementation of visitInstruction
39 // though!.
40 //
41 //===----------------------------------------------------------------------===//
42
43 #ifndef LLVM_SUPPORT_INSTVISITOR_H
44 #define LLVM_SUPPORT_INSTVISITOR_H
45
46 #include "llvm/Instruction.h"
47
48 class Module;
49
50 // We operate on opaque instruction classes, so forward declare all instruction
51 // types now...
52 //
53 #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
54 #include "llvm/Instruction.def"
55
56 // Forward declare the intermediate types...
57 class TerminatorInst; class BinaryOperator;
58 class AllocationInst;
59
60
61 #define DELEGATE(CLASS_TO_VISIT) \
62   return ((SubClass*)this)->visit##CLASS_TO_VISIT((CLASS_TO_VISIT&)I)
63
64
65 template<typename SubClass, typename RetTy=void>
66 struct InstVisitor {
67   virtual ~InstVisitor() {}           // We are meant to be derived from
68
69   //===--------------------------------------------------------------------===//
70   // Interface code - This is the public interface of the InstVisitor that you
71   // use to visit instructions...
72   //
73
74   // Generic visit method - Allow visitation to all instructions in a range
75   template<class Iterator>
76   void visit(Iterator Start, Iterator End) {
77     while (Start != End)
78       ((SubClass*)this)->visit(*Start++);
79   }
80
81   // Define visitors for modules, functions and basic blocks...
82   //
83   void visit(Module &M) {
84     ((SubClass*)this)->visitModule(M);
85     visit(M.begin(), M.end());
86   }
87   void visit(Function &F) {
88     ((SubClass*)this)->visitFunction(F);
89     visit(F.begin(), F.end());
90   }
91   void visit(BasicBlock &BB) {
92     ((SubClass*)this)->visitBasicBlock(BB);
93     visit(BB.begin(), BB.end());
94   }
95
96   // Forwarding functions so that the user can visit with pointers AND refs.
97   void visit(Module       *M)  { visit(*M); }
98   void visit(Function     *F)  { visit(*F); }
99   void visit(BasicBlock   *BB) { visit(*BB); }
100   RetTy visit(Instruction *I)  { return visit(*I); }
101
102   // visit - Finally, code to visit an instruction...
103   //
104   RetTy visit(Instruction &I) {
105     switch (I.getOpcode()) {
106     default: assert(0 && "Unknown instruction type encountered!");
107              abort();
108       // Build the switch statement using the Instruction.def file...
109 #define HANDLE_INST(NUM, OPCODE, CLASS) \
110     case Instruction::OPCODE:return ((SubClass*)this)->visit##OPCODE((CLASS&)I);
111 #include "llvm/Instruction.def"
112     }
113   }
114
115   //===--------------------------------------------------------------------===//
116   // Visitation functions... these functions provide default fallbacks in case
117   // the user does not specify what to do for a particular instruction type.
118   // The default behavior is to generalize the instruction type to its subtype
119   // and try visiting the subtype.  All of this should be inlined perfectly,
120   // because there are no virtual functions to get in the way.
121   //
122
123   // When visiting a module, function or basic block directly, these methods get
124   // called to indicate when transitioning into a new unit.
125   //
126   void visitModule    (Module &M) {}
127   void visitFunction  (Function &F) {}
128   void visitBasicBlock(BasicBlock &BB) {}
129
130
131   // Define instruction specific visitor functions that can be overridden to
132   // handle SPECIFIC instructions.  These functions automatically define
133   // visitMul to proxy to visitBinaryOperator for instance in case the user does
134   // not need this generality.
135   //
136   // The one problem case we have to handle here though is that the PHINode
137   // class and opcode name are the exact same.  Because of this, we cannot
138   // define visitPHINode (the inst version) to forward to visitPHINode (the
139   // generic version) without multiply defined symbols and recursion.  To handle
140   // this, we do not autoexpand "Other" instructions, we do it manually.
141   //
142 #define HANDLE_INST(NUM, OPCODE, CLASS) \
143     RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
144 #define HANDLE_OTHER_INST(NUM, OPCODE, CLASS)   // Ignore "other" instructions
145 #include "llvm/Instruction.def"
146
147   // Implement all "other" instructions, except for PHINode
148   RetTy visitCast(CastInst &I)       { DELEGATE(CastInst);    }
149   RetTy visitCall(CallInst &I)       { DELEGATE(CallInst);    }
150   RetTy visitShr(ShiftInst &I)       { DELEGATE(ShiftInst);   }
151   RetTy visitShl(ShiftInst &I)       { DELEGATE(ShiftInst);   }
152   RetTy visitVarArg(VarArgInst &I)   { DELEGATE(VarArgInst);  }
153   RetTy visitUserOp1(Instruction &I) { DELEGATE(Instruction); }
154   RetTy visitUserOp2(Instruction &I) { DELEGATE(Instruction); }
155
156   
157   // Specific Instruction type classes... note that all of the casts are
158   // neccesary because we use the instruction classes as opaque types...
159   //
160   RetTy visitReturnInst(ReturnInst &I)              { DELEGATE(TerminatorInst);}
161   RetTy visitBranchInst(BranchInst &I)              { DELEGATE(TerminatorInst);}
162   RetTy visitSwitchInst(SwitchInst &I)              { DELEGATE(TerminatorInst);}
163   RetTy visitInvokeInst(InvokeInst &I)              { DELEGATE(TerminatorInst);}
164   RetTy visitSetCondInst(SetCondInst &I)            { DELEGATE(BinaryOperator);}
165   RetTy visitMallocInst(MallocInst &I)              { DELEGATE(AllocationInst);}
166   RetTy visitAllocaInst(AllocaInst &I)              { DELEGATE(AllocationInst);}
167   RetTy visitFreeInst(FreeInst     &I)              { DELEGATE(Instruction); }
168   RetTy visitLoadInst(LoadInst     &I)              { DELEGATE(Instruction); }
169   RetTy visitStoreInst(StoreInst   &I)              { DELEGATE(Instruction); }
170   RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction); }
171   RetTy visitPHINode(PHINode       &I)              { DELEGATE(Instruction); }
172   RetTy visitCastInst(CastInst     &I)              { DELEGATE(Instruction); }
173   RetTy visitCallInst(CallInst     &I)              { DELEGATE(Instruction); }
174   RetTy visitShiftInst(ShiftInst   &I)              { DELEGATE(Instruction); }
175   RetTy visitVarArgInst(VarArgInst &I)              { DELEGATE(Instruction); }
176
177   // Next level propagators... if the user does not overload a specific
178   // instruction type, they can overload one of these to get the whole class
179   // of instructions...
180   //
181   RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); }
182   RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); }
183   RetTy visitAllocationInst(AllocationInst &I) { DELEGATE(Instruction); }
184
185   // If the user wants a 'default' case, they can choose to override this
186   // function.  If this function is not overloaded in the users subclass, then
187   // this instruction just gets ignored.
188   //
189   // Note that you MUST override this function if your return type is not void.
190   //
191   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
192 };
193
194 #undef DELEGATE
195
196 #endif