-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[RISCV] Add late optimization pass for RISC-V to optimize branch instructions #131728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4511b8c
33091e8
1d0f096
e064268
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===-- RISCVLatePeephole.cpp - Late stage peephole optimization ----------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// This file provides RISC-V late peephole optimizations | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "MCTargetDesc/RISCVMCTargetDesc.h" | ||
#include "RISCV.h" | ||
#include "RISCVInstrInfo.h" | ||
#include "RISCVSubtarget.h" | ||
#include "llvm/CodeGen/MachineBasicBlock.h" | ||
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" | ||
#include "llvm/CodeGen/MachineDominators.h" | ||
#include "llvm/CodeGen/MachineInstrBuilder.h" | ||
#include "llvm/CodeGen/Passes.h" | ||
#include "llvm/CodeGen/RegisterScavenging.h" | ||
#include "llvm/MC/TargetRegistry.h" | ||
#include "llvm/Support/Debug.h" | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "riscv-late-peephole" | ||
#define RISCV_LATE_PEEPHOLE_NAME "RISC-V Late Stage Peephole" | ||
|
||
namespace { | ||
|
||
struct RISCVLatePeepholeOpt : public MachineFunctionPass { | ||
static char ID; | ||
|
||
RISCVLatePeepholeOpt() : MachineFunctionPass(ID) {} | ||
|
||
StringRef getPassName() const override { return RISCV_LATE_PEEPHOLE_NAME; } | ||
|
||
void getAnalysisUsage(AnalysisUsage &AU) const override { | ||
MachineFunctionPass::getAnalysisUsage(AU); | ||
} | ||
|
||
bool runOnMachineFunction(MachineFunction &Fn) override; | ||
|
||
private: | ||
bool optimizeBlock(MachineBasicBlock &MBB); | ||
|
||
const RISCVInstrInfo *TII = nullptr; | ||
}; | ||
} // namespace | ||
|
||
char RISCVLatePeepholeOpt::ID = 0; | ||
INITIALIZE_PASS(RISCVLatePeepholeOpt, "riscv-late-peephole", | ||
RISCV_LATE_PEEPHOLE_NAME, false, false) | ||
|
||
bool RISCVLatePeepholeOpt::optimizeBlock(MachineBasicBlock &MBB) { | ||
|
||
// Use trySimplifyCondBr directly to know whether the optimization occured. | ||
MachineBasicBlock *TBB, *FBB; | ||
SmallVector<MachineOperand, 4> Cond; | ||
if (!TII->analyzeBranch(MBB, TBB, FBB, Cond, false)) | ||
return TII->trySimplifyCondBr(MBB, TBB, FBB, Cond); | ||
|
||
return false; | ||
} | ||
|
||
bool RISCVLatePeepholeOpt::runOnMachineFunction(MachineFunction &MF) { | ||
if (skipFunction(MF.getFunction())) | ||
return false; | ||
|
||
TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo(); | ||
|
||
bool MadeChange = false; | ||
|
||
for (MachineBasicBlock &MBB : MF) | ||
MadeChange |= optimizeBlock(MBB); | ||
|
||
return MadeChange; | ||
} | ||
|
||
/// Returns an instance of the Make Compressible Optimization pass. | ||
FunctionPass *llvm::createRISCVLatePeepholeOptPass() { | ||
return new RISCVLatePeepholeOpt(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() { | |
initializeKCFIPass(*PR); | ||
initializeRISCVDeadRegisterDefinitionsPass(*PR); | ||
initializeRISCVMakeCompressibleOptPass(*PR); | ||
initializeRISCVLatePeepholeOptPass(*PR); | ||
initializeRISCVGatherScatterLoweringPass(*PR); | ||
initializeRISCVCodeGenPreparePass(*PR); | ||
initializeRISCVPostRAExpandPseudoPass(*PR); | ||
|
@@ -567,6 +568,7 @@ void RISCVPassConfig::addPreEmitPass() { | |
addPass(createMachineCopyPropagationPass(true)); | ||
addPass(&BranchRelaxationPassID); | ||
addPass(createRISCVMakeCompressibleOptPass()); | ||
addPass(createRISCVLatePeepholeOptPass()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should mess with branches after BranchRelaxationPass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm checking my notes here and the pattern that generates the branch, i.e.:
Seems to be formed by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put it immediately before branch relaxation, unless you're also sure that machine copy propagation won't also introduce these kinds of branches. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lenary, MCP is definitely introducing these kinds of branches. |
||
} | ||
|
||
void RISCVPassConfig::addPreEmitPass2() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
occurred*