Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 06a22af

Browse files
author
Krzysztof Parzyszek
committed
[Hexagon] Pick the right branch opcode depending on branch probabilities
Specifically, pick the opcode with the correct branch prediction, i.e. jump:t or jump:nt. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@296821 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2f35f8a commit 06a22af

File tree

2 files changed

+113
-15
lines changed

2 files changed

+113
-15
lines changed

lib/Target/Hexagon/HexagonInstrInfo.cpp

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,20 +3470,75 @@ int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
34703470
int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
34713471
const MachineBranchProbabilityInfo *MBPI) const {
34723472
// We assume that block can have at most two successors.
3473-
bool taken = false;
34743473
const MachineBasicBlock *Src = MI.getParent();
34753474
const MachineOperand &BrTarget = MI.getOperand(1);
3476-
const MachineBasicBlock *Dst = BrTarget.getMBB();
3475+
bool Taken = false;
3476+
const BranchProbability OneHalf(1, 2);
34773477

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.
34813536

34823537
switch (MI.getOpcode()) {
34833538
case Hexagon::J2_jumpt:
3484-
return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3539+
return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
34853540
case Hexagon::J2_jumpf:
3486-
return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3541+
return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
34873542

34883543
default:
34893544
llvm_unreachable("Unexpected jump instruction.");
@@ -3493,20 +3548,19 @@ int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
34933548
// Return .new predicate version for an instruction.
34943549
int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
34953550
const MachineBranchProbabilityInfo *MBPI) const {
3496-
int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3497-
if (NewOpcode >= 0) // Valid predicate new instruction
3498-
return NewOpcode;
3499-
35003551
switch (MI.getOpcode()) {
35013552
// Condtional Jumps
35023553
case Hexagon::J2_jumpt:
35033554
case Hexagon::J2_jumpf:
35043555
return getDotNewPredJumpOp(MI, MBPI);
3505-
3506-
default:
3507-
assert(0 && "Unknown .new type");
35083556
}
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);
35103564
}
35113565

35123566
int HexagonInstrInfo::getDotOldOp(const int opc) const {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; RUN: llc -march=hexagon -disable-block-placement < %s | FileCheck %s
2+
3+
; Check that the branch to the block b10 is marked as taken (i.e. ":t").
4+
; CHECK-LABEL: foo
5+
; CHECK: if ({{.*}}) jump:t .LBB0_[[LAB:[0-9]+]]
6+
; CHECK: [[LAB]]:
7+
; CHECK: add({{.*}},#65)
8+
9+
target triple = "hexagon"
10+
11+
define i32 @foo(i32 %a0) local_unnamed_addr #0 {
12+
b1:
13+
%v2 = icmp eq i32 %a0, 0
14+
br i1 %v2, label %b3, label %b10, !prof !0
15+
16+
b3: ; preds = %b1
17+
br label %b4
18+
19+
b4: ; preds = %b4, %b3
20+
%v5 = phi i32 [ %v6, %b4 ], [ 0, %b3 ]
21+
%v6 = add nuw nsw i32 %v5, 1
22+
%v7 = mul nuw nsw i32 %v5, 67
23+
%v8 = tail call i32 @bar(i32 %v7) #0
24+
%v9 = icmp eq i32 %v6, 10
25+
br i1 %v9, label %b13, label %b4
26+
27+
b10: ; preds = %b1
28+
%v11 = add nsw i32 %a0, 65
29+
%v12 = tail call i32 @bar(i32 %v11) #0
30+
br label %b14
31+
32+
b13: ; preds = %b4
33+
br label %b14
34+
35+
b14: ; preds = %b13, %b10
36+
%v15 = phi i32 [ %v12, %b10 ], [ 0, %b13 ]
37+
ret i32 %v15
38+
}
39+
40+
declare i32 @bar(i32) local_unnamed_addr #0
41+
42+
attributes #0 = { nounwind "target-cpu"="hexagonv60" "target-features"="+hvx,-hvx-double,-long-calls" }
43+
44+
!0 = !{!"branch_weights", i32 1, i32 2000}

0 commit comments

Comments
 (0)