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