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