From 0a11af1f73916b53cd4eb4ebac37527facd9cf3b Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Wed, 17 Jan 2007 02:46:11 +0000 Subject: [PATCH] For PR1117: Expose the previously hidden checkCast function as CastInst::castIsValid. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33282 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/InstrTypes.h | 6 ++++ lib/VMCore/Instructions.cpp | 58 ++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/include/llvm/InstrTypes.h b/include/llvm/InstrTypes.h index 50c1e7ab585..348671a527a 100644 --- a/include/llvm/InstrTypes.h +++ b/include/llvm/InstrTypes.h @@ -459,6 +459,12 @@ public: /// @brief Return the destination type, as a convenience const Type* getDestTy() const { return getType(); } + /// This method can be used to determine if a cast from S to DstTy using + /// Opcode op is valid or not. + /// @returns true iff the proposed cast is valid. + /// @brief Determine if a cast is valid without creating one. + static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy); + /// @brief Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const CastInst *) { return true; } static inline bool classof(const Instruction *I) { diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 5efe408ee5c..f98f7cb85a8 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -1603,9 +1603,9 @@ CastInst *CastInst::createFPCast(Value *C, const Type *Ty, // Provide a way to get a "cast" where the cast opcode is inferred from the // types and size of the operand. This, basically, is a parallel of the -// logic in the checkCast function below. This axiom should hold: -// checkCast( getCastOpcode(Val, Ty), Val, Ty) -// should not assert in checkCast. In other words, this produces a "correct" +// logic in the castIsValid function below. This axiom should hold: +// castIsValid( getCastOpcode(Val, Ty), Val, Ty) +// should not assert in castIsValid. In other words, this produces a "correct" // casting opcode for the arguments passed to it. Instruction::CastOps CastInst::getCastOpcode( @@ -1699,8 +1699,8 @@ CastInst::getCastOpcode( /// could be broken out into the separate constructors but it is useful to have /// it in one place and to eliminate the redundant code for getting the sizes /// of the types involved. -static bool -checkCast(Instruction::CastOps op, Value *S, const Type *DstTy) { +bool +CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { // Check for type sanity on the arguments const Type *SrcTy = S->getType(); @@ -1754,144 +1754,144 @@ checkCast(Instruction::CastOps op, Value *S, const Type *DstTy) { TruncInst::TruncInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal Trunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); } TruncInst::TruncInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal Trunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); } ZExtInst::ZExtInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal ZExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); } ZExtInst::ZExtInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal ZExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); } SExtInst::SExtInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, SExt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); } SExtInst::SExtInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); } FPTruncInst::FPTruncInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPTrunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); } FPTruncInst::FPTruncInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPTrunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); } FPExtInst::FPExtInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); } FPExtInst::FPExtInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); } UIToFPInst::UIToFPInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal UIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); } UIToFPInst::UIToFPInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal UIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); } SIToFPInst::SIToFPInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); } SIToFPInst::SIToFPInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); } FPToUIInst::FPToUIInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToUI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); } FPToUIInst::FPToUIInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToUI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); } FPToSIInst::FPToSIInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToSI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); } FPToSIInst::FPToSIInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToSI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); } PtrToIntInst::PtrToIntInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal PtrToInt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); } PtrToIntInst::PtrToIntInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal PtrToInt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); } IntToPtrInst::IntToPtrInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal IntToPtr"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); } IntToPtrInst::IntToPtrInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal IntToPtr"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); } BitCastInst::BitCastInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal BitCast"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); } BitCastInst::BitCastInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal BitCast"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); } //===----------------------------------------------------------------------===//