LLVM 24.0.0git
LoongArchLateBranchOpt.cpp
Go to the documentation of this file.
1//===---- LoongArchLateBranchOpt.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 LoongArch 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 "LoongArch.h"
16#include "LoongArchInstrInfo.h"
17#include "LoongArchSubtarget.h"
19#include "llvm/IR/Analysis.h"
20
21using namespace llvm;
22
23#define LOONGARCH_LATE_BRANCH_OPT_NAME "LoongArch Late Branch Optimisation Pass"
24
25namespace {
26
27struct LoongArchLateBranchOptLegacy : public MachineFunctionPass {
28 static char ID;
29
30 LoongArchLateBranchOptLegacy() : MachineFunctionPass(ID) {}
31
32 StringRef getPassName() const override {
34 }
35
36 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 }
39
40 bool runOnMachineFunction(MachineFunction &MF) override;
41};
42} // namespace
43
44char LoongArchLateBranchOptLegacy::ID = 0;
45INITIALIZE_PASS(LoongArchLateBranchOptLegacy, "loongarch-late-branch-opt",
47
52 if (TII->analyzeBranch(MBB, TBB, FBB, Cond, /*AllowModify=*/false))
53 return false;
54
55 if (!TBB || Cond.size() < 1)
56 return false;
57
58 // Try and convert a conditional branch that can be evaluated statically
59 // into an unconditional branch.
60 unsigned Opc = Cond[0].getImm();
63 case LoongArch::BEQZ:
64 case LoongArch::BNEZ:
65 if (Cond.size() < 2 || !Cond[1].isReg() ||
66 Cond[1].getReg() != LoongArch::R0)
67 return false;
68 Folded = (Opc == LoongArch::BEQZ) ? TBB : FBB;
69 break;
70 case LoongArch::BEQ:
71 case LoongArch::BNE:
72 if (Cond.size() < 3 || !Cond[1].isReg() || !Cond[2].isReg() ||
73 Cond[1].getReg() != Cond[2].getReg())
74 return false;
75 Folded = (Opc == LoongArch::BEQ) ? TBB : FBB;
76 break;
77 default:
78 return false;
79 }
80
81 // At this point, its legal to optimize.
82 TII->removeBranch(MBB);
83
84 // Only need to insert a branch if we're not falling through.
85 if (Folded) {
86 DebugLoc DL = MBB.findBranchDebugLoc();
87 TII->insertBranch(MBB, Folded, nullptr, {}, DL);
88 }
89
90 // Update the successors. Remove them all and add back the correct one.
91 while (!MBB.succ_empty())
92 MBB.removeSuccessor(MBB.succ_end() - 1);
93
94 // If it's a fallthrough, we need to figure out where MBB is going.
96 MachineFunction::iterator Fallthrough = ++MBB.getIterator();
97 if (Fallthrough != MBB.getParent()->end())
98 MBB.addSuccessor(&*Fallthrough);
99 } else
100 MBB.addSuccessor(Folded);
101
102 return true;
103}
104
106 auto &ST = MF.getSubtarget<LoongArchSubtarget>();
107 const LoongArchInstrInfo *TII = ST.getInstrInfo();
108
109 bool Changed = false;
110 for (MachineBasicBlock &MBB : MF)
112 return Changed;
113}
114
115bool LoongArchLateBranchOptLegacy::runOnMachineFunction(MachineFunction &MF) {
116 if (skipFunction(MF.getFunction()))
117 return false;
118 return runLateBranchOpt(MF);
119}
120
122 return new LoongArchLateBranchOptLegacy();
123}
124
aarch64 promote const
const AArch64InstrInfo & TII
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool runLateBranchOpt(MachineFunction &MF)
MachineBasicBlock * Folded
SmallVector< MachineOperand, 4 > Cond
#define LOONGARCH_LATE_BRANCH_OPT_NAME
unsigned Opc
static bool runOnBasicBlock(MachineBasicBlock *MBB, unsigned BasicBlockNum, VRegRenamer &Renamer)
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
Represent the analysis usage information of a pass.
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createLoongArchLateBranchOptLegacyPass()