Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
13 KiB

  1. //===- llvm/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_INSTVISITOR_H
  10. #define LLVM_INSTVISITOR_H
  11. #include "llvm/IR/Function.h"
  12. #include "llvm/IR/Instructions.h"
  13. #include "llvm/IR/IntrinsicInst.h"
  14. #include "llvm/IR/Intrinsics.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/Support/CallSite.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 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. // Handle the special instrinsic instruction classes.
  187. RetTy visitDbgDeclareInst(DbgDeclareInst &I) { DELEGATE(DbgInfoIntrinsic);}
  188. RetTy visitDbgValueInst(DbgValueInst &I) { DELEGATE(DbgInfoIntrinsic);}
  189. RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I) { DELEGATE(IntrinsicInst); }
  190. RetTy visitMemSetInst(MemSetInst &I) { DELEGATE(MemIntrinsic); }
  191. RetTy visitMemCpyInst(MemCpyInst &I) { DELEGATE(MemTransferInst); }
  192. RetTy visitMemMoveInst(MemMoveInst &I) { DELEGATE(MemTransferInst); }
  193. RetTy visitMemTransferInst(MemTransferInst &I) { DELEGATE(MemIntrinsic); }
  194. RetTy visitMemIntrinsic(MemIntrinsic &I) { DELEGATE(IntrinsicInst); }
  195. RetTy visitVAStartInst(VAStartInst &I) { DELEGATE(IntrinsicInst); }
  196. RetTy visitVAEndInst(VAEndInst &I) { DELEGATE(IntrinsicInst); }
  197. RetTy visitVACopyInst(VACopyInst &I) { DELEGATE(IntrinsicInst); }
  198. RetTy visitIntrinsicInst(IntrinsicInst &I) { DELEGATE(CallInst); }
  199. // Call and Invoke are slightly different as they delegate first through
  200. // a generic CallSite visitor.
  201. RetTy visitCallInst(CallInst &I) {
  202. return static_cast<SubClass*>(this)->visitCallSite(&I);
  203. }
  204. RetTy visitInvokeInst(InvokeInst &I) {
  205. return static_cast<SubClass*>(this)->visitCallSite(&I);
  206. }
  207. // Next level propagators: If the user does not overload a specific
  208. // instruction type, they can overload one of these to get the whole class
  209. // of instructions...
  210. //
  211. RetTy visitCastInst(CastInst &I) { DELEGATE(UnaryInstruction);}
  212. RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction);}
  213. RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction);}
  214. RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction);}
  215. RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
  216. // Provide a special visitor for a 'callsite' that visits both calls and
  217. // invokes. When unimplemented, properly delegates to either the terminator or
  218. // regular instruction visitor.
  219. RetTy visitCallSite(CallSite CS) {
  220. assert(CS);
  221. Instruction &I = *CS.getInstruction();
  222. if (CS.isCall())
  223. DELEGATE(Instruction);
  224. assert(CS.isInvoke());
  225. DELEGATE(TerminatorInst);
  226. }
  227. // If the user wants a 'default' case, they can choose to override this
  228. // function. If this function is not overloaded in the user's subclass, then
  229. // this instruction just gets ignored.
  230. //
  231. // Note that you MUST override this function if your return type is not void.
  232. //
  233. void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
  234. private:
  235. // Special helper function to delegate to CallInst subclass visitors.
  236. RetTy delegateCallInst(CallInst &I) {
  237. if (const Function *F = I.getCalledFunction()) {
  238. switch ((Intrinsic::ID)F->getIntrinsicID()) {
  239. default: DELEGATE(IntrinsicInst);
  240. case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst);
  241. case Intrinsic::dbg_value: DELEGATE(DbgValueInst);
  242. case Intrinsic::memcpy: DELEGATE(MemCpyInst);
  243. case Intrinsic::memmove: DELEGATE(MemMoveInst);
  244. case Intrinsic::memset: DELEGATE(MemSetInst);
  245. case Intrinsic::vastart: DELEGATE(VAStartInst);
  246. case Intrinsic::vaend: DELEGATE(VAEndInst);
  247. case Intrinsic::vacopy: DELEGATE(VACopyInst);
  248. case Intrinsic::not_intrinsic: break;
  249. }
  250. }
  251. DELEGATE(CallInst);
  252. }
  253. // An overload that will never actually be called, it is used only from dead
  254. // code in the dispatching from opcodes to instruction subclasses.
  255. RetTy delegateCallInst(Instruction &I) {
  256. llvm_unreachable("delegateCallInst called for non-CallInst");
  257. }
  258. };
  259. #undef DELEGATE
  260. } // End llvm namespace
  261. #endif