InstVisitor.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InstVisitor.h - Instruction visitor templates ------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_INSTVISITOR_H
  14. #define LLVM_IR_INSTVISITOR_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. namespace llvm {
  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/IR/Instruction.def"
  26. #define DELEGATE(CLASS_TO_VISIT) \
  27. return static_cast<SubClass*>(this)-> \
  28. visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
  29. /// Base class for instruction visitors
  30. ///
  31. /// Instruction visitors are used when you want to perform different actions
  32. /// for different kinds of instructions without having to use lots of casts
  33. /// and a big switch statement (in your code, that is).
  34. ///
  35. /// To define your own visitor, inherit from this class, specifying your
  36. /// new type for the 'SubClass' template parameter, and "override" visitXXX
  37. /// functions in your class. I say "override" because this class is defined
  38. /// in terms of statically resolved overloading, not virtual functions.
  39. ///
  40. /// For example, here is a visitor that counts the number of malloc
  41. /// instructions processed:
  42. ///
  43. /// /// Declare the class. Note that we derive from InstVisitor instantiated
  44. /// /// with _our new subclasses_ type.
  45. /// ///
  46. /// struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
  47. /// unsigned Count;
  48. /// CountAllocaVisitor() : Count(0) {}
  49. ///
  50. /// void visitAllocaInst(AllocaInst &AI) { ++Count; }
  51. /// };
  52. ///
  53. /// And this class would be used like this:
  54. /// CountAllocaVisitor CAV;
  55. /// CAV.visit(function);
  56. /// NumAllocas = CAV.Count;
  57. ///
  58. /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
  59. /// Function, and Module, which recursively process all contained instructions.
  60. ///
  61. /// Note that if you don't implement visitXXX for some instruction type,
  62. /// the visitXXX method for instruction superclass will be invoked. So
  63. /// if instructions are added in the future, they will be automatically
  64. /// supported, if you handle one of their superclasses.
  65. ///
  66. /// The optional second template argument specifies the type that instruction
  67. /// visitation functions should return. If you specify this, you *MUST* provide
  68. /// an implementation of visitInstruction though!.
  69. ///
  70. /// Note that this class is specifically designed as a template to avoid
  71. /// virtual function call overhead. Defining and using an InstVisitor is just
  72. /// as efficient as having your own switch statement over the instruction
  73. /// opcode.
  74. template<typename SubClass, typename RetTy=void>
  75. class InstVisitor {
  76. //===--------------------------------------------------------------------===//
  77. // Interface code - This is the public interface of the InstVisitor that you
  78. // use to visit instructions...
  79. //
  80. public:
  81. // Generic visit method - Allow visitation to all instructions in a range
  82. template<class Iterator>
  83. void visit(Iterator Start, Iterator End) {
  84. while (Start != End)
  85. static_cast<SubClass*>(this)->visit(*Start++);
  86. }
  87. // Define visitors for functions and basic blocks...
  88. //
  89. void visit(Module &M) {
  90. static_cast<SubClass*>(this)->visitModule(M);
  91. visit(M.begin(), M.end());
  92. }
  93. void visit(Function &F) {
  94. static_cast<SubClass*>(this)->visitFunction(F);
  95. visit(F.begin(), F.end());
  96. }
  97. void visit(BasicBlock &BB) {
  98. static_cast<SubClass*>(this)->visitBasicBlock(BB);
  99. visit(BB.begin(), BB.end());
  100. }
  101. // Forwarding functions so that the user can visit with pointers AND refs.
  102. void visit(Module *M) { visit(*M); }
  103. void visit(Function *F) { visit(*F); }
  104. void visit(BasicBlock *BB) { visit(*BB); }
  105. RetTy visit(Instruction *I) { return visit(*I); }
  106. // visit - Finally, code to visit an instruction...
  107. //
  108. RetTy visit(Instruction &I) {
  109. static_assert(std::is_base_of<InstVisitor, SubClass>::value,
  110. "Must pass the derived type to this template!");
  111. switch (I.getOpcode()) {
  112. default: llvm_unreachable("Unknown instruction type encountered!");
  113. // Build the switch statement using the Instruction.def file...
  114. #define HANDLE_INST(NUM, OPCODE, CLASS) \
  115. case Instruction::OPCODE: return \
  116. static_cast<SubClass*>(this)-> \
  117. visit##OPCODE(static_cast<CLASS&>(I));
  118. #include "llvm/IR/Instruction.def"
  119. }
  120. }
  121. //===--------------------------------------------------------------------===//
  122. // Visitation functions... these functions provide default fallbacks in case
  123. // the user does not specify what to do for a particular instruction type.
  124. // The default behavior is to generalize the instruction type to its subtype
  125. // and try visiting the subtype. All of this should be inlined perfectly,
  126. // because there are no virtual functions to get in the way.
  127. //
  128. // When visiting a module, function or basic block directly, these methods get
  129. // called to indicate when transitioning into a new unit.
  130. //
  131. void visitModule (Module &M) {}
  132. void visitFunction (Function &F) {}
  133. void visitBasicBlock(BasicBlock &BB) {}
  134. // Define instruction specific visitor functions that can be overridden to
  135. // handle SPECIFIC instructions. These functions automatically define
  136. // visitMul to proxy to visitBinaryOperator for instance in case the user does
  137. // not need this generality.
  138. //
  139. // These functions can also implement fan-out, when a single opcode and
  140. // instruction have multiple more specific Instruction subclasses. The Call
  141. // instruction currently supports this. We implement that by redirecting that
  142. // instruction to a special delegation helper.
  143. #define HANDLE_INST(NUM, OPCODE, CLASS) \
  144. RetTy visit##OPCODE(CLASS &I) { \
  145. if (NUM == Instruction::Call) \
  146. return delegateCallInst(I); \
  147. else \
  148. DELEGATE(CLASS); \
  149. }
  150. #include "llvm/IR/Instruction.def"
  151. // Specific Instruction type classes... note that all of the casts are
  152. // necessary because we use the instruction classes as opaque types...
  153. //
  154. RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);}
  155. RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);}
  156. RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(UnaryInstruction);}
  157. RetTy visitLoadInst(LoadInst &I) { DELEGATE(UnaryInstruction);}
  158. RetTy visitStoreInst(StoreInst &I) { DELEGATE(Instruction);}
  159. RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { DELEGATE(Instruction);}
  160. RetTy visitAtomicRMWInst(AtomicRMWInst &I) { DELEGATE(Instruction);}
  161. RetTy visitFenceInst(FenceInst &I) { DELEGATE(Instruction);}
  162. RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction);}
  163. RetTy visitPHINode(PHINode &I) { DELEGATE(Instruction);}
  164. RetTy visitTruncInst(TruncInst &I) { DELEGATE(CastInst);}
  165. RetTy visitZExtInst(ZExtInst &I) { DELEGATE(CastInst);}
  166. RetTy visitSExtInst(SExtInst &I) { DELEGATE(CastInst);}
  167. RetTy visitFPTruncInst(FPTruncInst &I) { DELEGATE(CastInst);}
  168. RetTy visitFPExtInst(FPExtInst &I) { DELEGATE(CastInst);}
  169. RetTy visitFPToUIInst(FPToUIInst &I) { DELEGATE(CastInst);}
  170. RetTy visitFPToSIInst(FPToSIInst &I) { DELEGATE(CastInst);}
  171. RetTy visitUIToFPInst(UIToFPInst &I) { DELEGATE(CastInst);}
  172. RetTy visitSIToFPInst(SIToFPInst &I) { DELEGATE(CastInst);}
  173. RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst);}
  174. RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst);}
  175. RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst);}
  176. RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}
  177. RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction);}
  178. RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(UnaryInstruction);}
  179. RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
  180. RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction);}
  181. RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction);}
  182. RetTy visitExtractValueInst(ExtractValueInst &I){ DELEGATE(UnaryInstruction);}
  183. RetTy visitInsertValueInst(InsertValueInst &I) { DELEGATE(Instruction); }
  184. RetTy visitLandingPadInst(LandingPadInst &I) { DELEGATE(Instruction); }
  185. RetTy visitFuncletPadInst(FuncletPadInst &I) { DELEGATE(Instruction); }
  186. RetTy visitCleanupPadInst(CleanupPadInst &I) { DELEGATE(FuncletPadInst); }
  187. RetTy visitCatchPadInst(CatchPadInst &I) { DELEGATE(FuncletPadInst); }
  188. RetTy visitFreezeInst(FreezeInst &I) { DELEGATE(Instruction); }
  189. // Handle the special intrinsic instruction classes.
  190. RetTy visitDbgDeclareInst(DbgDeclareInst &I) { DELEGATE(DbgVariableIntrinsic);}
  191. RetTy visitDbgValueInst(DbgValueInst &I) { DELEGATE(DbgVariableIntrinsic);}
  192. RetTy visitDbgVariableIntrinsic(DbgVariableIntrinsic &I)
  193. { DELEGATE(DbgInfoIntrinsic);}
  194. RetTy visitDbgLabelInst(DbgLabelInst &I) { DELEGATE(DbgInfoIntrinsic);}
  195. RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I){ DELEGATE(IntrinsicInst); }
  196. RetTy visitMemSetInst(MemSetInst &I) { DELEGATE(MemIntrinsic); }
  197. RetTy visitMemSetInlineInst(MemSetInlineInst &I){ DELEGATE(MemSetInst); }
  198. RetTy visitMemCpyInst(MemCpyInst &I) { DELEGATE(MemTransferInst); }
  199. RetTy visitMemCpyInlineInst(MemCpyInlineInst &I){ DELEGATE(MemCpyInst); }
  200. RetTy visitMemMoveInst(MemMoveInst &I) { DELEGATE(MemTransferInst); }
  201. RetTy visitMemTransferInst(MemTransferInst &I) { DELEGATE(MemIntrinsic); }
  202. RetTy visitMemIntrinsic(MemIntrinsic &I) { DELEGATE(IntrinsicInst); }
  203. RetTy visitVAStartInst(VAStartInst &I) { DELEGATE(IntrinsicInst); }
  204. RetTy visitVAEndInst(VAEndInst &I) { DELEGATE(IntrinsicInst); }
  205. RetTy visitVACopyInst(VACopyInst &I) { DELEGATE(IntrinsicInst); }
  206. RetTy visitIntrinsicInst(IntrinsicInst &I) { DELEGATE(CallInst); }
  207. RetTy visitCallInst(CallInst &I) { DELEGATE(CallBase); }
  208. RetTy visitInvokeInst(InvokeInst &I) { DELEGATE(CallBase); }
  209. RetTy visitCallBrInst(CallBrInst &I) { DELEGATE(CallBase); }
  210. // While terminators don't have a distinct type modeling them, we support
  211. // intercepting them with dedicated a visitor callback.
  212. RetTy visitReturnInst(ReturnInst &I) {
  213. return static_cast<SubClass *>(this)->visitTerminator(I);
  214. }
  215. RetTy visitBranchInst(BranchInst &I) {
  216. return static_cast<SubClass *>(this)->visitTerminator(I);
  217. }
  218. RetTy visitSwitchInst(SwitchInst &I) {
  219. return static_cast<SubClass *>(this)->visitTerminator(I);
  220. }
  221. RetTy visitIndirectBrInst(IndirectBrInst &I) {
  222. return static_cast<SubClass *>(this)->visitTerminator(I);
  223. }
  224. RetTy visitResumeInst(ResumeInst &I) {
  225. return static_cast<SubClass *>(this)->visitTerminator(I);
  226. }
  227. RetTy visitUnreachableInst(UnreachableInst &I) {
  228. return static_cast<SubClass *>(this)->visitTerminator(I);
  229. }
  230. RetTy visitCleanupReturnInst(CleanupReturnInst &I) {
  231. return static_cast<SubClass *>(this)->visitTerminator(I);
  232. }
  233. RetTy visitCatchReturnInst(CatchReturnInst &I) {
  234. return static_cast<SubClass *>(this)->visitTerminator(I);
  235. }
  236. RetTy visitCatchSwitchInst(CatchSwitchInst &I) {
  237. return static_cast<SubClass *>(this)->visitTerminator(I);
  238. }
  239. RetTy visitTerminator(Instruction &I) { DELEGATE(Instruction);}
  240. // Next level propagators: If the user does not overload a specific
  241. // instruction type, they can overload one of these to get the whole class
  242. // of instructions...
  243. //
  244. RetTy visitCastInst(CastInst &I) { DELEGATE(UnaryInstruction);}
  245. RetTy visitUnaryOperator(UnaryOperator &I) { DELEGATE(UnaryInstruction);}
  246. RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction);}
  247. RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction);}
  248. RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
  249. // The next level delegation for `CallBase` is slightly more complex in order
  250. // to support visiting cases where the call is also a terminator.
  251. RetTy visitCallBase(CallBase &I) {
  252. if (isa<InvokeInst>(I) || isa<CallBrInst>(I))
  253. return static_cast<SubClass *>(this)->visitTerminator(I);
  254. DELEGATE(Instruction);
  255. }
  256. // If the user wants a 'default' case, they can choose to override this
  257. // function. If this function is not overloaded in the user's subclass, then
  258. // this instruction just gets ignored.
  259. //
  260. // Note that you MUST override this function if your return type is not void.
  261. //
  262. void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
  263. private:
  264. // Special helper function to delegate to CallInst subclass visitors.
  265. RetTy delegateCallInst(CallInst &I) {
  266. if (const Function *F = I.getCalledFunction()) {
  267. switch (F->getIntrinsicID()) {
  268. default: DELEGATE(IntrinsicInst);
  269. case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst);
  270. case Intrinsic::dbg_value: DELEGATE(DbgValueInst);
  271. case Intrinsic::dbg_label: DELEGATE(DbgLabelInst);
  272. case Intrinsic::memcpy: DELEGATE(MemCpyInst);
  273. case Intrinsic::memcpy_inline:
  274. DELEGATE(MemCpyInlineInst);
  275. case Intrinsic::memmove: DELEGATE(MemMoveInst);
  276. case Intrinsic::memset: DELEGATE(MemSetInst);
  277. case Intrinsic::memset_inline:
  278. DELEGATE(MemSetInlineInst);
  279. case Intrinsic::vastart: DELEGATE(VAStartInst);
  280. case Intrinsic::vaend: DELEGATE(VAEndInst);
  281. case Intrinsic::vacopy: DELEGATE(VACopyInst);
  282. case Intrinsic::not_intrinsic: break;
  283. }
  284. }
  285. DELEGATE(CallInst);
  286. }
  287. // An overload that will never actually be called, it is used only from dead
  288. // code in the dispatching from opcodes to instruction subclasses.
  289. RetTy delegateCallInst(Instruction &I) {
  290. llvm_unreachable("delegateCallInst called for non-CallInst");
  291. }
  292. };
  293. #undef DELEGATE
  294. } // End llvm namespace
  295. #endif
  296. #ifdef __GNUC__
  297. #pragma GCC diagnostic pop
  298. #endif