X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FConstantFold.cpp;h=eb58f7907c7de48b2932a73a4acf19fd86a13608;hb=3069cbf7b3ef9a31bbb8e434686b7259052c364a;hp=261816804e4c99c1b9643fd2d81b42c5fd9dd31c;hpb=1df9859c40492511b8aa4321eb76496005d3b75b;p=oota-llvm.git diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 261816804e4..eb58f7907c7 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -334,11 +334,7 @@ static Constant *getFoldedSizeOf(const Type *Ty, const Type *DestTy, Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true); return ConstantExpr::getNUWMul(E, N); } - if (const VectorType *VTy = dyn_cast(Ty)) { - Constant *N = ConstantInt::get(DestTy, VTy->getNumElements()); - Constant *E = getFoldedSizeOf(VTy->getElementType(), DestTy, true); - return ConstantExpr::getNUWMul(E, N); - } + if (const StructType *STy = dyn_cast(Ty)) if (!STy->isPacked()) { unsigned NumElems = STy->getNumElements(); @@ -464,13 +460,7 @@ static Constant *getFoldedOffsetOf(const Type *Ty, Constant *FieldNo, Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true); return ConstantExpr::getNUWMul(E, N); } - if (const VectorType *VTy = dyn_cast(Ty)) { - Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false, - DestTy, false), - FieldNo, DestTy); - Constant *E = getFoldedSizeOf(VTy->getElementType(), DestTy, true); - return ConstantExpr::getNUWMul(E, N); - } + if (const StructType *STy = dyn_cast(Ty)) if (!STy->isPacked()) { unsigned NumElems = STy->getNumElements(); @@ -634,7 +624,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, } } // Handle an offsetof-like expression. - if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()){ + if (Ty->isStructTy() || Ty->isArrayTy()) { if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2), DestTy, false)) return C; @@ -647,9 +637,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, case Instruction::SIToFP: if (ConstantInt *CI = dyn_cast(V)) { APInt api = CI->getValue(); - const uint64_t zero[] = {0, 0}; - APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(), - 2, zero)); + APFloat apf(APInt::getNullValue(DestTy->getPrimitiveSizeInBits())); (void)apf.convertFromAPInt(api, opc==Instruction::SIToFP, APFloat::rmNearestTiesToEven); @@ -885,8 +873,6 @@ Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, unsigned numOps; if (const ArrayType *AR = dyn_cast(AggTy)) numOps = AR->getNumElements(); - else if (AggTy->isUnionTy()) - numOps = 1; else numOps = cast(AggTy)->getNumElements(); @@ -903,10 +889,6 @@ Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, if (const StructType* ST = dyn_cast(AggTy)) return ConstantStruct::get(ST->getContext(), Ops, ST->isPacked()); - if (const UnionType* UT = dyn_cast(AggTy)) { - assert(Ops.size() == 1 && "Union can only contain a single value!"); - return ConstantUnion::get(UT, Ops[0]); - } return ConstantArray::get(cast(AggTy), Ops); } @@ -1105,6 +1087,10 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, return ConstantExpr::getLShr(C1, C2); break; } + } else if (isa(C1)) { + // If C1 is a ConstantInt and C2 is not, swap the operands. + if (Instruction::isCommutative(Opcode)) + return ConstantExpr::get(Opcode, C2, C1); } // At this point we know neither constant is an UndefValue. @@ -1364,31 +1350,8 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, } else if (isa(C2)) { // If C2 is a constant expr and C1 isn't, flop them around and fold the // other way if possible. - switch (Opcode) { - case Instruction::Add: - case Instruction::FAdd: - case Instruction::Mul: - case Instruction::FMul: - case Instruction::And: - case Instruction::Or: - case Instruction::Xor: - // No change of opcode required. + if (Instruction::isCommutative(Opcode)) return ConstantFoldBinaryInstruction(Opcode, C2, C1); - - case Instruction::Shl: - case Instruction::LShr: - case Instruction::AShr: - case Instruction::Sub: - case Instruction::FSub: - case Instruction::SDiv: - case Instruction::UDiv: - case Instruction::FDiv: - case Instruction::URem: - case Instruction::SRem: - case Instruction::FRem: - default: // These instructions cannot be flopped around. - break; - } } // i1 can be simplified in many cases. @@ -1427,7 +1390,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, /// isZeroSizedType - This type is zero sized if its an array or structure of /// zero sized types. The only leaf zero sized type is an empty structure. static bool isMaybeZeroSizedType(const Type *Ty) { - if (isa(Ty)) return true; // Can't say. + if (Ty->isOpaqueTy()) return true; // Can't say. if (const StructType *STy = dyn_cast(Ty)) { // If all of elements have zero size, this does too. @@ -1812,8 +1775,15 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, return Constant::getAllOnesValue(ResultTy); // Handle some degenerate cases first - if (isa(C1) || isa(C2)) - return UndefValue::get(ResultTy); + if (isa(C1) || isa(C2)) { + // For EQ and NE, we can always pick a value for the undef to make the + // predicate pass or fail, so we can return undef. + if (ICmpInst::isEquality(ICmpInst::Predicate(pred))) + return UndefValue::get(ResultTy); + // Otherwise, pick the same value as the non-undef operand, and fold + // it to true or false. + return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred)); + } // No compile-time operations on this type yet. if (C1->getType()->isPPC_FP128Ty()) @@ -2065,7 +2035,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, if (ConstantExpr *CE2 = dyn_cast(C2)) { Constant *CE2Op0 = CE2->getOperand(0); if (CE2->getOpcode() == Instruction::BitCast && - CE2->getType()->isVectorTy()==CE2Op0->getType()->isVectorTy()) { + CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) { Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType()); return ConstantExpr::getICmp(pred, Inverse, CE2Op0); } @@ -2073,8 +2043,8 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, // If the left hand side is an extension, try eliminating it. if (ConstantExpr *CE1 = dyn_cast(C1)) { - if (CE1->getOpcode() == Instruction::SExt || - CE1->getOpcode() == Instruction::ZExt) { + if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) || + (CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){ Constant *CE1Op0 = CE1->getOperand(0); Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType()); if (CE1Inverse == CE1Op0) { @@ -2092,27 +2062,8 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, // If C2 is a constant expr and C1 isn't, flip them around and fold the // other way if possible. // Also, if C1 is null and C2 isn't, flip them around. - switch (pred) { - case ICmpInst::ICMP_EQ: - case ICmpInst::ICMP_NE: - // No change of predicate required. - return ConstantExpr::getICmp(pred, C2, C1); - - case ICmpInst::ICMP_ULT: - case ICmpInst::ICMP_SLT: - case ICmpInst::ICMP_UGT: - case ICmpInst::ICMP_SGT: - case ICmpInst::ICMP_ULE: - case ICmpInst::ICMP_SLE: - case ICmpInst::ICMP_UGE: - case ICmpInst::ICMP_SGE: - // Change the predicate as necessary to swap the operands. - pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred); - return ConstantExpr::getICmp(pred, C2, C1); - - default: // These predicates cannot be flopped around. - break; - } + pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred); + return ConstantExpr::getICmp(pred, C2, C1); } } return 0; @@ -2208,7 +2159,7 @@ Constant *llvm::ConstantFoldGetElementPtr(Constant *C, } NewIndices.push_back(Combined); - NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx); + NewIndices.append(Idxs+1, Idxs+NumIdx); return (inBounds && cast(CE)->isInBounds()) ? ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0), &NewIndices[0],