ba193336cfa650196642cdc8219258a2a0023f74
[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 //===----------------------------------------------------------------------===//
36
37 #ifndef LLVM_SUPPORT_INSTVISITOR_H
38 #define LLVM_SUPPORT_INSTVISITOR_H
39
40 #include "llvm/Instruction.h"
41
42 // We operate on opaque instruction classes, so forward declare all instruction
43 // types now...
44 //
45 #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
46 #include "llvm/Instruction.def"
47
48 // Forward declare the intermediate types...
49 class TerminatorInst; class UnaryOperator; class BinaryOperator;
50 class AllocationInst; class MemAccessInst;
51
52 template<typename SubClass>
53 struct InstVisitor {
54   ~InstVisitor() {}           // We are meant to be derived from
55
56   //===--------------------------------------------------------------------===//
57   // Interface code - This is the public interface of the InstVisitor that you
58   // use to visit instructions...
59   //
60
61   // Generic visit method - Allow visitation to all instructions in a range
62   template<class Iterator>
63   void visit(Iterator Start, Iterator End) {
64     while (Start != End)
65       visit(*Start++);
66   }
67
68   // Define visitors for modules, functions and basic blocks...
69   //
70   void visit(Module *M) {
71     ((SubClass*)this)->visitModule(M);
72     visit(M->begin(), M->end());
73   }
74   void visit(Function *F) {
75     ((SubClass*)this)->visitFunction(F);
76     visit(F->begin(), F->end());
77   }
78   void visit(BasicBlock *BB) {
79     ((SubClass*)this)->visitBasicBlock(BB);
80     visit(BB->begin(), BB->end());
81   }
82
83   // visit - Finally, code to visit an instruction...
84   //
85   void visit(Instruction *I) {
86     switch (I->getOpcode()) {
87       // Build the switch statement using the Instruction.def file...
88 #define HANDLE_INST(NUM, OPCODE, CLASS) \
89     case Instruction::OPCODE: ((SubClass*)this)->visit##CLASS((CLASS*)I); return;
90 #include "llvm/Instruction.def"
91
92     default: assert(0 && "Unknown instruction type encountered!");
93     }
94   }
95
96   //===--------------------------------------------------------------------===//
97   // Visitation functions... these functions provide default fallbacks in case
98   // the user does not specify what to do for a particular instruction type.
99   // The default behavior is to generalize the instruction type to its subtype
100   // and try visiting the subtype.  All of this should be inlined perfectly,
101   // because there are no virtual functions to get in the way.
102   //
103
104   // When visiting a module, function or basic block directly, these methods get
105   // called to indicate when transitioning into a new unit.
106   //
107   void visitModule    (Module *M) {}
108   void visitFunction  (Function *F) {}
109   void visitBasicBlock(BasicBlock *BB) {}
110   
111   // Specific Instruction type classes... note that all of the casts are
112   // neccesary because we use the instruction classes as opaque types...
113   //
114   void visitReturnInst(ReturnInst *I)               { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
115   void visitBranchInst(BranchInst *I)               { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
116   void visitSwitchInst(SwitchInst *I)               { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
117   void visitInvokeInst(InvokeInst *I)               { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
118   void visitGenericUnaryInst(GenericUnaryInst  *I)  { ((SubClass*)this)->visitUnaryOperator((UnaryOperator*)I); }
119   void visitGenericBinaryInst(GenericBinaryInst *I) { ((SubClass*)this)->visitBinaryOperator((BinaryOperator*)I); }
120   void visitSetCondInst(SetCondInst *I)             { ((SubClass*)this)->visitBinaryOperator((BinaryOperator *)I); }
121   void visitMallocInst(MallocInst *I)               { ((SubClass*)this)->visitAllocationInst((AllocationInst *)I); }
122   void visitAllocaInst(AllocaInst *I)               { ((SubClass*)this)->visitAllocationInst((AllocationInst *)I); }
123   void visitFreeInst(FreeInst   *I)                 { ((SubClass*)this)->visitInstruction((Instruction *)I); }
124   void visitLoadInst(LoadInst   *I)                 { ((SubClass*)this)->visitMemAccessInst((MemAccessInst *)I); }
125   void visitStoreInst(StoreInst  *I)                { ((SubClass*)this)->visitMemAccessInst((MemAccessInst *)I); }
126   void visitGetElementPtrInst(GetElementPtrInst *I) { ((SubClass*)this)->visitMemAccessInst((MemAccessInst *)I); }
127   void visitPHINode(PHINode    *I)                  { ((SubClass*)this)->visitInstruction((Instruction *)I); }
128   void visitCastInst(CastInst   *I)                 { ((SubClass*)this)->visitInstruction((Instruction *)I); }
129   void visitCallInst(CallInst   *I)                 { ((SubClass*)this)->visitInstruction((Instruction *)I); }
130   void visitShiftInst(ShiftInst  *I)                { ((SubClass*)this)->visitInstruction((Instruction *)I); }
131
132   // Next level propogators... if the user does not overload a specific
133   // instruction type, they can overload one of these to get the whole class
134   // of instructions...
135   //
136   void visitTerminatorInst(TerminatorInst *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
137   void visitUnaryOperator (UnaryOperator  *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
138   void visitBinaryOperator(BinaryOperator *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
139   void visitAllocationInst(AllocationInst *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
140   void visitMemAccessInst (MemAccessInst  *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
141
142   // If the user wants a 'default' case, they can choose to override this
143   // function.  If this function is not overloaded in the users subclass, then
144   // this instruction just gets ignored.
145   //
146   void visitInstruction(Instruction *I) {}  // Ignore unhandled instructions
147 };
148
149 #endif