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