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