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