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