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