1 /*
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_REGISTER_ALLOCATION_RESOLVER_H_
18 #define ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATION_RESOLVER_H_
19 
20 #include "base/array_ref.h"
21 #include "base/macros.h"
22 #include "base/value_object.h"
23 #include "data_type.h"
24 
25 namespace art HIDDEN {
26 
27 class ArenaAllocator;
28 class CodeGenerator;
29 class HBasicBlock;
30 class HInstruction;
31 class HParallelMove;
32 class LiveInterval;
33 class Location;
34 class SsaLivenessAnalysis;
35 
36 /**
37  * Reconciles the locations assigned to live intervals with the location
38  * summary of each instruction, and inserts moves to resolve split intervals,
39  * nonlinear control flow, and phi inputs.
40  */
41 class RegisterAllocationResolver : ValueObject {
42  public:
43   RegisterAllocationResolver(CodeGenerator* codegen, const SsaLivenessAnalysis& liveness);
44 
45   void Resolve(ArrayRef<HInstruction* const> safepoints,
46                size_t reserved_out_slots,  // Includes slot(s) for the art method.
47                size_t int_spill_slots,
48                size_t long_spill_slots,
49                size_t float_spill_slots,
50                size_t double_spill_slots,
51                size_t catch_phi_spill_slots,
52                ArrayRef<LiveInterval* const> temp_intervals);
53 
54  private:
55   // Update live registers of safepoint location summary.
56   void UpdateSafepointLiveRegisters();
57 
58   // Calculate the maximum size of the spill area for safepoints.
59   size_t CalculateMaximumSafepointSpillSize(ArrayRef<HInstruction* const> safepoints);
60 
61   // Connect adjacent siblings within blocks, and resolve inputs along the way.
62   void ConnectSiblings(LiveInterval* interval);
63 
64   // Connect siblings between block entries and exits.
65   void ConnectSplitSiblings(LiveInterval* interval, HBasicBlock* from, HBasicBlock* to) const;
66 
67   // Helper methods for inserting parallel moves in the graph.
68   void InsertParallelMoveAtExitOf(HBasicBlock* block,
69                                   HInstruction* instruction,
70                                   Location source,
71                                   Location destination) const;
72   void InsertParallelMoveAtEntryOf(HBasicBlock* block,
73                                    HInstruction* instruction,
74                                    Location source,
75                                    Location destination) const;
76   void InsertMoveAfter(HInstruction* instruction, Location source, Location destination) const;
77   void AddInputMoveFor(HInstruction* input,
78                        HInstruction* user,
79                        Location source,
80                        Location destination) const;
81   void InsertParallelMoveAt(size_t position,
82                             HInstruction* instruction,
83                             Location source,
84                             Location destination) const;
85   void AddMove(HParallelMove* move,
86                Location source,
87                Location destination,
88                HInstruction* instruction,
89                DataType::Type type) const;
90 
91   ArenaAllocator* const allocator_;
92   CodeGenerator* const codegen_;
93   const SsaLivenessAnalysis& liveness_;
94 
95   DISALLOW_COPY_AND_ASSIGN(RegisterAllocationResolver);
96 };
97 
98 }  // namespace art
99 
100 #endif  // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATION_RESOLVER_H_
101