llvm.org GIT mirror llvm / 4133415
NewGVN: Fix PR 33119, PR 33129, due to regressed undef handling Fix PR33120 and others by eliminating self-cycles a different way. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303875 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 2 years ago
2 changed file(s) with 44 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
857857
858858 // Filter out unreachable phi operands.
859859 auto Filtered = make_filter_range(PHIOperands, [&](const Use *U) {
860 return ReachableEdges.count({PN->getIncomingBlock(*U), PHIBlock});
860 if (*U == PN)
861 return false;
862 if (!ReachableEdges.count({PN->getIncomingBlock(*U), PHIBlock}))
863 return false;
864 // Things in TOPClass are equivalent to everything.
865 if (ValueToClass.lookup(*U) == TOPClass)
866 return false;
867 return true;
861868 });
862869 std::transform(Filtered.begin(), Filtered.end(), op_inserter(E),
863870 [&](const Use *U) -> Value * {
865872 HasBackedge = HasBackedge || isBackedge(BB, PHIBlock);
866873 OriginalOpsConstant =
867874 OriginalOpsConstant && isa(*U);
868 // Use nullptr to distinguish between things that were
869 // originally self-defined and those that have an operand
870 // leader that is self-defined.
871 if (*U == PN)
872 return nullptr;
873 // Things in TOPClass are equivalent to everything.
874 if (ValueToClass.lookup(*U) == TOPClass)
875 return nullptr;
876875 return lookupOperandLeader(*U);
877876 });
878877 return E;
15841583
15851584 // Evaluate PHI nodes symbolically, and create an expression result.
15861585 const Expression *NewGVN::performSymbolicPHIEvaluation(Instruction *I) const {
1586 // Resolve irreducible and reducible phi cycles.
1587 // FIXME: This is hopefully a temporary solution while we resolve the issues
1588 // with fixpointing self-cycles. It currently should be "guaranteed" to be
1589 // correct, but non-optimal. The SCCFinder does not, for example, take
1590 // reachability of arguments into account, etc.
1591 SCCFinder.Start(I);
1592 bool CanOptimize = true;
1593 SmallPtrSet OuterOps;
1594
1595 auto &Component = SCCFinder.getComponentFor(I);
1596 for (auto *Member : Component) {
1597 if (!isa(Member)) {
1598 CanOptimize = false;
1599 break;
1600 }
1601 for (auto &PHIOp : cast(Member)->operands())
1602 if (!isa(PHIOp) || !Component.count(cast(PHIOp)))
1603 OuterOps.insert(PHIOp);
1604 }
1605 if (CanOptimize && OuterOps.size() == 1) {
1606 DEBUG(dbgs() << "Resolving cyclic phi to value " << *(*OuterOps.begin())
1607 << "\n");
1608 return createVariableOrConstant(*OuterOps.begin());
1609 }
15871610 // True if one of the incoming phi edges is a backedge.
15881611 bool HasBackedge = false;
15891612 // All constant tracks the state of whether all the *original* phi operands
15971620 // See if all arguments are the same.
15981621 // We track if any were undef because they need special handling.
15991622 bool HasUndef = false;
1600 bool CycleFree = isCycleFree(I);
16011623 auto Filtered = make_filter_range(E->operands(), [&](Value *Arg) {
1602 if (Arg == nullptr)
1603 return false;
1604 // Original self-operands are already eliminated during expression creation.
1605 // We can only eliminate value-wise self-operands if it's cycle
1606 // free. Otherwise, eliminating the operand can cause our value to change,
1607 // which can cause us to not eliminate the operand, which changes the value
1608 // back to what it was before, cycling forever.
1609 if (CycleFree && Arg == I)
1610 return false;
16111624 if (isa(Arg)) {
16121625 HasUndef = true;
16131626 return false;
16161629 });
16171630 // If we are left with no operands, it's dead.
16181631 if (Filtered.begin() == Filtered.end()) {
1632 // If it has undef at this point, it means there are no-non-undef arguments,
1633 // and thus, the value of the phi node must be undef.
1634 if (HasUndef) {
1635 DEBUG(dbgs() << "PHI Node " << *I
1636 << " has no non-undef arguments, valuing it as undef\n");
1637 return createConstantExpression(UndefValue::get(I->getType()));
1638 }
1639
16191640 DEBUG(dbgs() << "No arguments of PHI node " << *I << " are live\n");
16201641 deleteExpression(E);
16211642 return createDeadExpression();
16451666 // constants, or all operands are ignored but the undef, it also must be
16461667 // cycle free.
16471668 if (!AllConstant && HasBackedge && NumOps > 0 &&
1648 !isa(AllSameValue) && !CycleFree)
1669 !isa(AllSameValue) && !isCycleFree(I))
16491670 return E;
16501671
16511672 // Only have to check for instructions
35593580 // Map to store the use counts
35603581 DenseMap UseCounts;
35613582 for (auto *CC : reverse(CongruenceClasses)) {
3583 DEBUG(dbgs() << "Eliminating in congruence class " << CC->getID() << "\n");
35623584 // Track the equivalent store info so we can decide whether to try
35633585 // dead store elimination.
35643586 SmallVector PossibleDeadStores;
36053627 }
36063628 CC->swap(MembersLeft);
36073629 } else {
3608 DEBUG(dbgs() << "Eliminating in congruence class " << CC->getID()
3609 << "\n");
36103630 // If this is a singleton, we can skip it.
36113631 if (CC->size() != 1 || RealToTemp.lookup(Leader)) {
36123632 // This is a stack because equality replacement/etc may place
1616 ; CHECK-NEXT: [[INC_I:%.*]] = add nsw i32 [[REFIDXLX_0]], 1
1717 ; CHECK-NEXT: br label [[FOR_BODY8_I:%.*]]
1818 ; CHECK: for.body8.i:
19 ; CHECK-NEXT: br i1 undef, label [[FOR_INC24_I:%.*]], label [[IF_THEN17_I:%.*]]
19 ; CHECK-NEXT: [[NIDX_052_I:%.*]] = phi i32 [ [[INC_I]], [[IF_THEN13]] ], [ [[NIDX_052_I]], [[FOR_INC24_I:%.*]] ]
20 ; CHECK-NEXT: br i1 undef, label [[FOR_INC24_I]], label [[IF_THEN17_I:%.*]]
2021 ; CHECK: if.then17.i:
2122 ; CHECK-NEXT: br label [[FOR_INC24_I]]
2223 ; CHECK: for.inc24.i: