llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
riscvcall.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 <vector>
8 
9 #include <llvm/ADT/ArrayRef.h>
10 #include <llvm/ADT/iterator_range.h>
11 #include <llvm/MC/MCRegister.h>
12 
13 #include "core/inst.h"
14 #include "core/func.h"
15 #include "emitter/call_lowering.h"
16 
17 class CallInst;
18 class InvokeInst;
19 class ReturnInst;
20 class RaiseInst;
21 class Func;
22 
23 
24 
28 class RISCVCall final : public CallLowering {
29 public:
31  RISCVCall(const Func *func)
32  : CallLowering(func)
33  , numFixedArgs_(func->GetNumParams())
34  {
35  AnalyseFunc(func);
36  }
37 
39  RISCVCall(const CallSite *inst)
40  : CallLowering(inst)
41  , numFixedArgs_(inst->GetNumFixedArgs().value_or(inst->arg_size()))
42  {
43  AnalyseCall(inst);
44  }
45 
47  RISCVCall(const ReturnInst *inst)
48  : CallLowering(inst)
49  {
50  AnalyseReturn(inst);
51  }
52 
54  RISCVCall(const LandingPadInst *inst)
55  : CallLowering(inst)
56  {
57  AnalysePad(inst);
58  }
59 
61  RISCVCall(const RaiseInst *inst)
62  : CallLowering(inst)
63  {
64  AnalyseRaise(inst);
65  }
66 
68  llvm::ArrayRef<llvm::MCPhysReg> GetUnusedGPRs() const;
70  llvm::ArrayRef<llvm::MCPhysReg> GetUsedGPRs() const;
72  llvm::ArrayRef<llvm::MCPhysReg> GetUnusedFPRs() const;
74  llvm::ArrayRef<llvm::MCPhysReg> GetUsedFPRs() const;
75 
77  unsigned GetFrameSize() const override { return stack_; }
78 
79 private:
81  void AssignArgC(unsigned i, FlaggedType type) override;
83  void AssignArgOCaml(unsigned i, FlaggedType type) override;
85  void AssignArgOCamlAlloc(unsigned i, FlaggedType type) override;
87  void AssignArgOCamlGc(unsigned i, FlaggedType type) override;
89  void AssignArgXen(unsigned i, FlaggedType type) override;
91  void AssignArgWin64(unsigned i, FlaggedType type) override;
92 
94  void AssignRetC(unsigned i, FlaggedType type) override;
96  void AssignRetOCaml(unsigned i, FlaggedType type) override;
98  void AssignRetOCamlAlloc(unsigned i, FlaggedType type) override;
100  void AssignRetOCamlGc(unsigned i, FlaggedType type) override;
102  void AssignRetXen(unsigned i, FlaggedType type) override;
104  void AssignRetWin64(unsigned i, FlaggedType type) override;
105 
107  void AssignArgReg(ArgLoc &loc, llvm::MVT vt, llvm::Register reg);
109  void AssignArgStack(ArgLoc &loc, llvm::MVT type, unsigned size);
111  void AssignRetReg(RetLoc &loc, llvm::MVT vt, llvm::Register reg);
112 
114  llvm::ArrayRef<llvm::MCPhysReg> GetGPRs() const;
116  llvm::ArrayRef<llvm::MCPhysReg> GetFPRs() const;
117 
118 private:
120  unsigned numFixedArgs_ = 0;
122  unsigned argI_ = 0;
124  unsigned argF_ = 0;
126  unsigned retI_ = 0;
128  unsigned retF_ = 0;
130  unsigned stack_ = 0;
131 };
RISCVCall
Definition: riscvcall.h:28
Func
Definition: func.h:30
RISCVCall::RISCVCall
RISCVCall(const LandingPadInst *inst)
Analyses a return site.
Definition: riscvcall.h:54
CallLowering::AnalyseFunc
void AnalyseFunc(const Func *func)
Analyse a function.
Definition: call_lowering.cpp:64
CallLowering::AnalyseReturn
void AnalyseReturn(const ReturnInst *inst)
Analyse a return instruction.
Definition: call_lowering.cpp:73
RISCVCall::GetUnusedGPRs
llvm::ArrayRef< llvm::MCPhysReg > GetUnusedGPRs() const
Returns unused GPRs.
Definition: riscvcall.cpp:95
CallLowering
Definition: call_lowering.h:21
CallLowering::AnalyseRaise
void AnalyseRaise(const RaiseInst *inst)
Analyse a raise instruction.
Definition: call_lowering.cpp:82
FlaggedType
Definition: type.h:77
RISCVCall::GetFrameSize
unsigned GetFrameSize() const override
Returns the number of bytes allocated on the stack.
Definition: riscvcall.h:77
RISCVCall::GetUsedGPRs
llvm::ArrayRef< llvm::MCPhysReg > GetUsedGPRs() const
Returns the used GPRs.
Definition: riscvcall.cpp:102
CallLowering::AnalyseCall
void AnalyseCall(const CallSite *call)
Analyse a call.
Definition: call_lowering.cpp:48
RISCVCall::RISCVCall
RISCVCall(const CallSite *inst)
Analyses a call site.
Definition: riscvcall.h:39
RISCVCall::RISCVCall
RISCVCall(const RaiseInst *inst)
Analyses a raise site.
Definition: riscvcall.h:61
CallLowering::AnalysePad
void AnalysePad(const LandingPadInst *inst)
Analyse a landing pad instruction.
Definition: call_lowering.cpp:91
RISCVCall::RISCVCall
RISCVCall(const ReturnInst *inst)
Analyses a return site.
Definition: riscvcall.h:47
RISCVCall::GetUnusedFPRs
llvm::ArrayRef< llvm::MCPhysReg > GetUnusedFPRs() const
Returns unused FPRs.
Definition: riscvcall.cpp:109
RISCVCall::RISCVCall
RISCVCall(const Func *func)
Analyses a function for arguments.
Definition: riscvcall.h:31
RISCVCall::GetUsedFPRs
llvm::ArrayRef< llvm::MCPhysReg > GetUsedFPRs() const
Returns the used FPRs.
Definition: riscvcall.cpp:116