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