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