@@ -3470,20 +3470,75 @@ int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3470
3470
int HexagonInstrInfo::getDotNewPredJumpOp (const MachineInstr &MI,
3471
3471
const MachineBranchProbabilityInfo *MBPI) const {
3472
3472
// We assume that block can have at most two successors.
3473
- bool taken = false ;
3474
3473
const MachineBasicBlock *Src = MI.getParent ();
3475
3474
const MachineOperand &BrTarget = MI.getOperand (1 );
3476
- const MachineBasicBlock *Dst = BrTarget.getMBB ();
3475
+ bool Taken = false ;
3476
+ const BranchProbability OneHalf (1 , 2 );
3477
3477
3478
- const BranchProbability Prediction = MBPI->getEdgeProbability (Src, Dst);
3479
- if (Prediction >= BranchProbability (1 ,2 ))
3480
- taken = true ;
3478
+ if (BrTarget.isMBB ()) {
3479
+ const MachineBasicBlock *Dst = BrTarget.getMBB ();
3480
+ Taken = MBPI->getEdgeProbability (Src, Dst) >= OneHalf;
3481
+ } else {
3482
+ // The branch target is not a basic block (most likely a function).
3483
+ // Since BPI only gives probabilities for targets that are basic blocks,
3484
+ // try to identify another target of this branch (potentially a fall-
3485
+ // -through) and check the probability of that target.
3486
+ //
3487
+ // The only handled branch combinations are:
3488
+ // - one conditional branch,
3489
+ // - one conditional branch followed by one unconditional branch.
3490
+ // Otherwise, assume not-taken.
3491
+ assert (MI.isConditionalBranch ());
3492
+ const MachineBasicBlock &B = *MI.getParent ();
3493
+ bool SawCond = false , Bad = false ;
3494
+ for (const MachineInstr &I : B) {
3495
+ if (!I.isBranch ())
3496
+ continue ;
3497
+ if (I.isConditionalBranch ()) {
3498
+ SawCond = true ;
3499
+ if (&I != &MI) {
3500
+ Bad = true ;
3501
+ break ;
3502
+ }
3503
+ }
3504
+ if (I.isUnconditionalBranch () && !SawCond) {
3505
+ Bad = true ;
3506
+ break ;
3507
+ }
3508
+ }
3509
+ if (!Bad) {
3510
+ MachineBasicBlock::const_instr_iterator It (MI);
3511
+ MachineBasicBlock::const_instr_iterator NextIt = std::next (It);
3512
+ if (NextIt == B.instr_end ()) {
3513
+ // If this branch is the last, look for the fall-through block.
3514
+ for (const MachineBasicBlock *SB : B.successors ()) {
3515
+ if (!B.isLayoutSuccessor (SB))
3516
+ continue ;
3517
+ Taken = MBPI->getEdgeProbability (Src, SB) < OneHalf;
3518
+ break ;
3519
+ }
3520
+ } else {
3521
+ assert (NextIt->isUnconditionalBranch ());
3522
+ // Find the first MBB operand and assume it's the target.
3523
+ const MachineBasicBlock *BT = nullptr ;
3524
+ for (const MachineOperand &Op : NextIt->operands ()) {
3525
+ if (!Op.isMBB ())
3526
+ continue ;
3527
+ BT = Op.getMBB ();
3528
+ break ;
3529
+ }
3530
+ Taken = BT && MBPI->getEdgeProbability (Src, BT) < OneHalf;
3531
+ }
3532
+ } // if (!Bad)
3533
+ }
3534
+
3535
+ // The Taken flag should be set to something reasonable by this point.
3481
3536
3482
3537
switch (MI.getOpcode ()) {
3483
3538
case Hexagon::J2_jumpt:
3484
- return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3539
+ return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3485
3540
case Hexagon::J2_jumpf:
3486
- return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3541
+ return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3487
3542
3488
3543
default :
3489
3544
llvm_unreachable (" Unexpected jump instruction." );
@@ -3493,20 +3548,19 @@ int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
3493
3548
// Return .new predicate version for an instruction.
3494
3549
int HexagonInstrInfo::getDotNewPredOp (const MachineInstr &MI,
3495
3550
const MachineBranchProbabilityInfo *MBPI) const {
3496
- int NewOpcode = Hexagon::getPredNewOpcode (MI.getOpcode ());
3497
- if (NewOpcode >= 0 ) // Valid predicate new instruction
3498
- return NewOpcode;
3499
-
3500
3551
switch (MI.getOpcode ()) {
3501
3552
// Condtional Jumps
3502
3553
case Hexagon::J2_jumpt:
3503
3554
case Hexagon::J2_jumpf:
3504
3555
return getDotNewPredJumpOp (MI, MBPI);
3505
-
3506
- default :
3507
- assert (0 && " Unknown .new type" );
3508
3556
}
3509
- return 0 ;
3557
+
3558
+ int NewOpcode = Hexagon::getPredNewOpcode (MI.getOpcode ());
3559
+ if (NewOpcode >= 0 )
3560
+ return NewOpcode;
3561
+
3562
+ dbgs () << " Cannot convert to .new: " << getName (MI.getOpcode ()) << ' \n ' ;
3563
+ llvm_unreachable (nullptr );
3510
3564
}
3511
3565
3512
3566
int HexagonInstrInfo::getDotOldOp (const int opc) const {
0 commit comments