Don't attribute in file headers anymore. See llvmdev for the
[oota-llvm.git] / include / llvm / Support / InstVisitor.h
1 //===- llvm/Support/InstVisitor.h - Define instruction visitors -*- C++ -*-===//
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
11 #ifndef LLVM_SUPPORT_INSTVISITOR_H
12 #define LLVM_SUPPORT_INSTVISITOR_H
13
14 #include "llvm/Function.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/Module.h"
17
18 namespace llvm {
19
20 // We operate on opaque instruction classes, so forward declare all instruction
21 // types now...
22 //
23 #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
24 #include "llvm/Instruction.def"
25
26 // Forward declare the intermediate types...
27 class TerminatorInst; class BinaryOperator;
28 class AllocationInst;
29
30 #define DELEGATE(CLASS_TO_VISIT) \
31   return static_cast<SubClass*>(this)-> \
32                visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
33
34
35 /// @brief Base class for instruction visitors
36 ///
37 /// Instruction visitors are used when you want to perform different action for
38 /// different kinds of instruction without without having to use lots of casts 
39 /// and a big switch statement (in your code that is). 
40 ///
41 /// To define your own visitor, inherit from this class, specifying your
42 /// new type for the 'SubClass' template parameter, and "override" visitXXX
43 /// functions in your class. I say "overriding" because this class is defined 
44 /// in terms of statically resolved overloading, not virtual functions.  
45 /// 
46 /// For example, here is a visitor that counts the number of malloc 
47 /// instructions processed:
48 ///
49 ///  /// Declare the class.  Note that we derive from InstVisitor instantiated
50 ///  /// with _our new subclasses_ type.
51 ///  ///
52 ///  struct CountMallocVisitor : public InstVisitor<CountMallocVisitor> {
53 ///    unsigned Count;
54 ///    CountMallocVisitor() : Count(0) {}
55 ///
56 ///    void visitMallocInst(MallocInst &MI) { ++Count; }
57 ///  };
58 ///
59 ///  And this class would be used like this:
60 ///    CountMallocVistor CMV;
61 ///    CMV.visit(function);
62 ///    NumMallocs = CMV.Count;
63 ///
64 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
65 /// Function, and Module, which recursively process all conained instructions.
66 ///
67 /// Note that if you don't implement visitXXX for some instruction type,
68 /// the visitXXX method for instruction superclass will be invoked. So
69 /// if instructions are added in the future, they will be automatically
70 /// supported, if you handle on of their superclasses.
71 ///
72 /// The optional second template argument specifies the type that instruction 
73 /// visitation functions should return. If you specify this, you *MUST* provide 
74 /// an implementation of visitInstruction though!.
75 ///
76 /// Note that this class is specifically designed as a template to avoid
77 /// virtual function call overhead.  Defining and using an InstVisitor is just
78 /// as efficient as having your own switch statement over the instruction
79 /// opcode.
80 template<typename SubClass, typename RetTy=void>
81 class InstVisitor {
82   //===--------------------------------------------------------------------===//
83   // Interface code - This is the public interface of the InstVisitor that you
84   // use to visit instructions...
85   //
86
87 public:
88   // Generic visit method - Allow visitation to all instructions in a range
89   template<class Iterator>
90   void visit(Iterator Start, Iterator End) {
91     while (Start != End)
92       static_cast<SubClass*>(this)->visit(*Start++);
93   }
94
95   // Define visitors for functions and basic blocks...
96   //
97   void visit(Module &M) {
98     static_cast<SubClass*>(this)->visitModule(M);
99     visit(M.begin(), M.end());
100   }
101   void visit(Function &F) {
102     static_cast<SubClass*>(this)->visitFunction(F);
103     visit(F.begin(), F.end());
104   }
105   void visit(BasicBlock &BB) {
106     static_cast<SubClass*>(this)->visitBasicBlock(BB);
107     visit(BB.begin(), BB.end());
108   }
109
110   // Forwarding functions so that the user can visit with pointers AND refs.
111   void visit(Module       *M)  { visit(*M); }
112   void visit(Function     *F)  { visit(*F); }
113   void visit(BasicBlock   *BB) { visit(*BB); }
114   RetTy visit(Instruction *I)  { return visit(*I); }
115
116   // visit - Finally, code to visit an instruction...
117   //
118   RetTy visit(Instruction &I) {
119     switch (I.getOpcode()) {
120     default: assert(0 && "Unknown instruction type encountered!");
121              abort();
122       // Build the switch statement using the Instruction.def file...
123 #define HANDLE_INST(NUM, OPCODE, CLASS) \
124     case Instruction::OPCODE: return \
125            static_cast<SubClass*>(this)-> \
126                       visit##OPCODE(static_cast<CLASS&>(I));
127 #include "llvm/Instruction.def"
128     }
129   }
130
131   //===--------------------------------------------------------------------===//
132   // Visitation functions... these functions provide default fallbacks in case
133   // the user does not specify what to do for a particular instruction type.
134   // The default behavior is to generalize the instruction type to its subtype
135   // and try visiting the subtype.  All of this should be inlined perfectly,
136   // because there are no virtual functions to get in the way.
137   //
138
139   // When visiting a module, function or basic block directly, these methods get
140   // called to indicate when transitioning into a new unit.
141   //
142   void visitModule    (Module &M) {}
143   void visitFunction  (Function &F) {}
144   void visitBasicBlock(BasicBlock &BB) {}
145
146   // Define instruction specific visitor functions that can be overridden to
147   // handle SPECIFIC instructions.  These functions automatically define
148   // visitMul to proxy to visitBinaryOperator for instance in case the user does
149   // not need this generality.
150   //
151   // The one problem case we have to handle here though is that the PHINode
152   // class and opcode name are the exact same.  Because of this, we cannot
153   // define visitPHINode (the inst version) to forward to visitPHINode (the
154   // generic version) without multiply defined symbols and recursion.  To handle
155   // this, we do not autoexpand "Other" instructions, we do it manually.
156   //
157 #define HANDLE_INST(NUM, OPCODE, CLASS) \
158     RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
159 #include "llvm/Instruction.def"
160
161   // Specific Instruction type classes... note that all of the casts are
162   // necessary because we use the instruction classes as opaque types...
163   //
164   RetTy visitReturnInst(ReturnInst &I)              { DELEGATE(TerminatorInst);}
165   RetTy visitBranchInst(BranchInst &I)              { DELEGATE(TerminatorInst);}
166   RetTy visitSwitchInst(SwitchInst &I)              { DELEGATE(TerminatorInst);}
167   RetTy visitInvokeInst(InvokeInst &I)              { DELEGATE(TerminatorInst);}
168   RetTy visitUnwindInst(UnwindInst &I)              { DELEGATE(TerminatorInst);}
169   RetTy visitUnreachableInst(UnreachableInst &I)    { DELEGATE(TerminatorInst);}
170   RetTy visitICmpInst(ICmpInst &I)                  { DELEGATE(CmpInst);}
171   RetTy visitFCmpInst(FCmpInst &I)                  { DELEGATE(CmpInst);}
172   RetTy visitMallocInst(MallocInst &I)              { DELEGATE(AllocationInst);}
173   RetTy visitAllocaInst(AllocaInst &I)              { DELEGATE(AllocationInst);}
174   RetTy visitFreeInst(FreeInst     &I)              { DELEGATE(Instruction); }
175   RetTy visitLoadInst(LoadInst     &I)              { DELEGATE(Instruction); }
176   RetTy visitStoreInst(StoreInst   &I)              { DELEGATE(Instruction); }
177   RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction); }
178   RetTy visitPHINode(PHINode       &I)              { DELEGATE(Instruction); }
179   RetTy visitTruncInst(TruncInst &I)                { DELEGATE(CastInst); }
180   RetTy visitZExtInst(ZExtInst &I)                  { DELEGATE(CastInst); }
181   RetTy visitSExtInst(SExtInst &I)                  { DELEGATE(CastInst); }
182   RetTy visitFPTruncInst(FPTruncInst &I)            { DELEGATE(CastInst); }
183   RetTy visitFPExtInst(FPExtInst &I)                { DELEGATE(CastInst); }
184   RetTy visitFPToUIInst(FPToUIInst &I)              { DELEGATE(CastInst); }
185   RetTy visitFPToSIInst(FPToSIInst &I)              { DELEGATE(CastInst); }
186   RetTy visitUIToFPInst(UIToFPInst &I)              { DELEGATE(CastInst); }
187   RetTy visitSIToFPInst(SIToFPInst &I)              { DELEGATE(CastInst); }
188   RetTy visitPtrToIntInst(PtrToIntInst &I)          { DELEGATE(CastInst); }
189   RetTy visitIntToPtrInst(IntToPtrInst &I)          { DELEGATE(CastInst); }
190   RetTy visitBitCastInst(BitCastInst &I)            { DELEGATE(CastInst); }
191   RetTy visitSelectInst(SelectInst &I)              { DELEGATE(Instruction); }
192   RetTy visitCallInst(CallInst     &I)              { DELEGATE(Instruction); }
193   RetTy visitVAArgInst(VAArgInst   &I)              { DELEGATE(Instruction); }
194   RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
195   RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction); }
196   RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction); }
197
198   // Next level propagators... if the user does not overload a specific
199   // instruction type, they can overload one of these to get the whole class
200   // of instructions...
201   //
202   RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); }
203   RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); }
204   RetTy visitAllocationInst(AllocationInst &I) { DELEGATE(Instruction); }
205   RetTy visitCmpInst(CmpInst &I)               { DELEGATE(Instruction); }
206   RetTy visitCastInst(CastInst &I)             { DELEGATE(Instruction); }
207
208   // If the user wants a 'default' case, they can choose to override this
209   // function.  If this function is not overloaded in the users subclass, then
210   // this instruction just gets ignored.
211   //
212   // Note that you MUST override this function if your return type is not void.
213   //
214   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
215 };
216
217 #undef DELEGATE
218
219 } // End llvm namespace
220
221 #endif