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