LCOV - code coverage report
Current view: top level - include/llvm/IR - InstVisitor.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 122 212 57.5 %
Date: 2018-09-23 13:06:45 Functions: 59 831 7.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- InstVisitor.h - Instruction visitor templates ------------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : 
      11             : #ifndef LLVM_IR_INSTVISITOR_H
      12             : #define LLVM_IR_INSTVISITOR_H
      13             : 
      14             : #include "llvm/IR/CallSite.h"
      15             : #include "llvm/IR/Function.h"
      16             : #include "llvm/IR/Instructions.h"
      17             : #include "llvm/IR/IntrinsicInst.h"
      18             : #include "llvm/IR/Intrinsics.h"
      19             : #include "llvm/IR/Module.h"
      20             : #include "llvm/Support/ErrorHandling.h"
      21             : 
      22             : namespace llvm {
      23             : 
      24             : // We operate on opaque instruction classes, so forward declare all instruction
      25             : // types now...
      26             : //
      27             : #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
      28             : #include "llvm/IR/Instruction.def"
      29             : 
      30             : #define DELEGATE(CLASS_TO_VISIT) \
      31             :   return static_cast<SubClass*>(this)-> \
      32             :                visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
      33             : 
      34             : 
      35             : /// Base class for instruction visitors
      36             : ///
      37             : /// Instruction visitors are used when you want to perform different actions
      38             : /// for different kinds of instructions without having to use lots of casts
      39             : /// and a big switch statement (in your code, that is).
      40             : ///
      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 "override" because this class is defined
      44             : /// in terms of statically resolved overloading, not virtual functions.
      45             : ///
      46             : /// For example, here is a visitor that counts the number of malloc
      47             : /// instructions processed:
      48             : ///
      49             : ///  /// Declare the class.  Note that we derive from InstVisitor instantiated
      50             : ///  /// with _our new subclasses_ type.
      51             : ///  ///
      52             : ///  struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
      53             : ///    unsigned Count;
      54             : ///    CountAllocaVisitor() : Count(0) {}
      55             : ///
      56             : ///    void visitAllocaInst(AllocaInst &AI) { ++Count; }
      57             : ///  };
      58             : ///
      59             : ///  And this class would be used like this:
      60             : ///    CountAllocaVisitor CAV;
      61             : ///    CAV.visit(function);
      62             : ///    NumAllocas = CAV.Count;
      63             : ///
      64             : /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
      65             : /// Function, and Module, which recursively process all contained instructions.
      66             : ///
      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 one of their superclasses.
      71             : ///
      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!.
      75             : ///
      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
      79             : /// opcode.
      80             : template<typename SubClass, typename RetTy=void>
      81             : class InstVisitor {
      82             :   //===--------------------------------------------------------------------===//
      83             :   // Interface code - This is the public interface of the InstVisitor that you
      84             :   // use to visit instructions...
      85             :   //
      86             : 
      87             : public:
      88             :   // Generic visit method - Allow visitation to all instructions in a range
      89             :   template<class Iterator>
      90      110398 :   void visit(Iterator Start, Iterator End) {
      91    22758749 :     while (Start != End)
      92    18725048 :       static_cast<SubClass*>(this)->visit(*Start++);
      93      110397 :   }
      94         405 : 
      95        7050 :   // Define visitors for functions and basic blocks...
      96        3655 :   //
      97         405 :   void visit(Module &M) {
      98         304 :     static_cast<SubClass*>(this)->visitModule(M);
      99        5535 :     visit(M.begin(), M.end());
     100        2875 :   }
     101     1109013 :   void visit(Function &F) {
     102     1108865 :     static_cast<SubClass*>(this)->visitFunction(F);
     103      112185 :     visit(F.begin(), F.end());
     104     1110041 :   }
     105     1201524 :   void visit(BasicBlock &BB) {
     106     1201368 :     static_cast<SubClass*>(this)->visitBasicBlock(BB);
     107             :     visit(BB.begin(), BB.end());
     108     1201368 :   }
     109           0 : 
     110             :   // Forwarding functions so that the user can visit with pointers AND refs.
     111             :   void visit(Module       *M)  { visit(*M); }
     112           0 :   void visit(Function     *F)  { visit(*F); }
     113           0 :   void visit(BasicBlock   *BB) { visit(*BB); }
     114    19844585 :   RetTy visit(Instruction *I)  { return visit(*I); }
     115         709 : 
     116           0 :   // visit - Finally, code to visit an instruction...
     117             :   //
     118    71093174 :   RetTy visit(Instruction &I) {
     119             :     static_assert(std::is_base_of<InstVisitor, SubClass>::value,
     120             :                   "Must pass the derived type to this template!");
     121             : 
     122    64349776 :     switch (I.getOpcode()) {
     123           0 :     default: llvm_unreachable("Unknown instruction type encountered!");
     124             :       // Build the switch statement using the Instruction.def file...
     125             : #define HANDLE_INST(NUM, OPCODE, CLASS) \
     126           0 :     case Instruction::OPCODE: return \
     127             :            static_cast<SubClass*>(this)-> \
     128             :                       visit##OPCODE(static_cast<CLASS&>(I));
     129             : #include "llvm/IR/Instruction.def"
     130        7862 :     }
     131           0 :   }
     132      652669 : 
     133             :   //===--------------------------------------------------------------------===//
     134             :   // Visitation functions... these functions provide default fallbacks in case
     135           0 :   // the user does not specify what to do for a particular instruction type.
     136      652669 :   // The default behavior is to generalize the instruction type to its subtype
     137           0 :   // and try visiting the subtype.  All of this should be inlined perfectly,
     138             :   // because there are no virtual functions to get in the way.
     139             :   //
     140             : 
     141             :   // When visiting a module, function or basic block directly, these methods get
     142             :   // called to indicate when transitioning into a new unit.
     143             :   //
     144        3655 :   void visitModule    (Module &M) {}
     145           0 :   void visitFunction  (Function &F) {}
     146     2622123 :   void visitBasicBlock(BasicBlock &BB) {}
     147             : 
     148             :   // Define instruction specific visitor functions that can be overridden to
     149           0 :   // handle SPECIFIC instructions.  These functions automatically define
     150     2622123 :   // visitMul to proxy to visitBinaryOperator for instance in case the user does
     151           0 :   // not need this generality.
     152             :   //
     153             :   // These functions can also implement fan-out, when a single opcode and
     154             :   // instruction have multiple more specific Instruction subclasses. The Call
     155             :   // instruction currently supports this. We implement that by redirecting that
     156             :   // instruction to a special delegation helper.
     157             : #define HANDLE_INST(NUM, OPCODE, CLASS) \
     158        2875 :     RetTy visit##OPCODE(CLASS &I) { \
     159             :       if (NUM == Instruction::Call) \
     160      370311 :         return delegateCallInst(I); \
     161             :       else \
     162             :         DELEGATE(CLASS); \
     163           0 :     }
     164      370311 : #include "llvm/IR/Instruction.def"
     165           0 : 
     166             :   // Specific Instruction type classes... note that all of the casts are
     167             :   // necessary because we use the instruction classes as opaque types...
     168             :   //
     169           0 :   RetTy visitReturnInst(ReturnInst &I)            { DELEGATE(TerminatorInst);}
     170      341630 :   RetTy visitBranchInst(BranchInst &I)            { DELEGATE(TerminatorInst);}
     171        2488 :   RetTy visitSwitchInst(SwitchInst &I)            { DELEGATE(TerminatorInst);}
     172        1346 :   RetTy visitIndirectBrInst(IndirectBrInst &I)    { DELEGATE(TerminatorInst);}
     173           0 :   RetTy visitResumeInst(ResumeInst &I)            { DELEGATE(TerminatorInst);}
     174       12502 :   RetTy visitUnreachableInst(UnreachableInst &I)  { DELEGATE(TerminatorInst);}
     175          34 :   RetTy visitCleanupReturnInst(CleanupReturnInst &I) { DELEGATE(TerminatorInst);}
     176           8 :   RetTy visitCatchReturnInst(CatchReturnInst &I)  { DELEGATE(TerminatorInst); }
     177           0 :   RetTy visitCatchSwitchInst(CatchSwitchInst &I)  { DELEGATE(TerminatorInst);}
     178      513696 :   RetTy visitICmpInst(ICmpInst &I)                { DELEGATE(CmpInst);}
     179        2668 :   RetTy visitFCmpInst(FCmpInst &I)                { DELEGATE(CmpInst);}
     180     2344476 :   RetTy visitAllocaInst(AllocaInst &I)            { DELEGATE(UnaryInstruction);}
     181           0 :   RetTy visitLoadInst(LoadInst     &I)            { DELEGATE(UnaryInstruction);}
     182        5288 :   RetTy visitStoreInst(StoreInst   &I)            { DELEGATE(Instruction);}
     183         600 :   RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { DELEGATE(Instruction);}
     184         527 :   RetTy visitAtomicRMWInst(AtomicRMWInst &I)      { DELEGATE(Instruction);}
     185          10 :   RetTy visitFenceInst(FenceInst   &I)            { DELEGATE(Instruction);}
     186        6974 :   RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction);}
     187        9441 :   RetTy visitPHINode(PHINode       &I)            { DELEGATE(Instruction);}
     188       15642 :   RetTy visitTruncInst(TruncInst &I)              { DELEGATE(CastInst);}
     189       52557 :   RetTy visitZExtInst(ZExtInst &I)                { DELEGATE(CastInst);}
     190       13783 :   RetTy visitSExtInst(SExtInst &I)                { DELEGATE(CastInst);}
     191         383 :   RetTy visitFPTruncInst(FPTruncInst &I)          { DELEGATE(CastInst);}
     192         509 :   RetTy visitFPExtInst(FPExtInst &I)              { DELEGATE(CastInst);}
     193          62 :   RetTy visitFPToUIInst(FPToUIInst &I)            { DELEGATE(CastInst);}
     194          78 :   RetTy visitFPToSIInst(FPToSIInst &I)            { DELEGATE(CastInst);}
     195         118 :   RetTy visitUIToFPInst(UIToFPInst &I)            { DELEGATE(CastInst);}
     196         197 :   RetTy visitSIToFPInst(SIToFPInst &I)            { DELEGATE(CastInst);}
     197       19160 :   RetTy visitPtrToIntInst(PtrToIntInst &I)        { DELEGATE(CastInst);}
     198        5465 :   RetTy visitIntToPtrInst(IntToPtrInst &I)        { DELEGATE(CastInst);}
     199      261753 :   RetTy visitBitCastInst(BitCastInst &I)          { DELEGATE(CastInst);}
     200         504 :   RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}
     201       11284 :   RetTy visitSelectInst(SelectInst &I)            { DELEGATE(Instruction);}
     202           0 :   RetTy visitVAArgInst(VAArgInst   &I)            { DELEGATE(UnaryInstruction);}
     203           8 :   RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
     204           0 :   RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction);}
     205           0 :   RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction);}
     206           0 :   RetTy visitExtractValueInst(ExtractValueInst &I){ DELEGATE(UnaryInstruction);}
     207           0 :   RetTy visitInsertValueInst(InsertValueInst &I)  { DELEGATE(Instruction); }
     208      130067 :   RetTy visitLandingPadInst(LandingPadInst &I)    { DELEGATE(Instruction); }
     209           6 :   RetTy visitFuncletPadInst(FuncletPadInst &I) { DELEGATE(Instruction); }
     210           0 :   RetTy visitCleanupPadInst(CleanupPadInst &I) { DELEGATE(FuncletPadInst); }
     211           0 :   RetTy visitCatchPadInst(CatchPadInst &I)     { DELEGATE(FuncletPadInst); }
     212           0 : 
     213           0 :   // Handle the special instrinsic instruction classes.
     214           0 :   RetTy visitDbgDeclareInst(DbgDeclareInst &I)    { DELEGATE(DbgVariableIntrinsic);}
     215           0 :   RetTy visitDbgValueInst(DbgValueInst &I)        { DELEGATE(DbgVariableIntrinsic);}
     216           0 :   RetTy visitDbgVariableIntrinsic(DbgVariableIntrinsic &I)
     217           0 :                                                   { DELEGATE(DbgInfoIntrinsic);}
     218           0 :   RetTy visitDbgLabelInst(DbgLabelInst &I)        { DELEGATE(DbgInfoIntrinsic);}
     219          47 :   RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I){ DELEGATE(IntrinsicInst); }
     220        1871 :   RetTy visitMemSetInst(MemSetInst &I)            { DELEGATE(MemIntrinsic); }
     221           0 :   RetTy visitMemCpyInst(MemCpyInst &I)            { DELEGATE(MemTransferInst); }
     222           0 :   RetTy visitMemMoveInst(MemMoveInst &I)          { DELEGATE(MemTransferInst); }
     223        2232 :   RetTy visitMemTransferInst(MemTransferInst &I)  { DELEGATE(MemIntrinsic); }
     224          92 :   RetTy visitMemIntrinsic(MemIntrinsic &I)        { DELEGATE(IntrinsicInst); }
     225          20 :   RetTy visitVAStartInst(VAStartInst &I)          { DELEGATE(IntrinsicInst); }
     226           9 :   RetTy visitVAEndInst(VAEndInst &I)              { DELEGATE(IntrinsicInst); }
     227           0 :   RetTy visitVACopyInst(VACopyInst &I)            { DELEGATE(IntrinsicInst); }
     228     2567194 :   RetTy visitIntrinsicInst(IntrinsicInst &I)      { DELEGATE(CallInst); }
     229           0 : 
     230           0 :   // Call and Invoke are slightly different as they delegate first through
     231           0 :   // a generic CallSite visitor.
     232           0 :   RetTy visitCallInst(CallInst &I) {
     233      975474 :     return static_cast<SubClass*>(this)->visitCallSite(&I);
     234           0 :   }
     235           0 :   RetTy visitInvokeInst(InvokeInst &I) {
     236      187478 :     return static_cast<SubClass*>(this)->visitCallSite(&I);
     237           0 :   }
     238           0 : 
     239           0 :   // Next level propagators: If the user does not overload a specific
     240           0 :   // instruction type, they can overload one of these to get the whole class
     241           0 :   // of instructions...
     242           0 :   //
     243           0 :   RetTy visitCastInst(CastInst &I)                { DELEGATE(UnaryInstruction);}
     244           0 :   RetTy visitBinaryOperator(BinaryOperator &I)    { DELEGATE(Instruction);}
     245         600 :   RetTy visitCmpInst(CmpInst &I)                  { DELEGATE(Instruction);}
     246      194577 :   RetTy visitTerminatorInst(TerminatorInst &I)    { DELEGATE(Instruction);}
     247        2232 :   RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
     248           0 : 
     249           1 :   // Provide a special visitor for a 'callsite' that visits both calls and
     250           0 :   // invokes. When unimplemented, properly delegates to either the terminator or
     251           0 :   // regular instruction visitor.
     252     1133648 :   RetTy visitCallSite(CallSite CS) {
     253           0 :     assert(CS);
     254           0 :     Instruction &I = *CS.getInstruction();
     255     1133648 :     if (CS.isCall())
     256      947111 :       DELEGATE(Instruction);
     257           0 : 
     258           0 :     assert(CS.isInvoke());
     259      187118 :     DELEGATE(TerminatorInst);
     260           0 :   }
     261       21537 : 
     262           0 :   // If the user wants a 'default' case, they can choose to override this
     263           0 :   // function.  If this function is not overloaded in the user's subclass, then
     264        3274 :   // this instruction just gets ignored.
     265           0 :   //
     266           0 :   // Note that you MUST override this function if your return type is not void.
     267        9697 :   //
     268          22 :   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
     269           0 : 
     270           0 : private:
     271          38 :   // Special helper function to delegate to CallInst subclass visitors.
     272     5456306 :   RetTy delegateCallInst(CallInst &I) {
     273           2 :     if (const Function *F = I.getCalledFunction()) {
     274     5242257 :       switch (F->getIntrinsicID()) {
     275     1795454 :       default:                     DELEGATE(IntrinsicInst);
     276       11615 :       case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst);
     277      453276 :       case Intrinsic::dbg_value:   DELEGATE(DbgValueInst);
     278       23794 :       case Intrinsic::dbg_label:   DELEGATE(DbgLabelInst);
     279       70739 :       case Intrinsic::memcpy:      DELEGATE(MemCpyInst);
     280       10665 :       case Intrinsic::memmove:     DELEGATE(MemMoveInst);
     281      250663 :       case Intrinsic::memset:      DELEGATE(MemSetInst);
     282        1920 :       case Intrinsic::vastart:     DELEGATE(VAStartInst);
     283        1080 :       case Intrinsic::vaend:       DELEGATE(VAEndInst);
     284         106 :       case Intrinsic::vacopy:      DELEGATE(VACopyInst);
     285           0 :       case Intrinsic::not_intrinsic: break;
     286          14 :       }
     287       23717 :     }
     288     2016968 :     DELEGATE(CallInst);
     289             :   }
     290       29147 : 
     291       29246 :   // An overload that will never actually be called, it is used only from dead
     292             :   // code in the dispatching from opcodes to instruction subclasses.
     293       29147 :   RetTy delegateCallInst(Instruction &I) {
     294       29147 :     llvm_unreachable("delegateCallInst called for non-CallInst");
     295             :   }
     296           0 : };
     297           0 : 
     298             : #undef DELEGATE
     299           0 : 
     300       21544 : } // End llvm namespace
     301             : 
     302       21537 : #endif

Generated by: LCOV version 1.13