diff options
author | Owen Anderson <resistor@mac.com> | 2009-06-19 23:16:19 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2009-06-19 23:16:19 +0000 |
commit | 430444b10237abd37eb8157c3f84509a7d9636f8 (patch) | |
tree | bb95b125f17911c2cf3abaf7c98b0d1ccb1aa41f /lib/VMCore/ConstantFold.cpp | |
parent | 4d1c1efd800727165c12c2d186a5cb0b4f5834ab (diff) |
Fix a serious bug that would cause deadlock during abstract type refinement. The constant creation
gets involved, and we end up trying to recursively acquire a writer lock. The fix for this is slightly horrible,
and involves passing a boolean "locked" parameter around in Constants.cpp, but it's better than having locked and
unlocked versions of most of the code.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73790 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/ConstantFold.cpp')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 111 |
1 files changed, 60 insertions, 51 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 6c392145a5..4b2a3f25db 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -40,7 +40,8 @@ using namespace llvm; /// specified vector type. At this point, we know that the elements of the /// input vector constant are all simple integer or FP values. static Constant *BitCastConstantVector(ConstantVector *CV, - const VectorType *DstTy) { + const VectorType *DstTy, + bool locked) { // If this cast changes element count then we can't handle it here: // doing so requires endianness information. This should be handled by // Analysis/ConstantFolding.cpp @@ -60,7 +61,7 @@ static Constant *BitCastConstantVector(ConstantVector *CV, const Type *DstEltTy = DstTy->getElementType(); for (unsigned i = 0; i != NumElts; ++i) Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i), DstEltTy)); - return ConstantVector::get(Result); + return ConstantVector::get(Result, locked); } /// This function determines which opcode to use to fold two constant cast @@ -88,7 +89,8 @@ foldConstantCastPair( Type::Int64Ty); } -static Constant *FoldBitCast(Constant *V, const Type *DestTy) { +static Constant *FoldBitCast(Constant *V, const Type *DestTy, + bool locked = true) { const Type *SrcTy = V->getType(); if (SrcTy == DestTy) return V; // no-op cast @@ -99,7 +101,7 @@ static Constant *FoldBitCast(Constant *V, const Type *DestTy) { if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) if (PTy->getAddressSpace() == DPTy->getAddressSpace()) { SmallVector<Value*, 8> IdxList; - IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); + IdxList.push_back(Constant::getNullValue(Type::Int32Ty, locked)); const Type *ElTy = PTy->getElementType(); while (ElTy != DPTy->getElementType()) { if (const StructType *STy = dyn_cast<StructType>(ElTy)) { @@ -117,7 +119,8 @@ static Constant *FoldBitCast(Constant *V, const Type *DestTy) { } if (ElTy == DPTy->getElementType()) - return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size()); + return ConstantExpr::getGetElementPtr(V, &IdxList[0], + IdxList.size(), locked); } // Handle casts from one vector constant to another. We know that the src @@ -129,23 +132,24 @@ static Constant *FoldBitCast(Constant *V, const Type *DestTy) { SrcTy = NULL; // First, check for null. Undef is already handled. if (isa<ConstantAggregateZero>(V)) - return Constant::getNullValue(DestTy); + return Constant::getNullValue(DestTy, locked); if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) - return BitCastConstantVector(CV, DestPTy); + return BitCastConstantVector(CV, DestPTy, locked); } // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts // This allows for other simplifications (although some of them // can only be handled by Analysis/ConstantFolding.cpp). if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) - return ConstantExpr::getBitCast(ConstantVector::get(&V, 1), DestPTy); + return ConstantExpr::getBitCast(ConstantVector::get(&V, 1, locked), + DestPTy, locked); } // Finally, implement bitcast folding now. The code below doesn't handle // bitcast right. if (isa<ConstantPointerNull>(V)) // ptr->ptr cast. - return ConstantPointerNull::get(cast<PointerType>(DestTy)); + return ConstantPointerNull::get(cast<PointerType>(DestTy), locked); // Handle integral constant input. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { @@ -156,7 +160,7 @@ static Constant *FoldBitCast(Constant *V, const Type *DestTy) { if (DestTy->isFloatingPoint()) return ConstantFP::get(APFloat(CI->getValue(), - DestTy != Type::PPC_FP128Ty)); + DestTy != Type::PPC_FP128Ty), locked); // Otherwise, can't fold this (vector?) return 0; @@ -165,22 +169,22 @@ static Constant *FoldBitCast(Constant *V, const Type *DestTy) { // Handle ConstantFP input. if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) // FP -> Integral. - return ConstantInt::get(FP->getValueAPF().bitcastToAPInt()); + return ConstantInt::get(FP->getValueAPF().bitcastToAPInt(), locked); return 0; } Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, - const Type *DestTy) { + const Type *DestTy, bool locked) { if (isa<UndefValue>(V)) { // zext(undef) = 0, because the top bits will be zero. // sext(undef) = 0, because the top bits will all be the same. // [us]itofp(undef) = 0, because the result value is bounded. if (opc == Instruction::ZExt || opc == Instruction::SExt || opc == Instruction::UIToFP || opc == Instruction::SIToFP) - return Constant::getNullValue(DestTy); - return UndefValue::get(DestTy); + return Constant::getNullValue(DestTy, locked); + return UndefValue::get(DestTy, locked); } // No compile-time operations on this type yet. if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty) @@ -192,7 +196,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, if (CE->isCast()) { // Try hard to fold cast of cast because they are often eliminable. if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) - return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); + return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy, locked); } 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. @@ -204,7 +208,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, } if (isAllNull) // This is casting one pointer type to another, always BitCast - return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy); + return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy, locked); } } @@ -220,8 +224,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, const Type *DstEltTy = DestVecTy->getElementType(); for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) res.push_back(ConstantExpr::getCast(opc, - CV->getOperand(i), DstEltTy)); - return ConstantVector::get(DestVecTy, res); + CV->getOperand(i), DstEltTy, locked)); + return ConstantVector::get(DestVecTy, res, locked); } // We actually have to do a cast now. Perform the cast according to the @@ -238,7 +242,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, DestTy == Type::FP128Ty ? APFloat::IEEEquad : APFloat::Bogus, APFloat::rmNearestTiesToEven, &ignored); - return ConstantFP::get(Val); + return ConstantFP::get(Val, locked); } return 0; // Can't fold. case Instruction::FPToUI: @@ -251,16 +255,16 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI, APFloat::rmTowardZero, &ignored); APInt Val(DestBitWidth, 2, x); - return ConstantInt::get(Val); + return ConstantInt::get(Val, locked); } return 0; // Can't fold. case Instruction::IntToPtr: //always treated as unsigned if (V->isNullValue()) // Is it an integral null value? - return ConstantPointerNull::get(cast<PointerType>(DestTy)); + return ConstantPointerNull::get(cast<PointerType>(DestTy), locked); return 0; // Other pointer types cannot be casted case Instruction::PtrToInt: // always treated as unsigned if (V->isNullValue()) // is it a null pointer value? - return ConstantInt::get(DestTy, 0); + return ConstantInt::get(DestTy, 0, locked); return 0; // Other pointer types cannot be casted case Instruction::UIToFP: case Instruction::SIToFP: @@ -272,7 +276,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, (void)apf.convertFromAPInt(api, opc==Instruction::SIToFP, APFloat::rmNearestTiesToEven); - return ConstantFP::get(apf); + return ConstantFP::get(apf, locked); } return 0; case Instruction::ZExt: @@ -280,7 +284,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); APInt Result(CI->getValue()); Result.zext(BitWidth); - return ConstantInt::get(Result); + return ConstantInt::get(Result, locked); } return 0; case Instruction::SExt: @@ -288,7 +292,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); APInt Result(CI->getValue()); Result.sext(BitWidth); - return ConstantInt::get(Result); + return ConstantInt::get(Result, locked); } return 0; case Instruction::Trunc: @@ -296,11 +300,11 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); APInt Result(CI->getValue()); Result.trunc(BitWidth); - return ConstantInt::get(Result); + return ConstantInt::get(Result, locked); } return 0; case Instruction::BitCast: - return FoldBitCast(const_cast<Constant*>(V), DestTy); + return FoldBitCast(const_cast<Constant*>(V), DestTy, locked); default: assert(!"Invalid CE CastInst opcode"); break; @@ -312,7 +316,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond, const Constant *V1, - const Constant *V2) { + const Constant *V2, bool locked) { if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond)) return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2); @@ -566,21 +570,22 @@ Constant *llvm::ConstantFoldInsertValueInstruction(const Constant *Agg, static Constant *EvalVectorOp(const ConstantVector *V1, const ConstantVector *V2, const VectorType *VTy, - Constant *(*FP)(Constant*, Constant*)) { + Constant *(*FP)(Constant*, Constant*, bool)) { std::vector<Constant*> Res; const Type *EltTy = VTy->getElementType(); for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { const Constant *C1 = V1 ? V1->getOperand(i) : Constant::getNullValue(EltTy); const Constant *C2 = V2 ? V2->getOperand(i) : Constant::getNullValue(EltTy); Res.push_back(FP(const_cast<Constant*>(C1), - const_cast<Constant*>(C2))); + const_cast<Constant*>(C2), true)); } return ConstantVector::get(Res); } Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *C1, - const Constant *C2) { + const Constant *C2, + bool locked) { // No compile-time operations on this type yet. if (C1->getType() == Type::PPC_FP128Ty) return 0; @@ -592,29 +597,29 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // Handle undef ^ undef -> 0 special case. This is a common // idiom (misuse). - return Constant::getNullValue(C1->getType()); + return Constant::getNullValue(C1->getType(), locked); // Fallthrough case Instruction::Add: case Instruction::Sub: - return UndefValue::get(C1->getType()); + return UndefValue::get(C1->getType(), locked); case Instruction::Mul: case Instruction::And: - return Constant::getNullValue(C1->getType()); + return Constant::getNullValue(C1->getType(), locked); case Instruction::UDiv: case Instruction::SDiv: case Instruction::URem: case Instruction::SRem: if (!isa<UndefValue>(C2)) // undef / X -> 0 - return Constant::getNullValue(C1->getType()); + return Constant::getNullValue(C1->getType(), locked); return const_cast<Constant*>(C2); // X / undef -> undef case Instruction::Or: // X | undef -> -1 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType())) - return ConstantVector::getAllOnesValue(PTy); - return ConstantInt::getAllOnesValue(C1->getType()); + return ConstantVector::getAllOnesValue(PTy, locked); + return ConstantInt::getAllOnesValue(C1->getType(), locked); case Instruction::LShr: if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) return const_cast<Constant*>(C1); // undef lshr undef -> undef - return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 + return Constant::getNullValue(C1->getType(), locked); // X lshr undef -> 0 // undef lshr X -> 0 case Instruction::AShr: if (!isa<UndefValue>(C2)) @@ -625,7 +630,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, return const_cast<Constant*>(C1); // X ashr undef --> X case Instruction::Shl: // undef << X -> 0 or X << undef -> 0 - return Constant::getNullValue(C1->getType()); + return Constant::getNullValue(C1->getType(), locked); } } @@ -1572,7 +1577,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, Constant* const *Idxs, - unsigned NumIdx) { + unsigned NumIdx, bool locked) { if (NumIdx == 0 || (NumIdx == 1 && Idxs[0]->isNullValue())) return const_cast<Constant*>(C); @@ -1583,7 +1588,8 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, (Value **)Idxs, (Value **)Idxs+NumIdx); assert(Ty != 0 && "Invalid indices for GEP!"); - return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace())); + return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()), + locked); } Constant *Idx0 = Idxs[0]; @@ -1601,7 +1607,8 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, (Value**)Idxs+NumIdx); assert(Ty != 0 && "Invalid indices for GEP!"); return - ConstantPointerNull::get(PointerType::get(Ty,Ptr->getAddressSpace())); + ConstantPointerNull::get(PointerType::get(Ty,Ptr->getAddressSpace()), + locked); } } @@ -1629,20 +1636,22 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, if (!Idx0->isNullValue()) { const Type *IdxTy = Combined->getType(); if (IdxTy != Idx0->getType()) { - Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty); + Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty, + locked); Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, - Type::Int64Ty); - Combined = ConstantExpr::get(Instruction::Add, C1, C2); + Type::Int64Ty, + locked); + Combined = ConstantExpr::get(Instruction::Add, C1, C2, locked); } else { Combined = - ConstantExpr::get(Instruction::Add, Idx0, Combined); + ConstantExpr::get(Instruction::Add, Idx0, Combined, locked); } } NewIndices.push_back(Combined); NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx); return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0], - NewIndices.size()); + NewIndices.size(), locked); } } @@ -1659,7 +1668,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) if (CAT->getElementType() == SAT->getElementType()) return ConstantExpr::getGetElementPtr( - (Constant*)CE->getOperand(0), Idxs, NumIdx); + (Constant*)CE->getOperand(0), Idxs, NumIdx, locked); } // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1) @@ -1677,10 +1686,10 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, Offset = ConstantExpr::getSExt(Offset, Base->getType()); else if (Base->getType()->getPrimitiveSizeInBits() < Offset->getType()->getPrimitiveSizeInBits()) - Base = ConstantExpr::getZExt(Base, Offset->getType()); + Base = ConstantExpr::getZExt(Base, Offset->getType(), locked); - Base = ConstantExpr::getAdd(Base, Offset); - return ConstantExpr::getIntToPtr(Base, CE->getType()); + Base = ConstantExpr::getAdd(Base, Offset, locked); + return ConstantExpr::getIntToPtr(Base, CE->getType(), locked); } } return 0; |