LLVM API Documentation
00001 //===- llvm/Support/InstVisitor.h - Define instruction visitors -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 00011 #ifndef LLVM_SUPPORT_INSTVISITOR_H 00012 #define LLVM_SUPPORT_INSTVISITOR_H 00013 00014 #include "llvm/Function.h" 00015 #include "llvm/Instructions.h" 00016 #include "llvm/Module.h" 00017 00018 namespace llvm { 00019 00020 // We operate on opaque instruction classes, so forward declare all instruction 00021 // types now... 00022 // 00023 #define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS; 00024 #include "llvm/Instruction.def" 00025 00026 #define DELEGATE(CLASS_TO_VISIT) \ 00027 return static_cast<SubClass*>(this)-> \ 00028 visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I)) 00029 00030 00031 /// @brief Base class for instruction visitors 00032 /// 00033 /// Instruction visitors are used when you want to perform different action for 00034 /// different kinds of instruction without without having to use lots of casts 00035 /// and a big switch statement (in your code that is). 00036 /// 00037 /// To define your own visitor, inherit from this class, specifying your 00038 /// new type for the 'SubClass' template parameter, and "override" visitXXX 00039 /// functions in your class. I say "overriding" because this class is defined 00040 /// in terms of statically resolved overloading, not virtual functions. 00041 /// 00042 /// For example, here is a visitor that counts the number of malloc 00043 /// instructions processed: 00044 /// 00045 /// /// Declare the class. Note that we derive from InstVisitor instantiated 00046 /// /// with _our new subclasses_ type. 00047 /// /// 00048 /// struct CountMallocVisitor : public InstVisitor<CountMallocVisitor> { 00049 /// unsigned Count; 00050 /// CountMallocVisitor() : Count(0) {} 00051 /// 00052 /// void visitMallocInst(MallocInst &MI) { ++Count; } 00053 /// }; 00054 /// 00055 /// And this class would be used like this: 00056 /// CountMallocVistor CMV; 00057 /// CMV.visit(function); 00058 /// NumMallocs = CMV.Count; 00059 /// 00060 /// The defined has 'visit' methods for Instruction, and also for BasicBlock, 00061 /// Function, and Module, which recursively process all conained instructions. 00062 /// 00063 /// Note that if you don't implement visitXXX for some instruction type, 00064 /// the visitXXX method for instruction superclass will be invoked. So 00065 /// if instructions are added in the future, they will be automatically 00066 /// supported, if you handle on of their superclasses. 00067 /// 00068 /// The optional second template argument specifies the type that instruction 00069 /// visitation functions should return. If you specify this, you *MUST* provide 00070 /// an implementation of visitInstruction though!. 00071 /// 00072 /// Note that this class is specifically designed as a template to avoid 00073 /// virtual function call overhead. Defining and using an InstVisitor is just 00074 /// as efficient as having your own switch statement over the instruction 00075 /// opcode. 00076 template<typename SubClass, typename RetTy=void> 00077 class InstVisitor { 00078 //===--------------------------------------------------------------------===// 00079 // Interface code - This is the public interface of the InstVisitor that you 00080 // use to visit instructions... 00081 // 00082 00083 public: 00084 // Generic visit method - Allow visitation to all instructions in a range 00085 template<class Iterator> 00086 void visit(Iterator Start, Iterator End) { 00087 while (Start != End) 00088 static_cast<SubClass*>(this)->visit(*Start++); 00089 } 00090 00091 // Define visitors for functions and basic blocks... 00092 // 00093 void visit(Module &M) { 00094 static_cast<SubClass*>(this)->visitModule(M); 00095 visit(M.begin(), M.end()); 00096 } 00097 void visit(Function &F) { 00098 static_cast<SubClass*>(this)->visitFunction(F); 00099 visit(F.begin(), F.end()); 00100 } 00101 void visit(BasicBlock &BB) { 00102 static_cast<SubClass*>(this)->visitBasicBlock(BB); 00103 visit(BB.begin(), BB.end()); 00104 } 00105 00106 // Forwarding functions so that the user can visit with pointers AND refs. 00107 void visit(Module *M) { visit(*M); } 00108 void visit(Function *F) { visit(*F); } 00109 void visit(BasicBlock *BB) { visit(*BB); } 00110 RetTy visit(Instruction *I) { return visit(*I); } 00111 00112 // visit - Finally, code to visit an instruction... 00113 // 00114 RetTy visit(Instruction &I) { 00115 switch (I.getOpcode()) { 00116 default: assert(0 && "Unknown instruction type encountered!"); 00117 abort(); 00118 // Build the switch statement using the Instruction.def file... 00119 #define HANDLE_INST(NUM, OPCODE, CLASS) \ 00120 case Instruction::OPCODE: return \ 00121 static_cast<SubClass*>(this)-> \ 00122 visit##OPCODE(static_cast<CLASS&>(I)); 00123 #include "llvm/Instruction.def" 00124 } 00125 } 00126 00127 //===--------------------------------------------------------------------===// 00128 // Visitation functions... these functions provide default fallbacks in case 00129 // the user does not specify what to do for a particular instruction type. 00130 // The default behavior is to generalize the instruction type to its subtype 00131 // and try visiting the subtype. All of this should be inlined perfectly, 00132 // because there are no virtual functions to get in the way. 00133 // 00134 00135 // When visiting a module, function or basic block directly, these methods get 00136 // called to indicate when transitioning into a new unit. 00137 // 00138 void visitModule (Module &M) {} 00139 void visitFunction (Function &F) {} 00140 void visitBasicBlock(BasicBlock &BB) {} 00141 00142 // Define instruction specific visitor functions that can be overridden to 00143 // handle SPECIFIC instructions. These functions automatically define 00144 // visitMul to proxy to visitBinaryOperator for instance in case the user does 00145 // not need this generality. 00146 // 00147 // The one problem case we have to handle here though is that the PHINode 00148 // class and opcode name are the exact same. Because of this, we cannot 00149 // define visitPHINode (the inst version) to forward to visitPHINode (the 00150 // generic version) without multiply defined symbols and recursion. To handle 00151 // this, we do not autoexpand "Other" instructions, we do it manually. 00152 // 00153 #define HANDLE_INST(NUM, OPCODE, CLASS) \ 00154 RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); } 00155 #include "llvm/Instruction.def" 00156 00157 // Specific Instruction type classes... note that all of the casts are 00158 // necessary because we use the instruction classes as opaque types... 00159 // 00160 RetTy visitReturnInst(ReturnInst &I) { DELEGATE(TerminatorInst);} 00161 RetTy visitBranchInst(BranchInst &I) { DELEGATE(TerminatorInst);} 00162 RetTy visitSwitchInst(SwitchInst &I) { DELEGATE(TerminatorInst);} 00163 RetTy visitInvokeInst(InvokeInst &I) { DELEGATE(TerminatorInst);} 00164 RetTy visitUnwindInst(UnwindInst &I) { DELEGATE(TerminatorInst);} 00165 RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);} 00166 RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);} 00167 RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);} 00168 RetTy visitVICmpInst(VICmpInst &I) { DELEGATE(CmpInst);} 00169 RetTy visitVFCmpInst(VFCmpInst &I) { DELEGATE(CmpInst);} 00170 RetTy visitMallocInst(MallocInst &I) { DELEGATE(AllocationInst);} 00171 RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(AllocationInst);} 00172 RetTy visitFreeInst(FreeInst &I) { DELEGATE(Instruction); } 00173 RetTy visitLoadInst(LoadInst &I) { DELEGATE(Instruction); } 00174 RetTy visitStoreInst(StoreInst &I) { DELEGATE(Instruction); } 00175 RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction); } 00176 RetTy visitPHINode(PHINode &I) { DELEGATE(Instruction); } 00177 RetTy visitTruncInst(TruncInst &I) { DELEGATE(CastInst); } 00178 RetTy visitZExtInst(ZExtInst &I) { DELEGATE(CastInst); } 00179 RetTy visitSExtInst(SExtInst &I) { DELEGATE(CastInst); } 00180 RetTy visitFPTruncInst(FPTruncInst &I) { DELEGATE(CastInst); } 00181 RetTy visitFPExtInst(FPExtInst &I) { DELEGATE(CastInst); } 00182 RetTy visitFPToUIInst(FPToUIInst &I) { DELEGATE(CastInst); } 00183 RetTy visitFPToSIInst(FPToSIInst &I) { DELEGATE(CastInst); } 00184 RetTy visitUIToFPInst(UIToFPInst &I) { DELEGATE(CastInst); } 00185 RetTy visitSIToFPInst(SIToFPInst &I) { DELEGATE(CastInst); } 00186 RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst); } 00187 RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst); } 00188 RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst); } 00189 RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction); } 00190 RetTy visitCallInst(CallInst &I) { DELEGATE(Instruction); } 00191 RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(Instruction); } 00192 RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);} 00193 RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction); } 00194 RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction); } 00195 RetTy visitExtractValueInst(ExtractValueInst &I) { DELEGATE(Instruction);} 00196 RetTy visitInsertValueInst(InsertValueInst &I) { DELEGATE(Instruction); } 00197 00198 // Next level propagators... if the user does not overload a specific 00199 // instruction type, they can overload one of these to get the whole class 00200 // of instructions... 00201 // 00202 RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); } 00203 RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); } 00204 RetTy visitAllocationInst(AllocationInst &I) { DELEGATE(Instruction); } 00205 RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction); } 00206 RetTy visitCastInst(CastInst &I) { DELEGATE(Instruction); } 00207 00208 // If the user wants a 'default' case, they can choose to override this 00209 // function. If this function is not overloaded in the users subclass, then 00210 // this instruction just gets ignored. 00211 // 00212 // Note that you MUST override this function if your return type is not void. 00213 // 00214 void visitInstruction(Instruction &I) {} // Ignore unhandled instructions 00215 }; 00216 00217 #undef DELEGATE 00218 00219 } // End llvm namespace 00220 00221 #endif