@@ -460,12 +460,12 @@ static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
460
460
// some input conditions to the branch.
461
461
bool FullUnswitch = false ;
462
462
463
- if (L.isLoopInvariant (BI.getCondition ())) {
464
- Invariants.push_back (BI.getCondition ());
463
+ Value *Cond = skipTrivialSelect (BI.getCondition ());
464
+ if (L.isLoopInvariant (Cond)) {
465
+ Invariants.push_back (Cond);
465
466
FullUnswitch = true ;
466
467
} else {
467
- if (auto *CondInst =
468
- dyn_cast<Instruction>(skipTrivialSelect (BI.getCondition ())))
468
+ if (auto *CondInst = dyn_cast<Instruction>(Cond))
469
469
Invariants = collectHomogenousInstGraphLoopInvariants (L, *CondInst, LI);
470
470
if (Invariants.empty ()) {
471
471
LLVM_DEBUG (dbgs () << " Couldn't find invariant inputs!\n " );
@@ -499,7 +499,6 @@ static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
499
499
// is a graph of `or` operations, or the exit block is along the false edge
500
500
// and the condition is a graph of `and` operations.
501
501
if (!FullUnswitch) {
502
- Value *Cond = skipTrivialSelect (BI.getCondition ());
503
502
if (ExitDirection ? !match (Cond, m_LogicalOr ())
504
503
: !match (Cond, m_LogicalAnd ())) {
505
504
LLVM_DEBUG (dbgs () << " Branch condition is in improper form for "
@@ -566,6 +565,7 @@ static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
566
565
// its successors.
567
566
OldPH->getInstList ().splice (OldPH->end (), BI.getParent ()->getInstList (),
568
567
BI);
568
+ BI.setCondition (Cond);
569
569
if (MSSAU) {
570
570
// Temporarily clone the terminator, to make MSSA update cheaper by
571
571
// separating "insert edge" updates from "remove edge" ones.
@@ -1040,7 +1040,8 @@ static bool unswitchAllTrivialConditions(Loop &L, DominatorTree &DT,
1040
1040
// Don't bother trying to unswitch past an unconditional branch or a branch
1041
1041
// with a constant value. These should be removed by simplifycfg prior to
1042
1042
// running this pass.
1043
- if (!BI->isConditional () || isa<Constant>(BI->getCondition ()))
1043
+ if (!BI->isConditional () ||
1044
+ isa<Constant>(skipTrivialSelect (BI->getCondition ())))
1044
1045
return Changed;
1045
1046
1046
1047
// Found a trivial condition candidate: non-foldable conditional branch. If
0 commit comments