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