diff options
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/AsmWriter.cpp | 3 | ||||
-rw-r--r-- | lib/VMCore/AutoUpgrade.cpp | 8 | ||||
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 298 | ||||
-rw-r--r-- | lib/VMCore/ConstantFold.h | 6 | ||||
-rw-r--r-- | lib/VMCore/ConstantFolding.h | 6 | ||||
-rw-r--r-- | lib/VMCore/Constants.cpp | 215 | ||||
-rw-r--r-- | lib/VMCore/Function.cpp | 4 | ||||
-rw-r--r-- | lib/VMCore/Instruction.cpp | 33 | ||||
-rw-r--r-- | lib/VMCore/Instructions.cpp | 601 | ||||
-rw-r--r-- | lib/VMCore/IntrinsicInst.cpp | 2 | ||||
-rw-r--r-- | lib/VMCore/Type.cpp | 34 | ||||
-rw-r--r-- | lib/VMCore/Verifier.cpp | 175 |
12 files changed, 1195 insertions, 190 deletions
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index 4f2cbc4..8ff55b6 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -541,10 +541,11 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV, Out << ", "; } - if (CE->getOpcode() == Instruction::Cast) { + if (CE->isCast()) { Out << " to "; printTypeInt(Out, CE->getType(), TypeTable); } + Out << ')'; } else { diff --git a/lib/VMCore/AutoUpgrade.cpp b/lib/VMCore/AutoUpgrade.cpp index 1529d1b..57a09e2 100644 --- a/lib/VMCore/AutoUpgrade.cpp +++ b/lib/VMCore/AutoUpgrade.cpp @@ -206,8 +206,8 @@ static Value *CastArg(Value *Arg, const Type *Ty, Instruction *InsertBefore) { if (Constant *C = dyn_cast<Constant>(Arg)) { return ConstantExpr::getCast(C, Ty); } else { - Value *Cast = new CastInst(Arg, Ty, "autoupgrade_cast", InsertBefore); - return Cast; + return CastInst::createInferredCast(Arg, Ty, "autoupgrade_cast", + InsertBefore); } } @@ -261,8 +261,8 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { Instruction *RetVal = NewCI; if (F->getReturnType() != NewFn->getReturnType()) { - RetVal = new CastInst(NewCI, F->getReturnType(), - NewCI->getName(), CI); + RetVal = + new BitCastInst(NewCI, F->getReturnType(), NewCI->getName(), CI); NewCI->moveBefore(RetVal); } diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 64dd1b1..9974071 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -507,7 +507,7 @@ struct VISIBILITY_HIDDEN DirectIntRules // Casting operators. ick #define DEF_CAST(TYPE, CLASS, CTYPE) \ static Constant *CastTo##TYPE (const ConstantInt *V) { \ - return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getZExtValue()); \ + return CLASS::get(Type::TYPE##Ty, (CTYPE)((BuiltinType)V->getZExtValue()));\ } DEF_CAST(Bool , ConstantBool, bool) @@ -721,15 +721,6 @@ ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) { //===----------------------------------------------------------------------===// // ConstantFold*Instruction Implementations //===----------------------------------------------------------------------===// -// -// These methods contain the special case hackery required to symbolically -// evaluate some constant expression cases, and use the ConstantRules class to -// evaluate normal constants. -// -static unsigned getSize(const Type *Ty) { - unsigned S = Ty->getPrimitiveSize(); - return S ? S : 8; // Treat pointers at 8 bytes -} /// CastConstantPacked - Convert the specified ConstantPacked node to the /// specified packed type. At this point, we know that the elements of the @@ -746,17 +737,20 @@ static Constant *CastConstantPacked(ConstantPacked *CP, if (SrcNumElts == DstNumElts) { std::vector<Constant*> Result; - // If the src and dest elements are both integers, just cast each one - // which will do the appropriate bit-convert. - if (SrcEltTy->isIntegral() && DstEltTy->isIntegral()) { + // If the src and dest elements are both integers, or both floats, we can + // just BitCast each element because the elements are the same size. + if ((SrcEltTy->isIntegral() && DstEltTy->isIntegral()) || + (SrcEltTy->isFloatingPoint() && DstEltTy->isFloatingPoint())) { for (unsigned i = 0; i != SrcNumElts; ++i) - Result.push_back(ConstantExpr::getCast(CP->getOperand(i), - DstEltTy)); + Result.push_back( + ConstantExpr::getCast(Instruction::BitCast, CP->getOperand(1), + DstEltTy)); return ConstantPacked::get(Result); } + // If this is an int-to-fp cast .. if (SrcEltTy->isIntegral()) { - // Otherwise, this is an int-to-fp cast. + // Ensure that it is int-to-fp cast assert(DstEltTy->isFloatingPoint()); if (DstEltTy->getTypeID() == Type::DoubleTyID) { for (unsigned i = 0; i != SrcNumElts; ++i) { @@ -805,34 +799,50 @@ static Constant *CastConstantPacked(ConstantPacked *CP, return 0; } +/// This function determines which opcode to use to fold two constant cast +/// expressions together. It uses CastInst::isEliminableCastPair to determine +/// the opcode. Consequently its just a wrapper around that function. +/// @Determine if it is valid to fold a cast of a cast +static unsigned +foldConstantCastPair( + unsigned opc, ///< opcode of the second cast constant expression + const ConstantExpr*Op, ///< the first cast constant expression + const Type *DstTy ///< desintation type of the first cast +) { + assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); + assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); + assert(CastInst::isCast(opc) && "Invalid cast opcode"); + + // The the types and opcodes for the two Cast constant expressions + const Type *SrcTy = Op->getOperand(0)->getType(); + const Type *MidTy = Op->getType(); + Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); + Instruction::CastOps secondOp = Instruction::CastOps(opc); + + // Let CastInst::isEliminableCastPair do the heavy lifting. + return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, + Type::ULongTy); +} -Constant *llvm::ConstantFoldCastInstruction(const Constant *V, +Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, const Type *DestTy) { - if (V->getType() == DestTy) return (Constant*)V; - - // Cast of a global address to boolean is always true. - if (isa<GlobalValue>(V)) { - if (DestTy == Type::BoolTy) - // FIXME: When we support 'external weak' references, we have to prevent - // this transformation from happening. This code will need to be updated - // to ignore external weak symbols when we support it. - return ConstantBool::getTrue(); - } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { - if (CE->getOpcode() == Instruction::Cast) { - Constant *Op = const_cast<Constant*>(CE->getOperand(0)); - // Try to not produce a cast of a cast, which is almost always redundant. - if (!Op->getType()->isFloatingPoint() && - !CE->getType()->isFloatingPoint() && - !DestTy->isFloatingPoint()) { - unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType()); - unsigned S3 = getSize(DestTy); - if (Op->getType() == DestTy && S3 >= S2) - return Op; - if (S1 >= S2 && S2 >= S3) - return ConstantExpr::getCast(Op, DestTy); - if (S1 <= S2 && S2 >= S3 && S1 <= S3) - return ConstantExpr::getCast(Op, DestTy); - } + const Type *SrcTy = V->getType(); + + // Handle some simple cases + if (SrcTy == DestTy) + return (Constant*)V; // no-op cast + + if (isa<UndefValue>(V)) + return UndefValue::get(DestTy); + + // If the cast operand is a constant expression, there's a few things we can + // do to try to simplify it. + if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { + if (CE->isCast()) { + // Try hard to fold cast of cast because they are almost always + // eliminable. + if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) + return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); } else if (CE->getOpcode() == Instruction::GetElementPtr) { // If all of the indexes in the GEP are null values, there is no pointer // adjustment going on. We might as well cast the source pointer. @@ -845,69 +855,132 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V, if (isAllNull) return ConstantExpr::getCast(CE->getOperand(0), DestTy); } - } else if (isa<UndefValue>(V)) { - return UndefValue::get(DestTy); } - // Check to see if we are casting an pointer to an aggregate to a pointer to - // the first element. If so, return the appropriate GEP instruction. - if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) - if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) { - std::vector<Value*> IdxList; - IdxList.push_back(Constant::getNullValue(Type::IntTy)); - const Type *ElTy = PTy->getElementType(); - while (ElTy != DPTy->getElementType()) { - if (const StructType *STy = dyn_cast<StructType>(ElTy)) { - if (STy->getNumElements() == 0) break; - ElTy = STy->getElementType(0); - IdxList.push_back(Constant::getNullValue(Type::UIntTy)); - } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) { - if (isa<PointerType>(ElTy)) break; // Can't index into pointers! - ElTy = STy->getElementType(); - IdxList.push_back(IdxList[0]); - } else { - break; - } - } + // We actually have to do a cast now, but first, we might need to fix up + // the value of the operand. + switch (opc) { + case Instruction::FPTrunc: + case Instruction::Trunc: + case Instruction::FPExt: + break; // floating point input & output, no fixup needed + case Instruction::FPToUI: { + ConstRules &Rules = ConstRules::get(V, V); + V = Rules.castToULong(V); // make sure we get an unsigned value first + break; + } + case Instruction::FPToSI: { + ConstRules &Rules = ConstRules::get(V, V); + V = Rules.castToLong(V); // make sure we get a signed value first + break; + } + case Instruction::IntToPtr: //always treated as unsigned + case Instruction::UIToFP: + case Instruction::ZExt: + // A ZExt always produces an unsigned value so we need to cast the value + // now before we try to cast it to the destination type + if (isa<ConstantInt>(V)) + V = ConstantInt::get(SrcTy->getUnsignedVersion(), + cast<ConstantIntegral>(V)->getZExtValue()); + break; + case Instruction::SIToFP: + case Instruction::SExt: + // A SExt always produces a signed value so we need to cast the value + // now before we try to cast it to the destiniation type. + if (isa<ConstantInt>(V)) + V = ConstantInt::get(SrcTy->getSignedVersion(), + cast<ConstantIntegral>(V)->getSExtValue()); + break; - if (ElTy == DPTy->getElementType()) - return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList); + case Instruction::PtrToInt: + // Cast of a global address to boolean is always true. + if (isa<GlobalValue>(V)) { + if (DestTy == Type::BoolTy) + // FIXME: When we support 'external weak' references, we have to + // prevent this transformation from happening. This code will need + // to be updated to ignore external weak symbols when we support it. + return ConstantBool::getTrue(); } - - // Handle casts from one packed constant to another. We know that the src and - // dest type have the same size. - if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) { - if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) { - assert(DestPTy->getElementType()->getPrimitiveSizeInBits() * - DestPTy->getNumElements() == - SrcTy->getElementType()->getPrimitiveSizeInBits() * - SrcTy->getNumElements() && "Not cast between same sized vectors!"); - if (isa<ConstantAggregateZero>(V)) - return Constant::getNullValue(DestTy); - if (isa<UndefValue>(V)) - return UndefValue::get(DestTy); - if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) { - // This is a cast from a ConstantPacked of one type to a ConstantPacked - // of another type. Check to see if all elements of the input are - // simple. - bool AllSimpleConstants = true; - for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { - if (!isa<ConstantInt>(CP->getOperand(i)) && - !isa<ConstantFP>(CP->getOperand(i))) { - AllSimpleConstants = false; + break; + case Instruction::BitCast: + // Check to see if we are casting a pointer to an aggregate to a pointer to + // the first element. If so, return the appropriate GEP instruction. + if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) + if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) { + std::vector<Value*> IdxList; + IdxList.push_back(Constant::getNullValue(Type::IntTy)); + const Type *ElTy = PTy->getElementType(); + while (ElTy != DPTy->getElementType()) { + if (const StructType *STy = dyn_cast<StructType>(ElTy)) { + if (STy->getNumElements() == 0) break; + ElTy = STy->getElementType(0); + IdxList.push_back(Constant::getNullValue(Type::UIntTy)); + } else if (const SequentialType *STy = + dyn_cast<SequentialType>(ElTy)) { + if (isa<PointerType>(ElTy)) break; // Can't index into pointers! + ElTy = STy->getElementType(); + IdxList.push_back(IdxList[0]); + } else { break; } } - - // If all of the elements are simple constants, we can fold this. - if (AllSimpleConstants) - return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy); + + if (ElTy == DPTy->getElementType()) + return ConstantExpr::getGetElementPtr( + const_cast<Constant*>(V),IdxList); + } + + // Handle casts from one packed constant to another. We know that the src + // and dest type have the same size (otherwise its an illegal cast). + if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) { + if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) { + assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() && + "Not cast between same sized vectors!"); + // First, check for null and undef + if (isa<ConstantAggregateZero>(V)) + return Constant::getNullValue(DestTy); + if (isa<UndefValue>(V)) + return UndefValue::get(DestTy); + + if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) { + // This is a cast from a ConstantPacked of one type to a + // ConstantPacked of another type. Check to see if all elements of + // the input are simple. + bool AllSimpleConstants = true; + for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { + if (!isa<ConstantInt>(CP->getOperand(i)) && + !isa<ConstantFP>(CP->getOperand(i))) { + AllSimpleConstants = false; + break; + } + } + + // If all of the elements are simple constants, we can fold this. + if (AllSimpleConstants) + return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy); + } } } + + // Handle sign conversion for integer no-op casts. We need to cast the + // value to the correct signedness before we try to cast it to the + // destination type. Be careful to do this only for integer types. + if (isa<ConstantIntegral>(V) && SrcTy->isInteger()) { + if (SrcTy->isSigned()) + V = ConstantInt::get(SrcTy->getUnsignedVersion(), + cast<ConstantIntegral>(V)->getZExtValue()); + else + V = ConstantInt::get(SrcTy->getSignedVersion(), + cast<ConstantIntegral>(V)->getSExtValue()); + } + break; + default: + assert(!"Invalid CE CastInst opcode"); + break; } + // Okay, no more folding possible, time to cast ConstRules &Rules = ConstRules::get(V, V); - switch (DestTy->getTypeID()) { case Type::BoolTyID: return Rules.castToBool(V); case Type::UByteTyID: return Rules.castToUByte(V); @@ -922,6 +995,7 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V, case Type::DoubleTyID: return Rules.castToDouble(V); case Type::PointerTyID: return Rules.castToPointer(V, cast<PointerType>(DestTy)); + // what about packed ? default: return 0; } } @@ -1049,15 +1123,22 @@ static bool isMaybeZeroSizedType(const Type *Ty) { static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) { if (C1 == C2) return 0; - // Ok, we found a different index. Are either of the operands - // ConstantExprs? If so, we can't do anything with them. + // Ok, we found a different index. Are either of the operands ConstantExprs? + // If so, we can't do anything with them. if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) return -2; // don't know! // Ok, we have two differing integer indices. Sign extend them to be the same // type. Long is always big enough, so we use it. - C1 = ConstantExpr::getSignExtend(C1, Type::LongTy); - C2 = ConstantExpr::getSignExtend(C2, Type::LongTy); + if (C1->getType() != Type::LongTy && C1->getType() != Type::ULongTy) + C1 = ConstantExpr::getSignExtend(C1, Type::LongTy); + else + C1 = ConstantExpr::getBitCast(C1, Type::LongTy); + if (C2->getType() != Type::LongTy && C1->getType() != Type::ULongTy) + C2 = ConstantExpr::getSignExtend(C2, Type::LongTy); + else + C2 = ConstantExpr::getBitCast(C2, Type::LongTy); + if (C1 == C2) return 0; // Are they just differing types? // If the type being indexed over is really just a zero sized type, there is @@ -1141,7 +1222,19 @@ static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) { Constant *CE1Op0 = CE1->getOperand(0); switch (CE1->getOpcode()) { - case Instruction::Cast: + case Instruction::Trunc: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::FPToUI: + case Instruction::FPToSI: + break; // We don't do anything with floating point. + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::PtrToInt: + case Instruction::IntToPtr: + case Instruction::BitCast: // If the cast is not actually changing bits, and the second operand is a // null pointer, do the comparison with the pre-casted value. if (V2->isNullValue() && @@ -1154,8 +1247,7 @@ static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) { // important for things like "seteq (cast 4 to int*), (cast 5 to int*)", // which happens a lot in compilers with tagged integers. if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) - if (isa<PointerType>(CE1->getType()) && - CE2->getOpcode() == Instruction::Cast && + if (isa<PointerType>(CE1->getType()) && CE2->isCast() && CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() && CE1->getOperand(0)->getType()->isIntegral()) { return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0)); @@ -1423,8 +1515,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, if (cast<ConstantIntegral>(V2)->isAllOnesValue()) return const_cast<Constant*>(V1); // X & -1 == X if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0 - if (CE1->getOpcode() == Instruction::Cast && - isa<GlobalValue>(CE1->getOperand(0))) { + if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) { GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0)); // Functions are at least 4-byte aligned. If and'ing the address of a @@ -1566,8 +1657,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, // long 0, long 0) // To: int* getelementptr ([3 x int]* %X, long 0, long 0) // - if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 && - Idx0->isNullValue()) + if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue()) if (const PointerType *SPT = dyn_cast<PointerType>(CE->getOperand(0)->getType())) if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) diff --git a/lib/VMCore/ConstantFold.h b/lib/VMCore/ConstantFold.h index 5119aaf..2824979 100644 --- a/lib/VMCore/ConstantFold.h +++ b/lib/VMCore/ConstantFold.h @@ -27,7 +27,11 @@ namespace llvm { class Type; // Constant fold various types of instruction... - Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy); + Constant *ConstantFoldCastInstruction( + unsigned opcode, ///< The opcode of the cast + const Constant *V, ///< The source constant + const Type *DestTy ///< The destination type + ); Constant *ConstantFoldSelectInstruction(const Constant *Cond, const Constant *V1, const Constant *V2); diff --git a/lib/VMCore/ConstantFolding.h b/lib/VMCore/ConstantFolding.h index 5119aaf..2824979 100644 --- a/lib/VMCore/ConstantFolding.h +++ b/lib/VMCore/ConstantFolding.h @@ -27,7 +27,11 @@ namespace llvm { class Type; // Constant fold various types of instruction... - Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy); + Constant *ConstantFoldCastInstruction( + unsigned opcode, ///< The opcode of the cast + const Constant *V, ///< The source constant + const Type *DestTy ///< The destination type + ); Constant *ConstantFoldSelectInstruction(const Constant *Cond, const Constant *V1, const Constant *V2); diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index d91e218..06dcbb3 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -427,6 +427,14 @@ struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr { }; } + +// Utility function for determining if a ConstantExpr is a CastOp or not. This +// can't be inline because we don't want to #include Instruction.h into +// Constant.h +bool ConstantExpr::isCast() const { + return Instruction::isCast(getOpcode()); +} + /// ConstantExpr::get* - Return some common constants without having to /// specify the full Instruction::OPCODE identifier. /// @@ -507,8 +515,8 @@ Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) { /// getWithOperandReplaced - Return a constant expression identical to this /// one, but with the specified operand set to the specified value. -Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo, - Constant *Op) const { +Constant * +ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { assert(OpNo < getNumOperands() && "Operand num is out of range!"); assert(Op->getType() == getOperand(OpNo)->getType() && "Replacing operand with value of different type!"); @@ -517,8 +525,19 @@ Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op0, *Op1, *Op2; switch (getOpcode()) { - case Instruction::Cast: - return ConstantExpr::getCast(Op, getType()); + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + case Instruction::PtrToInt: + case Instruction::IntToPtr: + case Instruction::BitCast: + return ConstantExpr::getCast(getOpcode(), Op, getType()); case Instruction::Select: Op0 = (OpNo == 0) ? Op : getOperand(0); Op1 = (OpNo == 1) ? Op : getOperand(1); @@ -571,8 +590,19 @@ getWithOperands(const std::vector<Constant*> &Ops) const { return const_cast<ConstantExpr*>(this); switch (getOpcode()) { - case Instruction::Cast: - return ConstantExpr::getCast(Ops[0], getType()); + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + case Instruction::PtrToInt: + case Instruction::IntToPtr: + case Instruction::BitCast: + return ConstantExpr::getCast(getOpcode(), Ops[0], getType()); case Instruction::Select: return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); case Instruction::InsertElement: @@ -1317,8 +1347,8 @@ namespace llvm { template<> struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) { - if (V.first == Instruction::Cast) - return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty); + if (Instruction::isCast(V.first)) + return new UnaryConstantExpr(V.first, V.second[0], Ty); if ((V.first >= Instruction::BinaryOpsBegin && V.first < Instruction::BinaryOpsEnd) || V.first == Instruction::Shl || @@ -1348,8 +1378,20 @@ namespace llvm { static void convert(ConstantExpr *OldC, const Type *NewTy) { Constant *New; switch (OldC->getOpcode()) { - case Instruction::Cast: - New = ConstantExpr::getCast(OldC->getOperand(0), NewTy); + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + case Instruction::PtrToInt: + case Instruction::IntToPtr: + case Instruction::BitCast: + New = ConstantExpr::getCast( + OldC->getOpcode(), OldC->getOperand(0), NewTy); break; case Instruction::Select: New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), @@ -1394,40 +1436,143 @@ static ExprMapKeyType getValType(ConstantExpr *CE) { static ManagedStatic<ValueMap<ExprMapKeyType, Type, ConstantExpr> > ExprConstants; -Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) { +/// This is a utility function to handle folding of casts and lookup of the +/// cast in the ExprConstants map. It is usedby the various get* methods below. +static inline Constant *getFoldedCast( + Instruction::CastOps opc, Constant *C, const Type *Ty) { assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); - - if (Constant *FC = ConstantFoldCastInstruction(C, Ty)) - return FC; // Fold a few common cases... + // Fold a few common cases + if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) + return FC; // Look up the constant in the table first to ensure uniqueness std::vector<Constant*> argVec(1, C); - ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec); + ExprMapKeyType Key = std::make_pair(opc, argVec); return ExprConstants->getOrCreate(Ty, Key); } -Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) { - assert(C->getType()->isIntegral() && Ty->isIntegral() && - C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && - "This is an illegal sign extension!"); - if (C->getType() != Type::BoolTy) { - C = ConstantExpr::getCast(C, C->getType()->getSignedVersion()); - return ConstantExpr::getCast(C, Ty); - } else { - if (C == ConstantBool::getTrue()) - return ConstantIntegral::getAllOnesValue(Ty); - else - return ConstantIntegral::getNullValue(Ty); +Constant *ConstantExpr::getCast( Constant *C, const Type *Ty ) { + // Note: we can't inline this because it requires the Instructions.h header + return getCast(CastInst::getCastOpcode(C, Ty), C, Ty); +} + +Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { + Instruction::CastOps opc = Instruction::CastOps(oc); + assert(Instruction::isCast(opc) && "opcode out of range"); + assert(C && Ty && "Null arguments to getCast"); + assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); + + switch (opc) { + default: + assert(0 && "Invalid cast opcode"); + break; + case Instruction::Trunc: return getTrunc(C, Ty); + case Instruction::ZExt: return getZeroExtend(C, Ty); + case Instruction::SExt: return getSignExtend(C, Ty); + case Instruction::FPTrunc: return getFPTrunc(C, Ty); + case Instruction::FPExt: return getFPExtend(C, Ty); + case Instruction::UIToFP: return getUIToFP(C, Ty); + case Instruction::SIToFP: return getSIToFP(C, Ty); + case Instruction::FPToUI: return getFPToUI(C, Ty); + case Instruction::FPToSI: return getFPToSI(C, Ty); + case Instruction::PtrToInt: return getPtrToInt(C, Ty); + case Instruction::IntToPtr: return getIntToPtr(C, Ty); + case Instruction::BitCast: return getBitCast(C, Ty); } + return 0; +} + +Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) { + assert(C->getType()->isInteger() && "Trunc operand must be integer"); + assert(Ty->isIntegral() && "Trunc produces only integral"); + assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&& + "SrcTy must be larger than DestTy for Trunc!"); + + return getFoldedCast(Instruction::Trunc, C, Ty); +} + +Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) { + assert(C->getType()->isIntegral() && "SEXt operand must be integral"); + assert(Ty->isInteger() && "SExt produces only integer"); + assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& + "SrcTy must be smaller than DestTy for SExt!"); + + return getFoldedCast(Instruction::SExt, C, Ty); } Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) { - assert(C->getType()->isIntegral() && Ty->isIntegral() && - C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && - "This is an illegal zero extension!"); - if (C->getType() != Type::BoolTy) - C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion()); - return ConstantExpr::getCast(C, Ty); + assert(C->getType()->isIntegral() && "ZEXt operand must be integral"); + assert(Ty->isInteger() && "ZExt produces only integer"); + assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& + "SrcTy must be smaller than DestTy for ZExt!"); + + return getFoldedCast(Instruction::ZExt, C, Ty); +} + +Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) { + assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && + C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&& + "This is an illegal floating point truncation!"); + return getFoldedCast(Instruction::FPTrunc, C, Ty); +} + +Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) { + assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && + C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&& + "This is an illegal floating point extension!"); + return getFoldedCast(Instruction::FPExt, C, Ty); +} + +Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) { + assert(C->getType()->isIntegral() && Ty->isFloatingPoint() && + "This is an illegal uint to floating point cast!"); + return getFoldedCast(Instruction::UIToFP, C, Ty); +} + +Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) { + assert(C->getType()->isIntegral() && Ty->isFloatingPoint() && + "This is an illegal sint to floating point cast!"); + return getFoldedCast(Instruction::SIToFP, C, Ty); +} + +Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) { + assert(C->getType()->isFloatingPoint() && Ty->isIntegral() && + "This is an illegal floating point to uint cast!"); + return getFoldedCast(Instruction::FPToUI, C, Ty); +} + +Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) { + assert(C->getType()->isFloatingPoint() && Ty->isIntegral() && + "This is an illegal floating point to sint cast!"); + return getFoldedCast(Instruction::FPToSI, C, Ty); +} + +Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) { + assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer"); + assert(DstTy->isIntegral() && "PtrToInt destination must be integral"); + return getFoldedCast(Instruction::PtrToInt, C, DstTy); +} + +Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) { + assert(C->getType()->isIntegral() && "IntToPtr source must be integral"); + assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer"); + return getFoldedCast(Instruction::IntToPtr, C, DstTy); +} + +Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) { + // BitCast implies a no-op cast of type only. No bits change. However, you + // can't cast pointers to anything but pointers. + const Type *SrcTy = C->getType(); + assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) && + "Bitcast cannot cast pointer to non-pointer and vice versa"); + + // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr + // or nonptr->ptr). For all the other types, the cast is okay if source and + // destination bit widths are identical. + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); + assert(SrcBitSize == DstBitSize && "Bitcast requies types of same width"); + return getFoldedCast(Instruction::BitCast, C, DstTy); } Constant *ConstantExpr::getSizeOf(const Type *Ty) { @@ -1858,9 +2003,9 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, Indices.push_back(Val); } Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); - } else if (getOpcode() == Instruction::Cast) { + } else if (isCast()) { assert(getOperand(0) == From && "Cast only has one use!"); - Replacement = ConstantExpr::getCast(To, getType()); + Replacement = ConstantExpr::getCast(getOpcode(), To, getType()); } else if (getOpcode() == Instruction::Select) { Constant *C1 = getOperand(0); Constant *C2 = getOperand(1); diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp index 641cb9f..7a44ec0 100644 --- a/lib/VMCore/Function.cpp +++ b/lib/VMCore/Function.cpp @@ -226,7 +226,7 @@ const char *Intrinsic::getName(ID id) { Value *IntrinsicInst::StripPointerCasts(Value *Ptr) { if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { - if (CE->getOpcode() == Instruction::Cast) { + if (CE->getOpcode() == Instruction::BitCast) { if (isa<PointerType>(CE->getOperand(0)->getType())) return StripPointerCasts(CE->getOperand(0)); } else if (CE->getOpcode() == Instruction::GetElementPtr) { @@ -238,7 +238,7 @@ Value *IntrinsicInst::StripPointerCasts(Value *Ptr) { return Ptr; } - if (CastInst *CI = dyn_cast<CastInst>(Ptr)) { + if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) { if (isa<PointerType>(CI->getOperand(0)->getType())) return StripPointerCasts(CI->getOperand(0)); } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { diff --git a/lib/VMCore/Instruction.cpp b/lib/VMCore/Instruction.cpp index 64a9e12..5c741f7 100644 --- a/lib/VMCore/Instruction.cpp +++ b/lib/VMCore/Instruction.cpp @@ -122,18 +122,31 @@ const char *Instruction::getOpcodeName(unsigned OpCode) { case Store: return "store"; case GetElementPtr: return "getelementptr"; + // Convert instructions... + case Trunc: return "trunc"; + case ZExt: return "zext"; + case SExt: return "sext"; + case FPTrunc: return "fptrunc"; + case FPExt: return "fpext"; + case FPToUI: return "fptoui"; + case FPToSI: return "fptosi"; + case UIToFP: return "uitofp"; + case SIToFP: return "sitofp"; + case IntToPtr: return "inttoptr"; + case PtrToInt: return "ptrtoint"; + case BitCast: return "bitcast"; + // Other instructions... - case PHI: return "phi"; - case Cast: return "cast"; - case Select: return "select"; - case Call: return "call"; - case Shl: return "shl"; - case LShr: return "lshr"; - case AShr: return "ashr"; - case VAArg: return "va_arg"; + case PHI: return "phi"; + case Select: return "select"; + case Call: return "call"; + case Shl: return "shl"; + case LShr: return "lshr"; + case AShr: return "ashr"; + case VAArg: return "va_arg"; case ExtractElement: return "extractelement"; - case InsertElement: return "insertelement"; - case ShuffleVector: return "shufflevector"; + case InsertElement: return "insertelement"; + case ShuffleVector: return "shufflevector"; default: return "<Invalid operator> "; } diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index f559c63..d421adb 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -1226,18 +1226,571 @@ bool BinaryOperator::swapOperands() { // CastInst Class //===----------------------------------------------------------------------===// -/// isTruncIntCast - Return true if this is a truncating integer cast -/// instruction, e.g. a cast from long to uint. -bool CastInst::isTruncIntCast() const { - // The dest type has to be integral, the input has to be integer. - if (!getType()->isIntegral() || !getOperand(0)->getType()->isInteger()) +// Just determine if this cast only deals with integral->integral conversion. +bool CastInst::isIntegerCast() const { + switch (getOpcode()) { + default: return false; + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::Trunc: + return true; + case Instruction::BitCast: + return getOperand(0)->getType()->isIntegral() && getType()->isIntegral(); + } +} + +bool CastInst::isLosslessCast() const { + // Only BitCast can be lossless, exit fast if we're not BitCast + if (getOpcode() != Instruction::BitCast) return false; - // Has to be large to smaller. - return getOperand(0)->getType()->getPrimitiveSizeInBits() > - getType()->getPrimitiveSizeInBits(); + // Identity cast is always lossless + const Type* SrcTy = getOperand(0)->getType(); + const Type* DstTy = getType(); + if (SrcTy == DstTy) + return true; + + // The remaining possibilities are lossless if the typeID of the source type + // matches the type ID of the destination in size and fundamental type. This + // prevents things like int -> ptr, int -> float, packed -> int, mismatched + // packed types of the same size, and etc. + switch (SrcTy->getTypeID()) { + case Type::UByteTyID: return DstTy == Type::SByteTy; + case Type::SByteTyID: return DstTy == Type::UByteTy; + case Type::UShortTyID: return DstTy == Type::ShortTy; + case Type::ShortTyID: return DstTy == Type::UShortTy; + case Type::UIntTyID: return DstTy == Type::IntTy; + case Type::IntTyID: return DstTy == Type::UIntTy; + case Type::ULongTyID: return DstTy == Type::LongTy; + case Type::LongTyID: return DstTy == Type::ULongTy; + case Type::PointerTyID: return isa<PointerType>(DstTy); + default: + break; + } + return false; // Other types have no identity values +} + +/// This function determines if the CastInst does not require any bits to be +/// changed in order to effect the cast. Essentially, it identifies cases where +/// no code gen is necessary for the cast, hence the name no-op cast. For +/// example, the following are all no-op casts: +/// # bitcast uint %X, int +/// # bitcast uint* %x, sbyte* +/// # bitcast packed< 2 x int > %x, packed< 4 x short> +/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only +/// @brief Determine if a cast is a no-op. +bool CastInst::isNoopCast(const Type *IntPtrTy) const { + switch (getOpcode()) { + default: + assert(!"Invalid CastOp"); + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + return false; // These always modify bits + case Instruction::BitCast: + return true; // BitCast never modifies bits. + case Instruction::PtrToInt: + return IntPtrTy->getPrimitiveSizeInBits() == + getType()->getPrimitiveSizeInBits(); + case Instruction::IntToPtr: + return IntPtrTy->getPrimitiveSizeInBits() == + getOperand(0)->getType()->getPrimitiveSizeInBits(); + } +} + +/// This function determines if a pair of casts can be eliminated and what +/// opcode should be used in the elimination. This assumes that there are two +/// instructions like this: +/// * %F = firstOpcode SrcTy %x to MidTy +/// * %S = secondOpcode MidTy %F to DstTy +/// The function returns a resultOpcode so these two casts can be replaced with: +/// * %Replacement = resultOpcode %SrcTy %x to DstTy +/// If no such cast is permited, the function returns 0. +unsigned CastInst::isEliminableCastPair( + Instruction::CastOps firstOp, Instruction::CastOps secondOp, + const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) +{ + // Define the 144 possibilities for these two cast instructions. The values + // in this matrix determine what to do in a given situation and select the + // case in the switch below. The rows correspond to firstOp, the columns + // correspond to secondOp. In looking at the table below, keep in mind + // the following cast properties: + // + // Size Compare Source Destination + // Operator Src ? Size Type Sign Type Sign + // -------- ------------ ------------------- --------------------- + // TRUNC > Integer Any Integral Any + // ZEXT < Integral Unsigned Integer Any + // SEXT < Integral Signed Integer Any + // FPTOUI n/a FloatPt n/a Integral Unsigned + // FPTOSI n/a FloatPt n/a Integral Signed + // UITOFP n/a Integral Unsigned FloatPt n/a + // SITOFP n/a Integral Signed FloatPt n/a + // FPTRUNC > FloatPt n/a FloatPt n/a + // FPEXT < FloatPt n/a FloatPt n/a + // PTRTOINT n/a Pointer n/a Integral Unsigned + // INTTOPTR n/a Integral Unsigned Pointer n/a + // BITCONVERT = FirstClass n/a FirstClass n/a + // + const unsigned numCastOps = + Instruction::CastOpsEnd - Instruction::CastOpsBegin; + static const uint8_t CastResults[numCastOps][numCastOps] = { + // T F F U S F F P I B -+ + // R Z S P P I I T P 2 N T | + // U E E 2 2 2 2 R E I T C +- secondOp + // N X X U S F F N X N 2 V | + // C T T I I P P C T T P T -+ + { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ + { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | + { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | + { 0, 1, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | + { 0, 0, 1,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | + { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp + { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | + { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | + { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | + { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | + { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | + { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ + }; + + int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] + [secondOp-Instruction::CastOpsBegin]; + switch (ElimCase) { + case 0: + // categorically disallowed + return 0; + case 1: + // allowed, use first cast's opcode + return firstOp; + case 2: + // allowed, use second cast's opcode + return secondOp; + case 3: + // no-op cast in second op implies firstOp as long as the DestTy + // is integer + if (DstTy->isInteger()) + return firstOp; + return 0; + case 4: + // no-op cast in second op implies firstOp as long as the DestTy + // is floating point + if (DstTy->isFloatingPoint()) + return firstOp; + return 0; + case 5: + // no-op cast in first op implies secondOp as long as the SrcTy + // is an integer + if (SrcTy->isInteger()) + return secondOp; + return 0; + case 6: + // no-op cast in first op implies secondOp as long as the SrcTy + // is a floating point + if (SrcTy->isFloatingPoint()) + return secondOp; + return 0; + case 7: { + // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size + unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); + unsigned MidSize = MidTy->getPrimitiveSizeInBits(); + if (MidSize >= PtrSize) + return Instruction::BitCast; + return 0; + } + case 8: { + // ext, trunc -> bitcast, if the SrcTy and DstTy are same size + // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) + // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) + unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DstSize = DstTy->getPrimitiveSizeInBits(); + if (SrcSize == DstSize) + return Instruction::BitCast; + else if (SrcSize < DstSize) + return firstOp; + return secondOp; + } + case 9: // zext, sext -> zext, because sext can't sign extend after zext + return Instruction::ZExt; + case 10: + // fpext followed by ftrunc is allowed if the bit size returned to is + // the same as the original, in which case its just a bitcast + if (SrcTy == DstTy) + return Instruction::BitCast; + return 0; // If the types are not the same we can't eliminate it. + case 11: + // bitcast followed by ptrtoint is allowed as long as the bitcast + // is a pointer to pointer cast. + if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) + return secondOp; + return 0; + case 12: + // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast + if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) + return firstOp; + return 0; + case 13: { + // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize + unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); + unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DstSize = DstTy->getPrimitiveSizeInBits(); + if (SrcSize <= PtrSize && SrcSize == DstSize) + return Instruction::BitCast; + return 0; + } + case 99: + // cast combination can't happen (error in input). This is for all cases + // where the MidTy is not the same for the two cast instructions. + assert(!"Invalid Cast Combination"); + return 0; + default: + assert(!"Error in CastResults table!!!"); + return 0; + } + return 0; +} + +CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, + const std::string &Name, Instruction *InsertBefore) { + // Construct and return the appropriate CastInst subclass + switch (op) { + case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); + case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); + case SExt: return new SExtInst (S, Ty, Name, InsertBefore); + case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); + case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); + case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); + case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); + case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); + case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); + case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); + case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); + case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); + default: + assert(!"Invalid opcode provided"); + } + return 0; } +CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, + const std::string &Name, BasicBlock *InsertAtEnd) { + // Construct and return the appropriate CastInst subclass + switch (op) { + case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); + case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); + case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); + case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); + case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); + case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); + case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); + case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); + case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); + case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); + case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); + case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); + default: + assert(!"Invalid opcode provided"); + } + return 0; +} + +// 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" +// casting opcode for the arguments passed to it. +Instruction::CastOps +CastInst::getCastOpcode(const Value *Src, const Type *DestTy) { + // Get the bit sizes, we'll need these + const Type *SrcTy = Src->getType(); + unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed + unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed + + // Run through the possibilities ... + if (DestTy->isIntegral()) { // Casting to integral + if (SrcTy->isIntegral()) { // Casting from integral + if (DestBits < SrcBits) + return Trunc; // int -> smaller int + else if (DestBits > SrcBits) { // its an extension + if (SrcTy->isSigned()) + return SExt; // signed -> SEXT + else + return ZExt; // unsigned -> ZEXT + } else { + return BitCast; // Same size, No-op cast + } + } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt + if (DestTy->isSigned()) + return FPToSI; // FP -> sint + else + return FPToUI; // FP -> uint + } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) { + assert(DestBits == PTy->getBitWidth() && + "Casting packed to integer of different width"); + return BitCast; // Same size, no-op cast + } else { + assert(isa<PointerType>(SrcTy) && + "Casting from a value that is not first-class type"); + return PtrToInt; // ptr -> int + } + } else if (DestTy->isFloatingPoint()) { // Casting to floating pt + if (SrcTy->isIntegral()) { // Casting from integral + if (SrcTy->isSigned()) + return SIToFP; // sint -> FP + else + return UIToFP; // uint -> FP + } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt + if (DestBits < SrcBits) { + return FPTrunc; // FP -> smaller FP + } else if (DestBits > SrcBits) { + return FPExt; // FP -> larger FP + } else { + return BitCast; // same size, no-op cast + } + } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) { + assert(DestBits == PTy->getBitWidth() && + "Casting packed to floating point of different width"); + return BitCast; // same size, no-op cast + } else { + assert(0 && "Casting pointer or non-first class to float"); + } + } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) { + if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) { + assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && + "Casting packed to packed of different widths"); + return BitCast; // packed -> packed + } else if (DestPTy->getBitWidth() == SrcBits) { + return BitCast; // float/int -> packed + } else { + assert(!"Illegal cast to packed (wrong type or size)"); + } + } else if (isa<PointerType>(DestTy)) { + if (isa<PointerType>(SrcTy)) { + return BitCast; // ptr -> ptr + } else if (SrcTy->isIntegral()) { + return IntToPtr; // int -> ptr + } else { + assert(!"Casting pointer to other than pointer or int"); + } + } else { + assert(!"Casting to type that is not first-class"); + } + + // If we fall through to here we probably hit an assertion cast above + // and assertions are not turned on. Anything we return is an error, so + // BitCast is as good a choice as any. + return BitCast; +} + +//===----------------------------------------------------------------------===// +// CastInst SubClass Constructors +//===----------------------------------------------------------------------===// + +/// Check that the construction parameters for a CastInst are correct. This +/// 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) { + + // Check for type sanity on the arguments + const Type *SrcTy = S->getType(); + if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) + return false; + + // Get the size of the types in bits, we'll need this later + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); + + // Switch on the opcode provided + switch (op) { + default: return false; // This is an input error + case Instruction::Trunc: + return SrcTy->isInteger() && DstTy->isIntegral() && SrcBitSize > DstBitSize; + case Instruction::ZExt: + return SrcTy->isIntegral() && DstTy->isInteger() && SrcBitSize < DstBitSize; + case Instruction::SExt: + return SrcTy->isIntegral() && DstTy->isInteger() && SrcBitSize < DstBitSize; + case Instruction::FPTrunc: + return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && + SrcBitSize > DstBitSize; + case Instruction::FPExt: + return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && + SrcBitSize < DstBitSize; + case Instruction::UIToFP: + return SrcTy->isIntegral() && DstTy->isFloatingPoint(); + case Instruction::SIToFP: + return SrcTy->isIntegral() && DstTy->isFloatingPoint(); + case Instruction::FPToUI: + return SrcTy->isFloatingPoint() && DstTy->isIntegral(); + case Instruction::FPToSI: + return SrcTy->isFloatingPoint() && DstTy->isIntegral(); + case Instruction::PtrToInt: + return isa<PointerType>(SrcTy) && DstTy->isIntegral(); + case Instruction::IntToPtr: + return SrcTy->isIntegral() && isa<PointerType>(DstTy); + case Instruction::BitCast: + // BitCast implies a no-op cast of type only. No bits change. + // However, you can't cast pointers to anything but pointers. + if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) + return false; + + // Now we know we're not dealing with a pointer/non-poiner mismatch. In all + // these cases, the cast is okay if the source and destination bit widths + // are identical. + return SrcBitSize == DstBitSize; + } +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} +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"); +} + +SExtInst::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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} + +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"); +} //===----------------------------------------------------------------------===// // SetCondInst Class @@ -1608,16 +2161,28 @@ CmpInst* CmpInst::clone() const { Ops[0], Ops[1]); } -MallocInst *MallocInst::clone() const { return new MallocInst(*this); } -AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } -FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } -LoadInst *LoadInst::clone() const { return new LoadInst(*this); } -StoreInst *StoreInst::clone() const { return new StoreInst(*this); } -CastInst *CastInst::clone() const { return new CastInst(*this); } -CallInst *CallInst::clone() const { return new CallInst(*this); } -ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } -SelectInst *SelectInst::clone() const { return new SelectInst(*this); } -VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } +MallocInst *MallocInst::clone() const { return new MallocInst(*this); } +AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } +FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } +LoadInst *LoadInst::clone() const { return new LoadInst(*this); } +StoreInst *StoreInst::clone() const { return new StoreInst(*this); } +CastInst *TruncInst::clone() const { return new TruncInst(*this); } +CastInst *ZExtInst::clone() const { return new ZExtInst(*this); } +CastInst *SExtInst::clone() const { return new SExtInst(*this); } +CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); } +CastInst *FPExtInst::clone() const { return new FPExtInst(*this); } +CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); } +CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); } +CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); } +CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } +CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } +CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } +CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } +CallInst *CallInst::clone() const { return new CallInst(*this); } +ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } +SelectInst *SelectInst::clone() const { return new SelectInst(*this); } +VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } + ExtractElementInst *ExtractElementInst::clone() const { return new ExtractElementInst(*this); } diff --git a/lib/VMCore/IntrinsicInst.cpp b/lib/VMCore/IntrinsicInst.cpp index 5c43b6f..84addcc 100644 --- a/lib/VMCore/IntrinsicInst.cpp +++ b/lib/VMCore/IntrinsicInst.cpp @@ -37,7 +37,7 @@ using namespace llvm; static Value *CastOperand(Value *C) { if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) - if (CE->getOpcode() == Instruction::Cast) + if (CE->isCast()) return CE->getOperand(0); return NULL; } diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp index fa948aa..ca8149e 100644 --- a/lib/VMCore/Type.cpp +++ b/lib/VMCore/Type.cpp @@ -98,24 +98,27 @@ bool Type::isFPOrFPVector() const { return cast<PackedType>(this)->getElementType()->isFloatingPoint(); } - -// isLosslesslyConvertibleTo - Return true if this type can be converted to +// canLosslesllyBitCastTo - Return true if this type can be converted to // 'Ty' without any reinterpretation of bits. For example, uint to int. // -bool Type::isLosslesslyConvertibleTo(const Type *Ty) const { - if (this == Ty) return true; - - // Packed type conversions are always bitwise. - if (isa<PackedType>(this) && isa<PackedType>(Ty)) +bool Type::canLosslesslyBitCastTo(const Type *Ty) const { + // Identity cast means no change so return true + if (this == Ty) return true; - if ((!isPrimitiveType() && !isa<PointerType>(this)) || - (!isa<PointerType>(Ty) && !Ty->isPrimitiveType())) return false; + // They are not convertible unless they are at least first class types + if (!this->isFirstClassType() || !Ty->isFirstClassType()) + return false; - if (getTypeID() == Ty->getTypeID()) - return true; // Handles identity cast, and cast of differing pointer types + // Packed -> Packed conversions are always lossless if the two packed types + // have the same size, otherwise not. + if (const PackedType *thisPTy = dyn_cast<PackedType>(this)) + if (const PackedType *thatPTy = dyn_cast<PackedType>(Ty)) + return thisPTy->getBitWidth() == thatPTy->getBitWidth(); - // Now we know that they are two differing primitive or pointer types + // At this point we have only various mismatches of the first class types + // remaining and ptr->ptr. Just select the lossless conversions. Everything + // else is not lossless. switch (getTypeID()) { case Type::UByteTyID: return Ty == Type::SByteTy; case Type::SByteTyID: return Ty == Type::UByteTy; @@ -127,8 +130,9 @@ bool Type::isLosslesslyConvertibleTo(const Type *Ty) const { case Type::LongTyID: return Ty == Type::ULongTy; case Type::PointerTyID: return isa<PointerType>(Ty); default: - return false; // Other types have no identity values + break; } + return false; // Other types have no identity values } /// getUnsignedVersion - If this is an integer type, return the unsigned @@ -200,6 +204,10 @@ unsigned Type::getPrimitiveSizeInBits() const { case Type::LongTyID: case Type::ULongTyID: case Type::DoubleTyID: return 64; + case Type::PackedTyID: { + const PackedType *PTy = cast<PackedType>(this); + return PTy->getBitWidth(); + } default: return 0; } } diff --git a/lib/VMCore/Verifier.cpp b/lib/VMCore/Verifier.cpp index 065ac3b..d57cd51 100644 --- a/lib/VMCore/Verifier.cpp +++ b/lib/VMCore/Verifier.cpp @@ -177,6 +177,18 @@ namespace { // Anonymous namespace for class void visitGlobalVariable(GlobalVariable &GV); void visitFunction(Function &F); void visitBasicBlock(BasicBlock &BB); + void visitTruncInst(TruncInst &I); + void visitZExtInst(ZExtInst &I); + void visitSExtInst(SExtInst &I); + void visitFPTruncInst(FPTruncInst &I); + void visitFPExtInst(FPExtInst &I); + void visitFPToUIInst(FPToUIInst &I); + void visitFPToSIInst(FPToSIInst &I); + void visitUIToFPInst(UIToFPInst &I); + void visitSIToFPInst(SIToFPInst &I); + void visitIntToPtrInst(IntToPtrInst &I); + void visitPtrToIntInst(PtrToIntInst &I); + void visitBitCastInst(BitCastInst &I); void visitPHINode(PHINode &PN); void visitBinaryOperator(BinaryOperator &B); void visitICmpInst(ICmpInst &IC); @@ -467,6 +479,169 @@ void Verifier::visitUserOp1(Instruction &I) { Assert1(0, "User-defined operators should not live outside of a pass!", &I); } +void Verifier::visitTruncInst(TruncInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + // Get the size of the types in bits, we'll need this later + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); + + Assert1(SrcTy->isIntegral(), "Trunc only operates on integer", &I); + Assert1(DestTy->isIntegral(),"Trunc only produces integral", &I); + Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I); + + visitInstruction(I); +} + +void Verifier::visitZExtInst(ZExtInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + // Get the size of the types in bits, we'll need this later + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); + + Assert1(SrcTy->isIntegral(),"ZExt only operates on integral", &I); + Assert1(DestTy->isInteger(),"ZExt only produces an integer", &I); + Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I); + + visitInstruction(I); +} + +void Verifier::visitSExtInst(SExtInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + // Get the size of the types in bits, we'll need this later + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); + + Assert1(SrcTy->isIntegral(),"SExt only operates on integral", &I); + Assert1(DestTy->isInteger(),"SExt only produces an integer", &I); + Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I); + + visitInstruction(I); +} + +void Verifier::visitFPTruncInst(FPTruncInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + // Get the size of the types in bits, we'll need this later + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); + + Assert1(SrcTy->isFloatingPoint(),"FPTrunc only operates on FP", &I); + Assert1(DestTy->isFloatingPoint(),"FPTrunc only produces an FP", &I); + Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I); + + visitInstruction(I); +} + +void Verifier::visitFPExtInst(FPExtInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + // Get the size of the types in bits, we'll need this later + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); + + Assert1(SrcTy->isFloatingPoint(),"FPExt only operates on FP", &I); + Assert1(DestTy->isFloatingPoint(),"FPExt only produces an FP", &I); + Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I); + + visitInstruction(I); +} + +void Verifier::visitUIToFPInst(UIToFPInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + Assert1(SrcTy->isIntegral(),"UInt2FP source must be integral", &I); + Assert1(DestTy->isFloatingPoint(),"UInt2FP result must be FP", &I); + + visitInstruction(I); +} + +void Verifier::visitSIToFPInst(SIToFPInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + Assert1(SrcTy->isIntegral(),"SInt2FP source must be integral", &I); + Assert1(DestTy->isFloatingPoint(),"SInt2FP result must be FP", &I); + + visitInstruction(I); +} + +void Verifier::visitFPToUIInst(FPToUIInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + Assert1(SrcTy->isFloatingPoint(),"FP2UInt source must be FP", &I); + Assert1(DestTy->isIntegral(),"FP2UInt result must be integral", &I); + + visitInstruction(I); +} + +void Verifier::visitFPToSIInst(FPToSIInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + Assert1(SrcTy->isFloatingPoint(),"FPToSI source must be FP", &I); + Assert1(DestTy->isIntegral(),"FP2ToI result must be integral", &I); + + visitInstruction(I); +} + +void Verifier::visitPtrToIntInst(PtrToIntInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + Assert1(isa<PointerType>(SrcTy), "PtrToInt source must be pointer", &I); + Assert1(DestTy->isIntegral(), "PtrToInt result must be integral", &I); + + visitInstruction(I); +} + +void Verifier::visitIntToPtrInst(IntToPtrInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + Assert1(SrcTy->isIntegral(), "IntToPtr source must be an integral", &I); + Assert1(isa<PointerType>(DestTy), "IntToPtr result must be a pointer",&I); + + visitInstruction(I); +} + +void Verifier::visitBitCastInst(BitCastInst &I) { + // Get the source and destination types + const Type *SrcTy = I.getOperand(0)->getType(); + const Type *DestTy = I.getType(); + + // Get the size of the types in bits, we'll need this later + unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); + unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); + + // BitCast implies a no-op cast of type only. No bits change. + // However, you can't cast pointers to anything but pointers. + Assert1(isa<PointerType>(DestTy) == isa<PointerType>(DestTy), + "Bitcast requires both operands to be pointer or neither", &I); + Assert1(SrcBitSize == DestBitSize, "Bitcast requies types of same width", &I); + + visitInstruction(I); +} + /// visitPHINode - Ensure that a PHI node is well formed. /// void Verifier::visitPHINode(PHINode &PN) { |