1 //===- llvm/Support/InstVisitor.h - Define instruction visitors --*- C++ -*--=//
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).
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.
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:
20 // // Declare the class. Note that we derive from InstVisitor instantiated
21 // // with _our new subclasses_ type.
23 // struct CountMallocVisitor : public InstVisitor<CountMallocVisitor> {
25 // CountMallocVisitor() : Count(0) {}
27 // void visitMallocInst(MallocInst *MI) { ++Count; }
30 // And this class would be used like this:
31 // CountMallocVistor CMV;
33 // NumMallocs = CMV.Count;
35 //===----------------------------------------------------------------------===//
37 #ifndef LLVM_SUPPORT_INSTVISITOR_H
38 #define LLVM_SUPPORT_INSTVISITOR_H
40 #include "llvm/Instruction.h"
42 // We operate on opaque instruction classes, so forward declare all instruction
45 #define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS;
46 #include "llvm/Instruction.def"
48 // Forward declare the intermediate types...
49 class TerminatorInst; class UnaryOperator; class BinaryOperator;
50 class AllocationInst; class MemAccessInst;
52 template<typename SubClass>
54 ~InstVisitor() {} // We are meant to be derived from
56 //===--------------------------------------------------------------------===//
57 // Interface code - This is the public interface of the InstVisitor that you
58 // use to visit instructions...
61 // Generic visit method - Allow visitation to all instructions in a range
62 template<class Iterator>
63 void visit(Iterator Start, Iterator End) {
68 // Define visitors for modules, methods and basic blocks...
70 void visit(Module *M) { visit(M->begin(), M->end()); }
71 void visit(Method *M) { visit(M->begin(), M->end()); }
72 void visit(BasicBlock *BB) { visit(BB->begin(), BB->end()); }
74 // visit - Finally, code to visit an instruction...
76 void visit(Instruction *I) {
77 switch (I->getOpcode()) {
78 // Build the switch statement using the Instruction.def file...
79 #define HANDLE_INST(NUM, OPCODE, CLASS) \
80 case Instruction::OPCODE: ((SubClass*)this)->visit##CLASS((CLASS*)I); return;
81 #include "llvm/Instruction.def"
83 default: assert(0 && "Unknown instruction type encountered!");
87 //===--------------------------------------------------------------------===//
88 // Visitation functions... these functions provide default fallbacks in case
89 // the user does not specify what to do for a particular instruction type.
90 // The default behavior is to generalize the instruction type to its subtype
91 // and try visiting the subtype. All of this should be inlined perfectly,
92 // because there are no virtual functions to get in the way.
95 // Specific Instruction type classes... note that all of the casts are
96 // neccesary because we use the instruction classes as opaque types...
98 void visitReturnInst(ReturnInst *I) { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
99 void visitBranchInst(BranchInst *I) { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
100 void visitSwitchInst(SwitchInst *I) { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
101 void visitInvokeInst(InvokeInst *I) { ((SubClass*)this)->visitTerminatorInst((TerminatorInst*)I); }
102 void visitGenericUnaryInst(GenericUnaryInst *I) { ((SubClass*)this)->visitUnaryOperator((UnaryOperator*)I); }
103 void visitGenericBinaryInst(GenericBinaryInst *I) { ((SubClass*)this)->visitBinaryOperator((BinaryOperator*)I); }
104 void visitSetCondInst(SetCondInst *I) { ((SubClass*)this)->visitBinaryOperator((BinaryOperator *)I); }
105 void visitMallocInst(MallocInst *I) { ((SubClass*)this)->visitAllocationInst((AllocationInst *)I); }
106 void visitAllocaInst(AllocaInst *I) { ((SubClass*)this)->visitAllocationInst((AllocationInst *)I); }
107 void visitFreeInst(FreeInst *I) { ((SubClass*)this)->visitInstruction((Instruction *)I); }
108 void visitLoadInst(LoadInst *I) { ((SubClass*)this)->visitMemAccessInst((MemAccessInst *)I); }
109 void visitStoreInst(StoreInst *I) { ((SubClass*)this)->visitMemAccessInst((MemAccessInst *)I); }
110 void visitGetElementPtrInst(GetElementPtrInst *I) { ((SubClass*)this)->visitMemAccessInst((MemAccessInst *)I); }
111 void visitPHINode(PHINode *I) { ((SubClass*)this)->visitInstruction((Instruction *)I); }
112 void visitCastInst(CastInst *I) { ((SubClass*)this)->visitInstruction((Instruction *)I); }
113 void visitCallInst(CallInst *I) { ((SubClass*)this)->visitInstruction((Instruction *)I); }
114 void visitShiftInst(ShiftInst *I) { ((SubClass*)this)->visitInstruction((Instruction *)I); }
116 // Next level propogators... if the user does not overload a specific
117 // instruction type, they can overload one of these to get the whole class
118 // of instructions...
120 void visitTerminatorInst(TerminatorInst *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
121 void visitUnaryOperator (UnaryOperator *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
122 void visitBinaryOperator(BinaryOperator *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
123 void visitAllocationInst(AllocationInst *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
124 void visitMemAccessInst (MemAccessInst *I) { ((SubClass*)this)->visitInstruction((Instruction*)I); }
126 // If the user wants a 'default' case, they can choose to override this
127 // function. If this function is not overloaded in the users subclass, then
128 // this instruction just gets ignored.
130 void visitInstruction(Instruction *I) {} // Ignore unhandled instructions