Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions llvm/lib/Target/RISCV/RISCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ class RISCVRegisterBankInfo;
class RISCVSubtarget;
class RISCVTargetMachine;

FunctionPass *createRISCVCodeGenPreparePass();
void initializeRISCVCodeGenPreparePass(PassRegistry &);
class RISCVCodeGenPreparePass : public PassInfoMixin<RISCVCodeGenPreparePass> {
private:
const RISCVTargetMachine *TM;

public:
RISCVCodeGenPreparePass(const RISCVTargetMachine *TM) : TM(TM) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
};
FunctionPass *createRISCVCodeGenPrepareLegacyPass();
void initializeRISCVCodeGenPrepareLegacyPassPass(PassRegistry &);

FunctionPass *createRISCVDeadRegisterDefinitionsPass();
void initializeRISCVDeadRegisterDefinitionsPass(PassRegistry &);
Expand Down
82 changes: 53 additions & 29 deletions llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,42 @@ using namespace llvm;
#define PASS_NAME "RISC-V CodeGenPrepare"

namespace {

class RISCVCodeGenPrepare : public FunctionPass,
public InstVisitor<RISCVCodeGenPrepare, bool> {
class RISCVCodeGenPrepare : public InstVisitor<RISCVCodeGenPrepare, bool> {
Function &F;
const DataLayout *DL;
const DominatorTree *DT;
const RISCVSubtarget *ST;

public:
RISCVCodeGenPrepare(Function &F, const DominatorTree *DT,
const RISCVSubtarget *ST)
: F(F), DL(&F.getDataLayout()), DT(DT), ST(ST) {}
bool run();
bool visitInstruction(Instruction &I) { return false; }
bool visitAnd(BinaryOperator &BO);
bool visitIntrinsicInst(IntrinsicInst &I);
bool expandVPStrideLoad(IntrinsicInst &I);
bool widenVPMerge(IntrinsicInst &I);
};
} // namespace

namespace {
class RISCVCodeGenPrepareLegacyPass : public FunctionPass {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just drop Pass from the end of the name to fix the "PassPass" issue?

There are other examples like
SelectionDAGISelLegacy, LiveDebugVariablesWrapperLegacy, LiveStacksWrapperLegacy, DXILDataScalarizationLegacy, DXILFinalizeLinkageLegacy, DXILLegalizeLegacy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's another option that's been used in-tree. To me it makes sense that the shared class containing the logic is just "RISCVCodeGenPrepare" and we have separate RISCVCodeGenPreparePass (newpm) and RISCVCodeGenPrepareLegacyPass (oldpm) classes providing the pass manager interfaces. But I don't feel overly strongly. And hopefully the *Legacy* classes can be removed one day too.

(Sidenote: I think the "new" pass manager will be a teenager next year!).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

48 of the classes in lib/CodeGen use Legacy and 11 use LegacyPass. I'm not sure if that suggests an overall project preference or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or to put it another way - I agree that's a reasonable alternative. My feeling is that having the ugly auto-generated initializeFooPassPass is worth it for a more intuitive (IMHO) naming of the pass adapters. But If you feel otherwise, I'm happy to change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly. I knew that this is not a new issue and was trying to figure out what had been done before.

public:
static char ID;

RISCVCodeGenPrepare() : FunctionPass(ID) {}
RISCVCodeGenPrepareLegacyPass() : FunctionPass(ID) {}

bool runOnFunction(Function &F) override;

StringRef getPassName() const override { return PASS_NAME; }

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<TargetPassConfig>();
}

bool visitInstruction(Instruction &I) { return false; }
bool visitAnd(BinaryOperator &BO);
bool visitIntrinsicInst(IntrinsicInst &I);
bool expandVPStrideLoad(IntrinsicInst &I);
bool widenVPMerge(IntrinsicInst &I);
};

} // end anonymous namespace
} // namespace

// Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set,
// but bits 63:32 are zero. If we know that bit 31 of X is 0, we can fill
Expand Down Expand Up @@ -273,17 +279,7 @@ bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &II) {
return true;
}

bool RISCVCodeGenPrepare::runOnFunction(Function &F) {
if (skipFunction(F))
return false;

auto &TPC = getAnalysis<TargetPassConfig>();
auto &TM = TPC.getTM<RISCVTargetMachine>();
ST = &TM.getSubtarget<RISCVSubtarget>(F);

DL = &F.getDataLayout();
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();

bool RISCVCodeGenPrepare::run() {
bool MadeChange = false;
for (auto &BB : F)
for (Instruction &I : llvm::make_early_inc_range(BB))
Expand All @@ -292,12 +288,40 @@ bool RISCVCodeGenPrepare::runOnFunction(Function &F) {
return MadeChange;
}

INITIALIZE_PASS_BEGIN(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false)
bool RISCVCodeGenPrepareLegacyPass::runOnFunction(Function &F) {
if (skipFunction(F))
return false;

auto &TPC = getAnalysis<TargetPassConfig>();
auto &TM = TPC.getTM<RISCVTargetMachine>();
auto ST = &TM.getSubtarget<RISCVSubtarget>(F);
auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();

RISCVCodeGenPrepare RVCGP(F, DT, ST);
return RVCGP.run();
}

INITIALIZE_PASS_BEGIN(RISCVCodeGenPrepareLegacyPass, DEBUG_TYPE, PASS_NAME,
false, false)
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
INITIALIZE_PASS_END(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false)
INITIALIZE_PASS_END(RISCVCodeGenPrepareLegacyPass, DEBUG_TYPE, PASS_NAME, false,
false)

char RISCVCodeGenPrepare::ID = 0;
char RISCVCodeGenPrepareLegacyPass::ID = 0;

FunctionPass *llvm::createRISCVCodeGenPrepareLegacyPass() {
return new RISCVCodeGenPrepareLegacyPass();
}

FunctionPass *llvm::createRISCVCodeGenPreparePass() {
return new RISCVCodeGenPrepare();
PreservedAnalyses RISCVCodeGenPreparePass::run(Function &F,
FunctionAnalysisManager &FAM) {
DominatorTree *DT = &FAM.getResult<DominatorTreeAnalysis>(F);
auto ST = &TM->getSubtarget<RISCVSubtarget>(F);
bool Changed = RISCVCodeGenPrepare(F, DT, ST).run();
if (!Changed)
return PreservedAnalyses::all();

PreservedAnalyses PA = PreservedAnalyses::none();
PA.preserveSet<CFGAnalyses>();
return PA;
}
20 changes: 20 additions & 0 deletions llvm/lib/Target/RISCV/RISCVPassRegistry.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===- RISCVPassRegistry.def - Registry of RISC-V passes --------*- C++ -*-===//
//
// 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 is used as the registry of passes that are part of the RISC-V
// backend.
//
//===----------------------------------------------------------------------===//

// NOTE: NO INCLUDE GUARD DESIRED!

#ifndef FUNCTION_PASS
#define FUNCTION_PASS(NAME, CREATE_PASS)
#endif
FUNCTION_PASS("riscv-codegenprepare", RISCVCodeGenPreparePass(this))
#undef FUNCTION_PASS
7 changes: 5 additions & 2 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
initializeRISCVLateBranchOptPass(*PR);
initializeRISCVMakeCompressibleOptPass(*PR);
initializeRISCVGatherScatterLoweringPass(*PR);
initializeRISCVCodeGenPreparePass(*PR);
initializeRISCVCodeGenPrepareLegacyPassPass(*PR);
initializeRISCVPostRAExpandPseudoPass(*PR);
initializeRISCVMergeBaseOffsetOptPass(*PR);
initializeRISCVOptWInstrsPass(*PR);
Expand Down Expand Up @@ -456,7 +456,7 @@ void RISCVPassConfig::addIRPasses() {

addPass(createRISCVGatherScatterLoweringPass());
addPass(createInterleavedAccessPass());
addPass(createRISCVCodeGenPreparePass());
addPass(createRISCVCodeGenPrepareLegacyPass());
}

TargetPassConfig::addIRPasses();
Expand Down Expand Up @@ -628,6 +628,9 @@ bool RISCVPassConfig::addILPOpts() {
}

void RISCVTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
#define GET_PASS_REGISTRY "RISCVPassRegistry.def"
#include "llvm/Passes/TargetPassRegistry.inc"

PB.registerLateLoopOptimizationsEPCallback([=](LoopPassManager &LPM,
OptimizationLevel Level) {
if (Level != OptimizationLevel::O0)
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/RISCV/riscv-codegenprepare.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt %s -S -riscv-codegenprepare -mtriple=riscv64 | FileCheck %s
; RUN: opt %s -S -passes=riscv-codegenprepare -mtriple=riscv64 | FileCheck %s

; Make sure we convert the 4294967294 in for.body.preheader.new to -2 based on
; the upper 33 bits being zero by the dominating condition %cmp3.
Expand Down
Loading