/// NOTE: This routine will set operands of non-leaf non-root nodes to undef in
/// order to ensure that every non-root node in the expression has *exactly one*
/// use by a non-leaf node of the expression. This destruction means that the
-/// caller MUST use something like RewriteExprTree to put the values back in.
+/// caller MUST either replace 'I' with a new expression or use something like
+/// RewriteExprTree to put the values back in.
///
/// In the above example either the right operand of A or the left operand of B
/// will be replaced by undef. If it is B's operand then this gives:
/// / \ / \ / \ |
/// + * | F, G
///
-/// Note that if you visit operands recursively starting from a leaf node then
-/// you will never encounter such an undef operand unless you get back to 'I',
+/// Note that such undef operands can only be reached by passing through 'I'.
+/// For example, if you visit operands recursively starting from a leaf node
+/// then you will never see such an undef operand unless you get back to 'I',
/// which requires passing through a phi node.
///
/// Note that this routine may also mutate binary operators of the wrong type
unsigned Opcode = I->getOpcode();
NodesToRewrite.push_back(I);
- // ExpressionChanged - Whether the rewritten expression differs non-trivially
- // from the original, requiring the clearing of all optional flags.
- bool ExpressionChanged = false;
+ // ExpressionChanged - Non-null if the rewritten expression differs from the
+ // original in some non-trivial way, requiring the clearing of optional flags.
+ // Flags are cleared from the operator in ExpressionChanged up to I inclusive.
+ BinaryOperator *ExpressionChanged = 0;
BinaryOperator *Previous;
BinaryOperator *Op = 0;
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
assert(!NodesToRewrite.empty() &&
"Optimized expressions has more nodes than original!");
Previous = Op; Op = NodesToRewrite.pop_back_val();
- // Compactify the tree instructions together with each other to guarantee
- // that the expression tree is dominated by all of Ops.
- if (Previous)
+ if (ExpressionChanged)
+ // Compactify the tree instructions together with each other to guarantee
+ // that the expression tree is dominated by all of Ops.
Op->moveBefore(Previous);
// The last operation (which comes earliest in the IR) is special as both
}
DEBUG(dbgs() << "TO: " << *Op << '\n');
- ExpressionChanged = true;
+ ExpressionChanged = Op;
MadeChange = true;
++NumChanged;
if (BinaryOperator *BO = isReassociableOp(Op->getOperand(1), Opcode))
NodesToRewrite.push_back(BO);
Op->setOperand(1, NewRHS);
- ExpressionChanged = true;
+ ExpressionChanged = Op;
}
DEBUG(dbgs() << "TO: " << *Op << '\n');
MadeChange = true;
DEBUG(dbgs() << "RA: " << *Op << '\n');
Op->setOperand(0, NodesToRewrite.back());
DEBUG(dbgs() << "TO: " << *Op << '\n');
- ExpressionChanged = true;
+ ExpressionChanged = Op;
MadeChange = true;
++NumChanged;
}
- // If the expression changed non-trivially then clear out all subclass data in
- // the entire rewritten expression.
+ // If the expression changed non-trivially then clear out all subclass data
+ // starting from the operator specified in ExpressionChanged.
if (ExpressionChanged) {
do {
- Op->clearSubclassOptionalData();
- if (Op == I)
+ ExpressionChanged->clearSubclassOptionalData();
+ if (ExpressionChanged == I)
break;
- Op = cast<BinaryOperator>(*Op->use_begin());
+ ExpressionChanged = cast<BinaryOperator>(*ExpressionChanged->use_begin());
} while (1);
}
++Count;
if (Count == 1)
continue;
- // Move an even number of occurences to Factors.
+ // Move an even number of occurrences to Factors.
Count &= ~1U;
Idx -= Count;
FactorPowerSum += Count;
SmallVector<ValueEntry, 8> Ops;
LinearizeExprTree(I, Ops);
+ DEBUG(dbgs() << "RAIn:\t"; PrintOps(I, Ops); dbgs() << '\n');
+
// Now that we have linearized the tree to a list and have gathered all of
// the operands and their ranks, sort the operands by their rank. Use a
// stable_sort so that values with equal ranks will have their relative
// the vector.
std::stable_sort(Ops.begin(), Ops.end());
- DEBUG(dbgs() << "RAIn:\t"; PrintOps(I, Ops); dbgs() << '\n');
-
// OptimizeExpression - Now that we have the expression tree in a convenient
// sorted form, optimize it globally if possible.
if (Value *V = OptimizeExpression(I, Ops)) {