blob: 17d5122542367ac829cb5509bfa39411801dea37 [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
Vladimir Marko225b6462015-09-28 12:17:40 +010020#include "base/arena_containers.h"
Vladimir Markoe2727152019-10-10 10:46:42 +010021#include "base/macros.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070022#include "base/value_object.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010023#include "data_type.h"
Zheng Xuad4450e2015-04-17 18:48:56 +080024#include "locations.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025
Vladimir Markoe2727152019-10-10 10:46:42 +010026namespace art HIDDEN {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027
28class HParallelMove;
29class MoveOperands;
30
Zheng Xuad4450e2015-04-17 18:48:56 +080031// Helper classes to resolve a set of parallel moves. Architecture dependent code generator must
32// have their own subclass that implements corresponding virtual functions.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033class ParallelMoveResolver : public ValueObject {
34 public:
Vladimir Marko225b6462015-09-28 12:17:40 +010035 explicit ParallelMoveResolver(ArenaAllocator* allocator)
36 : moves_(allocator->Adapter(kArenaAllocParallelMoveResolver)) {
37 moves_.reserve(32);
38 }
Nicolas Geoffrayaa037b52014-05-23 10:40:42 +010039 virtual ~ParallelMoveResolver() {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040
41 // Resolve a set of parallel moves, emitting assembler instructions.
Zheng Xuad4450e2015-04-17 18:48:56 +080042 virtual void EmitNativeCode(HParallelMove* parallel_move) = 0;
43
44 protected:
45 // Build the initial list of moves.
46 void BuildInitialMoveList(HParallelMove* parallel_move);
47
Vladimir Marko225b6462015-09-28 12:17:40 +010048 ArenaVector<MoveOperands*> moves_;
Zheng Xuad4450e2015-04-17 18:48:56 +080049
50 private:
51 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolver);
52};
53
54// This helper class uses swap to resolve dependencies and may emit swap.
55class ParallelMoveResolverWithSwap : public ParallelMoveResolver {
56 public:
57 explicit ParallelMoveResolverWithSwap(ArenaAllocator* allocator)
58 : ParallelMoveResolver(allocator) {}
59 virtual ~ParallelMoveResolverWithSwap() {}
60
61 // Resolve a set of parallel moves, emitting assembler instructions.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010062 void EmitNativeCode(HParallelMove* parallel_move) override;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010063
64 protected:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010065 class ScratchRegisterScope : public ValueObject {
66 public:
Zheng Xuad4450e2015-04-17 18:48:56 +080067 ScratchRegisterScope(ParallelMoveResolverWithSwap* resolver,
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +010068 int blocked,
69 int if_scratch,
70 int number_of_registers);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010071 ~ScratchRegisterScope();
72
73 int GetRegister() const { return reg_; }
74 bool IsSpilled() const { return spilled_; }
75
76 private:
Zheng Xuad4450e2015-04-17 18:48:56 +080077 ParallelMoveResolverWithSwap* resolver_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010078 int reg_;
79 bool spilled_;
80 };
81
Zheng Xuad4450e2015-04-17 18:48:56 +080082 // Return true if the location can be scratched.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010083 bool IsScratchLocation(Location loc);
Nicolas Geoffray48c310c2015-01-14 10:45:05 +000084
85 // Allocate a scratch register for performing a move. The method will try to use
86 // a register that is the destination of a move, but that move has not been emitted yet.
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +010087 int AllocateScratchRegister(int blocked, int if_scratch, int register_count, bool* spilled);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010089 // Emit a move.
90 virtual void EmitMove(size_t index) = 0;
91
92 // Execute a move by emitting a swap of two operands.
93 virtual void EmitSwap(size_t index) = 0;
94
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010095 virtual void SpillScratch(int reg) = 0;
96 virtual void RestoreScratch(int reg) = 0;
97
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010098 static constexpr int kNoRegister = -1;
99
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100100 private:
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100101 // Perform the move at the moves_ index in question (possibly requiring
102 // other moves to satisfy dependencies).
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000103 //
104 // Return whether another move in the dependency cycle needs to swap. This
Nicolas Geoffray90218252015-04-15 11:56:51 +0100105 // is to handle 64bits swaps:
106 // 1) In the case of register pairs, where we want the pair to swap first to avoid
107 // building pairs that are unexpected by the code generator. For example, if
108 // we were to swap R1 with R2, we would need to update all locations using
109 // R2 to R1. So a (R2,R3) pair register could become (R1,R3). We could make
110 // the code generator understand such pairs, but it's easier and cleaner to
111 // just not create such pairs and exchange pairs in priority.
112 // 2) Even when the architecture does not have pairs, we must handle 64bits swaps
113 // first. Consider the case: (R0->R1) (R1->S) (S->R0), where 'S' is a single
114 // stack slot. If we end up swapping S and R0, S will only contain the low bits
115 // of R0. If R0->R1 is for a 64bits instruction, R1 will therefore not contain
116 // the right value.
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000117 MoveOperands* PerformMove(size_t index);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100118
Zheng Xuad4450e2015-04-17 18:48:56 +0800119 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverWithSwap);
120};
121
122// This helper class uses additional scratch registers to resolve dependencies. It supports all kind
123// of dependency cycles and does not care about the register layout.
124class ParallelMoveResolverNoSwap : public ParallelMoveResolver {
125 public:
126 explicit ParallelMoveResolverNoSwap(ArenaAllocator* allocator)
Vladimir Marko225b6462015-09-28 12:17:40 +0100127 : ParallelMoveResolver(allocator),
128 scratches_(allocator->Adapter(kArenaAllocParallelMoveResolver)),
129 pending_moves_(allocator->Adapter(kArenaAllocParallelMoveResolver)),
130 allocator_(allocator) {
131 scratches_.reserve(32);
132 pending_moves_.reserve(8);
133 }
Zheng Xuad4450e2015-04-17 18:48:56 +0800134 virtual ~ParallelMoveResolverNoSwap() {}
135
136 // Resolve a set of parallel moves, emitting assembler instructions.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100137 void EmitNativeCode(HParallelMove* parallel_move) override;
Zheng Xuad4450e2015-04-17 18:48:56 +0800138
139 protected:
140 // Called at the beginning of EmitNativeCode(). A subclass may put some architecture dependent
141 // initialization here.
142 virtual void PrepareForEmitNativeCode() = 0;
143
144 // Called at the end of EmitNativeCode(). A subclass may put some architecture dependent cleanup
145 // here. All scratch locations will be removed after this call.
146 virtual void FinishEmitNativeCode() = 0;
147
148 // Allocate a scratch location to perform a move from input kind of location. A subclass should
149 // implement this to get the best fit location. If there is no suitable physical register, it can
150 // also return a stack slot.
151 virtual Location AllocateScratchLocationFor(Location::Kind kind) = 0;
152
153 // Called after a move which takes a scratch location as source. A subclass can defer the cleanup
154 // to FinishEmitNativeCode().
155 virtual void FreeScratchLocation(Location loc) = 0;
156
157 // Emit a move.
158 virtual void EmitMove(size_t index) = 0;
159
160 // Return a scratch location from the moves which exactly matches the kind.
161 // Return Location::NoLocation() if no matching scratch location can be found.
162 Location GetScratchLocation(Location::Kind kind);
163
164 // Add a location to the scratch list which can be returned from GetScratchLocation() to resolve
165 // dependency cycles.
166 void AddScratchLocation(Location loc);
167
168 // Remove a location from the scratch list.
169 void RemoveScratchLocation(Location loc);
170
171 // List of scratch locations.
Vladimir Marko225b6462015-09-28 12:17:40 +0100172 ArenaVector<Location> scratches_;
Zheng Xuad4450e2015-04-17 18:48:56 +0800173
174 private:
175 // Perform the move at the given index in `moves_` (possibly requiring other moves to satisfy
176 // dependencies).
177 void PerformMove(size_t index);
178
179 void UpdateMoveSource(Location from, Location to);
180
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 void AddPendingMove(Location source, Location destination, DataType::Type type);
Zheng Xuad4450e2015-04-17 18:48:56 +0800182
183 void DeletePendingMove(MoveOperands* move);
184
185 // Find a move that may be unblocked after (loc -> XXX) is performed.
186 MoveOperands* GetUnblockedPendingMove(Location loc);
187
188 // Return true if the location is blocked by outstanding moves.
189 bool IsBlockedByMoves(Location loc);
190
191 // Return the number of pending moves.
192 size_t GetNumberOfPendingMoves();
193
194 // Additional pending moves which might be added to resolve dependency cycle.
Vladimir Marko225b6462015-09-28 12:17:40 +0100195 ArenaVector<MoveOperands*> pending_moves_;
Zheng Xuad4450e2015-04-17 18:48:56 +0800196
197 // Used to allocate pending MoveOperands.
198 ArenaAllocator* const allocator_;
199
200 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverNoSwap);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100201};
202
203} // namespace art
204
205#endif // ART_COMPILER_OPTIMIZING_PARALLEL_MOVE_RESOLVER_H_