llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
solver.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 #include <set>
6 #include <queue>
7 
8 #include "core/block.h"
9 #include "core/cast.h"
10 #include "core/constant.h"
11 #include "core/func.h"
12 #include "core/inst_visitor.h"
13 #include "core/insts.h"
14 #include "core/prog.h"
15 #include "passes/sccp/lattice.h"
16 
17 class Target;
18 
19 
20 
24 class SCCPSolver : InstVisitor<void> {
25 public:
27  SCCPSolver(Prog &prog, const Target *target);
28 
30  Lattice &GetValue(Ref<Inst> inst);
32  bool IsExecutable(const Block &block) { return executable_.count(&block); }
33 
34 private:
36  void Visit(Block *block)
37  {
38  for (auto &inst : *block) {
39  Visit(inst);
40  }
41  }
42 
44  void Visit(Inst &inst)
45  {
46  assert(executable_.count(inst.getParent()) && "bb not yet visited");
47  Dispatch(inst);
48  }
49 
51  bool CanEvaluate(CallSite &site);
53  void MarkOverdefinedCall(TailCallInst &site);
55  void MarkCall(CallSite &site, Func &callee, Block *cont);
56 
58  bool MarkBlock(Block *block);
60  bool MarkEdge(Inst &inst, Block *to);
62  bool MarkOverdefined(Inst &inst)
63  {
64  bool changed = false;
65  for (unsigned i = 0, n = inst.GetNumRets(); i < n; ++i) {
66  changed |= Mark(inst.GetSubValue(i), Lattice::Overdefined());
67  }
68  return changed;
69  }
71  bool Mark(Ref<Inst> inst, const Lattice &value);
73  bool Mark(Ref<Inst> inst, bool flag);
74 
75 private:
76  void VisitArgInst(ArgInst &inst) override;
77  void VisitCallInst(CallInst &inst) override;
78  void VisitTailCallInst(TailCallInst &inst) override;
79  void VisitInvokeInst(InvokeInst &inst) override;
80  void VisitReturnInst(ReturnInst &inst) override;
81  void VisitLoadInst(LoadInst &inst) override;
82  void VisitBinaryInst(BinaryInst &inst) override;
83  void VisitCmpInst(CmpInst &inst) override;
84  void VisitUnaryInst(UnaryInst &inst) override;
85  void VisitJumpInst(JumpInst &inst) override;
86  void VisitJumpCondInst(JumpCondInst &inst) override;
87  void VisitSwitchInst(SwitchInst &inst) override;
88  void VisitSelectInst(SelectInst &inst) override;
89  void VisitFrameInst(FrameInst &inst) override;
90  void VisitMovInst(MovInst &inst) override;
91  void VisitUndefInst(UndefInst &inst) override;
92  void VisitPhiInst(PhiInst &inst) override;
93  void VisitInst(Inst &inst) override;
94 
95  void VisitX86_CpuIdInst(X86_CpuIdInst &inst) override;
96 
97 private:
99  const Target *target_;
100 
102  std::queue<Inst *> bottomList_;
104  std::queue<Block *> blockList_;
106  std::queue<Inst *> instList_;
107 
109  std::unordered_map<Ref<Inst>, Lattice> values_;
111  std::set<std::pair<Block *, Block *>> edges_;
113  std::set<const Block *> executable_;
115  std::map<const Func *, std::map<unsigned, std::set<ArgInst *>>> args_;
117  std::map<const Func *, std::set<std::pair<CallSite *, Block *>>> calls_;
119  using ResultMap = std::map<unsigned, std::pair<Type, Lattice>>;
121  std::map<const Func *, ResultMap> returns_;
122 };
Inst
Definition: inst.h:53
Func
Definition: func.h:30
Lattice
Definition: lattice.h:22
Target
Definition: target.h:24
Inst::GetSubValue
Ref< Inst > GetSubValue(unsigned i)
Returns the ith sub-value.
Definition: inst.h:138
SCCPSolver::SCCPSolver
SCCPSolver(Prog &prog, const Target *target)
Solves constraints for the whole program.
Definition: solver.cpp:26
MovInst
Definition: mov.h:17
Prog
Definition: prog.h:33
Inst::getParent
Block * getParent() const
Returns the parent node.
Definition: inst.h:86
InstVisitor
Definition: inst_visitor.h:15
Ref
Definition: ref.h:63
Lattice::Overdefined
static Lattice Overdefined()
Creates an overdefined value.
Definition: lattice.cpp:333
SCCPSolver::GetValue
Lattice & GetValue(Ref< Inst > inst)
Returns a lattice value.
Definition: solver.cpp:158
Block
Definition: block.h:29
Inst::GetNumRets
virtual unsigned GetNumRets() const
Returns the number of returned values.
Definition: inst.h:88
SCCPSolver::IsExecutable
bool IsExecutable(const Block &block)
Checks if a block is executable.
Definition: solver.h:32
PhiInst
Definition: phi.h:14
SCCPSolver
Definition: solver.h:24