aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-exegesis/lib/RISCV/RISCVExegesisPreprocessing.cpp
blob: 7f1cfd9ea52df8c686a18d5bc38804af5195d21c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//===- RISCVExegesisPreprocessing.cpp -------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// \file
//
//===----------------------------------------------------------------------===//

#include "RISCV.h"
#include "RISCVExegesisPasses.h"
#include "RISCVRegisterInfo.h"
#include "RISCVSubtarget.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"

using namespace llvm;

#define DEBUG_TYPE "riscv-exegesis-preprocessing"

namespace {
struct RISCVExegesisPreprocessing : public MachineFunctionPass {
  static char ID;

  RISCVExegesisPreprocessing() : MachineFunctionPass(ID) {}

  bool runOnMachineFunction(MachineFunction &MF) override;

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

char RISCVExegesisPreprocessing::ID = 0;

static bool processAVLOperand(MachineInstr &MI, MachineRegisterInfo &MRI,
                              const TargetInstrInfo &TII) {
  const MCInstrDesc &Desc = TII.get(MI.getOpcode());
  uint64_t TSFlags = Desc.TSFlags;
  if (!RISCVII::hasVLOp(TSFlags))
    return false;

  const MachineOperand &VLOp = MI.getOperand(RISCVII::getVLOpNum(Desc));
  if (VLOp.isReg()) {
    Register VLReg = VLOp.getReg();
    if (VLReg.isVirtual())
      return false;
    assert(RISCV::GPRRegClass.contains(VLReg));
    // Replace all uses of the original physical register with a new virtual
    // register. The only reason we can do such replacement here is because it's
    // almost certain that VLReg only has a single definition.
    Register NewVLReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
    MRI.replaceRegWith(VLReg, NewVLReg);
    return true;
  }

  return false;
}

bool RISCVExegesisPreprocessing::runOnMachineFunction(MachineFunction &MF) {
  MachineRegisterInfo &MRI = MF.getRegInfo();
  const auto &STI = MF.getSubtarget<RISCVSubtarget>();
  if (!STI.hasVInstructions())
    return false;
  const TargetInstrInfo &TII = *STI.getInstrInfo();

  LLVM_DEBUG(MF.print(dbgs() << "===Before RISCVExegesisPoreprocessing===\n");
             dbgs() << "\n");

  bool Changed = false;
  for (auto &MBB : MF)
    for (auto &MI : MBB) {
      Changed |= processAVLOperand(MI, MRI, TII);
    }

  return Changed;
}

FunctionPass *llvm::exegesis::createRISCVPreprocessingPass() {
  return new RISCVExegesisPreprocessing();
}