1//===-- RISCVLateBranchOpt.cpp - Late Stage Branch Optimization -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// This file provides RISC-V specific target optimizations, currently it's
10/// limited to convert conditional branches into unconditional branches when
11/// the condition can be statically evaluated.
12///
13//===----------------------------------------------------------------------===//
14
15#include "RISCV.h"
16#include "RISCVInstrInfo.h"
17#include "RISCVSubtarget.h"
18#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
19#include "llvm/CodeGen/MachinePassManager.h"
20#include "llvm/IR/Analysis.h"
21
22using namespace llvm;
23
24#define RISCV_LATE_BRANCH_OPT_NAME "RISC-V Late Branch Optimisation Pass"
25
26namespace {
27
28struct RISCVLateBranchOptLegacy : public MachineFunctionPass {
29 static char ID;
30
31 RISCVLateBranchOptLegacy() : MachineFunctionPass(ID) {}
32
33 StringRef getPassName() const override { return RISCV_LATE_BRANCH_OPT_NAME; }
34
35 void getAnalysisUsage(AnalysisUsage &AU) const override {
36 MachineFunctionPass::getAnalysisUsage(AU);
37 }
38
39 bool runOnMachineFunction(MachineFunction &Fn) override;
40};
41} // namespace
42
43char RISCVLateBranchOptLegacy::ID = 0;
44INITIALIZE_PASS(RISCVLateBranchOptLegacy, "riscv-late-branch-opt",
45 RISCV_LATE_BRANCH_OPT_NAME, false, false)
46
47static bool runLateBranchOptOnMBB(MachineBasicBlock &MBB,
48 const RISCVInstrInfo *RII) {
49 MachineBasicBlock *TBB, *FBB;
50 SmallVector<MachineOperand, 4> Cond;
51 if (RII->analyzeBranch(MBB, TBB, FBB, Cond, /*AllowModify=*/false))
52 return false;
53
54 if (!TBB || Cond.size() != 3)
55 return false;
56
57 RISCVCC::CondCode CC = RISCVInstrInfo::getCondFromBranchOpc(Opc: Cond[0].getImm());
58 assert(CC != RISCVCC::COND_INVALID);
59
60 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
61
62 // Try and convert a conditional branch that can be evaluated statically
63 // into an unconditional branch.
64 int64_t C0, C1;
65 if (!RISCVInstrInfo::isFromLoadImm(MRI, Op: Cond[1], Imm&: C0) ||
66 !RISCVInstrInfo::isFromLoadImm(MRI, Op: Cond[2], Imm&: C1))
67 return false;
68
69 MachineBasicBlock *Folded =
70 RISCVInstrInfo::evaluateCondBranch(CC, C0, C1) ? TBB : FBB;
71
72 // At this point, its legal to optimize.
73 RII->removeBranch(MBB);
74
75 // Only need to insert a branch if we're not falling through.
76 if (Folded) {
77 DebugLoc DL = MBB.findBranchDebugLoc();
78 RII->insertBranch(MBB, TBB: Folded, FBB: nullptr, Cond: {}, dl: DL);
79 }
80
81 // Update the successors. Remove them all and add back the correct one.
82 while (!MBB.succ_empty())
83 MBB.removeSuccessor(I: MBB.succ_end() - 1);
84
85 // If it's a fallthrough, we need to figure out where MBB is going.
86 if (!Folded) {
87 MachineFunction::iterator Fallthrough = ++MBB.getIterator();
88 if (Fallthrough != MBB.getParent()->end())
89 MBB.addSuccessor(Succ: &*Fallthrough);
90 } else
91 MBB.addSuccessor(Succ: Folded);
92
93 return true;
94}
95
96static bool runLateBranchOptOnMF(MachineFunction &Fn) {
97 auto &ST = Fn.getSubtarget<RISCVSubtarget>();
98 const RISCVInstrInfo *RII = ST.getInstrInfo();
99
100 bool Changed = false;
101 for (MachineBasicBlock &MBB : Fn)
102 Changed |= runLateBranchOptOnMBB(MBB, RII);
103 return Changed;
104}
105
106FunctionPass *llvm::createRISCVLateBranchOptLegacyPass() {
107 return new RISCVLateBranchOptLegacy();
108}
109
110bool RISCVLateBranchOptLegacy::runOnMachineFunction(MachineFunction &MF) {
111 if (skipFunction(F: MF.getFunction()))
112 return false;
113 return runLateBranchOptOnMF(Fn&: MF);
114}
115
116PreservedAnalyses
117RISCVLateBranchOptPass::run(MachineFunction &MF,
118 MachineFunctionAnalysisManager &MFAM) {
119 return runLateBranchOptOnMF(Fn&: MF) ? getMachineFunctionPassPreservedAnalyses()
120 : PreservedAnalyses::all();
121}
122