Skip to content
Closed
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
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_llvm_target(RISCVCodeGen
RISCVCallingConv.cpp
RISCVCodeGenPrepare.cpp
RISCVConstantPoolValue.cpp
RISCVCopyCombine.cpp
RISCVDeadRegisterDefinitions.cpp
RISCVExpandAtomicPseudoInsts.cpp
RISCVExpandPseudoInsts.cpp
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/RISCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ void initializeRISCVCodeGenPreparePass(PassRegistry &);

FunctionPass *createRISCVDeadRegisterDefinitionsPass();
void initializeRISCVDeadRegisterDefinitionsPass(PassRegistry &);
FunctionPass *createRISCVCopyCombinePass();
void initializeRISCVCopyCombinePass(PassRegistry &);

FunctionPass *createRISCVIndirectBranchTrackingPass();
void initializeRISCVIndirectBranchTrackingPass(PassRegistry &);
Expand Down
186 changes: 186 additions & 0 deletions llvm/lib/Target/RISCV/RISCVCopyCombine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
//===- RISCVCopyCombine.cpp - Remove special copy for RISC-V --------------===//
//
// 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 attempts a shrink-wrap optimization for special cases, which is
// effective when data types require extension.
//
// After finalize-isel:
// bb0:
// liveins: $x10, $x11
// %1:gpr = COPY $x11 ---- will be delete in this pass
// %0:gpr = COPY $x10
// %2:gpr = COPY %1:gpr ---- without this pass, sink to bb1 in machine-sink,
// then delete at regalloc
// BEQ %0:gpr, killed %3:gpr, %bb.3 PseudoBR %bb1
//
// bb1:
// bb2:
// BNE %2:gpr, killed %5:gpr, %bb.2
// ...
// After regalloc
// bb0:
// liveins: $x10, $x11
// renamable $x8 = COPY $x11
// renamable $x11 = ADDI $x0, 57 --- def x11, so COPY can not be sink
// BEQ killed renamable $x10, killed renamable $x11, %bb.4
// PseudoBR %bb.1
//
// bb1:
// bb2:
// BEQ killed renamable $x8, killed renamable $x10, %bb.4
//
// ----->
//
// After this pass:
// bb0:
// liveins: $x10, $x11
// %0:gpr = COPY $x10
// %2:gpr = COPY $x11
// BEQ %0:gpr, killed %3:gpr, %bb.3
// PseudoBR %bb1
//
// bb1:
// bb2:
// BNE %2:gpr, killed %5:gpr, %bb.2
// ...
// After regalloc
// bb0:
// liveins: $x10, $x11
// renamable $x12 = ADDI $x0, 57
// renamable $x8 = COPY $x11
// BEQ killed renamable $x10, killed renamable $x11, %bb.4
// PseudoBR %bb.1
//
// bb1:
// bb2:
// BEQ killed renamable $x8, killed renamable $x10, %bb.4
//===----------------------------------------------------------------------===//

#include "RISCV.h"
#include "RISCVSubtarget.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunctionPass.h"

using namespace llvm;
#define DEBUG_TYPE "riscv-copy-combine"
#define RISCV_COPY_COMBINE "RISC-V Copy Combine"

STATISTIC(NumCopyDeleted, "Number of copy deleted");

namespace {
class RISCVCopyCombine : public MachineFunctionPass {
public:
static char ID;
const TargetInstrInfo *TII;
MachineRegisterInfo *MRI;
const TargetRegisterInfo *TRI;

RISCVCopyCombine() : MachineFunctionPass(ID) {}
bool runOnMachineFunction(MachineFunction &MF) override;
MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::IsSSA);
}

StringRef getPassName() const override { return RISCV_COPY_COMBINE; }

private:
bool optimizeBlock(MachineBasicBlock &MBB);
bool copyCombine(MachineOperand &Op);
};
} // end anonymous namespace

char RISCVCopyCombine::ID = 0;
INITIALIZE_PASS(RISCVCopyCombine, DEBUG_TYPE, RISCV_COPY_COMBINE, false, false)

/// Check if it's safe to move From down to To, checking that no physical
/// registers are clobbered.
static bool isSafeToMove(const MachineInstr &From, const MachineInstr &To) {
SmallVector<Register> PhysUses;
for (const MachineOperand &MO : From.all_uses())
if (MO.getReg().isPhysical())
PhysUses.push_back(MO.getReg());
bool SawStore = false;
for (auto II = From.getIterator(); II != To.getIterator(); II++) {
for (Register PhysReg : PhysUses)
if (II->definesRegister(PhysReg, nullptr))
return false;
if (II->mayStore()) {
SawStore = true;
break;
}
}
return From.isSafeToMove(SawStore);
}

bool RISCVCopyCombine::copyCombine(MachineOperand &Op) {
if (!Op.isReg())
return false;

Register Reg = Op.getReg();
if (!Reg.isVirtual())
return false;

MachineInstr *MI = MRI->getVRegDef(Reg);
if (MI->getOpcode() != RISCV::COPY)
return false;

Register Op1reg = MI->getOperand(1).getReg();
if (!MRI->hasOneUse(Op1reg) || !Op1reg.isVirtual() ||
!MI->getOperand(0).getReg().isVirtual())
return false;

MachineInstr *Src = MRI->getVRegDef(Op1reg);
if (!Src || Src->hasUnmodeledSideEffects() ||
Src->getOpcode() != RISCV::COPY || Src->getParent() != MI->getParent() ||
Src->getNumDefs() != 1)
return false;

if (!isSafeToMove(*Src, *MI))
return false;

Register SrcOp1reg = Src->getOperand(1).getReg();
MRI->replaceRegWith(Op1reg, SrcOp1reg);
MRI->clearKillFlags(SrcOp1reg);
LLVM_DEBUG(dbgs() << "Deleting this copy instruction "; Src->print(dbgs()));
++NumCopyDeleted;
Src->eraseFromParent();
return true;
}

bool RISCVCopyCombine::optimizeBlock(MachineBasicBlock &MBB) {
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 3> Cond;
if (TII->analyzeBranch(MBB, TBB, FBB, Cond, /*AllowModify*/ false) ||
Copy link
Collaborator

Choose a reason for hiding this comment

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

What makes branch uses special?

Cond.empty())
return false;

if (!TBB || Cond.size() != 3)
return false;

return copyCombine(Cond[1]) || copyCombine(Cond[2]);
}

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

TII = MF.getSubtarget().getInstrInfo();
MRI = &MF.getRegInfo();
TRI = MRI->getTargetRegisterInfo();

bool Changed = false;
for (MachineBasicBlock &MBB : MF)
Changed |= optimizeBlock(MBB);

return Changed;
}

FunctionPass *llvm::createRISCVCopyCombinePass() {
return new RISCVCopyCombine();
}
3 changes: 3 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
initializeRISCVPostLegalizerCombinerPass(*PR);
initializeKCFIPass(*PR);
initializeRISCVDeadRegisterDefinitionsPass(*PR);
initializeRISCVCopyCombinePass(*PR);
initializeRISCVLateBranchOptPass(*PR);
initializeRISCVMakeCompressibleOptPass(*PR);
initializeRISCVGatherScatterLoweringPass(*PR);
Expand Down Expand Up @@ -455,6 +456,7 @@ bool RISCVPassConfig::addRegAssignAndRewriteFast() {
if (TM->getOptLevel() != CodeGenOptLevel::None &&
EnableRISCVDeadRegisterElimination)
addPass(createRISCVDeadRegisterDefinitionsPass());

return TargetPassConfig::addRegAssignAndRewriteFast();
}

Expand Down Expand Up @@ -598,6 +600,7 @@ void RISCVPassConfig::addPreEmitPass2() {
}

void RISCVPassConfig::addMachineSSAOptimization() {
addPass(createRISCVCopyCombinePass());
addPass(createRISCVVectorPeepholePass());
addPass(createRISCVFoldMemOffsetPass());

Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/RISCV/O3-pipeline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
; CHECK-NEXT: Lazy Block Frequency Analysis
; CHECK-NEXT: RISC-V DAG->DAG Pattern Instruction Selection
; CHECK-NEXT: Finalize ISel and expand pseudo-instructions
; CHECK-NEXT: RISC-V Copy Combine
; CHECK-NEXT: RISC-V Vector Peephole Optimization
; CHECK-NEXT: RISC-V Fold Memory Offset
; CHECK-NEXT: Lazy Machine Block Frequency Analysis
Expand Down
Loading
Loading