1 //===- llvm/Support/InstVisitor.h - Define instruction visitors -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_SUPPORT_INSTVISITOR_H
12 #define LLVM_SUPPORT_INSTVISITOR_H
14 #include "llvm/Function.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/Module.h"
20 // We operate on opaque instruction classes, so forward declare all instruction
23 #define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS;
24 #include "llvm/Instruction.def"
26 // Forward declare the intermediate types...
27 class TerminatorInst; class BinaryOperator;
30 #define DELEGATE(CLASS_TO_VISIT) \
31 return static_cast<SubClass*>(this)-> \
32 visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
35 /// @brief Base class for instruction visitors
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).
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.
46 /// For example, here is a visitor that counts the number of malloc
47 /// instructions processed:
49 /// /// Declare the class. Note that we derive from InstVisitor instantiated
50 /// /// with _our new subclasses_ type.
52 /// struct CountMallocVisitor : public InstVisitor<CountMallocVisitor> {
54 /// CountMallocVisitor() : Count(0) {}
56 /// void visitMallocInst(MallocInst *MI) { ++Count; }
59 /// And this class would be used like this:
60 /// CountMallocVistor CMV;
61 /// CMV.visit(function);
62 /// NumMallocs = CMV.Count;
64 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
65 /// Function, and Module, which recursively process all conained instructions.
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.
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!.
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
80 template<typename SubClass, typename RetTy=void>
82 //===--------------------------------------------------------------------===//
83 // Interface code - This is the public interface of the InstVisitor that you
84 // use to visit instructions...
88 // Generic visit method - Allow visitation to all instructions in a range
89 template<class Iterator>
90 void visit(Iterator Start, Iterator End) {
92 static_cast<SubClass*>(this)->visit(*Start++);
95 // Define visitors for functions and basic blocks...
97 void visit(Module &M) {
98 static_cast<SubClass*>(this)->visitModule(M);
99 visit(M.begin(), M.end());
101 void visit(Function &F) {
102 static_cast<SubClass*>(this)->visitFunction(F);
103 visit(F.begin(), F.end());
105 void visit(BasicBlock &BB) {
106 static_cast<SubClass*>(this)->visitBasicBlock(BB);
107 visit(BB.begin(), BB.end());
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); }
116 // visit - Finally, code to visit an instruction...
118 RetTy visit(Instruction &I) {
119 switch (I.getOpcode()) {
120 default: assert(0 && "Unknown instruction type encountered!");
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"
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.
139 // When visiting a module, function or basic block directly, these methods get
140 // called to indicate when transitioning into a new unit.
142 void visitModule (Module &M) {}
143 void visitFunction (Function &F) {}
144 void visitBasicBlock(BasicBlock &BB) {}
147 // Define instruction specific visitor functions that can be overridden to
148 // handle SPECIFIC instructions. These functions automatically define
149 // visitMul to proxy to visitBinaryOperator for instance in case the user does
150 // not need this generality.
152 // The one problem case we have to handle here though is that the PHINode
153 // class and opcode name are the exact same. Because of this, we cannot
154 // define visitPHINode (the inst version) to forward to visitPHINode (the
155 // generic version) without multiply defined symbols and recursion. To handle
156 // this, we do not autoexpand "Other" instructions, we do it manually.
158 #define HANDLE_INST(NUM, OPCODE, CLASS) \
159 RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
160 #include "llvm/Instruction.def"
162 // Specific Instruction type classes... note that all of the casts are
163 // necessary because we use the instruction classes as opaque types...
165 RetTy visitReturnInst(ReturnInst &I) { DELEGATE(TerminatorInst);}
166 RetTy visitBranchInst(BranchInst &I) { DELEGATE(TerminatorInst);}
167 RetTy visitSwitchInst(SwitchInst &I) { DELEGATE(TerminatorInst);}
168 RetTy visitInvokeInst(InvokeInst &I) { DELEGATE(TerminatorInst);}
169 RetTy visitUnwindInst(UnwindInst &I) { DELEGATE(TerminatorInst);}
170 RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);}
171 RetTy visitSetCondInst(SetCondInst &I) { DELEGATE(BinaryOperator);}
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 visitCastInst(CastInst &I) { DELEGATE(Instruction); }
180 RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction); }
181 RetTy visitCallInst(CallInst &I) { DELEGATE(Instruction); }
182 RetTy visitShiftInst(ShiftInst &I) { DELEGATE(Instruction); }
183 RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(Instruction); }
184 RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction); }
185 RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction); }
186 RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction); }
188 // Next level propagators... if the user does not overload a specific
189 // instruction type, they can overload one of these to get the whole class
190 // of instructions...
192 RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); }
193 RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); }
194 RetTy visitAllocationInst(AllocationInst &I) { DELEGATE(Instruction); }
196 // If the user wants a 'default' case, they can choose to override this
197 // function. If this function is not overloaded in the users subclass, then
198 // this instruction just gets ignored.
200 // Note that you MUST override this function if your return type is not void.
202 void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
207 } // End llvm namespace