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