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