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