InstVisitor.h 14 KB

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