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