LLVM  8.0.0svn
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/CallSite.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"
22 namespace llvm {
24 // We operate on opaque instruction classes, so forward declare all instruction
25 // types now...
26 //
28 #include "llvm/IR/Instruction.def"
31  return static_cast<SubClass*>(this)-> \
32  visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
35 /// Base class for instruction visitors
36 ///
37 /// Instruction visitors are used when you want to perform different actions
38 /// for different kinds of instructions without having to use lots of casts
39 /// and a big switch statement (in your code, that is).
40 ///
41 /// To define your own visitor, inherit from this class, specifying your
42 /// new type for the 'SubClass' template parameter, and "override" visitXXX
43 /// functions in your class. I say "override" because this class is defined
44 /// in terms of statically resolved overloading, not virtual functions.
45 ///
46 /// For example, here is a visitor that counts the number of malloc
47 /// instructions processed:
48 ///
49 /// /// Declare the class. Note that we derive from InstVisitor instantiated
50 /// /// with _our new subclasses_ type.
51 /// ///
52 /// struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
53 /// unsigned Count;
54 /// CountAllocaVisitor() : Count(0) {}
55 ///
56 /// void visitAllocaInst(AllocaInst &AI) { ++Count; }
57 /// };
58 ///
59 /// And this class would be used like this:
60 /// CountAllocaVisitor CAV;
61 /// CAV.visit(function);
62 /// NumAllocas = CAV.Count;
63 ///
64 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
65 /// Function, and Module, which recursively process all contained instructions.
66 ///
67 /// Note that if you don't implement visitXXX for some instruction type,
68 /// the visitXXX method for instruction superclass will be invoked. So
69 /// if instructions are added in the future, they will be automatically
70 /// supported, if you handle one of their superclasses.
71 ///
72 /// The optional second template argument specifies the type that instruction
73 /// visitation functions should return. If you specify this, you *MUST* provide
74 /// an implementation of visitInstruction though!.
75 ///
76 /// Note that this class is specifically designed as a template to avoid
77 /// virtual function call overhead. Defining and using an InstVisitor is just
78 /// as efficient as having your own switch statement over the instruction
79 /// opcode.
80 template<typename SubClass, typename RetTy=void>
81 class InstVisitor {
82  //===--------------------------------------------------------------------===//
83  // Interface code - This is the public interface of the InstVisitor that you
84  // use to visit instructions...
85  //
87 public:
88  // Generic visit method - Allow visitation to all instructions in a range
89  template<class Iterator>
90  void visit(Iterator Start, Iterator End) {
91  while (Start != End)
92  static_cast<SubClass*>(this)->visit(*Start++);
93  }
95  // Define visitors for functions and basic blocks...
96  //
97  void visit(Module &M) {
98  static_cast<SubClass*>(this)->visitModule(M);
99  visit(M.begin(), M.end());
100  }
101  void visit(Function &F) {
102  static_cast<SubClass*>(this)->visitFunction(F);
103  visit(F.begin(), F.end());
104  }
105  void visit(BasicBlock &BB) {
106  static_cast<SubClass*>(this)->visitBasicBlock(BB);
107  visit(BB.begin(), BB.end());
108  }
110  // Forwarding functions so that the user can visit with pointers AND refs.
111  void visit(Module *M) { visit(*M); }
112  void visit(Function *F) { visit(*F); }
113  void visit(BasicBlock *BB) { visit(*BB); }
114  RetTy visit(Instruction *I) { return visit(*I); }
116  // visit - Finally, code to visit an instruction...
117  //
118  RetTy visit(Instruction &I) {
119  static_assert(std::is_base_of<InstVisitor, SubClass>::value,
120  "Must pass the derived type to this template!");
122  switch (I.getOpcode()) {
123  default: llvm_unreachable("Unknown instruction type encountered!");
124  // Build the switch statement using the Instruction.def file...
126  case Instruction::OPCODE: return \
127  static_cast<SubClass*>(this)-> \
128  visit##OPCODE(static_cast<CLASS&>(I));
129 #include "llvm/IR/Instruction.def"
130  }
131  }
133  //===--------------------------------------------------------------------===//
134  // Visitation functions... these functions provide default fallbacks in case
135  // the user does not specify what to do for a particular instruction type.
136  // The default behavior is to generalize the instruction type to its subtype
137  // and try visiting the subtype. All of this should be inlined perfectly,
138  // because there are no virtual functions to get in the way.
139  //
141  // When visiting a module, function or basic block directly, these methods get
142  // called to indicate when transitioning into a new unit.
143  //
144  void visitModule (Module &M) {}
148  // Define instruction specific visitor functions that can be overridden to
149  // handle SPECIFIC instructions. These functions automatically define
150  // visitMul to proxy to visitBinaryOperator for instance in case the user does
151  // not need this generality.
152  //
153  // These functions can also implement fan-out, when a single opcode and
154  // instruction have multiple more specific Instruction subclasses. The Call
155  // instruction currently supports this. We implement that by redirecting that
156  // instruction to a special delegation helper.
158  RetTy visit##OPCODE(CLASS &I) { \
159  if (NUM == Instruction::Call) \
160  return delegateCallInst(I); \
161  else \
163  }
164 #include "llvm/IR/Instruction.def"
166  // Specific Instruction type classes... note that all of the casts are
167  // necessary because we use the instruction classes as opaque types...
168  //
204  // Handle the special instrinsic instruction classes.
221  // Call and Invoke are slightly different as they delegate first through
222  // a generic CallSite visitor.
224  return static_cast<SubClass*>(this)->visitCallSite(&I);
225  }
227  return static_cast<SubClass*>(this)->visitCallSite(&I);
228  }
230  // While terminators don't have a distinct type modeling them, we support
231  // intercepting them with dedicated a visitor callback.
233  return static_cast<SubClass *>(this)->visitTerminator(I);
234  }
236  return static_cast<SubClass *>(this)->visitTerminator(I);
237  }
239  return static_cast<SubClass *>(this)->visitTerminator(I);
240  }
242  return static_cast<SubClass *>(this)->visitTerminator(I);
243  }
245  return static_cast<SubClass *>(this)->visitTerminator(I);
246  }
248  return static_cast<SubClass *>(this)->visitTerminator(I);
249  }
251  return static_cast<SubClass *>(this)->visitTerminator(I);
252  }
254  return static_cast<SubClass *>(this)->visitTerminator(I);
255  }
257  return static_cast<SubClass *>(this)->visitTerminator(I);
258  }
261  // Next level propagators: If the user does not overload a specific
262  // instruction type, they can overload one of these to get the whole class
263  // of instructions...
264  //
271  // Provide a special visitor for a 'callsite' that visits both calls and
272  // invokes. When unimplemented, properly delegates to either the terminator or
273  // regular instruction visitor.
275  assert(CS);
276  Instruction &I = *CS.getInstruction();
277  if (CS.isCall())
280  assert(CS.isInvoke());
281  return static_cast<SubClass *>(this)->visitTerminator(I);
282  }
284  // If the user wants a 'default' case, they can choose to override this
285  // function. If this function is not overloaded in the user's subclass, then
286  // this instruction just gets ignored.
287  //
288  // Note that you MUST override this function if your return type is not void.
289  //
290  void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
292 private:
293  // Special helper function to delegate to CallInst subclass visitors.
294  RetTy delegateCallInst(CallInst &I) {
295  if (const Function *F = I.getCalledFunction()) {
296  switch (F->getIntrinsicID()) {
297  default: DELEGATE(IntrinsicInst);
298  case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst);
299  case Intrinsic::dbg_value: DELEGATE(DbgValueInst);
300  case Intrinsic::dbg_label: DELEGATE(DbgLabelInst);
301  case Intrinsic::memcpy: DELEGATE(MemCpyInst);
302  case Intrinsic::memmove: DELEGATE(MemMoveInst);
303  case Intrinsic::memset: DELEGATE(MemSetInst);
304  case Intrinsic::vastart: DELEGATE(VAStartInst);
305  case Intrinsic::vaend: DELEGATE(VAEndInst);
306  case Intrinsic::vacopy: DELEGATE(VACopyInst);
307  case Intrinsic::not_intrinsic: break;
308  }
309  }
311  }
313  // An overload that will never actually be called, it is used only from dead
314  // code in the dispatching from opcodes to instruction subclasses.
315  RetTy delegateCallInst(Instruction &I) {
316  llvm_unreachable("delegateCallInst called for non-CallInst");
317  }
318 };
320 #undef DELEGATE
322 } // End llvm namespace
324 #endif
RetTy visitSelectInst(SelectInst &I)
Definition: InstVisitor.h:192
RetTy visitAtomicRMWInst(AtomicRMWInst &I)
Definition: InstVisitor.h:175
Return a value (possibly void), from a function.
RetTy visitUnaryOperator(UnaryOperator &I)
Definition: InstVisitor.h:266
RetTy visitVAStartInst(VAStartInst &I)
Definition: InstVisitor.h:216
RetTy visitMemSetInst(MemSetInst &I)
Definition: InstVisitor.h:211
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:632
This instruction extracts a struct member or array element value from an aggregate value...
RetTy visitCallSite(CallSite CS)
Definition: InstVisitor.h:274
Base class for instruction visitors.
Definition: InstVisitor.h:81
This represents the llvm.dbg.label instruction.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
RetTy visitDbgDeclareInst(DbgDeclareInst &I)
Definition: InstVisitor.h:205
RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I)
Definition: InstVisitor.h:210
RetTy visitFPExtInst(FPExtInst &I)
Definition: InstVisitor.h:183
This represents the llvm.va_end intrinsic.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
An instruction for ordering other memory operations.
Definition: Instructions.h:444
iterator end()
Definition: Function.h:658
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:518
RetTy visitCmpInst(CmpInst &I)
Definition: InstVisitor.h:268
RetTy visitTerminator(Instruction &I)
Definition: InstVisitor.h:259
RetTy visitFCmpInst(FCmpInst &I)
Definition: InstVisitor.h:170
This class represents zero extension of integer types.
RetTy visitCastInst(CastInst &I)
Definition: InstVisitor.h:265
This class represents a function call, abstracting a target machine&#39;s calling convention.
RetTy visitBranchInst(BranchInst &I)
Definition: InstVisitor.h:235
RetTy visitLandingPadInst(LandingPadInst &I)
Definition: InstVisitor.h:199
This instruction constructs a fixed permutation of two input vectors.
This class wraps the llvm.memset intrinsic.
void visit(BasicBlock *BB)
Definition: InstVisitor.h:113
This class represents a sign extension of integer types.
An instruction for reading from memory.
Definition: Instructions.h:168
RetTy visitDbgVariableIntrinsic(DbgVariableIntrinsic &I)
Definition: InstVisitor.h:207
RetTy visitPHINode(PHINode &I)
Definition: InstVisitor.h:178
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:681
RetTy visitICmpInst(ICmpInst &I)
Definition: InstVisitor.h:169
RetTy visitExtractElementInst(ExtractElementInst &I)
Definition: InstVisitor.h:194
RetTy visitVACopyInst(VACopyInst &I)
Definition: InstVisitor.h:218
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:269
RetTy visitSExtInst(SExtInst &I)
Definition: InstVisitor.h:181
void visit(Module *M)
Definition: InstVisitor.h:111
void visitFunction(Function &F)
Definition: InstVisitor.h:145
This class represents a conversion between pointers from one address space to another.
RetTy visitMemCpyInst(MemCpyInst &I)
Definition: InstVisitor.h:212
This class represents the LLVM &#39;select&#39; instruction.
RetTy visitIntrinsicInst(IntrinsicInst &I)
Definition: InstVisitor.h:219
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:349
This class wraps the llvm.memmove intrinsic.
RetTy visitUIToFPInst(UIToFPInst &I)
Definition: InstVisitor.h:186
RetTy visitMemMoveInst(MemMoveInst &I)
Definition: InstVisitor.h:213
InstrTy * getInstruction() const
Definition: CallSite.h:92
RetTy visitFPToUIInst(FPToUIInst &I)
Definition: InstVisitor.h:184
RetTy visitInsertValueInst(InsertValueInst &I)
Definition: InstVisitor.h:198
This class represents a cast from a pointer to an integer.
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
This represents the llvm.va_start intrinsic.
This is the common base class for debug info intrinsics for variables.
Definition: IntrinsicInst.h:88
This instruction compares its operands according to the predicate given to the constructor.
This class represents a no-op cast from one type to another.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:126
An instruction for storing to memory.
Definition: Instructions.h:310
This class represents a cast from floating point to signed integer.
RetTy visitMemTransferInst(MemTransferInst &I)
Definition: InstVisitor.h:214
iterator begin()
Definition: Function.h:656
RetTy visitExtractValueInst(ExtractValueInst &I)
Definition: InstVisitor.h:197
This class represents a truncation of integer types.
bool isCall() const
Return true if a CallInst is enclosed.
Definition: CallSite.h:87
RetTy visitVAEndInst(VAEndInst &I)
Definition: InstVisitor.h:217
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:843
RetTy visitFuncletPadInst(FuncletPadInst &I)
Definition: InstVisitor.h:200
This instruction inserts a single (scalar) element into a VectorType value.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
void visitBasicBlock(BasicBlock &BB)
Definition: InstVisitor.h:146
RetTy visitVAArgInst(VAArgInst &I)
Definition: InstVisitor.h:193
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
RetTy visitIntToPtrInst(IntToPtrInst &I)
Definition: InstVisitor.h:189
Conditional or Unconditional Branch instruction.
void visit(Function *F)
Definition: InstVisitor.h:112
This function has undefined behavior.
RetTy visitResumeInst(ResumeInst &I)
Definition: InstVisitor.h:244
a unary instruction
Resume the propagation of an exception.
Indirect Branch Instruction.
RetTy visitCatchReturnInst(CatchReturnInst &I)
Definition: InstVisitor.h:253
This instruction compares its operands according to the predicate given to the constructor.
RetTy visitDbgLabelInst(DbgLabelInst &I)
Definition: InstVisitor.h:209
RetTy visitSwitchInst(SwitchInst &I)
Definition: InstVisitor.h:238
RetTy visitFPTruncInst(FPTruncInst &I)
Definition: InstVisitor.h:182
RetTy visitLoadInst(LoadInst &I)
Definition: InstVisitor.h:172
This class represents a cast from an integer to a pointer.
RetTy visitDbgValueInst(DbgValueInst &I)
Definition: InstVisitor.h:206
RetTy visitZExtInst(ZExtInst &I)
Definition: InstVisitor.h:180
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
RetTy visitReturnInst(ReturnInst &I)
Definition: InstVisitor.h:232
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isInvoke() const
Return true if a InvokeInst is enclosed.
Definition: CallSite.h:90
RetTy visitSIToFPInst(SIToFPInst &I)
Definition: InstVisitor.h:187
RetTy visitInvokeInst(InvokeInst &I)
Definition: InstVisitor.h:226
RetTy visitBinaryOperator(BinaryOperator &I)
Definition: InstVisitor.h:267
This is the common base class for memset/memcpy/memmove.
RetTy visitShuffleVectorInst(ShuffleVectorInst &I)
Definition: InstVisitor.h:196
RetTy visitIndirectBrInst(IndirectBrInst &I)
Definition: InstVisitor.h:241
RetTy visitCatchSwitchInst(CatchSwitchInst &I)
Definition: InstVisitor.h:256
RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I)
Definition: InstVisitor.h:174
iterator end()
Definition: BasicBlock.h:271
Module.h This file contains the declarations for the Module class.
RetTy visitFPToSIInst(FPToSIInst &I)
Definition: InstVisitor.h:185
RetTy visitCatchPadInst(CatchPadInst &I)
Definition: InstVisitor.h:202
This is the common base class for debug info intrinsics.
Definition: IntrinsicInst.h:67
This class represents a cast from floating point to unsigned integer.
This class wraps the llvm.memcpy intrinsic.
RetTy visitCleanupPadInst(CleanupPadInst &I)
Definition: InstVisitor.h:201
RetTy visitFenceInst(FenceInst &I)
Definition: InstVisitor.h:176
RetTy visitStoreInst(StoreInst &I)
Definition: InstVisitor.h:173
RetTy visitCallInst(CallInst &I)
Definition: InstVisitor.h:223
This class wraps the llvm.memcpy/memmove intrinsics.
void visitModule(Module &M)
Definition: InstVisitor.h:144
RetTy visitPtrToIntInst(PtrToIntInst &I)
Definition: InstVisitor.h:188
void visit(Function &F)
Definition: InstVisitor.h:101
iterator end()
Definition: Module.h:588
This represents the llvm.dbg.value instruction.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1112
#define I(x, y, z)
Definition: MD5.cpp:58
This class represents a cast unsigned integer to floating point.
iterator begin()
Definition: Module.h:586
This instruction extracts a single (scalar) element from a VectorType value.
RetTy visitGetElementPtrInst(GetElementPtrInst &I)
Definition: InstVisitor.h:177
RetTy visitBitCastInst(BitCastInst &I)
Definition: InstVisitor.h:190
RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I)
Definition: InstVisitor.h:191
RetTy visitAllocaInst(AllocaInst &I)
Definition: InstVisitor.h:171
RetTy visitMemIntrinsic(MemIntrinsic &I)
Definition: InstVisitor.h:215
Multiway switch.
void visit(BasicBlock &BB)
Definition: InstVisitor.h:105
This class represents a cast from signed integer to floating point.
This represents the llvm.va_copy intrinsic.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RetTy visit(Instruction &I)
Definition: InstVisitor.h:118
RetTy visitUnreachableInst(UnreachableInst &I)
Definition: InstVisitor.h:247
This class represents a truncation of floating point types.
Invoke instruction.
Definition: InstVisitor.h:30
RetTy visitInsertElementInst(InsertElementInst &I)
Definition: InstVisitor.h:195
RetTy visit(Instruction *I)
Definition: InstVisitor.h:114
void visit(Module &M)
Definition: InstVisitor.h:97
void visitInstruction(Instruction &I)
Definition: InstVisitor.h:290
This class represents an extension of floating point types.
This represents the llvm.dbg.declare instruction.
RetTy visitCleanupReturnInst(CleanupReturnInst &I)
Definition: InstVisitor.h:250
RetTy visitUnaryInstruction(UnaryInstruction &I)
Definition: InstVisitor.h:269
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
an instruction to allocate memory on the stack
Definition: Instructions.h:60
This instruction inserts a struct field of array element value into an aggregate value.
RetTy visitTruncInst(TruncInst &I)
Definition: InstVisitor.h:179