llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
riscvisel.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <set>
8 #include <unordered_map>
9 
10 #include <llvm/ADT/DenseMap.h>
11 #include <llvm/Analysis/OptimizationRemarkEmitter.h>
12 #include <llvm/CodeGen/MachineBasicBlock.h>
13 #include <llvm/IR/Module.h>
14 #include <llvm/IR/Type.h>
15 #include <llvm/Pass.h>
16 #include <llvm/Target/RISCV/RISCVInstrInfo.h>
17 #include <llvm/Target/RISCV/RISCVISelDAGToDAG.h>
18 #include <llvm/Target/RISCV/RISCVMachineFunctionInfo.h>
19 #include <llvm/Target/RISCV/RISCVRegisterInfo.h>
20 #include <llvm/Target/RISCV/RISCVSubtarget.h>
21 #include <llvm/Target/RISCV/RISCVTargetMachine.h>
22 
23 #include "core/insts.h"
24 #include "emitter/isel.h"
25 #include "emitter/riscv/riscvcall.h"
26 
27 class Data;
28 class Func;
29 class Inst;
30 class Prog;
31 
32 
33 
37 class RISCVMatcher : public llvm::RISCVDAGMatcher {
38 public:
41  llvm::RISCVTargetMachine &tm,
42  llvm::CodeGenOpt::Level ol,
43  llvm::MachineFunction &mf
44  );
46  ~RISCVMatcher();
47 
49  llvm::SelectionDAG &GetDAG() const { return *CurDAG; }
51  const llvm::TargetLowering *getTargetLowering() const override
52  {
53  return CurDAG->getMachineFunction().getSubtarget().getTargetLowering();
54  }
55 
56 private:
58  llvm::RISCVTargetMachine &tm_;
59 };
60 
61 
62 
66 class RISCVISel final : public ISel {
67 public:
68  static char ID;
69 
70  RISCVISel(
71  const Target &target,
72  llvm::RISCVTargetMachine &tm,
73  llvm::TargetLibraryInfo &libInfo,
74  const Prog &prog,
75  llvm::CodeGenOpt::Level ol,
76  bool shared
77  );
78 
79 private:
81  void Lower(llvm::MachineFunction &mf) override
82  {
83  m_.reset(new RISCVMatcher(tm_, ol_, mf));
84  }
85 
87  SDValue GetRegArch(Register reg) override;
88 
90  void LowerArch(const Inst *inst) override;
91 
93  void LowerSyscall(const SyscallInst *inst) override;
95  void LowerClone(const CloneInst *inst) override;
97  void LowerReturn(const ReturnInst *inst) override;
99  void LowerRaise(const RaiseInst *inst) override;
101  void LowerSpawn(const SpawnInst *inst) override;
103  void LowerLandingPad(const LandingPadInst *inst) override;
105  void LowerSet(const SetInst *inst) override;
107  void LowerXchg(const RISCV_XchgInst *inst);
109  void LowerCmpXchg(const RISCV_CmpXchgInst *inst);
111  void LowerFence(const RISCV_FenceInst *inst);
113  void LowerGP(const RISCV_GpInst *inst);
114 
116  void LowerArguments(bool hasVAStart) override;
118  void LowerVASetup(const RISCVCall &ci);
119 
120 private:
122  llvm::SelectionDAG &GetDAG() const override { return m_->GetDAG(); }
124  void PreprocessISelDAG() override { m_->PreprocessISelDAG(); }
126  void PostprocessISelDAG() override { m_->PostprocessISelDAG(); }
128  void Select(SDNode *node) override { m_->Select(node); }
129 
131  llvm::Register GetStackRegister() const override { return llvm::RISCV::X2; }
132 
133 private:
135  void SaveVarArgRegisters(const RISCVCall &ci, bool isWin64);
137  llvm::SDValue LowerCallee(ConstRef<Inst> inst);
139  void LowerCallSite(llvm::SDValue chain, const CallSite *call) override;
140 
141 private:
143  llvm::RISCVTargetMachine &tm_;
145  std::unique_ptr<RISCVMatcher> m_;
147  llvm::Function *trampoline_;
149  bool shared_;
150 };
RISCVCall
Definition: riscvcall.h:28
Inst
Definition: inst.h:53
Func
Definition: func.h:30
RISCVISel
Definition: riscvisel.h:66
ConstRef< Inst >
Target
Definition: target.h:24
Data
Definition: data.h:47
RISCVMatcher::getTargetLowering
const llvm::TargetLowering * getTargetLowering() const override
Returns the target lowering.
Definition: riscvisel.h:51
ID
Definition: id.h:19
ISel
Definition: isel.h:26
RISCVMatcher::RISCVMatcher
RISCVMatcher(llvm::RISCVTargetMachine &tm, llvm::CodeGenOpt::Level ol, llvm::MachineFunction &mf)
Construct a new wrapper around the LLVM selector.
Definition: riscvisel.cpp:43
Prog
Definition: prog.h:33
RISCVMatcher::~RISCVMatcher
~RISCVMatcher()
Delete the matcher.
Definition: riscvisel.cpp:65
RISCVMatcher::GetDAG
llvm::SelectionDAG & GetDAG() const
Return the current DAG.
Definition: riscvisel.h:49
ISel::ol_
llvm::CodeGenOpt::Level ol_
Chosen optimisation level.
Definition: isel.h:327
RISCVMatcher
Definition: riscvisel.h:37