1// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_COMPILER_COMPILER_PASS_H_
6#define RUNTIME_VM_COMPILER_COMPILER_PASS_H_
7
8#if defined(DART_PRECOMPILED_RUNTIME)
9#error "AOT runtime should not use compiler sources (including header files)"
10#endif // defined(DART_PRECOMPILED_RUNTIME)
11
12#include <initializer_list>
13
14#include "vm/growable_array.h"
15#include "vm/timer.h"
16#include "vm/token_position.h"
17#include "vm/zone.h"
18
19namespace dart {
20
21#define COMPILER_PASS_LIST(V) \
22 V(AllocateRegisters) \
23 V(AllocateRegistersForGraphIntrinsic) \
24 V(AllocationSinking_DetachMaterializations) \
25 V(AllocationSinking_Sink) \
26 V(ApplyClassIds) \
27 V(ApplyICData) \
28 V(BranchSimplify) \
29 V(CSE) \
30 V(Canonicalize) \
31 V(ComputeSSA) \
32 V(ConstantPropagation) \
33 V(DCE) \
34 V(DelayAllocations) \
35 V(DSE) \
36 V(EliminateDeadPhis) \
37 V(EliminateEnvironments) \
38 V(EliminateStackOverflowChecks) \
39 V(FinalizeGraph) \
40 V(IfConvert) \
41 V(Inlining) \
42 V(LICM) \
43 V(OptimisticallySpecializeSmiPhis) \
44 V(OptimizeBranches) \
45 V(OptimizeTypedDataAccesses) \
46 V(RangeAnalysis) \
47 V(ReorderBlocks) \
48 V(SelectRepresentations) \
49 V(SelectRepresentations_Final) \
50 V(SetOuterInliningId) \
51 V(TryCatchOptimization) \
52 V(TryOptimizePatterns) \
53 V(TypePropagation) \
54 V(UseTableDispatch) \
55 V(WidenSmiToInt32) \
56 V(EliminateWriteBarriers) \
57 V(GenerateCode)
58
59class AllocationSinking;
60class BlockScheduler;
61class CallSpecializer;
62class FlowGraph;
63class FlowGraphCompiler;
64class Function;
65class Precompiler;
66class SpeculativeInliningPolicy;
67class TimelineStream;
68class Thread;
69
70struct CompilerPassState {
71 CompilerPassState(Thread* thread,
72 FlowGraph* flow_graph,
73 SpeculativeInliningPolicy* speculative_policy,
74 Precompiler* precompiler = nullptr);
75
76 FlowGraph* flow_graph() const { return flow_graph_; }
77
78 void set_flow_graph(FlowGraph* flow_graph);
79
80 Thread* const thread;
81 Precompiler* const precompiler;
82 int inlining_depth;
83 AllocationSinking* sinking;
84
85 // Maps inline_id_to_function[inline_id] -> function. Top scope
86 // function has inline_id 0. The map is populated by the inliner.
87 GrowableArray<const Function*> inline_id_to_function;
88 // Token position where inlining occurred.
89 GrowableArray<TokenPosition> inline_id_to_token_pos;
90 // For a given inlining-id(index) specifies the caller's inlining-id.
91 GrowableArray<intptr_t> caller_inline_id;
92
93 CallSpecializer* call_specializer;
94
95 SpeculativeInliningPolicy* speculative_policy;
96
97 bool reorder_blocks;
98
99 intptr_t sticky_flags;
100
101 FlowGraphCompiler* graph_compiler = nullptr;
102
103 private:
104 FlowGraph* flow_graph_;
105};
106
107class CompilerPass {
108 public:
109 enum Id {
110#define DEF(name) k##name,
111 COMPILER_PASS_LIST(DEF)
112#undef DEF
113 };
114
115#define ADD_ONE(name) +1
116 static constexpr intptr_t kNumPasses = 0 COMPILER_PASS_LIST(ADD_ONE);
117#undef ADD_ONE
118
119 CompilerPass(Id id, const char* name) : id_(id), name_(name) {
120 ASSERT(passes_[id] == nullptr);
121 passes_[id] = this;
122
123 // By default print the final flow-graph after the register allocation.
124 if (id == kAllocateRegisters) {
125 flags_[id] = kTraceAfter;
126 } else {
127 flags_[id] = 0;
128 }
129 }
130 virtual ~CompilerPass() {}
131
132 enum Flag {
133 kDisabled = 1 << 0,
134 kTraceBefore = 1 << 1,
135 kTraceAfter = 1 << 2,
136 kSticky = 1 << 3,
137 kTraceBeforeOrAfter = kTraceBefore | kTraceAfter,
138 };
139
140 void Run(CompilerPassState* state) const;
141
142 uint8_t flags() const { return flags_[id()]; }
143 const char* name() const { return name_; }
144 Id id() const { return id_; }
145
146 static CompilerPass* Get(Id id) { return passes_[id]; }
147
148 static void ParseFiltersFromFlag(const char* filter);
149 static uint8_t* ParseFiltersFromPragma(const char* filter);
150 static void ParseFilters(const char* filter, uint8_t* flags);
151 static void ParseOneFilter(const char* start,
152 const char* end,
153 uint8_t* flags);
154
155 enum PipelineMode { kJIT, kAOT };
156
157 static void GenerateCode(CompilerPassState* state) {
158 CompilerPass::Get(id: CompilerPass::kGenerateCode)->Run(state);
159 }
160
161 static void RunGraphIntrinsicPipeline(CompilerPassState* state);
162
163 static void RunInliningPipeline(PipelineMode mode, CompilerPassState* state);
164
165 // RunPipeline(WithPasses) may have the side effect of changing the FlowGraph
166 // stored in the CompilerPassState. However, existing callers may depend on
167 // the old invariant that the FlowGraph stored in the CompilerPassState was
168 // always updated, never entirely replaced.
169 //
170 // To make sure callers are updated properly, these methods also return
171 // the final FlowGraph and we add a check that callers use this result.
172 DART_WARN_UNUSED_RESULT
173 static FlowGraph* RunPipeline(PipelineMode mode, CompilerPassState* state);
174 DART_WARN_UNUSED_RESULT
175 static FlowGraph* RunPipelineWithPasses(
176 CompilerPassState* state,
177 std::initializer_list<CompilerPass::Id> passes);
178
179 // Pipeline which is used for "force-optimized" functions.
180 //
181 // Must not include speculative or inter-procedural optimizations.
182 DART_WARN_UNUSED_RESULT
183 static FlowGraph* RunForceOptimizedPipeline(PipelineMode mode,
184 CompilerPassState* state);
185
186 protected:
187 // This function executes the pass. If it returns true then
188 // we will run Canonicalize on the graph and execute the pass
189 // again.
190 virtual bool DoBody(CompilerPassState* state) const = 0;
191
192 private:
193 static CompilerPass* FindPassByName(const char* name) {
194 for (intptr_t i = 0; i < kNumPasses; i++) {
195 if ((passes_[i] != nullptr) && (strcmp(passes_[i]->name_, name) == 0)) {
196 return passes_[i];
197 }
198 }
199 return nullptr;
200 }
201
202 void PrintGraph(CompilerPassState* state, Flag mask, intptr_t round) const;
203
204 static CompilerPass* passes_[];
205 static uint8_t flags_[];
206
207 Id id_;
208 const char* name_;
209};
210
211} // namespace dart
212
213#endif // RUNTIME_VM_COMPILER_COMPILER_PASS_H_
214

source code of dart_sdk/runtime/vm/compiler/compiler_pass.h