blob: 55e436f0b7e44dee5fa70d845a94916f19537637 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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 Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // 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 Allison20dfc792014-06-16 20:44:29 -070099};
100
David Brazdild9510df2015-11-04 23:30:22 +0000101enum BuildSsaResult {
102 kBuildSsaFailNonNaturalLoop,
103 kBuildSsaFailThrowCatchLoop,
104 kBuildSsaFailAmbiguousArrayGet,
105 kBuildSsaSuccess,
106};
107
Vladimir Markof9f64412015-09-02 14:05:49 +0100108class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 public:
110 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
111
112 void AddInstruction(HInstruction* instruction);
113 void RemoveInstruction(HInstruction* instruction);
114
David Brazdilc3d743f2015-04-22 13:40:50 +0100115 // Insert `instruction` before/after an existing instruction `cursor`.
116 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
117 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
118
Roland Levillain6b469232014-09-25 10:10:38 +0100119 // Return true if this list contains `instruction`.
120 bool Contains(HInstruction* instruction) const;
121
Roland Levillainccc07a92014-09-16 14:48:16 +0100122 // 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 Geoffray276d9da2015-02-02 18:24:11 +0000128 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);
135 void Add(const HInstructionList& instruction_list);
136
David Brazdil2d7352b2015-04-20 14:52:42 +0100137 // Return the number of instructions in the list. This is an expensive operation.
138 size_t CountSize() const;
139
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 private:
141 HInstruction* first_instruction_;
142 HInstruction* last_instruction_;
143
144 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000145 friend class HGraph;
146 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100148 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100149
150 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
151};
152
David Brazdild9510df2015-11-04 23:30:22 +0000153class ReferenceTypeInfo : ValueObject {
154 public:
155 typedef Handle<mirror::Class> TypeHandle;
156
157 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
158 // The constructor will check that the type_handle is valid.
159 return ReferenceTypeInfo(type_handle, is_exact);
160 }
161
162 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
163
164 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
165 return handle.GetReference() != nullptr;
166 }
167
168 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
169 return IsValidHandle(type_handle_);
170 }
171
172 bool IsExact() const { return is_exact_; }
173
174 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
175 DCHECK(IsValid());
176 return GetTypeHandle()->IsObjectClass();
177 }
178
179 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
180 DCHECK(IsValid());
181 return GetTypeHandle()->IsStringClass();
182 }
183
184 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
187 }
188
189 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return GetTypeHandle()->IsInterface();
192 }
193
194 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsArrayClass();
197 }
198
199 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsPrimitiveArray();
202 }
203
204 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
207 }
208
209 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 if (!IsExact()) return false;
212 if (!IsArrayClass()) return false;
213 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
214 }
215
216 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
217 DCHECK(IsValid());
218 if (!IsExact()) return false;
219 if (!IsArrayClass()) return false;
220 if (!rti.IsArrayClass()) return false;
221 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
222 rti.GetTypeHandle()->GetComponentType());
223 }
224
225 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
226
227 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
228 DCHECK(IsValid());
229 DCHECK(rti.IsValid());
230 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
231 }
232
233 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
234 DCHECK(IsValid());
235 DCHECK(rti.IsValid());
236 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
237 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
238 }
239
240 // Returns true if the type information provide the same amount of details.
241 // Note that it does not mean that the instructions have the same actual type
242 // (because the type can be the result of a merge).
243 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
244 if (!IsValid() && !rti.IsValid()) {
245 // Invalid types are equal.
246 return true;
247 }
248 if (!IsValid() || !rti.IsValid()) {
249 // One is valid, the other not.
250 return false;
251 }
252 return IsExact() == rti.IsExact()
253 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
254 }
255
256 private:
257 ReferenceTypeInfo();
258 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
259
260 // The class of the object.
261 TypeHandle type_handle_;
262 // Whether or not the type is exact or a superclass of the actual type.
263 // Whether or not we have any information about this type.
264 bool is_exact_;
265};
266
267std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
268
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100270class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100272 HGraph(ArenaAllocator* arena,
273 const DexFile& dex_file,
274 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100275 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100277 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 bool debuggable = false,
279 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100281 blocks_(arena->Adapter(kArenaAllocBlockList)),
282 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
283 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700284 entry_block_(nullptr),
285 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100286 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100287 number_of_vregs_(0),
288 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000289 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400290 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000291 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000292 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000293 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100294 dex_file_(dex_file),
295 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100296 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100297 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100298 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100301 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
304 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdild9510df2015-11-04 23:30:22 +0000305 cached_current_method_(nullptr),
306 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 blocks_.reserve(kDefaultNumberOfBlocks);
308 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000310 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100311 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
312
David Brazdil69ba7b72015-06-23 18:27:30 +0100313 bool IsInSsaForm() const { return in_ssa_form_; }
314
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000315 HBasicBlock* GetEntryBlock() const { return entry_block_; }
316 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100317 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000318
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
320 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000321
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100323
David Brazdild9510df2015-11-04 23:30:22 +0000324 // Try building the SSA form of this graph, with dominance computation and
325 // loop recognition. Returns a code specifying that it was successful or the
326 // reason for failure.
327 BuildSsaResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000328
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100329 void ComputeDominanceInformation();
330 void ClearDominanceInformation();
331
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000332 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100334 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335
David Brazdild9510df2015-11-04 23:30:22 +0000336 // Analyze all natural loops in this graph. Returns a code specifying that it
337 // was successful or the reason for failure. The method will fail if a loop
338 // is not natural, that is the header does not dominate a back edge, or if it
339 // is a throw-catch loop, i.e. the header is a catch block.
340 BuildSsaResult AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341
David Brazdilffee3d32015-07-06 11:48:53 +0100342 // Iterate over blocks to compute try block membership. Needs reverse post
343 // order and loop information.
344 void ComputeTryBlockInformation();
345
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000346 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000347 // Returns the instruction used to replace the invoke expression or null if the
348 // invoke is for a void method.
349 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350
Mingyao Yang3584bce2015-05-19 16:01:59 -0700351 // Need to add a couple of blocks to test if the loop body is entered and
352 // put deoptimization instructions, etc.
353 void TransformLoopHeaderForBCE(HBasicBlock* header);
354
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000355 // Removes `block` from the graph. Assumes `block` has been disconnected from
356 // other blocks and has no instructions or phis.
357 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000358
David Brazdilfc6a86a2015-06-26 10:33:45 +0000359 // Splits the edge between `block` and `successor` while preserving the
360 // indices in the predecessor/successor lists. If there are multiple edges
361 // between the blocks, the lowest indices are used.
362 // Returns the new block which is empty and has the same dex pc as `successor`.
363 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
364
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
366 void SimplifyLoop(HBasicBlock* header);
367
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000368 int32_t GetNextInstructionId() {
369 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000370 return current_instruction_id_++;
371 }
372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000373 int32_t GetCurrentInstructionId() const {
374 return current_instruction_id_;
375 }
376
377 void SetCurrentInstructionId(int32_t id) {
378 current_instruction_id_ = id;
379 }
380
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100381 uint16_t GetMaximumNumberOfOutVRegs() const {
382 return maximum_number_of_out_vregs_;
383 }
384
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
386 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100387 }
388
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100389 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
390 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
391 }
392
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000393 void UpdateTemporariesVRegSlots(size_t slots) {
394 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100395 }
396
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000397 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100398 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000399 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100400 }
401
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100402 void SetNumberOfVRegs(uint16_t number_of_vregs) {
403 number_of_vregs_ = number_of_vregs;
404 }
405
406 uint16_t GetNumberOfVRegs() const {
407 return number_of_vregs_;
408 }
409
410 void SetNumberOfInVRegs(uint16_t value) {
411 number_of_in_vregs_ = value;
412 }
413
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100414 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100415 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100416 return number_of_vregs_ - number_of_in_vregs_;
417 }
418
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100419 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100421 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100422
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100424 return linear_order_;
425 }
426
Mark Mendell1152c922015-04-24 17:06:35 -0400427 bool HasBoundsChecks() const {
428 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800429 }
430
Mark Mendell1152c922015-04-24 17:06:35 -0400431 void SetHasBoundsChecks(bool value) {
432 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800433 }
434
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100435 bool ShouldGenerateConstructorBarrier() const {
436 return should_generate_constructor_barrier_;
437 }
438
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000439 bool IsDebuggable() const { return debuggable_; }
440
David Brazdil8d5b8b22015-03-24 10:51:52 +0000441 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000442 // already, it is created and inserted into the graph. This method is only for
443 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600444 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000445
446 // TODO: This is problematic for the consistency of reference type propagation
447 // because it can be created anytime after the pass and thus it will be left
448 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600449 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000450
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600451 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
452 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000453 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600454 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
455 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000456 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600457 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
458 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000459 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600460 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
461 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000463
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100464 HCurrentMethod* GetCurrentMethod();
465
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100466 const DexFile& GetDexFile() const {
467 return dex_file_;
468 }
469
470 uint32_t GetMethodIdx() const {
471 return method_idx_;
472 }
473
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100474 InvokeType GetInvokeType() const {
475 return invoke_type_;
476 }
477
Mark Mendellc4701932015-04-10 13:18:51 -0400478 InstructionSet GetInstructionSet() const {
479 return instruction_set_;
480 }
481
David Brazdil77a48ae2015-09-15 12:34:04 +0000482 bool HasTryCatch() const { return has_try_catch_; }
483 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100484
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100485 ArtMethod* GetArtMethod() const { return art_method_; }
486 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
487
Mark Mendellf6529172015-11-17 11:16:56 -0500488 // Returns an instruction with the opposite boolean value from 'cond'.
489 // The instruction has been inserted into the graph, either as a constant, or
490 // before cursor.
491 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
492
David Brazdil2d7352b2015-04-20 14:52:42 +0100493 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100494 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000495 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100496 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000498 template <class InstructionType, typename ValueType>
499 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600500 ArenaSafeMap<ValueType, InstructionType*>* cache,
501 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000502 // Try to find an existing constant of the given value.
503 InstructionType* constant = nullptr;
504 auto cached_constant = cache->find(value);
505 if (cached_constant != cache->end()) {
506 constant = cached_constant->second;
507 }
508
509 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100510 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000511 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000513 cache->Overwrite(value, constant);
514 InsertConstant(constant);
515 }
516 return constant;
517 }
518
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 void InsertConstant(HConstant* instruction);
520
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000521 // Cache a float constant into the graph. This method should only be
522 // called by the SsaBuilder when creating "equivalent" instructions.
523 void CacheFloatConstant(HFloatConstant* constant);
524
525 // See CacheFloatConstant comment.
526 void CacheDoubleConstant(HDoubleConstant* constant);
527
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000528 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000529
530 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100531 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000532
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100533 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100534 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100536 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100537 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100538
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000539 HBasicBlock* entry_block_;
540 HBasicBlock* exit_block_;
541
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100542 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100543 uint16_t maximum_number_of_out_vregs_;
544
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100545 // The number of virtual registers in this method. Contains the parameters.
546 uint16_t number_of_vregs_;
547
548 // The number of virtual registers used by parameters of this method.
549 uint16_t number_of_in_vregs_;
550
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000551 // Number of vreg size slots that the temporaries use (used in baseline compiler).
552 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100553
Mark Mendell1152c922015-04-24 17:06:35 -0400554 // Has bounds checks. We can totally skip BCE if it's false.
555 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800556
David Brazdil77a48ae2015-09-15 12:34:04 +0000557 // Flag whether there are any try/catch blocks in the graph. We will skip
558 // try/catch-related passes if false.
559 bool has_try_catch_;
560
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000561 // Indicates whether the graph should be compiled in a way that
562 // ensures full debuggability. If false, we can apply more
563 // aggressive optimizations that may limit the level of debugging.
564 const bool debuggable_;
565
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000566 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000567 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000568
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100569 // The dex file from which the method is from.
570 const DexFile& dex_file_;
571
572 // The method index in the dex file.
573 const uint32_t method_idx_;
574
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100575 // If inlined, this encodes how the callee is being invoked.
576 const InvokeType invoke_type_;
577
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100578 // Whether the graph has been transformed to SSA form. Only used
579 // in debug mode to ensure we are not using properties only valid
580 // for non-SSA form (like the number of temporaries).
581 bool in_ssa_form_;
582
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100583 const bool should_generate_constructor_barrier_;
584
Mathieu Chartiere401d142015-04-22 13:56:20 -0700585 const InstructionSet instruction_set_;
586
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000588 HNullConstant* cached_null_constant_;
589 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000590 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000591 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000592 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000593
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100594 HCurrentMethod* cached_current_method_;
595
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100596 // The ArtMethod this graph is for. Note that for AOT, it may be null,
597 // for example for methods whose declaring class could not be resolved
598 // (such as when the superclass could not be found).
599 ArtMethod* art_method_;
600
David Brazdild9510df2015-11-04 23:30:22 +0000601 // Keep the RTI of inexact Object to avoid having to pass stack handle
602 // collection pointer to passes which may create NullConstant.
603 ReferenceTypeInfo inexact_object_rti_;
604
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100606 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000607 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000608 DISALLOW_COPY_AND_ASSIGN(HGraph);
609};
610
Vladimir Markof9f64412015-09-02 14:05:49 +0100611class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000612 public:
613 HLoopInformation(HBasicBlock* header, HGraph* graph)
614 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100615 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100616 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100617 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100618 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
619 back_edges_.reserve(kDefaultNumberOfBackEdges);
620 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100621
622 HBasicBlock* GetHeader() const {
623 return header_;
624 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000625
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000626 void SetHeader(HBasicBlock* block) {
627 header_ = block;
628 }
629
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100630 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
631 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
632 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
633
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100635 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000636 }
637
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100638 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100639 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100640 }
641
David Brazdil46e2a392015-03-16 17:31:52 +0000642 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100643 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100644 }
645
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000646 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100647 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 }
649
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100650 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100651
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654 }
655
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100656 // Returns the lifetime position of the back edge that has the
657 // greatest lifetime position.
658 size_t GetLifetimeEnd() const;
659
660 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100662 }
663
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100664 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100665 // that is the header dominates the back edge.
666 bool Populate();
667
David Brazdila4b8c212015-05-07 09:59:30 +0100668 // Reanalyzes the loop by removing loop info from its blocks and re-running
669 // Populate(). If there are no back edges left, the loop info is completely
670 // removed as well as its SuspendCheck instruction. It must be run on nested
671 // inner loops first.
672 void Update();
673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 // Returns whether this loop information contains `block`.
675 // Note that this loop information *must* be populated before entering this function.
676 bool Contains(const HBasicBlock& block) const;
677
678 // Returns whether this loop information is an inner loop of `other`.
679 // Note that `other` *must* be populated before entering this function.
680 bool IsIn(const HLoopInformation& other) const;
681
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800682 // Returns true if instruction is not defined within this loop.
683 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 const ArenaBitVector& GetBlocks() const { return blocks_; }
686
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000687 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000688 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100691 // Internal recursive implementation of `Populate`.
692 void PopulateRecursive(HBasicBlock* block);
693
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100695 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698
699 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
700};
701
David Brazdilec16f792015-08-19 15:04:01 +0100702// Stores try/catch information for basic blocks.
703// Note that HGraph is constructed so that catch blocks cannot simultaneously
704// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100705class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100706 public:
707 // Try block information constructor.
708 explicit TryCatchInformation(const HTryBoundary& try_entry)
709 : try_entry_(&try_entry),
710 catch_dex_file_(nullptr),
711 catch_type_index_(DexFile::kDexNoIndex16) {
712 DCHECK(try_entry_ != nullptr);
713 }
714
715 // Catch block information constructor.
716 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
717 : try_entry_(nullptr),
718 catch_dex_file_(&dex_file),
719 catch_type_index_(catch_type_index) {}
720
721 bool IsTryBlock() const { return try_entry_ != nullptr; }
722
723 const HTryBoundary& GetTryEntry() const {
724 DCHECK(IsTryBlock());
725 return *try_entry_;
726 }
727
728 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
729
730 bool IsCatchAllTypeIndex() const {
731 DCHECK(IsCatchBlock());
732 return catch_type_index_ == DexFile::kDexNoIndex16;
733 }
734
735 uint16_t GetCatchTypeIndex() const {
736 DCHECK(IsCatchBlock());
737 return catch_type_index_;
738 }
739
740 const DexFile& GetCatchDexFile() const {
741 DCHECK(IsCatchBlock());
742 return *catch_dex_file_;
743 }
744
745 private:
746 // One of possibly several TryBoundary instructions entering the block's try.
747 // Only set for try blocks.
748 const HTryBoundary* try_entry_;
749
750 // Exception type information. Only set for catch blocks.
751 const DexFile* catch_dex_file_;
752 const uint16_t catch_type_index_;
753};
754
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100755static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100757
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000758// A block in a method. Contains the list of instructions represented
759// as a double linked list. Each block knows its predecessors and
760// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100761
Vladimir Markof9f64412015-09-02 14:05:49 +0100762class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600764 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000765 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000766 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
767 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768 loop_information_(nullptr),
769 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000770 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100771 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100772 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100773 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000774 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000775 try_catch_information_(nullptr) {
776 predecessors_.reserve(kDefaultNumberOfPredecessors);
777 successors_.reserve(kDefaultNumberOfSuccessors);
778 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
779 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780
Vladimir Marko60584552015-09-03 13:35:12 +0000781 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783 }
784
Vladimir Marko60584552015-09-03 13:35:12 +0000785 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787 }
788
David Brazdild26a4112015-11-10 11:07:31 +0000789 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
790 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
791
Vladimir Marko60584552015-09-03 13:35:12 +0000792 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
793 return ContainsElement(successors_, block, start_from);
794 }
795
796 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100797 return dominated_blocks_;
798 }
799
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100800 bool IsEntryBlock() const {
801 return graph_->GetEntryBlock() == this;
802 }
803
804 bool IsExitBlock() const {
805 return graph_->GetExitBlock() == this;
806 }
807
David Brazdil46e2a392015-03-16 17:31:52 +0000808 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000809 bool IsSingleTryBoundary() const;
810
811 // Returns true if this block emits nothing but a jump.
812 bool IsSingleJump() const {
813 HLoopInformation* loop_info = GetLoopInformation();
814 return (IsSingleGoto() || IsSingleTryBoundary())
815 // Back edges generate a suspend check.
816 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
817 }
David Brazdil46e2a392015-03-16 17:31:52 +0000818
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000819 void AddBackEdge(HBasicBlock* back_edge) {
820 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000822 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100823 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000824 loop_information_->AddBackEdge(back_edge);
825 }
826
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000827 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000828 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100830 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600832 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000834 HBasicBlock* GetDominator() const { return dominator_; }
835 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000836 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
837
838 void RemoveDominatedBlock(HBasicBlock* block) {
839 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100840 }
Vladimir Marko60584552015-09-03 13:35:12 +0000841
842 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
843 ReplaceElement(dominated_blocks_, existing, new_block);
844 }
845
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100846 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000847
848 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100849 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850 }
851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
853 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100854 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100855 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100856 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
857 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858
859 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000860 successors_.push_back(block);
861 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862 }
863
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100864 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
865 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100866 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000867 new_block->predecessors_.push_back(this);
868 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869 }
870
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000871 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
872 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000873 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000874 new_block->successors_.push_back(this);
875 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000876 }
877
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100878 // Insert `this` between `predecessor` and `successor. This method
879 // preserves the indicies, and will update the first edge found between
880 // `predecessor` and `successor`.
881 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
882 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100883 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000884 successor->predecessors_[predecessor_index] = this;
885 predecessor->successors_[successor_index] = this;
886 successors_.push_back(successor);
887 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100888 }
889
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100890 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000891 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 }
893
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000895 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896 }
897
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000899 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 }
901
902 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000903 predecessors_.push_back(block);
904 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 }
906
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100907 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000908 DCHECK_EQ(predecessors_.size(), 2u);
909 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100910 }
911
David Brazdil769c9e52015-04-27 13:54:09 +0100912 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000913 DCHECK_EQ(successors_.size(), 2u);
914 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100915 }
916
David Brazdilfc6a86a2015-06-26 10:33:45 +0000917 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000918 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100919 }
920
David Brazdilfc6a86a2015-06-26 10:33:45 +0000921 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000922 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100923 }
924
David Brazdilfc6a86a2015-06-26 10:33:45 +0000925 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000926 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100927 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000928 }
929
930 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000931 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100932 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000933 }
934
935 // Returns whether the first occurrence of `predecessor` in the list of
936 // predecessors is at index `idx`.
937 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100938 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000939 return GetPredecessorIndexOf(predecessor) == idx;
940 }
941
David Brazdild7558da2015-09-22 13:04:14 +0100942 // Create a new block between this block and its predecessors. The new block
943 // is added to the graph, all predecessor edges are relinked to it and an edge
944 // is created to `this`. Returns the new empty block. Reverse post order or
945 // loop and try/catch information are not updated.
946 HBasicBlock* CreateImmediateDominator();
947
David Brazdilfc6a86a2015-06-26 10:33:45 +0000948 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100949 // created, latter block. Note that this method will add the block to the
950 // graph, create a Goto at the end of the former block and will create an edge
951 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100952 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000953 HBasicBlock* SplitBefore(HInstruction* cursor);
954
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000955 // Split the block into two blocks just after `cursor`. Returns the newly
956 // created block. Note that this method just updates raw block information,
957 // like predecessors, successors, dominators, and instruction list. It does not
958 // update the graph, reverse post order, loop information, nor make sure the
959 // blocks are consistent (for example ending with a control flow instruction).
960 HBasicBlock* SplitAfter(HInstruction* cursor);
961
David Brazdil9bc43612015-11-05 21:25:24 +0000962 // Split catch block into two blocks after the original move-exception bytecode
963 // instruction, or at the beginning if not present. Returns the newly created,
964 // latter block, or nullptr if such block could not be created (must be dead
965 // in that case). Note that this method just updates raw block information,
966 // like predecessors, successors, dominators, and instruction list. It does not
967 // update the graph, reverse post order, loop information, nor make sure the
968 // blocks are consistent (for example ending with a control flow instruction).
969 HBasicBlock* SplitCatchBlockAfterMoveException();
970
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000971 // Merge `other` at the end of `this`. Successors and dominated blocks of
972 // `other` are changed to be successors and dominated blocks of `this`. Note
973 // that this method does not update the graph, reverse post order, loop
974 // information, nor make sure the blocks are consistent (for example ending
975 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100976 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000977
978 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
979 // of `this` are moved to `other`.
980 // Note that this method does not update the graph, reverse post order, loop
981 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000982 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000983 void ReplaceWith(HBasicBlock* other);
984
David Brazdil2d7352b2015-04-20 14:52:42 +0100985 // Merge `other` at the end of `this`. This method updates loops, reverse post
986 // order, links to predecessors, successors, dominators and deletes the block
987 // from the graph. The two blocks must be successive, i.e. `this` the only
988 // predecessor of `other` and vice versa.
989 void MergeWith(HBasicBlock* other);
990
991 // Disconnects `this` from all its predecessors, successors and dominator,
992 // removes it from all loops it is included in and eventually from the graph.
993 // The block must not dominate any other block. Predecessors and successors
994 // are safely updated.
995 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000996
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100998 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100999 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001000 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001001 // Replace instruction `initial` with `replacement` within this block.
1002 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1003 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001004 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001005 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001006 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1007 // instruction list. With 'ensure_safety' set to true, it verifies that the
1008 // instruction is not in use and removes it from the use lists of its inputs.
1009 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1010 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001011 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001012
1013 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001014 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015 }
1016
Roland Levillain6b879dd2014-09-22 17:13:44 +01001017 bool IsLoopPreHeaderFirstPredecessor() const {
1018 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001019 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001020 }
1021
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001022 HLoopInformation* GetLoopInformation() const {
1023 return loop_information_;
1024 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001025
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001027 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001028 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001029 void SetInLoop(HLoopInformation* info) {
1030 if (IsLoopHeader()) {
1031 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001032 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001033 loop_information_ = info;
1034 } else if (loop_information_->Contains(*info->GetHeader())) {
1035 // Block is currently part of an outer loop. Make it part of this inner loop.
1036 // Note that a non loop header having a loop information means this loop information
1037 // has already been populated
1038 loop_information_ = info;
1039 } else {
1040 // Block is part of an inner loop. Do not update the loop information.
1041 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1042 // at this point, because this method is being called while populating `info`.
1043 }
1044 }
1045
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001046 // Raw update of the loop information.
1047 void SetLoopInformation(HLoopInformation* info) {
1048 loop_information_ = info;
1049 }
1050
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001051 bool IsInLoop() const { return loop_information_ != nullptr; }
1052
David Brazdilec16f792015-08-19 15:04:01 +01001053 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1054
1055 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1056 try_catch_information_ = try_catch_information;
1057 }
1058
1059 bool IsTryBlock() const {
1060 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1061 }
1062
1063 bool IsCatchBlock() const {
1064 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1065 }
David Brazdilffee3d32015-07-06 11:48:53 +01001066
1067 // Returns the try entry that this block's successors should have. They will
1068 // be in the same try, unless the block ends in a try boundary. In that case,
1069 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001070 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001071
David Brazdild7558da2015-09-22 13:04:14 +01001072 bool HasThrowingInstructions() const;
1073
David Brazdila4b8c212015-05-07 09:59:30 +01001074 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 bool Dominates(HBasicBlock* block) const;
1076
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001077 size_t GetLifetimeStart() const { return lifetime_start_; }
1078 size_t GetLifetimeEnd() const { return lifetime_end_; }
1079
1080 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1081 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1082
David Brazdil8d5b8b22015-03-24 10:51:52 +00001083 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001084 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001085 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001086 bool HasSinglePhi() const;
1087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001089 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001090 ArenaVector<HBasicBlock*> predecessors_;
1091 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001092 HInstructionList instructions_;
1093 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001094 HLoopInformation* loop_information_;
1095 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001096 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001097 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001098 // The dex program counter of the first instruction of this block.
1099 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001100 size_t lifetime_start_;
1101 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001102 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001103
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001104 friend class HGraph;
1105 friend class HInstruction;
1106
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1108};
1109
David Brazdilb2bd1c52015-03-25 11:17:37 +00001110// Iterates over the LoopInformation of all loops which contain 'block'
1111// from the innermost to the outermost.
1112class HLoopInformationOutwardIterator : public ValueObject {
1113 public:
1114 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1115 : current_(block.GetLoopInformation()) {}
1116
1117 bool Done() const { return current_ == nullptr; }
1118
1119 void Advance() {
1120 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001121 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001122 }
1123
1124 HLoopInformation* Current() const {
1125 DCHECK(!Done());
1126 return current_;
1127 }
1128
1129 private:
1130 HLoopInformation* current_;
1131
1132 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1133};
1134
Alexandre Ramesef20f712015-06-09 10:29:30 +01001135#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001136 M(Above, Condition) \
1137 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001139 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001140 M(ArrayGet, Instruction) \
1141 M(ArrayLength, Instruction) \
1142 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001143 M(Below, Condition) \
1144 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001145 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001146 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001147 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001148 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001149 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001150 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001151 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001152 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001153 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001154 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001155 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001156 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001157 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001158 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001159 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001160 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001161 M(FloatConstant, Constant) \
1162 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001163 M(GreaterThan, Condition) \
1164 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001165 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001166 M(InstanceFieldGet, Instruction) \
1167 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001168 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001169 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001170 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001171 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001172 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001173 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001174 M(LessThan, Condition) \
1175 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001176 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001177 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001179 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001180 M(Local, Instruction) \
1181 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001182 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001183 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001184 M(Mul, BinaryOperation) \
1185 M(Neg, UnaryOperation) \
1186 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001188 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001189 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001190 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001192 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001193 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001194 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001195 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001196 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001197 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 M(Return, Instruction) \
1199 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001200 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001201 M(Shl, BinaryOperation) \
1202 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001203 M(StaticFieldGet, Instruction) \
1204 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001205 M(UnresolvedInstanceFieldGet, Instruction) \
1206 M(UnresolvedInstanceFieldSet, Instruction) \
1207 M(UnresolvedStaticFieldGet, Instruction) \
1208 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(StoreLocal, Instruction) \
1210 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001211 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001212 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001213 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001214 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001215 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001216 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001217 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001218
Vladimir Markob4536b72015-11-24 13:45:23 +00001219#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001220#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001221#else
1222#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1223 M(ArmDexCacheArraysBase, Instruction)
1224#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001225
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001226#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001227#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001228#else
1229#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001230 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001231 M(Arm64IntermediateAddress, Instruction) \
1232 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001233#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001234
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001235#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1236
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001237#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1238
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001239#ifndef ART_ENABLE_CODEGEN_x86
1240#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1241#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001242#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1243 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001244 M(X86LoadFromConstantTable, Instruction) \
1245 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001246#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001247
1248#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1249
1250#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1251 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1252 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1253 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001254 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001255 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001256 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1257 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1258
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259#define FOR_EACH_INSTRUCTION(M) \
1260 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1261 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001262 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001263 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001264 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001265
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001267FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1268#undef FORWARD_DECLARATION
1269
Roland Levillainccc07a92014-09-16 14:48:16 +01001270#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001271 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1272 const char* DebugName() const OVERRIDE { return #type; } \
1273 const H##type* As##type() const OVERRIDE { return this; } \
1274 H##type* As##type() OVERRIDE { return this; } \
1275 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001276 return other->Is##type(); \
1277 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001278 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279
David Brazdiled596192015-01-23 10:39:45 +00001280template <typename T> class HUseList;
1281
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001282template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001283class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001284 public:
David Brazdiled596192015-01-23 10:39:45 +00001285 HUseListNode* GetPrevious() const { return prev_; }
1286 HUseListNode* GetNext() const { return next_; }
1287 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001288 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001289 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001290
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001291 private:
David Brazdiled596192015-01-23 10:39:45 +00001292 HUseListNode(T user, size_t index)
1293 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1294
1295 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001296 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001297 HUseListNode<T>* prev_;
1298 HUseListNode<T>* next_;
1299
1300 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001301
1302 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1303};
1304
David Brazdiled596192015-01-23 10:39:45 +00001305template <typename T>
1306class HUseList : public ValueObject {
1307 public:
1308 HUseList() : first_(nullptr) {}
1309
1310 void Clear() {
1311 first_ = nullptr;
1312 }
1313
1314 // Adds a new entry at the beginning of the use list and returns
1315 // the newly created node.
1316 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001317 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001318 if (IsEmpty()) {
1319 first_ = new_node;
1320 } else {
1321 first_->prev_ = new_node;
1322 new_node->next_ = first_;
1323 first_ = new_node;
1324 }
1325 return new_node;
1326 }
1327
1328 HUseListNode<T>* GetFirst() const {
1329 return first_;
1330 }
1331
1332 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001333 DCHECK(node != nullptr);
1334 DCHECK(Contains(node));
1335
David Brazdiled596192015-01-23 10:39:45 +00001336 if (node->prev_ != nullptr) {
1337 node->prev_->next_ = node->next_;
1338 }
1339 if (node->next_ != nullptr) {
1340 node->next_->prev_ = node->prev_;
1341 }
1342 if (node == first_) {
1343 first_ = node->next_;
1344 }
1345 }
1346
David Brazdil1abb4192015-02-17 18:33:36 +00001347 bool Contains(const HUseListNode<T>* node) const {
1348 if (node == nullptr) {
1349 return false;
1350 }
1351 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1352 if (current == node) {
1353 return true;
1354 }
1355 }
1356 return false;
1357 }
1358
David Brazdiled596192015-01-23 10:39:45 +00001359 bool IsEmpty() const {
1360 return first_ == nullptr;
1361 }
1362
1363 bool HasOnlyOneUse() const {
1364 return first_ != nullptr && first_->next_ == nullptr;
1365 }
1366
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001367 size_t SizeSlow() const {
1368 size_t count = 0;
1369 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1370 ++count;
1371 }
1372 return count;
1373 }
1374
David Brazdiled596192015-01-23 10:39:45 +00001375 private:
1376 HUseListNode<T>* first_;
1377};
1378
1379template<typename T>
1380class HUseIterator : public ValueObject {
1381 public:
1382 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1383
1384 bool Done() const { return current_ == nullptr; }
1385
1386 void Advance() {
1387 DCHECK(!Done());
1388 current_ = current_->GetNext();
1389 }
1390
1391 HUseListNode<T>* Current() const {
1392 DCHECK(!Done());
1393 return current_;
1394 }
1395
1396 private:
1397 HUseListNode<T>* current_;
1398
1399 friend class HValue;
1400};
1401
David Brazdil1abb4192015-02-17 18:33:36 +00001402// This class is used by HEnvironment and HInstruction classes to record the
1403// instructions they use and pointers to the corresponding HUseListNodes kept
1404// by the used instructions.
1405template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001406class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001407 public:
1408 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1409 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1410
1411 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1412 : instruction_(old_record.instruction_), use_node_(use_node) {
1413 DCHECK(instruction_ != nullptr);
1414 DCHECK(use_node_ != nullptr);
1415 DCHECK(old_record.use_node_ == nullptr);
1416 }
1417
1418 HInstruction* GetInstruction() const { return instruction_; }
1419 HUseListNode<T>* GetUseNode() const { return use_node_; }
1420
1421 private:
1422 // Instruction used by the user.
1423 HInstruction* instruction_;
1424
1425 // Corresponding entry in the use list kept by 'instruction_'.
1426 HUseListNode<T>* use_node_;
1427};
1428
Aart Bik854a02b2015-07-14 16:07:00 -07001429/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001431 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 * For write/read dependences on fields/arrays, the dependence analysis uses
1433 * type disambiguation (e.g. a float field write cannot modify the value of an
1434 * integer field read) and the access type (e.g. a reference array write cannot
1435 * modify the value of a reference field read [although it may modify the
1436 * reference fetch prior to reading the field, which is represented by its own
1437 * write/read dependence]). The analysis makes conservative points-to
1438 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1439 * the same, and any reference read depends on any reference read without
1440 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001441 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 * The internal representation uses 38-bit and is described in the table below.
1443 * The first line indicates the side effect, and for field/array accesses the
1444 * second line indicates the type of the access (in the order of the
1445 * Primitive::Type enum).
1446 * The two numbered lines below indicate the bit position in the bitfield (read
1447 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001448 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1450 * +-------------+---------+---------+--------------+---------+---------+
1451 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1452 * | 3 |333333322|222222221| 1 |111111110|000000000|
1453 * | 7 |654321098|765432109| 8 |765432109|876543210|
1454 *
1455 * Note that, to ease the implementation, 'changes' bits are least significant
1456 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001457 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458class SideEffects : public ValueObject {
1459 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001460 SideEffects() : flags_(0) {}
1461
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462 static SideEffects None() {
1463 return SideEffects(0);
1464 }
1465
1466 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001467 return SideEffects(kAllChangeBits | kAllDependOnBits);
1468 }
1469
1470 static SideEffects AllChanges() {
1471 return SideEffects(kAllChangeBits);
1472 }
1473
1474 static SideEffects AllDependencies() {
1475 return SideEffects(kAllDependOnBits);
1476 }
1477
1478 static SideEffects AllExceptGCDependency() {
1479 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1480 }
1481
1482 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001483 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001484 }
1485
Aart Bik34c3ba92015-07-20 14:08:59 -07001486 static SideEffects AllWrites() {
1487 return SideEffects(kAllWrites);
1488 }
1489
1490 static SideEffects AllReads() {
1491 return SideEffects(kAllReads);
1492 }
1493
1494 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1495 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001496 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001497 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001498 }
1499
Aart Bik854a02b2015-07-14 16:07:00 -07001500 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1501 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001502 }
1503
Aart Bik34c3ba92015-07-20 14:08:59 -07001504 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1505 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001506 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001507 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001508 }
1509
1510 static SideEffects ArrayReadOfType(Primitive::Type type) {
1511 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1512 }
1513
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001514 static SideEffects CanTriggerGC() {
1515 return SideEffects(1ULL << kCanTriggerGCBit);
1516 }
1517
1518 static SideEffects DependsOnGC() {
1519 return SideEffects(1ULL << kDependsOnGCBit);
1520 }
1521
Aart Bik854a02b2015-07-14 16:07:00 -07001522 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001523 SideEffects Union(SideEffects other) const {
1524 return SideEffects(flags_ | other.flags_);
1525 }
1526
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 SideEffects Exclusion(SideEffects other) const {
1528 return SideEffects(flags_ & ~other.flags_);
1529 }
1530
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001531 void Add(SideEffects other) {
1532 flags_ |= other.flags_;
1533 }
1534
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001535 bool Includes(SideEffects other) const {
1536 return (other.flags_ & flags_) == other.flags_;
1537 }
1538
1539 bool HasSideEffects() const {
1540 return (flags_ & kAllChangeBits);
1541 }
1542
1543 bool HasDependencies() const {
1544 return (flags_ & kAllDependOnBits);
1545 }
1546
1547 // Returns true if there are no side effects or dependencies.
1548 bool DoesNothing() const {
1549 return flags_ == 0;
1550 }
1551
Aart Bik854a02b2015-07-14 16:07:00 -07001552 // Returns true if something is written.
1553 bool DoesAnyWrite() const {
1554 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001555 }
1556
Aart Bik854a02b2015-07-14 16:07:00 -07001557 // Returns true if something is read.
1558 bool DoesAnyRead() const {
1559 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001560 }
1561
Aart Bik854a02b2015-07-14 16:07:00 -07001562 // Returns true if potentially everything is written and read
1563 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 bool DoesAllReadWrite() const {
1565 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1566 }
1567
Aart Bik854a02b2015-07-14 16:07:00 -07001568 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001570 }
1571
Roland Levillain0d5a2812015-11-13 10:07:31 +00001572 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001573 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1575 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001576 }
1577
1578 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001580 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001581 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 for (int s = kLastBit; s >= 0; s--) {
1583 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1584 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1585 // This is a bit for the GC side effect.
1586 if (current_bit_is_set) {
1587 flags += "GC";
1588 }
Aart Bik854a02b2015-07-14 16:07:00 -07001589 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001590 } else {
1591 // This is a bit for the array/field analysis.
1592 // The underscore character stands for the 'can trigger GC' bit.
1593 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1594 if (current_bit_is_set) {
1595 flags += kDebug[s];
1596 }
1597 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1598 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1599 flags += "|";
1600 }
1601 }
Aart Bik854a02b2015-07-14 16:07:00 -07001602 }
1603 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001604 }
1605
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001606 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001607
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001608 private:
1609 static constexpr int kFieldArrayAnalysisBits = 9;
1610
1611 static constexpr int kFieldWriteOffset = 0;
1612 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1613 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1614 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1615
1616 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1617
1618 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1619 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1620 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1621 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1622
1623 static constexpr int kLastBit = kDependsOnGCBit;
1624 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1625
1626 // Aliases.
1627
1628 static_assert(kChangeBits == kDependOnBits,
1629 "the 'change' bits should match the 'depend on' bits.");
1630
1631 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1632 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1633 static constexpr uint64_t kAllWrites =
1634 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1635 static constexpr uint64_t kAllReads =
1636 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001637
Aart Bik854a02b2015-07-14 16:07:00 -07001638 // Work around the fact that HIR aliases I/F and J/D.
1639 // TODO: remove this interceptor once HIR types are clean
1640 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1641 switch (type) {
1642 case Primitive::kPrimInt:
1643 case Primitive::kPrimFloat:
1644 return TypeFlag(Primitive::kPrimInt, offset) |
1645 TypeFlag(Primitive::kPrimFloat, offset);
1646 case Primitive::kPrimLong:
1647 case Primitive::kPrimDouble:
1648 return TypeFlag(Primitive::kPrimLong, offset) |
1649 TypeFlag(Primitive::kPrimDouble, offset);
1650 default:
1651 return TypeFlag(type, offset);
1652 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001653 }
1654
Aart Bik854a02b2015-07-14 16:07:00 -07001655 // Translates type to bit flag.
1656 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1657 CHECK_NE(type, Primitive::kPrimVoid);
1658 const uint64_t one = 1;
1659 const int shift = type; // 0-based consecutive enum
1660 DCHECK_LE(kFieldWriteOffset, shift);
1661 DCHECK_LT(shift, kArrayWriteOffset);
1662 return one << (type + offset);
1663 }
1664
1665 // Private constructor on direct flags value.
1666 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1667
1668 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001669};
1670
David Brazdiled596192015-01-23 10:39:45 +00001671// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001672class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001673 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001674 HEnvironment(ArenaAllocator* arena,
1675 size_t number_of_vregs,
1676 const DexFile& dex_file,
1677 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001678 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001679 InvokeType invoke_type,
1680 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001681 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1682 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001683 parent_(nullptr),
1684 dex_file_(dex_file),
1685 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001686 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001687 invoke_type_(invoke_type),
1688 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001689 }
1690
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001691 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001692 : HEnvironment(arena,
1693 to_copy.Size(),
1694 to_copy.GetDexFile(),
1695 to_copy.GetMethodIdx(),
1696 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001697 to_copy.GetInvokeType(),
1698 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001699
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001700 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001701 if (parent_ != nullptr) {
1702 parent_->SetAndCopyParentChain(allocator, parent);
1703 } else {
1704 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1705 parent_->CopyFrom(parent);
1706 if (parent->GetParent() != nullptr) {
1707 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1708 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001709 }
David Brazdiled596192015-01-23 10:39:45 +00001710 }
1711
Vladimir Marko71bf8092015-09-15 15:33:14 +01001712 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001713 void CopyFrom(HEnvironment* environment);
1714
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001715 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1716 // input to the loop phi instead. This is for inserting instructions that
1717 // require an environment (like HDeoptimization) in the loop pre-header.
1718 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001719
1720 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001721 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001722 }
1723
1724 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001725 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001726 }
1727
David Brazdil1abb4192015-02-17 18:33:36 +00001728 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001729
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001730 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001731
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001732 HEnvironment* GetParent() const { return parent_; }
1733
1734 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001735 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001736 }
1737
1738 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001739 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001740 }
1741
1742 uint32_t GetDexPc() const {
1743 return dex_pc_;
1744 }
1745
1746 uint32_t GetMethodIdx() const {
1747 return method_idx_;
1748 }
1749
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001750 InvokeType GetInvokeType() const {
1751 return invoke_type_;
1752 }
1753
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001754 const DexFile& GetDexFile() const {
1755 return dex_file_;
1756 }
1757
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001758 HInstruction* GetHolder() const {
1759 return holder_;
1760 }
1761
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001762
1763 bool IsFromInlinedInvoke() const {
1764 return GetParent() != nullptr;
1765 }
1766
David Brazdiled596192015-01-23 10:39:45 +00001767 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001768 // Record instructions' use entries of this environment for constant-time removal.
1769 // It should only be called by HInstruction when a new environment use is added.
1770 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1771 DCHECK(env_use->GetUser() == this);
1772 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001773 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001774 }
David Brazdiled596192015-01-23 10:39:45 +00001775
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001776 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1777 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001778 HEnvironment* parent_;
1779 const DexFile& dex_file_;
1780 const uint32_t method_idx_;
1781 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001782 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001783
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001784 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001785 HInstruction* const holder_;
1786
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001787 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001788
1789 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1790};
1791
Vladimir Markof9f64412015-09-02 14:05:49 +01001792class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001793 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001794 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795 : previous_(nullptr),
1796 next_(nullptr),
1797 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001798 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001800 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001801 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001802 locations_(nullptr),
1803 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001804 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001805 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001806 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807
Dave Allison20dfc792014-06-16 20:44:29 -07001808 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001809
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001810#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001811 enum InstructionKind {
1812 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1813 };
1814#undef DECLARE_KIND
1815
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001816 HInstruction* GetNext() const { return next_; }
1817 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001818
Calin Juravle77520bc2015-01-12 18:45:46 +00001819 HInstruction* GetNextDisregardingMoves() const;
1820 HInstruction* GetPreviousDisregardingMoves() const;
1821
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001822 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001823 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001824 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001825 bool IsInBlock() const { return block_ != nullptr; }
1826 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001827 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001828
Roland Levillain6b879dd2014-09-22 17:13:44 +01001829 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001830 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001831
1832 virtual void Accept(HGraphVisitor* visitor) = 0;
1833 virtual const char* DebugName() const = 0;
1834
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001835 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001836 void SetRawInputAt(size_t index, HInstruction* input) {
1837 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1838 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001840 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001841
1842 uint32_t GetDexPc() const { return dex_pc_; }
1843
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001844 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001845
Roland Levillaine161a2a2014-10-03 12:45:18 +01001846 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001847 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001848
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001849 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001850 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851
Calin Juravle10e244f2015-01-26 18:54:32 +00001852 // Does not apply for all instructions, but having this at top level greatly
1853 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001854 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001855 virtual bool CanBeNull() const {
1856 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1857 return true;
1858 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001859
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001860 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001861 return false;
1862 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001863
Calin Juravle2e768302015-07-28 14:41:11 +00001864 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001865
Calin Juravle61d544b2015-02-23 16:46:57 +00001866 ReferenceTypeInfo GetReferenceTypeInfo() const {
1867 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1868 return reference_type_info_;
1869 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001870
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001872 DCHECK(user != nullptr);
1873 HUseListNode<HInstruction*>* use =
1874 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1875 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001876 }
1877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001879 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001880 HUseListNode<HEnvironment*>* env_use =
1881 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1882 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883 }
1884
David Brazdil1abb4192015-02-17 18:33:36 +00001885 void RemoveAsUserOfInput(size_t input) {
1886 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1887 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1888 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889
David Brazdil1abb4192015-02-17 18:33:36 +00001890 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1891 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001892
David Brazdiled596192015-01-23 10:39:45 +00001893 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1894 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001895 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001896 bool HasOnlyOneNonEnvironmentUse() const {
1897 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1898 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001899
Roland Levillain6c82d402014-10-13 16:10:27 +01001900 // Does this instruction strictly dominate `other_instruction`?
1901 // Returns false if this instruction and `other_instruction` are the same.
1902 // Aborts if this instruction and `other_instruction` are both phis.
1903 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001904
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001905 int GetId() const { return id_; }
1906 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001908 int GetSsaIndex() const { return ssa_index_; }
1909 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1910 bool HasSsaIndex() const { return ssa_index_ != -1; }
1911
1912 bool HasEnvironment() const { return environment_ != nullptr; }
1913 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001914 // Set the `environment_` field. Raw because this method does not
1915 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001916 void SetRawEnvironment(HEnvironment* environment) {
1917 DCHECK(environment_ == nullptr);
1918 DCHECK_EQ(environment->GetHolder(), this);
1919 environment_ = environment;
1920 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001921
1922 // Set the environment of this instruction, copying it from `environment`. While
1923 // copying, the uses lists are being updated.
1924 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001925 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001926 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001927 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001928 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001929 if (environment->GetParent() != nullptr) {
1930 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1931 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001932 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001933
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001934 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1935 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001936 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001937 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001939 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001940 if (environment->GetParent() != nullptr) {
1941 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1942 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001943 }
1944
Nicolas Geoffray39468442014-09-02 15:17:15 +01001945 // Returns the number of entries in the environment. Typically, that is the
1946 // number of dex registers in a method. It could be more in case of inlining.
1947 size_t EnvironmentSize() const;
1948
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001949 LocationSummary* GetLocations() const { return locations_; }
1950 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001951
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001952 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001953 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954
Alexandre Rames188d4312015-04-09 18:30:21 +01001955 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1956 // uses of this instruction by `other` are *not* updated.
1957 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1958 ReplaceWith(other);
1959 other->ReplaceInput(this, use_index);
1960 }
1961
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001962 // Move `this` instruction before `cursor`.
1963 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001964
Vladimir Markofb337ea2015-11-25 15:25:10 +00001965 // Move `this` before its first user and out of any loops. If there is no
1966 // out-of-loop user that dominates all other users, move the instruction
1967 // to the end of the out-of-loop common dominator of the user's blocks.
1968 //
1969 // This can be used only on non-throwing instructions with no side effects that
1970 // have at least one use but no environment uses.
1971 void MoveBeforeFirstUserAndOutOfLoops();
1972
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001973#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001974 bool Is##type() const { return (As##type() != nullptr); } \
1975 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001976 virtual H##type* As##type() { return nullptr; }
1977
1978 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1979#undef INSTRUCTION_TYPE_CHECK
1980
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 // Returns whether the instruction can be moved within the graph.
1982 virtual bool CanBeMoved() const { return false; }
1983
1984 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001985 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001986 return false;
1987 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001988
1989 // Returns whether any data encoded in the two instructions is equal.
1990 // This method does not look at the inputs. Both instructions must be
1991 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001992 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001993 return false;
1994 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995
1996 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001997 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001998 // 2) Their inputs are identical.
1999 bool Equals(HInstruction* other) const;
2000
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002001 virtual InstructionKind GetKind() const = 0;
2002
2003 virtual size_t ComputeHashCode() const {
2004 size_t result = GetKind();
2005 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2006 result = (result * 31) + InputAt(i)->GetId();
2007 }
2008 return result;
2009 }
2010
2011 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002012 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002013
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002014 size_t GetLifetimePosition() const { return lifetime_position_; }
2015 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2016 LiveInterval* GetLiveInterval() const { return live_interval_; }
2017 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2018 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2019
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002020 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2021
2022 // Returns whether the code generation of the instruction will require to have access
2023 // to the current method. Such instructions are:
2024 // (1): Instructions that require an environment, as calling the runtime requires
2025 // to walk the stack and have the current method stored at a specific stack address.
2026 // (2): Object literals like classes and strings, that are loaded from the dex cache
2027 // fields of the current method.
2028 bool NeedsCurrentMethod() const {
2029 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2030 }
2031
Vladimir Markodc151b22015-10-15 18:02:30 +01002032 // Returns whether the code generation of the instruction will require to have access
2033 // to the dex cache of the current method's declaring class via the current method.
2034 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002035
Mark Mendellc4701932015-04-10 13:18:51 -04002036 // Does this instruction have any use in an environment before
2037 // control flow hits 'other'?
2038 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2039
2040 // Remove all references to environment uses of this instruction.
2041 // The caller must ensure that this is safe to do.
2042 void RemoveEnvironmentUsers();
2043
David Brazdil1abb4192015-02-17 18:33:36 +00002044 protected:
2045 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2046 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2047
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002049 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2050
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051 HInstruction* previous_;
2052 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002053 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002054 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002056 // An instruction gets an id when it is added to the graph.
2057 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002058 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059 int id_;
2060
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002061 // When doing liveness analysis, instructions that have uses get an SSA index.
2062 int ssa_index_;
2063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002064 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002065 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066
2067 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002068 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069
Nicolas Geoffray39468442014-09-02 15:17:15 +01002070 // The environment associated with this instruction. Not null if the instruction
2071 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002073
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002074 // Set by the code generator.
2075 LocationSummary* locations_;
2076
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002077 // Set by the liveness analysis.
2078 LiveInterval* live_interval_;
2079
2080 // Set by the liveness analysis, this is the position in a linear
2081 // order of blocks where this instruction's live interval start.
2082 size_t lifetime_position_;
2083
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002084 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002085
Calin Juravleacf735c2015-02-12 15:25:22 +00002086 // TODO: for primitive types this should be marked as invalid.
2087 ReferenceTypeInfo reference_type_info_;
2088
David Brazdil1abb4192015-02-17 18:33:36 +00002089 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002091 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002092 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094
2095 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2096};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002097std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002099class HInputIterator : public ValueObject {
2100 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002101 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002102
2103 bool Done() const { return index_ == instruction_->InputCount(); }
2104 HInstruction* Current() const { return instruction_->InputAt(index_); }
2105 void Advance() { index_++; }
2106
2107 private:
2108 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002110
2111 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2112};
2113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114class HInstructionIterator : public ValueObject {
2115 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116 explicit HInstructionIterator(const HInstructionList& instructions)
2117 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002118 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119 }
2120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121 bool Done() const { return instruction_ == nullptr; }
2122 HInstruction* Current() const { return instruction_; }
2123 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002125 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126 }
2127
2128 private:
2129 HInstruction* instruction_;
2130 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002131
2132 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133};
2134
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002135class HBackwardInstructionIterator : public ValueObject {
2136 public:
2137 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2138 : instruction_(instructions.last_instruction_) {
2139 next_ = Done() ? nullptr : instruction_->GetPrevious();
2140 }
2141
2142 bool Done() const { return instruction_ == nullptr; }
2143 HInstruction* Current() const { return instruction_; }
2144 void Advance() {
2145 instruction_ = next_;
2146 next_ = Done() ? nullptr : instruction_->GetPrevious();
2147 }
2148
2149 private:
2150 HInstruction* instruction_;
2151 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002152
2153 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002154};
2155
Vladimir Markof9f64412015-09-02 14:05:49 +01002156template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157class HTemplateInstruction: public HInstruction {
2158 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002159 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002160 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002161 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002163 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002165 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002166 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2167 DCHECK_LT(i, N);
2168 return inputs_[i];
2169 }
David Brazdil1abb4192015-02-17 18:33:36 +00002170
2171 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002172 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002173 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174 }
2175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002177 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178
2179 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180};
2181
Vladimir Markof9f64412015-09-02 14:05:49 +01002182// HTemplateInstruction specialization for N=0.
2183template<>
2184class HTemplateInstruction<0>: public HInstruction {
2185 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002186 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002187 : HInstruction(side_effects, dex_pc) {}
2188
Vladimir Markof9f64412015-09-02 14:05:49 +01002189 virtual ~HTemplateInstruction() {}
2190
2191 size_t InputCount() const OVERRIDE { return 0; }
2192
2193 protected:
2194 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2195 LOG(FATAL) << "Unreachable";
2196 UNREACHABLE();
2197 }
2198
2199 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2200 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2201 LOG(FATAL) << "Unreachable";
2202 UNREACHABLE();
2203 }
2204
2205 private:
2206 friend class SsaBuilder;
2207};
2208
Dave Allison20dfc792014-06-16 20:44:29 -07002209template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002210class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002211 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002212 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002213 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002214 virtual ~HExpression() {}
2215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002216 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002217
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002218 protected:
2219 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002220};
2221
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002222// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2223// instruction that branches to the exit block.
2224class HReturnVoid : public HTemplateInstruction<0> {
2225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002226 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2227 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002228
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002229 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002231 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
2233 private:
2234 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2235};
2236
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237// Represents dex's RETURN opcodes. A HReturn is a control flow
2238// instruction that branches to the exit block.
2239class HReturn : public HTemplateInstruction<1> {
2240 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002241 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2242 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002243 SetRawInputAt(0, value);
2244 }
2245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002247
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002248 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002249
2250 private:
2251 DISALLOW_COPY_AND_ASSIGN(HReturn);
2252};
2253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002254// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002255// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256// exit block.
2257class HExit : public HTemplateInstruction<0> {
2258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002259 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002261 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002263 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002264
2265 private:
2266 DISALLOW_COPY_AND_ASSIGN(HExit);
2267};
2268
2269// Jumps from one block to another.
2270class HGoto : public HTemplateInstruction<0> {
2271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002272 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002273
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002274 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002276 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002277 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002278 }
2279
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002280 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281
2282 private:
2283 DISALLOW_COPY_AND_ASSIGN(HGoto);
2284};
2285
Roland Levillain9867bc72015-08-05 10:21:34 +01002286class HConstant : public HExpression<0> {
2287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2289 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002290
2291 bool CanBeMoved() const OVERRIDE { return true; }
2292
2293 virtual bool IsMinusOne() const { return false; }
2294 virtual bool IsZero() const { return false; }
2295 virtual bool IsOne() const { return false; }
2296
David Brazdil77a48ae2015-09-15 12:34:04 +00002297 virtual uint64_t GetValueAsUint64() const = 0;
2298
Roland Levillain9867bc72015-08-05 10:21:34 +01002299 DECLARE_INSTRUCTION(Constant);
2300
2301 private:
2302 DISALLOW_COPY_AND_ASSIGN(HConstant);
2303};
2304
2305class HNullConstant : public HConstant {
2306 public:
2307 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2308 return true;
2309 }
2310
David Brazdil77a48ae2015-09-15 12:34:04 +00002311 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2312
Roland Levillain9867bc72015-08-05 10:21:34 +01002313 size_t ComputeHashCode() const OVERRIDE { return 0; }
2314
2315 DECLARE_INSTRUCTION(NullConstant);
2316
2317 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002319
2320 friend class HGraph;
2321 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2322};
2323
2324// Constants of the type int. Those can be from Dex instructions, or
2325// synthesized (for example with the if-eqz instruction).
2326class HIntConstant : public HConstant {
2327 public:
2328 int32_t GetValue() const { return value_; }
2329
David Brazdil9f389d42015-10-01 14:32:56 +01002330 uint64_t GetValueAsUint64() const OVERRIDE {
2331 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2332 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002333
Roland Levillain9867bc72015-08-05 10:21:34 +01002334 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2335 DCHECK(other->IsIntConstant());
2336 return other->AsIntConstant()->value_ == value_;
2337 }
2338
2339 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2340
2341 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2342 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2343 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2344
2345 DECLARE_INSTRUCTION(IntConstant);
2346
2347 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002348 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2349 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2350 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2351 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002352
2353 const int32_t value_;
2354
2355 friend class HGraph;
2356 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2357 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2358 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2359};
2360
2361class HLongConstant : public HConstant {
2362 public:
2363 int64_t GetValue() const { return value_; }
2364
David Brazdil77a48ae2015-09-15 12:34:04 +00002365 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2366
Roland Levillain9867bc72015-08-05 10:21:34 +01002367 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2368 DCHECK(other->IsLongConstant());
2369 return other->AsLongConstant()->value_ == value_;
2370 }
2371
2372 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2373
2374 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2375 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2376 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2377
2378 DECLARE_INSTRUCTION(LongConstant);
2379
2380 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002381 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2382 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002383
2384 const int64_t value_;
2385
2386 friend class HGraph;
2387 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2388};
Dave Allison20dfc792014-06-16 20:44:29 -07002389
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002390// Conditional branch. A block ending with an HIf instruction must have
2391// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002392class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002393 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2395 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002396 SetRawInputAt(0, input);
2397 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002398
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002399 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002400
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002402 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002403 }
2404
2405 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002406 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002407 }
2408
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002409 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HIf);
2413};
2414
David Brazdilfc6a86a2015-06-26 10:33:45 +00002415
2416// Abstract instruction which marks the beginning and/or end of a try block and
2417// links it to the respective exception handlers. Behaves the same as a Goto in
2418// non-exceptional control flow.
2419// Normal-flow successor is stored at index zero, exception handlers under
2420// higher indices in no particular order.
2421class HTryBoundary : public HTemplateInstruction<0> {
2422 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002423 enum BoundaryKind {
2424 kEntry,
2425 kExit,
2426 };
2427
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002428 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2429 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002430
2431 bool IsControlFlow() const OVERRIDE { return true; }
2432
2433 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002434 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002435
David Brazdild26a4112015-11-10 11:07:31 +00002436 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2437 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2438 }
2439
David Brazdilfc6a86a2015-06-26 10:33:45 +00002440 // Returns whether `handler` is among its exception handlers (non-zero index
2441 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002442 bool HasExceptionHandler(const HBasicBlock& handler) const {
2443 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002444 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002445 }
2446
2447 // If not present already, adds `handler` to its block's list of exception
2448 // handlers.
2449 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002450 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002451 GetBlock()->AddSuccessor(handler);
2452 }
2453 }
2454
David Brazdil56e1acc2015-06-30 15:41:36 +01002455 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002456
David Brazdilffee3d32015-07-06 11:48:53 +01002457 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2458
David Brazdilfc6a86a2015-06-26 10:33:45 +00002459 DECLARE_INSTRUCTION(TryBoundary);
2460
2461 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002462 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002463
2464 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2465};
2466
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002467// Deoptimize to interpreter, upon checking a condition.
2468class HDeoptimize : public HTemplateInstruction<1> {
2469 public:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002470 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002472 SetRawInputAt(0, cond);
2473 }
2474
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002475 bool CanBeMoved() const OVERRIDE { return true; }
2476 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2477 return true;
2478 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002479 bool NeedsEnvironment() const OVERRIDE { return true; }
2480 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002481
2482 DECLARE_INSTRUCTION(Deoptimize);
2483
2484 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002485 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2486};
2487
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002488// Represents the ArtMethod that was passed as a first argument to
2489// the method. It is used by instructions that depend on it, like
2490// instructions that work with the dex cache.
2491class HCurrentMethod : public HExpression<0> {
2492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002493 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2494 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002495
2496 DECLARE_INSTRUCTION(CurrentMethod);
2497
2498 private:
2499 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2500};
2501
Mark Mendellfe57faa2015-09-18 09:26:15 -04002502// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2503// have one successor for each entry in the switch table, and the final successor
2504// will be the block containing the next Dex opcode.
2505class HPackedSwitch : public HTemplateInstruction<1> {
2506 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002507 HPackedSwitch(int32_t start_value,
2508 uint32_t num_entries,
2509 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002510 uint32_t dex_pc = kNoDexPc)
2511 : HTemplateInstruction(SideEffects::None(), dex_pc),
2512 start_value_(start_value),
2513 num_entries_(num_entries) {
2514 SetRawInputAt(0, input);
2515 }
2516
2517 bool IsControlFlow() const OVERRIDE { return true; }
2518
2519 int32_t GetStartValue() const { return start_value_; }
2520
Vladimir Marko211c2112015-09-24 16:52:33 +01002521 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002522
2523 HBasicBlock* GetDefaultBlock() const {
2524 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002525 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002526 }
2527 DECLARE_INSTRUCTION(PackedSwitch);
2528
2529 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002530 const int32_t start_value_;
2531 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002532
2533 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2534};
2535
Roland Levillain88cb1752014-10-20 16:36:47 +01002536class HUnaryOperation : public HExpression<1> {
2537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2539 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002540 SetRawInputAt(0, input);
2541 }
2542
2543 HInstruction* GetInput() const { return InputAt(0); }
2544 Primitive::Type GetResultType() const { return GetType(); }
2545
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002546 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002547 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002548 return true;
2549 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002550
Roland Levillain9240d6a2014-10-20 16:47:04 +01002551 // Try to statically evaluate `operation` and return a HConstant
2552 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002553 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002554 HConstant* TryStaticEvaluation() const;
2555
2556 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002557 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2558 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002559
Roland Levillain88cb1752014-10-20 16:36:47 +01002560 DECLARE_INSTRUCTION(UnaryOperation);
2561
2562 private:
2563 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2564};
2565
Dave Allison20dfc792014-06-16 20:44:29 -07002566class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002567 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002568 HBinaryOperation(Primitive::Type result_type,
2569 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002570 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002571 SideEffects side_effects = SideEffects::None(),
2572 uint32_t dex_pc = kNoDexPc)
2573 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002574 SetRawInputAt(0, left);
2575 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002576 }
2577
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002578 HInstruction* GetLeft() const { return InputAt(0); }
2579 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002580 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002581
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002582 virtual bool IsCommutative() const { return false; }
2583
2584 // Put constant on the right.
2585 // Returns whether order is changed.
2586 bool OrderInputsWithConstantOnTheRight() {
2587 HInstruction* left = InputAt(0);
2588 HInstruction* right = InputAt(1);
2589 if (left->IsConstant() && !right->IsConstant()) {
2590 ReplaceInput(right, 0);
2591 ReplaceInput(left, 1);
2592 return true;
2593 }
2594 return false;
2595 }
2596
2597 // Order inputs by instruction id, but favor constant on the right side.
2598 // This helps GVN for commutative ops.
2599 void OrderInputs() {
2600 DCHECK(IsCommutative());
2601 HInstruction* left = InputAt(0);
2602 HInstruction* right = InputAt(1);
2603 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2604 return;
2605 }
2606 if (OrderInputsWithConstantOnTheRight()) {
2607 return;
2608 }
2609 // Order according to instruction id.
2610 if (left->GetId() > right->GetId()) {
2611 ReplaceInput(right, 0);
2612 ReplaceInput(left, 1);
2613 }
2614 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002615
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002616 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002617 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002618 return true;
2619 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002620
Roland Levillain9240d6a2014-10-20 16:47:04 +01002621 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002622 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002623 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002624 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002625
2626 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002627 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2628 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2629 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2630 HLongConstant* y ATTRIBUTE_UNUSED) const {
2631 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2632 return nullptr;
2633 }
2634 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2635 HIntConstant* y ATTRIBUTE_UNUSED) const {
2636 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2637 return nullptr;
2638 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002639 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2640 HNullConstant* y ATTRIBUTE_UNUSED) const {
2641 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2642 return nullptr;
2643 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002644
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002645 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002646 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002647 HConstant* GetConstantRight() const;
2648
2649 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002650 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002651 HInstruction* GetLeastConstantLeft() const;
2652
Roland Levillainccc07a92014-09-16 14:48:16 +01002653 DECLARE_INSTRUCTION(BinaryOperation);
2654
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002655 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002656 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2657};
2658
Mark Mendellc4701932015-04-10 13:18:51 -04002659// The comparison bias applies for floating point operations and indicates how NaN
2660// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002661enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002662 kNoBias, // bias is not applicable (i.e. for long operation)
2663 kGtBias, // return 1 for NaN comparisons
2664 kLtBias, // return -1 for NaN comparisons
2665};
2666
Dave Allison20dfc792014-06-16 20:44:29 -07002667class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002669 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2670 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002671 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002672 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002673
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002674 bool NeedsMaterialization() const { return needs_materialization_; }
2675 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002676
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002677 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002678 // `instruction`, and disregard moves in between.
2679 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002680
Dave Allison20dfc792014-06-16 20:44:29 -07002681 DECLARE_INSTRUCTION(Condition);
2682
2683 virtual IfCondition GetCondition() const = 0;
2684
Mark Mendellc4701932015-04-10 13:18:51 -04002685 virtual IfCondition GetOppositeCondition() const = 0;
2686
Roland Levillain4fa13f62015-07-06 18:11:54 +01002687 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002688
2689 void SetBias(ComparisonBias bias) { bias_ = bias; }
2690
2691 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2692 return bias_ == other->AsCondition()->bias_;
2693 }
2694
Roland Levillain4fa13f62015-07-06 18:11:54 +01002695 bool IsFPConditionTrueIfNaN() const {
2696 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2697 IfCondition if_cond = GetCondition();
2698 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2699 }
2700
2701 bool IsFPConditionFalseIfNaN() const {
2702 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2703 IfCondition if_cond = GetCondition();
2704 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2705 }
2706
Dave Allison20dfc792014-06-16 20:44:29 -07002707 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002708 // For register allocation purposes, returns whether this instruction needs to be
2709 // materialized (that is, not just be in the processor flags).
2710 bool needs_materialization_;
2711
Mark Mendellc4701932015-04-10 13:18:51 -04002712 // Needed if we merge a HCompare into a HCondition.
2713 ComparisonBias bias_;
2714
Dave Allison20dfc792014-06-16 20:44:29 -07002715 DISALLOW_COPY_AND_ASSIGN(HCondition);
2716};
2717
2718// Instruction to check if two inputs are equal to each other.
2719class HEqual : public HCondition {
2720 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2722 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002723
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002724 bool IsCommutative() const OVERRIDE { return true; }
2725
Roland Levillain9867bc72015-08-05 10:21:34 +01002726 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 return GetBlock()->GetGraph()->GetIntConstant(
2728 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002729 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 return GetBlock()->GetGraph()->GetIntConstant(
2732 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002733 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002734 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2735 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002736 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002737 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002738
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002739 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002741 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002742 return kCondEQ;
2743 }
2744
Mark Mendellc4701932015-04-10 13:18:51 -04002745 IfCondition GetOppositeCondition() const OVERRIDE {
2746 return kCondNE;
2747 }
2748
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002749 private:
Aart Bike9f37602015-10-09 11:15:55 -07002750 template <typename T> bool Compute(T x, T y) const { return x == y; }
2751
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002752 DISALLOW_COPY_AND_ASSIGN(HEqual);
2753};
2754
Dave Allison20dfc792014-06-16 20:44:29 -07002755class HNotEqual : public HCondition {
2756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2758 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002759
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002760 bool IsCommutative() const OVERRIDE { return true; }
2761
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 return GetBlock()->GetGraph()->GetIntConstant(
2764 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002765 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002766 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002767 return GetBlock()->GetGraph()->GetIntConstant(
2768 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002769 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002770 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2771 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002772 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002773 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002774
Dave Allison20dfc792014-06-16 20:44:29 -07002775 DECLARE_INSTRUCTION(NotEqual);
2776
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002777 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002778 return kCondNE;
2779 }
2780
Mark Mendellc4701932015-04-10 13:18:51 -04002781 IfCondition GetOppositeCondition() const OVERRIDE {
2782 return kCondEQ;
2783 }
2784
Dave Allison20dfc792014-06-16 20:44:29 -07002785 private:
Aart Bike9f37602015-10-09 11:15:55 -07002786 template <typename T> bool Compute(T x, T y) const { return x != y; }
2787
Dave Allison20dfc792014-06-16 20:44:29 -07002788 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2789};
2790
2791class HLessThan : public HCondition {
2792 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2794 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002795
Roland Levillain9867bc72015-08-05 10:21:34 +01002796 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 return GetBlock()->GetGraph()->GetIntConstant(
2798 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002799 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002800 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002801 return GetBlock()->GetGraph()->GetIntConstant(
2802 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002803 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002804
Dave Allison20dfc792014-06-16 20:44:29 -07002805 DECLARE_INSTRUCTION(LessThan);
2806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002807 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002808 return kCondLT;
2809 }
2810
Mark Mendellc4701932015-04-10 13:18:51 -04002811 IfCondition GetOppositeCondition() const OVERRIDE {
2812 return kCondGE;
2813 }
2814
Dave Allison20dfc792014-06-16 20:44:29 -07002815 private:
Aart Bike9f37602015-10-09 11:15:55 -07002816 template <typename T> bool Compute(T x, T y) const { return x < y; }
2817
Dave Allison20dfc792014-06-16 20:44:29 -07002818 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2819};
2820
2821class HLessThanOrEqual : public HCondition {
2822 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002823 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2824 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002825
Roland Levillain9867bc72015-08-05 10:21:34 +01002826 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002827 return GetBlock()->GetGraph()->GetIntConstant(
2828 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002829 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002830 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002831 return GetBlock()->GetGraph()->GetIntConstant(
2832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002833 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002834
Dave Allison20dfc792014-06-16 20:44:29 -07002835 DECLARE_INSTRUCTION(LessThanOrEqual);
2836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002837 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002838 return kCondLE;
2839 }
2840
Mark Mendellc4701932015-04-10 13:18:51 -04002841 IfCondition GetOppositeCondition() const OVERRIDE {
2842 return kCondGT;
2843 }
2844
Dave Allison20dfc792014-06-16 20:44:29 -07002845 private:
Aart Bike9f37602015-10-09 11:15:55 -07002846 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2849};
2850
2851class HGreaterThan : public HCondition {
2852 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2854 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002855
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002857 return GetBlock()->GetGraph()->GetIntConstant(
2858 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002859 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002860 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 return GetBlock()->GetGraph()->GetIntConstant(
2862 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002863 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002864
Dave Allison20dfc792014-06-16 20:44:29 -07002865 DECLARE_INSTRUCTION(GreaterThan);
2866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002867 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002868 return kCondGT;
2869 }
2870
Mark Mendellc4701932015-04-10 13:18:51 -04002871 IfCondition GetOppositeCondition() const OVERRIDE {
2872 return kCondLE;
2873 }
2874
Dave Allison20dfc792014-06-16 20:44:29 -07002875 private:
Aart Bike9f37602015-10-09 11:15:55 -07002876 template <typename T> bool Compute(T x, T y) const { return x > y; }
2877
Dave Allison20dfc792014-06-16 20:44:29 -07002878 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2879};
2880
2881class HGreaterThanOrEqual : public HCondition {
2882 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002883 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2884 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002885
Roland Levillain9867bc72015-08-05 10:21:34 +01002886 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002887 return GetBlock()->GetGraph()->GetIntConstant(
2888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002889 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002890 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002891 return GetBlock()->GetGraph()->GetIntConstant(
2892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002893 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002894
Dave Allison20dfc792014-06-16 20:44:29 -07002895 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002897 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002898 return kCondGE;
2899 }
2900
Mark Mendellc4701932015-04-10 13:18:51 -04002901 IfCondition GetOppositeCondition() const OVERRIDE {
2902 return kCondLT;
2903 }
2904
Dave Allison20dfc792014-06-16 20:44:29 -07002905 private:
Aart Bike9f37602015-10-09 11:15:55 -07002906 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2907
Dave Allison20dfc792014-06-16 20:44:29 -07002908 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2909};
2910
Aart Bike9f37602015-10-09 11:15:55 -07002911class HBelow : public HCondition {
2912 public:
2913 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2914 : HCondition(first, second, dex_pc) {}
2915
2916 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2917 return GetBlock()->GetGraph()->GetIntConstant(
2918 Compute(static_cast<uint32_t>(x->GetValue()),
2919 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2920 }
2921 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2922 return GetBlock()->GetGraph()->GetIntConstant(
2923 Compute(static_cast<uint64_t>(x->GetValue()),
2924 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2925 }
2926
2927 DECLARE_INSTRUCTION(Below);
2928
2929 IfCondition GetCondition() const OVERRIDE {
2930 return kCondB;
2931 }
2932
2933 IfCondition GetOppositeCondition() const OVERRIDE {
2934 return kCondAE;
2935 }
2936
2937 private:
2938 template <typename T> bool Compute(T x, T y) const { return x < y; }
2939
2940 DISALLOW_COPY_AND_ASSIGN(HBelow);
2941};
2942
2943class HBelowOrEqual : public HCondition {
2944 public:
2945 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2946 : HCondition(first, second, dex_pc) {}
2947
2948 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2949 return GetBlock()->GetGraph()->GetIntConstant(
2950 Compute(static_cast<uint32_t>(x->GetValue()),
2951 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2952 }
2953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2954 return GetBlock()->GetGraph()->GetIntConstant(
2955 Compute(static_cast<uint64_t>(x->GetValue()),
2956 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2957 }
2958
2959 DECLARE_INSTRUCTION(BelowOrEqual);
2960
2961 IfCondition GetCondition() const OVERRIDE {
2962 return kCondBE;
2963 }
2964
2965 IfCondition GetOppositeCondition() const OVERRIDE {
2966 return kCondA;
2967 }
2968
2969 private:
2970 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2971
2972 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2973};
2974
2975class HAbove : public HCondition {
2976 public:
2977 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2978 : HCondition(first, second, dex_pc) {}
2979
2980 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2981 return GetBlock()->GetGraph()->GetIntConstant(
2982 Compute(static_cast<uint32_t>(x->GetValue()),
2983 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2984 }
2985 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2986 return GetBlock()->GetGraph()->GetIntConstant(
2987 Compute(static_cast<uint64_t>(x->GetValue()),
2988 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2989 }
2990
2991 DECLARE_INSTRUCTION(Above);
2992
2993 IfCondition GetCondition() const OVERRIDE {
2994 return kCondA;
2995 }
2996
2997 IfCondition GetOppositeCondition() const OVERRIDE {
2998 return kCondBE;
2999 }
3000
3001 private:
3002 template <typename T> bool Compute(T x, T y) const { return x > y; }
3003
3004 DISALLOW_COPY_AND_ASSIGN(HAbove);
3005};
3006
3007class HAboveOrEqual : public HCondition {
3008 public:
3009 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3010 : HCondition(first, second, dex_pc) {}
3011
3012 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3013 return GetBlock()->GetGraph()->GetIntConstant(
3014 Compute(static_cast<uint32_t>(x->GetValue()),
3015 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3016 }
3017 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3018 return GetBlock()->GetGraph()->GetIntConstant(
3019 Compute(static_cast<uint64_t>(x->GetValue()),
3020 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3021 }
3022
3023 DECLARE_INSTRUCTION(AboveOrEqual);
3024
3025 IfCondition GetCondition() const OVERRIDE {
3026 return kCondAE;
3027 }
3028
3029 IfCondition GetOppositeCondition() const OVERRIDE {
3030 return kCondB;
3031 }
3032
3033 private:
3034 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3035
3036 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3037};
Dave Allison20dfc792014-06-16 20:44:29 -07003038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003039// Instruction to check how two inputs compare to each other.
3040// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3041class HCompare : public HBinaryOperation {
3042 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003043 HCompare(Primitive::Type type,
3044 HInstruction* first,
3045 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003046 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003047 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003048 : HBinaryOperation(Primitive::kPrimInt,
3049 first,
3050 second,
3051 SideEffectsForArchRuntimeCalls(type),
3052 dex_pc),
3053 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003054 DCHECK_EQ(type, first->GetType());
3055 DCHECK_EQ(type, second->GetType());
3056 }
3057
Roland Levillain9867bc72015-08-05 10:21:34 +01003058 template <typename T>
3059 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003060
Roland Levillain9867bc72015-08-05 10:21:34 +01003061 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003062 return GetBlock()->GetGraph()->GetIntConstant(
3063 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003064 }
3065 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003066 return GetBlock()->GetGraph()->GetIntConstant(
3067 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003068 }
3069
Calin Juravleddb7df22014-11-25 20:56:51 +00003070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3071 return bias_ == other->AsCompare()->bias_;
3072 }
3073
Mark Mendellc4701932015-04-10 13:18:51 -04003074 ComparisonBias GetBias() const { return bias_; }
3075
Roland Levillain4fa13f62015-07-06 18:11:54 +01003076 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003077
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003078
3079 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3080 // MIPS64 uses a runtime call for FP comparisons.
3081 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3082 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003083
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003084 DECLARE_INSTRUCTION(Compare);
3085
3086 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003087 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003088
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003089 DISALLOW_COPY_AND_ASSIGN(HCompare);
3090};
3091
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003092// A local in the graph. Corresponds to a Dex register.
3093class HLocal : public HTemplateInstruction<0> {
3094 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003095 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003097
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003098 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003099
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003100 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003101
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003102 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003103 // The Dex register number.
3104 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003105
3106 DISALLOW_COPY_AND_ASSIGN(HLocal);
3107};
3108
3109// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003110class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003111 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003112 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3113 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003114 SetRawInputAt(0, local);
3115 }
3116
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003117 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003119 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003120
3121 private:
3122 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3123};
3124
3125// Store a value in a given local. This instruction has two inputs: the value
3126// and the local.
3127class HStoreLocal : public HTemplateInstruction<2> {
3128 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003129 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3130 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003131 SetRawInputAt(0, local);
3132 SetRawInputAt(1, value);
3133 }
3134
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003135 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003137 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138
3139 private:
3140 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3141};
3142
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143class HFloatConstant : public HConstant {
3144 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 float GetValue() const { return value_; }
3146
David Brazdil77a48ae2015-09-15 12:34:04 +00003147 uint64_t GetValueAsUint64() const OVERRIDE {
3148 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3149 }
3150
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003151 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003152 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003153 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003154 }
3155
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003156 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003157
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003158 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003159 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003160 }
3161 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003162 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003163 }
3164 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003165 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3166 }
3167 bool IsNaN() const {
3168 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003169 }
3170
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003171 DECLARE_INSTRUCTION(FloatConstant);
3172
3173 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003174 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3175 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3176 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3177 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003178
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003179 const float value_;
3180
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003181 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003182 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003183 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003184 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3185};
3186
3187class HDoubleConstant : public HConstant {
3188 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003189 double GetValue() const { return value_; }
3190
David Brazdil77a48ae2015-09-15 12:34:04 +00003191 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003193 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003194 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003195 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003196 }
3197
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003198 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003199
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003200 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003201 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003202 }
3203 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003204 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003205 }
3206 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003207 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3208 }
3209 bool IsNaN() const {
3210 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003211 }
3212
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003213 DECLARE_INSTRUCTION(DoubleConstant);
3214
3215 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003216 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3217 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3218 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3219 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003220
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003221 const double value_;
3222
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003223 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003224 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003225 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003226 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3227};
3228
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003229enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003230#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003231#include "intrinsics_list.h"
3232 kNone,
3233 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3234#undef INTRINSICS_LIST
3235#undef OPTIMIZING_INTRINSICS
3236};
3237std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3238
Agi Csaki05f20562015-08-19 14:58:14 -07003239enum IntrinsicNeedsEnvironmentOrCache {
3240 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3241 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003242};
3243
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003244class HInvoke : public HInstruction {
3245 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003246 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003247
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003248 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003249
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003250 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003251 SetRawInputAt(index, argument);
3252 }
3253
Roland Levillain3e3d7332015-04-28 11:00:54 +01003254 // Return the number of arguments. This number can be lower than
3255 // the number of inputs returned by InputCount(), as some invoke
3256 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3257 // inputs at the end of their list of inputs.
3258 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003260 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003261
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003262
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003263 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003264 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003265
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003266 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3267
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003268 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 return intrinsic_;
3270 }
3271
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003272 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003273
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003274 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003275 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003276 }
3277
3278 bool CanThrow() const OVERRIDE { return true; }
3279
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003280 uint32_t* GetIntrinsicOptimizations() {
3281 return &intrinsic_optimizations_;
3282 }
3283
3284 const uint32_t* GetIntrinsicOptimizations() const {
3285 return &intrinsic_optimizations_;
3286 }
3287
3288 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3289
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003290 DECLARE_INSTRUCTION(Invoke);
3291
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003292 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003293 HInvoke(ArenaAllocator* arena,
3294 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003295 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003296 Primitive::Type return_type,
3297 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003298 uint32_t dex_method_index,
3299 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003300 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003301 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003302 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003303 inputs_(number_of_arguments + number_of_other_inputs,
3304 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003305 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003306 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003307 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003308 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003309 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003310 }
3311
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003312 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003313 return inputs_[index];
3314 }
3315
David Brazdil1abb4192015-02-17 18:33:36 +00003316 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003317 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003318 }
3319
Roland Levillain3e3d7332015-04-28 11:00:54 +01003320 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003321 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003322 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003323 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003324 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003325 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003326
3327 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3328 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003329
3330 private:
3331 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3332};
3333
Calin Juravle175dc732015-08-25 15:42:32 +01003334class HInvokeUnresolved : public HInvoke {
3335 public:
3336 HInvokeUnresolved(ArenaAllocator* arena,
3337 uint32_t number_of_arguments,
3338 Primitive::Type return_type,
3339 uint32_t dex_pc,
3340 uint32_t dex_method_index,
3341 InvokeType invoke_type)
3342 : HInvoke(arena,
3343 number_of_arguments,
3344 0u /* number_of_other_inputs */,
3345 return_type,
3346 dex_pc,
3347 dex_method_index,
3348 invoke_type) {
3349 }
3350
3351 DECLARE_INSTRUCTION(InvokeUnresolved);
3352
3353 private:
3354 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3355};
3356
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003357class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003358 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003359 // Requirements of this method call regarding the class
3360 // initialization (clinit) check of its declaring class.
3361 enum class ClinitCheckRequirement {
3362 kNone, // Class already initialized.
3363 kExplicit, // Static call having explicit clinit check as last input.
3364 kImplicit, // Static call implicitly requiring a clinit check.
3365 };
3366
Vladimir Marko58155012015-08-19 12:49:41 +00003367 // Determines how to load the target ArtMethod*.
3368 enum class MethodLoadKind {
3369 // Use a String init ArtMethod* loaded from Thread entrypoints.
3370 kStringInit,
3371
3372 // Use the method's own ArtMethod* loaded by the register allocator.
3373 kRecursive,
3374
3375 // Use ArtMethod* at a known address, embed the direct address in the code.
3376 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3377 kDirectAddress,
3378
3379 // Use ArtMethod* at an address that will be known at link time, embed the direct
3380 // address in the code. If the image is relocatable, emit .patch_oat entry.
3381 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3382 // the image relocatable or not.
3383 kDirectAddressWithFixup,
3384
3385 // Load from resoved methods array in the dex cache using a PC-relative load.
3386 // Used when we need to use the dex cache, for example for invoke-static that
3387 // may cause class initialization (the entry may point to a resolution method),
3388 // and we know that we can access the dex cache arrays using a PC-relative load.
3389 kDexCachePcRelative,
3390
3391 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3392 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3393 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3394 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3395 kDexCacheViaMethod,
3396 };
3397
3398 // Determines the location of the code pointer.
3399 enum class CodePtrLocation {
3400 // Recursive call, use local PC-relative call instruction.
3401 kCallSelf,
3402
3403 // Use PC-relative call instruction patched at link time.
3404 // Used for calls within an oat file, boot->boot or app->app.
3405 kCallPCRelative,
3406
3407 // Call to a known target address, embed the direct address in code.
3408 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3409 kCallDirect,
3410
3411 // Call to a target address that will be known at link time, embed the direct
3412 // address in code. If the image is relocatable, emit .patch_oat entry.
3413 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3414 // the image relocatable or not.
3415 kCallDirectWithFixup,
3416
3417 // Use code pointer from the ArtMethod*.
3418 // Used when we don't know the target code. This is also the last-resort-kind used when
3419 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3420 kCallArtMethod,
3421 };
3422
3423 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003424 MethodLoadKind method_load_kind;
3425 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003426 // The method load data holds
3427 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3428 // Note that there are multiple string init methods, each having its own offset.
3429 // - the method address for kDirectAddress
3430 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003431 uint64_t method_load_data;
3432 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003433 };
3434
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003435 HInvokeStaticOrDirect(ArenaAllocator* arena,
3436 uint32_t number_of_arguments,
3437 Primitive::Type return_type,
3438 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003439 uint32_t method_index,
3440 MethodReference target_method,
3441 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003442 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003443 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003444 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003445 : HInvoke(arena,
3446 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003447 // There is potentially one extra argument for the HCurrentMethod node, and
3448 // potentially one other if the clinit check is explicit, and potentially
3449 // one other if the method is a string factory.
3450 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3451 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3452 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003453 return_type,
3454 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003455 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003456 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003457 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003458 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003459 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003460 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003461
Vladimir Markodc151b22015-10-15 18:02:30 +01003462 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003463 bool had_current_method_input = HasCurrentMethodInput();
3464 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3465
3466 // Using the current method is the default and once we find a better
3467 // method load kind, we should not go back to using the current method.
3468 DCHECK(had_current_method_input || !needs_current_method_input);
3469
3470 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003471 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3472 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003473 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003474 dispatch_info_ = dispatch_info;
3475 }
3476
Vladimir Markoc53c0792015-11-19 15:48:33 +00003477 void AddSpecialInput(HInstruction* input) {
3478 // We allow only one special input.
3479 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3480 DCHECK(InputCount() == GetSpecialInputIndex() ||
3481 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3482 InsertInputAt(GetSpecialInputIndex(), input);
3483 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003484
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003485 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003486 // We access the method via the dex cache so we can't do an implicit null check.
3487 // TODO: for intrinsics we can generate implicit null checks.
3488 return false;
3489 }
3490
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003491 bool CanBeNull() const OVERRIDE {
3492 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3493 }
3494
Vladimir Markoc53c0792015-11-19 15:48:33 +00003495 // Get the index of the special input, if any.
3496 //
3497 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3498 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3499 // the "special input" is the current method pointer; otherwise there may be one
3500 // platform-specific special input, such as PC-relative addressing base.
3501 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3502
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003503 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3504 void SetOptimizedInvokeType(InvokeType invoke_type) {
3505 optimized_invoke_type_ = invoke_type;
3506 }
3507
Vladimir Marko58155012015-08-19 12:49:41 +00003508 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3509 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3510 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003511 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003512 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003513 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003514 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003515 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3516 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003517 bool HasCurrentMethodInput() const {
3518 // This function can be called only after the invoke has been fully initialized by the builder.
3519 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003520 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003521 return true;
3522 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003523 DCHECK(InputCount() == GetSpecialInputIndex() ||
3524 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003525 return false;
3526 }
3527 }
Vladimir Marko58155012015-08-19 12:49:41 +00003528 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3529 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003530 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003531
3532 int32_t GetStringInitOffset() const {
3533 DCHECK(IsStringInit());
3534 return dispatch_info_.method_load_data;
3535 }
3536
3537 uint64_t GetMethodAddress() const {
3538 DCHECK(HasMethodAddress());
3539 return dispatch_info_.method_load_data;
3540 }
3541
3542 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003543 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003544 return dispatch_info_.method_load_data;
3545 }
3546
3547 uint64_t GetDirectCodePtr() const {
3548 DCHECK(HasDirectCodePtr());
3549 return dispatch_info_.direct_code_ptr;
3550 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003551
Calin Juravle68ad6492015-08-18 17:08:12 +01003552 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3553
Roland Levillain4c0eb422015-04-24 16:43:49 +01003554 // Is this instruction a call to a static method?
3555 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003556 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003557 }
3558
Vladimir Markofbb184a2015-11-13 14:47:00 +00003559 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3560 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3561 // instruction; only relevant for static calls with explicit clinit check.
3562 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003563 DCHECK(IsStaticWithExplicitClinitCheck());
3564 size_t last_input_index = InputCount() - 1;
3565 HInstruction* last_input = InputAt(last_input_index);
3566 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003567 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003568 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003569 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003570 clinit_check_requirement_ = new_requirement;
3571 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003572 }
3573
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003574 bool IsStringFactoryFor(HFakeString* str) const {
3575 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003576 DCHECK(!HasCurrentMethodInput());
3577 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003578 return InputAt(InputCount() - 1)->AsFakeString() == str;
3579 }
3580
3581 void RemoveFakeStringArgumentAsLastInput() {
3582 DCHECK(IsStringInit());
3583 size_t last_input_index = InputCount() - 1;
3584 HInstruction* last_input = InputAt(last_input_index);
3585 DCHECK(last_input != nullptr);
3586 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3587 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003588 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003589 }
3590
Roland Levillain4c0eb422015-04-24 16:43:49 +01003591 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003592 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003593 bool IsStaticWithExplicitClinitCheck() const {
3594 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3595 }
3596
3597 // Is this a call to a static method whose declaring class has an
3598 // implicit intialization check requirement?
3599 bool IsStaticWithImplicitClinitCheck() const {
3600 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3601 }
3602
Vladimir Markob554b5a2015-11-06 12:57:55 +00003603 // Does this method load kind need the current method as an input?
3604 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3605 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3606 }
3607
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003608 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003609
Roland Levillain4c0eb422015-04-24 16:43:49 +01003610 protected:
3611 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3612 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3613 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3614 HInstruction* input = input_record.GetInstruction();
3615 // `input` is the last input of a static invoke marked as having
3616 // an explicit clinit check. It must either be:
3617 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3618 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3619 DCHECK(input != nullptr);
3620 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3621 }
3622 return input_record;
3623 }
3624
Vladimir Markoc53c0792015-11-19 15:48:33 +00003625 void InsertInputAt(size_t index, HInstruction* input);
3626 void RemoveInputAt(size_t index);
3627
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003628 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003629 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003630 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003631 // The target method may refer to different dex file or method index than the original
3632 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3633 // in derived class to increase visibility.
3634 MethodReference target_method_;
3635 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003636
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003637 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003638};
Vladimir Markof64242a2015-12-01 14:58:23 +00003639std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003640std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003641
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003642class HInvokeVirtual : public HInvoke {
3643 public:
3644 HInvokeVirtual(ArenaAllocator* arena,
3645 uint32_t number_of_arguments,
3646 Primitive::Type return_type,
3647 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003648 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003649 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003650 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003651 vtable_index_(vtable_index) {}
3652
Calin Juravle641547a2015-04-21 22:08:51 +01003653 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003654 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003655 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003656 }
3657
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003658 uint32_t GetVTableIndex() const { return vtable_index_; }
3659
3660 DECLARE_INSTRUCTION(InvokeVirtual);
3661
3662 private:
3663 const uint32_t vtable_index_;
3664
3665 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3666};
3667
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003668class HInvokeInterface : public HInvoke {
3669 public:
3670 HInvokeInterface(ArenaAllocator* arena,
3671 uint32_t number_of_arguments,
3672 Primitive::Type return_type,
3673 uint32_t dex_pc,
3674 uint32_t dex_method_index,
3675 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003676 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003677 imt_index_(imt_index) {}
3678
Calin Juravle641547a2015-04-21 22:08:51 +01003679 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003680 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003681 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003682 }
3683
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003684 uint32_t GetImtIndex() const { return imt_index_; }
3685 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3686
3687 DECLARE_INSTRUCTION(InvokeInterface);
3688
3689 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003690 const uint32_t imt_index_;
3691
3692 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3693};
3694
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003695class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003696 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003697 HNewInstance(HInstruction* cls,
3698 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003699 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003700 uint16_t type_index,
3701 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003702 bool can_throw,
3703 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003704 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003705 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003706 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003707 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003708 can_throw_(can_throw),
3709 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003710 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003711 SetRawInputAt(0, cls);
3712 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003713 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003714
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003715 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003716 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003718 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003719 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003720
3721 // It may throw when called on type that's not instantiable/accessible.
3722 // It can throw OOME.
3723 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3724 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3725
3726 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003727
Calin Juravle10e244f2015-01-26 18:54:32 +00003728 bool CanBeNull() const OVERRIDE { return false; }
3729
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003730 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3731
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003732 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3733 entrypoint_ = entrypoint;
3734 }
3735
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003736 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003737
3738 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003739 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003740 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003741 const bool can_throw_;
3742 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003743 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003744
3745 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3746};
3747
Roland Levillain88cb1752014-10-20 16:36:47 +01003748class HNeg : public HUnaryOperation {
3749 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003750 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3751 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003752
Roland Levillain9867bc72015-08-05 10:21:34 +01003753 template <typename T> T Compute(T x) const { return -x; }
3754
3755 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003756 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003757 }
3758 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003759 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003761
Roland Levillain88cb1752014-10-20 16:36:47 +01003762 DECLARE_INSTRUCTION(Neg);
3763
3764 private:
3765 DISALLOW_COPY_AND_ASSIGN(HNeg);
3766};
3767
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003768class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003769 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003770 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003771 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003772 uint32_t dex_pc,
3773 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003774 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003775 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003776 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003777 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003778 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003779 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003780 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003781 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003782 }
3783
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003784 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003785 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003786
3787 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003788 bool NeedsEnvironment() const OVERRIDE { return true; }
3789
Mingyao Yang0c365e62015-03-31 15:09:29 -07003790 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3791 bool CanThrow() const OVERRIDE { return true; }
3792
Calin Juravle10e244f2015-01-26 18:54:32 +00003793 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003794
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003795 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3796
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003797 DECLARE_INSTRUCTION(NewArray);
3798
3799 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003800 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003801 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003802 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003803
3804 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3805};
3806
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003807class HAdd : public HBinaryOperation {
3808 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003809 HAdd(Primitive::Type result_type,
3810 HInstruction* left,
3811 HInstruction* right,
3812 uint32_t dex_pc = kNoDexPc)
3813 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003815 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003816
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 template <typename T> T Compute(T x, T y) const { return x + y; }
3818
3819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 return GetBlock()->GetGraph()->GetIntConstant(
3821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003822 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003823 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003824 return GetBlock()->GetGraph()->GetLongConstant(
3825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003826 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003827
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003828 DECLARE_INSTRUCTION(Add);
3829
3830 private:
3831 DISALLOW_COPY_AND_ASSIGN(HAdd);
3832};
3833
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003834class HSub : public HBinaryOperation {
3835 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003836 HSub(Primitive::Type result_type,
3837 HInstruction* left,
3838 HInstruction* right,
3839 uint32_t dex_pc = kNoDexPc)
3840 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003841
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 template <typename T> T Compute(T x, T y) const { return x - y; }
3843
3844 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003845 return GetBlock()->GetGraph()->GetIntConstant(
3846 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003847 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003848 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003849 return GetBlock()->GetGraph()->GetLongConstant(
3850 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003851 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003852
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003853 DECLARE_INSTRUCTION(Sub);
3854
3855 private:
3856 DISALLOW_COPY_AND_ASSIGN(HSub);
3857};
3858
Calin Juravle34bacdf2014-10-07 20:23:36 +01003859class HMul : public HBinaryOperation {
3860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 HMul(Primitive::Type result_type,
3862 HInstruction* left,
3863 HInstruction* right,
3864 uint32_t dex_pc = kNoDexPc)
3865 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003867 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003868
Roland Levillain9867bc72015-08-05 10:21:34 +01003869 template <typename T> T Compute(T x, T y) const { return x * y; }
3870
3871 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetIntConstant(
3873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003874 }
3875 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 return GetBlock()->GetGraph()->GetLongConstant(
3877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003879
3880 DECLARE_INSTRUCTION(Mul);
3881
3882 private:
3883 DISALLOW_COPY_AND_ASSIGN(HMul);
3884};
3885
Calin Juravle7c4954d2014-10-28 16:57:40 +00003886class HDiv : public HBinaryOperation {
3887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003888 HDiv(Primitive::Type result_type,
3889 HInstruction* left,
3890 HInstruction* right,
3891 uint32_t dex_pc)
3892 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003893
Roland Levillain9867bc72015-08-05 10:21:34 +01003894 template <typename T>
3895 T Compute(T x, T y) const {
3896 // Our graph structure ensures we never have 0 for `y` during
3897 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003898 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003899 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003900 return (y == -1) ? -x : x / y;
3901 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003902
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003904 return GetBlock()->GetGraph()->GetIntConstant(
3905 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003906 }
3907 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003908 return GetBlock()->GetGraph()->GetLongConstant(
3909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003910 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003911
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003912 static SideEffects SideEffectsForArchRuntimeCalls() {
3913 // The generated code can use a runtime call.
3914 return SideEffects::CanTriggerGC();
3915 }
3916
Calin Juravle7c4954d2014-10-28 16:57:40 +00003917 DECLARE_INSTRUCTION(Div);
3918
3919 private:
3920 DISALLOW_COPY_AND_ASSIGN(HDiv);
3921};
3922
Calin Juravlebacfec32014-11-14 15:54:36 +00003923class HRem : public HBinaryOperation {
3924 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003925 HRem(Primitive::Type result_type,
3926 HInstruction* left,
3927 HInstruction* right,
3928 uint32_t dex_pc)
3929 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003930
Roland Levillain9867bc72015-08-05 10:21:34 +01003931 template <typename T>
3932 T Compute(T x, T y) const {
3933 // Our graph structure ensures we never have 0 for `y` during
3934 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003935 DCHECK_NE(y, 0);
3936 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3937 return (y == -1) ? 0 : x % y;
3938 }
3939
Roland Levillain9867bc72015-08-05 10:21:34 +01003940 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003941 return GetBlock()->GetGraph()->GetIntConstant(
3942 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003943 }
3944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003945 return GetBlock()->GetGraph()->GetLongConstant(
3946 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003947 }
3948
Calin Juravlebacfec32014-11-14 15:54:36 +00003949
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003950 static SideEffects SideEffectsForArchRuntimeCalls() {
3951 return SideEffects::CanTriggerGC();
3952 }
3953
Calin Juravlebacfec32014-11-14 15:54:36 +00003954 DECLARE_INSTRUCTION(Rem);
3955
3956 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003957 DISALLOW_COPY_AND_ASSIGN(HRem);
3958};
3959
Calin Juravled0d48522014-11-04 16:40:20 +00003960class HDivZeroCheck : public HExpression<1> {
3961 public:
3962 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003964 SetRawInputAt(0, value);
3965 }
3966
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003967 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3968
Calin Juravled0d48522014-11-04 16:40:20 +00003969 bool CanBeMoved() const OVERRIDE { return true; }
3970
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003971 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003972 return true;
3973 }
3974
3975 bool NeedsEnvironment() const OVERRIDE { return true; }
3976 bool CanThrow() const OVERRIDE { return true; }
3977
Calin Juravled0d48522014-11-04 16:40:20 +00003978 DECLARE_INSTRUCTION(DivZeroCheck);
3979
3980 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003981 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3982};
3983
Calin Juravle9aec02f2014-11-18 23:06:35 +00003984class HShl : public HBinaryOperation {
3985 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 HShl(Primitive::Type result_type,
3987 HInstruction* left,
3988 HInstruction* right,
3989 uint32_t dex_pc = kNoDexPc)
3990 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003991
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 template <typename T, typename U, typename V>
3993 T Compute(T x, U y, V max_shift_value) const {
3994 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3995 "V is not the unsigned integer type corresponding to T");
3996 return x << (y & max_shift_value);
3997 }
3998
3999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4000 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004001 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 }
4003 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4004 // case is handled as `x << static_cast<int>(y)`.
4005 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4010 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004011 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004012 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004013
4014 DECLARE_INSTRUCTION(Shl);
4015
4016 private:
4017 DISALLOW_COPY_AND_ASSIGN(HShl);
4018};
4019
4020class HShr : public HBinaryOperation {
4021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 HShr(Primitive::Type result_type,
4023 HInstruction* left,
4024 HInstruction* right,
4025 uint32_t dex_pc = kNoDexPc)
4026 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004027
Roland Levillain9867bc72015-08-05 10:21:34 +01004028 template <typename T, typename U, typename V>
4029 T Compute(T x, U y, V max_shift_value) const {
4030 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4031 "V is not the unsigned integer type corresponding to T");
4032 return x >> (y & max_shift_value);
4033 }
4034
4035 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4036 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004037 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004038 }
4039 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4040 // case is handled as `x >> static_cast<int>(y)`.
4041 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4042 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004044 }
4045 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4046 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004048 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049
4050 DECLARE_INSTRUCTION(Shr);
4051
4052 private:
4053 DISALLOW_COPY_AND_ASSIGN(HShr);
4054};
4055
4056class HUShr : public HBinaryOperation {
4057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 HUShr(Primitive::Type result_type,
4059 HInstruction* left,
4060 HInstruction* right,
4061 uint32_t dex_pc = kNoDexPc)
4062 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004063
Roland Levillain9867bc72015-08-05 10:21:34 +01004064 template <typename T, typename U, typename V>
4065 T Compute(T x, U y, V max_shift_value) const {
4066 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4067 "V is not the unsigned integer type corresponding to T");
4068 V ux = static_cast<V>(x);
4069 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004070 }
4071
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4073 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004075 }
4076 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4077 // case is handled as `x >>> static_cast<int>(y)`.
4078 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4079 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004081 }
4082 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4083 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004084 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004085 }
4086
4087 DECLARE_INSTRUCTION(UShr);
4088
4089 private:
4090 DISALLOW_COPY_AND_ASSIGN(HUShr);
4091};
4092
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004093class HAnd : public HBinaryOperation {
4094 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 HAnd(Primitive::Type result_type,
4096 HInstruction* left,
4097 HInstruction* right,
4098 uint32_t dex_pc = kNoDexPc)
4099 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004100
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004101 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004102
Roland Levillain9867bc72015-08-05 10:21:34 +01004103 template <typename T, typename U>
4104 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4105
4106 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004107 return GetBlock()->GetGraph()->GetIntConstant(
4108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 }
4110 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004111 return GetBlock()->GetGraph()->GetLongConstant(
4112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
4114 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 return GetBlock()->GetGraph()->GetLongConstant(
4116 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
4118 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 return GetBlock()->GetGraph()->GetLongConstant(
4120 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004122
4123 DECLARE_INSTRUCTION(And);
4124
4125 private:
4126 DISALLOW_COPY_AND_ASSIGN(HAnd);
4127};
4128
4129class HOr : public HBinaryOperation {
4130 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 HOr(Primitive::Type result_type,
4132 HInstruction* left,
4133 HInstruction* right,
4134 uint32_t dex_pc = kNoDexPc)
4135 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004136
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004137 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004138
Roland Levillain9867bc72015-08-05 10:21:34 +01004139 template <typename T, typename U>
4140 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4141
4142 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 return GetBlock()->GetGraph()->GetIntConstant(
4144 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004145 }
4146 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 return GetBlock()->GetGraph()->GetLongConstant(
4148 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004149 }
4150 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 return GetBlock()->GetGraph()->GetLongConstant(
4152 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 }
4154 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004155 return GetBlock()->GetGraph()->GetLongConstant(
4156 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004158
4159 DECLARE_INSTRUCTION(Or);
4160
4161 private:
4162 DISALLOW_COPY_AND_ASSIGN(HOr);
4163};
4164
4165class HXor : public HBinaryOperation {
4166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 HXor(Primitive::Type result_type,
4168 HInstruction* left,
4169 HInstruction* right,
4170 uint32_t dex_pc = kNoDexPc)
4171 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004172
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004173 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004174
Roland Levillain9867bc72015-08-05 10:21:34 +01004175 template <typename T, typename U>
4176 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4177
4178 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004179 return GetBlock()->GetGraph()->GetIntConstant(
4180 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004181 }
4182 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004183 return GetBlock()->GetGraph()->GetLongConstant(
4184 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 }
4186 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 return GetBlock()->GetGraph()->GetLongConstant(
4188 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004189 }
4190 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004191 return GetBlock()->GetGraph()->GetLongConstant(
4192 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004193 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004194
4195 DECLARE_INSTRUCTION(Xor);
4196
4197 private:
4198 DISALLOW_COPY_AND_ASSIGN(HXor);
4199};
4200
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004201class HRor : public HBinaryOperation {
4202 public:
4203 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4204 : HBinaryOperation(result_type, value, distance) {}
4205
4206 template <typename T, typename U, typename V>
4207 T Compute(T x, U y, V max_shift_value) const {
4208 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4209 "V is not the unsigned integer type corresponding to T");
4210 V ux = static_cast<V>(x);
4211 if ((y & max_shift_value) == 0) {
4212 return static_cast<T>(ux);
4213 } else {
4214 const V reg_bits = sizeof(T) * 8;
4215 return static_cast<T>(ux >> (y & max_shift_value)) |
4216 (x << (reg_bits - (y & max_shift_value)));
4217 }
4218 }
4219
4220 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4221 return GetBlock()->GetGraph()->GetIntConstant(
4222 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4223 }
4224 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4225 return GetBlock()->GetGraph()->GetLongConstant(
4226 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4227 }
4228 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4229 return GetBlock()->GetGraph()->GetLongConstant(
4230 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4231 }
4232
4233 DECLARE_INSTRUCTION(Ror);
4234
4235 private:
4236 DISALLOW_COPY_AND_ASSIGN(HRor);
4237};
4238
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004239// The value of a parameter in this method. Its location depends on
4240// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004241class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004242 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004243 HParameterValue(const DexFile& dex_file,
4244 uint16_t type_index,
4245 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 Primitive::Type parameter_type,
4247 bool is_this = false)
4248 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004249 dex_file_(dex_file),
4250 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004251 index_(index),
4252 is_this_(is_this),
4253 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004254
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004255 const DexFile& GetDexFile() const { return dex_file_; }
4256 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004257 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004258 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004259
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004260 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4261 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004262
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004263 DECLARE_INSTRUCTION(ParameterValue);
4264
4265 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004266 const DexFile& dex_file_;
4267 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004268 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004269 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004270 const uint8_t index_;
4271
Calin Juravle10e244f2015-01-26 18:54:32 +00004272 // Whether or not the parameter value corresponds to 'this' argument.
4273 const bool is_this_;
4274
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004275 bool can_be_null_;
4276
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004277 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4278};
4279
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004280class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004281 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004282 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4283 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004285 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004286 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004287 return true;
4288 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004289
Roland Levillain9867bc72015-08-05 10:21:34 +01004290 template <typename T> T Compute(T x) const { return ~x; }
4291
4292 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004293 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004294 }
4295 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004297 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004298
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004299 DECLARE_INSTRUCTION(Not);
4300
4301 private:
4302 DISALLOW_COPY_AND_ASSIGN(HNot);
4303};
4304
David Brazdil66d126e2015-04-03 16:02:44 +01004305class HBooleanNot : public HUnaryOperation {
4306 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4308 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004309
4310 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004311 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004312 return true;
4313 }
4314
Roland Levillain9867bc72015-08-05 10:21:34 +01004315 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004316 DCHECK(IsUint<1>(x));
4317 return !x;
4318 }
4319
Roland Levillain9867bc72015-08-05 10:21:34 +01004320 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004321 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004322 }
4323 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4324 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004325 UNREACHABLE();
4326 }
4327
4328 DECLARE_INSTRUCTION(BooleanNot);
4329
4330 private:
4331 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4332};
4333
Roland Levillaindff1f282014-11-05 14:15:05 +00004334class HTypeConversion : public HExpression<1> {
4335 public:
4336 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004337 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004338 : HExpression(result_type,
4339 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4340 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004341 SetRawInputAt(0, input);
4342 DCHECK_NE(input->GetType(), result_type);
4343 }
4344
4345 HInstruction* GetInput() const { return InputAt(0); }
4346 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4347 Primitive::Type GetResultType() const { return GetType(); }
4348
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004349 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004350 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004351
Roland Levillaindff1f282014-11-05 14:15:05 +00004352 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004353 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004354
Mark Mendelle82549b2015-05-06 10:55:34 -04004355 // Try to statically evaluate the conversion and return a HConstant
4356 // containing the result. If the input cannot be converted, return nullptr.
4357 HConstant* TryStaticEvaluation() const;
4358
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004359 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4360 Primitive::Type result_type) {
4361 // Some architectures may not require the 'GC' side effects, but at this point
4362 // in the compilation process we do not know what architecture we will
4363 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004364 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4365 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004366 return SideEffects::CanTriggerGC();
4367 }
4368 return SideEffects::None();
4369 }
4370
Roland Levillaindff1f282014-11-05 14:15:05 +00004371 DECLARE_INSTRUCTION(TypeConversion);
4372
4373 private:
4374 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4375};
4376
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004377static constexpr uint32_t kNoRegNumber = -1;
4378
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004379class HPhi : public HInstruction {
4380 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004381 HPhi(ArenaAllocator* arena,
4382 uint32_t reg_number,
4383 size_t number_of_inputs,
4384 Primitive::Type type,
4385 uint32_t dex_pc = kNoDexPc)
4386 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004387 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004388 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004389 type_(ToPhiType(type)),
4390 // Phis are constructed live and marked dead if conflicting or unused.
4391 // Individual steps of SsaBuilder should assume that if a phi has been
4392 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4393 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004394 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004395 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004396 }
4397
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004398 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4399 static Primitive::Type ToPhiType(Primitive::Type type) {
4400 switch (type) {
4401 case Primitive::kPrimBoolean:
4402 case Primitive::kPrimByte:
4403 case Primitive::kPrimShort:
4404 case Primitive::kPrimChar:
4405 return Primitive::kPrimInt;
4406 default:
4407 return type;
4408 }
4409 }
4410
David Brazdilffee3d32015-07-06 11:48:53 +01004411 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4412
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004413 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004414
4415 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004416 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004417
Calin Juravle10e244f2015-01-26 18:54:32 +00004418 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdild9510df2015-11-04 23:30:22 +00004419 void SetType(Primitive::Type new_type) {
4420 // Make sure that only valid type changes occur. The following are allowed:
4421 // (1) int -> float/ref (primitive type propagation),
4422 // (2) long -> double (primitive type propagation).
4423 DCHECK(type_ == new_type ||
4424 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4425 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4426 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4427 type_ = new_type;
4428 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004429
Calin Juravle10e244f2015-01-26 18:54:32 +00004430 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4431 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4432
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004433 uint32_t GetRegNumber() const { return reg_number_; }
4434
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004435 void SetDead() { is_live_ = false; }
4436 void SetLive() { is_live_ = true; }
4437 bool IsDead() const { return !is_live_; }
4438 bool IsLive() const { return is_live_; }
4439
David Brazdil77a48ae2015-09-15 12:34:04 +00004440 bool IsVRegEquivalentOf(HInstruction* other) const {
4441 return other != nullptr
4442 && other->IsPhi()
4443 && other->AsPhi()->GetBlock() == GetBlock()
4444 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4445 }
4446
Calin Juravlea4f88312015-04-16 12:57:19 +01004447 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4448 // An equivalent phi is a phi having the same dex register and type.
4449 // It assumes that phis with the same dex register are adjacent.
4450 HPhi* GetNextEquivalentPhiWithSameType() {
4451 HInstruction* next = GetNext();
4452 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4453 if (next->GetType() == GetType()) {
4454 return next->AsPhi();
4455 }
4456 next = next->GetNext();
4457 }
4458 return nullptr;
4459 }
4460
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004461 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004462
David Brazdil1abb4192015-02-17 18:33:36 +00004463 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004464 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004465 return inputs_[index];
4466 }
David Brazdil1abb4192015-02-17 18:33:36 +00004467
4468 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004469 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004470 }
4471
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004472 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004473 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004474 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004475 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004476 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004477 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004478
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004479 DISALLOW_COPY_AND_ASSIGN(HPhi);
4480};
4481
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482class HNullCheck : public HExpression<1> {
4483 public:
4484 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004485 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004486 SetRawInputAt(0, value);
4487 }
4488
Calin Juravle10e244f2015-01-26 18:54:32 +00004489 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004490 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004491 return true;
4492 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004493
Calin Juravle10e244f2015-01-26 18:54:32 +00004494 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004495
Calin Juravle10e244f2015-01-26 18:54:32 +00004496 bool CanThrow() const OVERRIDE { return true; }
4497
4498 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004499
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004500
4501 DECLARE_INSTRUCTION(NullCheck);
4502
4503 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4505};
4506
4507class FieldInfo : public ValueObject {
4508 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004509 FieldInfo(MemberOffset field_offset,
4510 Primitive::Type field_type,
4511 bool is_volatile,
4512 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004513 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004514 const DexFile& dex_file,
4515 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004516 : field_offset_(field_offset),
4517 field_type_(field_type),
4518 is_volatile_(is_volatile),
4519 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004520 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004521 dex_file_(dex_file),
4522 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523
4524 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004525 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004526 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004527 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004528 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004529 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004530 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531
4532 private:
4533 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004534 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004535 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004536 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004537 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004538 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004539 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004540};
4541
4542class HInstanceFieldGet : public HExpression<1> {
4543 public:
4544 HInstanceFieldGet(HInstruction* value,
4545 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004546 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004547 bool is_volatile,
4548 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004549 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004550 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004551 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004552 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004553 : HExpression(field_type,
4554 SideEffects::FieldReadOfType(field_type, is_volatile),
4555 dex_pc),
4556 field_info_(field_offset,
4557 field_type,
4558 is_volatile,
4559 field_idx,
4560 declaring_class_def_index,
4561 dex_file,
4562 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563 SetRawInputAt(0, value);
4564 }
4565
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004566 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004567
4568 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4569 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4570 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004571 }
4572
Calin Juravle641547a2015-04-21 22:08:51 +01004573 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4574 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004575 }
4576
4577 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004578 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4579 }
4580
Calin Juravle52c48962014-12-16 17:02:57 +00004581 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004582 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004583 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004584 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004585
4586 DECLARE_INSTRUCTION(InstanceFieldGet);
4587
4588 private:
4589 const FieldInfo field_info_;
4590
4591 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4592};
4593
4594class HInstanceFieldSet : public HTemplateInstruction<2> {
4595 public:
4596 HInstanceFieldSet(HInstruction* object,
4597 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004598 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004599 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004600 bool is_volatile,
4601 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004602 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004603 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004604 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004605 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004606 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4607 dex_pc),
4608 field_info_(field_offset,
4609 field_type,
4610 is_volatile,
4611 field_idx,
4612 declaring_class_def_index,
4613 dex_file,
4614 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004615 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004616 SetRawInputAt(0, object);
4617 SetRawInputAt(1, value);
4618 }
4619
Calin Juravle641547a2015-04-21 22:08:51 +01004620 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4621 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004622 }
4623
Calin Juravle52c48962014-12-16 17:02:57 +00004624 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004625 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004626 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004627 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004628 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004629 bool GetValueCanBeNull() const { return value_can_be_null_; }
4630 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004631
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004632 DECLARE_INSTRUCTION(InstanceFieldSet);
4633
4634 private:
4635 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004636 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004637
4638 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4639};
4640
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004641class HArrayGet : public HExpression<2> {
4642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 HArrayGet(HInstruction* array,
4644 HInstruction* index,
4645 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004646 uint32_t dex_pc,
4647 SideEffects additional_side_effects = SideEffects::None())
4648 : HExpression(type,
4649 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004650 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 SetRawInputAt(0, array);
4652 SetRawInputAt(1, index);
4653 }
4654
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004655 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004656 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004657 return true;
4658 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004659 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004660 // TODO: We can be smarter here.
4661 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4662 // which generates the implicit null check. There are cases when these can be removed
4663 // to produce better code. If we ever add optimizations to do so we should allow an
4664 // implicit check here (as long as the address falls in the first page).
4665 return false;
4666 }
4667
David Brazdild9510df2015-11-04 23:30:22 +00004668 bool IsEquivalentOf(HArrayGet* other) const {
4669 bool result = (GetDexPc() == other->GetDexPc());
4670 if (kIsDebugBuild && result) {
4671 DCHECK_EQ(GetBlock(), other->GetBlock());
4672 DCHECK_EQ(GetArray(), other->GetArray());
4673 DCHECK_EQ(GetIndex(), other->GetIndex());
4674 if (Primitive::IsIntOrLongType(GetType())) {
4675 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4676 } else {
4677 DCHECK(Primitive::IsFloatingPointType(GetType()));
4678 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4679 }
4680 }
4681 return result;
4682 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004683
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004684 HInstruction* GetArray() const { return InputAt(0); }
4685 HInstruction* GetIndex() const { return InputAt(1); }
4686
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687 DECLARE_INSTRUCTION(ArrayGet);
4688
4689 private:
4690 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4691};
4692
4693class HArraySet : public HTemplateInstruction<3> {
4694 public:
4695 HArraySet(HInstruction* array,
4696 HInstruction* index,
4697 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004698 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004699 uint32_t dex_pc,
4700 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004701 : HTemplateInstruction(
4702 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004703 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4704 additional_side_effects),
4705 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004706 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004707 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004708 value_can_be_null_(true),
4709 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710 SetRawInputAt(0, array);
4711 SetRawInputAt(1, index);
4712 SetRawInputAt(2, value);
4713 }
4714
Calin Juravle77520bc2015-01-12 18:45:46 +00004715 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716 // We currently always call a runtime method to catch array store
4717 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004718 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719 }
4720
Mingyao Yang81014cb2015-06-02 03:16:27 -07004721 // Can throw ArrayStoreException.
4722 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4723
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004724 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004725 // TODO: Same as for ArrayGet.
4726 return false;
4727 }
4728
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004729 void ClearNeedsTypeCheck() {
4730 needs_type_check_ = false;
4731 }
4732
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004733 void ClearValueCanBeNull() {
4734 value_can_be_null_ = false;
4735 }
4736
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004737 void SetStaticTypeOfArrayIsObjectArray() {
4738 static_type_of_array_is_object_array_ = true;
4739 }
4740
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004741 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004742 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004743 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004744
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004745 HInstruction* GetArray() const { return InputAt(0); }
4746 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004747 HInstruction* GetValue() const { return InputAt(2); }
4748
4749 Primitive::Type GetComponentType() const {
4750 // The Dex format does not type floating point index operations. Since the
4751 // `expected_component_type_` is set during building and can therefore not
4752 // be correct, we also check what is the value type. If it is a floating
4753 // point type, we must use that type.
4754 Primitive::Type value_type = GetValue()->GetType();
4755 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4756 ? value_type
4757 : expected_component_type_;
4758 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004759
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004760 Primitive::Type GetRawExpectedComponentType() const {
4761 return expected_component_type_;
4762 }
4763
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004764 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4765 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4766 }
4767
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 DECLARE_INSTRUCTION(ArraySet);
4769
4770 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004771 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004772 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004773 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004774 // Cached information for the reference_type_info_ so that codegen
4775 // does not need to inspect the static type.
4776 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004777
4778 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4779};
4780
4781class HArrayLength : public HExpression<1> {
4782 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004783 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004784 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004785 // Note that arrays do not change length, so the instruction does not
4786 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787 SetRawInputAt(0, array);
4788 }
4789
Calin Juravle77520bc2015-01-12 18:45:46 +00004790 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004791 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004792 return true;
4793 }
Calin Juravle641547a2015-04-21 22:08:51 +01004794 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4795 return obj == InputAt(0);
4796 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004797
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 DECLARE_INSTRUCTION(ArrayLength);
4799
4800 private:
4801 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4802};
4803
4804class HBoundsCheck : public HExpression<2> {
4805 public:
4806 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004808 DCHECK(index->GetType() == Primitive::kPrimInt);
4809 SetRawInputAt(0, index);
4810 SetRawInputAt(1, length);
4811 }
4812
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004813 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004814 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004815 return true;
4816 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004817
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004818 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004820 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004821
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004822 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004823
4824 DECLARE_INSTRUCTION(BoundsCheck);
4825
4826 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4828};
4829
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004830/**
4831 * Some DEX instructions are folded into multiple HInstructions that need
4832 * to stay live until the last HInstruction. This class
4833 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004834 * HInstruction stays live. `index` represents the stack location index of the
4835 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004836 */
4837class HTemporary : public HTemplateInstruction<0> {
4838 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4840 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004841
4842 size_t GetIndex() const { return index_; }
4843
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004844 Primitive::Type GetType() const OVERRIDE {
4845 // The previous instruction is the one that will be stored in the temporary location.
4846 DCHECK(GetPrevious() != nullptr);
4847 return GetPrevious()->GetType();
4848 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004849
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004850 DECLARE_INSTRUCTION(Temporary);
4851
4852 private:
4853 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004854 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4855};
4856
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004857class HSuspendCheck : public HTemplateInstruction<0> {
4858 public:
4859 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004860 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004861
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004862 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004863 return true;
4864 }
4865
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004866 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4867 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004868
4869 DECLARE_INSTRUCTION(SuspendCheck);
4870
4871 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004872 // Only used for code generation, in order to share the same slow path between back edges
4873 // of a same loop.
4874 SlowPathCode* slow_path_;
4875
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004876 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4877};
4878
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004879/**
4880 * Instruction to load a Class object.
4881 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004882class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004883 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004884 HLoadClass(HCurrentMethod* current_method,
4885 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004886 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004887 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004888 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004889 bool needs_access_check,
4890 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004892 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004893 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004894 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004895 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004896 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004897 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004898 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004899 // Referrers class should not need access check. We never inline unverified
4900 // methods so we can't possibly end up in this situation.
4901 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004902 SetRawInputAt(0, current_method);
4903 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004904
4905 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004906
4907 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004908 // Note that we don't need to test for generate_clinit_check_.
4909 // Whether or not we need to generate the clinit check is processed in
4910 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004911 return other->AsLoadClass()->type_index_ == type_index_ &&
4912 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004913 }
4914
4915 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4916
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004917 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004918 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004919 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004920
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004921 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004922 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004923 }
4924
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004925 bool MustGenerateClinitCheck() const {
4926 return generate_clinit_check_;
4927 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004928
Calin Juravle0ba218d2015-05-19 18:46:01 +01004929 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004930 // The entrypoint the code generator is going to call does not do
4931 // clinit of the class.
4932 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004933 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004934 }
4935
4936 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004937 return MustGenerateClinitCheck() ||
4938 (!is_referrers_class_ && !is_in_dex_cache_) ||
4939 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004940 }
4941
4942 bool NeedsAccessCheck() const {
4943 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004944 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004945
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004946 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004947 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004948 }
4949
Calin Juravleacf735c2015-02-12 15:25:22 +00004950 ReferenceTypeInfo GetLoadedClassRTI() {
4951 return loaded_class_rti_;
4952 }
4953
4954 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4955 // Make sure we only set exact types (the loaded class should never be merged).
4956 DCHECK(rti.IsExact());
4957 loaded_class_rti_ = rti;
4958 }
4959
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004960 const DexFile& GetDexFile() { return dex_file_; }
4961
Vladimir Markodc151b22015-10-15 18:02:30 +01004962 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004963
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004964 static SideEffects SideEffectsForArchRuntimeCalls() {
4965 return SideEffects::CanTriggerGC();
4966 }
4967
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004968 bool IsInDexCache() const { return is_in_dex_cache_; }
4969
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004970 DECLARE_INSTRUCTION(LoadClass);
4971
4972 private:
4973 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004974 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004975 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004976 // Whether this instruction must generate the initialization check.
4977 // Used for code generation.
4978 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004979 const bool needs_access_check_;
4980 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004981
Calin Juravleacf735c2015-02-12 15:25:22 +00004982 ReferenceTypeInfo loaded_class_rti_;
4983
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004984 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4985};
4986
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004987class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004988 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004989 HLoadString(HCurrentMethod* current_method,
4990 uint32_t string_index,
4991 uint32_t dex_pc,
4992 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004993 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004994 string_index_(string_index),
4995 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004996 SetRawInputAt(0, current_method);
4997 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004998
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004999 bool CanBeMoved() const OVERRIDE { return true; }
5000
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005001 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5002 return other->AsLoadString()->string_index_ == string_index_;
5003 }
5004
5005 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5006
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005007 uint32_t GetStringIndex() const { return string_index_; }
5008
5009 // TODO: Can we deopt or debug when we resolve a string?
5010 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005011 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005012 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005013 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005014
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005015 static SideEffects SideEffectsForArchRuntimeCalls() {
5016 return SideEffects::CanTriggerGC();
5017 }
5018
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005019 DECLARE_INSTRUCTION(LoadString);
5020
5021 private:
5022 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005023 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005024
5025 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5026};
5027
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005028/**
5029 * Performs an initialization check on its Class object input.
5030 */
5031class HClinitCheck : public HExpression<1> {
5032 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005033 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005034 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005035 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005036 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5037 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005038 SetRawInputAt(0, constant);
5039 }
5040
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005041 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005042 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005043 return true;
5044 }
5045
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005046 bool NeedsEnvironment() const OVERRIDE {
5047 // May call runtime to initialize the class.
5048 return true;
5049 }
5050
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005051 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005052
5053 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5054
5055 DECLARE_INSTRUCTION(ClinitCheck);
5056
5057 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005058 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5059};
5060
5061class HStaticFieldGet : public HExpression<1> {
5062 public:
5063 HStaticFieldGet(HInstruction* cls,
5064 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005065 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005066 bool is_volatile,
5067 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005068 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005069 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005070 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005071 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005072 : HExpression(field_type,
5073 SideEffects::FieldReadOfType(field_type, is_volatile),
5074 dex_pc),
5075 field_info_(field_offset,
5076 field_type,
5077 is_volatile,
5078 field_idx,
5079 declaring_class_def_index,
5080 dex_file,
5081 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005082 SetRawInputAt(0, cls);
5083 }
5084
Calin Juravle52c48962014-12-16 17:02:57 +00005085
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005086 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005087
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005088 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005089 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5090 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005091 }
5092
5093 size_t ComputeHashCode() const OVERRIDE {
5094 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5095 }
5096
Calin Juravle52c48962014-12-16 17:02:57 +00005097 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005098 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5099 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005100 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005101
5102 DECLARE_INSTRUCTION(StaticFieldGet);
5103
5104 private:
5105 const FieldInfo field_info_;
5106
5107 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5108};
5109
5110class HStaticFieldSet : public HTemplateInstruction<2> {
5111 public:
5112 HStaticFieldSet(HInstruction* cls,
5113 HInstruction* value,
5114 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005115 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005116 bool is_volatile,
5117 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005118 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005119 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005120 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005121 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005122 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5123 dex_pc),
5124 field_info_(field_offset,
5125 field_type,
5126 is_volatile,
5127 field_idx,
5128 declaring_class_def_index,
5129 dex_file,
5130 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005131 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005132 SetRawInputAt(0, cls);
5133 SetRawInputAt(1, value);
5134 }
5135
Calin Juravle52c48962014-12-16 17:02:57 +00005136 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005137 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5138 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005139 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005140
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005141 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005142 bool GetValueCanBeNull() const { return value_can_be_null_; }
5143 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005145 DECLARE_INSTRUCTION(StaticFieldSet);
5146
5147 private:
5148 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005149 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005150
5151 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5152};
5153
Calin Juravlee460d1d2015-09-29 04:52:17 +01005154class HUnresolvedInstanceFieldGet : public HExpression<1> {
5155 public:
5156 HUnresolvedInstanceFieldGet(HInstruction* obj,
5157 Primitive::Type field_type,
5158 uint32_t field_index,
5159 uint32_t dex_pc)
5160 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5161 field_index_(field_index) {
5162 SetRawInputAt(0, obj);
5163 }
5164
5165 bool NeedsEnvironment() const OVERRIDE { return true; }
5166 bool CanThrow() const OVERRIDE { return true; }
5167
5168 Primitive::Type GetFieldType() const { return GetType(); }
5169 uint32_t GetFieldIndex() const { return field_index_; }
5170
5171 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5172
5173 private:
5174 const uint32_t field_index_;
5175
5176 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5177};
5178
5179class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5180 public:
5181 HUnresolvedInstanceFieldSet(HInstruction* obj,
5182 HInstruction* value,
5183 Primitive::Type field_type,
5184 uint32_t field_index,
5185 uint32_t dex_pc)
5186 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5187 field_type_(field_type),
5188 field_index_(field_index) {
5189 DCHECK_EQ(field_type, value->GetType());
5190 SetRawInputAt(0, obj);
5191 SetRawInputAt(1, value);
5192 }
5193
5194 bool NeedsEnvironment() const OVERRIDE { return true; }
5195 bool CanThrow() const OVERRIDE { return true; }
5196
5197 Primitive::Type GetFieldType() const { return field_type_; }
5198 uint32_t GetFieldIndex() const { return field_index_; }
5199
5200 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5201
5202 private:
5203 const Primitive::Type field_type_;
5204 const uint32_t field_index_;
5205
5206 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5207};
5208
5209class HUnresolvedStaticFieldGet : public HExpression<0> {
5210 public:
5211 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5212 uint32_t field_index,
5213 uint32_t dex_pc)
5214 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5215 field_index_(field_index) {
5216 }
5217
5218 bool NeedsEnvironment() const OVERRIDE { return true; }
5219 bool CanThrow() const OVERRIDE { return true; }
5220
5221 Primitive::Type GetFieldType() const { return GetType(); }
5222 uint32_t GetFieldIndex() const { return field_index_; }
5223
5224 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5225
5226 private:
5227 const uint32_t field_index_;
5228
5229 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5230};
5231
5232class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5233 public:
5234 HUnresolvedStaticFieldSet(HInstruction* value,
5235 Primitive::Type field_type,
5236 uint32_t field_index,
5237 uint32_t dex_pc)
5238 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5239 field_type_(field_type),
5240 field_index_(field_index) {
5241 DCHECK_EQ(field_type, value->GetType());
5242 SetRawInputAt(0, value);
5243 }
5244
5245 bool NeedsEnvironment() const OVERRIDE { return true; }
5246 bool CanThrow() const OVERRIDE { return true; }
5247
5248 Primitive::Type GetFieldType() const { return field_type_; }
5249 uint32_t GetFieldIndex() const { return field_index_; }
5250
5251 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5252
5253 private:
5254 const Primitive::Type field_type_;
5255 const uint32_t field_index_;
5256
5257 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5258};
5259
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005260// Implement the move-exception DEX instruction.
5261class HLoadException : public HExpression<0> {
5262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005263 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5264 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005265
David Brazdilbbd733e2015-08-18 17:48:17 +01005266 bool CanBeNull() const OVERRIDE { return false; }
5267
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005268 DECLARE_INSTRUCTION(LoadException);
5269
5270 private:
5271 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5272};
5273
David Brazdilcb1c0552015-08-04 16:22:25 +01005274// Implicit part of move-exception which clears thread-local exception storage.
5275// Must not be removed because the runtime expects the TLS to get cleared.
5276class HClearException : public HTemplateInstruction<0> {
5277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005278 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5279 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005280
5281 DECLARE_INSTRUCTION(ClearException);
5282
5283 private:
5284 DISALLOW_COPY_AND_ASSIGN(HClearException);
5285};
5286
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005287class HThrow : public HTemplateInstruction<1> {
5288 public:
5289 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005290 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005291 SetRawInputAt(0, exception);
5292 }
5293
5294 bool IsControlFlow() const OVERRIDE { return true; }
5295
5296 bool NeedsEnvironment() const OVERRIDE { return true; }
5297
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005298 bool CanThrow() const OVERRIDE { return true; }
5299
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005300
5301 DECLARE_INSTRUCTION(Throw);
5302
5303 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005304 DISALLOW_COPY_AND_ASSIGN(HThrow);
5305};
5306
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005307/**
5308 * Implementation strategies for the code generator of a HInstanceOf
5309 * or `HCheckCast`.
5310 */
5311enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005312 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005313 kExactCheck, // Can do a single class compare.
5314 kClassHierarchyCheck, // Can just walk the super class chain.
5315 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5316 kInterfaceCheck, // No optimization yet when checking against an interface.
5317 kArrayObjectCheck, // Can just check if the array is not primitive.
5318 kArrayCheck // No optimization yet when checking against a generic array.
5319};
5320
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005321class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005322 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005323 HInstanceOf(HInstruction* object,
5324 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005325 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005326 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005327 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005328 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005329 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005330 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005331 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005332 SetRawInputAt(0, object);
5333 SetRawInputAt(1, constant);
5334 }
5335
5336 bool CanBeMoved() const OVERRIDE { return true; }
5337
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005338 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005339 return true;
5340 }
5341
5342 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005343 return false;
5344 }
5345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005346 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5347
5348 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005349
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005350 // Used only in code generation.
5351 bool MustDoNullCheck() const { return must_do_null_check_; }
5352 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5353
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005354 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5355 return (check_kind == TypeCheckKind::kExactCheck)
5356 ? SideEffects::None()
5357 // Mips currently does runtime calls for any other checks.
5358 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005359 }
5360
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005361 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005362
5363 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005364 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005365 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005366
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005367 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5368};
5369
Calin Juravleb1498f62015-02-16 13:13:29 +00005370class HBoundType : public HExpression<1> {
5371 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005372 // Constructs an HBoundType with the given upper_bound.
5373 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005374 HBoundType(HInstruction* input,
5375 ReferenceTypeInfo upper_bound,
5376 bool upper_can_be_null,
5377 uint32_t dex_pc = kNoDexPc)
5378 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005379 upper_bound_(upper_bound),
5380 upper_can_be_null_(upper_can_be_null),
5381 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005382 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005383 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005384 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005385 }
5386
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005387 // GetUpper* should only be used in reference type propagation.
5388 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5389 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005390
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005391 void SetCanBeNull(bool can_be_null) {
5392 DCHECK(upper_can_be_null_ || !can_be_null);
5393 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005394 }
5395
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005396 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5397
Calin Juravleb1498f62015-02-16 13:13:29 +00005398 DECLARE_INSTRUCTION(BoundType);
5399
5400 private:
5401 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005402 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5403 // It is used to bound the type in cases like:
5404 // if (x instanceof ClassX) {
5405 // // uper_bound_ will be ClassX
5406 // }
5407 const ReferenceTypeInfo upper_bound_;
5408 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5409 // is false then can_be_null_ cannot be true).
5410 const bool upper_can_be_null_;
5411 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005412
5413 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5414};
5415
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005416class HCheckCast : public HTemplateInstruction<2> {
5417 public:
5418 HCheckCast(HInstruction* object,
5419 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005420 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005421 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005422 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005423 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005424 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005425 SetRawInputAt(0, object);
5426 SetRawInputAt(1, constant);
5427 }
5428
5429 bool CanBeMoved() const OVERRIDE { return true; }
5430
5431 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5432 return true;
5433 }
5434
5435 bool NeedsEnvironment() const OVERRIDE {
5436 // Instruction may throw a CheckCastError.
5437 return true;
5438 }
5439
5440 bool CanThrow() const OVERRIDE { return true; }
5441
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005442 bool MustDoNullCheck() const { return must_do_null_check_; }
5443 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005445
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005446 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005447
5448 DECLARE_INSTRUCTION(CheckCast);
5449
5450 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005451 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005452 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005453
5454 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005455};
5456
Calin Juravle27df7582015-04-17 19:12:31 +01005457class HMemoryBarrier : public HTemplateInstruction<0> {
5458 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005459 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005460 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005461 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005462 barrier_kind_(barrier_kind) {}
5463
5464 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5465
5466 DECLARE_INSTRUCTION(MemoryBarrier);
5467
5468 private:
5469 const MemBarrierKind barrier_kind_;
5470
5471 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5472};
5473
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005474class HMonitorOperation : public HTemplateInstruction<1> {
5475 public:
5476 enum OperationKind {
5477 kEnter,
5478 kExit,
5479 };
5480
5481 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005482 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005483 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5484 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005485 SetRawInputAt(0, object);
5486 }
5487
5488 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005489 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5490
5491 bool CanThrow() const OVERRIDE {
5492 // Verifier guarantees that monitor-exit cannot throw.
5493 // This is important because it allows the HGraphBuilder to remove
5494 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5495 return IsEnter();
5496 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005497
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005498
5499 bool IsEnter() const { return kind_ == kEnter; }
5500
5501 DECLARE_INSTRUCTION(MonitorOperation);
5502
Calin Juravle52c48962014-12-16 17:02:57 +00005503 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005504 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005505
5506 private:
5507 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5508};
5509
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005510/**
5511 * A HInstruction used as a marker for the replacement of new + <init>
5512 * of a String to a call to a StringFactory. Only baseline will see
5513 * the node at code generation, where it will be be treated as null.
5514 * When compiling non-baseline, `HFakeString` instructions are being removed
5515 * in the instruction simplifier.
5516 */
5517class HFakeString : public HTemplateInstruction<0> {
5518 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005519 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5520 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005521
5522 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5523
5524 DECLARE_INSTRUCTION(FakeString);
5525
5526 private:
5527 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5528};
5529
Vladimir Markof9f64412015-09-02 14:05:49 +01005530class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005531 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005532 MoveOperands(Location source,
5533 Location destination,
5534 Primitive::Type type,
5535 HInstruction* instruction)
5536 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005537
5538 Location GetSource() const { return source_; }
5539 Location GetDestination() const { return destination_; }
5540
5541 void SetSource(Location value) { source_ = value; }
5542 void SetDestination(Location value) { destination_ = value; }
5543
5544 // The parallel move resolver marks moves as "in-progress" by clearing the
5545 // destination (but not the source).
5546 Location MarkPending() {
5547 DCHECK(!IsPending());
5548 Location dest = destination_;
5549 destination_ = Location::NoLocation();
5550 return dest;
5551 }
5552
5553 void ClearPending(Location dest) {
5554 DCHECK(IsPending());
5555 destination_ = dest;
5556 }
5557
5558 bool IsPending() const {
5559 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5560 return destination_.IsInvalid() && !source_.IsInvalid();
5561 }
5562
5563 // True if this blocks a move from the given location.
5564 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005565 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005566 }
5567
5568 // A move is redundant if it's been eliminated, if its source and
5569 // destination are the same, or if its destination is unneeded.
5570 bool IsRedundant() const {
5571 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5572 }
5573
5574 // We clear both operands to indicate move that's been eliminated.
5575 void Eliminate() {
5576 source_ = destination_ = Location::NoLocation();
5577 }
5578
5579 bool IsEliminated() const {
5580 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5581 return source_.IsInvalid();
5582 }
5583
Alexey Frunze4dda3372015-06-01 18:31:49 -07005584 Primitive::Type GetType() const { return type_; }
5585
Nicolas Geoffray90218252015-04-15 11:56:51 +01005586 bool Is64BitMove() const {
5587 return Primitive::Is64BitType(type_);
5588 }
5589
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005590 HInstruction* GetInstruction() const { return instruction_; }
5591
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005592 private:
5593 Location source_;
5594 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005595 // The type this move is for.
5596 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005597 // The instruction this move is assocatied with. Null when this move is
5598 // for moving an input in the expected locations of user (including a phi user).
5599 // This is only used in debug mode, to ensure we do not connect interval siblings
5600 // in the same parallel move.
5601 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005602};
5603
5604static constexpr size_t kDefaultNumberOfMoves = 4;
5605
5606class HParallelMove : public HTemplateInstruction<0> {
5607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005608 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005609 : HTemplateInstruction(SideEffects::None(), dex_pc),
5610 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5611 moves_.reserve(kDefaultNumberOfMoves);
5612 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005613
Nicolas Geoffray90218252015-04-15 11:56:51 +01005614 void AddMove(Location source,
5615 Location destination,
5616 Primitive::Type type,
5617 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005618 DCHECK(source.IsValid());
5619 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005620 if (kIsDebugBuild) {
5621 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005622 for (const MoveOperands& move : moves_) {
5623 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005624 // Special case the situation where the move is for the spill slot
5625 // of the instruction.
5626 if ((GetPrevious() == instruction)
5627 || ((GetPrevious() == nullptr)
5628 && instruction->IsPhi()
5629 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005630 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005631 << "Doing parallel moves for the same instruction.";
5632 } else {
5633 DCHECK(false) << "Doing parallel moves for the same instruction.";
5634 }
5635 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005636 }
5637 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005638 for (const MoveOperands& move : moves_) {
5639 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005640 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005641 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005642 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005643 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005644 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005645 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005646 }
5647
Vladimir Marko225b6462015-09-28 12:17:40 +01005648 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005649 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005650 }
5651
Vladimir Marko225b6462015-09-28 12:17:40 +01005652 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005653
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005654 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005655
5656 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005657 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005658
5659 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5660};
5661
Mark Mendell0616ae02015-04-17 12:49:27 -04005662} // namespace art
5663
Vladimir Markob4536b72015-11-24 13:45:23 +00005664#ifdef ART_ENABLE_CODEGEN_arm
5665#include "nodes_arm.h"
5666#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005667#ifdef ART_ENABLE_CODEGEN_arm64
5668#include "nodes_arm64.h"
5669#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005670#ifdef ART_ENABLE_CODEGEN_x86
5671#include "nodes_x86.h"
5672#endif
5673
5674namespace art {
5675
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005676class HGraphVisitor : public ValueObject {
5677 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005678 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5679 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005680
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005681 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005682 virtual void VisitBasicBlock(HBasicBlock* block);
5683
Roland Levillain633021e2014-10-01 14:12:25 +01005684 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005685 void VisitInsertionOrder();
5686
Roland Levillain633021e2014-10-01 14:12:25 +01005687 // Visit the graph following dominator tree reverse post-order.
5688 void VisitReversePostOrder();
5689
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005690 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005691
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005692 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005693#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005694 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5695
5696 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5697
5698#undef DECLARE_VISIT_INSTRUCTION
5699
5700 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005701 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005702
5703 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5704};
5705
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005706class HGraphDelegateVisitor : public HGraphVisitor {
5707 public:
5708 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5709 virtual ~HGraphDelegateVisitor() {}
5710
5711 // Visit functions that delegate to to super class.
5712#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005713 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005714
5715 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5716
5717#undef DECLARE_VISIT_INSTRUCTION
5718
5719 private:
5720 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5721};
5722
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005723class HInsertionOrderIterator : public ValueObject {
5724 public:
5725 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5726
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005727 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005728 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005729 void Advance() { ++index_; }
5730
5731 private:
5732 const HGraph& graph_;
5733 size_t index_;
5734
5735 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5736};
5737
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005738class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005739 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005740 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5741 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005742 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005743 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005744
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005745 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5746 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005747 void Advance() { ++index_; }
5748
5749 private:
5750 const HGraph& graph_;
5751 size_t index_;
5752
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005753 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005754};
5755
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005756class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005757 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005758 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005759 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005760 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005761 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005762 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005763
5764 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005765 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005766 void Advance() { --index_; }
5767
5768 private:
5769 const HGraph& graph_;
5770 size_t index_;
5771
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005772 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005773};
5774
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005775class HLinearPostOrderIterator : public ValueObject {
5776 public:
5777 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005778 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005779
5780 bool Done() const { return index_ == 0; }
5781
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005782 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005783
5784 void Advance() {
5785 --index_;
5786 DCHECK_GE(index_, 0U);
5787 }
5788
5789 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005790 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005791 size_t index_;
5792
5793 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5794};
5795
5796class HLinearOrderIterator : public ValueObject {
5797 public:
5798 explicit HLinearOrderIterator(const HGraph& graph)
5799 : order_(graph.GetLinearOrder()), index_(0) {}
5800
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005801 bool Done() const { return index_ == order_.size(); }
5802 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005803 void Advance() { ++index_; }
5804
5805 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005806 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005807 size_t index_;
5808
5809 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5810};
5811
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005812// Iterator over the blocks that art part of the loop. Includes blocks part
5813// of an inner loop. The order in which the blocks are iterated is on their
5814// block id.
5815class HBlocksInLoopIterator : public ValueObject {
5816 public:
5817 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5818 : blocks_in_loop_(info.GetBlocks()),
5819 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5820 index_(0) {
5821 if (!blocks_in_loop_.IsBitSet(index_)) {
5822 Advance();
5823 }
5824 }
5825
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005826 bool Done() const { return index_ == blocks_.size(); }
5827 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005828 void Advance() {
5829 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005830 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005831 if (blocks_in_loop_.IsBitSet(index_)) {
5832 break;
5833 }
5834 }
5835 }
5836
5837 private:
5838 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005839 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005840 size_t index_;
5841
5842 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5843};
5844
Mingyao Yang3584bce2015-05-19 16:01:59 -07005845// Iterator over the blocks that art part of the loop. Includes blocks part
5846// of an inner loop. The order in which the blocks are iterated is reverse
5847// post order.
5848class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5849 public:
5850 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5851 : blocks_in_loop_(info.GetBlocks()),
5852 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5853 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005854 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005855 Advance();
5856 }
5857 }
5858
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005859 bool Done() const { return index_ == blocks_.size(); }
5860 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005861 void Advance() {
5862 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005863 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5864 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005865 break;
5866 }
5867 }
5868 }
5869
5870 private:
5871 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005872 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005873 size_t index_;
5874
5875 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5876};
5877
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005878inline int64_t Int64FromConstant(HConstant* constant) {
5879 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5880 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5881 : constant->AsLongConstant()->GetValue();
5882}
5883
Vladimir Marko58155012015-08-19 12:49:41 +00005884inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5885 // For the purposes of the compiler, the dex files must actually be the same object
5886 // if we want to safely treat them as the same. This is especially important for JIT
5887 // as custom class loaders can open the same underlying file (or memory) multiple
5888 // times and provide different class resolution but no two class loaders should ever
5889 // use the same DexFile object - doing so is an unsupported hack that can lead to
5890 // all sorts of weird failures.
5891 return &lhs == &rhs;
5892}
5893
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005894} // namespace art
5895
5896#endif // ART_COMPILER_OPTIMIZING_NODES_H_