|
| 1 | +//===-- RISCVLatePeephole.cpp - Late stage peephole 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 late peephole optimizations |
| 10 | +/// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "MCTargetDesc/RISCVMCTargetDesc.h" |
| 14 | +#include "RISCV.h" |
| 15 | +#include "RISCVInstrInfo.h" |
| 16 | +#include "RISCVSubtarget.h" |
| 17 | +#include "llvm/CodeGen/MachineBasicBlock.h" |
| 18 | +#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
| 19 | +#include "llvm/CodeGen/MachineDominators.h" |
| 20 | +#include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | +#include "llvm/CodeGen/Passes.h" |
| 22 | +#include "llvm/CodeGen/RegisterScavenging.h" |
| 23 | +#include "llvm/MC/TargetRegistry.h" |
| 24 | +#include "llvm/Support/Debug.h" |
| 25 | + |
| 26 | +using namespace llvm; |
| 27 | + |
| 28 | +#define DEBUG_TYPE "riscv-late-peephole" |
| 29 | +#define RISCV_LATE_PEEPHOLE_NAME "RISC-V Late Stage Peephole" |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +struct RISCVLatePeepholeOpt : public MachineFunctionPass { |
| 34 | + static char ID; |
| 35 | + |
| 36 | + RISCVLatePeepholeOpt() : MachineFunctionPass(ID) {} |
| 37 | + |
| 38 | + StringRef getPassName() const override { return RISCV_LATE_PEEPHOLE_NAME; } |
| 39 | + |
| 40 | + void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 41 | + MachineFunctionPass::getAnalysisUsage(AU); |
| 42 | + } |
| 43 | + |
| 44 | + bool runOnMachineFunction(MachineFunction &Fn) override; |
| 45 | + |
| 46 | +private: |
| 47 | + bool optimizeBlock(MachineBasicBlock &MBB); |
| 48 | + |
| 49 | + const RISCVInstrInfo *TII = nullptr; |
| 50 | +}; |
| 51 | +} // namespace |
| 52 | + |
| 53 | +char RISCVLatePeepholeOpt::ID = 0; |
| 54 | +INITIALIZE_PASS(RISCVLatePeepholeOpt, "riscv-late-peephole", |
| 55 | + RISCV_LATE_PEEPHOLE_NAME, false, false) |
| 56 | + |
| 57 | +bool RISCVLatePeepholeOpt::optimizeBlock(MachineBasicBlock &MBB) { |
| 58 | + |
| 59 | + // Use trySimplifyCondBr directly to know whether the optimization occured. |
| 60 | + MachineBasicBlock *TBB, *FBB; |
| 61 | + SmallVector<MachineOperand, 4> Cond; |
| 62 | + if (!TII->analyzeBranch(MBB, TBB, FBB, Cond, false)) |
| 63 | + return TII->trySimplifyCondBr(MBB, TBB, FBB, Cond); |
| 64 | + |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +bool RISCVLatePeepholeOpt::runOnMachineFunction(MachineFunction &MF) { |
| 69 | + if (skipFunction(MF.getFunction())) |
| 70 | + return false; |
| 71 | + |
| 72 | + TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo(); |
| 73 | + |
| 74 | + bool MadeChange = false; |
| 75 | + |
| 76 | + for (MachineBasicBlock &MBB : MF) |
| 77 | + MadeChange |= optimizeBlock(MBB); |
| 78 | + |
| 79 | + return MadeChange; |
| 80 | +} |
| 81 | + |
| 82 | +/// Returns an instance of the Make Compressible Optimization pass. |
| 83 | +FunctionPass *llvm::createRISCVLatePeepholeOptPass() { |
| 84 | + return new RISCVLatePeepholeOpt(); |
| 85 | +} |
0 commit comments