LLVM API Documentation

InstVisitor.h
Go to the documentation of this file.
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 #include "llvm/Support/CallSite.h"
00018 #include "llvm/Support/ErrorHandling.h"
00019 
00020 namespace llvm {
00021 
00022 // We operate on opaque instruction classes, so forward declare all instruction
00023 // types now...
00024 //
00025 #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
00026 #include "llvm/Instruction.def"
00027 
00028 #define DELEGATE(CLASS_TO_VISIT) \
00029   return static_cast<SubClass*>(this)-> \
00030                visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
00031 
00032 
00033 /// @brief Base class for instruction visitors
00034 ///
00035 /// Instruction visitors are used when you want to perform different actions
00036 /// for different kinds of instructions without having to use lots of casts
00037 /// and a big switch statement (in your code, that is).
00038 ///
00039 /// To define your own visitor, inherit from this class, specifying your
00040 /// new type for the 'SubClass' template parameter, and "override" visitXXX
00041 /// functions in your class. I say "override" because this class is defined
00042 /// in terms of statically resolved overloading, not virtual functions.
00043 ///
00044 /// For example, here is a visitor that counts the number of malloc
00045 /// instructions processed:
00046 ///
00047 ///  /// Declare the class.  Note that we derive from InstVisitor instantiated
00048 ///  /// with _our new subclasses_ type.
00049 ///  ///
00050 ///  struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
00051 ///    unsigned Count;
00052 ///    CountAllocaVisitor() : Count(0) {}
00053 ///
00054 ///    void visitAllocaInst(AllocaInst &AI) { ++Count; }
00055 ///  };
00056 ///
00057 ///  And this class would be used like this:
00058 ///    CountAllocaVisitor CAV;
00059 ///    CAV.visit(function);
00060 ///    NumAllocas = CAV.Count;
00061 ///
00062 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
00063 /// Function, and Module, which recursively process all contained instructions.
00064 ///
00065 /// Note that if you don't implement visitXXX for some instruction type,
00066 /// the visitXXX method for instruction superclass will be invoked. So
00067 /// if instructions are added in the future, they will be automatically
00068 /// supported, if you handle one of their superclasses.
00069 ///
00070 /// The optional second template argument specifies the type that instruction
00071 /// visitation functions should return. If you specify this, you *MUST* provide
00072 /// an implementation of visitInstruction though!.
00073 ///
00074 /// Note that this class is specifically designed as a template to avoid
00075 /// virtual function call overhead.  Defining and using an InstVisitor is just
00076 /// as efficient as having your own switch statement over the instruction
00077 /// opcode.
00078 template<typename SubClass, typename RetTy=void>
00079 class InstVisitor {
00080   //===--------------------------------------------------------------------===//
00081   // Interface code - This is the public interface of the InstVisitor that you
00082   // use to visit instructions...
00083   //
00084 
00085 public:
00086   // Generic visit method - Allow visitation to all instructions in a range
00087   template<class Iterator>
00088   void visit(Iterator Start, Iterator End) {
00089     while (Start != End)
00090       static_cast<SubClass*>(this)->visit(*Start++);
00091   }
00092 
00093   // Define visitors for functions and basic blocks...
00094   //
00095   void visit(Module &M) {
00096     static_cast<SubClass*>(this)->visitModule(M);
00097     visit(M.begin(), M.end());
00098   }
00099   void visit(Function &F) {
00100     static_cast<SubClass*>(this)->visitFunction(F);
00101     visit(F.begin(), F.end());
00102   }
00103   void visit(BasicBlock &BB) {
00104     static_cast<SubClass*>(this)->visitBasicBlock(BB);
00105     visit(BB.begin(), BB.end());
00106   }
00107 
00108   // Forwarding functions so that the user can visit with pointers AND refs.
00109   void visit(Module       *M)  { visit(*M); }
00110   void visit(Function     *F)  { visit(*F); }
00111   void visit(BasicBlock   *BB) { visit(*BB); }
00112   RetTy visit(Instruction *I)  { return visit(*I); }
00113 
00114   // visit - Finally, code to visit an instruction...
00115   //
00116   RetTy visit(Instruction &I) {
00117     switch (I.getOpcode()) {
00118     default: llvm_unreachable("Unknown instruction type encountered!");
00119       // Build the switch statement using the Instruction.def file...
00120 #define HANDLE_INST(NUM, OPCODE, CLASS) \
00121     case Instruction::OPCODE: return \
00122            static_cast<SubClass*>(this)-> \
00123                       visit##OPCODE(static_cast<CLASS&>(I));
00124 #include "llvm/Instruction.def"
00125     }
00126   }
00127 
00128   //===--------------------------------------------------------------------===//
00129   // Visitation functions... these functions provide default fallbacks in case
00130   // the user does not specify what to do for a particular instruction type.
00131   // The default behavior is to generalize the instruction type to its subtype
00132   // and try visiting the subtype.  All of this should be inlined perfectly,
00133   // because there are no virtual functions to get in the way.
00134   //
00135 
00136   // When visiting a module, function or basic block directly, these methods get
00137   // called to indicate when transitioning into a new unit.
00138   //
00139   void visitModule    (Module &M) {}
00140   void visitFunction  (Function &F) {}
00141   void visitBasicBlock(BasicBlock &BB) {}
00142 
00143   // Define instruction specific visitor functions that can be overridden to
00144   // handle SPECIFIC instructions.  These functions automatically define
00145   // visitMul to proxy to visitBinaryOperator for instance in case the user does
00146   // not need this generality.
00147   //
00148   // The one problem case we have to handle here though is that the PHINode
00149   // class and opcode name are the exact same.  Because of this, we cannot
00150   // define visitPHINode (the inst version) to forward to visitPHINode (the
00151   // generic version) without multiply defined symbols and recursion.  To handle
00152   // this, we do not autoexpand "Other" instructions, we do it manually.
00153   //
00154 #define HANDLE_INST(NUM, OPCODE, CLASS) \
00155     RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
00156 #include "llvm/Instruction.def"
00157 
00158   // Specific Instruction type classes... note that all of the casts are
00159   // necessary because we use the instruction classes as opaque types...
00160   //
00161   RetTy visitReturnInst(ReturnInst &I)            { DELEGATE(TerminatorInst);}
00162   RetTy visitBranchInst(BranchInst &I)            { DELEGATE(TerminatorInst);}
00163   RetTy visitSwitchInst(SwitchInst &I)            { DELEGATE(TerminatorInst);}
00164   RetTy visitIndirectBrInst(IndirectBrInst &I)    { DELEGATE(TerminatorInst);}
00165   RetTy visitResumeInst(ResumeInst &I)            { DELEGATE(TerminatorInst);}
00166   RetTy visitUnreachableInst(UnreachableInst &I)  { DELEGATE(TerminatorInst);}
00167   RetTy visitICmpInst(ICmpInst &I)                { DELEGATE(CmpInst);}
00168   RetTy visitFCmpInst(FCmpInst &I)                { DELEGATE(CmpInst);}
00169   RetTy visitAllocaInst(AllocaInst &I)            { DELEGATE(UnaryInstruction);}
00170   RetTy visitLoadInst(LoadInst     &I)            { DELEGATE(UnaryInstruction);}
00171   RetTy visitStoreInst(StoreInst   &I)            { DELEGATE(Instruction);}
00172   RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { DELEGATE(Instruction);}
00173   RetTy visitAtomicRMWInst(AtomicRMWInst &I)      { DELEGATE(Instruction);}
00174   RetTy visitFenceInst(FenceInst   &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 visitVAArgInst(VAArgInst   &I)            { DELEGATE(UnaryInstruction);}
00191   RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
00192   RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction);}
00193   RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction);}
00194   RetTy visitExtractValueInst(ExtractValueInst &I){ DELEGATE(UnaryInstruction);}
00195   RetTy visitInsertValueInst(InsertValueInst &I)  { DELEGATE(Instruction); }
00196   RetTy visitLandingPadInst(LandingPadInst &I)    { DELEGATE(Instruction); }
00197 
00198   // Call and Invoke are slightly different as they delegate first through
00199   // a generic CallSite visitor.
00200   RetTy visitCallInst(CallInst &I) {
00201     return static_cast<SubClass*>(this)->visitCallSite(&I);
00202   }
00203   RetTy visitInvokeInst(InvokeInst &I) {
00204     return static_cast<SubClass*>(this)->visitCallSite(&I);
00205   }
00206 
00207   // Next level propagators: If the user does not overload a specific
00208   // instruction type, they can overload one of these to get the whole class
00209   // of instructions...
00210   //
00211   RetTy visitCastInst(CastInst &I)                { DELEGATE(UnaryInstruction);}
00212   RetTy visitBinaryOperator(BinaryOperator &I)    { DELEGATE(Instruction);}
00213   RetTy visitCmpInst(CmpInst &I)                  { DELEGATE(Instruction);}
00214   RetTy visitTerminatorInst(TerminatorInst &I)    { DELEGATE(Instruction);}
00215   RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
00216 
00217   // Provide a special visitor for a 'callsite' that visits both calls and
00218   // invokes. When unimplemented, properly delegates to either the terminator or
00219   // regular instruction visitor.
00220   RetTy visitCallSite(CallSite CS) {
00221     assert(CS);
00222     Instruction &I = *CS.getInstruction();
00223     if (CS.isCall())
00224       DELEGATE(Instruction);
00225 
00226     assert(CS.isInvoke());
00227     DELEGATE(TerminatorInst);
00228   }
00229 
00230   // If the user wants a 'default' case, they can choose to override this
00231   // function.  If this function is not overloaded in the user's subclass, then
00232   // this instruction just gets ignored.
00233   //
00234   // Note that you MUST override this function if your return type is not void.
00235   //
00236   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
00237 };
00238 
00239 #undef DELEGATE
00240 
00241 } // End llvm namespace
00242 
00243 #endif