llvm.org GIT mirror llvm / b80f778
Get rid of an optimization in SCCP which appears to have many issues. Specifically, it doesn't handle many cases involving undef correctly, and it is missing other checks which lead to it trying to re-mark a value marked as a constant with a different value. It also appears to trigger very rarely. Fixes PR11357. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@144352 91177308-0d34-0410-b5e6-96231b3b80d8 Eli Friedman 8 years ago
2 changed file(s) with 1 addition(s) and 187 deletion(s). Raw diff Collapse all Expand all
200200
201201 SmallVector BBWorkList; // The BasicBlock work list
202202
203 /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
204 /// overdefined, despite the fact that the PHI node is overdefined.
205 std::multimap UsersOfOverdefinedPHIs;
206
207203 /// KnownFeasibleEdges - Entries in this set are edges which have already had
208204 /// PHI nodes retriggered.
209205 typedef std::pair Edge;
464460 void OperandChangedState(Instruction *I) {
465461 if (BBExecutable.count(I->getParent())) // Inst is executable?
466462 visit(*I);
467 }
468
469 /// RemoveFromOverdefinedPHIs - If I has any entries in the
470 /// UsersOfOverdefinedPHIs map for PN, remove them now.
471 void RemoveFromOverdefinedPHIs(Instruction *I, PHINode *PN) {
472 if (UsersOfOverdefinedPHIs.empty()) return;
473 typedef std::multimap::iterator ItTy;
474 std::pair Range = UsersOfOverdefinedPHIs.equal_range(PN);
475 for (ItTy It = Range.first, E = Range.second; It != E;) {
476 if (It->second == I)
477 UsersOfOverdefinedPHIs.erase(It++);
478 else
479 ++It;
480 }
481 }
482
483 /// InsertInOverdefinedPHIs - Insert an entry in the UsersOfOverdefinedPHIS
484 /// map for I and PN, but if one is there already, do not create another.
485 /// (Duplicate entries do not break anything directly, but can lead to
486 /// exponential growth of the table in rare cases.)
487 void InsertInOverdefinedPHIs(Instruction *I, PHINode *PN) {
488 typedef std::multimap::iterator ItTy;
489 std::pair Range = UsersOfOverdefinedPHIs.equal_range(PN);
490 for (ItTy J = Range.first, E = Range.second; J != E; ++J)
491 if (J->second == I)
492 return;
493 UsersOfOverdefinedPHIs.insert(std::make_pair(PN, I));
494463 }
495464
496465 private:
699668 if (PN.getType()->isStructTy())
700669 return markAnythingOverdefined(&PN);
701670
702 if (getValueState(&PN).isOverdefined()) {
703 // There may be instructions using this PHI node that are not overdefined
704 // themselves. If so, make sure that they know that the PHI node operand
705 // changed.
706 typedef std::multimap::iterator ItTy;
707 std::pair Range = UsersOfOverdefinedPHIs.equal_range(&PN);
708
709 if (Range.first == Range.second)
710 return;
711
712 SmallVector Users;
713 for (ItTy I = Range.first, E = Range.second; I != E; ++I)
714 Users.push_back(I->second);
715 while (!Users.empty())
716 visit(Users.pop_back_val());
671 if (getValueState(&PN).isOverdefined())
717672 return; // Quick exit
718 }
719673
720674 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
721675 // and slow us down a lot. Just mark them overdefined.
958912 }
959913
960914
961 // If both operands are PHI nodes, it is possible that this instruction has
962 // a constant value, despite the fact that the PHI node doesn't. Check for
963 // this condition now.
964 if (PHINode *PN1 = dyn_cast(I.getOperand(0)))
965 if (PHINode *PN2 = dyn_cast(I.getOperand(1)))
966 if (PN1->getParent() == PN2->getParent()) {
967 // Since the two PHI nodes are in the same basic block, they must have
968 // entries for the same predecessors. Walk the predecessor list, and
969 // if all of the incoming values are constants, and the result of
970 // evaluating this expression with all incoming value pairs is the
971 // same, then this expression is a constant even though the PHI node
972 // is not a constant!
973 LatticeVal Result;
974 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
975 LatticeVal In1 = getValueState(PN1->getIncomingValue(i));
976 BasicBlock *InBlock = PN1->getIncomingBlock(i);
977 LatticeVal In2 =getValueState(PN2->getIncomingValueForBlock(InBlock));
978
979 if (In1.isOverdefined() || In2.isOverdefined()) {
980 Result.markOverdefined();
981 break; // Cannot fold this operation over the PHI nodes!
982 }
983
984 if (In1.isConstant() && In2.isConstant()) {
985 Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(),
986 In2.getConstant());
987 if (Result.isUndefined())
988 Result.markConstant(V);
989 else if (Result.isConstant() && Result.getConstant() != V) {
990 Result.markOverdefined();
991 break;
992 }
993 }
994 }
995
996 // If we found a constant value here, then we know the instruction is
997 // constant despite the fact that the PHI nodes are overdefined.
998 if (Result.isConstant()) {
999 markConstant(IV, &I, Result.getConstant());
1000 // Remember that this instruction is virtually using the PHI node
1001 // operands.
1002 InsertInOverdefinedPHIs(&I, PN1);
1003 InsertInOverdefinedPHIs(&I, PN2);
1004 return;
1005 }
1006
1007 if (Result.isUndefined())
1008 return;
1009
1010 // Okay, this really is overdefined now. Since we might have
1011 // speculatively thought that this was not overdefined before, and
1012 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
1013 // make sure to clean out any entries that we put there, for
1014 // efficiency.
1015 RemoveFromOverdefinedPHIs(&I, PN1);
1016 RemoveFromOverdefinedPHIs(&I, PN2);
1017 }
1018
1019915 markOverdefined(&I);
1020916 }
1021917
1036932 if (!V1State.isOverdefined() && !V2State.isOverdefined())
1037933 return;
1038934
1039 // If something is overdefined, use some tricks to avoid ending up and over
1040 // defined if we can.
1041
1042 // If both operands are PHI nodes, it is possible that this instruction has
1043 // a constant value, despite the fact that the PHI node doesn't. Check for
1044 // this condition now.
1045 if (PHINode *PN1 = dyn_cast(I.getOperand(0)))
1046 if (PHINode *PN2 = dyn_cast(I.getOperand(1)))
1047 if (PN1->getParent() == PN2->getParent()) {
1048 // Since the two PHI nodes are in the same basic block, they must have
1049 // entries for the same predecessors. Walk the predecessor list, and
1050 // if all of the incoming values are constants, and the result of
1051 // evaluating this expression with all incoming value pairs is the
1052 // same, then this expression is a constant even though the PHI node
1053 // is not a constant!
1054 LatticeVal Result;
1055 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
1056 LatticeVal In1 = getValueState(PN1->getIncomingValue(i));
1057 BasicBlock *InBlock = PN1->getIncomingBlock(i);
1058 LatticeVal In2 =getValueState(PN2->getIncomingValueForBlock(InBlock));
1059
1060 if (In1.isOverdefined() || In2.isOverdefined()) {
1061 Result.markOverdefined();
1062 break; // Cannot fold this operation over the PHI nodes!
1063 }
1064
1065 if (In1.isConstant() && In2.isConstant()) {
1066 Constant *V = ConstantExpr::getCompare(I.getPredicate(),
1067 In1.getConstant(),
1068 In2.getConstant());
1069 if (Result.isUndefined())
1070 Result.markConstant(V);
1071 else if (Result.isConstant() && Result.getConstant() != V) {
1072 Result.markOverdefined();
1073 break;
1074 }
1075 }
1076 }
1077
1078 // If we found a constant value here, then we know the instruction is
1079 // constant despite the fact that the PHI nodes are overdefined.
1080 if (Result.isConstant()) {
1081 markConstant(&I, Result.getConstant());
1082 // Remember that this instruction is virtually using the PHI node
1083 // operands.
1084 InsertInOverdefinedPHIs(&I, PN1);
1085 InsertInOverdefinedPHIs(&I, PN2);
1086 return;
1087 }
1088
1089 if (Result.isUndefined())
1090 return;
1091
1092 // Okay, this really is overdefined now. Since we might have
1093 // speculatively thought that this was not overdefined before, and
1094 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
1095 // make sure to clean out any entries that we put there, for
1096 // efficiency.
1097 RemoveFromOverdefinedPHIs(&I, PN1);
1098 RemoveFromOverdefinedPHIs(&I, PN2);
1099 }
1100
1101935 markOverdefined(&I);
1102936 }
1103937
+0
-20
test/Transforms/SCCP/phitest.ll less more
None ; RUN: opt < %s -sccp -dce -simplifycfg -S | not grep br
1
2 define i32 @test(i32 %param) {
3 entry:
4 %tmp.1 = icmp ne i32 %param, 0 ; [#uses=1]
5 br i1 %tmp.1, label %endif.0, label %else
6 else: ; preds = %entry
7 br label %endif.0
8 endif.0: ; preds = %else, %entry
9 %a.0 = phi i32 [ 2, %else ], [ 3, %entry ] ; [#uses=1]
10 %b.0 = phi i32 [ 3, %else ], [ 2, %entry ] ; [#uses=1]
11 %tmp.5 = add i32 %a.0, %b.0 ; [#uses=1]
12 %tmp.7 = icmp ne i32 %tmp.5, 5 ; [#uses=1]
13 br i1 %tmp.7, label %UnifiedReturnBlock, label %endif.1
14 endif.1: ; preds = %endif.0
15 ret i32 0
16 UnifiedReturnBlock: ; preds = %endif.0
17 ret i32 2
18 }
19