blob: 59c07690b13bab81393636cb9dbef3a7fe29de95 [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
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000101enum GraphAnalysisResult {
102 kAnalysisFailThrowCatchLoop,
103 kAnalysisFailAmbiguousArrayOp,
104 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000105};
106
Vladimir Markof9f64412015-09-02 14:05:49 +0100107class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100108 public:
109 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
110
111 void AddInstruction(HInstruction* instruction);
112 void RemoveInstruction(HInstruction* instruction);
113
David Brazdilc3d743f2015-04-22 13:40:50 +0100114 // Insert `instruction` before/after an existing instruction `cursor`.
115 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
116 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
117
Roland Levillain6b469232014-09-25 10:10:38 +0100118 // Return true if this list contains `instruction`.
119 bool Contains(HInstruction* instruction) const;
120
Roland Levillainccc07a92014-09-16 14:48:16 +0100121 // Return true if `instruction1` is found before `instruction2` in
122 // this instruction list and false otherwise. Abort if none
123 // of these instructions is found.
124 bool FoundBefore(const HInstruction* instruction1,
125 const HInstruction* instruction2) const;
126
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000127 bool IsEmpty() const { return first_instruction_ == nullptr; }
128 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
129
130 // Update the block of all instructions to be `block`.
131 void SetBlockOfInstructions(HBasicBlock* block) const;
132
133 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
134 void Add(const HInstructionList& instruction_list);
135
David Brazdil2d7352b2015-04-20 14:52:42 +0100136 // Return the number of instructions in the list. This is an expensive operation.
137 size_t CountSize() const;
138
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 private:
140 HInstruction* first_instruction_;
141 HInstruction* last_instruction_;
142
143 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 friend class HGraph;
145 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100146 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148
149 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
150};
151
David Brazdil4833f5a2015-12-16 10:37:39 +0000152class ReferenceTypeInfo : ValueObject {
153 public:
154 typedef Handle<mirror::Class> TypeHandle;
155
156 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
157 // The constructor will check that the type_handle is valid.
158 return ReferenceTypeInfo(type_handle, is_exact);
159 }
160
161 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
162
163 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
164 return handle.GetReference() != nullptr;
165 }
166
167 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
168 return IsValidHandle(type_handle_);
169 }
170
171 bool IsExact() const { return is_exact_; }
172
173 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
174 DCHECK(IsValid());
175 return GetTypeHandle()->IsObjectClass();
176 }
177
178 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
179 DCHECK(IsValid());
180 return GetTypeHandle()->IsStringClass();
181 }
182
183 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
184 DCHECK(IsValid());
185 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
186 }
187
188 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
189 DCHECK(IsValid());
190 return GetTypeHandle()->IsInterface();
191 }
192
193 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
194 DCHECK(IsValid());
195 return GetTypeHandle()->IsArrayClass();
196 }
197
198 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
199 DCHECK(IsValid());
200 return GetTypeHandle()->IsPrimitiveArray();
201 }
202
203 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
204 DCHECK(IsValid());
205 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
206 }
207
208 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
209 DCHECK(IsValid());
210 if (!IsExact()) return false;
211 if (!IsArrayClass()) return false;
212 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
213 }
214
215 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 if (!IsExact()) return false;
218 if (!IsArrayClass()) return false;
219 if (!rti.IsArrayClass()) return false;
220 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
221 rti.GetTypeHandle()->GetComponentType());
222 }
223
224 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
225
226 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
227 DCHECK(IsValid());
228 DCHECK(rti.IsValid());
229 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
230 }
231
232 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 DCHECK(rti.IsValid());
235 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
236 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
237 }
238
239 // Returns true if the type information provide the same amount of details.
240 // Note that it does not mean that the instructions have the same actual type
241 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000242 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 if (!IsValid() && !rti.IsValid()) {
244 // Invalid types are equal.
245 return true;
246 }
247 if (!IsValid() || !rti.IsValid()) {
248 // One is valid, the other not.
249 return false;
250 }
251 return IsExact() == rti.IsExact()
252 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
253 }
254
255 private:
256 ReferenceTypeInfo();
257 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
258
259 // The class of the object.
260 TypeHandle type_handle_;
261 // Whether or not the type is exact or a superclass of the actual type.
262 // Whether or not we have any information about this type.
263 bool is_exact_;
264};
265
266std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
267
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000268// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100269class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100271 HGraph(ArenaAllocator* arena,
272 const DexFile& dex_file,
273 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100274 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100276 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100277 bool debuggable = false,
278 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100280 blocks_(arena->Adapter(kArenaAllocBlockList)),
281 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
282 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283 entry_block_(nullptr),
284 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100285 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286 number_of_vregs_(0),
287 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000288 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400289 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000290 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000291 has_irreducible_loops_(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 Brazdil4833f5a2015-12-16 10:37:39 +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 Brazdil4833f5a2015-12-16 10:37:39 +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.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000327 GraphAnalysisResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000328
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100329 void ComputeDominanceInformation();
330 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000331 void ClearLoopInformation();
332 void FindBackEdges(ArenaBitVector* visited);
333 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100334 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100335 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000336
David Brazdil4833f5a2015-12-16 10:37:39 +0000337 // Analyze all natural loops in this graph. Returns a code specifying that it
338 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000339 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000340 GraphAnalysisResult AnalyzeLoops() 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 Geoffray15bd2282016-01-05 15:55:41 +0000485 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
486 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
487
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100488 ArtMethod* GetArtMethod() const { return art_method_; }
489 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
490
Mark Mendellf6529172015-11-17 11:16:56 -0500491 // Returns an instruction with the opposite boolean value from 'cond'.
492 // The instruction has been inserted into the graph, either as a constant, or
493 // before cursor.
494 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
495
David Brazdil2d7352b2015-04-20 14:52:42 +0100496 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000497 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100498 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000499
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000500 template <class InstructionType, typename ValueType>
501 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600502 ArenaSafeMap<ValueType, InstructionType*>* cache,
503 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000504 // Try to find an existing constant of the given value.
505 InstructionType* constant = nullptr;
506 auto cached_constant = cache->find(value);
507 if (cached_constant != cache->end()) {
508 constant = cached_constant->second;
509 }
510
511 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100512 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000513 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000515 cache->Overwrite(value, constant);
516 InsertConstant(constant);
517 }
518 return constant;
519 }
520
David Brazdil8d5b8b22015-03-24 10:51:52 +0000521 void InsertConstant(HConstant* instruction);
522
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000523 // Cache a float constant into the graph. This method should only be
524 // called by the SsaBuilder when creating "equivalent" instructions.
525 void CacheFloatConstant(HFloatConstant* constant);
526
527 // See CacheFloatConstant comment.
528 void CacheDoubleConstant(HDoubleConstant* constant);
529
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000530 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000531
532 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000534
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100536 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000537
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100538 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100539 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100540
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000541 HBasicBlock* entry_block_;
542 HBasicBlock* exit_block_;
543
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100544 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100545 uint16_t maximum_number_of_out_vregs_;
546
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100547 // The number of virtual registers in this method. Contains the parameters.
548 uint16_t number_of_vregs_;
549
550 // The number of virtual registers used by parameters of this method.
551 uint16_t number_of_in_vregs_;
552
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000553 // Number of vreg size slots that the temporaries use (used in baseline compiler).
554 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100555
Mark Mendell1152c922015-04-24 17:06:35 -0400556 // Has bounds checks. We can totally skip BCE if it's false.
557 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800558
David Brazdil77a48ae2015-09-15 12:34:04 +0000559 // Flag whether there are any try/catch blocks in the graph. We will skip
560 // try/catch-related passes if false.
561 bool has_try_catch_;
562
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000563 // Flag whether there are any irreducible loops in the graph.
564 bool has_irreducible_loops_;
565
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000566 // Indicates whether the graph should be compiled in a way that
567 // ensures full debuggability. If false, we can apply more
568 // aggressive optimizations that may limit the level of debugging.
569 const bool debuggable_;
570
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000571 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000572 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000573
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100574 // The dex file from which the method is from.
575 const DexFile& dex_file_;
576
577 // The method index in the dex file.
578 const uint32_t method_idx_;
579
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100580 // If inlined, this encodes how the callee is being invoked.
581 const InvokeType invoke_type_;
582
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100583 // Whether the graph has been transformed to SSA form. Only used
584 // in debug mode to ensure we are not using properties only valid
585 // for non-SSA form (like the number of temporaries).
586 bool in_ssa_form_;
587
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100588 const bool should_generate_constructor_barrier_;
589
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 const InstructionSet instruction_set_;
591
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000592 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000593 HNullConstant* cached_null_constant_;
594 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000595 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000596 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000597 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000598
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100599 HCurrentMethod* cached_current_method_;
600
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100601 // The ArtMethod this graph is for. Note that for AOT, it may be null,
602 // for example for methods whose declaring class could not be resolved
603 // (such as when the superclass could not be found).
604 ArtMethod* art_method_;
605
David Brazdil4833f5a2015-12-16 10:37:39 +0000606 // Keep the RTI of inexact Object to avoid having to pass stack handle
607 // collection pointer to passes which may create NullConstant.
608 ReferenceTypeInfo inexact_object_rti_;
609
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000610 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100611 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000612 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613 DISALLOW_COPY_AND_ASSIGN(HGraph);
614};
615
Vladimir Markof9f64412015-09-02 14:05:49 +0100616class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617 public:
618 HLoopInformation(HBasicBlock* header, HGraph* graph)
619 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100620 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000621 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100622 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100623 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100624 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
625 back_edges_.reserve(kDefaultNumberOfBackEdges);
626 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100627
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000628 bool IsIrreducible() const { return irreducible_; }
629
630 void Dump(std::ostream& os);
631
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100632 HBasicBlock* GetHeader() const {
633 return header_;
634 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000636 void SetHeader(HBasicBlock* block) {
637 header_ = block;
638 }
639
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100640 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
641 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
642 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
643
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100645 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000646 }
647
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100648 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100649 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100650 }
651
David Brazdil46e2a392015-03-16 17:31:52 +0000652 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 }
655
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000656 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100657 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000658 }
659
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100660 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100661
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100662 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100664 }
665
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100666 // Returns the lifetime position of the back edge that has the
667 // greatest lifetime position.
668 size_t GetLifetimeEnd() const;
669
670 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100672 }
673
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000674 // Finds blocks that are part of this loop.
675 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100676
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100677 // Returns whether this loop information contains `block`.
678 // Note that this loop information *must* be populated before entering this function.
679 bool Contains(const HBasicBlock& block) const;
680
681 // Returns whether this loop information is an inner loop of `other`.
682 // Note that `other` *must* be populated before entering this function.
683 bool IsIn(const HLoopInformation& other) const;
684
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800685 // Returns true if instruction is not defined within this loop.
686 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700687
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100688 const ArenaBitVector& GetBlocks() const { return blocks_; }
689
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000690 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000691 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000692
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 // Internal recursive implementation of `Populate`.
695 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000696 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100699 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100702 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000703
704 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
705};
706
David Brazdilec16f792015-08-19 15:04:01 +0100707// Stores try/catch information for basic blocks.
708// Note that HGraph is constructed so that catch blocks cannot simultaneously
709// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100710class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100711 public:
712 // Try block information constructor.
713 explicit TryCatchInformation(const HTryBoundary& try_entry)
714 : try_entry_(&try_entry),
715 catch_dex_file_(nullptr),
716 catch_type_index_(DexFile::kDexNoIndex16) {
717 DCHECK(try_entry_ != nullptr);
718 }
719
720 // Catch block information constructor.
721 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
722 : try_entry_(nullptr),
723 catch_dex_file_(&dex_file),
724 catch_type_index_(catch_type_index) {}
725
726 bool IsTryBlock() const { return try_entry_ != nullptr; }
727
728 const HTryBoundary& GetTryEntry() const {
729 DCHECK(IsTryBlock());
730 return *try_entry_;
731 }
732
733 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
734
735 bool IsCatchAllTypeIndex() const {
736 DCHECK(IsCatchBlock());
737 return catch_type_index_ == DexFile::kDexNoIndex16;
738 }
739
740 uint16_t GetCatchTypeIndex() const {
741 DCHECK(IsCatchBlock());
742 return catch_type_index_;
743 }
744
745 const DexFile& GetCatchDexFile() const {
746 DCHECK(IsCatchBlock());
747 return *catch_dex_file_;
748 }
749
750 private:
751 // One of possibly several TryBoundary instructions entering the block's try.
752 // Only set for try blocks.
753 const HTryBoundary* try_entry_;
754
755 // Exception type information. Only set for catch blocks.
756 const DexFile* catch_dex_file_;
757 const uint16_t catch_type_index_;
758};
759
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100760static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100761static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100762
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000763// A block in a method. Contains the list of instructions represented
764// as a double linked list. Each block knows its predecessors and
765// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100766
Vladimir Markof9f64412015-09-02 14:05:49 +0100767class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600769 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000770 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
772 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000773 loop_information_(nullptr),
774 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000775 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100777 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100778 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000779 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000780 try_catch_information_(nullptr) {
781 predecessors_.reserve(kDefaultNumberOfPredecessors);
782 successors_.reserve(kDefaultNumberOfSuccessors);
783 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
784 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785
Vladimir Marko60584552015-09-03 13:35:12 +0000786 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000788 }
789
Vladimir Marko60584552015-09-03 13:35:12 +0000790 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100791 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792 }
793
David Brazdild26a4112015-11-10 11:07:31 +0000794 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
795 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
796
Vladimir Marko60584552015-09-03 13:35:12 +0000797 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
798 return ContainsElement(successors_, block, start_from);
799 }
800
801 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100802 return dominated_blocks_;
803 }
804
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100805 bool IsEntryBlock() const {
806 return graph_->GetEntryBlock() == this;
807 }
808
809 bool IsExitBlock() const {
810 return graph_->GetExitBlock() == this;
811 }
812
David Brazdil46e2a392015-03-16 17:31:52 +0000813 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000814 bool IsSingleTryBoundary() const;
815
816 // Returns true if this block emits nothing but a jump.
817 bool IsSingleJump() const {
818 HLoopInformation* loop_info = GetLoopInformation();
819 return (IsSingleGoto() || IsSingleTryBoundary())
820 // Back edges generate a suspend check.
821 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
822 }
David Brazdil46e2a392015-03-16 17:31:52 +0000823
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000824 void AddBackEdge(HBasicBlock* back_edge) {
825 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000826 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000827 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100828 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000829 loop_information_->AddBackEdge(back_edge);
830 }
831
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000832 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100835 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000836 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600837 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839 HBasicBlock* GetDominator() const { return dominator_; }
840 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000841 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
842
843 void RemoveDominatedBlock(HBasicBlock* block) {
844 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100845 }
Vladimir Marko60584552015-09-03 13:35:12 +0000846
847 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
848 ReplaceElement(dominated_blocks_, existing, new_block);
849 }
850
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100851 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000852
853 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100854 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855 }
856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
858 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100859 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100860 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100861 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
862 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000863
864 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000865 successors_.push_back(block);
866 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000867 }
868
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100869 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
870 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100871 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000872 new_block->predecessors_.push_back(this);
873 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 }
875
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000876 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
877 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000878 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000879 new_block->successors_.push_back(this);
880 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000881 }
882
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100883 // Insert `this` between `predecessor` and `successor. This method
884 // preserves the indicies, and will update the first edge found between
885 // `predecessor` and `successor`.
886 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
887 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100888 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000889 successor->predecessors_[predecessor_index] = this;
890 predecessor->successors_[successor_index] = this;
891 successors_.push_back(successor);
892 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100893 }
894
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100895 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000896 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100897 }
898
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000899 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000900 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000901 }
902
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100903 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000904 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 }
906
907 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000908 predecessors_.push_back(block);
909 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 }
911
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100912 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000913 DCHECK_EQ(predecessors_.size(), 2u);
914 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100915 }
916
David Brazdil769c9e52015-04-27 13:54:09 +0100917 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000918 DCHECK_EQ(successors_.size(), 2u);
919 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100920 }
921
David Brazdilfc6a86a2015-06-26 10:33:45 +0000922 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000923 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100924 }
925
David Brazdilfc6a86a2015-06-26 10:33:45 +0000926 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000927 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100928 }
929
David Brazdilfc6a86a2015-06-26 10:33:45 +0000930 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000931 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100932 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000933 }
934
935 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100937 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000938 }
939
940 // Returns whether the first occurrence of `predecessor` in the list of
941 // predecessors is at index `idx`.
942 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100943 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000944 return GetPredecessorIndexOf(predecessor) == idx;
945 }
946
David Brazdild7558da2015-09-22 13:04:14 +0100947 // Create a new block between this block and its predecessors. The new block
948 // is added to the graph, all predecessor edges are relinked to it and an edge
949 // is created to `this`. Returns the new empty block. Reverse post order or
950 // loop and try/catch information are not updated.
951 HBasicBlock* CreateImmediateDominator();
952
David Brazdilfc6a86a2015-06-26 10:33:45 +0000953 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100954 // created, latter block. Note that this method will add the block to the
955 // graph, create a Goto at the end of the former block and will create an edge
956 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100957 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 HBasicBlock* SplitBefore(HInstruction* cursor);
959
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000960 // Split the block into two blocks just after `cursor`. Returns the newly
961 // created block. Note that this method just updates raw block information,
962 // like predecessors, successors, dominators, and instruction list. It does not
963 // update the graph, reverse post order, loop information, nor make sure the
964 // blocks are consistent (for example ending with a control flow instruction).
965 HBasicBlock* SplitAfter(HInstruction* cursor);
966
David Brazdil9bc43612015-11-05 21:25:24 +0000967 // Split catch block into two blocks after the original move-exception bytecode
968 // instruction, or at the beginning if not present. Returns the newly created,
969 // latter block, or nullptr if such block could not be created (must be dead
970 // in that case). Note that this method just updates raw block information,
971 // like predecessors, successors, dominators, and instruction list. It does not
972 // update the graph, reverse post order, loop information, nor make sure the
973 // blocks are consistent (for example ending with a control flow instruction).
974 HBasicBlock* SplitCatchBlockAfterMoveException();
975
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000976 // Merge `other` at the end of `this`. Successors and dominated blocks of
977 // `other` are changed to be successors and dominated blocks of `this`. Note
978 // that this method does not update the graph, reverse post order, loop
979 // information, nor make sure the blocks are consistent (for example ending
980 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100981 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000982
983 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
984 // of `this` are moved to `other`.
985 // Note that this method does not update the graph, reverse post order, loop
986 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000987 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000988 void ReplaceWith(HBasicBlock* other);
989
David Brazdil2d7352b2015-04-20 14:52:42 +0100990 // Merge `other` at the end of `this`. This method updates loops, reverse post
991 // order, links to predecessors, successors, dominators and deletes the block
992 // from the graph. The two blocks must be successive, i.e. `this` the only
993 // predecessor of `other` and vice versa.
994 void MergeWith(HBasicBlock* other);
995
996 // Disconnects `this` from all its predecessors, successors and dominator,
997 // removes it from all loops it is included in and eventually from the graph.
998 // The block must not dominate any other block. Predecessors and successors
999 // are safely updated.
1000 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001001
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001003 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001004 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001005 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001006 // Replace instruction `initial` with `replacement` within this block.
1007 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1008 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001009 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001010 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001011 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1012 // instruction list. With 'ensure_safety' set to true, it verifies that the
1013 // instruction is not in use and removes it from the use lists of its inputs.
1014 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1015 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001016 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001017
1018 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001019 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001020 }
1021
Roland Levillain6b879dd2014-09-22 17:13:44 +01001022 bool IsLoopPreHeaderFirstPredecessor() const {
1023 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001024 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001025 }
1026
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001027 bool IsFirstPredecessorBackEdge() const {
1028 DCHECK(IsLoopHeader());
1029 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1030 }
1031
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001032 HLoopInformation* GetLoopInformation() const {
1033 return loop_information_;
1034 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001036 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001037 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001038 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001039 void SetInLoop(HLoopInformation* info) {
1040 if (IsLoopHeader()) {
1041 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001042 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001043 loop_information_ = info;
1044 } else if (loop_information_->Contains(*info->GetHeader())) {
1045 // Block is currently part of an outer loop. Make it part of this inner loop.
1046 // Note that a non loop header having a loop information means this loop information
1047 // has already been populated
1048 loop_information_ = info;
1049 } else {
1050 // Block is part of an inner loop. Do not update the loop information.
1051 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1052 // at this point, because this method is being called while populating `info`.
1053 }
1054 }
1055
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001056 // Raw update of the loop information.
1057 void SetLoopInformation(HLoopInformation* info) {
1058 loop_information_ = info;
1059 }
1060
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001061 bool IsInLoop() const { return loop_information_ != nullptr; }
1062
David Brazdilec16f792015-08-19 15:04:01 +01001063 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1064
1065 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1066 try_catch_information_ = try_catch_information;
1067 }
1068
1069 bool IsTryBlock() const {
1070 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1071 }
1072
1073 bool IsCatchBlock() const {
1074 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1075 }
David Brazdilffee3d32015-07-06 11:48:53 +01001076
1077 // Returns the try entry that this block's successors should have. They will
1078 // be in the same try, unless the block ends in a try boundary. In that case,
1079 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001080 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001081
David Brazdild7558da2015-09-22 13:04:14 +01001082 bool HasThrowingInstructions() const;
1083
David Brazdila4b8c212015-05-07 09:59:30 +01001084 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001085 bool Dominates(HBasicBlock* block) const;
1086
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001087 size_t GetLifetimeStart() const { return lifetime_start_; }
1088 size_t GetLifetimeEnd() const { return lifetime_end_; }
1089
1090 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1091 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1092
David Brazdil8d5b8b22015-03-24 10:51:52 +00001093 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001094 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001095 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001096 bool HasSinglePhi() const;
1097
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001098 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001099 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001100 ArenaVector<HBasicBlock*> predecessors_;
1101 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001102 HInstructionList instructions_;
1103 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001104 HLoopInformation* loop_information_;
1105 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001106 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001107 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001108 // The dex program counter of the first instruction of this block.
1109 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001110 size_t lifetime_start_;
1111 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001112 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001113
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001114 friend class HGraph;
1115 friend class HInstruction;
1116
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001117 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1118};
1119
David Brazdilb2bd1c52015-03-25 11:17:37 +00001120// Iterates over the LoopInformation of all loops which contain 'block'
1121// from the innermost to the outermost.
1122class HLoopInformationOutwardIterator : public ValueObject {
1123 public:
1124 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1125 : current_(block.GetLoopInformation()) {}
1126
1127 bool Done() const { return current_ == nullptr; }
1128
1129 void Advance() {
1130 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001131 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001132 }
1133
1134 HLoopInformation* Current() const {
1135 DCHECK(!Done());
1136 return current_;
1137 }
1138
1139 private:
1140 HLoopInformation* current_;
1141
1142 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1143};
1144
Alexandre Ramesef20f712015-06-09 10:29:30 +01001145#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001146 M(Above, Condition) \
1147 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001148 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001149 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001150 M(ArrayGet, Instruction) \
1151 M(ArrayLength, Instruction) \
1152 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001153 M(Below, Condition) \
1154 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001155 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001156 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001157 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001158 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001159 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001160 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001161 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001162 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001163 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001164 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001165 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001166 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001167 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001168 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001169 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001170 M(FloatConstant, Constant) \
1171 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 M(GreaterThan, Condition) \
1173 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001175 M(InstanceFieldGet, Instruction) \
1176 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001177 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001179 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001180 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001181 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(LessThan, Condition) \
1184 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001185 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001186 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001188 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(Local, Instruction) \
1190 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001191 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001192 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001194 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001195 M(Neg, UnaryOperation) \
1196 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001198 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001202 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001203 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001207 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(Return, Instruction) \
1209 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001210 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001211 M(Shl, BinaryOperation) \
1212 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001213 M(StaticFieldGet, Instruction) \
1214 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001215 M(UnresolvedInstanceFieldGet, Instruction) \
1216 M(UnresolvedInstanceFieldSet, Instruction) \
1217 M(UnresolvedStaticFieldGet, Instruction) \
1218 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(StoreLocal, Instruction) \
1220 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001223 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001224 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001225 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001226 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001227 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001228
Vladimir Markob4536b72015-11-24 13:45:23 +00001229#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001230#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001231#else
1232#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1233 M(ArmDexCacheArraysBase, Instruction)
1234#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001235
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001236#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001237#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001238#else
1239#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001240 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001241 M(Arm64IntermediateAddress, Instruction) \
1242 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001243#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001244
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1246
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001247#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1248
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001249#ifndef ART_ENABLE_CODEGEN_x86
1250#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1251#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001252#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1253 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001254 M(X86LoadFromConstantTable, Instruction) \
1255 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001256#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001257
1258#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1259
1260#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1261 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1262 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1263 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001264 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001265 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001266 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1267 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1268
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001269#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1270 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001272 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001273 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001274 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001275
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001276#define FOR_EACH_INSTRUCTION(M) \
1277 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1278 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1279
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001281FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1282#undef FORWARD_DECLARATION
1283
Roland Levillainccc07a92014-09-16 14:48:16 +01001284#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001285 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001286 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001287 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001288 return other->Is##type(); \
1289 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001290 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001292#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1293 bool Is##type() const { return As##type() != nullptr; } \
1294 const H##type* As##type() const { return this; } \
1295 H##type* As##type() { return this; }
1296
David Brazdiled596192015-01-23 10:39:45 +00001297template <typename T> class HUseList;
1298
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001300class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001301 public:
David Brazdiled596192015-01-23 10:39:45 +00001302 HUseListNode* GetPrevious() const { return prev_; }
1303 HUseListNode* GetNext() const { return next_; }
1304 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001305 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001306 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001307
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001308 private:
David Brazdiled596192015-01-23 10:39:45 +00001309 HUseListNode(T user, size_t index)
1310 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1311
1312 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001313 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001314 HUseListNode<T>* prev_;
1315 HUseListNode<T>* next_;
1316
1317 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318
1319 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1320};
1321
David Brazdiled596192015-01-23 10:39:45 +00001322template <typename T>
1323class HUseList : public ValueObject {
1324 public:
1325 HUseList() : first_(nullptr) {}
1326
1327 void Clear() {
1328 first_ = nullptr;
1329 }
1330
1331 // Adds a new entry at the beginning of the use list and returns
1332 // the newly created node.
1333 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001334 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001335 if (IsEmpty()) {
1336 first_ = new_node;
1337 } else {
1338 first_->prev_ = new_node;
1339 new_node->next_ = first_;
1340 first_ = new_node;
1341 }
1342 return new_node;
1343 }
1344
1345 HUseListNode<T>* GetFirst() const {
1346 return first_;
1347 }
1348
1349 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001350 DCHECK(node != nullptr);
1351 DCHECK(Contains(node));
1352
David Brazdiled596192015-01-23 10:39:45 +00001353 if (node->prev_ != nullptr) {
1354 node->prev_->next_ = node->next_;
1355 }
1356 if (node->next_ != nullptr) {
1357 node->next_->prev_ = node->prev_;
1358 }
1359 if (node == first_) {
1360 first_ = node->next_;
1361 }
1362 }
1363
David Brazdil1abb4192015-02-17 18:33:36 +00001364 bool Contains(const HUseListNode<T>* node) const {
1365 if (node == nullptr) {
1366 return false;
1367 }
1368 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1369 if (current == node) {
1370 return true;
1371 }
1372 }
1373 return false;
1374 }
1375
David Brazdiled596192015-01-23 10:39:45 +00001376 bool IsEmpty() const {
1377 return first_ == nullptr;
1378 }
1379
1380 bool HasOnlyOneUse() const {
1381 return first_ != nullptr && first_->next_ == nullptr;
1382 }
1383
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001384 size_t SizeSlow() const {
1385 size_t count = 0;
1386 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1387 ++count;
1388 }
1389 return count;
1390 }
1391
David Brazdiled596192015-01-23 10:39:45 +00001392 private:
1393 HUseListNode<T>* first_;
1394};
1395
1396template<typename T>
1397class HUseIterator : public ValueObject {
1398 public:
1399 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1400
1401 bool Done() const { return current_ == nullptr; }
1402
1403 void Advance() {
1404 DCHECK(!Done());
1405 current_ = current_->GetNext();
1406 }
1407
1408 HUseListNode<T>* Current() const {
1409 DCHECK(!Done());
1410 return current_;
1411 }
1412
1413 private:
1414 HUseListNode<T>* current_;
1415
1416 friend class HValue;
1417};
1418
David Brazdil1abb4192015-02-17 18:33:36 +00001419// This class is used by HEnvironment and HInstruction classes to record the
1420// instructions they use and pointers to the corresponding HUseListNodes kept
1421// by the used instructions.
1422template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001423class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001424 public:
1425 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1426 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1427
1428 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1429 : instruction_(old_record.instruction_), use_node_(use_node) {
1430 DCHECK(instruction_ != nullptr);
1431 DCHECK(use_node_ != nullptr);
1432 DCHECK(old_record.use_node_ == nullptr);
1433 }
1434
1435 HInstruction* GetInstruction() const { return instruction_; }
1436 HUseListNode<T>* GetUseNode() const { return use_node_; }
1437
1438 private:
1439 // Instruction used by the user.
1440 HInstruction* instruction_;
1441
1442 // Corresponding entry in the use list kept by 'instruction_'.
1443 HUseListNode<T>* use_node_;
1444};
1445
Aart Bik854a02b2015-07-14 16:07:00 -07001446/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001448 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 * For write/read dependences on fields/arrays, the dependence analysis uses
1450 * type disambiguation (e.g. a float field write cannot modify the value of an
1451 * integer field read) and the access type (e.g. a reference array write cannot
1452 * modify the value of a reference field read [although it may modify the
1453 * reference fetch prior to reading the field, which is represented by its own
1454 * write/read dependence]). The analysis makes conservative points-to
1455 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1456 * the same, and any reference read depends on any reference read without
1457 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001458 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 * The internal representation uses 38-bit and is described in the table below.
1460 * The first line indicates the side effect, and for field/array accesses the
1461 * second line indicates the type of the access (in the order of the
1462 * Primitive::Type enum).
1463 * The two numbered lines below indicate the bit position in the bitfield (read
1464 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001465 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1467 * +-------------+---------+---------+--------------+---------+---------+
1468 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1469 * | 3 |333333322|222222221| 1 |111111110|000000000|
1470 * | 7 |654321098|765432109| 8 |765432109|876543210|
1471 *
1472 * Note that, to ease the implementation, 'changes' bits are least significant
1473 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001474 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475class SideEffects : public ValueObject {
1476 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001477 SideEffects() : flags_(0) {}
1478
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 static SideEffects None() {
1480 return SideEffects(0);
1481 }
1482
1483 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001484 return SideEffects(kAllChangeBits | kAllDependOnBits);
1485 }
1486
1487 static SideEffects AllChanges() {
1488 return SideEffects(kAllChangeBits);
1489 }
1490
1491 static SideEffects AllDependencies() {
1492 return SideEffects(kAllDependOnBits);
1493 }
1494
1495 static SideEffects AllExceptGCDependency() {
1496 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1497 }
1498
1499 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001500 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 }
1502
Aart Bik34c3ba92015-07-20 14:08:59 -07001503 static SideEffects AllWrites() {
1504 return SideEffects(kAllWrites);
1505 }
1506
1507 static SideEffects AllReads() {
1508 return SideEffects(kAllReads);
1509 }
1510
1511 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1512 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001513 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001514 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 }
1516
Aart Bik854a02b2015-07-14 16:07:00 -07001517 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1518 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 }
1520
Aart Bik34c3ba92015-07-20 14:08:59 -07001521 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1522 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001524 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001525 }
1526
1527 static SideEffects ArrayReadOfType(Primitive::Type type) {
1528 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1529 }
1530
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001531 static SideEffects CanTriggerGC() {
1532 return SideEffects(1ULL << kCanTriggerGCBit);
1533 }
1534
1535 static SideEffects DependsOnGC() {
1536 return SideEffects(1ULL << kDependsOnGCBit);
1537 }
1538
Aart Bik854a02b2015-07-14 16:07:00 -07001539 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001540 SideEffects Union(SideEffects other) const {
1541 return SideEffects(flags_ | other.flags_);
1542 }
1543
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 SideEffects Exclusion(SideEffects other) const {
1545 return SideEffects(flags_ & ~other.flags_);
1546 }
1547
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001548 void Add(SideEffects other) {
1549 flags_ |= other.flags_;
1550 }
1551
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 bool Includes(SideEffects other) const {
1553 return (other.flags_ & flags_) == other.flags_;
1554 }
1555
1556 bool HasSideEffects() const {
1557 return (flags_ & kAllChangeBits);
1558 }
1559
1560 bool HasDependencies() const {
1561 return (flags_ & kAllDependOnBits);
1562 }
1563
1564 // Returns true if there are no side effects or dependencies.
1565 bool DoesNothing() const {
1566 return flags_ == 0;
1567 }
1568
Aart Bik854a02b2015-07-14 16:07:00 -07001569 // Returns true if something is written.
1570 bool DoesAnyWrite() const {
1571 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001572 }
1573
Aart Bik854a02b2015-07-14 16:07:00 -07001574 // Returns true if something is read.
1575 bool DoesAnyRead() const {
1576 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001577 }
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579 // Returns true if potentially everything is written and read
1580 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 bool DoesAllReadWrite() const {
1582 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1583 }
1584
Aart Bik854a02b2015-07-14 16:07:00 -07001585 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001587 }
1588
Roland Levillain0d5a2812015-11-13 10:07:31 +00001589 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001590 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1592 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001593 }
1594
1595 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001597 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001598 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 for (int s = kLastBit; s >= 0; s--) {
1600 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1601 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1602 // This is a bit for the GC side effect.
1603 if (current_bit_is_set) {
1604 flags += "GC";
1605 }
Aart Bik854a02b2015-07-14 16:07:00 -07001606 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001607 } else {
1608 // This is a bit for the array/field analysis.
1609 // The underscore character stands for the 'can trigger GC' bit.
1610 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1611 if (current_bit_is_set) {
1612 flags += kDebug[s];
1613 }
1614 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1615 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1616 flags += "|";
1617 }
1618 }
Aart Bik854a02b2015-07-14 16:07:00 -07001619 }
1620 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001621 }
1622
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 private:
1626 static constexpr int kFieldArrayAnalysisBits = 9;
1627
1628 static constexpr int kFieldWriteOffset = 0;
1629 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1630 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1631 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1632
1633 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1634
1635 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1636 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1637 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1638 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1639
1640 static constexpr int kLastBit = kDependsOnGCBit;
1641 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1642
1643 // Aliases.
1644
1645 static_assert(kChangeBits == kDependOnBits,
1646 "the 'change' bits should match the 'depend on' bits.");
1647
1648 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1649 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1650 static constexpr uint64_t kAllWrites =
1651 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1652 static constexpr uint64_t kAllReads =
1653 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001654
Aart Bik854a02b2015-07-14 16:07:00 -07001655 // Work around the fact that HIR aliases I/F and J/D.
1656 // TODO: remove this interceptor once HIR types are clean
1657 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1658 switch (type) {
1659 case Primitive::kPrimInt:
1660 case Primitive::kPrimFloat:
1661 return TypeFlag(Primitive::kPrimInt, offset) |
1662 TypeFlag(Primitive::kPrimFloat, offset);
1663 case Primitive::kPrimLong:
1664 case Primitive::kPrimDouble:
1665 return TypeFlag(Primitive::kPrimLong, offset) |
1666 TypeFlag(Primitive::kPrimDouble, offset);
1667 default:
1668 return TypeFlag(type, offset);
1669 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001670 }
1671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Translates type to bit flag.
1673 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1674 CHECK_NE(type, Primitive::kPrimVoid);
1675 const uint64_t one = 1;
1676 const int shift = type; // 0-based consecutive enum
1677 DCHECK_LE(kFieldWriteOffset, shift);
1678 DCHECK_LT(shift, kArrayWriteOffset);
1679 return one << (type + offset);
1680 }
1681
1682 // Private constructor on direct flags value.
1683 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1684
1685 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686};
1687
David Brazdiled596192015-01-23 10:39:45 +00001688// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001689class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001690 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001691 HEnvironment(ArenaAllocator* arena,
1692 size_t number_of_vregs,
1693 const DexFile& dex_file,
1694 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001695 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001696 InvokeType invoke_type,
1697 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001698 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1699 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001700 parent_(nullptr),
1701 dex_file_(dex_file),
1702 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001703 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001704 invoke_type_(invoke_type),
1705 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001706 }
1707
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001708 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001709 : HEnvironment(arena,
1710 to_copy.Size(),
1711 to_copy.GetDexFile(),
1712 to_copy.GetMethodIdx(),
1713 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001714 to_copy.GetInvokeType(),
1715 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001716
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001717 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001718 if (parent_ != nullptr) {
1719 parent_->SetAndCopyParentChain(allocator, parent);
1720 } else {
1721 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1722 parent_->CopyFrom(parent);
1723 if (parent->GetParent() != nullptr) {
1724 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1725 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001726 }
David Brazdiled596192015-01-23 10:39:45 +00001727 }
1728
Vladimir Marko71bf8092015-09-15 15:33:14 +01001729 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001730 void CopyFrom(HEnvironment* environment);
1731
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001732 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1733 // input to the loop phi instead. This is for inserting instructions that
1734 // require an environment (like HDeoptimization) in the loop pre-header.
1735 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001736
1737 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001738 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001739 }
1740
1741 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001743 }
1744
David Brazdil1abb4192015-02-17 18:33:36 +00001745 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001746
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001747 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001748
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 HEnvironment* GetParent() const { return parent_; }
1750
1751 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001752 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001753 }
1754
1755 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001756 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 }
1758
1759 uint32_t GetDexPc() const {
1760 return dex_pc_;
1761 }
1762
1763 uint32_t GetMethodIdx() const {
1764 return method_idx_;
1765 }
1766
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001767 InvokeType GetInvokeType() const {
1768 return invoke_type_;
1769 }
1770
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001771 const DexFile& GetDexFile() const {
1772 return dex_file_;
1773 }
1774
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001775 HInstruction* GetHolder() const {
1776 return holder_;
1777 }
1778
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001779
1780 bool IsFromInlinedInvoke() const {
1781 return GetParent() != nullptr;
1782 }
1783
David Brazdiled596192015-01-23 10:39:45 +00001784 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001785 // Record instructions' use entries of this environment for constant-time removal.
1786 // It should only be called by HInstruction when a new environment use is added.
1787 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1788 DCHECK(env_use->GetUser() == this);
1789 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001790 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001791 }
David Brazdiled596192015-01-23 10:39:45 +00001792
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001793 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1794 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001795 HEnvironment* parent_;
1796 const DexFile& dex_file_;
1797 const uint32_t method_idx_;
1798 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001799 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001800
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001801 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001802 HInstruction* const holder_;
1803
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001804 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001805
1806 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1807};
1808
Vladimir Markof9f64412015-09-02 14:05:49 +01001809class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001811 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812 : previous_(nullptr),
1813 next_(nullptr),
1814 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001815 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001816 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001817 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001818 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001819 locations_(nullptr),
1820 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001821 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001822 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001823 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001824
Dave Allison20dfc792014-06-16 20:44:29 -07001825 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001827#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001828 enum InstructionKind {
1829 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1830 };
1831#undef DECLARE_KIND
1832
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001833 HInstruction* GetNext() const { return next_; }
1834 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001835
Calin Juravle77520bc2015-01-12 18:45:46 +00001836 HInstruction* GetNextDisregardingMoves() const;
1837 HInstruction* GetPreviousDisregardingMoves() const;
1838
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001839 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001840 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001841 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001842 bool IsInBlock() const { return block_ != nullptr; }
1843 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001844 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1845 bool IsIrreducibleLoopHeaderPhi() const {
1846 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1847 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001848
Roland Levillain6b879dd2014-09-22 17:13:44 +01001849 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001850 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001851
1852 virtual void Accept(HGraphVisitor* visitor) = 0;
1853 virtual const char* DebugName() const = 0;
1854
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001855 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001856 void SetRawInputAt(size_t index, HInstruction* input) {
1857 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001861
1862 uint32_t GetDexPc() const { return dex_pc_; }
1863
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001864 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001865
Roland Levillaine161a2a2014-10-03 12:45:18 +01001866 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001867 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001868
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001869 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001870 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871
Calin Juravle10e244f2015-01-26 18:54:32 +00001872 // Does not apply for all instructions, but having this at top level greatly
1873 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001874 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001875 virtual bool CanBeNull() const {
1876 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1877 return true;
1878 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001880 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001881 return false;
1882 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001883
Calin Juravle2e768302015-07-28 14:41:11 +00001884 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001885
Calin Juravle61d544b2015-02-23 16:46:57 +00001886 ReferenceTypeInfo GetReferenceTypeInfo() const {
1887 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1888 return reference_type_info_;
1889 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001891 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001892 DCHECK(user != nullptr);
1893 HUseListNode<HInstruction*>* use =
1894 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1895 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001896 }
1897
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001898 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001899 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001900 HUseListNode<HEnvironment*>* env_use =
1901 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1902 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001903 }
1904
David Brazdil1abb4192015-02-17 18:33:36 +00001905 void RemoveAsUserOfInput(size_t input) {
1906 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1907 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1908 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909
David Brazdil1abb4192015-02-17 18:33:36 +00001910 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1911 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001912
David Brazdiled596192015-01-23 10:39:45 +00001913 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1914 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001915 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001916 bool HasOnlyOneNonEnvironmentUse() const {
1917 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1918 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919
Roland Levillain6c82d402014-10-13 16:10:27 +01001920 // Does this instruction strictly dominate `other_instruction`?
1921 // Returns false if this instruction and `other_instruction` are the same.
1922 // Aborts if this instruction and `other_instruction` are both phis.
1923 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001924
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001925 int GetId() const { return id_; }
1926 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001927
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001928 int GetSsaIndex() const { return ssa_index_; }
1929 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1930 bool HasSsaIndex() const { return ssa_index_ != -1; }
1931
1932 bool HasEnvironment() const { return environment_ != nullptr; }
1933 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001934 // Set the `environment_` field. Raw because this method does not
1935 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001936 void SetRawEnvironment(HEnvironment* environment) {
1937 DCHECK(environment_ == nullptr);
1938 DCHECK_EQ(environment->GetHolder(), this);
1939 environment_ = environment;
1940 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001941
1942 // Set the environment of this instruction, copying it from `environment`. While
1943 // copying, the uses lists are being updated.
1944 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001946 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001948 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001949 if (environment->GetParent() != nullptr) {
1950 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1951 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001952 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001953
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001954 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1955 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001956 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001957 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001959 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001960 if (environment->GetParent() != nullptr) {
1961 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1962 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001963 }
1964
Nicolas Geoffray39468442014-09-02 15:17:15 +01001965 // Returns the number of entries in the environment. Typically, that is the
1966 // number of dex registers in a method. It could be more in case of inlining.
1967 size_t EnvironmentSize() const;
1968
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001969 LocationSummary* GetLocations() const { return locations_; }
1970 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001972 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001973 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
Alexandre Rames188d4312015-04-09 18:30:21 +01001975 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1976 // uses of this instruction by `other` are *not* updated.
1977 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1978 ReplaceWith(other);
1979 other->ReplaceInput(this, use_index);
1980 }
1981
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001982 // Move `this` instruction before `cursor`.
1983 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001984
Vladimir Markofb337ea2015-11-25 15:25:10 +00001985 // Move `this` before its first user and out of any loops. If there is no
1986 // out-of-loop user that dominates all other users, move the instruction
1987 // to the end of the out-of-loop common dominator of the user's blocks.
1988 //
1989 // This can be used only on non-throwing instructions with no side effects that
1990 // have at least one use but no environment uses.
1991 void MoveBeforeFirstUserAndOutOfLoops();
1992
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001993#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 bool Is##type() const; \
1995 const H##type* As##type() const; \
1996 H##type* As##type();
1997
1998 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1999#undef INSTRUCTION_TYPE_CHECK
2000
2001#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002002 bool Is##type() const { return (As##type() != nullptr); } \
2003 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002004 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002006#undef INSTRUCTION_TYPE_CHECK
2007
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008 // Returns whether the instruction can be moved within the graph.
2009 virtual bool CanBeMoved() const { return false; }
2010
2011 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002012 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002013 return false;
2014 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002015
2016 // Returns whether any data encoded in the two instructions is equal.
2017 // This method does not look at the inputs. Both instructions must be
2018 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002019 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002020 return false;
2021 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002022
2023 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002024 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002025 // 2) Their inputs are identical.
2026 bool Equals(HInstruction* other) const;
2027
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2029 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2030 // the virtual function because the __attribute__((__pure__)) doesn't really
2031 // apply the strong requirement for virtual functions, preventing optimizations.
2032 InstructionKind GetKind() const PURE;
2033 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002034
2035 virtual size_t ComputeHashCode() const {
2036 size_t result = GetKind();
2037 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2038 result = (result * 31) + InputAt(i)->GetId();
2039 }
2040 return result;
2041 }
2042
2043 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002044 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002045
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002046 size_t GetLifetimePosition() const { return lifetime_position_; }
2047 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2048 LiveInterval* GetLiveInterval() const { return live_interval_; }
2049 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2050 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2051
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002052 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2053
2054 // Returns whether the code generation of the instruction will require to have access
2055 // to the current method. Such instructions are:
2056 // (1): Instructions that require an environment, as calling the runtime requires
2057 // to walk the stack and have the current method stored at a specific stack address.
2058 // (2): Object literals like classes and strings, that are loaded from the dex cache
2059 // fields of the current method.
2060 bool NeedsCurrentMethod() const {
2061 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2062 }
2063
Vladimir Markodc151b22015-10-15 18:02:30 +01002064 // Returns whether the code generation of the instruction will require to have access
2065 // to the dex cache of the current method's declaring class via the current method.
2066 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002067
Mark Mendellc4701932015-04-10 13:18:51 -04002068 // Does this instruction have any use in an environment before
2069 // control flow hits 'other'?
2070 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2071
2072 // Remove all references to environment uses of this instruction.
2073 // The caller must ensure that this is safe to do.
2074 void RemoveEnvironmentUsers();
2075
David Brazdil1abb4192015-02-17 18:33:36 +00002076 protected:
2077 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2078 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002079 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002080
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002082 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2083
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084 HInstruction* previous_;
2085 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002086 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002087 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002089 // An instruction gets an id when it is added to the graph.
2090 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002091 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002092 int id_;
2093
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002094 // When doing liveness analysis, instructions that have uses get an SSA index.
2095 int ssa_index_;
2096
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002097 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002098 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002099
2100 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002101 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002102
Nicolas Geoffray39468442014-09-02 15:17:15 +01002103 // The environment associated with this instruction. Not null if the instruction
2104 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002105 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002106
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002107 // Set by the code generator.
2108 LocationSummary* locations_;
2109
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002110 // Set by the liveness analysis.
2111 LiveInterval* live_interval_;
2112
2113 // Set by the liveness analysis, this is the position in a linear
2114 // order of blocks where this instruction's live interval start.
2115 size_t lifetime_position_;
2116
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002117 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002118
Calin Juravleacf735c2015-02-12 15:25:22 +00002119 // TODO: for primitive types this should be marked as invalid.
2120 ReferenceTypeInfo reference_type_info_;
2121
David Brazdil1abb4192015-02-17 18:33:36 +00002122 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002123 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002124 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002125 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127
2128 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2129};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002130std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132class HInputIterator : public ValueObject {
2133 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002134 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002135
2136 bool Done() const { return index_ == instruction_->InputCount(); }
2137 HInstruction* Current() const { return instruction_->InputAt(index_); }
2138 void Advance() { index_++; }
2139
2140 private:
2141 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002142 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002143
2144 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2145};
2146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147class HInstructionIterator : public ValueObject {
2148 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002149 explicit HInstructionIterator(const HInstructionList& instructions)
2150 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002151 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152 }
2153
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002154 bool Done() const { return instruction_ == nullptr; }
2155 HInstruction* Current() const { return instruction_; }
2156 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002158 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159 }
2160
2161 private:
2162 HInstruction* instruction_;
2163 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002164
2165 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166};
2167
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002168class HBackwardInstructionIterator : public ValueObject {
2169 public:
2170 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2171 : instruction_(instructions.last_instruction_) {
2172 next_ = Done() ? nullptr : instruction_->GetPrevious();
2173 }
2174
2175 bool Done() const { return instruction_ == nullptr; }
2176 HInstruction* Current() const { return instruction_; }
2177 void Advance() {
2178 instruction_ = next_;
2179 next_ = Done() ? nullptr : instruction_->GetPrevious();
2180 }
2181
2182 private:
2183 HInstruction* instruction_;
2184 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002185
2186 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002187};
2188
Vladimir Markof9f64412015-09-02 14:05:49 +01002189template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002190class HTemplateInstruction: public HInstruction {
2191 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002192 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002194 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002199 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2200 DCHECK_LT(i, N);
2201 return inputs_[i];
2202 }
David Brazdil1abb4192015-02-17 18:33:36 +00002203
2204 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002205 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002206 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002207 }
2208
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002210 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002211
2212 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213};
2214
Vladimir Markof9f64412015-09-02 14:05:49 +01002215// HTemplateInstruction specialization for N=0.
2216template<>
2217class HTemplateInstruction<0>: public HInstruction {
2218 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002219 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002220 : HInstruction(side_effects, dex_pc) {}
2221
Vladimir Markof9f64412015-09-02 14:05:49 +01002222 virtual ~HTemplateInstruction() {}
2223
2224 size_t InputCount() const OVERRIDE { return 0; }
2225
2226 protected:
2227 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2228 LOG(FATAL) << "Unreachable";
2229 UNREACHABLE();
2230 }
2231
2232 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2233 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2234 LOG(FATAL) << "Unreachable";
2235 UNREACHABLE();
2236 }
2237
2238 private:
2239 friend class SsaBuilder;
2240};
2241
Dave Allison20dfc792014-06-16 20:44:29 -07002242template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002243class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002244 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002245 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002246 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002247 virtual ~HExpression() {}
2248
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002249 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002250
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002251 protected:
2252 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002253};
2254
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2256// instruction that branches to the exit block.
2257class HReturnVoid : public HTemplateInstruction<0> {
2258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002259 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2260 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002261
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002262 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002264 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265
2266 private:
2267 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2268};
2269
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002270// Represents dex's RETURN opcodes. A HReturn is a control flow
2271// instruction that branches to the exit block.
2272class HReturn : public HTemplateInstruction<1> {
2273 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002274 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2275 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002276 SetRawInputAt(0, value);
2277 }
2278
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002279 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002280
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002281 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002282
2283 private:
2284 DISALLOW_COPY_AND_ASSIGN(HReturn);
2285};
2286
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002287// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002288// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289// exit block.
2290class HExit : public HTemplateInstruction<0> {
2291 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002292 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002294 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002295
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002296 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002297
2298 private:
2299 DISALLOW_COPY_AND_ASSIGN(HExit);
2300};
2301
2302// Jumps from one block to another.
2303class HGoto : public HTemplateInstruction<0> {
2304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002305 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002306
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002307 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002308
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002309 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002310 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002311 }
2312
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002313 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314
2315 private:
2316 DISALLOW_COPY_AND_ASSIGN(HGoto);
2317};
2318
Roland Levillain9867bc72015-08-05 10:21:34 +01002319class HConstant : public HExpression<0> {
2320 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2322 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002323
2324 bool CanBeMoved() const OVERRIDE { return true; }
2325
2326 virtual bool IsMinusOne() const { return false; }
2327 virtual bool IsZero() const { return false; }
2328 virtual bool IsOne() const { return false; }
2329
David Brazdil77a48ae2015-09-15 12:34:04 +00002330 virtual uint64_t GetValueAsUint64() const = 0;
2331
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002332 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002333
2334 private:
2335 DISALLOW_COPY_AND_ASSIGN(HConstant);
2336};
2337
2338class HNullConstant : public HConstant {
2339 public:
2340 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2341 return true;
2342 }
2343
David Brazdil77a48ae2015-09-15 12:34:04 +00002344 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2345
Roland Levillain9867bc72015-08-05 10:21:34 +01002346 size_t ComputeHashCode() const OVERRIDE { return 0; }
2347
2348 DECLARE_INSTRUCTION(NullConstant);
2349
2350 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002352
2353 friend class HGraph;
2354 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2355};
2356
2357// Constants of the type int. Those can be from Dex instructions, or
2358// synthesized (for example with the if-eqz instruction).
2359class HIntConstant : public HConstant {
2360 public:
2361 int32_t GetValue() const { return value_; }
2362
David Brazdil9f389d42015-10-01 14:32:56 +01002363 uint64_t GetValueAsUint64() const OVERRIDE {
2364 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2365 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002366
Roland Levillain9867bc72015-08-05 10:21:34 +01002367 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2368 DCHECK(other->IsIntConstant());
2369 return other->AsIntConstant()->value_ == value_;
2370 }
2371
2372 size_t ComputeHashCode() const OVERRIDE { return 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(IntConstant);
2379
2380 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002381 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2382 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2383 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2384 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002385
2386 const int32_t value_;
2387
2388 friend class HGraph;
2389 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2390 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2391 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2392};
2393
2394class HLongConstant : public HConstant {
2395 public:
2396 int64_t GetValue() const { return value_; }
2397
David Brazdil77a48ae2015-09-15 12:34:04 +00002398 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2399
Roland Levillain9867bc72015-08-05 10:21:34 +01002400 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2401 DCHECK(other->IsLongConstant());
2402 return other->AsLongConstant()->value_ == value_;
2403 }
2404
2405 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2406
2407 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2408 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2409 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2410
2411 DECLARE_INSTRUCTION(LongConstant);
2412
2413 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002414 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2415 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002416
2417 const int64_t value_;
2418
2419 friend class HGraph;
2420 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2421};
Dave Allison20dfc792014-06-16 20:44:29 -07002422
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002423// Conditional branch. A block ending with an HIf instruction must have
2424// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002425class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002426 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002427 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2428 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002429 SetRawInputAt(0, input);
2430 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002431
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002432 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002433
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002434 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002435 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002436 }
2437
2438 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002439 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002440 }
2441
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002442 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002443
2444 private:
2445 DISALLOW_COPY_AND_ASSIGN(HIf);
2446};
2447
David Brazdilfc6a86a2015-06-26 10:33:45 +00002448
2449// Abstract instruction which marks the beginning and/or end of a try block and
2450// links it to the respective exception handlers. Behaves the same as a Goto in
2451// non-exceptional control flow.
2452// Normal-flow successor is stored at index zero, exception handlers under
2453// higher indices in no particular order.
2454class HTryBoundary : public HTemplateInstruction<0> {
2455 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002456 enum BoundaryKind {
2457 kEntry,
2458 kExit,
2459 };
2460
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002461 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2462 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002463
2464 bool IsControlFlow() const OVERRIDE { return true; }
2465
2466 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002467 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002468
David Brazdild26a4112015-11-10 11:07:31 +00002469 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2470 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2471 }
2472
David Brazdilfc6a86a2015-06-26 10:33:45 +00002473 // Returns whether `handler` is among its exception handlers (non-zero index
2474 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002475 bool HasExceptionHandler(const HBasicBlock& handler) const {
2476 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002477 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002478 }
2479
2480 // If not present already, adds `handler` to its block's list of exception
2481 // handlers.
2482 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002483 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002484 GetBlock()->AddSuccessor(handler);
2485 }
2486 }
2487
David Brazdil56e1acc2015-06-30 15:41:36 +01002488 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002489
David Brazdilffee3d32015-07-06 11:48:53 +01002490 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2491
David Brazdilfc6a86a2015-06-26 10:33:45 +00002492 DECLARE_INSTRUCTION(TryBoundary);
2493
2494 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002495 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002496
2497 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2498};
2499
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002500// Deoptimize to interpreter, upon checking a condition.
2501class HDeoptimize : public HTemplateInstruction<1> {
2502 public:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002503 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002504 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002505 SetRawInputAt(0, cond);
2506 }
2507
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002508 bool CanBeMoved() const OVERRIDE { return true; }
2509 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2510 return true;
2511 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002512 bool NeedsEnvironment() const OVERRIDE { return true; }
2513 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002514
2515 DECLARE_INSTRUCTION(Deoptimize);
2516
2517 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002518 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2519};
2520
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002521// Represents the ArtMethod that was passed as a first argument to
2522// the method. It is used by instructions that depend on it, like
2523// instructions that work with the dex cache.
2524class HCurrentMethod : public HExpression<0> {
2525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002526 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2527 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002528
2529 DECLARE_INSTRUCTION(CurrentMethod);
2530
2531 private:
2532 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2533};
2534
Mark Mendellfe57faa2015-09-18 09:26:15 -04002535// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2536// have one successor for each entry in the switch table, and the final successor
2537// will be the block containing the next Dex opcode.
2538class HPackedSwitch : public HTemplateInstruction<1> {
2539 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002540 HPackedSwitch(int32_t start_value,
2541 uint32_t num_entries,
2542 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002543 uint32_t dex_pc = kNoDexPc)
2544 : HTemplateInstruction(SideEffects::None(), dex_pc),
2545 start_value_(start_value),
2546 num_entries_(num_entries) {
2547 SetRawInputAt(0, input);
2548 }
2549
2550 bool IsControlFlow() const OVERRIDE { return true; }
2551
2552 int32_t GetStartValue() const { return start_value_; }
2553
Vladimir Marko211c2112015-09-24 16:52:33 +01002554 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002555
2556 HBasicBlock* GetDefaultBlock() const {
2557 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002558 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002559 }
2560 DECLARE_INSTRUCTION(PackedSwitch);
2561
2562 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002563 const int32_t start_value_;
2564 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002565
2566 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2567};
2568
Roland Levillain88cb1752014-10-20 16:36:47 +01002569class HUnaryOperation : public HExpression<1> {
2570 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002571 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2572 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002573 SetRawInputAt(0, input);
2574 }
2575
2576 HInstruction* GetInput() const { return InputAt(0); }
2577 Primitive::Type GetResultType() const { return GetType(); }
2578
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002579 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002580 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002581 return true;
2582 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002583
Roland Levillain9240d6a2014-10-20 16:47:04 +01002584 // Try to statically evaluate `operation` and return a HConstant
2585 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002586 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002587 HConstant* TryStaticEvaluation() const;
2588
2589 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002590 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2591 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002592
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002593 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002594
2595 private:
2596 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2597};
2598
Dave Allison20dfc792014-06-16 20:44:29 -07002599class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002600 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002601 HBinaryOperation(Primitive::Type result_type,
2602 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002603 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002604 SideEffects side_effects = SideEffects::None(),
2605 uint32_t dex_pc = kNoDexPc)
2606 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002607 SetRawInputAt(0, left);
2608 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002609 }
2610
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002611 HInstruction* GetLeft() const { return InputAt(0); }
2612 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002613 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002614
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002615 virtual bool IsCommutative() const { return false; }
2616
2617 // Put constant on the right.
2618 // Returns whether order is changed.
2619 bool OrderInputsWithConstantOnTheRight() {
2620 HInstruction* left = InputAt(0);
2621 HInstruction* right = InputAt(1);
2622 if (left->IsConstant() && !right->IsConstant()) {
2623 ReplaceInput(right, 0);
2624 ReplaceInput(left, 1);
2625 return true;
2626 }
2627 return false;
2628 }
2629
2630 // Order inputs by instruction id, but favor constant on the right side.
2631 // This helps GVN for commutative ops.
2632 void OrderInputs() {
2633 DCHECK(IsCommutative());
2634 HInstruction* left = InputAt(0);
2635 HInstruction* right = InputAt(1);
2636 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2637 return;
2638 }
2639 if (OrderInputsWithConstantOnTheRight()) {
2640 return;
2641 }
2642 // Order according to instruction id.
2643 if (left->GetId() > right->GetId()) {
2644 ReplaceInput(right, 0);
2645 ReplaceInput(left, 1);
2646 }
2647 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002649 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002650 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002651 return true;
2652 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002653
Roland Levillain9240d6a2014-10-20 16:47:04 +01002654 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002655 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002656 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002657 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002658
2659 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2661 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2662 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2663 HLongConstant* y ATTRIBUTE_UNUSED) const {
2664 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2665 return nullptr;
2666 }
2667 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2668 HIntConstant* y ATTRIBUTE_UNUSED) const {
2669 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2670 return nullptr;
2671 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002672 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2673 HNullConstant* y ATTRIBUTE_UNUSED) const {
2674 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2675 return nullptr;
2676 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002677
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002678 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002679 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002680 HConstant* GetConstantRight() const;
2681
2682 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002683 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002684 HInstruction* GetLeastConstantLeft() const;
2685
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002686 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002687
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002688 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002689 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2690};
2691
Mark Mendellc4701932015-04-10 13:18:51 -04002692// The comparison bias applies for floating point operations and indicates how NaN
2693// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002694enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002695 kNoBias, // bias is not applicable (i.e. for long operation)
2696 kGtBias, // return 1 for NaN comparisons
2697 kLtBias, // return -1 for NaN comparisons
2698};
2699
Dave Allison20dfc792014-06-16 20:44:29 -07002700class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002701 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002702 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2703 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002704 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002705 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002706
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002707 bool NeedsMaterialization() const { return needs_materialization_; }
2708 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002709
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002710 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002711 // `instruction`, and disregard moves in between.
2712 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002713
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002714 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002715
2716 virtual IfCondition GetCondition() const = 0;
2717
Mark Mendellc4701932015-04-10 13:18:51 -04002718 virtual IfCondition GetOppositeCondition() const = 0;
2719
Roland Levillain4fa13f62015-07-06 18:11:54 +01002720 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002721
2722 void SetBias(ComparisonBias bias) { bias_ = bias; }
2723
2724 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2725 return bias_ == other->AsCondition()->bias_;
2726 }
2727
Roland Levillain4fa13f62015-07-06 18:11:54 +01002728 bool IsFPConditionTrueIfNaN() const {
2729 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2730 IfCondition if_cond = GetCondition();
2731 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2732 }
2733
2734 bool IsFPConditionFalseIfNaN() const {
2735 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2736 IfCondition if_cond = GetCondition();
2737 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2738 }
2739
Dave Allison20dfc792014-06-16 20:44:29 -07002740 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002741 // For register allocation purposes, returns whether this instruction needs to be
2742 // materialized (that is, not just be in the processor flags).
2743 bool needs_materialization_;
2744
Mark Mendellc4701932015-04-10 13:18:51 -04002745 // Needed if we merge a HCompare into a HCondition.
2746 ComparisonBias bias_;
2747
Dave Allison20dfc792014-06-16 20:44:29 -07002748 DISALLOW_COPY_AND_ASSIGN(HCondition);
2749};
2750
2751// Instruction to check if two inputs are equal to each other.
2752class HEqual : public HCondition {
2753 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002754 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2755 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002756
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002757 bool IsCommutative() const OVERRIDE { return true; }
2758
Roland Levillain9867bc72015-08-05 10:21:34 +01002759 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 return GetBlock()->GetGraph()->GetIntConstant(
2761 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002762 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 return GetBlock()->GetGraph()->GetIntConstant(
2765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002766 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002767 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2768 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002769 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002770 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002771
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002772 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002773
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002774 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002775 return kCondEQ;
2776 }
2777
Mark Mendellc4701932015-04-10 13:18:51 -04002778 IfCondition GetOppositeCondition() const OVERRIDE {
2779 return kCondNE;
2780 }
2781
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002782 private:
Aart Bike9f37602015-10-09 11:15:55 -07002783 template <typename T> bool Compute(T x, T y) const { return x == y; }
2784
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002785 DISALLOW_COPY_AND_ASSIGN(HEqual);
2786};
2787
Dave Allison20dfc792014-06-16 20:44:29 -07002788class HNotEqual : public HCondition {
2789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2791 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002792
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002793 bool IsCommutative() const OVERRIDE { return true; }
2794
Roland Levillain9867bc72015-08-05 10:21:34 +01002795 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 return GetBlock()->GetGraph()->GetIntConstant(
2797 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002798 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002799 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 return GetBlock()->GetGraph()->GetIntConstant(
2801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002802 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002803 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2804 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002805 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002806 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002807
Dave Allison20dfc792014-06-16 20:44:29 -07002808 DECLARE_INSTRUCTION(NotEqual);
2809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002810 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002811 return kCondNE;
2812 }
2813
Mark Mendellc4701932015-04-10 13:18:51 -04002814 IfCondition GetOppositeCondition() const OVERRIDE {
2815 return kCondEQ;
2816 }
2817
Dave Allison20dfc792014-06-16 20:44:29 -07002818 private:
Aart Bike9f37602015-10-09 11:15:55 -07002819 template <typename T> bool Compute(T x, T y) const { return x != y; }
2820
Dave Allison20dfc792014-06-16 20:44:29 -07002821 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2822};
2823
2824class HLessThan : public HCondition {
2825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2827 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002828
Roland Levillain9867bc72015-08-05 10:21:34 +01002829 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 return GetBlock()->GetGraph()->GetIntConstant(
2831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002832 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 return GetBlock()->GetGraph()->GetIntConstant(
2835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002836 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002837
Dave Allison20dfc792014-06-16 20:44:29 -07002838 DECLARE_INSTRUCTION(LessThan);
2839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002840 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002841 return kCondLT;
2842 }
2843
Mark Mendellc4701932015-04-10 13:18:51 -04002844 IfCondition GetOppositeCondition() const OVERRIDE {
2845 return kCondGE;
2846 }
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848 private:
Aart Bike9f37602015-10-09 11:15:55 -07002849 template <typename T> bool Compute(T x, T y) const { return x < y; }
2850
Dave Allison20dfc792014-06-16 20:44:29 -07002851 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2852};
2853
2854class HLessThanOrEqual : public HCondition {
2855 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002856 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2857 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002858
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002860 return GetBlock()->GetGraph()->GetIntConstant(
2861 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002862 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002863 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002864 return GetBlock()->GetGraph()->GetIntConstant(
2865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002866 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002867
Dave Allison20dfc792014-06-16 20:44:29 -07002868 DECLARE_INSTRUCTION(LessThanOrEqual);
2869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002870 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002871 return kCondLE;
2872 }
2873
Mark Mendellc4701932015-04-10 13:18:51 -04002874 IfCondition GetOppositeCondition() const OVERRIDE {
2875 return kCondGT;
2876 }
2877
Dave Allison20dfc792014-06-16 20:44:29 -07002878 private:
Aart Bike9f37602015-10-09 11:15:55 -07002879 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2880
Dave Allison20dfc792014-06-16 20:44:29 -07002881 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2882};
2883
2884class HGreaterThan : public HCondition {
2885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002886 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2887 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002888
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002890 return GetBlock()->GetGraph()->GetIntConstant(
2891 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002892 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002894 return GetBlock()->GetGraph()->GetIntConstant(
2895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002896 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002897
Dave Allison20dfc792014-06-16 20:44:29 -07002898 DECLARE_INSTRUCTION(GreaterThan);
2899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002900 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002901 return kCondGT;
2902 }
2903
Mark Mendellc4701932015-04-10 13:18:51 -04002904 IfCondition GetOppositeCondition() const OVERRIDE {
2905 return kCondLE;
2906 }
2907
Dave Allison20dfc792014-06-16 20:44:29 -07002908 private:
Aart Bike9f37602015-10-09 11:15:55 -07002909 template <typename T> bool Compute(T x, T y) const { return x > y; }
2910
Dave Allison20dfc792014-06-16 20:44:29 -07002911 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2912};
2913
2914class HGreaterThanOrEqual : public HCondition {
2915 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002916 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2917 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002918
Roland Levillain9867bc72015-08-05 10:21:34 +01002919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002920 return GetBlock()->GetGraph()->GetIntConstant(
2921 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002922 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002923 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 return GetBlock()->GetGraph()->GetIntConstant(
2925 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002926 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002927
Dave Allison20dfc792014-06-16 20:44:29 -07002928 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2929
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002930 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002931 return kCondGE;
2932 }
2933
Mark Mendellc4701932015-04-10 13:18:51 -04002934 IfCondition GetOppositeCondition() const OVERRIDE {
2935 return kCondLT;
2936 }
2937
Dave Allison20dfc792014-06-16 20:44:29 -07002938 private:
Aart Bike9f37602015-10-09 11:15:55 -07002939 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2940
Dave Allison20dfc792014-06-16 20:44:29 -07002941 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2942};
2943
Aart Bike9f37602015-10-09 11:15:55 -07002944class HBelow : public HCondition {
2945 public:
2946 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2947 : HCondition(first, second, dex_pc) {}
2948
2949 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2950 return GetBlock()->GetGraph()->GetIntConstant(
2951 Compute(static_cast<uint32_t>(x->GetValue()),
2952 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2953 }
2954 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2955 return GetBlock()->GetGraph()->GetIntConstant(
2956 Compute(static_cast<uint64_t>(x->GetValue()),
2957 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2958 }
2959
2960 DECLARE_INSTRUCTION(Below);
2961
2962 IfCondition GetCondition() const OVERRIDE {
2963 return kCondB;
2964 }
2965
2966 IfCondition GetOppositeCondition() const OVERRIDE {
2967 return kCondAE;
2968 }
2969
2970 private:
2971 template <typename T> bool Compute(T x, T y) const { return x < y; }
2972
2973 DISALLOW_COPY_AND_ASSIGN(HBelow);
2974};
2975
2976class HBelowOrEqual : public HCondition {
2977 public:
2978 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2979 : HCondition(first, second, dex_pc) {}
2980
2981 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2982 return GetBlock()->GetGraph()->GetIntConstant(
2983 Compute(static_cast<uint32_t>(x->GetValue()),
2984 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2985 }
2986 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2987 return GetBlock()->GetGraph()->GetIntConstant(
2988 Compute(static_cast<uint64_t>(x->GetValue()),
2989 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2990 }
2991
2992 DECLARE_INSTRUCTION(BelowOrEqual);
2993
2994 IfCondition GetCondition() const OVERRIDE {
2995 return kCondBE;
2996 }
2997
2998 IfCondition GetOppositeCondition() const OVERRIDE {
2999 return kCondA;
3000 }
3001
3002 private:
3003 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3004
3005 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3006};
3007
3008class HAbove : public HCondition {
3009 public:
3010 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3011 : HCondition(first, second, dex_pc) {}
3012
3013 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3014 return GetBlock()->GetGraph()->GetIntConstant(
3015 Compute(static_cast<uint32_t>(x->GetValue()),
3016 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3017 }
3018 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3019 return GetBlock()->GetGraph()->GetIntConstant(
3020 Compute(static_cast<uint64_t>(x->GetValue()),
3021 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3022 }
3023
3024 DECLARE_INSTRUCTION(Above);
3025
3026 IfCondition GetCondition() const OVERRIDE {
3027 return kCondA;
3028 }
3029
3030 IfCondition GetOppositeCondition() const OVERRIDE {
3031 return kCondBE;
3032 }
3033
3034 private:
3035 template <typename T> bool Compute(T x, T y) const { return x > y; }
3036
3037 DISALLOW_COPY_AND_ASSIGN(HAbove);
3038};
3039
3040class HAboveOrEqual : public HCondition {
3041 public:
3042 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3043 : HCondition(first, second, dex_pc) {}
3044
3045 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3046 return GetBlock()->GetGraph()->GetIntConstant(
3047 Compute(static_cast<uint32_t>(x->GetValue()),
3048 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3049 }
3050 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3051 return GetBlock()->GetGraph()->GetIntConstant(
3052 Compute(static_cast<uint64_t>(x->GetValue()),
3053 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3054 }
3055
3056 DECLARE_INSTRUCTION(AboveOrEqual);
3057
3058 IfCondition GetCondition() const OVERRIDE {
3059 return kCondAE;
3060 }
3061
3062 IfCondition GetOppositeCondition() const OVERRIDE {
3063 return kCondB;
3064 }
3065
3066 private:
3067 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3068
3069 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3070};
Dave Allison20dfc792014-06-16 20:44:29 -07003071
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003072// Instruction to check how two inputs compare to each other.
3073// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3074class HCompare : public HBinaryOperation {
3075 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003076 HCompare(Primitive::Type type,
3077 HInstruction* first,
3078 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003079 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003080 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003081 : HBinaryOperation(Primitive::kPrimInt,
3082 first,
3083 second,
3084 SideEffectsForArchRuntimeCalls(type),
3085 dex_pc),
3086 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003087 DCHECK_EQ(type, first->GetType());
3088 DCHECK_EQ(type, second->GetType());
3089 }
3090
Roland Levillain9867bc72015-08-05 10:21:34 +01003091 template <typename T>
3092 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003093
Roland Levillain9867bc72015-08-05 10:21:34 +01003094 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003095 return GetBlock()->GetGraph()->GetIntConstant(
3096 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003097 }
3098 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003099 return GetBlock()->GetGraph()->GetIntConstant(
3100 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003101 }
3102
Calin Juravleddb7df22014-11-25 20:56:51 +00003103 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3104 return bias_ == other->AsCompare()->bias_;
3105 }
3106
Mark Mendellc4701932015-04-10 13:18:51 -04003107 ComparisonBias GetBias() const { return bias_; }
3108
Roland Levillain4fa13f62015-07-06 18:11:54 +01003109 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003110
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003111
3112 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3113 // MIPS64 uses a runtime call for FP comparisons.
3114 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3115 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003116
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003117 DECLARE_INSTRUCTION(Compare);
3118
3119 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003120 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003121
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003122 DISALLOW_COPY_AND_ASSIGN(HCompare);
3123};
3124
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003125// A local in the graph. Corresponds to a Dex register.
3126class HLocal : public HTemplateInstruction<0> {
3127 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003128 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003129 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003130
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003131 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003132
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003133 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003134
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003135 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003136 // The Dex register number.
3137 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138
3139 DISALLOW_COPY_AND_ASSIGN(HLocal);
3140};
3141
3142// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003143class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003145 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3146 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003147 SetRawInputAt(0, local);
3148 }
3149
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003150 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3151
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003152 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003153
3154 private:
3155 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3156};
3157
3158// Store a value in a given local. This instruction has two inputs: the value
3159// and the local.
3160class HStoreLocal : public HTemplateInstruction<2> {
3161 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003162 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3163 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003164 SetRawInputAt(0, local);
3165 SetRawInputAt(1, value);
3166 }
3167
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003168 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3169
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003170 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003171
3172 private:
3173 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3174};
3175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003176class HFloatConstant : public HConstant {
3177 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003178 float GetValue() const { return value_; }
3179
David Brazdil77a48ae2015-09-15 12:34:04 +00003180 uint64_t GetValueAsUint64() const OVERRIDE {
3181 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3182 }
3183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003184 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003185 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003186 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003187 }
3188
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003189 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003190
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003191 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003192 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003193 }
3194 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003195 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003196 }
3197 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003198 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3199 }
3200 bool IsNaN() const {
3201 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003202 }
3203
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003204 DECLARE_INSTRUCTION(FloatConstant);
3205
3206 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003207 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3208 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3209 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3210 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003211
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003212 const float value_;
3213
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003214 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003215 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003216 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003217 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3218};
3219
3220class HDoubleConstant : public HConstant {
3221 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003222 double GetValue() const { return value_; }
3223
David Brazdil77a48ae2015-09-15 12:34:04 +00003224 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003226 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003227 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003228 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003229 }
3230
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003231 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003232
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003233 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003234 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003235 }
3236 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003237 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003238 }
3239 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003240 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3241 }
3242 bool IsNaN() const {
3243 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003244 }
3245
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003246 DECLARE_INSTRUCTION(DoubleConstant);
3247
3248 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003249 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3250 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3251 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3252 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003253
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003254 const double value_;
3255
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003256 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003257 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003258 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003259 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3260};
3261
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003262enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003263#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3264 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003265#include "intrinsics_list.h"
3266 kNone,
3267 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3268#undef INTRINSICS_LIST
3269#undef OPTIMIZING_INTRINSICS
3270};
3271std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3272
Agi Csaki05f20562015-08-19 14:58:14 -07003273enum IntrinsicNeedsEnvironmentOrCache {
3274 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3275 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003276};
3277
Aart Bik5d75afe2015-12-14 11:57:01 -08003278enum IntrinsicSideEffects {
3279 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3280 kReadSideEffects, // Intrinsic may read heap memory.
3281 kWriteSideEffects, // Intrinsic may write heap memory.
3282 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3283};
3284
3285enum IntrinsicExceptions {
3286 kNoThrow, // Intrinsic does not throw any exceptions.
3287 kCanThrow // Intrinsic may throw exceptions.
3288};
3289
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003290class HInvoke : public HInstruction {
3291 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003292 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003293
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003294 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003295
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003296 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003297 SetRawInputAt(index, argument);
3298 }
3299
Roland Levillain3e3d7332015-04-28 11:00:54 +01003300 // Return the number of arguments. This number can be lower than
3301 // the number of inputs returned by InputCount(), as some invoke
3302 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3303 // inputs at the end of their list of inputs.
3304 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003306 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003307
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003308 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003309 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003310
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003311 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3312
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003313 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003314 return intrinsic_;
3315 }
3316
Aart Bik5d75afe2015-12-14 11:57:01 -08003317 void SetIntrinsic(Intrinsics intrinsic,
3318 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3319 IntrinsicSideEffects side_effects,
3320 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003321
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003322 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003323 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003324 }
3325
Aart Bik5d75afe2015-12-14 11:57:01 -08003326 bool CanThrow() const OVERRIDE { return can_throw_; }
3327
3328 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3329
3330 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3331 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3332 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003333
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003334 uint32_t* GetIntrinsicOptimizations() {
3335 return &intrinsic_optimizations_;
3336 }
3337
3338 const uint32_t* GetIntrinsicOptimizations() const {
3339 return &intrinsic_optimizations_;
3340 }
3341
3342 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3343
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003344 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003345
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003346 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003347 HInvoke(ArenaAllocator* arena,
3348 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003349 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003350 Primitive::Type return_type,
3351 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003352 uint32_t dex_method_index,
3353 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003354 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003355 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003356 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003357 inputs_(number_of_arguments + number_of_other_inputs,
3358 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003359 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003360 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003361 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003362 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003363 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003364 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003365 }
3366
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003367 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003368 return inputs_[index];
3369 }
3370
David Brazdil1abb4192015-02-17 18:33:36 +00003371 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003372 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003373 }
3374
Aart Bik5d75afe2015-12-14 11:57:01 -08003375 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3376
Roland Levillain3e3d7332015-04-28 11:00:54 +01003377 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003378 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003379 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003380 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003381 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003382 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003383 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003384
3385 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3386 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003387
3388 private:
3389 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3390};
3391
Calin Juravle175dc732015-08-25 15:42:32 +01003392class HInvokeUnresolved : public HInvoke {
3393 public:
3394 HInvokeUnresolved(ArenaAllocator* arena,
3395 uint32_t number_of_arguments,
3396 Primitive::Type return_type,
3397 uint32_t dex_pc,
3398 uint32_t dex_method_index,
3399 InvokeType invoke_type)
3400 : HInvoke(arena,
3401 number_of_arguments,
3402 0u /* number_of_other_inputs */,
3403 return_type,
3404 dex_pc,
3405 dex_method_index,
3406 invoke_type) {
3407 }
3408
3409 DECLARE_INSTRUCTION(InvokeUnresolved);
3410
3411 private:
3412 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3413};
3414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003415class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003416 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003417 // Requirements of this method call regarding the class
3418 // initialization (clinit) check of its declaring class.
3419 enum class ClinitCheckRequirement {
3420 kNone, // Class already initialized.
3421 kExplicit, // Static call having explicit clinit check as last input.
3422 kImplicit, // Static call implicitly requiring a clinit check.
3423 };
3424
Vladimir Marko58155012015-08-19 12:49:41 +00003425 // Determines how to load the target ArtMethod*.
3426 enum class MethodLoadKind {
3427 // Use a String init ArtMethod* loaded from Thread entrypoints.
3428 kStringInit,
3429
3430 // Use the method's own ArtMethod* loaded by the register allocator.
3431 kRecursive,
3432
3433 // Use ArtMethod* at a known address, embed the direct address in the code.
3434 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3435 kDirectAddress,
3436
3437 // Use ArtMethod* at an address that will be known at link time, embed the direct
3438 // address in the code. If the image is relocatable, emit .patch_oat entry.
3439 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3440 // the image relocatable or not.
3441 kDirectAddressWithFixup,
3442
3443 // Load from resoved methods array in the dex cache using a PC-relative load.
3444 // Used when we need to use the dex cache, for example for invoke-static that
3445 // may cause class initialization (the entry may point to a resolution method),
3446 // and we know that we can access the dex cache arrays using a PC-relative load.
3447 kDexCachePcRelative,
3448
3449 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3450 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3451 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3452 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3453 kDexCacheViaMethod,
3454 };
3455
3456 // Determines the location of the code pointer.
3457 enum class CodePtrLocation {
3458 // Recursive call, use local PC-relative call instruction.
3459 kCallSelf,
3460
3461 // Use PC-relative call instruction patched at link time.
3462 // Used for calls within an oat file, boot->boot or app->app.
3463 kCallPCRelative,
3464
3465 // Call to a known target address, embed the direct address in code.
3466 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3467 kCallDirect,
3468
3469 // Call to a target address that will be known at link time, embed the direct
3470 // address in code. If the image is relocatable, emit .patch_oat entry.
3471 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3472 // the image relocatable or not.
3473 kCallDirectWithFixup,
3474
3475 // Use code pointer from the ArtMethod*.
3476 // Used when we don't know the target code. This is also the last-resort-kind used when
3477 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3478 kCallArtMethod,
3479 };
3480
3481 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003482 MethodLoadKind method_load_kind;
3483 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003484 // The method load data holds
3485 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3486 // Note that there are multiple string init methods, each having its own offset.
3487 // - the method address for kDirectAddress
3488 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003489 uint64_t method_load_data;
3490 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003491 };
3492
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003493 HInvokeStaticOrDirect(ArenaAllocator* arena,
3494 uint32_t number_of_arguments,
3495 Primitive::Type return_type,
3496 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003497 uint32_t method_index,
3498 MethodReference target_method,
3499 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003500 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003501 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003502 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003503 : HInvoke(arena,
3504 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003505 // There is potentially one extra argument for the HCurrentMethod node, and
3506 // potentially one other if the clinit check is explicit, and potentially
3507 // one other if the method is a string factory.
3508 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3509 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3510 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003511 return_type,
3512 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003513 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003514 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003515 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003516 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003517 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003518 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003519
Vladimir Markodc151b22015-10-15 18:02:30 +01003520 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003521 bool had_current_method_input = HasCurrentMethodInput();
3522 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3523
3524 // Using the current method is the default and once we find a better
3525 // method load kind, we should not go back to using the current method.
3526 DCHECK(had_current_method_input || !needs_current_method_input);
3527
3528 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003529 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3530 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003531 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003532 dispatch_info_ = dispatch_info;
3533 }
3534
Vladimir Markoc53c0792015-11-19 15:48:33 +00003535 void AddSpecialInput(HInstruction* input) {
3536 // We allow only one special input.
3537 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3538 DCHECK(InputCount() == GetSpecialInputIndex() ||
3539 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3540 InsertInputAt(GetSpecialInputIndex(), input);
3541 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003542
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003543 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003544 // We access the method via the dex cache so we can't do an implicit null check.
3545 // TODO: for intrinsics we can generate implicit null checks.
3546 return false;
3547 }
3548
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003549 bool CanBeNull() const OVERRIDE {
3550 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3551 }
3552
Vladimir Markoc53c0792015-11-19 15:48:33 +00003553 // Get the index of the special input, if any.
3554 //
3555 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3556 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3557 // the "special input" is the current method pointer; otherwise there may be one
3558 // platform-specific special input, such as PC-relative addressing base.
3559 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3560
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003561 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3562 void SetOptimizedInvokeType(InvokeType invoke_type) {
3563 optimized_invoke_type_ = invoke_type;
3564 }
3565
Vladimir Marko58155012015-08-19 12:49:41 +00003566 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3567 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3568 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003569 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003570 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003571 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003572 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003573 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3574 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003575 bool HasCurrentMethodInput() const {
3576 // This function can be called only after the invoke has been fully initialized by the builder.
3577 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003578 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003579 return true;
3580 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003581 DCHECK(InputCount() == GetSpecialInputIndex() ||
3582 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003583 return false;
3584 }
3585 }
Vladimir Marko58155012015-08-19 12:49:41 +00003586 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3587 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003588 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003589
3590 int32_t GetStringInitOffset() const {
3591 DCHECK(IsStringInit());
3592 return dispatch_info_.method_load_data;
3593 }
3594
3595 uint64_t GetMethodAddress() const {
3596 DCHECK(HasMethodAddress());
3597 return dispatch_info_.method_load_data;
3598 }
3599
3600 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003601 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003602 return dispatch_info_.method_load_data;
3603 }
3604
3605 uint64_t GetDirectCodePtr() const {
3606 DCHECK(HasDirectCodePtr());
3607 return dispatch_info_.direct_code_ptr;
3608 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003609
Calin Juravle68ad6492015-08-18 17:08:12 +01003610 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3611
Roland Levillain4c0eb422015-04-24 16:43:49 +01003612 // Is this instruction a call to a static method?
3613 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003614 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003615 }
3616
Vladimir Markofbb184a2015-11-13 14:47:00 +00003617 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3618 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3619 // instruction; only relevant for static calls with explicit clinit check.
3620 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003621 DCHECK(IsStaticWithExplicitClinitCheck());
3622 size_t last_input_index = InputCount() - 1;
3623 HInstruction* last_input = InputAt(last_input_index);
3624 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003625 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003626 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003627 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003628 clinit_check_requirement_ = new_requirement;
3629 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003630 }
3631
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003632 bool IsStringFactoryFor(HFakeString* str) const {
3633 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003634 DCHECK(!HasCurrentMethodInput());
3635 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003636 return InputAt(InputCount() - 1)->AsFakeString() == str;
3637 }
3638
3639 void RemoveFakeStringArgumentAsLastInput() {
3640 DCHECK(IsStringInit());
3641 size_t last_input_index = InputCount() - 1;
3642 HInstruction* last_input = InputAt(last_input_index);
3643 DCHECK(last_input != nullptr);
3644 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3645 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003646 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003647 }
3648
Roland Levillain4c0eb422015-04-24 16:43:49 +01003649 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003650 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003651 bool IsStaticWithExplicitClinitCheck() const {
3652 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3653 }
3654
3655 // Is this a call to a static method whose declaring class has an
3656 // implicit intialization check requirement?
3657 bool IsStaticWithImplicitClinitCheck() const {
3658 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3659 }
3660
Vladimir Markob554b5a2015-11-06 12:57:55 +00003661 // Does this method load kind need the current method as an input?
3662 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3663 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3664 }
3665
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003666 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003667
Roland Levillain4c0eb422015-04-24 16:43:49 +01003668 protected:
3669 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3670 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3671 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3672 HInstruction* input = input_record.GetInstruction();
3673 // `input` is the last input of a static invoke marked as having
3674 // an explicit clinit check. It must either be:
3675 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3676 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3677 DCHECK(input != nullptr);
3678 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3679 }
3680 return input_record;
3681 }
3682
Vladimir Markoc53c0792015-11-19 15:48:33 +00003683 void InsertInputAt(size_t index, HInstruction* input);
3684 void RemoveInputAt(size_t index);
3685
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003686 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003687 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003688 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003689 // The target method may refer to different dex file or method index than the original
3690 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3691 // in derived class to increase visibility.
3692 MethodReference target_method_;
3693 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003694
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003695 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003696};
Vladimir Markof64242a2015-12-01 14:58:23 +00003697std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003698std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003699
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003700class HInvokeVirtual : public HInvoke {
3701 public:
3702 HInvokeVirtual(ArenaAllocator* arena,
3703 uint32_t number_of_arguments,
3704 Primitive::Type return_type,
3705 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003706 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003707 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003708 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003709 vtable_index_(vtable_index) {}
3710
Calin Juravle641547a2015-04-21 22:08:51 +01003711 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003712 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003713 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003714 }
3715
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003716 uint32_t GetVTableIndex() const { return vtable_index_; }
3717
3718 DECLARE_INSTRUCTION(InvokeVirtual);
3719
3720 private:
3721 const uint32_t vtable_index_;
3722
3723 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3724};
3725
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003726class HInvokeInterface : public HInvoke {
3727 public:
3728 HInvokeInterface(ArenaAllocator* arena,
3729 uint32_t number_of_arguments,
3730 Primitive::Type return_type,
3731 uint32_t dex_pc,
3732 uint32_t dex_method_index,
3733 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003734 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003735 imt_index_(imt_index) {}
3736
Calin Juravle641547a2015-04-21 22:08:51 +01003737 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003738 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003739 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003740 }
3741
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003742 uint32_t GetImtIndex() const { return imt_index_; }
3743 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3744
3745 DECLARE_INSTRUCTION(InvokeInterface);
3746
3747 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003748 const uint32_t imt_index_;
3749
3750 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3751};
3752
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003753class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003754 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003755 HNewInstance(HInstruction* cls,
3756 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003757 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003758 uint16_t type_index,
3759 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003760 bool can_throw,
3761 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003762 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003763 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003764 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003765 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003766 can_throw_(can_throw),
3767 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003768 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003769 SetRawInputAt(0, cls);
3770 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003771 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003772
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003773 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003774 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003775
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003776 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003777 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003778
3779 // It may throw when called on type that's not instantiable/accessible.
3780 // It can throw OOME.
3781 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3782 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3783
3784 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003785
Calin Juravle10e244f2015-01-26 18:54:32 +00003786 bool CanBeNull() const OVERRIDE { return false; }
3787
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003788 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3789
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003790 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3791 entrypoint_ = entrypoint;
3792 }
3793
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003794 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003795
3796 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003797 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003798 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003799 const bool can_throw_;
3800 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003801 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003802
3803 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3804};
3805
Roland Levillain88cb1752014-10-20 16:36:47 +01003806class HNeg : public HUnaryOperation {
3807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3809 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003810
Roland Levillain9867bc72015-08-05 10:21:34 +01003811 template <typename T> T Compute(T x) const { return -x; }
3812
3813 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003814 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003815 }
3816 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003817 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003819
Roland Levillain88cb1752014-10-20 16:36:47 +01003820 DECLARE_INSTRUCTION(Neg);
3821
3822 private:
3823 DISALLOW_COPY_AND_ASSIGN(HNeg);
3824};
3825
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003826class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003827 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003828 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003829 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003830 uint32_t dex_pc,
3831 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003832 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003833 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003834 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003835 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003836 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003837 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003838 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003839 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003840 }
3841
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003842 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003843 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003844
3845 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003846 bool NeedsEnvironment() const OVERRIDE { return true; }
3847
Mingyao Yang0c365e62015-03-31 15:09:29 -07003848 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3849 bool CanThrow() const OVERRIDE { return true; }
3850
Calin Juravle10e244f2015-01-26 18:54:32 +00003851 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003852
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003853 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3854
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003855 DECLARE_INSTRUCTION(NewArray);
3856
3857 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003858 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003859 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003860 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003861
3862 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3863};
3864
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003865class HAdd : public HBinaryOperation {
3866 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003867 HAdd(Primitive::Type result_type,
3868 HInstruction* left,
3869 HInstruction* right,
3870 uint32_t dex_pc = kNoDexPc)
3871 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003873 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003874
Roland Levillain9867bc72015-08-05 10:21:34 +01003875 template <typename T> T Compute(T x, T y) const { return x + y; }
3876
3877 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003878 return GetBlock()->GetGraph()->GetIntConstant(
3879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003880 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003881 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 return GetBlock()->GetGraph()->GetLongConstant(
3883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003884 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003885
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003886 DECLARE_INSTRUCTION(Add);
3887
3888 private:
3889 DISALLOW_COPY_AND_ASSIGN(HAdd);
3890};
3891
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003892class HSub : public HBinaryOperation {
3893 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 HSub(Primitive::Type result_type,
3895 HInstruction* left,
3896 HInstruction* right,
3897 uint32_t dex_pc = kNoDexPc)
3898 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003899
Roland Levillain9867bc72015-08-05 10:21:34 +01003900 template <typename T> T Compute(T x, T y) const { return x - y; }
3901
3902 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003903 return GetBlock()->GetGraph()->GetIntConstant(
3904 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003905 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003906 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003907 return GetBlock()->GetGraph()->GetLongConstant(
3908 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003909 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003910
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003911 DECLARE_INSTRUCTION(Sub);
3912
3913 private:
3914 DISALLOW_COPY_AND_ASSIGN(HSub);
3915};
3916
Calin Juravle34bacdf2014-10-07 20:23:36 +01003917class HMul : public HBinaryOperation {
3918 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003919 HMul(Primitive::Type result_type,
3920 HInstruction* left,
3921 HInstruction* right,
3922 uint32_t dex_pc = kNoDexPc)
3923 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003924
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003925 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003926
Roland Levillain9867bc72015-08-05 10:21:34 +01003927 template <typename T> T Compute(T x, T y) const { return x * y; }
3928
3929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003930 return GetBlock()->GetGraph()->GetIntConstant(
3931 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003932 }
3933 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 return GetBlock()->GetGraph()->GetLongConstant(
3935 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003936 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003937
3938 DECLARE_INSTRUCTION(Mul);
3939
3940 private:
3941 DISALLOW_COPY_AND_ASSIGN(HMul);
3942};
3943
Calin Juravle7c4954d2014-10-28 16:57:40 +00003944class HDiv : public HBinaryOperation {
3945 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003946 HDiv(Primitive::Type result_type,
3947 HInstruction* left,
3948 HInstruction* right,
3949 uint32_t dex_pc)
3950 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003951
Roland Levillain9867bc72015-08-05 10:21:34 +01003952 template <typename T>
3953 T Compute(T x, T y) const {
3954 // Our graph structure ensures we never have 0 for `y` during
3955 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003956 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003957 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003958 return (y == -1) ? -x : x / y;
3959 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003960
Roland Levillain9867bc72015-08-05 10:21:34 +01003961 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003962 return GetBlock()->GetGraph()->GetIntConstant(
3963 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003964 }
3965 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003966 return GetBlock()->GetGraph()->GetLongConstant(
3967 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003968 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003969
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003970 static SideEffects SideEffectsForArchRuntimeCalls() {
3971 // The generated code can use a runtime call.
3972 return SideEffects::CanTriggerGC();
3973 }
3974
Calin Juravle7c4954d2014-10-28 16:57:40 +00003975 DECLARE_INSTRUCTION(Div);
3976
3977 private:
3978 DISALLOW_COPY_AND_ASSIGN(HDiv);
3979};
3980
Calin Juravlebacfec32014-11-14 15:54:36 +00003981class HRem : public HBinaryOperation {
3982 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003983 HRem(Primitive::Type result_type,
3984 HInstruction* left,
3985 HInstruction* right,
3986 uint32_t dex_pc)
3987 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003988
Roland Levillain9867bc72015-08-05 10:21:34 +01003989 template <typename T>
3990 T Compute(T x, T y) const {
3991 // Our graph structure ensures we never have 0 for `y` during
3992 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003993 DCHECK_NE(y, 0);
3994 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3995 return (y == -1) ? 0 : x % y;
3996 }
3997
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003999 return GetBlock()->GetGraph()->GetIntConstant(
4000 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004001 }
4002 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004003 return GetBlock()->GetGraph()->GetLongConstant(
4004 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004005 }
4006
Calin Juravlebacfec32014-11-14 15:54:36 +00004007
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004008 static SideEffects SideEffectsForArchRuntimeCalls() {
4009 return SideEffects::CanTriggerGC();
4010 }
4011
Calin Juravlebacfec32014-11-14 15:54:36 +00004012 DECLARE_INSTRUCTION(Rem);
4013
4014 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004015 DISALLOW_COPY_AND_ASSIGN(HRem);
4016};
4017
Calin Juravled0d48522014-11-04 16:40:20 +00004018class HDivZeroCheck : public HExpression<1> {
4019 public:
4020 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004021 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004022 SetRawInputAt(0, value);
4023 }
4024
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004025 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4026
Calin Juravled0d48522014-11-04 16:40:20 +00004027 bool CanBeMoved() const OVERRIDE { return true; }
4028
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004029 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004030 return true;
4031 }
4032
4033 bool NeedsEnvironment() const OVERRIDE { return true; }
4034 bool CanThrow() const OVERRIDE { return true; }
4035
Calin Juravled0d48522014-11-04 16:40:20 +00004036 DECLARE_INSTRUCTION(DivZeroCheck);
4037
4038 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004039 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4040};
4041
Calin Juravle9aec02f2014-11-18 23:06:35 +00004042class HShl : public HBinaryOperation {
4043 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004044 HShl(Primitive::Type result_type,
4045 HInstruction* left,
4046 HInstruction* right,
4047 uint32_t dex_pc = kNoDexPc)
4048 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049
Roland Levillain9867bc72015-08-05 10:21:34 +01004050 template <typename T, typename U, typename V>
4051 T Compute(T x, U y, V max_shift_value) const {
4052 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4053 "V is not the unsigned integer type corresponding to T");
4054 return x << (y & max_shift_value);
4055 }
4056
4057 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4058 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004059 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004060 }
4061 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4062 // case is handled as `x << static_cast<int>(y)`.
4063 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4064 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004066 }
4067 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4068 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004069 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004070 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004071
4072 DECLARE_INSTRUCTION(Shl);
4073
4074 private:
4075 DISALLOW_COPY_AND_ASSIGN(HShl);
4076};
4077
4078class HShr : public HBinaryOperation {
4079 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 HShr(Primitive::Type result_type,
4081 HInstruction* left,
4082 HInstruction* right,
4083 uint32_t dex_pc = kNoDexPc)
4084 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004085
Roland Levillain9867bc72015-08-05 10:21:34 +01004086 template <typename T, typename U, typename V>
4087 T Compute(T x, U y, V max_shift_value) const {
4088 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4089 "V is not the unsigned integer type corresponding to T");
4090 return x >> (y & max_shift_value);
4091 }
4092
4093 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4094 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004096 }
4097 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4098 // case is handled as `x >> static_cast<int>(y)`.
4099 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4100 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004101 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004102 }
4103 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4104 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004105 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004106 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004107
4108 DECLARE_INSTRUCTION(Shr);
4109
4110 private:
4111 DISALLOW_COPY_AND_ASSIGN(HShr);
4112};
4113
4114class HUShr : public HBinaryOperation {
4115 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004116 HUShr(Primitive::Type result_type,
4117 HInstruction* left,
4118 HInstruction* right,
4119 uint32_t dex_pc = kNoDexPc)
4120 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004121
Roland Levillain9867bc72015-08-05 10:21:34 +01004122 template <typename T, typename U, typename V>
4123 T Compute(T x, U y, V max_shift_value) const {
4124 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4125 "V is not the unsigned integer type corresponding to T");
4126 V ux = static_cast<V>(x);
4127 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004128 }
4129
Roland Levillain9867bc72015-08-05 10:21:34 +01004130 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4131 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004132 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004133 }
4134 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4135 // case is handled as `x >>> static_cast<int>(y)`.
4136 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4137 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004138 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004139 }
4140 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4141 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004142 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004143 }
4144
4145 DECLARE_INSTRUCTION(UShr);
4146
4147 private:
4148 DISALLOW_COPY_AND_ASSIGN(HUShr);
4149};
4150
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004151class HAnd : public HBinaryOperation {
4152 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 HAnd(Primitive::Type result_type,
4154 HInstruction* left,
4155 HInstruction* right,
4156 uint32_t dex_pc = kNoDexPc)
4157 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004158
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004159 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004160
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 template <typename T, typename U>
4162 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4163
4164 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004165 return GetBlock()->GetGraph()->GetIntConstant(
4166 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004167 }
4168 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004169 return GetBlock()->GetGraph()->GetLongConstant(
4170 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004171 }
4172 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004173 return GetBlock()->GetGraph()->GetLongConstant(
4174 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004175 }
4176 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004177 return GetBlock()->GetGraph()->GetLongConstant(
4178 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004180
4181 DECLARE_INSTRUCTION(And);
4182
4183 private:
4184 DISALLOW_COPY_AND_ASSIGN(HAnd);
4185};
4186
4187class HOr : public HBinaryOperation {
4188 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004189 HOr(Primitive::Type result_type,
4190 HInstruction* left,
4191 HInstruction* right,
4192 uint32_t dex_pc = kNoDexPc)
4193 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004194
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004195 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004196
Roland Levillain9867bc72015-08-05 10:21:34 +01004197 template <typename T, typename U>
4198 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4199
4200 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004201 return GetBlock()->GetGraph()->GetIntConstant(
4202 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004203 }
4204 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004205 return GetBlock()->GetGraph()->GetLongConstant(
4206 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004207 }
4208 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004209 return GetBlock()->GetGraph()->GetLongConstant(
4210 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004211 }
4212 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004213 return GetBlock()->GetGraph()->GetLongConstant(
4214 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004215 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004216
4217 DECLARE_INSTRUCTION(Or);
4218
4219 private:
4220 DISALLOW_COPY_AND_ASSIGN(HOr);
4221};
4222
4223class HXor : public HBinaryOperation {
4224 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004225 HXor(Primitive::Type result_type,
4226 HInstruction* left,
4227 HInstruction* right,
4228 uint32_t dex_pc = kNoDexPc)
4229 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004230
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004231 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004232
Roland Levillain9867bc72015-08-05 10:21:34 +01004233 template <typename T, typename U>
4234 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4235
4236 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 return GetBlock()->GetGraph()->GetIntConstant(
4238 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004239 }
4240 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004241 return GetBlock()->GetGraph()->GetLongConstant(
4242 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004243 }
4244 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004245 return GetBlock()->GetGraph()->GetLongConstant(
4246 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004247 }
4248 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004249 return GetBlock()->GetGraph()->GetLongConstant(
4250 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004251 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004252
4253 DECLARE_INSTRUCTION(Xor);
4254
4255 private:
4256 DISALLOW_COPY_AND_ASSIGN(HXor);
4257};
4258
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004259class HRor : public HBinaryOperation {
4260 public:
4261 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4262 : HBinaryOperation(result_type, value, distance) {}
4263
4264 template <typename T, typename U, typename V>
4265 T Compute(T x, U y, V max_shift_value) const {
4266 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4267 "V is not the unsigned integer type corresponding to T");
4268 V ux = static_cast<V>(x);
4269 if ((y & max_shift_value) == 0) {
4270 return static_cast<T>(ux);
4271 } else {
4272 const V reg_bits = sizeof(T) * 8;
4273 return static_cast<T>(ux >> (y & max_shift_value)) |
4274 (x << (reg_bits - (y & max_shift_value)));
4275 }
4276 }
4277
4278 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4279 return GetBlock()->GetGraph()->GetIntConstant(
4280 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4281 }
4282 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4283 return GetBlock()->GetGraph()->GetLongConstant(
4284 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4285 }
4286 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4287 return GetBlock()->GetGraph()->GetLongConstant(
4288 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4289 }
4290
4291 DECLARE_INSTRUCTION(Ror);
4292
4293 private:
4294 DISALLOW_COPY_AND_ASSIGN(HRor);
4295};
4296
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004297// The value of a parameter in this method. Its location depends on
4298// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004299class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004300 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004301 HParameterValue(const DexFile& dex_file,
4302 uint16_t type_index,
4303 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 Primitive::Type parameter_type,
4305 bool is_this = false)
4306 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004307 dex_file_(dex_file),
4308 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004309 index_(index),
4310 is_this_(is_this),
4311 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004312
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004313 const DexFile& GetDexFile() const { return dex_file_; }
4314 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004315 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004316 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004317
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004318 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4319 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004320
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004321 DECLARE_INSTRUCTION(ParameterValue);
4322
4323 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004324 const DexFile& dex_file_;
4325 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004326 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004327 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004328 const uint8_t index_;
4329
Calin Juravle10e244f2015-01-26 18:54:32 +00004330 // Whether or not the parameter value corresponds to 'this' argument.
4331 const bool is_this_;
4332
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004333 bool can_be_null_;
4334
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004335 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4336};
4337
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004338class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4341 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004342
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004343 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004344 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004345 return true;
4346 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004347
Roland Levillain9867bc72015-08-05 10:21:34 +01004348 template <typename T> T Compute(T x) const { return ~x; }
4349
4350 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004351 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004352 }
4353 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004354 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004356
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004357 DECLARE_INSTRUCTION(Not);
4358
4359 private:
4360 DISALLOW_COPY_AND_ASSIGN(HNot);
4361};
4362
David Brazdil66d126e2015-04-03 16:02:44 +01004363class HBooleanNot : public HUnaryOperation {
4364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004365 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4366 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004367
4368 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004369 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004370 return true;
4371 }
4372
Roland Levillain9867bc72015-08-05 10:21:34 +01004373 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004374 DCHECK(IsUint<1>(x));
4375 return !x;
4376 }
4377
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004379 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004380 }
4381 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4382 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004383 UNREACHABLE();
4384 }
4385
4386 DECLARE_INSTRUCTION(BooleanNot);
4387
4388 private:
4389 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4390};
4391
Roland Levillaindff1f282014-11-05 14:15:05 +00004392class HTypeConversion : public HExpression<1> {
4393 public:
4394 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004395 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004396 : HExpression(result_type,
4397 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4398 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004399 SetRawInputAt(0, input);
4400 DCHECK_NE(input->GetType(), result_type);
4401 }
4402
4403 HInstruction* GetInput() const { return InputAt(0); }
4404 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4405 Primitive::Type GetResultType() const { return GetType(); }
4406
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004407 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004408 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004409
Roland Levillaindff1f282014-11-05 14:15:05 +00004410 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004411 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004412
Mark Mendelle82549b2015-05-06 10:55:34 -04004413 // Try to statically evaluate the conversion and return a HConstant
4414 // containing the result. If the input cannot be converted, return nullptr.
4415 HConstant* TryStaticEvaluation() const;
4416
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004417 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4418 Primitive::Type result_type) {
4419 // Some architectures may not require the 'GC' side effects, but at this point
4420 // in the compilation process we do not know what architecture we will
4421 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004422 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4423 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004424 return SideEffects::CanTriggerGC();
4425 }
4426 return SideEffects::None();
4427 }
4428
Roland Levillaindff1f282014-11-05 14:15:05 +00004429 DECLARE_INSTRUCTION(TypeConversion);
4430
4431 private:
4432 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4433};
4434
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004435static constexpr uint32_t kNoRegNumber = -1;
4436
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004437class HPhi : public HInstruction {
4438 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004439 HPhi(ArenaAllocator* arena,
4440 uint32_t reg_number,
4441 size_t number_of_inputs,
4442 Primitive::Type type,
4443 uint32_t dex_pc = kNoDexPc)
4444 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004445 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004446 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004447 type_(ToPhiType(type)),
4448 // Phis are constructed live and marked dead if conflicting or unused.
4449 // Individual steps of SsaBuilder should assume that if a phi has been
4450 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4451 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004452 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004453 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004454 }
4455
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004456 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4457 static Primitive::Type ToPhiType(Primitive::Type type) {
4458 switch (type) {
4459 case Primitive::kPrimBoolean:
4460 case Primitive::kPrimByte:
4461 case Primitive::kPrimShort:
4462 case Primitive::kPrimChar:
4463 return Primitive::kPrimInt;
4464 default:
4465 return type;
4466 }
4467 }
4468
David Brazdilffee3d32015-07-06 11:48:53 +01004469 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4470
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004471 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004472
4473 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004474 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004475
Calin Juravle10e244f2015-01-26 18:54:32 +00004476 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004477 void SetType(Primitive::Type new_type) {
4478 // Make sure that only valid type changes occur. The following are allowed:
4479 // (1) int -> float/ref (primitive type propagation),
4480 // (2) long -> double (primitive type propagation).
4481 DCHECK(type_ == new_type ||
4482 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4483 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4484 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4485 type_ = new_type;
4486 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004487
Calin Juravle10e244f2015-01-26 18:54:32 +00004488 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4489 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4490
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004491 uint32_t GetRegNumber() const { return reg_number_; }
4492
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004493 void SetDead() { is_live_ = false; }
4494 void SetLive() { is_live_ = true; }
4495 bool IsDead() const { return !is_live_; }
4496 bool IsLive() const { return is_live_; }
4497
David Brazdil77a48ae2015-09-15 12:34:04 +00004498 bool IsVRegEquivalentOf(HInstruction* other) const {
4499 return other != nullptr
4500 && other->IsPhi()
4501 && other->AsPhi()->GetBlock() == GetBlock()
4502 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4503 }
4504
Calin Juravlea4f88312015-04-16 12:57:19 +01004505 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4506 // An equivalent phi is a phi having the same dex register and type.
4507 // It assumes that phis with the same dex register are adjacent.
4508 HPhi* GetNextEquivalentPhiWithSameType() {
4509 HInstruction* next = GetNext();
4510 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4511 if (next->GetType() == GetType()) {
4512 return next->AsPhi();
4513 }
4514 next = next->GetNext();
4515 }
4516 return nullptr;
4517 }
4518
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004519 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004520
David Brazdil1abb4192015-02-17 18:33:36 +00004521 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004522 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004523 return inputs_[index];
4524 }
David Brazdil1abb4192015-02-17 18:33:36 +00004525
4526 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004527 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004528 }
4529
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004530 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004531 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004532 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004533 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004534 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004535 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004536
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004537 DISALLOW_COPY_AND_ASSIGN(HPhi);
4538};
4539
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004540class HNullCheck : public HExpression<1> {
4541 public:
4542 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004543 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004544 SetRawInputAt(0, value);
4545 }
4546
Calin Juravle10e244f2015-01-26 18:54:32 +00004547 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004548 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004549 return true;
4550 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004551
Calin Juravle10e244f2015-01-26 18:54:32 +00004552 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553
Calin Juravle10e244f2015-01-26 18:54:32 +00004554 bool CanThrow() const OVERRIDE { return true; }
4555
4556 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004557
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004558
4559 DECLARE_INSTRUCTION(NullCheck);
4560
4561 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004562 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4563};
4564
4565class FieldInfo : public ValueObject {
4566 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004567 FieldInfo(MemberOffset field_offset,
4568 Primitive::Type field_type,
4569 bool is_volatile,
4570 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004571 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004572 const DexFile& dex_file,
4573 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004574 : field_offset_(field_offset),
4575 field_type_(field_type),
4576 is_volatile_(is_volatile),
4577 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004578 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004579 dex_file_(dex_file),
4580 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004581
4582 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004583 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004584 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004585 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004586 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004587 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004588 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004589
4590 private:
4591 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004592 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004593 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004594 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004595 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004596 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004597 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598};
4599
4600class HInstanceFieldGet : public HExpression<1> {
4601 public:
4602 HInstanceFieldGet(HInstruction* value,
4603 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004604 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004605 bool is_volatile,
4606 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004607 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004608 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004609 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004610 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004611 : HExpression(field_type,
4612 SideEffects::FieldReadOfType(field_type, is_volatile),
4613 dex_pc),
4614 field_info_(field_offset,
4615 field_type,
4616 is_volatile,
4617 field_idx,
4618 declaring_class_def_index,
4619 dex_file,
4620 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004621 SetRawInputAt(0, value);
4622 }
4623
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004624 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004625
4626 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4627 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4628 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004629 }
4630
Calin Juravle641547a2015-04-21 22:08:51 +01004631 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4632 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004633 }
4634
4635 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004636 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4637 }
4638
Calin Juravle52c48962014-12-16 17:02:57 +00004639 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004640 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004641 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004642 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643
4644 DECLARE_INSTRUCTION(InstanceFieldGet);
4645
4646 private:
4647 const FieldInfo field_info_;
4648
4649 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4650};
4651
4652class HInstanceFieldSet : public HTemplateInstruction<2> {
4653 public:
4654 HInstanceFieldSet(HInstruction* object,
4655 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004656 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004657 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004658 bool is_volatile,
4659 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004660 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004661 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004662 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004663 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004664 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4665 dex_pc),
4666 field_info_(field_offset,
4667 field_type,
4668 is_volatile,
4669 field_idx,
4670 declaring_class_def_index,
4671 dex_file,
4672 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004673 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004674 SetRawInputAt(0, object);
4675 SetRawInputAt(1, value);
4676 }
4677
Calin Juravle641547a2015-04-21 22:08:51 +01004678 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4679 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004680 }
4681
Calin Juravle52c48962014-12-16 17:02:57 +00004682 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004683 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004684 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004685 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004686 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004687 bool GetValueCanBeNull() const { return value_can_be_null_; }
4688 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004689
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004690 DECLARE_INSTRUCTION(InstanceFieldSet);
4691
4692 private:
4693 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004694 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004695
4696 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4697};
4698
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004699class HArrayGet : public HExpression<2> {
4700 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 HArrayGet(HInstruction* array,
4702 HInstruction* index,
4703 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004704 uint32_t dex_pc,
4705 SideEffects additional_side_effects = SideEffects::None())
4706 : HExpression(type,
4707 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004708 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709 SetRawInputAt(0, array);
4710 SetRawInputAt(1, index);
4711 }
4712
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004713 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004714 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004715 return true;
4716 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004717 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004718 // TODO: We can be smarter here.
4719 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4720 // which generates the implicit null check. There are cases when these can be removed
4721 // to produce better code. If we ever add optimizations to do so we should allow an
4722 // implicit check here (as long as the address falls in the first page).
4723 return false;
4724 }
4725
David Brazdil4833f5a2015-12-16 10:37:39 +00004726 bool IsEquivalentOf(HArrayGet* other) const {
4727 bool result = (GetDexPc() == other->GetDexPc());
4728 if (kIsDebugBuild && result) {
4729 DCHECK_EQ(GetBlock(), other->GetBlock());
4730 DCHECK_EQ(GetArray(), other->GetArray());
4731 DCHECK_EQ(GetIndex(), other->GetIndex());
4732 if (Primitive::IsIntOrLongType(GetType())) {
4733 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4734 } else {
4735 DCHECK(Primitive::IsFloatingPointType(GetType()));
4736 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4737 }
4738 }
4739 return result;
4740 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004741
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004742 HInstruction* GetArray() const { return InputAt(0); }
4743 HInstruction* GetIndex() const { return InputAt(1); }
4744
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004745 DECLARE_INSTRUCTION(ArrayGet);
4746
4747 private:
4748 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4749};
4750
4751class HArraySet : public HTemplateInstruction<3> {
4752 public:
4753 HArraySet(HInstruction* array,
4754 HInstruction* index,
4755 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004756 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004757 uint32_t dex_pc,
4758 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004759 : HTemplateInstruction(
4760 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004761 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4762 additional_side_effects),
4763 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004764 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004765 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004766 value_can_be_null_(true),
4767 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 SetRawInputAt(0, array);
4769 SetRawInputAt(1, index);
4770 SetRawInputAt(2, value);
4771 }
4772
Calin Juravle77520bc2015-01-12 18:45:46 +00004773 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 // We currently always call a runtime method to catch array store
4775 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004776 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004777 }
4778
Mingyao Yang81014cb2015-06-02 03:16:27 -07004779 // Can throw ArrayStoreException.
4780 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4781
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004782 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004783 // TODO: Same as for ArrayGet.
4784 return false;
4785 }
4786
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004787 void ClearNeedsTypeCheck() {
4788 needs_type_check_ = false;
4789 }
4790
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004791 void ClearValueCanBeNull() {
4792 value_can_be_null_ = false;
4793 }
4794
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004795 void SetStaticTypeOfArrayIsObjectArray() {
4796 static_type_of_array_is_object_array_ = true;
4797 }
4798
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004799 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004800 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004802
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004803 HInstruction* GetArray() const { return InputAt(0); }
4804 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004805 HInstruction* GetValue() const { return InputAt(2); }
4806
4807 Primitive::Type GetComponentType() const {
4808 // The Dex format does not type floating point index operations. Since the
4809 // `expected_component_type_` is set during building and can therefore not
4810 // be correct, we also check what is the value type. If it is a floating
4811 // point type, we must use that type.
4812 Primitive::Type value_type = GetValue()->GetType();
4813 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4814 ? value_type
4815 : expected_component_type_;
4816 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004817
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004818 Primitive::Type GetRawExpectedComponentType() const {
4819 return expected_component_type_;
4820 }
4821
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004822 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4823 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4824 }
4825
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004826 DECLARE_INSTRUCTION(ArraySet);
4827
4828 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004829 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004830 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004831 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 // Cached information for the reference_type_info_ so that codegen
4833 // does not need to inspect the static type.
4834 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004835
4836 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4837};
4838
4839class HArrayLength : public HExpression<1> {
4840 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004841 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004843 // Note that arrays do not change length, so the instruction does not
4844 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004845 SetRawInputAt(0, array);
4846 }
4847
Calin Juravle77520bc2015-01-12 18:45:46 +00004848 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004849 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004850 return true;
4851 }
Calin Juravle641547a2015-04-21 22:08:51 +01004852 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4853 return obj == InputAt(0);
4854 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004855
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004856 DECLARE_INSTRUCTION(ArrayLength);
4857
4858 private:
4859 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4860};
4861
4862class HBoundsCheck : public HExpression<2> {
4863 public:
4864 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866 DCHECK(index->GetType() == Primitive::kPrimInt);
4867 SetRawInputAt(0, index);
4868 SetRawInputAt(1, length);
4869 }
4870
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004871 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004872 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004873 return true;
4874 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004876 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004878 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004879
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004880 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881
4882 DECLARE_INSTRUCTION(BoundsCheck);
4883
4884 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004885 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4886};
4887
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004888/**
4889 * Some DEX instructions are folded into multiple HInstructions that need
4890 * to stay live until the last HInstruction. This class
4891 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004892 * HInstruction stays live. `index` represents the stack location index of the
4893 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004894 */
4895class HTemporary : public HTemplateInstruction<0> {
4896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004897 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4898 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004899
4900 size_t GetIndex() const { return index_; }
4901
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004902 Primitive::Type GetType() const OVERRIDE {
4903 // The previous instruction is the one that will be stored in the temporary location.
4904 DCHECK(GetPrevious() != nullptr);
4905 return GetPrevious()->GetType();
4906 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004907
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004908 DECLARE_INSTRUCTION(Temporary);
4909
4910 private:
4911 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004912 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4913};
4914
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004915class HSuspendCheck : public HTemplateInstruction<0> {
4916 public:
4917 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004918 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004919
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004920 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004921 return true;
4922 }
4923
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004924 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4925 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004926
4927 DECLARE_INSTRUCTION(SuspendCheck);
4928
4929 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004930 // Only used for code generation, in order to share the same slow path between back edges
4931 // of a same loop.
4932 SlowPathCode* slow_path_;
4933
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004934 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4935};
4936
David Srbecky0cf44932015-12-09 14:09:59 +00004937// Pseudo-instruction which provides the native debugger with mapping information.
4938// It ensures that we can generate line number and local variables at this point.
4939class HNativeDebugInfo : public HTemplateInstruction<0> {
4940 public:
4941 explicit HNativeDebugInfo(uint32_t dex_pc)
4942 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4943
4944 bool NeedsEnvironment() const OVERRIDE {
4945 return true;
4946 }
4947
4948 DECLARE_INSTRUCTION(NativeDebugInfo);
4949
4950 private:
4951 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4952};
4953
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004954/**
4955 * Instruction to load a Class object.
4956 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004957class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004958 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004959 HLoadClass(HCurrentMethod* current_method,
4960 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004961 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004962 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004963 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004964 bool needs_access_check,
4965 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004966 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004967 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004968 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004969 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004970 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004971 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004972 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004973 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004974 // Referrers class should not need access check. We never inline unverified
4975 // methods so we can't possibly end up in this situation.
4976 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004977 SetRawInputAt(0, current_method);
4978 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004979
4980 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004981
4982 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004983 // Note that we don't need to test for generate_clinit_check_.
4984 // Whether or not we need to generate the clinit check is processed in
4985 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004986 return other->AsLoadClass()->type_index_ == type_index_ &&
4987 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004988 }
4989
4990 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4991
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004993 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004994 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004995
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004996 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004997 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004998 }
4999
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005000 bool MustGenerateClinitCheck() const {
5001 return generate_clinit_check_;
5002 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005003
Calin Juravle0ba218d2015-05-19 18:46:01 +01005004 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005005 // The entrypoint the code generator is going to call does not do
5006 // clinit of the class.
5007 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005008 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005009 }
5010
5011 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005012 return MustGenerateClinitCheck() ||
5013 (!is_referrers_class_ && !is_in_dex_cache_) ||
5014 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005015 }
5016
5017 bool NeedsAccessCheck() const {
5018 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005019 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005020
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005021 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005022 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005023 }
5024
Calin Juravleacf735c2015-02-12 15:25:22 +00005025 ReferenceTypeInfo GetLoadedClassRTI() {
5026 return loaded_class_rti_;
5027 }
5028
5029 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5030 // Make sure we only set exact types (the loaded class should never be merged).
5031 DCHECK(rti.IsExact());
5032 loaded_class_rti_ = rti;
5033 }
5034
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005035 const DexFile& GetDexFile() { return dex_file_; }
5036
Vladimir Markodc151b22015-10-15 18:02:30 +01005037 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005038
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005039 static SideEffects SideEffectsForArchRuntimeCalls() {
5040 return SideEffects::CanTriggerGC();
5041 }
5042
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005043 bool IsInDexCache() const { return is_in_dex_cache_; }
5044
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005045 DECLARE_INSTRUCTION(LoadClass);
5046
5047 private:
5048 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005049 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005050 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005051 // Whether this instruction must generate the initialization check.
5052 // Used for code generation.
5053 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005054 const bool needs_access_check_;
5055 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005056
Calin Juravleacf735c2015-02-12 15:25:22 +00005057 ReferenceTypeInfo loaded_class_rti_;
5058
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005059 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5060};
5061
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005062class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005063 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005064 HLoadString(HCurrentMethod* current_method,
5065 uint32_t string_index,
5066 uint32_t dex_pc,
5067 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005068 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005069 string_index_(string_index),
5070 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005071 SetRawInputAt(0, current_method);
5072 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005073
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005074 bool CanBeMoved() const OVERRIDE { return true; }
5075
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005076 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5077 return other->AsLoadString()->string_index_ == string_index_;
5078 }
5079
5080 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5081
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005082 uint32_t GetStringIndex() const { return string_index_; }
5083
5084 // TODO: Can we deopt or debug when we resolve a string?
5085 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005086 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005087 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005088 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005089
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005090 static SideEffects SideEffectsForArchRuntimeCalls() {
5091 return SideEffects::CanTriggerGC();
5092 }
5093
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005094 DECLARE_INSTRUCTION(LoadString);
5095
5096 private:
5097 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005098 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005099
5100 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5101};
5102
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005103/**
5104 * Performs an initialization check on its Class object input.
5105 */
5106class HClinitCheck : public HExpression<1> {
5107 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005108 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005109 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005110 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005111 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5112 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005113 SetRawInputAt(0, constant);
5114 }
5115
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005116 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005117 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005118 return true;
5119 }
5120
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005121 bool NeedsEnvironment() const OVERRIDE {
5122 // May call runtime to initialize the class.
5123 return true;
5124 }
5125
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005126 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005127
5128 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5129
5130 DECLARE_INSTRUCTION(ClinitCheck);
5131
5132 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005133 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5134};
5135
5136class HStaticFieldGet : public HExpression<1> {
5137 public:
5138 HStaticFieldGet(HInstruction* cls,
5139 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005140 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005141 bool is_volatile,
5142 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005143 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005144 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005146 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005147 : HExpression(field_type,
5148 SideEffects::FieldReadOfType(field_type, is_volatile),
5149 dex_pc),
5150 field_info_(field_offset,
5151 field_type,
5152 is_volatile,
5153 field_idx,
5154 declaring_class_def_index,
5155 dex_file,
5156 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005157 SetRawInputAt(0, cls);
5158 }
5159
Calin Juravle52c48962014-12-16 17:02:57 +00005160
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005161 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005162
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005163 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005164 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5165 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005166 }
5167
5168 size_t ComputeHashCode() const OVERRIDE {
5169 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5170 }
5171
Calin Juravle52c48962014-12-16 17:02:57 +00005172 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005173 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5174 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005175 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005176
5177 DECLARE_INSTRUCTION(StaticFieldGet);
5178
5179 private:
5180 const FieldInfo field_info_;
5181
5182 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5183};
5184
5185class HStaticFieldSet : public HTemplateInstruction<2> {
5186 public:
5187 HStaticFieldSet(HInstruction* cls,
5188 HInstruction* value,
5189 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005190 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005191 bool is_volatile,
5192 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005193 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005194 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005195 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005196 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005197 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5198 dex_pc),
5199 field_info_(field_offset,
5200 field_type,
5201 is_volatile,
5202 field_idx,
5203 declaring_class_def_index,
5204 dex_file,
5205 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005206 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005207 SetRawInputAt(0, cls);
5208 SetRawInputAt(1, value);
5209 }
5210
Calin Juravle52c48962014-12-16 17:02:57 +00005211 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005212 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5213 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005214 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005215
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005216 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005217 bool GetValueCanBeNull() const { return value_can_be_null_; }
5218 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005219
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005220 DECLARE_INSTRUCTION(StaticFieldSet);
5221
5222 private:
5223 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005224 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005225
5226 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5227};
5228
Calin Juravlee460d1d2015-09-29 04:52:17 +01005229class HUnresolvedInstanceFieldGet : public HExpression<1> {
5230 public:
5231 HUnresolvedInstanceFieldGet(HInstruction* obj,
5232 Primitive::Type field_type,
5233 uint32_t field_index,
5234 uint32_t dex_pc)
5235 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5236 field_index_(field_index) {
5237 SetRawInputAt(0, obj);
5238 }
5239
5240 bool NeedsEnvironment() const OVERRIDE { return true; }
5241 bool CanThrow() const OVERRIDE { return true; }
5242
5243 Primitive::Type GetFieldType() const { return GetType(); }
5244 uint32_t GetFieldIndex() const { return field_index_; }
5245
5246 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5247
5248 private:
5249 const uint32_t field_index_;
5250
5251 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5252};
5253
5254class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5255 public:
5256 HUnresolvedInstanceFieldSet(HInstruction* obj,
5257 HInstruction* value,
5258 Primitive::Type field_type,
5259 uint32_t field_index,
5260 uint32_t dex_pc)
5261 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5262 field_type_(field_type),
5263 field_index_(field_index) {
5264 DCHECK_EQ(field_type, value->GetType());
5265 SetRawInputAt(0, obj);
5266 SetRawInputAt(1, value);
5267 }
5268
5269 bool NeedsEnvironment() const OVERRIDE { return true; }
5270 bool CanThrow() const OVERRIDE { return true; }
5271
5272 Primitive::Type GetFieldType() const { return field_type_; }
5273 uint32_t GetFieldIndex() const { return field_index_; }
5274
5275 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5276
5277 private:
5278 const Primitive::Type field_type_;
5279 const uint32_t field_index_;
5280
5281 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5282};
5283
5284class HUnresolvedStaticFieldGet : public HExpression<0> {
5285 public:
5286 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5287 uint32_t field_index,
5288 uint32_t dex_pc)
5289 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5290 field_index_(field_index) {
5291 }
5292
5293 bool NeedsEnvironment() const OVERRIDE { return true; }
5294 bool CanThrow() const OVERRIDE { return true; }
5295
5296 Primitive::Type GetFieldType() const { return GetType(); }
5297 uint32_t GetFieldIndex() const { return field_index_; }
5298
5299 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5300
5301 private:
5302 const uint32_t field_index_;
5303
5304 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5305};
5306
5307class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5308 public:
5309 HUnresolvedStaticFieldSet(HInstruction* value,
5310 Primitive::Type field_type,
5311 uint32_t field_index,
5312 uint32_t dex_pc)
5313 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5314 field_type_(field_type),
5315 field_index_(field_index) {
5316 DCHECK_EQ(field_type, value->GetType());
5317 SetRawInputAt(0, value);
5318 }
5319
5320 bool NeedsEnvironment() const OVERRIDE { return true; }
5321 bool CanThrow() const OVERRIDE { return true; }
5322
5323 Primitive::Type GetFieldType() const { return field_type_; }
5324 uint32_t GetFieldIndex() const { return field_index_; }
5325
5326 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5327
5328 private:
5329 const Primitive::Type field_type_;
5330 const uint32_t field_index_;
5331
5332 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5333};
5334
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005335// Implement the move-exception DEX instruction.
5336class HLoadException : public HExpression<0> {
5337 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005338 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5339 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005340
David Brazdilbbd733e2015-08-18 17:48:17 +01005341 bool CanBeNull() const OVERRIDE { return false; }
5342
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005343 DECLARE_INSTRUCTION(LoadException);
5344
5345 private:
5346 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5347};
5348
David Brazdilcb1c0552015-08-04 16:22:25 +01005349// Implicit part of move-exception which clears thread-local exception storage.
5350// Must not be removed because the runtime expects the TLS to get cleared.
5351class HClearException : public HTemplateInstruction<0> {
5352 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005353 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5354 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005355
5356 DECLARE_INSTRUCTION(ClearException);
5357
5358 private:
5359 DISALLOW_COPY_AND_ASSIGN(HClearException);
5360};
5361
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005362class HThrow : public HTemplateInstruction<1> {
5363 public:
5364 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005365 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005366 SetRawInputAt(0, exception);
5367 }
5368
5369 bool IsControlFlow() const OVERRIDE { return true; }
5370
5371 bool NeedsEnvironment() const OVERRIDE { return true; }
5372
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005373 bool CanThrow() const OVERRIDE { return true; }
5374
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005375
5376 DECLARE_INSTRUCTION(Throw);
5377
5378 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005379 DISALLOW_COPY_AND_ASSIGN(HThrow);
5380};
5381
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005382/**
5383 * Implementation strategies for the code generator of a HInstanceOf
5384 * or `HCheckCast`.
5385 */
5386enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005387 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005388 kExactCheck, // Can do a single class compare.
5389 kClassHierarchyCheck, // Can just walk the super class chain.
5390 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5391 kInterfaceCheck, // No optimization yet when checking against an interface.
5392 kArrayObjectCheck, // Can just check if the array is not primitive.
5393 kArrayCheck // No optimization yet when checking against a generic array.
5394};
5395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005396class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005397 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005398 HInstanceOf(HInstruction* object,
5399 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005400 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005401 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005402 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005403 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005404 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005405 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005406 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005407 SetRawInputAt(0, object);
5408 SetRawInputAt(1, constant);
5409 }
5410
5411 bool CanBeMoved() const OVERRIDE { return true; }
5412
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005413 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005414 return true;
5415 }
5416
5417 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005418 return false;
5419 }
5420
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005421 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5422
5423 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005424
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005425 // Used only in code generation.
5426 bool MustDoNullCheck() const { return must_do_null_check_; }
5427 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5428
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005429 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5430 return (check_kind == TypeCheckKind::kExactCheck)
5431 ? SideEffects::None()
5432 // Mips currently does runtime calls for any other checks.
5433 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005434 }
5435
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005436 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005437
5438 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005439 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005440 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005441
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005442 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5443};
5444
Calin Juravleb1498f62015-02-16 13:13:29 +00005445class HBoundType : public HExpression<1> {
5446 public:
David Brazdilf5552582015-12-27 13:36:12 +00005447 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005448 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005449 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5450 upper_can_be_null_(true),
5451 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005452 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005453 SetRawInputAt(0, input);
5454 }
5455
David Brazdilf5552582015-12-27 13:36:12 +00005456 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005457 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5458 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005459 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005460
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005461 void SetCanBeNull(bool can_be_null) {
5462 DCHECK(upper_can_be_null_ || !can_be_null);
5463 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005464 }
5465
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005466 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5467
Calin Juravleb1498f62015-02-16 13:13:29 +00005468 DECLARE_INSTRUCTION(BoundType);
5469
5470 private:
5471 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005472 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5473 // It is used to bound the type in cases like:
5474 // if (x instanceof ClassX) {
5475 // // uper_bound_ will be ClassX
5476 // }
David Brazdilf5552582015-12-27 13:36:12 +00005477 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005478 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5479 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005480 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005481 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005482
5483 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5484};
5485
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005486class HCheckCast : public HTemplateInstruction<2> {
5487 public:
5488 HCheckCast(HInstruction* object,
5489 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005490 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005491 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005492 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005493 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005494 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005495 SetRawInputAt(0, object);
5496 SetRawInputAt(1, constant);
5497 }
5498
5499 bool CanBeMoved() const OVERRIDE { return true; }
5500
5501 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5502 return true;
5503 }
5504
5505 bool NeedsEnvironment() const OVERRIDE {
5506 // Instruction may throw a CheckCastError.
5507 return true;
5508 }
5509
5510 bool CanThrow() const OVERRIDE { return true; }
5511
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005512 bool MustDoNullCheck() const { return must_do_null_check_; }
5513 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005514 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005515
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005516 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005517
5518 DECLARE_INSTRUCTION(CheckCast);
5519
5520 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005521 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005522 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005523
5524 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005525};
5526
Calin Juravle27df7582015-04-17 19:12:31 +01005527class HMemoryBarrier : public HTemplateInstruction<0> {
5528 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005529 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005530 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005531 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005532 barrier_kind_(barrier_kind) {}
5533
5534 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5535
5536 DECLARE_INSTRUCTION(MemoryBarrier);
5537
5538 private:
5539 const MemBarrierKind barrier_kind_;
5540
5541 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5542};
5543
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005544class HMonitorOperation : public HTemplateInstruction<1> {
5545 public:
5546 enum OperationKind {
5547 kEnter,
5548 kExit,
5549 };
5550
5551 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005552 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005553 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5554 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005555 SetRawInputAt(0, object);
5556 }
5557
5558 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005559 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5560
5561 bool CanThrow() const OVERRIDE {
5562 // Verifier guarantees that monitor-exit cannot throw.
5563 // This is important because it allows the HGraphBuilder to remove
5564 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5565 return IsEnter();
5566 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005567
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005568
5569 bool IsEnter() const { return kind_ == kEnter; }
5570
5571 DECLARE_INSTRUCTION(MonitorOperation);
5572
Calin Juravle52c48962014-12-16 17:02:57 +00005573 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005574 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005575
5576 private:
5577 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5578};
5579
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005580/**
5581 * A HInstruction used as a marker for the replacement of new + <init>
5582 * of a String to a call to a StringFactory. Only baseline will see
5583 * the node at code generation, where it will be be treated as null.
5584 * When compiling non-baseline, `HFakeString` instructions are being removed
5585 * in the instruction simplifier.
5586 */
5587class HFakeString : public HTemplateInstruction<0> {
5588 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005589 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5590 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005591
5592 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5593
5594 DECLARE_INSTRUCTION(FakeString);
5595
5596 private:
5597 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5598};
5599
Vladimir Markof9f64412015-09-02 14:05:49 +01005600class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005601 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005602 MoveOperands(Location source,
5603 Location destination,
5604 Primitive::Type type,
5605 HInstruction* instruction)
5606 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005607
5608 Location GetSource() const { return source_; }
5609 Location GetDestination() const { return destination_; }
5610
5611 void SetSource(Location value) { source_ = value; }
5612 void SetDestination(Location value) { destination_ = value; }
5613
5614 // The parallel move resolver marks moves as "in-progress" by clearing the
5615 // destination (but not the source).
5616 Location MarkPending() {
5617 DCHECK(!IsPending());
5618 Location dest = destination_;
5619 destination_ = Location::NoLocation();
5620 return dest;
5621 }
5622
5623 void ClearPending(Location dest) {
5624 DCHECK(IsPending());
5625 destination_ = dest;
5626 }
5627
5628 bool IsPending() const {
5629 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5630 return destination_.IsInvalid() && !source_.IsInvalid();
5631 }
5632
5633 // True if this blocks a move from the given location.
5634 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005635 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005636 }
5637
5638 // A move is redundant if it's been eliminated, if its source and
5639 // destination are the same, or if its destination is unneeded.
5640 bool IsRedundant() const {
5641 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5642 }
5643
5644 // We clear both operands to indicate move that's been eliminated.
5645 void Eliminate() {
5646 source_ = destination_ = Location::NoLocation();
5647 }
5648
5649 bool IsEliminated() const {
5650 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5651 return source_.IsInvalid();
5652 }
5653
Alexey Frunze4dda3372015-06-01 18:31:49 -07005654 Primitive::Type GetType() const { return type_; }
5655
Nicolas Geoffray90218252015-04-15 11:56:51 +01005656 bool Is64BitMove() const {
5657 return Primitive::Is64BitType(type_);
5658 }
5659
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005660 HInstruction* GetInstruction() const { return instruction_; }
5661
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005662 private:
5663 Location source_;
5664 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005665 // The type this move is for.
5666 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005667 // The instruction this move is assocatied with. Null when this move is
5668 // for moving an input in the expected locations of user (including a phi user).
5669 // This is only used in debug mode, to ensure we do not connect interval siblings
5670 // in the same parallel move.
5671 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005672};
5673
5674static constexpr size_t kDefaultNumberOfMoves = 4;
5675
5676class HParallelMove : public HTemplateInstruction<0> {
5677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005678 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005679 : HTemplateInstruction(SideEffects::None(), dex_pc),
5680 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5681 moves_.reserve(kDefaultNumberOfMoves);
5682 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005683
Nicolas Geoffray90218252015-04-15 11:56:51 +01005684 void AddMove(Location source,
5685 Location destination,
5686 Primitive::Type type,
5687 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005688 DCHECK(source.IsValid());
5689 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005690 if (kIsDebugBuild) {
5691 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005692 for (const MoveOperands& move : moves_) {
5693 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005694 // Special case the situation where the move is for the spill slot
5695 // of the instruction.
5696 if ((GetPrevious() == instruction)
5697 || ((GetPrevious() == nullptr)
5698 && instruction->IsPhi()
5699 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005700 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005701 << "Doing parallel moves for the same instruction.";
5702 } else {
5703 DCHECK(false) << "Doing parallel moves for the same instruction.";
5704 }
5705 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005706 }
5707 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005708 for (const MoveOperands& move : moves_) {
5709 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005710 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005711 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005712 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005713 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005714 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005715 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005716 }
5717
Vladimir Marko225b6462015-09-28 12:17:40 +01005718 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005719 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005720 }
5721
Vladimir Marko225b6462015-09-28 12:17:40 +01005722 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005723
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005724 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005725
5726 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005727 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005728
5729 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5730};
5731
Mark Mendell0616ae02015-04-17 12:49:27 -04005732} // namespace art
5733
Vladimir Markob4536b72015-11-24 13:45:23 +00005734#ifdef ART_ENABLE_CODEGEN_arm
5735#include "nodes_arm.h"
5736#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005737#ifdef ART_ENABLE_CODEGEN_arm64
5738#include "nodes_arm64.h"
5739#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005740#ifdef ART_ENABLE_CODEGEN_x86
5741#include "nodes_x86.h"
5742#endif
5743
5744namespace art {
5745
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005746class HGraphVisitor : public ValueObject {
5747 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005748 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5749 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005750
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005751 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005752 virtual void VisitBasicBlock(HBasicBlock* block);
5753
Roland Levillain633021e2014-10-01 14:12:25 +01005754 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005755 void VisitInsertionOrder();
5756
Roland Levillain633021e2014-10-01 14:12:25 +01005757 // Visit the graph following dominator tree reverse post-order.
5758 void VisitReversePostOrder();
5759
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005760 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005761
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005762 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005763#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005764 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5765
5766 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5767
5768#undef DECLARE_VISIT_INSTRUCTION
5769
5770 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005771 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005772
5773 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5774};
5775
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005776class HGraphDelegateVisitor : public HGraphVisitor {
5777 public:
5778 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5779 virtual ~HGraphDelegateVisitor() {}
5780
5781 // Visit functions that delegate to to super class.
5782#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005783 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005784
5785 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5786
5787#undef DECLARE_VISIT_INSTRUCTION
5788
5789 private:
5790 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5791};
5792
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005793class HInsertionOrderIterator : public ValueObject {
5794 public:
5795 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5796
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005797 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005798 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005799 void Advance() { ++index_; }
5800
5801 private:
5802 const HGraph& graph_;
5803 size_t index_;
5804
5805 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5806};
5807
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005808class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005809 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005810 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5811 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005812 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005813 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005814
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005815 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5816 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005817 void Advance() { ++index_; }
5818
5819 private:
5820 const HGraph& graph_;
5821 size_t index_;
5822
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005823 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005824};
5825
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005826class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005827 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005828 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005829 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005830 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005831 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005832 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005833
5834 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005835 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005836 void Advance() { --index_; }
5837
5838 private:
5839 const HGraph& graph_;
5840 size_t index_;
5841
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005842 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005843};
5844
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005845class HLinearPostOrderIterator : public ValueObject {
5846 public:
5847 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005848 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005849
5850 bool Done() const { return index_ == 0; }
5851
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005852 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005853
5854 void Advance() {
5855 --index_;
5856 DCHECK_GE(index_, 0U);
5857 }
5858
5859 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005860 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005861 size_t index_;
5862
5863 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5864};
5865
5866class HLinearOrderIterator : public ValueObject {
5867 public:
5868 explicit HLinearOrderIterator(const HGraph& graph)
5869 : order_(graph.GetLinearOrder()), index_(0) {}
5870
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005871 bool Done() const { return index_ == order_.size(); }
5872 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005873 void Advance() { ++index_; }
5874
5875 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005876 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005877 size_t index_;
5878
5879 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5880};
5881
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005882// Iterator over the blocks that art part of the loop. Includes blocks part
5883// of an inner loop. The order in which the blocks are iterated is on their
5884// block id.
5885class HBlocksInLoopIterator : public ValueObject {
5886 public:
5887 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5888 : blocks_in_loop_(info.GetBlocks()),
5889 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5890 index_(0) {
5891 if (!blocks_in_loop_.IsBitSet(index_)) {
5892 Advance();
5893 }
5894 }
5895
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005896 bool Done() const { return index_ == blocks_.size(); }
5897 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005898 void Advance() {
5899 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005900 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005901 if (blocks_in_loop_.IsBitSet(index_)) {
5902 break;
5903 }
5904 }
5905 }
5906
5907 private:
5908 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005909 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005910 size_t index_;
5911
5912 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5913};
5914
Mingyao Yang3584bce2015-05-19 16:01:59 -07005915// Iterator over the blocks that art part of the loop. Includes blocks part
5916// of an inner loop. The order in which the blocks are iterated is reverse
5917// post order.
5918class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5919 public:
5920 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5921 : blocks_in_loop_(info.GetBlocks()),
5922 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5923 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005924 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005925 Advance();
5926 }
5927 }
5928
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005929 bool Done() const { return index_ == blocks_.size(); }
5930 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005931 void Advance() {
5932 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005933 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5934 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005935 break;
5936 }
5937 }
5938 }
5939
5940 private:
5941 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005942 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005943 size_t index_;
5944
5945 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5946};
5947
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005948inline int64_t Int64FromConstant(HConstant* constant) {
5949 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5950 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5951 : constant->AsLongConstant()->GetValue();
5952}
5953
Vladimir Marko58155012015-08-19 12:49:41 +00005954inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5955 // For the purposes of the compiler, the dex files must actually be the same object
5956 // if we want to safely treat them as the same. This is especially important for JIT
5957 // as custom class loaders can open the same underlying file (or memory) multiple
5958 // times and provide different class resolution but no two class loaders should ever
5959 // use the same DexFile object - doing so is an unsupported hack that can lead to
5960 // all sorts of weird failures.
5961 return &lhs == &rhs;
5962}
5963
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005964#define INSTRUCTION_TYPE_CHECK(type, super) \
5965 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5966 inline const H##type* HInstruction::As##type() const { \
5967 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5968 } \
5969 inline H##type* HInstruction::As##type() { \
5970 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5971 }
5972
5973 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5974#undef INSTRUCTION_TYPE_CHECK
5975
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005976} // namespace art
5977
5978#endif // ART_COMPILER_OPTIMIZING_NODES_H_