llvm.org GIT mirror llvm / 7e81ef7
NewGVN: Allow dependent PHI of ops git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314610 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 2 years ago
2 changed file(s) with 176 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
5555 #include "llvm/ADT/MapVector.h"
5656 #include "llvm/ADT/PostOrderIterator.h"
5757 #include "llvm/ADT/SmallSet.h"
58 #include "llvm/ADT/SparseBitVector.h"
5859 #include "llvm/ADT/Statistic.h"
5960 #include "llvm/Analysis/AliasAnalysis.h"
6061 #include "llvm/Analysis/AssumptionCache.h"
452453 // op(phi, phi).
453454 // These mappings just store various data that would normally be part of the
454455 // IR.
455 DenseSet> PHINodeUses;
456 SmallPtrSet> PHINodeUses;
457
456458 DenseMap OpSafeForPHIOfOps;
457459 // Map a temporary instruction we created to a parent block.
458460 DenseMap TempToBlock;
470472 mutable DenseMap> AdditionalUsers;
471473 DenseMap>
472474 ExpressionToPhiOfOps;
473 // Map from basic block to the temporary operations we created
474 DenseMap> PHIOfOpsPHIs;
475475 // Map from temporary operation to MemoryAccess.
476476 DenseMap TempToMemory;
477477 // Set of all temporary instructions we created.
480480 // RealToTemp.
481481
482482 DenseSet AllTempInstructions;
483
484 // This is the set of instructions to revisit on a reachability change. At
485 // the end of the main iteration loop it will contain at least all the phi of
486 // ops instructions that will be changed to phis, as well as regular phis.
487 // During the iteration loop, it may contain other things, such as phi of ops
488 // instructions that used edge reachability to reach a result, and so need to
489 // be revisited when the edge changes, independent of whether the phi they
490 // depended on changes.
491 DenseMap> RevisitOnReachabilityChange;
483492
484493 // Mapping from predicate info we used to the instructions we used it with.
485494 // In order to correctly ensure propagation, we must keep track of what
620629 return CClass;
621630 }
622631 void initializeCongruenceClasses(Function &F);
623 const Expression *makePossiblePhiOfOps(Instruction *,
632 const Expression *makePossiblePHIOfOps(Instruction *,
624633 SmallPtrSetImpl &);
625634 Value *findLeaderForInst(Instruction *ValueOp,
626635 SmallPtrSetImpl &Visited,
845854 static bool isCopyOfAPHI(const Value *V) {
846855 auto *CO = getCopyOf(V);
847856 return CO && isa(CO);
857 }
858
859 // Return true if V is a value that will always be available (IE can
860 // be placed anywhere) in the function. We don't do globals here
861 // because they are often worse to put in place.
862 static bool alwaysAvailable(Value *V) {
863 return isa(V) || isa(V);
848864 }
849865
850866 PHIExpression *NewGVN::createPHIExpression(Instruction *I, bool &HasBackedge,
11381154 bool NewGVN::someEquivalentDominates(const Instruction *Inst,
11391155 const Instruction *U) const {
11401156 auto *CC = ValueToClass.lookup(Inst);
1141 // This must be an instruction because we are only called from phi nodes
1157 // This must be an instruction because we are only called from phi nodes
11421158 // in the case that the value it needs to check against is an instruction.
11431159
11441160 // The most likely candiates for dominance are the leader and the next leader.
11561172 // any of these siblings.
11571173 if (!CC)
11581174 return false;
1175 if (alwaysAvailable(CC->getLeader()))
1176 return true;
11591177 if (DT->dominates(cast(CC->getLeader()), U))
11601178 return true;
11611179 if (CC->getNextLeader().first &&
18461864 }
18471865 // Create expression will take care of simplifyCmpInst
18481866 return createExpression(I);
1849 }
1850
1851 // Return true if V is a value that will always be available (IE can
1852 // be placed anywhere) in the function. We don't do globals here
1853 // because they are often worse to put in place.
1854 static bool alwaysAvailable(Value *V) {
1855 return isa(V) || isa(V);
18561867 }
18571868
18581869 // Substitute and symbolize the value before value numbering.
23402351 if (MemoryAccess *MemPhi = getMemoryAccess(To))
23412352 TouchedInstructions.set(InstrToDFSNum(MemPhi));
23422353
2343 auto BI = To->begin();
2344 while (isa(BI)) {
2345 TouchedInstructions.set(InstrToDFSNum(&*BI));
2346 ++BI;
2347 }
2348 for_each_found(PHIOfOpsPHIs, To, [&](const PHINode *I) {
2349 TouchedInstructions.set(InstrToDFSNum(I));
2350 });
2354 // FIXME: We should just add a union op on a Bitvector and
2355 // SparseBitVector. We can do it word by word faster than we are doing it
2356 // here.
2357 for (auto InstNum : RevisitOnReachabilityChange[To])
2358 TouchedInstructions.set(InstNum);
23512359 }
23522360 }
23532361 }
24482456 void NewGVN::removePhiOfOps(Instruction *I, PHINode *PHITemp) {
24492457 InstrDFS.erase(PHITemp);
24502458 // It's still a temp instruction. We keep it in the array so it gets erased.
2451 // However, it's no longer used by I, or in the block/
2452 PHIOfOpsPHIs[getBlockForValue(PHITemp)].erase(PHITemp);
2459 // However, it's no longer used by I, or in the block
24532460 TempToBlock.erase(PHITemp);
24542461 RealToTemp.erase(I);
2462 // We don't remove the users from the phi node uses. This wastes a little
2463 // time, but such is life. We could use two sets to track which were there
2464 // are the start of NewGVN, and which were added, but right nowt he cost of
2465 // tracking is more than the cost of checking for more phi of ops.
24552466 }
24562467
24572468 // Add PHI Op in BB as a PHI of operations version of ExistingValue.
24592470 Instruction *ExistingValue) {
24602471 InstrDFS[Op] = InstrToDFSNum(ExistingValue);
24612472 AllTempInstructions.insert(Op);
2462 PHIOfOpsPHIs[BB].insert(Op);
24632473 TempToBlock[Op] = BB;
24642474 RealToTemp[ExistingValue] = Op;
2475 // Add all users to phi node use, as they are now uses of the phi of ops phis
2476 // and may themselves be phi of ops.
2477 for (auto *U : ExistingValue->users())
2478 if (auto *UI = dyn_cast(U))
2479 PHINodeUses.insert(UI);
24652480 }
24662481
24672482 static bool okayForPHIOfOps(const Instruction *I) {
25912606 // When we see an instruction that is an op of phis, generate the equivalent phi
25922607 // of ops form.
25932608 const Expression *
2594 NewGVN::makePossiblePhiOfOps(Instruction *I,
2609 NewGVN::makePossiblePHIOfOps(Instruction *I,
25952610 SmallPtrSetImpl &Visited) {
25962611 if (!okayForPHIOfOps(I))
25972612 return nullptr;
26192634
26202635 SmallPtrSet VisitedOps;
26212636 // Convert op of phis to phi of ops
2622 for (auto &Op : I->operands()) {
2623 if (!isa(Op))
2624 continue;
2637 for (auto *Op : I->operand_values()) {
2638 if (!isa(Op)) {
2639 auto *ValuePHI = RealToTemp.lookup(Op);
2640 if (!ValuePHI)
2641 continue;
2642 DEBUG(dbgs() << "Found possible dependent phi of ops\n");
2643 Op = ValuePHI;
2644 }
26252645 auto *OpPHI = cast(Op);
26262646 // No point in doing this for one-operand phis.
26272647 if (OpPHI->getNumOperands() == 1)
26302650 return nullptr;
26312651 SmallVector, 4> Ops;
26322652 auto *PHIBlock = getBlockForValue(OpPHI);
2633 for (auto PredBB : OpPHI->blocks()) {
2653 RevisitOnReachabilityChange[PHIBlock].reset(InstrToDFSNum(I));
2654 for (unsigned PredNum = 0; PredNum < OpPHI->getNumOperands(); ++PredNum) {
2655 auto *PredBB = OpPHI->getIncomingBlock(PredNum);
26342656 Value *FoundVal = nullptr;
26352657 // We could just skip unreachable edges entirely but it's tricky to do
26362658 // with rewriting existing phi nodes.
26372659 if (ReachableEdges.count({PredBB, PHIBlock})) {
2638 // Clone the instruction, create an expression from it, and see if we
2639 // have a leader.
2660 // Clone the instruction, create an expression from it that is
2661 // translated back into the predecessor, and see if we have a leader.
26402662 Instruction *ValueOp = I->clone();
26412663 if (MemAccess)
26422664 TempToMemory.insert({ValueOp, MemAccess});
26442666 VisitedOps.clear();
26452667 for (auto &Op : ValueOp->operands()) {
26462668 auto *OrigOp = &*Op;
2647 Op = Op->DoPHITranslation(PHIBlock, PredBB);
2669 // When these operand changes, it could change whether there is a
2670 // leader for us or not, so we have to add additional users
2671 if (isa(Op)) {
2672 Op = Op->DoPHITranslation(PHIBlock, PredBB);
2673 if (Op != OrigOp && Op != I)
2674 addAdditionalUsers(Op, I);
2675 } else if (auto *ValuePHI = RealToTemp.lookup(Op)) {
2676 if (getBlockForValue(ValuePHI) == PHIBlock)
2677 Op = ValuePHI->getIncomingValue(PredNum);
2678 }
26482679 // When this operand changes, it could change whether there is a
26492680 // leader for us or not.
26502681 addAdditionalUsers(Op, I);
26692700 << getBlockName(PredBB)
26702701 << " because the block is unreachable\n");
26712702 FoundVal = UndefValue::get(I->getType());
2703 RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(I));
26722704 }
26732705
26742706 Ops.push_back({FoundVal, PredBB});
26752707 DEBUG(dbgs() << "Found phi of ops operand " << *FoundVal << " in "
26762708 << getBlockName(PredBB) << "\n");
26772709 }
2710
2711 // FIXME: We should evaluate the phi operands first and see if it ends up a
2712 // constant or variable expression.
26782713 auto *ValuePHI = RealToTemp.lookup(I);
26792714 bool NewPHI = false;
26802715 if (!ValuePHI) {
26952730 ++i;
26962731 }
26972732 }
2698
2733 RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(I));
26992734 DEBUG(dbgs() << "Created phi of ops " << *ValuePHI << " for " << *I
27002735 << "\n");
27012736 return performSymbolicEvaluation(ValuePHI, Visited);
27442779 if (MD && isa(MD->getMemoryInst()))
27452780 TOPClass->incStoreCount();
27462781 }
2782
2783 // FIXME: This is trying to discover which instructions are uses of phi
2784 // nodes. We should move this into one of the myriad of places that walk
2785 // all the operands already.
27472786 for (auto &I : *BB) {
2748 // TODO: Move to helper
27492787 if (isa(&I))
27502788 for (auto *U : I.users())
27512789 if (auto *UInst = dyn_cast(U))
28032841 ExpressionToPhiOfOps.clear();
28042842 TempToBlock.clear();
28052843 TempToMemory.clear();
2806 PHIOfOpsPHIs.clear();
28072844 PHINodeUses.clear();
28082845 OpSafeForPHIOfOps.clear();
28092846 ReachableBlocks.clear();
28192856 MemoryAccessToClass.clear();
28202857 PredicateToUsers.clear();
28212858 MemoryToUsers.clear();
2859 RevisitOnReachabilityChange.clear();
28222860 }
28232861
28242862 // Assign local DFS number mapping to instructions, and leave space for Value
28422880 markInstructionForDeletion(&I);
28432881 continue;
28442882 }
2883 if (isa(&I))
2884 RevisitOnReachabilityChange[B].set(End);
28452885 InstrDFS[&I] = End++;
28462886 DFSToInstr.emplace_back(&I);
28472887 }
29312971 // Make a phi of ops if necessary
29322972 if (Symbolized && !isa(Symbolized) &&
29332973 !isa(Symbolized) && PHINodeUses.count(I)) {
2934 auto *PHIE = makePossiblePhiOfOps(I, Visited);
2974 auto *PHIE = makePossiblePHIOfOps(I, Visited);
29352975 // If we created a phi of ops, use it.
29362976 // If we couldn't create one, make sure we don't leave one lying around
29372977 if (PHIE) {
37093749
37103750 // Go through all of our phi nodes, and kill the arguments associated with
37113751 // unreachable edges.
3712 auto ReplaceUnreachablePHIArgs = [&](PHINode &PHI, BasicBlock *BB) {
3713 for (auto &Operand : PHI.incoming_values())
3714 if (!ReachableEdges.count({PHI.getIncomingBlock(Operand), BB})) {
3752 auto ReplaceUnreachablePHIArgs = [&](PHINode *PHI, BasicBlock *BB) {
3753 for (auto &Operand : PHI->incoming_values())
3754 if (!ReachableEdges.count({PHI->getIncomingBlock(Operand), BB})) {
37153755 DEBUG(dbgs() << "Replacing incoming value of " << PHI << " for block "
3716 << getBlockName(PHI.getIncomingBlock(Operand))
3756 << getBlockName(PHI->getIncomingBlock(Operand))
37173757 << " with undef due to it being unreachable\n");
3718 Operand.set(UndefValue::get(PHI.getType()));
3758 Operand.set(UndefValue::get(PHI->getType()));
37193759 }
37203760 };
3721 SmallPtrSet BlocksWithPhis;
3722 for (auto &B : F)
3723 if ((!B.empty() && isa(*B.begin())) ||
3724 (PHIOfOpsPHIs.find(&B) != PHIOfOpsPHIs.end()))
3725 BlocksWithPhis.insert(&B);
3761 // Replace unreachable phi arguments.
3762 // At this point, RevisitOnReachabilityChange only contains:
3763 //
3764 // 1. PHIs
3765 // 2. Temporaries that will convert to PHIs
3766 // 3. Operations that are affected by an unreachable edge but do not fit into
3767 // 1 or 2 (rare).
3768 // So it is a slight overshoot of what we want. We could make it exact by
3769 // using two SparseBitVectors per block.
37263770 DenseMap ReachablePredCount;
3727 for (auto KV : ReachableEdges)
3771 for (auto &KV : ReachableEdges)
37283772 ReachablePredCount[KV.getEnd()]++;
3729 for (auto *BB : BlocksWithPhis)
3730 // TODO: It would be faster to use getNumIncomingBlocks() on a phi node in
3731 // the block and subtract the pred count, but it's more complicated.
3732 if (ReachablePredCount.lookup(BB) !=
3733 unsigned(std::distance(pred_begin(BB), pred_end(BB)))) {
3734 for (auto II = BB->begin(); isa(II); ++II) {
3735 auto &PHI = cast(*II);
3773 for (auto &BBPair : RevisitOnReachabilityChange) {
3774 for (auto InstNum : BBPair.second) {
3775 auto *Inst = InstrFromDFSNum(InstNum);
3776 auto *PHI = dyn_cast(Inst);
3777 PHI = PHI ? PHI : dyn_cast_or_null(RealToTemp.lookup(Inst));
3778 if (!PHI)
3779 continue;
3780 auto *BB = BBPair.first;
3781 if (ReachablePredCount.lookup(BB) != PHI->getNumIncomingValues())
37363782 ReplaceUnreachablePHIArgs(PHI, BB);
3737 }
3738 for_each_found(PHIOfOpsPHIs, BB, [&](PHINode *PHI) {
3739 ReplaceUnreachablePHIArgs(*PHI, BB);
3740 });
3741 }
3783 }
3784 }
37423785
37433786 // Map to store the use counts
37443787 DenseMap UseCounts;
2424 %.0 = phi i32 [ 5, %4 ], [ 7, %5 ]
2525 %7 = mul nsw i32 %.0, 15
2626 ret i32 %7
27 }
28 ;; Dependent phi of ops
29 define i32 @test1b(i32, i8**) {
30 ; CHECK-LABEL: @test1b(
31 ; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP0:%.*]], 0
32 ; CHECK-NEXT: br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP5:%.*]]
33 ; CHECK: br label [[TMP6:%.*]]
34 ; CHECK: br label [[TMP6]]
35 ; CHECK: [[PHIOFOPS1:%.*]] = phi i32 [ 75, [[TMP4]] ], [ 105, [[TMP5]] ]
36 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi i32 [ 1125, [[TMP4]] ], [ 1575, [[TMP5]] ]
37 ; CHECK-NEXT: [[DOT0:%.*]] = phi i32 [ 5, [[TMP4]] ], [ 7, [[TMP5]] ]
38 ; CHECK-NEXT: ret i32 [[PHIOFOPS]]
39 ;
40 %3 = icmp ne i32 %0, 0
41 br i1 %3, label %4, label %5
42
43 ;
44 br label %6
45
46 ;
47 br label %6
48
49 ;
50 %.0 = phi i32 [ 5, %4 ], [ 7, %5 ]
51 %7 = mul nsw i32 %.0, 15
52 %8 = mul nsw i32 %7, 15
53 ret i32 %8
2754 }
2855
2956 define i32 @test2(i32) {
469496 }
470497
471498 declare i32* @wombat()
499
500 ;; Ensure that when reachability affects a phi of ops, we recompute
501 ;; it. Here, the phi node is marked for recomputation when bb7->bb3
502 ;; becomes live, but the value does not change. if we do not directly
503 ;; recompute the phi of ops instruction (tmp5), the value number will
504 ;; change in the verifier, as it goes from a constant value to a
505 ;; phi of [true, false]
506
507 define void @test12() {
508 ; CHECK-LABEL: @test12(
509 ; CHECK-NEXT: bb:
510 ; CHECK-NEXT: [[TMP:%.*]] = load i32, i32* null
511 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[TMP]], 0
512 ; CHECK-NEXT: br i1 [[TMP1]], label [[BB2:%.*]], label [[BB8:%.*]]
513 ; CHECK: bb2:
514 ; CHECK-NEXT: br label [[BB3:%.*]]
515 ; CHECK: bb3:
516 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi i1 [ true, [[BB2]] ], [ false, [[BB7:%.*]] ]
517 ; CHECK-NEXT: br i1 [[PHIOFOPS]], label [[BB6:%.*]], label [[BB7]]
518 ; CHECK: bb6:
519 ; CHECK-NEXT: br label [[BB7]]
520 ; CHECK: bb7:
521 ; CHECK-NEXT: br label [[BB3]]
522 ; CHECK: bb8:
523 ; CHECK-NEXT: ret void
524 ;
525 bb:
526 %tmp = load i32, i32* null
527 %tmp1 = icmp sgt i32 %tmp, 0
528 br i1 %tmp1, label %bb2, label %bb8
529
530 bb2: ; preds = %bb
531 br label %bb3
532
533 bb3: ; preds = %bb7, %bb2
534 %tmp4 = phi i32 [ %tmp, %bb2 ], [ undef, %bb7 ]
535 %tmp5 = icmp sgt i32 %tmp4, 0
536 br i1 %tmp5, label %bb6, label %bb7
537
538 bb6: ; preds = %bb3
539 br label %bb7
540
541 bb7: ; preds = %bb6, %bb3
542 br label %bb3
543
544 bb8: ; preds = %bb
545 ret void
546 }