llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
x86call.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 
12 #include "core/inst.h"
13 #include "emitter/call_lowering.h"
14 
15 class CallInst;
16 class InvokeInst;
17 class ReturnInst;
18 class RaiseInst;
19 class Func;
20 
21 
25 class X86Call : public CallLowering {
26 public:
28  X86Call(const Func *func) : CallLowering(func) {}
30  X86Call(const CallSite *inst) : CallLowering(inst) {}
32  X86Call(const ReturnInst *inst) : CallLowering(inst) {}
34  X86Call(const RaiseInst *inst) : CallLowering(inst) {}
36  X86Call(const LandingPadInst *inst) : CallLowering(inst) {}
37 
39  unsigned GetFrameSize() const override
40  {
41  return llvm::alignTo(stack_, maxAlign_);
42  }
43 
45  llvm::ArrayRef<unsigned> GetUnusedGPRs() const;
47  llvm::ArrayRef<unsigned> GetUsedGPRs() const;
49  llvm::ArrayRef<unsigned> GetUnusedXMMs() const;
51  llvm::ArrayRef<unsigned> GetUsedXMMs() const;
52 
53 protected:
55  void AssignArgReg(ArgLoc &loc, llvm::MVT t, llvm::Register reg);
57  void AssignArgStack(ArgLoc &loc, llvm::MVT t, unsigned size);
59  void AssignArgByVal(ArgLoc &loc, llvm::MVT t, unsigned size, llvm::Align a);
61  void AssignRetReg(RetLoc &loc, llvm::MVT t, llvm::Register reg);
62 
63 protected:
65  virtual llvm::ArrayRef<unsigned> GetGPRs() const = 0;
67  virtual llvm::ArrayRef<unsigned> GetXMMs() const = 0;
68 
69 protected:
71  unsigned argRegs_ = 0;
73  unsigned argXMMs_ = 0;
75  unsigned retRegs_ = 0;
77  unsigned retXMMs_ = 0;
79  unsigned retFPs_ = 0;
81  unsigned stack_ = 0;
83  llvm::Align maxAlign_ = llvm::Align(8);
84 };
85 
89 class X86_32Call final : public X86Call {
90 public:
92  X86_32Call(const Func *func) : X86Call(func) { AnalyseFunc(func); }
94  X86_32Call(const CallSite *inst) : X86Call(inst) { AnalyseCall(inst); }
96  X86_32Call(const ReturnInst *inst) : X86Call(inst) { AnalyseReturn(inst); }
98  X86_32Call(const RaiseInst *inst) : X86Call(inst) { AnalyseRaise(inst); }
100  X86_32Call(const LandingPadInst *inst) : X86Call(inst) { AnalysePad(inst); }
101 
102 protected:
104  llvm::ArrayRef<unsigned> GetGPRs() const override;
106  llvm::ArrayRef<unsigned> GetXMMs() const override;
107 
108 private:
110  void AssignArgC(unsigned i, FlaggedType type) override;
112  void AssignArgOCaml(unsigned i, FlaggedType type) override;
114  void AssignArgOCamlAlloc(unsigned i, FlaggedType type) override;
116  void AssignArgOCamlGc(unsigned i, FlaggedType type) override;
118  void AssignArgXen(unsigned i, FlaggedType type) override;
120  void AssignArgMultiboot(unsigned i, FlaggedType type) override;
122  void AssignArgWin64(unsigned i, FlaggedType type) override;
123 
125  void AssignRetC(unsigned i, FlaggedType type) override;
127  void AssignRetOCaml(unsigned i, FlaggedType type) override;
129  void AssignRetOCamlAlloc(unsigned i, FlaggedType type) override;
131  void AssignRetOCamlGc(unsigned i, FlaggedType type) override;
133  void AssignRetXen(unsigned i, FlaggedType type) override;
135  void AssignRetWin64(unsigned i, FlaggedType type) override;
136 };
137 
141 class X86_64Call final : public X86Call {
142 public:
144  X86_64Call(const Func *func) : X86Call(func) { AnalyseFunc(func); }
146  X86_64Call(const CallSite *inst) : X86Call(inst) { AnalyseCall(inst); }
148  X86_64Call(const ReturnInst *inst) : X86Call(inst) { AnalyseReturn(inst); }
150  X86_64Call(const RaiseInst *inst) : X86Call(inst) { AnalyseRaise(inst); }
152  X86_64Call(const LandingPadInst *inst) : X86Call(inst) { AnalysePad(inst); }
153 
154 protected:
156  llvm::ArrayRef<unsigned> GetGPRs() const override;
158  llvm::ArrayRef<unsigned> GetXMMs() const override;
159 
160 private:
162  void AssignArgC(unsigned i, FlaggedType type) override;
164  void AssignArgOCaml(unsigned i, FlaggedType type) override;
166  void AssignArgOCamlAlloc(unsigned i, FlaggedType type) override;
168  void AssignArgOCamlGc(unsigned i, FlaggedType type) override;
170  void AssignArgXen(unsigned i, FlaggedType type) override;
172  void AssignArgMultiboot(unsigned i, FlaggedType type) override;
174  void AssignArgWin64(unsigned i, FlaggedType type) override;
175 
177  void AssignRetC(unsigned i, FlaggedType type) override;
179  void AssignRetOCaml(unsigned i, FlaggedType type) override;
181  void AssignRetOCamlAlloc(unsigned i, FlaggedType type) override;
183  void AssignRetOCamlGc(unsigned i, FlaggedType type) override;
185  void AssignRetXen(unsigned i, FlaggedType type) override;
187  void AssignRetWin64(unsigned i, FlaggedType type) override;
188 };
X86Call::X86Call
X86Call(const CallSite *inst)
Analyses a call site.
Definition: x86call.h:30
X86Call::X86Call
X86Call(const ReturnInst *inst)
Analyses a return site.
Definition: x86call.h:32
Func
Definition: func.h:30
X86Call::argXMMs_
unsigned argXMMs_
Number of arguments in vector registers.
Definition: x86call.h:73
X86_32Call
Definition: x86call.h:89
CallLowering::AnalyseFunc
void AnalyseFunc(const Func *func)
Analyse a function.
Definition: call_lowering.cpp:64
X86_64Call::X86_64Call
X86_64Call(const LandingPadInst *inst)
Analyses a landing pad.
Definition: x86call.h:152
X86_32Call::X86_32Call
X86_32Call(const LandingPadInst *inst)
Analyses a landing pad.
Definition: x86call.h:100
X86_64Call::X86_64Call
X86_64Call(const ReturnInst *inst)
Analyses a return site.
Definition: x86call.h:148
CallLowering::AnalyseReturn
void AnalyseReturn(const ReturnInst *inst)
Analyse a return instruction.
Definition: call_lowering.cpp:73
X86_64Call::X86_64Call
X86_64Call(const CallSite *inst)
Analyses a call site.
Definition: x86call.h:146
X86Call::stack_
unsigned stack_
Number of bytes allocated on the stack.
Definition: x86call.h:81
CallLowering
Definition: call_lowering.h:21
X86Call::AssignArgReg
void AssignArgReg(ArgLoc &loc, llvm::MVT t, llvm::Register reg)
Assigns a location to a register.
Definition: x86call.cpp:187
X86_32Call::X86_32Call
X86_32Call(const ReturnInst *inst)
Analyses a return site.
Definition: x86call.h:96
CallLowering::AnalyseRaise
void AnalyseRaise(const RaiseInst *inst)
Analyse a raise instruction.
Definition: call_lowering.cpp:82
FlaggedType
Definition: type.h:77
X86_32Call::X86_32Call
X86_32Call(const Func *func)
Analyses a function for arguments.
Definition: x86call.h:92
X86Call::GetXMMs
virtual llvm::ArrayRef< unsigned > GetXMMs() const =0
Returns the list of XMM registers.
X86Call::X86Call
X86Call(const Func *func)
Analyses a function for arguments.
Definition: x86call.h:28
X86Call::GetUnusedXMMs
llvm::ArrayRef< unsigned > GetUnusedXMMs() const
Returns unused XMMs.
Definition: x86call.cpp:175
X86Call::GetUsedXMMs
llvm::ArrayRef< unsigned > GetUsedXMMs() const
Returns the used XMMs.
Definition: x86call.cpp:181
CallLowering::AnalyseCall
void AnalyseCall(const CallSite *call)
Analyse a call.
Definition: call_lowering.cpp:48
X86Call::retRegs_
unsigned retRegs_
Number of returns in regular registers.
Definition: x86call.h:75
X86_32Call::X86_32Call
X86_32Call(const CallSite *inst)
Analyses a call site.
Definition: x86call.h:94
X86_64Call::X86_64Call
X86_64Call(const Func *func)
Analyses a function for arguments.
Definition: x86call.h:144
X86Call
Definition: x86call.h:25
X86_32Call::GetXMMs
llvm::ArrayRef< unsigned > GetXMMs() const override
Returns the list of XMM registers.
Definition: x86call.cpp:357
X86Call::AssignArgStack
void AssignArgStack(ArgLoc &loc, llvm::MVT t, unsigned size)
Assigns a location to the stack.
Definition: x86call.cpp:193
X86_64Call
Definition: x86call.h:141
X86Call::GetFrameSize
unsigned GetFrameSize() const override
Returns the number of bytes allocated on the stack.
Definition: x86call.h:39
X86Call::GetGPRs
virtual llvm::ArrayRef< unsigned > GetGPRs() const =0
Returns the list of GPR registers.
X86_32Call::X86_32Call
X86_32Call(const RaiseInst *inst)
Analyses a raise site.
Definition: x86call.h:98
X86Call::AssignArgByVal
void AssignArgByVal(ArgLoc &loc, llvm::MVT t, unsigned size, llvm::Align a)
Assigns a location to the stack.
Definition: x86call.cpp:201
X86_32Call::GetGPRs
llvm::ArrayRef< unsigned > GetGPRs() const override
Returns the list of GPR registers.
Definition: x86call.cpp:351
CallLowering::AnalysePad
void AnalysePad(const LandingPadInst *inst)
Analyse a landing pad instruction.
Definition: call_lowering.cpp:91
X86_64Call::GetXMMs
llvm::ArrayRef< unsigned > GetXMMs() const override
Returns the list of XMM registers.
Definition: x86call.cpp:852
X86Call::X86Call
X86Call(const RaiseInst *inst)
Analyses a raise site.
Definition: x86call.h:34
X86_64Call::X86_64Call
X86_64Call(const RaiseInst *inst)
Analyses a raise site.
Definition: x86call.h:150
X86Call::AssignRetReg
void AssignRetReg(RetLoc &loc, llvm::MVT t, llvm::Register reg)
Assigns a location to a register.
Definition: x86call.cpp:214
X86Call::retXMMs_
unsigned retXMMs_
Number of returns in vector registers.
Definition: x86call.h:77
X86Call::argRegs_
unsigned argRegs_
Number of arguments in regular registers.
Definition: x86call.h:71
X86Call::GetUnusedGPRs
llvm::ArrayRef< unsigned > GetUnusedGPRs() const
Returns unused GPRs.
Definition: x86call.cpp:163
X86_64Call::GetGPRs
llvm::ArrayRef< unsigned > GetGPRs() const override
Returns the list of GPR registers.
Definition: x86call.cpp:823
X86Call::X86Call
X86Call(const LandingPadInst *inst)
Analyses a landing pad.
Definition: x86call.h:36
X86Call::maxAlign_
llvm::Align maxAlign_
Maximum alignment on the stack.
Definition: x86call.h:83
X86Call::GetUsedGPRs
llvm::ArrayRef< unsigned > GetUsedGPRs() const
Returns the used GPRs.
Definition: x86call.cpp:169
X86Call::retFPs_
unsigned retFPs_
Number of returns in floating point registers.
Definition: x86call.h:79