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 4f2cbc42fc..8ff55b6640 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 1529d1bf1f..57a09e2d07 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 64dd1b1234..9974071385 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 5119aaf3f7..2824979cf4 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 5119aaf3f7..2824979cf4 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 d91e21841d..06dcbb38a7 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 641cb9fe7e..7a44ec0786 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) { |