@@ -606,8 +606,8 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
606
606
SmallVectorImpl<ValueEntry> &Ops) {
607
607
assert (Ops.size () > 1 && " Single values should be used directly!" );
608
608
609
- // Since our optimizations never increase the number of operations, the new
610
- // expression can always be written by reusing the existing binary operators
609
+ // Since our optimizations should never increase the number of operations, the
610
+ // new expression can usually be written reusing the existing binary operators
611
611
// from the original expression tree, without creating any new instructions,
612
612
// though the rewritten expression may have a completely different topology.
613
613
// We take care to not change anything if the new expression will be the same
@@ -621,6 +621,20 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
621
621
unsigned Opcode = I->getOpcode ();
622
622
BinaryOperator *Op = I;
623
623
624
+ // / NotRewritable - The operands being written will be the leaves of the new
625
+ // / expression and must not be used as inner nodes (via NodesToRewrite) by
626
+ // / mistake. Inner nodes are always reassociable, and usually leaves are not
627
+ // / (if they were they would have been incorporated into the expression and so
628
+ // / would not be leaves), so most of the time there is no danger of this. But
629
+ // / in rare cases a leaf may become reassociable if an optimization kills uses
630
+ // / of it, or it may momentarily become reassociable during rewriting (below)
631
+ // / due it being removed as an operand of one of its uses. Ensure that misuse
632
+ // / of leaf nodes as inner nodes cannot occur by remembering all of the future
633
+ // / leaves and refusing to reuse any of them as inner nodes.
634
+ SmallPtrSet<Value*, 8 > NotRewritable;
635
+ for (unsigned i = 0 , e = Ops.size (); i != e; ++i)
636
+ NotRewritable.insert (Ops[i].Op );
637
+
624
638
// ExpressionChanged - Non-null if the rewritten expression differs from the
625
639
// original in some non-trivial way, requiring the clearing of optional flags.
626
640
// Flags are cleared from the operator in ExpressionChanged up to I inclusive.
@@ -653,12 +667,14 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
653
667
// the old operands with the new ones.
654
668
DEBUG (dbgs () << " RA: " << *Op << ' \n ' );
655
669
if (NewLHS != OldLHS) {
656
- if (BinaryOperator *BO = isReassociableOp (OldLHS, Opcode))
670
+ BinaryOperator *BO = isReassociableOp (OldLHS, Opcode);
671
+ if (BO && !NotRewritable.count (BO))
657
672
NodesToRewrite.push_back (BO);
658
673
Op->setOperand (0 , NewLHS);
659
674
}
660
675
if (NewRHS != OldRHS) {
661
- if (BinaryOperator *BO = isReassociableOp (OldRHS, Opcode))
676
+ BinaryOperator *BO = isReassociableOp (OldRHS, Opcode);
677
+ if (BO && !NotRewritable.count (BO))
662
678
NodesToRewrite.push_back (BO);
663
679
Op->setOperand (1 , NewRHS);
664
680
}
@@ -682,7 +698,8 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
682
698
Op->swapOperands ();
683
699
} else {
684
700
// Overwrite with the new right-hand side.
685
- if (BinaryOperator *BO = isReassociableOp (Op->getOperand (1 ), Opcode))
701
+ BinaryOperator *BO = isReassociableOp (Op->getOperand (1 ), Opcode);
702
+ if (BO && !NotRewritable.count (BO))
686
703
NodesToRewrite.push_back (BO);
687
704
Op->setOperand (1 , NewRHS);
688
705
ExpressionChanged = Op;
@@ -695,7 +712,8 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
695
712
// Now deal with the left-hand side. If this is already an operation node
696
713
// from the original expression then just rewrite the rest of the expression
697
714
// into it.
698
- if (BinaryOperator *BO = isReassociableOp (Op->getOperand (0 ), Opcode)) {
715
+ BinaryOperator *BO = isReassociableOp (Op->getOperand (0 ), Opcode);
716
+ if (BO && !NotRewritable.count (BO)) {
699
717
Op = BO;
700
718
continue ;
701
719
}
0 commit comments