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" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class HParallelMove; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 26 | class Location; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 27 | class 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 | */ |
| 34 | class ParallelMoveResolver : public ValueObject { |
| 35 | public: |
| 36 | explicit ParallelMoveResolver(ArenaAllocator* allocator) : moves_(allocator, 32) {} |
Nicolas Geoffray | aa037b5 | 2014-05-23 10:40:42 +0100 | [diff] [blame] | 37 | virtual ~ParallelMoveResolver() {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 38 | |
| 39 | // Resolve a set of parallel moves, emitting assembler instructions. |
| 40 | void EmitNativeCode(HParallelMove* parallel_move); |
| 41 | |
| 42 | protected: |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 43 | class ScratchRegisterScope : public ValueObject { |
| 44 | public: |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 45 | ScratchRegisterScope(ParallelMoveResolver* resolver, |
| 46 | int blocked, |
| 47 | int if_scratch, |
| 48 | int number_of_registers); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 49 | ~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 Geoffray | 48c310c | 2015-01-14 10:45:05 +0000 | [diff] [blame] | 61 | |
| 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 Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 64 | int AllocateScratchRegister(int blocked, int if_scratch, int register_count, bool* spilled); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 65 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 66 | // 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 Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 72 | virtual void SpillScratch(int reg) = 0; |
| 73 | virtual void RestoreScratch(int reg) = 0; |
| 74 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 75 | // List of moves not yet resolved. |
| 76 | GrowableArray<MoveOperands*> moves_; |
| 77 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 78 | static constexpr int kNoRegister = -1; |
| 79 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 80 | 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 Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 86 | // |
| 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 Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolver); |
| 97 | }; |
| 98 | |
| 99 | } // namespace art |
| 100 | |
| 101 | #endif // ART_COMPILER_OPTIMIZING_PARALLEL_MOVE_RESOLVER_H_ |