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