For PR950:
Make necessary changes to support DIV -> [SUF]Div. This changes llvm to
have three division instructions: signed, unsigned, floating point. The
bytecode and assembler are bacwards compatible, however.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31195 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index ad9a33f..2589739 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -40,7 +40,9 @@
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 @@
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 @@
// 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 @@
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 @@
DEF_CAST(Double, ConstantFP , double)
#undef DEF_CAST
- static Constant *Div(const ConstantInt *V1, const ConstantInt *V2) {
- if (V2->isNullValue()) return 0;
- if (V2->isAllOnesValue() && // MIN_INT / -1
- (BuiltinType)V1->getZExtValue() == -(BuiltinType)V1->getZExtValue())
+ static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) {
+ if (V2->isNullValue())
return 0;
- BuiltinType R =
- (BuiltinType)V1->getZExtValue() / (BuiltinType)V2->getZExtValue();
+ BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue());
return ConstantInt::get(*Ty, R);
}
- static Constant *Rem(const ConstantInt *V1,
- const ConstantInt *V2) {
+ static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) {
+ if (V2->isNullValue())
+ return 0;
+ if (V2->isAllOnesValue() && // MIN_INT / -1
+ (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
+ return 0;
+ BuiltinType R =
+ (BuiltinType)(V1->getSExtValue() / V2->getSExtValue());
+ return ConstantInt::get(*Ty, R);
+ }
+
+ 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 @@
(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 @@
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 @@
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 @@
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 @@
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 05b4445..2c996f5 100644
--- a/lib/VMCore/Constants.cpp
+++ b/lib/VMCore/Constants.cpp
@@ -75,7 +75,9 @@
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::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 @@
#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 b295146..c5e8e30 100644
--- a/lib/VMCore/Instruction.cpp
+++ b/lib/VMCore/Instruction.cpp
@@ -94,7 +94,9 @@
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::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 800eb9c..790f6ac 100644
--- a/lib/VMCore/Instructions.cpp
+++ b/lib/VMCore/Instructions.cpp
@@ -1022,7 +1022,7 @@
#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 @@
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() &&