assert(0) -> LLVM_UNREACHABLE.
[oota-llvm.git] / include / llvm / Support / InstVisitor.h
1 //===- llvm/Support/InstVisitor.h - Define instruction visitors -*- 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_SUPPORT_INSTVISITOR_H
12 #define LLVM_SUPPORT_INSTVISITOR_H
13
14 #include "llvm/Function.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/Module.h"
17 #include "llvm/Support/ErrorHandling.h"
18
19 namespace llvm {
20
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/Instruction.def"
26
27 #define DELEGATE(CLASS_TO_VISIT) \
28   return static_cast<SubClass*>(this)-> \
29                visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
30
31
32 /// @brief Base class for instruction visitors
33 ///
34 /// Instruction visitors are used when you want to perform different action for
35 /// different kinds of instruction without without having to use lots of casts
36 /// and a big switch statement (in your code that is).
37 ///
38 /// To define your own visitor, inherit from this class, specifying your
39 /// new type for the 'SubClass' template parameter, and "override" visitXXX
40 /// functions in your class. I say "overriding" because this class is defined
41 /// in terms of statically resolved overloading, not virtual functions.
42 ///
43 /// For example, here is a visitor that counts the number of malloc
44 /// instructions processed:
45 ///
46 ///  /// Declare the class.  Note that we derive from InstVisitor instantiated
47 ///  /// with _our new subclasses_ type.
48 ///  ///
49 ///  struct CountMallocVisitor : public InstVisitor<CountMallocVisitor> {
50 ///    unsigned Count;
51 ///    CountMallocVisitor() : Count(0) {}
52 ///
53 ///    void visitMallocInst(MallocInst &MI) { ++Count; }
54 ///  };
55 ///
56 ///  And this class would be used like this:
57 ///    CountMallocVistor CMV;
58 ///    CMV.visit(function);
59 ///    NumMallocs = CMV.Count;
60 ///
61 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
62 /// Function, and Module, which recursively process all conained instructions.
63 ///
64 /// Note that if you don't implement visitXXX for some instruction type,
65 /// the visitXXX method for instruction superclass will be invoked. So
66 /// if instructions are added in the future, they will be automatically
67 /// supported, if you handle on of their superclasses.
68 ///
69 /// The optional second template argument specifies the type that instruction
70 /// visitation functions should return. If you specify this, you *MUST* provide
71 /// an implementation of visitInstruction though!.
72 ///
73 /// Note that this class is specifically designed as a template to avoid
74 /// virtual function call overhead.  Defining and using an InstVisitor is just
75 /// as efficient as having your own switch statement over the instruction
76 /// opcode.
77 template<typename SubClass, typename RetTy=void>
78 class InstVisitor {
79   //===--------------------------------------------------------------------===//
80   // Interface code - This is the public interface of the InstVisitor that you
81   // use to visit instructions...
82   //
83
84 public:
85   // Generic visit method - Allow visitation to all instructions in a range
86   template<class Iterator>
87   void visit(Iterator Start, Iterator End) {
88     while (Start != End)
89       static_cast<SubClass*>(this)->visit(*Start++);
90   }
91
92   // Define visitors for functions and basic blocks...
93   //
94   void visit(Module &M) {
95     static_cast<SubClass*>(this)->visitModule(M);
96     visit(M.begin(), M.end());
97   }
98   void visit(Function &F) {
99     static_cast<SubClass*>(this)->visitFunction(F);
100     visit(F.begin(), F.end());
101   }
102   void visit(BasicBlock &BB) {
103     static_cast<SubClass*>(this)->visitBasicBlock(BB);
104     visit(BB.begin(), BB.end());
105   }
106
107   // Forwarding functions so that the user can visit with pointers AND refs.
108   void visit(Module       *M)  { visit(*M); }
109   void visit(Function     *F)  { visit(*F); }
110   void visit(BasicBlock   *BB) { visit(*BB); }
111   RetTy visit(Instruction *I)  { return visit(*I); }
112
113   // visit - Finally, code to visit an instruction...
114   //
115   RetTy visit(Instruction &I) {
116     switch (I.getOpcode()) {
117     default: LLVM_UNREACHABLE("Unknown instruction type encountered!");
118       // Build the switch statement using the Instruction.def file...
119 #define HANDLE_INST(NUM, OPCODE, CLASS) \
120     case Instruction::OPCODE: return \
121            static_cast<SubClass*>(this)-> \
122                       visit##OPCODE(static_cast<CLASS&>(I));
123 #include "llvm/Instruction.def"
124     }
125   }
126
127   //===--------------------------------------------------------------------===//
128   // Visitation functions... these functions provide default fallbacks in case
129   // the user does not specify what to do for a particular instruction type.
130   // The default behavior is to generalize the instruction type to its subtype
131   // and try visiting the subtype.  All of this should be inlined perfectly,
132   // because there are no virtual functions to get in the way.
133   //
134
135   // When visiting a module, function or basic block directly, these methods get
136   // called to indicate when transitioning into a new unit.
137   //
138   void visitModule    (Module &M) {}
139   void visitFunction  (Function &F) {}
140   void visitBasicBlock(BasicBlock &BB) {}
141
142   // Define instruction specific visitor functions that can be overridden to
143   // handle SPECIFIC instructions.  These functions automatically define
144   // visitMul to proxy to visitBinaryOperator for instance in case the user does
145   // not need this generality.
146   //
147   // The one problem case we have to handle here though is that the PHINode
148   // class and opcode name are the exact same.  Because of this, we cannot
149   // define visitPHINode (the inst version) to forward to visitPHINode (the
150   // generic version) without multiply defined symbols and recursion.  To handle
151   // this, we do not autoexpand "Other" instructions, we do it manually.
152   //
153 #define HANDLE_INST(NUM, OPCODE, CLASS) \
154     RetTy visit##OPCODE(CLASS &I) { DELEGATE(CLASS); }
155 #include "llvm/Instruction.def"
156
157   // Specific Instruction type classes... note that all of the casts are
158   // necessary because we use the instruction classes as opaque types...
159   //
160   RetTy visitReturnInst(ReturnInst &I)              { DELEGATE(TerminatorInst);}
161   RetTy visitBranchInst(BranchInst &I)              { DELEGATE(TerminatorInst);}
162   RetTy visitSwitchInst(SwitchInst &I)              { DELEGATE(TerminatorInst);}
163   RetTy visitInvokeInst(InvokeInst &I)              { DELEGATE(TerminatorInst);}
164   RetTy visitUnwindInst(UnwindInst &I)              { DELEGATE(TerminatorInst);}
165   RetTy visitUnreachableInst(UnreachableInst &I)    { DELEGATE(TerminatorInst);}
166   RetTy visitICmpInst(ICmpInst &I)                  { DELEGATE(CmpInst);}
167   RetTy visitFCmpInst(FCmpInst &I)                  { DELEGATE(CmpInst);}
168   RetTy visitMallocInst(MallocInst &I)              { DELEGATE(AllocationInst);}
169   RetTy visitAllocaInst(AllocaInst &I)              { DELEGATE(AllocationInst);}
170   RetTy visitFreeInst(FreeInst     &I)              { DELEGATE(Instruction); }
171   RetTy visitLoadInst(LoadInst     &I)              { DELEGATE(Instruction); }
172   RetTy visitStoreInst(StoreInst   &I)              { DELEGATE(Instruction); }
173   RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction); }
174   RetTy visitPHINode(PHINode       &I)              { DELEGATE(Instruction); }
175   RetTy visitTruncInst(TruncInst &I)                { DELEGATE(CastInst); }
176   RetTy visitZExtInst(ZExtInst &I)                  { DELEGATE(CastInst); }
177   RetTy visitSExtInst(SExtInst &I)                  { DELEGATE(CastInst); }
178   RetTy visitFPTruncInst(FPTruncInst &I)            { DELEGATE(CastInst); }
179   RetTy visitFPExtInst(FPExtInst &I)                { DELEGATE(CastInst); }
180   RetTy visitFPToUIInst(FPToUIInst &I)              { DELEGATE(CastInst); }
181   RetTy visitFPToSIInst(FPToSIInst &I)              { DELEGATE(CastInst); }
182   RetTy visitUIToFPInst(UIToFPInst &I)              { DELEGATE(CastInst); }
183   RetTy visitSIToFPInst(SIToFPInst &I)              { DELEGATE(CastInst); }
184   RetTy visitPtrToIntInst(PtrToIntInst &I)          { DELEGATE(CastInst); }
185   RetTy visitIntToPtrInst(IntToPtrInst &I)          { DELEGATE(CastInst); }
186   RetTy visitBitCastInst(BitCastInst &I)            { DELEGATE(CastInst); }
187   RetTy visitSelectInst(SelectInst &I)              { DELEGATE(Instruction); }
188   RetTy visitCallInst(CallInst     &I)              { DELEGATE(Instruction); }
189   RetTy visitVAArgInst(VAArgInst   &I)              { DELEGATE(Instruction); }
190   RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
191   RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction); }
192   RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction); }
193   RetTy visitExtractValueInst(ExtractValueInst &I)  { DELEGATE(Instruction);}
194   RetTy visitInsertValueInst(InsertValueInst &I)    { DELEGATE(Instruction); }
195
196   // Next level propagators... if the user does not overload a specific
197   // instruction type, they can overload one of these to get the whole class
198   // of instructions...
199   //
200   RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction); }
201   RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction); }
202   RetTy visitAllocationInst(AllocationInst &I) { DELEGATE(Instruction); }
203   RetTy visitCmpInst(CmpInst &I)               { DELEGATE(Instruction); }
204   RetTy visitCastInst(CastInst &I)             { DELEGATE(Instruction); }
205
206   // If the user wants a 'default' case, they can choose to override this
207   // function.  If this function is not overloaded in the users subclass, then
208   // this instruction just gets ignored.
209   //
210   // Note that you MUST override this function if your return type is not void.
211   //
212   void visitInstruction(Instruction &I) {}  // Ignore unhandled instructions
213 };
214
215 #undef DELEGATE
216
217 } // End llvm namespace
218
219 #endif