Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:03

0001 //===- InstVisitor.h - Instruction visitor templates ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 
0010 #ifndef LLVM_IR_INSTVISITOR_H
0011 #define LLVM_IR_INSTVISITOR_H
0012 
0013 #include "llvm/IR/Function.h"
0014 #include "llvm/IR/Instructions.h"
0015 #include "llvm/IR/IntrinsicInst.h"
0016 #include "llvm/IR/Intrinsics.h"
0017 #include "llvm/IR/Module.h"
0018 
0019 namespace llvm {
0020 
0021 // We operate on opaque instruction classes, so forward declare all instruction
0022 // types now...
0023 //
0024 #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
0025 #include "llvm/IR/Instruction.def"
0026 
0027 #define DELEGATE(CLASS_TO_VISIT) \
0028   return static_cast<SubClass*>(this)-> \
0029                visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
0030 
0031 
0032 /// Base class for instruction visitors
0033 ///
0034 /// Instruction visitors are used when you want to perform different actions
0035 /// for different kinds of instructions without having to use lots of casts
0036 /// and a big switch statement (in your code, that is).
0037 ///
0038 /// To define your own visitor, inherit from this class, specifying your
0039 /// new type for the 'SubClass' template parameter, and "override" visitXXX
0040 /// functions in your class. I say "override" because this class is defined
0041 /// in terms of statically resolved overloading, not virtual functions.
0042 ///
0043 /// For example, here is a visitor that counts the number of malloc
0044 /// instructions processed:
0045 ///
0046 ///  /// Declare the class.  Note that we derive from InstVisitor instantiated
0047 ///  /// with _our new subclasses_ type.
0048 ///  ///
0049 ///  struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
0050 ///    unsigned Count;
0051 ///    CountAllocaVisitor() : Count(0) {}
0052 ///
0053 ///    void visitAllocaInst(AllocaInst &AI) { ++Count; }
0054 ///  };
0055 ///
0056 ///  And this class would be used like this:
0057 ///    CountAllocaVisitor CAV;
0058 ///    CAV.visit(function);
0059 ///    NumAllocas = CAV.Count;
0060 ///
0061 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
0062 /// Function, and Module, which recursively process all contained instructions.
0063 ///
0064 /// Note that if you don't implement visitXXX for some instruction type,
0065 /// the visitXXX method for instruction superclass will be invoked. So
0066 /// if instructions are added in the future, they will be automatically
0067 /// supported, if you handle one of their superclasses.
0068 ///
0069 /// The optional second template argument specifies the type that instruction
0070 /// visitation functions should return. If you specify this, you *MUST* provide
0071 /// an implementation of visitInstruction though!.
0072 ///
0073 /// Note that this class is specifically designed as a template to avoid
0074 /// virtual function call overhead.  Defining and using an InstVisitor is just
0075 /// as efficient as having your own switch statement over the instruction
0076 /// opcode.
0077 template<typename SubClass, typename RetTy=void>
0078 class InstVisitor {
0079   //===--------------------------------------------------------------------===//
0080   // Interface code - This is the public interface of the InstVisitor that you
0081   // use to visit instructions...
0082   //
0083 
0084 public:
0085   // Generic visit method - Allow visitation to all instructions in a range
0086   template<class Iterator>
0087   void visit(Iterator Start, Iterator End) {
0088     while (Start != End)
0089       static_cast<SubClass*>(this)->visit(*Start++);
0090   }
0091 
0092   // Define visitors for functions and basic blocks...
0093   //
0094   void visit(Module &M) {
0095     static_cast<SubClass*>(this)->visitModule(M);
0096     visit(M.begin(), M.end());
0097   }
0098   void visit(Function &F) {
0099     static_cast<SubClass*>(this)->visitFunction(F);
0100     visit(F.begin(), F.end());
0101   }
0102   void visit(BasicBlock &BB) {
0103     static_cast<SubClass*>(this)->visitBasicBlock(BB);
0104     visit(BB.begin(), BB.end());
0105   }
0106 
0107   // Forwarding functions so that the user can visit with pointers AND refs.
0108   void visit(Module       *M)  { visit(*M); }
0109   void visit(Function     *F)  { visit(*F); }
0110   void visit(BasicBlock   *BB) { visit(*BB); }
0111   RetTy visit(Instruction *I)  { return visit(*I); }
0112 
0113   // visit - Finally, code to visit an instruction...
0114   //
0115   RetTy visit(Instruction &I) {
0116     static_assert(std::is_base_of<InstVisitor, SubClass>::value,
0117                   "Must pass the derived type to this template!");
0118 
0119     switch (I.getOpcode()) {
0120     default: llvm_unreachable("Unknown instruction type encountered!");
0121       // Build the switch statement using the Instruction.def file...
0122 #define HANDLE_INST(NUM, OPCODE, CLASS) \
0123     case Instruction::OPCODE: return \
0124            static_cast<SubClass*>(this)-> \
0125                       visit##OPCODE(static_cast<CLASS&>(I));
0126 #include "llvm/IR/Instruction.def"
0127     }
0128   }
0129 
0130   //===--------------------------------------------------------------------===//
0131   // Visitation functions... these functions provide default fallbacks in case
0132   // the user does not specify what to do for a particular instruction type.
0133   // The default behavior is to generalize the instruction type to its subtype
0134   // and try visiting the subtype.  All of this should be inlined perfectly,
0135   // because there are no virtual functions to get in the way.
0136   //
0137 
0138   // When visiting a module, function or basic block directly, these methods get
0139   // called to indicate when transitioning into a new unit.
0140   //
0141   void visitModule    (Module &M) {}
0142   void visitFunction  (Function &F) {}
0143   void visitBasicBlock(BasicBlock &BB) {}
0144 
0145   // Define instruction specific visitor functions that can be overridden to
0146   // handle SPECIFIC instructions.  These functions automatically define
0147   // visitMul to proxy to visitBinaryOperator for instance in case the user does
0148   // not need this generality.
0149   //
0150   // These functions can also implement fan-out, when a single opcode and
0151   // instruction have multiple more specific Instruction subclasses. The Call
0152   // instruction currently supports this. We implement that by redirecting that
0153   // instruction to a special delegation helper.
0154 #define HANDLE_INST(NUM, OPCODE, CLASS) \
0155     RetTy visit##OPCODE(CLASS &I) { \
0156       if (NUM == Instruction::Call) \
0157         return delegateCallInst(I); \
0158       else \
0159         DELEGATE(CLASS); \
0160     }
0161 #include "llvm/IR/Instruction.def"
0162 
0163   // Specific Instruction type classes... note that all of the casts are
0164   // necessary because we use the instruction classes as opaque types...
0165   //
0166   RetTy visitICmpInst(ICmpInst &I)                { DELEGATE(CmpInst);}
0167   RetTy visitFCmpInst(FCmpInst &I)                { DELEGATE(CmpInst);}
0168   RetTy visitAllocaInst(AllocaInst &I)            { DELEGATE(UnaryInstruction);}
0169   RetTy visitLoadInst(LoadInst     &I)            { DELEGATE(UnaryInstruction);}
0170   RetTy visitStoreInst(StoreInst   &I)            { DELEGATE(Instruction);}
0171   RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { DELEGATE(Instruction);}
0172   RetTy visitAtomicRMWInst(AtomicRMWInst &I)      { DELEGATE(Instruction);}
0173   RetTy visitFenceInst(FenceInst   &I)            { DELEGATE(Instruction);}
0174   RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction);}
0175   RetTy visitPHINode(PHINode       &I)            { DELEGATE(Instruction);}
0176   RetTy visitTruncInst(TruncInst &I)              { DELEGATE(CastInst);}
0177   RetTy visitZExtInst(ZExtInst &I)                { DELEGATE(CastInst);}
0178   RetTy visitSExtInst(SExtInst &I)                { DELEGATE(CastInst);}
0179   RetTy visitFPTruncInst(FPTruncInst &I)          { DELEGATE(CastInst);}
0180   RetTy visitFPExtInst(FPExtInst &I)              { DELEGATE(CastInst);}
0181   RetTy visitFPToUIInst(FPToUIInst &I)            { DELEGATE(CastInst);}
0182   RetTy visitFPToSIInst(FPToSIInst &I)            { DELEGATE(CastInst);}
0183   RetTy visitUIToFPInst(UIToFPInst &I)            { DELEGATE(CastInst);}
0184   RetTy visitSIToFPInst(SIToFPInst &I)            { DELEGATE(CastInst);}
0185   RetTy visitPtrToIntInst(PtrToIntInst &I)        { DELEGATE(CastInst);}
0186   RetTy visitIntToPtrInst(IntToPtrInst &I)        { DELEGATE(CastInst);}
0187   RetTy visitBitCastInst(BitCastInst &I)          { DELEGATE(CastInst);}
0188   RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}
0189   RetTy visitSelectInst(SelectInst &I)            { DELEGATE(Instruction);}
0190   RetTy visitVAArgInst(VAArgInst   &I)            { DELEGATE(UnaryInstruction);}
0191   RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
0192   RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction);}
0193   RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction);}
0194   RetTy visitExtractValueInst(ExtractValueInst &I){ DELEGATE(UnaryInstruction);}
0195   RetTy visitInsertValueInst(InsertValueInst &I)  { DELEGATE(Instruction); }
0196   RetTy visitLandingPadInst(LandingPadInst &I)    { DELEGATE(Instruction); }
0197   RetTy visitFuncletPadInst(FuncletPadInst &I) { DELEGATE(Instruction); }
0198   RetTy visitCleanupPadInst(CleanupPadInst &I) { DELEGATE(FuncletPadInst); }
0199   RetTy visitCatchPadInst(CatchPadInst &I)     { DELEGATE(FuncletPadInst); }
0200   RetTy visitFreezeInst(FreezeInst &I)         { DELEGATE(Instruction); }
0201 
0202   // Handle the special intrinsic instruction classes.
0203   RetTy visitDbgDeclareInst(DbgDeclareInst &I)    { DELEGATE(DbgVariableIntrinsic);}
0204   RetTy visitDbgValueInst(DbgValueInst &I)        { DELEGATE(DbgVariableIntrinsic);}
0205   RetTy visitDbgVariableIntrinsic(DbgVariableIntrinsic &I)
0206                                                   { DELEGATE(DbgInfoIntrinsic);}
0207   RetTy visitDbgLabelInst(DbgLabelInst &I)        { DELEGATE(DbgInfoIntrinsic);}
0208   RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I){ DELEGATE(IntrinsicInst); }
0209   RetTy visitMemSetInst(MemSetInst &I)            { DELEGATE(MemIntrinsic); }
0210   RetTy visitMemSetInlineInst(MemSetInlineInst &I){ DELEGATE(MemSetInst); }
0211   RetTy visitMemSetPatternInst(MemSetPatternInst &I) {
0212     DELEGATE(IntrinsicInst);
0213   }
0214   RetTy visitMemCpyInst(MemCpyInst &I)            { DELEGATE(MemTransferInst); }
0215   RetTy visitMemCpyInlineInst(MemCpyInlineInst &I){ DELEGATE(MemCpyInst); }
0216   RetTy visitMemMoveInst(MemMoveInst &I)          { DELEGATE(MemTransferInst); }
0217   RetTy visitMemTransferInst(MemTransferInst &I)  { DELEGATE(MemIntrinsic); }
0218   RetTy visitMemIntrinsic(MemIntrinsic &I)        { DELEGATE(IntrinsicInst); }
0219   RetTy visitVAStartInst(VAStartInst &I)          { DELEGATE(IntrinsicInst); }
0220   RetTy visitVAEndInst(VAEndInst &I)              { DELEGATE(IntrinsicInst); }
0221   RetTy visitVACopyInst(VACopyInst &I)            { DELEGATE(IntrinsicInst); }
0222   RetTy visitIntrinsicInst(IntrinsicInst &I)      { DELEGATE(CallInst); }
0223   RetTy visitCallInst(CallInst &I)                { DELEGATE(CallBase); }
0224   RetTy visitInvokeInst(InvokeInst &I)            { DELEGATE(CallBase); }
0225   RetTy visitCallBrInst(CallBrInst &I)            { DELEGATE(CallBase); }
0226 
0227   // While terminators don't have a distinct type modeling them, we support
0228   // intercepting them with dedicated a visitor callback.
0229   RetTy visitReturnInst(ReturnInst &I) {
0230     return static_cast<SubClass *>(this)->visitTerminator(I);
0231   }
0232   RetTy visitBranchInst(BranchInst &I) {
0233     return static_cast<SubClass *>(this)->visitTerminator(I);
0234   }
0235   RetTy visitSwitchInst(SwitchInst &I) {
0236     return static_cast<SubClass *>(this)->visitTerminator(I);
0237   }
0238   RetTy visitIndirectBrInst(IndirectBrInst &I) {
0239     return static_cast<SubClass *>(this)->visitTerminator(I);
0240   }
0241   RetTy visitResumeInst(ResumeInst &I) {
0242     return static_cast<SubClass *>(this)->visitTerminator(I);
0243   }
0244   RetTy visitUnreachableInst(UnreachableInst &I) {
0245     return static_cast<SubClass *>(this)->visitTerminator(I);
0246   }
0247   RetTy visitCleanupReturnInst(CleanupReturnInst &I) {
0248     return static_cast<SubClass *>(this)->visitTerminator(I);
0249   }
0250   RetTy visitCatchReturnInst(CatchReturnInst &I) {
0251     return static_cast<SubClass *>(this)->visitTerminator(I);
0252   }
0253   RetTy visitCatchSwitchInst(CatchSwitchInst &I) {
0254     return static_cast<SubClass *>(this)->visitTerminator(I);
0255   }
0256   RetTy visitTerminator(Instruction &I)    { DELEGATE(Instruction);}
0257 
0258   // Next level propagators: If the user does not overload a specific
0259   // instruction type, they can overload one of these to get the whole class
0260   // of instructions...
0261   //
0262   RetTy visitCastInst(CastInst &I)                { DELEGATE(UnaryInstruction);}
0263   RetTy visitUnaryOperator(UnaryOperator &I)      { DELEGATE(UnaryInstruction);}
0264   RetTy visitBinaryOperator(BinaryOperator &I)    { DELEGATE(Instruction);}
0265   RetTy visitCmpInst(CmpInst &I)                  { DELEGATE(Instruction);}
0266   RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
0267 
0268   // The next level delegation for `CallBase` is slightly more complex in order
0269   // to support visiting cases where the call is also a terminator.
0270   RetTy visitCallBase(CallBase &I) {
0271     if (isa<InvokeInst>(I) || isa<CallBrInst>(I))
0272       return static_cast<SubClass *>(this)->visitTerminator(I);
0273 
0274     DELEGATE(Instruction);
0275   }
0276 
0277   // If the user wants a 'default' case, they can choose to override this
0278   // function.  If this function is not overloaded in the user's subclass, then
0279   // this instruction just gets ignored.
0280   //
0281   // Note that you MUST override this function if your return type is not void.
0282   //
0283   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
0284 
0285 private:
0286   // Special helper function to delegate to CallInst subclass visitors.
0287   RetTy delegateCallInst(CallInst &I) {
0288     if (const Function *F = I.getCalledFunction()) {
0289       switch (F->getIntrinsicID()) {
0290       default:                     DELEGATE(IntrinsicInst);
0291       case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst);
0292       case Intrinsic::dbg_value:   DELEGATE(DbgValueInst);
0293       case Intrinsic::dbg_label:   DELEGATE(DbgLabelInst);
0294       case Intrinsic::memcpy:      DELEGATE(MemCpyInst);
0295       case Intrinsic::memcpy_inline:
0296         DELEGATE(MemCpyInlineInst);
0297       case Intrinsic::memmove:     DELEGATE(MemMoveInst);
0298       case Intrinsic::memset:      DELEGATE(MemSetInst);
0299       case Intrinsic::memset_inline:
0300         DELEGATE(MemSetInlineInst);
0301       case Intrinsic::experimental_memset_pattern:
0302         DELEGATE(MemSetPatternInst);
0303       case Intrinsic::vastart:     DELEGATE(VAStartInst);
0304       case Intrinsic::vaend:       DELEGATE(VAEndInst);
0305       case Intrinsic::vacopy:      DELEGATE(VACopyInst);
0306       case Intrinsic::not_intrinsic: break;
0307       }
0308     }
0309     DELEGATE(CallInst);
0310   }
0311 
0312   // An overload that will never actually be called, it is used only from dead
0313   // code in the dispatching from opcodes to instruction subclasses.
0314   RetTy delegateCallInst(Instruction &I) {
0315     llvm_unreachable("delegateCallInst called for non-CallInst");
0316   }
0317 };
0318 
0319 #undef DELEGATE
0320 
0321 } // End llvm namespace
0322 
0323 #endif