llvm.org GIT mirror llvm / 3f74a7b
[LICM] Reapply r347190 "Make LICM able to hoist phis" with fix This commit caused failures because it failed to correctly handle cases where we hoist a phi, then hoist a use of that phi, then have to rehoist that use. We need to make sure that we rehoist the use to _after_ the hoisted phi, which we do by always rehoisting to the immediate dominator instead of just rehoisting everything to the original preheader. An option is also added to control whether control flow is hoisted, which is off in this commit but will be turned on in a subsequent commit. Differential Revision: https://reviews.llvm.org/D52827 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@347776 91177308-0d34-0410-b5e6-96231b3b80d8 John Brawn 9 months ago
2 changed file(s) with 1666 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
3030 //===----------------------------------------------------------------------===//
3131
3232 #include "llvm/Transforms/Scalar/LICM.h"
33 #include "llvm/ADT/SetOperations.h"
3334 #include "llvm/ADT/Statistic.h"
3435 #include "llvm/Analysis/AliasAnalysis.h"
3536 #include "llvm/Analysis/AliasSetTracker.h"
4041 #include "llvm/Analysis/GuardUtils.h"
4142 #include "llvm/Analysis/Loads.h"
4243 #include "llvm/Analysis/LoopInfo.h"
44 #include "llvm/Analysis/LoopIterator.h"
4345 #include "llvm/Analysis/LoopPass.h"
4446 #include "llvm/Analysis/MemoryBuiltins.h"
4547 #include "llvm/Analysis/MemorySSA.h"
7476
7577 #define DEBUG_TYPE "licm"
7678
79 STATISTIC(NumCreatedBlocks, "Number of blocks created");
80 STATISTIC(NumClonedBranches, "Number of branches cloned");
7781 STATISTIC(NumSunk, "Number of instructions sunk out of loop");
7882 STATISTIC(NumHoisted, "Number of instructions hoisted out of loop");
7983 STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk");
8488 static cl::opt
8589 DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false),
8690 cl::desc("Disable memory promotion in LICM pass"));
91
92 static cl::opt ControlFlowHoisting(
93 "licm-control-flow-hoisting", cl::Hidden, cl::init(false),
94 cl::desc("Enable control flow (and PHI) hoisting in LICM"));
8795
8896 static cl::opt MaxNumUsesTraversed(
8997 "licm-max-num-uses-traversed", cl::Hidden, cl::init(8),
102110 const LoopSafetyInfo *SafetyInfo,
103111 TargetTransformInfo *TTI, bool &FreeInLoop);
104112 static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
105 ICFLoopSafetyInfo *SafetyInfo,
113 BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo,
106114 OptimizationRemarkEmitter *ORE);
107115 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
108116 const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo,
436444 return Changed;
437445 }
438446
447 // This is a helper class for hoistRegion to make it able to hoist control flow
448 // in order to be able to hoist phis. The way this works is that we initially
449 // start hoisting to the loop preheader, and when we see a loop invariant branch
450 // we make note of this. When we then come to hoist an instruction that's
451 // conditional on such a branch we duplicate the branch and the relevant control
452 // flow, then hoist the instruction into the block corresponding to its original
453 // block in the duplicated control flow.
454 class ControlFlowHoister {
455 private:
456 // Information about the loop we are hoisting from
457 LoopInfo *LI;
458 DominatorTree *DT;
459 Loop *CurLoop;
460
461 // A map of blocks in the loop to the block their instructions will be hoisted
462 // to.
463 DenseMap HoistDestinationMap;
464
465 // The branches that we can hoist, mapped to the block that marks a
466 // convergence point of their control flow.
467 DenseMap HoistableBranches;
468
469 public:
470 ControlFlowHoister(LoopInfo *LI, DominatorTree *DT, Loop *CurLoop)
471 : LI(LI), DT(DT), CurLoop(CurLoop) {}
472
473 void registerPossiblyHoistableBranch(BranchInst *BI) {
474 // We can only hoist conditional branches with loop invariant operands.
475 if (!ControlFlowHoisting || !BI->isConditional() ||
476 !CurLoop->hasLoopInvariantOperands(BI))
477 return;
478
479 // The branch destinations need to be in the loop, and we don't gain
480 // anything by duplicating conditional branches with duplicate successors,
481 // as it's essentially the same as an unconditional branch.
482 BasicBlock *TrueDest = BI->getSuccessor(0);
483 BasicBlock *FalseDest = BI->getSuccessor(1);
484 if (!CurLoop->contains(TrueDest) || !CurLoop->contains(FalseDest) ||
485 TrueDest == FalseDest)
486 return;
487
488 // We can hoist BI if one branch destination is the successor of the other,
489 // or both have common successor which we check by seeing if the
490 // intersection of their successors is non-empty.
491 // TODO: This could be expanded to allowing branches where both ends
492 // eventually converge to a single block.
493 SmallPtrSet TrueDestSucc, FalseDestSucc;
494 TrueDestSucc.insert(succ_begin(TrueDest), succ_end(TrueDest));
495 FalseDestSucc.insert(succ_begin(FalseDest), succ_end(FalseDest));
496 BasicBlock *CommonSucc = nullptr;
497 if (TrueDestSucc.count(FalseDest)) {
498 CommonSucc = FalseDest;
499 } else if (FalseDestSucc.count(TrueDest)) {
500 CommonSucc = TrueDest;
501 } else {
502 set_intersect(TrueDestSucc, FalseDestSucc);
503 // If there's one common successor use that.
504 if (TrueDestSucc.size() == 1)
505 CommonSucc = *TrueDestSucc.begin();
506 // If there's more than one pick whichever appears first in the block list
507 // (we can't use the value returned by TrueDestSucc.begin() as it's
508 // unpredicatable which element gets returned).
509 else if (!TrueDestSucc.empty()) {
510 Function *F = TrueDest->getParent();
511 auto IsSucc = [&](BasicBlock &BB) { return TrueDestSucc.count(&BB); };
512 auto It = std::find_if(F->begin(), F->end(), IsSucc);
513 assert(It != F->end() && "Could not find successor in function");
514 CommonSucc = &*It;
515 }
516 }
517 // The common successor has to be dominated by the branch, as otherwise
518 // there will be some other path to the successor that will not be
519 // controlled by this branch so any phi we hoist would be controlled by the
520 // wrong condition. This also takes care of avoiding hoisting of loop back
521 // edges.
522 // TODO: In some cases this could be relaxed if the successor is dominated
523 // by another block that's been hoisted and we can guarantee that the
524 // control flow has been replicated exactly.
525 if (CommonSucc && DT->dominates(BI, CommonSucc))
526 HoistableBranches[BI] = CommonSucc;
527 }
528
529 bool canHoistPHI(PHINode *PN) {
530 // The phi must have loop invariant operands.
531 if (!ControlFlowHoisting || !CurLoop->hasLoopInvariantOperands(PN))
532 return false;
533 // We can hoist phis if the block they are in is the target of hoistable
534 // branches which cover all of the predecessors of the block.
535 SmallPtrSet PredecessorBlocks;
536 BasicBlock *BB = PN->getParent();
537 for (BasicBlock *PredBB : predecessors(BB))
538 PredecessorBlocks.insert(PredBB);
539 // If we have less predecessor blocks than predecessors then the phi will
540 // have more than one incoming value for the same block which we can't
541 // handle.
542 // TODO: This could be handled be erasing some of the duplicate incoming
543 // values.
544 if (PredecessorBlocks.size() != pred_size(BB))
545 return false;
546 for (auto &Pair : HoistableBranches) {
547 if (Pair.second == BB) {
548 // Which blocks are predecessors via this branch depends on if the
549 // branch is triangle-like or diamond-like.
550 if (Pair.first->getSuccessor(0) == BB) {
551 PredecessorBlocks.erase(Pair.first->getParent());
552 PredecessorBlocks.erase(Pair.first->getSuccessor(1));
553 } else if (Pair.first->getSuccessor(1) == BB) {
554 PredecessorBlocks.erase(Pair.first->getParent());
555 PredecessorBlocks.erase(Pair.first->getSuccessor(0));
556 } else {
557 PredecessorBlocks.erase(Pair.first->getSuccessor(0));
558 PredecessorBlocks.erase(Pair.first->getSuccessor(1));
559 }
560 }
561 }
562 // PredecessorBlocks will now be empty if for every predecessor of BB we
563 // found a hoistable branch source.
564 return PredecessorBlocks.empty();
565 }
566
567 BasicBlock *getOrCreateHoistedBlock(BasicBlock *BB) {
568 // If BB has already been hoisted, return that
569 if (HoistDestinationMap.count(BB))
570 return HoistDestinationMap[BB];
571
572 // Check if this block is conditional based on a pending branch
573 auto HasBBAsSuccessor =
574 [&](DenseMap::value_type &Pair) {
575 return BB != Pair.second && (Pair.first->getSuccessor(0) == BB ||
576 Pair.first->getSuccessor(1) == BB);
577 };
578 auto It = std::find_if(HoistableBranches.begin(), HoistableBranches.end(),
579 HasBBAsSuccessor);
580
581 // If not involved in a pending branch, hoist to preheader
582 BasicBlock *InitialPreheader = CurLoop->getLoopPreheader();
583 if (It == HoistableBranches.end()) {
584 LLVM_DEBUG(dbgs() << "LICM using " << InitialPreheader->getName()
585 << " as hoist destination for " << BB->getName()
586 << "\n");
587 HoistDestinationMap[BB] = InitialPreheader;
588 return InitialPreheader;
589 }
590 BranchInst *BI = It->first;
591 assert(std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) ==
592 HoistableBranches.end() &&
593 "BB is expected to be the target of at most one branch");
594
595 LLVMContext &C = BB->getContext();
596 BasicBlock *TrueDest = BI->getSuccessor(0);
597 BasicBlock *FalseDest = BI->getSuccessor(1);
598 BasicBlock *CommonSucc = HoistableBranches[BI];
599 BasicBlock *HoistTarget = getOrCreateHoistedBlock(BI->getParent());
600
601 // Create hoisted versions of blocks that currently don't have them
602 auto CreateHoistedBlock = [&](BasicBlock *Orig) {
603 if (HoistDestinationMap.count(Orig))
604 return HoistDestinationMap[Orig];
605 BasicBlock *New =
606 BasicBlock::Create(C, Orig->getName() + ".licm", Orig->getParent());
607 HoistDestinationMap[Orig] = New;
608 DT->addNewBlock(New, HoistTarget);
609 if (CurLoop->getParentLoop())
610 CurLoop->getParentLoop()->addBasicBlockToLoop(New, *LI);
611 ++NumCreatedBlocks;
612 LLVM_DEBUG(dbgs() << "LICM created " << New->getName()
613 << " as hoist destination for " << Orig->getName()
614 << "\n");
615 return New;
616 };
617 BasicBlock *HoistTrueDest = CreateHoistedBlock(TrueDest);
618 BasicBlock *HoistFalseDest = CreateHoistedBlock(FalseDest);
619 BasicBlock *HoistCommonSucc = CreateHoistedBlock(CommonSucc);
620
621 // Link up these blocks with branches.
622 if (!HoistCommonSucc->getTerminator()) {
623 // The new common successor we've generated will branch to whatever that
624 // hoist target branched to.
625 BasicBlock *TargetSucc = HoistTarget->getSingleSuccessor();
626 assert(TargetSucc && "Expected hoist target to have a single successor");
627 HoistCommonSucc->moveBefore(TargetSucc);
628 BranchInst::Create(TargetSucc, HoistCommonSucc);
629 }
630 if (!HoistTrueDest->getTerminator()) {
631 HoistTrueDest->moveBefore(HoistCommonSucc);
632 BranchInst::Create(HoistCommonSucc, HoistTrueDest);
633 }
634 if (!HoistFalseDest->getTerminator()) {
635 HoistFalseDest->moveBefore(HoistCommonSucc);
636 BranchInst::Create(HoistCommonSucc, HoistFalseDest);
637 }
638
639 // If BI is being cloned to what was originally the preheader then
640 // HoistCommonSucc will now be the new preheader.
641 if (HoistTarget == InitialPreheader) {
642 // Phis in the loop header now need to use the new preheader.
643 InitialPreheader->replaceSuccessorsPhiUsesWith(HoistCommonSucc);
644 // The new preheader dominates the loop header.
645 DomTreeNode *PreheaderNode = DT->getNode(HoistCommonSucc);
646 DomTreeNode *HeaderNode = DT->getNode(CurLoop->getHeader());
647 DT->changeImmediateDominator(HeaderNode, PreheaderNode);
648 // The preheader hoist destination is now the new preheader, with the
649 // exception of the hoist destination of this branch.
650 for (auto &Pair : HoistDestinationMap)
651 if (Pair.second == InitialPreheader && Pair.first != BI->getParent())
652 Pair.second = HoistCommonSucc;
653 }
654
655 // Now finally clone BI.
656 ReplaceInstWithInst(
657 HoistTarget->getTerminator(),
658 BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition()));
659 ++NumClonedBranches;
660
661 assert(CurLoop->getLoopPreheader() &&
662 "Hoisting blocks should not have destroyed preheader");
663 return HoistDestinationMap[BB];
664 }
665 };
666
439667 /// Walk the specified region of the CFG (defined by all blocks dominated by
440668 /// the specified block, and that are in the current loop) in depth first
441669 /// order w.r.t the DominatorTree. This allows us to visit definitions before
450678 CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr &&
451679 "Unexpected input to hoistRegion");
452680
453 // We want to visit parents before children. We will enque all the parents
454 // before their children in the worklist and process the worklist in order.
455 SmallVector Worklist = collectChildrenInLoop(N, CurLoop);
456
681 ControlFlowHoister CFH(LI, DT, CurLoop);
682
683 // Keep track of instructions that have been hoisted, as they may need to be
684 // re-hoisted if they end up not dominating all of their uses.
685 SmallVector HoistedInstructions;
686
687 // For PHI hoisting to work we need to hoist blocks before their successors.
688 // We can do this by iterating through the blocks in the loop in reverse
689 // post-order.
690 LoopBlocksRPO Worklist(CurLoop);
691 Worklist.perform(LI);
457692 bool Changed = false;
458 for (DomTreeNode *DTN : Worklist) {
459 BasicBlock *BB = DTN->getBlock();
693 for (BasicBlock *BB : Worklist) {
460694 // Only need to process the contents of this block if it is not part of a
461695 // subloop (which would already have been processed).
462696 if (inSubLoop(BB, CurLoop, LI))
482716 // Try hoisting the instruction out to the preheader. We can only do
483717 // this if all of the operands of the instruction are loop invariant and
484718 // if it is safe to hoist the instruction.
485 //
719 // TODO: It may be safe to hoist if we are hoisting to a conditional block
720 // and we have accurately duplicated the control flow from the loop header
721 // to that block.
486722 if (CurLoop->hasLoopInvariantOperands(&I) &&
487723 canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, true, ORE) &&
488724 isSafeToExecuteUnconditionally(
489725 I, DT, CurLoop, SafetyInfo, ORE,
490726 CurLoop->getLoopPreheader()->getTerminator())) {
491 hoist(I, DT, CurLoop, SafetyInfo, ORE);
727 hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, ORE);
728 HoistedInstructions.push_back(&I);
492729 Changed = true;
493730 continue;
494731 }
513750 I.replaceAllUsesWith(Product);
514751 eraseInstruction(I, *SafetyInfo, CurAST);
515752
516 hoist(*ReciprocalDivisor, DT, CurLoop, SafetyInfo, ORE);
753 hoist(*ReciprocalDivisor, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB),
754 SafetyInfo, ORE);
755 HoistedInstructions.push_back(ReciprocalDivisor);
517756 Changed = true;
518757 continue;
519758 }
525764 CurLoop->hasLoopInvariantOperands(&I) &&
526765 SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop) &&
527766 SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop)) {
528 hoist(I, DT, CurLoop, SafetyInfo, ORE);
767 hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, ORE);
768 HoistedInstructions.push_back(&I);
529769 Changed = true;
530770 continue;
531771 }
532 }
533 }
772
773 if (PHINode *PN = dyn_cast(&I)) {
774 if (CFH.canHoistPHI(PN)) {
775 // Redirect incoming blocks first to ensure that we create hoisted
776 // versions of those blocks before we hoist the phi.
777 for (unsigned int i = 0; i < PN->getNumIncomingValues(); ++i)
778 PN->setIncomingBlock(
779 i, CFH.getOrCreateHoistedBlock(PN->getIncomingBlock(i)));
780 hoist(*PN, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo,
781 ORE);
782 assert(DT->dominates(PN, BB) && "Conditional PHIs not expected");
783 Changed = true;
784 continue;
785 }
786 }
787
788 // Remember possibly hoistable branches so we can actually hoist them
789 // later if needed.
790 if (BranchInst *BI = dyn_cast(&I))
791 CFH.registerPossiblyHoistableBranch(BI);
792 }
793 }
794
795 // If we hoisted instructions to a conditional block they may not dominate
796 // their uses that weren't hoisted (such as phis where some operands are not
797 // loop invariant). If so make them unconditional by moving them to their
798 // immediate dominator. We iterate through the instructions in reverse order
799 // which ensures that when we rehoist an instruction we rehoist its operands,
800 // and also keep track of where in the block we are rehoisting to to make sure
801 // that we rehoist instructions before the instructions that use them.
802 Instruction *HoistPoint = nullptr;
803 for (Instruction *I : reverse(HoistedInstructions)) {
804 if (!llvm::all_of(I->uses(), [&](Use &U) { return DT->dominates(I, U); })) {
805 BasicBlock *Dominator =
806 DT->getNode(I->getParent())->getIDom()->getBlock();
807 LLVM_DEBUG(dbgs() << "LICM rehoisting to " << Dominator->getName() << ": "
808 << *I << "\n");
809 if (!HoistPoint || HoistPoint->getParent() != Dominator) {
810 if (HoistPoint)
811 assert(DT->dominates(Dominator, HoistPoint->getParent()) &&
812 "New hoist point expected to dominate old hoist point");
813 HoistPoint = Dominator->getTerminator();
814 }
815 moveInstructionBefore(*I, *HoistPoint, *SafetyInfo);
816 HoistPoint = I;
817 Changed = true;
818 }
819 }
820
821 // Now that we've finished hoisting make sure that LI and DT are still valid.
822 #ifndef NDEBUG
823 assert(DT->verify(DominatorTree::VerificationLevel::Fast) &&
824 "Dominator tree verification failed");
825 LI->verify(*DT);
826 #endif
534827
535828 return Changed;
536829 }
10991392 /// is safe to hoist, this instruction is called to do the dirty work.
11001393 ///
11011394 static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
1102 ICFLoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE) {
1103 auto *Preheader = CurLoop->getLoopPreheader();
1104 LLVM_DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": " << I
1395 BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo,
1396 OptimizationRemarkEmitter *ORE) {
1397 LLVM_DEBUG(dbgs() << "LICM hoisting to " << Dest->getName() << ": " << I
11051398 << "\n");
11061399 ORE->emit([&]() {
11071400 return OptimizationRemark(DEBUG_TYPE, "Hoisted", &I) << "hoisting "
11191412 !SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop))
11201413 I.dropUnknownNonDebugMetadata();
11211414
1122 // Move the new node to the Preheader, before its terminator.
1123 moveInstructionBefore(I, *Preheader->getTerminator(), *SafetyInfo);
1415 if (isa(I))
1416 // Move the new node to the end of the phi list in the destination block.
1417 moveInstructionBefore(I, *Dest->getFirstNonPHI(), *SafetyInfo);
1418 else
1419 // Move the new node to the destination block, before its terminator.
1420 moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo);
11241421
11251422 // Do not retain debug locations when we are moving instructions to different
11261423 // basic blocks, because we want to avoid jumpy line tables. Calls, however,
0 ; RUN: opt -S -licm < %s | FileCheck %s -check-prefixes=CHECK,CHECK-DISABLED
1 ; RUN: opt -S -licm -licm-control-flow-hoisting=1 < %s | FileCheck %s -check-prefixes=CHECK,CHECK-ENABLED
2 ; RUN: opt -S -licm -licm-control-flow-hoisting=0 < %s | FileCheck %s -check-prefixes=CHECK,CHECK-DISABLED
3 ; RUN: opt -passes='require,loop(licm)' -S < %s | FileCheck %s -check-prefixes=CHECK,CHECK-DISABLED
4 ; RUN: opt -passes='require,loop(licm)' -licm-control-flow-hoisting=1 -S < %s | FileCheck %s -check-prefixes=CHECK,CHECK-ENABLED
5 ; RUN: opt -passes='require,loop(licm)' -licm-control-flow-hoisting=0 -S < %s | FileCheck %s -check-prefixes=CHECK,CHECK-DISABLED
6
7 ; CHECK-LABEL: @triangle_phi
8 define void @triangle_phi(i32 %x, i32* %p) {
9 ; CHECK-LABEL: entry:
10 ; CHECK: %cmp1 = icmp sgt i32 %x, 0
11 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
12 entry:
13 br label %loop
14
15 ; CHECK-ENABLED: [[IF_LICM]]:
16 ; CHECK: %add = add i32 %x, 1
17 ; CHECK-ENABLED: br label %[[THEN_LICM]]
18
19 ; CHECK-ENABLED: [[THEN_LICM]]:
20 ; CHECK-ENABLED: phi i32 [ %add, %[[IF_LICM]] ], [ %x, %entry ]
21 ; CHECK-ENABLED: store i32 %phi, i32* %p
22 ; CHECK-ENABLED: %cmp2 = icmp ne i32 %phi, 0
23 ; CHECK: br label %loop
24
25 loop:
26 %cmp1 = icmp sgt i32 %x, 0
27 br i1 %cmp1, label %if, label %then
28
29 if:
30 %add = add i32 %x, 1
31 br label %then
32
33 ; CHECK-LABEL: then:
34 ; CHECK-DISABLED: %phi = phi i32 [ %add, %if ], [ %x, %loop ]
35 ; CHECK-DISABLED: %cmp2 = icmp ne i32 %phi, 0
36 then:
37 %phi = phi i32 [ %add, %if ], [ %x, %loop ]
38 store i32 %phi, i32* %p
39 %cmp2 = icmp ne i32 %phi, 0
40 br i1 %cmp2, label %loop, label %end
41
42 ; CHECK-LABEL: end:
43 ; CHECK-DISABLED: %[[PHI_LCSSA:.*]] = phi i32 [ %phi, %then ]
44 ; CHECK-DISABLED: store i32 %[[PHI_LCSSA]], i32* %p
45 end:
46 ret void
47 }
48
49 ; CHECK-LABEL: @diamond_phi
50 define void @diamond_phi(i32 %x, i32* %p) {
51 ; CHECK-LABEL: entry:
52 ; CHECK: %cmp1 = icmp sgt i32 %x, 0
53 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[ELSE_LICM:.*]]
54 entry:
55 br label %loop
56
57 ; CHECK-ENABLED: [[IF_LICM]]:
58 ; CHECK-DAG: %add = add i32 %x, 1
59 ; CHECK-ENABLED: br label %[[THEN_LICM:.*]]
60
61 ; CHECK-ENABLED: [[ELSE_LICM]]:
62 ; CHECK-DAG: %sub = sub i32 %x, 1
63 ; CHECK-ENABLED: br label %[[THEN_LICM]]
64
65 ; CHECK-ENABLED: [[THEN_LICM]]
66 ; CHECK-ENABLED: %phi = phi i32 [ %add, %[[IF_LICM]] ], [ %sub, %[[ELSE_LICM]] ]
67 ; CHECK-ENABLED: store i32 %phi, i32* %p
68 ; CHECK-ENABLED: %cmp2 = icmp ne i32 %phi, 0
69 ; CHECK: br label %loop
70
71 loop:
72 %cmp1 = icmp sgt i32 %x, 0
73 br i1 %cmp1, label %if, label %else
74
75 if:
76 %add = add i32 %x, 1
77 br label %then
78
79 else:
80 %sub = sub i32 %x, 1
81 br label %then
82
83 ; CHECK-LABEL: then:
84 ; CHECK-DISABLED: %phi = phi i32 [ %add, %if ], [ %sub, %else ]
85 ; CHECK-DISABLED: %cmp2 = icmp ne i32 %phi, 0
86 then:
87 %phi = phi i32 [ %add, %if ], [ %sub, %else ]
88 store i32 %phi, i32* %p
89 %cmp2 = icmp ne i32 %phi, 0
90 br i1 %cmp2, label %loop, label %end
91
92 ; CHECK-LABEL: end:
93 ; CHECK-DISABLED: %[[PHI_LCSSA:.*]] = phi i32 [ %phi, %then ]
94 ; CHECK-DISABLED: store i32 %[[PHI_LCSSA]], i32* %p
95 end:
96 ret void
97 }
98
99 ; TODO: This is currently too complicated for us to be able to hoist the phi.
100 ; CHECK-LABEL: @three_way_phi
101 define void @three_way_phi(i32 %x, i32* %p) {
102 ; CHECK-LABEL: entry:
103 ; CHECK-DAG: %cmp1 = icmp sgt i32 %x, 0
104 ; CHECK-DAG: %add = add i32 %x, 1
105 ; CHECK-DAG: %cmp2 = icmp sgt i32 %add, 0
106 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[ELSE_LICM:.*]]
107
108 ; CHECK-ENABLED: [[IF_LICM]]:
109 ; CHECK-ENABLED: br label %[[THEN_LICM:.*]]
110
111 ; CHECK-ENABLED: [[THEN_LICM]]:
112 ; CHECK: %sub = sub i32 %x, 1
113 ; CHECK: br label %loop
114
115 entry:
116 br label %loop
117
118 loop:
119 %cmp1 = icmp sgt i32 %x, 0
120 br i1 %cmp1, label %if, label %then
121
122 if:
123 %add = add i32 %x, 1
124 %cmp2 = icmp sgt i32 %add, 0
125 br i1 %cmp2, label %if.if, label %then
126
127 if.if:
128 %sub = sub i32 %x, 1
129 br label %then
130
131 then:
132 %phi = phi i32 [ 0, %loop ], [ %add, %if ], [ %sub, %if.if ]
133 store i32 %phi, i32* %p
134 %cmp3 = icmp ne i32 %phi, 0
135 br i1 %cmp3, label %loop, label %end
136
137 end:
138 ret void
139 }
140
141 ; TODO: This is currently too complicated for us to be able to hoist the phi.
142 ; CHECK-LABEL: @tree_phi
143 define void @tree_phi(i32 %x, i32* %p) {
144 ; CHECK-LABEL: entry:
145 ; CHECK-DAG: %cmp1 = icmp sgt i32 %x, 0
146 ; CHECK-DAG: %add = add i32 %x, 1
147 ; CHECK-DAG: %cmp2 = icmp sgt i32 %add, 0
148 ; CHECK-DAG: %sub = sub i32 %x, 1
149 ; CHECK: br label %loop
150
151 entry:
152 br label %loop
153
154 loop:
155 %cmp1 = icmp sgt i32 %x, 0
156 br i1 %cmp1, label %if, label %else
157
158 if:
159 %add = add i32 %x, 1
160 %cmp2 = icmp sgt i32 %add, 0
161 br i1 %cmp2, label %if.if, label %if.else
162
163 if.if:
164 br label %then
165
166 if.else:
167 br label %then
168
169 else:
170 %sub = sub i32 %x, 1
171 br label %then
172
173 then:
174 %phi = phi i32 [ %add, %if.if ], [ 0, %if.else ], [ %sub, %else ]
175 store i32 %phi, i32* %p
176 %cmp3 = icmp ne i32 %phi, 0
177 br i1 %cmp3, label %loop, label %end
178
179 end:
180 ret void
181 }
182
183 ; TODO: We can hoist the first phi, but not the second.
184 ; CHECK-LABEL: @phi_phi
185 define void @phi_phi(i32 %x, i32* %p) {
186 ; CHECK-LABEL: entry:
187 ; CHECK-DAG: %cmp1 = icmp sgt i32 %x, 0
188 ; CHECK-DAG: %add = add i32 %x, 1
189 ; CHECK-DAG: %cmp2 = icmp sgt i32 %add, 0
190 ; CHECK-DAG: %sub = sub i32 %x, 1
191 ; CHECK-ENABLED: br i1 %cmp2, label %[[IF_IF_LICM:.*]], label %[[IF_ELSE_LICM:.*]]
192
193 ; CHECK-ENABLED: [[IF_IF_LICM]]:
194 ; CHECK-ENABLED: br label %[[IF_THEN_LICM:.*]]
195
196 ; CHECK-ENABLED: [[IF_ELSE_LICM]]:
197 ; CHECK-ENABLED: br label %[[IF_THEN_LICM]]
198
199 ; CHECK-ENABLED: [[IF_THEN_LICM]]:
200 ; CHECK-ENABLED: %phi1 = phi i32 [ %add, %[[IF_IF_LICM]] ], [ 0, %[[IF_ELSE_LICM]] ]
201 ; CHECK: br label %loop
202
203 entry:
204 br label %loop
205
206 loop:
207 %cmp1 = icmp sgt i32 %x, 0
208 br i1 %cmp1, label %if, label %else
209
210 if:
211 %add = add i32 %x, 1
212 %cmp2 = icmp sgt i32 %add, 0
213 br i1 %cmp2, label %if.if, label %if.else
214
215 if.if:
216 br label %if.then
217
218 if.else:
219 br label %if.then
220
221 ; CHECK-LABEL: if.then:
222 ; CHECK-DISABLED: %phi1 = phi i32 [ %add, %if.if ], [ 0, %if.else ]
223 if.then:
224 %phi1 = phi i32 [ %add, %if.if ], [ 0, %if.else ]
225 br label %then
226
227 else:
228 %sub = sub i32 %x, 1
229 br label %then
230
231 ; CHECK-LABEL: then:
232 ; CHECK: %phi2 = phi i32 [ %phi1, %if.then ], [ %sub, %else ]
233 then:
234 %phi2 = phi i32 [ %phi1, %if.then ], [ %sub, %else ]
235 store i32 %phi2, i32* %p
236 %cmp3 = icmp ne i32 %phi2, 0
237 br i1 %cmp3, label %loop, label %end
238
239 end:
240 ret void
241 }
242
243 ; Check that we correctly duplicate empty control flow.
244 ; CHECK-LABEL: @empty_triangle_phi
245 define i8 @empty_triangle_phi(i32 %x, i32 %y) {
246 ; CHECK-LABEL: entry:
247 ; CHECK: %cmp1 = icmp eq i32 %x, 0
248 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
249 entry:
250 br label %loop
251
252 ; CHECK-ENABLED: [[IF_LICM]]:
253 ; CHECK-ENABLED: br label %[[THEN_LICM]]
254
255 ; CHECK-ENABLED: [[THEN_LICM]]:
256 ; CHECK-ENABLED: %phi = phi i8 [ 0, %[[IF_LICM]] ], [ 1, %entry ]
257 ; CHECK: %cmp2 = icmp eq i32 %y, 0
258 ; CHECK: br label %loop
259
260 loop:
261 %cmp1 = icmp eq i32 %x, 0
262 br i1 %cmp1, label %if, label %then
263
264 if:
265 br label %then
266
267 ; CHECK-LABEL: then:
268 ; CHECK-DISABLED: %phi = phi i8 [ 0, %if ], [ 1, %loop ]
269 then:
270 %phi = phi i8 [ 0, %if ], [ 1, %loop ]
271 %cmp2 = icmp eq i32 %y, 0
272 br i1 %cmp2, label %end, label %loop
273
274 end:
275 ret i8 %phi
276 }
277
278 ; CHECK-LABEL: @empty_diamond_phi
279 define i8 @empty_diamond_phi(i32 %x, i32 %y) {
280 ; CHECK-LABEL: entry:
281 ; CHECK: %cmp1 = icmp eq i32 %x, 0
282 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[ELSE_LICM:.*]]
283 entry:
284 br label %loop
285
286 ; CHECK-ENABLED: [[IF_LICM]]:
287 ; CHECK-ENABLED: br label %[[THEN_LICM:.*]]
288
289 ; CHECK-ENABLED: [[ELSE_LICM]]:
290 ; CHECK-ENABLED: br label %[[THEN_LICM]]
291
292 ; CHECK-ENABLED: [[THEN_LICM]]:
293 ; CHECK-ENABLED: %phi = phi i8 [ 0, %[[IF_LICM]] ], [ 1, %[[ELSE_LICM]] ]
294 ; CHECK: %cmp2 = icmp eq i32 %y, 0
295 ; CHECK: br label %loop
296
297 loop:
298 %cmp1 = icmp eq i32 %x, 0
299 br i1 %cmp1, label %if, label %else
300
301 if:
302 br label %then
303
304 else:
305 br label %then
306
307 ; CHECK-LABEL: then:
308 ; CHECK-DISABLED: %phi = phi i8 [ 0, %if ], [ 1, %else ]
309 then:
310 %phi = phi i8 [ 0, %if ], [ 1, %else ]
311 %cmp2 = icmp eq i32 %y, 0
312 br i1 %cmp2, label %end, label %loop
313
314 end:
315 ret i8 %phi
316 }
317
318 ; Check that we correctly handle the case that the first thing we try to hoist is a phi.
319 ; CHECK-LABEL: @empty_triangle_phi_first
320 define i8 @empty_triangle_phi_first(i32 %x, i1 %cond) {
321 ; CHECK-LABEL: entry:
322 ; CHECK-ENABLED: br i1 %cond, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
323 entry:
324 br label %loop
325
326 ; CHECK-ENABLED: [[IF_LICM]]:
327 ; CHECK-ENABLED: br label %[[THEN_LICM]]
328
329 ; CHECK-ENABLED: [[THEN_LICM]]:
330 ; CHECK-ENABLED: %phi = phi i8 [ 0, %[[IF_LICM]] ], [ 1, %entry ]
331 ; CHECK: %cmp = icmp eq i32 %x, 0
332 ; CHECK: br label %loop
333
334 loop:
335 br i1 %cond, label %if, label %then
336
337 if:
338 br label %then
339
340 ; CHECK-LABEL: then:
341 ; CHECK-DISABLED: %phi = phi i8 [ 0, %if ], [ 1, %loop ]
342 then:
343 %phi = phi i8 [ 0, %if ], [ 1, %loop ]
344 %cmp = icmp eq i32 %x, 0
345 br i1 %cmp, label %end, label %loop
346
347 end:
348 ret i8 %phi
349 }
350
351 ; CHECK-LABEL: @empty_diamond_phi
352 define i8 @empty_diamond_phi_first(i32 %x, i1 %cond) {
353 ; CHECK-LABEL: entry:
354 ; CHECK-ENABLED: br i1 %cond, label %[[IF_LICM:.*]], label %[[ELSE_LICM:.*]]
355 entry:
356 br label %loop
357
358 ; CHECK-ENABLED: [[IF_LICM]]:
359 ; CHECK-ENABLED: br label %[[THEN_LICM:.*]]
360
361 ; CHECK-ENABLED: [[ELSE_LICM]]:
362 ; CHECK-ENABLED: br label %[[THEN_LICM]]
363
364 ; CHECK-ENABLED: [[THEN_LICM]]:
365 ; CHECK-ENABLED: %phi = phi i8 [ 0, %[[IF_LICM]] ], [ 1, %[[ELSE_LICM]] ]
366 ; CHECK: %cmp = icmp eq i32 %x, 0
367 ; CHECK: br label %loop
368
369 loop:
370 br i1 %cond, label %if, label %else
371
372 if:
373 br label %then
374
375 else:
376 br label %then
377
378 ; CHECK-LABEL: then:
379 ; CHECK-DISABLED: %phi = phi i8 [ 0, %if ], [ 1, %else ]
380 then:
381 %phi = phi i8 [ 0, %if ], [ 1, %else ]
382 %cmp = icmp eq i32 %x, 0
383 br i1 %cmp, label %end, label %loop
384
385 end:
386 ret i8 %phi
387 }
388
389 ; CHECK-LABEL: @empty_triangle_phi_first
390 define i8 @empty_triangle_phi_first_empty_loop_head(i32 %x, i1 %cond) {
391 ; CHECK-LABEL: entry:
392 ; CHECK-ENABLED: br i1 %cond, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
393 entry:
394 br label %loop
395
396 ; CHECK-ENABLED: [[IF_LICM]]:
397 ; CHECK-ENABLED: br label %[[THEN_LICM]]
398
399 ; CHECK-ENABLED: [[THEN_LICM]]:
400 ; CHECK-ENABLED: %phi = phi i8 [ 0, %[[IF_LICM]] ], [ 1, %entry ]
401 ; CHECK: %cmp = icmp eq i32 %x, 0
402 ; CHECK: br label %loop
403
404 loop:
405 br label %test
406
407 test:
408 br i1 %cond, label %if, label %then
409
410 if:
411 br label %then
412
413 ; CHECK-LABEL: then:
414 ; CHECK-DISABLED: %phi = phi i8 [ 0, %if ], [ 1, %test ]
415 then:
416 %phi = phi i8 [ 0, %if ], [ 1, %test ]
417 %cmp = icmp eq i32 %x, 0
418 br i1 %cmp, label %end, label %loop
419
420 end:
421 ret i8 %phi
422 }
423
424 ; CHECK-LABEL: @empty_diamond_phi_first_empty_loop_head
425 define i8 @empty_diamond_phi_first_empty_loop_head(i32 %x, i1 %cond) {
426 ; CHECK-LABEL: entry:
427 ; CHECK-ENABLED: br i1 %cond, label %[[IF_LICM:.*]], label %[[ELSE_LICM:.*]]
428 entry:
429 br label %loop
430
431 ; CHECK-ENABLED: [[IF_LICM]]:
432 ; CHECK-ENABLED: br label %[[THEN_LICM:.*]]
433
434 ; CHECK-ENABLED: [[ELSE_LICM]]:
435 ; CHECK-ENABLED: br label %[[THEN_LICM]]
436
437 ; CHECK-ENABLED: [[THEN_LICM]]:
438 ; CHECK-ENABLED: %phi = phi i8 [ 0, %[[IF_LICM]] ], [ 1, %[[ELSE_LICM]] ]
439 ; CHECK: %cmp = icmp eq i32 %x, 0
440 ; CHECK: br label %loop
441
442 loop:
443 br label %test
444
445 test:
446 br i1 %cond, label %if, label %else
447
448 if:
449 br label %then
450
451 else:
452 br label %then
453
454 ; CHECK-LABEL: then:
455 ; CHECK-DISABLED: %phi = phi i8 [ 0, %if ], [ 1, %else ]
456 then:
457 %phi = phi i8 [ 0, %if ], [ 1, %else ]
458 %cmp = icmp eq i32 %x, 0
459 br i1 %cmp, label %end, label %loop
460
461 end:
462 ret i8 %phi
463 }
464
465 ; The phi is on one branch of a diamond while simultaneously at the end of a
466 ; triangle. Check that we duplicate the triangle and not the diamond.
467 ; CHECK-LABEL: @triangle_diamond
468 define void @triangle_diamond(i32* %ptr, i32 %x, i32 %y) {
469 ; CHECK-LABEL: entry:
470 ; CHECK-DAG: %cmp1 = icmp ne i32 %x, 0
471 ; CHECK-DAG: %cmp2 = icmp ne i32 %y, 0
472 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
473 entry:
474 br label %loop
475
476 ; CHECK-ENABLED: [[IF_LICM]]:
477 ; CHECK-ENABLED: br label %[[THEN_LICM]]
478
479 ; CHECK-ENABLED: [[THEN_LICM]]:
480 ; CHECK-ENABLED: %phi = phi i32 [ 0, %[[IF_LICM]] ], [ 127, %entry ]
481 ; CHECK: br label %loop
482
483 loop:
484 %cmp1 = icmp ne i32 %x, 0
485 br i1 %cmp1, label %if, label %then
486
487 if:
488 %cmp2 = icmp ne i32 %y, 0
489 br i1 %cmp2, label %if.then, label %then
490
491 ; CHECK-LABEL: then:
492 ; CHECK-DISABLED: %phi = phi i32 [ 0, %if ], [ 127, %loop ]
493 then:
494 %phi = phi i32 [ 0, %if ], [ 127, %loop ]
495 store i32 %phi, i32* %ptr
496 br label %end
497
498 if.then:
499 br label %end
500
501 end:
502 br label %loop
503 }
504
505 ; As the previous, but the end of the diamond is the head of the loop.
506 ; CHECK-LABEL: @triangle_diamond_backedge
507 define void @triangle_diamond_backedge(i32* %ptr, i32 %x, i32 %y) {
508 ; CHECK-LABEL: entry:
509 ; CHECK-DAG: %cmp1 = icmp ne i32 %x, 0
510 ; CHECK-DAG: %cmp2 = icmp ne i32 %y, 0
511 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
512 entry:
513 br label %loop
514
515 ; CHECK-ENABLED: [[IF_LICM]]:
516 ; CHECK-ENABLED: br label %[[THEN_LICM]]
517
518 ; CHECK-ENABLED: [[THEN_LICM]]:
519 ; CHECK-ENABLED: %phi = phi i32 [ 0, %[[IF_LICM]] ], [ 127, %entry ]
520 ; CHECK: br label %loop
521
522 loop:
523 %cmp1 = icmp ne i32 %x, 0
524 br i1 %cmp1, label %if, label %then
525
526 if:
527 %cmp2 = icmp ne i32 %y, 0
528 br i1 %cmp2, label %backedge, label %then
529
530 ; CHECK-LABEL: then:
531 ; CHECK-DISABLED: %phi = phi i32 [ 0, %if ], [ 127, %loop ]
532 then:
533 %phi = phi i32 [ 0, %if ], [ 127, %loop ]
534 store i32 %phi, i32* %ptr
535 br label %loop
536
537 backedge:
538 br label %loop
539 }
540
541 ; TODO: The inner diamonds can be hoisted, but not currently the outer diamond
542 ; CHECK-LABEL: @diamonds_inside_diamond
543 define void @diamonds_inside_diamond(i32 %x, i32* %p) {
544 ; CHECK-LABEL: entry:
545 ; CHECK-DAG: %cmp1 = icmp sgt i32 %x, 0
546 ; CHECK-DAG: %cmp3 = icmp slt i32 %x, -10
547 ; CHECK-ENABLED: br i1 %cmp3, label %[[ELSE_IF_LICM:.*]], label %[[ELSE_ELSE_LICM:.*]]
548 entry:
549 br label %loop
550
551 ; CHECK-ENABLED: [[ELSE_IF_LICM]]:
552 ; CHECK-ENABLED: br label %[[ELSE_THEN_LICM:.*]]
553
554 ; CHECK-ENABLED: [[ELSE_ELSE_LICM]]:
555 ; CHECK-ENABLED: br label %[[ELSE_THEN_LICM]]
556
557 ; CHECK-ENABLED: [[ELSE_THEN_LICM]]:
558 ; CHECK-ENABLED: %phi2 = phi i32 [ 2, %[[ELSE_IF_LICM]] ], [ 3, %[[ELSE_ELSE_LICM]] ]
559 ; CHECK: %cmp2 = icmp sgt i32 %x, 10
560 ; CHECK-ENABLED: br i1 %cmp2, label %[[IF_IF_LICM:.*]], label %[[IF_ELSE_LICM:.*]]
561
562 ; CHECK-ENABLED: [[IF_IF_LICM]]:
563 ; CHECK-ENABLED: br label %[[IF_THEN_LICM:.*]]
564
565 ; CHECK-ENABLED: [[IF_ELSE_LICM]]:
566 ; CHECK-ENABLED: br label %[[IF_THEN_LICM]]
567
568 ; CHECK-ENABLED: [[IF_THEN_LICM]]:
569 ; CHECK-ENABLED: %phi1 = phi i32 [ 0, %[[IF_IF_LICM]] ], [ 1, %[[IF_ELSE_LICM]] ]
570 ; CHECK: br label %loop
571
572 loop:
573 %cmp1 = icmp sgt i32 %x, 0
574 br i1 %cmp1, label %if, label %else
575
576 if:
577 %cmp2 = icmp sgt i32 %x, 10
578 br i1 %cmp2, label %if.if, label %if.else
579
580 if.if:
581 br label %if.then
582
583 if.else:
584 br label %if.then
585
586 ; CHECK-LABEL: if.then:
587 ; CHECK-DISABLED: %phi1 = phi i32 [ 0, %if.if ], [ 1, %if.else ]
588 if.then:
589 %phi1 = phi i32 [ 0, %if.if ], [ 1, %if.else ]
590 br label %then
591
592 else:
593 %cmp3 = icmp slt i32 %x, -10
594 br i1 %cmp3, label %else.if, label %else.else
595
596 else.if:
597 br label %else.then
598
599 else.else:
600 br label %else.then
601
602 ; CHECK-LABEL: else.then:
603 ; CHECK-DISABLED: %phi2 = phi i32 [ 2, %else.if ], [ 3, %else.else ]
604 else.then:
605 %phi2 = phi i32 [ 2, %else.if ], [ 3, %else.else ]
606 br label %then
607
608 ; CHECK-LABEL: then:
609 ; CHECK: %phi3 = phi i32 [ %phi1, %if.then ], [ %phi2, %else.then ]
610 ; CHECK: %cmp4 = icmp ne i32 %phi3, 0
611 then:
612 %phi3 = phi i32 [ %phi1, %if.then ], [ %phi2, %else.then ]
613 store i32 %phi3, i32* %p
614 %cmp4 = icmp ne i32 %phi3, 0
615 br i1 %cmp4, label %loop, label %end
616
617 end:
618 ret void
619 }
620
621 ; We can hoist blocks that contain an edge that exits the loop by ignoring that
622 ; edge in the hoisted block.
623 ; CHECK-LABEL: @triangle_phi_loopexit
624 define void @triangle_phi_loopexit(i32 %x, i32* %p) {
625 ; CHECK-LABEL: entry:
626 ; CHECK-DAG: %add = add i32 %x, 1
627 ; CHECK-DAG: %cmp1 = icmp sgt i32 %x, 0
628 ; CHECK-DAG: %cmp2 = icmp sgt i32 10, %add
629 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
630 entry:
631 br label %loop
632
633 ; CHECK-ENABLED: [[IF_LICM]]:
634 ; CHECK-ENABLED: br label %[[THEN_LICM]]
635
636 ; CHECK-ENABLED: [[THEN_LICM]]:
637 ; CHECK-ENABLED: %phi = phi i32 [ %add, %[[IF_LICM]] ], [ %x, %entry ]
638 ; CHECK: br label %loop
639
640 loop:
641 %cmp1 = icmp sgt i32 %x, 0
642 br i1 %cmp1, label %if, label %then
643
644 if:
645 %add = add i32 %x, 1
646 %cmp2 = icmp sgt i32 10, %add
647 br i1 %cmp2, label %then, label %end
648
649 ; CHECK-LABEL: then:
650 ; CHECK-DISABLED: %phi = phi i32 [ %add, %if ], [ %x, %loop ]
651 then:
652 %phi = phi i32 [ %add, %if ], [ %x, %loop ]
653 store i32 %phi, i32* %p
654 %cmp3 = icmp ne i32 %phi, 0
655 br i1 %cmp3, label %loop, label %end
656
657 end:
658 ret void
659 }
660
661 ; CHECK-LABEL: @diamond_phi_oneloopexit
662 define void @diamond_phi_oneloopexit(i32 %x, i32* %p) {
663 ; CHECK-LABEL: entry:
664 ; CHECK-DAG: %add = add i32 %x, 1
665 ; CHECK-DAG: %cmp1 = icmp sgt i32 %x, 0
666 ; CHECK-DAG: %cmp2 = icmp sgt i32 10, %add
667 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
668 entry:
669 br label %loop
670
671 ; CHECK-ENABLED: [[IF_LICM]]:
672 ; CHECK-ENABLED: br label %[[THEN_LICM:.*]]
673
674 ; CHECK-ENABLED: [[ELSE_LICM]]:
675 ; CHECK-DAG: %sub = sub i32 %x, 1
676 ; CHECK-ENABLED: br label %[[THEN_LICM]]
677
678 ; CHECK-ENABLED: [[THEN_LICM]]
679 ; CHECK-ENABLED: %phi = phi i32 [ %add, %[[IF_LICM]] ], [ %sub, %[[ELSE_LICM]] ]
680 ; CHECK-ENABLED: %cmp3 = icmp ne i32 %phi, 0
681 ; CHECK: br label %loop
682
683 loop:
684 %cmp1 = icmp sgt i32 %x, 0
685 br i1 %cmp1, label %if, label %else
686
687 if:
688 %add = add i32 %x, 1
689 %cmp2 = icmp sgt i32 10, %add
690 br i1 %cmp2, label %then, label %end
691
692 else:
693 %sub = sub i32 %x, 1
694 br label %then
695
696 ; CHECK-LABEL: then:
697 ; CHECK-DISABLED: %phi = phi i32 [ %add, %if ], [ %sub, %else ]
698 then:
699 %phi = phi i32 [ %add, %if ], [ %sub, %else ]
700 store i32 %phi, i32* %p
701 %cmp3 = icmp ne i32 %phi, 0
702 br i1 %cmp3, label %loop, label %end
703
704 end:
705 ret void
706 }
707
708 ; CHECK-LABEL: @diamond_phi_twoloopexit
709 define void @diamond_phi_twoloopexit(i32 %x, i32* %p) {
710 ; CHECK-LABEL: entry:
711 ; CHECK-DAG: %sub = sub i32 %x, 1
712 ; CHECK-DAG: %add = add i32 %x, 1
713 ; CHECK-DAG: %cmp1 = icmp sgt i32 %x, 0
714 ; CHECK-DAG: %cmp2 = icmp sgt i32 10, %add
715 ; CHECK-DAG: %cmp3 = icmp sgt i32 10, %sub
716 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
717 entry:
718 br label %loop
719
720 ; CHECK-ENABLED: [[IF_LICM]]:
721 ; CHECK-ENABLED: br label %[[THEN_LICM:.*]]
722
723 ; CHECK-ENABLED: [[ELSE_LICM]]:
724 ; CHECK-ENABLED: br label %[[THEN_LICM]]
725
726 ; CHECK-ENABLED: [[THEN_LICM]]
727 ; CHECK-ENABLED: %phi = phi i32 [ %add, %[[IF_LICM]] ], [ %sub, %[[ELSE_LICM]] ]
728 ; CHECK-ENABLED: %cmp4 = icmp ne i32 %phi, 0
729 ; CHECK: br label %loop
730
731 loop:
732 %cmp1 = icmp sgt i32 %x, 0
733 br i1 %cmp1, label %if, label %else
734
735 if:
736 %add = add i32 %x, 1
737 %cmp2 = icmp sgt i32 10, %add
738 br i1 %cmp2, label %then, label %end
739
740 else:
741 %sub = sub i32 %x, 1
742 %cmp3 = icmp sgt i32 10, %sub
743 br i1 %cmp3, label %then, label %end
744
745 ; CHECK-LABEL: then:
746 ; CHECK-DISABLED: %phi = phi i32 [ %add, %if ], [ %sub, %else ]
747 ; CHECK-DISABLED: %cmp4 = icmp ne i32 %phi, 0
748 then:
749 %phi = phi i32 [ %add, %if ], [ %sub, %else ]
750 store i32 %phi, i32* %p
751 %cmp4 = icmp ne i32 %phi, 0
752 br i1 %cmp4, label %loop, label %end
753
754 end:
755 ret void
756 }
757
758 ; The store cannot be hoisted, so add and shr cannot be hoisted into a
759 ; conditional block.
760 ; CHECK-LABEL: @conditional_use
761 define void @conditional_use(i32 %x, i32* %p) {
762 ; CHECK-LABEL: entry:
763 ; CHECK-DAG: %cond = icmp ugt i32 %x, 0
764 ; CHECK-DAG: %add = add i32 %x, 5
765 ; CHECK-DAG: %shr = ashr i32 %add, 1
766 ; CHECK: br label %loop
767 entry:
768 br label %loop
769
770 loop:
771 %cond = icmp ugt i32 %x, 0
772 br i1 %cond, label %if, label %else
773
774 ; CHECK-LABEL: if:
775 ; CHECK: store i32 %shr, i32* %p, align 4
776 if:
777 %add = add i32 %x, 5
778 %shr = ashr i32 %add, 1
779 store i32 %shr, i32* %p, align 4
780 br label %then
781
782 else:
783 br label %then
784
785 then:
786 br label %loop
787 }
788
789 ; A diamond with two triangles on the left and one on the right. This test is
790 ; to check that we have a unique loop preheader when we hoist the store (and so
791 ; don't fail an assertion).
792 ; CHECK-LABEL: @triangles_in_diamond
793 define void @triangles_in_diamond(i32* %ptr) {
794 ; CHECK-LABEL: entry:
795 ; CHECK: store i32 0, i32* %ptr, align 4
796 ; CHECK: br label %loop
797 entry:
798 br label %loop
799
800 loop:
801 br i1 undef, label %left_triangle_1, label %right_triangle
802
803 left_triangle_1:
804 br i1 undef, label %left_triangle_1_if, label %left_triangle_2
805
806 left_triangle_1_if:
807 br label %left_triangle_2
808
809 left_triangle_2:
810 br i1 undef, label %left_triangle_2_if, label %left_triangle_2_then
811
812 left_triangle_2_if:
813 br label %left_triangle_2_then
814
815 left_triangle_2_then:
816 br label %loop.end
817
818 right_triangle:
819 br i1 undef, label %right_triangle.if, label %right_triangle.then
820
821 right_triangle.if:
822 br label %right_triangle.then
823
824 right_triangle.then:
825 br label %loop.end
826
827 loop.end:
828 store i32 0, i32* %ptr, align 4
829 br label %loop
830 }
831
832 ; %cmp dominates its used after being hoisted, but not after %brmerge is rehoisted
833 ; CHECK-LABEL: @rehoist
834 define void @rehoist(i8* %this, i32 %x) {
835 ; CHECK-LABEL: entry:
836 ; CHECK-DAG: %sub = add nsw i32 %x, -1
837 ; CHECK-DAG: %fptr = bitcast i8* %this to void (i8*)*
838 ; CHECK-DAG: %cmp = icmp eq i32 0, %sub
839 ; CHECK-DAG: %brmerge = or i1 %cmp, true
840 entry:
841 %sub = add nsw i32 %x, -1
842 br label %loop
843
844 loop:
845 br i1 undef, label %if1, label %else1
846
847 if1:
848 %fptr = bitcast i8* %this to void (i8*)*
849 call void %fptr(i8* %this)
850 br label %then1
851
852 else1:
853 br label %then1
854
855 then1:
856 %cmp = icmp eq i32 0, %sub
857 br i1 %cmp, label %end, label %else2
858
859 else2:
860 %brmerge = or i1 %cmp, true
861 br i1 %brmerge, label %if3, label %end
862
863 if3:
864 br label %end
865
866 end:
867 br label %loop
868 }
869
870 ; A test case that uses empty blocks in a way that can cause control flow
871 ; hoisting to get confused.
872 ; CHECK-LABEL: @empty_blocks_multiple_conditional_branches
873 define void @empty_blocks_multiple_conditional_branches(float %arg, float* %ptr) {
874 ; CHECK-LABEL: entry
875 ; CHECK-DAG: %div1 = fmul float %arg, 4.000000e+00
876 ; CHECK-DAG: %div2 = fmul float %arg, 2.000000e+00
877 entry:
878 br label %loop
879
880 ; The exact path to the phi isn't checked here, because it depends on whether
881 ; cond2 or cond3 is hoisted first
882 ; CHECK-ENABLED: %phi = phi float [ 0.000000e+00, %{{.*}} ], [ %div1, %{{.*}} ]
883 ; CHECK: br label %loop
884
885 loop:
886 br i1 undef, label %backedge2, label %cond1
887
888 cond1:
889 br i1 undef, label %cond1.if, label %cond1.else
890
891 cond1.else:
892 br label %cond3
893
894 cond1.if:
895 br label %cond1.if.next
896
897 cond1.if.next:
898 br label %cond2
899
900 cond2:
901 %div1 = fmul float %arg, 4.000000e+00
902 br i1 undef, label %cond2.if, label %cond2.then
903
904 cond2.if:
905 br label %cond2.then
906
907 ; CHECK-LABEL: cond2.then:
908 ; CHECK-DISABLED: %phi = phi float [ 0.000000e+00, %cond2 ], [ %div1, %cond2.if ]
909 cond2.then:
910 %phi = phi float [ 0.000000e+00, %cond2 ], [ %div1, %cond2.if ]
911 store float %phi, float* %ptr
912 br label %backedge2
913
914 cond3:
915 br i1 undef, label %cond3.then, label %cond3.if
916
917 cond3.if:
918 %div2 = fmul float %arg, 2.000000e+00
919 store float %div2, float* %ptr
920 br label %cond3.then
921
922 cond3.then:
923 br label %loop
924
925 backedge2:
926 br label %loop
927 }
928
929 ; We can't do much here, so mainly just check that we don't crash.
930 ; CHECK-LABEL: @many_path_phi
931 define void @many_path_phi(i32* %ptr1, i32* %ptr2) {
932 ; CHECK-LABEL: entry:
933 ; CHECK-DAG: %gep3 = getelementptr inbounds i32, i32* %ptr2, i32 2
934 ; CHECK-DAG: %gep2 = getelementptr inbounds i32, i32* %ptr2, i32 2
935 ; CHECK: br label %loop
936 entry:
937 br label %loop
938
939 loop:
940 %phi1 = phi i32 [ 0, %entry ], [ %phi2, %end ]
941 %cmp1 = icmp ugt i32 %phi1, 3
942 br i1 %cmp1, label %cond2, label %cond1
943
944 cond1:
945 br i1 undef, label %end, label %cond1.else
946
947 cond1.else:
948 %gep2 = getelementptr inbounds i32, i32* %ptr2, i32 2
949 %val2 = load i32, i32* %gep2, align 4
950 %cmp2 = icmp eq i32 %val2, 13
951 br i1 %cmp2, label %cond1.end, label %end
952
953 cond1.end:
954 br label %end
955
956 cond2:
957 br i1 undef, label %end, label %cond2.else
958
959 cond2.else:
960 %gep3 = getelementptr inbounds i32, i32* %ptr2, i32 2
961 %val3 = load i32, i32* %gep3, align 4
962 %cmp3 = icmp eq i32 %val3, 13
963 br i1 %cmp3, label %cond2.end, label %end
964
965 cond2.end:
966 br label %end
967
968 end:
969 %phi2 = phi i32 [ 1, %cond1 ], [ 2, %cond1.else ], [ 3, %cond1.end ], [ 4, %cond2 ], [ 5, %cond2.else ], [ 6, %cond2.end ]
970 br label %loop
971 }
972
973 ; Check that we correctly handle the hoisting of %gep when theres a critical
974 ; edge that branches to the preheader.
975 ; CHECK-LABEL: @crit_edge
976 define void @crit_edge(i32* %ptr, i32 %idx, i1 %cond1, i1 %cond2) {
977 ; CHECK-LABEL: entry:
978 ; CHECK: %gep = getelementptr inbounds i32, i32* %ptr, i32 %idx
979 ; CHECK: br label %preheader
980 entry:
981 br label %preheader
982
983 preheader:
984 br label %loop
985
986 loop:
987 br i1 %cond1, label %then, label %if
988
989 if:
990 %gep = getelementptr inbounds i32, i32* %ptr, i32 %idx
991 %val = load i32, i32* %gep
992 br label %then
993
994 then:
995 %phi = phi i32 [ %val, %if ], [ 0, %loop ]
996 store i32 %phi, i32* %ptr
997 br i1 %cond2, label %loop, label %crit_edge
998
999 crit_edge:
1000 br label %preheader
1001 }
1002
1003 ; Check that the conditional sub is correctly hoisted from the inner loop to the
1004 ; preheader of the outer loop.
1005 ; CHECK-LABEL: @hoist_from_innermost_loop
1006 define void @hoist_from_innermost_loop(i32 %nx, i32* %ptr) {
1007 ; CHECK-LABEL: entry:
1008 ; CHECK-DAG: %sub = sub nsw i32 0, %nx
1009 ; CHECK: br label %outer_loop
1010 entry:
1011 br label %outer_loop
1012
1013 outer_loop:
1014 br label %middle_loop
1015
1016 middle_loop:
1017 br label %inner_loop
1018
1019 inner_loop:
1020 br i1 undef, label %inner_loop_end, label %if
1021
1022 if:
1023 %sub = sub nsw i32 0, %nx
1024 store i32 %sub, i32* %ptr, align 4
1025 br label %inner_loop_end
1026
1027 inner_loop_end:
1028 br i1 undef, label %inner_loop, label %middle_loop_end
1029
1030 middle_loop_end:
1031 br i1 undef, label %middle_loop, label %outer_loop_end
1032
1033 outer_loop_end:
1034 br label %outer_loop
1035 }
1036
1037 ; We have a diamond starting from %if, but %if.if is also reachable from %loop,
1038 ; so %gep should not be conditionally hoisted.
1039 ; CHECK-LABEL: @diamond_with_extra_in_edge
1040 define void @diamond_with_extra_in_edge(i32* %ptr1, i32* %ptr2, i32 %arg) {
1041 ; CHECK-LABEL: entry:
1042 ; CHECK-DAG: %cmp2 = icmp ne i32 0, %arg
1043 ; CHECK-DAG: %gep = getelementptr i32, i32* %ptr1, i32 4
1044 ; CHECK: br label %loop
1045 entry:
1046 br label %loop
1047
1048 loop:
1049 %phi1 = phi i32 [ 0, %entry ], [ %phi2, %then ]
1050 %cmp1 = icmp ugt i32 16, %phi1
1051 br i1 %cmp1, label %if, label %if.if
1052
1053 if:
1054 %cmp2 = icmp ne i32 0, %arg
1055 br i1 %cmp2, label %if.if, label %if.else
1056
1057 if.if:
1058 %gep = getelementptr i32, i32* %ptr1, i32 4
1059 %val = load i32, i32* %gep, align 4
1060 br label %then
1061
1062 if.else:
1063 br label %then
1064
1065 then:
1066 %phi2 = phi i32 [ %val, %if.if ], [ %phi1, %if.else ]
1067 store i32 %phi2, i32* %ptr2, align 4
1068 br label %loop
1069 }
1070
1071 ; %loop/%if/%then form a triangle, but %loop/%if/%then/%end also form a diamond.
1072 ; The triangle should be picked for conditional hoisting.
1073 ; CHECK-LABEL: @both_triangle_and_diamond
1074 define void @both_triangle_and_diamond(i32* %ptr1, i32* %ptr2, i32 %arg) {
1075 ; CHECK-LABEL: entry:
1076 ; CHECK-DAG: %cmp1 = icmp ne i32 0, %arg
1077 ; CHECK-DAG: %gep = getelementptr i32, i32* %ptr1, i32 4
1078 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]]
1079 entry:
1080 br label %loop
1081
1082 ; CHECK-ENABLED: [[IF_LICM]]:
1083 ; CHECK-ENABLED: br label %[[THEN_LICM]]
1084
1085 ; CHECK-ENABLED: [[THEN_LICM]]:
1086 ; CHECK-ENABLED: %phi2 = phi i32 [ 0, %[[IF_LICM]] ], [ 1, %entry ]
1087 ; CHECK: br label %loop
1088
1089 loop:
1090 %phi1 = phi i32 [ 0, %entry ], [ %phi3, %end ]
1091 %cmp1 = icmp ne i32 0, %arg
1092 br i1 %cmp1, label %if, label %then
1093
1094 if:
1095 %gep = getelementptr i32, i32* %ptr1, i32 4
1096 %val = load i32, i32* %gep, align 4
1097 %cmp2 = icmp ugt i32 16, %phi1
1098 br i1 %cmp2, label %end, label %then
1099
1100 ; CHECK-LABEL: then:
1101 ; CHECK-DISABLED: %phi2 = phi i32 [ 0, %if ], [ 1, %loop ]
1102 then:
1103 %phi2 = phi i32 [ 0, %if ], [ 1, %loop ]
1104 br label %end
1105
1106 end:
1107 %phi3 = phi i32 [ %phi2, %then ], [ %val, %if ]
1108 store i32 %phi3, i32* %ptr2, align 4
1109 br label %loop
1110 }
1111
1112 ; We shouldn't duplicate the branch at the end of %loop and should instead hoist
1113 ; %val to %entry.
1114 ; CHECK-LABEL: @same_destination_branch
1115 define i32 @same_destination_branch(i32 %arg1, i32 %arg2) {
1116 ; CHECK-LABEL: entry:
1117 ; CHECK-DAG: %cmp1 = icmp ne i32 %arg2, 0
1118 ; CHECK-DAG: %val = add i32 %arg1, 1
1119 ; CHECK: br label %loop
1120 entry:
1121 br label %loop
1122
1123 ; CHECK-LABEL: loop:
1124 ; CHECK: %phi = phi i32 [ 0, %entry ], [ %add, %then ]
1125 loop:
1126 %phi = phi i32 [ 0, %entry ], [ %add, %then ]
1127 %add = add i32 %phi, 1
1128 %cmp1 = icmp ne i32 %arg2, 0
1129 br i1 %cmp1, label %if, label %if
1130
1131 if:
1132 %val = add i32 %arg1, 1
1133 br label %then
1134
1135 then:
1136 %cmp2 = icmp ne i32 %val, %phi
1137 br i1 %cmp2, label %loop, label %end
1138
1139 end:
1140 ret i32 %val
1141 }
1142
1143 ; Diamond-like control flow but the left/right blocks actually have the same
1144 ; destinations.
1145 ; TODO: We could potentially hoist all of phi2-4, but currently only hoist phi2.
1146 ; CHECK-LABEL: @diamond_like_same_destinations
1147 define i32 @diamond_like_same_destinations(i32 %arg1, i32 %arg2) {
1148 ; CHECK-LABEL: entry:
1149 ; CHECK-DAG: %cmp1 = icmp ne i32 %arg1, 0
1150 ; CHECK-DAG: %cmp2 = icmp ugt i32 %arg2, 1
1151 ; CHECK-DAG: %cmp3 = icmp ugt i32 %arg2, 2
1152 ; CHECK-ENABLED: br i1 %cmp1, label %[[LEFT1_LICM:.*]], label %[[RIGHT1_LICM:.*]]
1153 entry:
1154 br label %loop
1155
1156 ; CHECK-ENABLED: [[LEFT1_LICM]]:
1157 ; CHECK-ENABLED: br label %[[LEFT2_LICM:.*]]
1158
1159 ; CHECK-ENABLED: [[RIGHT1_LICM]]:
1160 ; CHECK-ENABLED: br label %[[LEFT2_LICM]]
1161
1162 ; CHECK-ENABLED: [[LEFT2_LICM]]:
1163 ; CHECK-ENABLED: %phi2 = phi i32 [ 0, %[[LEFT1_LICM]] ], [ 1, %[[RIGHT1_LICM]] ]
1164 ; CHECK: br label %loop
1165
1166 loop:
1167 %phi1 = phi i32 [ 0, %entry ], [ %add, %loopend ]
1168 %add = add i32 %phi1, 1
1169 %cmp1 = icmp ne i32 %arg1, 0
1170 br i1 %cmp1, label %left1, label %right1
1171
1172 left1:
1173 %cmp2 = icmp ugt i32 %arg2, 1
1174 br i1 %cmp2, label %left2, label %right2
1175
1176 right1:
1177 %cmp3 = icmp ugt i32 %arg2, 2
1178 br i1 %cmp3, label %left2, label %right2
1179
1180 ; CHECK-LABEL: left2:
1181 ; CHECK-DISABLED: %phi2 = phi i32 [ 0, %left1 ], [ 1, %right1 ]
1182 left2:
1183 %phi2 = phi i32 [ 0, %left1 ], [ 1, %right1 ]
1184 br label %loopend
1185
1186 ; CHECK-LABEL: right2:
1187 ; CHECK: %phi3 = phi i32 [ 2, %left1 ], [ 3, %right1 ]
1188 right2:
1189 %phi3 = phi i32 [ 2, %left1 ], [ 3, %right1 ]
1190 br label %loopend
1191
1192 ; CHECK-LABEL: loopend:
1193 ; CHECK: %phi4 = phi i32 [ %phi2, %left2 ], [ %phi3, %right2 ]
1194 loopend:
1195 %phi4 = phi i32 [ %phi2, %left2 ], [ %phi3, %right2 ]
1196 %cmp4 = icmp ne i32 %phi1, 32
1197 br i1 %cmp4, label %loop, label %end
1198
1199 end:
1200 ret i32 %phi4
1201 }
1202
1203 ; A phi with multiple incoming values for the same block due to a branch with
1204 ; two destinations that are actually the same. We can't hoist this.
1205 ; TODO: This could be hoisted by erasing one of the incoming values.
1206 ; CHECK-LABEL: @phi_multiple_values_same_block
1207 define i32 @phi_multiple_values_same_block(i32 %arg) {
1208 ; CHECK-LABEL: entry:
1209 ; CHECK: %cmp = icmp sgt i32 %arg, 4
1210 ; CHECK-NOT: phi
1211 ; CHECK: br label %loop
1212 entry:
1213 br label %loop
1214
1215 loop:
1216 %cmp = icmp sgt i32 %arg, 4
1217 br i1 %cmp, label %if, label %then
1218
1219 if:
1220 br i1 undef, label %then, label %then
1221
1222 then:
1223 %phi = phi i32 [ %arg, %loop ], [ 1, %if ], [ 1, %if ]
1224 br i1 undef, label %exit, label %loop
1225
1226 exit:
1227 ret i32 %phi
1228 }
1229
1230 ; %phi is conditionally used in %d, and the store that %d is used in cannot be
1231 ; hoisted. This means that we have to rehoist %d, but have to make sure to
1232 ; rehoist it after %phi.
1233 ; CHECK-LABEL: @phi_conditional_use
1234 define i64 @phi_conditional_use(i32 %f, i32* %g) {
1235 ; CHECK-LABEL: entry:
1236 ; CHECK: %cmp1 = icmp eq i32 %f, 1
1237 ; CHECK: %cmp2 = icmp eq i32 %f, 0
1238 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_END_LICM:.*]], label %[[IF_THEN_LICM:.*]]
1239 entry:
1240 %cmp1 = icmp eq i32 %f, 1
1241 %cmp2 = icmp eq i32 %f, 0
1242 br label %loop
1243
1244 ; CHECK-ENABLED: [[IF_THEN_LICM]]:
1245 ; CHECK-ENABLED: br label %[[IF_END_LICM]]
1246
1247 ; CHECK-ENABLED: [[IF_END_LICM]]:
1248 ; CHECK-ENABLED: %phi = phi i64 [ 0, %entry ], [ 1, %[[IF_THEN_LICM]] ]
1249 ; CHECK-ENABLED: %d = getelementptr inbounds i32, i32* %g, i64 %phi
1250 ; CHECK-ENABLED: i1 %cmp2, label %[[LOOP_BACKEDGE_LICM:.*]], label %[[IF_THEN2_LICM:.*]]
1251
1252 ; CHECK-ENABLED: [[IF_THEN2_LICM]]:
1253 ; CHECK-ENABLED: br label %[[LOOP_BACKEDGE_LICM]]
1254
1255 ; CHECK-ENABLED: [[LOOP_BACKEDGE_LICM]]:
1256 ; CHECK: br label %loop
1257
1258 loop:
1259 br i1 %cmp1, label %if.end, label %if.then
1260
1261 if.then:
1262 br label %if.end
1263
1264 ; CHECK-LABEL: if.end:
1265 ; CHECK-DISABLED: %phi = phi i64 [ 0, %loop ], [ 1, %if.then ]
1266 if.end:
1267 %phi = phi i64 [ 0, %loop ], [ 1, %if.then ]
1268 br i1 %cmp2, label %loop.backedge, label %if.then2
1269
1270 ; CHECK-LABEL: if.then2:
1271 ; CHECK-DISABLED: %d = getelementptr inbounds i32, i32* %g, i64 %phi
1272 if.then2:
1273 %d = getelementptr inbounds i32, i32* %g, i64 %phi
1274 store i32 1, i32* %d, align 4
1275 br label %loop.backedge
1276
1277 loop.backedge:
1278 br label %loop
1279 }
1280
1281 ; As above, but we have two such phis
1282 ; CHECK-LABEL: @phi_conditional_use_twice
1283 define i64 @phi_conditional_use_twice(i32 %f, i32* %g) {
1284 ; CHECK-LABEL: entry:
1285 ; CHECK: %cmp1 = icmp eq i32 %f, 1
1286 ; CHECK: %cmp2 = icmp eq i32 %f, 0
1287 ; CHECK-ENABLED: br i1 %cmp1, label %[[IF_END_LICM:.*]], label %[[IF_THEN_LICM:.*]]
1288 entry:
1289 %cmp1 = icmp eq i32 %f, 1
1290 %cmp2 = icmp eq i32 %f, 0
1291 %cmp3 = icmp sgt i32 %f, 0
1292 br label %loop
1293
1294 ; CHECK-ENABLED: [[IF_THEN_LICM]]:
1295 ; CHECK-ENABLED: br label %[[IF_END_LICM]]
1296
1297 ; CHECK-ENABLED: [[IF_END_LICM]]:
1298 ; CHECK-ENABLED: %phi1 = phi i64 [ 0, %entry ], [ 1, %[[IF_THEN_LICM]] ]
1299 ; CHECK-ENABLED: %d = getelementptr inbounds i32, i32* %g, i64 %phi1
1300 ; CHECK-ENABLED: i1 %cmp2, label %[[IF_END2_LICM:.*]], label %[[IF_THEN2_LICM:.*]]
1301
1302 ; CHECK-ENABLED: [[IF_THEN2_LICM]]:
1303 ; CHECK-ENABLED: br label %[[IF_END2_LICM]]
1304
1305 ; CHECK-ENABLED: [[IF_END2_LICM]]:
1306 ; CHECK-ENABLED: %phi2 = phi i64 [ 2, %[[IF_END_LICM]] ], [ 3, %[[IF_THEN2_LICM]] ]
1307 ; CHECK-ENABLED: %e = getelementptr inbounds i32, i32* %g, i64 %phi2
1308 ; CHECK-ENABLED: i1 %cmp3, label %[[LOOP_BACKEDGE_LICM:.*]], label %[[IF_THEN3_LICM:.*]]
1309
1310 ; CHECK-ENABLED: [[IF_THEN3_LICM]]:
1311 ; CHECK-ENABLED: br label %[[LOOP_BACKEDGE_LICM]]
1312
1313 ; CHECK-ENABLED: [[LOOP_BACKEDGE_LICM]]:
1314 ; CHECK: br label %loop
1315
1316 loop:
1317 br i1 %cmp1, label %if.end, label %if.then
1318
1319 if.then:
1320 br label %if.end
1321
1322 ; CHECK-LABEL: if.end:
1323 ; CHECK-DISABLED: %phi1 = phi i64 [ 0, %loop ], [ 1, %if.then ]
1324 if.end:
1325 %phi1 = phi i64 [ 0, %loop ], [ 1, %if.then ]
1326 br i1 %cmp2, label %if.end2, label %if.then2
1327
1328 ; CHECK-LABEL: if.then2:
1329 ; CHECK-DISABLED: %d = getelementptr inbounds i32, i32* %g, i64 %phi1
1330 if.then2:
1331 %d = getelementptr inbounds i32, i32* %g, i64 %phi1
1332 store i32 1, i32* %d, align 4
1333 br label %if.end2
1334
1335 ; CHECK-LABEL: if.end2:
1336 ; CHECK-DISABLED: %phi2 = phi i64 [ 2, %if.end ], [ 3, %if.then2 ]
1337 if.end2:
1338 %phi2 = phi i64 [ 2, %if.end ], [ 3, %if.then2 ]
1339 br i1 %cmp3, label %loop.backedge, label %if.then3
1340
1341 ; CHECK-LABEL: if.then3:
1342 ; CHECK-DISABLED: %e = getelementptr inbounds i32, i32* %g, i64 %phi2
1343 if.then3:
1344 %e = getelementptr inbounds i32, i32* %g, i64 %phi2
1345 store i32 1, i32* %e, align 4
1346 br label %loop.backedge
1347
1348 loop.backedge:
1349 br label %loop
1350 }