• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/Support/InstVisitor.h - Define instruction visitors -*- 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_SUPPORT_INSTVISITOR_H
12 #define LLVM_SUPPORT_INSTVISITOR_H
13 
14 #include "llvm/Function.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/Module.h"
17 #include "llvm/Support/ErrorHandling.h"
18 
19 namespace llvm {
20 
21 // We operate on opaque instruction classes, so forward declare all instruction
22 // types now...
23 //
24 #define HANDLE_INST(NUM, OPCODE, CLASS)   class CLASS;
25 #include "llvm/Instruction.def"
26 
27 #define DELEGATE(CLASS_TO_VISIT) \
28   return static_cast<SubClass*>(this)-> \
29                visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
30 
31 
32 /// @brief Base class for instruction visitors
33 ///
34 /// Instruction visitors are used when you want to perform different actions
35 /// for different kinds of instructions without having to use lots of casts
36 /// and a big switch statement (in your code, that is).
37 ///
38 /// To define your own visitor, inherit from this class, specifying your
39 /// new type for the 'SubClass' template parameter, and "override" visitXXX
40 /// functions in your class. I say "override" because this class is defined
41 /// in terms of statically resolved overloading, not virtual functions.
42 ///
43 /// For example, here is a visitor that counts the number of malloc
44 /// instructions processed:
45 ///
46 ///  /// Declare the class.  Note that we derive from InstVisitor instantiated
47 ///  /// with _our new subclasses_ type.
48 ///  ///
49 ///  struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
50 ///    unsigned Count;
51 ///    CountAllocaVisitor() : Count(0) {}
52 ///
53 ///    void visitAllocaInst(AllocaInst &AI) { ++Count; }
54 ///  };
55 ///
56 ///  And this class would be used like this:
57 ///    CountAllocaVisitor CAV;
58 ///    CAV.visit(function);
59 ///    NumAllocas = CAV.Count;
60 ///
61 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
62 /// Function, and Module, which recursively process all contained instructions.
63 ///
64 /// Note that if you don't implement visitXXX for some instruction type,
65 /// the visitXXX method for instruction superclass will be invoked. So
66 /// if instructions are added in the future, they will be automatically
67 /// supported, if you handle one of their superclasses.
68 ///
69 /// The optional second template argument specifies the type that instruction
70 /// visitation functions should return. If you specify this, you *MUST* provide
71 /// an implementation of visitInstruction though!.
72 ///
73 /// Note that this class is specifically designed as a template to avoid
74 /// virtual function call overhead.  Defining and using an InstVisitor is just
75 /// as efficient as having your own switch statement over the instruction
76 /// opcode.
77 template<typename SubClass, typename RetTy=void>
78 class InstVisitor {
79   //===--------------------------------------------------------------------===//
80   // Interface code - This is the public interface of the InstVisitor that you
81   // use to visit instructions...
82   //
83 
84 public:
85   // Generic visit method - Allow visitation to all instructions in a range
86   template<class Iterator>
visit(Iterator Start,Iterator End)87   void visit(Iterator Start, Iterator End) {
88     while (Start != End)
89       static_cast<SubClass*>(this)->visit(*Start++);
90   }
91 
92   // Define visitors for functions and basic blocks...
93   //
visit(Module & M)94   void visit(Module &M) {
95     static_cast<SubClass*>(this)->visitModule(M);
96     visit(M.begin(), M.end());
97   }
visit(Function & F)98   void visit(Function &F) {
99     static_cast<SubClass*>(this)->visitFunction(F);
100     visit(F.begin(), F.end());
101   }
visit(BasicBlock & BB)102   void visit(BasicBlock &BB) {
103     static_cast<SubClass*>(this)->visitBasicBlock(BB);
104     visit(BB.begin(), BB.end());
105   }
106 
107   // Forwarding functions so that the user can visit with pointers AND refs.
visit(Module * M)108   void visit(Module       *M)  { visit(*M); }
visit(Function * F)109   void visit(Function     *F)  { visit(*F); }
visit(BasicBlock * BB)110   void visit(BasicBlock   *BB) { visit(*BB); }
visit(Instruction * I)111   RetTy visit(Instruction *I)  { return visit(*I); }
112 
113   // visit - Finally, code to visit an instruction...
114   //
visit(Instruction & I)115   RetTy visit(Instruction &I) {
116     switch (I.getOpcode()) {
117     default: llvm_unreachable("Unknown instruction type encountered!");
118       // Build the switch statement using the Instruction.def file...
119 #define HANDLE_INST(NUM, OPCODE, CLASS) \
120     case Instruction::OPCODE: return \
121            static_cast<SubClass*>(this)-> \
122                       visit##OPCODE(static_cast<CLASS&>(I));
123 #include "llvm/Instruction.def"
124     }
125   }
126 
127   //===--------------------------------------------------------------------===//
128   // Visitation functions... these functions provide default fallbacks in case
129   // the user does not specify what to do for a particular instruction type.
130   // The default behavior is to generalize the instruction type to its subtype
131   // and try visiting the subtype.  All of this should be inlined perfectly,
132   // because there are no virtual functions to get in the way.
133   //
134 
135   // When visiting a module, function or basic block directly, these methods get
136   // called to indicate when transitioning into a new unit.
137   //
visitModule(Module & M)138   void visitModule    (Module &M) {}
visitFunction(Function & F)139   void visitFunction  (Function &F) {}
visitBasicBlock(BasicBlock & BB)140   void visitBasicBlock(BasicBlock &BB) {}
141 
142   // Define instruction specific visitor functions that can be overridden to
143   // handle SPECIFIC instructions.  These functions automatically define
144   // visitMul to proxy to visitBinaryOperator for instance in case the user does
145   // not need this generality.
146   //
147   // The one problem case we have to handle here though is that the PHINode
148   // class and opcode name are the exact same.  Because of this, we cannot
149   // define visitPHINode (the inst version) to forward to visitPHINode (the
150   // generic version) without multiply defined symbols and recursion.  To handle
151   // this, we do not autoexpand "Other" instructions, we do it manually.
152   //
153 #define HANDLE_INST(NUM, OPCODE, CLASS) \
154     RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
155 #include "llvm/Instruction.def"
156 
157   // Specific Instruction type classes... note that all of the casts are
158   // necessary because we use the instruction classes as opaque types...
159   //
visitReturnInst(ReturnInst & I)160   RetTy visitReturnInst(ReturnInst &I)              { DELEGATE(TerminatorInst);}
visitBranchInst(BranchInst & I)161   RetTy visitBranchInst(BranchInst &I)              { DELEGATE(TerminatorInst);}
visitSwitchInst(SwitchInst & I)162   RetTy visitSwitchInst(SwitchInst &I)              { DELEGATE(TerminatorInst);}
visitIndirectBrInst(IndirectBrInst & I)163   RetTy visitIndirectBrInst(IndirectBrInst &I)      { DELEGATE(TerminatorInst);}
visitInvokeInst(InvokeInst & I)164   RetTy visitInvokeInst(InvokeInst &I)              { DELEGATE(TerminatorInst);}
visitUnwindInst(UnwindInst & I)165   RetTy visitUnwindInst(UnwindInst &I)              { DELEGATE(TerminatorInst);}
visitResumeInst(ResumeInst & I)166   RetTy visitResumeInst(ResumeInst &I)              { DELEGATE(TerminatorInst);}
visitUnreachableInst(UnreachableInst & I)167   RetTy visitUnreachableInst(UnreachableInst &I)    { DELEGATE(TerminatorInst);}
visitICmpInst(ICmpInst & I)168   RetTy visitICmpInst(ICmpInst &I)                  { DELEGATE(CmpInst);}
visitFCmpInst(FCmpInst & I)169   RetTy visitFCmpInst(FCmpInst &I)                  { DELEGATE(CmpInst);}
visitAllocaInst(AllocaInst & I)170   RetTy visitAllocaInst(AllocaInst &I)              { DELEGATE(Instruction); }
visitLoadInst(LoadInst & I)171   RetTy visitLoadInst(LoadInst     &I)              { DELEGATE(Instruction); }
visitStoreInst(StoreInst & I)172   RetTy visitStoreInst(StoreInst   &I)              { DELEGATE(Instruction); }
visitAtomicCmpXchgInst(AtomicCmpXchgInst & I)173   RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I){ DELEGATE(Instruction); }
visitAtomicRMWInst(AtomicRMWInst & I)174   RetTy visitAtomicRMWInst(AtomicRMWInst &I)        { DELEGATE(Instruction); }
visitFenceInst(FenceInst & I)175   RetTy visitFenceInst(FenceInst   &I)              { DELEGATE(Instruction); }
visitGetElementPtrInst(GetElementPtrInst & I)176   RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction); }
visitPHINode(PHINode & I)177   RetTy visitPHINode(PHINode       &I)              { DELEGATE(Instruction); }
visitTruncInst(TruncInst & I)178   RetTy visitTruncInst(TruncInst &I)                { DELEGATE(CastInst); }
visitZExtInst(ZExtInst & I)179   RetTy visitZExtInst(ZExtInst &I)                  { DELEGATE(CastInst); }
visitSExtInst(SExtInst & I)180   RetTy visitSExtInst(SExtInst &I)                  { DELEGATE(CastInst); }
visitFPTruncInst(FPTruncInst & I)181   RetTy visitFPTruncInst(FPTruncInst &I)            { DELEGATE(CastInst); }
visitFPExtInst(FPExtInst & I)182   RetTy visitFPExtInst(FPExtInst &I)                { DELEGATE(CastInst); }
visitFPToUIInst(FPToUIInst & I)183   RetTy visitFPToUIInst(FPToUIInst &I)              { DELEGATE(CastInst); }
visitFPToSIInst(FPToSIInst & I)184   RetTy visitFPToSIInst(FPToSIInst &I)              { DELEGATE(CastInst); }
visitUIToFPInst(UIToFPInst & I)185   RetTy visitUIToFPInst(UIToFPInst &I)              { DELEGATE(CastInst); }
visitSIToFPInst(SIToFPInst & I)186   RetTy visitSIToFPInst(SIToFPInst &I)              { DELEGATE(CastInst); }
visitPtrToIntInst(PtrToIntInst & I)187   RetTy visitPtrToIntInst(PtrToIntInst &I)          { DELEGATE(CastInst); }
visitIntToPtrInst(IntToPtrInst & I)188   RetTy visitIntToPtrInst(IntToPtrInst &I)          { DELEGATE(CastInst); }
visitBitCastInst(BitCastInst & I)189   RetTy visitBitCastInst(BitCastInst &I)            { DELEGATE(CastInst); }
visitSelectInst(SelectInst & I)190   RetTy visitSelectInst(SelectInst &I)              { DELEGATE(Instruction); }
visitCallInst(CallInst & I)191   RetTy visitCallInst(CallInst     &I)              { DELEGATE(Instruction); }
visitVAArgInst(VAArgInst & I)192   RetTy visitVAArgInst(VAArgInst   &I)              { DELEGATE(Instruction); }
visitExtractElementInst(ExtractElementInst & I)193   RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
visitInsertElementInst(InsertElementInst & I)194   RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction); }
visitShuffleVectorInst(ShuffleVectorInst & I)195   RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction); }
visitExtractValueInst(ExtractValueInst & I)196   RetTy visitExtractValueInst(ExtractValueInst &I)  { DELEGATE(Instruction);}
visitInsertValueInst(InsertValueInst & I)197   RetTy visitInsertValueInst(InsertValueInst &I)    { DELEGATE(Instruction); }
visitLandingPadInst(LandingPadInst & I)198   RetTy visitLandingPadInst(LandingPadInst &I)      { DELEGATE(Instruction); }
199 
200   // Next level propagators: If the user does not overload a specific
201   // instruction type, they can overload one of these to get the whole class
202   // of instructions...
203   //
visitTerminatorInst(TerminatorInst & I)204   RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); }
visitBinaryOperator(BinaryOperator & I)205   RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); }
visitCmpInst(CmpInst & I)206   RetTy visitCmpInst(CmpInst &I)               { DELEGATE(Instruction); }
visitCastInst(CastInst & I)207   RetTy visitCastInst(CastInst &I)             { DELEGATE(Instruction); }
208 
209   // If the user wants a 'default' case, they can choose to override this
210   // function.  If this function is not overloaded in the user's subclass, then
211   // this instruction just gets ignored.
212   //
213   // Note that you MUST override this function if your return type is not void.
214   //
visitInstruction(Instruction & I)215   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
216 };
217 
218 #undef DELEGATE
219 
220 } // End llvm namespace
221 
222 #endif
223