| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#ifndef LLVM_IR_INSTVISITOR_H |
| 11 |
#define LLVM_IR_INSTVISITOR_H |
| 12 |
|
| 13 |
#include "llvm/IR/Function.h" |
| 14 |
#include "llvm/IR/Instructions.h" |
| 15 |
#include "llvm/IR/IntrinsicInst.h" |
| 16 |
#include "llvm/IR/Intrinsics.h" |
| 17 |
#include "llvm/IR/Module.h" |
| 18 |
#include "llvm/Support/ErrorHandling.h" |
| 19 |
|
| 20 |
namespace llvm { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS; |
| 26 |
#include "llvm/IR/Instruction.def" |
| 27 |
|
| 28 |
#define DELEGATE(CLASS_TO_VISIT) \ |
| 29 |
return static_cast<SubClass*>(this)-> \ |
| 30 |
visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I)) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
template<typename SubClass, typename RetTy=void> |
| 79 |
class InstVisitor { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public: |
| 86 |
|
| 87 |
template<class Iterator> |
| 88 |
void visit(Iterator Start, Iterator End) { |
| 89 |
while (Start != End) |
| 90 |
static_cast<SubClass*>(this)->visit(*Start++); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void visit(Module &M) { |
| 96 |
static_cast<SubClass*>(this)->visitModule(M); |
| 97 |
visit(M.begin(), M.end()); |
| 98 |
} |
| 99 |
void visit(Function &F) { |
| 100 |
static_cast<SubClass*>(this)->visitFunction(F); |
| 101 |
visit(F.begin(), F.end()); |
| 102 |
} |
| 103 |
void visit(BasicBlock &BB) { |
| 104 |
static_cast<SubClass*>(this)->visitBasicBlock(BB); |
| 105 |
visit(BB.begin(), BB.end()); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
void visit(Module *M) { visit(*M); } |
| 110 |
void visit(Function *F) { visit(*F); } |
| 111 |
void visit(BasicBlock *BB) { visit(*BB); } |
| 112 |
RetTy visit(Instruction *I) { return visit(*I); } |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
RetTy visit(Instruction &I) { |
| 117 |
static_assert(std::is_base_of<InstVisitor, SubClass>::value, |
| 118 |
"Must pass the derived type to this template!"); |
| 119 |
|
| 120 |
switch (I.getOpcode()) { |
| 121 |
default: llvm_unreachable("Unknown instruction type encountered!"); |
| 122 |
|
| 123 |
#define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 124 |
case Instruction::OPCODE: return \ |
| 125 |
static_cast<SubClass*>(this)-> \ |
| 126 |
visit##OPCODE(static_cast<CLASS&>(I)); |
| 127 |
#include "llvm/IR/Instruction.def" |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
void visitModule (Module &M) {} |
| 143 |
void visitFunction (Function &F) {} |
| 144 |
void visitBasicBlock(BasicBlock &BB) {} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 156 |
RetTy visit##OPCODE(CLASS &I) { \ |
| 157 |
if (NUM == Instruction::Call) \ |
| 158 |
return delegateCallInst(I); \ |
| 159 |
else \ |
| 160 |
DELEGATE(CLASS); \ |
| 161 |
} |
| 162 |
#include "llvm/IR/Instruction.def" |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);} |
| 168 |
RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);} |
| 169 |
RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(UnaryInstruction);} |
| 170 |
RetTy visitLoadInst(LoadInst &I) { DELEGATE(UnaryInstruction);} |
| 171 |
RetTy visitStoreInst(StoreInst &I) { DELEGATE(Instruction);} |
| 172 |
RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { DELEGATE(Instruction);} |
| 173 |
RetTy visitAtomicRMWInst(AtomicRMWInst &I) { DELEGATE(Instruction);} |
| 174 |
RetTy visitFenceInst(FenceInst &I) { DELEGATE(Instruction);} |
| 175 |
RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction);} |
| 176 |
RetTy visitPHINode(PHINode &I) { DELEGATE(Instruction);} |
| 177 |
RetTy visitTruncInst(TruncInst &I) { DELEGATE(CastInst);} |
| 178 |
RetTy visitZExtInst(ZExtInst &I) { DELEGATE(CastInst);} |
| 179 |
RetTy visitSExtInst(SExtInst &I) { DELEGATE(CastInst);} |
| 180 |
RetTy visitFPTruncInst(FPTruncInst &I) { DELEGATE(CastInst);} |
| 181 |
RetTy visitFPExtInst(FPExtInst &I) { DELEGATE(CastInst);} |
| 182 |
RetTy visitFPToUIInst(FPToUIInst &I) { DELEGATE(CastInst);} |
| 183 |
RetTy visitFPToSIInst(FPToSIInst &I) { DELEGATE(CastInst);} |
| 184 |
RetTy visitUIToFPInst(UIToFPInst &I) { DELEGATE(CastInst);} |
| 185 |
RetTy visitSIToFPInst(SIToFPInst &I) { DELEGATE(CastInst);} |
| 186 |
RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst);} |
| 187 |
RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst);} |
| 188 |
RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst);} |
| 189 |
RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);} |
| 190 |
RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction);} |
| 191 |
RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(UnaryInstruction);} |
| 192 |
RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);} |
| 193 |
RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction);} |
| 194 |
RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction);} |
| 195 |
RetTy visitExtractValueInst(ExtractValueInst &I){ DELEGATE(UnaryInstruction);} |
| 196 |
RetTy visitInsertValueInst(InsertValueInst &I) { DELEGATE(Instruction); } |
| 197 |
RetTy visitLandingPadInst(LandingPadInst &I) { DELEGATE(Instruction); } |
| 198 |
RetTy visitFuncletPadInst(FuncletPadInst &I) { DELEGATE(Instruction); } |
| 199 |
RetTy visitCleanupPadInst(CleanupPadInst &I) { DELEGATE(FuncletPadInst); } |
| 200 |
RetTy visitCatchPadInst(CatchPadInst &I) { DELEGATE(FuncletPadInst); } |
| 201 |
RetTy visitFreezeInst(FreezeInst &I) { DELEGATE(Instruction); } |
| 202 |
|
| 203 |
|
| 204 |
RetTy visitDbgDeclareInst(DbgDeclareInst &I) { DELEGATE(DbgVariableIntrinsic);} |
| 205 |
RetTy visitDbgValueInst(DbgValueInst &I) { DELEGATE(DbgVariableIntrinsic);} |
| 206 |
RetTy visitDbgVariableIntrinsic(DbgVariableIntrinsic &I) |
| 207 |
{ DELEGATE(DbgInfoIntrinsic);} |
| 208 |
RetTy visitDbgLabelInst(DbgLabelInst &I) { DELEGATE(DbgInfoIntrinsic);} |
| 209 |
RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I){ DELEGATE(IntrinsicInst); } |
| 210 |
RetTy visitMemSetInst(MemSetInst &I) { DELEGATE(MemIntrinsic); } |
| 211 |
RetTy visitMemCpyInst(MemCpyInst &I) { DELEGATE(MemTransferInst); } |
| 212 |
RetTy visitMemCpyInlineInst(MemCpyInlineInst &I) { |
| 213 |
DELEGATE(MemTransferInst); |
| 214 |
} |
| 215 |
RetTy visitMemMoveInst(MemMoveInst &I) { DELEGATE(MemTransferInst); } |
| 216 |
RetTy visitMemTransferInst(MemTransferInst &I) { DELEGATE(MemIntrinsic); } |
| 217 |
RetTy visitMemIntrinsic(MemIntrinsic &I) { DELEGATE(IntrinsicInst); } |
| 218 |
RetTy visitVAStartInst(VAStartInst &I) { DELEGATE(IntrinsicInst); } |
| 219 |
RetTy visitVAEndInst(VAEndInst &I) { DELEGATE(IntrinsicInst); } |
| 220 |
RetTy visitVACopyInst(VACopyInst &I) { DELEGATE(IntrinsicInst); } |
| 221 |
RetTy visitIntrinsicInst(IntrinsicInst &I) { DELEGATE(CallInst); } |
| 222 |
RetTy visitCallInst(CallInst &I) { DELEGATE(CallBase); } |
| 223 |
RetTy visitInvokeInst(InvokeInst &I) { DELEGATE(CallBase); } |
| 224 |
RetTy visitCallBrInst(CallBrInst &I) { DELEGATE(CallBase); } |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
RetTy visitReturnInst(ReturnInst &I) { |
| 229 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 230 |
} |
| 231 |
RetTy visitBranchInst(BranchInst &I) { |
| 232 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 233 |
} |
| 234 |
RetTy visitSwitchInst(SwitchInst &I) { |
| 235 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 236 |
} |
| 237 |
RetTy visitIndirectBrInst(IndirectBrInst &I) { |
| 238 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 239 |
} |
| 240 |
RetTy visitResumeInst(ResumeInst &I) { |
| 241 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 242 |
} |
| 243 |
RetTy visitUnreachableInst(UnreachableInst &I) { |
| 244 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 245 |
} |
| 246 |
RetTy visitCleanupReturnInst(CleanupReturnInst &I) { |
| 247 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 248 |
} |
| 249 |
RetTy visitCatchReturnInst(CatchReturnInst &I) { |
| 250 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 251 |
} |
| 252 |
RetTy visitCatchSwitchInst(CatchSwitchInst &I) { |
| 253 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 254 |
} |
| 255 |
RetTy visitTerminator(Instruction &I) { DELEGATE(Instruction);} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
RetTy visitCastInst(CastInst &I) { DELEGATE(UnaryInstruction);} |
| 262 |
RetTy visitUnaryOperator(UnaryOperator &I) { DELEGATE(UnaryInstruction);} |
| 263 |
RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction);} |
| 264 |
RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction);} |
| 265 |
RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
RetTy visitCallBase(CallBase &I) { |
| 270 |
if (isa<InvokeInst>(I) || isa<CallBrInst>(I)) |
| 271 |
return static_cast<SubClass *>(this)->visitTerminator(I); |
| 272 |
|
| 273 |
DELEGATE(Instruction); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
void visitInstruction(Instruction &I) {} |
| 283 |
|
| 284 |
private: |
| 285 |
|
| 286 |
RetTy delegateCallInst(CallInst &I) { |
| 287 |
if (const Function *F = I.getCalledFunction()) { |
| 288 |
switch (F->getIntrinsicID()) { |
| 289 |
default: DELEGATE(IntrinsicInst); |
| 290 |
case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst); |
| 291 |
case Intrinsic::dbg_value: DELEGATE(DbgValueInst); |
| 292 |
case Intrinsic::dbg_label: DELEGATE(DbgLabelInst); |
| 293 |
case Intrinsic::memcpy: DELEGATE(MemCpyInst); |
| 294 |
case Intrinsic::memmove: DELEGATE(MemMoveInst); |
| 295 |
case Intrinsic::memset: DELEGATE(MemSetInst); |
| 296 |
case Intrinsic::vastart: DELEGATE(VAStartInst); |
| 297 |
case Intrinsic::vaend: DELEGATE(VAEndInst); |
| 298 |
case Intrinsic::vacopy: DELEGATE(VACopyInst); |
| 299 |
case Intrinsic::not_intrinsic: break; |
| 300 |
} |
| 301 |
} |
| 302 |
DELEGATE(CallInst); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
RetTy delegateCallInst(Instruction &I) { |
| 308 |
llvm_unreachable("delegateCallInst called for non-CallInst"); |
| 309 |
} |
| 310 |
}; |
| 311 |
|
| 312 |
#undef DELEGATE |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
#endif |