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