1 //===- llvm/Support/InstVisitor.h - Define instruction visitors -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
17 #include "llvm/Support/ErrorHandling.h"
21 // We operate on opaque instruction classes, so forward declare all instruction
24 #define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS;
25 #include "llvm/Instruction.def"
27 #define DELEGATE(CLASS_TO_VISIT) \
28 return static_cast<SubClass*>(this)-> \
29 visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
32 /// @brief Base class for instruction visitors
34 /// Instruction visitors are used when you want to perform different actions
35 /// for different kinds of instructions without having to use lots of casts
36 /// and a big switch statement (in your code, that is).
38 /// To define your own visitor, inherit from this class, specifying your
39 /// new type for the 'SubClass' template parameter, and "override" visitXXX
40 /// functions in your class. I say "override" because this class is defined
41 /// in terms of statically resolved overloading, not virtual functions.
43 /// For example, here is a visitor that counts the number of malloc
44 /// instructions processed:
46 /// /// Declare the class. Note that we derive from InstVisitor instantiated
47 /// /// with _our new subclasses_ type.
49 /// struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
51 /// CountAllocaVisitor() : Count(0) {}
53 /// void visitAllocaInst(AllocaInst &AI) { ++Count; }
56 /// And this class would be used like this:
57 /// CountAllocaVisitor CAV;
58 /// CAV.visit(function);
59 /// NumAllocas = CAV.Count;
61 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
62 /// Function, and Module, which recursively process all contained instructions.
64 /// Note that if you don't implement visitXXX for some instruction type,
65 /// the visitXXX method for instruction superclass will be invoked. So
66 /// if instructions are added in the future, they will be automatically
67 /// supported, if you handle one of their superclasses.
69 /// The optional second template argument specifies the type that instruction
70 /// visitation functions should return. If you specify this, you *MUST* provide
71 /// an implementation of visitInstruction though!.
73 /// Note that this class is specifically designed as a template to avoid
74 /// virtual function call overhead. Defining and using an InstVisitor is just
75 /// as efficient as having your own switch statement over the instruction
77 template<typename SubClass, typename RetTy=void>
79 //===--------------------------------------------------------------------===//
80 // Interface code - This is the public interface of the InstVisitor that you
81 // use to visit instructions...
85 // Generic visit method - Allow visitation to all instructions in a range
86 template<class Iterator>
87 void visit(Iterator Start, Iterator End) {
89 static_cast<SubClass*>(this)->visit(*Start++);
92 // Define visitors for functions and basic blocks...
94 void visit(Module &M) {
95 static_cast<SubClass*>(this)->visitModule(M);
96 visit(M.begin(), M.end());
98 void visit(Function &F) {
99 static_cast<SubClass*>(this)->visitFunction(F);
100 visit(F.begin(), F.end());
102 void visit(BasicBlock &BB) {
103 static_cast<SubClass*>(this)->visitBasicBlock(BB);
104 visit(BB.begin(), BB.end());
107 // Forwarding functions so that the user can visit with pointers AND refs.
108 void visit(Module *M) { visit(*M); }
109 void visit(Function *F) { visit(*F); }
110 void visit(BasicBlock *BB) { visit(*BB); }
111 RetTy visit(Instruction *I) { return visit(*I); }
113 // visit - Finally, code to visit an instruction...
115 RetTy visit(Instruction &I) {
116 switch (I.getOpcode()) {
117 default: llvm_unreachable("Unknown instruction type encountered!");
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"
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.
135 // When visiting a module, function or basic block directly, these methods get
136 // called to indicate when transitioning into a new unit.
138 void visitModule (Module &M) {}
139 void visitFunction (Function &F) {}
140 void visitBasicBlock(BasicBlock &BB) {}
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.
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.
153 #define HANDLE_INST(NUM, OPCODE, CLASS) \
154 RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
155 #include "llvm/Instruction.def"
157 // Specific Instruction type classes... note that all of the casts are
158 // necessary because we use the instruction classes as opaque types...
160 RetTy visitReturnInst(ReturnInst &I) { DELEGATE(TerminatorInst);}
161 RetTy visitBranchInst(BranchInst &I) { DELEGATE(TerminatorInst);}
162 RetTy visitSwitchInst(SwitchInst &I) { DELEGATE(TerminatorInst);}
163 RetTy visitIndirectBrInst(IndirectBrInst &I) { DELEGATE(TerminatorInst);}
164 RetTy visitInvokeInst(InvokeInst &I) { DELEGATE(TerminatorInst);}
165 RetTy visitUnwindInst(UnwindInst &I) { DELEGATE(TerminatorInst);}
166 RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);}
167 RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);}
168 RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);}
169 RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(Instruction); }
170 RetTy visitLoadInst(LoadInst &I) { DELEGATE(Instruction); }
171 RetTy visitStoreInst(StoreInst &I) { DELEGATE(Instruction); }
172 RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction); }
173 RetTy visitPHINode(PHINode &I) { DELEGATE(Instruction); }
174 RetTy visitTruncInst(TruncInst &I) { DELEGATE(CastInst); }
175 RetTy visitZExtInst(ZExtInst &I) { DELEGATE(CastInst); }
176 RetTy visitSExtInst(SExtInst &I) { DELEGATE(CastInst); }
177 RetTy visitFPTruncInst(FPTruncInst &I) { DELEGATE(CastInst); }
178 RetTy visitFPExtInst(FPExtInst &I) { DELEGATE(CastInst); }
179 RetTy visitFPToUIInst(FPToUIInst &I) { DELEGATE(CastInst); }
180 RetTy visitFPToSIInst(FPToSIInst &I) { DELEGATE(CastInst); }
181 RetTy visitUIToFPInst(UIToFPInst &I) { DELEGATE(CastInst); }
182 RetTy visitSIToFPInst(SIToFPInst &I) { DELEGATE(CastInst); }
183 RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst); }
184 RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst); }
185 RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst); }
186 RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction); }
187 RetTy visitCallInst(CallInst &I) { DELEGATE(Instruction); }
188 RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(Instruction); }
189 RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
190 RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction); }
191 RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction); }
192 RetTy visitExtractValueInst(ExtractValueInst &I) { DELEGATE(Instruction);}
193 RetTy visitInsertValueInst(InsertValueInst &I) { DELEGATE(Instruction); }
195 // Next level propagators: If the user does not overload a specific
196 // instruction type, they can overload one of these to get the whole class
197 // of instructions...
199 RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); }
200 RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); }
201 RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction); }
202 RetTy visitCastInst(CastInst &I) { DELEGATE(Instruction); }
204 // If the user wants a 'default' case, they can choose to override this
205 // function. If this function is not overloaded in the user's subclass, then
206 // this instruction just gets ignored.
208 // Note that you MUST override this function if your return type is not void.
210 void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
215 } // End llvm namespace