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