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