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