LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InstVisitor.h
Go to the documentation of this file.
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 
10 
11 #ifndef LLVM_INSTVISITOR_H
12 #define LLVM_INSTVISITOR_H
13 
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/IntrinsicInst.h"
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/CallSite.h"
21 
22 namespace llvm {
23 
24 // We operate on opaque instruction classes, so forward declare all instruction
25 // types now...
26 //
27 #define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS;
28 #include "llvm/IR/Instruction.def"
29 
30 #define DELEGATE(CLASS_TO_VISIT) \
31  return static_cast<SubClass*>(this)-> \
32  visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
33 
34 
35 /// @brief 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  //
86 
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  }
94 
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  }
109 
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); }
115 
116  // visit - Finally, code to visit an instruction...
117  //
118  RetTy visit(Instruction &I) {
119  switch (I.getOpcode()) {
120  default: llvm_unreachable("Unknown instruction type encountered!");
121  // Build the switch statement using the Instruction.def file...
122 #define HANDLE_INST(NUM, OPCODE, CLASS) \
123  case Instruction::OPCODE: return \
124  static_cast<SubClass*>(this)-> \
125  visit##OPCODE(static_cast<CLASS&>(I));
126 #include "llvm/IR/Instruction.def"
127  }
128  }
129 
130  //===--------------------------------------------------------------------===//
131  // Visitation functions... these functions provide default fallbacks in case
132  // the user does not specify what to do for a particular instruction type.
133  // The default behavior is to generalize the instruction type to its subtype
134  // and try visiting the subtype. All of this should be inlined perfectly,
135  // because there are no virtual functions to get in the way.
136  //
137 
138  // When visiting a module, function or basic block directly, these methods get
139  // called to indicate when transitioning into a new unit.
140  //
141  void visitModule (Module &M) {}
144 
145  // Define instruction specific visitor functions that can be overridden to
146  // handle SPECIFIC instructions. These functions automatically define
147  // visitMul to proxy to visitBinaryOperator for instance in case the user does
148  // not need this generality.
149  //
150  // These functions can also implement fan-out, when a single opcode and
151  // instruction have multiple more specific Instruction subclasses. The Call
152  // instruction currently supports this. We implement that by redirecting that
153  // instruction to a special delegation helper.
154 #define HANDLE_INST(NUM, OPCODE, CLASS) \
155  RetTy visit##OPCODE(CLASS &I) { \
156  if (NUM == Instruction::Call) \
157  return delegateCallInst(I); \
158  else \
159  DELEGATE(CLASS); \
160  }
161 #include "llvm/IR/Instruction.def"
162 
163  // Specific Instruction type classes... note that all of the casts are
164  // necessary because we use the instruction classes as opaque types...
165  //
203 
204  // Handle the special instrinsic instruction classes.
217 
218  // Call and Invoke are slightly different as they delegate first through
219  // a generic CallSite visitor.
221  return static_cast<SubClass*>(this)->visitCallSite(&I);
222  }
224  return static_cast<SubClass*>(this)->visitCallSite(&I);
225  }
226 
227  // Next level propagators: If the user does not overload a specific
228  // instruction type, they can overload one of these to get the whole class
229  // of instructions...
230  //
236 
237  // Provide a special visitor for a 'callsite' that visits both calls and
238  // invokes. When unimplemented, properly delegates to either the terminator or
239  // regular instruction visitor.
241  assert(CS);
242  Instruction &I = *CS.getInstruction();
243  if (CS.isCall())
245 
246  assert(CS.isInvoke());
248  }
249 
250  // If the user wants a 'default' case, they can choose to override this
251  // function. If this function is not overloaded in the user's subclass, then
252  // this instruction just gets ignored.
253  //
254  // Note that you MUST override this function if your return type is not void.
255  //
256  void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
257 
258 private:
259  // Special helper function to delegate to CallInst subclass visitors.
260  RetTy delegateCallInst(CallInst &I) {
261  if (const Function *F = I.getCalledFunction()) {
262  switch ((Intrinsic::ID)F->getIntrinsicID()) {
263  default: DELEGATE(IntrinsicInst);
272  case Intrinsic::not_intrinsic: break;
273  }
274  }
275  DELEGATE(CallInst);
276  }
277 
278  // An overload that will never actually be called, it is used only from dead
279  // code in the dispatching from opcodes to instruction subclasses.
280  RetTy delegateCallInst(Instruction &I) {
281  llvm_unreachable("delegateCallInst called for non-CallInst");
282  }
283 };
284 
285 #undef DELEGATE
286 
287 } // End llvm namespace
288 
289 #endif
RetTy visitSelectInst(SelectInst &I)
Definition: InstVisitor.h:195
RetTy visitAtomicRMWInst(AtomicRMWInst &I)
Definition: InstVisitor.h:178
RetTy visitVAStartInst(VAStartInst &I)
Definition: InstVisitor.h:213
RetTy visitMemSetInst(MemSetInst &I)
Definition: InstVisitor.h:208
Abstract base class of comparison instructions.
Definition: InstrTypes.h:633
RetTy visitCallSite(CallSite CS)
Definition: InstVisitor.h:240
Base class for instruction visitors.
Definition: InstVisitor.h:81
RetTy visitDbgDeclareInst(DbgDeclareInst &I)
Definition: InstVisitor.h:205
RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I)
Definition: InstVisitor.h:207
RetTy visitFPExtInst(FPExtInst &I)
Definition: InstVisitor.h:186
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
iterator end()
Definition: Function.h:397
RetTy visitCmpInst(CmpInst &I)
Definition: InstVisitor.h:233
RetTy visitFCmpInst(FCmpInst &I)
Definition: InstVisitor.h:173
This class represents zero extension of integer types.
RetTy visitCastInst(CastInst &I)
Definition: InstVisitor.h:231
RetTy visitBranchInst(BranchInst &I)
Definition: InstVisitor.h:167
RetTy visitLandingPadInst(LandingPadInst &I)
Definition: InstVisitor.h:202
void visit(BasicBlock *BB)
Definition: InstVisitor.h:113
F(f)
This class represents a sign extension of integer types.
RetTy visitPHINode(PHINode &I)
Definition: InstVisitor.h:181
RetTy visitICmpInst(ICmpInst &I)
Definition: InstVisitor.h:172
RetTy visitExtractElementInst(ExtractElementInst &I)
Definition: InstVisitor.h:197
RetTy visitVACopyInst(VACopyInst &I)
Definition: InstVisitor.h:215
iterator begin()
Definition: BasicBlock.h:193
RetTy visitSExtInst(SExtInst &I)
Definition: InstVisitor.h:184
void visit(Module *M)
Definition: InstVisitor.h:111
void visitFunction(Function &F)
Definition: InstVisitor.h:142
This class represents a conversion between pointers from one address space to another.
RetTy visitMemCpyInst(MemCpyInst &I)
Definition: InstVisitor.h:209
RetTy visitIntrinsicInst(IntrinsicInst &I)
Definition: InstVisitor.h:216
Base class of casting instructions.
Definition: InstrTypes.h:387
RetTy visitUIToFPInst(UIToFPInst &I)
Definition: InstVisitor.h:189
RetTy visitMemMoveInst(MemMoveInst &I)
Definition: InstVisitor.h:210
#define llvm_unreachable(msg)
RetTy visitFPToUIInst(FPToUIInst &I)
Definition: InstVisitor.h:187
RetTy visitInsertValueInst(InsertValueInst &I)
Definition: InstVisitor.h:201
This class represents a cast from a pointer to an integer.
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
Represents a floating point comparison operator.
This class represents a no-op cast from one type to another.
This class represents a cast from floating point to signed integer.
RetTy visitMemTransferInst(MemTransferInst &I)
Definition: InstVisitor.h:211
iterator begin()
Definition: Function.h:395
RetTy visitExtractValueInst(ExtractValueInst &I)
Definition: InstVisitor.h:200
This class represents a truncation of integer types.
RetTy visitVAEndInst(VAEndInst &I)
Definition: InstVisitor.h:214
bool isInvoke() const
Definition: CallSite.h:77
void visitBasicBlock(BasicBlock &BB)
Definition: InstVisitor.h:143
RetTy visitVAArgInst(VAArgInst &I)
Definition: InstVisitor.h:196
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
InstrTy * getInstruction() const
Definition: CallSite.h:79
RetTy visitIntToPtrInst(IntToPtrInst &I)
Definition: InstVisitor.h:192
void visit(Function *F)
Definition: InstVisitor.h:112
RetTy visitResumeInst(ResumeInst &I)
Definition: InstVisitor.h:170
RetTy visitTerminatorInst(TerminatorInst &I)
Definition: InstVisitor.h:234
Represent an integer comparison operator.
Definition: Instructions.h:911
RetTy visitSwitchInst(SwitchInst &I)
Definition: InstVisitor.h:168
RetTy visitFPTruncInst(FPTruncInst &I)
Definition: InstVisitor.h:185
RetTy visitLoadInst(LoadInst &I)
Definition: InstVisitor.h:175
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:183
RetTy visitReturnInst(ReturnInst &I)
Definition: InstVisitor.h:166
RetTy visitSIToFPInst(SIToFPInst &I)
Definition: InstVisitor.h:190
RetTy visitInvokeInst(InvokeInst &I)
Definition: InstVisitor.h:223
RetTy visitBinaryOperator(BinaryOperator &I)
Definition: InstVisitor.h:232
RetTy visitShuffleVectorInst(ShuffleVectorInst &I)
Definition: InstVisitor.h:199
RetTy visitIndirectBrInst(IndirectBrInst &I)
Definition: InstVisitor.h:169
RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I)
Definition: InstVisitor.h:177
iterator end()
Definition: BasicBlock.h:195
RetTy visitFPToSIInst(FPToSIInst &I)
Definition: InstVisitor.h:188
This class represents a cast from floating point to unsigned integer.
Function * getCalledFunction() const
RetTy visitFenceInst(FenceInst &I)
Definition: InstVisitor.h:179
RetTy visitStoreInst(StoreInst &I)
Definition: InstVisitor.h:176
RetTy visitCallInst(CallInst &I)
Definition: InstVisitor.h:220
void visitModule(Module &M)
Definition: InstVisitor.h:141
RetTy visitPtrToIntInst(PtrToIntInst &I)
Definition: InstVisitor.h:191
void visit(Function &F)
Definition: InstVisitor.h:101
iterator end()
Definition: Module.h:533
bool isCall() const
Definition: CallSite.h:73
#define I(x, y, z)
Definition: MD5.cpp:54
This class represents a cast unsigned integer to floating point.
iterator begin()
Definition: Module.h:531
RetTy visitGetElementPtrInst(GetElementPtrInst &I)
Definition: InstVisitor.h:180
RetTy visitBitCastInst(BitCastInst &I)
Definition: InstVisitor.h:193
RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I)
Definition: InstVisitor.h:194
RetTy visitAllocaInst(AllocaInst &I)
Definition: InstVisitor.h:174
RetTy visitMemIntrinsic(MemIntrinsic &I)
Definition: InstVisitor.h:212
void visit(BasicBlock &BB)
Definition: InstVisitor.h:105
This class represents a cast from signed integer to floating point.
RetTy visit(Instruction &I)
Definition: InstVisitor.h:118
RetTy visitUnreachableInst(UnreachableInst &I)
Definition: InstVisitor.h:171
This class represents a truncation of floating point types.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:83
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
RetTy visitInsertElementInst(InsertElementInst &I)
Definition: InstVisitor.h:198
RetTy visit(Instruction *I)
Definition: InstVisitor.h:114
void visit(Module &M)
Definition: InstVisitor.h:97
void visitInstruction(Instruction &I)
Definition: InstVisitor.h:256
This class represents an extension of floating point types.
RetTy visitUnaryInstruction(UnaryInstruction &I)
Definition: InstVisitor.h:235
RetTy visitTruncInst(TruncInst &I)
Definition: InstVisitor.h:182