LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This template class is used to define instruction visitors in a typesafe
00011 // manner without having to use lots of casts and a big switch statement (in
00012 // your code that is).  The win here is that if instructions are added in the
00013 // future, they will be added to the InstVisitor<T> class, allowing you to
00014 // automatically support them (if you handle on of their superclasses).
00015 //
00016 // Note that this library is specifically designed as a template to avoid
00017 // virtual function call overhead.  Defining and using an InstVisitor is just as
00018 // efficient as having your own switch statement over the instruction opcode.
00019 //
00020 // InstVisitor Usage:
00021 //   You define InstVisitors from inheriting from the InstVisitor base class
00022 // and "overriding" functions in your class.  I say "overriding" because this
00023 // class is defined in terms of statically resolved overloading, not virtual
00024 // functions.  As an example, here is a visitor that counts the number of malloc
00025 // instructions processed:
00026 //
00027 //  // Declare the class.  Note that we derive from InstVisitor instantiated
00028 //  // with _our new subclasses_ type.
00029 //  //
00030 //  struct CountMallocVisitor : public InstVisitor<CountMallocVisitor> {
00031 //    unsigned Count;
00032 //    CountMallocVisitor() : Count(0) {}
00033 //
00034 //    void visitMallocInst(MallocInst *MI) { ++Count; }
00035 //  };
00036 //
00037 //  And this class would be used like this:
00038 //    CountMallocVistor CMV;
00039 //    CMV.visit(function);
00040 //    NumMallocs = CMV.Count;
00041 //
00042 // Returning a value from the visitation function:
00043 //   The InstVisitor class takes an optional second template argument that
00044 // specifies what type the instruction visitation functions should return.  If
00045 // you specify this, you *MUST* provide an implementation of visitInstruction
00046 // though!.
00047 //
00048 //===----------------------------------------------------------------------===//
00049 
00050 #ifndef LLVM_SUPPORT_INSTVISITOR_H
00051 #define LLVM_SUPPORT_INSTVISITOR_H
00052 
00053 #include "llvm/Function.h"
00054 #include "llvm/Module.h"
00055 
00056 namespace llvm {
00057 
00058 // We operate on opaque instruction classes, so forward declare all instruction
00059 // types now...
00060 //
00061 #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
00062 #include "llvm/Instruction.def"
00063 
00064 // Forward declare the intermediate types...
00065 class TerminatorInst; class BinaryOperator;
00066 class AllocationInst;
00067 
00068 #define DELEGATE(CLASS_TO_VISIT) \
00069   return ((SubClass*)this)->visit##CLASS_TO_VISIT((CLASS_TO_VISIT&)I)
00070 
00071 
00072 template<typename SubClass, typename RetTy=void>
00073 class InstVisitor {
00074   //===--------------------------------------------------------------------===//
00075   // Interface code - This is the public interface of the InstVisitor that you
00076   // use to visit instructions...
00077   //
00078 
00079 public:
00080   // Generic visit method - Allow visitation to all instructions in a range
00081   template<class Iterator>
00082   void visit(Iterator Start, Iterator End) {
00083     while (Start != End)
00084       ((SubClass*)this)->visit(*Start++);
00085   }
00086 
00087   // Define visitors for functions and basic blocks...
00088   //
00089   void visit(Module &M) {
00090     ((SubClass*)this)->visitModule(M);
00091     visit(M.begin(), M.end());
00092   }
00093   void visit(Function &F) {
00094     ((SubClass*)this)->visitFunction(F);
00095     visit(F.begin(), F.end());
00096   }
00097   void visit(BasicBlock &BB) {
00098     ((SubClass*)this)->visitBasicBlock(BB);
00099     visit(BB.begin(), BB.end());
00100   }
00101 
00102   // Forwarding functions so that the user can visit with pointers AND refs.
00103   void visit(Module       *M)  { visit(*M); }
00104   void visit(Function     *F)  { visit(*F); }
00105   void visit(BasicBlock   *BB) { visit(*BB); }
00106   RetTy visit(Instruction *I)  { return visit(*I); }
00107 
00108   // visit - Finally, code to visit an instruction...
00109   //
00110   RetTy visit(Instruction &I) {
00111     switch (I.getOpcode()) {
00112     default: assert(0 && "Unknown instruction type encountered!");
00113              abort();
00114       // Build the switch statement using the Instruction.def file...
00115 #define HANDLE_INST(NUM, OPCODE, CLASS) \
00116     case Instruction::OPCODE:return ((SubClass*)this)->visit##OPCODE((CLASS&)I);
00117 #include "llvm/Instruction.def"
00118     }
00119   }
00120 
00121   //===--------------------------------------------------------------------===//
00122   // Visitation functions... these functions provide default fallbacks in case
00123   // the user does not specify what to do for a particular instruction type.
00124   // The default behavior is to generalize the instruction type to its subtype
00125   // and try visiting the subtype.  All of this should be inlined perfectly,
00126   // because there are no virtual functions to get in the way.
00127   //
00128 
00129   // When visiting a module, function or basic block directly, these methods get
00130   // called to indicate when transitioning into a new unit.
00131   //
00132   void visitModule    (Module &M) {}
00133   void visitFunction  (Function &F) {}
00134   void visitBasicBlock(BasicBlock &BB) {}
00135 
00136 
00137   // Define instruction specific visitor functions that can be overridden to
00138   // handle SPECIFIC instructions.  These functions automatically define
00139   // visitMul to proxy to visitBinaryOperator for instance in case the user does
00140   // not need this generality.
00141   //
00142   // The one problem case we have to handle here though is that the PHINode
00143   // class and opcode name are the exact same.  Because of this, we cannot
00144   // define visitPHINode (the inst version) to forward to visitPHINode (the
00145   // generic version) without multiply defined symbols and recursion.  To handle
00146   // this, we do not autoexpand "Other" instructions, we do it manually.
00147   //
00148 #define HANDLE_INST(NUM, OPCODE, CLASS) \
00149     RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
00150 #include "llvm/Instruction.def"
00151 
00152   // Specific Instruction type classes... note that all of the casts are
00153   // necessary because we use the instruction classes as opaque types...
00154   //
00155   RetTy visitReturnInst(ReturnInst &I)              { DELEGATE(TerminatorInst);}
00156   RetTy visitBranchInst(BranchInst &I)              { DELEGATE(TerminatorInst);}
00157   RetTy visitSwitchInst(SwitchInst &I)              { DELEGATE(TerminatorInst);}
00158   RetTy visitInvokeInst(InvokeInst &I)              { DELEGATE(TerminatorInst);}
00159   RetTy visitUnwindInst(UnwindInst &I)              { DELEGATE(TerminatorInst);}
00160   RetTy visitUnreachableInst(UnreachableInst &I)    { DELEGATE(TerminatorInst);}
00161   RetTy visitSetCondInst(SetCondInst &I)            { DELEGATE(BinaryOperator);}
00162   RetTy visitMallocInst(MallocInst &I)              { DELEGATE(AllocationInst);}
00163   RetTy visitAllocaInst(AllocaInst &I)              { DELEGATE(AllocationInst);}
00164   RetTy visitFreeInst(FreeInst     &I)              { DELEGATE(Instruction); }
00165   RetTy visitLoadInst(LoadInst     &I)              { DELEGATE(Instruction); }
00166   RetTy visitStoreInst(StoreInst   &I)              { DELEGATE(Instruction); }
00167   RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction); }
00168   RetTy visitPHINode(PHINode       &I)              { DELEGATE(Instruction); }
00169   RetTy visitCastInst(CastInst     &I)              { DELEGATE(Instruction); }
00170   RetTy visitSelectInst(SelectInst &I)              { DELEGATE(Instruction); }
00171   RetTy visitCallInst(CallInst     &I)              { DELEGATE(Instruction); }
00172   RetTy visitShiftInst(ShiftInst   &I)              { DELEGATE(Instruction); }
00173   RetTy visitVANextInst(VANextInst &I)              { DELEGATE(Instruction); }
00174   RetTy visitVAArgInst(VAArgInst   &I)              { DELEGATE(Instruction); }
00175 
00176   // Next level propagators... if the user does not overload a specific
00177   // instruction type, they can overload one of these to get the whole class
00178   // of instructions...
00179   //
00180   RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); }
00181   RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); }
00182   RetTy visitAllocationInst(AllocationInst &I) { DELEGATE(Instruction); }
00183 
00184   // If the user wants a 'default' case, they can choose to override this
00185   // function.  If this function is not overloaded in the users subclass, then
00186   // this instruction just gets ignored.
00187   //
00188   // Note that you MUST override this function if your return type is not void.
00189   //
00190   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
00191 };
00192 
00193 #undef DELEGATE
00194 
00195 } // End llvm namespace
00196 
00197 #endif