llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
refinement.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 <queue>
8 #include <set>
9 #include <unordered_set>
10 
11 #include "core/inst_visitor.h"
12 #include "core/target.h"
13 #include "passes/tags/tagged_type.h"
14 
15 
16 
17 namespace tags {
18 
19 class RegisterAnalysis;
20 struct DominatorCache;
21 
25 class Refinement : public InstVisitor<void> {
26 public:
27  Refinement(
28  RegisterAnalysis &analysis,
29  const Target *target,
30  bool banPolymorphism,
31  Func &func
32  );
33 
34  void Run();
35 
36 private:
38  void PullFrontier();
39 
41  void Refine(Block *parent, Ref<Inst> ref, const TaggedType &type);
43  void Refine(Block *start, Block *end, Ref<Inst> ref, const TaggedType &type);
45  void Refine(Ref<Inst> ref, const TaggedType &type);
47  void RefineJoin(Ref<Inst> ref, const TaggedType &ty, Use &use, Type type);
49  Ref<Inst> Cast(Ref<Inst> ref, const TaggedType &ty);
51  bool IsNonPolymorphic(Ref<Inst> ref, const TaggedType &ty);
53  void Specialise(
54  Ref<Inst> ref,
55  const Block *from,
56  const std::vector<std::pair<TaggedType, Block *>> &branches
57  );
58 
59 private:
61  void RefineUpdate(Ref<Inst> inst, const TaggedType &type);
63  void DefineUpdate(Ref<Inst> inst, const TaggedType &type);
65  void Queue(Ref<Inst> inst);
66 
67 private:
69  void RefineAddr(Inst &inst, Ref<Inst> addr);
71  void RefineInt(Inst &inst, Ref<Inst> addr);
73  void RefineFunc(Inst &inst, Ref<Inst> addr);
74 
75 private:
76  void VisitMemoryLoadInst(MemoryLoadInst &i) override;
77  void VisitMemoryStoreInst(MemoryStoreInst &i) override;
78  void VisitSelectInst(SelectInst &i) override;
79  void VisitSubInst(SubInst &i) override;
80  void VisitAddInst(AddInst &i) override;
81  void VisitAndInst(AndInst &i) override;
82  void VisitOrInst(OrInst &i) override;
83  void VisitXorInst(XorInst &i) override;
84  void VisitCmpInst(CmpInst &i) override;
85  void VisitMovInst(MovInst &i) override;
86  void VisitPhiInst(PhiInst &phi) override;
87  void VisitArgInst(ArgInst &arg) override;
88  void VisitCallSite(CallSite &site) override;
89  void VisitJumpCondInst(JumpCondInst &site) override;
90  void VisitInst(Inst &i) override {}
91 
92 private:
94  void RefineEquality(
95  Ref<Inst> lhs,
96  Ref<Inst> rhs,
97  Block *b,
98  Block *bt,
99  Block *bf
100  );
102  void RefineInequality(
103  Ref<Inst> lhs,
104  Ref<Inst> rhs,
105  Block *b,
106  Block *bt,
107  Block *bf
108  );
110  void RefineAndOne(Ref<Inst> arg, Block *b, Block *bt, Block *bf);
111 
112 private:
114  std::pair<std::set<Block *>, std::set<Block *>>
115  Liveness(
116  Ref<Inst> ref,
117  const llvm::SmallPtrSetImpl<const Block *> &defs
118  );
120  void DefineSplits(
121  DominatorCache &doms,
122  Ref<Inst> ref,
123  const std::unordered_map<const Block *, TaggedType> &splits
124  );
125 
126 private:
128  RegisterAnalysis &analysis_;
130  const Target *target_;
132  bool banPolymorphism_;
134  Func &func_;
136  std::queue<Inst *> queue_;
138  std::unordered_set<Inst *> inQueue_;
139 };
140 
141 } // end namespace
Use
Definition: use.h:18
Inst
Definition: inst.h:53
Func
Definition: func.h:30
Target
Definition: target.h:24
MovInst
Definition: mov.h:17
tags::Refinement
Definition: refinement.h:25
tags::RegisterAnalysis
Definition: register_analysis.h:37
InstVisitor
Definition: inst_visitor.h:15
tags::DominatorCache
Cache of dominator/post-dominator trees and frontiers.
Definition: register_analysis.h:24
Ref
Definition: ref.h:63
Queue
Definition: queue.h:15
Block
Definition: block.h:29
tags::TaggedType
Definition: tagged_type.h:17
PhiInst
Definition: phi.h:14