blob: 3fa1b37afdcbc5553c1f46c753fca39a168c1407 [file] [log] [blame]
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001/*
2 * Copyright (C) 2014 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_PARALLEL_MOVE_RESOLVER_H_
18#define ART_COMPILER_OPTIMIZING_PARALLEL_MOVE_RESOLVER_H_
19
Ian Rogers0279ebb2014-10-08 17:27:48 -070020#include "base/value_object.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010021#include "utils/growable_array.h"
22
23namespace art {
24
25class HParallelMove;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010026class Location;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027class MoveOperands;
28
29/**
30 * Helper class to resolve a set of parallel moves. Architecture dependent code
31 * generator must have their own subclass that implements the `EmitMove` and `EmitSwap`
32 * operations.
33 */
34class ParallelMoveResolver : public ValueObject {
35 public:
36 explicit ParallelMoveResolver(ArenaAllocator* allocator) : moves_(allocator, 32) {}
Nicolas Geoffrayaa037b52014-05-23 10:40:42 +010037 virtual ~ParallelMoveResolver() {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038
39 // Resolve a set of parallel moves, emitting assembler instructions.
40 void EmitNativeCode(HParallelMove* parallel_move);
41
42 protected:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010043 class ScratchRegisterScope : public ValueObject {
44 public:
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +010045 ScratchRegisterScope(ParallelMoveResolver* resolver,
46 int blocked,
47 int if_scratch,
48 int number_of_registers);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010049 ~ScratchRegisterScope();
50
51 int GetRegister() const { return reg_; }
52 bool IsSpilled() const { return spilled_; }
53
54 private:
55 ParallelMoveResolver* resolver_;
56 int reg_;
57 bool spilled_;
58 };
59
60 bool IsScratchLocation(Location loc);
Nicolas Geoffray48c310c2015-01-14 10:45:05 +000061
62 // Allocate a scratch register for performing a move. The method will try to use
63 // a register that is the destination of a move, but that move has not been emitted yet.
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +010064 int AllocateScratchRegister(int blocked, int if_scratch, int register_count, bool* spilled);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010065
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010066 // Emit a move.
67 virtual void EmitMove(size_t index) = 0;
68
69 // Execute a move by emitting a swap of two operands.
70 virtual void EmitSwap(size_t index) = 0;
71
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010072 virtual void SpillScratch(int reg) = 0;
73 virtual void RestoreScratch(int reg) = 0;
74
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010075 // List of moves not yet resolved.
76 GrowableArray<MoveOperands*> moves_;
77
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010078 static constexpr int kNoRegister = -1;
79
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010080 private:
81 // Build the initial list of moves.
82 void BuildInitialMoveList(HParallelMove* parallel_move);
83
84 // Perform the move at the moves_ index in question (possibly requiring
85 // other moves to satisfy dependencies).
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +000086 //
87 // Return whether another move in the dependency cycle needs to swap. This
88 // is to handle pair swaps, where we want the pair to swap first to avoid
89 // building pairs that are unexpected by the code generator. For example, if
90 // we were to swap R1 with R2, we would need to update all locations using
91 // R2 to R1. So a (R2,R3) pair register could become (R1,R3). We could make
92 // the code generator understand such pairs, but it's easier and cleaner to
93 // just not create such pairs and exchange pairs in priority.
94 MoveOperands* PerformMove(size_t index);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010095
96 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolver);
97};
98
99} // namespace art
100
101#endif // ART_COMPILER_OPTIMIZING_PARALLEL_MOVE_RESOLVER_H_