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