Skip to content

Commit

Permalink
[RISCV] Merge SExtWRemoval and StripWSuffix into a single pass.
Browse files Browse the repository at this point in the history
These run together in the pipeline and are the only users of
TII.hasAllWUsers. Merging them will allow us to move hasAllWUsers
back from TII.

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D147173
  • Loading branch information
topperc committed Mar 29, 2023
1 parent a7005d7 commit 4c10a61
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 125 deletions.
3 changes: 1 addition & 2 deletions llvm/lib/Target/RISCV/CMakeLists.txt
Expand Up @@ -34,11 +34,10 @@ add_llvm_target(RISCVCodeGen
RISCVMacroFusion.cpp
RISCVMCInstLower.cpp
RISCVMergeBaseOffset.cpp
RISCVOptWInstrs.cpp
RISCVRedundantCopyElimination.cpp
RISCVRegisterInfo.cpp
RISCVRVVInitUndef.cpp
RISCVSExtWRemoval.cpp
RISCVStripWSuffix.cpp
RISCVSubtarget.cpp
RISCVTargetMachine.cpp
RISCVTargetObjectFile.cpp
Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/Target/RISCV/RISCV.h
Expand Up @@ -47,11 +47,8 @@ void initializeRISCVMakeCompressibleOptPass(PassRegistry &);
FunctionPass *createRISCVGatherScatterLoweringPass();
void initializeRISCVGatherScatterLoweringPass(PassRegistry &);

FunctionPass *createRISCVSExtWRemovalPass();
void initializeRISCVSExtWRemovalPass(PassRegistry &);

FunctionPass *createRISCVStripWSuffixPass();
void initializeRISCVStripWSuffixPass(PassRegistry &);
FunctionPass *createRISCVOptWInstrsPass();
void initializeRISCVOptWInstrsPass(PassRegistry &);

FunctionPass *createRISCVMergeBaseOffsetOptPass();
void initializeRISCVMergeBaseOffsetOptPass(PassRegistry &);
Expand Down
@@ -1,14 +1,21 @@
//===-------------- RISCVSExtWRemoval.cpp - MI sext.w Removal -------------===//
//===- RISCVOptWInstrs.cpp - MI W instruction optimizations ---------------===//
//
// 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 pass removes unneeded sext.w instructions at the MI level. Either
// because the sign extended bits aren't consumed or because the input was
// already sign extended by an earlier instruction.
// This pass does some optimizations for *W instructions at the MI level.
//
// First it removes unneeded sext.w instructions. Either because the sign
// extended bits aren't consumed or because the input was already sign extended
// by an earlier instruction.
//
// Then it removes the -w suffix from each addiw and slliw instructions
// whenever all users are dependent only on the lower word of the result of the
// instruction. We do this only for addiw, slliw, and mulw because the -w forms
// are less compressible.
//
//===---------------------------------------------------------------------===//

Expand All @@ -21,7 +28,8 @@

using namespace llvm;

#define DEBUG_TYPE "riscv-sextw-removal"
#define DEBUG_TYPE "riscv-opt-w-instrs"
#define RISCV_OPT_W_INSTRS_NAME "RISC-V Optimize W Instructions"

STATISTIC(NumRemovedSExtW, "Number of removed sign-extensions");
STATISTIC(NumTransformedToWInstrs,
Expand All @@ -30,34 +38,42 @@ STATISTIC(NumTransformedToWInstrs,
static cl::opt<bool> DisableSExtWRemoval("riscv-disable-sextw-removal",
cl::desc("Disable removal of sext.w"),
cl::init(false), cl::Hidden);
static cl::opt<bool> DisableStripWSuffix("riscv-disable-strip-w-suffix",
cl::desc("Disable strip W suffix"),
cl::init(false), cl::Hidden);

namespace {

class RISCVSExtWRemoval : public MachineFunctionPass {
class RISCVOptWInstrs : public MachineFunctionPass {
public:
static char ID;

RISCVSExtWRemoval() : MachineFunctionPass(ID) {
initializeRISCVSExtWRemovalPass(*PassRegistry::getPassRegistry());
RISCVOptWInstrs() : MachineFunctionPass(ID) {
initializeRISCVOptWInstrsPass(*PassRegistry::getPassRegistry());
}

bool runOnMachineFunction(MachineFunction &MF) override;
bool removeSExtWInstrs(MachineFunction &MF, const RISCVInstrInfo &TII,
MachineRegisterInfo &MRI);
bool stripWSuffixes(MachineFunction &MF, const RISCVInstrInfo &TII,
MachineRegisterInfo &MRI);

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
MachineFunctionPass::getAnalysisUsage(AU);
}

StringRef getPassName() const override { return "RISC-V sext.w Removal"; }
StringRef getPassName() const override { return RISCV_OPT_W_INSTRS_NAME; }
};

} // end anonymous namespace

char RISCVSExtWRemoval::ID = 0;
INITIALIZE_PASS(RISCVSExtWRemoval, DEBUG_TYPE, "RISC-V sext.w Removal", false,
char RISCVOptWInstrs::ID = 0;
INITIALIZE_PASS(RISCVOptWInstrs, DEBUG_TYPE, RISCV_OPT_W_INSTRS_NAME, false,
false)

FunctionPass *llvm::createRISCVSExtWRemovalPass() {
return new RISCVSExtWRemoval();
FunctionPass *llvm::createRISCVOptWInstrsPass() {
return new RISCVOptWInstrs();
}

// This function returns true if the machine instruction always outputs a value
Expand Down Expand Up @@ -317,19 +333,13 @@ static unsigned getWOp(unsigned Opcode) {
}
}

bool RISCVSExtWRemoval::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()) || DisableSExtWRemoval)
return false;

MachineRegisterInfo &MRI = MF.getRegInfo();
const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
const RISCVInstrInfo &TII = *ST.getInstrInfo();

if (!ST.is64Bit())
bool RISCVOptWInstrs::removeSExtWInstrs(MachineFunction &MF,
const RISCVInstrInfo &TII,
MachineRegisterInfo &MRI) {
if (DisableSExtWRemoval)
return false;

bool MadeChange = false;

for (MachineBasicBlock &MBB : MF) {
for (auto I = MBB.begin(), IE = MBB.end(); I != IE;) {
MachineInstr *MI = &*I++;
Expand Down Expand Up @@ -375,3 +385,51 @@ bool RISCVSExtWRemoval::runOnMachineFunction(MachineFunction &MF) {

return MadeChange;
}

bool RISCVOptWInstrs::stripWSuffixes(MachineFunction &MF,
const RISCVInstrInfo &TII,
MachineRegisterInfo &MRI) {
if (DisableStripWSuffix)
return false;

bool MadeChange = false;
for (MachineBasicBlock &MBB : MF) {
for (auto I = MBB.begin(), IE = MBB.end(); I != IE; ++I) {
MachineInstr &MI = *I;

unsigned Opc;
switch (MI.getOpcode()) {
default:
continue;
case RISCV::ADDW: Opc = RISCV::ADD; break;
case RISCV::MULW: Opc = RISCV::MUL; break;
case RISCV::SLLIW: Opc = RISCV::SLLI; break;
}

if (TII.hasAllWUsers(MI, MRI)) {
MI.setDesc(TII.get(Opc));
MadeChange = true;
}
}
}

return MadeChange;
}

bool RISCVOptWInstrs::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;

MachineRegisterInfo &MRI = MF.getRegInfo();
const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
const RISCVInstrInfo &TII = *ST.getInstrInfo();

if (!ST.is64Bit())
return false;

bool MadeChange = false;
MadeChange |= removeSExtWInstrs(MF, TII, MRI);
MadeChange |= stripWSuffixes(MF, TII, MRI);

return MadeChange;
}
89 changes: 0 additions & 89 deletions llvm/lib/Target/RISCV/RISCVStripWSuffix.cpp

This file was deleted.

6 changes: 2 additions & 4 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
Expand Up @@ -80,8 +80,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
initializeRISCVGatherScatterLoweringPass(*PR);
initializeRISCVCodeGenPreparePass(*PR);
initializeRISCVMergeBaseOffsetOptPass(*PR);
initializeRISCVSExtWRemovalPass(*PR);
initializeRISCVStripWSuffixPass(*PR);
initializeRISCVOptWInstrsPass(*PR);
initializeRISCVPreRAExpandPseudoPass(*PR);
initializeRISCVExpandPseudoPass(*PR);
initializeRISCVInsertVSETVLIPass(*PR);
Expand Down Expand Up @@ -362,8 +361,7 @@ void RISCVPassConfig::addMachineSSAOptimization() {
addPass(&MachineCombinerID);

if (TM->getTargetTriple().getArch() == Triple::riscv64) {
addPass(createRISCVSExtWRemovalPass());
addPass(createRISCVStripWSuffixPass());
addPass(createRISCVOptWInstrsPass());
}
}

Expand Down
3 changes: 1 addition & 2 deletions llvm/test/CodeGen/RISCV/O3-pipeline.ll
Expand Up @@ -103,8 +103,7 @@
; CHECK-NEXT: Machine Trace Metrics
; CHECK-NEXT: Lazy Machine Block Frequency Analysis
; CHECK-NEXT: Machine InstCombiner
; RV64-NEXT: RISC-V sext.w Removal
; RV64-NEXT: RISC-V Strip W Suffix
; RV64-NEXT: RISC-V Optimize W Instructions
; CHECK-NEXT: RISC-V Pre-RA pseudo instruction expansion pass
; CHECK-NEXT: RISC-V Merge Base Offset
; CHECK-NEXT: RISC-V Insert VSETVLI pass
Expand Down

0 comments on commit 4c10a61

Please sign in to comment.