73
73
#include " llvm/Support/raw_ostream.h"
74
74
#include " llvm/Transforms/Utils/BasicBlockUtils.h"
75
75
#include " llvm/Transforms/Utils/Local.h"
76
+ #include " llvm/Transforms/Utils/UnrollLoop.h"
76
77
#include " llvm/Transforms/Utils/ValueMapper.h"
77
78
#include < algorithm>
78
79
#include < cassert>
@@ -3634,6 +3635,59 @@ static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI,
3634
3635
}
3635
3636
}
3636
3637
3638
+ bool hasUnrollHint (Instruction *TI) {
3639
+ MDNode *MD = TI->getMetadata (LLVMContext::MD_loop);
3640
+ if (!MD)
3641
+ return false ;
3642
+
3643
+ return GetUnrollMetadata (MD, " llvm.loop.unroll.enable" ) ||
3644
+ GetUnrollMetadata (MD, " llvm.loop.unroll.full" ) ||
3645
+ GetUnrollMetadata (MD, " llvm.loop.unroll.count" );
3646
+ }
3647
+
3648
+ // Escape folding "I < ConstNum" with "Cond2" when loops with constant
3649
+ // iterations and expected unroll.
3650
+ // #pragma unroll
3651
+ // for (int I = 0; I < ConstNum; ++I) { // ConstNum > 1
3652
+ // if (Cond2) {
3653
+ // break;
3654
+ // }
3655
+ // xxx loop body;
3656
+ // }
3657
+ // Folding these conditional branches may break/affect loop unroll.
3658
+ static bool isConstantLoopWithUnrollHint (BranchInst *PBI) {
3659
+ ICmpInst *ICmp = dyn_cast<ICmpInst>(PBI->getCondition ());
3660
+ if (!ICmp)
3661
+ return false ;
3662
+
3663
+ // Make sure ConstNum > 1
3664
+ bool DoFold = true ;
3665
+ for (unsigned I = 0 ; I < ICmp->getNumOperands (); ++I) {
3666
+ ConstantInt *Op = dyn_cast<ConstantInt>(ICmp->getOperand (I));
3667
+ if (!Op)
3668
+ continue ;
3669
+ if (Op->getSExtValue () > 1 ) {
3670
+ DoFold = false ;
3671
+ break ;
3672
+ }
3673
+ }
3674
+ if (DoFold)
3675
+ return false ;
3676
+
3677
+ // Loop information has not been established yet, so here we easily judge
3678
+ // whether it is a loop by backedge.
3679
+ BasicBlock *PBB = PBI->getParent ();
3680
+ for (Function::iterator I = PBB->getIterator (), E = PBB->getParent ()->end ();
3681
+ I != E; ++I) {
3682
+ BasicBlock *BB = &*I;
3683
+ if (is_contained (predecessors (PBB), BB)) {
3684
+ if (hasUnrollHint (BB->getTerminator ()))
3685
+ return true ;
3686
+ }
3687
+ }
3688
+ return false ;
3689
+ }
3690
+
3637
3691
// / Determine if the two branches share a common destination and deduce a glue
3638
3692
// / that joins the branches' conditions to arrive at the common destination if
3639
3693
// / that would be profitable.
@@ -3645,6 +3699,9 @@ shouldFoldCondBranchesToCommonDestination(BranchInst *BI, BranchInst *PBI,
3645
3699
assert (is_contained (predecessors (BI->getParent ()), PBI->getParent ()) &&
3646
3700
" PredBB must be a predecessor of BB." );
3647
3701
3702
+ if (isConstantLoopWithUnrollHint (PBI))
3703
+ return std::nullopt;
3704
+
3648
3705
// We have the potential to fold the conditions together, but if the
3649
3706
// predecessor branch is predictable, we may not want to merge them.
3650
3707
uint64_t PTWeight, PFWeight;
0 commit comments