-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[RISCV] Add late optimization pass for riscv #133256
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4a64565
[RISCV] Canonicalize foldable branch conditions in optimizeCondBranch
preames 40439cd
clang-format
preames 25c88ba
[RISCV] Add late optimization pass for riscv
mikhailramalho 8aac8e9
Fix pipeline tests
mikhailramalho 3d28d5f
Updated description
mikhailramalho 36d9adc
Removed messy code
mikhailramalho 08ddd9e
Updated tests
mikhailramalho df34ba6
Make evaluateCondBranch, isLoadImm, and isFromLoadImm static member f…
mikhailramalho 81dffc3
Merge remote-tracking branch 'origin/main' into riscv-late-opt1
mikhailramalho e53bb1b
Cleanup includes
mikhailramalho b9303d3
Moved declaration of Folded
mikhailramalho 51d4ef3
Added documentation to new static functions
mikhailramalho e353109
Make isLoadImm private
mikhailramalho fbec07f
No need to update TBB and FBB
mikhailramalho bbda8ff
Don't run the new pass on -O0
mikhailramalho 24e7a91
Merge remote-tracking branch 'origin/main' into riscv-late-opt1
mikhailramalho eedbddd
Updated test
mikhailramalho f710dbe
No need to clear cond
mikhailramalho cad5541
clang-format
mikhailramalho acecac2
Moved isLoadImm out of RISCVInstrInfo
mikhailramalho d9020ff
Removed wrong comment
mikhailramalho 66be46c
Sort list of files
mikhailramalho 81f8b1e
Rename trySimplifyCondBr and return early when possible
mikhailramalho ef5b386
Renamed pass
mikhailramalho 953a78d
clang-format
mikhailramalho 2505795
Update header
mikhailramalho 0230f5d
Merge remote-tracking branch 'origin/main' into riscv-late-opt1
mikhailramalho 46aec7c
Rename file
mikhailramalho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//===-- RISCVLateOpt.cpp - Late stage 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 specific target optimizations, currently it's | ||
/// limited to convert conditional branches into unconditional branches when | ||
/// the condition can be statically evaluated. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "RISCVInstrInfo.h" | ||
#include "RISCVSubtarget.h" | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "riscv-late-opt" | ||
#define RISCV_LATE_OPT_NAME "RISC-V Late Stage Optimizations" | ||
|
||
namespace { | ||
|
||
struct RISCVLateOpt : public MachineFunctionPass { | ||
static char ID; | ||
|
||
RISCVLateOpt() : MachineFunctionPass(ID) {} | ||
|
||
StringRef getPassName() const override { return RISCV_LATE_OPT_NAME; } | ||
|
||
void getAnalysisUsage(AnalysisUsage &AU) const override { | ||
MachineFunctionPass::getAnalysisUsage(AU); | ||
} | ||
|
||
bool runOnMachineFunction(MachineFunction &Fn) override; | ||
|
||
private: | ||
bool trySimplifyCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB, | ||
MachineBasicBlock *FBB, | ||
SmallVectorImpl<MachineOperand> &Cond) const; | ||
|
||
const RISCVInstrInfo *RII = nullptr; | ||
}; | ||
} // namespace | ||
|
||
char RISCVLateOpt::ID = 0; | ||
INITIALIZE_PASS(RISCVLateOpt, "riscv-late-opt", RISCV_LATE_OPT_NAME, false, | ||
false) | ||
|
||
bool RISCVLateOpt::trySimplifyCondBr( | ||
MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, | ||
SmallVectorImpl<MachineOperand> &Cond) const { | ||
|
||
if (!TBB || Cond.size() != 3) | ||
return false; | ||
|
||
RISCVCC::CondCode CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm()); | ||
assert(CC != RISCVCC::COND_INVALID); | ||
|
||
// Try and convert a conditional branch that can be evaluated statically | ||
// into an unconditional branch. | ||
MachineBasicBlock *Folded = nullptr; | ||
mikhailramalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int64_t C0, C1; | ||
|
||
MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); | ||
if (RISCVInstrInfo::isFromLoadImm(MRI, Cond[1], C0) && | ||
RISCVInstrInfo::isFromLoadImm(MRI, Cond[2], C1)) { | ||
mikhailramalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Folded = RISCVInstrInfo::evaluateCondBranch(CC, C0, C1) ? TBB : FBB; | ||
|
||
// At this point, its legal to optimize. | ||
RII->removeBranch(MBB); | ||
Cond.clear(); | ||
mikhailramalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Only need to insert a branch if we're not falling through. | ||
if (Folded) { | ||
DebugLoc DL = MBB.findBranchDebugLoc(); | ||
RII->insertBranch(MBB, Folded, nullptr, {}, DL); | ||
} | ||
|
||
// Update the successors. Remove them all and add back the correct one. | ||
while (!MBB.succ_empty()) | ||
MBB.removeSuccessor(MBB.succ_end() - 1); | ||
|
||
// If it's a fallthrough, we need to figure out where MBB is going. | ||
if (!Folded) { | ||
MachineFunction::iterator Fallthrough = ++MBB.getIterator(); | ||
if (Fallthrough != MBB.getParent()->end()) | ||
MBB.addSuccessor(&*Fallthrough); | ||
} else | ||
MBB.addSuccessor(Folded); | ||
|
||
TBB = Folded; | ||
mikhailramalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FBB = nullptr; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool RISCVLateOpt::runOnMachineFunction(MachineFunction &Fn) { | ||
if (skipFunction(Fn.getFunction())) | ||
return false; | ||
|
||
auto &ST = Fn.getSubtarget<RISCVSubtarget>(); | ||
RII = ST.getInstrInfo(); | ||
|
||
bool Changed = false; | ||
|
||
for (MachineBasicBlock &MBB : Fn) { | ||
MachineBasicBlock *TBB, *FBB; | ||
SmallVector<MachineOperand, 4> Cond; | ||
if (!RII->analyzeBranch(MBB, TBB, FBB, Cond, /*AllowModify=*/false)) | ||
Changed |= trySimplifyCondBr(MBB, TBB, FBB, Cond); | ||
mikhailramalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return Changed; | ||
} | ||
|
||
/// Returns an instance of the Make Compressible Optimization pass. | ||
michaelmaitland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FunctionPass *llvm::createRISCVLateOptPass() { return new RISCVLateOpt(); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.