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