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 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 21 | #include <array> |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 22 | #include <type_traits> |
| 23 | |
Mathieu Chartier | e5d80f8 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 24 | #include "base/arena_bit_vector.h" |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 25 | #include "base/arena_containers.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 26 | #include "base/arena_object.h" |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 28 | #include "dex/compiler_enums.h" |
Andreas Gampe | 7c9c31c | 2016-03-08 16:00:41 -0800 | [diff] [blame] | 29 | #include "dex_instruction-inl.h" |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 30 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 31 | #include "handle.h" |
| 32 | #include "handle_scope.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 33 | #include "invoke_type.h" |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 34 | #include "locations.h" |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 35 | #include "method_reference.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 36 | #include "mirror/class.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 37 | #include "offsets.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 38 | #include "primitive.h" |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 39 | #include "utils/array_ref.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 40 | |
| 41 | namespace art { |
| 42 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 43 | class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 44 | class HBasicBlock; |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 45 | class HCurrentMethod; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 46 | class HDoubleConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 47 | class HEnvironment; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 48 | class HFloatConstant; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 49 | class HGraphBuilder; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 50 | class HGraphVisitor; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 51 | class HInstruction; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 52 | class HIntConstant; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 53 | class HInvoke; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 54 | class HLongConstant; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 55 | class HNullConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 56 | class HPhi; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 57 | class HSuspendCheck; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 58 | class HTryBoundary; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 59 | class LiveInterval; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 60 | class LocationSummary; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 61 | class SlowPathCode; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 62 | class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 63 | |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 64 | namespace mirror { |
| 65 | class DexCache; |
| 66 | } // namespace mirror |
| 67 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 68 | static const int kDefaultNumberOfBlocks = 8; |
| 69 | static const int kDefaultNumberOfSuccessors = 2; |
| 70 | static const int kDefaultNumberOfPredecessors = 2; |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 71 | static const int kDefaultNumberOfExceptionalPredecessors = 0; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 72 | static const int kDefaultNumberOfDominatedBlocks = 1; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 73 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 74 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 75 | static constexpr uint32_t kMaxIntShiftValue = 0x1f; |
| 76 | static constexpr uint64_t kMaxLongShiftValue = 0x3f; |
| 77 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 78 | static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 79 | static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 80 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 81 | static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1); |
| 82 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 83 | static constexpr uint32_t kNoDexPc = -1; |
| 84 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 85 | enum IfCondition { |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 86 | // All types. |
| 87 | kCondEQ, // == |
| 88 | kCondNE, // != |
| 89 | // Signed integers and floating-point numbers. |
| 90 | kCondLT, // < |
| 91 | kCondLE, // <= |
| 92 | kCondGT, // > |
| 93 | kCondGE, // >= |
| 94 | // Unsigned integers. |
| 95 | kCondB, // < |
| 96 | kCondBE, // <= |
| 97 | kCondA, // > |
| 98 | kCondAE, // >= |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 101 | enum GraphAnalysisResult { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 102 | kAnalysisInvalidBytecode, |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 103 | kAnalysisFailThrowCatchLoop, |
| 104 | kAnalysisFailAmbiguousArrayOp, |
| 105 | kAnalysisSuccess, |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 108 | class HInstructionList : public ValueObject { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 109 | public: |
| 110 | HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} |
| 111 | |
| 112 | void AddInstruction(HInstruction* instruction); |
| 113 | void RemoveInstruction(HInstruction* instruction); |
| 114 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 115 | // Insert `instruction` before/after an existing instruction `cursor`. |
| 116 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
| 117 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
| 118 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 119 | // Return true if this list contains `instruction`. |
| 120 | bool Contains(HInstruction* instruction) const; |
| 121 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 122 | // Return true if `instruction1` is found before `instruction2` in |
| 123 | // this instruction list and false otherwise. Abort if none |
| 124 | // of these instructions is found. |
| 125 | bool FoundBefore(const HInstruction* instruction1, |
| 126 | const HInstruction* instruction2) const; |
| 127 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 128 | bool IsEmpty() const { return first_instruction_ == nullptr; } |
| 129 | void Clear() { first_instruction_ = last_instruction_ = nullptr; } |
| 130 | |
| 131 | // Update the block of all instructions to be `block`. |
| 132 | void SetBlockOfInstructions(HBasicBlock* block) const; |
| 133 | |
| 134 | void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 135 | void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 136 | void Add(const HInstructionList& instruction_list); |
| 137 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 138 | // Return the number of instructions in the list. This is an expensive operation. |
| 139 | size_t CountSize() const; |
| 140 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 141 | private: |
| 142 | HInstruction* first_instruction_; |
| 143 | HInstruction* last_instruction_; |
| 144 | |
| 145 | friend class HBasicBlock; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 146 | friend class HGraph; |
| 147 | friend class HInstruction; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 148 | friend class HInstructionIterator; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 149 | friend class HBackwardInstructionIterator; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 150 | |
| 151 | DISALLOW_COPY_AND_ASSIGN(HInstructionList); |
| 152 | }; |
| 153 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 154 | class ReferenceTypeInfo : ValueObject { |
| 155 | public: |
| 156 | typedef Handle<mirror::Class> TypeHandle; |
| 157 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 158 | static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact); |
| 159 | |
| 160 | static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 161 | return ReferenceTypeInfo(type_handle, is_exact); |
| 162 | } |
| 163 | |
| 164 | static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); } |
| 165 | |
Vladimir Marko | f39745e | 2016-01-26 12:16:55 +0000 | [diff] [blame] | 166 | static bool IsValidHandle(TypeHandle handle) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 167 | return handle.GetReference() != nullptr; |
| 168 | } |
| 169 | |
| 170 | bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 171 | return IsValidHandle(type_handle_); |
| 172 | } |
| 173 | |
| 174 | bool IsExact() const { return is_exact_; } |
| 175 | |
| 176 | bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 177 | DCHECK(IsValid()); |
| 178 | return GetTypeHandle()->IsObjectClass(); |
| 179 | } |
| 180 | |
| 181 | bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 182 | DCHECK(IsValid()); |
| 183 | return GetTypeHandle()->IsStringClass(); |
| 184 | } |
| 185 | |
| 186 | bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 187 | DCHECK(IsValid()); |
| 188 | return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass(); |
| 189 | } |
| 190 | |
| 191 | bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 192 | DCHECK(IsValid()); |
| 193 | return GetTypeHandle()->IsInterface(); |
| 194 | } |
| 195 | |
| 196 | bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 197 | DCHECK(IsValid()); |
| 198 | return GetTypeHandle()->IsArrayClass(); |
| 199 | } |
| 200 | |
| 201 | bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 202 | DCHECK(IsValid()); |
| 203 | return GetTypeHandle()->IsPrimitiveArray(); |
| 204 | } |
| 205 | |
| 206 | bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 207 | DCHECK(IsValid()); |
| 208 | return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray(); |
| 209 | } |
| 210 | |
| 211 | bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 212 | DCHECK(IsValid()); |
| 213 | if (!IsExact()) return false; |
| 214 | if (!IsArrayClass()) return false; |
| 215 | return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 216 | } |
| 217 | |
| 218 | bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 219 | DCHECK(IsValid()); |
| 220 | if (!IsExact()) return false; |
| 221 | if (!IsArrayClass()) return false; |
| 222 | if (!rti.IsArrayClass()) return false; |
| 223 | return GetTypeHandle()->GetComponentType()->IsAssignableFrom( |
| 224 | rti.GetTypeHandle()->GetComponentType()); |
| 225 | } |
| 226 | |
| 227 | Handle<mirror::Class> GetTypeHandle() const { return type_handle_; } |
| 228 | |
| 229 | bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 230 | DCHECK(IsValid()); |
| 231 | DCHECK(rti.IsValid()); |
| 232 | return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 233 | } |
| 234 | |
| 235 | bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 236 | DCHECK(IsValid()); |
| 237 | DCHECK(rti.IsValid()); |
| 238 | return GetTypeHandle().Get() != rti.GetTypeHandle().Get() && |
| 239 | GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 240 | } |
| 241 | |
| 242 | // Returns true if the type information provide the same amount of details. |
| 243 | // Note that it does not mean that the instructions have the same actual type |
| 244 | // (because the type can be the result of a merge). |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 245 | bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 246 | if (!IsValid() && !rti.IsValid()) { |
| 247 | // Invalid types are equal. |
| 248 | return true; |
| 249 | } |
| 250 | if (!IsValid() || !rti.IsValid()) { |
| 251 | // One is valid, the other not. |
| 252 | return false; |
| 253 | } |
| 254 | return IsExact() == rti.IsExact() |
| 255 | && GetTypeHandle().Get() == rti.GetTypeHandle().Get(); |
| 256 | } |
| 257 | |
| 258 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 259 | ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {} |
| 260 | ReferenceTypeInfo(TypeHandle type_handle, bool is_exact) |
| 261 | : type_handle_(type_handle), is_exact_(is_exact) { } |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 262 | |
| 263 | // The class of the object. |
| 264 | TypeHandle type_handle_; |
| 265 | // Whether or not the type is exact or a superclass of the actual type. |
| 266 | // Whether or not we have any information about this type. |
| 267 | bool is_exact_; |
| 268 | }; |
| 269 | |
| 270 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs); |
| 271 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 272 | // Control-flow graph of a method. Contains a list of basic blocks. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 273 | class HGraph : public ArenaObject<kArenaAllocGraph> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 274 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 275 | HGraph(ArenaAllocator* arena, |
| 276 | const DexFile& dex_file, |
| 277 | uint32_t method_idx, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 278 | bool should_generate_constructor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 279 | InstructionSet instruction_set, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 280 | InvokeType invoke_type = kInvalidInvokeType, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 281 | bool debuggable = false, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 282 | bool osr = false, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 283 | int start_instruction_id = 0) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 284 | : arena_(arena), |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 285 | blocks_(arena->Adapter(kArenaAllocBlockList)), |
| 286 | reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)), |
| 287 | linear_order_(arena->Adapter(kArenaAllocLinearOrder)), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 288 | entry_block_(nullptr), |
| 289 | exit_block_(nullptr), |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 290 | maximum_number_of_out_vregs_(0), |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 291 | number_of_vregs_(0), |
| 292 | number_of_in_vregs_(0), |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 293 | temporaries_vreg_slots_(0), |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 294 | has_bounds_checks_(false), |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 295 | has_try_catch_(false), |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 296 | has_irreducible_loops_(false), |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 297 | debuggable_(debuggable), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 298 | current_instruction_id_(start_instruction_id), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 299 | dex_file_(dex_file), |
| 300 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 301 | invoke_type_(invoke_type), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 302 | in_ssa_form_(false), |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 303 | should_generate_constructor_barrier_(should_generate_constructor_barrier), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 304 | instruction_set_(instruction_set), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 305 | cached_null_constant_(nullptr), |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 306 | cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)), |
| 307 | cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)), |
| 308 | cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)), |
| 309 | cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)), |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 310 | cached_current_method_(nullptr), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 311 | inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()), |
| 312 | osr_(osr) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 313 | blocks_.reserve(kDefaultNumberOfBlocks); |
| 314 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 315 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 316 | // Acquires and stores RTI of inexact Object to be used when creating HNullConstant. |
| 317 | void InitializeInexactObjectRTI(StackHandleScopeCollection* handles); |
| 318 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 319 | ArenaAllocator* GetArena() const { return arena_; } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 320 | const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; } |
| 321 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 322 | bool IsInSsaForm() const { return in_ssa_form_; } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 323 | void SetInSsaForm() { in_ssa_form_ = true; } |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 324 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 325 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 326 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 327 | bool HasExitBlock() const { return exit_block_ != nullptr; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 328 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 329 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 330 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 331 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 332 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 333 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 334 | void ComputeDominanceInformation(); |
| 335 | void ClearDominanceInformation(); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 336 | void ClearLoopInformation(); |
| 337 | void FindBackEdges(ArenaBitVector* visited); |
| 338 | GraphAnalysisResult BuildDominatorTree(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 339 | void SimplifyCFG(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 340 | void SimplifyCatchBlocks(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 341 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 342 | // Analyze all natural loops in this graph. Returns a code specifying that it |
| 343 | // was successful or the reason for failure. The method will fail if a loop |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 344 | // is a throw-catch loop, i.e. the header is a catch block. |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 345 | GraphAnalysisResult AnalyzeLoops() const; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 346 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 347 | // Iterate over blocks to compute try block membership. Needs reverse post |
| 348 | // order and loop information. |
| 349 | void ComputeTryBlockInformation(); |
| 350 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 351 | // Inline this graph in `outer_graph`, replacing the given `invoke` instruction. |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 352 | // Returns the instruction to replace the invoke expression or null if the |
| 353 | // invoke is for a void method. Note that the caller is responsible for replacing |
| 354 | // and removing the invoke instruction. |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 355 | HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 356 | |
Nicolas Geoffray | a1d8ddf | 2016-02-29 11:46:58 +0000 | [diff] [blame] | 357 | // Update the loop and try membership of `block`, which was spawned from `reference`. |
| 358 | // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block` |
| 359 | // should be the new back edge. |
| 360 | void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block, |
| 361 | HBasicBlock* reference, |
| 362 | bool replace_if_back_edge); |
| 363 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 364 | // Need to add a couple of blocks to test if the loop body is entered and |
| 365 | // put deoptimization instructions, etc. |
| 366 | void TransformLoopHeaderForBCE(HBasicBlock* header); |
| 367 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame] | 368 | // Removes `block` from the graph. Assumes `block` has been disconnected from |
| 369 | // other blocks and has no instructions or phis. |
| 370 | void DeleteDeadEmptyBlock(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 371 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 372 | // Splits the edge between `block` and `successor` while preserving the |
| 373 | // indices in the predecessor/successor lists. If there are multiple edges |
| 374 | // between the blocks, the lowest indices are used. |
| 375 | // Returns the new block which is empty and has the same dex pc as `successor`. |
| 376 | HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor); |
| 377 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 378 | void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); |
| 379 | void SimplifyLoop(HBasicBlock* header); |
| 380 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 381 | int32_t GetNextInstructionId() { |
| 382 | DCHECK_NE(current_instruction_id_, INT32_MAX); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 383 | return current_instruction_id_++; |
| 384 | } |
| 385 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 386 | int32_t GetCurrentInstructionId() const { |
| 387 | return current_instruction_id_; |
| 388 | } |
| 389 | |
| 390 | void SetCurrentInstructionId(int32_t id) { |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 391 | DCHECK_GE(id, current_instruction_id_); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 392 | current_instruction_id_ = id; |
| 393 | } |
| 394 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 395 | uint16_t GetMaximumNumberOfOutVRegs() const { |
| 396 | return maximum_number_of_out_vregs_; |
| 397 | } |
| 398 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 399 | void SetMaximumNumberOfOutVRegs(uint16_t new_value) { |
| 400 | maximum_number_of_out_vregs_ = new_value; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 403 | void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) { |
| 404 | maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value); |
| 405 | } |
| 406 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 407 | void UpdateTemporariesVRegSlots(size_t slots) { |
| 408 | temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 411 | size_t GetTemporariesVRegSlots() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 412 | DCHECK(!in_ssa_form_); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 413 | return temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 416 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 417 | number_of_vregs_ = number_of_vregs; |
| 418 | } |
| 419 | |
| 420 | uint16_t GetNumberOfVRegs() const { |
| 421 | return number_of_vregs_; |
| 422 | } |
| 423 | |
| 424 | void SetNumberOfInVRegs(uint16_t value) { |
| 425 | number_of_in_vregs_ = value; |
| 426 | } |
| 427 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 428 | uint16_t GetNumberOfLocalVRegs() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 429 | DCHECK(!in_ssa_form_); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 430 | return number_of_vregs_ - number_of_in_vregs_; |
| 431 | } |
| 432 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 433 | const ArenaVector<HBasicBlock*>& GetReversePostOrder() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 434 | return reverse_post_order_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 435 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 436 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 437 | const ArenaVector<HBasicBlock*>& GetLinearOrder() const { |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 438 | return linear_order_; |
| 439 | } |
| 440 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 441 | bool HasBoundsChecks() const { |
| 442 | return has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 445 | void SetHasBoundsChecks(bool value) { |
| 446 | has_bounds_checks_ = value; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 449 | bool ShouldGenerateConstructorBarrier() const { |
| 450 | return should_generate_constructor_barrier_; |
| 451 | } |
| 452 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 453 | bool IsDebuggable() const { return debuggable_; } |
| 454 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 455 | // Returns a constant of the given type and value. If it does not exist |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 456 | // already, it is created and inserted into the graph. This method is only for |
| 457 | // integral types. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 458 | HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 459 | |
| 460 | // TODO: This is problematic for the consistency of reference type propagation |
| 461 | // because it can be created anytime after the pass and thus it will be left |
| 462 | // with an invalid type. |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 463 | HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 464 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 465 | HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) { |
| 466 | return CreateConstant(value, &cached_int_constants_, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 467 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 468 | HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) { |
| 469 | return CreateConstant(value, &cached_long_constants_, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 470 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 471 | HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) { |
| 472 | return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 473 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 474 | HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) { |
| 475 | return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 476 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 477 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 478 | HCurrentMethod* GetCurrentMethod(); |
| 479 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 480 | const DexFile& GetDexFile() const { |
| 481 | return dex_file_; |
| 482 | } |
| 483 | |
| 484 | uint32_t GetMethodIdx() const { |
| 485 | return method_idx_; |
| 486 | } |
| 487 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 488 | InvokeType GetInvokeType() const { |
| 489 | return invoke_type_; |
| 490 | } |
| 491 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 492 | InstructionSet GetInstructionSet() const { |
| 493 | return instruction_set_; |
| 494 | } |
| 495 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 496 | bool IsCompilingOsr() const { return osr_; } |
| 497 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 498 | bool HasTryCatch() const { return has_try_catch_; } |
| 499 | void SetHasTryCatch(bool value) { has_try_catch_ = value; } |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 500 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 501 | bool HasIrreducibleLoops() const { return has_irreducible_loops_; } |
| 502 | void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; } |
| 503 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 504 | ArtMethod* GetArtMethod() const { return art_method_; } |
| 505 | void SetArtMethod(ArtMethod* method) { art_method_ = method; } |
| 506 | |
Mark Mendell | f652917 | 2015-11-17 11:16:56 -0500 | [diff] [blame] | 507 | // Returns an instruction with the opposite boolean value from 'cond'. |
| 508 | // The instruction has been inserted into the graph, either as a constant, or |
| 509 | // before cursor. |
| 510 | HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor); |
| 511 | |
Nicolas Geoffray | 18401b7 | 2016-03-11 13:35:51 +0000 | [diff] [blame] | 512 | ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; } |
| 513 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 514 | private: |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 515 | void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 516 | void RemoveDeadBlocks(const ArenaBitVector& visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 517 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 518 | template <class InstructionType, typename ValueType> |
| 519 | InstructionType* CreateConstant(ValueType value, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 520 | ArenaSafeMap<ValueType, InstructionType*>* cache, |
| 521 | uint32_t dex_pc = kNoDexPc) { |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 522 | // Try to find an existing constant of the given value. |
| 523 | InstructionType* constant = nullptr; |
| 524 | auto cached_constant = cache->find(value); |
| 525 | if (cached_constant != cache->end()) { |
| 526 | constant = cached_constant->second; |
| 527 | } |
| 528 | |
| 529 | // If not found or previously deleted, create and cache a new instruction. |
Nicolas Geoffray | f78848f | 2015-06-17 11:57:56 +0100 | [diff] [blame] | 530 | // Don't bother reviving a previously deleted instruction, for simplicity. |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 531 | if (constant == nullptr || constant->GetBlock() == nullptr) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 532 | constant = new (arena_) InstructionType(value, dex_pc); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 533 | cache->Overwrite(value, constant); |
| 534 | InsertConstant(constant); |
| 535 | } |
| 536 | return constant; |
| 537 | } |
| 538 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 539 | void InsertConstant(HConstant* instruction); |
| 540 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 541 | // Cache a float constant into the graph. This method should only be |
| 542 | // called by the SsaBuilder when creating "equivalent" instructions. |
| 543 | void CacheFloatConstant(HFloatConstant* constant); |
| 544 | |
| 545 | // See CacheFloatConstant comment. |
| 546 | void CacheDoubleConstant(HDoubleConstant* constant); |
| 547 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 548 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 549 | |
| 550 | // List of blocks in insertion order. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 551 | ArenaVector<HBasicBlock*> blocks_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 552 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 553 | // List of blocks to perform a reverse post order tree traversal. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 554 | ArenaVector<HBasicBlock*> reverse_post_order_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 555 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 556 | // List of blocks to perform a linear order tree traversal. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 557 | ArenaVector<HBasicBlock*> linear_order_; |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 558 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 559 | HBasicBlock* entry_block_; |
| 560 | HBasicBlock* exit_block_; |
| 561 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 562 | // 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] | 563 | uint16_t maximum_number_of_out_vregs_; |
| 564 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 565 | // The number of virtual registers in this method. Contains the parameters. |
| 566 | uint16_t number_of_vregs_; |
| 567 | |
| 568 | // The number of virtual registers used by parameters of this method. |
| 569 | uint16_t number_of_in_vregs_; |
| 570 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 571 | // Number of vreg size slots that the temporaries use (used in baseline compiler). |
| 572 | size_t temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 573 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 574 | // Has bounds checks. We can totally skip BCE if it's false. |
| 575 | bool has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 576 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 577 | // Flag whether there are any try/catch blocks in the graph. We will skip |
| 578 | // try/catch-related passes if false. |
| 579 | bool has_try_catch_; |
| 580 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 581 | // Flag whether there are any irreducible loops in the graph. |
| 582 | bool has_irreducible_loops_; |
| 583 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 584 | // Indicates whether the graph should be compiled in a way that |
| 585 | // ensures full debuggability. If false, we can apply more |
| 586 | // aggressive optimizations that may limit the level of debugging. |
| 587 | const bool debuggable_; |
| 588 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 589 | // 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] | 590 | int32_t current_instruction_id_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 591 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 592 | // The dex file from which the method is from. |
| 593 | const DexFile& dex_file_; |
| 594 | |
| 595 | // The method index in the dex file. |
| 596 | const uint32_t method_idx_; |
| 597 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 598 | // If inlined, this encodes how the callee is being invoked. |
| 599 | const InvokeType invoke_type_; |
| 600 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 601 | // Whether the graph has been transformed to SSA form. Only used |
| 602 | // in debug mode to ensure we are not using properties only valid |
| 603 | // for non-SSA form (like the number of temporaries). |
| 604 | bool in_ssa_form_; |
| 605 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 606 | const bool should_generate_constructor_barrier_; |
| 607 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 608 | const InstructionSet instruction_set_; |
| 609 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 610 | // Cached constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 611 | HNullConstant* cached_null_constant_; |
| 612 | ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 613 | ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 614 | ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 615 | ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 616 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 617 | HCurrentMethod* cached_current_method_; |
| 618 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 619 | // The ArtMethod this graph is for. Note that for AOT, it may be null, |
| 620 | // for example for methods whose declaring class could not be resolved |
| 621 | // (such as when the superclass could not be found). |
| 622 | ArtMethod* art_method_; |
| 623 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 624 | // Keep the RTI of inexact Object to avoid having to pass stack handle |
| 625 | // collection pointer to passes which may create NullConstant. |
| 626 | ReferenceTypeInfo inexact_object_rti_; |
| 627 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 628 | // Whether we are compiling this graph for on stack replacement: this will |
| 629 | // make all loops seen as irreducible and emit special stack maps to mark |
| 630 | // compiled code entries which the interpreter can directly jump to. |
| 631 | const bool osr_; |
| 632 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 633 | friend class SsaBuilder; // For caching constants. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 634 | friend class SsaLivenessAnalysis; // For the linear order. |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 635 | friend class HInliner; // For the reverse post order. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 636 | ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 637 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 638 | }; |
| 639 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 640 | class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 641 | public: |
| 642 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 643 | : header_(header), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 644 | suspend_check_(nullptr), |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 645 | irreducible_(false), |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 646 | back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)), |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 647 | // Make bit vector growable, as the number of blocks may change. |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 648 | blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 649 | back_edges_.reserve(kDefaultNumberOfBackEdges); |
| 650 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 651 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 652 | bool IsIrreducible() const { return irreducible_; } |
| 653 | |
| 654 | void Dump(std::ostream& os); |
| 655 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 656 | HBasicBlock* GetHeader() const { |
| 657 | return header_; |
| 658 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 659 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 660 | void SetHeader(HBasicBlock* block) { |
| 661 | header_ = block; |
| 662 | } |
| 663 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 664 | HSuspendCheck* GetSuspendCheck() const { return suspend_check_; } |
| 665 | void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; } |
| 666 | bool HasSuspendCheck() const { return suspend_check_ != nullptr; } |
| 667 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 668 | void AddBackEdge(HBasicBlock* back_edge) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 669 | back_edges_.push_back(back_edge); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 672 | void RemoveBackEdge(HBasicBlock* back_edge) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 673 | RemoveElement(back_edges_, back_edge); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 674 | } |
| 675 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 676 | bool IsBackEdge(const HBasicBlock& block) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 677 | return ContainsElement(back_edges_, &block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 680 | size_t NumberOfBackEdges() const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 681 | return back_edges_.size(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 684 | HBasicBlock* GetPreHeader() const; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 685 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 686 | const ArenaVector<HBasicBlock*>& GetBackEdges() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 687 | return back_edges_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 690 | // Returns the lifetime position of the back edge that has the |
| 691 | // greatest lifetime position. |
| 692 | size_t GetLifetimeEnd() const; |
| 693 | |
| 694 | void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 695 | ReplaceElement(back_edges_, existing, new_back_edge); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 698 | // Finds blocks that are part of this loop. |
| 699 | void Populate(); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 700 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 701 | // Returns whether this loop information contains `block`. |
| 702 | // Note that this loop information *must* be populated before entering this function. |
| 703 | bool Contains(const HBasicBlock& block) const; |
| 704 | |
| 705 | // Returns whether this loop information is an inner loop of `other`. |
| 706 | // Note that `other` *must* be populated before entering this function. |
| 707 | bool IsIn(const HLoopInformation& other) const; |
| 708 | |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 709 | // Returns true if instruction is not defined within this loop. |
| 710 | bool IsDefinedOutOfTheLoop(HInstruction* instruction) const; |
Aart Bik | 73f1f3b | 2015-10-28 15:28:08 -0700 | [diff] [blame] | 711 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 712 | const ArenaBitVector& GetBlocks() const { return blocks_; } |
| 713 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 714 | void Add(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 715 | void Remove(HBasicBlock* block); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 716 | |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 717 | void ClearAllBlocks() { |
| 718 | blocks_.ClearAllBits(); |
| 719 | } |
| 720 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 721 | private: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 722 | // Internal recursive implementation of `Populate`. |
| 723 | void PopulateRecursive(HBasicBlock* block); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 724 | void PopulateIrreducibleRecursive(HBasicBlock* block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 725 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 726 | HBasicBlock* header_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 727 | HSuspendCheck* suspend_check_; |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 728 | bool irreducible_; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 729 | ArenaVector<HBasicBlock*> back_edges_; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 730 | ArenaBitVector blocks_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 731 | |
| 732 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 733 | }; |
| 734 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 735 | // Stores try/catch information for basic blocks. |
| 736 | // Note that HGraph is constructed so that catch blocks cannot simultaneously |
| 737 | // be try blocks. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 738 | class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 739 | public: |
| 740 | // Try block information constructor. |
| 741 | explicit TryCatchInformation(const HTryBoundary& try_entry) |
| 742 | : try_entry_(&try_entry), |
| 743 | catch_dex_file_(nullptr), |
| 744 | catch_type_index_(DexFile::kDexNoIndex16) { |
| 745 | DCHECK(try_entry_ != nullptr); |
| 746 | } |
| 747 | |
| 748 | // Catch block information constructor. |
| 749 | TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file) |
| 750 | : try_entry_(nullptr), |
| 751 | catch_dex_file_(&dex_file), |
| 752 | catch_type_index_(catch_type_index) {} |
| 753 | |
| 754 | bool IsTryBlock() const { return try_entry_ != nullptr; } |
| 755 | |
| 756 | const HTryBoundary& GetTryEntry() const { |
| 757 | DCHECK(IsTryBlock()); |
| 758 | return *try_entry_; |
| 759 | } |
| 760 | |
| 761 | bool IsCatchBlock() const { return catch_dex_file_ != nullptr; } |
| 762 | |
| 763 | bool IsCatchAllTypeIndex() const { |
| 764 | DCHECK(IsCatchBlock()); |
| 765 | return catch_type_index_ == DexFile::kDexNoIndex16; |
| 766 | } |
| 767 | |
| 768 | uint16_t GetCatchTypeIndex() const { |
| 769 | DCHECK(IsCatchBlock()); |
| 770 | return catch_type_index_; |
| 771 | } |
| 772 | |
| 773 | const DexFile& GetCatchDexFile() const { |
| 774 | DCHECK(IsCatchBlock()); |
| 775 | return *catch_dex_file_; |
| 776 | } |
| 777 | |
| 778 | private: |
| 779 | // One of possibly several TryBoundary instructions entering the block's try. |
| 780 | // Only set for try blocks. |
| 781 | const HTryBoundary* try_entry_; |
| 782 | |
| 783 | // Exception type information. Only set for catch blocks. |
| 784 | const DexFile* catch_dex_file_; |
| 785 | const uint16_t catch_type_index_; |
| 786 | }; |
| 787 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 788 | static constexpr size_t kNoLifetime = -1; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 789 | static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 790 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 791 | // A block in a method. Contains the list of instructions represented |
| 792 | // as a double linked list. Each block knows its predecessors and |
| 793 | // successors. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 794 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 795 | class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 796 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 797 | HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 798 | : graph_(graph), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 799 | predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)), |
| 800 | successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 801 | loop_information_(nullptr), |
| 802 | dominator_(nullptr), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 803 | dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)), |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 804 | block_id_(kInvalidBlockId), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 805 | dex_pc_(dex_pc), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 806 | lifetime_start_(kNoLifetime), |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 807 | lifetime_end_(kNoLifetime), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 808 | try_catch_information_(nullptr) { |
| 809 | predecessors_.reserve(kDefaultNumberOfPredecessors); |
| 810 | successors_.reserve(kDefaultNumberOfSuccessors); |
| 811 | dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks); |
| 812 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 813 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 814 | const ArenaVector<HBasicBlock*>& GetPredecessors() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 815 | return predecessors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 818 | const ArenaVector<HBasicBlock*>& GetSuccessors() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 819 | return successors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 820 | } |
| 821 | |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 822 | ArrayRef<HBasicBlock* const> GetNormalSuccessors() const; |
| 823 | ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const; |
| 824 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 825 | bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) { |
| 826 | return ContainsElement(successors_, block, start_from); |
| 827 | } |
| 828 | |
| 829 | const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 830 | return dominated_blocks_; |
| 831 | } |
| 832 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 833 | bool IsEntryBlock() const { |
| 834 | return graph_->GetEntryBlock() == this; |
| 835 | } |
| 836 | |
| 837 | bool IsExitBlock() const { |
| 838 | return graph_->GetExitBlock() == this; |
| 839 | } |
| 840 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 841 | bool IsSingleGoto() const; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 842 | bool IsSingleTryBoundary() const; |
| 843 | |
| 844 | // Returns true if this block emits nothing but a jump. |
| 845 | bool IsSingleJump() const { |
| 846 | HLoopInformation* loop_info = GetLoopInformation(); |
| 847 | return (IsSingleGoto() || IsSingleTryBoundary()) |
| 848 | // Back edges generate a suspend check. |
| 849 | && (loop_info == nullptr || !loop_info->IsBackEdge(*this)); |
| 850 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 851 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 852 | void AddBackEdge(HBasicBlock* back_edge) { |
| 853 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 854 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 855 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 856 | DCHECK_EQ(loop_information_->GetHeader(), this); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 857 | loop_information_->AddBackEdge(back_edge); |
| 858 | } |
| 859 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 860 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 861 | void SetGraph(HGraph* graph) { graph_ = graph; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 862 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 863 | uint32_t GetBlockId() const { return block_id_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 864 | void SetBlockId(int id) { block_id_ = id; } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 865 | uint32_t GetDexPc() const { return dex_pc_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 866 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 867 | HBasicBlock* GetDominator() const { return dominator_; } |
| 868 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 869 | void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); } |
| 870 | |
| 871 | void RemoveDominatedBlock(HBasicBlock* block) { |
| 872 | RemoveElement(dominated_blocks_, block); |
Vladimir Marko | 91e11c0 | 2015-09-02 17:03:22 +0100 | [diff] [blame] | 873 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 874 | |
| 875 | void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) { |
| 876 | ReplaceElement(dominated_blocks_, existing, new_block); |
| 877 | } |
| 878 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 879 | void ClearDominanceInformation(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 880 | |
| 881 | int NumberOfBackEdges() const { |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 882 | return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 885 | HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } |
| 886 | HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 887 | const HInstructionList& GetInstructions() const { return instructions_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 888 | HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 889 | HInstruction* GetLastPhi() const { return phis_.last_instruction_; } |
| 890 | const HInstructionList& GetPhis() const { return phis_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 891 | |
Nicolas Geoffray | 09aa147 | 2016-01-19 10:52:54 +0000 | [diff] [blame] | 892 | HInstruction* GetFirstInstructionDisregardMoves() const; |
| 893 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 894 | void AddSuccessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 895 | successors_.push_back(block); |
| 896 | block->predecessors_.push_back(this); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 899 | void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 900 | size_t successor_index = GetSuccessorIndexOf(existing); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 901 | existing->RemovePredecessor(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 902 | new_block->predecessors_.push_back(this); |
| 903 | successors_[successor_index] = new_block; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 906 | void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 907 | size_t predecessor_index = GetPredecessorIndexOf(existing); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 908 | existing->RemoveSuccessor(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 909 | new_block->successors_.push_back(this); |
| 910 | predecessors_[predecessor_index] = new_block; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 913 | // Insert `this` between `predecessor` and `successor. This method |
| 914 | // preserves the indicies, and will update the first edge found between |
| 915 | // `predecessor` and `successor`. |
| 916 | void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) { |
| 917 | size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor); |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 918 | size_t successor_index = predecessor->GetSuccessorIndexOf(successor); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 919 | successor->predecessors_[predecessor_index] = this; |
| 920 | predecessor->successors_[successor_index] = this; |
| 921 | successors_.push_back(successor); |
| 922 | predecessors_.push_back(predecessor); |
Nicolas Geoffray | 8b20f88 | 2015-06-19 16:17:05 +0100 | [diff] [blame] | 923 | } |
| 924 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 925 | void RemovePredecessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 926 | predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block)); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 927 | } |
| 928 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 929 | void RemoveSuccessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 930 | successors_.erase(successors_.begin() + GetSuccessorIndexOf(block)); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 933 | void ClearAllPredecessors() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 934 | predecessors_.clear(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | void AddPredecessor(HBasicBlock* block) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 938 | predecessors_.push_back(block); |
| 939 | block->successors_.push_back(this); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 942 | void SwapPredecessors() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 943 | DCHECK_EQ(predecessors_.size(), 2u); |
| 944 | std::swap(predecessors_[0], predecessors_[1]); |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 945 | } |
| 946 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 947 | void SwapSuccessors() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 948 | DCHECK_EQ(successors_.size(), 2u); |
| 949 | std::swap(successors_[0], successors_[1]); |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 950 | } |
| 951 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 952 | size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 953 | return IndexOfElement(predecessors_, predecessor); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 954 | } |
| 955 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 956 | size_t GetSuccessorIndexOf(HBasicBlock* successor) const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 957 | return IndexOfElement(successors_, successor); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 958 | } |
| 959 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 960 | HBasicBlock* GetSinglePredecessor() const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 961 | DCHECK_EQ(GetPredecessors().size(), 1u); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 962 | return GetPredecessors()[0]; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | HBasicBlock* GetSingleSuccessor() const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 966 | DCHECK_EQ(GetSuccessors().size(), 1u); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 967 | return GetSuccessors()[0]; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | // Returns whether the first occurrence of `predecessor` in the list of |
| 971 | // predecessors is at index `idx`. |
| 972 | bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 973 | DCHECK_EQ(GetPredecessors()[idx], predecessor); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 974 | return GetPredecessorIndexOf(predecessor) == idx; |
| 975 | } |
| 976 | |
David Brazdil | d7558da | 2015-09-22 13:04:14 +0100 | [diff] [blame] | 977 | // Create a new block between this block and its predecessors. The new block |
| 978 | // is added to the graph, all predecessor edges are relinked to it and an edge |
| 979 | // is created to `this`. Returns the new empty block. Reverse post order or |
| 980 | // loop and try/catch information are not updated. |
| 981 | HBasicBlock* CreateImmediateDominator(); |
| 982 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 983 | // Split the block into two blocks just before `cursor`. Returns the newly |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 984 | // created, latter block. Note that this method will add the block to the |
| 985 | // graph, create a Goto at the end of the former block and will create an edge |
| 986 | // between the blocks. It will not, however, update the reverse post order or |
David Brazdil | d7558da | 2015-09-22 13:04:14 +0100 | [diff] [blame] | 987 | // loop and try/catch information. |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 988 | HBasicBlock* SplitBefore(HInstruction* cursor); |
| 989 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 990 | // Split the block into two blocks just before `cursor`. Returns the newly |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 991 | // created block. Note that this method just updates raw block information, |
| 992 | // like predecessors, successors, dominators, and instruction list. It does not |
| 993 | // update the graph, reverse post order, loop information, nor make sure the |
| 994 | // blocks are consistent (for example ending with a control flow instruction). |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 995 | HBasicBlock* SplitBeforeForInlining(HInstruction* cursor); |
| 996 | |
| 997 | // Similar to `SplitBeforeForInlining` but does it after `cursor`. |
| 998 | HBasicBlock* SplitAfterForInlining(HInstruction* cursor); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 999 | |
David Brazdil | 9bc4361 | 2015-11-05 21:25:24 +0000 | [diff] [blame] | 1000 | // Split catch block into two blocks after the original move-exception bytecode |
| 1001 | // instruction, or at the beginning if not present. Returns the newly created, |
| 1002 | // latter block, or nullptr if such block could not be created (must be dead |
| 1003 | // in that case). Note that this method just updates raw block information, |
| 1004 | // like predecessors, successors, dominators, and instruction list. It does not |
| 1005 | // update the graph, reverse post order, loop information, nor make sure the |
| 1006 | // blocks are consistent (for example ending with a control flow instruction). |
| 1007 | HBasicBlock* SplitCatchBlockAfterMoveException(); |
| 1008 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1009 | // Merge `other` at the end of `this`. Successors and dominated blocks of |
| 1010 | // `other` are changed to be successors and dominated blocks of `this`. Note |
| 1011 | // that this method does not update the graph, reverse post order, loop |
| 1012 | // information, nor make sure the blocks are consistent (for example ending |
| 1013 | // with a control flow instruction). |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1014 | void MergeWithInlined(HBasicBlock* other); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1015 | |
| 1016 | // Replace `this` with `other`. Predecessors, successors, and dominated blocks |
| 1017 | // of `this` are moved to `other`. |
| 1018 | // Note that this method does not update the graph, reverse post order, loop |
| 1019 | // information, nor make sure the blocks are consistent (for example ending |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1020 | // with a control flow instruction). |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1021 | void ReplaceWith(HBasicBlock* other); |
| 1022 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1023 | // Merge `other` at the end of `this`. This method updates loops, reverse post |
| 1024 | // order, links to predecessors, successors, dominators and deletes the block |
| 1025 | // from the graph. The two blocks must be successive, i.e. `this` the only |
| 1026 | // predecessor of `other` and vice versa. |
| 1027 | void MergeWith(HBasicBlock* other); |
| 1028 | |
| 1029 | // Disconnects `this` from all its predecessors, successors and dominator, |
| 1030 | // removes it from all loops it is included in and eventually from the graph. |
| 1031 | // The block must not dominate any other block. Predecessors and successors |
| 1032 | // are safely updated. |
| 1033 | void DisconnectAndDelete(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1034 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1035 | void AddInstruction(HInstruction* instruction); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 1036 | // Insert `instruction` before/after an existing instruction `cursor`. |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1037 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 1038 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1039 | // Replace instruction `initial` with `replacement` within this block. |
| 1040 | void ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 1041 | HInstruction* replacement); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1042 | void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1043 | void AddPhi(HPhi* phi); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1044 | void InsertPhiAfter(HPhi* instruction, HPhi* cursor); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1045 | // RemoveInstruction and RemovePhi delete a given instruction from the respective |
| 1046 | // instruction list. With 'ensure_safety' set to true, it verifies that the |
| 1047 | // instruction is not in use and removes it from the use lists of its inputs. |
| 1048 | void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true); |
| 1049 | void RemovePhi(HPhi* phi, bool ensure_safety = true); |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 1050 | void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1051 | |
| 1052 | bool IsLoopHeader() const { |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 1053 | return IsInLoop() && (loop_information_->GetHeader() == this); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1056 | bool IsLoopPreHeaderFirstPredecessor() const { |
| 1057 | DCHECK(IsLoopHeader()); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1058 | return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader(); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 1061 | bool IsFirstPredecessorBackEdge() const { |
| 1062 | DCHECK(IsLoopHeader()); |
| 1063 | return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]); |
| 1064 | } |
| 1065 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1066 | HLoopInformation* GetLoopInformation() const { |
| 1067 | return loop_information_; |
| 1068 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1069 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1070 | // Set the loop_information_ on this block. Overrides the current |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1071 | // 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] | 1072 | // Note that this method is called while creating the loop information. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1073 | void SetInLoop(HLoopInformation* info) { |
| 1074 | if (IsLoopHeader()) { |
| 1075 | // Nothing to do. This just means `info` is an outer loop. |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 1076 | } else if (!IsInLoop()) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1077 | loop_information_ = info; |
| 1078 | } else if (loop_information_->Contains(*info->GetHeader())) { |
| 1079 | // Block is currently part of an outer loop. Make it part of this inner loop. |
| 1080 | // Note that a non loop header having a loop information means this loop information |
| 1081 | // has already been populated |
| 1082 | loop_information_ = info; |
| 1083 | } else { |
| 1084 | // Block is part of an inner loop. Do not update the loop information. |
| 1085 | // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` |
| 1086 | // at this point, because this method is being called while populating `info`. |
| 1087 | } |
| 1088 | } |
| 1089 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1090 | // Raw update of the loop information. |
| 1091 | void SetLoopInformation(HLoopInformation* info) { |
| 1092 | loop_information_ = info; |
| 1093 | } |
| 1094 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 1095 | bool IsInLoop() const { return loop_information_ != nullptr; } |
| 1096 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1097 | TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; } |
| 1098 | |
| 1099 | void SetTryCatchInformation(TryCatchInformation* try_catch_information) { |
| 1100 | try_catch_information_ = try_catch_information; |
| 1101 | } |
| 1102 | |
| 1103 | bool IsTryBlock() const { |
| 1104 | return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock(); |
| 1105 | } |
| 1106 | |
| 1107 | bool IsCatchBlock() const { |
| 1108 | return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock(); |
| 1109 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1110 | |
| 1111 | // Returns the try entry that this block's successors should have. They will |
| 1112 | // be in the same try, unless the block ends in a try boundary. In that case, |
| 1113 | // the appropriate try entry will be returned. |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1114 | const HTryBoundary* ComputeTryEntryOfSuccessors() const; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1115 | |
David Brazdil | d7558da | 2015-09-22 13:04:14 +0100 | [diff] [blame] | 1116 | bool HasThrowingInstructions() const; |
| 1117 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 1118 | // Returns whether this block dominates the blocked passed as parameter. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1119 | bool Dominates(HBasicBlock* block) const; |
| 1120 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1121 | size_t GetLifetimeStart() const { return lifetime_start_; } |
| 1122 | size_t GetLifetimeEnd() const { return lifetime_end_; } |
| 1123 | |
| 1124 | void SetLifetimeStart(size_t start) { lifetime_start_ = start; } |
| 1125 | void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } |
| 1126 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 1127 | bool EndsWithControlFlowInstruction() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1128 | bool EndsWithIf() const; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1129 | bool EndsWithTryBoundary() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1130 | bool HasSinglePhi() const; |
| 1131 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1132 | private: |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1133 | HGraph* graph_; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1134 | ArenaVector<HBasicBlock*> predecessors_; |
| 1135 | ArenaVector<HBasicBlock*> successors_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1136 | HInstructionList instructions_; |
| 1137 | HInstructionList phis_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1138 | HLoopInformation* loop_information_; |
| 1139 | HBasicBlock* dominator_; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1140 | ArenaVector<HBasicBlock*> dominated_blocks_; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1141 | uint32_t block_id_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1142 | // The dex program counter of the first instruction of this block. |
| 1143 | const uint32_t dex_pc_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1144 | size_t lifetime_start_; |
| 1145 | size_t lifetime_end_; |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1146 | TryCatchInformation* try_catch_information_; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1147 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1148 | friend class HGraph; |
| 1149 | friend class HInstruction; |
| 1150 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1151 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 1152 | }; |
| 1153 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1154 | // Iterates over the LoopInformation of all loops which contain 'block' |
| 1155 | // from the innermost to the outermost. |
| 1156 | class HLoopInformationOutwardIterator : public ValueObject { |
| 1157 | public: |
| 1158 | explicit HLoopInformationOutwardIterator(const HBasicBlock& block) |
| 1159 | : current_(block.GetLoopInformation()) {} |
| 1160 | |
| 1161 | bool Done() const { return current_ == nullptr; } |
| 1162 | |
| 1163 | void Advance() { |
| 1164 | DCHECK(!Done()); |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 1165 | current_ = current_->GetPreHeader()->GetLoopInformation(); |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | HLoopInformation* Current() const { |
| 1169 | DCHECK(!Done()); |
| 1170 | return current_; |
| 1171 | } |
| 1172 | |
| 1173 | private: |
| 1174 | HLoopInformation* current_; |
| 1175 | |
| 1176 | DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator); |
| 1177 | }; |
| 1178 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1179 | #define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 1180 | M(Above, Condition) \ |
| 1181 | M(AboveOrEqual, Condition) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1182 | M(Add, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1183 | M(And, BinaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1184 | M(ArrayGet, Instruction) \ |
| 1185 | M(ArrayLength, Instruction) \ |
| 1186 | M(ArraySet, Instruction) \ |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 1187 | M(Below, Condition) \ |
| 1188 | M(BelowOrEqual, Condition) \ |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 1189 | M(BooleanNot, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1190 | M(BoundsCheck, Instruction) \ |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1191 | M(BoundType, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 1192 | M(CheckCast, Instruction) \ |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1193 | M(ClassTableGet, Instruction) \ |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 1194 | M(ClearException, Instruction) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1195 | M(ClinitCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1196 | M(Compare, BinaryOperation) \ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 1197 | M(CurrentMethod, Instruction) \ |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1198 | M(Deoptimize, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1199 | M(Div, BinaryOperation) \ |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1200 | M(DivZeroCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1201 | M(DoubleConstant, Constant) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1202 | M(Equal, Condition) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1203 | M(Exit, Instruction) \ |
| 1204 | M(FloatConstant, Constant) \ |
| 1205 | M(Goto, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1206 | M(GreaterThan, Condition) \ |
| 1207 | M(GreaterThanOrEqual, Condition) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1208 | M(If, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1209 | M(InstanceFieldGet, Instruction) \ |
| 1210 | M(InstanceFieldSet, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 1211 | M(InstanceOf, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1212 | M(IntConstant, Constant) \ |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 1213 | M(InvokeUnresolved, Invoke) \ |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 1214 | M(InvokeInterface, Invoke) \ |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1215 | M(InvokeStaticOrDirect, Invoke) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1216 | M(InvokeVirtual, Invoke) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1217 | M(LessThan, Condition) \ |
| 1218 | M(LessThanOrEqual, Condition) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1219 | M(LoadClass, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1220 | M(LoadException, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1221 | M(LoadLocal, Instruction) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1222 | M(LoadString, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1223 | M(Local, Instruction) \ |
| 1224 | M(LongConstant, Constant) \ |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 1225 | M(MemoryBarrier, Instruction) \ |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 1226 | M(MonitorOperation, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1227 | M(Mul, BinaryOperation) \ |
David Srbecky | 0cf4493 | 2015-12-09 14:09:59 +0000 | [diff] [blame] | 1228 | M(NativeDebugInfo, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1229 | M(Neg, UnaryOperation) \ |
| 1230 | M(NewArray, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1231 | M(NewInstance, Instruction) \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 1232 | M(Not, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1233 | M(NotEqual, Condition) \ |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1234 | M(NullConstant, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1235 | M(NullCheck, Instruction) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1236 | M(Or, BinaryOperation) \ |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1237 | M(PackedSwitch, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1238 | M(ParallelMove, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 1239 | M(ParameterValue, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1240 | M(Phi, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1241 | M(Rem, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1242 | M(Return, Instruction) \ |
| 1243 | M(ReturnVoid, Instruction) \ |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1244 | M(Ror, BinaryOperation) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1245 | M(Shl, BinaryOperation) \ |
| 1246 | M(Shr, BinaryOperation) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1247 | M(StaticFieldGet, Instruction) \ |
| 1248 | M(StaticFieldSet, Instruction) \ |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 1249 | M(UnresolvedInstanceFieldGet, Instruction) \ |
| 1250 | M(UnresolvedInstanceFieldSet, Instruction) \ |
| 1251 | M(UnresolvedStaticFieldGet, Instruction) \ |
| 1252 | M(UnresolvedStaticFieldSet, Instruction) \ |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1253 | M(Select, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1254 | M(StoreLocal, Instruction) \ |
| 1255 | M(Sub, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1256 | M(SuspendCheck, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1257 | M(Throw, Instruction) \ |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1258 | M(TryBoundary, Instruction) \ |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 1259 | M(TypeConversion, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 1260 | M(UShr, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 1261 | M(Xor, BinaryOperation) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1262 | |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 1263 | /* |
| 1264 | * Instructions, shared across several (not all) architectures. |
| 1265 | */ |
| 1266 | #if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64) |
| 1267 | #define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) |
| 1268 | #else |
| 1269 | #define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \ |
Artem Serov | 7fc6350 | 2016-02-09 17:15:29 +0000 | [diff] [blame] | 1270 | M(BitwiseNegatedRight, Instruction) \ |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 1271 | M(MultiplyAccumulate, Instruction) |
| 1272 | #endif |
| 1273 | |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 1274 | #ifndef ART_ENABLE_CODEGEN_arm |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1275 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 1276 | #else |
| 1277 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \ |
| 1278 | M(ArmDexCacheArraysBase, Instruction) |
| 1279 | #endif |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1280 | |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 1281 | #ifndef ART_ENABLE_CODEGEN_arm64 |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1282 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 1283 | #else |
| 1284 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \ |
Alexandre Rames | 8626b74 | 2015-11-25 16:28:08 +0000 | [diff] [blame] | 1285 | M(Arm64DataProcWithShifterOp, Instruction) \ |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 1286 | M(Arm64IntermediateAddress, Instruction) |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 1287 | #endif |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1288 | |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 1289 | #define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) |
| 1290 | |
Alexandre Rames | f39e064 | 2015-06-23 11:33:45 +0100 | [diff] [blame] | 1291 | #define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) |
| 1292 | |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 1293 | #ifndef ART_ENABLE_CODEGEN_x86 |
| 1294 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) |
| 1295 | #else |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 1296 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \ |
| 1297 | M(X86ComputeBaseMethodAddress, Instruction) \ |
Mark Mendell | 805b3b5 | 2015-09-18 14:10:29 -0400 | [diff] [blame] | 1298 | M(X86LoadFromConstantTable, Instruction) \ |
Mark P Mendell | 2f10a5f | 2016-01-25 14:47:50 +0000 | [diff] [blame] | 1299 | M(X86FPNeg, Instruction) \ |
Mark Mendell | 805b3b5 | 2015-09-18 14:10:29 -0400 | [diff] [blame] | 1300 | M(X86PackedSwitch, Instruction) |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 1301 | #endif |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1302 | |
| 1303 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 1304 | |
| 1305 | #define FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 1306 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 1307 | FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \ |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1308 | FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \ |
| 1309 | FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \ |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 1310 | FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \ |
Alexandre Rames | f39e064 | 2015-06-23 11:33:45 +0100 | [diff] [blame] | 1311 | FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \ |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame] | 1312 | FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \ |
| 1313 | FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 1314 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1315 | #define FOR_EACH_ABSTRACT_INSTRUCTION(M) \ |
| 1316 | M(Condition, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1317 | M(Constant, Instruction) \ |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1318 | M(UnaryOperation, Instruction) \ |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 1319 | M(BinaryOperation, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1320 | M(Invoke, Instruction) |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1321 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1322 | #define FOR_EACH_INSTRUCTION(M) \ |
| 1323 | FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 1324 | FOR_EACH_ABSTRACT_INSTRUCTION(M) |
| 1325 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1326 | #define FORWARD_DECLARATION(type, super) class H##type; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1327 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 1328 | #undef FORWARD_DECLARATION |
| 1329 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1330 | #define DECLARE_INSTRUCTION(type) \ |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1331 | InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1332 | const char* DebugName() const OVERRIDE { return #type; } \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1333 | bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1334 | return other->Is##type(); \ |
| 1335 | } \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1336 | void Accept(HGraphVisitor* visitor) OVERRIDE |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1337 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 1338 | #define DECLARE_ABSTRACT_INSTRUCTION(type) \ |
| 1339 | bool Is##type() const { return As##type() != nullptr; } \ |
| 1340 | const H##type* As##type() const { return this; } \ |
| 1341 | H##type* As##type() { return this; } |
| 1342 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1343 | template <typename T> class HUseList; |
| 1344 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1345 | template <typename T> |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1346 | class HUseListNode : public ArenaObject<kArenaAllocUseListNode> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1347 | public: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1348 | HUseListNode* GetPrevious() const { return prev_; } |
| 1349 | HUseListNode* GetNext() const { return next_; } |
| 1350 | T GetUser() const { return user_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1351 | size_t GetIndex() const { return index_; } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1352 | void SetIndex(size_t index) { index_ = index; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1353 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1354 | private: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1355 | HUseListNode(T user, size_t index) |
| 1356 | : user_(user), index_(index), prev_(nullptr), next_(nullptr) {} |
| 1357 | |
| 1358 | T const user_; |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1359 | size_t index_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1360 | HUseListNode<T>* prev_; |
| 1361 | HUseListNode<T>* next_; |
| 1362 | |
| 1363 | friend class HUseList<T>; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1364 | |
| 1365 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 1366 | }; |
| 1367 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1368 | template <typename T> |
| 1369 | class HUseList : public ValueObject { |
| 1370 | public: |
| 1371 | HUseList() : first_(nullptr) {} |
| 1372 | |
| 1373 | void Clear() { |
| 1374 | first_ = nullptr; |
| 1375 | } |
| 1376 | |
| 1377 | // Adds a new entry at the beginning of the use list and returns |
| 1378 | // the newly created node. |
| 1379 | HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) { |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 1380 | HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1381 | if (IsEmpty()) { |
| 1382 | first_ = new_node; |
| 1383 | } else { |
| 1384 | first_->prev_ = new_node; |
| 1385 | new_node->next_ = first_; |
| 1386 | first_ = new_node; |
| 1387 | } |
| 1388 | return new_node; |
| 1389 | } |
| 1390 | |
| 1391 | HUseListNode<T>* GetFirst() const { |
| 1392 | return first_; |
| 1393 | } |
| 1394 | |
| 1395 | void Remove(HUseListNode<T>* node) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1396 | DCHECK(node != nullptr); |
| 1397 | DCHECK(Contains(node)); |
| 1398 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1399 | if (node->prev_ != nullptr) { |
| 1400 | node->prev_->next_ = node->next_; |
| 1401 | } |
| 1402 | if (node->next_ != nullptr) { |
| 1403 | node->next_->prev_ = node->prev_; |
| 1404 | } |
| 1405 | if (node == first_) { |
| 1406 | first_ = node->next_; |
| 1407 | } |
| 1408 | } |
| 1409 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1410 | bool Contains(const HUseListNode<T>* node) const { |
| 1411 | if (node == nullptr) { |
| 1412 | return false; |
| 1413 | } |
| 1414 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1415 | if (current == node) { |
| 1416 | return true; |
| 1417 | } |
| 1418 | } |
| 1419 | return false; |
| 1420 | } |
| 1421 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1422 | bool IsEmpty() const { |
| 1423 | return first_ == nullptr; |
| 1424 | } |
| 1425 | |
| 1426 | bool HasOnlyOneUse() const { |
| 1427 | return first_ != nullptr && first_->next_ == nullptr; |
| 1428 | } |
| 1429 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1430 | size_t SizeSlow() const { |
| 1431 | size_t count = 0; |
| 1432 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1433 | ++count; |
| 1434 | } |
| 1435 | return count; |
| 1436 | } |
| 1437 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1438 | private: |
| 1439 | HUseListNode<T>* first_; |
| 1440 | }; |
| 1441 | |
| 1442 | template<typename T> |
| 1443 | class HUseIterator : public ValueObject { |
| 1444 | public: |
| 1445 | explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {} |
| 1446 | |
| 1447 | bool Done() const { return current_ == nullptr; } |
| 1448 | |
| 1449 | void Advance() { |
| 1450 | DCHECK(!Done()); |
| 1451 | current_ = current_->GetNext(); |
| 1452 | } |
| 1453 | |
| 1454 | HUseListNode<T>* Current() const { |
| 1455 | DCHECK(!Done()); |
| 1456 | return current_; |
| 1457 | } |
| 1458 | |
| 1459 | private: |
| 1460 | HUseListNode<T>* current_; |
| 1461 | |
| 1462 | friend class HValue; |
| 1463 | }; |
| 1464 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1465 | // This class is used by HEnvironment and HInstruction classes to record the |
| 1466 | // instructions they use and pointers to the corresponding HUseListNodes kept |
| 1467 | // by the used instructions. |
| 1468 | template <typename T> |
Vladimir Marko | 76c92ac | 2015-09-17 15:39:16 +0100 | [diff] [blame] | 1469 | class HUserRecord : public ValueObject { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1470 | public: |
| 1471 | HUserRecord() : instruction_(nullptr), use_node_(nullptr) {} |
| 1472 | explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {} |
| 1473 | |
| 1474 | HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node) |
| 1475 | : instruction_(old_record.instruction_), use_node_(use_node) { |
| 1476 | DCHECK(instruction_ != nullptr); |
| 1477 | DCHECK(use_node_ != nullptr); |
| 1478 | DCHECK(old_record.use_node_ == nullptr); |
| 1479 | } |
| 1480 | |
| 1481 | HInstruction* GetInstruction() const { return instruction_; } |
| 1482 | HUseListNode<T>* GetUseNode() const { return use_node_; } |
| 1483 | |
| 1484 | private: |
| 1485 | // Instruction used by the user. |
| 1486 | HInstruction* instruction_; |
| 1487 | |
| 1488 | // Corresponding entry in the use list kept by 'instruction_'. |
| 1489 | HUseListNode<T>* use_node_; |
| 1490 | }; |
| 1491 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1492 | /** |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1493 | * Side-effects representation. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1494 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1495 | * For write/read dependences on fields/arrays, the dependence analysis uses |
| 1496 | * type disambiguation (e.g. a float field write cannot modify the value of an |
| 1497 | * integer field read) and the access type (e.g. a reference array write cannot |
| 1498 | * modify the value of a reference field read [although it may modify the |
| 1499 | * reference fetch prior to reading the field, which is represented by its own |
| 1500 | * write/read dependence]). The analysis makes conservative points-to |
| 1501 | * assumptions on reference types (e.g. two same typed arrays are assumed to be |
| 1502 | * the same, and any reference read depends on any reference read without |
| 1503 | * further regard of its type). |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1504 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1505 | * The internal representation uses 38-bit and is described in the table below. |
| 1506 | * The first line indicates the side effect, and for field/array accesses the |
| 1507 | * second line indicates the type of the access (in the order of the |
| 1508 | * Primitive::Type enum). |
| 1509 | * The two numbered lines below indicate the bit position in the bitfield (read |
| 1510 | * vertically). |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1511 | * |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1512 | * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W | |
| 1513 | * +-------------+---------+---------+--------------+---------+---------+ |
| 1514 | * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL| |
| 1515 | * | 3 |333333322|222222221| 1 |111111110|000000000| |
| 1516 | * | 7 |654321098|765432109| 8 |765432109|876543210| |
| 1517 | * |
| 1518 | * Note that, to ease the implementation, 'changes' bits are least significant |
| 1519 | * bits, while 'dependency' bits are most significant bits. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1520 | */ |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1521 | class SideEffects : public ValueObject { |
| 1522 | public: |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1523 | SideEffects() : flags_(0) {} |
| 1524 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1525 | static SideEffects None() { |
| 1526 | return SideEffects(0); |
| 1527 | } |
| 1528 | |
| 1529 | static SideEffects All() { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1530 | return SideEffects(kAllChangeBits | kAllDependOnBits); |
| 1531 | } |
| 1532 | |
| 1533 | static SideEffects AllChanges() { |
| 1534 | return SideEffects(kAllChangeBits); |
| 1535 | } |
| 1536 | |
| 1537 | static SideEffects AllDependencies() { |
| 1538 | return SideEffects(kAllDependOnBits); |
| 1539 | } |
| 1540 | |
| 1541 | static SideEffects AllExceptGCDependency() { |
| 1542 | return AllWritesAndReads().Union(SideEffects::CanTriggerGC()); |
| 1543 | } |
| 1544 | |
| 1545 | static SideEffects AllWritesAndReads() { |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1546 | return SideEffects(kAllWrites | kAllReads); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1549 | static SideEffects AllWrites() { |
| 1550 | return SideEffects(kAllWrites); |
| 1551 | } |
| 1552 | |
| 1553 | static SideEffects AllReads() { |
| 1554 | return SideEffects(kAllReads); |
| 1555 | } |
| 1556 | |
| 1557 | static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) { |
| 1558 | return is_volatile |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1559 | ? AllWritesAndReads() |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1560 | : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset)); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1563 | static SideEffects ArrayWriteOfType(Primitive::Type type) { |
| 1564 | return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset)); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1565 | } |
| 1566 | |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1567 | static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) { |
| 1568 | return is_volatile |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1569 | ? AllWritesAndReads() |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 1570 | : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset)); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | static SideEffects ArrayReadOfType(Primitive::Type type) { |
| 1574 | return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset)); |
| 1575 | } |
| 1576 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1577 | static SideEffects CanTriggerGC() { |
| 1578 | return SideEffects(1ULL << kCanTriggerGCBit); |
| 1579 | } |
| 1580 | |
| 1581 | static SideEffects DependsOnGC() { |
| 1582 | return SideEffects(1ULL << kDependsOnGCBit); |
| 1583 | } |
| 1584 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1585 | // Combines the side-effects of this and the other. |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1586 | SideEffects Union(SideEffects other) const { |
| 1587 | return SideEffects(flags_ | other.flags_); |
| 1588 | } |
| 1589 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1590 | SideEffects Exclusion(SideEffects other) const { |
| 1591 | return SideEffects(flags_ & ~other.flags_); |
| 1592 | } |
| 1593 | |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 1594 | void Add(SideEffects other) { |
| 1595 | flags_ |= other.flags_; |
| 1596 | } |
| 1597 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1598 | bool Includes(SideEffects other) const { |
| 1599 | return (other.flags_ & flags_) == other.flags_; |
| 1600 | } |
| 1601 | |
| 1602 | bool HasSideEffects() const { |
| 1603 | return (flags_ & kAllChangeBits); |
| 1604 | } |
| 1605 | |
| 1606 | bool HasDependencies() const { |
| 1607 | return (flags_ & kAllDependOnBits); |
| 1608 | } |
| 1609 | |
| 1610 | // Returns true if there are no side effects or dependencies. |
| 1611 | bool DoesNothing() const { |
| 1612 | return flags_ == 0; |
| 1613 | } |
| 1614 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1615 | // Returns true if something is written. |
| 1616 | bool DoesAnyWrite() const { |
| 1617 | return (flags_ & kAllWrites); |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1618 | } |
| 1619 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1620 | // Returns true if something is read. |
| 1621 | bool DoesAnyRead() const { |
| 1622 | return (flags_ & kAllReads); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1625 | // Returns true if potentially everything is written and read |
| 1626 | // (every type and every kind of access). |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1627 | bool DoesAllReadWrite() const { |
| 1628 | return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads); |
| 1629 | } |
| 1630 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1631 | bool DoesAll() const { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1632 | return flags_ == (kAllChangeBits | kAllDependOnBits); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1635 | // Returns true if `this` may read something written by `other`. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1636 | bool MayDependOn(SideEffects other) const { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1637 | const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits; |
| 1638 | return (other.flags_ & depends_on_flags); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | // Returns string representation of flags (for debugging only). |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1642 | // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL| |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1643 | std::string ToString() const { |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1644 | std::string flags = "|"; |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1645 | for (int s = kLastBit; s >= 0; s--) { |
| 1646 | bool current_bit_is_set = ((flags_ >> s) & 1) != 0; |
| 1647 | if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) { |
| 1648 | // This is a bit for the GC side effect. |
| 1649 | if (current_bit_is_set) { |
| 1650 | flags += "GC"; |
| 1651 | } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1652 | flags += "|"; |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1653 | } else { |
| 1654 | // This is a bit for the array/field analysis. |
| 1655 | // The underscore character stands for the 'can trigger GC' bit. |
| 1656 | static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD"; |
| 1657 | if (current_bit_is_set) { |
| 1658 | flags += kDebug[s]; |
| 1659 | } |
| 1660 | if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) || |
| 1661 | (s == kFieldReadOffset) || (s == kArrayReadOffset)) { |
| 1662 | flags += "|"; |
| 1663 | } |
| 1664 | } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1665 | } |
| 1666 | return flags; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1667 | } |
| 1668 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1669 | bool Equals(const SideEffects& other) const { return flags_ == other.flags_; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1670 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1671 | private: |
| 1672 | static constexpr int kFieldArrayAnalysisBits = 9; |
| 1673 | |
| 1674 | static constexpr int kFieldWriteOffset = 0; |
| 1675 | static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits; |
| 1676 | static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1; |
| 1677 | static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1; |
| 1678 | |
| 1679 | static constexpr int kChangeBits = kCanTriggerGCBit + 1; |
| 1680 | |
| 1681 | static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1; |
| 1682 | static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits; |
| 1683 | static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1; |
| 1684 | static constexpr int kDependsOnGCBit = kLastBitForReads + 1; |
| 1685 | |
| 1686 | static constexpr int kLastBit = kDependsOnGCBit; |
| 1687 | static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits; |
| 1688 | |
| 1689 | // Aliases. |
| 1690 | |
| 1691 | static_assert(kChangeBits == kDependOnBits, |
| 1692 | "the 'change' bits should match the 'depend on' bits."); |
| 1693 | |
| 1694 | static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1); |
| 1695 | static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits; |
| 1696 | static constexpr uint64_t kAllWrites = |
| 1697 | ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset; |
| 1698 | static constexpr uint64_t kAllReads = |
| 1699 | ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1700 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1701 | // Work around the fact that HIR aliases I/F and J/D. |
| 1702 | // TODO: remove this interceptor once HIR types are clean |
| 1703 | static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) { |
| 1704 | switch (type) { |
| 1705 | case Primitive::kPrimInt: |
| 1706 | case Primitive::kPrimFloat: |
| 1707 | return TypeFlag(Primitive::kPrimInt, offset) | |
| 1708 | TypeFlag(Primitive::kPrimFloat, offset); |
| 1709 | case Primitive::kPrimLong: |
| 1710 | case Primitive::kPrimDouble: |
| 1711 | return TypeFlag(Primitive::kPrimLong, offset) | |
| 1712 | TypeFlag(Primitive::kPrimDouble, offset); |
| 1713 | default: |
| 1714 | return TypeFlag(type, offset); |
| 1715 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1716 | } |
| 1717 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1718 | // Translates type to bit flag. |
| 1719 | static uint64_t TypeFlag(Primitive::Type type, int offset) { |
| 1720 | CHECK_NE(type, Primitive::kPrimVoid); |
| 1721 | const uint64_t one = 1; |
| 1722 | const int shift = type; // 0-based consecutive enum |
| 1723 | DCHECK_LE(kFieldWriteOffset, shift); |
| 1724 | DCHECK_LT(shift, kArrayWriteOffset); |
| 1725 | return one << (type + offset); |
| 1726 | } |
| 1727 | |
| 1728 | // Private constructor on direct flags value. |
| 1729 | explicit SideEffects(uint64_t flags) : flags_(flags) {} |
| 1730 | |
| 1731 | uint64_t flags_; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1732 | }; |
| 1733 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1734 | // A HEnvironment object contains the values of virtual registers at a given location. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1735 | class HEnvironment : public ArenaObject<kArenaAllocEnvironment> { |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1736 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1737 | HEnvironment(ArenaAllocator* arena, |
| 1738 | size_t number_of_vregs, |
| 1739 | const DexFile& dex_file, |
| 1740 | uint32_t method_idx, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1741 | uint32_t dex_pc, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1742 | InvokeType invoke_type, |
| 1743 | HInstruction* holder) |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1744 | : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)), |
| 1745 | locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1746 | parent_(nullptr), |
| 1747 | dex_file_(dex_file), |
| 1748 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1749 | dex_pc_(dex_pc), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1750 | invoke_type_(invoke_type), |
| 1751 | holder_(holder) { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1752 | } |
| 1753 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1754 | HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1755 | : HEnvironment(arena, |
| 1756 | to_copy.Size(), |
| 1757 | to_copy.GetDexFile(), |
| 1758 | to_copy.GetMethodIdx(), |
| 1759 | to_copy.GetDexPc(), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1760 | to_copy.GetInvokeType(), |
| 1761 | holder) {} |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1762 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1763 | void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1764 | if (parent_ != nullptr) { |
| 1765 | parent_->SetAndCopyParentChain(allocator, parent); |
| 1766 | } else { |
| 1767 | parent_ = new (allocator) HEnvironment(allocator, *parent, holder_); |
| 1768 | parent_->CopyFrom(parent); |
| 1769 | if (parent->GetParent() != nullptr) { |
| 1770 | parent_->SetAndCopyParentChain(allocator, parent->GetParent()); |
| 1771 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1772 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 1775 | void CopyFrom(const ArenaVector<HInstruction*>& locals); |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 1776 | void CopyFrom(HEnvironment* environment); |
| 1777 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1778 | // Copy from `env`. If it's a loop phi for `loop_header`, copy the first |
| 1779 | // input to the loop phi instead. This is for inserting instructions that |
| 1780 | // require an environment (like HDeoptimization) in the loop pre-header. |
| 1781 | void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1782 | |
| 1783 | void SetRawEnvAt(size_t index, HInstruction* instruction) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1784 | vregs_[index] = HUserRecord<HEnvironment*>(instruction); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | HInstruction* GetInstructionAt(size_t index) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1788 | return vregs_[index].GetInstruction(); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1791 | void RemoveAsUserOfInput(size_t index) const; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1792 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1793 | size_t Size() const { return vregs_.size(); } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1794 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1795 | HEnvironment* GetParent() const { return parent_; } |
| 1796 | |
| 1797 | void SetLocationAt(size_t index, Location location) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1798 | locations_[index] = location; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | Location GetLocationAt(size_t index) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1802 | return locations_[index]; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | uint32_t GetDexPc() const { |
| 1806 | return dex_pc_; |
| 1807 | } |
| 1808 | |
| 1809 | uint32_t GetMethodIdx() const { |
| 1810 | return method_idx_; |
| 1811 | } |
| 1812 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1813 | InvokeType GetInvokeType() const { |
| 1814 | return invoke_type_; |
| 1815 | } |
| 1816 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1817 | const DexFile& GetDexFile() const { |
| 1818 | return dex_file_; |
| 1819 | } |
| 1820 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1821 | HInstruction* GetHolder() const { |
| 1822 | return holder_; |
| 1823 | } |
| 1824 | |
Nicolas Geoffray | 8e1ef53 | 2015-11-23 12:04:37 +0000 | [diff] [blame] | 1825 | |
| 1826 | bool IsFromInlinedInvoke() const { |
| 1827 | return GetParent() != nullptr; |
| 1828 | } |
| 1829 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1830 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1831 | // Record instructions' use entries of this environment for constant-time removal. |
| 1832 | // It should only be called by HInstruction when a new environment use is added. |
| 1833 | void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) { |
| 1834 | DCHECK(env_use->GetUser() == this); |
| 1835 | size_t index = env_use->GetIndex(); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1836 | vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1837 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1838 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1839 | ArenaVector<HUserRecord<HEnvironment*>> vregs_; |
| 1840 | ArenaVector<Location> locations_; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1841 | HEnvironment* parent_; |
| 1842 | const DexFile& dex_file_; |
| 1843 | const uint32_t method_idx_; |
| 1844 | const uint32_t dex_pc_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1845 | const InvokeType invoke_type_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1846 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 1847 | // The instruction that holds this environment. |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1848 | HInstruction* const holder_; |
| 1849 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1850 | friend class HInstruction; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1851 | |
| 1852 | DISALLOW_COPY_AND_ASSIGN(HEnvironment); |
| 1853 | }; |
| 1854 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1855 | class HInstruction : public ArenaObject<kArenaAllocInstruction> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1856 | public: |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 1857 | HInstruction(SideEffects side_effects, uint32_t dex_pc) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1858 | : previous_(nullptr), |
| 1859 | next_(nullptr), |
| 1860 | block_(nullptr), |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1861 | dex_pc_(dex_pc), |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1862 | id_(-1), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1863 | ssa_index_(-1), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 1864 | packed_fields_(0u), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1865 | environment_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1866 | locations_(nullptr), |
| 1867 | live_interval_(nullptr), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1868 | lifetime_position_(kNoLifetime), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1869 | side_effects_(side_effects), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 1870 | reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) { |
| 1871 | SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact()); |
| 1872 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1873 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1874 | virtual ~HInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1875 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1876 | #define DECLARE_KIND(type, super) k##type, |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1877 | enum InstructionKind { |
| 1878 | FOR_EACH_INSTRUCTION(DECLARE_KIND) |
| 1879 | }; |
| 1880 | #undef DECLARE_KIND |
| 1881 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1882 | HInstruction* GetNext() const { return next_; } |
| 1883 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1884 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1885 | HInstruction* GetNextDisregardingMoves() const; |
| 1886 | HInstruction* GetPreviousDisregardingMoves() const; |
| 1887 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1888 | HBasicBlock* GetBlock() const { return block_; } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1889 | ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1890 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 1891 | bool IsInBlock() const { return block_ != nullptr; } |
| 1892 | bool IsInLoop() const { return block_->IsInLoop(); } |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 1893 | bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); } |
| 1894 | bool IsIrreducibleLoopHeaderPhi() const { |
| 1895 | return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible(); |
| 1896 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1897 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1898 | virtual size_t InputCount() const = 0; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1899 | HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1900 | |
| 1901 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 1902 | virtual const char* DebugName() const = 0; |
| 1903 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1904 | virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1905 | void SetRawInputAt(size_t index, HInstruction* input) { |
| 1906 | SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input)); |
| 1907 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1908 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1909 | virtual bool NeedsEnvironment() const { return false; } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1910 | |
| 1911 | uint32_t GetDexPc() const { return dex_pc_; } |
| 1912 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1913 | virtual bool IsControlFlow() const { return false; } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1914 | |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 1915 | virtual bool CanThrow() const { return false; } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1916 | bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1917 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 1918 | bool HasSideEffects() const { return side_effects_.HasSideEffects(); } |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 1919 | bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1920 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1921 | // Does not apply for all instructions, but having this at top level greatly |
| 1922 | // simplifies the null check elimination. |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 1923 | // TODO: Consider merging can_be_null into ReferenceTypeInfo. |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1924 | virtual bool CanBeNull() const { |
| 1925 | DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types"; |
| 1926 | return true; |
| 1927 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1928 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1929 | virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const { |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1930 | return false; |
| 1931 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1932 | |
Nicolas Geoffray | a3eca2d | 2016-01-12 16:03:16 +0000 | [diff] [blame] | 1933 | virtual bool IsActualObject() const { |
| 1934 | return GetType() == Primitive::kPrimNot; |
| 1935 | } |
| 1936 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1937 | void SetReferenceTypeInfo(ReferenceTypeInfo rti); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1938 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1939 | ReferenceTypeInfo GetReferenceTypeInfo() const { |
| 1940 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 1941 | return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_, |
| 1942 | GetPackedFlag<kFlagReferenceTypeIsExact>());; |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1943 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1944 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1945 | void AddUseAt(HInstruction* user, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1946 | DCHECK(user != nullptr); |
| 1947 | HUseListNode<HInstruction*>* use = |
| 1948 | uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1949 | user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1952 | void AddEnvUseAt(HEnvironment* user, size_t index) { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 1953 | DCHECK(user != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1954 | HUseListNode<HEnvironment*>* env_use = |
| 1955 | env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1956 | user->RecordEnvUse(env_use); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1959 | void RemoveAsUserOfInput(size_t input) { |
| 1960 | HUserRecord<HInstruction*> input_use = InputRecordAt(input); |
| 1961 | input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode()); |
| 1962 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1963 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1964 | const HUseList<HInstruction*>& GetUses() const { return uses_; } |
| 1965 | const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1966 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1967 | bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); } |
| 1968 | bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); } |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1969 | bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1970 | bool HasOnlyOneNonEnvironmentUse() const { |
| 1971 | return !HasEnvironmentUses() && GetUses().HasOnlyOneUse(); |
| 1972 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1973 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 1974 | // Does this instruction strictly dominate `other_instruction`? |
| 1975 | // Returns false if this instruction and `other_instruction` are the same. |
| 1976 | // Aborts if this instruction and `other_instruction` are both phis. |
| 1977 | bool StrictlyDominates(HInstruction* other_instruction) const; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1978 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1979 | int GetId() const { return id_; } |
| 1980 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1981 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1982 | int GetSsaIndex() const { return ssa_index_; } |
| 1983 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 1984 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 1985 | |
| 1986 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 1987 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1988 | // Set the `environment_` field. Raw because this method does not |
| 1989 | // update the uses lists. |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1990 | void SetRawEnvironment(HEnvironment* environment) { |
| 1991 | DCHECK(environment_ == nullptr); |
| 1992 | DCHECK_EQ(environment->GetHolder(), this); |
| 1993 | environment_ = environment; |
| 1994 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1995 | |
| 1996 | // Set the environment of this instruction, copying it from `environment`. While |
| 1997 | // copying, the uses lists are being updated. |
| 1998 | void CopyEnvironmentFrom(HEnvironment* environment) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1999 | DCHECK(environment_ == nullptr); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 2000 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 2001 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 2002 | environment_->CopyFrom(environment); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2003 | if (environment->GetParent() != nullptr) { |
| 2004 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 2005 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 2006 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2007 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 2008 | void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment, |
| 2009 | HBasicBlock* block) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 2010 | DCHECK(environment_ == nullptr); |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 2011 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 2012 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2013 | environment_->CopyFromWithLoopPhiAdjustment(environment, block); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2014 | if (environment->GetParent() != nullptr) { |
| 2015 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 2016 | } |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2019 | // Returns the number of entries in the environment. Typically, that is the |
| 2020 | // number of dex registers in a method. It could be more in case of inlining. |
| 2021 | size_t EnvironmentSize() const; |
| 2022 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2023 | LocationSummary* GetLocations() const { return locations_; } |
| 2024 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2025 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2026 | void ReplaceWith(HInstruction* instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2027 | void ReplaceInput(HInstruction* replacement, size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2028 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2029 | // This is almost the same as doing `ReplaceWith()`. But in this helper, the |
| 2030 | // uses of this instruction by `other` are *not* updated. |
| 2031 | void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) { |
| 2032 | ReplaceWith(other); |
| 2033 | other->ReplaceInput(this, use_index); |
| 2034 | } |
| 2035 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 2036 | // Move `this` instruction before `cursor`. |
| 2037 | void MoveBefore(HInstruction* cursor); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2038 | |
Vladimir Marko | fb337ea | 2015-11-25 15:25:10 +0000 | [diff] [blame] | 2039 | // Move `this` before its first user and out of any loops. If there is no |
| 2040 | // out-of-loop user that dominates all other users, move the instruction |
| 2041 | // to the end of the out-of-loop common dominator of the user's blocks. |
| 2042 | // |
| 2043 | // This can be used only on non-throwing instructions with no side effects that |
| 2044 | // have at least one use but no environment uses. |
| 2045 | void MoveBeforeFirstUserAndOutOfLoops(); |
| 2046 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2047 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2048 | bool Is##type() const; \ |
| 2049 | const H##type* As##type() const; \ |
| 2050 | H##type* As##type(); |
| 2051 | |
| 2052 | FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 2053 | #undef INSTRUCTION_TYPE_CHECK |
| 2054 | |
| 2055 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 2056 | bool Is##type() const { return (As##type() != nullptr); } \ |
| 2057 | virtual const H##type* As##type() const { return nullptr; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2058 | virtual H##type* As##type() { return nullptr; } |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2059 | FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2060 | #undef INSTRUCTION_TYPE_CHECK |
| 2061 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2062 | // Returns whether the instruction can be moved within the graph. |
| 2063 | virtual bool CanBeMoved() const { return false; } |
| 2064 | |
| 2065 | // Returns whether the two instructions are of the same kind. |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2066 | virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2067 | return false; |
| 2068 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2069 | |
| 2070 | // Returns whether any data encoded in the two instructions is equal. |
| 2071 | // This method does not look at the inputs. Both instructions must be |
| 2072 | // of the same type, otherwise the method has undefined behavior. |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2073 | virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2074 | return false; |
| 2075 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2076 | |
| 2077 | // Returns whether two instructions are equal, that is: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2078 | // 1) They have the same type and contain the same data (InstructionDataEquals). |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2079 | // 2) Their inputs are identical. |
| 2080 | bool Equals(HInstruction* other) const; |
| 2081 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2082 | // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744) |
| 2083 | // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide |
| 2084 | // the virtual function because the __attribute__((__pure__)) doesn't really |
| 2085 | // apply the strong requirement for virtual functions, preventing optimizations. |
| 2086 | InstructionKind GetKind() const PURE; |
| 2087 | virtual InstructionKind GetKindInternal() const = 0; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 2088 | |
| 2089 | virtual size_t ComputeHashCode() const { |
| 2090 | size_t result = GetKind(); |
| 2091 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 2092 | result = (result * 31) + InputAt(i)->GetId(); |
| 2093 | } |
| 2094 | return result; |
| 2095 | } |
| 2096 | |
| 2097 | SideEffects GetSideEffects() const { return side_effects_; } |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 2098 | void SetSideEffects(SideEffects other) { side_effects_ = other; } |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 2099 | void AddSideEffects(SideEffects other) { side_effects_.Add(other); } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 2100 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2101 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 2102 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 2103 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 2104 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 2105 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 2106 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 2107 | bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); } |
| 2108 | |
| 2109 | // Returns whether the code generation of the instruction will require to have access |
| 2110 | // to the current method. Such instructions are: |
| 2111 | // (1): Instructions that require an environment, as calling the runtime requires |
| 2112 | // to walk the stack and have the current method stored at a specific stack address. |
| 2113 | // (2): Object literals like classes and strings, that are loaded from the dex cache |
| 2114 | // fields of the current method. |
| 2115 | bool NeedsCurrentMethod() const { |
| 2116 | return NeedsEnvironment() || IsLoadClass() || IsLoadString(); |
| 2117 | } |
| 2118 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 2119 | // Returns whether the code generation of the instruction will require to have access |
| 2120 | // to the dex cache of the current method's declaring class via the current method. |
| 2121 | virtual bool NeedsDexCacheOfDeclaringClass() const { return false; } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 2122 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2123 | // Does this instruction have any use in an environment before |
| 2124 | // control flow hits 'other'? |
| 2125 | bool HasAnyEnvironmentUseBefore(HInstruction* other); |
| 2126 | |
| 2127 | // Remove all references to environment uses of this instruction. |
| 2128 | // The caller must ensure that this is safe to do. |
| 2129 | void RemoveEnvironmentUsers(); |
| 2130 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2131 | bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); } |
| 2132 | void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); } |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 2133 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2134 | protected: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2135 | // If set, the machine code for this instruction is assumed to be generated by |
| 2136 | // its users. Used by liveness analysis to compute use positions accordingly. |
| 2137 | static constexpr size_t kFlagEmittedAtUseSite = 0u; |
| 2138 | static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1; |
| 2139 | static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1; |
| 2140 | static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte; |
| 2141 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2142 | virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0; |
| 2143 | virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0; |
| 2144 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2145 | uint32_t GetPackedFields() const { |
| 2146 | return packed_fields_; |
| 2147 | } |
| 2148 | |
| 2149 | template <size_t flag> |
| 2150 | bool GetPackedFlag() const { |
| 2151 | return (packed_fields_ & (1u << flag)) != 0u; |
| 2152 | } |
| 2153 | |
| 2154 | template <size_t flag> |
| 2155 | void SetPackedFlag(bool value = true) { |
| 2156 | packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag); |
| 2157 | } |
| 2158 | |
| 2159 | template <typename BitFieldType> |
| 2160 | typename BitFieldType::value_type GetPackedField() const { |
| 2161 | return BitFieldType::Decode(packed_fields_); |
| 2162 | } |
| 2163 | |
| 2164 | template <typename BitFieldType> |
| 2165 | void SetPackedField(typename BitFieldType::value_type value) { |
| 2166 | DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value))); |
| 2167 | packed_fields_ = BitFieldType::Update(value, packed_fields_); |
| 2168 | } |
| 2169 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2170 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2171 | void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); } |
| 2172 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2173 | HInstruction* previous_; |
| 2174 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 2175 | HBasicBlock* block_; |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2176 | const uint32_t dex_pc_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2177 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2178 | // An instruction gets an id when it is added to the graph. |
| 2179 | // It reflects creation order. A negative id means the instruction |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2180 | // has not been added to the graph. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2181 | int id_; |
| 2182 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 2183 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 2184 | int ssa_index_; |
| 2185 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2186 | // Packed fields. |
| 2187 | uint32_t packed_fields_; |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 2188 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2189 | // List of instructions that have this instruction as input. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 2190 | HUseList<HInstruction*> uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2191 | |
| 2192 | // List of environments that contain this instruction. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 2193 | HUseList<HEnvironment*> env_uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2194 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 2195 | // The environment associated with this instruction. Not null if the instruction |
| 2196 | // might jump out of the method. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2197 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2198 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2199 | // Set by the code generator. |
| 2200 | LocationSummary* locations_; |
| 2201 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2202 | // Set by the liveness analysis. |
| 2203 | LiveInterval* live_interval_; |
| 2204 | |
| 2205 | // Set by the liveness analysis, this is the position in a linear |
| 2206 | // order of blocks where this instruction's live interval start. |
| 2207 | size_t lifetime_position_; |
| 2208 | |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 2209 | SideEffects side_effects_; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2210 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2211 | // The reference handle part of the reference type info. |
| 2212 | // The IsExact() flag is stored in packed fields. |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 2213 | // TODO: for primitive types this should be marked as invalid. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2214 | ReferenceTypeInfo::TypeHandle reference_type_handle_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 2215 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2216 | friend class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2217 | friend class HBasicBlock; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2218 | friend class HEnvironment; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2219 | friend class HGraph; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2220 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2221 | |
| 2222 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 2223 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2224 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2225 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2226 | class HInputIterator : public ValueObject { |
| 2227 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2228 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2229 | |
| 2230 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 2231 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 2232 | void Advance() { index_++; } |
| 2233 | |
| 2234 | private: |
| 2235 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2236 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2237 | |
| 2238 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 2239 | }; |
| 2240 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2241 | class HInstructionIterator : public ValueObject { |
| 2242 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2243 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 2244 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2245 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2248 | bool Done() const { return instruction_ == nullptr; } |
| 2249 | HInstruction* Current() const { return instruction_; } |
| 2250 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2251 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2252 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | private: |
| 2256 | HInstruction* instruction_; |
| 2257 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2258 | |
| 2259 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2260 | }; |
| 2261 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 2262 | class HBackwardInstructionIterator : public ValueObject { |
| 2263 | public: |
| 2264 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 2265 | : instruction_(instructions.last_instruction_) { |
| 2266 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 2267 | } |
| 2268 | |
| 2269 | bool Done() const { return instruction_ == nullptr; } |
| 2270 | HInstruction* Current() const { return instruction_; } |
| 2271 | void Advance() { |
| 2272 | instruction_ = next_; |
| 2273 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 2274 | } |
| 2275 | |
| 2276 | private: |
| 2277 | HInstruction* instruction_; |
| 2278 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 2279 | |
| 2280 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 2281 | }; |
| 2282 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2283 | template<size_t N> |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2284 | class HTemplateInstruction: public HInstruction { |
| 2285 | public: |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 2286 | HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2287 | : HInstruction(side_effects, dex_pc), inputs_() {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2288 | virtual ~HTemplateInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2289 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2290 | size_t InputCount() const OVERRIDE { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2291 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2292 | protected: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2293 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 2294 | DCHECK_LT(i, N); |
| 2295 | return inputs_[i]; |
| 2296 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2297 | |
| 2298 | void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE { |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2299 | DCHECK_LT(i, N); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2300 | inputs_[i] = input; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2303 | private: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2304 | std::array<HUserRecord<HInstruction*>, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2305 | |
| 2306 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2307 | }; |
| 2308 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2309 | // HTemplateInstruction specialization for N=0. |
| 2310 | template<> |
| 2311 | class HTemplateInstruction<0>: public HInstruction { |
| 2312 | public: |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 2313 | explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2314 | : HInstruction(side_effects, dex_pc) {} |
| 2315 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 2316 | virtual ~HTemplateInstruction() {} |
| 2317 | |
| 2318 | size_t InputCount() const OVERRIDE { return 0; } |
| 2319 | |
| 2320 | protected: |
| 2321 | const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2322 | LOG(FATAL) << "Unreachable"; |
| 2323 | UNREACHABLE(); |
| 2324 | } |
| 2325 | |
| 2326 | void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED, |
| 2327 | const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE { |
| 2328 | LOG(FATAL) << "Unreachable"; |
| 2329 | UNREACHABLE(); |
| 2330 | } |
| 2331 | |
| 2332 | private: |
| 2333 | friend class SsaBuilder; |
| 2334 | }; |
| 2335 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2336 | template<intptr_t N> |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2337 | class HExpression : public HTemplateInstruction<N> { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2338 | public: |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 2339 | HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc) |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2340 | : HTemplateInstruction<N>(side_effects, dex_pc) { |
| 2341 | this->template SetPackedField<TypeField>(type); |
| 2342 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2343 | virtual ~HExpression() {} |
| 2344 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2345 | Primitive::Type GetType() const OVERRIDE { |
| 2346 | return TypeField::Decode(this->GetPackedFields()); |
| 2347 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2348 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2349 | protected: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2350 | static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits; |
| 2351 | static constexpr size_t kFieldTypeSize = |
| 2352 | MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast)); |
| 2353 | static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize; |
| 2354 | static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits, |
| 2355 | "Too many packed fields."); |
| 2356 | using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2357 | }; |
| 2358 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2359 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 2360 | // instruction that branches to the exit block. |
| 2361 | class HReturnVoid : public HTemplateInstruction<0> { |
| 2362 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2363 | explicit HReturnVoid(uint32_t dex_pc = kNoDexPc) |
| 2364 | : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2365 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2366 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2367 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2368 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2369 | |
| 2370 | private: |
| 2371 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 2372 | }; |
| 2373 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2374 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 2375 | // instruction that branches to the exit block. |
| 2376 | class HReturn : public HTemplateInstruction<1> { |
| 2377 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2378 | explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc) |
| 2379 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2380 | SetRawInputAt(0, value); |
| 2381 | } |
| 2382 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2383 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2384 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2385 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2386 | |
| 2387 | private: |
| 2388 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 2389 | }; |
| 2390 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2391 | // The exit instruction is the only instruction of the exit block. |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2392 | // Instructions aborting the method (HThrow and HReturn) must branch to the |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2393 | // exit block. |
| 2394 | class HExit : public HTemplateInstruction<0> { |
| 2395 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2396 | explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 2397 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2398 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2399 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2400 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2401 | |
| 2402 | private: |
| 2403 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 2404 | }; |
| 2405 | |
| 2406 | // Jumps from one block to another. |
| 2407 | class HGoto : public HTemplateInstruction<0> { |
| 2408 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2409 | explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2410 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2411 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2412 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 2413 | HBasicBlock* GetSuccessor() const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2414 | return GetBlock()->GetSingleSuccessor(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2417 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2418 | |
| 2419 | private: |
| 2420 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 2421 | }; |
| 2422 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2423 | class HConstant : public HExpression<0> { |
| 2424 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2425 | explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2426 | : HExpression(type, SideEffects::None(), dex_pc) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2427 | |
| 2428 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2429 | |
| 2430 | virtual bool IsMinusOne() const { return false; } |
| 2431 | virtual bool IsZero() const { return false; } |
| 2432 | virtual bool IsOne() const { return false; } |
| 2433 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 2434 | virtual uint64_t GetValueAsUint64() const = 0; |
| 2435 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2436 | DECLARE_ABSTRACT_INSTRUCTION(Constant); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2437 | |
| 2438 | private: |
| 2439 | DISALLOW_COPY_AND_ASSIGN(HConstant); |
| 2440 | }; |
| 2441 | |
| 2442 | class HNullConstant : public HConstant { |
| 2443 | public: |
| 2444 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2445 | return true; |
| 2446 | } |
| 2447 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 2448 | uint64_t GetValueAsUint64() const OVERRIDE { return 0; } |
| 2449 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2450 | size_t ComputeHashCode() const OVERRIDE { return 0; } |
| 2451 | |
| 2452 | DECLARE_INSTRUCTION(NullConstant); |
| 2453 | |
| 2454 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2455 | explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2456 | |
| 2457 | friend class HGraph; |
| 2458 | DISALLOW_COPY_AND_ASSIGN(HNullConstant); |
| 2459 | }; |
| 2460 | |
| 2461 | // Constants of the type int. Those can be from Dex instructions, or |
| 2462 | // synthesized (for example with the if-eqz instruction). |
| 2463 | class HIntConstant : public HConstant { |
| 2464 | public: |
| 2465 | int32_t GetValue() const { return value_; } |
| 2466 | |
David Brazdil | 9f389d4 | 2015-10-01 14:32:56 +0100 | [diff] [blame] | 2467 | uint64_t GetValueAsUint64() const OVERRIDE { |
| 2468 | return static_cast<uint64_t>(static_cast<uint32_t>(value_)); |
| 2469 | } |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 2470 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2471 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2472 | DCHECK(other->IsIntConstant()) << other->DebugName(); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2473 | return other->AsIntConstant()->value_ == value_; |
| 2474 | } |
| 2475 | |
| 2476 | size_t ComputeHashCode() const OVERRIDE { return GetValue(); } |
| 2477 | |
| 2478 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2479 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2480 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2481 | |
| 2482 | DECLARE_INSTRUCTION(IntConstant); |
| 2483 | |
| 2484 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2485 | explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) |
| 2486 | : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {} |
| 2487 | explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc) |
| 2488 | : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2489 | |
| 2490 | const int32_t value_; |
| 2491 | |
| 2492 | friend class HGraph; |
| 2493 | ART_FRIEND_TEST(GraphTest, InsertInstructionBefore); |
| 2494 | ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast); |
| 2495 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 2496 | }; |
| 2497 | |
| 2498 | class HLongConstant : public HConstant { |
| 2499 | public: |
| 2500 | int64_t GetValue() const { return value_; } |
| 2501 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 2502 | uint64_t GetValueAsUint64() const OVERRIDE { return value_; } |
| 2503 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2504 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2505 | DCHECK(other->IsLongConstant()) << other->DebugName(); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2506 | return other->AsLongConstant()->value_ == value_; |
| 2507 | } |
| 2508 | |
| 2509 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
| 2510 | |
| 2511 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2512 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2513 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2514 | |
| 2515 | DECLARE_INSTRUCTION(LongConstant); |
| 2516 | |
| 2517 | private: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2518 | explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) |
| 2519 | : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {} |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2520 | |
| 2521 | const int64_t value_; |
| 2522 | |
| 2523 | friend class HGraph; |
| 2524 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 2525 | }; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2526 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2527 | class HFloatConstant : public HConstant { |
| 2528 | public: |
| 2529 | float GetValue() const { return value_; } |
| 2530 | |
| 2531 | uint64_t GetValueAsUint64() const OVERRIDE { |
| 2532 | return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_)); |
| 2533 | } |
| 2534 | |
| 2535 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2536 | DCHECK(other->IsFloatConstant()) << other->DebugName(); |
| 2537 | return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64(); |
| 2538 | } |
| 2539 | |
| 2540 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
| 2541 | |
| 2542 | bool IsMinusOne() const OVERRIDE { |
| 2543 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f)); |
| 2544 | } |
| 2545 | bool IsZero() const OVERRIDE { |
Nicolas Geoffray | 949e54d | 2016-03-15 16:23:04 +0000 | [diff] [blame] | 2546 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f); |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2547 | } |
| 2548 | bool IsOne() const OVERRIDE { |
| 2549 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f); |
| 2550 | } |
| 2551 | bool IsNaN() const { |
| 2552 | return std::isnan(value_); |
| 2553 | } |
| 2554 | |
| 2555 | DECLARE_INSTRUCTION(FloatConstant); |
| 2556 | |
| 2557 | private: |
| 2558 | explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc) |
| 2559 | : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {} |
| 2560 | explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc) |
| 2561 | : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {} |
| 2562 | |
| 2563 | const float value_; |
| 2564 | |
| 2565 | // Only the SsaBuilder and HGraph can create floating-point constants. |
| 2566 | friend class SsaBuilder; |
| 2567 | friend class HGraph; |
| 2568 | DISALLOW_COPY_AND_ASSIGN(HFloatConstant); |
| 2569 | }; |
| 2570 | |
| 2571 | class HDoubleConstant : public HConstant { |
| 2572 | public: |
| 2573 | double GetValue() const { return value_; } |
| 2574 | |
| 2575 | uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); } |
| 2576 | |
| 2577 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2578 | DCHECK(other->IsDoubleConstant()) << other->DebugName(); |
| 2579 | return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64(); |
| 2580 | } |
| 2581 | |
| 2582 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
| 2583 | |
| 2584 | bool IsMinusOne() const OVERRIDE { |
| 2585 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0)); |
| 2586 | } |
| 2587 | bool IsZero() const OVERRIDE { |
Nicolas Geoffray | 949e54d | 2016-03-15 16:23:04 +0000 | [diff] [blame] | 2588 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0)); |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2589 | } |
| 2590 | bool IsOne() const OVERRIDE { |
| 2591 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0); |
| 2592 | } |
| 2593 | bool IsNaN() const { |
| 2594 | return std::isnan(value_); |
| 2595 | } |
| 2596 | |
| 2597 | DECLARE_INSTRUCTION(DoubleConstant); |
| 2598 | |
| 2599 | private: |
| 2600 | explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) |
| 2601 | : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {} |
| 2602 | explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc) |
| 2603 | : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {} |
| 2604 | |
| 2605 | const double value_; |
| 2606 | |
| 2607 | // Only the SsaBuilder and HGraph can create floating-point constants. |
| 2608 | friend class SsaBuilder; |
| 2609 | friend class HGraph; |
| 2610 | DISALLOW_COPY_AND_ASSIGN(HDoubleConstant); |
| 2611 | }; |
| 2612 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2613 | // Conditional branch. A block ending with an HIf instruction must have |
| 2614 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2615 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2616 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2617 | explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 2618 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2619 | SetRawInputAt(0, input); |
| 2620 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2621 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 2622 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2623 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2624 | HBasicBlock* IfTrueSuccessor() const { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 2625 | return GetBlock()->GetSuccessors()[0]; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | HBasicBlock* IfFalseSuccessor() const { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 2629 | return GetBlock()->GetSuccessors()[1]; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2632 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 2633 | |
| 2634 | private: |
| 2635 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 2636 | }; |
| 2637 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2638 | |
| 2639 | // Abstract instruction which marks the beginning and/or end of a try block and |
| 2640 | // links it to the respective exception handlers. Behaves the same as a Goto in |
| 2641 | // non-exceptional control flow. |
| 2642 | // Normal-flow successor is stored at index zero, exception handlers under |
| 2643 | // higher indices in no particular order. |
| 2644 | class HTryBoundary : public HTemplateInstruction<0> { |
| 2645 | public: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2646 | enum class BoundaryKind { |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2647 | kEntry, |
| 2648 | kExit, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2649 | kLast = kExit |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 2650 | }; |
| 2651 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2652 | explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc) |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2653 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
| 2654 | SetPackedField<BoundaryKindField>(kind); |
| 2655 | } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2656 | |
| 2657 | bool IsControlFlow() const OVERRIDE { return true; } |
| 2658 | |
| 2659 | // Returns the block's non-exceptional successor (index zero). |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 2660 | HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2661 | |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 2662 | ArrayRef<HBasicBlock* const> GetExceptionHandlers() const { |
| 2663 | return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u); |
| 2664 | } |
| 2665 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2666 | // Returns whether `handler` is among its exception handlers (non-zero index |
| 2667 | // successors). |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2668 | bool HasExceptionHandler(const HBasicBlock& handler) const { |
| 2669 | DCHECK(handler.IsCatchBlock()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 2670 | return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | // If not present already, adds `handler` to its block's list of exception |
| 2674 | // handlers. |
| 2675 | void AddExceptionHandler(HBasicBlock* handler) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2676 | if (!HasExceptionHandler(*handler)) { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2677 | GetBlock()->AddSuccessor(handler); |
| 2678 | } |
| 2679 | } |
| 2680 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2681 | BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); } |
| 2682 | bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2683 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 2684 | bool HasSameExceptionHandlersAs(const HTryBoundary& other) const; |
| 2685 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2686 | DECLARE_INSTRUCTION(TryBoundary); |
| 2687 | |
| 2688 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2689 | static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits; |
| 2690 | static constexpr size_t kFieldBoundaryKindSize = |
| 2691 | MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast)); |
| 2692 | static constexpr size_t kNumberOfTryBoundaryPackedBits = |
| 2693 | kFieldBoundaryKind + kFieldBoundaryKindSize; |
| 2694 | static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits, |
| 2695 | "Too many packed fields."); |
| 2696 | using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 2697 | |
| 2698 | DISALLOW_COPY_AND_ASSIGN(HTryBoundary); |
| 2699 | }; |
| 2700 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2701 | // Deoptimize to interpreter, upon checking a condition. |
| 2702 | class HDeoptimize : public HTemplateInstruction<1> { |
| 2703 | public: |
Nicolas Geoffray | 1cde058 | 2016-01-13 13:56:20 +0000 | [diff] [blame] | 2704 | // We set CanTriggerGC to prevent any intermediate address to be live |
| 2705 | // at the point of the `HDeoptimize`. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 2706 | HDeoptimize(HInstruction* cond, uint32_t dex_pc) |
Nicolas Geoffray | 1cde058 | 2016-01-13 13:56:20 +0000 | [diff] [blame] | 2707 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) { |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2708 | SetRawInputAt(0, cond); |
| 2709 | } |
| 2710 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 2711 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2712 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2713 | return true; |
| 2714 | } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2715 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2716 | bool CanThrow() const OVERRIDE { return true; } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2717 | |
| 2718 | DECLARE_INSTRUCTION(Deoptimize); |
| 2719 | |
| 2720 | private: |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2721 | DISALLOW_COPY_AND_ASSIGN(HDeoptimize); |
| 2722 | }; |
| 2723 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 2724 | // Represents the ArtMethod that was passed as a first argument to |
| 2725 | // the method. It is used by instructions that depend on it, like |
| 2726 | // instructions that work with the dex cache. |
| 2727 | class HCurrentMethod : public HExpression<0> { |
| 2728 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2729 | explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 2730 | : HExpression(type, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 2731 | |
| 2732 | DECLARE_INSTRUCTION(CurrentMethod); |
| 2733 | |
| 2734 | private: |
| 2735 | DISALLOW_COPY_AND_ASSIGN(HCurrentMethod); |
| 2736 | }; |
| 2737 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2738 | // Fetches an ArtMethod from the virtual table or the interface method table |
| 2739 | // of a class. |
| 2740 | class HClassTableGet : public HExpression<1> { |
| 2741 | public: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2742 | enum class TableKind { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2743 | kVTable, |
| 2744 | kIMTable, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2745 | kLast = kIMTable |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2746 | }; |
| 2747 | HClassTableGet(HInstruction* cls, |
| 2748 | Primitive::Type type, |
| 2749 | TableKind kind, |
| 2750 | size_t index, |
| 2751 | uint32_t dex_pc) |
| 2752 | : HExpression(type, SideEffects::None(), dex_pc), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2753 | index_(index) { |
| 2754 | SetPackedField<TableKindField>(kind); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2755 | SetRawInputAt(0, cls); |
| 2756 | } |
| 2757 | |
| 2758 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2759 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2760 | return other->AsClassTableGet()->GetIndex() == index_ && |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2761 | other->AsClassTableGet()->GetPackedFields() == GetPackedFields(); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2764 | TableKind GetTableKind() const { return GetPackedField<TableKindField>(); } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2765 | size_t GetIndex() const { return index_; } |
| 2766 | |
| 2767 | DECLARE_INSTRUCTION(ClassTableGet); |
| 2768 | |
| 2769 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2770 | static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits; |
| 2771 | static constexpr size_t kFieldTableKindSize = |
| 2772 | MinimumBitsToStore(static_cast<size_t>(TableKind::kLast)); |
| 2773 | static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize; |
| 2774 | static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits, |
| 2775 | "Too many packed fields."); |
| 2776 | using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>; |
| 2777 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2778 | // The index of the ArtMethod in the table. |
| 2779 | const size_t index_; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 2780 | |
| 2781 | DISALLOW_COPY_AND_ASSIGN(HClassTableGet); |
| 2782 | }; |
| 2783 | |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 2784 | // PackedSwitch (jump table). A block ending with a PackedSwitch instruction will |
| 2785 | // have one successor for each entry in the switch table, and the final successor |
| 2786 | // will be the block containing the next Dex opcode. |
| 2787 | class HPackedSwitch : public HTemplateInstruction<1> { |
| 2788 | public: |
Mark Mendell | 3b9f304 | 2015-09-24 08:43:40 -0400 | [diff] [blame] | 2789 | HPackedSwitch(int32_t start_value, |
| 2790 | uint32_t num_entries, |
| 2791 | HInstruction* input, |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 2792 | uint32_t dex_pc = kNoDexPc) |
| 2793 | : HTemplateInstruction(SideEffects::None(), dex_pc), |
| 2794 | start_value_(start_value), |
| 2795 | num_entries_(num_entries) { |
| 2796 | SetRawInputAt(0, input); |
| 2797 | } |
| 2798 | |
| 2799 | bool IsControlFlow() const OVERRIDE { return true; } |
| 2800 | |
| 2801 | int32_t GetStartValue() const { return start_value_; } |
| 2802 | |
Vladimir Marko | 211c211 | 2015-09-24 16:52:33 +0100 | [diff] [blame] | 2803 | uint32_t GetNumEntries() const { return num_entries_; } |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 2804 | |
| 2805 | HBasicBlock* GetDefaultBlock() const { |
| 2806 | // Last entry is the default block. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 2807 | return GetBlock()->GetSuccessors()[num_entries_]; |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 2808 | } |
| 2809 | DECLARE_INSTRUCTION(PackedSwitch); |
| 2810 | |
| 2811 | private: |
Mark Mendell | 3b9f304 | 2015-09-24 08:43:40 -0400 | [diff] [blame] | 2812 | const int32_t start_value_; |
| 2813 | const uint32_t num_entries_; |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 2814 | |
| 2815 | DISALLOW_COPY_AND_ASSIGN(HPackedSwitch); |
| 2816 | }; |
| 2817 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2818 | class HUnaryOperation : public HExpression<1> { |
| 2819 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2820 | HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 2821 | : HExpression(result_type, SideEffects::None(), dex_pc) { |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2822 | SetRawInputAt(0, input); |
| 2823 | } |
| 2824 | |
| 2825 | HInstruction* GetInput() const { return InputAt(0); } |
| 2826 | Primitive::Type GetResultType() const { return GetType(); } |
| 2827 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2828 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2829 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2830 | return true; |
| 2831 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2832 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2833 | // Try to statically evaluate `this` and return a HConstant |
| 2834 | // containing the result of this evaluation. If `this` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2835 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2836 | HConstant* TryStaticEvaluation() const; |
| 2837 | |
| 2838 | // Apply this operation to `x`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2839 | virtual HConstant* Evaluate(HIntConstant* x) const = 0; |
| 2840 | virtual HConstant* Evaluate(HLongConstant* x) const = 0; |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2841 | virtual HConstant* Evaluate(HFloatConstant* x) const = 0; |
| 2842 | virtual HConstant* Evaluate(HDoubleConstant* x) const = 0; |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2843 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2844 | DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation); |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2845 | |
| 2846 | private: |
| 2847 | DISALLOW_COPY_AND_ASSIGN(HUnaryOperation); |
| 2848 | }; |
| 2849 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2850 | class HBinaryOperation : public HExpression<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2851 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2852 | HBinaryOperation(Primitive::Type result_type, |
| 2853 | HInstruction* left, |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 2854 | HInstruction* right, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2855 | SideEffects side_effects = SideEffects::None(), |
| 2856 | uint32_t dex_pc = kNoDexPc) |
| 2857 | : HExpression(result_type, side_effects, dex_pc) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2858 | SetRawInputAt(0, left); |
| 2859 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2862 | HInstruction* GetLeft() const { return InputAt(0); } |
| 2863 | HInstruction* GetRight() const { return InputAt(1); } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2864 | Primitive::Type GetResultType() const { return GetType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2865 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2866 | virtual bool IsCommutative() const { return false; } |
| 2867 | |
| 2868 | // Put constant on the right. |
| 2869 | // Returns whether order is changed. |
| 2870 | bool OrderInputsWithConstantOnTheRight() { |
| 2871 | HInstruction* left = InputAt(0); |
| 2872 | HInstruction* right = InputAt(1); |
| 2873 | if (left->IsConstant() && !right->IsConstant()) { |
| 2874 | ReplaceInput(right, 0); |
| 2875 | ReplaceInput(left, 1); |
| 2876 | return true; |
| 2877 | } |
| 2878 | return false; |
| 2879 | } |
| 2880 | |
| 2881 | // Order inputs by instruction id, but favor constant on the right side. |
| 2882 | // This helps GVN for commutative ops. |
| 2883 | void OrderInputs() { |
| 2884 | DCHECK(IsCommutative()); |
| 2885 | HInstruction* left = InputAt(0); |
| 2886 | HInstruction* right = InputAt(1); |
| 2887 | if (left == right || (!left->IsConstant() && right->IsConstant())) { |
| 2888 | return; |
| 2889 | } |
| 2890 | if (OrderInputsWithConstantOnTheRight()) { |
| 2891 | return; |
| 2892 | } |
| 2893 | // Order according to instruction id. |
| 2894 | if (left->GetId() > right->GetId()) { |
| 2895 | ReplaceInput(right, 0); |
| 2896 | ReplaceInput(left, 1); |
| 2897 | } |
| 2898 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2899 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2900 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 2901 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2902 | return true; |
| 2903 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2904 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2905 | // Try to statically evaluate `this` and return a HConstant |
| 2906 | // containing the result of this evaluation. If `this` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2907 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2908 | HConstant* TryStaticEvaluation() const; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2909 | |
| 2910 | // Apply this operation to `x` and `y`. |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2911 | virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED, |
| 2912 | HNullConstant* y ATTRIBUTE_UNUSED) const { |
Roland Levillain | e53bd81 | 2016-02-24 14:54:18 +0000 | [diff] [blame] | 2913 | LOG(FATAL) << DebugName() << " is not defined for the (null, null) case."; |
| 2914 | UNREACHABLE(); |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2915 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2916 | virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0; |
| 2917 | virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0; |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2918 | virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED, |
| 2919 | HIntConstant* y ATTRIBUTE_UNUSED) const { |
Roland Levillain | e53bd81 | 2016-02-24 14:54:18 +0000 | [diff] [blame] | 2920 | LOG(FATAL) << DebugName() << " is not defined for the (long, int) case."; |
| 2921 | UNREACHABLE(); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 2922 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2923 | virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0; |
| 2924 | virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2925 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2926 | // Returns an input that can legally be used as the right input and is |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2927 | // constant, or null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2928 | HConstant* GetConstantRight() const; |
| 2929 | |
| 2930 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2931 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2932 | HInstruction* GetLeastConstantLeft() const; |
| 2933 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2934 | DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 2935 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2936 | private: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2937 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 2938 | }; |
| 2939 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2940 | // The comparison bias applies for floating point operations and indicates how NaN |
| 2941 | // comparisons are treated: |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2942 | enum class ComparisonBias { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2943 | kNoBias, // bias is not applicable (i.e. for long operation) |
| 2944 | kGtBias, // return 1 for NaN comparisons |
| 2945 | kLtBias, // return -1 for NaN comparisons |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2946 | kLast = kLtBias |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2947 | }; |
| 2948 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2949 | std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs); |
| 2950 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2951 | class HCondition : public HBinaryOperation { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2952 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 2953 | HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2954 | : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) { |
| 2955 | SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias); |
| 2956 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2957 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2958 | // For code generation purposes, returns whether this instruction is just before |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2959 | // `instruction`, and disregard moves in between. |
| 2960 | bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2961 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 2962 | DECLARE_ABSTRACT_INSTRUCTION(Condition); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2963 | |
| 2964 | virtual IfCondition GetCondition() const = 0; |
| 2965 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2966 | virtual IfCondition GetOppositeCondition() const = 0; |
| 2967 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2968 | bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; } |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 2969 | bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; } |
| 2970 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2971 | ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); } |
| 2972 | void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2973 | |
| 2974 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 2975 | return GetPackedFields() == other->AsCondition()->GetPackedFields(); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2976 | } |
| 2977 | |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2978 | bool IsFPConditionTrueIfNaN() const { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2979 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType(); |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2980 | IfCondition if_cond = GetCondition(); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 2981 | if (if_cond == kCondNE) { |
| 2982 | return true; |
| 2983 | } else if (if_cond == kCondEQ) { |
| 2984 | return false; |
| 2985 | } |
| 2986 | return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias(); |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | bool IsFPConditionFalseIfNaN() const { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 2990 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType(); |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2991 | IfCondition if_cond = GetCondition(); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 2992 | if (if_cond == kCondEQ) { |
| 2993 | return true; |
| 2994 | } else if (if_cond == kCondNE) { |
| 2995 | return false; |
| 2996 | } |
| 2997 | return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias(); |
Roland Levillain | 4fa13f6 | 2015-07-06 18:11:54 +0100 | [diff] [blame] | 2998 | } |
| 2999 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3000 | protected: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3001 | // Needed if we merge a HCompare into a HCondition. |
| 3002 | static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits; |
| 3003 | static constexpr size_t kFieldComparisonBiasSize = |
| 3004 | MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast)); |
| 3005 | static constexpr size_t kNumberOfConditionPackedBits = |
| 3006 | kFieldComparisonBias + kFieldComparisonBiasSize; |
| 3007 | static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 3008 | using ComparisonBiasField = |
| 3009 | BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>; |
| 3010 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3011 | template <typename T> |
| 3012 | int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); } |
| 3013 | |
| 3014 | template <typename T> |
| 3015 | int32_t CompareFP(T x, T y) const { |
| 3016 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType(); |
| 3017 | DCHECK_NE(GetBias(), ComparisonBias::kNoBias); |
| 3018 | // Handle the bias. |
| 3019 | return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y); |
| 3020 | } |
| 3021 | |
| 3022 | // Return an integer constant containing the result of a condition evaluated at compile time. |
| 3023 | HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const { |
| 3024 | return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc); |
| 3025 | } |
| 3026 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3027 | private: |
| 3028 | DISALLOW_COPY_AND_ASSIGN(HCondition); |
| 3029 | }; |
| 3030 | |
| 3031 | // Instruction to check if two inputs are equal to each other. |
| 3032 | class HEqual : public HCondition { |
| 3033 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3034 | HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3035 | : HCondition(first, second, dex_pc) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 3036 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3037 | bool IsCommutative() const OVERRIDE { return true; } |
| 3038 | |
Vladimir Marko | 9e23df5 | 2015-11-10 17:14:35 +0000 | [diff] [blame] | 3039 | HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED, |
| 3040 | HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3041 | return MakeConstantCondition(true, GetDexPc()); |
| 3042 | } |
| 3043 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3044 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3045 | } |
| 3046 | // In the following Evaluate methods, a HCompare instruction has |
| 3047 | // been merged into this HEqual instruction; evaluate it as |
| 3048 | // `Compare(x, y) == 0`. |
| 3049 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3050 | return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), |
| 3051 | GetDexPc()); |
| 3052 | } |
| 3053 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 3054 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3055 | } |
| 3056 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 3057 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
Vladimir Marko | 9e23df5 | 2015-11-10 17:14:35 +0000 | [diff] [blame] | 3058 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3059 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3060 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3061 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3062 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3063 | return kCondEQ; |
| 3064 | } |
| 3065 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3066 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3067 | return kCondNE; |
| 3068 | } |
| 3069 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3070 | private: |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3071 | template <typename T> bool Compute(T x, T y) const { return x == y; } |
| 3072 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3073 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 3074 | }; |
| 3075 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3076 | class HNotEqual : public HCondition { |
| 3077 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3078 | HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3079 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3080 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3081 | bool IsCommutative() const OVERRIDE { return true; } |
| 3082 | |
Vladimir Marko | 9e23df5 | 2015-11-10 17:14:35 +0000 | [diff] [blame] | 3083 | HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED, |
| 3084 | HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3085 | return MakeConstantCondition(false, GetDexPc()); |
| 3086 | } |
| 3087 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 3088 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3089 | } |
| 3090 | // In the following Evaluate methods, a HCompare instruction has |
| 3091 | // been merged into this HNotEqual instruction; evaluate it as |
| 3092 | // `Compare(x, y) != 0`. |
| 3093 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 3094 | return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3095 | } |
| 3096 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 3097 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3098 | } |
| 3099 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 3100 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
Vladimir Marko | 9e23df5 | 2015-11-10 17:14:35 +0000 | [diff] [blame] | 3101 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3102 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3103 | DECLARE_INSTRUCTION(NotEqual); |
| 3104 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3105 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3106 | return kCondNE; |
| 3107 | } |
| 3108 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3109 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3110 | return kCondEQ; |
| 3111 | } |
| 3112 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3113 | private: |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3114 | template <typename T> bool Compute(T x, T y) const { return x != y; } |
| 3115 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3116 | DISALLOW_COPY_AND_ASSIGN(HNotEqual); |
| 3117 | }; |
| 3118 | |
| 3119 | class HLessThan : public HCondition { |
| 3120 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3121 | HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3122 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3123 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3124 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3125 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3126 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3127 | // In the following Evaluate methods, a HCompare instruction has |
| 3128 | // been merged into this HLessThan instruction; evaluate it as |
| 3129 | // `Compare(x, y) < 0`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3130 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3131 | return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3132 | } |
| 3133 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 3134 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3135 | } |
| 3136 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 3137 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3138 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3139 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3140 | DECLARE_INSTRUCTION(LessThan); |
| 3141 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3142 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3143 | return kCondLT; |
| 3144 | } |
| 3145 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3146 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3147 | return kCondGE; |
| 3148 | } |
| 3149 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3150 | private: |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3151 | template <typename T> bool Compute(T x, T y) const { return x < y; } |
| 3152 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3153 | DISALLOW_COPY_AND_ASSIGN(HLessThan); |
| 3154 | }; |
| 3155 | |
| 3156 | class HLessThanOrEqual : public HCondition { |
| 3157 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3158 | HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3159 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3160 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3161 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3162 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3163 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3164 | // In the following Evaluate methods, a HCompare instruction has |
| 3165 | // been merged into this HLessThanOrEqual instruction; evaluate it as |
| 3166 | // `Compare(x, y) <= 0`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3167 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3168 | return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3169 | } |
| 3170 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 3171 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3172 | } |
| 3173 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 3174 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3175 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3176 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3177 | DECLARE_INSTRUCTION(LessThanOrEqual); |
| 3178 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3179 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3180 | return kCondLE; |
| 3181 | } |
| 3182 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3183 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3184 | return kCondGT; |
| 3185 | } |
| 3186 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3187 | private: |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3188 | template <typename T> bool Compute(T x, T y) const { return x <= y; } |
| 3189 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3190 | DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); |
| 3191 | }; |
| 3192 | |
| 3193 | class HGreaterThan : public HCondition { |
| 3194 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3195 | HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3196 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3197 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3198 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3199 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3200 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3201 | // In the following Evaluate methods, a HCompare instruction has |
| 3202 | // been merged into this HGreaterThan instruction; evaluate it as |
| 3203 | // `Compare(x, y) > 0`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3204 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3205 | return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3206 | } |
| 3207 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 3208 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3209 | } |
| 3210 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 3211 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3212 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3213 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3214 | DECLARE_INSTRUCTION(GreaterThan); |
| 3215 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3216 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3217 | return kCondGT; |
| 3218 | } |
| 3219 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3220 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3221 | return kCondLE; |
| 3222 | } |
| 3223 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3224 | private: |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3225 | template <typename T> bool Compute(T x, T y) const { return x > y; } |
| 3226 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3227 | DISALLOW_COPY_AND_ASSIGN(HGreaterThan); |
| 3228 | }; |
| 3229 | |
| 3230 | class HGreaterThanOrEqual : public HCondition { |
| 3231 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3232 | HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3233 | : HCondition(first, second, dex_pc) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3234 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3235 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3236 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3237 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3238 | // In the following Evaluate methods, a HCompare instruction has |
| 3239 | // been merged into this HGreaterThanOrEqual instruction; evaluate it as |
| 3240 | // `Compare(x, y) >= 0`. |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3241 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3242 | return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3243 | } |
| 3244 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 3245 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
| 3246 | } |
| 3247 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 3248 | return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 3249 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3250 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3251 | DECLARE_INSTRUCTION(GreaterThanOrEqual); |
| 3252 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3253 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3254 | return kCondGE; |
| 3255 | } |
| 3256 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3257 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3258 | return kCondLT; |
| 3259 | } |
| 3260 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3261 | private: |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3262 | template <typename T> bool Compute(T x, T y) const { return x >= y; } |
| 3263 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3264 | DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); |
| 3265 | }; |
| 3266 | |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3267 | class HBelow : public HCondition { |
| 3268 | public: |
| 3269 | HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3270 | : HCondition(first, second, dex_pc) {} |
| 3271 | |
| 3272 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3273 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3274 | } |
| 3275 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3276 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3277 | } |
| 3278 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 3279 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3280 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 3281 | UNREACHABLE(); |
| 3282 | } |
| 3283 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 3284 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3285 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 3286 | UNREACHABLE(); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | DECLARE_INSTRUCTION(Below); |
| 3290 | |
| 3291 | IfCondition GetCondition() const OVERRIDE { |
| 3292 | return kCondB; |
| 3293 | } |
| 3294 | |
| 3295 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3296 | return kCondAE; |
| 3297 | } |
| 3298 | |
| 3299 | private: |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3300 | template <typename T> bool Compute(T x, T y) const { |
| 3301 | return MakeUnsigned(x) < MakeUnsigned(y); |
| 3302 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3303 | |
| 3304 | DISALLOW_COPY_AND_ASSIGN(HBelow); |
| 3305 | }; |
| 3306 | |
| 3307 | class HBelowOrEqual : public HCondition { |
| 3308 | public: |
| 3309 | HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3310 | : HCondition(first, second, dex_pc) {} |
| 3311 | |
| 3312 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3313 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3314 | } |
| 3315 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3316 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3317 | } |
| 3318 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 3319 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3320 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 3321 | UNREACHABLE(); |
| 3322 | } |
| 3323 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 3324 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3325 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 3326 | UNREACHABLE(); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | DECLARE_INSTRUCTION(BelowOrEqual); |
| 3330 | |
| 3331 | IfCondition GetCondition() const OVERRIDE { |
| 3332 | return kCondBE; |
| 3333 | } |
| 3334 | |
| 3335 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3336 | return kCondA; |
| 3337 | } |
| 3338 | |
| 3339 | private: |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3340 | template <typename T> bool Compute(T x, T y) const { |
| 3341 | return MakeUnsigned(x) <= MakeUnsigned(y); |
| 3342 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3343 | |
| 3344 | DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual); |
| 3345 | }; |
| 3346 | |
| 3347 | class HAbove : public HCondition { |
| 3348 | public: |
| 3349 | HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3350 | : HCondition(first, second, dex_pc) {} |
| 3351 | |
| 3352 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3353 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3354 | } |
| 3355 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3356 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3357 | } |
| 3358 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 3359 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3360 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 3361 | UNREACHABLE(); |
| 3362 | } |
| 3363 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 3364 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3365 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 3366 | UNREACHABLE(); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | DECLARE_INSTRUCTION(Above); |
| 3370 | |
| 3371 | IfCondition GetCondition() const OVERRIDE { |
| 3372 | return kCondA; |
| 3373 | } |
| 3374 | |
| 3375 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3376 | return kCondBE; |
| 3377 | } |
| 3378 | |
| 3379 | private: |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3380 | template <typename T> bool Compute(T x, T y) const { |
| 3381 | return MakeUnsigned(x) > MakeUnsigned(y); |
| 3382 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3383 | |
| 3384 | DISALLOW_COPY_AND_ASSIGN(HAbove); |
| 3385 | }; |
| 3386 | |
| 3387 | class HAboveOrEqual : public HCondition { |
| 3388 | public: |
| 3389 | HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc) |
| 3390 | : HCondition(first, second, dex_pc) {} |
| 3391 | |
| 3392 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3393 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3394 | } |
| 3395 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3396 | return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3397 | } |
| 3398 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 3399 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3400 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 3401 | UNREACHABLE(); |
| 3402 | } |
| 3403 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 3404 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3405 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 3406 | UNREACHABLE(); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | DECLARE_INSTRUCTION(AboveOrEqual); |
| 3410 | |
| 3411 | IfCondition GetCondition() const OVERRIDE { |
| 3412 | return kCondAE; |
| 3413 | } |
| 3414 | |
| 3415 | IfCondition GetOppositeCondition() const OVERRIDE { |
| 3416 | return kCondB; |
| 3417 | } |
| 3418 | |
| 3419 | private: |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3420 | template <typename T> bool Compute(T x, T y) const { |
| 3421 | return MakeUnsigned(x) >= MakeUnsigned(y); |
| 3422 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 3423 | |
| 3424 | DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual); |
| 3425 | }; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3426 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 3427 | // Instruction to check how two inputs compare to each other. |
| 3428 | // Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. |
| 3429 | class HCompare : public HBinaryOperation { |
| 3430 | public: |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 3431 | // Note that `comparison_type` is the type of comparison performed |
| 3432 | // between the comparison's inputs, not the type of the instantiated |
| 3433 | // HCompare instruction (which is always Primitive::kPrimInt). |
| 3434 | HCompare(Primitive::Type comparison_type, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 3435 | HInstruction* first, |
| 3436 | HInstruction* second, |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3437 | ComparisonBias bias, |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 3438 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3439 | : HBinaryOperation(Primitive::kPrimInt, |
| 3440 | first, |
| 3441 | second, |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 3442 | SideEffectsForArchRuntimeCalls(comparison_type), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3443 | dex_pc) { |
| 3444 | SetPackedField<ComparisonBiasField>(bias); |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 3445 | if (kIsDebugBuild) { |
| 3446 | DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType())); |
| 3447 | DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType())); |
| 3448 | } |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 3449 | } |
| 3450 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3451 | template <typename T> |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3452 | int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); } |
| 3453 | |
| 3454 | template <typename T> |
| 3455 | int32_t ComputeFP(T x, T y) const { |
| 3456 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType(); |
| 3457 | DCHECK_NE(GetBias(), ComparisonBias::kNoBias); |
| 3458 | // Handle the bias. |
| 3459 | return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y); |
| 3460 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 3461 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3462 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3463 | // Note that there is no "cmp-int" Dex instruction so we shouldn't |
| 3464 | // reach this code path when processing a freshly built HIR |
| 3465 | // graph. However HCompare integer instructions can be synthesized |
| 3466 | // by the instruction simplifier to implement IntegerCompare and |
| 3467 | // IntegerSignum intrinsics, so we have to handle this case. |
| 3468 | return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 3469 | } |
| 3470 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3471 | return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3472 | } |
| 3473 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 3474 | return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc()); |
| 3475 | } |
| 3476 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 3477 | return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 3478 | } |
| 3479 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 3480 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3481 | return GetPackedFields() == other->AsCompare()->GetPackedFields(); |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3484 | ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 3485 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3486 | // Does this compare instruction have a "gt bias" (vs an "lt bias")? |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3487 | // Only meaningful for floating-point comparisons. |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3488 | bool IsGtBias() const { |
| 3489 | DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType(); |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3490 | return GetBias() == ComparisonBias::kGtBias; |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3491 | } |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3492 | |
Roland Levillain | 1693a1f | 2016-03-15 14:57:31 +0000 | [diff] [blame] | 3493 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) { |
| 3494 | // Comparisons do not require a runtime call in any back end. |
| 3495 | return SideEffects::None(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3496 | } |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 3497 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 3498 | DECLARE_INSTRUCTION(Compare); |
| 3499 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3500 | protected: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3501 | static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits; |
| 3502 | static constexpr size_t kFieldComparisonBiasSize = |
| 3503 | MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast)); |
| 3504 | static constexpr size_t kNumberOfComparePackedBits = |
| 3505 | kFieldComparisonBias + kFieldComparisonBiasSize; |
| 3506 | static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 3507 | using ComparisonBiasField = |
| 3508 | BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>; |
| 3509 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 3510 | // Return an integer constant containing the result of a comparison evaluated at compile time. |
| 3511 | HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const { |
| 3512 | DCHECK(value == -1 || value == 0 || value == 1) << value; |
| 3513 | return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc); |
| 3514 | } |
| 3515 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 3516 | private: |
| 3517 | DISALLOW_COPY_AND_ASSIGN(HCompare); |
| 3518 | }; |
| 3519 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3520 | // A local in the graph. Corresponds to a Dex register. |
| 3521 | class HLocal : public HTemplateInstruction<0> { |
| 3522 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3523 | explicit HLocal(uint16_t reg_number) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3524 | : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3525 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3526 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3527 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 3528 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 3529 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3530 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 3531 | // The Dex register number. |
| 3532 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3533 | |
| 3534 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 3535 | }; |
| 3536 | |
| 3537 | // Load a given local. The local is an input of this instruction. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3538 | class HLoadLocal : public HExpression<1> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3539 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3540 | HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc) |
| 3541 | : HExpression(type, SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3542 | SetRawInputAt(0, local); |
| 3543 | } |
| 3544 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 3545 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 3546 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3547 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3548 | |
| 3549 | private: |
| 3550 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 3551 | }; |
| 3552 | |
| 3553 | // Store a value in a given local. This instruction has two inputs: the value |
| 3554 | // and the local. |
| 3555 | class HStoreLocal : public HTemplateInstruction<2> { |
| 3556 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3557 | HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc) |
| 3558 | : HTemplateInstruction(SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3559 | SetRawInputAt(0, local); |
| 3560 | SetRawInputAt(1, value); |
| 3561 | } |
| 3562 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 3563 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 3564 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3565 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 3566 | |
| 3567 | private: |
| 3568 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 3569 | }; |
| 3570 | |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3571 | class HNewInstance : public HExpression<2> { |
| 3572 | public: |
| 3573 | HNewInstance(HInstruction* cls, |
| 3574 | HCurrentMethod* current_method, |
| 3575 | uint32_t dex_pc, |
| 3576 | uint16_t type_index, |
| 3577 | const DexFile& dex_file, |
| 3578 | bool can_throw, |
| 3579 | bool finalizable, |
| 3580 | QuickEntrypointEnum entrypoint) |
| 3581 | : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc), |
| 3582 | type_index_(type_index), |
| 3583 | dex_file_(dex_file), |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3584 | entrypoint_(entrypoint) { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3585 | SetPackedFlag<kFlagCanThrow>(can_throw); |
| 3586 | SetPackedFlag<kFlagFinalizable>(finalizable); |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3587 | SetRawInputAt(0, cls); |
| 3588 | SetRawInputAt(1, current_method); |
| 3589 | } |
| 3590 | |
| 3591 | uint16_t GetTypeIndex() const { return type_index_; } |
| 3592 | const DexFile& GetDexFile() const { return dex_file_; } |
| 3593 | |
| 3594 | // Calls runtime so needs an environment. |
| 3595 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3596 | |
| 3597 | // It may throw when called on type that's not instantiable/accessible. |
| 3598 | // It can throw OOME. |
| 3599 | // TODO: distinguish between the two cases so we can for example allow allocation elimination. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3600 | bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; } |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3601 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3602 | bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); } |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3603 | |
| 3604 | bool CanBeNull() const OVERRIDE { return false; } |
| 3605 | |
| 3606 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 3607 | |
| 3608 | void SetEntrypoint(QuickEntrypointEnum entrypoint) { |
| 3609 | entrypoint_ = entrypoint; |
| 3610 | } |
| 3611 | |
| 3612 | bool IsStringAlloc() const; |
| 3613 | |
| 3614 | DECLARE_INSTRUCTION(NewInstance); |
| 3615 | |
| 3616 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3617 | static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits; |
| 3618 | static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1; |
| 3619 | static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1; |
| 3620 | static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits, |
| 3621 | "Too many packed fields."); |
| 3622 | |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3623 | const uint16_t type_index_; |
| 3624 | const DexFile& dex_file_; |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3625 | QuickEntrypointEnum entrypoint_; |
| 3626 | |
| 3627 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 3628 | }; |
| 3629 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3630 | enum class Intrinsics { |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 3631 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \ |
| 3632 | k ## Name, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3633 | #include "intrinsics_list.h" |
| 3634 | kNone, |
| 3635 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 3636 | #undef INTRINSICS_LIST |
| 3637 | #undef OPTIMIZING_INTRINSICS |
| 3638 | }; |
| 3639 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic); |
| 3640 | |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 3641 | enum IntrinsicNeedsEnvironmentOrCache { |
| 3642 | kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache. |
| 3643 | kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache. |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 3644 | }; |
| 3645 | |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 3646 | enum IntrinsicSideEffects { |
| 3647 | kNoSideEffects, // Intrinsic does not have any heap memory side effects. |
| 3648 | kReadSideEffects, // Intrinsic may read heap memory. |
| 3649 | kWriteSideEffects, // Intrinsic may write heap memory. |
| 3650 | kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC. |
| 3651 | }; |
| 3652 | |
| 3653 | enum IntrinsicExceptions { |
| 3654 | kNoThrow, // Intrinsic does not throw any exceptions. |
| 3655 | kCanThrow // Intrinsic may throw exceptions. |
| 3656 | }; |
| 3657 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3658 | class HInvoke : public HInstruction { |
| 3659 | public: |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 3660 | size_t InputCount() const OVERRIDE { return inputs_.size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3661 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 3662 | bool NeedsEnvironment() const OVERRIDE; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3663 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 3664 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3665 | SetRawInputAt(index, argument); |
| 3666 | } |
| 3667 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3668 | // Return the number of arguments. This number can be lower than |
| 3669 | // the number of inputs returned by InputCount(), as some invoke |
| 3670 | // instructions (e.g. HInvokeStaticOrDirect) can have non-argument |
| 3671 | // inputs at the end of their list of inputs. |
| 3672 | uint32_t GetNumberOfArguments() const { return number_of_arguments_; } |
| 3673 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3674 | Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 3675 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3676 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3677 | const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3678 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3679 | InvokeType GetOriginalInvokeType() const { |
| 3680 | return GetPackedField<OriginalInvokeTypeField>(); |
| 3681 | } |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3682 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 3683 | Intrinsics GetIntrinsic() const { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3684 | return intrinsic_; |
| 3685 | } |
| 3686 | |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 3687 | void SetIntrinsic(Intrinsics intrinsic, |
| 3688 | IntrinsicNeedsEnvironmentOrCache needs_env_or_cache, |
| 3689 | IntrinsicSideEffects side_effects, |
| 3690 | IntrinsicExceptions exceptions); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3691 | |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 3692 | bool IsFromInlinedInvoke() const { |
Nicolas Geoffray | 8e1ef53 | 2015-11-23 12:04:37 +0000 | [diff] [blame] | 3693 | return GetEnvironment()->IsFromInlinedInvoke(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 3694 | } |
| 3695 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3696 | bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); } |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 3697 | |
| 3698 | bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); } |
| 3699 | |
| 3700 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3701 | return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_; |
| 3702 | } |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 3703 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 3704 | uint32_t* GetIntrinsicOptimizations() { |
| 3705 | return &intrinsic_optimizations_; |
| 3706 | } |
| 3707 | |
| 3708 | const uint32_t* GetIntrinsicOptimizations() const { |
| 3709 | return &intrinsic_optimizations_; |
| 3710 | } |
| 3711 | |
| 3712 | bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; } |
| 3713 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 3714 | DECLARE_ABSTRACT_INSTRUCTION(Invoke); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 3715 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3716 | protected: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3717 | static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits; |
| 3718 | static constexpr size_t kFieldOriginalInvokeTypeSize = |
| 3719 | MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType)); |
| 3720 | static constexpr size_t kFieldReturnType = |
| 3721 | kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize; |
| 3722 | static constexpr size_t kFieldReturnTypeSize = |
| 3723 | MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast)); |
| 3724 | static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize; |
| 3725 | static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1; |
| 3726 | static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 3727 | using OriginalInvokeTypeField = |
| 3728 | BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>; |
| 3729 | using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>; |
| 3730 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3731 | HInvoke(ArenaAllocator* arena, |
| 3732 | uint32_t number_of_arguments, |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3733 | uint32_t number_of_other_inputs, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3734 | Primitive::Type return_type, |
| 3735 | uint32_t dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3736 | uint32_t dex_method_index, |
| 3737 | InvokeType original_invoke_type) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 3738 | : HInstruction( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 3739 | SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays. |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3740 | number_of_arguments_(number_of_arguments), |
Vladimir Marko | b7d8e8c | 2015-09-17 15:47:05 +0100 | [diff] [blame] | 3741 | inputs_(number_of_arguments + number_of_other_inputs, |
| 3742 | arena->Adapter(kArenaAllocInvokeInputs)), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3743 | dex_method_index_(dex_method_index), |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 3744 | intrinsic_(Intrinsics::kNone), |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 3745 | intrinsic_optimizations_(0) { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3746 | SetPackedField<ReturnTypeField>(return_type); |
| 3747 | SetPackedField<OriginalInvokeTypeField>(original_invoke_type); |
| 3748 | SetPackedFlag<kFlagCanThrow>(true); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3749 | } |
| 3750 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 3751 | const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 3752 | return inputs_[index]; |
| 3753 | } |
| 3754 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3755 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 3756 | inputs_[index] = input; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3759 | void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); } |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 3760 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3761 | uint32_t number_of_arguments_; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 3762 | ArenaVector<HUserRecord<HInstruction*>> inputs_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 3763 | const uint32_t dex_method_index_; |
| 3764 | Intrinsics intrinsic_; |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 3765 | |
| 3766 | // A magic word holding optimizations for intrinsics. See intrinsics.h. |
| 3767 | uint32_t intrinsic_optimizations_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3768 | |
| 3769 | private: |
| 3770 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 3771 | }; |
| 3772 | |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 3773 | class HInvokeUnresolved : public HInvoke { |
| 3774 | public: |
| 3775 | HInvokeUnresolved(ArenaAllocator* arena, |
| 3776 | uint32_t number_of_arguments, |
| 3777 | Primitive::Type return_type, |
| 3778 | uint32_t dex_pc, |
| 3779 | uint32_t dex_method_index, |
| 3780 | InvokeType invoke_type) |
| 3781 | : HInvoke(arena, |
| 3782 | number_of_arguments, |
| 3783 | 0u /* number_of_other_inputs */, |
| 3784 | return_type, |
| 3785 | dex_pc, |
| 3786 | dex_method_index, |
| 3787 | invoke_type) { |
| 3788 | } |
| 3789 | |
| 3790 | DECLARE_INSTRUCTION(InvokeUnresolved); |
| 3791 | |
| 3792 | private: |
| 3793 | DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved); |
| 3794 | }; |
| 3795 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3796 | class HInvokeStaticOrDirect : public HInvoke { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3797 | public: |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3798 | // Requirements of this method call regarding the class |
| 3799 | // initialization (clinit) check of its declaring class. |
| 3800 | enum class ClinitCheckRequirement { |
| 3801 | kNone, // Class already initialized. |
| 3802 | kExplicit, // Static call having explicit clinit check as last input. |
| 3803 | kImplicit, // Static call implicitly requiring a clinit check. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3804 | kLast = kImplicit |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3805 | }; |
| 3806 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3807 | // Determines how to load the target ArtMethod*. |
| 3808 | enum class MethodLoadKind { |
| 3809 | // Use a String init ArtMethod* loaded from Thread entrypoints. |
| 3810 | kStringInit, |
| 3811 | |
| 3812 | // Use the method's own ArtMethod* loaded by the register allocator. |
| 3813 | kRecursive, |
| 3814 | |
| 3815 | // Use ArtMethod* at a known address, embed the direct address in the code. |
| 3816 | // Used for app->boot calls with non-relocatable image and for JIT-compiled calls. |
| 3817 | kDirectAddress, |
| 3818 | |
| 3819 | // Use ArtMethod* at an address that will be known at link time, embed the direct |
| 3820 | // address in the code. If the image is relocatable, emit .patch_oat entry. |
| 3821 | // Used for app->boot calls with relocatable image and boot->boot calls, whether |
| 3822 | // the image relocatable or not. |
| 3823 | kDirectAddressWithFixup, |
| 3824 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3825 | // Load from resolved methods array in the dex cache using a PC-relative load. |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3826 | // Used when we need to use the dex cache, for example for invoke-static that |
| 3827 | // may cause class initialization (the entry may point to a resolution method), |
| 3828 | // and we know that we can access the dex cache arrays using a PC-relative load. |
| 3829 | kDexCachePcRelative, |
| 3830 | |
| 3831 | // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*. |
| 3832 | // Used for JIT when we need to use the dex cache. This is also the last-resort-kind |
| 3833 | // used when other kinds are unavailable (say, dex cache arrays are not PC-relative) |
| 3834 | // or unimplemented or impractical (i.e. slow) on a particular architecture. |
| 3835 | kDexCacheViaMethod, |
| 3836 | }; |
| 3837 | |
| 3838 | // Determines the location of the code pointer. |
| 3839 | enum class CodePtrLocation { |
| 3840 | // Recursive call, use local PC-relative call instruction. |
| 3841 | kCallSelf, |
| 3842 | |
| 3843 | // Use PC-relative call instruction patched at link time. |
| 3844 | // Used for calls within an oat file, boot->boot or app->app. |
| 3845 | kCallPCRelative, |
| 3846 | |
| 3847 | // Call to a known target address, embed the direct address in code. |
| 3848 | // Used for app->boot call with non-relocatable image and for JIT-compiled calls. |
| 3849 | kCallDirect, |
| 3850 | |
| 3851 | // Call to a target address that will be known at link time, embed the direct |
| 3852 | // address in code. If the image is relocatable, emit .patch_oat entry. |
| 3853 | // Used for app->boot calls with relocatable image and boot->boot calls, whether |
| 3854 | // the image relocatable or not. |
| 3855 | kCallDirectWithFixup, |
| 3856 | |
| 3857 | // Use code pointer from the ArtMethod*. |
| 3858 | // Used when we don't know the target code. This is also the last-resort-kind used when |
| 3859 | // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture. |
| 3860 | kCallArtMethod, |
| 3861 | }; |
| 3862 | |
| 3863 | struct DispatchInfo { |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 3864 | MethodLoadKind method_load_kind; |
| 3865 | CodePtrLocation code_ptr_location; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3866 | // The method load data holds |
| 3867 | // - thread entrypoint offset for kStringInit method if this is a string init invoke. |
| 3868 | // Note that there are multiple string init methods, each having its own offset. |
| 3869 | // - the method address for kDirectAddress |
| 3870 | // - the dex cache arrays offset for kDexCachePcRel. |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 3871 | uint64_t method_load_data; |
| 3872 | uint64_t direct_code_ptr; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3873 | }; |
| 3874 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 3875 | HInvokeStaticOrDirect(ArenaAllocator* arena, |
| 3876 | uint32_t number_of_arguments, |
| 3877 | Primitive::Type return_type, |
| 3878 | uint32_t dex_pc, |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3879 | uint32_t method_index, |
| 3880 | MethodReference target_method, |
| 3881 | DispatchInfo dispatch_info, |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 3882 | InvokeType original_invoke_type, |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 3883 | InvokeType optimized_invoke_type, |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 3884 | ClinitCheckRequirement clinit_check_requirement) |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3885 | : HInvoke(arena, |
| 3886 | number_of_arguments, |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 3887 | // There is potentially one extra argument for the HCurrentMethod node, and |
| 3888 | // potentially one other if the clinit check is explicit, and potentially |
| 3889 | // one other if the method is a string factory. |
| 3890 | (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) + |
| 3891 | (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) + |
| 3892 | (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u), |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 3893 | return_type, |
| 3894 | dex_pc, |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3895 | method_index, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 3896 | original_invoke_type), |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3897 | target_method_(target_method), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3898 | dispatch_info_(dispatch_info) { |
| 3899 | SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type); |
| 3900 | SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement); |
| 3901 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3902 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 3903 | void SetDispatchInfo(const DispatchInfo& dispatch_info) { |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 3904 | bool had_current_method_input = HasCurrentMethodInput(); |
| 3905 | bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind); |
| 3906 | |
| 3907 | // Using the current method is the default and once we find a better |
| 3908 | // method load kind, we should not go back to using the current method. |
| 3909 | DCHECK(had_current_method_input || !needs_current_method_input); |
| 3910 | |
| 3911 | if (had_current_method_input && !needs_current_method_input) { |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 3912 | DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod()); |
| 3913 | RemoveInputAt(GetSpecialInputIndex()); |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 3914 | } |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 3915 | dispatch_info_ = dispatch_info; |
| 3916 | } |
| 3917 | |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 3918 | void AddSpecialInput(HInstruction* input) { |
| 3919 | // We allow only one special input. |
| 3920 | DCHECK(!IsStringInit() && !HasCurrentMethodInput()); |
| 3921 | DCHECK(InputCount() == GetSpecialInputIndex() || |
| 3922 | (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck())); |
| 3923 | InsertInputAt(GetSpecialInputIndex(), input); |
| 3924 | } |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 3925 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 3926 | bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3927 | // We access the method via the dex cache so we can't do an implicit null check. |
| 3928 | // TODO: for intrinsics we can generate implicit null checks. |
| 3929 | return false; |
| 3930 | } |
| 3931 | |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 3932 | bool CanBeNull() const OVERRIDE { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3933 | return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit(); |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 3934 | } |
| 3935 | |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 3936 | // Get the index of the special input, if any. |
| 3937 | // |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 3938 | // If the invoke HasCurrentMethodInput(), the "special input" is the current |
| 3939 | // method pointer; otherwise there may be one platform-specific special input, |
| 3940 | // such as PC-relative addressing base. |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 3941 | uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); } |
Nicolas Geoffray | 9779307 | 2016-02-16 15:33:54 +0000 | [diff] [blame] | 3942 | bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); } |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 3943 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3944 | InvokeType GetOptimizedInvokeType() const { |
| 3945 | return GetPackedField<OptimizedInvokeTypeField>(); |
| 3946 | } |
| 3947 | |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 3948 | void SetOptimizedInvokeType(InvokeType invoke_type) { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3949 | SetPackedField<OptimizedInvokeTypeField>(invoke_type); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3952 | MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; } |
| 3953 | CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; } |
| 3954 | bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; } |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 3955 | bool NeedsDexCacheOfDeclaringClass() const OVERRIDE; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3956 | bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3957 | bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; } |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 3958 | bool HasPcRelativeDexCache() const { |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 3959 | return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; |
| 3960 | } |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 3961 | bool HasCurrentMethodInput() const { |
| 3962 | // This function can be called only after the invoke has been fully initialized by the builder. |
| 3963 | if (NeedsCurrentMethodInput(GetMethodLoadKind())) { |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 3964 | DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod()); |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 3965 | return true; |
| 3966 | } else { |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 3967 | DCHECK(InputCount() == GetSpecialInputIndex() || |
| 3968 | !InputAt(GetSpecialInputIndex())->IsCurrentMethod()); |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 3969 | return false; |
| 3970 | } |
| 3971 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3972 | bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; } |
| 3973 | MethodReference GetTargetMethod() const { return target_method_; } |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 3974 | void SetTargetMethod(MethodReference method) { target_method_ = method; } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3975 | |
| 3976 | int32_t GetStringInitOffset() const { |
| 3977 | DCHECK(IsStringInit()); |
| 3978 | return dispatch_info_.method_load_data; |
| 3979 | } |
| 3980 | |
| 3981 | uint64_t GetMethodAddress() const { |
| 3982 | DCHECK(HasMethodAddress()); |
| 3983 | return dispatch_info_.method_load_data; |
| 3984 | } |
| 3985 | |
| 3986 | uint32_t GetDexCacheArrayOffset() const { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 3987 | DCHECK(HasPcRelativeDexCache()); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 3988 | return dispatch_info_.method_load_data; |
| 3989 | } |
| 3990 | |
| 3991 | uint64_t GetDirectCodePtr() const { |
| 3992 | DCHECK(HasDirectCodePtr()); |
| 3993 | return dispatch_info_.direct_code_ptr; |
| 3994 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 3995 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 3996 | ClinitCheckRequirement GetClinitCheckRequirement() const { |
| 3997 | return GetPackedField<ClinitCheckRequirementField>(); |
| 3998 | } |
Calin Juravle | 68ad649 | 2015-08-18 17:08:12 +0100 | [diff] [blame] | 3999 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4000 | // Is this instruction a call to a static method? |
| 4001 | bool IsStatic() const { |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 4002 | return GetOriginalInvokeType() == kStatic; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4003 | } |
| 4004 | |
Vladimir Marko | fbb184a | 2015-11-13 14:47:00 +0000 | [diff] [blame] | 4005 | // Remove the HClinitCheck or the replacement HLoadClass (set as last input by |
| 4006 | // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck) |
| 4007 | // instruction; only relevant for static calls with explicit clinit check. |
| 4008 | void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4009 | DCHECK(IsStaticWithExplicitClinitCheck()); |
| 4010 | size_t last_input_index = InputCount() - 1; |
| 4011 | HInstruction* last_input = InputAt(last_input_index); |
| 4012 | DCHECK(last_input != nullptr); |
Vladimir Marko | fbb184a | 2015-11-13 14:47:00 +0000 | [diff] [blame] | 4013 | DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName(); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4014 | RemoveAsUserOfInput(last_input_index); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 4015 | inputs_.pop_back(); |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4016 | SetPackedField<ClinitCheckRequirementField>(new_requirement); |
Vladimir Marko | fbb184a | 2015-11-13 14:47:00 +0000 | [diff] [blame] | 4017 | DCHECK(!IsStaticWithExplicitClinitCheck()); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4018 | } |
| 4019 | |
David Brazdil | bc9ab16 | 2016-01-20 14:50:19 +0000 | [diff] [blame] | 4020 | HInstruction* GetAndRemoveThisArgumentOfStringInit() { |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 4021 | DCHECK(IsStringInit()); |
| 4022 | size_t index = InputCount() - 1; |
David Brazdil | bc9ab16 | 2016-01-20 14:50:19 +0000 | [diff] [blame] | 4023 | HInstruction* input = InputAt(index); |
David Brazdil | 6de1938 | 2016-01-08 17:37:10 +0000 | [diff] [blame] | 4024 | RemoveAsUserOfInput(index); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 4025 | inputs_.pop_back(); |
David Brazdil | bc9ab16 | 2016-01-20 14:50:19 +0000 | [diff] [blame] | 4026 | return input; |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 4027 | } |
| 4028 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4029 | // Is this a call to a static method whose declaring class has an |
Vladimir Marko | fbb184a | 2015-11-13 14:47:00 +0000 | [diff] [blame] | 4030 | // explicit initialization check in the graph? |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4031 | bool IsStaticWithExplicitClinitCheck() const { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4032 | return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | // Is this a call to a static method whose declaring class has an |
| 4036 | // implicit intialization check requirement? |
| 4037 | bool IsStaticWithImplicitClinitCheck() const { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4038 | return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4039 | } |
| 4040 | |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 4041 | // Does this method load kind need the current method as an input? |
| 4042 | static bool NeedsCurrentMethodInput(MethodLoadKind kind) { |
| 4043 | return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod; |
| 4044 | } |
| 4045 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 4046 | DECLARE_INSTRUCTION(InvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 4047 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 4048 | protected: |
| 4049 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 4050 | const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i); |
| 4051 | if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) { |
| 4052 | HInstruction* input = input_record.GetInstruction(); |
| 4053 | // `input` is the last input of a static invoke marked as having |
| 4054 | // an explicit clinit check. It must either be: |
| 4055 | // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or |
| 4056 | // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation. |
| 4057 | DCHECK(input != nullptr); |
| 4058 | DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName(); |
| 4059 | } |
| 4060 | return input_record; |
| 4061 | } |
| 4062 | |
Vladimir Marko | c53c079 | 2015-11-19 15:48:33 +0000 | [diff] [blame] | 4063 | void InsertInputAt(size_t index, HInstruction* input); |
| 4064 | void RemoveInputAt(size_t index); |
| 4065 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 4066 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4067 | static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits; |
| 4068 | static constexpr size_t kFieldOptimizedInvokeTypeSize = |
| 4069 | MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType)); |
| 4070 | static constexpr size_t kFieldClinitCheckRequirement = |
| 4071 | kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize; |
| 4072 | static constexpr size_t kFieldClinitCheckRequirementSize = |
| 4073 | MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast)); |
| 4074 | static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits = |
| 4075 | kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize; |
| 4076 | static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits, |
| 4077 | "Too many packed fields."); |
| 4078 | using OptimizedInvokeTypeField = |
| 4079 | BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>; |
| 4080 | using ClinitCheckRequirementField = BitField<ClinitCheckRequirement, |
| 4081 | kFieldClinitCheckRequirement, |
| 4082 | kFieldClinitCheckRequirementSize>; |
| 4083 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 4084 | // The target method may refer to different dex file or method index than the original |
| 4085 | // invoke. This happens for sharpened calls and for calls where a method was redeclared |
| 4086 | // in derived class to increase visibility. |
| 4087 | MethodReference target_method_; |
| 4088 | DispatchInfo dispatch_info_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 4089 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 4090 | DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 4091 | }; |
Vladimir Marko | f64242a | 2015-12-01 14:58:23 +0000 | [diff] [blame] | 4092 | std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs); |
Vladimir Marko | fbb184a | 2015-11-13 14:47:00 +0000 | [diff] [blame] | 4093 | std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 4094 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 4095 | class HInvokeVirtual : public HInvoke { |
| 4096 | public: |
| 4097 | HInvokeVirtual(ArenaAllocator* arena, |
| 4098 | uint32_t number_of_arguments, |
| 4099 | Primitive::Type return_type, |
| 4100 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 4101 | uint32_t dex_method_index, |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 4102 | uint32_t vtable_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 4103 | : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual), |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 4104 | vtable_index_(vtable_index) {} |
| 4105 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4106 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4107 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4108 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 4111 | uint32_t GetVTableIndex() const { return vtable_index_; } |
| 4112 | |
| 4113 | DECLARE_INSTRUCTION(InvokeVirtual); |
| 4114 | |
| 4115 | private: |
| 4116 | const uint32_t vtable_index_; |
| 4117 | |
| 4118 | DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); |
| 4119 | }; |
| 4120 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 4121 | class HInvokeInterface : public HInvoke { |
| 4122 | public: |
| 4123 | HInvokeInterface(ArenaAllocator* arena, |
| 4124 | uint32_t number_of_arguments, |
| 4125 | Primitive::Type return_type, |
| 4126 | uint32_t dex_pc, |
| 4127 | uint32_t dex_method_index, |
| 4128 | uint32_t imt_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 4129 | : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface), |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 4130 | imt_index_(imt_index) {} |
| 4131 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4132 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4133 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 4134 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 4137 | uint32_t GetImtIndex() const { return imt_index_; } |
| 4138 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 4139 | |
| 4140 | DECLARE_INSTRUCTION(InvokeInterface); |
| 4141 | |
| 4142 | private: |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 4143 | const uint32_t imt_index_; |
| 4144 | |
| 4145 | DISALLOW_COPY_AND_ASSIGN(HInvokeInterface); |
| 4146 | }; |
| 4147 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 4148 | class HNeg : public HUnaryOperation { |
| 4149 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4150 | HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 4151 | : HUnaryOperation(result_type, input, dex_pc) {} |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 4152 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4153 | template <typename T> T Compute(T x) const { return -x; } |
| 4154 | |
| 4155 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4156 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4157 | } |
| 4158 | HConstant* Evaluate(HLongConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4159 | return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4160 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4161 | HConstant* Evaluate(HFloatConstant* x) const OVERRIDE { |
| 4162 | return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc()); |
| 4163 | } |
| 4164 | HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE { |
| 4165 | return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc()); |
| 4166 | } |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 4167 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 4168 | DECLARE_INSTRUCTION(Neg); |
| 4169 | |
| 4170 | private: |
| 4171 | DISALLOW_COPY_AND_ASSIGN(HNeg); |
| 4172 | }; |
| 4173 | |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 4174 | class HNewArray : public HExpression<2> { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4175 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 4176 | HNewArray(HInstruction* length, |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 4177 | HCurrentMethod* current_method, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 4178 | uint32_t dex_pc, |
| 4179 | uint16_t type_index, |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 4180 | const DexFile& dex_file, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 4181 | QuickEntrypointEnum entrypoint) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4182 | : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 4183 | type_index_(type_index), |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 4184 | dex_file_(dex_file), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 4185 | entrypoint_(entrypoint) { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4186 | SetRawInputAt(0, length); |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 4187 | SetRawInputAt(1, current_method); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4188 | } |
| 4189 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4190 | uint16_t GetTypeIndex() const { return type_index_; } |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 4191 | const DexFile& GetDexFile() const { return dex_file_; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4192 | |
| 4193 | // Calls runtime so needs an environment. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4194 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 4195 | |
Mingyao Yang | 0c365e6 | 2015-03-31 15:09:29 -0700 | [diff] [blame] | 4196 | // May throw NegativeArraySizeException, OutOfMemoryError, etc. |
| 4197 | bool CanThrow() const OVERRIDE { return true; } |
| 4198 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4199 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4200 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 4201 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 4202 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4203 | DECLARE_INSTRUCTION(NewArray); |
| 4204 | |
| 4205 | private: |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4206 | const uint16_t type_index_; |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 4207 | const DexFile& dex_file_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 4208 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 4209 | |
| 4210 | DISALLOW_COPY_AND_ASSIGN(HNewArray); |
| 4211 | }; |
| 4212 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 4213 | class HAdd : public HBinaryOperation { |
| 4214 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4215 | HAdd(Primitive::Type result_type, |
| 4216 | HInstruction* left, |
| 4217 | HInstruction* right, |
| 4218 | uint32_t dex_pc = kNoDexPc) |
| 4219 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 4220 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4221 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 4222 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4223 | template <typename T> T Compute(T x, T y) const { return x + y; } |
| 4224 | |
| 4225 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4226 | return GetBlock()->GetGraph()->GetIntConstant( |
| 4227 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 4228 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4229 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4230 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4231 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 4232 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4233 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 4234 | return GetBlock()->GetGraph()->GetFloatConstant( |
| 4235 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4236 | } |
| 4237 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 4238 | return GetBlock()->GetGraph()->GetDoubleConstant( |
| 4239 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4240 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 4241 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 4242 | DECLARE_INSTRUCTION(Add); |
| 4243 | |
| 4244 | private: |
| 4245 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 4246 | }; |
| 4247 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4248 | class HSub : public HBinaryOperation { |
| 4249 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4250 | HSub(Primitive::Type result_type, |
| 4251 | HInstruction* left, |
| 4252 | HInstruction* right, |
| 4253 | uint32_t dex_pc = kNoDexPc) |
| 4254 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4255 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4256 | template <typename T> T Compute(T x, T y) const { return x - y; } |
| 4257 | |
| 4258 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4259 | return GetBlock()->GetGraph()->GetIntConstant( |
| 4260 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 4261 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4262 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4263 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4264 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 4265 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4266 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 4267 | return GetBlock()->GetGraph()->GetFloatConstant( |
| 4268 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4269 | } |
| 4270 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 4271 | return GetBlock()->GetGraph()->GetDoubleConstant( |
| 4272 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4273 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 4274 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4275 | DECLARE_INSTRUCTION(Sub); |
| 4276 | |
| 4277 | private: |
| 4278 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 4279 | }; |
| 4280 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 4281 | class HMul : public HBinaryOperation { |
| 4282 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4283 | HMul(Primitive::Type result_type, |
| 4284 | HInstruction* left, |
| 4285 | HInstruction* right, |
| 4286 | uint32_t dex_pc = kNoDexPc) |
| 4287 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 4288 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4289 | bool IsCommutative() const OVERRIDE { return true; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 4290 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4291 | template <typename T> T Compute(T x, T y) const { return x * y; } |
| 4292 | |
| 4293 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4294 | return GetBlock()->GetGraph()->GetIntConstant( |
| 4295 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4296 | } |
| 4297 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4298 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4299 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4300 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4301 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 4302 | return GetBlock()->GetGraph()->GetFloatConstant( |
| 4303 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4304 | } |
| 4305 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 4306 | return GetBlock()->GetGraph()->GetDoubleConstant( |
| 4307 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4308 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 4309 | |
| 4310 | DECLARE_INSTRUCTION(Mul); |
| 4311 | |
| 4312 | private: |
| 4313 | DISALLOW_COPY_AND_ASSIGN(HMul); |
| 4314 | }; |
| 4315 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 4316 | class HDiv : public HBinaryOperation { |
| 4317 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4318 | HDiv(Primitive::Type result_type, |
| 4319 | HInstruction* left, |
| 4320 | HInstruction* right, |
| 4321 | uint32_t dex_pc) |
| 4322 | : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {} |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 4323 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4324 | template <typename T> |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4325 | T ComputeIntegral(T x, T y) const { |
| 4326 | DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType(); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4327 | // Our graph structure ensures we never have 0 for `y` during |
| 4328 | // constant folding. |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 4329 | DCHECK_NE(y, 0); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4330 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 4331 | return (y == -1) ? -x : x / y; |
| 4332 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4333 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4334 | template <typename T> |
| 4335 | T ComputeFP(T x, T y) const { |
| 4336 | DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType(); |
| 4337 | return x / y; |
| 4338 | } |
| 4339 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4340 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4341 | return GetBlock()->GetGraph()->GetIntConstant( |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4342 | ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4343 | } |
| 4344 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4345 | return GetBlock()->GetGraph()->GetLongConstant( |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4346 | ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4347 | } |
| 4348 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 4349 | return GetBlock()->GetGraph()->GetFloatConstant( |
| 4350 | ComputeFP(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4351 | } |
| 4352 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 4353 | return GetBlock()->GetGraph()->GetDoubleConstant( |
| 4354 | ComputeFP(x->GetValue(), y->GetValue()), GetDexPc()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4355 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 4356 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4357 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 4358 | // The generated code can use a runtime call. |
| 4359 | return SideEffects::CanTriggerGC(); |
| 4360 | } |
| 4361 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 4362 | DECLARE_INSTRUCTION(Div); |
| 4363 | |
| 4364 | private: |
| 4365 | DISALLOW_COPY_AND_ASSIGN(HDiv); |
| 4366 | }; |
| 4367 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4368 | class HRem : public HBinaryOperation { |
| 4369 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4370 | HRem(Primitive::Type result_type, |
| 4371 | HInstruction* left, |
| 4372 | HInstruction* right, |
| 4373 | uint32_t dex_pc) |
| 4374 | : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {} |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4375 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4376 | template <typename T> |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4377 | T ComputeIntegral(T x, T y) const { |
| 4378 | DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType(); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4379 | // Our graph structure ensures we never have 0 for `y` during |
| 4380 | // constant folding. |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4381 | DCHECK_NE(y, 0); |
| 4382 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 4383 | return (y == -1) ? 0 : x % y; |
| 4384 | } |
| 4385 | |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4386 | template <typename T> |
| 4387 | T ComputeFP(T x, T y) const { |
| 4388 | DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType(); |
| 4389 | return std::fmod(x, y); |
| 4390 | } |
| 4391 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4392 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4393 | return GetBlock()->GetGraph()->GetIntConstant( |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4394 | ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4395 | } |
| 4396 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4397 | return GetBlock()->GetGraph()->GetLongConstant( |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4398 | ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc()); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4399 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4400 | HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE { |
| 4401 | return GetBlock()->GetGraph()->GetFloatConstant( |
| 4402 | ComputeFP(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4403 | } |
| 4404 | HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE { |
| 4405 | return GetBlock()->GetGraph()->GetDoubleConstant( |
| 4406 | ComputeFP(x->GetValue(), y->GetValue()), GetDexPc()); |
| 4407 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4408 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4409 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 4410 | return SideEffects::CanTriggerGC(); |
| 4411 | } |
| 4412 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4413 | DECLARE_INSTRUCTION(Rem); |
| 4414 | |
| 4415 | private: |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 4416 | DISALLOW_COPY_AND_ASSIGN(HRem); |
| 4417 | }; |
| 4418 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 4419 | class HDivZeroCheck : public HExpression<1> { |
| 4420 | public: |
Alexandre Rames | 780aece | 2016-01-13 14:34:39 +0000 | [diff] [blame] | 4421 | // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException` |
| 4422 | // constructor. |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 4423 | HDivZeroCheck(HInstruction* value, uint32_t dex_pc) |
Alexandre Rames | 780aece | 2016-01-13 14:34:39 +0000 | [diff] [blame] | 4424 | : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 4425 | SetRawInputAt(0, value); |
| 4426 | } |
| 4427 | |
Serguei Katkov | 8c0676c | 2015-08-03 13:55:33 +0600 | [diff] [blame] | 4428 | Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); } |
| 4429 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 4430 | bool CanBeMoved() const OVERRIDE { return true; } |
| 4431 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 4432 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 4433 | return true; |
| 4434 | } |
| 4435 | |
| 4436 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 4437 | bool CanThrow() const OVERRIDE { return true; } |
| 4438 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 4439 | DECLARE_INSTRUCTION(DivZeroCheck); |
| 4440 | |
| 4441 | private: |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 4442 | DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck); |
| 4443 | }; |
| 4444 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4445 | class HShl : public HBinaryOperation { |
| 4446 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4447 | HShl(Primitive::Type result_type, |
| 4448 | HInstruction* left, |
| 4449 | HInstruction* right, |
| 4450 | uint32_t dex_pc = kNoDexPc) |
| 4451 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4452 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4453 | template <typename T, typename U, typename V> |
| 4454 | T Compute(T x, U y, V max_shift_value) const { |
| 4455 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 4456 | "V is not the unsigned integer type corresponding to T"); |
| 4457 | return x << (y & max_shift_value); |
| 4458 | } |
| 4459 | |
| 4460 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 4461 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4462 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4463 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4464 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 4465 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4466 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4467 | } |
| 4468 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 4469 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4470 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4471 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4472 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 4473 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4474 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4475 | UNREACHABLE(); |
| 4476 | } |
| 4477 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 4478 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4479 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4480 | UNREACHABLE(); |
| 4481 | } |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4482 | |
| 4483 | DECLARE_INSTRUCTION(Shl); |
| 4484 | |
| 4485 | private: |
| 4486 | DISALLOW_COPY_AND_ASSIGN(HShl); |
| 4487 | }; |
| 4488 | |
| 4489 | class HShr : public HBinaryOperation { |
| 4490 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4491 | HShr(Primitive::Type result_type, |
| 4492 | HInstruction* left, |
| 4493 | HInstruction* right, |
| 4494 | uint32_t dex_pc = kNoDexPc) |
| 4495 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4496 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4497 | template <typename T, typename U, typename V> |
| 4498 | T Compute(T x, U y, V max_shift_value) const { |
| 4499 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 4500 | "V is not the unsigned integer type corresponding to T"); |
| 4501 | return x >> (y & max_shift_value); |
| 4502 | } |
| 4503 | |
| 4504 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 4505 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4506 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4507 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4508 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 4509 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4510 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4511 | } |
| 4512 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 4513 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4514 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4515 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4516 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 4517 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4518 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4519 | UNREACHABLE(); |
| 4520 | } |
| 4521 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 4522 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4523 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4524 | UNREACHABLE(); |
| 4525 | } |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4526 | |
| 4527 | DECLARE_INSTRUCTION(Shr); |
| 4528 | |
| 4529 | private: |
| 4530 | DISALLOW_COPY_AND_ASSIGN(HShr); |
| 4531 | }; |
| 4532 | |
| 4533 | class HUShr : public HBinaryOperation { |
| 4534 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4535 | HUShr(Primitive::Type result_type, |
| 4536 | HInstruction* left, |
| 4537 | HInstruction* right, |
| 4538 | uint32_t dex_pc = kNoDexPc) |
| 4539 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4540 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4541 | template <typename T, typename U, typename V> |
| 4542 | T Compute(T x, U y, V max_shift_value) const { |
| 4543 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 4544 | "V is not the unsigned integer type corresponding to T"); |
| 4545 | V ux = static_cast<V>(x); |
| 4546 | return static_cast<T>(ux >> (y & max_shift_value)); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4549 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 4550 | return GetBlock()->GetGraph()->GetIntConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4551 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4552 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4553 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 4554 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4555 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4556 | } |
| 4557 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 4558 | return GetBlock()->GetGraph()->GetLongConstant( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4559 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4560 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4561 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 4562 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4563 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4564 | UNREACHABLE(); |
| 4565 | } |
| 4566 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 4567 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4568 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4569 | UNREACHABLE(); |
| 4570 | } |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 4571 | |
| 4572 | DECLARE_INSTRUCTION(UShr); |
| 4573 | |
| 4574 | private: |
| 4575 | DISALLOW_COPY_AND_ASSIGN(HUShr); |
| 4576 | }; |
| 4577 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4578 | class HAnd : public HBinaryOperation { |
| 4579 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4580 | HAnd(Primitive::Type result_type, |
| 4581 | HInstruction* left, |
| 4582 | HInstruction* right, |
| 4583 | uint32_t dex_pc = kNoDexPc) |
| 4584 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4585 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 4586 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4587 | |
Roland Levillain | e53bd81 | 2016-02-24 14:54:18 +0000 | [diff] [blame] | 4588 | template <typename T> T Compute(T x, T y) const { return x & y; } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4589 | |
| 4590 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4591 | return GetBlock()->GetGraph()->GetIntConstant( |
| 4592 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4593 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4594 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4595 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4596 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4597 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4598 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 4599 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4600 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4601 | UNREACHABLE(); |
| 4602 | } |
| 4603 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 4604 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4605 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4606 | UNREACHABLE(); |
| 4607 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4608 | |
| 4609 | DECLARE_INSTRUCTION(And); |
| 4610 | |
| 4611 | private: |
| 4612 | DISALLOW_COPY_AND_ASSIGN(HAnd); |
| 4613 | }; |
| 4614 | |
| 4615 | class HOr : public HBinaryOperation { |
| 4616 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4617 | HOr(Primitive::Type result_type, |
| 4618 | HInstruction* left, |
| 4619 | HInstruction* right, |
| 4620 | uint32_t dex_pc = kNoDexPc) |
| 4621 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4622 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 4623 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4624 | |
Roland Levillain | e53bd81 | 2016-02-24 14:54:18 +0000 | [diff] [blame] | 4625 | template <typename T> T Compute(T x, T y) const { return x | y; } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4626 | |
| 4627 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4628 | return GetBlock()->GetGraph()->GetIntConstant( |
| 4629 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4630 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4631 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4632 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4633 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4634 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4635 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 4636 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4637 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4638 | UNREACHABLE(); |
| 4639 | } |
| 4640 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 4641 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4642 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4643 | UNREACHABLE(); |
| 4644 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4645 | |
| 4646 | DECLARE_INSTRUCTION(Or); |
| 4647 | |
| 4648 | private: |
| 4649 | DISALLOW_COPY_AND_ASSIGN(HOr); |
| 4650 | }; |
| 4651 | |
| 4652 | class HXor : public HBinaryOperation { |
| 4653 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4654 | HXor(Primitive::Type result_type, |
| 4655 | HInstruction* left, |
| 4656 | HInstruction* right, |
| 4657 | uint32_t dex_pc = kNoDexPc) |
| 4658 | : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4659 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 4660 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4661 | |
Roland Levillain | e53bd81 | 2016-02-24 14:54:18 +0000 | [diff] [blame] | 4662 | template <typename T> T Compute(T x, T y) const { return x ^ y; } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4663 | |
| 4664 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4665 | return GetBlock()->GetGraph()->GetIntConstant( |
| 4666 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4667 | } |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4668 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4669 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4670 | Compute(x->GetValue(), y->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4671 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4672 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 4673 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4674 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4675 | UNREACHABLE(); |
| 4676 | } |
| 4677 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 4678 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4679 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4680 | UNREACHABLE(); |
| 4681 | } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 4682 | |
| 4683 | DECLARE_INSTRUCTION(Xor); |
| 4684 | |
| 4685 | private: |
| 4686 | DISALLOW_COPY_AND_ASSIGN(HXor); |
| 4687 | }; |
| 4688 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 4689 | class HRor : public HBinaryOperation { |
| 4690 | public: |
| 4691 | HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance) |
Roland Levillain | 22c4922 | 2016-03-18 14:04:28 +0000 | [diff] [blame] | 4692 | : HBinaryOperation(result_type, value, distance) { |
| 4693 | if (kIsDebugBuild) { |
| 4694 | DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType())); |
| 4695 | DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType())); |
| 4696 | } |
| 4697 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 4698 | |
| 4699 | template <typename T, typename U, typename V> |
| 4700 | T Compute(T x, U y, V max_shift_value) const { |
| 4701 | static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value, |
| 4702 | "V is not the unsigned integer type corresponding to T"); |
| 4703 | V ux = static_cast<V>(x); |
| 4704 | if ((y & max_shift_value) == 0) { |
| 4705 | return static_cast<T>(ux); |
| 4706 | } else { |
| 4707 | const V reg_bits = sizeof(T) * 8; |
| 4708 | return static_cast<T>(ux >> (y & max_shift_value)) | |
| 4709 | (x << (reg_bits - (y & max_shift_value))); |
| 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE { |
| 4714 | return GetBlock()->GetGraph()->GetIntConstant( |
| 4715 | Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc()); |
| 4716 | } |
| 4717 | HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE { |
| 4718 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4719 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
| 4720 | } |
| 4721 | HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE { |
| 4722 | return GetBlock()->GetGraph()->GetLongConstant( |
| 4723 | Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc()); |
| 4724 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4725 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED, |
| 4726 | HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4727 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4728 | UNREACHABLE(); |
| 4729 | } |
| 4730 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED, |
| 4731 | HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4732 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4733 | UNREACHABLE(); |
| 4734 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 4735 | |
| 4736 | DECLARE_INSTRUCTION(Ror); |
| 4737 | |
| 4738 | private: |
| 4739 | DISALLOW_COPY_AND_ASSIGN(HRor); |
| 4740 | }; |
| 4741 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4742 | // The value of a parameter in this method. Its location depends on |
| 4743 | // the calling convention. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 4744 | class HParameterValue : public HExpression<0> { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4745 | public: |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 4746 | HParameterValue(const DexFile& dex_file, |
| 4747 | uint16_t type_index, |
| 4748 | uint8_t index, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4749 | Primitive::Type parameter_type, |
| 4750 | bool is_this = false) |
| 4751 | : HExpression(parameter_type, SideEffects::None(), kNoDexPc), |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 4752 | dex_file_(dex_file), |
| 4753 | type_index_(type_index), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4754 | index_(index) { |
| 4755 | SetPackedFlag<kFlagIsThis>(is_this); |
| 4756 | SetPackedFlag<kFlagCanBeNull>(!is_this); |
| 4757 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4758 | |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 4759 | const DexFile& GetDexFile() const { return dex_file_; } |
| 4760 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4761 | uint8_t GetIndex() const { return index_; } |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4762 | bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4763 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4764 | bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); } |
| 4765 | void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4766 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4767 | DECLARE_INSTRUCTION(ParameterValue); |
| 4768 | |
| 4769 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4770 | // Whether or not the parameter value corresponds to 'this' argument. |
| 4771 | static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits; |
| 4772 | static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1; |
| 4773 | static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1; |
| 4774 | static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits, |
| 4775 | "Too many packed fields."); |
| 4776 | |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 4777 | const DexFile& dex_file_; |
| 4778 | const uint16_t type_index_; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4779 | // The index of this parameter in the parameters list. Must be less |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4780 | // than HGraph::number_of_in_vregs_. |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 4781 | const uint8_t index_; |
| 4782 | |
| 4783 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 4784 | }; |
| 4785 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4786 | class HNot : public HUnaryOperation { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 4787 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4788 | HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 4789 | : HUnaryOperation(result_type, input, dex_pc) {} |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 4790 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4791 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 4792 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4793 | return true; |
| 4794 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4795 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4796 | template <typename T> T Compute(T x) const { return ~x; } |
| 4797 | |
| 4798 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4799 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4800 | } |
| 4801 | HConstant* Evaluate(HLongConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4802 | return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4803 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4804 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4805 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4806 | UNREACHABLE(); |
| 4807 | } |
| 4808 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4809 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4810 | UNREACHABLE(); |
| 4811 | } |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 4812 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 4813 | DECLARE_INSTRUCTION(Not); |
| 4814 | |
| 4815 | private: |
| 4816 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 4817 | }; |
| 4818 | |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4819 | class HBooleanNot : public HUnaryOperation { |
| 4820 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4821 | explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc) |
| 4822 | : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {} |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4823 | |
| 4824 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 4825 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4826 | return true; |
| 4827 | } |
| 4828 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4829 | template <typename T> bool Compute(T x) const { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4830 | DCHECK(IsUint<1>(x)) << x; |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4831 | return !x; |
| 4832 | } |
| 4833 | |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4834 | HConstant* Evaluate(HIntConstant* x) const OVERRIDE { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4835 | return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc()); |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 4836 | } |
| 4837 | HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4838 | LOG(FATAL) << DebugName() << " is not defined for long values"; |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4839 | UNREACHABLE(); |
| 4840 | } |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 4841 | HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4842 | LOG(FATAL) << DebugName() << " is not defined for float values"; |
| 4843 | UNREACHABLE(); |
| 4844 | } |
| 4845 | HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 4846 | LOG(FATAL) << DebugName() << " is not defined for double values"; |
| 4847 | UNREACHABLE(); |
| 4848 | } |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 4849 | |
| 4850 | DECLARE_INSTRUCTION(BooleanNot); |
| 4851 | |
| 4852 | private: |
| 4853 | DISALLOW_COPY_AND_ASSIGN(HBooleanNot); |
| 4854 | }; |
| 4855 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 4856 | class HTypeConversion : public HExpression<1> { |
| 4857 | public: |
| 4858 | // Instantiate a type conversion of `input` to `result_type`. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 4859 | HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4860 | : HExpression(result_type, |
| 4861 | SideEffectsForArchRuntimeCalls(input->GetType(), result_type), |
| 4862 | dex_pc) { |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 4863 | SetRawInputAt(0, input); |
| 4864 | DCHECK_NE(input->GetType(), result_type); |
| 4865 | } |
| 4866 | |
| 4867 | HInstruction* GetInput() const { return InputAt(0); } |
| 4868 | Primitive::Type GetInputType() const { return GetInput()->GetType(); } |
| 4869 | Primitive::Type GetResultType() const { return GetType(); } |
| 4870 | |
| 4871 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | ed9b195 | 2014-11-06 11:10:17 +0000 | [diff] [blame] | 4872 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 4873 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 4874 | // Try to statically evaluate the conversion and return a HConstant |
| 4875 | // containing the result. If the input cannot be converted, return nullptr. |
| 4876 | HConstant* TryStaticEvaluation() const; |
| 4877 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4878 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type, |
| 4879 | Primitive::Type result_type) { |
| 4880 | // Some architectures may not require the 'GC' side effects, but at this point |
| 4881 | // in the compilation process we do not know what architecture we will |
| 4882 | // generate code for, so we must be conservative. |
Roland Levillain | df3f822 | 2015-08-13 12:31:44 +0100 | [diff] [blame] | 4883 | if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type)) |
| 4884 | || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) { |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 4885 | return SideEffects::CanTriggerGC(); |
| 4886 | } |
| 4887 | return SideEffects::None(); |
| 4888 | } |
| 4889 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 4890 | DECLARE_INSTRUCTION(TypeConversion); |
| 4891 | |
| 4892 | private: |
| 4893 | DISALLOW_COPY_AND_ASSIGN(HTypeConversion); |
| 4894 | }; |
| 4895 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 4896 | static constexpr uint32_t kNoRegNumber = -1; |
| 4897 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4898 | class HPhi : public HInstruction { |
| 4899 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 4900 | HPhi(ArenaAllocator* arena, |
| 4901 | uint32_t reg_number, |
| 4902 | size_t number_of_inputs, |
| 4903 | Primitive::Type type, |
| 4904 | uint32_t dex_pc = kNoDexPc) |
| 4905 | : HInstruction(SideEffects::None(), dex_pc), |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 4906 | inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4907 | reg_number_(reg_number) { |
| 4908 | SetPackedField<TypeField>(ToPhiType(type)); |
| 4909 | DCHECK_NE(GetType(), Primitive::kPrimVoid); |
| 4910 | // Phis are constructed live and marked dead if conflicting or unused. |
| 4911 | // Individual steps of SsaBuilder should assume that if a phi has been |
| 4912 | // marked dead, it can be ignored and will be removed by SsaPhiElimination. |
| 4913 | SetPackedFlag<kFlagIsLive>(true); |
| 4914 | SetPackedFlag<kFlagCanBeNull>(true); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4915 | } |
| 4916 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 4917 | // Returns a type equivalent to the given `type`, but that a `HPhi` can hold. |
| 4918 | static Primitive::Type ToPhiType(Primitive::Type type) { |
| 4919 | switch (type) { |
| 4920 | case Primitive::kPrimBoolean: |
| 4921 | case Primitive::kPrimByte: |
| 4922 | case Primitive::kPrimShort: |
| 4923 | case Primitive::kPrimChar: |
| 4924 | return Primitive::kPrimInt; |
| 4925 | default: |
| 4926 | return type; |
| 4927 | } |
| 4928 | } |
| 4929 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 4930 | bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); } |
| 4931 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 4932 | size_t InputCount() const OVERRIDE { return inputs_.size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4933 | |
| 4934 | void AddInput(HInstruction* input); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 4935 | void RemoveInputAt(size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4936 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4937 | Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); } |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 4938 | void SetType(Primitive::Type new_type) { |
| 4939 | // Make sure that only valid type changes occur. The following are allowed: |
| 4940 | // (1) int -> float/ref (primitive type propagation), |
| 4941 | // (2) long -> double (primitive type propagation). |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4942 | DCHECK(GetType() == new_type || |
| 4943 | (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) || |
| 4944 | (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) || |
| 4945 | (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble)); |
| 4946 | SetPackedField<TypeField>(new_type); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 4947 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4948 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4949 | bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); } |
| 4950 | void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 4951 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4952 | uint32_t GetRegNumber() const { return reg_number_; } |
| 4953 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4954 | void SetDead() { SetPackedFlag<kFlagIsLive>(false); } |
| 4955 | void SetLive() { SetPackedFlag<kFlagIsLive>(true); } |
| 4956 | bool IsDead() const { return !IsLive(); } |
| 4957 | bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 4958 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 4959 | bool IsVRegEquivalentOf(HInstruction* other) const { |
| 4960 | return other != nullptr |
| 4961 | && other->IsPhi() |
| 4962 | && other->AsPhi()->GetBlock() == GetBlock() |
| 4963 | && other->AsPhi()->GetRegNumber() == GetRegNumber(); |
| 4964 | } |
| 4965 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 4966 | // Returns the next equivalent phi (starting from the current one) or null if there is none. |
| 4967 | // An equivalent phi is a phi having the same dex register and type. |
| 4968 | // It assumes that phis with the same dex register are adjacent. |
| 4969 | HPhi* GetNextEquivalentPhiWithSameType() { |
| 4970 | HInstruction* next = GetNext(); |
| 4971 | while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) { |
| 4972 | if (next->GetType() == GetType()) { |
| 4973 | return next->AsPhi(); |
| 4974 | } |
| 4975 | next = next->GetNext(); |
| 4976 | } |
| 4977 | return nullptr; |
| 4978 | } |
| 4979 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4980 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 4981 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 4982 | protected: |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 4983 | const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 4984 | return inputs_[index]; |
| 4985 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 4986 | |
| 4987 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 4988 | inputs_[index] = input; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 4991 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 4992 | static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits; |
| 4993 | static constexpr size_t kFieldTypeSize = |
| 4994 | MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast)); |
| 4995 | static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize; |
| 4996 | static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1; |
| 4997 | static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1; |
| 4998 | static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 4999 | using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>; |
| 5000 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 5001 | ArenaVector<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 5002 | const uint32_t reg_number_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 5003 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 5004 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 5005 | }; |
| 5006 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5007 | class HNullCheck : public HExpression<1> { |
| 5008 | public: |
Nicolas Geoffray | 1af564e | 2016-01-13 12:09:39 +0000 | [diff] [blame] | 5009 | // `HNullCheck` can trigger GC, as it may call the `NullPointerException` |
| 5010 | // constructor. |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5011 | HNullCheck(HInstruction* value, uint32_t dex_pc) |
Nicolas Geoffray | 1af564e | 2016-01-13 12:09:39 +0000 | [diff] [blame] | 5012 | : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5013 | SetRawInputAt(0, value); |
| 5014 | } |
| 5015 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 5016 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5017 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 5018 | return true; |
| 5019 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 5020 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 5021 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5022 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 5023 | bool CanThrow() const OVERRIDE { return true; } |
| 5024 | |
| 5025 | bool CanBeNull() const OVERRIDE { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 5026 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5027 | |
| 5028 | DECLARE_INSTRUCTION(NullCheck); |
| 5029 | |
| 5030 | private: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5031 | DISALLOW_COPY_AND_ASSIGN(HNullCheck); |
| 5032 | }; |
| 5033 | |
| 5034 | class FieldInfo : public ValueObject { |
| 5035 | public: |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5036 | FieldInfo(MemberOffset field_offset, |
| 5037 | Primitive::Type field_type, |
| 5038 | bool is_volatile, |
| 5039 | uint32_t index, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5040 | uint16_t declaring_class_def_index, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5041 | const DexFile& dex_file, |
| 5042 | Handle<mirror::DexCache> dex_cache) |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5043 | : field_offset_(field_offset), |
| 5044 | field_type_(field_type), |
| 5045 | is_volatile_(is_volatile), |
| 5046 | index_(index), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5047 | declaring_class_def_index_(declaring_class_def_index), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5048 | dex_file_(dex_file), |
| 5049 | dex_cache_(dex_cache) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5050 | |
| 5051 | MemberOffset GetFieldOffset() const { return field_offset_; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 5052 | Primitive::Type GetFieldType() const { return field_type_; } |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5053 | uint32_t GetFieldIndex() const { return index_; } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5054 | uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;} |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5055 | const DexFile& GetDexFile() const { return dex_file_; } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5056 | bool IsVolatile() const { return is_volatile_; } |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5057 | Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5058 | |
| 5059 | private: |
| 5060 | const MemberOffset field_offset_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 5061 | const Primitive::Type field_type_; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5062 | const bool is_volatile_; |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5063 | const uint32_t index_; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5064 | const uint16_t declaring_class_def_index_; |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5065 | const DexFile& dex_file_; |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5066 | const Handle<mirror::DexCache> dex_cache_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5067 | }; |
| 5068 | |
| 5069 | class HInstanceFieldGet : public HExpression<1> { |
| 5070 | public: |
| 5071 | HInstanceFieldGet(HInstruction* value, |
| 5072 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5073 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5074 | bool is_volatile, |
| 5075 | uint32_t field_idx, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5076 | uint16_t declaring_class_def_index, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5077 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5078 | Handle<mirror::DexCache> dex_cache, |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 5079 | uint32_t dex_pc) |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5080 | : HExpression(field_type, |
| 5081 | SideEffects::FieldReadOfType(field_type, is_volatile), |
| 5082 | dex_pc), |
| 5083 | field_info_(field_offset, |
| 5084 | field_type, |
| 5085 | is_volatile, |
| 5086 | field_idx, |
| 5087 | declaring_class_def_index, |
| 5088 | dex_file, |
| 5089 | dex_cache) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5090 | SetRawInputAt(0, value); |
| 5091 | } |
| 5092 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 5093 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5094 | |
| 5095 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 5096 | HInstanceFieldGet* other_get = other->AsInstanceFieldGet(); |
| 5097 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 5098 | } |
| 5099 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 5100 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 5101 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | size_t ComputeHashCode() const OVERRIDE { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 5105 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 5106 | } |
| 5107 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5108 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5109 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 5110 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5111 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5112 | |
| 5113 | DECLARE_INSTRUCTION(InstanceFieldGet); |
| 5114 | |
| 5115 | private: |
| 5116 | const FieldInfo field_info_; |
| 5117 | |
| 5118 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); |
| 5119 | }; |
| 5120 | |
| 5121 | class HInstanceFieldSet : public HTemplateInstruction<2> { |
| 5122 | public: |
| 5123 | HInstanceFieldSet(HInstruction* object, |
| 5124 | HInstruction* value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 5125 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5126 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5127 | bool is_volatile, |
| 5128 | uint32_t field_idx, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5129 | uint16_t declaring_class_def_index, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5130 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5131 | Handle<mirror::DexCache> dex_cache, |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 5132 | uint32_t dex_pc) |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5133 | : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), |
| 5134 | dex_pc), |
| 5135 | field_info_(field_offset, |
| 5136 | field_type, |
| 5137 | is_volatile, |
| 5138 | field_idx, |
| 5139 | declaring_class_def_index, |
| 5140 | dex_file, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5141 | dex_cache) { |
| 5142 | SetPackedFlag<kFlagValueCanBeNull>(true); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5143 | SetRawInputAt(0, object); |
| 5144 | SetRawInputAt(1, value); |
| 5145 | } |
| 5146 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 5147 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 5148 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5151 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5152 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 5153 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5154 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5155 | HInstruction* GetValue() const { return InputAt(1); } |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5156 | bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); } |
| 5157 | void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5158 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5159 | DECLARE_INSTRUCTION(InstanceFieldSet); |
| 5160 | |
| 5161 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5162 | static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits; |
| 5163 | static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1; |
| 5164 | static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits, |
| 5165 | "Too many packed fields."); |
| 5166 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 5167 | const FieldInfo field_info_; |
| 5168 | |
| 5169 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); |
| 5170 | }; |
| 5171 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5172 | class HArrayGet : public HExpression<2> { |
| 5173 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5174 | HArrayGet(HInstruction* array, |
| 5175 | HInstruction* index, |
| 5176 | Primitive::Type type, |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 5177 | uint32_t dex_pc, |
| 5178 | SideEffects additional_side_effects = SideEffects::None()) |
| 5179 | : HExpression(type, |
| 5180 | SideEffects::ArrayReadOfType(type).Union(additional_side_effects), |
David Brazdil | 2bd4c5c | 2015-11-04 22:48:45 +0000 | [diff] [blame] | 5181 | dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5182 | SetRawInputAt(0, array); |
| 5183 | SetRawInputAt(1, index); |
| 5184 | } |
| 5185 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5186 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5187 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 5188 | return true; |
| 5189 | } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5190 | bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5191 | // TODO: We can be smarter here. |
| 5192 | // Currently, the array access is always preceded by an ArrayLength or a NullCheck |
| 5193 | // which generates the implicit null check. There are cases when these can be removed |
| 5194 | // to produce better code. If we ever add optimizations to do so we should allow an |
| 5195 | // implicit check here (as long as the address falls in the first page). |
| 5196 | return false; |
| 5197 | } |
| 5198 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 5199 | bool IsEquivalentOf(HArrayGet* other) const { |
| 5200 | bool result = (GetDexPc() == other->GetDexPc()); |
| 5201 | if (kIsDebugBuild && result) { |
| 5202 | DCHECK_EQ(GetBlock(), other->GetBlock()); |
| 5203 | DCHECK_EQ(GetArray(), other->GetArray()); |
| 5204 | DCHECK_EQ(GetIndex(), other->GetIndex()); |
| 5205 | if (Primitive::IsIntOrLongType(GetType())) { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 5206 | DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 5207 | } else { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 5208 | DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType(); |
| 5209 | DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 5210 | } |
| 5211 | } |
| 5212 | return result; |
| 5213 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 5214 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5215 | HInstruction* GetArray() const { return InputAt(0); } |
| 5216 | HInstruction* GetIndex() const { return InputAt(1); } |
| 5217 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5218 | DECLARE_INSTRUCTION(ArrayGet); |
| 5219 | |
| 5220 | private: |
| 5221 | DISALLOW_COPY_AND_ASSIGN(HArrayGet); |
| 5222 | }; |
| 5223 | |
| 5224 | class HArraySet : public HTemplateInstruction<3> { |
| 5225 | public: |
| 5226 | HArraySet(HInstruction* array, |
| 5227 | HInstruction* index, |
| 5228 | HInstruction* value, |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5229 | Primitive::Type expected_component_type, |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 5230 | uint32_t dex_pc, |
| 5231 | SideEffects additional_side_effects = SideEffects::None()) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 5232 | : HTemplateInstruction( |
| 5233 | SideEffects::ArrayWriteOfType(expected_component_type).Union( |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 5234 | SideEffectsForArchRuntimeCalls(value->GetType())).Union( |
| 5235 | additional_side_effects), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5236 | dex_pc) { |
| 5237 | SetPackedField<ExpectedComponentTypeField>(expected_component_type); |
| 5238 | SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot); |
| 5239 | SetPackedFlag<kFlagValueCanBeNull>(true); |
| 5240 | SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5241 | SetRawInputAt(0, array); |
| 5242 | SetRawInputAt(1, index); |
| 5243 | SetRawInputAt(2, value); |
| 5244 | } |
| 5245 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5246 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 03196cf | 2016-02-01 12:23:22 +0000 | [diff] [blame] | 5247 | // We call a runtime method to throw ArrayStoreException. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5248 | return NeedsTypeCheck(); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5249 | } |
| 5250 | |
Mingyao Yang | 81014cb | 2015-06-02 03:16:27 -0700 | [diff] [blame] | 5251 | // Can throw ArrayStoreException. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5252 | bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); } |
Mingyao Yang | 81014cb | 2015-06-02 03:16:27 -0700 | [diff] [blame] | 5253 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5254 | bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5255 | // TODO: Same as for ArrayGet. |
| 5256 | return false; |
| 5257 | } |
| 5258 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5259 | void ClearNeedsTypeCheck() { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5260 | SetPackedFlag<kFlagNeedsTypeCheck>(false); |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 5263 | void ClearValueCanBeNull() { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5264 | SetPackedFlag<kFlagValueCanBeNull>(false); |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 5265 | } |
| 5266 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 5267 | void SetStaticTypeOfArrayIsObjectArray() { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5268 | SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 5269 | } |
| 5270 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5271 | bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); } |
| 5272 | bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); } |
| 5273 | bool StaticTypeOfArrayIsObjectArray() const { |
| 5274 | return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(); |
| 5275 | } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5276 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5277 | HInstruction* GetArray() const { return InputAt(0); } |
| 5278 | HInstruction* GetIndex() const { return InputAt(1); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5279 | HInstruction* GetValue() const { return InputAt(2); } |
| 5280 | |
| 5281 | Primitive::Type GetComponentType() const { |
| 5282 | // The Dex format does not type floating point index operations. Since the |
| 5283 | // `expected_component_type_` is set during building and can therefore not |
| 5284 | // be correct, we also check what is the value type. If it is a floating |
| 5285 | // point type, we must use that type. |
| 5286 | Primitive::Type value_type = GetValue()->GetType(); |
| 5287 | return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble)) |
| 5288 | ? value_type |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5289 | : GetRawExpectedComponentType(); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 5290 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 5291 | |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 5292 | Primitive::Type GetRawExpectedComponentType() const { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5293 | return GetPackedField<ExpectedComponentTypeField>(); |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 5294 | } |
| 5295 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 5296 | static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) { |
| 5297 | return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None(); |
| 5298 | } |
| 5299 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5300 | DECLARE_INSTRUCTION(ArraySet); |
| 5301 | |
| 5302 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5303 | static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits; |
| 5304 | static constexpr size_t kFieldExpectedComponentTypeSize = |
| 5305 | MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast)); |
| 5306 | static constexpr size_t kFlagNeedsTypeCheck = |
| 5307 | kFieldExpectedComponentType + kFieldExpectedComponentTypeSize; |
| 5308 | static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1; |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 5309 | // Cached information for the reference_type_info_ so that codegen |
| 5310 | // does not need to inspect the static type. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5311 | static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1; |
| 5312 | static constexpr size_t kNumberOfArraySetPackedBits = |
| 5313 | kFlagStaticTypeOfArrayIsObjectArray + 1; |
| 5314 | static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 5315 | using ExpectedComponentTypeField = |
| 5316 | BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5317 | |
| 5318 | DISALLOW_COPY_AND_ASSIGN(HArraySet); |
| 5319 | }; |
| 5320 | |
| 5321 | class HArrayLength : public HExpression<1> { |
| 5322 | public: |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 5323 | HArrayLength(HInstruction* array, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5324 | : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 5325 | // Note that arrays do not change length, so the instruction does not |
| 5326 | // depend on any write. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5327 | SetRawInputAt(0, array); |
| 5328 | } |
| 5329 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 5330 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5331 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 5332 | return true; |
| 5333 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 5334 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 5335 | return obj == InputAt(0); |
| 5336 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 5337 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5338 | DECLARE_INSTRUCTION(ArrayLength); |
| 5339 | |
| 5340 | private: |
| 5341 | DISALLOW_COPY_AND_ASSIGN(HArrayLength); |
| 5342 | }; |
| 5343 | |
| 5344 | class HBoundsCheck : public HExpression<2> { |
| 5345 | public: |
Nicolas Geoffray | 1af564e | 2016-01-13 12:09:39 +0000 | [diff] [blame] | 5346 | // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException` |
| 5347 | // constructor. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5348 | HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) |
Nicolas Geoffray | 1af564e | 2016-01-13 12:09:39 +0000 | [diff] [blame] | 5349 | : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5350 | DCHECK(index->GetType() == Primitive::kPrimInt); |
| 5351 | SetRawInputAt(0, index); |
| 5352 | SetRawInputAt(1, length); |
| 5353 | } |
| 5354 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 5355 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5356 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 5357 | return true; |
| 5358 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 5359 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 5360 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5361 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 5362 | bool CanThrow() const OVERRIDE { return true; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 5363 | |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 5364 | HInstruction* GetIndex() const { return InputAt(0); } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5365 | |
| 5366 | DECLARE_INSTRUCTION(BoundsCheck); |
| 5367 | |
| 5368 | private: |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 5369 | DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); |
| 5370 | }; |
| 5371 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5372 | class HSuspendCheck : public HTemplateInstruction<0> { |
| 5373 | public: |
| 5374 | explicit HSuspendCheck(uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5375 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5376 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 5377 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5378 | return true; |
| 5379 | } |
| 5380 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 5381 | void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; } |
| 5382 | SlowPathCode* GetSlowPath() const { return slow_path_; } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5383 | |
| 5384 | DECLARE_INSTRUCTION(SuspendCheck); |
| 5385 | |
| 5386 | private: |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 5387 | // Only used for code generation, in order to share the same slow path between back edges |
| 5388 | // of a same loop. |
| 5389 | SlowPathCode* slow_path_; |
| 5390 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 5391 | DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); |
| 5392 | }; |
| 5393 | |
David Srbecky | 0cf4493 | 2015-12-09 14:09:59 +0000 | [diff] [blame] | 5394 | // Pseudo-instruction which provides the native debugger with mapping information. |
| 5395 | // It ensures that we can generate line number and local variables at this point. |
| 5396 | class HNativeDebugInfo : public HTemplateInstruction<0> { |
| 5397 | public: |
| 5398 | explicit HNativeDebugInfo(uint32_t dex_pc) |
| 5399 | : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {} |
| 5400 | |
| 5401 | bool NeedsEnvironment() const OVERRIDE { |
| 5402 | return true; |
| 5403 | } |
| 5404 | |
| 5405 | DECLARE_INSTRUCTION(NativeDebugInfo); |
| 5406 | |
| 5407 | private: |
| 5408 | DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo); |
| 5409 | }; |
| 5410 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5411 | /** |
| 5412 | * Instruction to load a Class object. |
| 5413 | */ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 5414 | class HLoadClass : public HExpression<1> { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5415 | public: |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 5416 | HLoadClass(HCurrentMethod* current_method, |
| 5417 | uint16_t type_index, |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 5418 | const DexFile& dex_file, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5419 | bool is_referrers_class, |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 5420 | uint32_t dex_pc, |
Nicolas Geoffray | 42e372e | 2015-11-24 15:48:56 +0000 | [diff] [blame] | 5421 | bool needs_access_check, |
| 5422 | bool is_in_dex_cache) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5423 | : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5424 | type_index_(type_index), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 5425 | dex_file_(dex_file), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 5426 | loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) { |
Calin Juravle | 4e2a557 | 2015-10-07 18:55:43 +0100 | [diff] [blame] | 5427 | // Referrers class should not need access check. We never inline unverified |
| 5428 | // methods so we can't possibly end up in this situation. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5429 | DCHECK(!is_referrers_class || !needs_access_check); |
| 5430 | |
| 5431 | SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class); |
| 5432 | SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check); |
| 5433 | SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache); |
| 5434 | SetPackedFlag<kFlagGenerateClInitCheck>(false); |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 5435 | SetRawInputAt(0, current_method); |
| 5436 | } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5437 | |
| 5438 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5439 | |
| 5440 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | a9a306d | 2015-10-08 16:48:31 +0100 | [diff] [blame] | 5441 | // Note that we don't need to test for generate_clinit_check_. |
| 5442 | // Whether or not we need to generate the clinit check is processed in |
| 5443 | // prepare_for_register_allocator based on existing HInvokes and HClinitChecks. |
Calin Juravle | 386062d | 2015-10-07 18:55:43 +0100 | [diff] [blame] | 5444 | return other->AsLoadClass()->type_index_ == type_index_ && |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5445 | other->AsLoadClass()->GetPackedFields() == GetPackedFields(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | size_t ComputeHashCode() const OVERRIDE { return type_index_; } |
| 5449 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5450 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | 7d5ea03 | 2015-07-02 15:48:27 +0100 | [diff] [blame] | 5451 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5452 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5453 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 42e372e | 2015-11-24 15:48:56 +0000 | [diff] [blame] | 5454 | return CanCallRuntime(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5455 | } |
| 5456 | |
Calin Juravle | 0ba218d | 2015-05-19 18:46:01 +0100 | [diff] [blame] | 5457 | void SetMustGenerateClinitCheck(bool generate_clinit_check) { |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 5458 | // The entrypoint the code generator is going to call does not do |
| 5459 | // clinit of the class. |
| 5460 | DCHECK(!NeedsAccessCheck()); |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5461 | SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check); |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | bool CanCallRuntime() const { |
Nicolas Geoffray | 42e372e | 2015-11-24 15:48:56 +0000 | [diff] [blame] | 5465 | return MustGenerateClinitCheck() || |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5466 | (!IsReferrersClass() && !IsInDexCache()) || |
| 5467 | NeedsAccessCheck(); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 5468 | } |
| 5469 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5470 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 5471 | bool CanThrow() const OVERRIDE { |
Nicolas Geoffray | 78f4fa7 | 2015-06-12 09:35:05 +0100 | [diff] [blame] | 5472 | return CanCallRuntime(); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 5475 | ReferenceTypeInfo GetLoadedClassRTI() { |
| 5476 | return loaded_class_rti_; |
| 5477 | } |
| 5478 | |
| 5479 | void SetLoadedClassRTI(ReferenceTypeInfo rti) { |
| 5480 | // Make sure we only set exact types (the loaded class should never be merged). |
| 5481 | DCHECK(rti.IsExact()); |
| 5482 | loaded_class_rti_ = rti; |
| 5483 | } |
| 5484 | |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 5485 | const DexFile& GetDexFile() { return dex_file_; } |
| 5486 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5487 | bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 5488 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 5489 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 5490 | return SideEffects::CanTriggerGC(); |
| 5491 | } |
| 5492 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5493 | bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); } |
| 5494 | bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); } |
| 5495 | bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); } |
| 5496 | bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); } |
Nicolas Geoffray | 42e372e | 2015-11-24 15:48:56 +0000 | [diff] [blame] | 5497 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5498 | DECLARE_INSTRUCTION(LoadClass); |
| 5499 | |
| 5500 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5501 | static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits; |
| 5502 | static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1; |
| 5503 | static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5504 | // Whether this instruction must generate the initialization check. |
| 5505 | // Used for code generation. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5506 | static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1; |
| 5507 | static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1; |
| 5508 | static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields."); |
| 5509 | |
| 5510 | const uint16_t type_index_; |
| 5511 | const DexFile& dex_file_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5512 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 5513 | ReferenceTypeInfo loaded_class_rti_; |
| 5514 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5515 | DISALLOW_COPY_AND_ASSIGN(HLoadClass); |
| 5516 | }; |
| 5517 | |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 5518 | class HLoadString : public HExpression<1> { |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5519 | public: |
Nicolas Geoffray | 917d016 | 2015-11-24 18:25:35 +0000 | [diff] [blame] | 5520 | HLoadString(HCurrentMethod* current_method, |
| 5521 | uint32_t string_index, |
| 5522 | uint32_t dex_pc, |
| 5523 | bool is_in_dex_cache) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5524 | : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5525 | string_index_(string_index) { |
| 5526 | SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache); |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 5527 | SetRawInputAt(0, current_method); |
| 5528 | } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5529 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5530 | bool CanBeMoved() const OVERRIDE { return true; } |
| 5531 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5532 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 5533 | return other->AsLoadString()->string_index_ == string_index_; |
| 5534 | } |
| 5535 | |
| 5536 | size_t ComputeHashCode() const OVERRIDE { return string_index_; } |
| 5537 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5538 | uint32_t GetStringIndex() const { return string_index_; } |
| 5539 | |
Nicolas Geoffray | 03196cf | 2016-02-01 12:23:22 +0000 | [diff] [blame] | 5540 | // Will call the runtime if the string is not already in the dex cache. |
| 5541 | bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); } |
| 5542 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 5543 | bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 5544 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | 03196cf | 2016-02-01 12:23:22 +0000 | [diff] [blame] | 5545 | bool CanThrow() const OVERRIDE { return !IsInDexCache(); } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5546 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 5547 | static SideEffects SideEffectsForArchRuntimeCalls() { |
| 5548 | return SideEffects::CanTriggerGC(); |
| 5549 | } |
| 5550 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5551 | bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); } |
| 5552 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5553 | DECLARE_INSTRUCTION(LoadString); |
| 5554 | |
| 5555 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5556 | static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits; |
| 5557 | static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1; |
| 5558 | static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 5559 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5560 | const uint32_t string_index_; |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 5561 | |
| 5562 | DISALLOW_COPY_AND_ASSIGN(HLoadString); |
| 5563 | }; |
| 5564 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5565 | /** |
| 5566 | * Performs an initialization check on its Class object input. |
| 5567 | */ |
| 5568 | class HClinitCheck : public HExpression<1> { |
| 5569 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 5570 | HClinitCheck(HLoadClass* constant, uint32_t dex_pc) |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 5571 | : HExpression( |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 5572 | Primitive::kPrimNot, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5573 | SideEffects::AllChanges(), // Assume write/read on all fields/arrays. |
| 5574 | dex_pc) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5575 | SetRawInputAt(0, constant); |
| 5576 | } |
| 5577 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5578 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 5579 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 5580 | return true; |
| 5581 | } |
| 5582 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5583 | bool NeedsEnvironment() const OVERRIDE { |
| 5584 | // May call runtime to initialize the class. |
| 5585 | return true; |
| 5586 | } |
| 5587 | |
Nicolas Geoffray | 729645a | 2015-11-19 13:29:02 +0000 | [diff] [blame] | 5588 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5589 | |
| 5590 | HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); } |
| 5591 | |
| 5592 | DECLARE_INSTRUCTION(ClinitCheck); |
| 5593 | |
| 5594 | private: |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5595 | DISALLOW_COPY_AND_ASSIGN(HClinitCheck); |
| 5596 | }; |
| 5597 | |
| 5598 | class HStaticFieldGet : public HExpression<1> { |
| 5599 | public: |
| 5600 | HStaticFieldGet(HInstruction* cls, |
| 5601 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5602 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5603 | bool is_volatile, |
| 5604 | uint32_t field_idx, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5605 | uint16_t declaring_class_def_index, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5606 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5607 | Handle<mirror::DexCache> dex_cache, |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 5608 | uint32_t dex_pc) |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5609 | : HExpression(field_type, |
| 5610 | SideEffects::FieldReadOfType(field_type, is_volatile), |
| 5611 | dex_pc), |
| 5612 | field_info_(field_offset, |
| 5613 | field_type, |
| 5614 | is_volatile, |
| 5615 | field_idx, |
| 5616 | declaring_class_def_index, |
| 5617 | dex_file, |
| 5618 | dex_cache) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5619 | SetRawInputAt(0, cls); |
| 5620 | } |
| 5621 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5622 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 5623 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5624 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5625 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5626 | HStaticFieldGet* other_get = other->AsStaticFieldGet(); |
| 5627 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | size_t ComputeHashCode() const OVERRIDE { |
| 5631 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 5632 | } |
| 5633 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5634 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5635 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 5636 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5637 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5638 | |
| 5639 | DECLARE_INSTRUCTION(StaticFieldGet); |
| 5640 | |
| 5641 | private: |
| 5642 | const FieldInfo field_info_; |
| 5643 | |
| 5644 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet); |
| 5645 | }; |
| 5646 | |
| 5647 | class HStaticFieldSet : public HTemplateInstruction<2> { |
| 5648 | public: |
| 5649 | HStaticFieldSet(HInstruction* cls, |
| 5650 | HInstruction* value, |
| 5651 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5652 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 5653 | bool is_volatile, |
| 5654 | uint32_t field_idx, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5655 | uint16_t declaring_class_def_index, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 5656 | const DexFile& dex_file, |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5657 | Handle<mirror::DexCache> dex_cache, |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 5658 | uint32_t dex_pc) |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 5659 | : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), |
| 5660 | dex_pc), |
| 5661 | field_info_(field_offset, |
| 5662 | field_type, |
| 5663 | is_volatile, |
| 5664 | field_idx, |
| 5665 | declaring_class_def_index, |
| 5666 | dex_file, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5667 | dex_cache) { |
| 5668 | SetPackedFlag<kFlagValueCanBeNull>(true); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5669 | SetRawInputAt(0, cls); |
| 5670 | SetRawInputAt(1, value); |
| 5671 | } |
| 5672 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5673 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5674 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 5675 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 5676 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5677 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5678 | HInstruction* GetValue() const { return InputAt(1); } |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5679 | bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); } |
| 5680 | void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 5681 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5682 | DECLARE_INSTRUCTION(StaticFieldSet); |
| 5683 | |
| 5684 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5685 | static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits; |
| 5686 | static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1; |
| 5687 | static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits, |
| 5688 | "Too many packed fields."); |
| 5689 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 5690 | const FieldInfo field_info_; |
| 5691 | |
| 5692 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet); |
| 5693 | }; |
| 5694 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5695 | class HUnresolvedInstanceFieldGet : public HExpression<1> { |
| 5696 | public: |
| 5697 | HUnresolvedInstanceFieldGet(HInstruction* obj, |
| 5698 | Primitive::Type field_type, |
| 5699 | uint32_t field_index, |
| 5700 | uint32_t dex_pc) |
| 5701 | : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc), |
| 5702 | field_index_(field_index) { |
| 5703 | SetRawInputAt(0, obj); |
| 5704 | } |
| 5705 | |
| 5706 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 5707 | bool CanThrow() const OVERRIDE { return true; } |
| 5708 | |
| 5709 | Primitive::Type GetFieldType() const { return GetType(); } |
| 5710 | uint32_t GetFieldIndex() const { return field_index_; } |
| 5711 | |
| 5712 | DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet); |
| 5713 | |
| 5714 | private: |
| 5715 | const uint32_t field_index_; |
| 5716 | |
| 5717 | DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet); |
| 5718 | }; |
| 5719 | |
| 5720 | class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> { |
| 5721 | public: |
| 5722 | HUnresolvedInstanceFieldSet(HInstruction* obj, |
| 5723 | HInstruction* value, |
| 5724 | Primitive::Type field_type, |
| 5725 | uint32_t field_index, |
| 5726 | uint32_t dex_pc) |
| 5727 | : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc), |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5728 | field_index_(field_index) { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5729 | SetPackedField<FieldTypeField>(field_type); |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5730 | DCHECK_EQ(field_type, value->GetType()); |
| 5731 | SetRawInputAt(0, obj); |
| 5732 | SetRawInputAt(1, value); |
| 5733 | } |
| 5734 | |
| 5735 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 5736 | bool CanThrow() const OVERRIDE { return true; } |
| 5737 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5738 | Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); } |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5739 | uint32_t GetFieldIndex() const { return field_index_; } |
| 5740 | |
| 5741 | DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet); |
| 5742 | |
| 5743 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5744 | static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits; |
| 5745 | static constexpr size_t kFieldFieldTypeSize = |
| 5746 | MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast)); |
| 5747 | static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits = |
| 5748 | kFieldFieldType + kFieldFieldTypeSize; |
| 5749 | static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits, |
| 5750 | "Too many packed fields."); |
| 5751 | using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>; |
| 5752 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5753 | const uint32_t field_index_; |
| 5754 | |
| 5755 | DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet); |
| 5756 | }; |
| 5757 | |
| 5758 | class HUnresolvedStaticFieldGet : public HExpression<0> { |
| 5759 | public: |
| 5760 | HUnresolvedStaticFieldGet(Primitive::Type field_type, |
| 5761 | uint32_t field_index, |
| 5762 | uint32_t dex_pc) |
| 5763 | : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc), |
| 5764 | field_index_(field_index) { |
| 5765 | } |
| 5766 | |
| 5767 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 5768 | bool CanThrow() const OVERRIDE { return true; } |
| 5769 | |
| 5770 | Primitive::Type GetFieldType() const { return GetType(); } |
| 5771 | uint32_t GetFieldIndex() const { return field_index_; } |
| 5772 | |
| 5773 | DECLARE_INSTRUCTION(UnresolvedStaticFieldGet); |
| 5774 | |
| 5775 | private: |
| 5776 | const uint32_t field_index_; |
| 5777 | |
| 5778 | DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet); |
| 5779 | }; |
| 5780 | |
| 5781 | class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> { |
| 5782 | public: |
| 5783 | HUnresolvedStaticFieldSet(HInstruction* value, |
| 5784 | Primitive::Type field_type, |
| 5785 | uint32_t field_index, |
| 5786 | uint32_t dex_pc) |
| 5787 | : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc), |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5788 | field_index_(field_index) { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5789 | SetPackedField<FieldTypeField>(field_type); |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5790 | DCHECK_EQ(field_type, value->GetType()); |
| 5791 | SetRawInputAt(0, value); |
| 5792 | } |
| 5793 | |
| 5794 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 5795 | bool CanThrow() const OVERRIDE { return true; } |
| 5796 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5797 | Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); } |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5798 | uint32_t GetFieldIndex() const { return field_index_; } |
| 5799 | |
| 5800 | DECLARE_INSTRUCTION(UnresolvedStaticFieldSet); |
| 5801 | |
| 5802 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5803 | static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits; |
| 5804 | static constexpr size_t kFieldFieldTypeSize = |
| 5805 | MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast)); |
| 5806 | static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits = |
| 5807 | kFieldFieldType + kFieldFieldTypeSize; |
| 5808 | static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits, |
| 5809 | "Too many packed fields."); |
| 5810 | using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>; |
| 5811 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 5812 | const uint32_t field_index_; |
| 5813 | |
| 5814 | DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet); |
| 5815 | }; |
| 5816 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5817 | // Implement the move-exception DEX instruction. |
| 5818 | class HLoadException : public HExpression<0> { |
| 5819 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5820 | explicit HLoadException(uint32_t dex_pc = kNoDexPc) |
| 5821 | : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {} |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5822 | |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 5823 | bool CanBeNull() const OVERRIDE { return false; } |
| 5824 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5825 | DECLARE_INSTRUCTION(LoadException); |
| 5826 | |
| 5827 | private: |
| 5828 | DISALLOW_COPY_AND_ASSIGN(HLoadException); |
| 5829 | }; |
| 5830 | |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 5831 | // Implicit part of move-exception which clears thread-local exception storage. |
| 5832 | // Must not be removed because the runtime expects the TLS to get cleared. |
| 5833 | class HClearException : public HTemplateInstruction<0> { |
| 5834 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5835 | explicit HClearException(uint32_t dex_pc = kNoDexPc) |
| 5836 | : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {} |
David Brazdil | cb1c055 | 2015-08-04 16:22:25 +0100 | [diff] [blame] | 5837 | |
| 5838 | DECLARE_INSTRUCTION(ClearException); |
| 5839 | |
| 5840 | private: |
| 5841 | DISALLOW_COPY_AND_ASSIGN(HClearException); |
| 5842 | }; |
| 5843 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5844 | class HThrow : public HTemplateInstruction<1> { |
| 5845 | public: |
| 5846 | HThrow(HInstruction* exception, uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5847 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) { |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5848 | SetRawInputAt(0, exception); |
| 5849 | } |
| 5850 | |
| 5851 | bool IsControlFlow() const OVERRIDE { return true; } |
| 5852 | |
| 5853 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 5854 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 5855 | bool CanThrow() const OVERRIDE { return true; } |
| 5856 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5857 | |
| 5858 | DECLARE_INSTRUCTION(Throw); |
| 5859 | |
| 5860 | private: |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 5861 | DISALLOW_COPY_AND_ASSIGN(HThrow); |
| 5862 | }; |
| 5863 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5864 | /** |
| 5865 | * Implementation strategies for the code generator of a HInstanceOf |
| 5866 | * or `HCheckCast`. |
| 5867 | */ |
| 5868 | enum class TypeCheckKind { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 5869 | kUnresolvedCheck, // Check against an unresolved type. |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5870 | kExactCheck, // Can do a single class compare. |
| 5871 | kClassHierarchyCheck, // Can just walk the super class chain. |
| 5872 | kAbstractClassCheck, // Can just walk the super class chain, starting one up. |
| 5873 | kInterfaceCheck, // No optimization yet when checking against an interface. |
| 5874 | kArrayObjectCheck, // Can just check if the array is not primitive. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5875 | kArrayCheck, // No optimization yet when checking against a generic array. |
| 5876 | kLast = kArrayCheck |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5877 | }; |
| 5878 | |
Roland Levillain | 8650378 | 2016-02-11 19:07:30 +0000 | [diff] [blame] | 5879 | std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs); |
| 5880 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5881 | class HInstanceOf : public HExpression<2> { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5882 | public: |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5883 | HInstanceOf(HInstruction* object, |
| 5884 | HLoadClass* constant, |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5885 | TypeCheckKind check_kind, |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5886 | uint32_t dex_pc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5887 | : HExpression(Primitive::kPrimBoolean, |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5888 | SideEffectsForArchRuntimeCalls(check_kind), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5889 | dex_pc) { |
| 5890 | SetPackedField<TypeCheckKindField>(check_kind); |
| 5891 | SetPackedFlag<kFlagMustDoNullCheck>(true); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5892 | SetRawInputAt(0, object); |
| 5893 | SetRawInputAt(1, constant); |
| 5894 | } |
| 5895 | |
| 5896 | bool CanBeMoved() const OVERRIDE { return true; } |
| 5897 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5898 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5899 | return true; |
| 5900 | } |
| 5901 | |
| 5902 | bool NeedsEnvironment() const OVERRIDE { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5903 | return CanCallRuntime(GetTypeCheckKind()); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5904 | } |
| 5905 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 5906 | // Used only in code generation. |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5907 | bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); } |
| 5908 | void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); } |
| 5909 | TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); } |
| 5910 | bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; } |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 5911 | |
Nicolas Geoffray | 03196cf | 2016-02-01 12:23:22 +0000 | [diff] [blame] | 5912 | static bool CanCallRuntime(TypeCheckKind check_kind) { |
| 5913 | // Mips currently does runtime calls for any other checks. |
| 5914 | return check_kind != TypeCheckKind::kExactCheck; |
| 5915 | } |
| 5916 | |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5917 | static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) { |
Nicolas Geoffray | 03196cf | 2016-02-01 12:23:22 +0000 | [diff] [blame] | 5918 | return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None(); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 5919 | } |
| 5920 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5921 | DECLARE_INSTRUCTION(InstanceOf); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5922 | |
| 5923 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5924 | static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits; |
| 5925 | static constexpr size_t kFieldTypeCheckKindSize = |
| 5926 | MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast)); |
| 5927 | static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize; |
| 5928 | static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1; |
| 5929 | static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 5930 | using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>; |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 5931 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5932 | DISALLOW_COPY_AND_ASSIGN(HInstanceOf); |
| 5933 | }; |
| 5934 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 5935 | class HBoundType : public HExpression<1> { |
| 5936 | public: |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 5937 | HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 5938 | : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc), |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5939 | upper_bound_(ReferenceTypeInfo::CreateInvalid()) { |
| 5940 | SetPackedFlag<kFlagUpperCanBeNull>(true); |
| 5941 | SetPackedFlag<kFlagCanBeNull>(true); |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 5942 | DCHECK_EQ(input->GetType(), Primitive::kPrimNot); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 5943 | SetRawInputAt(0, input); |
| 5944 | } |
| 5945 | |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 5946 | // {Get,Set}Upper* should only be used in reference type propagation. |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 5947 | const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; } |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5948 | bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); } |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 5949 | void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 5950 | |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 5951 | void SetCanBeNull(bool can_be_null) { |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5952 | DCHECK(GetUpperCanBeNull() || !can_be_null); |
| 5953 | SetPackedFlag<kFlagCanBeNull>(can_be_null); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 5954 | } |
| 5955 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5956 | bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); } |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 5957 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 5958 | DECLARE_INSTRUCTION(BoundType); |
| 5959 | |
| 5960 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5961 | // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this |
| 5962 | // is false then CanBeNull() cannot be true). |
| 5963 | static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits; |
| 5964 | static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1; |
| 5965 | static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1; |
| 5966 | static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 5967 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 5968 | // Encodes the most upper class that this instruction can have. In other words |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 5969 | // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()). |
| 5970 | // It is used to bound the type in cases like: |
| 5971 | // if (x instanceof ClassX) { |
| 5972 | // // uper_bound_ will be ClassX |
| 5973 | // } |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 5974 | ReferenceTypeInfo upper_bound_; |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 5975 | |
| 5976 | DISALLOW_COPY_AND_ASSIGN(HBoundType); |
| 5977 | }; |
| 5978 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5979 | class HCheckCast : public HTemplateInstruction<2> { |
| 5980 | public: |
| 5981 | HCheckCast(HInstruction* object, |
| 5982 | HLoadClass* constant, |
Nicolas Geoffray | 85c7bab | 2015-09-18 13:40:46 +0000 | [diff] [blame] | 5983 | TypeCheckKind check_kind, |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5984 | uint32_t dex_pc) |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 5985 | : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) { |
| 5986 | SetPackedField<TypeCheckKindField>(check_kind); |
| 5987 | SetPackedFlag<kFlagMustDoNullCheck>(true); |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 5988 | SetRawInputAt(0, object); |
| 5989 | SetRawInputAt(1, constant); |
| 5990 | } |
| 5991 | |
| 5992 | bool CanBeMoved() const OVERRIDE { return true; } |
| 5993 | |
| 5994 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 5995 | return true; |
| 5996 | } |
| 5997 | |
| 5998 | bool NeedsEnvironment() const OVERRIDE { |
| 5999 | // Instruction may throw a CheckCastError. |
| 6000 | return true; |
| 6001 | } |
| 6002 | |
| 6003 | bool CanThrow() const OVERRIDE { return true; } |
| 6004 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6005 | bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); } |
| 6006 | void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); } |
| 6007 | TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); } |
| 6008 | bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; } |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 6009 | |
| 6010 | DECLARE_INSTRUCTION(CheckCast); |
| 6011 | |
| 6012 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6013 | static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits; |
| 6014 | static constexpr size_t kFieldTypeCheckKindSize = |
| 6015 | MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast)); |
| 6016 | static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize; |
| 6017 | static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1; |
| 6018 | static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields."); |
| 6019 | using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>; |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 6020 | |
| 6021 | DISALLOW_COPY_AND_ASSIGN(HCheckCast); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 6022 | }; |
| 6023 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 6024 | class HMemoryBarrier : public HTemplateInstruction<0> { |
| 6025 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 6026 | explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc) |
Aart Bik | 34c3ba9 | 2015-07-20 14:08:59 -0700 | [diff] [blame] | 6027 | : HTemplateInstruction( |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6028 | SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays. |
| 6029 | SetPackedField<BarrierKindField>(barrier_kind); |
| 6030 | } |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 6031 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6032 | MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); } |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 6033 | |
| 6034 | DECLARE_INSTRUCTION(MemoryBarrier); |
| 6035 | |
| 6036 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6037 | static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits; |
| 6038 | static constexpr size_t kFieldBarrierKindSize = |
| 6039 | MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind)); |
| 6040 | static constexpr size_t kNumberOfMemoryBarrierPackedBits = |
| 6041 | kFieldBarrierKind + kFieldBarrierKindSize; |
| 6042 | static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits, |
| 6043 | "Too many packed fields."); |
| 6044 | using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>; |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 6045 | |
| 6046 | DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier); |
| 6047 | }; |
| 6048 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6049 | class HMonitorOperation : public HTemplateInstruction<1> { |
| 6050 | public: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6051 | enum class OperationKind { |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6052 | kEnter, |
| 6053 | kExit, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6054 | kLast = kExit |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6055 | }; |
| 6056 | |
| 6057 | HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc) |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 6058 | : HTemplateInstruction( |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6059 | SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays. |
| 6060 | dex_pc) { |
| 6061 | SetPackedField<OperationKindField>(kind); |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6062 | SetRawInputAt(0, object); |
| 6063 | } |
| 6064 | |
Nicolas Geoffray | 03196cf | 2016-02-01 12:23:22 +0000 | [diff] [blame] | 6065 | // Instruction may go into runtime, so we need an environment. |
| 6066 | bool NeedsEnvironment() const OVERRIDE { return true; } |
David Brazdil | bff7503 | 2015-07-08 17:26:51 +0000 | [diff] [blame] | 6067 | |
| 6068 | bool CanThrow() const OVERRIDE { |
| 6069 | // Verifier guarantees that monitor-exit cannot throw. |
| 6070 | // This is important because it allows the HGraphBuilder to remove |
| 6071 | // a dead throw-catch loop generated for `synchronized` blocks/methods. |
| 6072 | return IsEnter(); |
| 6073 | } |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6074 | |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6075 | OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); } |
| 6076 | bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; } |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6077 | |
| 6078 | DECLARE_INSTRUCTION(MonitorOperation); |
| 6079 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 6080 | private: |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 6081 | static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits; |
| 6082 | static constexpr size_t kFieldOperationKindSize = |
| 6083 | MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast)); |
| 6084 | static constexpr size_t kNumberOfMonitorOperationPackedBits = |
| 6085 | kFieldOperationKind + kFieldOperationKindSize; |
| 6086 | static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits, |
| 6087 | "Too many packed fields."); |
| 6088 | using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>; |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 6089 | |
| 6090 | private: |
| 6091 | DISALLOW_COPY_AND_ASSIGN(HMonitorOperation); |
| 6092 | }; |
| 6093 | |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 6094 | class HSelect : public HExpression<3> { |
| 6095 | public: |
| 6096 | HSelect(HInstruction* condition, |
| 6097 | HInstruction* true_value, |
| 6098 | HInstruction* false_value, |
| 6099 | uint32_t dex_pc) |
| 6100 | : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) { |
| 6101 | DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType())); |
| 6102 | |
| 6103 | // First input must be `true_value` or `false_value` to allow codegens to |
| 6104 | // use the SameAsFirstInput allocation policy. We make it `false_value`, so |
| 6105 | // that architectures which implement HSelect as a conditional move also |
| 6106 | // will not need to invert the condition. |
| 6107 | SetRawInputAt(0, false_value); |
| 6108 | SetRawInputAt(1, true_value); |
| 6109 | SetRawInputAt(2, condition); |
| 6110 | } |
| 6111 | |
| 6112 | HInstruction* GetFalseValue() const { return InputAt(0); } |
| 6113 | HInstruction* GetTrueValue() const { return InputAt(1); } |
| 6114 | HInstruction* GetCondition() const { return InputAt(2); } |
| 6115 | |
| 6116 | bool CanBeMoved() const OVERRIDE { return true; } |
| 6117 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
| 6118 | |
| 6119 | bool CanBeNull() const OVERRIDE { |
| 6120 | return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull(); |
| 6121 | } |
| 6122 | |
| 6123 | DECLARE_INSTRUCTION(Select); |
| 6124 | |
| 6125 | private: |
| 6126 | DISALLOW_COPY_AND_ASSIGN(HSelect); |
| 6127 | }; |
| 6128 | |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 6129 | class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6130 | public: |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 6131 | MoveOperands(Location source, |
| 6132 | Location destination, |
| 6133 | Primitive::Type type, |
| 6134 | HInstruction* instruction) |
| 6135 | : source_(source), destination_(destination), type_(type), instruction_(instruction) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6136 | |
| 6137 | Location GetSource() const { return source_; } |
| 6138 | Location GetDestination() const { return destination_; } |
| 6139 | |
| 6140 | void SetSource(Location value) { source_ = value; } |
| 6141 | void SetDestination(Location value) { destination_ = value; } |
| 6142 | |
| 6143 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 6144 | // destination (but not the source). |
| 6145 | Location MarkPending() { |
| 6146 | DCHECK(!IsPending()); |
| 6147 | Location dest = destination_; |
| 6148 | destination_ = Location::NoLocation(); |
| 6149 | return dest; |
| 6150 | } |
| 6151 | |
| 6152 | void ClearPending(Location dest) { |
| 6153 | DCHECK(IsPending()); |
| 6154 | destination_ = dest; |
| 6155 | } |
| 6156 | |
| 6157 | bool IsPending() const { |
Roland Levillain | c928591 | 2015-12-18 10:38:42 +0000 | [diff] [blame] | 6158 | DCHECK(source_.IsValid() || destination_.IsInvalid()); |
| 6159 | return destination_.IsInvalid() && source_.IsValid(); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | // True if this blocks a move from the given location. |
| 6163 | bool Blocks(Location loc) const { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 6164 | return !IsEliminated() && source_.OverlapsWith(loc); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | // A move is redundant if it's been eliminated, if its source and |
| 6168 | // destination are the same, or if its destination is unneeded. |
| 6169 | bool IsRedundant() const { |
| 6170 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 6171 | } |
| 6172 | |
| 6173 | // We clear both operands to indicate move that's been eliminated. |
| 6174 | void Eliminate() { |
| 6175 | source_ = destination_ = Location::NoLocation(); |
| 6176 | } |
| 6177 | |
| 6178 | bool IsEliminated() const { |
| 6179 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 6180 | return source_.IsInvalid(); |
| 6181 | } |
| 6182 | |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 6183 | Primitive::Type GetType() const { return type_; } |
| 6184 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 6185 | bool Is64BitMove() const { |
| 6186 | return Primitive::Is64BitType(type_); |
| 6187 | } |
| 6188 | |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 6189 | HInstruction* GetInstruction() const { return instruction_; } |
| 6190 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6191 | private: |
| 6192 | Location source_; |
| 6193 | Location destination_; |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 6194 | // The type this move is for. |
| 6195 | Primitive::Type type_; |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 6196 | // The instruction this move is assocatied with. Null when this move is |
| 6197 | // for moving an input in the expected locations of user (including a phi user). |
| 6198 | // This is only used in debug mode, to ensure we do not connect interval siblings |
| 6199 | // in the same parallel move. |
| 6200 | HInstruction* instruction_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6201 | }; |
| 6202 | |
Roland Levillain | c928591 | 2015-12-18 10:38:42 +0000 | [diff] [blame] | 6203 | std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs); |
| 6204 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6205 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 6206 | |
| 6207 | class HParallelMove : public HTemplateInstruction<0> { |
| 6208 | public: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 6209 | explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc) |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6210 | : HTemplateInstruction(SideEffects::None(), dex_pc), |
| 6211 | moves_(arena->Adapter(kArenaAllocMoveOperands)) { |
| 6212 | moves_.reserve(kDefaultNumberOfMoves); |
| 6213 | } |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6214 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 6215 | void AddMove(Location source, |
| 6216 | Location destination, |
| 6217 | Primitive::Type type, |
| 6218 | HInstruction* instruction) { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 6219 | DCHECK(source.IsValid()); |
| 6220 | DCHECK(destination.IsValid()); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 6221 | if (kIsDebugBuild) { |
| 6222 | if (instruction != nullptr) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6223 | for (const MoveOperands& move : moves_) { |
| 6224 | if (move.GetInstruction() == instruction) { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 6225 | // Special case the situation where the move is for the spill slot |
| 6226 | // of the instruction. |
| 6227 | if ((GetPrevious() == instruction) |
| 6228 | || ((GetPrevious() == nullptr) |
| 6229 | && instruction->IsPhi() |
| 6230 | && instruction->GetBlock() == GetBlock())) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6231 | DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind()) |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 6232 | << "Doing parallel moves for the same instruction."; |
| 6233 | } else { |
| 6234 | DCHECK(false) << "Doing parallel moves for the same instruction."; |
| 6235 | } |
| 6236 | } |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 6237 | } |
| 6238 | } |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6239 | for (const MoveOperands& move : moves_) { |
| 6240 | DCHECK(!destination.OverlapsWith(move.GetDestination())) |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 6241 | << "Overlapped destination for two moves in a parallel move: " |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6242 | << move.GetSource() << " ==> " << move.GetDestination() << " and " |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 6243 | << source << " ==> " << destination; |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 6244 | } |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 6245 | } |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6246 | moves_.emplace_back(source, destination, type, instruction); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6247 | } |
| 6248 | |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6249 | MoveOperands* MoveOperandsAt(size_t index) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6250 | return &moves_[index]; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6251 | } |
| 6252 | |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6253 | size_t NumMoves() const { return moves_.size(); } |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6254 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 6255 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6256 | |
| 6257 | private: |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 6258 | ArenaVector<MoveOperands> moves_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 6259 | |
| 6260 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 6261 | }; |
| 6262 | |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 6263 | } // namespace art |
| 6264 | |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 6265 | #if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64) |
| 6266 | #include "nodes_shared.h" |
| 6267 | #endif |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 6268 | #ifdef ART_ENABLE_CODEGEN_arm |
| 6269 | #include "nodes_arm.h" |
| 6270 | #endif |
Alexandre Rames | e6dbf48 | 2015-10-19 10:10:41 +0100 | [diff] [blame] | 6271 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 6272 | #include "nodes_arm64.h" |
| 6273 | #endif |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 6274 | #ifdef ART_ENABLE_CODEGEN_x86 |
| 6275 | #include "nodes_x86.h" |
| 6276 | #endif |
| 6277 | |
| 6278 | namespace art { |
| 6279 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6280 | class HGraphVisitor : public ValueObject { |
| 6281 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 6282 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} |
| 6283 | virtual ~HGraphVisitor() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6284 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 6285 | virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6286 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 6287 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 6288 | // Visit the graph following basic block insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6289 | void VisitInsertionOrder(); |
| 6290 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 6291 | // Visit the graph following dominator tree reverse post-order. |
| 6292 | void VisitReversePostOrder(); |
| 6293 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 6294 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 6295 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6296 | // Visit functions for instruction classes. |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 6297 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6298 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 6299 | |
| 6300 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 6301 | |
| 6302 | #undef DECLARE_VISIT_INSTRUCTION |
| 6303 | |
| 6304 | private: |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 6305 | HGraph* const graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6306 | |
| 6307 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 6308 | }; |
| 6309 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 6310 | class HGraphDelegateVisitor : public HGraphVisitor { |
| 6311 | public: |
| 6312 | explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} |
| 6313 | virtual ~HGraphDelegateVisitor() {} |
| 6314 | |
| 6315 | // Visit functions that delegate to to super class. |
| 6316 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 6317 | void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 6318 | |
| 6319 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 6320 | |
| 6321 | #undef DECLARE_VISIT_INSTRUCTION |
| 6322 | |
| 6323 | private: |
| 6324 | DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); |
| 6325 | }; |
| 6326 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6327 | class HInsertionOrderIterator : public ValueObject { |
| 6328 | public: |
| 6329 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 6330 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6331 | bool Done() const { return index_ == graph_.GetBlocks().size(); } |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 6332 | HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6333 | void Advance() { ++index_; } |
| 6334 | |
| 6335 | private: |
| 6336 | const HGraph& graph_; |
| 6337 | size_t index_; |
| 6338 | |
| 6339 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 6340 | }; |
| 6341 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 6342 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6343 | public: |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 6344 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) { |
| 6345 | // Check that reverse post order of the graph has been built. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6346 | DCHECK(!graph.GetReversePostOrder().empty()); |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 6347 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6348 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6349 | bool Done() const { return index_ == graph_.GetReversePostOrder().size(); } |
| 6350 | HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6351 | void Advance() { ++index_; } |
| 6352 | |
| 6353 | private: |
| 6354 | const HGraph& graph_; |
| 6355 | size_t index_; |
| 6356 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 6357 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6358 | }; |
| 6359 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 6360 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6361 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 6362 | explicit HPostOrderIterator(const HGraph& graph) |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6363 | : graph_(graph), index_(graph_.GetReversePostOrder().size()) { |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 6364 | // Check that reverse post order of the graph has been built. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6365 | DCHECK(!graph.GetReversePostOrder().empty()); |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 6366 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6367 | |
| 6368 | bool Done() const { return index_ == 0; } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6369 | HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6370 | void Advance() { --index_; } |
| 6371 | |
| 6372 | private: |
| 6373 | const HGraph& graph_; |
| 6374 | size_t index_; |
| 6375 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 6376 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 6377 | }; |
| 6378 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 6379 | class HLinearPostOrderIterator : public ValueObject { |
| 6380 | public: |
| 6381 | explicit HLinearPostOrderIterator(const HGraph& graph) |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6382 | : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {} |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 6383 | |
| 6384 | bool Done() const { return index_ == 0; } |
| 6385 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6386 | HBasicBlock* Current() const { return order_[index_ - 1u]; } |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 6387 | |
| 6388 | void Advance() { |
| 6389 | --index_; |
| 6390 | DCHECK_GE(index_, 0U); |
| 6391 | } |
| 6392 | |
| 6393 | private: |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6394 | const ArenaVector<HBasicBlock*>& order_; |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 6395 | size_t index_; |
| 6396 | |
| 6397 | DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator); |
| 6398 | }; |
| 6399 | |
| 6400 | class HLinearOrderIterator : public ValueObject { |
| 6401 | public: |
| 6402 | explicit HLinearOrderIterator(const HGraph& graph) |
| 6403 | : order_(graph.GetLinearOrder()), index_(0) {} |
| 6404 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6405 | bool Done() const { return index_ == order_.size(); } |
| 6406 | HBasicBlock* Current() const { return order_[index_]; } |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 6407 | void Advance() { ++index_; } |
| 6408 | |
| 6409 | private: |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6410 | const ArenaVector<HBasicBlock*>& order_; |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 6411 | size_t index_; |
| 6412 | |
| 6413 | DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator); |
| 6414 | }; |
| 6415 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 6416 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 6417 | // of an inner loop. The order in which the blocks are iterated is on their |
| 6418 | // block id. |
| 6419 | class HBlocksInLoopIterator : public ValueObject { |
| 6420 | public: |
| 6421 | explicit HBlocksInLoopIterator(const HLoopInformation& info) |
| 6422 | : blocks_in_loop_(info.GetBlocks()), |
| 6423 | blocks_(info.GetHeader()->GetGraph()->GetBlocks()), |
| 6424 | index_(0) { |
| 6425 | if (!blocks_in_loop_.IsBitSet(index_)) { |
| 6426 | Advance(); |
| 6427 | } |
| 6428 | } |
| 6429 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6430 | bool Done() const { return index_ == blocks_.size(); } |
| 6431 | HBasicBlock* Current() const { return blocks_[index_]; } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 6432 | void Advance() { |
| 6433 | ++index_; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6434 | for (size_t e = blocks_.size(); index_ < e; ++index_) { |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 6435 | if (blocks_in_loop_.IsBitSet(index_)) { |
| 6436 | break; |
| 6437 | } |
| 6438 | } |
| 6439 | } |
| 6440 | |
| 6441 | private: |
| 6442 | const BitVector& blocks_in_loop_; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6443 | const ArenaVector<HBasicBlock*>& blocks_; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 6444 | size_t index_; |
| 6445 | |
| 6446 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator); |
| 6447 | }; |
| 6448 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 6449 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 6450 | // of an inner loop. The order in which the blocks are iterated is reverse |
| 6451 | // post order. |
| 6452 | class HBlocksInLoopReversePostOrderIterator : public ValueObject { |
| 6453 | public: |
| 6454 | explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info) |
| 6455 | : blocks_in_loop_(info.GetBlocks()), |
| 6456 | blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()), |
| 6457 | index_(0) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6458 | if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) { |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 6459 | Advance(); |
| 6460 | } |
| 6461 | } |
| 6462 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6463 | bool Done() const { return index_ == blocks_.size(); } |
| 6464 | HBasicBlock* Current() const { return blocks_[index_]; } |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 6465 | void Advance() { |
| 6466 | ++index_; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6467 | for (size_t e = blocks_.size(); index_ < e; ++index_) { |
| 6468 | if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) { |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 6469 | break; |
| 6470 | } |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | private: |
| 6475 | const BitVector& blocks_in_loop_; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 6476 | const ArenaVector<HBasicBlock*>& blocks_; |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 6477 | size_t index_; |
| 6478 | |
| 6479 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator); |
| 6480 | }; |
| 6481 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 6482 | inline int64_t Int64FromConstant(HConstant* constant) { |
David Brazdil | c0b601b | 2016-02-08 14:20:45 +0000 | [diff] [blame] | 6483 | if (constant->IsIntConstant()) { |
| 6484 | return constant->AsIntConstant()->GetValue(); |
| 6485 | } else if (constant->IsLongConstant()) { |
| 6486 | return constant->AsLongConstant()->GetValue(); |
| 6487 | } else { |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 6488 | DCHECK(constant->IsNullConstant()) << constant->DebugName(); |
David Brazdil | c0b601b | 2016-02-08 14:20:45 +0000 | [diff] [blame] | 6489 | return 0; |
| 6490 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 6491 | } |
| 6492 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 6493 | inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) { |
| 6494 | // For the purposes of the compiler, the dex files must actually be the same object |
| 6495 | // if we want to safely treat them as the same. This is especially important for JIT |
| 6496 | // as custom class loaders can open the same underlying file (or memory) multiple |
| 6497 | // times and provide different class resolution but no two class loaders should ever |
| 6498 | // use the same DexFile object - doing so is an unsupported hack that can lead to |
| 6499 | // all sorts of weird failures. |
| 6500 | return &lhs == &rhs; |
| 6501 | } |
| 6502 | |
Vladimir Marko | 5f7b58e | 2015-11-23 19:49:34 +0000 | [diff] [blame] | 6503 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
| 6504 | inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \ |
| 6505 | inline const H##type* HInstruction::As##type() const { \ |
| 6506 | return Is##type() ? down_cast<const H##type*>(this) : nullptr; \ |
| 6507 | } \ |
| 6508 | inline H##type* HInstruction::As##type() { \ |
| 6509 | return Is##type() ? static_cast<H##type*>(this) : nullptr; \ |
| 6510 | } |
| 6511 | |
| 6512 | FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 6513 | #undef INSTRUCTION_TYPE_CHECK |
| 6514 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 6515 | class SwitchTable : public ValueObject { |
| 6516 | public: |
| 6517 | SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse) |
| 6518 | : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) { |
| 6519 | int32_t table_offset = instruction.VRegB_31t(); |
| 6520 | const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset; |
| 6521 | if (sparse) { |
| 6522 | CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature)); |
| 6523 | } else { |
| 6524 | CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature)); |
| 6525 | } |
| 6526 | num_entries_ = table[1]; |
| 6527 | values_ = reinterpret_cast<const int32_t*>(&table[2]); |
| 6528 | } |
| 6529 | |
| 6530 | uint16_t GetNumEntries() const { |
| 6531 | return num_entries_; |
| 6532 | } |
| 6533 | |
| 6534 | void CheckIndex(size_t index) const { |
| 6535 | if (sparse_) { |
| 6536 | // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order. |
| 6537 | DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_)); |
| 6538 | } else { |
| 6539 | // In a packed table, we have the starting key and num_entries_ values. |
| 6540 | DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_)); |
| 6541 | } |
| 6542 | } |
| 6543 | |
| 6544 | int32_t GetEntryAt(size_t index) const { |
| 6545 | CheckIndex(index); |
| 6546 | return values_[index]; |
| 6547 | } |
| 6548 | |
| 6549 | uint32_t GetDexPcForIndex(size_t index) const { |
| 6550 | CheckIndex(index); |
| 6551 | return dex_pc_ + |
| 6552 | (reinterpret_cast<const int16_t*>(values_ + index) - |
| 6553 | reinterpret_cast<const int16_t*>(&instruction_)); |
| 6554 | } |
| 6555 | |
| 6556 | // Index of the first value in the table. |
| 6557 | size_t GetFirstValueIndex() const { |
| 6558 | if (sparse_) { |
| 6559 | // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order. |
| 6560 | return num_entries_; |
| 6561 | } else { |
| 6562 | // In a packed table, we have the starting key and num_entries_ values. |
| 6563 | return 1; |
| 6564 | } |
| 6565 | } |
| 6566 | |
| 6567 | private: |
| 6568 | const Instruction& instruction_; |
| 6569 | const uint32_t dex_pc_; |
| 6570 | |
| 6571 | // Whether this is a sparse-switch table (or a packed-switch one). |
| 6572 | const bool sparse_; |
| 6573 | |
| 6574 | // This can't be const as it needs to be computed off of the given instruction, and complicated |
| 6575 | // expressions in the initializer list seemed very ugly. |
| 6576 | uint16_t num_entries_; |
| 6577 | |
| 6578 | const int32_t* values_; |
| 6579 | |
| 6580 | DISALLOW_COPY_AND_ASSIGN(SwitchTable); |
| 6581 | }; |
| 6582 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 6583 | // Create space in `blocks` for adding `number_of_new_blocks` entries |
| 6584 | // starting at location `at`. Blocks after `at` are moved accordingly. |
| 6585 | inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks, |
| 6586 | size_t number_of_new_blocks, |
| 6587 | size_t after) { |
| 6588 | DCHECK_LT(after, blocks->size()); |
| 6589 | size_t old_size = blocks->size(); |
| 6590 | size_t new_size = old_size + number_of_new_blocks; |
| 6591 | blocks->resize(new_size); |
| 6592 | std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end()); |
| 6593 | } |
| 6594 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 6595 | } // namespace art |
| 6596 | |
| 6597 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |