2014-03-06 03:23:41 +00:00
|
|
|
//===- InstVisitor.h - Instruction visitor templates ------------*- C++ -*-===//
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2006-05-31 15:30:18 +00:00
|
|
|
|
2002-03-18 19:07:42 +00:00
|
|
|
|
2014-03-06 03:23:41 +00:00
|
|
|
#ifndef LLVM_IR_INSTVISITOR_H
|
|
|
|
#define LLVM_IR_INSTVISITOR_H
|
2002-03-18 19:07:42 +00:00
|
|
|
|
2014-03-04 11:01:28 +00:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2003-06-11 14:01:36 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-03-18 19:07:42 +00:00
|
|
|
// We operate on opaque instruction classes, so forward declare all instruction
|
|
|
|
// types now...
|
|
|
|
//
|
|
|
|
#define HANDLE_INST(NUM, OPCODE, CLASS) class CLASS;
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2002-03-18 19:07:42 +00:00
|
|
|
|
2002-04-18 16:16:16 +00:00
|
|
|
#define DELEGATE(CLASS_TO_VISIT) \
|
2005-03-09 05:35:16 +00:00
|
|
|
return static_cast<SubClass*>(this)-> \
|
|
|
|
visit##CLASS_TO_VISIT(static_cast<CLASS_TO_VISIT&>(I))
|
2002-04-18 16:16:16 +00:00
|
|
|
|
|
|
|
|
2006-05-31 15:30:18 +00:00
|
|
|
/// @brief Base class for instruction visitors
|
|
|
|
///
|
2009-07-15 22:43:51 +00:00
|
|
|
/// Instruction visitors are used when you want to perform different actions
|
|
|
|
/// for different kinds of instructions without having to use lots of casts
|
|
|
|
/// and a big switch statement (in your code, that is).
|
2006-05-31 15:30:18 +00:00
|
|
|
///
|
|
|
|
/// To define your own visitor, inherit from this class, specifying your
|
|
|
|
/// new type for the 'SubClass' template parameter, and "override" visitXXX
|
2009-07-15 22:43:51 +00:00
|
|
|
/// functions in your class. I say "override" because this class is defined
|
2009-02-20 22:51:36 +00:00
|
|
|
/// in terms of statically resolved overloading, not virtual functions.
|
|
|
|
///
|
|
|
|
/// For example, here is a visitor that counts the number of malloc
|
2006-05-31 15:30:18 +00:00
|
|
|
/// instructions processed:
|
|
|
|
///
|
|
|
|
/// /// Declare the class. Note that we derive from InstVisitor instantiated
|
|
|
|
/// /// with _our new subclasses_ type.
|
|
|
|
/// ///
|
2009-10-17 01:18:07 +00:00
|
|
|
/// struct CountAllocaVisitor : public InstVisitor<CountAllocaVisitor> {
|
2006-05-31 15:30:18 +00:00
|
|
|
/// unsigned Count;
|
2009-10-17 01:18:07 +00:00
|
|
|
/// CountAllocaVisitor() : Count(0) {}
|
2006-05-31 15:30:18 +00:00
|
|
|
///
|
2009-10-17 01:18:07 +00:00
|
|
|
/// void visitAllocaInst(AllocaInst &AI) { ++Count; }
|
2006-05-31 15:30:18 +00:00
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// And this class would be used like this:
|
2009-10-17 01:18:07 +00:00
|
|
|
/// CountAllocaVisitor CAV;
|
|
|
|
/// CAV.visit(function);
|
|
|
|
/// NumAllocas = CAV.Count;
|
2006-05-31 15:30:18 +00:00
|
|
|
///
|
|
|
|
/// The defined has 'visit' methods for Instruction, and also for BasicBlock,
|
2009-07-15 22:43:51 +00:00
|
|
|
/// Function, and Module, which recursively process all contained instructions.
|
2006-05-31 15:30:18 +00:00
|
|
|
///
|
|
|
|
/// Note that if you don't implement visitXXX for some instruction type,
|
|
|
|
/// the visitXXX method for instruction superclass will be invoked. So
|
|
|
|
/// if instructions are added in the future, they will be automatically
|
2009-07-15 22:43:51 +00:00
|
|
|
/// supported, if you handle one of their superclasses.
|
2006-05-31 15:30:18 +00:00
|
|
|
///
|
2009-02-20 22:51:36 +00:00
|
|
|
/// The optional second template argument specifies the type that instruction
|
|
|
|
/// visitation functions should return. If you specify this, you *MUST* provide
|
2006-05-31 15:30:18 +00:00
|
|
|
/// an implementation of visitInstruction though!.
|
|
|
|
///
|
|
|
|
/// Note that this class is specifically designed as a template to avoid
|
|
|
|
/// virtual function call overhead. Defining and using an InstVisitor is just
|
|
|
|
/// as efficient as having your own switch statement over the instruction
|
|
|
|
/// opcode.
|
2002-04-18 15:46:40 +00:00
|
|
|
template<typename SubClass, typename RetTy=void>
|
2004-11-16 06:58:55 +00:00
|
|
|
class InstVisitor {
|
2002-03-18 19:07:42 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Interface code - This is the public interface of the InstVisitor that you
|
|
|
|
// use to visit instructions...
|
|
|
|
//
|
|
|
|
|
2004-11-16 06:58:55 +00:00
|
|
|
public:
|
2002-03-18 19:07:42 +00:00
|
|
|
// Generic visit method - Allow visitation to all instructions in a range
|
|
|
|
template<class Iterator>
|
|
|
|
void visit(Iterator Start, Iterator End) {
|
|
|
|
while (Start != End)
|
2005-03-09 05:35:16 +00:00
|
|
|
static_cast<SubClass*>(this)->visit(*Start++);
|
2002-03-18 19:07:42 +00:00
|
|
|
}
|
|
|
|
|
2003-08-29 14:48:21 +00:00
|
|
|
// Define visitors for functions and basic blocks...
|
2002-03-18 19:07:42 +00:00
|
|
|
//
|
2004-05-04 18:30:38 +00:00
|
|
|
void visit(Module &M) {
|
2005-03-09 05:35:16 +00:00
|
|
|
static_cast<SubClass*>(this)->visitModule(M);
|
2004-05-04 18:30:38 +00:00
|
|
|
visit(M.begin(), M.end());
|
|
|
|
}
|
2002-06-25 16:13:24 +00:00
|
|
|
void visit(Function &F) {
|
2005-03-09 05:35:16 +00:00
|
|
|
static_cast<SubClass*>(this)->visitFunction(F);
|
2002-06-25 16:13:24 +00:00
|
|
|
visit(F.begin(), F.end());
|
2002-04-15 19:32:36 +00:00
|
|
|
}
|
2002-06-25 16:13:24 +00:00
|
|
|
void visit(BasicBlock &BB) {
|
2005-03-09 05:35:16 +00:00
|
|
|
static_cast<SubClass*>(this)->visitBasicBlock(BB);
|
2002-06-25 16:13:24 +00:00
|
|
|
visit(BB.begin(), BB.end());
|
2002-04-15 19:32:36 +00:00
|
|
|
}
|
2002-03-18 19:07:42 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
// Forwarding functions so that the user can visit with pointers AND refs.
|
|
|
|
void visit(Module *M) { visit(*M); }
|
|
|
|
void visit(Function *F) { visit(*F); }
|
|
|
|
void visit(BasicBlock *BB) { visit(*BB); }
|
|
|
|
RetTy visit(Instruction *I) { return visit(*I); }
|
|
|
|
|
2002-03-18 19:07:42 +00:00
|
|
|
// visit - Finally, code to visit an instruction...
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
RetTy visit(Instruction &I) {
|
|
|
|
switch (I.getOpcode()) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unknown instruction type encountered!");
|
2002-03-18 19:07:42 +00:00
|
|
|
// Build the switch statement using the Instruction.def file...
|
|
|
|
#define HANDLE_INST(NUM, OPCODE, CLASS) \
|
2005-03-09 05:35:16 +00:00
|
|
|
case Instruction::OPCODE: return \
|
|
|
|
static_cast<SubClass*>(this)-> \
|
|
|
|
visit##OPCODE(static_cast<CLASS&>(I));
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2002-03-18 19:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitation functions... these functions provide default fallbacks in case
|
|
|
|
// the user does not specify what to do for a particular instruction type.
|
|
|
|
// The default behavior is to generalize the instruction type to its subtype
|
|
|
|
// and try visiting the subtype. All of this should be inlined perfectly,
|
|
|
|
// because there are no virtual functions to get in the way.
|
|
|
|
//
|
2002-04-15 19:32:36 +00:00
|
|
|
|
|
|
|
// When visiting a module, function or basic block directly, these methods get
|
|
|
|
// called to indicate when transitioning into a new unit.
|
|
|
|
//
|
2004-05-04 18:30:38 +00:00
|
|
|
void visitModule (Module &M) {}
|
2002-06-25 16:13:24 +00:00
|
|
|
void visitFunction (Function &F) {}
|
|
|
|
void visitBasicBlock(BasicBlock &BB) {}
|
2002-04-18 16:16:16 +00:00
|
|
|
|
|
|
|
// Define instruction specific visitor functions that can be overridden to
|
|
|
|
// handle SPECIFIC instructions. These functions automatically define
|
|
|
|
// visitMul to proxy to visitBinaryOperator for instance in case the user does
|
|
|
|
// not need this generality.
|
|
|
|
//
|
2012-07-30 23:45:06 +00:00
|
|
|
// These functions can also implement fan-out, when a single opcode and
|
|
|
|
// instruction have multiple more specific Instruction subclasses. The Call
|
|
|
|
// instruction currently supports this. We implement that by redirecting that
|
|
|
|
// instruction to a special delegation helper.
|
2002-04-18 16:16:16 +00:00
|
|
|
#define HANDLE_INST(NUM, OPCODE, CLASS) \
|
2012-07-30 23:45:06 +00:00
|
|
|
RetTy visit##OPCODE(CLASS &I) { \
|
|
|
|
if (NUM == Instruction::Call) \
|
|
|
|
return delegateCallInst(I); \
|
|
|
|
else \
|
|
|
|
DELEGATE(CLASS); \
|
|
|
|
}
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2002-04-18 16:16:16 +00:00
|
|
|
|
2002-03-18 19:07:42 +00:00
|
|
|
// Specific Instruction type classes... note that all of the casts are
|
2003-08-18 14:43:39 +00:00
|
|
|
// necessary because we use the instruction classes as opaque types...
|
2002-03-18 19:07:42 +00:00
|
|
|
//
|
2012-03-18 23:45:14 +00:00
|
|
|
RetTy visitReturnInst(ReturnInst &I) { DELEGATE(TerminatorInst);}
|
|
|
|
RetTy visitBranchInst(BranchInst &I) { DELEGATE(TerminatorInst);}
|
|
|
|
RetTy visitSwitchInst(SwitchInst &I) { DELEGATE(TerminatorInst);}
|
|
|
|
RetTy visitIndirectBrInst(IndirectBrInst &I) { DELEGATE(TerminatorInst);}
|
|
|
|
RetTy visitResumeInst(ResumeInst &I) { DELEGATE(TerminatorInst);}
|
|
|
|
RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);}
|
|
|
|
RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);}
|
|
|
|
RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);}
|
|
|
|
RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(UnaryInstruction);}
|
|
|
|
RetTy visitLoadInst(LoadInst &I) { DELEGATE(UnaryInstruction);}
|
|
|
|
RetTy visitStoreInst(StoreInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitAtomicRMWInst(AtomicRMWInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitFenceInst(FenceInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitGetElementPtrInst(GetElementPtrInst &I){ DELEGATE(Instruction);}
|
|
|
|
RetTy visitPHINode(PHINode &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitTruncInst(TruncInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitZExtInst(ZExtInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitSExtInst(SExtInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitFPTruncInst(FPTruncInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitFPExtInst(FPExtInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitFPToUIInst(FPToUIInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitFPToSIInst(FPToSIInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitUIToFPInst(UIToFPInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitSIToFPInst(SIToFPInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitPtrToIntInst(PtrToIntInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitIntToPtrInst(IntToPtrInst &I) { DELEGATE(CastInst);}
|
|
|
|
RetTy visitBitCastInst(BitCastInst &I) { DELEGATE(CastInst);}
|
2013-11-15 01:34:59 +00:00
|
|
|
RetTy visitAddrSpaceCastInst(AddrSpaceCastInst &I) { DELEGATE(CastInst);}
|
2012-03-18 23:45:14 +00:00
|
|
|
RetTy visitSelectInst(SelectInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitVAArgInst(VAArgInst &I) { DELEGATE(UnaryInstruction);}
|
2006-11-27 01:05:10 +00:00
|
|
|
RetTy visitExtractElementInst(ExtractElementInst &I) { DELEGATE(Instruction);}
|
2012-03-18 23:45:14 +00:00
|
|
|
RetTy visitInsertElementInst(InsertElementInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitExtractValueInst(ExtractValueInst &I){ DELEGATE(UnaryInstruction);}
|
|
|
|
RetTy visitInsertValueInst(InsertValueInst &I) { DELEGATE(Instruction); }
|
|
|
|
RetTy visitLandingPadInst(LandingPadInst &I) { DELEGATE(Instruction); }
|
2002-03-18 19:07:42 +00:00
|
|
|
|
2012-07-30 23:45:06 +00:00
|
|
|
// Handle the special instrinsic instruction classes.
|
|
|
|
RetTy visitDbgDeclareInst(DbgDeclareInst &I) { DELEGATE(DbgInfoIntrinsic);}
|
|
|
|
RetTy visitDbgValueInst(DbgValueInst &I) { DELEGATE(DbgInfoIntrinsic);}
|
|
|
|
RetTy visitDbgInfoIntrinsic(DbgInfoIntrinsic &I) { DELEGATE(IntrinsicInst); }
|
|
|
|
RetTy visitMemSetInst(MemSetInst &I) { DELEGATE(MemIntrinsic); }
|
|
|
|
RetTy visitMemCpyInst(MemCpyInst &I) { DELEGATE(MemTransferInst); }
|
|
|
|
RetTy visitMemMoveInst(MemMoveInst &I) { DELEGATE(MemTransferInst); }
|
|
|
|
RetTy visitMemTransferInst(MemTransferInst &I) { DELEGATE(MemIntrinsic); }
|
|
|
|
RetTy visitMemIntrinsic(MemIntrinsic &I) { DELEGATE(IntrinsicInst); }
|
2012-10-29 09:39:03 +00:00
|
|
|
RetTy visitVAStartInst(VAStartInst &I) { DELEGATE(IntrinsicInst); }
|
|
|
|
RetTy visitVAEndInst(VAEndInst &I) { DELEGATE(IntrinsicInst); }
|
|
|
|
RetTy visitVACopyInst(VACopyInst &I) { DELEGATE(IntrinsicInst); }
|
2012-07-30 23:45:06 +00:00
|
|
|
RetTy visitIntrinsicInst(IntrinsicInst &I) { DELEGATE(CallInst); }
|
|
|
|
|
2012-03-31 11:31:24 +00:00
|
|
|
// Call and Invoke are slightly different as they delegate first through
|
|
|
|
// a generic CallSite visitor.
|
|
|
|
RetTy visitCallInst(CallInst &I) {
|
|
|
|
return static_cast<SubClass*>(this)->visitCallSite(&I);
|
|
|
|
}
|
|
|
|
RetTy visitInvokeInst(InvokeInst &I) {
|
|
|
|
return static_cast<SubClass*>(this)->visitCallSite(&I);
|
|
|
|
}
|
|
|
|
|
2009-07-15 22:43:51 +00:00
|
|
|
// Next level propagators: If the user does not overload a specific
|
2002-03-18 19:07:42 +00:00
|
|
|
// instruction type, they can overload one of these to get the whole class
|
|
|
|
// of instructions...
|
|
|
|
//
|
2012-03-18 23:45:14 +00:00
|
|
|
RetTy visitCastInst(CastInst &I) { DELEGATE(UnaryInstruction);}
|
|
|
|
RetTy visitBinaryOperator(BinaryOperator &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitCmpInst(CmpInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitTerminatorInst(TerminatorInst &I) { DELEGATE(Instruction);}
|
|
|
|
RetTy visitUnaryInstruction(UnaryInstruction &I){ DELEGATE(Instruction);}
|
2002-03-18 19:07:42 +00:00
|
|
|
|
2012-03-31 11:31:24 +00:00
|
|
|
// Provide a special visitor for a 'callsite' that visits both calls and
|
|
|
|
// invokes. When unimplemented, properly delegates to either the terminator or
|
|
|
|
// regular instruction visitor.
|
|
|
|
RetTy visitCallSite(CallSite CS) {
|
|
|
|
assert(CS);
|
|
|
|
Instruction &I = *CS.getInstruction();
|
|
|
|
if (CS.isCall())
|
|
|
|
DELEGATE(Instruction);
|
|
|
|
|
|
|
|
assert(CS.isInvoke());
|
|
|
|
DELEGATE(TerminatorInst);
|
|
|
|
}
|
|
|
|
|
2002-03-18 19:07:42 +00:00
|
|
|
// If the user wants a 'default' case, they can choose to override this
|
2009-07-15 22:43:51 +00:00
|
|
|
// function. If this function is not overloaded in the user's subclass, then
|
2002-03-18 19:07:42 +00:00
|
|
|
// this instruction just gets ignored.
|
|
|
|
//
|
2002-04-18 15:46:40 +00:00
|
|
|
// Note that you MUST override this function if your return type is not void.
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
void visitInstruction(Instruction &I) {} // Ignore unhandled instructions
|
2012-07-30 23:45:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Special helper function to delegate to CallInst subclass visitors.
|
|
|
|
RetTy delegateCallInst(CallInst &I) {
|
|
|
|
if (const Function *F = I.getCalledFunction()) {
|
|
|
|
switch ((Intrinsic::ID)F->getIntrinsicID()) {
|
|
|
|
default: DELEGATE(IntrinsicInst);
|
|
|
|
case Intrinsic::dbg_declare: DELEGATE(DbgDeclareInst);
|
|
|
|
case Intrinsic::dbg_value: DELEGATE(DbgValueInst);
|
|
|
|
case Intrinsic::memcpy: DELEGATE(MemCpyInst);
|
|
|
|
case Intrinsic::memmove: DELEGATE(MemMoveInst);
|
|
|
|
case Intrinsic::memset: DELEGATE(MemSetInst);
|
2012-10-29 09:39:03 +00:00
|
|
|
case Intrinsic::vastart: DELEGATE(VAStartInst);
|
|
|
|
case Intrinsic::vaend: DELEGATE(VAEndInst);
|
|
|
|
case Intrinsic::vacopy: DELEGATE(VACopyInst);
|
2012-07-30 23:45:06 +00:00
|
|
|
case Intrinsic::not_intrinsic: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DELEGATE(CallInst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// An overload that will never actually be called, it is used only from dead
|
|
|
|
// code in the dispatching from opcodes to instruction subclasses.
|
|
|
|
RetTy delegateCallInst(Instruction &I) {
|
|
|
|
llvm_unreachable("delegateCallInst called for non-CallInst");
|
|
|
|
}
|
2002-03-18 19:07:42 +00:00
|
|
|
};
|
|
|
|
|
2002-04-18 16:16:16 +00:00
|
|
|
#undef DELEGATE
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-03-18 19:07:42 +00:00
|
|
|
#endif
|