summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2018-07-06 15:18:58 +0000
committerSanjay Patel <spatel@rotateright.com>2018-07-06 15:18:58 +0000
commite85a300a77cf66921e949a4cf595eba52c637198 (patch)
tree3ccb66d2cf11293d89f6e366734c0505fc88613b /llvm/lib
parent475dc9690182eaf94dccf631632964180fdbb343 (diff)
downloadbcm5719-llvm-e85a300a77cf66921e949a4cf595eba52c637198.tar.gz
bcm5719-llvm-e85a300a77cf66921e949a4cf595eba52c637198.zip
[Constants] extend getBinOpIdentity(); NFC
The enhanced version will be used in D48893 and related patches and an almost identical (fadd is different) version is proposed in D28907, so adding this as a preliminary step. llvm-svn: 336444
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/IR/Constants.cpp65
1 files changed, 41 insertions, 24 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 032793effed..2351e7e4a38 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2271,32 +2271,49 @@ Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
isExact ? PossiblyExactOperator::IsExact : 0);
}
-// FIXME: Add a parameter to specify the operand number for non-commutative ops.
-// For example, the operand 1 identity constant for any shift is the null value
-// because shift-by-0 always returns operand 0.
-Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
- switch (Opcode) {
- default:
- // Doesn't have an identity.
- return nullptr;
-
- case Instruction::Add:
- case Instruction::Or:
- case Instruction::Xor:
- return Constant::getNullValue(Ty);
-
- case Instruction::Mul:
- return ConstantInt::get(Ty, 1);
-
- case Instruction::And:
- return Constant::getAllOnesValue(Ty);
+Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
+ bool AllowRHSConstant) {
+ assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
+
+ // Commutative opcodes: it does not matter if AllowRHSConstant is set.
+ if (Instruction::isCommutative(Opcode)) {
+ switch (Opcode) {
+ case Instruction::Add: // X + 0 = X
+ case Instruction::Or: // X | 0 = X
+ case Instruction::Xor: // X ^ 0 = X
+ return Constant::getNullValue(Ty);
+ case Instruction::Mul: // X * 1 = X
+ return ConstantInt::get(Ty, 1);
+ case Instruction::And: // X & -1 = X
+ return Constant::getAllOnesValue(Ty);
+ case Instruction::FAdd: // X + -0.0 = X
+ // TODO: If the fadd has 'nsz', should we return +0.0?
+ return ConstantFP::getNegativeZero(Ty);
+ case Instruction::FMul: // X * 1.0 = X
+ return ConstantFP::get(Ty, 1.0);
+ default:
+ llvm_unreachable("Every commutative binop has an identity constant");
+ }
+ }
- // TODO: If the fadd has 'nsz', should we return +0.0?
- case Instruction::FAdd:
- return ConstantFP::getNegativeZero(Ty);
+ // Non-commutative opcodes: AllowRHSConstant must be set.
+ if (!AllowRHSConstant)
+ return nullptr;
- case Instruction::FMul:
- return ConstantFP::get(Ty, 1.0);
+ switch (Opcode) {
+ case Instruction::Sub: // X - 0 = X
+ case Instruction::Shl: // X << 0 = X
+ case Instruction::LShr: // X >>u 0 = X
+ case Instruction::AShr: // X >> 0 = X
+ case Instruction::FSub: // X - 0.0 = X
+ return Constant::getNullValue(Ty);
+ case Instruction::SDiv: // X / 1 = X
+ case Instruction::UDiv: // X /u 1 = X
+ return ConstantInt::get(Ty, 1);
+ case Instruction::FDiv: // X / 1.0 = X
+ return ConstantFP::get(Ty, 1.0);
+ default:
+ return nullptr;
}
}
OpenPOWER on IntegriCloud