blob: a97eda7e855043d18e6bccdbd1ef5536a4f67382 [file] [log] [blame]
Alexandre Rames22aa54b2016-10-18 09:32:29 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_SCHEDULER_H_
18#define ART_COMPILER_OPTIMIZING_SCHEDULER_H_
19
20#include <fstream>
21
Vladimir Markoca6fff82017-10-03 14:49:14 +010022#include "base/scoped_arena_allocator.h"
23#include "base/scoped_arena_containers.h"
Evgeny Astigeevich957c5382019-03-18 12:37:58 +000024#include "base/stl_util.h"
Alexandre Rames22aa54b2016-10-18 09:32:29 +010025#include "base/time_utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "code_generator.h"
xueliang.zhong2a3471f2017-05-08 18:36:40 +010027#include "load_store_analysis.h"
Alexandre Rames22aa54b2016-10-18 09:32:29 +010028#include "nodes.h"
29#include "optimization.h"
30
31namespace art {
32
33// General description of instruction scheduling.
34//
35// This pass tries to improve the quality of the generated code by reordering
36// instructions in the graph to avoid execution delays caused by execution
37// dependencies.
38// Currently, scheduling is performed at the block level, so no `HInstruction`
39// ever leaves its block in this pass.
40//
41// The scheduling process iterates through blocks in the graph. For blocks that
42// we can and want to schedule:
43// 1) Build a dependency graph for instructions.
44// It includes data dependencies (inputs/uses), but also environment
45// dependencies and side-effect dependencies.
46// 2) Schedule the dependency graph.
47// This is a topological sort of the dependency graph, using heuristics to
48// decide what node to scheduler first when there are multiple candidates.
49//
50// A few factors impacting the quality of the scheduling are:
51// - The heuristics used to decide what node to schedule in the topological sort
52// when there are multiple valid candidates. There is a wide range of
53// complexity possible here, going from a simple model only considering
54// latencies, to a super detailed CPU pipeline model.
55// - Fewer dependencies in the dependency graph give more freedom for the
56// scheduling heuristics. For example de-aliasing can allow possibilities for
57// reordering of memory accesses.
58// - The level of abstraction of the IR. It is easier to evaluate scheduling for
59// IRs that translate to a single assembly instruction than for IRs
60// that generate multiple assembly instructions or generate different code
61// depending on properties of the IR.
62// - Scheduling is performed before register allocation, it is not aware of the
63// impact of moving instructions on register allocation.
64//
65//
66// The scheduling code uses the terms predecessors, successors, and dependencies.
67// This can be confusing at times, so here are clarifications.
68// These terms are used from the point of view of the program dependency graph. So
69// the inputs of an instruction are part of its dependencies, and hence part its
70// predecessors. So the uses of an instruction are (part of) its successors.
71// (Side-effect dependencies can yield predecessors or successors that are not
72// inputs or uses.)
73//
74// Here is a trivial example. For the Java code:
75//
76// int a = 1 + 2;
77//
78// we would have the instructions
79//
80// i1 HIntConstant 1
81// i2 HIntConstant 2
82// i3 HAdd [i1,i2]
83//
84// `i1` and `i2` are predecessors of `i3`.
85// `i3` is a successor of `i1` and a successor of `i2`.
86// In a scheduling graph for this code we would have three nodes `n1`, `n2`,
87// and `n3` (respectively for instructions `i1`, `i1`, and `i3`).
88// Conceptually the program dependency graph for this would contain two edges
89//
90// n1 -> n3
91// n2 -> n3
92//
93// Since we schedule backwards (starting from the last instruction in each basic
94// block), the implementation of nodes keeps a list of pointers their
95// predecessors. So `n3` would keep pointers to its predecessors `n1` and `n2`.
96//
97// Node dependencies are also referred to from the program dependency graph
98// point of view: we say that node `B` immediately depends on `A` if there is an
99// edge from `A` to `B` in the program dependency graph. `A` is a predecessor of
100// `B`, `B` is a successor of `A`. In the example above `n3` depends on `n1` and
101// `n2`.
102// Since nodes in the scheduling graph keep a list of their predecessors, node
103// `B` will have a pointer to its predecessor `A`.
104// As we schedule backwards, `B` will be selected for scheduling before `A` is.
105//
106// So the scheduling for the example above could happen as follow
107//
108// |---------------------------+------------------------|
109// | candidates for scheduling | instructions scheduled |
110// | --------------------------+------------------------|
111//
112// The only node without successors is `n3`, so it is the only initial
113// candidate.
114//
115// | n3 | (none) |
116//
117// We schedule `n3` as the last (and only) instruction. All its predecessors
118// that do not have any unscheduled successors become candidate. That is, `n1`
119// and `n2` become candidates.
120//
121// | n1, n2 | n3 |
122//
123// One of the candidates is selected. In practice this is where scheduling
124// heuristics kick in, to decide which of the candidates should be selected.
125// In this example, let it be `n1`. It is scheduled before previously scheduled
126// nodes (in program order). There are no other nodes to add to the list of
127// candidates.
128//
129// | n2 | n1 |
130// | | n3 |
131//
132// The only candidate available for scheduling is `n2`. Schedule it before
133// (in program order) the previously scheduled nodes.
134//
135// | (none) | n2 |
136// | | n1 |
137// | | n3 |
138// |---------------------------+------------------------|
139//
140// So finally the instructions will be executed in the order `i2`, `i1`, and `i3`.
141// In this trivial example, it does not matter which of `i1` and `i2` is
142// scheduled first since they are constants. However the same process would
143// apply if `i1` and `i2` were actual operations (for example `HMul` and `HDiv`).
144
145// Set to true to have instruction scheduling dump scheduling graphs to the file
146// `scheduling_graphs.dot`. See `SchedulingGraph::DumpAsDotGraph()`.
147static constexpr bool kDumpDotSchedulingGraphs = false;
148
149// Typically used as a default instruction latency.
150static constexpr uint32_t kGenericInstructionLatency = 1;
151
152class HScheduler;
153
154/**
155 * A node representing an `HInstruction` in the `SchedulingGraph`.
156 */
Vladimir Markoca6fff82017-10-03 14:49:14 +0100157class SchedulingNode : public DeletableArenaObject<kArenaAllocScheduler> {
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100158 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100159 SchedulingNode(HInstruction* instr, ScopedArenaAllocator* allocator, bool is_scheduling_barrier)
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100160 : latency_(0),
161 internal_latency_(0),
162 critical_path_(0),
163 instruction_(instr),
164 is_scheduling_barrier_(is_scheduling_barrier),
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100165 data_predecessors_(allocator->Adapter(kArenaAllocScheduler)),
166 other_predecessors_(allocator->Adapter(kArenaAllocScheduler)),
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100167 num_unscheduled_successors_(0) {
168 data_predecessors_.reserve(kPreallocatedPredecessors);
169 }
170
171 void AddDataPredecessor(SchedulingNode* predecessor) {
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000172 // Check whether the predecessor has been added earlier.
173 if (HasDataDependency(predecessor)) {
174 return;
175 }
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100176 data_predecessors_.push_back(predecessor);
177 predecessor->num_unscheduled_successors_++;
178 }
179
Vladimir Markoca6fff82017-10-03 14:49:14 +0100180 const ScopedArenaVector<SchedulingNode*>& GetDataPredecessors() const {
181 return data_predecessors_;
182 }
183
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100184 void AddOtherPredecessor(SchedulingNode* predecessor) {
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000185 // Check whether the predecessor has been added earlier.
186 if (HasOtherDependency(predecessor)) {
187 return;
188 }
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100189 other_predecessors_.push_back(predecessor);
190 predecessor->num_unscheduled_successors_++;
191 }
192
Vladimir Markoca6fff82017-10-03 14:49:14 +0100193 const ScopedArenaVector<SchedulingNode*>& GetOtherPredecessors() const {
194 return other_predecessors_;
195 }
196
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100197 void DecrementNumberOfUnscheduledSuccessors() {
198 num_unscheduled_successors_--;
199 }
200
201 void MaybeUpdateCriticalPath(uint32_t other_critical_path) {
202 critical_path_ = std::max(critical_path_, other_critical_path);
203 }
204
205 bool HasUnscheduledSuccessors() const {
206 return num_unscheduled_successors_ != 0;
207 }
208
209 HInstruction* GetInstruction() const { return instruction_; }
210 uint32_t GetLatency() const { return latency_; }
211 void SetLatency(uint32_t latency) { latency_ = latency; }
212 uint32_t GetInternalLatency() const { return internal_latency_; }
213 void SetInternalLatency(uint32_t internal_latency) { internal_latency_ = internal_latency; }
214 uint32_t GetCriticalPath() const { return critical_path_; }
215 bool IsSchedulingBarrier() const { return is_scheduling_barrier_; }
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100216
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000217 bool HasDataDependency(const SchedulingNode* node) const {
218 return ContainsElement(data_predecessors_, node);
219 }
220
221 bool HasOtherDependency(const SchedulingNode* node) const {
222 return ContainsElement(other_predecessors_, node);
223 }
224
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100225 private:
226 // The latency of this node. It represents the latency between the moment the
227 // last instruction for this node has executed to the moment the result
228 // produced by this node is available to users.
229 uint32_t latency_;
230 // This represents the time spent *within* the generated code for this node.
231 // It should be zero for nodes that only generate a single instruction.
232 uint32_t internal_latency_;
233
234 // The critical path from this instruction to the end of scheduling. It is
235 // used by the scheduling heuristics to measure the priority of this instruction.
236 // It is defined as
237 // critical_path_ = latency_ + max((use.internal_latency_ + use.critical_path_) for all uses)
238 // (Note that here 'uses' is equivalent to 'data successors'. Also see comments in
239 // `HScheduler::Schedule(SchedulingNode* scheduling_node)`).
240 uint32_t critical_path_;
241
242 // The instruction that this node represents.
243 HInstruction* const instruction_;
244
245 // If a node is scheduling barrier, other nodes cannot be scheduled before it.
246 const bool is_scheduling_barrier_;
247
248 // The lists of predecessors. They cannot be scheduled before this node. Once
249 // this node is scheduled, we check whether any of its predecessors has become a
250 // valid candidate for scheduling.
251 // Predecessors in `data_predecessors_` are data dependencies. Those in
252 // `other_predecessors_` contain side-effect dependencies, environment
253 // dependencies, and scheduling barrier dependencies.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100254 ScopedArenaVector<SchedulingNode*> data_predecessors_;
255 ScopedArenaVector<SchedulingNode*> other_predecessors_;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100256
257 // The number of unscheduled successors for this node. This number is
258 // decremented as successors are scheduled. When it reaches zero this node
259 // becomes a valid candidate to schedule.
260 uint32_t num_unscheduled_successors_;
261
262 static constexpr size_t kPreallocatedPredecessors = 4;
263};
264
265/*
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000266 * Provide analysis of instruction dependencies (side effects) which are not in a form of explicit
267 * def-use data dependencies.
268 */
269class SideEffectDependencyAnalysis {
270 public:
271 explicit SideEffectDependencyAnalysis(const HeapLocationCollector* heap_location_collector)
272 : memory_dependency_analysis_(heap_location_collector) {}
273
274 bool HasSideEffectDependency(HInstruction* instr1, HInstruction* instr2) const {
275 if (memory_dependency_analysis_.HasMemoryDependency(instr1, instr2)) {
276 return true;
277 }
278
279 // Even if above memory dependency check has passed, it is still necessary to
280 // check dependencies between instructions that can throw and instructions
281 // that write to memory.
282 if (HasExceptionDependency(instr1, instr2)) {
283 return true;
284 }
285
286 return false;
287 }
288
289 private:
290 static bool HasExceptionDependency(const HInstruction* instr1, const HInstruction* instr2);
291 static bool HasReorderingDependency(const HInstruction* instr1, const HInstruction* instr2);
292
293 /*
294 * Memory dependency analysis of instructions based on their memory side effects
295 * and heap location information from the LCA pass if it is provided.
296 */
297 class MemoryDependencyAnalysis {
298 public:
299 explicit MemoryDependencyAnalysis(const HeapLocationCollector* heap_location_collector)
300 : heap_location_collector_(heap_location_collector) {}
301
302 bool HasMemoryDependency(HInstruction* instr1, HInstruction* instr2) const;
303
304 private:
305 bool ArrayAccessMayAlias(HInstruction* instr1, HInstruction* instr2) const;
306 bool FieldAccessMayAlias(const HInstruction* instr1, const HInstruction* instr2) const;
307 size_t ArrayAccessHeapLocation(HInstruction* instruction) const;
308 size_t FieldAccessHeapLocation(const HInstruction* instruction) const;
309
310 const HeapLocationCollector* const heap_location_collector_;
311 };
312
313 MemoryDependencyAnalysis memory_dependency_analysis_;
314};
315
316/*
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100317 * Directed acyclic graph for scheduling.
318 */
319class SchedulingGraph : public ValueObject {
320 public:
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000321 SchedulingGraph(ScopedArenaAllocator* allocator,
Vladimir Markoced04832018-07-26 14:42:17 +0100322 const HeapLocationCollector* heap_location_collector)
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000323 : allocator_(allocator),
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100324 contains_scheduling_barrier_(false),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100325 nodes_map_(allocator_->Adapter(kArenaAllocScheduler)),
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000326 side_effect_dependency_analysis_(heap_location_collector) {}
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100327
328 SchedulingNode* AddNode(HInstruction* instr, bool is_scheduling_barrier = false) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100329 std::unique_ptr<SchedulingNode> node(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100330 new (allocator_) SchedulingNode(instr, allocator_, is_scheduling_barrier));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100331 SchedulingNode* result = node.get();
Vladimir Marko54159c62018-06-20 14:30:08 +0100332 nodes_map_.insert(std::make_pair(instr, std::move(node)));
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100333 contains_scheduling_barrier_ |= is_scheduling_barrier;
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000334 AddDependencies(result, is_scheduling_barrier);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100335 return result;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100336 }
337
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100338 SchedulingNode* GetNode(const HInstruction* instr) const {
Vladimir Marko54159c62018-06-20 14:30:08 +0100339 auto it = nodes_map_.find(instr);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100340 if (it == nodes_map_.end()) {
341 return nullptr;
342 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 return it->second.get();
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100344 }
345 }
346
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100347 size_t Size() const {
Vladimir Marko54159c62018-06-20 14:30:08 +0100348 return nodes_map_.size();
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100349 }
350
351 // Dump the scheduling graph, in dot file format, appending it to the file
352 // `scheduling_graphs.dot`.
353 void DumpAsDotGraph(const std::string& description,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100354 const ScopedArenaVector<SchedulingNode*>& initial_candidates);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100355
356 protected:
357 void AddDependency(SchedulingNode* node, SchedulingNode* dependency, bool is_data_dependency);
358 void AddDataDependency(SchedulingNode* node, SchedulingNode* dependency) {
359 AddDependency(node, dependency, /*is_data_dependency*/true);
360 }
361 void AddOtherDependency(SchedulingNode* node, SchedulingNode* dependency) {
362 AddDependency(node, dependency, /*is_data_dependency*/false);
363 }
364
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000365 // Add dependencies nodes for the given `SchedulingNode`: inputs, environments, and side-effects.
366 void AddDependencies(SchedulingNode* node, bool is_scheduling_barrier = false);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100367
Vladimir Marko69d310e2017-10-09 14:12:23 +0100368 ScopedArenaAllocator* const allocator_;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100369 bool contains_scheduling_barrier_;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100370 ScopedArenaHashMap<const HInstruction*, std::unique_ptr<SchedulingNode>> nodes_map_;
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000371 SideEffectDependencyAnalysis side_effect_dependency_analysis_;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100372};
373
374/*
375 * The visitors derived from this base class are used by schedulers to evaluate
376 * the latencies of `HInstruction`s.
377 */
378class SchedulingLatencyVisitor : public HGraphDelegateVisitor {
379 public:
380 // This class and its sub-classes will never be used to drive a visit of an
381 // `HGraph` but only to visit `HInstructions` one at a time, so we do not need
382 // to pass a valid graph to `HGraphDelegateVisitor()`.
Andreas Gamped9911ee2017-03-27 13:27:24 -0700383 SchedulingLatencyVisitor()
384 : HGraphDelegateVisitor(nullptr),
385 last_visited_latency_(0),
386 last_visited_internal_latency_(0) {}
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100388 void VisitInstruction(HInstruction* instruction) override {
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100389 LOG(FATAL) << "Error visiting " << instruction->DebugName() << ". "
390 "Architecture-specific scheduling latency visitors must handle all instructions"
391 " (potentially by overriding the generic `VisitInstruction()`.";
392 UNREACHABLE();
393 }
394
395 void Visit(HInstruction* instruction) {
396 instruction->Accept(this);
397 }
398
399 void CalculateLatency(SchedulingNode* node) {
400 // By default nodes have no internal latency.
401 last_visited_internal_latency_ = 0;
402 Visit(node->GetInstruction());
403 }
404
405 uint32_t GetLastVisitedLatency() const { return last_visited_latency_; }
406 uint32_t GetLastVisitedInternalLatency() const { return last_visited_internal_latency_; }
407
408 protected:
409 // The latency of the most recent visited SchedulingNode.
410 // This is for reporting the latency value to the user of this visitor.
411 uint32_t last_visited_latency_;
412 // This represents the time spent *within* the generated code for the most recent visited
413 // SchedulingNode. This is for reporting the internal latency value to the user of this visitor.
414 uint32_t last_visited_internal_latency_;
415};
416
417class SchedulingNodeSelector : public ArenaObject<kArenaAllocScheduler> {
418 public:
Vladimir Markoced04832018-07-26 14:42:17 +0100419 virtual void Reset() {}
Vladimir Markoca6fff82017-10-03 14:49:14 +0100420 virtual SchedulingNode* PopHighestPriorityNode(ScopedArenaVector<SchedulingNode*>* nodes,
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100421 const SchedulingGraph& graph) = 0;
422 virtual ~SchedulingNodeSelector() {}
423 protected:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 static void DeleteNodeAtIndex(ScopedArenaVector<SchedulingNode*>* nodes, size_t index) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100425 (*nodes)[index] = nodes->back();
426 nodes->pop_back();
427 }
428};
429
430/*
431 * Select a `SchedulingNode` at random within the candidates.
432 */
433class RandomSchedulingNodeSelector : public SchedulingNodeSelector {
434 public:
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800435 RandomSchedulingNodeSelector() : seed_(0) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100436 seed_ = static_cast<uint32_t>(NanoTime());
437 srand(seed_);
438 }
439
Vladimir Markoca6fff82017-10-03 14:49:14 +0100440 SchedulingNode* PopHighestPriorityNode(ScopedArenaVector<SchedulingNode*>* nodes,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100441 const SchedulingGraph& graph) override {
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100442 UNUSED(graph);
443 DCHECK(!nodes->empty());
444 size_t select = rand_r(&seed_) % nodes->size();
445 SchedulingNode* select_node = (*nodes)[select];
446 DeleteNodeAtIndex(nodes, select);
447 return select_node;
448 }
449
450 uint32_t seed_;
451};
452
453/*
454 * Select a `SchedulingNode` according to critical path information,
455 * with heuristics to favor certain instruction patterns like materialized condition.
456 */
457class CriticalPathSchedulingNodeSelector : public SchedulingNodeSelector {
458 public:
459 CriticalPathSchedulingNodeSelector() : prev_select_(nullptr) {}
460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100461 void Reset() override { prev_select_ = nullptr; }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100462 SchedulingNode* PopHighestPriorityNode(ScopedArenaVector<SchedulingNode*>* nodes,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100463 const SchedulingGraph& graph) override;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100464
465 protected:
466 SchedulingNode* GetHigherPrioritySchedulingNode(SchedulingNode* candidate,
467 SchedulingNode* check) const;
468
Vladimir Markoca6fff82017-10-03 14:49:14 +0100469 SchedulingNode* SelectMaterializedCondition(ScopedArenaVector<SchedulingNode*>* nodes,
470 const SchedulingGraph& graph) const;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100471
472 private:
473 const SchedulingNode* prev_select_;
474};
475
476class HScheduler {
477 public:
Vladimir Markoced04832018-07-26 14:42:17 +0100478 HScheduler(SchedulingLatencyVisitor* latency_visitor, SchedulingNodeSelector* selector)
479 : latency_visitor_(latency_visitor),
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100480 selector_(selector),
481 only_optimize_loop_blocks_(true),
Vladimir Markoced04832018-07-26 14:42:17 +0100482 cursor_(nullptr) {}
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100483 virtual ~HScheduler() {}
484
485 void Schedule(HGraph* graph);
486
487 void SetOnlyOptimizeLoopBlocks(bool loop_only) { only_optimize_loop_blocks_ = loop_only; }
488
489 // Instructions can not be rescheduled across a scheduling barrier.
490 virtual bool IsSchedulingBarrier(const HInstruction* instruction) const;
491
492 protected:
Vladimir Markoced04832018-07-26 14:42:17 +0100493 void Schedule(HBasicBlock* block, const HeapLocationCollector* heap_location_collector);
494 void Schedule(SchedulingNode* scheduling_node,
495 /*inout*/ ScopedArenaVector<SchedulingNode*>* candidates);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100496 void Schedule(HInstruction* instruction);
497
498 // Any instruction returning `false` via this method will prevent its
499 // containing basic block from being scheduled.
500 // This method is used to restrict scheduling to instructions that we know are
501 // safe to handle.
Artem Serov89ff8b22017-11-20 11:51:05 +0000502 //
503 // For newly introduced instructions by default HScheduler::IsSchedulable returns false.
504 // HScheduler${ARCH}::IsSchedulable can be overridden to return true for an instruction (see
505 // scheduler_arm64.h for example) if it is safe to schedule it; in this case one *must* also
506 // look at/update HScheduler${ARCH}::IsSchedulingBarrier for this instruction.
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100507 virtual bool IsSchedulable(const HInstruction* instruction) const;
508 bool IsSchedulable(const HBasicBlock* block) const;
509
510 void CalculateLatency(SchedulingNode* node) {
511 latency_visitor_->CalculateLatency(node);
512 node->SetLatency(latency_visitor_->GetLastVisitedLatency());
513 node->SetInternalLatency(latency_visitor_->GetLastVisitedInternalLatency());
514 }
515
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100516 SchedulingLatencyVisitor* const latency_visitor_;
517 SchedulingNodeSelector* const selector_;
518 bool only_optimize_loop_blocks_;
519
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100520 // A pointer indicating where the next instruction to be scheduled will be inserted.
521 HInstruction* cursor_;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100522
523 private:
524 DISALLOW_COPY_AND_ASSIGN(HScheduler);
525};
526
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100527class HInstructionScheduling : public HOptimization {
528 public:
Aart Bik2ca10eb2017-11-15 15:17:53 -0800529 HInstructionScheduling(HGraph* graph,
530 InstructionSet instruction_set,
531 CodeGenerator* cg = nullptr,
532 const char* name = kInstructionSchedulingPassName)
533 : HOptimization(graph, name),
xueliang.zhongf7caf682017-03-01 16:07:02 +0000534 codegen_(cg),
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100535 instruction_set_(instruction_set) {}
536
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100537 bool Run() override {
Aart Bik24773202018-04-26 10:28:51 -0700538 return Run(/*only_optimize_loop_blocks*/ true, /*schedule_randomly*/ false);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100539 }
Aart Bik24773202018-04-26 10:28:51 -0700540
541 bool Run(bool only_optimize_loop_blocks, bool schedule_randomly);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100542
Aart Bik2ca10eb2017-11-15 15:17:53 -0800543 static constexpr const char* kInstructionSchedulingPassName = "scheduler";
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100544
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100545 private:
xueliang.zhongf7caf682017-03-01 16:07:02 +0000546 CodeGenerator* const codegen_;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100547 const InstructionSet instruction_set_;
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100548 DISALLOW_COPY_AND_ASSIGN(HInstructionScheduling);
549};
550
551} // namespace art
552
553#endif // ART_COMPILER_OPTIMIZING_SCHEDULER_H_