summaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/Reassociate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Transforms/Scalar/Reassociate.cpp')
-rw-r--r--lib/Transforms/Scalar/Reassociate.cpp54
1 files changed, 22 insertions, 32 deletions
diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp
index 1df0a9c49fb1..cb893eab1654 100644
--- a/lib/Transforms/Scalar/Reassociate.cpp
+++ b/lib/Transforms/Scalar/Reassociate.cpp
@@ -63,6 +63,7 @@
using namespace llvm;
using namespace reassociate;
+using namespace PatternMatch;
#define DEBUG_TYPE "reassociate"
@@ -125,10 +126,10 @@ XorOpnd::XorOpnd(Value *V) {
Value *V0 = I->getOperand(0);
Value *V1 = I->getOperand(1);
const APInt *C;
- if (match(V0, PatternMatch::m_APInt(C)))
+ if (match(V0, m_APInt(C)))
std::swap(V0, V1);
- if (match(V1, PatternMatch::m_APInt(C))) {
+ if (match(V1, m_APInt(C))) {
ConstPart = *C;
SymbolicPart = V0;
isOr = (I->getOpcode() == Instruction::Or);
@@ -204,10 +205,10 @@ unsigned ReassociatePass::getRank(Value *V) {
for (unsigned i = 0, e = I->getNumOperands(); i != e && Rank != MaxRank; ++i)
Rank = std::max(Rank, getRank(I->getOperand(i)));
- // If this is a not or neg instruction, do not count it for rank. This
+ // If this is a 'not' or 'neg' instruction, do not count it for rank. This
// assures us that X and ~X will have the same rank.
- if (!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I) &&
- !BinaryOperator::isFNeg(I))
+ if (!match(I, m_Not(m_Value())) && !match(I, m_Neg(m_Value())) &&
+ !match(I, m_FNeg(m_Value())))
++Rank;
LLVM_DEBUG(dbgs() << "Calculated Rank[" << V->getName() << "] = " << Rank
@@ -573,8 +574,8 @@ static bool LinearizeExprTree(BinaryOperator *I,
// If this is a multiply expression, turn any internal negations into
// multiplies by -1 so they can be reassociated.
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op))
- if ((Opcode == Instruction::Mul && BinaryOperator::isNeg(BO)) ||
- (Opcode == Instruction::FMul && BinaryOperator::isFNeg(BO))) {
+ if ((Opcode == Instruction::Mul && match(BO, m_Neg(m_Value()))) ||
+ (Opcode == Instruction::FMul && match(BO, m_FNeg(m_Value())))) {
LLVM_DEBUG(dbgs()
<< "MORPH LEAF: " << *Op << " (" << Weight << ") TO ");
BO = LowerNegateToMultiply(BO);
@@ -788,13 +789,7 @@ void ReassociatePass::RewriteExprTree(BinaryOperator *I,
// Discard any debug info related to the expressions that has changed (we
// can leave debug infor related to the root, since the result of the
// expression tree should be the same even after reassociation).
- SmallVector<DbgInfoIntrinsic *, 1> DbgUsers;
- findDbgUsers(DbgUsers, ExpressionChanged);
- for (auto *DII : DbgUsers) {
- Value *Undef = UndefValue::get(ExpressionChanged->getType());
- DII->setOperand(0, MetadataAsValue::get(DII->getContext(),
- ValueAsMetadata::get(Undef)));
- }
+ replaceDbgUsesWithUndef(ExpressionChanged);
ExpressionChanged->moveBefore(I);
ExpressionChanged = cast<BinaryOperator>(*ExpressionChanged->user_begin());
@@ -854,7 +849,7 @@ static Value *NegateValue(Value *V, Instruction *BI,
// Okay, we need to materialize a negated version of V with an instruction.
// Scan the use lists of V to see if we have one already.
for (User *U : V->users()) {
- if (!BinaryOperator::isNeg(U) && !BinaryOperator::isFNeg(U))
+ if (!match(U, m_Neg(m_Value())) && !match(U, m_FNeg(m_Value())))
continue;
// We found one! Now we have to make sure that the definition dominates
@@ -899,7 +894,7 @@ static Value *NegateValue(Value *V, Instruction *BI,
/// Return true if we should break up this subtract of X-Y into (X + -Y).
static bool ShouldBreakUpSubtract(Instruction *Sub) {
// If this is a negation, we can't split it up!
- if (BinaryOperator::isNeg(Sub) || BinaryOperator::isFNeg(Sub))
+ if (match(Sub, m_Neg(m_Value())) || match(Sub, m_FNeg(m_Value())))
return false;
// Don't breakup X - undef.
@@ -1113,8 +1108,8 @@ static Value *OptimizeAndOrXor(unsigned Opcode,
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
// First, check for X and ~X in the operand list.
assert(i < Ops.size());
- if (BinaryOperator::isNot(Ops[i].Op)) { // Cannot occur for ^.
- Value *X = BinaryOperator::getNotArgument(Ops[i].Op);
+ Value *X;
+ if (match(Ops[i].Op, m_Not(m_Value(X)))) { // Cannot occur for ^.
unsigned FoundX = FindInOperandList(Ops, i, X);
if (FoundX != i) {
if (Opcode == Instruction::And) // ...&X&~X = 0
@@ -1304,7 +1299,7 @@ Value *ReassociatePass::OptimizeXor(Instruction *I,
Value *V = Ops[i].Op;
const APInt *C;
// TODO: Support non-splat vectors.
- if (match(V, PatternMatch::m_APInt(C))) {
+ if (match(V, m_APInt(C))) {
ConstOpnd ^= *C;
} else {
XorOpnd O(V);
@@ -1460,27 +1455,22 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I,
}
// Check for X and -X or X and ~X in the operand list.
- if (!BinaryOperator::isNeg(TheOp) && !BinaryOperator::isFNeg(TheOp) &&
- !BinaryOperator::isNot(TheOp))
+ Value *X;
+ if (!match(TheOp, m_Neg(m_Value(X))) && !match(TheOp, m_Not(m_Value(X))) &&
+ !match(TheOp, m_FNeg(m_Value(X))))
continue;
- Value *X = nullptr;
- if (BinaryOperator::isNeg(TheOp) || BinaryOperator::isFNeg(TheOp))
- X = BinaryOperator::getNegArgument(TheOp);
- else if (BinaryOperator::isNot(TheOp))
- X = BinaryOperator::getNotArgument(TheOp);
-
unsigned FoundX = FindInOperandList(Ops, i, X);
if (FoundX == i)
continue;
// Remove X and -X from the operand list.
if (Ops.size() == 2 &&
- (BinaryOperator::isNeg(TheOp) || BinaryOperator::isFNeg(TheOp)))
+ (match(TheOp, m_Neg(m_Value())) || match(TheOp, m_FNeg(m_Value()))))
return Constant::getNullValue(X->getType());
// Remove X and ~X from the operand list.
- if (Ops.size() == 2 && BinaryOperator::isNot(TheOp))
+ if (Ops.size() == 2 && match(TheOp, m_Not(m_Value())))
return Constant::getAllOnesValue(X->getType());
Ops.erase(Ops.begin()+i);
@@ -1494,7 +1484,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I,
e -= 2; // Removed two elements.
// if X and ~X we append -1 to the operand list.
- if (BinaryOperator::isNot(TheOp)) {
+ if (match(TheOp, m_Not(m_Value()))) {
Value *V = Constant::getAllOnesValue(X->getType());
Ops.insert(Ops.end(), ValueEntry(getRank(V), V));
e += 1;
@@ -2058,7 +2048,7 @@ void ReassociatePass::OptimizeInst(Instruction *I) {
RedoInsts.insert(I);
MadeChange = true;
I = NI;
- } else if (BinaryOperator::isNeg(I)) {
+ } else if (match(I, m_Neg(m_Value()))) {
// Otherwise, this is a negation. See if the operand is a multiply tree
// and if this is not an inner node of a multiply tree.
if (isReassociableOp(I->getOperand(1), Instruction::Mul) &&
@@ -2082,7 +2072,7 @@ void ReassociatePass::OptimizeInst(Instruction *I) {
RedoInsts.insert(I);
MadeChange = true;
I = NI;
- } else if (BinaryOperator::isFNeg(I)) {
+ } else if (match(I, m_FNeg(m_Value()))) {
// Otherwise, this is a negation. See if the operand is a multiply tree
// and if this is not an inner node of a multiply tree.
if (isReassociableOp(I->getOperand(1), Instruction::FMul) &&