Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1 | /* |
| 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 Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 20 | #include "base/value_object.h" |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 21 | #include "utils/growable_array.h" |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 22 | #include "locations.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame^] | 23 | #include "primitive.h" |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class HParallelMove; |
| 28 | class MoveOperands; |
| 29 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 30 | // Helper classes to resolve a set of parallel moves. Architecture dependent code generator must |
| 31 | // have their own subclass that implements corresponding virtual functions. |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 32 | class ParallelMoveResolver : public ValueObject { |
| 33 | public: |
| 34 | explicit ParallelMoveResolver(ArenaAllocator* allocator) : moves_(allocator, 32) {} |
Nicolas Geoffray | aa037b5 | 2014-05-23 10:40:42 +0100 | [diff] [blame] | 35 | virtual ~ParallelMoveResolver() {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 36 | |
| 37 | // Resolve a set of parallel moves, emitting assembler instructions. |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 38 | virtual void EmitNativeCode(HParallelMove* parallel_move) = 0; |
| 39 | |
| 40 | protected: |
| 41 | // Build the initial list of moves. |
| 42 | void BuildInitialMoveList(HParallelMove* parallel_move); |
| 43 | |
| 44 | GrowableArray<MoveOperands*> moves_; |
| 45 | |
| 46 | private: |
| 47 | DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolver); |
| 48 | }; |
| 49 | |
| 50 | // This helper class uses swap to resolve dependencies and may emit swap. |
| 51 | class ParallelMoveResolverWithSwap : public ParallelMoveResolver { |
| 52 | public: |
| 53 | explicit ParallelMoveResolverWithSwap(ArenaAllocator* allocator) |
| 54 | : ParallelMoveResolver(allocator) {} |
| 55 | virtual ~ParallelMoveResolverWithSwap() {} |
| 56 | |
| 57 | // Resolve a set of parallel moves, emitting assembler instructions. |
| 58 | void EmitNativeCode(HParallelMove* parallel_move) OVERRIDE; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 59 | |
| 60 | protected: |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 61 | class ScratchRegisterScope : public ValueObject { |
| 62 | public: |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 63 | ScratchRegisterScope(ParallelMoveResolverWithSwap* resolver, |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 64 | int blocked, |
| 65 | int if_scratch, |
| 66 | int number_of_registers); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 67 | ~ScratchRegisterScope(); |
| 68 | |
| 69 | int GetRegister() const { return reg_; } |
| 70 | bool IsSpilled() const { return spilled_; } |
| 71 | |
| 72 | private: |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 73 | ParallelMoveResolverWithSwap* resolver_; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 74 | int reg_; |
| 75 | bool spilled_; |
| 76 | }; |
| 77 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 78 | // Return true if the location can be scratched. |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 79 | bool IsScratchLocation(Location loc); |
Nicolas Geoffray | 48c310c | 2015-01-14 10:45:05 +0000 | [diff] [blame] | 80 | |
| 81 | // Allocate a scratch register for performing a move. The method will try to use |
| 82 | // a register that is the destination of a move, but that move has not been emitted yet. |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 83 | int AllocateScratchRegister(int blocked, int if_scratch, int register_count, bool* spilled); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 84 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 85 | // Emit a move. |
| 86 | virtual void EmitMove(size_t index) = 0; |
| 87 | |
| 88 | // Execute a move by emitting a swap of two operands. |
| 89 | virtual void EmitSwap(size_t index) = 0; |
| 90 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 91 | virtual void SpillScratch(int reg) = 0; |
| 92 | virtual void RestoreScratch(int reg) = 0; |
| 93 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 94 | static constexpr int kNoRegister = -1; |
| 95 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 96 | private: |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 97 | // Perform the move at the moves_ index in question (possibly requiring |
| 98 | // other moves to satisfy dependencies). |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 99 | // |
| 100 | // Return whether another move in the dependency cycle needs to swap. This |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 101 | // is to handle 64bits swaps: |
| 102 | // 1) In the case of register pairs, where we want the pair to swap first to avoid |
| 103 | // building pairs that are unexpected by the code generator. For example, if |
| 104 | // we were to swap R1 with R2, we would need to update all locations using |
| 105 | // R2 to R1. So a (R2,R3) pair register could become (R1,R3). We could make |
| 106 | // the code generator understand such pairs, but it's easier and cleaner to |
| 107 | // just not create such pairs and exchange pairs in priority. |
| 108 | // 2) Even when the architecture does not have pairs, we must handle 64bits swaps |
| 109 | // first. Consider the case: (R0->R1) (R1->S) (S->R0), where 'S' is a single |
| 110 | // stack slot. If we end up swapping S and R0, S will only contain the low bits |
| 111 | // of R0. If R0->R1 is for a 64bits instruction, R1 will therefore not contain |
| 112 | // the right value. |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 113 | MoveOperands* PerformMove(size_t index); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 114 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 115 | DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverWithSwap); |
| 116 | }; |
| 117 | |
| 118 | // This helper class uses additional scratch registers to resolve dependencies. It supports all kind |
| 119 | // of dependency cycles and does not care about the register layout. |
| 120 | class ParallelMoveResolverNoSwap : public ParallelMoveResolver { |
| 121 | public: |
| 122 | explicit ParallelMoveResolverNoSwap(ArenaAllocator* allocator) |
| 123 | : ParallelMoveResolver(allocator), scratches_(allocator, 32), |
| 124 | pending_moves_(allocator, 8), allocator_(allocator) {} |
| 125 | virtual ~ParallelMoveResolverNoSwap() {} |
| 126 | |
| 127 | // Resolve a set of parallel moves, emitting assembler instructions. |
| 128 | void EmitNativeCode(HParallelMove* parallel_move) OVERRIDE; |
| 129 | |
| 130 | protected: |
| 131 | // Called at the beginning of EmitNativeCode(). A subclass may put some architecture dependent |
| 132 | // initialization here. |
| 133 | virtual void PrepareForEmitNativeCode() = 0; |
| 134 | |
| 135 | // Called at the end of EmitNativeCode(). A subclass may put some architecture dependent cleanup |
| 136 | // here. All scratch locations will be removed after this call. |
| 137 | virtual void FinishEmitNativeCode() = 0; |
| 138 | |
| 139 | // Allocate a scratch location to perform a move from input kind of location. A subclass should |
| 140 | // implement this to get the best fit location. If there is no suitable physical register, it can |
| 141 | // also return a stack slot. |
| 142 | virtual Location AllocateScratchLocationFor(Location::Kind kind) = 0; |
| 143 | |
| 144 | // Called after a move which takes a scratch location as source. A subclass can defer the cleanup |
| 145 | // to FinishEmitNativeCode(). |
| 146 | virtual void FreeScratchLocation(Location loc) = 0; |
| 147 | |
| 148 | // Emit a move. |
| 149 | virtual void EmitMove(size_t index) = 0; |
| 150 | |
| 151 | // Return a scratch location from the moves which exactly matches the kind. |
| 152 | // Return Location::NoLocation() if no matching scratch location can be found. |
| 153 | Location GetScratchLocation(Location::Kind kind); |
| 154 | |
| 155 | // Add a location to the scratch list which can be returned from GetScratchLocation() to resolve |
| 156 | // dependency cycles. |
| 157 | void AddScratchLocation(Location loc); |
| 158 | |
| 159 | // Remove a location from the scratch list. |
| 160 | void RemoveScratchLocation(Location loc); |
| 161 | |
| 162 | // List of scratch locations. |
| 163 | GrowableArray<Location> scratches_; |
| 164 | |
| 165 | private: |
| 166 | // Perform the move at the given index in `moves_` (possibly requiring other moves to satisfy |
| 167 | // dependencies). |
| 168 | void PerformMove(size_t index); |
| 169 | |
| 170 | void UpdateMoveSource(Location from, Location to); |
| 171 | |
| 172 | void AddPendingMove(Location source, Location destination, Primitive::Type type); |
| 173 | |
| 174 | void DeletePendingMove(MoveOperands* move); |
| 175 | |
| 176 | // Find a move that may be unblocked after (loc -> XXX) is performed. |
| 177 | MoveOperands* GetUnblockedPendingMove(Location loc); |
| 178 | |
| 179 | // Return true if the location is blocked by outstanding moves. |
| 180 | bool IsBlockedByMoves(Location loc); |
| 181 | |
| 182 | // Return the number of pending moves. |
| 183 | size_t GetNumberOfPendingMoves(); |
| 184 | |
| 185 | // Additional pending moves which might be added to resolve dependency cycle. |
| 186 | GrowableArray<MoveOperands*> pending_moves_; |
| 187 | |
| 188 | // Used to allocate pending MoveOperands. |
| 189 | ArenaAllocator* const allocator_; |
| 190 | |
| 191 | DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverNoSwap); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | } // namespace art |
| 195 | |
| 196 | #endif // ART_COMPILER_OPTIMIZING_PARALLEL_MOVE_RESOLVER_H_ |