/// 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,
- bool locked) {
+ const VectorType *DstTy) {
// 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
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, locked);
+ return ConstantVector::get(Result);
}
/// This function determines which opcode to use to fold two constant cast
Type::Int64Ty);
}
-static Constant *FoldBitCast(Constant *V, const Type *DestTy,
- bool locked = true) {
+static Constant *FoldBitCast(Constant *V, const Type *DestTy) {
const Type *SrcTy = V->getType();
if (SrcTy == DestTy)
return V; // no-op cast
if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
SmallVector<Value*, 8> IdxList;
- IdxList.push_back(Constant::getNullValue(Type::Int32Ty, locked));
+ IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
const Type *ElTy = PTy->getElementType();
while (ElTy != DPTy->getElementType()) {
if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
}
if (ElTy == DPTy->getElementType())
- return ConstantExpr::getGetElementPtr(V, &IdxList[0],
- IdxList.size(), locked);
+ return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size());
}
// Handle casts from one vector constant to another. We know that the src
SrcTy = NULL;
// First, check for null. Undef is already handled.
if (isa<ConstantAggregateZero>(V))
- return Constant::getNullValue(DestTy, locked);
+ return Constant::getNullValue(DestTy);
if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
- return BitCastConstantVector(CV, DestPTy, locked);
+ return BitCastConstantVector(CV, DestPTy);
}
// 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, locked),
- DestPTy, locked);
+ return ConstantExpr::getBitCast(ConstantVector::get(&V, 1), DestPTy);
}
// 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), locked);
+ return ConstantPointerNull::get(cast<PointerType>(DestTy));
// Handle integral constant input.
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
if (DestTy->isFloatingPoint())
return ConstantFP::get(APFloat(CI->getValue(),
- DestTy != Type::PPC_FP128Ty), locked);
+ DestTy != Type::PPC_FP128Ty));
// Otherwise, can't fold this (vector?)
return 0;
// Handle ConstantFP input.
if (const ConstantFP *FP = dyn_cast<ConstantFP>(V))
// FP -> Integral.
- return ConstantInt::get(FP->getValueAPF().bitcastToAPInt(), locked);
+ return ConstantInt::get(FP->getValueAPF().bitcastToAPInt());
return 0;
}
Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
- const Type *DestTy, bool locked) {
+ const Type *DestTy) {
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, locked);
- return UndefValue::get(DestTy, locked);
+ return Constant::getNullValue(DestTy);
+ return UndefValue::get(DestTy);
}
// No compile-time operations on this type yet.
if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty)
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, locked);
+ 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.
}
if (isAllNull)
// This is casting one pointer type to another, always BitCast
- return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy, locked);
+ return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
}
}
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, locked));
- return ConstantVector::get(DestVecTy, res, locked);
+ CV->getOperand(i), DstEltTy));
+ return ConstantVector::get(DestVecTy, res);
}
// We actually have to do a cast now. Perform the cast according to the
DestTy == Type::FP128Ty ? APFloat::IEEEquad :
APFloat::Bogus,
APFloat::rmNearestTiesToEven, &ignored);
- return ConstantFP::get(Val, locked);
+ return ConstantFP::get(Val);
}
return 0; // Can't fold.
case Instruction::FPToUI:
(void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
APFloat::rmTowardZero, &ignored);
APInt Val(DestBitWidth, 2, x);
- return ConstantInt::get(Val, locked);
+ return ConstantInt::get(Val);
}
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), locked);
+ return ConstantPointerNull::get(cast<PointerType>(DestTy));
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, locked);
+ return ConstantInt::get(DestTy, 0);
return 0; // Other pointer types cannot be casted
case Instruction::UIToFP:
case Instruction::SIToFP:
(void)apf.convertFromAPInt(api,
opc==Instruction::SIToFP,
APFloat::rmNearestTiesToEven);
- return ConstantFP::get(apf, locked);
+ return ConstantFP::get(apf);
}
return 0;
case Instruction::ZExt:
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
APInt Result(CI->getValue());
Result.zext(BitWidth);
- return ConstantInt::get(Result, locked);
+ return ConstantInt::get(Result);
}
return 0;
case Instruction::SExt:
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
APInt Result(CI->getValue());
Result.sext(BitWidth);
- return ConstantInt::get(Result, locked);
+ return ConstantInt::get(Result);
}
return 0;
case Instruction::Trunc:
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
APInt Result(CI->getValue());
Result.trunc(BitWidth);
- return ConstantInt::get(Result, locked);
+ return ConstantInt::get(Result);
}
return 0;
case Instruction::BitCast:
- return FoldBitCast(const_cast<Constant*>(V), DestTy, locked);
+ return FoldBitCast(const_cast<Constant*>(V), DestTy);
default:
assert(!"Invalid CE CastInst opcode");
break;
Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
const Constant *V1,
- const Constant *V2, bool locked) {
+ const Constant *V2) {
if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
static Constant *EvalVectorOp(const ConstantVector *V1,
const ConstantVector *V2,
const VectorType *VTy,
- Constant *(*FP)(Constant*, Constant*, bool)) {
+ Constant *(*FP)(Constant*, Constant*)) {
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), true));
+ const_cast<Constant*>(C2)));
}
return ConstantVector::get(Res);
}
Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
const Constant *C1,
- const Constant *C2,
- bool locked) {
+ const Constant *C2) {
// No compile-time operations on this type yet.
if (C1->getType() == Type::PPC_FP128Ty)
return 0;
if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
// Handle undef ^ undef -> 0 special case. This is a common
// idiom (misuse).
- return Constant::getNullValue(C1->getType(), locked);
+ return Constant::getNullValue(C1->getType());
// Fallthrough
case Instruction::Add:
case Instruction::Sub:
- return UndefValue::get(C1->getType(), locked);
+ return UndefValue::get(C1->getType());
case Instruction::Mul:
case Instruction::And:
- return Constant::getNullValue(C1->getType(), locked);
+ return Constant::getNullValue(C1->getType());
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::URem:
case Instruction::SRem:
if (!isa<UndefValue>(C2)) // undef / X -> 0
- return Constant::getNullValue(C1->getType(), locked);
+ return Constant::getNullValue(C1->getType());
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, locked);
- return ConstantInt::getAllOnesValue(C1->getType(), locked);
+ return ConstantVector::getAllOnesValue(PTy);
+ return ConstantInt::getAllOnesValue(C1->getType());
case Instruction::LShr:
if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
return const_cast<Constant*>(C1); // undef lshr undef -> undef
- return Constant::getNullValue(C1->getType(), locked); // X lshr undef -> 0
+ return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
// undef lshr X -> 0
case Instruction::AShr:
if (!isa<UndefValue>(C2))
return const_cast<Constant*>(C1); // X ashr undef --> X
case Instruction::Shl:
// undef << X -> 0 or X << undef -> 0
- return Constant::getNullValue(C1->getType(), locked);
+ return Constant::getNullValue(C1->getType());
}
}
- // Handle simplifications of the RHS when a constant int.
+ // Handle simplifications when the RHS is a constant int.
if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
switch (Opcode) {
case Instruction::Add:
}
}
}
+
+ switch (Opcode) {
+ case Instruction::SDiv:
+ case Instruction::UDiv:
+ case Instruction::URem:
+ case Instruction::SRem:
+ case Instruction::LShr:
+ case Instruction::AShr:
+ case Instruction::Shl:
+ if (CI1->equalsInt(0)) return const_cast<Constant*>(C1);
+ break;
+ default:
+ break;
+ }
} else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
APFloat C1V = CFP1->getValueAPF();
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
if (R && !R->isZero())
return pred;
- pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
+ pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
if (R && !R->isZero())
return pred;
Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
const Constant *C1,
const Constant *C2) {
+ const Type *ResultTy;
+ if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
+ ResultTy = VectorType::get(Type::Int1Ty, VT->getNumElements());
+ else
+ ResultTy = Type::Int1Ty;
+
// Fold FCMP_FALSE/FCMP_TRUE unconditionally.
- if (pred == FCmpInst::FCMP_FALSE) {
- if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
- return Constant::getNullValue(VectorType::getInteger(VT));
- else
- return ConstantInt::getFalse();
- }
-
- if (pred == FCmpInst::FCMP_TRUE) {
- if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
- return Constant::getAllOnesValue(VectorType::getInteger(VT));
- else
- return ConstantInt::getTrue();
- }
-
+ if (pred == FCmpInst::FCMP_FALSE)
+ return Constant::getNullValue(ResultTy);
+
+ if (pred == FCmpInst::FCMP_TRUE)
+ return Constant::getAllOnesValue(ResultTy);
+
// Handle some degenerate cases first
- if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
- // vicmp/vfcmp -> [vector] undef
- if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType()))
- return UndefValue::get(VectorType::getInteger(VTy));
-
- // icmp/fcmp -> i1 undef
- return UndefValue::get(Type::Int1Ty);
- }
+ if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
+ return UndefValue::get(ResultTy);
// No compile-time operations on this type yet.
if (C1->getType() == Type::PPC_FP128Ty)
// If we can constant fold the comparison of each element, constant fold
// the whole vector comparison.
SmallVector<Constant*, 4> ResElts;
- const Type *InEltTy = C1Elts[0]->getType();
- bool isFP = InEltTy->isFloatingPoint();
- const Type *ResEltTy = InEltTy;
- if (isFP)
- ResEltTy = IntegerType::get(InEltTy->getPrimitiveSizeInBits());
-
for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
// Compare the elements, producing an i1 result or constant expr.
- Constant *C;
- if (isFP)
- C = ConstantExpr::getFCmp(pred, C1Elts[i], C2Elts[i]);
- else
- C = ConstantExpr::getICmp(pred, C1Elts[i], C2Elts[i]);
-
- // If it is a bool or undef result, convert to the dest type.
- if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
- if (CI->isZero())
- ResElts.push_back(Constant::getNullValue(ResEltTy));
- else
- ResElts.push_back(Constant::getAllOnesValue(ResEltTy));
- } else if (isa<UndefValue>(C)) {
- ResElts.push_back(UndefValue::get(ResEltTy));
- } else {
- break;
- }
+ ResElts.push_back(ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i]));
}
-
- if (ResElts.size() == C1Elts.size())
- return ConstantVector::get(&ResElts[0], ResElts.size());
+ return ConstantVector::get(&ResElts[0], ResElts.size());
}
if (C1->getType()->isFloatingPoint()) {
}
// If we evaluated the result, return it now.
- if (Result != -1) {
- if (const VectorType *VT = dyn_cast<VectorType>(C1->getType())) {
- if (Result == 0)
- return Constant::getNullValue(VectorType::getInteger(VT));
- else
- return Constant::getAllOnesValue(VectorType::getInteger(VT));
- }
+ if (Result != -1)
return ConstantInt::get(Type::Int1Ty, Result);
- }
-
+
} else {
// Evaluate the relation between the two constants, per the predicate.
int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
}
// If we evaluated the result, return it now.
- if (Result != -1) {
- if (const VectorType *VT = dyn_cast<VectorType>(C1->getType())) {
- if (Result == 0)
- return Constant::getNullValue(VT);
- else
- return Constant::getAllOnesValue(VT);
- }
+ if (Result != -1)
return ConstantInt::get(Type::Int1Ty, Result);
- }
if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
- // If C2 is a constant expr and C1 isn't, flop them around and fold the
+ // If C2 is a constant expr and C1 isn't, flip them around and fold the
// other way if possible.
switch (pred) {
case ICmpInst::ICMP_EQ:
}
}
return 0;
-}
+ }
Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Constant* const *Idxs,
- unsigned NumIdx, bool locked) {
+ unsigned NumIdx) {
if (NumIdx == 0 ||
(NumIdx == 1 && Idxs[0]->isNullValue()))
return const_cast<Constant*>(C);
(Value **)Idxs,
(Value **)Idxs+NumIdx);
assert(Ty != 0 && "Invalid indices for GEP!");
- return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()),
- locked);
+ return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
}
Constant *Idx0 = Idxs[0];
(Value**)Idxs+NumIdx);
assert(Ty != 0 && "Invalid indices for GEP!");
return
- ConstantPointerNull::get(PointerType::get(Ty,Ptr->getAddressSpace()),
- locked);
+ ConstantPointerNull::get(PointerType::get(Ty,Ptr->getAddressSpace()));
}
}
if (!Idx0->isNullValue()) {
const Type *IdxTy = Combined->getType();
if (IdxTy != Idx0->getType()) {
- Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty,
- locked);
+ Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
- Type::Int64Ty,
- locked);
- Combined = ConstantExpr::get(Instruction::Add, C1, C2, locked);
+ Type::Int64Ty);
+ Combined = ConstantExpr::get(Instruction::Add, C1, C2);
} else {
Combined =
- ConstantExpr::get(Instruction::Add, Idx0, Combined, locked);
+ ConstantExpr::get(Instruction::Add, Idx0, Combined);
}
}
NewIndices.push_back(Combined);
NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
- NewIndices.size(), locked);
+ NewIndices.size());
}
}
dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
if (CAT->getElementType() == SAT->getElementType())
return ConstantExpr::getGetElementPtr(
- (Constant*)CE->getOperand(0), Idxs, NumIdx, locked);
+ (Constant*)CE->getOperand(0), Idxs, NumIdx);
}
// Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
Offset = ConstantExpr::getSExt(Offset, Base->getType());
else if (Base->getType()->getPrimitiveSizeInBits() <
Offset->getType()->getPrimitiveSizeInBits())
- Base = ConstantExpr::getZExt(Base, Offset->getType(), locked);
+ Base = ConstantExpr::getZExt(Base, Offset->getType());
- Base = ConstantExpr::getAdd(Base, Offset, locked);
- return ConstantExpr::getIntToPtr(Base, CE->getType(), locked);
+ Base = ConstantExpr::getAdd(Base, Offset);
+ return ConstantExpr::getIntToPtr(Base, CE->getType());
}
}
return 0;