- return 0;
- }
- }
-
- // At this point we know neither constant is an UndefValue nor a ConstantExpr
- // so look at directly computing the value.
- if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
- if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
- if (CI1->getType() == Type::Int1Ty && CI2->getType() == Type::Int1Ty) {
- switch (Opcode) {
- default:
- break;
- case Instruction::And:
- return ConstantInt::get(Type::Int1Ty,
- CI1->getZExtValue() & CI2->getZExtValue());
- case Instruction::Or:
- return ConstantInt::get(Type::Int1Ty,
- CI1->getZExtValue() | CI2->getZExtValue());
- case Instruction::Xor:
- return ConstantInt::get(Type::Int1Ty,
- CI1->getZExtValue() ^ CI2->getZExtValue());
- }
- } else {
- uint64_t C1Val = CI1->getZExtValue();
- uint64_t C2Val = CI2->getZExtValue();
- switch (Opcode) {
- default:
- break;
- case Instruction::Add:
- return ConstantInt::get(C1->getType(), C1Val + C2Val);
- case Instruction::Sub:
- return ConstantInt::get(C1->getType(), C1Val - C2Val);
- case Instruction::Mul:
- return ConstantInt::get(C1->getType(), C1Val * C2Val);
- case Instruction::UDiv:
- if (CI2->isNullValue()) // X / 0 -> can't fold
- return 0;
- return ConstantInt::get(C1->getType(), C1Val / C2Val);
- case Instruction::SDiv:
- if (CI2->isNullValue()) return 0; // X / 0 -> can't fold
- if (CI2->isAllOnesValue() &&
- (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
- (CI1->getSExtValue() == INT64_MIN)) ||
- (CI1->getSExtValue() == -CI1->getSExtValue())))
- return 0; // MIN_INT / -1 -> overflow
- return ConstantInt::get(C1->getType(),
- CI1->getSExtValue() / CI2->getSExtValue());
- case Instruction::URem:
- if (C2->isNullValue()) return 0; // X / 0 -> can't fold
- return ConstantInt::get(C1->getType(), C1Val % C2Val);
- case Instruction::SRem:
- if (CI2->isNullValue()) return 0; // X % 0 -> can't fold
- if (CI2->isAllOnesValue() &&
- (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
- (CI1->getSExtValue() == INT64_MIN)) ||
- (CI1->getSExtValue() == -CI1->getSExtValue())))
- return 0; // MIN_INT % -1 -> overflow
- return ConstantInt::get(C1->getType(),
- CI1->getSExtValue() % CI2->getSExtValue());
- case Instruction::And:
- return ConstantInt::get(C1->getType(), C1Val & C2Val);
- case Instruction::Or:
- return ConstantInt::get(C1->getType(), C1Val | C2Val);
- case Instruction::Xor:
- return ConstantInt::get(C1->getType(), C1Val ^ C2Val);
- case Instruction::Shl:
- return ConstantInt::get(C1->getType(), C1Val << C2Val);
- case Instruction::LShr:
- return ConstantInt::get(C1->getType(), C1Val >> C2Val);
- case Instruction::AShr:
- return ConstantInt::get(C1->getType(),
- CI1->getSExtValue() >> C2Val);
- }
- }
- }
- } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
- if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
- double C1Val = CFP1->getValue();
- double C2Val = CFP2->getValue();
- switch (Opcode) {
- default:
- break;
- case Instruction::Add:
- return ConstantFP::get(CFP1->getType(), C1Val + C2Val);
- case Instruction::Sub:
- return ConstantFP::get(CFP1->getType(), C1Val - C2Val);
- case Instruction::Mul:
- return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
- case Instruction::FDiv:
- if (CFP2->isExactlyValue(0.0))
- return ConstantFP::get(CFP1->getType(),
- std::numeric_limits<double>::infinity());
- if (CFP2->isExactlyValue(-0.0))
- return ConstantFP::get(CFP1->getType(),
- -std::numeric_limits<double>::infinity());
- return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
- case Instruction::FRem:
- if (CFP2->isNullValue())
- return 0;
- return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
- }
- }
- } else if (const ConstantPacked *CP1 = dyn_cast<ConstantPacked>(C1)) {
- if (const ConstantPacked *CP2 = dyn_cast<ConstantPacked>(C2)) {
- switch (Opcode) {
- default:
- break;
- case Instruction::Add:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getAdd);
- case Instruction::Sub:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getSub);
- case Instruction::Mul:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getMul);
- case Instruction::UDiv:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getUDiv);
- case Instruction::SDiv:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getSDiv);
- case Instruction::FDiv:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getFDiv);
- case Instruction::URem:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getURem);
- case Instruction::SRem:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getSRem);
- case Instruction::FRem:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getFRem);
- case Instruction::And:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getAnd);
- case Instruction::Or:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getOr);
- case Instruction::Xor:
- return EvalVectorOp(CP1, CP2, ConstantExpr::getXor);
- }