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