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