Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [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_NODES_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_NODES_H_ |
| 19 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 20 | #include "locations.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 21 | #include "utils/allocation.h" |
Nicolas Geoffray | 0e33643 | 2014-02-26 18:24:38 +0000 | [diff] [blame] | 22 | #include "utils/arena_bit_vector.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 23 | #include "utils/growable_array.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class HBasicBlock; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 28 | class HEnvironment; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 29 | class HInstruction; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 30 | class HIntConstant; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 31 | class HGraphVisitor; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 32 | class HPhi; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 33 | class LiveInterval; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 34 | class LocationSummary; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 35 | |
| 36 | static const int kDefaultNumberOfBlocks = 8; |
| 37 | static const int kDefaultNumberOfSuccessors = 2; |
| 38 | static const int kDefaultNumberOfPredecessors = 2; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 39 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 40 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 41 | class HInstructionList { |
| 42 | public: |
| 43 | HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} |
| 44 | |
| 45 | void AddInstruction(HInstruction* instruction); |
| 46 | void RemoveInstruction(HInstruction* instruction); |
| 47 | |
| 48 | private: |
| 49 | HInstruction* first_instruction_; |
| 50 | HInstruction* last_instruction_; |
| 51 | |
| 52 | friend class HBasicBlock; |
| 53 | friend class HInstructionIterator; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 54 | friend class HBackwardInstructionIterator; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(HInstructionList); |
| 57 | }; |
| 58 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 59 | // Control-flow graph of a method. Contains a list of basic blocks. |
| 60 | class HGraph : public ArenaObject { |
| 61 | public: |
| 62 | explicit HGraph(ArenaAllocator* arena) |
| 63 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 64 | blocks_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 65 | reverse_post_order_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 66 | maximum_number_of_out_vregs_(0), |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 67 | number_of_vregs_(0), |
| 68 | number_of_in_vregs_(0), |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 69 | current_instruction_id_(0) { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 70 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 71 | ArenaAllocator* GetArena() const { return arena_; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 72 | const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 73 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 74 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 75 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 76 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 77 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 78 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 79 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 80 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 81 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 82 | void BuildDominatorTree(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 83 | void TransformToSSA(); |
| 84 | void SimplifyCFG(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 85 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 86 | // Find all natural loops in this graph. Aborts computation and returns false |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 87 | // if one loop is not natural, that is the header does not dominate the back |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 88 | // edge. |
| 89 | bool FindNaturalLoops() const; |
| 90 | |
| 91 | void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); |
| 92 | void SimplifyLoop(HBasicBlock* header); |
| 93 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 94 | int GetNextInstructionId() { |
| 95 | return current_instruction_id_++; |
| 96 | } |
| 97 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 98 | uint16_t GetMaximumNumberOfOutVRegs() const { |
| 99 | return maximum_number_of_out_vregs_; |
| 100 | } |
| 101 | |
| 102 | void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) { |
| 103 | maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_); |
| 104 | } |
| 105 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 106 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 107 | number_of_vregs_ = number_of_vregs; |
| 108 | } |
| 109 | |
| 110 | uint16_t GetNumberOfVRegs() const { |
| 111 | return number_of_vregs_; |
| 112 | } |
| 113 | |
| 114 | void SetNumberOfInVRegs(uint16_t value) { |
| 115 | number_of_in_vregs_ = value; |
| 116 | } |
| 117 | |
| 118 | uint16_t GetNumberOfInVRegs() const { |
| 119 | return number_of_in_vregs_; |
| 120 | } |
| 121 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 122 | const GrowableArray<HBasicBlock*>& GetReversePostOrder() const { |
| 123 | return reverse_post_order_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 124 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 125 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 126 | private: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 127 | HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; |
| 128 | void VisitBlockForDominatorTree(HBasicBlock* block, |
| 129 | HBasicBlock* predecessor, |
| 130 | GrowableArray<size_t>* visits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 131 | void FindBackEdges(ArenaBitVector* visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 132 | void VisitBlockForBackEdges(HBasicBlock* block, |
| 133 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 134 | ArenaBitVector* visiting); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 135 | void RemoveDeadBlocks(const ArenaBitVector& visited) const; |
| 136 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 137 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 138 | |
| 139 | // List of blocks in insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 140 | GrowableArray<HBasicBlock*> blocks_; |
| 141 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 142 | // List of blocks to perform a reverse post order tree traversal. |
| 143 | GrowableArray<HBasicBlock*> reverse_post_order_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 144 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 145 | HBasicBlock* entry_block_; |
| 146 | HBasicBlock* exit_block_; |
| 147 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 148 | // The maximum number of virtual registers arguments passed to a HInvoke in this graph. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 149 | uint16_t maximum_number_of_out_vregs_; |
| 150 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 151 | // The number of virtual registers in this method. Contains the parameters. |
| 152 | uint16_t number_of_vregs_; |
| 153 | |
| 154 | // The number of virtual registers used by parameters of this method. |
| 155 | uint16_t number_of_in_vregs_; |
| 156 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 157 | // The current id to assign to a newly added instruction. See HInstruction.id_. |
| 158 | int current_instruction_id_; |
| 159 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 160 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 161 | }; |
| 162 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 163 | class HLoopInformation : public ArenaObject { |
| 164 | public: |
| 165 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 166 | : header_(header), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 167 | back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges), |
| 168 | blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {} |
| 169 | |
| 170 | HBasicBlock* GetHeader() const { |
| 171 | return header_; |
| 172 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 173 | |
| 174 | void AddBackEdge(HBasicBlock* back_edge) { |
| 175 | back_edges_.Add(back_edge); |
| 176 | } |
| 177 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 178 | void RemoveBackEdge(HBasicBlock* back_edge) { |
| 179 | back_edges_.Delete(back_edge); |
| 180 | } |
| 181 | |
| 182 | bool IsBackEdge(HBasicBlock* block) { |
| 183 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 184 | if (back_edges_.Get(i) == block) return true; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 189 | int NumberOfBackEdges() const { |
| 190 | return back_edges_.Size(); |
| 191 | } |
| 192 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 193 | HBasicBlock* GetPreHeader() const; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 194 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 195 | const GrowableArray<HBasicBlock*>& GetBackEdges() const { |
| 196 | return back_edges_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 199 | void ClearBackEdges() { |
| 200 | back_edges_.Reset(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 203 | // Find blocks that are part of this loop. Returns whether the loop is a natural loop, |
| 204 | // that is the header dominates the back edge. |
| 205 | bool Populate(); |
| 206 | |
| 207 | // Returns whether this loop information contains `block`. |
| 208 | // Note that this loop information *must* be populated before entering this function. |
| 209 | bool Contains(const HBasicBlock& block) const; |
| 210 | |
| 211 | // Returns whether this loop information is an inner loop of `other`. |
| 212 | // Note that `other` *must* be populated before entering this function. |
| 213 | bool IsIn(const HLoopInformation& other) const; |
| 214 | |
| 215 | const ArenaBitVector& GetBlocks() const { return blocks_; } |
| 216 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 217 | private: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 218 | // Internal recursive implementation of `Populate`. |
| 219 | void PopulateRecursive(HBasicBlock* block); |
| 220 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 221 | HBasicBlock* header_; |
| 222 | GrowableArray<HBasicBlock*> back_edges_; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 223 | ArenaBitVector blocks_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 224 | |
| 225 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 226 | }; |
| 227 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 228 | static constexpr size_t kNoLifetime = -1; |
| 229 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 230 | // A block in a method. Contains the list of instructions represented |
| 231 | // as a double linked list. Each block knows its predecessors and |
| 232 | // successors. |
| 233 | class HBasicBlock : public ArenaObject { |
| 234 | public: |
| 235 | explicit HBasicBlock(HGraph* graph) |
| 236 | : graph_(graph), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 237 | predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors), |
| 238 | successors_(graph->GetArena(), kDefaultNumberOfSuccessors), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 239 | loop_information_(nullptr), |
| 240 | dominator_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 241 | block_id_(-1), |
| 242 | lifetime_start_(kNoLifetime), |
| 243 | lifetime_end_(kNoLifetime) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 244 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 245 | const GrowableArray<HBasicBlock*>& GetPredecessors() const { |
| 246 | return predecessors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 249 | const GrowableArray<HBasicBlock*>& GetSuccessors() const { |
| 250 | return successors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 253 | void AddBackEdge(HBasicBlock* back_edge) { |
| 254 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 255 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 256 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 257 | DCHECK_EQ(loop_information_->GetHeader(), this); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 258 | loop_information_->AddBackEdge(back_edge); |
| 259 | } |
| 260 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 261 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 262 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 263 | int GetBlockId() const { return block_id_; } |
| 264 | void SetBlockId(int id) { block_id_ = id; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 265 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 266 | HBasicBlock* GetDominator() const { return dominator_; } |
| 267 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 268 | |
| 269 | int NumberOfBackEdges() const { |
| 270 | return loop_information_ == nullptr |
| 271 | ? 0 |
| 272 | : loop_information_->NumberOfBackEdges(); |
| 273 | } |
| 274 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 275 | HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } |
| 276 | HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 277 | const HInstructionList& GetInstructions() const { return instructions_; } |
| 278 | const HInstructionList& GetPhis() const { return phis_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 279 | HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 280 | |
| 281 | void AddSuccessor(HBasicBlock* block) { |
| 282 | successors_.Add(block); |
| 283 | block->predecessors_.Add(this); |
| 284 | } |
| 285 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 286 | void RemovePredecessor(HBasicBlock* block, bool remove_in_successor = true) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 287 | predecessors_.Delete(block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 288 | if (remove_in_successor) { |
| 289 | block->successors_.Delete(this); |
| 290 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 293 | void RemoveSuccessor(HBasicBlock* block, bool remove_in_predecessor = true) { |
| 294 | successors_.Delete(block); |
| 295 | if (remove_in_predecessor) { |
| 296 | block->predecessors_.Delete(this); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void ClearAllPredecessors() { |
| 301 | predecessors_.Reset(); |
| 302 | } |
| 303 | |
| 304 | void AddPredecessor(HBasicBlock* block) { |
| 305 | predecessors_.Add(block); |
| 306 | block->successors_.Add(this); |
| 307 | } |
| 308 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 309 | size_t GetPredecessorIndexOf(HBasicBlock* predecessor) { |
| 310 | for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { |
| 311 | if (predecessors_.Get(i) == predecessor) { |
| 312 | return i; |
| 313 | } |
| 314 | } |
| 315 | return -1; |
| 316 | } |
| 317 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 318 | void AddInstruction(HInstruction* instruction); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 319 | void RemoveInstruction(HInstruction* instruction); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 320 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 321 | void AddPhi(HPhi* phi); |
| 322 | void RemovePhi(HPhi* phi); |
| 323 | |
| 324 | bool IsLoopHeader() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 325 | return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | HLoopInformation* GetLoopInformation() const { |
| 329 | return loop_information_; |
| 330 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 331 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 332 | // Set the loop_information_ on this block. This method overrides the current |
| 333 | // loop_information if it is an outer loop of the passed loop information. |
| 334 | void SetInLoop(HLoopInformation* info) { |
| 335 | if (IsLoopHeader()) { |
| 336 | // Nothing to do. This just means `info` is an outer loop. |
| 337 | } else if (loop_information_ == nullptr) { |
| 338 | loop_information_ = info; |
| 339 | } else if (loop_information_->Contains(*info->GetHeader())) { |
| 340 | // Block is currently part of an outer loop. Make it part of this inner loop. |
| 341 | // Note that a non loop header having a loop information means this loop information |
| 342 | // has already been populated |
| 343 | loop_information_ = info; |
| 344 | } else { |
| 345 | // Block is part of an inner loop. Do not update the loop information. |
| 346 | // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` |
| 347 | // at this point, because this method is being called while populating `info`. |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Returns wheter this block dominates the blocked passed as parameter. |
| 352 | bool Dominates(HBasicBlock* block) const; |
| 353 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 354 | size_t GetLifetimeStart() const { return lifetime_start_; } |
| 355 | size_t GetLifetimeEnd() const { return lifetime_end_; } |
| 356 | |
| 357 | void SetLifetimeStart(size_t start) { lifetime_start_ = start; } |
| 358 | void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } |
| 359 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 360 | private: |
| 361 | HGraph* const graph_; |
| 362 | GrowableArray<HBasicBlock*> predecessors_; |
| 363 | GrowableArray<HBasicBlock*> successors_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 364 | HInstructionList instructions_; |
| 365 | HInstructionList phis_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 366 | HLoopInformation* loop_information_; |
| 367 | HBasicBlock* dominator_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 368 | int block_id_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 369 | size_t lifetime_start_; |
| 370 | size_t lifetime_end_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 371 | |
| 372 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 373 | }; |
| 374 | |
| 375 | #define FOR_EACH_INSTRUCTION(M) \ |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 376 | M(Add) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 377 | M(Equal) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 378 | M(Exit) \ |
| 379 | M(Goto) \ |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 380 | M(If) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 381 | M(IntConstant) \ |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 382 | M(InvokeStatic) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 383 | M(LoadLocal) \ |
| 384 | M(Local) \ |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 385 | M(LongConstant) \ |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 386 | M(NewInstance) \ |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 387 | M(Not) \ |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 388 | M(ParameterValue) \ |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 389 | M(ParallelMove) \ |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 390 | M(Phi) \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 391 | M(Return) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 392 | M(ReturnVoid) \ |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 393 | M(StoreLocal) \ |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 394 | M(Sub) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 395 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 396 | #define FORWARD_DECLARATION(type) class H##type; |
| 397 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 398 | #undef FORWARD_DECLARATION |
| 399 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 400 | #define DECLARE_INSTRUCTION(type) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 401 | virtual const char* DebugName() const { return #type; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 402 | virtual H##type* As##type() { return this; } \ |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 403 | virtual void Accept(HGraphVisitor* visitor) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 404 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 405 | template <typename T> |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 406 | class HUseListNode : public ArenaObject { |
| 407 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 408 | HUseListNode(T* user, size_t index, HUseListNode* tail) |
| 409 | : user_(user), index_(index), tail_(tail) { } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 410 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 411 | HUseListNode* GetTail() const { return tail_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 412 | T* GetUser() const { return user_; } |
| 413 | size_t GetIndex() const { return index_; } |
| 414 | |
| 415 | void SetTail(HUseListNode<T>* node) { tail_ = node; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 416 | |
| 417 | private: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 418 | T* const user_; |
| 419 | const size_t index_; |
| 420 | HUseListNode<T>* tail_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 421 | |
| 422 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 423 | }; |
| 424 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 425 | class HInstruction : public ArenaObject { |
| 426 | public: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 427 | HInstruction() |
| 428 | : previous_(nullptr), |
| 429 | next_(nullptr), |
| 430 | block_(nullptr), |
| 431 | id_(-1), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 432 | ssa_index_(-1), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 433 | uses_(nullptr), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 434 | env_uses_(nullptr), |
| 435 | environment_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 436 | locations_(nullptr), |
| 437 | live_interval_(nullptr), |
| 438 | lifetime_position_(kNoLifetime) {} |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 439 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 440 | virtual ~HInstruction() { } |
| 441 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 442 | HInstruction* GetNext() const { return next_; } |
| 443 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 444 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 445 | HBasicBlock* GetBlock() const { return block_; } |
| 446 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 447 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 448 | virtual size_t InputCount() const = 0; |
| 449 | virtual HInstruction* InputAt(size_t i) const = 0; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 450 | |
| 451 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 452 | virtual const char* DebugName() const = 0; |
| 453 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 454 | virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 455 | virtual void SetRawInputAt(size_t index, HInstruction* input) = 0; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 456 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 457 | virtual bool NeedsEnvironment() const { return false; } |
| 458 | |
| 459 | void AddUseAt(HInstruction* user, size_t index) { |
| 460 | uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 463 | void AddEnvUseAt(HEnvironment* user, size_t index) { |
| 464 | env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>( |
| 465 | user, index, env_uses_); |
| 466 | } |
| 467 | |
| 468 | void RemoveUser(HInstruction* user, size_t index); |
| 469 | |
| 470 | HUseListNode<HInstruction>* GetUses() const { return uses_; } |
| 471 | HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 472 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 473 | bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 474 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 475 | size_t NumberOfUses() const { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 476 | // TODO: Optimize this method if it is used outside of the HGraphVisualizer. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 477 | size_t result = 0; |
| 478 | HUseListNode<HInstruction>* current = uses_; |
| 479 | while (current != nullptr) { |
| 480 | current = current->GetTail(); |
| 481 | ++result; |
| 482 | } |
| 483 | return result; |
| 484 | } |
| 485 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 486 | int GetId() const { return id_; } |
| 487 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 488 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 489 | int GetSsaIndex() const { return ssa_index_; } |
| 490 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 491 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 492 | |
| 493 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 494 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 495 | void SetEnvironment(HEnvironment* environment) { environment_ = environment; } |
| 496 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 497 | LocationSummary* GetLocations() const { return locations_; } |
| 498 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 499 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 500 | void ReplaceWith(HInstruction* instruction); |
| 501 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 502 | #define INSTRUCTION_TYPE_CHECK(type) \ |
| 503 | virtual H##type* As##type() { return nullptr; } |
| 504 | |
| 505 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 506 | #undef INSTRUCTION_TYPE_CHECK |
| 507 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 508 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 509 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 510 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 511 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 512 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 513 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 514 | private: |
| 515 | HInstruction* previous_; |
| 516 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 517 | HBasicBlock* block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 518 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 519 | // An instruction gets an id when it is added to the graph. |
| 520 | // It reflects creation order. A negative id means the instruction |
| 521 | // has not beed added to the graph. |
| 522 | int id_; |
| 523 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 524 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 525 | int ssa_index_; |
| 526 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 527 | // List of instructions that have this instruction as input. |
| 528 | HUseListNode<HInstruction>* uses_; |
| 529 | |
| 530 | // List of environments that contain this instruction. |
| 531 | HUseListNode<HEnvironment>* env_uses_; |
| 532 | |
| 533 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 534 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 535 | // Set by the code generator. |
| 536 | LocationSummary* locations_; |
| 537 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 538 | // Set by the liveness analysis. |
| 539 | LiveInterval* live_interval_; |
| 540 | |
| 541 | // Set by the liveness analysis, this is the position in a linear |
| 542 | // order of blocks where this instruction's live interval start. |
| 543 | size_t lifetime_position_; |
| 544 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 545 | friend class HBasicBlock; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 546 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 547 | |
| 548 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 549 | }; |
| 550 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 551 | template<typename T> |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 552 | class HUseIterator : public ValueObject { |
| 553 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 554 | explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 555 | |
| 556 | bool Done() const { return current_ == nullptr; } |
| 557 | |
| 558 | void Advance() { |
| 559 | DCHECK(!Done()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 560 | current_ = current_->GetTail(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 563 | HUseListNode<T>* Current() const { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 564 | DCHECK(!Done()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 565 | return current_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | private: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 569 | HUseListNode<T>* current_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 570 | |
| 571 | friend class HValue; |
| 572 | }; |
| 573 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 574 | // A HEnvironment object contains the values of virtual registers at a given location. |
| 575 | class HEnvironment : public ArenaObject { |
| 576 | public: |
| 577 | HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) { |
| 578 | vregs_.SetSize(number_of_vregs); |
| 579 | for (size_t i = 0; i < number_of_vregs; i++) { |
| 580 | vregs_.Put(i, nullptr); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void Populate(const GrowableArray<HInstruction*>& env) { |
| 585 | for (size_t i = 0; i < env.Size(); i++) { |
| 586 | HInstruction* instruction = env.Get(i); |
| 587 | vregs_.Put(i, instruction); |
| 588 | if (instruction != nullptr) { |
| 589 | instruction->AddEnvUseAt(this, i); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void SetRawEnvAt(size_t index, HInstruction* instruction) { |
| 595 | vregs_.Put(index, instruction); |
| 596 | } |
| 597 | |
| 598 | GrowableArray<HInstruction*>* GetVRegs() { |
| 599 | return &vregs_; |
| 600 | } |
| 601 | |
| 602 | private: |
| 603 | GrowableArray<HInstruction*> vregs_; |
| 604 | |
| 605 | DISALLOW_COPY_AND_ASSIGN(HEnvironment); |
| 606 | }; |
| 607 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 608 | class HInputIterator : public ValueObject { |
| 609 | public: |
| 610 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { } |
| 611 | |
| 612 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 613 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 614 | void Advance() { index_++; } |
| 615 | |
| 616 | private: |
| 617 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 618 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 619 | |
| 620 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 621 | }; |
| 622 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 623 | class HInstructionIterator : public ValueObject { |
| 624 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 625 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 626 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 627 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 630 | bool Done() const { return instruction_ == nullptr; } |
| 631 | HInstruction* Current() const { return instruction_; } |
| 632 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 633 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 634 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | private: |
| 638 | HInstruction* instruction_; |
| 639 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 640 | |
| 641 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 642 | }; |
| 643 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 644 | class HBackwardInstructionIterator : public ValueObject { |
| 645 | public: |
| 646 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 647 | : instruction_(instructions.last_instruction_) { |
| 648 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 649 | } |
| 650 | |
| 651 | bool Done() const { return instruction_ == nullptr; } |
| 652 | HInstruction* Current() const { return instruction_; } |
| 653 | void Advance() { |
| 654 | instruction_ = next_; |
| 655 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 656 | } |
| 657 | |
| 658 | private: |
| 659 | HInstruction* instruction_; |
| 660 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 661 | |
| 662 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 663 | }; |
| 664 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 665 | // An embedded container with N elements of type T. Used (with partial |
| 666 | // specialization for N=0) because embedded arrays cannot have size 0. |
| 667 | template<typename T, intptr_t N> |
| 668 | class EmbeddedArray { |
| 669 | public: |
| 670 | EmbeddedArray() : elements_() { } |
| 671 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 672 | intptr_t GetLength() const { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 673 | |
| 674 | const T& operator[](intptr_t i) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 675 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 676 | return elements_[i]; |
| 677 | } |
| 678 | |
| 679 | T& operator[](intptr_t i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 680 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 681 | return elements_[i]; |
| 682 | } |
| 683 | |
| 684 | const T& At(intptr_t i) const { |
| 685 | return (*this)[i]; |
| 686 | } |
| 687 | |
| 688 | void SetAt(intptr_t i, const T& val) { |
| 689 | (*this)[i] = val; |
| 690 | } |
| 691 | |
| 692 | private: |
| 693 | T elements_[N]; |
| 694 | }; |
| 695 | |
| 696 | template<typename T> |
| 697 | class EmbeddedArray<T, 0> { |
| 698 | public: |
| 699 | intptr_t length() const { return 0; } |
| 700 | const T& operator[](intptr_t i) const { |
| 701 | LOG(FATAL) << "Unreachable"; |
| 702 | static T sentinel = 0; |
| 703 | return sentinel; |
| 704 | } |
| 705 | T& operator[](intptr_t i) { |
| 706 | LOG(FATAL) << "Unreachable"; |
| 707 | static T sentinel = 0; |
| 708 | return sentinel; |
| 709 | } |
| 710 | }; |
| 711 | |
| 712 | template<intptr_t N> |
| 713 | class HTemplateInstruction: public HInstruction { |
| 714 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 715 | HTemplateInstruction<N>() : inputs_() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 716 | virtual ~HTemplateInstruction() { } |
| 717 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 718 | virtual size_t InputCount() const { return N; } |
| 719 | virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 720 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 721 | protected: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 722 | virtual void SetRawInputAt(size_t i, HInstruction* instruction) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 723 | inputs_[i] = instruction; |
| 724 | } |
| 725 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 726 | private: |
| 727 | EmbeddedArray<HInstruction*, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 728 | |
| 729 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 730 | }; |
| 731 | |
| 732 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 733 | // instruction that branches to the exit block. |
| 734 | class HReturnVoid : public HTemplateInstruction<0> { |
| 735 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 736 | HReturnVoid() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 737 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 738 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 739 | |
| 740 | private: |
| 741 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 742 | }; |
| 743 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 744 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 745 | // instruction that branches to the exit block. |
| 746 | class HReturn : public HTemplateInstruction<1> { |
| 747 | public: |
| 748 | explicit HReturn(HInstruction* value) { |
| 749 | SetRawInputAt(0, value); |
| 750 | } |
| 751 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 752 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 753 | |
| 754 | private: |
| 755 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 756 | }; |
| 757 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 758 | // The exit instruction is the only instruction of the exit block. |
| 759 | // Instructions aborting the method (HTrow and HReturn) must branch to the |
| 760 | // exit block. |
| 761 | class HExit : public HTemplateInstruction<0> { |
| 762 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 763 | HExit() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 764 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 765 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 766 | |
| 767 | private: |
| 768 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 769 | }; |
| 770 | |
| 771 | // Jumps from one block to another. |
| 772 | class HGoto : public HTemplateInstruction<0> { |
| 773 | public: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 774 | HGoto() { } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 775 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 776 | HBasicBlock* GetSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 777 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 780 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 781 | |
| 782 | private: |
| 783 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 784 | }; |
| 785 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 786 | // Conditional branch. A block ending with an HIf instruction must have |
| 787 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 788 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 789 | public: |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 790 | explicit HIf(HInstruction* input) { |
| 791 | SetRawInputAt(0, input); |
| 792 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 793 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 794 | HBasicBlock* IfTrueSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 795 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | HBasicBlock* IfFalseSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 799 | return GetBlock()->GetSuccessors().Get(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 802 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 803 | |
| 804 | private: |
| 805 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 806 | }; |
| 807 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 808 | class HBinaryOperation : public HTemplateInstruction<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 809 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 810 | HBinaryOperation(Primitive::Type result_type, |
| 811 | HInstruction* left, |
| 812 | HInstruction* right) : result_type_(result_type) { |
| 813 | SetRawInputAt(0, left); |
| 814 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 817 | HInstruction* GetLeft() const { return InputAt(0); } |
| 818 | HInstruction* GetRight() const { return InputAt(1); } |
| 819 | Primitive::Type GetResultType() const { return result_type_; } |
| 820 | |
| 821 | virtual bool IsCommutative() { return false; } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 822 | virtual Primitive::Type GetType() const { return GetResultType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 823 | |
| 824 | private: |
| 825 | const Primitive::Type result_type_; |
| 826 | |
| 827 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 828 | }; |
| 829 | |
| 830 | |
| 831 | // Instruction to check if two inputs are equal to each other. |
| 832 | class HEqual : public HBinaryOperation { |
| 833 | public: |
| 834 | HEqual(HInstruction* first, HInstruction* second) |
| 835 | : HBinaryOperation(Primitive::kPrimBoolean, first, second) {} |
| 836 | |
| 837 | virtual bool IsCommutative() { return true; } |
| 838 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 839 | virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; } |
| 840 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 841 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 842 | |
| 843 | private: |
| 844 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 845 | }; |
| 846 | |
| 847 | // A local in the graph. Corresponds to a Dex register. |
| 848 | class HLocal : public HTemplateInstruction<0> { |
| 849 | public: |
| 850 | explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { } |
| 851 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 852 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 853 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 854 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 855 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 856 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 857 | // The Dex register number. |
| 858 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 859 | |
| 860 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 861 | }; |
| 862 | |
| 863 | // Load a given local. The local is an input of this instruction. |
| 864 | class HLoadLocal : public HTemplateInstruction<1> { |
| 865 | public: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 866 | explicit HLoadLocal(HLocal* local, Primitive::Type type) : type_(type) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 867 | SetRawInputAt(0, local); |
| 868 | } |
| 869 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 870 | virtual Primitive::Type GetType() const { return type_; } |
| 871 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 872 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 873 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 874 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 875 | |
| 876 | private: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 877 | const Primitive::Type type_; |
| 878 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 879 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 880 | }; |
| 881 | |
| 882 | // Store a value in a given local. This instruction has two inputs: the value |
| 883 | // and the local. |
| 884 | class HStoreLocal : public HTemplateInstruction<2> { |
| 885 | public: |
| 886 | HStoreLocal(HLocal* local, HInstruction* value) { |
| 887 | SetRawInputAt(0, local); |
| 888 | SetRawInputAt(1, value); |
| 889 | } |
| 890 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 891 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 892 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 893 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 894 | |
| 895 | private: |
| 896 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 897 | }; |
| 898 | |
| 899 | // Constants of the type int. Those can be from Dex instructions, or |
| 900 | // synthesized (for example with the if-eqz instruction). |
| 901 | class HIntConstant : public HTemplateInstruction<0> { |
| 902 | public: |
| 903 | explicit HIntConstant(int32_t value) : value_(value) { } |
| 904 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 905 | int32_t GetValue() const { return value_; } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 906 | virtual Primitive::Type GetType() const { return Primitive::kPrimInt; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 907 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 908 | DECLARE_INSTRUCTION(IntConstant); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 909 | |
| 910 | private: |
| 911 | const int32_t value_; |
| 912 | |
| 913 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 914 | }; |
| 915 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 916 | class HLongConstant : public HTemplateInstruction<0> { |
| 917 | public: |
| 918 | explicit HLongConstant(int64_t value) : value_(value) { } |
| 919 | |
| 920 | int64_t GetValue() const { return value_; } |
| 921 | |
| 922 | virtual Primitive::Type GetType() const { return Primitive::kPrimLong; } |
| 923 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 924 | DECLARE_INSTRUCTION(LongConstant); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 925 | |
| 926 | private: |
| 927 | const int64_t value_; |
| 928 | |
| 929 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 930 | }; |
| 931 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 932 | class HInvoke : public HInstruction { |
| 933 | public: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 934 | HInvoke(ArenaAllocator* arena, |
| 935 | uint32_t number_of_arguments, |
| 936 | Primitive::Type return_type, |
| 937 | uint32_t dex_pc) |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 938 | : inputs_(arena, number_of_arguments), |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 939 | return_type_(return_type), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 940 | dex_pc_(dex_pc) { |
| 941 | inputs_.SetSize(number_of_arguments); |
| 942 | } |
| 943 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 944 | virtual size_t InputCount() const { return inputs_.Size(); } |
| 945 | virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); } |
| 946 | |
| 947 | // Runtime needs to walk the stack, so Dex -> Dex calls need to |
| 948 | // know their environment. |
| 949 | virtual bool NeedsEnvironment() const { return true; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 950 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 951 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 952 | SetRawInputAt(index, argument); |
| 953 | } |
| 954 | |
| 955 | virtual void SetRawInputAt(size_t index, HInstruction* input) { |
| 956 | inputs_.Put(index, input); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 957 | } |
| 958 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 959 | virtual Primitive::Type GetType() const { return return_type_; } |
| 960 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 961 | uint32_t GetDexPc() const { return dex_pc_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 962 | |
| 963 | protected: |
| 964 | GrowableArray<HInstruction*> inputs_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 965 | const Primitive::Type return_type_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 966 | const uint32_t dex_pc_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 967 | |
| 968 | private: |
| 969 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 970 | }; |
| 971 | |
| 972 | class HInvokeStatic : public HInvoke { |
| 973 | public: |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 974 | HInvokeStatic(ArenaAllocator* arena, |
| 975 | uint32_t number_of_arguments, |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 976 | Primitive::Type return_type, |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 977 | uint32_t dex_pc, |
| 978 | uint32_t index_in_dex_cache) |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 979 | : HInvoke(arena, number_of_arguments, return_type, dex_pc), |
| 980 | index_in_dex_cache_(index_in_dex_cache) {} |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 981 | |
| 982 | uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; } |
| 983 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 984 | DECLARE_INSTRUCTION(InvokeStatic); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 985 | |
| 986 | private: |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 987 | const uint32_t index_in_dex_cache_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 988 | |
| 989 | DISALLOW_COPY_AND_ASSIGN(HInvokeStatic); |
| 990 | }; |
| 991 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 992 | class HNewInstance : public HTemplateInstruction<0> { |
| 993 | public: |
| 994 | HNewInstance(uint32_t dex_pc, uint16_t type_index) : dex_pc_(dex_pc), type_index_(type_index) {} |
| 995 | |
| 996 | uint32_t GetDexPc() const { return dex_pc_; } |
| 997 | uint16_t GetTypeIndex() const { return type_index_; } |
| 998 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 999 | virtual Primitive::Type GetType() const { return Primitive::kPrimNot; } |
| 1000 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1001 | // Calls runtime so needs an environment. |
| 1002 | virtual bool NeedsEnvironment() const { return true; } |
| 1003 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1004 | DECLARE_INSTRUCTION(NewInstance); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 1005 | |
| 1006 | private: |
| 1007 | const uint32_t dex_pc_; |
| 1008 | const uint16_t type_index_; |
| 1009 | |
| 1010 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 1011 | }; |
| 1012 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1013 | class HAdd : public HBinaryOperation { |
| 1014 | public: |
| 1015 | HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 1016 | : HBinaryOperation(result_type, left, right) {} |
| 1017 | |
| 1018 | virtual bool IsCommutative() { return true; } |
| 1019 | |
| 1020 | DECLARE_INSTRUCTION(Add); |
| 1021 | |
| 1022 | private: |
| 1023 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 1024 | }; |
| 1025 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1026 | class HSub : public HBinaryOperation { |
| 1027 | public: |
| 1028 | HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 1029 | : HBinaryOperation(result_type, left, right) {} |
| 1030 | |
| 1031 | virtual bool IsCommutative() { return false; } |
| 1032 | |
| 1033 | DECLARE_INSTRUCTION(Sub); |
| 1034 | |
| 1035 | private: |
| 1036 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 1037 | }; |
| 1038 | |
| 1039 | // The value of a parameter in this method. Its location depends on |
| 1040 | // the calling convention. |
| 1041 | class HParameterValue : public HTemplateInstruction<0> { |
| 1042 | public: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1043 | HParameterValue(uint8_t index, Primitive::Type parameter_type) |
| 1044 | : index_(index), parameter_type_(parameter_type) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1045 | |
| 1046 | uint8_t GetIndex() const { return index_; } |
| 1047 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1048 | virtual Primitive::Type GetType() const { return parameter_type_; } |
| 1049 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1050 | DECLARE_INSTRUCTION(ParameterValue); |
| 1051 | |
| 1052 | private: |
| 1053 | // The index of this parameter in the parameters list. Must be less |
| 1054 | // than HGraph::number_of_in_vregs_; |
| 1055 | const uint8_t index_; |
| 1056 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1057 | const Primitive::Type parameter_type_; |
| 1058 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 1059 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 1060 | }; |
| 1061 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 1062 | class HNot : public HTemplateInstruction<1> { |
| 1063 | public: |
| 1064 | explicit HNot(HInstruction* input) { |
| 1065 | SetRawInputAt(0, input); |
| 1066 | } |
| 1067 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1068 | virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; } |
| 1069 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 1070 | DECLARE_INSTRUCTION(Not); |
| 1071 | |
| 1072 | private: |
| 1073 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 1074 | }; |
| 1075 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1076 | class HPhi : public HInstruction { |
| 1077 | public: |
| 1078 | HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type) |
| 1079 | : inputs_(arena, number_of_inputs), |
| 1080 | reg_number_(reg_number), |
| 1081 | type_(type) { |
| 1082 | inputs_.SetSize(number_of_inputs); |
| 1083 | } |
| 1084 | |
| 1085 | virtual size_t InputCount() const { return inputs_.Size(); } |
| 1086 | virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); } |
| 1087 | |
| 1088 | virtual void SetRawInputAt(size_t index, HInstruction* input) { |
| 1089 | inputs_.Put(index, input); |
| 1090 | } |
| 1091 | |
| 1092 | void AddInput(HInstruction* input); |
| 1093 | |
| 1094 | virtual Primitive::Type GetType() const { return type_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1095 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1096 | |
| 1097 | uint32_t GetRegNumber() const { return reg_number_; } |
| 1098 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1099 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1100 | |
| 1101 | protected: |
| 1102 | GrowableArray<HInstruction*> inputs_; |
| 1103 | const uint32_t reg_number_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1104 | Primitive::Type type_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1105 | |
| 1106 | private: |
| 1107 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 1108 | }; |
| 1109 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1110 | class MoveOperands : public ArenaObject { |
| 1111 | public: |
| 1112 | MoveOperands(Location source, Location destination) |
| 1113 | : source_(source), destination_(destination) {} |
| 1114 | |
| 1115 | Location GetSource() const { return source_; } |
| 1116 | Location GetDestination() const { return destination_; } |
| 1117 | |
| 1118 | void SetSource(Location value) { source_ = value; } |
| 1119 | void SetDestination(Location value) { destination_ = value; } |
| 1120 | |
| 1121 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 1122 | // destination (but not the source). |
| 1123 | Location MarkPending() { |
| 1124 | DCHECK(!IsPending()); |
| 1125 | Location dest = destination_; |
| 1126 | destination_ = Location::NoLocation(); |
| 1127 | return dest; |
| 1128 | } |
| 1129 | |
| 1130 | void ClearPending(Location dest) { |
| 1131 | DCHECK(IsPending()); |
| 1132 | destination_ = dest; |
| 1133 | } |
| 1134 | |
| 1135 | bool IsPending() const { |
| 1136 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 1137 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 1138 | } |
| 1139 | |
| 1140 | // True if this blocks a move from the given location. |
| 1141 | bool Blocks(Location loc) const { |
| 1142 | return !IsEliminated() && source_.Equals(loc); |
| 1143 | } |
| 1144 | |
| 1145 | // A move is redundant if it's been eliminated, if its source and |
| 1146 | // destination are the same, or if its destination is unneeded. |
| 1147 | bool IsRedundant() const { |
| 1148 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 1149 | } |
| 1150 | |
| 1151 | // We clear both operands to indicate move that's been eliminated. |
| 1152 | void Eliminate() { |
| 1153 | source_ = destination_ = Location::NoLocation(); |
| 1154 | } |
| 1155 | |
| 1156 | bool IsEliminated() const { |
| 1157 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 1158 | return source_.IsInvalid(); |
| 1159 | } |
| 1160 | |
| 1161 | private: |
| 1162 | Location source_; |
| 1163 | Location destination_; |
| 1164 | |
| 1165 | DISALLOW_COPY_AND_ASSIGN(MoveOperands); |
| 1166 | }; |
| 1167 | |
| 1168 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 1169 | |
| 1170 | class HParallelMove : public HTemplateInstruction<0> { |
| 1171 | public: |
| 1172 | explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {} |
| 1173 | |
| 1174 | void AddMove(MoveOperands* move) { |
| 1175 | moves_.Add(move); |
| 1176 | } |
| 1177 | |
| 1178 | MoveOperands* MoveOperandsAt(size_t index) const { |
| 1179 | return moves_.Get(index); |
| 1180 | } |
| 1181 | |
| 1182 | size_t NumMoves() const { return moves_.Size(); } |
| 1183 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1184 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1185 | |
| 1186 | private: |
| 1187 | GrowableArray<MoveOperands*> moves_; |
| 1188 | |
| 1189 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 1190 | }; |
| 1191 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1192 | class HGraphVisitor : public ValueObject { |
| 1193 | public: |
| 1194 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) { } |
| 1195 | virtual ~HGraphVisitor() { } |
| 1196 | |
| 1197 | virtual void VisitInstruction(HInstruction* instruction) { } |
| 1198 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 1199 | |
| 1200 | void VisitInsertionOrder(); |
| 1201 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1202 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1203 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1204 | // Visit functions for instruction classes. |
| 1205 | #define DECLARE_VISIT_INSTRUCTION(name) \ |
| 1206 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 1207 | |
| 1208 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 1209 | |
| 1210 | #undef DECLARE_VISIT_INSTRUCTION |
| 1211 | |
| 1212 | private: |
| 1213 | HGraph* graph_; |
| 1214 | |
| 1215 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 1216 | }; |
| 1217 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1218 | class HInsertionOrderIterator : public ValueObject { |
| 1219 | public: |
| 1220 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 1221 | |
| 1222 | bool Done() const { return index_ == graph_.GetBlocks().Size(); } |
| 1223 | HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } |
| 1224 | void Advance() { ++index_; } |
| 1225 | |
| 1226 | private: |
| 1227 | const HGraph& graph_; |
| 1228 | size_t index_; |
| 1229 | |
| 1230 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 1231 | }; |
| 1232 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1233 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1234 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1235 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1236 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1237 | bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } |
| 1238 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1239 | void Advance() { ++index_; } |
| 1240 | |
| 1241 | private: |
| 1242 | const HGraph& graph_; |
| 1243 | size_t index_; |
| 1244 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1245 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1246 | }; |
| 1247 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1248 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1249 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1250 | explicit HPostOrderIterator(const HGraph& graph) |
| 1251 | : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {} |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1252 | |
| 1253 | bool Done() const { return index_ == 0; } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1254 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1255 | void Advance() { --index_; } |
| 1256 | |
| 1257 | private: |
| 1258 | const HGraph& graph_; |
| 1259 | size_t index_; |
| 1260 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1261 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1262 | }; |
| 1263 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1264 | } // namespace art |
| 1265 | |
| 1266 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |