File: RISCVExegesisPreprocessing.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,236,292 kB
  • sloc: cpp: 7,619,115; ansic: 1,433,921; asm: 1,058,734; python: 252,125; f90: 94,671; objc: 70,753; lisp: 42,813; pascal: 18,401; sh: 10,093; ml: 5,111; perl: 4,720; awk: 3,523; makefile: 3,397; javascript: 2,272; xml: 892; fortran: 770
file content (85 lines) | stat: -rw-r--r-- 2,692 bytes parent folder | download | duplicates (3)
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();
}