llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
specialise.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 
9 #include "core/pass.h"
10 
11 class Func;
12 class CallSite;
13 
14 
15 
19 class SpecialisePass final : public Pass {
20 public:
22  static const char *kPassID;
23 
25  SpecialisePass(PassManager *passManager) : Pass(passManager) {}
26 
28  bool Run(Prog &prog) override;
29 
31  const char *GetPassName() const override;
32 
33 private:
35  class SpecialiseClone;
36 
38  struct Parameter {
39  enum class Kind {
40  INT,
41  FLOAT,
42  GLOBAL
43  };
44 
45  struct G {
46  Global *Symbol;
47  int64_t Offset;
48 
49  G(const G &that) : Symbol(that.Symbol), Offset(that.Offset) {}
50  G(Global *symbol, int64_t offset) : Symbol(symbol), Offset(offset) {}
51 
52  bool operator==(const G &that) const
53  {
54  return Symbol == that.Symbol && Offset == that.Offset;
55  }
56  };
57 
58  Parameter(const Parameter &that);
59  Parameter(const APInt &v) : K(Kind::INT), IntVal(v) {}
60  Parameter(const APFloat &v) : K(Kind::FLOAT), FloatVal(v) {}
61  Parameter(Global *g, int64_t i) : K(Kind::GLOBAL), GlobalVal(g, i) {}
62  ~Parameter();
63 
64  bool operator==(const Parameter &that) const;
65 
66  Parameter &operator=(const Parameter &that);
67 
68  Value *ToValue() const;
69 
70  Kind K;
71 
72  union {
73  APInt IntVal;
74  APFloat FloatVal;
75  G GlobalVal;
76  };
77  };
78 
80  using Parameters = std::map<unsigned, Parameter>;
81 
83  struct ParameterHash {
84  size_t operator()(const Parameter &param) const;
85  };
87  struct ParametersHash {
88  size_t operator()(const Parameters &param) const;
89  };
90 
92  void Specialise(
93  Func *func,
94  const Parameters &params,
95  const std::set<CallSite *> &callSites
96  );
98  Func *Specialise(Func *oldFunc, const Parameters &params);
100  std::pair<std::vector<Ref<Inst>>, std::vector<TypeFlag>>
101  Specialise(CallSite *inst, const Parameters &params);
102 };
SpecialisePass
Definition: specialise.h:19
SpecialisePass::Run
bool Run(Prog &prog) override
Runs the pass.
Definition: specialise.cpp:47
Func
Definition: func.h:30
PassManager
Definition: pass_manager.h:74
Pass
Definition: pass.h:17
Value
Definition: value.h:22
SpecialisePass::SpecialisePass
SpecialisePass(PassManager *passManager)
Initialises the pass.
Definition: specialise.h:25
Prog
Definition: prog.h:33
SpecialisePass::Parameter::G
Definition: specialise.h:45
SpecialisePass::GetPassName
const char * GetPassName() const override
Returns the name of the pass.
Definition: specialise.cpp:24
SpecialisePass::kPassID
static const char * kPassID
Pass identifier.
Definition: specialise.h:22
Global
Definition: global.h:23