llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
x86isel.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/X86/X86InstrInfo.h>
17 #include <llvm/Target/X86/X86ISelDAGToDAG.h>
18 #include <llvm/Target/X86/X86MachineFunctionInfo.h>
19 #include <llvm/Target/X86/X86RegisterInfo.h>
20 #include <llvm/Target/X86/X86Subtarget.h>
21 #include <llvm/Target/X86/X86TargetMachine.h>
22 
23 #include "core/insts.h"
24 #include "emitter/isel.h"
25 #include "emitter/x86/x86call.h"
26 
27 class Data;
28 class Func;
29 class Inst;
30 class Prog;
31 
32 
33 
37 class X86Matcher : public llvm::X86DAGMatcher {
38 public:
40  X86Matcher(
41  llvm::X86TargetMachine &tm,
42  llvm::CodeGenOpt::Level ol,
43  llvm::MachineFunction &mf
44  );
46  ~X86Matcher();
47 
49  llvm::SelectionDAG &GetDAG() const { return *CurDAG; }
51  const llvm::X86TargetMachine &getTargetMachine() const override { return tm_; }
52 
53 private:
55  llvm::X86TargetMachine &tm_;
56 };
57 
61 class X86ISel final : public ISel {
62 public:
63  static char ID;
64 
65  X86ISel(
66  Target &target,
67  llvm::X86TargetMachine &tm,
68  llvm::TargetLibraryInfo &libInfo,
69  const Prog &prog,
70  llvm::CodeGenOpt::Level ol,
71  bool shared
72  );
73 
74 private:
76  void Lower(llvm::MachineFunction &mf) override
77  {
78  m_.reset(new X86Matcher(tm_, ol_, mf));
79  }
80 
82  llvm::SDValue LowerCallee(ConstRef<Inst> inst);
83 
85  void LowerArch(const Inst *inst) override;
86 
88  void LowerSyscall(const SyscallInst *inst) override;
90  void LowerClone(const CloneInst *inst) override;
92  void LowerReturn(const ReturnInst *inst) override;
94  void LowerRaise(const RaiseInst *inst) override;
96  void LowerSpawn(const SpawnInst *inst) override;
98  void LowerLandingPad(const LandingPadInst *inst) override;
100  void LowerSet(const SetInst *inst) override;
101 
103  void LowerFPUControl(
104  unsigned opcode,
105  unsigned bytes,
106  bool store,
107  const Inst *inst
108  );
109 
111  void Lower(const BarrierInst *inst, unsigned op);
113  void Lower(const X86_CpuIdInst *inst);
115  void Lower(const X86_XchgInst *inst);
117  void Lower(const X86_CmpXchgInst *inst);
119  void Lower(const X86_FnClExInst *inst);
121  void Lower(const X86_RdTscInst *inst);
123  void Lower(const X86_InInst *inst);
125  void Lower(const X86_OutInst *inst);
127  void Lower(const X86_WrMsrInst *inst);
129  void Lower(const X86_RdMsrInst *inst);
131  void Lower(const X86_PauseInst *inst);
133  void Lower(const X86_YieldInst *inst);
135  void Lower(const X86_StiInst *inst);
137  void Lower(const X86_CliInst *inst);
139  void Lower(const X86_HltInst *inst);
141  void Lower(const X86_SpinInst *inst);
143  void Lower(const X86_LgdtInst *inst);
145  void Lower(const X86_LidtInst *inst);
147  void Lower(const X86_LtrInst *inst);
149  void Lower(const X86_XSaveOptInst *inst);
151  void Lower(const X86_XRestoreInst *inst);
152 
154  void LowerArguments(bool hasVAStart) override;
156  void LowerVASetup(const X86Call &lowering);
157 
159  virtual bool Finalize(llvm::MachineFunction &MF) override;
160 
161 private:
163  SDValue GetRegArch(Register reg) override;
165  void LowerSetSP(SDValue value);
167  void LowerSetReg(const char *code, MVT type, SDValue value);
169  void LowerRaise(SDValue spVal, SDValue pcVal, SDValue glue);
171  void LowerContextMask(bool store, unsigned op, SDValue addr, SDValue mask);
173  void LowerSetCs(SDValue value);
174 
175 private:
177  llvm::SelectionDAG &GetDAG() const override { return m_->GetDAG(); }
179  void PreprocessISelDAG() override { m_->PreprocessISelDAG(); }
181  void PostprocessISelDAG() override { m_->PostprocessISelDAG(); }
183  void Select(SDNode *node) override { m_->Select(node); }
184 
186  llvm::Register GetStackRegister() const override;
188  llvm::Register GetBaseRegister() const;
189 
190 private:
192  void LowerCallSite(llvm::SDValue chain, const CallSite *call) override;
193 
194 private:
196  llvm::X86TargetMachine &tm_;
198  std::unique_ptr<X86Matcher> m_;
200  llvm::Function *trampoline_;
202  bool shared_;
203 };
Inst
Definition: inst.h:53
Func
Definition: func.h:30
X86Matcher::getTargetMachine
const llvm::X86TargetMachine & getTargetMachine() const override
Return the X86 target machine.
Definition: x86isel.h:51
ConstRef< Inst >
X86Matcher::X86Matcher
X86Matcher(llvm::X86TargetMachine &tm, llvm::CodeGenOpt::Level ol, llvm::MachineFunction &mf)
Construct a new wrapper around the LLVM selector.
Definition: x86isel.cpp:53
Target
Definition: target.h:24
Data
Definition: data.h:47
ID
Definition: id.h:19
ISel
Definition: isel.h:26
X86Call
Definition: x86call.h:25
X86Matcher::GetDAG
llvm::SelectionDAG & GetDAG() const
Return the current DAG.
Definition: x86isel.h:49
X86ISel
Definition: x86isel.h:61
Prog
Definition: prog.h:33
X86Matcher
Definition: x86isel.h:37
ISel::ol_
llvm::CodeGenOpt::Level ol_
Chosen optimisation level.
Definition: isel.h:327
X86Matcher::~X86Matcher
~X86Matcher()
Delete the matcher.
Definition: x86isel.cpp:71