diff options
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 82 | ||||
-rw-r--r-- | lib/VMCore/Constants.cpp | 30 | ||||
-rw-r--r-- | lib/VMCore/Instruction.cpp | 8 | ||||
-rw-r--r-- | lib/VMCore/Instructions.cpp | 18 |
4 files changed, 105 insertions, 33 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index ad9a33f845..2589739d52 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -40,7 +40,9 @@ namespace { virtual Constant *add(const Constant *V1, const Constant *V2) const = 0; virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0; virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0; - virtual Constant *div(const Constant *V1, const Constant *V2) const = 0; + virtual Constant *udiv(const Constant *V1, const Constant *V2) const = 0; + virtual Constant *sdiv(const Constant *V1, const Constant *V2) const = 0; + virtual Constant *fdiv(const Constant *V1, const Constant *V2) const = 0; virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0; virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0; virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0; @@ -106,8 +108,14 @@ class VISIBILITY_HIDDEN TemplateRules : public ConstRules { virtual Constant *mul(const Constant *V1, const Constant *V2) const { return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2); } - virtual Constant *div(const Constant *V1, const Constant *V2) const { - return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2); + virtual Constant *udiv(const Constant *V1, const Constant *V2) const { + return SubClassName::UDiv((const ArgType *)V1, (const ArgType *)V2); + } + virtual Constant *sdiv(const Constant *V1, const Constant *V2) const { + return SubClassName::SDiv((const ArgType *)V1, (const ArgType *)V2); + } + virtual Constant *fdiv(const Constant *V1, const Constant *V2) const { + return SubClassName::FDiv((const ArgType *)V1, (const ArgType *)V2); } virtual Constant *rem(const Constant *V1, const Constant *V2) const { return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2); @@ -178,16 +186,18 @@ class VISIBILITY_HIDDEN TemplateRules : public ConstRules { // Default "noop" implementations //===--------------------------------------------------------------------===// - static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; } - static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Add (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Sub (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Mul (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *SDiv(const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *UDiv(const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *FDiv(const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Rem (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *And (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Xor (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Shl (const ArgType *V1, const ArgType *V2) { return 0; } + static Constant *Shr (const ArgType *V1, const ArgType *V2) { return 0; } static Constant *LessThan(const ArgType *V1, const ArgType *V2) { return 0; } @@ -373,8 +383,14 @@ struct VISIBILITY_HIDDEN ConstantPackedRules static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) { return EvalVectorOp(V1, V2, ConstantExpr::getMul); } - static Constant *Div(const ConstantPacked *V1, const ConstantPacked *V2) { - return EvalVectorOp(V1, V2, ConstantExpr::getDiv); + static Constant *UDiv(const ConstantPacked *V1, const ConstantPacked *V2) { + return EvalVectorOp(V1, V2, ConstantExpr::getUDiv); + } + static Constant *SDiv(const ConstantPacked *V1, const ConstantPacked *V2) { + return EvalVectorOp(V1, V2, ConstantExpr::getSDiv); + } + static Constant *FDiv(const ConstantPacked *V1, const ConstantPacked *V2) { + return EvalVectorOp(V1, V2, ConstantExpr::getFDiv); } static Constant *Rem(const ConstantPacked *V1, const ConstantPacked *V2) { return EvalVectorOp(V1, V2, ConstantExpr::getRem); @@ -493,18 +509,25 @@ struct VISIBILITY_HIDDEN DirectIntRules DEF_CAST(Double, ConstantFP , double) #undef DEF_CAST - static Constant *Div(const ConstantInt *V1, const ConstantInt *V2) { - if (V2->isNullValue()) return 0; + static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) { + if (V2->isNullValue()) + return 0; + BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue()); + return ConstantInt::get(*Ty, R); + } + + static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) { + if (V2->isNullValue()) + return 0; if (V2->isAllOnesValue() && // MIN_INT / -1 - (BuiltinType)V1->getZExtValue() == -(BuiltinType)V1->getZExtValue()) + (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue()) return 0; BuiltinType R = - (BuiltinType)V1->getZExtValue() / (BuiltinType)V2->getZExtValue(); + (BuiltinType)(V1->getSExtValue() / V2->getSExtValue()); return ConstantInt::get(*Ty, R); } - static Constant *Rem(const ConstantInt *V1, - const ConstantInt *V2) { + static Constant *Rem(const ConstantInt *V1, const ConstantInt *V2) { if (V2->isNullValue()) return 0; // X / 0 if (V2->isAllOnesValue() && // MIN_INT / -1 (BuiltinType)V1->getZExtValue() == -(BuiltinType)V1->getZExtValue()) @@ -615,7 +638,7 @@ struct VISIBILITY_HIDDEN DirectFPRules (BuiltinType)V2->getValue()); return ConstantFP::get(*Ty, Result); } - static Constant *Div(const ConstantFP *V1, const ConstantFP *V2) { + static Constant *FDiv(const ConstantFP *V1, const ConstantFP *V2) { BuiltinType inf = std::numeric_limits<BuiltinType>::infinity(); if (V2->isExactlyValue(0.0)) return ConstantFP::get(*Ty, inf); if (V2->isExactlyValue(-0.0)) return ConstantFP::get(*Ty, -inf); @@ -1224,7 +1247,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break; case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break; case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break; - case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break; + case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break; + case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break; + case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break; case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break; case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break; case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break; @@ -1307,7 +1332,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, case Instruction::Mul: case Instruction::And: return Constant::getNullValue(V1->getType()); - case Instruction::Div: + case Instruction::UDiv: + case Instruction::SDiv: + case Instruction::FDiv: case Instruction::Rem: if (!isa<UndefValue>(V2)) // undef/X -> 0 return Constant::getNullValue(V1->getType()); @@ -1358,7 +1385,8 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, if (CI->getZExtValue() == 1) return const_cast<Constant*>(V1); // X * 1 == X break; - case Instruction::Div: + case Instruction::UDiv: + case Instruction::SDiv: if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2)) if (CI->getZExtValue() == 1) return const_cast<Constant*>(V1); // X / 1 == X @@ -1419,7 +1447,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, case Instruction::Shl: case Instruction::Shr: case Instruction::Sub: - case Instruction::Div: + case Instruction::SDiv: + case Instruction::UDiv: + case Instruction::FDiv: case Instruction::Rem: default: // These instructions cannot be flopped around. break; diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 05b444573e..2c996f5301 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -75,7 +75,9 @@ bool Constant::canTrap() const { switch (CE->getOpcode()) { default: return false; - case Instruction::Div: + case Instruction::UDiv: + case Instruction::SDiv: + case Instruction::FDiv: case Instruction::Rem: // Div and rem can trap if the RHS is not known to be non-zero. if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue()) @@ -446,8 +448,14 @@ Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { return get(Instruction::Mul, C1, C2); } -Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) { - return get(Instruction::Div, C1, C2); +Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) { + return get(Instruction::UDiv, C1, C2); +} +Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) { + return get(Instruction::SDiv, C1, C2); +} +Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) { + return get(Instruction::FDiv, C1, C2); } Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) { return get(Instruction::Rem, C1, C2); @@ -1437,13 +1445,27 @@ Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { #ifndef NDEBUG switch (Opcode) { case Instruction::Add: case Instruction::Sub: - case Instruction::Mul: case Instruction::Div: + case Instruction::Mul: case Instruction::Rem: assert(C1->getType() == C2->getType() && "Op types should be identical!"); assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() || isa<PackedType>(C1->getType())) && "Tried to create an arithmetic operation on a non-arithmetic type!"); break; + + case Instruction::UDiv: + case Instruction::SDiv: + assert(C1->getType() == C2->getType() && "Op types should be identical!"); + assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) && + cast<PackedType>(C1->getType())->getElementType()->isInteger())) && + "Tried to create an arithmetic operation on a non-arithmetic type!"); + break; + case Instruction::FDiv: + assert(C1->getType() == C2->getType() && "Op types should be identical!"); + assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType()) + && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint())) + && "Tried to create an arithmetic operation on a non-arithmetic type!"); + break; case Instruction::And: case Instruction::Or: case Instruction::Xor: diff --git a/lib/VMCore/Instruction.cpp b/lib/VMCore/Instruction.cpp index b2951461d7..c5e8e30354 100644 --- a/lib/VMCore/Instruction.cpp +++ b/lib/VMCore/Instruction.cpp @@ -94,7 +94,9 @@ const char *Instruction::getOpcodeName(unsigned OpCode) { case Add: return "add"; case Sub: return "sub"; case Mul: return "mul"; - case Div: return "div"; + case UDiv: return "udiv"; + case SDiv: return "sdiv"; + case FDiv: return "fdiv"; case Rem: return "rem"; // Logical operators... @@ -221,7 +223,9 @@ bool Instruction::isComparison(unsigned op) { /// bool Instruction::isTrapping(unsigned op) { switch(op) { - case Div: + case UDiv: + case SDiv: + case FDiv: case Rem: case Load: case Store: diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 800eb9cd1d..790f6ac8a6 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -1022,7 +1022,7 @@ void BinaryOperator::init(BinaryOps iType) #ifndef NDEBUG switch (iType) { case Add: case Sub: - case Mul: case Div: + case Mul: case Rem: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); @@ -1030,6 +1030,22 @@ void BinaryOperator::init(BinaryOps iType) isa<PackedType>(getType())) && "Tried to create an arithmetic operation on a non-arithmetic type!"); break; + case UDiv: + case SDiv: + assert(getType() == LHS->getType() && + "Arithmetic operation should return same type as operands!"); + assert((getType()->isInteger() || (isa<PackedType>(getType()) && + cast<PackedType>(getType())->getElementType()->isInteger())) && + "Incorrect operand type (not integer) for S/UDIV"); + break; + case FDiv: + assert(getType() == LHS->getType() && + "Arithmetic operation should return same type as operands!"); + assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) && + cast<PackedType>(getType())->getElementType()->isFloatingPoint())) + && "Incorrect operand type (not floating point) for FDIV"); + break; + case And: case Or: case Xor: assert(getType() == LHS->getType() && |