llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
prog_reducer.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 <random>
8 #include <queue>
9 
10 #include "core/inst_visitor.h"
11 #include "core/insts.h"
12 #include "timeout.h"
13 
14 class Func;
15 
16 template <typename T>
17 using Iterator = std::optional<std::pair<std::unique_ptr<Prog>, T *>>;
18 
19 
20 
24 class ProgReducerBase : public InstVisitor<Iterator<Inst>> {
25 public:
27  ProgReducerBase(unsigned threads) : threads_(threads) {}
28 
30  std::unique_ptr<Prog> Reduce(
31  std::unique_ptr<Prog> &&prog,
32  const Timeout &Timeout
33  );
34 
36  virtual bool Verify(const Prog &prog) const = 0;
37 
38 private:
39  using It = Iterator<Inst>;
40  using Bt = Iterator<Block>;
41  using At = Iterator<Atom>;
42  using Ft = Iterator<Func>;
43 
45  It ReduceInst(Inst *i) { return Dispatch(*i); }
47  Bt ReduceBlock(Block *b);
49  At ReduceAtom(Atom *a);
51  Ft ReduceFunc(Func *f);
52 
54  It VisitArg(ArgInst &i);
56  It VisitCall(CallInst &i);
58  It VisitInvoke(InvokeInst &i);
60  It VisitTailCall(TailCallInst &i);
62  It VisitSyscall(SyscallInst &i);
64  It VisitClone(CloneInst &i);
66  It VisitRaise(RaiseInst &i);
68  It VisitStore(StoreInst &i);
70  It VisitMov(MovInst &i);
72  It VisitSwitch(SwitchInst &i);
74  It VisitJmp(JumpInst &i);
76  It VisitJcc(JumpCondInst &i);
78  It VisitReturn(ReturnInst &i);
80  It VisitPhi(PhiInst &i);
82  It VisitUndef(UndefInst &i);
84  It VisitVAStart(VaStartInst &i);
86  It VisitSet(SetInst &i);
88  It VisitAlloca(AllocaInst &i) { return ReduceOperator(i); }
90  It VisitFrame(FrameInst &i) { return ReduceOperator(i); }
92  It VisitLoad(LoadInst &i) { return ReduceOperator(i); }
94  It VisitUnary(UnaryInst &i) { return ReduceOperator(i); }
96  It VisitBinary(BinaryInst &i) { return ReduceOperator(i); }
98  It VisitSelect(SelectInst &i) { return ReduceOperator(i); }
100  It VisitXchg(X86_XchgInst &i) { return ReduceOperator(i); }
102  It VisitCmpXchg(X86_CmpXchgInst &i) { return ReduceOperator(i); }
104  It VisitX86_Rdtsc(X86_RdTscInst &i) { return ReduceOperator(i); }
106  It VisitX86_FPUControlInst(X86_FPUControlInst &i);
108  It VisitInst(Inst &i);
109 
111  It ReduceOperator(Inst &i);
112 
113  using Candidate = std::pair<std::unique_ptr<Prog>, Inst *>;
114  using CandidateList = std::queue<Candidate>;
115 
117  template <typename T>
118  void RemoveArg(CandidateList &cand, T &i);
120  void ReduceToOp(CandidateList &cand, Inst &i);
122  void ReduceToRet(CandidateList &cand, Inst &i);
124  void ReduceToTrap(CandidateList &cand, Inst &i);
126  void ReduceToUndef(CandidateList &cand, Inst &i);
128  void ReduceZero(CandidateList &cand, Inst &i);
130  void ReduceErase(CandidateList &cand, Inst &i);
132  void ReduceToArg(CandidateList &cand, Inst &i);
134  void ReduceOperator(CandidateList &cand, Inst &i);
135 
137  It Evaluate(CandidateList &&cand);
138 
140  void RemoveEdge(Block *from, Block *to);
141 
143  Constant *GetZero(Type type);
144 
145 private:
147  unsigned threads_;
148 };
Inst
Definition: inst.h:53
Func
Definition: func.h:30
ProgReducerBase::Verify
virtual bool Verify(const Prog &prog) const =0
Verifies a program.
Atom
Definition: atom.h:23
Constant
Definition: constant.h:21
ProgReducerBase
Definition: prog_reducer.h:24
MovInst
Definition: mov.h:17
Prog
Definition: prog.h:33
InstVisitor
Definition: inst_visitor.h:15
ProgReducerBase::ProgReducerBase
ProgReducerBase(unsigned threads)
Initialises the pass.
Definition: prog_reducer.h:27
Timeout
Definition: timeout.h:14
Block
Definition: block.h:29
PhiInst
Definition: phi.h:14
ProgReducerBase::Reduce
std::unique_ptr< Prog > Reduce(std::unique_ptr< Prog > &&prog, const Timeout &Timeout)
Runs the pass.
Definition: prog_reducer.cpp:96