X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FIR%2FConstants.cpp;h=9067b343122dbbb6d7aa63eeb5315e214bc74dd3;hb=1fe49e68baf567a689312eaf5973e017b8d2fdd5;hp=93275549b182cf4e9ecfe22a55f4ef30efb7be5a;hpb=c3d6de2fe52dbdbf41b1dfebb1430656a16b254b;p=oota-llvm.git diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index 93275549b18..9067b343122 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -47,6 +47,16 @@ bool Constant::isNegativeZeroValue() const { if (const ConstantFP *CFP = dyn_cast(this)) return CFP->isZero() && CFP->isNegative(); + // Equivalent for a vector of -0.0's. + if (const ConstantDataVector *CV = dyn_cast(this)) + if (ConstantFP *SplatCFP = dyn_cast_or_null(CV->getSplatValue())) + if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative()) + return true; + + // We've already handled true FP case; any other FP vectors can't represent -0.0. + if (getType()->isFPOrFPVectorTy()) + return false; + // Otherwise, just use +0.0. return isNullValue(); } @@ -119,7 +129,8 @@ Constant *Constant::getNullValue(Type *Ty) { APFloat::getZero(APFloat::IEEEquad)); case Type::PPC_FP128TyID: return ConstantFP::get(Ty->getContext(), - APFloat(APInt::getNullValue(128))); + APFloat(APFloat::PPCDoubleDouble, + APInt::getNullValue(128))); case Type::PointerTyID: return ConstantPointerNull::get(cast(Ty)); case Type::StructTyID: @@ -226,18 +237,21 @@ void Constant::destroyConstantImpl() { delete this; } -/// canTrap - Return true if evaluation of this constant could trap. This is -/// true for things like constant expressions that could divide by zero. -bool Constant::canTrap() const { - assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); +static bool canTrapImpl(const Constant *C, + SmallPtrSet &NonTrappingOps) { + assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); // The only thing that could possibly trap are constant exprs. - const ConstantExpr *CE = dyn_cast(this); - if (!CE) return false; + const ConstantExpr *CE = dyn_cast(C); + if (!CE) + return false; // ConstantExpr traps if any operands can trap. - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (CE->getOperand(i)->canTrap()) - return true; + for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { + if (ConstantExpr *Op = dyn_cast(CE->getOperand(i))) { + if (NonTrappingOps.insert(Op) && canTrapImpl(Op, NonTrappingOps)) + return true; + } + } // Otherwise, only specific operations can trap. switch (CE->getOpcode()) { @@ -256,6 +270,13 @@ bool Constant::canTrap() const { } } +/// canTrap - Return true if evaluation of this constant could trap. This is +/// true for things like constant expressions that could divide by zero. +bool Constant::canTrap() const { + SmallPtrSet NonTrappingOps; + return canTrapImpl(this, NonTrappingOps); +} + /// isThreadDependent - Return true if the value can vary between threads. bool Constant::isThreadDependent() const { SmallPtrSet Visited; @@ -462,8 +483,8 @@ ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { // Get the corresponding integer type for the bit width of the value. IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); // get an existing value or the insertion position - DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); - ConstantInt *&Slot = Context.pImpl->IntConstants[Key]; + LLVMContextImpl *pImpl = Context.pImpl; + ConstantInt *&Slot = pImpl->IntConstants[DenseMapAPIntKeyInfo::KeyTy(V, ITy)]; if (!Slot) Slot = new ConstantInt(ITy, V); return Slot; } @@ -587,11 +608,9 @@ Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { // ConstantFP accessors. ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { - DenseMapAPFloatKeyInfo::KeyTy Key(V); - LLVMContextImpl* pImpl = Context.pImpl; - ConstantFP *&Slot = pImpl->FPConstants[Key]; + ConstantFP *&Slot = pImpl->FPConstants[DenseMapAPFloatKeyInfo::KeyTy(V)]; if (!Slot) { Type *Ty; @@ -1370,7 +1389,7 @@ void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { BasicBlock *NewBB = getBasicBlock(); if (U == &Op<0>()) - NewF = cast(To); + NewF = cast(To->stripPointerCasts()); else NewBB = cast(To); @@ -1415,9 +1434,8 @@ static inline Constant *getFoldedCast( LLVMContextImpl *pImpl = Ty->getContext().pImpl; - // Look up the constant in the table first to ensure uniqueness - std::vector argVec(1, C); - ExprMapKeyType Key(opc, argVec); + // Look up the constant in the table first to ensure uniqueness. + ExprMapKeyType Key(opc, C); return pImpl->ExprConstants.getOrCreate(Ty, Key); } @@ -1465,10 +1483,11 @@ Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { } Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { - assert(S->getType()->isPointerTy() && "Invalid cast"); - assert((Ty->isIntegerTy() || Ty->isPointerTy()) && "Invalid cast"); + assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); + assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && + "Invalid cast"); - if (Ty->isIntegerTy()) + if (Ty->isIntOrIntVectorTy()) return getPtrToInt(S, Ty); return getBitCast(S, Ty); } @@ -1713,9 +1732,8 @@ Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) return FC; // Fold a few common cases. - std::vector argVec(1, C1); - argVec.push_back(C2); - ExprMapKeyType Key(Opcode, argVec, 0, Flags); + Constant *ArgVec[] = { C1, C2 }; + ExprMapKeyType Key(Opcode, ArgVec, 0, Flags); LLVMContextImpl *pImpl = C1->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(C1->getType(), Key); @@ -1791,10 +1809,8 @@ Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) { if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) return SC; // Fold common cases - std::vector argVec(3, C); - argVec[1] = V1; - argVec[2] = V2; - ExprMapKeyType Key(Instruction::Select, argVec); + Constant *ArgVec[] = { C, V1, V2 }; + ExprMapKeyType Key(Instruction::Select, ArgVec); LLVMContextImpl *pImpl = C->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(V1->getType(), Key); @@ -1846,9 +1862,7 @@ ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) { return FC; // Fold a few common cases... // Look up the constant in the table first to ensure uniqueness - std::vector ArgVec; - ArgVec.push_back(LHS); - ArgVec.push_back(RHS); + Constant *ArgVec[] = { LHS, RHS }; // Get the key type with both the opcode and predicate const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); @@ -1869,9 +1883,7 @@ ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) { return FC; // Fold a few common cases... // Look up the constant in the table first to ensure uniqueness - std::vector ArgVec; - ArgVec.push_back(LHS); - ArgVec.push_back(RHS); + Constant *ArgVec[] = { LHS, RHS }; // Get the key type with both the opcode and predicate const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); @@ -1893,9 +1905,8 @@ Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { return FC; // Fold a few common cases. // Look up the constant in the table first to ensure uniqueness - std::vector ArgVec(1, Val); - ArgVec.push_back(Idx); - const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); + Constant *ArgVec[] = { Val, Idx }; + const ExprMapKeyType Key(Instruction::ExtractElement, ArgVec); LLVMContextImpl *pImpl = Val->getContext().pImpl; Type *ReqTy = Val->getType()->getVectorElementType(); @@ -1914,10 +1925,8 @@ Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) return FC; // Fold a few common cases. // Look up the constant in the table first to ensure uniqueness - std::vector ArgVec(1, Val); - ArgVec.push_back(Elt); - ArgVec.push_back(Idx); - const ExprMapKeyType Key(Instruction::InsertElement,ArgVec); + Constant *ArgVec[] = { Val, Elt, Idx }; + const ExprMapKeyType Key(Instruction::InsertElement, ArgVec); LLVMContextImpl *pImpl = Val->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(Val->getType(), Key); @@ -1936,10 +1945,8 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, Type *ShufTy = VectorType::get(EltTy, NElts); // Look up the constant in the table first to ensure uniqueness - std::vector ArgVec(1, V1); - ArgVec.push_back(V2); - ArgVec.push_back(Mask); - const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec); + Constant *ArgVec[] = { V1, V2, Mask }; + const ExprMapKeyType Key(Instruction::ShuffleVector, ArgVec); LLVMContextImpl *pImpl = ShufTy->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(ShufTy, Key); @@ -1947,14 +1954,22 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, ArrayRef Idxs) { + assert(Agg->getType()->isFirstClassType() && + "Non-first-class type for constant insertvalue expression"); + assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == Val->getType() && "insertvalue indices invalid!"); - assert(Agg->getType()->isFirstClassType() && - "Non-first-class type for constant insertvalue expression"); - Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs); - assert(FC && "insertvalue constant expr couldn't be folded!"); - return FC; + Type *ReqTy = Val->getType(); + + if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs)) + return FC; + + Constant *ArgVec[] = { Agg, Val }; + const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs); + + LLVMContextImpl *pImpl = Agg->getContext().pImpl; + return pImpl->ExprConstants.getOrCreate(ReqTy, Key); } Constant *ConstantExpr::getExtractValue(Constant *Agg, @@ -1968,9 +1983,14 @@ Constant *ConstantExpr::getExtractValue(Constant *Agg, assert(Agg->getType()->isFirstClassType() && "Non-first-class type for constant extractvalue expression"); - Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs); - assert(FC && "ExtractValue constant expr couldn't be folded!"); - return FC; + if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs)) + return FC; + + Constant *ArgVec[] = { Agg }; + const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs); + + LLVMContextImpl *pImpl = Agg->getContext().pImpl; + return pImpl->ExprConstants.getOrCreate(ReqTy, Key); } Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {