Skip to content

Commit df46b90

Browse files
committed
Merging r168291: into the 3.2 release branch.
Fix PR14060, an infinite loop in reassociate. The problem was that one of the operands of the expression being written was wrongly thought to be reusable as an inner node of the expression resulting in it turning up as both an inner node *and* a leaf, creating a cycle in the def-use graph. This would have caused the verifier to blow up if things had gotten that far, however it managed to provoke an infinite loop first. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_32@168489 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d11161f commit df46b90

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

lib/Transforms/Scalar/Reassociate.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,8 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
606606
SmallVectorImpl<ValueEntry> &Ops) {
607607
assert(Ops.size() > 1 && "Single values should be used directly!");
608608

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
611611
// from the original expression tree, without creating any new instructions,
612612
// though the rewritten expression may have a completely different topology.
613613
// We take care to not change anything if the new expression will be the same
@@ -621,6 +621,20 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
621621
unsigned Opcode = I->getOpcode();
622622
BinaryOperator *Op = I;
623623

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+
624638
// ExpressionChanged - Non-null if the rewritten expression differs from the
625639
// original in some non-trivial way, requiring the clearing of optional flags.
626640
// Flags are cleared from the operator in ExpressionChanged up to I inclusive.
@@ -653,12 +667,14 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
653667
// the old operands with the new ones.
654668
DEBUG(dbgs() << "RA: " << *Op << '\n');
655669
if (NewLHS != OldLHS) {
656-
if (BinaryOperator *BO = isReassociableOp(OldLHS, Opcode))
670+
BinaryOperator *BO = isReassociableOp(OldLHS, Opcode);
671+
if (BO && !NotRewritable.count(BO))
657672
NodesToRewrite.push_back(BO);
658673
Op->setOperand(0, NewLHS);
659674
}
660675
if (NewRHS != OldRHS) {
661-
if (BinaryOperator *BO = isReassociableOp(OldRHS, Opcode))
676+
BinaryOperator *BO = isReassociableOp(OldRHS, Opcode);
677+
if (BO && !NotRewritable.count(BO))
662678
NodesToRewrite.push_back(BO);
663679
Op->setOperand(1, NewRHS);
664680
}
@@ -682,7 +698,8 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
682698
Op->swapOperands();
683699
} else {
684700
// 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))
686703
NodesToRewrite.push_back(BO);
687704
Op->setOperand(1, NewRHS);
688705
ExpressionChanged = Op;
@@ -695,7 +712,8 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
695712
// Now deal with the left-hand side. If this is already an operation node
696713
// from the original expression then just rewrite the rest of the expression
697714
// 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)) {
699717
Op = BO;
700718
continue;
701719
}

test/Transforms/Reassociate/crash.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,22 @@ define i32 @bar(i32 %arg, i32 %arg1, i32 %arg2) {
153153
%ret = add i32 %tmp2, %tmp3
154154
ret i32 %ret
155155
}
156+
157+
; PR14060
158+
define i8 @hang(i8 %p, i8 %p0, i8 %p1, i8 %p2, i8 %p3, i8 %p4, i8 %p5, i8 %p6, i8 %p7, i8 %p8, i8 %p9) {
159+
%tmp = zext i1 false to i8
160+
%tmp16 = or i8 %tmp, 1
161+
%tmp22 = or i8 %p7, %p0
162+
%tmp23 = or i8 %tmp16, %tmp22
163+
%tmp28 = or i8 %p9, %p1
164+
%tmp31 = or i8 %tmp23, %p2
165+
%tmp32 = or i8 %tmp31, %tmp28
166+
%tmp38 = or i8 %p8, %p3
167+
%tmp39 = or i8 %tmp16, %tmp38
168+
%tmp43 = or i8 %tmp39, %p4
169+
%tmp44 = or i8 %tmp43, 1
170+
%tmp47 = or i8 %tmp32, %p5
171+
%tmp50 = or i8 %tmp47, %p6
172+
%tmp51 = or i8 %tmp44, %tmp50
173+
ret i8 %tmp51
174+
}

0 commit comments

Comments
 (0)