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