InstVisitor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #ifndef LLVM_IR_INSTVISITOR_H
  10. #define LLVM_IR_INSTVISITOR_H
  11. #include "llvm/IR/CallSite.h"
  12. #include "llvm/IR/Function.h"
  13. #include "llvm/IR/Instructions.h"
  14. #include "llvm/IR/IntrinsicInst.h"
  15. #include "llvm/IR/Intrinsics.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. namespace llvm {
  19. // We operate on opaque instruction classes, so forward declare all instruction
  20. // types now...
  21. //
  22. #define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS;
  23. #include "llvm/IR/Instruction.def"
  24. #define DELEGATE(CLASS_TO_VISIT) \
  25. return static_cast<SubClass*>(this)-> \
  26. visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
  27. /// @brief Base class for instruction visitors
  28. ///
  29. /// Instruction visitors are used when you want to perform different actions
  30. /// for different kinds of instructions without having to use lots of casts
  31. /// and a big switch statement (in your code, that is).
  32. ///
  33. /// To define your own visitor, inherit from this class, specifying your
  34. /// new type for the 'SubClass' template parameter, and "override" visitXXX
  35. /// functions in your class. I say "override" because this class is defined
  36. /// in terms of statically resolved overloading, not virtual functions.
  37. ///
  38. /// For example, here is a visitor that counts the number of malloc
  39. /// instructions processed:
  40. ///
  41. /// /// Declare the class. Note that we derive from InstVisitor instantiated
  42. /// /// with _our new subclasses_ type.
  43. /// ///
  44. /// struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
  45. /// unsigned Count;
  46. /// CountAllocaVisitor() : Count(0) {}
  47. ///
  48. /// void visitAllocaInst(AllocaInst &AI) { ++Count; }
  49. /// };
  50. ///
  51. /// And this class would be used like this:
  52. /// CountAllocaVisitor CAV;
  53. /// CAV.visit(function);
  54. /// NumAllocas = CAV.Count;
  55. ///
  56. /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
  57. /// Function, and Module, which recursively process all contained instructions.
  58. ///
  59. /// Note that if you don't implement visitXXX for some instruction type,
  60. /// the visitXXX method for instruction superclass will be invoked. So
  61. /// if instructions are added in the future, they will be automatically
  62. /// supported, if you handle one of their superclasses.
  63. ///
  64. /// The optional second template argument specifies the type that instruction
  65. /// visitation functions should return. If you specify this, you *MUST* provide
  66. /// an implementation of visitInstruction though!.
  67. ///
  68. /// Note that this class is specifically designed as a template to avoid
  69. /// virtual function call overhead. Defining and using an InstVisitor is just
  70. /// as efficient as having your own switch statement over the instruction
  71. /// opcode.
  72. template<typename SubClass, typename RetTy=void>
  73. class InstVisitor {
  74. //===--------------------------------------------------------------------===//
  75. // Interface code - This is the public interface of the InstVisitor that you
  76. // use to visit instructions...
  77. //
  78. public:
  79. // Generic visit method - Allow visitation to all instructions in a range
  80. template<class Iterator>
  81. void visit(Iterator Start, Iterator End) {
  82. while (Start != End)
  83. static_cast<SubClass*>(this)->visit(*Start++);
  84. }
  85. // Define visitors for functions and basic blocks...
  86. //
  87. void visit(Module &M) {
  88. static_cast<SubClass*>(this)->visitModule(M);
  89. visit(M.begin(), M.end());
  90. }
  91. void visit(Function &F) {
  92. static_cast<SubClass*>(this)->visitFunction(F);
  93. visit(F.begin(), F.end());
  94. }
  95. void visit(BasicBlock &BB) {
  96. static_cast<SubClass*>(this)->visitBasicBlock(BB);
  97. visit(BB.begin(), BB.end());
  98. }
  99. // Forwarding functions so that the user can visit with pointers AND refs.
  100. void visit(Module *M) { visit(*M); }
  101. void visit(Function *F) { visit(*F); }
  102. void visit(BasicBlock *BB) { visit(*BB); }
  103. RetTy visit(Instruction *I) { return visit(*I); }
  104. // visit - Finally, code to visit an instruction...
  105. //
  106. RetTy visit(Instruction &I) {
  107. switch (I.getOpcode()) {
  108. default: llvm_unreachable("Unknown instruction type encountered!");
  109. // Build the switch statement using the Instruction.def file...
  110. #define HANDLE_INST(NUM, OPCODE, CLASS) \
  111. case Instruction::OPCODE: return \
  112. static_cast<SubClass*>(this)-> \
  113. visit##OPCODE(static_cast<CLASS&>(I));
  114. #include "llvm/IR/Instruction.def"
  115. }
  116. }
  117. //===--------------------------------------------------------------------===//
  118. // Visitation functions... these functions provide default fallbacks in case
  119. // the user does not specify what to do for a particular instruction type.
  120. // The default behavior is to generalize the instruction type to its subtype
  121. // and try visiting the subtype. All of this should be inlined perfectly,
  122. // because there are no virtual functions to get in the way.
  123. //
  124. // When visiting a module, function or basic block directly, these methods get
  125. // called to indicate when transitioning into a new unit.
  126. //
  127. void visitModule (Module &M) {}
  128. void visitFunction (Function &F) {}
  129. void visitBasicBlock(BasicBlock &BB) {}
  130. // Define instruction specific visitor functions that can be overridden to
  131. // handle SPECIFIC instructions. These functions automatically define
  132. // visitMul to proxy to visitBinaryOperator for instance in case the user does
  133. // not need this generality.
  134. //
  135. // These functions can also implement fan-out, when a single opcode and
  136. // instruction have multiple more specific Instruction subclasses. The Call
  137. // instruction currently supports this. We implement that by redirecting that
  138. // instruction to a special delegation helper.
  139. #define HANDLE_INST(NUM, OPCODE, CLASS) \
  140. RetTy visit##OPCODE(CLASS &I) { \
  141. if (NUM == Instruction::Call) \
  142. return delegateCallInst(I); \
  143. else \
  144. DELEGATE(CLASS); \
  145. }
  146. #include "llvm/IR/Instruction.def"
  147. // Specific Instruction type classes... note that all of the casts are
  148. // necessary because we use the instruction classes as opaque types...
  149. //
  150. RetTy visitReturnInst(ReturnInst &I) { DELEGATE(TerminatorInst);}
  151. RetTy visitBranchInst(BranchInst &I) { DELEGATE(TerminatorInst);}
  152. RetTy visitSwitchInst(SwitchInst &I) { DELEGATE(TerminatorInst);}
  153. RetTy visitIndirectBrInst(IndirectBrInst &I) { DELEGATE(TerminatorInst);}
  154. RetTy visitResumeInst(ResumeInst &I) { DELEGATE(TerminatorInst);}
  155. RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);}
  156. RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);}
  157. RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);}
  158. RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(UnaryInstruction);}
  159. RetTy visitLoadInst(LoadInst &I) { DELEGATE(UnaryInstruction);}
  160. RetTy visitStoreInst(StoreInst &I) { DELEGATE(Instruction);}
  161. RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { DELEGATE(Instruction);}
  162. RetTy visitAtomicRMWInst(AtomicRMWInst &I) { DELEGATE(Instruction);}
  163. RetTy visitFenceInst(FenceInst &I) { DELEGATE(Instruction);}
  164. RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction);}
  165. RetTy visitPHINode(PHINode &I) { DELEGATE(Instruction);}
  166. RetTy visitTruncInst(TruncInst &I) { DELEGATE(CastInst);}
  167. RetTy visitZExtInst(ZExtInst &I) { DELEGATE(CastInst);}
  168. RetTy visitSExtInst(SExtInst &I) { DELEGATE(CastInst);}
  169. RetTy visitFPTruncInst(FPTruncInst &I) { DELEGATE(CastInst);}
  170. RetTy visitFPExtInst(FPExtInst &I) { DELEGATE(CastInst);}
  171. RetTy visitFPToUIInst(FPToUIInst &I) { DELEGATE(CastInst);}
  172. RetTy visitFPToSIInst(FPToSIInst &I) { DELEGATE(CastInst);}
  173. RetTy visitUIToFPInst(UIToFPInst &I) { DELEGATE(CastInst);}
  174. RetTy visitSIToFPInst(SIToFPInst &I) { DELEGATE(CastInst);}
  175. RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst);}
  176. RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst);}
  177. RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst);}
  178. RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}
  179. RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction);}
  180. RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(UnaryInstruction);}
  181. RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
  182. RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction);}
  183. RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction);}
  184. RetTy visitExtractValueInst(ExtractValueInst &I){ DELEGATE(UnaryInstruction);}
  185. RetTy visitInsertValueInst(InsertValueInst &I) { DELEGATE(Instruction); }
  186. RetTy visitLandingPadInst(LandingPadInst &I) { DELEGATE(Instruction); }
  187. // Handle the special instrinsic instruction classes.
  188. RetTy visitDbgDeclareInst(DbgDeclareInst &I) { DELEGATE(DbgInfoIntrinsic);}
  189. RetTy visitDbgValueInst(DbgValueInst &I) { DELEGATE(DbgInfoIntrinsic);}
  190. RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I) { DELEGATE(IntrinsicInst); }
  191. RetTy visitMemSetInst(MemSetInst &I) { DELEGATE(MemIntrinsic); }
  192. RetTy visitMemCpyInst(MemCpyInst &I) { DELEGATE(MemTransferInst); }
  193. RetTy visitMemMoveInst(MemMoveInst &I) { DELEGATE(MemTransferInst); }
  194. RetTy visitMemTransferInst(MemTransferInst &I) { DELEGATE(MemIntrinsic); }
  195. RetTy visitMemIntrinsic(MemIntrinsic &I) { DELEGATE(IntrinsicInst); }
  196. RetTy visitVAStartInst(VAStartInst &I) { DELEGATE(IntrinsicInst); }
  197. RetTy visitVAEndInst(VAEndInst &I) { DELEGATE(IntrinsicInst); }
  198. RetTy visitVACopyInst(VACopyInst &I) { DELEGATE(IntrinsicInst); }
  199. RetTy visitIntrinsicInst(IntrinsicInst &I) { DELEGATE(CallInst); }
  200. // Call and Invoke are slightly different as they delegate first through
  201. // a generic CallSite visitor.
  202. RetTy visitCallInst(CallInst &I) {
  203. return static_cast<SubClass*>(this)->visitCallSite(&I);
  204. }
  205. RetTy visitInvokeInst(InvokeInst &I) {
  206. return static_cast<SubClass*>(this)->visitCallSite(&I);
  207. }
  208. // Next level propagators: If the user does not overload a specific
  209. // instruction type, they can overload one of these to get the whole class
  210. // of instructions...
  211. //
  212. RetTy visitCastInst(CastInst &I) { DELEGATE(UnaryInstruction);}
  213. RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction);}
  214. RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction);}
  215. RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction);}
  216. RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
  217. // Provide a special visitor for a 'callsite' that visits both calls and
  218. // invokes. When unimplemented, properly delegates to either the terminator or
  219. // regular instruction visitor.
  220. RetTy visitCallSite(CallSite CS) {
  221. assert(CS);
  222. Instruction &I = *CS.getInstruction();
  223. if (CS.isCall())
  224. DELEGATE(Instruction);
  225. assert(CS.isInvoke());
  226. DELEGATE(TerminatorInst);
  227. }
  228. // If the user wants a 'default' case, they can choose to override this
  229. // function. If this function is not overloaded in the user's subclass, then
  230. // this instruction just gets ignored.
  231. //
  232. // Note that you MUST override this function if your return type is not void.
  233. //
  234. void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
  235. private:
  236. // Special helper function to delegate to CallInst subclass visitors.
  237. RetTy delegateCallInst(CallInst &I) {
  238. if (const Function *F = I.getCalledFunction()) {
  239. switch (F->getIntrinsicID()) {
  240. default: DELEGATE(IntrinsicInst);
  241. case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst);
  242. case Intrinsic::dbg_value: DELEGATE(DbgValueInst);
  243. case Intrinsic::memcpy: DELEGATE(MemCpyInst);
  244. case Intrinsic::memmove: DELEGATE(MemMoveInst);
  245. case Intrinsic::memset: DELEGATE(MemSetInst);
  246. case Intrinsic::vastart: DELEGATE(VAStartInst);
  247. case Intrinsic::vaend: DELEGATE(VAEndInst);
  248. case Intrinsic::vacopy: DELEGATE(VACopyInst);
  249. case Intrinsic::not_intrinsic: break;
  250. }
  251. }
  252. DELEGATE(CallInst);
  253. }
  254. // An overload that will never actually be called, it is used only from dead
  255. // code in the dispatching from opcodes to instruction subclasses.
  256. RetTy delegateCallInst(Instruction &I) {
  257. llvm_unreachable("delegateCallInst called for non-CallInst");
  258. }
  259. };
  260. #undef DELEGATE
  261. } // End llvm namespace
  262. #endif