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 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 21 | #include "base/arena_object.h" |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 22 | #include "dex/compiler_enums.h" |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 23 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 24 | #include "handle.h" |
| 25 | #include "handle_scope.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 26 | #include "invoke_type.h" |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 27 | #include "locations.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 28 | #include "mirror/class.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 29 | #include "offsets.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 30 | #include "primitive.h" |
Nicolas Geoffray | 0e33643 | 2014-02-26 18:24:38 +0000 | [diff] [blame] | 31 | #include "utils/arena_bit_vector.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 32 | #include "utils/growable_array.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 36 | class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 37 | class HBasicBlock; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 38 | class HDoubleConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 39 | class HEnvironment; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 40 | class HFloatConstant; |
| 41 | class HGraphVisitor; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 42 | class HInstruction; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 43 | class HIntConstant; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 44 | class HInvoke; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 45 | class HLongConstant; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 46 | class HNullConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 47 | class HPhi; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 48 | class HSuspendCheck; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 49 | class LiveInterval; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 50 | class LocationSummary; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 51 | class SlowPathCode; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 52 | class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 53 | |
| 54 | static const int kDefaultNumberOfBlocks = 8; |
| 55 | static const int kDefaultNumberOfSuccessors = 2; |
| 56 | static const int kDefaultNumberOfPredecessors = 2; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 57 | static const int kDefaultNumberOfDominatedBlocks = 1; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 58 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 59 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 60 | static constexpr uint32_t kMaxIntShiftValue = 0x1f; |
| 61 | static constexpr uint64_t kMaxLongShiftValue = 0x3f; |
| 62 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 63 | static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1); |
| 64 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 65 | enum IfCondition { |
| 66 | kCondEQ, |
| 67 | kCondNE, |
| 68 | kCondLT, |
| 69 | kCondLE, |
| 70 | kCondGT, |
| 71 | kCondGE, |
| 72 | }; |
| 73 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 74 | class HInstructionList { |
| 75 | public: |
| 76 | HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} |
| 77 | |
| 78 | void AddInstruction(HInstruction* instruction); |
| 79 | void RemoveInstruction(HInstruction* instruction); |
| 80 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 81 | // Insert `instruction` before/after an existing instruction `cursor`. |
| 82 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
| 83 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
| 84 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 85 | // Return true if this list contains `instruction`. |
| 86 | bool Contains(HInstruction* instruction) const; |
| 87 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 88 | // Return true if `instruction1` is found before `instruction2` in |
| 89 | // this instruction list and false otherwise. Abort if none |
| 90 | // of these instructions is found. |
| 91 | bool FoundBefore(const HInstruction* instruction1, |
| 92 | const HInstruction* instruction2) const; |
| 93 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 94 | bool IsEmpty() const { return first_instruction_ == nullptr; } |
| 95 | void Clear() { first_instruction_ = last_instruction_ = nullptr; } |
| 96 | |
| 97 | // Update the block of all instructions to be `block`. |
| 98 | void SetBlockOfInstructions(HBasicBlock* block) const; |
| 99 | |
| 100 | void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list); |
| 101 | void Add(const HInstructionList& instruction_list); |
| 102 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 103 | // Return the number of instructions in the list. This is an expensive operation. |
| 104 | size_t CountSize() const; |
| 105 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 106 | private: |
| 107 | HInstruction* first_instruction_; |
| 108 | HInstruction* last_instruction_; |
| 109 | |
| 110 | friend class HBasicBlock; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 111 | friend class HGraph; |
| 112 | friend class HInstruction; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 113 | friend class HInstructionIterator; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 114 | friend class HBackwardInstructionIterator; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 115 | |
| 116 | DISALLOW_COPY_AND_ASSIGN(HInstructionList); |
| 117 | }; |
| 118 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 119 | // Control-flow graph of a method. Contains a list of basic blocks. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 120 | class HGraph : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 121 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 122 | HGraph(ArenaAllocator* arena, |
| 123 | const DexFile& dex_file, |
| 124 | uint32_t method_idx, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 125 | bool should_generate_constructor_barrier, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 126 | InvokeType invoke_type = kInvalidInvokeType, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 127 | bool debuggable = false, |
| 128 | int start_instruction_id = 0) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 129 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 130 | blocks_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 131 | reverse_post_order_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 132 | linear_order_(arena, kDefaultNumberOfBlocks), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 133 | entry_block_(nullptr), |
| 134 | exit_block_(nullptr), |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 135 | maximum_number_of_out_vregs_(0), |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 136 | number_of_vregs_(0), |
| 137 | number_of_in_vregs_(0), |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 138 | temporaries_vreg_slots_(0), |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 139 | has_bounds_checks_(false), |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 140 | debuggable_(debuggable), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 141 | current_instruction_id_(start_instruction_id), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 142 | dex_file_(dex_file), |
| 143 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 144 | invoke_type_(invoke_type), |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 145 | should_generate_constructor_barrier_(should_generate_constructor_barrier), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 146 | cached_null_constant_(nullptr), |
| 147 | cached_int_constants_(std::less<int32_t>(), arena->Adapter()), |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 148 | cached_float_constants_(std::less<int32_t>(), arena->Adapter()), |
| 149 | cached_long_constants_(std::less<int64_t>(), arena->Adapter()), |
| 150 | cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 151 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 152 | ArenaAllocator* GetArena() const { return arena_; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 153 | const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; } |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 154 | HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 155 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 156 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 157 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 158 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 159 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 160 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 161 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 162 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 163 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 164 | // Try building the SSA form of this graph, with dominance computation and loop |
| 165 | // recognition. Returns whether it was successful in doing all these steps. |
| 166 | bool TryBuildingSsa() { |
| 167 | BuildDominatorTree(); |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 168 | // The SSA builder requires loops to all be natural. Specifically, the dead phi |
| 169 | // elimination phase checks the consistency of the graph when doing a post-order |
| 170 | // visit for eliminating dead phis: a dead phi can only have loop header phi |
| 171 | // users remaining when being visited. |
| 172 | if (!AnalyzeNaturalLoops()) return false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 173 | TransformToSsa(); |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 174 | return true; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 177 | void BuildDominatorTree(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 178 | void TransformToSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 179 | void SimplifyCFG(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 180 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 181 | // Analyze all natural loops in this graph. Returns false if one |
| 182 | // loop is not natural, that is the header does not dominate the |
| 183 | // back edge. |
| 184 | bool AnalyzeNaturalLoops() const; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 185 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 186 | // Inline this graph in `outer_graph`, replacing the given `invoke` instruction. |
| 187 | void InlineInto(HGraph* outer_graph, HInvoke* invoke); |
| 188 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 189 | // Removes `block` from the graph. |
| 190 | void DeleteDeadBlock(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 191 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 192 | void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); |
| 193 | void SimplifyLoop(HBasicBlock* header); |
| 194 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 195 | int32_t GetNextInstructionId() { |
| 196 | DCHECK_NE(current_instruction_id_, INT32_MAX); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 197 | return current_instruction_id_++; |
| 198 | } |
| 199 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 200 | int32_t GetCurrentInstructionId() const { |
| 201 | return current_instruction_id_; |
| 202 | } |
| 203 | |
| 204 | void SetCurrentInstructionId(int32_t id) { |
| 205 | current_instruction_id_ = id; |
| 206 | } |
| 207 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 208 | uint16_t GetMaximumNumberOfOutVRegs() const { |
| 209 | return maximum_number_of_out_vregs_; |
| 210 | } |
| 211 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 212 | void SetMaximumNumberOfOutVRegs(uint16_t new_value) { |
| 213 | maximum_number_of_out_vregs_ = new_value; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 216 | void UpdateTemporariesVRegSlots(size_t slots) { |
| 217 | temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 220 | size_t GetTemporariesVRegSlots() const { |
| 221 | return temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 224 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 225 | number_of_vregs_ = number_of_vregs; |
| 226 | } |
| 227 | |
| 228 | uint16_t GetNumberOfVRegs() const { |
| 229 | return number_of_vregs_; |
| 230 | } |
| 231 | |
| 232 | void SetNumberOfInVRegs(uint16_t value) { |
| 233 | number_of_in_vregs_ = value; |
| 234 | } |
| 235 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 236 | uint16_t GetNumberOfLocalVRegs() const { |
| 237 | return number_of_vregs_ - number_of_in_vregs_; |
| 238 | } |
| 239 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 240 | const GrowableArray<HBasicBlock*>& GetReversePostOrder() const { |
| 241 | return reverse_post_order_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 242 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 243 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 244 | const GrowableArray<HBasicBlock*>& GetLinearOrder() const { |
| 245 | return linear_order_; |
| 246 | } |
| 247 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 248 | bool HasBoundsChecks() const { |
| 249 | return has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 252 | void SetHasBoundsChecks(bool value) { |
| 253 | has_bounds_checks_ = value; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 256 | bool ShouldGenerateConstructorBarrier() const { |
| 257 | return should_generate_constructor_barrier_; |
| 258 | } |
| 259 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 260 | bool IsDebuggable() const { return debuggable_; } |
| 261 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 262 | // Returns a constant of the given type and value. If it does not exist |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 263 | // already, it is created and inserted into the graph. This method is only for |
| 264 | // integral types. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 265 | HConstant* GetConstant(Primitive::Type type, int64_t value); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 266 | HNullConstant* GetNullConstant(); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 267 | HIntConstant* GetIntConstant(int32_t value) { |
| 268 | return CreateConstant(value, &cached_int_constants_); |
| 269 | } |
| 270 | HLongConstant* GetLongConstant(int64_t value) { |
| 271 | return CreateConstant(value, &cached_long_constants_); |
| 272 | } |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 273 | HFloatConstant* GetFloatConstant(float value) { |
| 274 | return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_); |
| 275 | } |
| 276 | HDoubleConstant* GetDoubleConstant(double value) { |
| 277 | return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_); |
| 278 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 279 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 280 | HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 281 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 282 | const DexFile& GetDexFile() const { |
| 283 | return dex_file_; |
| 284 | } |
| 285 | |
| 286 | uint32_t GetMethodIdx() const { |
| 287 | return method_idx_; |
| 288 | } |
| 289 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 290 | InvokeType GetInvokeType() const { |
| 291 | return invoke_type_; |
| 292 | } |
| 293 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 294 | private: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 295 | void VisitBlockForDominatorTree(HBasicBlock* block, |
| 296 | HBasicBlock* predecessor, |
| 297 | GrowableArray<size_t>* visits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 298 | void FindBackEdges(ArenaBitVector* visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 299 | void VisitBlockForBackEdges(HBasicBlock* block, |
| 300 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 301 | ArenaBitVector* visiting); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 302 | void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 303 | void RemoveDeadBlocks(const ArenaBitVector& visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 304 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 305 | template <class InstructionType, typename ValueType> |
| 306 | InstructionType* CreateConstant(ValueType value, |
| 307 | ArenaSafeMap<ValueType, InstructionType*>* cache) { |
| 308 | // Try to find an existing constant of the given value. |
| 309 | InstructionType* constant = nullptr; |
| 310 | auto cached_constant = cache->find(value); |
| 311 | if (cached_constant != cache->end()) { |
| 312 | constant = cached_constant->second; |
| 313 | } |
| 314 | |
| 315 | // If not found or previously deleted, create and cache a new instruction. |
| 316 | if (constant == nullptr || constant->GetBlock() == nullptr) { |
| 317 | constant = new (arena_) InstructionType(value); |
| 318 | cache->Overwrite(value, constant); |
| 319 | InsertConstant(constant); |
| 320 | } |
| 321 | return constant; |
| 322 | } |
| 323 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 324 | void InsertConstant(HConstant* instruction); |
| 325 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 326 | // Cache a float constant into the graph. This method should only be |
| 327 | // called by the SsaBuilder when creating "equivalent" instructions. |
| 328 | void CacheFloatConstant(HFloatConstant* constant); |
| 329 | |
| 330 | // See CacheFloatConstant comment. |
| 331 | void CacheDoubleConstant(HDoubleConstant* constant); |
| 332 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 333 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 334 | |
| 335 | // List of blocks in insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 336 | GrowableArray<HBasicBlock*> blocks_; |
| 337 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 338 | // List of blocks to perform a reverse post order tree traversal. |
| 339 | GrowableArray<HBasicBlock*> reverse_post_order_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 340 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 341 | // List of blocks to perform a linear order tree traversal. |
| 342 | GrowableArray<HBasicBlock*> linear_order_; |
| 343 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 344 | HBasicBlock* entry_block_; |
| 345 | HBasicBlock* exit_block_; |
| 346 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 347 | // 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] | 348 | uint16_t maximum_number_of_out_vregs_; |
| 349 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 350 | // The number of virtual registers in this method. Contains the parameters. |
| 351 | uint16_t number_of_vregs_; |
| 352 | |
| 353 | // The number of virtual registers used by parameters of this method. |
| 354 | uint16_t number_of_in_vregs_; |
| 355 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 356 | // Number of vreg size slots that the temporaries use (used in baseline compiler). |
| 357 | size_t temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 358 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 359 | // Has bounds checks. We can totally skip BCE if it's false. |
| 360 | bool has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 361 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 362 | // Indicates whether the graph should be compiled in a way that |
| 363 | // ensures full debuggability. If false, we can apply more |
| 364 | // aggressive optimizations that may limit the level of debugging. |
| 365 | const bool debuggable_; |
| 366 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 367 | // The current id to assign to a newly added instruction. See HInstruction.id_. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 368 | int32_t current_instruction_id_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 369 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 370 | // The dex file from which the method is from. |
| 371 | const DexFile& dex_file_; |
| 372 | |
| 373 | // The method index in the dex file. |
| 374 | const uint32_t method_idx_; |
| 375 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 376 | // If inlined, this encodes how the callee is being invoked. |
| 377 | const InvokeType invoke_type_; |
| 378 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 379 | const bool should_generate_constructor_barrier_; |
| 380 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 381 | // Cached constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 382 | HNullConstant* cached_null_constant_; |
| 383 | ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 384 | ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 385 | ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 386 | ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 387 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 388 | friend class SsaBuilder; // For caching constants. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 389 | friend class SsaLivenessAnalysis; // For the linear order. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 390 | ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 391 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 392 | }; |
| 393 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 394 | class HLoopInformation : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 395 | public: |
| 396 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 397 | : header_(header), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 398 | suspend_check_(nullptr), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 399 | back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges), |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 400 | // Make bit vector growable, as the number of blocks may change. |
| 401 | blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {} |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 402 | |
| 403 | HBasicBlock* GetHeader() const { |
| 404 | return header_; |
| 405 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 406 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 407 | void SetHeader(HBasicBlock* block) { |
| 408 | header_ = block; |
| 409 | } |
| 410 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 411 | HSuspendCheck* GetSuspendCheck() const { return suspend_check_; } |
| 412 | void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; } |
| 413 | bool HasSuspendCheck() const { return suspend_check_ != nullptr; } |
| 414 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 415 | void AddBackEdge(HBasicBlock* back_edge) { |
| 416 | back_edges_.Add(back_edge); |
| 417 | } |
| 418 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 419 | void RemoveBackEdge(HBasicBlock* back_edge) { |
| 420 | back_edges_.Delete(back_edge); |
| 421 | } |
| 422 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 423 | bool IsBackEdge(const HBasicBlock& block) const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 424 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 425 | if (back_edges_.Get(i) == &block) return true; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 426 | } |
| 427 | return false; |
| 428 | } |
| 429 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 430 | size_t NumberOfBackEdges() const { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 431 | return back_edges_.Size(); |
| 432 | } |
| 433 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 434 | HBasicBlock* GetPreHeader() const; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 435 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 436 | const GrowableArray<HBasicBlock*>& GetBackEdges() const { |
| 437 | return back_edges_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 440 | // Returns the lifetime position of the back edge that has the |
| 441 | // greatest lifetime position. |
| 442 | size_t GetLifetimeEnd() const; |
| 443 | |
| 444 | void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) { |
| 445 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 446 | if (back_edges_.Get(i) == existing) { |
| 447 | back_edges_.Put(i, new_back_edge); |
| 448 | return; |
| 449 | } |
| 450 | } |
| 451 | UNREACHABLE(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 454 | // Finds blocks that are part of this loop. Returns whether the loop is a natural loop, |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 455 | // that is the header dominates the back edge. |
| 456 | bool Populate(); |
| 457 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 458 | // Reanalyzes the loop by removing loop info from its blocks and re-running |
| 459 | // Populate(). If there are no back edges left, the loop info is completely |
| 460 | // removed as well as its SuspendCheck instruction. It must be run on nested |
| 461 | // inner loops first. |
| 462 | void Update(); |
| 463 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 464 | // Returns whether this loop information contains `block`. |
| 465 | // Note that this loop information *must* be populated before entering this function. |
| 466 | bool Contains(const HBasicBlock& block) const; |
| 467 | |
| 468 | // Returns whether this loop information is an inner loop of `other`. |
| 469 | // Note that `other` *must* be populated before entering this function. |
| 470 | bool IsIn(const HLoopInformation& other) const; |
| 471 | |
| 472 | const ArenaBitVector& GetBlocks() const { return blocks_; } |
| 473 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 474 | void Add(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 475 | void Remove(HBasicBlock* block); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 476 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 477 | private: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 478 | // Internal recursive implementation of `Populate`. |
| 479 | void PopulateRecursive(HBasicBlock* block); |
| 480 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 481 | HBasicBlock* header_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 482 | HSuspendCheck* suspend_check_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 483 | GrowableArray<HBasicBlock*> back_edges_; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 484 | ArenaBitVector blocks_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 485 | |
| 486 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 487 | }; |
| 488 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 489 | static constexpr size_t kNoLifetime = -1; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 490 | static constexpr uint32_t kNoDexPc = -1; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 491 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 492 | // A block in a method. Contains the list of instructions represented |
| 493 | // as a double linked list. Each block knows its predecessors and |
| 494 | // successors. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 495 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 496 | class HBasicBlock : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 497 | public: |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 498 | explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 499 | : graph_(graph), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 500 | predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors), |
| 501 | successors_(graph->GetArena(), kDefaultNumberOfSuccessors), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 502 | loop_information_(nullptr), |
| 503 | dominator_(nullptr), |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 504 | dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 505 | block_id_(-1), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 506 | dex_pc_(dex_pc), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 507 | lifetime_start_(kNoLifetime), |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 508 | lifetime_end_(kNoLifetime), |
| 509 | is_catch_block_(false) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 510 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 511 | const GrowableArray<HBasicBlock*>& GetPredecessors() const { |
| 512 | return predecessors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 515 | const GrowableArray<HBasicBlock*>& GetSuccessors() const { |
| 516 | return successors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 519 | const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const { |
| 520 | return dominated_blocks_; |
| 521 | } |
| 522 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 523 | bool IsEntryBlock() const { |
| 524 | return graph_->GetEntryBlock() == this; |
| 525 | } |
| 526 | |
| 527 | bool IsExitBlock() const { |
| 528 | return graph_->GetExitBlock() == this; |
| 529 | } |
| 530 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 531 | bool IsSingleGoto() const; |
| 532 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 533 | void AddBackEdge(HBasicBlock* back_edge) { |
| 534 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 535 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 536 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 537 | DCHECK_EQ(loop_information_->GetHeader(), this); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 538 | loop_information_->AddBackEdge(back_edge); |
| 539 | } |
| 540 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 541 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 542 | void SetGraph(HGraph* graph) { graph_ = graph; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 543 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 544 | int GetBlockId() const { return block_id_; } |
| 545 | void SetBlockId(int id) { block_id_ = id; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 546 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 547 | HBasicBlock* GetDominator() const { return dominator_; } |
| 548 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 549 | void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 550 | void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 551 | void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) { |
| 552 | for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) { |
| 553 | if (dominated_blocks_.Get(i) == existing) { |
| 554 | dominated_blocks_.Put(i, new_block); |
| 555 | return; |
| 556 | } |
| 557 | } |
| 558 | LOG(FATAL) << "Unreachable"; |
| 559 | UNREACHABLE(); |
| 560 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 561 | |
| 562 | int NumberOfBackEdges() const { |
| 563 | return loop_information_ == nullptr |
| 564 | ? 0 |
| 565 | : loop_information_->NumberOfBackEdges(); |
| 566 | } |
| 567 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 568 | HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } |
| 569 | HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 570 | const HInstructionList& GetInstructions() const { return instructions_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 571 | HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 572 | HInstruction* GetLastPhi() const { return phis_.last_instruction_; } |
| 573 | const HInstructionList& GetPhis() const { return phis_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 574 | |
| 575 | void AddSuccessor(HBasicBlock* block) { |
| 576 | successors_.Add(block); |
| 577 | block->predecessors_.Add(this); |
| 578 | } |
| 579 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 580 | void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 581 | size_t successor_index = GetSuccessorIndexOf(existing); |
| 582 | DCHECK_NE(successor_index, static_cast<size_t>(-1)); |
| 583 | existing->RemovePredecessor(this); |
| 584 | new_block->predecessors_.Add(this); |
| 585 | successors_.Put(successor_index, new_block); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 588 | void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 589 | size_t predecessor_index = GetPredecessorIndexOf(existing); |
| 590 | DCHECK_NE(predecessor_index, static_cast<size_t>(-1)); |
| 591 | existing->RemoveSuccessor(this); |
| 592 | new_block->successors_.Add(this); |
| 593 | predecessors_.Put(predecessor_index, new_block); |
| 594 | } |
| 595 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 596 | void RemovePredecessor(HBasicBlock* block) { |
| 597 | predecessors_.Delete(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 600 | void RemoveSuccessor(HBasicBlock* block) { |
| 601 | successors_.Delete(block); |
| 602 | } |
| 603 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 604 | void ClearAllPredecessors() { |
| 605 | predecessors_.Reset(); |
| 606 | } |
| 607 | |
| 608 | void AddPredecessor(HBasicBlock* block) { |
| 609 | predecessors_.Add(block); |
| 610 | block->successors_.Add(this); |
| 611 | } |
| 612 | |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 613 | void SwapPredecessors() { |
Nicolas Geoffray | c83d441 | 2014-09-18 16:46:20 +0100 | [diff] [blame] | 614 | DCHECK_EQ(predecessors_.Size(), 2u); |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 615 | HBasicBlock* temp = predecessors_.Get(0); |
| 616 | predecessors_.Put(0, predecessors_.Get(1)); |
| 617 | predecessors_.Put(1, temp); |
| 618 | } |
| 619 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 620 | void SwapSuccessors() { |
| 621 | DCHECK_EQ(successors_.Size(), 2u); |
| 622 | HBasicBlock* temp = successors_.Get(0); |
| 623 | successors_.Put(0, successors_.Get(1)); |
| 624 | successors_.Put(1, temp); |
| 625 | } |
| 626 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 627 | size_t GetPredecessorIndexOf(HBasicBlock* predecessor) { |
| 628 | for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { |
| 629 | if (predecessors_.Get(i) == predecessor) { |
| 630 | return i; |
| 631 | } |
| 632 | } |
| 633 | return -1; |
| 634 | } |
| 635 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 636 | size_t GetSuccessorIndexOf(HBasicBlock* successor) { |
| 637 | for (size_t i = 0, e = successors_.Size(); i < e; ++i) { |
| 638 | if (successors_.Get(i) == successor) { |
| 639 | return i; |
| 640 | } |
| 641 | } |
| 642 | return -1; |
| 643 | } |
| 644 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 645 | // Split the block into two blocks just after `cursor`. Returns the newly |
| 646 | // created block. Note that this method just updates raw block information, |
| 647 | // like predecessors, successors, dominators, and instruction list. It does not |
| 648 | // update the graph, reverse post order, loop information, nor make sure the |
| 649 | // blocks are consistent (for example ending with a control flow instruction). |
| 650 | HBasicBlock* SplitAfter(HInstruction* cursor); |
| 651 | |
| 652 | // Merge `other` at the end of `this`. Successors and dominated blocks of |
| 653 | // `other` are changed to be successors and dominated blocks of `this`. Note |
| 654 | // that this method does not update the graph, reverse post order, loop |
| 655 | // information, nor make sure the blocks are consistent (for example ending |
| 656 | // with a control flow instruction). |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 657 | void MergeWithInlined(HBasicBlock* other); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 658 | |
| 659 | // Replace `this` with `other`. Predecessors, successors, and dominated blocks |
| 660 | // of `this` are moved to `other`. |
| 661 | // Note that this method does not update the graph, reverse post order, loop |
| 662 | // information, nor make sure the blocks are consistent (for example ending |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 663 | // with a control flow instruction). |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 664 | void ReplaceWith(HBasicBlock* other); |
| 665 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 666 | // Merge `other` at the end of `this`. This method updates loops, reverse post |
| 667 | // order, links to predecessors, successors, dominators and deletes the block |
| 668 | // from the graph. The two blocks must be successive, i.e. `this` the only |
| 669 | // predecessor of `other` and vice versa. |
| 670 | void MergeWith(HBasicBlock* other); |
| 671 | |
| 672 | // Disconnects `this` from all its predecessors, successors and dominator, |
| 673 | // removes it from all loops it is included in and eventually from the graph. |
| 674 | // The block must not dominate any other block. Predecessors and successors |
| 675 | // are safely updated. |
| 676 | void DisconnectAndDelete(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 677 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 678 | void AddInstruction(HInstruction* instruction); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 679 | // Insert `instruction` before/after an existing instruction `cursor`. |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 680 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 681 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 682 | // Replace instruction `initial` with `replacement` within this block. |
| 683 | void ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 684 | HInstruction* replacement); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 685 | void AddPhi(HPhi* phi); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 686 | void InsertPhiAfter(HPhi* instruction, HPhi* cursor); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 687 | // RemoveInstruction and RemovePhi delete a given instruction from the respective |
| 688 | // instruction list. With 'ensure_safety' set to true, it verifies that the |
| 689 | // instruction is not in use and removes it from the use lists of its inputs. |
| 690 | void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true); |
| 691 | void RemovePhi(HPhi* phi, bool ensure_safety = true); |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 692 | void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 693 | |
| 694 | bool IsLoopHeader() const { |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 695 | return IsInLoop() && (loop_information_->GetHeader() == this); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 698 | bool IsLoopPreHeaderFirstPredecessor() const { |
| 699 | DCHECK(IsLoopHeader()); |
| 700 | DCHECK(!GetPredecessors().IsEmpty()); |
| 701 | return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader(); |
| 702 | } |
| 703 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 704 | HLoopInformation* GetLoopInformation() const { |
| 705 | return loop_information_; |
| 706 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 707 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 708 | // Set the loop_information_ on this block. Overrides the current |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 709 | // loop_information if it is an outer loop of the passed loop information. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 710 | // Note that this method is called while creating the loop information. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 711 | void SetInLoop(HLoopInformation* info) { |
| 712 | if (IsLoopHeader()) { |
| 713 | // Nothing to do. This just means `info` is an outer loop. |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 714 | } else if (!IsInLoop()) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 715 | loop_information_ = info; |
| 716 | } else if (loop_information_->Contains(*info->GetHeader())) { |
| 717 | // Block is currently part of an outer loop. Make it part of this inner loop. |
| 718 | // Note that a non loop header having a loop information means this loop information |
| 719 | // has already been populated |
| 720 | loop_information_ = info; |
| 721 | } else { |
| 722 | // Block is part of an inner loop. Do not update the loop information. |
| 723 | // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` |
| 724 | // at this point, because this method is being called while populating `info`. |
| 725 | } |
| 726 | } |
| 727 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 728 | // Raw update of the loop information. |
| 729 | void SetLoopInformation(HLoopInformation* info) { |
| 730 | loop_information_ = info; |
| 731 | } |
| 732 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 733 | bool IsInLoop() const { return loop_information_ != nullptr; } |
| 734 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 735 | // Returns whether this block dominates the blocked passed as parameter. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 736 | bool Dominates(HBasicBlock* block) const; |
| 737 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 738 | size_t GetLifetimeStart() const { return lifetime_start_; } |
| 739 | size_t GetLifetimeEnd() const { return lifetime_end_; } |
| 740 | |
| 741 | void SetLifetimeStart(size_t start) { lifetime_start_ = start; } |
| 742 | void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } |
| 743 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 744 | uint32_t GetDexPc() const { return dex_pc_; } |
| 745 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 746 | bool IsCatchBlock() const { return is_catch_block_; } |
| 747 | void SetIsCatchBlock() { is_catch_block_ = true; } |
| 748 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 749 | bool EndsWithControlFlowInstruction() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 750 | bool EndsWithIf() const; |
| 751 | bool HasSinglePhi() const; |
| 752 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 753 | private: |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 754 | HGraph* graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 755 | GrowableArray<HBasicBlock*> predecessors_; |
| 756 | GrowableArray<HBasicBlock*> successors_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 757 | HInstructionList instructions_; |
| 758 | HInstructionList phis_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 759 | HLoopInformation* loop_information_; |
| 760 | HBasicBlock* dominator_; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 761 | GrowableArray<HBasicBlock*> dominated_blocks_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 762 | int block_id_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 763 | // The dex program counter of the first instruction of this block. |
| 764 | const uint32_t dex_pc_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 765 | size_t lifetime_start_; |
| 766 | size_t lifetime_end_; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 767 | bool is_catch_block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 768 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 769 | friend class HGraph; |
| 770 | friend class HInstruction; |
| 771 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 772 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 773 | }; |
| 774 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 775 | // Iterates over the LoopInformation of all loops which contain 'block' |
| 776 | // from the innermost to the outermost. |
| 777 | class HLoopInformationOutwardIterator : public ValueObject { |
| 778 | public: |
| 779 | explicit HLoopInformationOutwardIterator(const HBasicBlock& block) |
| 780 | : current_(block.GetLoopInformation()) {} |
| 781 | |
| 782 | bool Done() const { return current_ == nullptr; } |
| 783 | |
| 784 | void Advance() { |
| 785 | DCHECK(!Done()); |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 786 | current_ = current_->GetPreHeader()->GetLoopInformation(); |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | HLoopInformation* Current() const { |
| 790 | DCHECK(!Done()); |
| 791 | return current_; |
| 792 | } |
| 793 | |
| 794 | private: |
| 795 | HLoopInformation* current_; |
| 796 | |
| 797 | DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator); |
| 798 | }; |
| 799 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 800 | #define FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 801 | M(Add, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 802 | M(And, BinaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 803 | M(ArrayGet, Instruction) \ |
| 804 | M(ArrayLength, Instruction) \ |
| 805 | M(ArraySet, Instruction) \ |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 806 | M(BooleanNot, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 807 | M(BoundsCheck, Instruction) \ |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 808 | M(BoundType, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 809 | M(CheckCast, Instruction) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 810 | M(ClinitCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 811 | M(Compare, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 812 | M(Condition, BinaryOperation) \ |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 813 | M(Deoptimize, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 814 | M(Div, BinaryOperation) \ |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 815 | M(DivZeroCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 816 | M(DoubleConstant, Constant) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 817 | M(Equal, Condition) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 818 | M(Exit, Instruction) \ |
| 819 | M(FloatConstant, Constant) \ |
| 820 | M(Goto, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 821 | M(GreaterThan, Condition) \ |
| 822 | M(GreaterThanOrEqual, Condition) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 823 | M(If, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 824 | M(InstanceFieldGet, Instruction) \ |
| 825 | M(InstanceFieldSet, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 826 | M(InstanceOf, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 827 | M(IntConstant, Constant) \ |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 828 | M(InvokeInterface, Invoke) \ |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 829 | M(InvokeStaticOrDirect, Invoke) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 830 | M(InvokeVirtual, Invoke) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 831 | M(LessThan, Condition) \ |
| 832 | M(LessThanOrEqual, Condition) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 833 | M(LoadClass, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 834 | M(LoadException, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 835 | M(LoadLocal, Instruction) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 836 | M(LoadString, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 837 | M(Local, Instruction) \ |
| 838 | M(LongConstant, Constant) \ |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 839 | M(MemoryBarrier, Instruction) \ |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 840 | M(MonitorOperation, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 841 | M(Mul, BinaryOperation) \ |
| 842 | M(Neg, UnaryOperation) \ |
| 843 | M(NewArray, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 844 | M(NewInstance, Instruction) \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 845 | M(Not, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 846 | M(NotEqual, Condition) \ |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 847 | M(NullConstant, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 848 | M(NullCheck, Instruction) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 849 | M(Or, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 850 | M(ParallelMove, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 851 | M(ParameterValue, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 852 | M(Phi, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 853 | M(Rem, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 854 | M(Return, Instruction) \ |
| 855 | M(ReturnVoid, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 856 | M(Shl, BinaryOperation) \ |
| 857 | M(Shr, BinaryOperation) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 858 | M(StaticFieldGet, Instruction) \ |
| 859 | M(StaticFieldSet, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 860 | M(StoreLocal, Instruction) \ |
| 861 | M(Sub, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 862 | M(SuspendCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 863 | M(Temporary, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 864 | M(Throw, Instruction) \ |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 865 | M(TypeConversion, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 866 | M(UShr, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 867 | M(Xor, BinaryOperation) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 868 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 869 | #define FOR_EACH_INSTRUCTION(M) \ |
| 870 | FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 871 | M(Constant, Instruction) \ |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 872 | M(UnaryOperation, Instruction) \ |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 873 | M(BinaryOperation, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 874 | M(Invoke, Instruction) |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 875 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 876 | #define FORWARD_DECLARATION(type, super) class H##type; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 877 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 878 | #undef FORWARD_DECLARATION |
| 879 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 880 | #define DECLARE_INSTRUCTION(type) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 881 | InstructionKind GetKind() const OVERRIDE { return k##type; } \ |
| 882 | const char* DebugName() const OVERRIDE { return #type; } \ |
| 883 | const H##type* As##type() const OVERRIDE { return this; } \ |
| 884 | H##type* As##type() OVERRIDE { return this; } \ |
| 885 | bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 886 | return other->Is##type(); \ |
| 887 | } \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 888 | void Accept(HGraphVisitor* visitor) OVERRIDE |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 889 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 890 | template <typename T> class HUseList; |
| 891 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 892 | template <typename T> |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 893 | class HUseListNode : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 894 | public: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 895 | HUseListNode* GetPrevious() const { return prev_; } |
| 896 | HUseListNode* GetNext() const { return next_; } |
| 897 | T GetUser() const { return user_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 898 | size_t GetIndex() const { return index_; } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 899 | void SetIndex(size_t index) { index_ = index; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 900 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 901 | private: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 902 | HUseListNode(T user, size_t index) |
| 903 | : user_(user), index_(index), prev_(nullptr), next_(nullptr) {} |
| 904 | |
| 905 | T const user_; |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 906 | size_t index_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 907 | HUseListNode<T>* prev_; |
| 908 | HUseListNode<T>* next_; |
| 909 | |
| 910 | friend class HUseList<T>; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 911 | |
| 912 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 913 | }; |
| 914 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 915 | template <typename T> |
| 916 | class HUseList : public ValueObject { |
| 917 | public: |
| 918 | HUseList() : first_(nullptr) {} |
| 919 | |
| 920 | void Clear() { |
| 921 | first_ = nullptr; |
| 922 | } |
| 923 | |
| 924 | // Adds a new entry at the beginning of the use list and returns |
| 925 | // the newly created node. |
| 926 | HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) { |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 927 | HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 928 | if (IsEmpty()) { |
| 929 | first_ = new_node; |
| 930 | } else { |
| 931 | first_->prev_ = new_node; |
| 932 | new_node->next_ = first_; |
| 933 | first_ = new_node; |
| 934 | } |
| 935 | return new_node; |
| 936 | } |
| 937 | |
| 938 | HUseListNode<T>* GetFirst() const { |
| 939 | return first_; |
| 940 | } |
| 941 | |
| 942 | void Remove(HUseListNode<T>* node) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 943 | DCHECK(node != nullptr); |
| 944 | DCHECK(Contains(node)); |
| 945 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 946 | if (node->prev_ != nullptr) { |
| 947 | node->prev_->next_ = node->next_; |
| 948 | } |
| 949 | if (node->next_ != nullptr) { |
| 950 | node->next_->prev_ = node->prev_; |
| 951 | } |
| 952 | if (node == first_) { |
| 953 | first_ = node->next_; |
| 954 | } |
| 955 | } |
| 956 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 957 | bool Contains(const HUseListNode<T>* node) const { |
| 958 | if (node == nullptr) { |
| 959 | return false; |
| 960 | } |
| 961 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 962 | if (current == node) { |
| 963 | return true; |
| 964 | } |
| 965 | } |
| 966 | return false; |
| 967 | } |
| 968 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 969 | bool IsEmpty() const { |
| 970 | return first_ == nullptr; |
| 971 | } |
| 972 | |
| 973 | bool HasOnlyOneUse() const { |
| 974 | return first_ != nullptr && first_->next_ == nullptr; |
| 975 | } |
| 976 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 977 | size_t SizeSlow() const { |
| 978 | size_t count = 0; |
| 979 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 980 | ++count; |
| 981 | } |
| 982 | return count; |
| 983 | } |
| 984 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 985 | private: |
| 986 | HUseListNode<T>* first_; |
| 987 | }; |
| 988 | |
| 989 | template<typename T> |
| 990 | class HUseIterator : public ValueObject { |
| 991 | public: |
| 992 | explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {} |
| 993 | |
| 994 | bool Done() const { return current_ == nullptr; } |
| 995 | |
| 996 | void Advance() { |
| 997 | DCHECK(!Done()); |
| 998 | current_ = current_->GetNext(); |
| 999 | } |
| 1000 | |
| 1001 | HUseListNode<T>* Current() const { |
| 1002 | DCHECK(!Done()); |
| 1003 | return current_; |
| 1004 | } |
| 1005 | |
| 1006 | private: |
| 1007 | HUseListNode<T>* current_; |
| 1008 | |
| 1009 | friend class HValue; |
| 1010 | }; |
| 1011 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1012 | // This class is used by HEnvironment and HInstruction classes to record the |
| 1013 | // instructions they use and pointers to the corresponding HUseListNodes kept |
| 1014 | // by the used instructions. |
| 1015 | template <typename T> |
| 1016 | class HUserRecord : public ValueObject { |
| 1017 | public: |
| 1018 | HUserRecord() : instruction_(nullptr), use_node_(nullptr) {} |
| 1019 | explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {} |
| 1020 | |
| 1021 | HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node) |
| 1022 | : instruction_(old_record.instruction_), use_node_(use_node) { |
| 1023 | DCHECK(instruction_ != nullptr); |
| 1024 | DCHECK(use_node_ != nullptr); |
| 1025 | DCHECK(old_record.use_node_ == nullptr); |
| 1026 | } |
| 1027 | |
| 1028 | HInstruction* GetInstruction() const { return instruction_; } |
| 1029 | HUseListNode<T>* GetUseNode() const { return use_node_; } |
| 1030 | |
| 1031 | private: |
| 1032 | // Instruction used by the user. |
| 1033 | HInstruction* instruction_; |
| 1034 | |
| 1035 | // Corresponding entry in the use list kept by 'instruction_'. |
| 1036 | HUseListNode<T>* use_node_; |
| 1037 | }; |
| 1038 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 1039 | // TODO: Add better documentation to this class and maybe refactor with more suggestive names. |
| 1040 | // - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething |
| 1041 | // flag is consider. |
| 1042 | // - DependsOn suggests that there is a real dependency between side effects but it only |
| 1043 | // checks DependendsOnSomething flag. |
| 1044 | // |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1045 | // Represents the side effects an instruction may have. |
| 1046 | class SideEffects : public ValueObject { |
| 1047 | public: |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1048 | SideEffects() : flags_(0) {} |
| 1049 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1050 | static SideEffects None() { |
| 1051 | return SideEffects(0); |
| 1052 | } |
| 1053 | |
| 1054 | static SideEffects All() { |
| 1055 | return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_); |
| 1056 | } |
| 1057 | |
| 1058 | static SideEffects ChangesSomething() { |
| 1059 | return SideEffects((1 << kFlagChangesCount) - 1); |
| 1060 | } |
| 1061 | |
| 1062 | static SideEffects DependsOnSomething() { |
| 1063 | int count = kFlagDependsOnCount - kFlagChangesCount; |
| 1064 | return SideEffects(((1 << count) - 1) << kFlagChangesCount); |
| 1065 | } |
| 1066 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1067 | SideEffects Union(SideEffects other) const { |
| 1068 | return SideEffects(flags_ | other.flags_); |
| 1069 | } |
| 1070 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1071 | bool HasSideEffects() const { |
| 1072 | size_t all_bits_set = (1 << kFlagChangesCount) - 1; |
| 1073 | return (flags_ & all_bits_set) != 0; |
| 1074 | } |
| 1075 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1076 | bool HasAllSideEffects() const { |
| 1077 | size_t all_bits_set = (1 << kFlagChangesCount) - 1; |
| 1078 | return all_bits_set == (flags_ & all_bits_set); |
| 1079 | } |
| 1080 | |
| 1081 | bool DependsOn(SideEffects other) const { |
| 1082 | size_t depends_flags = other.ComputeDependsFlags(); |
| 1083 | return (flags_ & depends_flags) != 0; |
| 1084 | } |
| 1085 | |
| 1086 | bool HasDependencies() const { |
| 1087 | int count = kFlagDependsOnCount - kFlagChangesCount; |
| 1088 | size_t all_bits_set = (1 << count) - 1; |
| 1089 | return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0; |
| 1090 | } |
| 1091 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1092 | private: |
| 1093 | static constexpr int kFlagChangesSomething = 0; |
| 1094 | static constexpr int kFlagChangesCount = kFlagChangesSomething + 1; |
| 1095 | |
| 1096 | static constexpr int kFlagDependsOnSomething = kFlagChangesCount; |
| 1097 | static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1; |
| 1098 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1099 | explicit SideEffects(size_t flags) : flags_(flags) {} |
| 1100 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1101 | size_t ComputeDependsFlags() const { |
| 1102 | return flags_ << kFlagChangesCount; |
| 1103 | } |
| 1104 | |
| 1105 | size_t flags_; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1106 | }; |
| 1107 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1108 | // A HEnvironment object contains the values of virtual registers at a given location. |
| 1109 | class HEnvironment : public ArenaObject<kArenaAllocMisc> { |
| 1110 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1111 | HEnvironment(ArenaAllocator* arena, |
| 1112 | size_t number_of_vregs, |
| 1113 | const DexFile& dex_file, |
| 1114 | uint32_t method_idx, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1115 | uint32_t dex_pc, |
| 1116 | InvokeType invoke_type) |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1117 | : vregs_(arena, number_of_vregs), |
| 1118 | locations_(arena, number_of_vregs), |
| 1119 | parent_(nullptr), |
| 1120 | dex_file_(dex_file), |
| 1121 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1122 | dex_pc_(dex_pc), |
| 1123 | invoke_type_(invoke_type) { |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1124 | vregs_.SetSize(number_of_vregs); |
| 1125 | for (size_t i = 0; i < number_of_vregs; i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1126 | vregs_.Put(i, HUserRecord<HEnvironment*>()); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1127 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1128 | |
| 1129 | locations_.SetSize(number_of_vregs); |
| 1130 | for (size_t i = 0; i < number_of_vregs; ++i) { |
| 1131 | locations_.Put(i, Location()); |
| 1132 | } |
| 1133 | } |
| 1134 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1135 | HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy) |
| 1136 | : HEnvironment(arena, |
| 1137 | to_copy.Size(), |
| 1138 | to_copy.GetDexFile(), |
| 1139 | to_copy.GetMethodIdx(), |
| 1140 | to_copy.GetDexPc(), |
| 1141 | to_copy.GetInvokeType()) {} |
| 1142 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1143 | void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) { |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1144 | parent_ = new (allocator) HEnvironment(allocator, *parent); |
| 1145 | parent_->CopyFrom(parent); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1146 | if (parent->GetParent() != nullptr) { |
| 1147 | parent_->SetAndCopyParentChain(allocator, parent->GetParent()); |
| 1148 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 1151 | void CopyFrom(const GrowableArray<HInstruction*>& locals); |
| 1152 | void CopyFrom(HEnvironment* environment); |
| 1153 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1154 | // Copy from `env`. If it's a loop phi for `loop_header`, copy the first |
| 1155 | // input to the loop phi instead. This is for inserting instructions that |
| 1156 | // require an environment (like HDeoptimization) in the loop pre-header. |
| 1157 | void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1158 | |
| 1159 | void SetRawEnvAt(size_t index, HInstruction* instruction) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1160 | vregs_.Put(index, HUserRecord<HEnvironment*>(instruction)); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | HInstruction* GetInstructionAt(size_t index) const { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1164 | return vregs_.Get(index).GetInstruction(); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1167 | void RemoveAsUserOfInput(size_t index) const; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1168 | |
| 1169 | size_t Size() const { return vregs_.Size(); } |
| 1170 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1171 | HEnvironment* GetParent() const { return parent_; } |
| 1172 | |
| 1173 | void SetLocationAt(size_t index, Location location) { |
| 1174 | locations_.Put(index, location); |
| 1175 | } |
| 1176 | |
| 1177 | Location GetLocationAt(size_t index) const { |
| 1178 | return locations_.Get(index); |
| 1179 | } |
| 1180 | |
| 1181 | uint32_t GetDexPc() const { |
| 1182 | return dex_pc_; |
| 1183 | } |
| 1184 | |
| 1185 | uint32_t GetMethodIdx() const { |
| 1186 | return method_idx_; |
| 1187 | } |
| 1188 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1189 | InvokeType GetInvokeType() const { |
| 1190 | return invoke_type_; |
| 1191 | } |
| 1192 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1193 | const DexFile& GetDexFile() const { |
| 1194 | return dex_file_; |
| 1195 | } |
| 1196 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1197 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1198 | // Record instructions' use entries of this environment for constant-time removal. |
| 1199 | // It should only be called by HInstruction when a new environment use is added. |
| 1200 | void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) { |
| 1201 | DCHECK(env_use->GetUser() == this); |
| 1202 | size_t index = env_use->GetIndex(); |
| 1203 | vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use)); |
| 1204 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1205 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1206 | GrowableArray<HUserRecord<HEnvironment*> > vregs_; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1207 | GrowableArray<Location> locations_; |
| 1208 | HEnvironment* parent_; |
| 1209 | const DexFile& dex_file_; |
| 1210 | const uint32_t method_idx_; |
| 1211 | const uint32_t dex_pc_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1212 | const InvokeType invoke_type_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1213 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1214 | friend class HInstruction; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1215 | |
| 1216 | DISALLOW_COPY_AND_ASSIGN(HEnvironment); |
| 1217 | }; |
| 1218 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1219 | class ReferenceTypeInfo : ValueObject { |
| 1220 | public: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1221 | typedef Handle<mirror::Class> TypeHandle; |
| 1222 | |
| 1223 | static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) |
| 1224 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1225 | if (type_handle->IsObjectClass()) { |
| 1226 | // Override the type handle to be consistent with the case when we get to |
| 1227 | // Top but don't have the Object class available. It avoids having to guess |
| 1228 | // what value the type_handle has when it's Top. |
| 1229 | return ReferenceTypeInfo(TypeHandle(), is_exact, true); |
| 1230 | } else { |
| 1231 | return ReferenceTypeInfo(type_handle, is_exact, false); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | static ReferenceTypeInfo CreateTop(bool is_exact) { |
| 1236 | return ReferenceTypeInfo(TypeHandle(), is_exact, true); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | bool IsExact() const { return is_exact_; } |
| 1240 | bool IsTop() const { return is_top_; } |
| 1241 | |
| 1242 | Handle<mirror::Class> GetTypeHandle() const { return type_handle_; } |
| 1243 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1244 | bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1245 | if (IsTop()) { |
| 1246 | // Top (equivalent for java.lang.Object) is supertype of anything. |
| 1247 | return true; |
| 1248 | } |
| 1249 | if (rti.IsTop()) { |
| 1250 | // If we get here `this` is not Top() so it can't be a supertype. |
| 1251 | return false; |
| 1252 | } |
| 1253 | return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 1254 | } |
| 1255 | |
| 1256 | // Returns true if the type information provide the same amount of details. |
| 1257 | // Note that it does not mean that the instructions have the same actual type |
| 1258 | // (e.g. tops are equal but they can be the result of a merge). |
| 1259 | bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1260 | if (IsExact() != rti.IsExact()) { |
| 1261 | return false; |
| 1262 | } |
| 1263 | if (IsTop() && rti.IsTop()) { |
| 1264 | // `Top` means java.lang.Object, so the types are equivalent. |
| 1265 | return true; |
| 1266 | } |
| 1267 | if (IsTop() || rti.IsTop()) { |
| 1268 | // If only one is top or object than they are not equivalent. |
| 1269 | // NB: We need this extra check because the type_handle of `Top` is invalid |
| 1270 | // and we cannot inspect its reference. |
| 1271 | return false; |
| 1272 | } |
| 1273 | |
| 1274 | // Finally check the types. |
| 1275 | return GetTypeHandle().Get() == rti.GetTypeHandle().Get(); |
| 1276 | } |
| 1277 | |
| 1278 | private: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1279 | ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {} |
| 1280 | ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top) |
| 1281 | : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {} |
| 1282 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1283 | // The class of the object. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1284 | TypeHandle type_handle_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1285 | // Whether or not the type is exact or a superclass of the actual type. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1286 | // Whether or not we have any information about this type. |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1287 | bool is_exact_; |
| 1288 | // A true value here means that the object type should be java.lang.Object. |
| 1289 | // We don't have access to the corresponding mirror object every time so this |
| 1290 | // flag acts as a substitute. When true, the TypeHandle refers to a null |
| 1291 | // pointer and should not be used. |
| 1292 | bool is_top_; |
| 1293 | }; |
| 1294 | |
| 1295 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs); |
| 1296 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1297 | class HInstruction : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1298 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1299 | explicit HInstruction(SideEffects side_effects) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1300 | : previous_(nullptr), |
| 1301 | next_(nullptr), |
| 1302 | block_(nullptr), |
| 1303 | id_(-1), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1304 | ssa_index_(-1), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1305 | environment_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1306 | locations_(nullptr), |
| 1307 | live_interval_(nullptr), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1308 | lifetime_position_(kNoLifetime), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1309 | side_effects_(side_effects), |
| 1310 | reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {} |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1311 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1312 | virtual ~HInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1313 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1314 | #define DECLARE_KIND(type, super) k##type, |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1315 | enum InstructionKind { |
| 1316 | FOR_EACH_INSTRUCTION(DECLARE_KIND) |
| 1317 | }; |
| 1318 | #undef DECLARE_KIND |
| 1319 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1320 | HInstruction* GetNext() const { return next_; } |
| 1321 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1322 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1323 | HInstruction* GetNextDisregardingMoves() const; |
| 1324 | HInstruction* GetPreviousDisregardingMoves() const; |
| 1325 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1326 | HBasicBlock* GetBlock() const { return block_; } |
| 1327 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 1328 | bool IsInBlock() const { return block_ != nullptr; } |
| 1329 | bool IsInLoop() const { return block_->IsInLoop(); } |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 1330 | bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1331 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1332 | virtual size_t InputCount() const = 0; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1333 | HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1334 | |
| 1335 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 1336 | virtual const char* DebugName() const = 0; |
| 1337 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1338 | virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1339 | void SetRawInputAt(size_t index, HInstruction* input) { |
| 1340 | SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input)); |
| 1341 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1342 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1343 | virtual bool NeedsEnvironment() const { return false; } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1344 | virtual uint32_t GetDexPc() const { |
| 1345 | LOG(FATAL) << "GetDexPc() cannot be called on an instruction that" |
| 1346 | " does not need an environment"; |
| 1347 | UNREACHABLE(); |
| 1348 | } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1349 | virtual bool IsControlFlow() const { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 1350 | virtual bool CanThrow() const { return false; } |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1351 | bool HasSideEffects() const { return side_effects_.HasSideEffects(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1352 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1353 | // Does not apply for all instructions, but having this at top level greatly |
| 1354 | // simplifies the null check elimination. |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1355 | virtual bool CanBeNull() const { |
| 1356 | DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types"; |
| 1357 | return true; |
| 1358 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1359 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1360 | virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const { |
| 1361 | UNUSED(obj); |
| 1362 | return false; |
| 1363 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1364 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1365 | void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1366 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1367 | reference_type_info_ = reference_type_info; |
| 1368 | } |
| 1369 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1370 | ReferenceTypeInfo GetReferenceTypeInfo() const { |
| 1371 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
| 1372 | return reference_type_info_; |
| 1373 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1374 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1375 | void AddUseAt(HInstruction* user, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1376 | DCHECK(user != nullptr); |
| 1377 | HUseListNode<HInstruction*>* use = |
| 1378 | uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1379 | user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1382 | void AddEnvUseAt(HEnvironment* user, size_t index) { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 1383 | DCHECK(user != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1384 | HUseListNode<HEnvironment*>* env_use = |
| 1385 | env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1386 | user->RecordEnvUse(env_use); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1389 | void RemoveAsUserOfInput(size_t input) { |
| 1390 | HUserRecord<HInstruction*> input_use = InputRecordAt(input); |
| 1391 | input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode()); |
| 1392 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1393 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1394 | const HUseList<HInstruction*>& GetUses() const { return uses_; } |
| 1395 | const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1396 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1397 | bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); } |
| 1398 | bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); } |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1399 | bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1400 | bool HasOnlyOneNonEnvironmentUse() const { |
| 1401 | return !HasEnvironmentUses() && GetUses().HasOnlyOneUse(); |
| 1402 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1403 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 1404 | // Does this instruction strictly dominate `other_instruction`? |
| 1405 | // Returns false if this instruction and `other_instruction` are the same. |
| 1406 | // Aborts if this instruction and `other_instruction` are both phis. |
| 1407 | bool StrictlyDominates(HInstruction* other_instruction) const; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1408 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1409 | int GetId() const { return id_; } |
| 1410 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1411 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1412 | int GetSsaIndex() const { return ssa_index_; } |
| 1413 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 1414 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 1415 | |
| 1416 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 1417 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1418 | // Set the `environment_` field. Raw because this method does not |
| 1419 | // update the uses lists. |
| 1420 | void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; } |
| 1421 | |
| 1422 | // Set the environment of this instruction, copying it from `environment`. While |
| 1423 | // copying, the uses lists are being updated. |
| 1424 | void CopyEnvironmentFrom(HEnvironment* environment) { |
| 1425 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1426 | environment_ = new (allocator) HEnvironment(allocator, *environment); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1427 | environment_->CopyFrom(environment); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1428 | if (environment->GetParent() != nullptr) { |
| 1429 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1430 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1431 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1432 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1433 | void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment, |
| 1434 | HBasicBlock* block) { |
| 1435 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1436 | environment_ = new (allocator) HEnvironment(allocator, *environment); |
| 1437 | environment_->CopyFromWithLoopPhiAdjustment(environment, block); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1438 | if (environment->GetParent() != nullptr) { |
| 1439 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1440 | } |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1443 | // Returns the number of entries in the environment. Typically, that is the |
| 1444 | // number of dex registers in a method. It could be more in case of inlining. |
| 1445 | size_t EnvironmentSize() const; |
| 1446 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1447 | LocationSummary* GetLocations() const { return locations_; } |
| 1448 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1449 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1450 | void ReplaceWith(HInstruction* instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1451 | void ReplaceInput(HInstruction* replacement, size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1452 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1453 | // This is almost the same as doing `ReplaceWith()`. But in this helper, the |
| 1454 | // uses of this instruction by `other` are *not* updated. |
| 1455 | void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) { |
| 1456 | ReplaceWith(other); |
| 1457 | other->ReplaceInput(this, use_index); |
| 1458 | } |
| 1459 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1460 | // Move `this` instruction before `cursor`. |
| 1461 | void MoveBefore(HInstruction* cursor); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1462 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1463 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1464 | bool Is##type() const { return (As##type() != nullptr); } \ |
| 1465 | virtual const H##type* As##type() const { return nullptr; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1466 | virtual H##type* As##type() { return nullptr; } |
| 1467 | |
| 1468 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 1469 | #undef INSTRUCTION_TYPE_CHECK |
| 1470 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1471 | // Returns whether the instruction can be moved within the graph. |
| 1472 | virtual bool CanBeMoved() const { return false; } |
| 1473 | |
| 1474 | // Returns whether the two instructions are of the same kind. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1475 | virtual bool InstructionTypeEquals(HInstruction* other) const { |
| 1476 | UNUSED(other); |
| 1477 | return false; |
| 1478 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1479 | |
| 1480 | // Returns whether any data encoded in the two instructions is equal. |
| 1481 | // This method does not look at the inputs. Both instructions must be |
| 1482 | // of the same type, otherwise the method has undefined behavior. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1483 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1484 | UNUSED(other); |
| 1485 | return false; |
| 1486 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1487 | |
| 1488 | // Returns whether two instructions are equal, that is: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1489 | // 1) They have the same type and contain the same data (InstructionDataEquals). |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1490 | // 2) Their inputs are identical. |
| 1491 | bool Equals(HInstruction* other) const; |
| 1492 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1493 | virtual InstructionKind GetKind() const = 0; |
| 1494 | |
| 1495 | virtual size_t ComputeHashCode() const { |
| 1496 | size_t result = GetKind(); |
| 1497 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1498 | result = (result * 31) + InputAt(i)->GetId(); |
| 1499 | } |
| 1500 | return result; |
| 1501 | } |
| 1502 | |
| 1503 | SideEffects GetSideEffects() const { return side_effects_; } |
| 1504 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1505 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 1506 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 1507 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 1508 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 1509 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 1510 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 1511 | bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); } |
| 1512 | |
| 1513 | // Returns whether the code generation of the instruction will require to have access |
| 1514 | // to the current method. Such instructions are: |
| 1515 | // (1): Instructions that require an environment, as calling the runtime requires |
| 1516 | // to walk the stack and have the current method stored at a specific stack address. |
| 1517 | // (2): Object literals like classes and strings, that are loaded from the dex cache |
| 1518 | // fields of the current method. |
| 1519 | bool NeedsCurrentMethod() const { |
| 1520 | return NeedsEnvironment() || IsLoadClass() || IsLoadString(); |
| 1521 | } |
| 1522 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1523 | virtual bool NeedsDexCache() const { return false; } |
| 1524 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1525 | protected: |
| 1526 | virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0; |
| 1527 | virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0; |
| 1528 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1529 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1530 | void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); } |
| 1531 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1532 | HInstruction* previous_; |
| 1533 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1534 | HBasicBlock* block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1535 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1536 | // An instruction gets an id when it is added to the graph. |
| 1537 | // It reflects creation order. A negative id means the instruction |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1538 | // has not been added to the graph. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1539 | int id_; |
| 1540 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1541 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 1542 | int ssa_index_; |
| 1543 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1544 | // List of instructions that have this instruction as input. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1545 | HUseList<HInstruction*> uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1546 | |
| 1547 | // List of environments that contain this instruction. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1548 | HUseList<HEnvironment*> env_uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1549 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1550 | // The environment associated with this instruction. Not null if the instruction |
| 1551 | // might jump out of the method. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1552 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1553 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1554 | // Set by the code generator. |
| 1555 | LocationSummary* locations_; |
| 1556 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1557 | // Set by the liveness analysis. |
| 1558 | LiveInterval* live_interval_; |
| 1559 | |
| 1560 | // Set by the liveness analysis, this is the position in a linear |
| 1561 | // order of blocks where this instruction's live interval start. |
| 1562 | size_t lifetime_position_; |
| 1563 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1564 | const SideEffects side_effects_; |
| 1565 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1566 | // TODO: for primitive types this should be marked as invalid. |
| 1567 | ReferenceTypeInfo reference_type_info_; |
| 1568 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1569 | friend class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1570 | friend class HBasicBlock; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1571 | friend class HEnvironment; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1572 | friend class HGraph; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1573 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1574 | |
| 1575 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 1576 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1577 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1578 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1579 | class HInputIterator : public ValueObject { |
| 1580 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1581 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1582 | |
| 1583 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 1584 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 1585 | void Advance() { index_++; } |
| 1586 | |
| 1587 | private: |
| 1588 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1589 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1590 | |
| 1591 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 1592 | }; |
| 1593 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1594 | class HInstructionIterator : public ValueObject { |
| 1595 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1596 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 1597 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1598 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1601 | bool Done() const { return instruction_ == nullptr; } |
| 1602 | HInstruction* Current() const { return instruction_; } |
| 1603 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1604 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1605 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | private: |
| 1609 | HInstruction* instruction_; |
| 1610 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1611 | |
| 1612 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1613 | }; |
| 1614 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1615 | class HBackwardInstructionIterator : public ValueObject { |
| 1616 | public: |
| 1617 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 1618 | : instruction_(instructions.last_instruction_) { |
| 1619 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1620 | } |
| 1621 | |
| 1622 | bool Done() const { return instruction_ == nullptr; } |
| 1623 | HInstruction* Current() const { return instruction_; } |
| 1624 | void Advance() { |
| 1625 | instruction_ = next_; |
| 1626 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1627 | } |
| 1628 | |
| 1629 | private: |
| 1630 | HInstruction* instruction_; |
| 1631 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1632 | |
| 1633 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1634 | }; |
| 1635 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1636 | // An embedded container with N elements of type T. Used (with partial |
| 1637 | // specialization for N=0) because embedded arrays cannot have size 0. |
| 1638 | template<typename T, intptr_t N> |
| 1639 | class EmbeddedArray { |
| 1640 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1641 | EmbeddedArray() : elements_() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1642 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1643 | intptr_t GetLength() const { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1644 | |
| 1645 | const T& operator[](intptr_t i) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1646 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1647 | return elements_[i]; |
| 1648 | } |
| 1649 | |
| 1650 | T& operator[](intptr_t i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1651 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1652 | return elements_[i]; |
| 1653 | } |
| 1654 | |
| 1655 | const T& At(intptr_t i) const { |
| 1656 | return (*this)[i]; |
| 1657 | } |
| 1658 | |
| 1659 | void SetAt(intptr_t i, const T& val) { |
| 1660 | (*this)[i] = val; |
| 1661 | } |
| 1662 | |
| 1663 | private: |
| 1664 | T elements_[N]; |
| 1665 | }; |
| 1666 | |
| 1667 | template<typename T> |
| 1668 | class EmbeddedArray<T, 0> { |
| 1669 | public: |
| 1670 | intptr_t length() const { return 0; } |
| 1671 | const T& operator[](intptr_t i) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1672 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1673 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1674 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1675 | } |
| 1676 | T& operator[](intptr_t i) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1677 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1678 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1679 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1680 | } |
| 1681 | }; |
| 1682 | |
| 1683 | template<intptr_t N> |
| 1684 | class HTemplateInstruction: public HInstruction { |
| 1685 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1686 | HTemplateInstruction<N>(SideEffects side_effects) |
| 1687 | : HInstruction(side_effects), inputs_() {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1688 | virtual ~HTemplateInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1689 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1690 | size_t InputCount() const OVERRIDE { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1691 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1692 | protected: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1693 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; } |
| 1694 | |
| 1695 | void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 1696 | inputs_[i] = input; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1699 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1700 | EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1701 | |
| 1702 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1703 | }; |
| 1704 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1705 | template<intptr_t N> |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1706 | class HExpression : public HTemplateInstruction<N> { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1707 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1708 | HExpression<N>(Primitive::Type type, SideEffects side_effects) |
| 1709 | : HTemplateInstruction<N>(side_effects), type_(type) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1710 | virtual ~HExpression() {} |
| 1711 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1712 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1713 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1714 | protected: |
| 1715 | Primitive::Type type_; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1716 | }; |
| 1717 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1718 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 1719 | // instruction that branches to the exit block. |
| 1720 | class HReturnVoid : public HTemplateInstruction<0> { |
| 1721 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1722 | HReturnVoid() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1723 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1724 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1725 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1726 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1727 | |
| 1728 | private: |
| 1729 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 1730 | }; |
| 1731 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1732 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 1733 | // instruction that branches to the exit block. |
| 1734 | class HReturn : public HTemplateInstruction<1> { |
| 1735 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1736 | explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1737 | SetRawInputAt(0, value); |
| 1738 | } |
| 1739 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1740 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1741 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1742 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1743 | |
| 1744 | private: |
| 1745 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 1746 | }; |
| 1747 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1748 | // The exit instruction is the only instruction of the exit block. |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1749 | // Instructions aborting the method (HThrow and HReturn) must branch to the |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1750 | // exit block. |
| 1751 | class HExit : public HTemplateInstruction<0> { |
| 1752 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1753 | HExit() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1754 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1755 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1756 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1757 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1758 | |
| 1759 | private: |
| 1760 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 1761 | }; |
| 1762 | |
| 1763 | // Jumps from one block to another. |
| 1764 | class HGoto : public HTemplateInstruction<0> { |
| 1765 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1766 | HGoto() : HTemplateInstruction(SideEffects::None()) {} |
| 1767 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1768 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1769 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1770 | HBasicBlock* GetSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1771 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1774 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1775 | |
| 1776 | private: |
| 1777 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 1778 | }; |
| 1779 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1780 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1781 | // Conditional branch. A block ending with an HIf instruction must have |
| 1782 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1783 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1784 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1785 | explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1786 | SetRawInputAt(0, input); |
| 1787 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1788 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1789 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1790 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1791 | HBasicBlock* IfTrueSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1792 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | HBasicBlock* IfFalseSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1796 | return GetBlock()->GetSuccessors().Get(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1799 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1800 | |
| 1801 | private: |
| 1802 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 1803 | }; |
| 1804 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1805 | // Deoptimize to interpreter, upon checking a condition. |
| 1806 | class HDeoptimize : public HTemplateInstruction<1> { |
| 1807 | public: |
| 1808 | HDeoptimize(HInstruction* cond, uint32_t dex_pc) |
| 1809 | : HTemplateInstruction(SideEffects::None()), |
| 1810 | dex_pc_(dex_pc) { |
| 1811 | SetRawInputAt(0, cond); |
| 1812 | } |
| 1813 | |
| 1814 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 1815 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1816 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1817 | |
| 1818 | DECLARE_INSTRUCTION(Deoptimize); |
| 1819 | |
| 1820 | private: |
| 1821 | uint32_t dex_pc_; |
| 1822 | |
| 1823 | DISALLOW_COPY_AND_ASSIGN(HDeoptimize); |
| 1824 | }; |
| 1825 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1826 | class HUnaryOperation : public HExpression<1> { |
| 1827 | public: |
| 1828 | HUnaryOperation(Primitive::Type result_type, HInstruction* input) |
| 1829 | : HExpression(result_type, SideEffects::None()) { |
| 1830 | SetRawInputAt(0, input); |
| 1831 | } |
| 1832 | |
| 1833 | HInstruction* GetInput() const { return InputAt(0); } |
| 1834 | Primitive::Type GetResultType() const { return GetType(); } |
| 1835 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1836 | bool CanBeMoved() const OVERRIDE { return true; } |
| 1837 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1838 | UNUSED(other); |
| 1839 | return true; |
| 1840 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1841 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1842 | // Try to statically evaluate `operation` and return a HConstant |
| 1843 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1844 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1845 | HConstant* TryStaticEvaluation() const; |
| 1846 | |
| 1847 | // Apply this operation to `x`. |
| 1848 | virtual int32_t Evaluate(int32_t x) const = 0; |
| 1849 | virtual int64_t Evaluate(int64_t x) const = 0; |
| 1850 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1851 | DECLARE_INSTRUCTION(UnaryOperation); |
| 1852 | |
| 1853 | private: |
| 1854 | DISALLOW_COPY_AND_ASSIGN(HUnaryOperation); |
| 1855 | }; |
| 1856 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1857 | class HBinaryOperation : public HExpression<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1858 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1859 | HBinaryOperation(Primitive::Type result_type, |
| 1860 | HInstruction* left, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1861 | HInstruction* right) : HExpression(result_type, SideEffects::None()) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1862 | SetRawInputAt(0, left); |
| 1863 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1866 | HInstruction* GetLeft() const { return InputAt(0); } |
| 1867 | HInstruction* GetRight() const { return InputAt(1); } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1868 | Primitive::Type GetResultType() const { return GetType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1869 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1870 | virtual bool IsCommutative() const { return false; } |
| 1871 | |
| 1872 | // Put constant on the right. |
| 1873 | // Returns whether order is changed. |
| 1874 | bool OrderInputsWithConstantOnTheRight() { |
| 1875 | HInstruction* left = InputAt(0); |
| 1876 | HInstruction* right = InputAt(1); |
| 1877 | if (left->IsConstant() && !right->IsConstant()) { |
| 1878 | ReplaceInput(right, 0); |
| 1879 | ReplaceInput(left, 1); |
| 1880 | return true; |
| 1881 | } |
| 1882 | return false; |
| 1883 | } |
| 1884 | |
| 1885 | // Order inputs by instruction id, but favor constant on the right side. |
| 1886 | // This helps GVN for commutative ops. |
| 1887 | void OrderInputs() { |
| 1888 | DCHECK(IsCommutative()); |
| 1889 | HInstruction* left = InputAt(0); |
| 1890 | HInstruction* right = InputAt(1); |
| 1891 | if (left == right || (!left->IsConstant() && right->IsConstant())) { |
| 1892 | return; |
| 1893 | } |
| 1894 | if (OrderInputsWithConstantOnTheRight()) { |
| 1895 | return; |
| 1896 | } |
| 1897 | // Order according to instruction id. |
| 1898 | if (left->GetId() > right->GetId()) { |
| 1899 | ReplaceInput(right, 0); |
| 1900 | ReplaceInput(left, 1); |
| 1901 | } |
| 1902 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1903 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1904 | bool CanBeMoved() const OVERRIDE { return true; } |
| 1905 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1906 | UNUSED(other); |
| 1907 | return true; |
| 1908 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1909 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1910 | // Try to statically evaluate `operation` and return a HConstant |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1911 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1912 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1913 | HConstant* TryStaticEvaluation() const; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1914 | |
| 1915 | // Apply this operation to `x` and `y`. |
| 1916 | virtual int32_t Evaluate(int32_t x, int32_t y) const = 0; |
| 1917 | virtual int64_t Evaluate(int64_t x, int64_t y) const = 0; |
| 1918 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1919 | // Returns an input that can legally be used as the right input and is |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1920 | // constant, or null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1921 | HConstant* GetConstantRight() const; |
| 1922 | |
| 1923 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1924 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1925 | HInstruction* GetLeastConstantLeft() const; |
| 1926 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1927 | DECLARE_INSTRUCTION(BinaryOperation); |
| 1928 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1929 | private: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1930 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 1931 | }; |
| 1932 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1933 | class HCondition : public HBinaryOperation { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1934 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1935 | HCondition(HInstruction* first, HInstruction* second) |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1936 | : HBinaryOperation(Primitive::kPrimBoolean, first, second), |
| 1937 | needs_materialization_(true) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1938 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1939 | bool NeedsMaterialization() const { return needs_materialization_; } |
| 1940 | void ClearNeedsMaterialization() { needs_materialization_ = false; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1941 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 1942 | // For code generation purposes, returns whether this instruction is just before |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1943 | // `instruction`, and disregard moves in between. |
| 1944 | bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 1945 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1946 | DECLARE_INSTRUCTION(Condition); |
| 1947 | |
| 1948 | virtual IfCondition GetCondition() const = 0; |
| 1949 | |
| 1950 | private: |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1951 | // For register allocation purposes, returns whether this instruction needs to be |
| 1952 | // materialized (that is, not just be in the processor flags). |
| 1953 | bool needs_materialization_; |
| 1954 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1955 | DISALLOW_COPY_AND_ASSIGN(HCondition); |
| 1956 | }; |
| 1957 | |
| 1958 | // Instruction to check if two inputs are equal to each other. |
| 1959 | class HEqual : public HCondition { |
| 1960 | public: |
| 1961 | HEqual(HInstruction* first, HInstruction* second) |
| 1962 | : HCondition(first, second) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1963 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1964 | bool IsCommutative() const OVERRIDE { return true; } |
| 1965 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1966 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1967 | return x == y ? 1 : 0; |
| 1968 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1969 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1970 | return x == y ? 1 : 0; |
| 1971 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1972 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1973 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1974 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1975 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1976 | return kCondEQ; |
| 1977 | } |
| 1978 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1979 | private: |
| 1980 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 1981 | }; |
| 1982 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1983 | class HNotEqual : public HCondition { |
| 1984 | public: |
| 1985 | HNotEqual(HInstruction* first, HInstruction* second) |
| 1986 | : HCondition(first, second) {} |
| 1987 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1988 | bool IsCommutative() const OVERRIDE { return true; } |
| 1989 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1990 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1991 | return x != y ? 1 : 0; |
| 1992 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1993 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 1994 | return x != y ? 1 : 0; |
| 1995 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1996 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1997 | DECLARE_INSTRUCTION(NotEqual); |
| 1998 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1999 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2000 | return kCondNE; |
| 2001 | } |
| 2002 | |
| 2003 | private: |
| 2004 | DISALLOW_COPY_AND_ASSIGN(HNotEqual); |
| 2005 | }; |
| 2006 | |
| 2007 | class HLessThan : public HCondition { |
| 2008 | public: |
| 2009 | HLessThan(HInstruction* first, HInstruction* second) |
| 2010 | : HCondition(first, second) {} |
| 2011 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2012 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2013 | return x < y ? 1 : 0; |
| 2014 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2015 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2016 | return x < y ? 1 : 0; |
| 2017 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2018 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2019 | DECLARE_INSTRUCTION(LessThan); |
| 2020 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2021 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2022 | return kCondLT; |
| 2023 | } |
| 2024 | |
| 2025 | private: |
| 2026 | DISALLOW_COPY_AND_ASSIGN(HLessThan); |
| 2027 | }; |
| 2028 | |
| 2029 | class HLessThanOrEqual : public HCondition { |
| 2030 | public: |
| 2031 | HLessThanOrEqual(HInstruction* first, HInstruction* second) |
| 2032 | : HCondition(first, second) {} |
| 2033 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2034 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2035 | return x <= y ? 1 : 0; |
| 2036 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2037 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2038 | return x <= y ? 1 : 0; |
| 2039 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2040 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2041 | DECLARE_INSTRUCTION(LessThanOrEqual); |
| 2042 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2043 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2044 | return kCondLE; |
| 2045 | } |
| 2046 | |
| 2047 | private: |
| 2048 | DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); |
| 2049 | }; |
| 2050 | |
| 2051 | class HGreaterThan : public HCondition { |
| 2052 | public: |
| 2053 | HGreaterThan(HInstruction* first, HInstruction* second) |
| 2054 | : HCondition(first, second) {} |
| 2055 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2056 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2057 | return x > y ? 1 : 0; |
| 2058 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2059 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2060 | return x > y ? 1 : 0; |
| 2061 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2062 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2063 | DECLARE_INSTRUCTION(GreaterThan); |
| 2064 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2065 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2066 | return kCondGT; |
| 2067 | } |
| 2068 | |
| 2069 | private: |
| 2070 | DISALLOW_COPY_AND_ASSIGN(HGreaterThan); |
| 2071 | }; |
| 2072 | |
| 2073 | class HGreaterThanOrEqual : public HCondition { |
| 2074 | public: |
| 2075 | HGreaterThanOrEqual(HInstruction* first, HInstruction* second) |
| 2076 | : HCondition(first, second) {} |
| 2077 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2078 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2079 | return x >= y ? 1 : 0; |
| 2080 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2081 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2082 | return x >= y ? 1 : 0; |
| 2083 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2084 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2085 | DECLARE_INSTRUCTION(GreaterThanOrEqual); |
| 2086 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2087 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2088 | return kCondGE; |
| 2089 | } |
| 2090 | |
| 2091 | private: |
| 2092 | DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); |
| 2093 | }; |
| 2094 | |
| 2095 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2096 | // Instruction to check how two inputs compare to each other. |
| 2097 | // Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. |
| 2098 | class HCompare : public HBinaryOperation { |
| 2099 | public: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2100 | // The bias applies for floating point operations and indicates how NaN |
| 2101 | // comparisons are treated: |
| 2102 | enum Bias { |
| 2103 | kNoBias, // bias is not applicable (i.e. for long operation) |
| 2104 | kGtBias, // return 1 for NaN comparisons |
| 2105 | kLtBias, // return -1 for NaN comparisons |
| 2106 | }; |
| 2107 | |
| 2108 | HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias) |
| 2109 | : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2110 | DCHECK_EQ(type, first->GetType()); |
| 2111 | DCHECK_EQ(type, second->GetType()); |
| 2112 | } |
| 2113 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2114 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2115 | return |
| 2116 | x == y ? 0 : |
| 2117 | x > y ? 1 : |
| 2118 | -1; |
| 2119 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2120 | |
| 2121 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2122 | return |
| 2123 | x == y ? 0 : |
| 2124 | x > y ? 1 : |
| 2125 | -1; |
| 2126 | } |
| 2127 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2128 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2129 | return bias_ == other->AsCompare()->bias_; |
| 2130 | } |
| 2131 | |
| 2132 | bool IsGtBias() { return bias_ == kGtBias; } |
| 2133 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2134 | DECLARE_INSTRUCTION(Compare); |
| 2135 | |
| 2136 | private: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2137 | const Bias bias_; |
| 2138 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2139 | DISALLOW_COPY_AND_ASSIGN(HCompare); |
| 2140 | }; |
| 2141 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2142 | // A local in the graph. Corresponds to a Dex register. |
| 2143 | class HLocal : public HTemplateInstruction<0> { |
| 2144 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2145 | explicit HLocal(uint16_t reg_number) |
| 2146 | : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2147 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2148 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2149 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2150 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2151 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2152 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2153 | // The Dex register number. |
| 2154 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2155 | |
| 2156 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 2157 | }; |
| 2158 | |
| 2159 | // Load a given local. The local is an input of this instruction. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2160 | class HLoadLocal : public HExpression<1> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2161 | public: |
Roland Levillain | 5799fc0 | 2014-09-25 12:15:20 +0100 | [diff] [blame] | 2162 | HLoadLocal(HLocal* local, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2163 | : HExpression(type, SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2164 | SetRawInputAt(0, local); |
| 2165 | } |
| 2166 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2167 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2168 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2169 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2170 | |
| 2171 | private: |
| 2172 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 2173 | }; |
| 2174 | |
| 2175 | // Store a value in a given local. This instruction has two inputs: the value |
| 2176 | // and the local. |
| 2177 | class HStoreLocal : public HTemplateInstruction<2> { |
| 2178 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2179 | HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2180 | SetRawInputAt(0, local); |
| 2181 | SetRawInputAt(1, value); |
| 2182 | } |
| 2183 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2184 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2185 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2186 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2187 | |
| 2188 | private: |
| 2189 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 2190 | }; |
| 2191 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2192 | class HConstant : public HExpression<0> { |
| 2193 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2194 | explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {} |
| 2195 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2196 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2197 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2198 | virtual bool IsMinusOne() const { return false; } |
| 2199 | virtual bool IsZero() const { return false; } |
| 2200 | virtual bool IsOne() const { return false; } |
| 2201 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2202 | DECLARE_INSTRUCTION(Constant); |
| 2203 | |
| 2204 | private: |
| 2205 | DISALLOW_COPY_AND_ASSIGN(HConstant); |
| 2206 | }; |
| 2207 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2208 | class HFloatConstant : public HConstant { |
| 2209 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2210 | float GetValue() const { return value_; } |
| 2211 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2212 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2213 | return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) == |
| 2214 | bit_cast<uint32_t, float>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2215 | } |
| 2216 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2217 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2218 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2219 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2220 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f)); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2221 | } |
| 2222 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2223 | return value_ == 0.0f; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2224 | } |
| 2225 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2226 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f); |
| 2227 | } |
| 2228 | bool IsNaN() const { |
| 2229 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2232 | DECLARE_INSTRUCTION(FloatConstant); |
| 2233 | |
| 2234 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2235 | explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {} |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2236 | explicit HFloatConstant(int32_t value) |
| 2237 | : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2238 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2239 | const float value_; |
| 2240 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2241 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2242 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2243 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2244 | DISALLOW_COPY_AND_ASSIGN(HFloatConstant); |
| 2245 | }; |
| 2246 | |
| 2247 | class HDoubleConstant : public HConstant { |
| 2248 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2249 | double GetValue() const { return value_; } |
| 2250 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2251 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2252 | return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) == |
| 2253 | bit_cast<uint64_t, double>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2254 | } |
| 2255 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2256 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2257 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2258 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2259 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0)); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2260 | } |
| 2261 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2262 | return value_ == 0.0; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2263 | } |
| 2264 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2265 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0); |
| 2266 | } |
| 2267 | bool IsNaN() const { |
| 2268 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2271 | DECLARE_INSTRUCTION(DoubleConstant); |
| 2272 | |
| 2273 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2274 | explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {} |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2275 | explicit HDoubleConstant(int64_t value) |
| 2276 | : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2277 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2278 | const double value_; |
| 2279 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2280 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2281 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2282 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2283 | DISALLOW_COPY_AND_ASSIGN(HDoubleConstant); |
| 2284 | }; |
| 2285 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2286 | class HNullConstant : public HConstant { |
| 2287 | public: |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2288 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2289 | return true; |
| 2290 | } |
| 2291 | |
| 2292 | size_t ComputeHashCode() const OVERRIDE { return 0; } |
| 2293 | |
| 2294 | DECLARE_INSTRUCTION(NullConstant); |
| 2295 | |
| 2296 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2297 | HNullConstant() : HConstant(Primitive::kPrimNot) {} |
| 2298 | |
| 2299 | friend class HGraph; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2300 | DISALLOW_COPY_AND_ASSIGN(HNullConstant); |
| 2301 | }; |
| 2302 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2303 | // Constants of the type int. Those can be from Dex instructions, or |
| 2304 | // synthesized (for example with the if-eqz instruction). |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2305 | class HIntConstant : public HConstant { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2306 | public: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2307 | int32_t GetValue() const { return value_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2308 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 2309 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2310 | return other->AsIntConstant()->value_ == value_; |
| 2311 | } |
| 2312 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 2313 | size_t ComputeHashCode() const OVERRIDE { return GetValue(); } |
| 2314 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2315 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2316 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2317 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2318 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2319 | DECLARE_INSTRUCTION(IntConstant); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2320 | |
| 2321 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2322 | explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {} |
| 2323 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2324 | const int32_t value_; |
| 2325 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2326 | friend class HGraph; |
| 2327 | ART_FRIEND_TEST(GraphTest, InsertInstructionBefore); |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 2328 | ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2329 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 2330 | }; |
| 2331 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2332 | class HLongConstant : public HConstant { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2333 | public: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2334 | int64_t GetValue() const { return value_; } |
| 2335 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2336 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2337 | return other->AsLongConstant()->value_ == value_; |
| 2338 | } |
| 2339 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2340 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 2341 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2342 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2343 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2344 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2345 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2346 | DECLARE_INSTRUCTION(LongConstant); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2347 | |
| 2348 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2349 | explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {} |
| 2350 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2351 | const int64_t value_; |
| 2352 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2353 | friend class HGraph; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2354 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 2355 | }; |
| 2356 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2357 | enum class Intrinsics { |
| 2358 | #define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name, |
| 2359 | #include "intrinsics_list.h" |
| 2360 | kNone, |
| 2361 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 2362 | #undef INTRINSICS_LIST |
| 2363 | #undef OPTIMIZING_INTRINSICS |
| 2364 | }; |
| 2365 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic); |
| 2366 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2367 | class HInvoke : public HInstruction { |
| 2368 | public: |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2369 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2370 | |
| 2371 | // Runtime needs to walk the stack, so Dex -> Dex calls need to |
| 2372 | // know their environment. |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2373 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2374 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 2375 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2376 | SetRawInputAt(index, argument); |
| 2377 | } |
| 2378 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2379 | // Return the number of arguments. This number can be lower than |
| 2380 | // the number of inputs returned by InputCount(), as some invoke |
| 2381 | // instructions (e.g. HInvokeStaticOrDirect) can have non-argument |
| 2382 | // inputs at the end of their list of inputs. |
| 2383 | uint32_t GetNumberOfArguments() const { return number_of_arguments_; } |
| 2384 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2385 | Primitive::Type GetType() const OVERRIDE { return return_type_; } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2386 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2387 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2388 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2389 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 2390 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2391 | InvokeType GetOriginalInvokeType() const { return original_invoke_type_; } |
| 2392 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 2393 | Intrinsics GetIntrinsic() const { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2394 | return intrinsic_; |
| 2395 | } |
| 2396 | |
| 2397 | void SetIntrinsic(Intrinsics intrinsic) { |
| 2398 | intrinsic_ = intrinsic; |
| 2399 | } |
| 2400 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2401 | DECLARE_INSTRUCTION(Invoke); |
| 2402 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2403 | protected: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2404 | HInvoke(ArenaAllocator* arena, |
| 2405 | uint32_t number_of_arguments, |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2406 | uint32_t number_of_other_inputs, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2407 | Primitive::Type return_type, |
| 2408 | uint32_t dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2409 | uint32_t dex_method_index, |
| 2410 | InvokeType original_invoke_type) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2411 | : HInstruction(SideEffects::All()), |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2412 | number_of_arguments_(number_of_arguments), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2413 | inputs_(arena, number_of_arguments), |
| 2414 | return_type_(return_type), |
| 2415 | dex_pc_(dex_pc), |
| 2416 | dex_method_index_(dex_method_index), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2417 | original_invoke_type_(original_invoke_type), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2418 | intrinsic_(Intrinsics::kNone) { |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2419 | uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs; |
| 2420 | inputs_.SetSize(number_of_inputs); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2421 | } |
| 2422 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2423 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 2424 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 2425 | inputs_.Put(index, input); |
| 2426 | } |
| 2427 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2428 | uint32_t number_of_arguments_; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2429 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2430 | const Primitive::Type return_type_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2431 | const uint32_t dex_pc_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2432 | const uint32_t dex_method_index_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2433 | const InvokeType original_invoke_type_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2434 | Intrinsics intrinsic_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2435 | |
| 2436 | private: |
| 2437 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 2438 | }; |
| 2439 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2440 | class HInvokeStaticOrDirect : public HInvoke { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2441 | public: |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2442 | // Requirements of this method call regarding the class |
| 2443 | // initialization (clinit) check of its declaring class. |
| 2444 | enum class ClinitCheckRequirement { |
| 2445 | kNone, // Class already initialized. |
| 2446 | kExplicit, // Static call having explicit clinit check as last input. |
| 2447 | kImplicit, // Static call implicitly requiring a clinit check. |
| 2448 | }; |
| 2449 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2450 | HInvokeStaticOrDirect(ArenaAllocator* arena, |
| 2451 | uint32_t number_of_arguments, |
| 2452 | Primitive::Type return_type, |
| 2453 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2454 | uint32_t dex_method_index, |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2455 | bool is_recursive, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2456 | int32_t string_init_offset, |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 2457 | InvokeType original_invoke_type, |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2458 | InvokeType invoke_type, |
| 2459 | ClinitCheckRequirement clinit_check_requirement) |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2460 | : HInvoke(arena, |
| 2461 | number_of_arguments, |
| 2462 | clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u, |
| 2463 | return_type, |
| 2464 | dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2465 | dex_method_index, |
| 2466 | original_invoke_type), |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2467 | invoke_type_(invoke_type), |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2468 | is_recursive_(is_recursive), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2469 | clinit_check_requirement_(clinit_check_requirement), |
| 2470 | string_init_offset_(string_init_offset) {} |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2471 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2472 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 2473 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2474 | // We access the method via the dex cache so we can't do an implicit null check. |
| 2475 | // TODO: for intrinsics we can generate implicit null checks. |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2479 | InvokeType GetInvokeType() const { return invoke_type_; } |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2480 | bool IsRecursive() const { return is_recursive_; } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 2481 | bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2482 | bool IsStringInit() const { return string_init_offset_ != 0; } |
| 2483 | int32_t GetStringInitOffset() const { return string_init_offset_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2484 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2485 | // Is this instruction a call to a static method? |
| 2486 | bool IsStatic() const { |
| 2487 | return GetInvokeType() == kStatic; |
| 2488 | } |
| 2489 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2490 | // Remove the art::HLoadClass instruction set as last input by |
| 2491 | // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of |
| 2492 | // the initial art::HClinitCheck instruction (only relevant for |
| 2493 | // static calls with explicit clinit check). |
| 2494 | void RemoveLoadClassAsLastInput() { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2495 | DCHECK(IsStaticWithExplicitClinitCheck()); |
| 2496 | size_t last_input_index = InputCount() - 1; |
| 2497 | HInstruction* last_input = InputAt(last_input_index); |
| 2498 | DCHECK(last_input != nullptr); |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2499 | DCHECK(last_input->IsLoadClass()) << last_input->DebugName(); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2500 | RemoveAsUserOfInput(last_input_index); |
| 2501 | inputs_.DeleteAt(last_input_index); |
| 2502 | clinit_check_requirement_ = ClinitCheckRequirement::kImplicit; |
| 2503 | DCHECK(IsStaticWithImplicitClinitCheck()); |
| 2504 | } |
| 2505 | |
| 2506 | // Is this a call to a static method whose declaring class has an |
| 2507 | // explicit intialization check in the graph? |
| 2508 | bool IsStaticWithExplicitClinitCheck() const { |
| 2509 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit); |
| 2510 | } |
| 2511 | |
| 2512 | // Is this a call to a static method whose declaring class has an |
| 2513 | // implicit intialization check requirement? |
| 2514 | bool IsStaticWithImplicitClinitCheck() const { |
| 2515 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit); |
| 2516 | } |
| 2517 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2518 | DECLARE_INSTRUCTION(InvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2519 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2520 | protected: |
| 2521 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 2522 | const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i); |
| 2523 | if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) { |
| 2524 | HInstruction* input = input_record.GetInstruction(); |
| 2525 | // `input` is the last input of a static invoke marked as having |
| 2526 | // an explicit clinit check. It must either be: |
| 2527 | // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or |
| 2528 | // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation. |
| 2529 | DCHECK(input != nullptr); |
| 2530 | DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName(); |
| 2531 | } |
| 2532 | return input_record; |
| 2533 | } |
| 2534 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2535 | private: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2536 | const InvokeType invoke_type_; |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2537 | const bool is_recursive_; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2538 | ClinitCheckRequirement clinit_check_requirement_; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2539 | // Thread entrypoint offset for string init method if this is a string init invoke. |
| 2540 | // Note that there are multiple string init methods, each having its own offset. |
| 2541 | int32_t string_init_offset_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2542 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2543 | DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2544 | }; |
| 2545 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2546 | class HInvokeVirtual : public HInvoke { |
| 2547 | public: |
| 2548 | HInvokeVirtual(ArenaAllocator* arena, |
| 2549 | uint32_t number_of_arguments, |
| 2550 | Primitive::Type return_type, |
| 2551 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2552 | uint32_t dex_method_index, |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2553 | uint32_t vtable_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2554 | : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual), |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2555 | vtable_index_(vtable_index) {} |
| 2556 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2557 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2558 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2559 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2562 | uint32_t GetVTableIndex() const { return vtable_index_; } |
| 2563 | |
| 2564 | DECLARE_INSTRUCTION(InvokeVirtual); |
| 2565 | |
| 2566 | private: |
| 2567 | const uint32_t vtable_index_; |
| 2568 | |
| 2569 | DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); |
| 2570 | }; |
| 2571 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2572 | class HInvokeInterface : public HInvoke { |
| 2573 | public: |
| 2574 | HInvokeInterface(ArenaAllocator* arena, |
| 2575 | uint32_t number_of_arguments, |
| 2576 | Primitive::Type return_type, |
| 2577 | uint32_t dex_pc, |
| 2578 | uint32_t dex_method_index, |
| 2579 | uint32_t imt_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2580 | : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface), |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2581 | imt_index_(imt_index) {} |
| 2582 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2583 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2584 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2585 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2588 | uint32_t GetImtIndex() const { return imt_index_; } |
| 2589 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 2590 | |
| 2591 | DECLARE_INSTRUCTION(InvokeInterface); |
| 2592 | |
| 2593 | private: |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2594 | const uint32_t imt_index_; |
| 2595 | |
| 2596 | DISALLOW_COPY_AND_ASSIGN(HInvokeInterface); |
| 2597 | }; |
| 2598 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2599 | class HNewInstance : public HExpression<0> { |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2600 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2601 | HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2602 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2603 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2604 | type_index_(type_index), |
| 2605 | entrypoint_(entrypoint) {} |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2606 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2607 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2608 | uint16_t GetTypeIndex() const { return type_index_; } |
| 2609 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2610 | // Calls runtime so needs an environment. |
Calin Juravle | 92a6ed2 | 2014-12-02 18:58:03 +0000 | [diff] [blame] | 2611 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2612 | // It may throw when called on: |
| 2613 | // - interfaces |
| 2614 | // - abstract/innaccessible/unknown classes |
| 2615 | // TODO: optimize when possible. |
| 2616 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2617 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2618 | bool CanBeNull() const OVERRIDE { return false; } |
| 2619 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2620 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2621 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2622 | DECLARE_INSTRUCTION(NewInstance); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2623 | |
| 2624 | private: |
| 2625 | const uint32_t dex_pc_; |
| 2626 | const uint16_t type_index_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2627 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2628 | |
| 2629 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 2630 | }; |
| 2631 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2632 | class HNeg : public HUnaryOperation { |
| 2633 | public: |
| 2634 | explicit HNeg(Primitive::Type result_type, HInstruction* input) |
| 2635 | : HUnaryOperation(result_type, input) {} |
| 2636 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2637 | int32_t Evaluate(int32_t x) const OVERRIDE { return -x; } |
| 2638 | int64_t Evaluate(int64_t x) const OVERRIDE { return -x; } |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2639 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2640 | DECLARE_INSTRUCTION(Neg); |
| 2641 | |
| 2642 | private: |
| 2643 | DISALLOW_COPY_AND_ASSIGN(HNeg); |
| 2644 | }; |
| 2645 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2646 | class HNewArray : public HExpression<1> { |
| 2647 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2648 | HNewArray(HInstruction* length, |
| 2649 | uint32_t dex_pc, |
| 2650 | uint16_t type_index, |
| 2651 | QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2652 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2653 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2654 | type_index_(type_index), |
| 2655 | entrypoint_(entrypoint) { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2656 | SetRawInputAt(0, length); |
| 2657 | } |
| 2658 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2659 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2660 | uint16_t GetTypeIndex() const { return type_index_; } |
| 2661 | |
| 2662 | // Calls runtime so needs an environment. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2663 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2664 | |
Mingyao Yang | 0c365e6 | 2015-03-31 15:09:29 -0700 | [diff] [blame] | 2665 | // May throw NegativeArraySizeException, OutOfMemoryError, etc. |
| 2666 | bool CanThrow() const OVERRIDE { return true; } |
| 2667 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2668 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2669 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2670 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2671 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2672 | DECLARE_INSTRUCTION(NewArray); |
| 2673 | |
| 2674 | private: |
| 2675 | const uint32_t dex_pc_; |
| 2676 | const uint16_t type_index_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2677 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2678 | |
| 2679 | DISALLOW_COPY_AND_ASSIGN(HNewArray); |
| 2680 | }; |
| 2681 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2682 | class HAdd : public HBinaryOperation { |
| 2683 | public: |
| 2684 | HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2685 | : HBinaryOperation(result_type, left, right) {} |
| 2686 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2687 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2688 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2689 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2690 | return x + y; |
| 2691 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2692 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2693 | return x + y; |
| 2694 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2695 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2696 | DECLARE_INSTRUCTION(Add); |
| 2697 | |
| 2698 | private: |
| 2699 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 2700 | }; |
| 2701 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2702 | class HSub : public HBinaryOperation { |
| 2703 | public: |
| 2704 | HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2705 | : HBinaryOperation(result_type, left, right) {} |
| 2706 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2707 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2708 | return x - y; |
| 2709 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2710 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2711 | return x - y; |
| 2712 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2713 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2714 | DECLARE_INSTRUCTION(Sub); |
| 2715 | |
| 2716 | private: |
| 2717 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 2718 | }; |
| 2719 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2720 | class HMul : public HBinaryOperation { |
| 2721 | public: |
| 2722 | HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2723 | : HBinaryOperation(result_type, left, right) {} |
| 2724 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2725 | bool IsCommutative() const OVERRIDE { return true; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2726 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2727 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; } |
| 2728 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2729 | |
| 2730 | DECLARE_INSTRUCTION(Mul); |
| 2731 | |
| 2732 | private: |
| 2733 | DISALLOW_COPY_AND_ASSIGN(HMul); |
| 2734 | }; |
| 2735 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2736 | class HDiv : public HBinaryOperation { |
| 2737 | public: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2738 | HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2739 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2740 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2741 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2742 | // Our graph structure ensures we never have 0 for `y` during constant folding. |
| 2743 | DCHECK_NE(y, 0); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2744 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2745 | return (y == -1) ? -x : x / y; |
| 2746 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2747 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2748 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2749 | DCHECK_NE(y, 0); |
| 2750 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2751 | return (y == -1) ? -x : x / y; |
| 2752 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2753 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2754 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2755 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2756 | DECLARE_INSTRUCTION(Div); |
| 2757 | |
| 2758 | private: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2759 | const uint32_t dex_pc_; |
| 2760 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2761 | DISALLOW_COPY_AND_ASSIGN(HDiv); |
| 2762 | }; |
| 2763 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2764 | class HRem : public HBinaryOperation { |
| 2765 | public: |
| 2766 | HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2767 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
| 2768 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2769 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2770 | DCHECK_NE(y, 0); |
| 2771 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2772 | return (y == -1) ? 0 : x % y; |
| 2773 | } |
| 2774 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2775 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2776 | DCHECK_NE(y, 0); |
| 2777 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2778 | return (y == -1) ? 0 : x % y; |
| 2779 | } |
| 2780 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2781 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2782 | |
| 2783 | DECLARE_INSTRUCTION(Rem); |
| 2784 | |
| 2785 | private: |
| 2786 | const uint32_t dex_pc_; |
| 2787 | |
| 2788 | DISALLOW_COPY_AND_ASSIGN(HRem); |
| 2789 | }; |
| 2790 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 2791 | class HDivZeroCheck : public HExpression<1> { |
| 2792 | public: |
| 2793 | HDivZeroCheck(HInstruction* value, uint32_t dex_pc) |
| 2794 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
| 2795 | SetRawInputAt(0, value); |
| 2796 | } |
| 2797 | |
| 2798 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2799 | |
| 2800 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2801 | UNUSED(other); |
| 2802 | return true; |
| 2803 | } |
| 2804 | |
| 2805 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2806 | bool CanThrow() const OVERRIDE { return true; } |
| 2807 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2808 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 2809 | |
| 2810 | DECLARE_INSTRUCTION(DivZeroCheck); |
| 2811 | |
| 2812 | private: |
| 2813 | const uint32_t dex_pc_; |
| 2814 | |
| 2815 | DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck); |
| 2816 | }; |
| 2817 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 2818 | class HShl : public HBinaryOperation { |
| 2819 | public: |
| 2820 | HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2821 | : HBinaryOperation(result_type, left, right) {} |
| 2822 | |
| 2823 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); } |
| 2824 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); } |
| 2825 | |
| 2826 | DECLARE_INSTRUCTION(Shl); |
| 2827 | |
| 2828 | private: |
| 2829 | DISALLOW_COPY_AND_ASSIGN(HShl); |
| 2830 | }; |
| 2831 | |
| 2832 | class HShr : public HBinaryOperation { |
| 2833 | public: |
| 2834 | HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2835 | : HBinaryOperation(result_type, left, right) {} |
| 2836 | |
| 2837 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); } |
| 2838 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); } |
| 2839 | |
| 2840 | DECLARE_INSTRUCTION(Shr); |
| 2841 | |
| 2842 | private: |
| 2843 | DISALLOW_COPY_AND_ASSIGN(HShr); |
| 2844 | }; |
| 2845 | |
| 2846 | class HUShr : public HBinaryOperation { |
| 2847 | public: |
| 2848 | HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2849 | : HBinaryOperation(result_type, left, right) {} |
| 2850 | |
| 2851 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 2852 | uint32_t ux = static_cast<uint32_t>(x); |
| 2853 | uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue; |
| 2854 | return static_cast<int32_t>(ux >> uy); |
| 2855 | } |
| 2856 | |
| 2857 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 2858 | uint64_t ux = static_cast<uint64_t>(x); |
| 2859 | uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue; |
| 2860 | return static_cast<int64_t>(ux >> uy); |
| 2861 | } |
| 2862 | |
| 2863 | DECLARE_INSTRUCTION(UShr); |
| 2864 | |
| 2865 | private: |
| 2866 | DISALLOW_COPY_AND_ASSIGN(HUShr); |
| 2867 | }; |
| 2868 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2869 | class HAnd : public HBinaryOperation { |
| 2870 | public: |
| 2871 | HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2872 | : HBinaryOperation(result_type, left, right) {} |
| 2873 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2874 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2875 | |
| 2876 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; } |
| 2877 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; } |
| 2878 | |
| 2879 | DECLARE_INSTRUCTION(And); |
| 2880 | |
| 2881 | private: |
| 2882 | DISALLOW_COPY_AND_ASSIGN(HAnd); |
| 2883 | }; |
| 2884 | |
| 2885 | class HOr : public HBinaryOperation { |
| 2886 | public: |
| 2887 | HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2888 | : HBinaryOperation(result_type, left, right) {} |
| 2889 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2890 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2891 | |
| 2892 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; } |
| 2893 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; } |
| 2894 | |
| 2895 | DECLARE_INSTRUCTION(Or); |
| 2896 | |
| 2897 | private: |
| 2898 | DISALLOW_COPY_AND_ASSIGN(HOr); |
| 2899 | }; |
| 2900 | |
| 2901 | class HXor : public HBinaryOperation { |
| 2902 | public: |
| 2903 | HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2904 | : HBinaryOperation(result_type, left, right) {} |
| 2905 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2906 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2907 | |
| 2908 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; } |
| 2909 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; } |
| 2910 | |
| 2911 | DECLARE_INSTRUCTION(Xor); |
| 2912 | |
| 2913 | private: |
| 2914 | DISALLOW_COPY_AND_ASSIGN(HXor); |
| 2915 | }; |
| 2916 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2917 | // The value of a parameter in this method. Its location depends on |
| 2918 | // the calling convention. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2919 | class HParameterValue : public HExpression<0> { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2920 | public: |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2921 | HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false) |
| 2922 | : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2923 | |
| 2924 | uint8_t GetIndex() const { return index_; } |
| 2925 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2926 | bool CanBeNull() const OVERRIDE { return !is_this_; } |
| 2927 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 2928 | bool IsThis() const { return is_this_; } |
| 2929 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2930 | DECLARE_INSTRUCTION(ParameterValue); |
| 2931 | |
| 2932 | private: |
| 2933 | // The index of this parameter in the parameters list. Must be less |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2934 | // than HGraph::number_of_in_vregs_. |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2935 | const uint8_t index_; |
| 2936 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2937 | // Whether or not the parameter value corresponds to 'this' argument. |
| 2938 | const bool is_this_; |
| 2939 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2940 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 2941 | }; |
| 2942 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2943 | class HNot : public HUnaryOperation { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2944 | public: |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2945 | explicit HNot(Primitive::Type result_type, HInstruction* input) |
| 2946 | : HUnaryOperation(result_type, input) {} |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2947 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2948 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2949 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2950 | UNUSED(other); |
| 2951 | return true; |
| 2952 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2953 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2954 | int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; } |
| 2955 | int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; } |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 2956 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 2957 | DECLARE_INSTRUCTION(Not); |
| 2958 | |
| 2959 | private: |
| 2960 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 2961 | }; |
| 2962 | |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 2963 | class HBooleanNot : public HUnaryOperation { |
| 2964 | public: |
| 2965 | explicit HBooleanNot(HInstruction* input) |
| 2966 | : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {} |
| 2967 | |
| 2968 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2969 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2970 | UNUSED(other); |
| 2971 | return true; |
| 2972 | } |
| 2973 | |
| 2974 | int32_t Evaluate(int32_t x) const OVERRIDE { |
| 2975 | DCHECK(IsUint<1>(x)); |
| 2976 | return !x; |
| 2977 | } |
| 2978 | |
| 2979 | int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2980 | LOG(FATAL) << DebugName() << " cannot be used with 64-bit values"; |
| 2981 | UNREACHABLE(); |
| 2982 | } |
| 2983 | |
| 2984 | DECLARE_INSTRUCTION(BooleanNot); |
| 2985 | |
| 2986 | private: |
| 2987 | DISALLOW_COPY_AND_ASSIGN(HBooleanNot); |
| 2988 | }; |
| 2989 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2990 | class HTypeConversion : public HExpression<1> { |
| 2991 | public: |
| 2992 | // Instantiate a type conversion of `input` to `result_type`. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 2993 | HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc) |
| 2994 | : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) { |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 2995 | SetRawInputAt(0, input); |
| 2996 | DCHECK_NE(input->GetType(), result_type); |
| 2997 | } |
| 2998 | |
| 2999 | HInstruction* GetInput() const { return InputAt(0); } |
| 3000 | Primitive::Type GetInputType() const { return GetInput()->GetType(); } |
| 3001 | Primitive::Type GetResultType() const { return GetType(); } |
| 3002 | |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3003 | // Required by the x86 and ARM code generators when producing calls |
| 3004 | // to the runtime. |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3005 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3006 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3007 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | ed9b195 | 2014-11-06 11:10:17 +0000 | [diff] [blame] | 3008 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3009 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 3010 | // Try to statically evaluate the conversion and return a HConstant |
| 3011 | // containing the result. If the input cannot be converted, return nullptr. |
| 3012 | HConstant* TryStaticEvaluation() const; |
| 3013 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3014 | DECLARE_INSTRUCTION(TypeConversion); |
| 3015 | |
| 3016 | private: |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3017 | const uint32_t dex_pc_; |
| 3018 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3019 | DISALLOW_COPY_AND_ASSIGN(HTypeConversion); |
| 3020 | }; |
| 3021 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 3022 | static constexpr uint32_t kNoRegNumber = -1; |
| 3023 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3024 | class HPhi : public HInstruction { |
| 3025 | public: |
| 3026 | 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] | 3027 | : HInstruction(SideEffects::None()), |
| 3028 | inputs_(arena, number_of_inputs), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3029 | reg_number_(reg_number), |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3030 | type_(type), |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3031 | is_live_(false), |
| 3032 | can_be_null_(true) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3033 | inputs_.SetSize(number_of_inputs); |
| 3034 | } |
| 3035 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 3036 | // Returns a type equivalent to the given `type`, but that a `HPhi` can hold. |
| 3037 | static Primitive::Type ToPhiType(Primitive::Type type) { |
| 3038 | switch (type) { |
| 3039 | case Primitive::kPrimBoolean: |
| 3040 | case Primitive::kPrimByte: |
| 3041 | case Primitive::kPrimShort: |
| 3042 | case Primitive::kPrimChar: |
| 3043 | return Primitive::kPrimInt; |
| 3044 | default: |
| 3045 | return type; |
| 3046 | } |
| 3047 | } |
| 3048 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3049 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3050 | |
| 3051 | void AddInput(HInstruction* input); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 3052 | void RemoveInputAt(size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3053 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3054 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3055 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3056 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3057 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 3058 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
| 3059 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3060 | uint32_t GetRegNumber() const { return reg_number_; } |
| 3061 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3062 | void SetDead() { is_live_ = false; } |
| 3063 | void SetLive() { is_live_ = true; } |
| 3064 | bool IsDead() const { return !is_live_; } |
| 3065 | bool IsLive() const { return is_live_; } |
| 3066 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 3067 | // Returns the next equivalent phi (starting from the current one) or null if there is none. |
| 3068 | // An equivalent phi is a phi having the same dex register and type. |
| 3069 | // It assumes that phis with the same dex register are adjacent. |
| 3070 | HPhi* GetNextEquivalentPhiWithSameType() { |
| 3071 | HInstruction* next = GetNext(); |
| 3072 | while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) { |
| 3073 | if (next->GetType() == GetType()) { |
| 3074 | return next->AsPhi(); |
| 3075 | } |
| 3076 | next = next->GetNext(); |
| 3077 | } |
| 3078 | return nullptr; |
| 3079 | } |
| 3080 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3081 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3082 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3083 | protected: |
| 3084 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 3085 | |
| 3086 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 3087 | inputs_.Put(index, input); |
| 3088 | } |
| 3089 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3090 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3091 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3092 | const uint32_t reg_number_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3093 | Primitive::Type type_; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3094 | bool is_live_; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3095 | bool can_be_null_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3096 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3097 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 3098 | }; |
| 3099 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3100 | class HNullCheck : public HExpression<1> { |
| 3101 | public: |
| 3102 | HNullCheck(HInstruction* value, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3103 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3104 | SetRawInputAt(0, value); |
| 3105 | } |
| 3106 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3107 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3108 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3109 | UNUSED(other); |
| 3110 | return true; |
| 3111 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3112 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3113 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3114 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3115 | bool CanThrow() const OVERRIDE { return true; } |
| 3116 | |
| 3117 | bool CanBeNull() const OVERRIDE { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 3118 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3119 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3120 | |
| 3121 | DECLARE_INSTRUCTION(NullCheck); |
| 3122 | |
| 3123 | private: |
| 3124 | const uint32_t dex_pc_; |
| 3125 | |
| 3126 | DISALLOW_COPY_AND_ASSIGN(HNullCheck); |
| 3127 | }; |
| 3128 | |
| 3129 | class FieldInfo : public ValueObject { |
| 3130 | public: |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3131 | FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile) |
| 3132 | : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3133 | |
| 3134 | MemberOffset GetFieldOffset() const { return field_offset_; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3135 | Primitive::Type GetFieldType() const { return field_type_; } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3136 | bool IsVolatile() const { return is_volatile_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3137 | |
| 3138 | private: |
| 3139 | const MemberOffset field_offset_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3140 | const Primitive::Type field_type_; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3141 | const bool is_volatile_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3142 | }; |
| 3143 | |
| 3144 | class HInstanceFieldGet : public HExpression<1> { |
| 3145 | public: |
| 3146 | HInstanceFieldGet(HInstruction* value, |
| 3147 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3148 | MemberOffset field_offset, |
| 3149 | bool is_volatile) |
Nicolas Geoffray | 2af2307 | 2015-04-30 11:15:40 +0000 | [diff] [blame] | 3150 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3151 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3152 | SetRawInputAt(0, value); |
| 3153 | } |
| 3154 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 3155 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3156 | |
| 3157 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3158 | HInstanceFieldGet* other_get = other->AsInstanceFieldGet(); |
| 3159 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3162 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3163 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | size_t ComputeHashCode() const OVERRIDE { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 3167 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 3168 | } |
| 3169 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3170 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3171 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3172 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3173 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3174 | |
| 3175 | DECLARE_INSTRUCTION(InstanceFieldGet); |
| 3176 | |
| 3177 | private: |
| 3178 | const FieldInfo field_info_; |
| 3179 | |
| 3180 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); |
| 3181 | }; |
| 3182 | |
| 3183 | class HInstanceFieldSet : public HTemplateInstruction<2> { |
| 3184 | public: |
| 3185 | HInstanceFieldSet(HInstruction* object, |
| 3186 | HInstruction* value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3187 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3188 | MemberOffset field_offset, |
| 3189 | bool is_volatile) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3190 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3191 | field_info_(field_offset, field_type, is_volatile), |
| 3192 | value_can_be_null_(true) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3193 | SetRawInputAt(0, object); |
| 3194 | SetRawInputAt(1, value); |
| 3195 | } |
| 3196 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3197 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3198 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3201 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3202 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3203 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3204 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3205 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3206 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 3207 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3208 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3209 | DECLARE_INSTRUCTION(InstanceFieldSet); |
| 3210 | |
| 3211 | private: |
| 3212 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3213 | bool value_can_be_null_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3214 | |
| 3215 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); |
| 3216 | }; |
| 3217 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3218 | class HArrayGet : public HExpression<2> { |
| 3219 | public: |
| 3220 | HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3221 | : HExpression(type, SideEffects::DependsOnSomething()) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3222 | SetRawInputAt(0, array); |
| 3223 | SetRawInputAt(1, index); |
| 3224 | } |
| 3225 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3226 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3227 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3228 | UNUSED(other); |
| 3229 | return true; |
| 3230 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3231 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3232 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3233 | // TODO: We can be smarter here. |
| 3234 | // Currently, the array access is always preceded by an ArrayLength or a NullCheck |
| 3235 | // which generates the implicit null check. There are cases when these can be removed |
| 3236 | // to produce better code. If we ever add optimizations to do so we should allow an |
| 3237 | // implicit check here (as long as the address falls in the first page). |
| 3238 | return false; |
| 3239 | } |
| 3240 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3241 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3242 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3243 | HInstruction* GetArray() const { return InputAt(0); } |
| 3244 | HInstruction* GetIndex() const { return InputAt(1); } |
| 3245 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3246 | DECLARE_INSTRUCTION(ArrayGet); |
| 3247 | |
| 3248 | private: |
| 3249 | DISALLOW_COPY_AND_ASSIGN(HArrayGet); |
| 3250 | }; |
| 3251 | |
| 3252 | class HArraySet : public HTemplateInstruction<3> { |
| 3253 | public: |
| 3254 | HArraySet(HInstruction* array, |
| 3255 | HInstruction* index, |
| 3256 | HInstruction* value, |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3257 | Primitive::Type expected_component_type, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3258 | uint32_t dex_pc) |
| 3259 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
| 3260 | dex_pc_(dex_pc), |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3261 | expected_component_type_(expected_component_type), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3262 | needs_type_check_(value->GetType() == Primitive::kPrimNot), |
| 3263 | value_can_be_null_(true) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3264 | SetRawInputAt(0, array); |
| 3265 | SetRawInputAt(1, index); |
| 3266 | SetRawInputAt(2, value); |
| 3267 | } |
| 3268 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3269 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3270 | // We currently always call a runtime method to catch array store |
| 3271 | // exceptions. |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3272 | return needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3273 | } |
| 3274 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3275 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3276 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3277 | // TODO: Same as for ArrayGet. |
| 3278 | return false; |
| 3279 | } |
| 3280 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3281 | void ClearNeedsTypeCheck() { |
| 3282 | needs_type_check_ = false; |
| 3283 | } |
| 3284 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3285 | void ClearValueCanBeNull() { |
| 3286 | value_can_be_null_ = false; |
| 3287 | } |
| 3288 | |
| 3289 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3290 | bool NeedsTypeCheck() const { return needs_type_check_; } |
| 3291 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3292 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3293 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3294 | HInstruction* GetArray() const { return InputAt(0); } |
| 3295 | HInstruction* GetIndex() const { return InputAt(1); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3296 | HInstruction* GetValue() const { return InputAt(2); } |
| 3297 | |
| 3298 | Primitive::Type GetComponentType() const { |
| 3299 | // The Dex format does not type floating point index operations. Since the |
| 3300 | // `expected_component_type_` is set during building and can therefore not |
| 3301 | // be correct, we also check what is the value type. If it is a floating |
| 3302 | // point type, we must use that type. |
| 3303 | Primitive::Type value_type = GetValue()->GetType(); |
| 3304 | return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble)) |
| 3305 | ? value_type |
| 3306 | : expected_component_type_; |
| 3307 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3308 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3309 | DECLARE_INSTRUCTION(ArraySet); |
| 3310 | |
| 3311 | private: |
| 3312 | const uint32_t dex_pc_; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3313 | const Primitive::Type expected_component_type_; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3314 | bool needs_type_check_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3315 | bool value_can_be_null_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3316 | |
| 3317 | DISALLOW_COPY_AND_ASSIGN(HArraySet); |
| 3318 | }; |
| 3319 | |
| 3320 | class HArrayLength : public HExpression<1> { |
| 3321 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3322 | explicit HArrayLength(HInstruction* array) |
| 3323 | : HExpression(Primitive::kPrimInt, SideEffects::None()) { |
| 3324 | // Note that arrays do not change length, so the instruction does not |
| 3325 | // depend on any write. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3326 | SetRawInputAt(0, array); |
| 3327 | } |
| 3328 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3329 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3330 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3331 | UNUSED(other); |
| 3332 | return true; |
| 3333 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3334 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3335 | return obj == InputAt(0); |
| 3336 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3337 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3338 | DECLARE_INSTRUCTION(ArrayLength); |
| 3339 | |
| 3340 | private: |
| 3341 | DISALLOW_COPY_AND_ASSIGN(HArrayLength); |
| 3342 | }; |
| 3343 | |
| 3344 | class HBoundsCheck : public HExpression<2> { |
| 3345 | public: |
| 3346 | HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3347 | : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3348 | DCHECK(index->GetType() == Primitive::kPrimInt); |
| 3349 | SetRawInputAt(0, index); |
| 3350 | SetRawInputAt(1, length); |
| 3351 | } |
| 3352 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3353 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3354 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3355 | UNUSED(other); |
| 3356 | return true; |
| 3357 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3358 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3359 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3360 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3361 | bool CanThrow() const OVERRIDE { return true; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 3362 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3363 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3364 | |
| 3365 | DECLARE_INSTRUCTION(BoundsCheck); |
| 3366 | |
| 3367 | private: |
| 3368 | const uint32_t dex_pc_; |
| 3369 | |
| 3370 | DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); |
| 3371 | }; |
| 3372 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3373 | /** |
| 3374 | * Some DEX instructions are folded into multiple HInstructions that need |
| 3375 | * to stay live until the last HInstruction. This class |
| 3376 | * is used as a marker for the baseline compiler to ensure its preceding |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 3377 | * HInstruction stays live. `index` represents the stack location index of the |
| 3378 | * instruction (the actual offset is computed as index * vreg_size). |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3379 | */ |
| 3380 | class HTemporary : public HTemplateInstruction<0> { |
| 3381 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3382 | explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3383 | |
| 3384 | size_t GetIndex() const { return index_; } |
| 3385 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 3386 | Primitive::Type GetType() const OVERRIDE { |
| 3387 | // The previous instruction is the one that will be stored in the temporary location. |
| 3388 | DCHECK(GetPrevious() != nullptr); |
| 3389 | return GetPrevious()->GetType(); |
| 3390 | } |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 3391 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3392 | DECLARE_INSTRUCTION(Temporary); |
| 3393 | |
| 3394 | private: |
| 3395 | const size_t index_; |
| 3396 | |
| 3397 | DISALLOW_COPY_AND_ASSIGN(HTemporary); |
| 3398 | }; |
| 3399 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3400 | class HSuspendCheck : public HTemplateInstruction<0> { |
| 3401 | public: |
| 3402 | explicit HSuspendCheck(uint32_t dex_pc) |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 3403 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3404 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3405 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3406 | return true; |
| 3407 | } |
| 3408 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3409 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 3410 | void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; } |
| 3411 | SlowPathCode* GetSlowPath() const { return slow_path_; } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3412 | |
| 3413 | DECLARE_INSTRUCTION(SuspendCheck); |
| 3414 | |
| 3415 | private: |
| 3416 | const uint32_t dex_pc_; |
| 3417 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 3418 | // Only used for code generation, in order to share the same slow path between back edges |
| 3419 | // of a same loop. |
| 3420 | SlowPathCode* slow_path_; |
| 3421 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3422 | DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); |
| 3423 | }; |
| 3424 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3425 | /** |
| 3426 | * Instruction to load a Class object. |
| 3427 | */ |
| 3428 | class HLoadClass : public HExpression<0> { |
| 3429 | public: |
| 3430 | HLoadClass(uint16_t type_index, |
| 3431 | bool is_referrers_class, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3432 | uint32_t dex_pc) |
| 3433 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3434 | type_index_(type_index), |
| 3435 | is_referrers_class_(is_referrers_class), |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3436 | dex_pc_(dex_pc), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3437 | generate_clinit_check_(false), |
| 3438 | loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {} |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3439 | |
| 3440 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3441 | |
| 3442 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3443 | return other->AsLoadClass()->type_index_ == type_index_; |
| 3444 | } |
| 3445 | |
| 3446 | size_t ComputeHashCode() const OVERRIDE { return type_index_; } |
| 3447 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3448 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3449 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3450 | bool IsReferrersClass() const { return is_referrers_class_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3451 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3452 | bool NeedsEnvironment() const OVERRIDE { |
| 3453 | // Will call runtime and load the class if the class is not loaded yet. |
| 3454 | // TODO: finer grain decision. |
| 3455 | return !is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3456 | } |
| 3457 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3458 | bool MustGenerateClinitCheck() const { |
| 3459 | return generate_clinit_check_; |
| 3460 | } |
| 3461 | |
Calin Juravle | 0ba218d | 2015-05-19 18:46:01 +0100 | [diff] [blame] | 3462 | void SetMustGenerateClinitCheck(bool generate_clinit_check) { |
| 3463 | generate_clinit_check_ = generate_clinit_check; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | bool CanCallRuntime() const { |
| 3467 | return MustGenerateClinitCheck() || !is_referrers_class_; |
| 3468 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3469 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3470 | bool CanThrow() const OVERRIDE { |
| 3471 | // May call runtime and and therefore can throw. |
| 3472 | // TODO: finer grain decision. |
| 3473 | return !is_referrers_class_; |
| 3474 | } |
| 3475 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 3476 | ReferenceTypeInfo GetLoadedClassRTI() { |
| 3477 | return loaded_class_rti_; |
| 3478 | } |
| 3479 | |
| 3480 | void SetLoadedClassRTI(ReferenceTypeInfo rti) { |
| 3481 | // Make sure we only set exact types (the loaded class should never be merged). |
| 3482 | DCHECK(rti.IsExact()); |
| 3483 | loaded_class_rti_ = rti; |
| 3484 | } |
| 3485 | |
| 3486 | bool IsResolved() { |
| 3487 | return loaded_class_rti_.IsExact(); |
| 3488 | } |
| 3489 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 3490 | bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; } |
| 3491 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3492 | DECLARE_INSTRUCTION(LoadClass); |
| 3493 | |
| 3494 | private: |
| 3495 | const uint16_t type_index_; |
| 3496 | const bool is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3497 | const uint32_t dex_pc_; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3498 | // Whether this instruction must generate the initialization check. |
| 3499 | // Used for code generation. |
| 3500 | bool generate_clinit_check_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3501 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 3502 | ReferenceTypeInfo loaded_class_rti_; |
| 3503 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3504 | DISALLOW_COPY_AND_ASSIGN(HLoadClass); |
| 3505 | }; |
| 3506 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3507 | class HLoadString : public HExpression<0> { |
| 3508 | public: |
| 3509 | HLoadString(uint32_t string_index, uint32_t dex_pc) |
| 3510 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3511 | string_index_(string_index), |
| 3512 | dex_pc_(dex_pc) {} |
| 3513 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3514 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3515 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3516 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3517 | return other->AsLoadString()->string_index_ == string_index_; |
| 3518 | } |
| 3519 | |
| 3520 | size_t ComputeHashCode() const OVERRIDE { return string_index_; } |
| 3521 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3522 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3523 | uint32_t GetStringIndex() const { return string_index_; } |
| 3524 | |
| 3525 | // TODO: Can we deopt or debug when we resolve a string? |
| 3526 | bool NeedsEnvironment() const OVERRIDE { return false; } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 3527 | bool NeedsDexCache() const OVERRIDE { return true; } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3528 | |
| 3529 | DECLARE_INSTRUCTION(LoadString); |
| 3530 | |
| 3531 | private: |
| 3532 | const uint32_t string_index_; |
| 3533 | const uint32_t dex_pc_; |
| 3534 | |
| 3535 | DISALLOW_COPY_AND_ASSIGN(HLoadString); |
| 3536 | }; |
| 3537 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3538 | /** |
| 3539 | * Performs an initialization check on its Class object input. |
| 3540 | */ |
| 3541 | class HClinitCheck : public HExpression<1> { |
| 3542 | public: |
| 3543 | explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc) |
Nicolas Geoffray | a0466e1 | 2015-03-27 15:00:40 +0000 | [diff] [blame] | 3544 | : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3545 | dex_pc_(dex_pc) { |
| 3546 | SetRawInputAt(0, constant); |
| 3547 | } |
| 3548 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3549 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3550 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3551 | UNUSED(other); |
| 3552 | return true; |
| 3553 | } |
| 3554 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3555 | bool NeedsEnvironment() const OVERRIDE { |
| 3556 | // May call runtime to initialize the class. |
| 3557 | return true; |
| 3558 | } |
| 3559 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3560 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3561 | |
| 3562 | HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); } |
| 3563 | |
| 3564 | DECLARE_INSTRUCTION(ClinitCheck); |
| 3565 | |
| 3566 | private: |
| 3567 | const uint32_t dex_pc_; |
| 3568 | |
| 3569 | DISALLOW_COPY_AND_ASSIGN(HClinitCheck); |
| 3570 | }; |
| 3571 | |
| 3572 | class HStaticFieldGet : public HExpression<1> { |
| 3573 | public: |
| 3574 | HStaticFieldGet(HInstruction* cls, |
| 3575 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3576 | MemberOffset field_offset, |
| 3577 | bool is_volatile) |
Nicolas Geoffray | 2af2307 | 2015-04-30 11:15:40 +0000 | [diff] [blame] | 3578 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3579 | field_info_(field_offset, field_type, is_volatile) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3580 | SetRawInputAt(0, cls); |
| 3581 | } |
| 3582 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3583 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 3584 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3585 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3586 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3587 | HStaticFieldGet* other_get = other->AsStaticFieldGet(); |
| 3588 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | size_t ComputeHashCode() const OVERRIDE { |
| 3592 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 3593 | } |
| 3594 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3595 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3596 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 3597 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3598 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3599 | |
| 3600 | DECLARE_INSTRUCTION(StaticFieldGet); |
| 3601 | |
| 3602 | private: |
| 3603 | const FieldInfo field_info_; |
| 3604 | |
| 3605 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet); |
| 3606 | }; |
| 3607 | |
| 3608 | class HStaticFieldSet : public HTemplateInstruction<2> { |
| 3609 | public: |
| 3610 | HStaticFieldSet(HInstruction* cls, |
| 3611 | HInstruction* value, |
| 3612 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3613 | MemberOffset field_offset, |
| 3614 | bool is_volatile) |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3615 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3616 | field_info_(field_offset, field_type, is_volatile), |
| 3617 | value_can_be_null_(true) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3618 | SetRawInputAt(0, cls); |
| 3619 | SetRawInputAt(1, value); |
| 3620 | } |
| 3621 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3622 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3623 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 3624 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3625 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3626 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3627 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3628 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 3629 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3630 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3631 | DECLARE_INSTRUCTION(StaticFieldSet); |
| 3632 | |
| 3633 | private: |
| 3634 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3635 | bool value_can_be_null_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3636 | |
| 3637 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet); |
| 3638 | }; |
| 3639 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3640 | // Implement the move-exception DEX instruction. |
| 3641 | class HLoadException : public HExpression<0> { |
| 3642 | public: |
| 3643 | HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {} |
| 3644 | |
| 3645 | DECLARE_INSTRUCTION(LoadException); |
| 3646 | |
| 3647 | private: |
| 3648 | DISALLOW_COPY_AND_ASSIGN(HLoadException); |
| 3649 | }; |
| 3650 | |
| 3651 | class HThrow : public HTemplateInstruction<1> { |
| 3652 | public: |
| 3653 | HThrow(HInstruction* exception, uint32_t dex_pc) |
| 3654 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) { |
| 3655 | SetRawInputAt(0, exception); |
| 3656 | } |
| 3657 | |
| 3658 | bool IsControlFlow() const OVERRIDE { return true; } |
| 3659 | |
| 3660 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3661 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3662 | bool CanThrow() const OVERRIDE { return true; } |
| 3663 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3664 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3665 | |
| 3666 | DECLARE_INSTRUCTION(Throw); |
| 3667 | |
| 3668 | private: |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3669 | const uint32_t dex_pc_; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3670 | |
| 3671 | DISALLOW_COPY_AND_ASSIGN(HThrow); |
| 3672 | }; |
| 3673 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3674 | class HInstanceOf : public HExpression<2> { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3675 | public: |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3676 | HInstanceOf(HInstruction* object, |
| 3677 | HLoadClass* constant, |
| 3678 | bool class_is_final, |
| 3679 | uint32_t dex_pc) |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3680 | : HExpression(Primitive::kPrimBoolean, SideEffects::None()), |
| 3681 | class_is_final_(class_is_final), |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3682 | must_do_null_check_(true), |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3683 | dex_pc_(dex_pc) { |
| 3684 | SetRawInputAt(0, object); |
| 3685 | SetRawInputAt(1, constant); |
| 3686 | } |
| 3687 | |
| 3688 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3689 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3690 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3691 | return true; |
| 3692 | } |
| 3693 | |
| 3694 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3695 | return false; |
| 3696 | } |
| 3697 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3698 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3699 | |
| 3700 | bool IsClassFinal() const { return class_is_final_; } |
| 3701 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3702 | // Used only in code generation. |
| 3703 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 3704 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 3705 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3706 | DECLARE_INSTRUCTION(InstanceOf); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3707 | |
| 3708 | private: |
| 3709 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3710 | bool must_do_null_check_; |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3711 | const uint32_t dex_pc_; |
| 3712 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3713 | DISALLOW_COPY_AND_ASSIGN(HInstanceOf); |
| 3714 | }; |
| 3715 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3716 | class HBoundType : public HExpression<1> { |
| 3717 | public: |
| 3718 | HBoundType(HInstruction* input, ReferenceTypeInfo bound_type) |
| 3719 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3720 | bound_type_(bound_type) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 3721 | DCHECK_EQ(input->GetType(), Primitive::kPrimNot); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3722 | SetRawInputAt(0, input); |
| 3723 | } |
| 3724 | |
| 3725 | const ReferenceTypeInfo& GetBoundType() const { return bound_type_; } |
| 3726 | |
| 3727 | bool CanBeNull() const OVERRIDE { |
| 3728 | // `null instanceof ClassX` always return false so we can't be null. |
| 3729 | return false; |
| 3730 | } |
| 3731 | |
| 3732 | DECLARE_INSTRUCTION(BoundType); |
| 3733 | |
| 3734 | private: |
| 3735 | // Encodes the most upper class that this instruction can have. In other words |
| 3736 | // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()). |
| 3737 | // It is used to bound the type in cases like `if (x instanceof ClassX) {}` |
| 3738 | const ReferenceTypeInfo bound_type_; |
| 3739 | |
| 3740 | DISALLOW_COPY_AND_ASSIGN(HBoundType); |
| 3741 | }; |
| 3742 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3743 | class HCheckCast : public HTemplateInstruction<2> { |
| 3744 | public: |
| 3745 | HCheckCast(HInstruction* object, |
| 3746 | HLoadClass* constant, |
| 3747 | bool class_is_final, |
| 3748 | uint32_t dex_pc) |
| 3749 | : HTemplateInstruction(SideEffects::None()), |
| 3750 | class_is_final_(class_is_final), |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3751 | must_do_null_check_(true), |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3752 | dex_pc_(dex_pc) { |
| 3753 | SetRawInputAt(0, object); |
| 3754 | SetRawInputAt(1, constant); |
| 3755 | } |
| 3756 | |
| 3757 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3758 | |
| 3759 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3760 | return true; |
| 3761 | } |
| 3762 | |
| 3763 | bool NeedsEnvironment() const OVERRIDE { |
| 3764 | // Instruction may throw a CheckCastError. |
| 3765 | return true; |
| 3766 | } |
| 3767 | |
| 3768 | bool CanThrow() const OVERRIDE { return true; } |
| 3769 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3770 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 3771 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 3772 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3773 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3774 | |
| 3775 | bool IsClassFinal() const { return class_is_final_; } |
| 3776 | |
| 3777 | DECLARE_INSTRUCTION(CheckCast); |
| 3778 | |
| 3779 | private: |
| 3780 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3781 | bool must_do_null_check_; |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3782 | const uint32_t dex_pc_; |
| 3783 | |
| 3784 | DISALLOW_COPY_AND_ASSIGN(HCheckCast); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3785 | }; |
| 3786 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 3787 | class HMemoryBarrier : public HTemplateInstruction<0> { |
| 3788 | public: |
| 3789 | explicit HMemoryBarrier(MemBarrierKind barrier_kind) |
| 3790 | : HTemplateInstruction(SideEffects::None()), |
| 3791 | barrier_kind_(barrier_kind) {} |
| 3792 | |
| 3793 | MemBarrierKind GetBarrierKind() { return barrier_kind_; } |
| 3794 | |
| 3795 | DECLARE_INSTRUCTION(MemoryBarrier); |
| 3796 | |
| 3797 | private: |
| 3798 | const MemBarrierKind barrier_kind_; |
| 3799 | |
| 3800 | DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier); |
| 3801 | }; |
| 3802 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3803 | class HMonitorOperation : public HTemplateInstruction<1> { |
| 3804 | public: |
| 3805 | enum OperationKind { |
| 3806 | kEnter, |
| 3807 | kExit, |
| 3808 | }; |
| 3809 | |
| 3810 | HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc) |
| 3811 | : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) { |
| 3812 | SetRawInputAt(0, object); |
| 3813 | } |
| 3814 | |
| 3815 | // Instruction may throw a Java exception, so we need an environment. |
| 3816 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3817 | bool CanThrow() const OVERRIDE { return true; } |
| 3818 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3819 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3820 | |
| 3821 | bool IsEnter() const { return kind_ == kEnter; } |
| 3822 | |
| 3823 | DECLARE_INSTRUCTION(MonitorOperation); |
| 3824 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3825 | private: |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3826 | const OperationKind kind_; |
| 3827 | const uint32_t dex_pc_; |
| 3828 | |
| 3829 | private: |
| 3830 | DISALLOW_COPY_AND_ASSIGN(HMonitorOperation); |
| 3831 | }; |
| 3832 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3833 | class MoveOperands : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3834 | public: |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 3835 | MoveOperands(Location source, |
| 3836 | Location destination, |
| 3837 | Primitive::Type type, |
| 3838 | HInstruction* instruction) |
| 3839 | : source_(source), destination_(destination), type_(type), instruction_(instruction) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3840 | |
| 3841 | Location GetSource() const { return source_; } |
| 3842 | Location GetDestination() const { return destination_; } |
| 3843 | |
| 3844 | void SetSource(Location value) { source_ = value; } |
| 3845 | void SetDestination(Location value) { destination_ = value; } |
| 3846 | |
| 3847 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 3848 | // destination (but not the source). |
| 3849 | Location MarkPending() { |
| 3850 | DCHECK(!IsPending()); |
| 3851 | Location dest = destination_; |
| 3852 | destination_ = Location::NoLocation(); |
| 3853 | return dest; |
| 3854 | } |
| 3855 | |
| 3856 | void ClearPending(Location dest) { |
| 3857 | DCHECK(IsPending()); |
| 3858 | destination_ = dest; |
| 3859 | } |
| 3860 | |
| 3861 | bool IsPending() const { |
| 3862 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 3863 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 3864 | } |
| 3865 | |
| 3866 | // True if this blocks a move from the given location. |
| 3867 | bool Blocks(Location loc) const { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 3868 | return !IsEliminated() && source_.OverlapsWith(loc); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | // A move is redundant if it's been eliminated, if its source and |
| 3872 | // destination are the same, or if its destination is unneeded. |
| 3873 | bool IsRedundant() const { |
| 3874 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 3875 | } |
| 3876 | |
| 3877 | // We clear both operands to indicate move that's been eliminated. |
| 3878 | void Eliminate() { |
| 3879 | source_ = destination_ = Location::NoLocation(); |
| 3880 | } |
| 3881 | |
| 3882 | bool IsEliminated() const { |
| 3883 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 3884 | return source_.IsInvalid(); |
| 3885 | } |
| 3886 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 3887 | bool Is64BitMove() const { |
| 3888 | return Primitive::Is64BitType(type_); |
| 3889 | } |
| 3890 | |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3891 | HInstruction* GetInstruction() const { return instruction_; } |
| 3892 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3893 | private: |
| 3894 | Location source_; |
| 3895 | Location destination_; |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 3896 | // The type this move is for. |
| 3897 | Primitive::Type type_; |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3898 | // The instruction this move is assocatied with. Null when this move is |
| 3899 | // for moving an input in the expected locations of user (including a phi user). |
| 3900 | // This is only used in debug mode, to ensure we do not connect interval siblings |
| 3901 | // in the same parallel move. |
| 3902 | HInstruction* instruction_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3903 | }; |
| 3904 | |
| 3905 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 3906 | |
| 3907 | class HParallelMove : public HTemplateInstruction<0> { |
| 3908 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3909 | explicit HParallelMove(ArenaAllocator* arena) |
| 3910 | : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3911 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 3912 | void AddMove(Location source, |
| 3913 | Location destination, |
| 3914 | Primitive::Type type, |
| 3915 | HInstruction* instruction) { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3916 | DCHECK(source.IsValid()); |
| 3917 | DCHECK(destination.IsValid()); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3918 | if (kIsDebugBuild) { |
| 3919 | if (instruction != nullptr) { |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 3920 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 3921 | if (moves_.Get(i).GetInstruction() == instruction) { |
| 3922 | // Special case the situation where the move is for the spill slot |
| 3923 | // of the instruction. |
| 3924 | if ((GetPrevious() == instruction) |
| 3925 | || ((GetPrevious() == nullptr) |
| 3926 | && instruction->IsPhi() |
| 3927 | && instruction->GetBlock() == GetBlock())) { |
| 3928 | DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind()) |
| 3929 | << "Doing parallel moves for the same instruction."; |
| 3930 | } else { |
| 3931 | DCHECK(false) << "Doing parallel moves for the same instruction."; |
| 3932 | } |
| 3933 | } |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 3934 | } |
| 3935 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3936 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 3937 | DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination())) |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 3938 | << "Overlapped destination for two moves in a parallel move: " |
| 3939 | << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and " |
| 3940 | << source << " ==> " << destination; |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 3941 | } |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 3942 | } |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 3943 | moves_.Add(MoveOperands(source, destination, type, instruction)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | MoveOperands* MoveOperandsAt(size_t index) const { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3947 | return moves_.GetRawStorage() + index; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | size_t NumMoves() const { return moves_.Size(); } |
| 3951 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3952 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3953 | |
| 3954 | private: |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 3955 | GrowableArray<MoveOperands> moves_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3956 | |
| 3957 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 3958 | }; |
| 3959 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3960 | class HGraphVisitor : public ValueObject { |
| 3961 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3962 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} |
| 3963 | virtual ~HGraphVisitor() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3964 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3965 | virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3966 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 3967 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 3968 | // Visit the graph following basic block insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3969 | void VisitInsertionOrder(); |
| 3970 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 3971 | // Visit the graph following dominator tree reverse post-order. |
| 3972 | void VisitReversePostOrder(); |
| 3973 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 3974 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 3975 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3976 | // Visit functions for instruction classes. |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3977 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3978 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 3979 | |
| 3980 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 3981 | |
| 3982 | #undef DECLARE_VISIT_INSTRUCTION |
| 3983 | |
| 3984 | private: |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 3985 | HGraph* const graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 3986 | |
| 3987 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 3988 | }; |
| 3989 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3990 | class HGraphDelegateVisitor : public HGraphVisitor { |
| 3991 | public: |
| 3992 | explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} |
| 3993 | virtual ~HGraphDelegateVisitor() {} |
| 3994 | |
| 3995 | // Visit functions that delegate to to super class. |
| 3996 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3997 | void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3998 | |
| 3999 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 4000 | |
| 4001 | #undef DECLARE_VISIT_INSTRUCTION |
| 4002 | |
| 4003 | private: |
| 4004 | DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); |
| 4005 | }; |
| 4006 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4007 | class HInsertionOrderIterator : public ValueObject { |
| 4008 | public: |
| 4009 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 4010 | |
| 4011 | bool Done() const { return index_ == graph_.GetBlocks().Size(); } |
| 4012 | HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } |
| 4013 | void Advance() { ++index_; } |
| 4014 | |
| 4015 | private: |
| 4016 | const HGraph& graph_; |
| 4017 | size_t index_; |
| 4018 | |
| 4019 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 4020 | }; |
| 4021 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4022 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4023 | public: |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 4024 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) { |
| 4025 | // Check that reverse post order of the graph has been built. |
| 4026 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 4027 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4028 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4029 | bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } |
| 4030 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4031 | void Advance() { ++index_; } |
| 4032 | |
| 4033 | private: |
| 4034 | const HGraph& graph_; |
| 4035 | size_t index_; |
| 4036 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4037 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4038 | }; |
| 4039 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4040 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4041 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4042 | explicit HPostOrderIterator(const HGraph& graph) |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 4043 | : graph_(graph), index_(graph_.GetReversePostOrder().Size()) { |
| 4044 | // Check that reverse post order of the graph has been built. |
| 4045 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 4046 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4047 | |
| 4048 | bool Done() const { return index_ == 0; } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4049 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4050 | void Advance() { --index_; } |
| 4051 | |
| 4052 | private: |
| 4053 | const HGraph& graph_; |
| 4054 | size_t index_; |
| 4055 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4056 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4057 | }; |
| 4058 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 4059 | class HLinearPostOrderIterator : public ValueObject { |
| 4060 | public: |
| 4061 | explicit HLinearPostOrderIterator(const HGraph& graph) |
| 4062 | : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {} |
| 4063 | |
| 4064 | bool Done() const { return index_ == 0; } |
| 4065 | |
| 4066 | HBasicBlock* Current() const { return order_.Get(index_ -1); } |
| 4067 | |
| 4068 | void Advance() { |
| 4069 | --index_; |
| 4070 | DCHECK_GE(index_, 0U); |
| 4071 | } |
| 4072 | |
| 4073 | private: |
| 4074 | const GrowableArray<HBasicBlock*>& order_; |
| 4075 | size_t index_; |
| 4076 | |
| 4077 | DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator); |
| 4078 | }; |
| 4079 | |
| 4080 | class HLinearOrderIterator : public ValueObject { |
| 4081 | public: |
| 4082 | explicit HLinearOrderIterator(const HGraph& graph) |
| 4083 | : order_(graph.GetLinearOrder()), index_(0) {} |
| 4084 | |
| 4085 | bool Done() const { return index_ == order_.Size(); } |
| 4086 | HBasicBlock* Current() const { return order_.Get(index_); } |
| 4087 | void Advance() { ++index_; } |
| 4088 | |
| 4089 | private: |
| 4090 | const GrowableArray<HBasicBlock*>& order_; |
| 4091 | size_t index_; |
| 4092 | |
| 4093 | DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator); |
| 4094 | }; |
| 4095 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4096 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 4097 | // of an inner loop. The order in which the blocks are iterated is on their |
| 4098 | // block id. |
| 4099 | class HBlocksInLoopIterator : public ValueObject { |
| 4100 | public: |
| 4101 | explicit HBlocksInLoopIterator(const HLoopInformation& info) |
| 4102 | : blocks_in_loop_(info.GetBlocks()), |
| 4103 | blocks_(info.GetHeader()->GetGraph()->GetBlocks()), |
| 4104 | index_(0) { |
| 4105 | if (!blocks_in_loop_.IsBitSet(index_)) { |
| 4106 | Advance(); |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | bool Done() const { return index_ == blocks_.Size(); } |
| 4111 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 4112 | void Advance() { |
| 4113 | ++index_; |
| 4114 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 4115 | if (blocks_in_loop_.IsBitSet(index_)) { |
| 4116 | break; |
| 4117 | } |
| 4118 | } |
| 4119 | } |
| 4120 | |
| 4121 | private: |
| 4122 | const BitVector& blocks_in_loop_; |
| 4123 | const GrowableArray<HBasicBlock*>& blocks_; |
| 4124 | size_t index_; |
| 4125 | |
| 4126 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator); |
| 4127 | }; |
| 4128 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 4129 | inline int64_t Int64FromConstant(HConstant* constant) { |
| 4130 | DCHECK(constant->IsIntConstant() || constant->IsLongConstant()); |
| 4131 | return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue() |
| 4132 | : constant->AsLongConstant()->GetValue(); |
| 4133 | } |
| 4134 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4135 | } // namespace art |
| 4136 | |
| 4137 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |