llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
register_analysis.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 <unordered_set>
9 #include <unordered_map>
10 #include <llvm/Support/raw_ostream.h>
11 
12 #include "core/inst_visitor.h"
13 #include "core/target.h"
14 #include "core/analysis/dominator.h"
15 #include "passes/tags/tagged_type.h"
16 
17 
18 
19 namespace tags {
20 class Init;
21 class Step;
22 
33 
34  DominatorCache(Func &func);
35 };
36 
38 public:
39  RegisterAnalysis(Prog &prog, const Target *target, bool banPolymorphism)
40  : prog_(prog)
41  , target_(target)
42  , banPolymorphism_(banPolymorphism)
43  {
44  Solve();
45  }
46 
47  void Solve();
48 
50  void dump(llvm::raw_ostream &os = llvm::errs());
51 
54  {
55  auto it = types_.find(ref);
56  return it == types_.end() ? TaggedType::Unknown() : it->second;
57  }
58 
60  void Replace(Ref<Inst> oldInst, Ref<Inst> newInst, const TaggedType &type);
62  void Replace(Inst *oldInst, Inst *newInst);
64  void Erase(Ref<Inst> oldInst);
65 
66 private:
67  friend class Init;
68  friend class Step;
69  friend class Refinement;
70  friend class ConstraintSolver;
71 
73  bool Mark(Ref<Inst> inst, const TaggedType &type);
75  bool Mark(Inst &inst, const TaggedType &type)
76  {
77  return Mark(inst.GetSubValue(0), type);
78  }
79 
81  bool Define(Ref<Inst> inst, const TaggedType &type);
83  bool Refine(Ref<Inst> inst, const TaggedType &type);
85  bool Refine(Inst &inst, const TaggedType &type)
86  {
87  return Refine(inst.GetSubValue(0), type);
88  }
90  bool IsDefined(Ref<Inst> inst)
91  {
92  return defs_.count(inst) != 0;
93  }
94 
96  void ForwardQueue(Ref<Inst> inst);
98  void BackwardQueue(Ref<Inst > inst);
99 
101  static bool IsPolymorphic(const Inst &inst);
102 
103 private:
105  DominatorCache &GetDoms(Func &func)
106  {
107  auto it = doms_.emplace(&func, nullptr);
108  if (it.second) {
109  it.first->second.reset(new DominatorCache(func));
110  }
111  return *it.first->second;
112  }
114  DominatorCache &RebuildDoms(Func &func)
115  {
116  auto it = doms_.emplace(&func, nullptr);
117  it.first->second.reset(new DominatorCache(func));
118  return *it.first->second;
119  }
120 
121 private:
123  Prog &prog_;
125  const Target *target_;
127  bool banPolymorphism_;
129  std::queue<Inst *> forwardQueue_;
131  std::queue<PhiInst *> forwardPhiQueue_;
133  std::unordered_set<Inst *> inForwardQueue_;
135  std::queue<Func *> backwardQueue_;
137  std::unordered_set<Func *> inBackwardQueue_;
139  std::queue<Inst *> refineQueue_;
141  std::unordered_set<Inst *> inRefineQueue_;
143  std::unordered_map<ConstRef<Inst>, TaggedType> types_;
145  std::unordered_map
146  < std::pair<const Func *, unsigned>
147  , std::vector<ArgInst *>
148  > args_;
150  std::unordered_map<const Func *, std::vector<TaggedType>> rets_;
152  std::unordered_map<Func *, std::unique_ptr<DominatorCache>> doms_;
154  std::unordered_set<Ref<Inst>> defs_;
155 };
156 
157 } // end namespace
Inst
Definition: inst.h:53
tags::RegisterAnalysis::Erase
void Erase(Ref< Inst > oldInst)
Erase a type after deleting an instruction.
Definition: register_analysis.cpp:35
Func
Definition: func.h:30
ConstRef< Inst >
tags::DominatorCache::DT
DominatorTree DT
Dominator tree.
Definition: register_analysis.h:26
PostDominatorTree
Definition: dominator.h:55
Target
Definition: target.h:24
tags::RegisterAnalysis::dump
void dump(llvm::raw_ostream &os=llvm::errs())
Dump the results of the analysis.
Definition: register_analysis.cpp:239
Inst::GetSubValue
Ref< Inst > GetSubValue(unsigned i)
Returns the ith sub-value.
Definition: inst.h:138
tags::Step
Definition: step.h:20
PostDominanceFrontier
Definition: dominator.h:98
tags::DominatorCache::PDT
PostDominatorTree PDT
Post-Dominator Tree.
Definition: register_analysis.h:30
tags::RegisterAnalysis::Replace
void Replace(Ref< Inst > oldInst, Ref< Inst > newInst, const TaggedType &type)
Set the type, typically after rewriting an instruction.
Definition: register_analysis.cpp:45
tags::Init
Definition: init.h:22
tags::DominatorCache::PDF
PostDominanceFrontier PDF
Post-Dominance Frontier.
Definition: register_analysis.h:32
tags::DominatorCache::DF
DominanceFrontier DF
Dominance frontier.
Definition: register_analysis.h:28
tags::Refinement
Definition: refinement.h:25
tags::RegisterAnalysis::Find
TaggedType Find(ConstRef< Inst > ref)
Find the type assigned to a vreg.
Definition: register_analysis.h:53
tags::RegisterAnalysis
Definition: register_analysis.h:37
DominanceFrontier
Definition: dominator.h:89
Prog
Definition: prog.h:33
tags::ConstraintSolver
Definition: constraints.h:27
tags::DominatorCache
Cache of dominator/post-dominator trees and frontiers.
Definition: register_analysis.h:24
Ref
Definition: ref.h:63
tags::TaggedType
Definition: tagged_type.h:17
DominatorTree
Definition: dominator.h:22