19
19
#include " llvm/Analysis/BlockFrequencyInfo.h"
20
20
#include " llvm/Analysis/CFG.h"
21
21
#include " llvm/Analysis/CodeMetrics.h"
22
- #include " llvm/Analysis/DomTreeUpdater.h"
23
22
#include " llvm/Analysis/GuardUtils.h"
24
23
#include " llvm/Analysis/LoopAnalysisManager.h"
25
24
#include " llvm/Analysis/LoopInfo.h"
@@ -75,7 +74,6 @@ using namespace llvm::PatternMatch;
75
74
76
75
STATISTIC (NumBranches, " Number of branches unswitched" );
77
76
STATISTIC (NumSwitches, " Number of switches unswitched" );
78
- STATISTIC (NumSelects, " Number of selects turned into branches for unswitching" );
79
77
STATISTIC (NumGuards, " Number of guards turned into branches for unswitching" );
80
78
STATISTIC (NumTrivial, " Number of unswitches that are trivial" );
81
79
STATISTIC (
@@ -2644,61 +2642,6 @@ static InstructionCost computeDomSubtreeCost(
2644
2642
return Cost;
2645
2643
}
2646
2644
2647
- // / Turns a select instruction into implicit control flow branch,
2648
- // / making the following replacement:
2649
- // /
2650
- // / head:
2651
- // / --code before select--
2652
- // / select %cond, %trueval, %falseval
2653
- // / --code after select--
2654
- // /
2655
- // / into
2656
- // /
2657
- // / head:
2658
- // / --code before select--
2659
- // / br i1 %cond, label %then, label %tail
2660
- // /
2661
- // / then:
2662
- // / br %tail
2663
- // /
2664
- // / tail:
2665
- // / phi [ %trueval, %then ], [ %falseval, %head]
2666
- // / unreachable
2667
- // /
2668
- // / It also makes all relevant DT and LI updates, so that all structures are in
2669
- // / valid state after this transform.
2670
- static BranchInst *turnSelectIntoBranch (SelectInst *SI, DominatorTree &DT,
2671
- LoopInfo &LI, MemorySSAUpdater *MSSAU,
2672
- AssumptionCache *AC) {
2673
- LLVM_DEBUG (dbgs () << " Turning " << *SI << " into a branch.\n " );
2674
- BasicBlock *HeadBB = SI->getParent ();
2675
-
2676
- Value *Cond = SI->getCondition ();
2677
- if (!isGuaranteedNotToBeUndefOrPoison (Cond, AC, SI, &DT))
2678
- Cond = new FreezeInst (Cond, Cond->getName () + " .fr" , SI);
2679
- DomTreeUpdater DTU =
2680
- DomTreeUpdater (DT, DomTreeUpdater::UpdateStrategy::Eager);
2681
- SplitBlockAndInsertIfThen (SI->getCondition (), SI, false ,
2682
- SI->getMetadata (LLVMContext::MD_prof), &DTU, &LI);
2683
- auto *CondBr = cast<BranchInst>(HeadBB->getTerminator ());
2684
- BasicBlock *ThenBB = CondBr->getSuccessor (0 ),
2685
- *TailBB = CondBr->getSuccessor (1 );
2686
- if (MSSAU)
2687
- MSSAU->moveAllAfterSpliceBlocks (HeadBB, TailBB, SI);
2688
-
2689
- PHINode *Phi = PHINode::Create (SI->getType (), 2 , " unswitched.select" , SI);
2690
- Phi->addIncoming (SI->getTrueValue (), ThenBB);
2691
- Phi->addIncoming (SI->getFalseValue (), HeadBB);
2692
- SI->replaceAllUsesWith (Phi);
2693
- SI->eraseFromParent ();
2694
-
2695
- if (MSSAU && VerifyMemorySSA)
2696
- MSSAU->getMemorySSA ()->verifyMemorySSA ();
2697
-
2698
- ++NumSelects;
2699
- return CondBr;
2700
- }
2701
-
2702
2645
// / Turns a llvm.experimental.guard intrinsic into implicit control flow branch,
2703
2646
// / making the following replacement:
2704
2647
// /
@@ -2806,10 +2749,9 @@ static int CalculateUnswitchCostMultiplier(
2806
2749
const BasicBlock *CondBlock = TI.getParent ();
2807
2750
if (DT.dominates (CondBlock, Latch) &&
2808
2751
(isGuard (&TI) ||
2809
- (TI.isTerminator () &&
2810
- llvm::count_if (successors (&TI), [&L](const BasicBlock *SuccBB) {
2811
- return L.contains (SuccBB);
2812
- }) <= 1 ))) {
2752
+ llvm::count_if (successors (&TI), [&L](const BasicBlock *SuccBB) {
2753
+ return L.contains (SuccBB);
2754
+ }) <= 1 )) {
2813
2755
NumCostMultiplierSkipped++;
2814
2756
return 1 ;
2815
2757
}
@@ -2818,17 +2760,12 @@ static int CalculateUnswitchCostMultiplier(
2818
2760
int SiblingsCount = (ParentL ? ParentL->getSubLoopsVector ().size ()
2819
2761
: std::distance (LI.begin (), LI.end ()));
2820
2762
// Count amount of clones that all the candidates might cause during
2821
- // unswitching. Branch/guard/select counts as 1, switch counts as log2 of its
2822
- // cases.
2763
+ // unswitching. Branch/guard counts as 1, switch counts as log2 of its cases.
2823
2764
int UnswitchedClones = 0 ;
2824
2765
for (const auto &Candidate : UnswitchCandidates) {
2825
2766
const Instruction *CI = Candidate.TI ;
2826
2767
const BasicBlock *CondBlock = CI->getParent ();
2827
2768
bool SkipExitingSuccessors = DT.dominates (CondBlock, Latch);
2828
- if (isa<SelectInst>(CI)) {
2829
- UnswitchedClones++;
2830
- continue ;
2831
- }
2832
2769
if (isGuard (CI)) {
2833
2770
if (!SkipExitingSuccessors)
2834
2771
UnswitchedClones++;
@@ -2891,20 +2828,15 @@ static bool collectUnswitchCandidates(
2891
2828
if (LI.getLoopFor (BB) != &L)
2892
2829
continue ;
2893
2830
2894
- for (auto &I : *BB) {
2895
- if (auto *SI = dyn_cast<SelectInst>(&I)) {
2896
- auto *Cond = SI->getCondition ();
2897
- // restrict to simple boolean selects
2898
- if (!isa<Constant>(Cond) && L.isLoopInvariant (Cond) && Cond->getType ()->isIntegerTy (1 ))
2899
- UnswitchCandidates.push_back ({&I, {Cond}});
2900
- } else if (CollectGuards && isGuard (&I)) {
2901
- auto *Cond =
2902
- skipTrivialSelect (cast<IntrinsicInst>(&I)->getArgOperand (0 ));
2903
- // TODO: Support AND, OR conditions and partial unswitching.
2904
- if (!isa<Constant>(Cond) && L.isLoopInvariant (Cond))
2905
- UnswitchCandidates.push_back ({&I, {Cond}});
2906
- }
2907
- }
2831
+ if (CollectGuards)
2832
+ for (auto &I : *BB)
2833
+ if (isGuard (&I)) {
2834
+ auto *Cond =
2835
+ skipTrivialSelect (cast<IntrinsicInst>(&I)->getArgOperand (0 ));
2836
+ // TODO: Support AND, OR conditions and partial unswitching.
2837
+ if (!isa<Constant>(Cond) && L.isLoopInvariant (Cond))
2838
+ UnswitchCandidates.push_back ({&I, {Cond}});
2839
+ }
2908
2840
2909
2841
if (auto *SI = dyn_cast<SwitchInst>(BB->getTerminator ())) {
2910
2842
// We can only consider fully loop-invariant switch conditions as we need
@@ -3406,8 +3338,7 @@ static NonTrivialUnswitchCandidate findBestNonTrivialUnswitchCandidate(
3406
3338
// loop. This is computing the new cost of unswitching a condition.
3407
3339
// Note that guards always have 2 unique successors that are implicit and
3408
3340
// will be materialized if we decide to unswitch it.
3409
- int SuccessorsCount =
3410
- isGuard (&TI) || isa<SelectInst>(TI) ? 2 : Visited.size ();
3341
+ int SuccessorsCount = isGuard (&TI) ? 2 : Visited.size ();
3411
3342
assert (SuccessorsCount > 1 &&
3412
3343
" Cannot unswitch a condition without multiple distinct successors!" );
3413
3344
return (LoopCost - Cost) * (SuccessorsCount - 1 );
@@ -3494,9 +3425,7 @@ static bool unswitchBestCondition(
3494
3425
PartialIVInfo.InstToDuplicate .clear ();
3495
3426
3496
3427
// If the best candidate is a guard, turn it into a branch.
3497
- if (auto *SI = dyn_cast<SelectInst>(Best.TI ))
3498
- Best.TI = turnSelectIntoBranch (SI, DT, LI, MSSAU, &AC);
3499
- else if (isGuard (Best.TI ))
3428
+ if (isGuard (Best.TI ))
3500
3429
Best.TI =
3501
3430
turnGuardIntoBranch (cast<IntrinsicInst>(Best.TI ), L, DT, LI, MSSAU);
3502
3431
0 commit comments