blob: 6d529259769bdf33c5e1fce11c673f3ffe9ec148 [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;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000100enum GraphAnalysisResult {
David Brazdilbadd8262016-02-02 16:28:56 +0000101 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000102 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
Vladimir Markof39745e2016-01-26 12:16:55 +0000163 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000164 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,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000278 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100279 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100281 blocks_(arena->Adapter(kArenaAllocBlockList)),
282 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
283 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700284 entry_block_(nullptr),
285 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100286 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100287 number_of_vregs_(0),
288 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000289 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400290 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000291 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000292 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000293 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000294 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100295 dex_file_(dex_file),
296 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100297 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100298 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100299 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100302 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
304 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
305 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000306 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000307 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
308 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 blocks_.reserve(kDefaultNumberOfBlocks);
310 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311
David Brazdilbadd8262016-02-02 16:28:56 +0000312 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
313 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
314
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000315 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100316 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
317
David Brazdil69ba7b72015-06-23 18:27:30 +0100318 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000319 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100320
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000321 HBasicBlock* GetEntryBlock() const { return entry_block_; }
322 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100323 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
326 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000327
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000328 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100329
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100330 void ComputeDominanceInformation();
331 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000332 void ClearLoopInformation();
333 void FindBackEdges(ArenaBitVector* visited);
334 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100335 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100336 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337
David Brazdil4833f5a2015-12-16 10:37:39 +0000338 // Analyze all natural loops in this graph. Returns a code specifying that it
339 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000340 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000341 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100342
David Brazdilffee3d32015-07-06 11:48:53 +0100343 // Iterate over blocks to compute try block membership. Needs reverse post
344 // order and loop information.
345 void ComputeTryBlockInformation();
346
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000347 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000348 // Returns the instruction to replace the invoke expression or null if the
349 // invoke is for a void method. Note that the caller is responsible for replacing
350 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000351 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000352
Mingyao Yang3584bce2015-05-19 16:01:59 -0700353 // Need to add a couple of blocks to test if the loop body is entered and
354 // put deoptimization instructions, etc.
355 void TransformLoopHeaderForBCE(HBasicBlock* header);
356
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000357 // Removes `block` from the graph. Assumes `block` has been disconnected from
358 // other blocks and has no instructions or phis.
359 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000360
David Brazdilfc6a86a2015-06-26 10:33:45 +0000361 // Splits the edge between `block` and `successor` while preserving the
362 // indices in the predecessor/successor lists. If there are multiple edges
363 // between the blocks, the lowest indices are used.
364 // Returns the new block which is empty and has the same dex pc as `successor`.
365 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
366
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100367 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
368 void SimplifyLoop(HBasicBlock* header);
369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000370 int32_t GetNextInstructionId() {
371 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000372 return current_instruction_id_++;
373 }
374
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000375 int32_t GetCurrentInstructionId() const {
376 return current_instruction_id_;
377 }
378
379 void SetCurrentInstructionId(int32_t id) {
380 current_instruction_id_ = id;
381 }
382
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100383 uint16_t GetMaximumNumberOfOutVRegs() const {
384 return maximum_number_of_out_vregs_;
385 }
386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
388 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100389 }
390
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100391 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
392 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
393 }
394
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000395 void UpdateTemporariesVRegSlots(size_t slots) {
396 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100397 }
398
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000399 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100400 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000401 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100402 }
403
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100404 void SetNumberOfVRegs(uint16_t number_of_vregs) {
405 number_of_vregs_ = number_of_vregs;
406 }
407
408 uint16_t GetNumberOfVRegs() const {
409 return number_of_vregs_;
410 }
411
412 void SetNumberOfInVRegs(uint16_t value) {
413 number_of_in_vregs_ = value;
414 }
415
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100416 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100417 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100418 return number_of_vregs_ - number_of_in_vregs_;
419 }
420
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100421 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100423 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100425 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100426 return linear_order_;
427 }
428
Mark Mendell1152c922015-04-24 17:06:35 -0400429 bool HasBoundsChecks() const {
430 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800431 }
432
Mark Mendell1152c922015-04-24 17:06:35 -0400433 void SetHasBoundsChecks(bool value) {
434 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800435 }
436
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100437 bool ShouldGenerateConstructorBarrier() const {
438 return should_generate_constructor_barrier_;
439 }
440
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000441 bool IsDebuggable() const { return debuggable_; }
442
David Brazdil8d5b8b22015-03-24 10:51:52 +0000443 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000444 // already, it is created and inserted into the graph. This method is only for
445 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600446 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000447
448 // TODO: This is problematic for the consistency of reference type propagation
449 // because it can be created anytime after the pass and thus it will be left
450 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600451 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000452
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
454 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600462 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
463 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000464 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000465
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100466 HCurrentMethod* GetCurrentMethod();
467
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100468 const DexFile& GetDexFile() const {
469 return dex_file_;
470 }
471
472 uint32_t GetMethodIdx() const {
473 return method_idx_;
474 }
475
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100476 InvokeType GetInvokeType() const {
477 return invoke_type_;
478 }
479
Mark Mendellc4701932015-04-10 13:18:51 -0400480 InstructionSet GetInstructionSet() const {
481 return instruction_set_;
482 }
483
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000484 bool IsCompilingOsr() const { return osr_; }
485
David Brazdil77a48ae2015-09-15 12:34:04 +0000486 bool HasTryCatch() const { return has_try_catch_; }
487 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100488
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000489 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
490 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
491
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100492 ArtMethod* GetArtMethod() const { return art_method_; }
493 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
494
Mark Mendellf6529172015-11-17 11:16:56 -0500495 // Returns an instruction with the opposite boolean value from 'cond'.
496 // The instruction has been inserted into the graph, either as a constant, or
497 // before cursor.
498 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
499
David Brazdil2d7352b2015-04-20 14:52:42 +0100500 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000501 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100502 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000503
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000504 template <class InstructionType, typename ValueType>
505 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600506 ArenaSafeMap<ValueType, InstructionType*>* cache,
507 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000508 // Try to find an existing constant of the given value.
509 InstructionType* constant = nullptr;
510 auto cached_constant = cache->find(value);
511 if (cached_constant != cache->end()) {
512 constant = cached_constant->second;
513 }
514
515 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100516 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000517 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000519 cache->Overwrite(value, constant);
520 InsertConstant(constant);
521 }
522 return constant;
523 }
524
David Brazdil8d5b8b22015-03-24 10:51:52 +0000525 void InsertConstant(HConstant* instruction);
526
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 // Cache a float constant into the graph. This method should only be
528 // called by the SsaBuilder when creating "equivalent" instructions.
529 void CacheFloatConstant(HFloatConstant* constant);
530
531 // See CacheFloatConstant comment.
532 void CacheDoubleConstant(HDoubleConstant* constant);
533
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000534 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000535
536 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100537 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000538
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100539 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100540 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000541
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100542 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100543 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100544
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000545 HBasicBlock* entry_block_;
546 HBasicBlock* exit_block_;
547
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100548 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100549 uint16_t maximum_number_of_out_vregs_;
550
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100551 // The number of virtual registers in this method. Contains the parameters.
552 uint16_t number_of_vregs_;
553
554 // The number of virtual registers used by parameters of this method.
555 uint16_t number_of_in_vregs_;
556
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000557 // Number of vreg size slots that the temporaries use (used in baseline compiler).
558 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100559
Mark Mendell1152c922015-04-24 17:06:35 -0400560 // Has bounds checks. We can totally skip BCE if it's false.
561 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800562
David Brazdil77a48ae2015-09-15 12:34:04 +0000563 // Flag whether there are any try/catch blocks in the graph. We will skip
564 // try/catch-related passes if false.
565 bool has_try_catch_;
566
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000567 // Flag whether there are any irreducible loops in the graph.
568 bool has_irreducible_loops_;
569
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000570 // Indicates whether the graph should be compiled in a way that
571 // ensures full debuggability. If false, we can apply more
572 // aggressive optimizations that may limit the level of debugging.
573 const bool debuggable_;
574
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000575 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000576 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000577
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100578 // The dex file from which the method is from.
579 const DexFile& dex_file_;
580
581 // The method index in the dex file.
582 const uint32_t method_idx_;
583
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100584 // If inlined, this encodes how the callee is being invoked.
585 const InvokeType invoke_type_;
586
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100587 // Whether the graph has been transformed to SSA form. Only used
588 // in debug mode to ensure we are not using properties only valid
589 // for non-SSA form (like the number of temporaries).
590 bool in_ssa_form_;
591
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100592 const bool should_generate_constructor_barrier_;
593
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 const InstructionSet instruction_set_;
595
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000597 HNullConstant* cached_null_constant_;
598 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000599 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000600 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000601 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000602
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100603 HCurrentMethod* cached_current_method_;
604
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100605 // The ArtMethod this graph is for. Note that for AOT, it may be null,
606 // for example for methods whose declaring class could not be resolved
607 // (such as when the superclass could not be found).
608 ArtMethod* art_method_;
609
David Brazdil4833f5a2015-12-16 10:37:39 +0000610 // Keep the RTI of inexact Object to avoid having to pass stack handle
611 // collection pointer to passes which may create NullConstant.
612 ReferenceTypeInfo inexact_object_rti_;
613
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000614 // Whether we are compiling this graph for on stack replacement: this will
615 // make all loops seen as irreducible and emit special stack maps to mark
616 // compiled code entries which the interpreter can directly jump to.
617 const bool osr_;
618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100620 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000621 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622 DISALLOW_COPY_AND_ASSIGN(HGraph);
623};
624
Vladimir Markof9f64412015-09-02 14:05:49 +0100625class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000626 public:
627 HLoopInformation(HBasicBlock* header, HGraph* graph)
628 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100629 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000630 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100632 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100633 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
634 back_edges_.reserve(kDefaultNumberOfBackEdges);
635 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100636
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000637 bool IsIrreducible() const { return irreducible_; }
638
639 void Dump(std::ostream& os);
640
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100641 HBasicBlock* GetHeader() const {
642 return header_;
643 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 void SetHeader(HBasicBlock* block) {
646 header_ = block;
647 }
648
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100649 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
650 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
651 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
652
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655 }
656
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 }
660
David Brazdil46e2a392015-03-16 17:31:52 +0000661 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100662 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 }
664
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000665 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100666 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000667 }
668
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100669 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100670
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100672 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100673 }
674
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100675 // Returns the lifetime position of the back edge that has the
676 // greatest lifetime position.
677 size_t GetLifetimeEnd() const;
678
679 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100681 }
682
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000683 // Finds blocks that are part of this loop.
684 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100685
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686 // Returns whether this loop information contains `block`.
687 // Note that this loop information *must* be populated before entering this function.
688 bool Contains(const HBasicBlock& block) const;
689
690 // Returns whether this loop information is an inner loop of `other`.
691 // Note that `other` *must* be populated before entering this function.
692 bool IsIn(const HLoopInformation& other) const;
693
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800694 // Returns true if instruction is not defined within this loop.
695 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700696
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 const ArenaBitVector& GetBlocks() const { return blocks_; }
698
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000699 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000700 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000701
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000702 void ClearAllBlocks() {
703 blocks_.ClearAllBits();
704 }
705
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 // Internal recursive implementation of `Populate`.
708 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000709 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000711 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100712 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000713 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100714 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100715 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000716
717 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
718};
719
David Brazdilec16f792015-08-19 15:04:01 +0100720// Stores try/catch information for basic blocks.
721// Note that HGraph is constructed so that catch blocks cannot simultaneously
722// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100723class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100724 public:
725 // Try block information constructor.
726 explicit TryCatchInformation(const HTryBoundary& try_entry)
727 : try_entry_(&try_entry),
728 catch_dex_file_(nullptr),
729 catch_type_index_(DexFile::kDexNoIndex16) {
730 DCHECK(try_entry_ != nullptr);
731 }
732
733 // Catch block information constructor.
734 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
735 : try_entry_(nullptr),
736 catch_dex_file_(&dex_file),
737 catch_type_index_(catch_type_index) {}
738
739 bool IsTryBlock() const { return try_entry_ != nullptr; }
740
741 const HTryBoundary& GetTryEntry() const {
742 DCHECK(IsTryBlock());
743 return *try_entry_;
744 }
745
746 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
747
748 bool IsCatchAllTypeIndex() const {
749 DCHECK(IsCatchBlock());
750 return catch_type_index_ == DexFile::kDexNoIndex16;
751 }
752
753 uint16_t GetCatchTypeIndex() const {
754 DCHECK(IsCatchBlock());
755 return catch_type_index_;
756 }
757
758 const DexFile& GetCatchDexFile() const {
759 DCHECK(IsCatchBlock());
760 return *catch_dex_file_;
761 }
762
763 private:
764 // One of possibly several TryBoundary instructions entering the block's try.
765 // Only set for try blocks.
766 const HTryBoundary* try_entry_;
767
768 // Exception type information. Only set for catch blocks.
769 const DexFile* catch_dex_file_;
770 const uint16_t catch_type_index_;
771};
772
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100773static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100774static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100775
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000776// A block in a method. Contains the list of instructions represented
777// as a double linked list. Each block knows its predecessors and
778// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100779
Vladimir Markof9f64412015-09-02 14:05:49 +0100780class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600782 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000784 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
785 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000786 loop_information_(nullptr),
787 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000788 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100789 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100790 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100791 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000792 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000793 try_catch_information_(nullptr) {
794 predecessors_.reserve(kDefaultNumberOfPredecessors);
795 successors_.reserve(kDefaultNumberOfSuccessors);
796 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
797 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798
Vladimir Marko60584552015-09-03 13:35:12 +0000799 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801 }
802
Vladimir Marko60584552015-09-03 13:35:12 +0000803 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 }
806
David Brazdild26a4112015-11-10 11:07:31 +0000807 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
808 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
809
Vladimir Marko60584552015-09-03 13:35:12 +0000810 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
811 return ContainsElement(successors_, block, start_from);
812 }
813
814 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100815 return dominated_blocks_;
816 }
817
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100818 bool IsEntryBlock() const {
819 return graph_->GetEntryBlock() == this;
820 }
821
822 bool IsExitBlock() const {
823 return graph_->GetExitBlock() == this;
824 }
825
David Brazdil46e2a392015-03-16 17:31:52 +0000826 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827 bool IsSingleTryBoundary() const;
828
829 // Returns true if this block emits nothing but a jump.
830 bool IsSingleJump() const {
831 HLoopInformation* loop_info = GetLoopInformation();
832 return (IsSingleGoto() || IsSingleTryBoundary())
833 // Back edges generate a suspend check.
834 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
835 }
David Brazdil46e2a392015-03-16 17:31:52 +0000836
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000837 void AddBackEdge(HBasicBlock* back_edge) {
838 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000840 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000842 loop_information_->AddBackEdge(back_edge);
843 }
844
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000845 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000847
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100848 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000849 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600850 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000852 HBasicBlock* GetDominator() const { return dominator_; }
853 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000854 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
855
856 void RemoveDominatedBlock(HBasicBlock* block) {
857 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100858 }
Vladimir Marko60584552015-09-03 13:35:12 +0000859
860 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
861 ReplaceElement(dominated_blocks_, existing, new_block);
862 }
863
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100864 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000865
866 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100867 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000868 }
869
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
871 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100872 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100873 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100874 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
875 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000877 HInstruction* GetFirstInstructionDisregardMoves() const;
878
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000880 successors_.push_back(block);
881 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 }
883
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100884 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
885 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100886 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000887 new_block->predecessors_.push_back(this);
888 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889 }
890
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000891 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
892 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000893 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000894 new_block->successors_.push_back(this);
895 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896 }
897
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100898 // Insert `this` between `predecessor` and `successor. This method
899 // preserves the indicies, and will update the first edge found between
900 // `predecessor` and `successor`.
901 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
902 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100903 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000904 successor->predecessors_[predecessor_index] = this;
905 predecessor->successors_[successor_index] = this;
906 successors_.push_back(successor);
907 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100908 }
909
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100910 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000911 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100912 }
913
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000914 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000915 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000916 }
917
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000919 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100920 }
921
922 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000923 predecessors_.push_back(block);
924 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100925 }
926
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100927 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 DCHECK_EQ(predecessors_.size(), 2u);
929 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100930 }
931
David Brazdil769c9e52015-04-27 13:54:09 +0100932 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000933 DCHECK_EQ(successors_.size(), 2u);
934 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100935 }
936
David Brazdilfc6a86a2015-06-26 10:33:45 +0000937 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000938 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939 }
940
David Brazdilfc6a86a2015-06-26 10:33:45 +0000941 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000942 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100943 }
944
David Brazdilfc6a86a2015-06-26 10:33:45 +0000945 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100947 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000948 }
949
950 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000951 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100952 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000953 }
954
955 // Returns whether the first occurrence of `predecessor` in the list of
956 // predecessors is at index `idx`.
957 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100958 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 return GetPredecessorIndexOf(predecessor) == idx;
960 }
961
David Brazdild7558da2015-09-22 13:04:14 +0100962 // Create a new block between this block and its predecessors. The new block
963 // is added to the graph, all predecessor edges are relinked to it and an edge
964 // is created to `this`. Returns the new empty block. Reverse post order or
965 // loop and try/catch information are not updated.
966 HBasicBlock* CreateImmediateDominator();
967
David Brazdilfc6a86a2015-06-26 10:33:45 +0000968 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100969 // created, latter block. Note that this method will add the block to the
970 // graph, create a Goto at the end of the former block and will create an edge
971 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100972 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000973 HBasicBlock* SplitBefore(HInstruction* cursor);
974
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000975 // Split the block into two blocks just after `cursor`. Returns the newly
976 // created block. Note that this method just updates raw block information,
977 // like predecessors, successors, dominators, and instruction list. It does not
978 // update the graph, reverse post order, loop information, nor make sure the
979 // blocks are consistent (for example ending with a control flow instruction).
980 HBasicBlock* SplitAfter(HInstruction* cursor);
981
David Brazdil9bc43612015-11-05 21:25:24 +0000982 // Split catch block into two blocks after the original move-exception bytecode
983 // instruction, or at the beginning if not present. Returns the newly created,
984 // latter block, or nullptr if such block could not be created (must be dead
985 // in that case). Note that this method just updates raw block information,
986 // like predecessors, successors, dominators, and instruction list. It does not
987 // update the graph, reverse post order, loop information, nor make sure the
988 // blocks are consistent (for example ending with a control flow instruction).
989 HBasicBlock* SplitCatchBlockAfterMoveException();
990
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000991 // Merge `other` at the end of `this`. Successors and dominated blocks of
992 // `other` are changed to be successors and dominated blocks of `this`. Note
993 // that this method does not update the graph, reverse post order, loop
994 // information, nor make sure the blocks are consistent (for example ending
995 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100996 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997
998 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
999 // of `this` are moved to `other`.
1000 // Note that this method does not update the graph, reverse post order, loop
1001 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001002 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001003 void ReplaceWith(HBasicBlock* other);
1004
David Brazdil2d7352b2015-04-20 14:52:42 +01001005 // Merge `other` at the end of `this`. This method updates loops, reverse post
1006 // order, links to predecessors, successors, dominators and deletes the block
1007 // from the graph. The two blocks must be successive, i.e. `this` the only
1008 // predecessor of `other` and vice versa.
1009 void MergeWith(HBasicBlock* other);
1010
1011 // Disconnects `this` from all its predecessors, successors and dominator,
1012 // removes it from all loops it is included in and eventually from the graph.
1013 // The block must not dominate any other block. Predecessors and successors
1014 // are safely updated.
1015 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001016
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001018 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001019 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001020 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001021 // Replace instruction `initial` with `replacement` within this block.
1022 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1023 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001024 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001025 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001026 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001027 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1028 // instruction list. With 'ensure_safety' set to true, it verifies that the
1029 // instruction is not in use and removes it from the use lists of its inputs.
1030 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1031 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001032 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001033
1034 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001035 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001036 }
1037
Roland Levillain6b879dd2014-09-22 17:13:44 +01001038 bool IsLoopPreHeaderFirstPredecessor() const {
1039 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001040 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001041 }
1042
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001043 bool IsFirstPredecessorBackEdge() const {
1044 DCHECK(IsLoopHeader());
1045 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1046 }
1047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048 HLoopInformation* GetLoopInformation() const {
1049 return loop_information_;
1050 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001051
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001052 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001053 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001054 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001055 void SetInLoop(HLoopInformation* info) {
1056 if (IsLoopHeader()) {
1057 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001058 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 loop_information_ = info;
1060 } else if (loop_information_->Contains(*info->GetHeader())) {
1061 // Block is currently part of an outer loop. Make it part of this inner loop.
1062 // Note that a non loop header having a loop information means this loop information
1063 // has already been populated
1064 loop_information_ = info;
1065 } else {
1066 // Block is part of an inner loop. Do not update the loop information.
1067 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1068 // at this point, because this method is being called while populating `info`.
1069 }
1070 }
1071
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 // Raw update of the loop information.
1073 void SetLoopInformation(HLoopInformation* info) {
1074 loop_information_ = info;
1075 }
1076
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001077 bool IsInLoop() const { return loop_information_ != nullptr; }
1078
David Brazdilec16f792015-08-19 15:04:01 +01001079 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1080
1081 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1082 try_catch_information_ = try_catch_information;
1083 }
1084
1085 bool IsTryBlock() const {
1086 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1087 }
1088
1089 bool IsCatchBlock() const {
1090 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1091 }
David Brazdilffee3d32015-07-06 11:48:53 +01001092
1093 // Returns the try entry that this block's successors should have. They will
1094 // be in the same try, unless the block ends in a try boundary. In that case,
1095 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001096 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001097
David Brazdild7558da2015-09-22 13:04:14 +01001098 bool HasThrowingInstructions() const;
1099
David Brazdila4b8c212015-05-07 09:59:30 +01001100 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 bool Dominates(HBasicBlock* block) const;
1102
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001103 size_t GetLifetimeStart() const { return lifetime_start_; }
1104 size_t GetLifetimeEnd() const { return lifetime_end_; }
1105
1106 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1107 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1108
David Brazdil8d5b8b22015-03-24 10:51:52 +00001109 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001110 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001111 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001112 bool HasSinglePhi() const;
1113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001114 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001115 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001116 ArenaVector<HBasicBlock*> predecessors_;
1117 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118 HInstructionList instructions_;
1119 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001120 HLoopInformation* loop_information_;
1121 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001122 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001123 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001124 // The dex program counter of the first instruction of this block.
1125 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001126 size_t lifetime_start_;
1127 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001128 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001129
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001130 friend class HGraph;
1131 friend class HInstruction;
1132
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001133 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1134};
1135
David Brazdilb2bd1c52015-03-25 11:17:37 +00001136// Iterates over the LoopInformation of all loops which contain 'block'
1137// from the innermost to the outermost.
1138class HLoopInformationOutwardIterator : public ValueObject {
1139 public:
1140 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1141 : current_(block.GetLoopInformation()) {}
1142
1143 bool Done() const { return current_ == nullptr; }
1144
1145 void Advance() {
1146 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001147 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001148 }
1149
1150 HLoopInformation* Current() const {
1151 DCHECK(!Done());
1152 return current_;
1153 }
1154
1155 private:
1156 HLoopInformation* current_;
1157
1158 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1159};
1160
Alexandre Ramesef20f712015-06-09 10:29:30 +01001161#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001162 M(Above, Condition) \
1163 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001164 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001165 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001166 M(ArrayGet, Instruction) \
1167 M(ArrayLength, Instruction) \
1168 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001169 M(Below, Condition) \
1170 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001171 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001172 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001173 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001174 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001175 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001176 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001177 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001178 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001179 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001180 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001182 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001184 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001185 M(Exit, Instruction) \
1186 M(FloatConstant, Constant) \
1187 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001188 M(GreaterThan, Condition) \
1189 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001190 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 M(InstanceFieldGet, Instruction) \
1192 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001193 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001194 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001195 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001196 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001197 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(LessThan, Condition) \
1200 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001201 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001202 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001204 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(Local, Instruction) \
1206 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001207 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001208 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001210 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001211 M(Neg, UnaryOperation) \
1212 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001214 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001216 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001217 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001218 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001219 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001221 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001222 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001223 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001224 M(Return, Instruction) \
1225 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001226 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001227 M(Shl, BinaryOperation) \
1228 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001229 M(StaticFieldGet, Instruction) \
1230 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231 M(UnresolvedInstanceFieldGet, Instruction) \
1232 M(UnresolvedInstanceFieldSet, Instruction) \
1233 M(UnresolvedStaticFieldGet, Instruction) \
1234 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001235 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(StoreLocal, Instruction) \
1237 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001239 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001240 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001241 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001242 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001243 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001244
Vladimir Markob4536b72015-11-24 13:45:23 +00001245#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001246#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001247#else
1248#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1249 M(ArmDexCacheArraysBase, Instruction)
1250#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001251
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001252#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001253#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001254#else
1255#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001256 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001257 M(Arm64IntermediateAddress, Instruction) \
1258 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001259#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001261#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1262
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001263#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1264
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001265#ifndef ART_ENABLE_CODEGEN_x86
1266#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1267#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001268#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1269 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001270 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001271 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001272 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001273#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001274
1275#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1276
1277#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1278 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1279 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1280 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001282 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001283 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1284 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1285
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001286#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1287 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001288 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001289 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001290 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001291 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001292
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001293#define FOR_EACH_INSTRUCTION(M) \
1294 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1295 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1296
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001297#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001298FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1299#undef FORWARD_DECLARATION
1300
Roland Levillainccc07a92014-09-16 14:48:16 +01001301#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001302 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001303 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001304 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001305 return other->Is##type(); \
1306 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001307 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001309#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1310 bool Is##type() const { return As##type() != nullptr; } \
1311 const H##type* As##type() const { return this; } \
1312 H##type* As##type() { return this; }
1313
David Brazdiled596192015-01-23 10:39:45 +00001314template <typename T> class HUseList;
1315
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001316template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001317class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318 public:
David Brazdiled596192015-01-23 10:39:45 +00001319 HUseListNode* GetPrevious() const { return prev_; }
1320 HUseListNode* GetNext() const { return next_; }
1321 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001322 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001323 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001324
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001325 private:
David Brazdiled596192015-01-23 10:39:45 +00001326 HUseListNode(T user, size_t index)
1327 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1328
1329 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001330 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001331 HUseListNode<T>* prev_;
1332 HUseListNode<T>* next_;
1333
1334 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001335
1336 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1337};
1338
David Brazdiled596192015-01-23 10:39:45 +00001339template <typename T>
1340class HUseList : public ValueObject {
1341 public:
1342 HUseList() : first_(nullptr) {}
1343
1344 void Clear() {
1345 first_ = nullptr;
1346 }
1347
1348 // Adds a new entry at the beginning of the use list and returns
1349 // the newly created node.
1350 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001351 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001352 if (IsEmpty()) {
1353 first_ = new_node;
1354 } else {
1355 first_->prev_ = new_node;
1356 new_node->next_ = first_;
1357 first_ = new_node;
1358 }
1359 return new_node;
1360 }
1361
1362 HUseListNode<T>* GetFirst() const {
1363 return first_;
1364 }
1365
1366 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001367 DCHECK(node != nullptr);
1368 DCHECK(Contains(node));
1369
David Brazdiled596192015-01-23 10:39:45 +00001370 if (node->prev_ != nullptr) {
1371 node->prev_->next_ = node->next_;
1372 }
1373 if (node->next_ != nullptr) {
1374 node->next_->prev_ = node->prev_;
1375 }
1376 if (node == first_) {
1377 first_ = node->next_;
1378 }
1379 }
1380
David Brazdil1abb4192015-02-17 18:33:36 +00001381 bool Contains(const HUseListNode<T>* node) const {
1382 if (node == nullptr) {
1383 return false;
1384 }
1385 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1386 if (current == node) {
1387 return true;
1388 }
1389 }
1390 return false;
1391 }
1392
David Brazdiled596192015-01-23 10:39:45 +00001393 bool IsEmpty() const {
1394 return first_ == nullptr;
1395 }
1396
1397 bool HasOnlyOneUse() const {
1398 return first_ != nullptr && first_->next_ == nullptr;
1399 }
1400
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001401 size_t SizeSlow() const {
1402 size_t count = 0;
1403 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1404 ++count;
1405 }
1406 return count;
1407 }
1408
David Brazdiled596192015-01-23 10:39:45 +00001409 private:
1410 HUseListNode<T>* first_;
1411};
1412
1413template<typename T>
1414class HUseIterator : public ValueObject {
1415 public:
1416 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1417
1418 bool Done() const { return current_ == nullptr; }
1419
1420 void Advance() {
1421 DCHECK(!Done());
1422 current_ = current_->GetNext();
1423 }
1424
1425 HUseListNode<T>* Current() const {
1426 DCHECK(!Done());
1427 return current_;
1428 }
1429
1430 private:
1431 HUseListNode<T>* current_;
1432
1433 friend class HValue;
1434};
1435
David Brazdil1abb4192015-02-17 18:33:36 +00001436// This class is used by HEnvironment and HInstruction classes to record the
1437// instructions they use and pointers to the corresponding HUseListNodes kept
1438// by the used instructions.
1439template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001440class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001441 public:
1442 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1443 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1444
1445 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1446 : instruction_(old_record.instruction_), use_node_(use_node) {
1447 DCHECK(instruction_ != nullptr);
1448 DCHECK(use_node_ != nullptr);
1449 DCHECK(old_record.use_node_ == nullptr);
1450 }
1451
1452 HInstruction* GetInstruction() const { return instruction_; }
1453 HUseListNode<T>* GetUseNode() const { return use_node_; }
1454
1455 private:
1456 // Instruction used by the user.
1457 HInstruction* instruction_;
1458
1459 // Corresponding entry in the use list kept by 'instruction_'.
1460 HUseListNode<T>* use_node_;
1461};
1462
Aart Bik854a02b2015-07-14 16:07:00 -07001463/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001465 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 * For write/read dependences on fields/arrays, the dependence analysis uses
1467 * type disambiguation (e.g. a float field write cannot modify the value of an
1468 * integer field read) and the access type (e.g. a reference array write cannot
1469 * modify the value of a reference field read [although it may modify the
1470 * reference fetch prior to reading the field, which is represented by its own
1471 * write/read dependence]). The analysis makes conservative points-to
1472 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1473 * the same, and any reference read depends on any reference read without
1474 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001475 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 * The internal representation uses 38-bit and is described in the table below.
1477 * The first line indicates the side effect, and for field/array accesses the
1478 * second line indicates the type of the access (in the order of the
1479 * Primitive::Type enum).
1480 * The two numbered lines below indicate the bit position in the bitfield (read
1481 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001482 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001483 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1484 * +-------------+---------+---------+--------------+---------+---------+
1485 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1486 * | 3 |333333322|222222221| 1 |111111110|000000000|
1487 * | 7 |654321098|765432109| 8 |765432109|876543210|
1488 *
1489 * Note that, to ease the implementation, 'changes' bits are least significant
1490 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001491 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492class SideEffects : public ValueObject {
1493 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001494 SideEffects() : flags_(0) {}
1495
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496 static SideEffects None() {
1497 return SideEffects(0);
1498 }
1499
1500 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001501 return SideEffects(kAllChangeBits | kAllDependOnBits);
1502 }
1503
1504 static SideEffects AllChanges() {
1505 return SideEffects(kAllChangeBits);
1506 }
1507
1508 static SideEffects AllDependencies() {
1509 return SideEffects(kAllDependOnBits);
1510 }
1511
1512 static SideEffects AllExceptGCDependency() {
1513 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1514 }
1515
1516 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001517 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518 }
1519
Aart Bik34c3ba92015-07-20 14:08:59 -07001520 static SideEffects AllWrites() {
1521 return SideEffects(kAllWrites);
1522 }
1523
1524 static SideEffects AllReads() {
1525 return SideEffects(kAllReads);
1526 }
1527
1528 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1529 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001530 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001531 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001532 }
1533
Aart Bik854a02b2015-07-14 16:07:00 -07001534 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1535 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 }
1537
Aart Bik34c3ba92015-07-20 14:08:59 -07001538 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1539 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001540 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001541 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001542 }
1543
1544 static SideEffects ArrayReadOfType(Primitive::Type type) {
1545 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1546 }
1547
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 static SideEffects CanTriggerGC() {
1549 return SideEffects(1ULL << kCanTriggerGCBit);
1550 }
1551
1552 static SideEffects DependsOnGC() {
1553 return SideEffects(1ULL << kDependsOnGCBit);
1554 }
1555
Aart Bik854a02b2015-07-14 16:07:00 -07001556 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001557 SideEffects Union(SideEffects other) const {
1558 return SideEffects(flags_ | other.flags_);
1559 }
1560
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001561 SideEffects Exclusion(SideEffects other) const {
1562 return SideEffects(flags_ & ~other.flags_);
1563 }
1564
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001565 void Add(SideEffects other) {
1566 flags_ |= other.flags_;
1567 }
1568
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 bool Includes(SideEffects other) const {
1570 return (other.flags_ & flags_) == other.flags_;
1571 }
1572
1573 bool HasSideEffects() const {
1574 return (flags_ & kAllChangeBits);
1575 }
1576
1577 bool HasDependencies() const {
1578 return (flags_ & kAllDependOnBits);
1579 }
1580
1581 // Returns true if there are no side effects or dependencies.
1582 bool DoesNothing() const {
1583 return flags_ == 0;
1584 }
1585
Aart Bik854a02b2015-07-14 16:07:00 -07001586 // Returns true if something is written.
1587 bool DoesAnyWrite() const {
1588 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001589 }
1590
Aart Bik854a02b2015-07-14 16:07:00 -07001591 // Returns true if something is read.
1592 bool DoesAnyRead() const {
1593 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001594 }
1595
Aart Bik854a02b2015-07-14 16:07:00 -07001596 // Returns true if potentially everything is written and read
1597 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001598 bool DoesAllReadWrite() const {
1599 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1600 }
1601
Aart Bik854a02b2015-07-14 16:07:00 -07001602 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001604 }
1605
Roland Levillain0d5a2812015-11-13 10:07:31 +00001606 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001607 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001608 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1609 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001610 }
1611
1612 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001614 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001615 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 for (int s = kLastBit; s >= 0; s--) {
1617 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1618 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1619 // This is a bit for the GC side effect.
1620 if (current_bit_is_set) {
1621 flags += "GC";
1622 }
Aart Bik854a02b2015-07-14 16:07:00 -07001623 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 } else {
1625 // This is a bit for the array/field analysis.
1626 // The underscore character stands for the 'can trigger GC' bit.
1627 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1628 if (current_bit_is_set) {
1629 flags += kDebug[s];
1630 }
1631 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1632 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1633 flags += "|";
1634 }
1635 }
Aart Bik854a02b2015-07-14 16:07:00 -07001636 }
1637 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001638 }
1639
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 private:
1643 static constexpr int kFieldArrayAnalysisBits = 9;
1644
1645 static constexpr int kFieldWriteOffset = 0;
1646 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1647 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1648 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1649
1650 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1651
1652 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1653 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1654 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1655 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1656
1657 static constexpr int kLastBit = kDependsOnGCBit;
1658 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1659
1660 // Aliases.
1661
1662 static_assert(kChangeBits == kDependOnBits,
1663 "the 'change' bits should match the 'depend on' bits.");
1664
1665 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1666 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1667 static constexpr uint64_t kAllWrites =
1668 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1669 static constexpr uint64_t kAllReads =
1670 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Work around the fact that HIR aliases I/F and J/D.
1673 // TODO: remove this interceptor once HIR types are clean
1674 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1675 switch (type) {
1676 case Primitive::kPrimInt:
1677 case Primitive::kPrimFloat:
1678 return TypeFlag(Primitive::kPrimInt, offset) |
1679 TypeFlag(Primitive::kPrimFloat, offset);
1680 case Primitive::kPrimLong:
1681 case Primitive::kPrimDouble:
1682 return TypeFlag(Primitive::kPrimLong, offset) |
1683 TypeFlag(Primitive::kPrimDouble, offset);
1684 default:
1685 return TypeFlag(type, offset);
1686 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001687 }
1688
Aart Bik854a02b2015-07-14 16:07:00 -07001689 // Translates type to bit flag.
1690 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1691 CHECK_NE(type, Primitive::kPrimVoid);
1692 const uint64_t one = 1;
1693 const int shift = type; // 0-based consecutive enum
1694 DCHECK_LE(kFieldWriteOffset, shift);
1695 DCHECK_LT(shift, kArrayWriteOffset);
1696 return one << (type + offset);
1697 }
1698
1699 // Private constructor on direct flags value.
1700 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1701
1702 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001703};
1704
David Brazdiled596192015-01-23 10:39:45 +00001705// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001706class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001707 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001708 HEnvironment(ArenaAllocator* arena,
1709 size_t number_of_vregs,
1710 const DexFile& dex_file,
1711 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001712 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001713 InvokeType invoke_type,
1714 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001715 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1716 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001717 parent_(nullptr),
1718 dex_file_(dex_file),
1719 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001720 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001721 invoke_type_(invoke_type),
1722 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 }
1724
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001725 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001726 : HEnvironment(arena,
1727 to_copy.Size(),
1728 to_copy.GetDexFile(),
1729 to_copy.GetMethodIdx(),
1730 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001731 to_copy.GetInvokeType(),
1732 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001733
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001735 if (parent_ != nullptr) {
1736 parent_->SetAndCopyParentChain(allocator, parent);
1737 } else {
1738 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1739 parent_->CopyFrom(parent);
1740 if (parent->GetParent() != nullptr) {
1741 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1742 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001743 }
David Brazdiled596192015-01-23 10:39:45 +00001744 }
1745
Vladimir Marko71bf8092015-09-15 15:33:14 +01001746 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001747 void CopyFrom(HEnvironment* environment);
1748
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001749 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1750 // input to the loop phi instead. This is for inserting instructions that
1751 // require an environment (like HDeoptimization) in the loop pre-header.
1752 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001753
1754 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001755 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001756 }
1757
1758 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001759 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001760 }
1761
David Brazdil1abb4192015-02-17 18:33:36 +00001762 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001763
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001764 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 HEnvironment* GetParent() const { return parent_; }
1767
1768 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001769 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001770 }
1771
1772 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001773 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001774 }
1775
1776 uint32_t GetDexPc() const {
1777 return dex_pc_;
1778 }
1779
1780 uint32_t GetMethodIdx() const {
1781 return method_idx_;
1782 }
1783
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001784 InvokeType GetInvokeType() const {
1785 return invoke_type_;
1786 }
1787
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 const DexFile& GetDexFile() const {
1789 return dex_file_;
1790 }
1791
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001792 HInstruction* GetHolder() const {
1793 return holder_;
1794 }
1795
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001796
1797 bool IsFromInlinedInvoke() const {
1798 return GetParent() != nullptr;
1799 }
1800
David Brazdiled596192015-01-23 10:39:45 +00001801 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001802 // Record instructions' use entries of this environment for constant-time removal.
1803 // It should only be called by HInstruction when a new environment use is added.
1804 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1805 DCHECK(env_use->GetUser() == this);
1806 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001807 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001808 }
David Brazdiled596192015-01-23 10:39:45 +00001809
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001810 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1811 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001812 HEnvironment* parent_;
1813 const DexFile& dex_file_;
1814 const uint32_t method_idx_;
1815 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001816 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001817
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001818 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001819 HInstruction* const holder_;
1820
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001821 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001822
1823 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1824};
1825
Vladimir Markof9f64412015-09-02 14:05:49 +01001826class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001827 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001828 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001829 : previous_(nullptr),
1830 next_(nullptr),
1831 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001832 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001833 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001834 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001835 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001837 locations_(nullptr),
1838 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001839 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001840 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001841 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842
Dave Allison20dfc792014-06-16 20:44:29 -07001843 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001844
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001845#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001846 enum InstructionKind {
1847 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1848 };
1849#undef DECLARE_KIND
1850
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 HInstruction* GetNext() const { return next_; }
1852 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853
Calin Juravle77520bc2015-01-12 18:45:46 +00001854 HInstruction* GetNextDisregardingMoves() const;
1855 HInstruction* GetPreviousDisregardingMoves() const;
1856
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001857 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001858 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001860 bool IsInBlock() const { return block_ != nullptr; }
1861 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001862 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1863 bool IsIrreducibleLoopHeaderPhi() const {
1864 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1865 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001866
Roland Levillain6b879dd2014-09-22 17:13:44 +01001867 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001868 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869
1870 virtual void Accept(HGraphVisitor* visitor) = 0;
1871 virtual const char* DebugName() const = 0;
1872
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001874 void SetRawInputAt(size_t index, HInstruction* input) {
1875 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1876 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001879
1880 uint32_t GetDexPc() const { return dex_pc_; }
1881
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001882 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001883
Roland Levillaine161a2a2014-10-03 12:45:18 +01001884 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001885 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001886
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001887 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001888 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889
Calin Juravle10e244f2015-01-26 18:54:32 +00001890 // Does not apply for all instructions, but having this at top level greatly
1891 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001892 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001893 virtual bool CanBeNull() const {
1894 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1895 return true;
1896 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001898 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001899 return false;
1900 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001901
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001902 virtual bool IsActualObject() const {
1903 return GetType() == Primitive::kPrimNot;
1904 }
1905
Calin Juravle2e768302015-07-28 14:41:11 +00001906 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001907
Calin Juravle61d544b2015-02-23 16:46:57 +00001908 ReferenceTypeInfo GetReferenceTypeInfo() const {
1909 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1910 return reference_type_info_;
1911 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001912
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001914 DCHECK(user != nullptr);
1915 HUseListNode<HInstruction*>* use =
1916 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1917 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001918 }
1919
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001921 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001922 HUseListNode<HEnvironment*>* env_use =
1923 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1924 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925 }
1926
David Brazdil1abb4192015-02-17 18:33:36 +00001927 void RemoveAsUserOfInput(size_t input) {
1928 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1929 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1930 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001931
David Brazdil1abb4192015-02-17 18:33:36 +00001932 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1933 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934
David Brazdiled596192015-01-23 10:39:45 +00001935 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1936 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001937 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001938 bool HasOnlyOneNonEnvironmentUse() const {
1939 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1940 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001941
Roland Levillain6c82d402014-10-13 16:10:27 +01001942 // Does this instruction strictly dominate `other_instruction`?
1943 // Returns false if this instruction and `other_instruction` are the same.
1944 // Aborts if this instruction and `other_instruction` are both phis.
1945 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001946
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001947 int GetId() const { return id_; }
1948 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001950 int GetSsaIndex() const { return ssa_index_; }
1951 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1952 bool HasSsaIndex() const { return ssa_index_ != -1; }
1953
1954 bool HasEnvironment() const { return environment_ != nullptr; }
1955 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001956 // Set the `environment_` field. Raw because this method does not
1957 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 void SetRawEnvironment(HEnvironment* environment) {
1959 DCHECK(environment_ == nullptr);
1960 DCHECK_EQ(environment->GetHolder(), this);
1961 environment_ = environment;
1962 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001963
1964 // Set the environment of this instruction, copying it from `environment`. While
1965 // copying, the uses lists are being updated.
1966 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001968 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001969 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001970 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001971 if (environment->GetParent() != nullptr) {
1972 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1973 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001974 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001975
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001976 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1977 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001978 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001979 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001980 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001981 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001982 if (environment->GetParent() != nullptr) {
1983 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1984 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001985 }
1986
Nicolas Geoffray39468442014-09-02 15:17:15 +01001987 // Returns the number of entries in the environment. Typically, that is the
1988 // number of dex registers in a method. It could be more in case of inlining.
1989 size_t EnvironmentSize() const;
1990
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001991 LocationSummary* GetLocations() const { return locations_; }
1992 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001994 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001995 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996
Alexandre Rames188d4312015-04-09 18:30:21 +01001997 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1998 // uses of this instruction by `other` are *not* updated.
1999 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2000 ReplaceWith(other);
2001 other->ReplaceInput(this, use_index);
2002 }
2003
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002004 // Move `this` instruction before `cursor`.
2005 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002006
Vladimir Markofb337ea2015-11-25 15:25:10 +00002007 // Move `this` before its first user and out of any loops. If there is no
2008 // out-of-loop user that dominates all other users, move the instruction
2009 // to the end of the out-of-loop common dominator of the user's blocks.
2010 //
2011 // This can be used only on non-throwing instructions with no side effects that
2012 // have at least one use but no environment uses.
2013 void MoveBeforeFirstUserAndOutOfLoops();
2014
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002015#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 bool Is##type() const; \
2017 const H##type* As##type() const; \
2018 H##type* As##type();
2019
2020 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2021#undef INSTRUCTION_TYPE_CHECK
2022
2023#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002024 bool Is##type() const { return (As##type() != nullptr); } \
2025 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002026 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028#undef INSTRUCTION_TYPE_CHECK
2029
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030 // Returns whether the instruction can be moved within the graph.
2031 virtual bool CanBeMoved() const { return false; }
2032
2033 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002035 return false;
2036 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037
2038 // Returns whether any data encoded in the two instructions is equal.
2039 // This method does not look at the inputs. Both instructions must be
2040 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002041 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002042 return false;
2043 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002044
2045 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002046 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002047 // 2) Their inputs are identical.
2048 bool Equals(HInstruction* other) const;
2049
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002050 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2051 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2052 // the virtual function because the __attribute__((__pure__)) doesn't really
2053 // apply the strong requirement for virtual functions, preventing optimizations.
2054 InstructionKind GetKind() const PURE;
2055 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002056
2057 virtual size_t ComputeHashCode() const {
2058 size_t result = GetKind();
2059 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2060 result = (result * 31) + InputAt(i)->GetId();
2061 }
2062 return result;
2063 }
2064
2065 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002066 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002067 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002068
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002069 size_t GetLifetimePosition() const { return lifetime_position_; }
2070 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2071 LiveInterval* GetLiveInterval() const { return live_interval_; }
2072 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2073 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2074
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002075 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2076
2077 // Returns whether the code generation of the instruction will require to have access
2078 // to the current method. Such instructions are:
2079 // (1): Instructions that require an environment, as calling the runtime requires
2080 // to walk the stack and have the current method stored at a specific stack address.
2081 // (2): Object literals like classes and strings, that are loaded from the dex cache
2082 // fields of the current method.
2083 bool NeedsCurrentMethod() const {
2084 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2085 }
2086
Vladimir Markodc151b22015-10-15 18:02:30 +01002087 // Returns whether the code generation of the instruction will require to have access
2088 // to the dex cache of the current method's declaring class via the current method.
2089 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002090
Mark Mendellc4701932015-04-10 13:18:51 -04002091 // Does this instruction have any use in an environment before
2092 // control flow hits 'other'?
2093 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2094
2095 // Remove all references to environment uses of this instruction.
2096 // The caller must ensure that this is safe to do.
2097 void RemoveEnvironmentUsers();
2098
David Brazdilb3e773e2016-01-26 11:28:37 +00002099 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2100 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2101
David Brazdil1abb4192015-02-17 18:33:36 +00002102 protected:
2103 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2104 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2105
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002106 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002107 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2108
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002109 HInstruction* previous_;
2110 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002111 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002112 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002114 // An instruction gets an id when it is added to the graph.
2115 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002116 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002117 int id_;
2118
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002119 // When doing liveness analysis, instructions that have uses get an SSA index.
2120 int ssa_index_;
2121
David Brazdilb3e773e2016-01-26 11:28:37 +00002122 // If set, the machine code for this instruction is assumed to be generated by
2123 // its users. Used by liveness analysis to compute use positions accordingly.
2124 bool emitted_at_use_site_;
2125
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002127 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002128
2129 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002130 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131
Nicolas Geoffray39468442014-09-02 15:17:15 +01002132 // The environment associated with this instruction. Not null if the instruction
2133 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002134 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002135
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002136 // Set by the code generator.
2137 LocationSummary* locations_;
2138
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002139 // Set by the liveness analysis.
2140 LiveInterval* live_interval_;
2141
2142 // Set by the liveness analysis, this is the position in a linear
2143 // order of blocks where this instruction's live interval start.
2144 size_t lifetime_position_;
2145
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002146 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002147
Calin Juravleacf735c2015-02-12 15:25:22 +00002148 // TODO: for primitive types this should be marked as invalid.
2149 ReferenceTypeInfo reference_type_info_;
2150
David Brazdil1abb4192015-02-17 18:33:36 +00002151 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002153 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002154 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002155 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
2157 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2158};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002159std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002160
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002161class HInputIterator : public ValueObject {
2162 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002163 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002164
2165 bool Done() const { return index_ == instruction_->InputCount(); }
2166 HInstruction* Current() const { return instruction_->InputAt(index_); }
2167 void Advance() { index_++; }
2168
2169 private:
2170 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002171 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002172
2173 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2174};
2175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176class HInstructionIterator : public ValueObject {
2177 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178 explicit HInstructionIterator(const HInstructionList& instructions)
2179 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002180 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181 }
2182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183 bool Done() const { return instruction_ == nullptr; }
2184 HInstruction* Current() const { return instruction_; }
2185 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002187 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188 }
2189
2190 private:
2191 HInstruction* instruction_;
2192 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002193
2194 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195};
2196
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002197class HBackwardInstructionIterator : public ValueObject {
2198 public:
2199 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2200 : instruction_(instructions.last_instruction_) {
2201 next_ = Done() ? nullptr : instruction_->GetPrevious();
2202 }
2203
2204 bool Done() const { return instruction_ == nullptr; }
2205 HInstruction* Current() const { return instruction_; }
2206 void Advance() {
2207 instruction_ = next_;
2208 next_ = Done() ? nullptr : instruction_->GetPrevious();
2209 }
2210
2211 private:
2212 HInstruction* instruction_;
2213 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002214
2215 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002216};
2217
Vladimir Markof9f64412015-09-02 14:05:49 +01002218template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219class HTemplateInstruction: public HInstruction {
2220 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002221 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002223 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002225 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002228 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2229 DCHECK_LT(i, N);
2230 return inputs_[i];
2231 }
David Brazdil1abb4192015-02-17 18:33:36 +00002232
2233 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002234 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002235 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236 }
2237
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002239 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002240
2241 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242};
2243
Vladimir Markof9f64412015-09-02 14:05:49 +01002244// HTemplateInstruction specialization for N=0.
2245template<>
2246class HTemplateInstruction<0>: public HInstruction {
2247 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002248 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002249 : HInstruction(side_effects, dex_pc) {}
2250
Vladimir Markof9f64412015-09-02 14:05:49 +01002251 virtual ~HTemplateInstruction() {}
2252
2253 size_t InputCount() const OVERRIDE { return 0; }
2254
2255 protected:
2256 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2257 LOG(FATAL) << "Unreachable";
2258 UNREACHABLE();
2259 }
2260
2261 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2262 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2263 LOG(FATAL) << "Unreachable";
2264 UNREACHABLE();
2265 }
2266
2267 private:
2268 friend class SsaBuilder;
2269};
2270
Dave Allison20dfc792014-06-16 20:44:29 -07002271template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002272class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002273 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002274 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002276 virtual ~HExpression() {}
2277
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002278 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002279
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002280 protected:
2281 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002282};
2283
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2285// instruction that branches to the exit block.
2286class HReturnVoid : public HTemplateInstruction<0> {
2287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2289 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002290
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002291 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002293 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294
2295 private:
2296 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2297};
2298
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299// Represents dex's RETURN opcodes. A HReturn is a control flow
2300// instruction that branches to the exit block.
2301class HReturn : public HTemplateInstruction<1> {
2302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2304 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002305 SetRawInputAt(0, value);
2306 }
2307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002308 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002309
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002310 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002311
2312 private:
2313 DISALLOW_COPY_AND_ASSIGN(HReturn);
2314};
2315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002317// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318// exit block.
2319class HExit : public HTemplateInstruction<0> {
2320 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002325 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002326
2327 private:
2328 DISALLOW_COPY_AND_ASSIGN(HExit);
2329};
2330
2331// Jumps from one block to another.
2332class HGoto : public HTemplateInstruction<0> {
2333 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002334 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002335
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002336 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002337
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002338 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002339 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002340 }
2341
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002342 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343
2344 private:
2345 DISALLOW_COPY_AND_ASSIGN(HGoto);
2346};
2347
Roland Levillain9867bc72015-08-05 10:21:34 +01002348class HConstant : public HExpression<0> {
2349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002350 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2351 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002352
2353 bool CanBeMoved() const OVERRIDE { return true; }
2354
2355 virtual bool IsMinusOne() const { return false; }
2356 virtual bool IsZero() const { return false; }
2357 virtual bool IsOne() const { return false; }
2358
David Brazdil77a48ae2015-09-15 12:34:04 +00002359 virtual uint64_t GetValueAsUint64() const = 0;
2360
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002361 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002362
2363 private:
2364 DISALLOW_COPY_AND_ASSIGN(HConstant);
2365};
2366
2367class HNullConstant : public HConstant {
2368 public:
2369 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2370 return true;
2371 }
2372
David Brazdil77a48ae2015-09-15 12:34:04 +00002373 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2374
Roland Levillain9867bc72015-08-05 10:21:34 +01002375 size_t ComputeHashCode() const OVERRIDE { return 0; }
2376
2377 DECLARE_INSTRUCTION(NullConstant);
2378
2379 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002380 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002381
2382 friend class HGraph;
2383 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2384};
2385
2386// Constants of the type int. Those can be from Dex instructions, or
2387// synthesized (for example with the if-eqz instruction).
2388class HIntConstant : public HConstant {
2389 public:
2390 int32_t GetValue() const { return value_; }
2391
David Brazdil9f389d42015-10-01 14:32:56 +01002392 uint64_t GetValueAsUint64() const OVERRIDE {
2393 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2394 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002395
Roland Levillain9867bc72015-08-05 10:21:34 +01002396 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002397 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002398 return other->AsIntConstant()->value_ == value_;
2399 }
2400
2401 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2402
2403 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2404 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2405 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2406
2407 DECLARE_INSTRUCTION(IntConstant);
2408
2409 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002410 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2411 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2412 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2413 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002414
2415 const int32_t value_;
2416
2417 friend class HGraph;
2418 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2419 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2420 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2421};
2422
2423class HLongConstant : public HConstant {
2424 public:
2425 int64_t GetValue() const { return value_; }
2426
David Brazdil77a48ae2015-09-15 12:34:04 +00002427 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2428
Roland Levillain9867bc72015-08-05 10:21:34 +01002429 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002430 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002431 return other->AsLongConstant()->value_ == value_;
2432 }
2433
2434 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2435
2436 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2437 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2438 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2439
2440 DECLARE_INSTRUCTION(LongConstant);
2441
2442 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002443 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2444 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002445
2446 const int64_t value_;
2447
2448 friend class HGraph;
2449 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2450};
Dave Allison20dfc792014-06-16 20:44:29 -07002451
Roland Levillain31dd3d62016-02-16 12:21:02 +00002452class HFloatConstant : public HConstant {
2453 public:
2454 float GetValue() const { return value_; }
2455
2456 uint64_t GetValueAsUint64() const OVERRIDE {
2457 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2458 }
2459
2460 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2461 DCHECK(other->IsFloatConstant()) << other->DebugName();
2462 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2463 }
2464
2465 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2466
2467 bool IsMinusOne() const OVERRIDE {
2468 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2469 }
2470 bool IsZero() const OVERRIDE {
2471 return value_ == 0.0f;
2472 }
2473 bool IsOne() const OVERRIDE {
2474 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2475 }
2476 bool IsNaN() const {
2477 return std::isnan(value_);
2478 }
2479
2480 DECLARE_INSTRUCTION(FloatConstant);
2481
2482 private:
2483 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2484 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2485 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2486 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2487
2488 const float value_;
2489
2490 // Only the SsaBuilder and HGraph can create floating-point constants.
2491 friend class SsaBuilder;
2492 friend class HGraph;
2493 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2494};
2495
2496class HDoubleConstant : public HConstant {
2497 public:
2498 double GetValue() const { return value_; }
2499
2500 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2501
2502 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2503 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2504 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2505 }
2506
2507 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2508
2509 bool IsMinusOne() const OVERRIDE {
2510 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2511 }
2512 bool IsZero() const OVERRIDE {
2513 return value_ == 0.0;
2514 }
2515 bool IsOne() const OVERRIDE {
2516 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2517 }
2518 bool IsNaN() const {
2519 return std::isnan(value_);
2520 }
2521
2522 DECLARE_INSTRUCTION(DoubleConstant);
2523
2524 private:
2525 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2526 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2527 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2528 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2529
2530 const double value_;
2531
2532 // Only the SsaBuilder and HGraph can create floating-point constants.
2533 friend class SsaBuilder;
2534 friend class HGraph;
2535 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2536};
2537
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002538// Conditional branch. A block ending with an HIf instruction must have
2539// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002540class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002541 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002542 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2543 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 SetRawInputAt(0, input);
2545 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002546
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002547 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002548
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002549 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002550 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002551 }
2552
2553 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002554 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002555 }
2556
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002557 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002558
2559 private:
2560 DISALLOW_COPY_AND_ASSIGN(HIf);
2561};
2562
David Brazdilfc6a86a2015-06-26 10:33:45 +00002563
2564// Abstract instruction which marks the beginning and/or end of a try block and
2565// links it to the respective exception handlers. Behaves the same as a Goto in
2566// non-exceptional control flow.
2567// Normal-flow successor is stored at index zero, exception handlers under
2568// higher indices in no particular order.
2569class HTryBoundary : public HTemplateInstruction<0> {
2570 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002571 enum BoundaryKind {
2572 kEntry,
2573 kExit,
2574 };
2575
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002576 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2577 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002578
2579 bool IsControlFlow() const OVERRIDE { return true; }
2580
2581 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002582 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002583
David Brazdild26a4112015-11-10 11:07:31 +00002584 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2585 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2586 }
2587
David Brazdilfc6a86a2015-06-26 10:33:45 +00002588 // Returns whether `handler` is among its exception handlers (non-zero index
2589 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002590 bool HasExceptionHandler(const HBasicBlock& handler) const {
2591 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002592 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002593 }
2594
2595 // If not present already, adds `handler` to its block's list of exception
2596 // handlers.
2597 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002598 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002599 GetBlock()->AddSuccessor(handler);
2600 }
2601 }
2602
David Brazdil56e1acc2015-06-30 15:41:36 +01002603 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002604
David Brazdilffee3d32015-07-06 11:48:53 +01002605 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2606
David Brazdilfc6a86a2015-06-26 10:33:45 +00002607 DECLARE_INSTRUCTION(TryBoundary);
2608
2609 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002610 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002611
2612 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2613};
2614
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002615// Deoptimize to interpreter, upon checking a condition.
2616class HDeoptimize : public HTemplateInstruction<1> {
2617 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002618 // We set CanTriggerGC to prevent any intermediate address to be live
2619 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002620 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002621 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002622 SetRawInputAt(0, cond);
2623 }
2624
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002625 bool CanBeMoved() const OVERRIDE { return true; }
2626 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2627 return true;
2628 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002629 bool NeedsEnvironment() const OVERRIDE { return true; }
2630 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002631
2632 DECLARE_INSTRUCTION(Deoptimize);
2633
2634 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002635 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2636};
2637
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002638// Represents the ArtMethod that was passed as a first argument to
2639// the method. It is used by instructions that depend on it, like
2640// instructions that work with the dex cache.
2641class HCurrentMethod : public HExpression<0> {
2642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002643 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2644 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002645
2646 DECLARE_INSTRUCTION(CurrentMethod);
2647
2648 private:
2649 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2650};
2651
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002652// Fetches an ArtMethod from the virtual table or the interface method table
2653// of a class.
2654class HClassTableGet : public HExpression<1> {
2655 public:
2656 enum TableKind {
2657 kVTable,
2658 kIMTable,
2659 };
2660 HClassTableGet(HInstruction* cls,
2661 Primitive::Type type,
2662 TableKind kind,
2663 size_t index,
2664 uint32_t dex_pc)
2665 : HExpression(type, SideEffects::None(), dex_pc),
2666 index_(index),
2667 table_kind_(kind) {
2668 SetRawInputAt(0, cls);
2669 }
2670
2671 bool CanBeMoved() const OVERRIDE { return true; }
2672 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2673 return other->AsClassTableGet()->GetIndex() == index_ &&
2674 other->AsClassTableGet()->GetTableKind() == table_kind_;
2675 }
2676
2677 TableKind GetTableKind() const { return table_kind_; }
2678 size_t GetIndex() const { return index_; }
2679
2680 DECLARE_INSTRUCTION(ClassTableGet);
2681
2682 private:
2683 // The index of the ArtMethod in the table.
2684 const size_t index_;
2685 const TableKind table_kind_;
2686
2687 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2688};
2689
Mark Mendellfe57faa2015-09-18 09:26:15 -04002690// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2691// have one successor for each entry in the switch table, and the final successor
2692// will be the block containing the next Dex opcode.
2693class HPackedSwitch : public HTemplateInstruction<1> {
2694 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002695 HPackedSwitch(int32_t start_value,
2696 uint32_t num_entries,
2697 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002698 uint32_t dex_pc = kNoDexPc)
2699 : HTemplateInstruction(SideEffects::None(), dex_pc),
2700 start_value_(start_value),
2701 num_entries_(num_entries) {
2702 SetRawInputAt(0, input);
2703 }
2704
2705 bool IsControlFlow() const OVERRIDE { return true; }
2706
2707 int32_t GetStartValue() const { return start_value_; }
2708
Vladimir Marko211c2112015-09-24 16:52:33 +01002709 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002710
2711 HBasicBlock* GetDefaultBlock() const {
2712 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002713 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002714 }
2715 DECLARE_INSTRUCTION(PackedSwitch);
2716
2717 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002718 const int32_t start_value_;
2719 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002720
2721 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2722};
2723
Roland Levillain88cb1752014-10-20 16:36:47 +01002724class HUnaryOperation : public HExpression<1> {
2725 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2727 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002728 SetRawInputAt(0, input);
2729 }
2730
2731 HInstruction* GetInput() const { return InputAt(0); }
2732 Primitive::Type GetResultType() const { return GetType(); }
2733
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002734 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002735 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002736 return true;
2737 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002738
Roland Levillain31dd3d62016-02-16 12:21:02 +00002739 // Try to statically evaluate `this` and return a HConstant
2740 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002741 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002742 HConstant* TryStaticEvaluation() const;
2743
2744 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002745 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2746 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002747 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2748 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002749
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002750 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002751
2752 private:
2753 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2754};
2755
Dave Allison20dfc792014-06-16 20:44:29 -07002756class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002757 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002758 HBinaryOperation(Primitive::Type result_type,
2759 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002760 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002761 SideEffects side_effects = SideEffects::None(),
2762 uint32_t dex_pc = kNoDexPc)
2763 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002764 SetRawInputAt(0, left);
2765 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002766 }
2767
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002768 HInstruction* GetLeft() const { return InputAt(0); }
2769 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002770 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002771
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002772 virtual bool IsCommutative() const { return false; }
2773
2774 // Put constant on the right.
2775 // Returns whether order is changed.
2776 bool OrderInputsWithConstantOnTheRight() {
2777 HInstruction* left = InputAt(0);
2778 HInstruction* right = InputAt(1);
2779 if (left->IsConstant() && !right->IsConstant()) {
2780 ReplaceInput(right, 0);
2781 ReplaceInput(left, 1);
2782 return true;
2783 }
2784 return false;
2785 }
2786
2787 // Order inputs by instruction id, but favor constant on the right side.
2788 // This helps GVN for commutative ops.
2789 void OrderInputs() {
2790 DCHECK(IsCommutative());
2791 HInstruction* left = InputAt(0);
2792 HInstruction* right = InputAt(1);
2793 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2794 return;
2795 }
2796 if (OrderInputsWithConstantOnTheRight()) {
2797 return;
2798 }
2799 // Order according to instruction id.
2800 if (left->GetId() > right->GetId()) {
2801 ReplaceInput(right, 0);
2802 ReplaceInput(left, 1);
2803 }
2804 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002806 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002807 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002808 return true;
2809 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002810
Roland Levillain31dd3d62016-02-16 12:21:02 +00002811 // Try to statically evaluate `this` and return a HConstant
2812 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002813 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002814 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002815
2816 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002817 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2818 HNullConstant* y ATTRIBUTE_UNUSED) const {
2819 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2820 return nullptr;
2821 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002822 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2823 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2824 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2825 HLongConstant* y ATTRIBUTE_UNUSED) const {
2826 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2827 return nullptr;
2828 }
2829 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2830 HIntConstant* y ATTRIBUTE_UNUSED) const {
2831 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2832 return nullptr;
2833 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002834 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2835 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002836
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002837 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002838 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002839 HConstant* GetConstantRight() const;
2840
2841 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002842 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002843 HInstruction* GetLeastConstantLeft() const;
2844
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002845 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002846
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002847 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002848 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2849};
2850
Mark Mendellc4701932015-04-10 13:18:51 -04002851// The comparison bias applies for floating point operations and indicates how NaN
2852// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002853enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002854 kNoBias, // bias is not applicable (i.e. for long operation)
2855 kGtBias, // return 1 for NaN comparisons
2856 kLtBias, // return -1 for NaN comparisons
2857};
2858
Roland Levillain31dd3d62016-02-16 12:21:02 +00002859std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2860
Dave Allison20dfc792014-06-16 20:44:29 -07002861class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002862 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002863 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2864 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002865 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002866
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002867 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002868 // `instruction`, and disregard moves in between.
2869 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002870
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002871 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002872
2873 virtual IfCondition GetCondition() const = 0;
2874
Mark Mendellc4701932015-04-10 13:18:51 -04002875 virtual IfCondition GetOppositeCondition() const = 0;
2876
Roland Levillain4fa13f62015-07-06 18:11:54 +01002877 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002878 ComparisonBias GetBias() const { return bias_; }
Mark Mendellc4701932015-04-10 13:18:51 -04002879 void SetBias(ComparisonBias bias) { bias_ = bias; }
2880
2881 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2882 return bias_ == other->AsCondition()->bias_;
2883 }
2884
Roland Levillain4fa13f62015-07-06 18:11:54 +01002885 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002886 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002887 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002888 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002889 }
2890
2891 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002892 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002893 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002894 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002895 }
2896
Roland Levillain31dd3d62016-02-16 12:21:02 +00002897 protected:
2898 template <typename T>
2899 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2900
2901 template <typename T>
2902 int32_t CompareFP(T x, T y) const {
2903 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
2904 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
2905 // Handle the bias.
2906 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
2907 }
2908
2909 // Return an integer constant containing the result of a condition evaluated at compile time.
2910 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
2911 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
2912 }
2913
Dave Allison20dfc792014-06-16 20:44:29 -07002914 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002915 // Needed if we merge a HCompare into a HCondition.
2916 ComparisonBias bias_;
2917
Dave Allison20dfc792014-06-16 20:44:29 -07002918 DISALLOW_COPY_AND_ASSIGN(HCondition);
2919};
2920
2921// Instruction to check if two inputs are equal to each other.
2922class HEqual : public HCondition {
2923 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2925 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002926
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002927 bool IsCommutative() const OVERRIDE { return true; }
2928
Vladimir Marko9e23df52015-11-10 17:14:35 +00002929 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2930 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002931 return MakeConstantCondition(true, GetDexPc());
2932 }
2933 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2934 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2935 }
2936 // In the following Evaluate methods, a HCompare instruction has
2937 // been merged into this HEqual instruction; evaluate it as
2938 // `Compare(x, y) == 0`.
2939 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2940 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
2941 GetDexPc());
2942 }
2943 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2944 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2945 }
2946 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
2947 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00002948 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002949
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002950 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002951
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002952 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002953 return kCondEQ;
2954 }
2955
Mark Mendellc4701932015-04-10 13:18:51 -04002956 IfCondition GetOppositeCondition() const OVERRIDE {
2957 return kCondNE;
2958 }
2959
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002960 private:
Aart Bike9f37602015-10-09 11:15:55 -07002961 template <typename T> bool Compute(T x, T y) const { return x == y; }
2962
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002963 DISALLOW_COPY_AND_ASSIGN(HEqual);
2964};
2965
Dave Allison20dfc792014-06-16 20:44:29 -07002966class HNotEqual : public HCondition {
2967 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002968 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2969 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002970
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002971 bool IsCommutative() const OVERRIDE { return true; }
2972
Vladimir Marko9e23df52015-11-10 17:14:35 +00002973 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2974 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002975 return MakeConstantCondition(false, GetDexPc());
2976 }
2977 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2978 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2979 }
2980 // In the following Evaluate methods, a HCompare instruction has
2981 // been merged into this HNotEqual instruction; evaluate it as
2982 // `Compare(x, y) != 0`.
2983 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2984 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
2985 }
2986 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2987 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2988 }
2989 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
2990 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00002991 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002992
Dave Allison20dfc792014-06-16 20:44:29 -07002993 DECLARE_INSTRUCTION(NotEqual);
2994
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002995 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002996 return kCondNE;
2997 }
2998
Mark Mendellc4701932015-04-10 13:18:51 -04002999 IfCondition GetOppositeCondition() const OVERRIDE {
3000 return kCondEQ;
3001 }
3002
Dave Allison20dfc792014-06-16 20:44:29 -07003003 private:
Aart Bike9f37602015-10-09 11:15:55 -07003004 template <typename T> bool Compute(T x, T y) const { return x != y; }
3005
Dave Allison20dfc792014-06-16 20:44:29 -07003006 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3007};
3008
3009class HLessThan : public HCondition {
3010 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003011 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3012 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003013
Roland Levillain9867bc72015-08-05 10:21:34 +01003014 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003016 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003017 // In the following Evaluate methods, a HCompare instruction has
3018 // been merged into this HLessThan instruction; evaluate it as
3019 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3022 }
3023 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3024 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3025 }
3026 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3027 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003028 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003029
Dave Allison20dfc792014-06-16 20:44:29 -07003030 DECLARE_INSTRUCTION(LessThan);
3031
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003032 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003033 return kCondLT;
3034 }
3035
Mark Mendellc4701932015-04-10 13:18:51 -04003036 IfCondition GetOppositeCondition() const OVERRIDE {
3037 return kCondGE;
3038 }
3039
Dave Allison20dfc792014-06-16 20:44:29 -07003040 private:
Aart Bike9f37602015-10-09 11:15:55 -07003041 template <typename T> bool Compute(T x, T y) const { return x < y; }
3042
Dave Allison20dfc792014-06-16 20:44:29 -07003043 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3044};
3045
3046class HLessThanOrEqual : public HCondition {
3047 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003048 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3049 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003050
Roland Levillain9867bc72015-08-05 10:21:34 +01003051 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003052 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003053 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003054 // In the following Evaluate methods, a HCompare instruction has
3055 // been merged into this HLessThanOrEqual instruction; evaluate it as
3056 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003057 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3059 }
3060 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3061 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3062 }
3063 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3064 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003065 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003066
Dave Allison20dfc792014-06-16 20:44:29 -07003067 DECLARE_INSTRUCTION(LessThanOrEqual);
3068
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003069 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003070 return kCondLE;
3071 }
3072
Mark Mendellc4701932015-04-10 13:18:51 -04003073 IfCondition GetOppositeCondition() const OVERRIDE {
3074 return kCondGT;
3075 }
3076
Dave Allison20dfc792014-06-16 20:44:29 -07003077 private:
Aart Bike9f37602015-10-09 11:15:55 -07003078 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3079
Dave Allison20dfc792014-06-16 20:44:29 -07003080 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3081};
3082
3083class HGreaterThan : public HCondition {
3084 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003085 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3086 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003087
Roland Levillain9867bc72015-08-05 10:21:34 +01003088 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003090 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003091 // In the following Evaluate methods, a HCompare instruction has
3092 // been merged into this HGreaterThan instruction; evaluate it as
3093 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003094 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003095 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3096 }
3097 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3098 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3099 }
3100 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3101 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003102 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003103
Dave Allison20dfc792014-06-16 20:44:29 -07003104 DECLARE_INSTRUCTION(GreaterThan);
3105
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003106 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003107 return kCondGT;
3108 }
3109
Mark Mendellc4701932015-04-10 13:18:51 -04003110 IfCondition GetOppositeCondition() const OVERRIDE {
3111 return kCondLE;
3112 }
3113
Dave Allison20dfc792014-06-16 20:44:29 -07003114 private:
Aart Bike9f37602015-10-09 11:15:55 -07003115 template <typename T> bool Compute(T x, T y) const { return x > y; }
3116
Dave Allison20dfc792014-06-16 20:44:29 -07003117 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3118};
3119
3120class HGreaterThanOrEqual : public HCondition {
3121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003122 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3123 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003124
Roland Levillain9867bc72015-08-05 10:21:34 +01003125 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003126 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003127 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003128 // In the following Evaluate methods, a HCompare instruction has
3129 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3130 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003131 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003132 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3133 }
3134 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3135 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3136 }
3137 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3138 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003139 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003140
Dave Allison20dfc792014-06-16 20:44:29 -07003141 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3142
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003143 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003144 return kCondGE;
3145 }
3146
Mark Mendellc4701932015-04-10 13:18:51 -04003147 IfCondition GetOppositeCondition() const OVERRIDE {
3148 return kCondLT;
3149 }
3150
Dave Allison20dfc792014-06-16 20:44:29 -07003151 private:
Aart Bike9f37602015-10-09 11:15:55 -07003152 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3153
Dave Allison20dfc792014-06-16 20:44:29 -07003154 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3155};
3156
Aart Bike9f37602015-10-09 11:15:55 -07003157class HBelow : public HCondition {
3158 public:
3159 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3160 : HCondition(first, second, dex_pc) {}
3161
3162 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003163 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003164 }
3165 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003166 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3167 }
3168 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3169 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3170 LOG(FATAL) << DebugName() << " is not defined for float values";
3171 UNREACHABLE();
3172 }
3173 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3174 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3175 LOG(FATAL) << DebugName() << " is not defined for double values";
3176 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003177 }
3178
3179 DECLARE_INSTRUCTION(Below);
3180
3181 IfCondition GetCondition() const OVERRIDE {
3182 return kCondB;
3183 }
3184
3185 IfCondition GetOppositeCondition() const OVERRIDE {
3186 return kCondAE;
3187 }
3188
3189 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003190 template <typename T> bool Compute(T x, T y) const {
3191 return MakeUnsigned(x) < MakeUnsigned(y);
3192 }
Aart Bike9f37602015-10-09 11:15:55 -07003193
3194 DISALLOW_COPY_AND_ASSIGN(HBelow);
3195};
3196
3197class HBelowOrEqual : public HCondition {
3198 public:
3199 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3200 : HCondition(first, second, dex_pc) {}
3201
3202 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003204 }
3205 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003206 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3207 }
3208 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3209 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3210 LOG(FATAL) << DebugName() << " is not defined for float values";
3211 UNREACHABLE();
3212 }
3213 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3214 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3215 LOG(FATAL) << DebugName() << " is not defined for double values";
3216 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003217 }
3218
3219 DECLARE_INSTRUCTION(BelowOrEqual);
3220
3221 IfCondition GetCondition() const OVERRIDE {
3222 return kCondBE;
3223 }
3224
3225 IfCondition GetOppositeCondition() const OVERRIDE {
3226 return kCondA;
3227 }
3228
3229 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003230 template <typename T> bool Compute(T x, T y) const {
3231 return MakeUnsigned(x) <= MakeUnsigned(y);
3232 }
Aart Bike9f37602015-10-09 11:15:55 -07003233
3234 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3235};
3236
3237class HAbove : public HCondition {
3238 public:
3239 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3240 : HCondition(first, second, dex_pc) {}
3241
3242 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003243 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003244 }
3245 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003246 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3247 }
3248 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3249 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3250 LOG(FATAL) << DebugName() << " is not defined for float values";
3251 UNREACHABLE();
3252 }
3253 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3254 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3255 LOG(FATAL) << DebugName() << " is not defined for double values";
3256 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003257 }
3258
3259 DECLARE_INSTRUCTION(Above);
3260
3261 IfCondition GetCondition() const OVERRIDE {
3262 return kCondA;
3263 }
3264
3265 IfCondition GetOppositeCondition() const OVERRIDE {
3266 return kCondBE;
3267 }
3268
3269 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003270 template <typename T> bool Compute(T x, T y) const {
3271 return MakeUnsigned(x) > MakeUnsigned(y);
3272 }
Aart Bike9f37602015-10-09 11:15:55 -07003273
3274 DISALLOW_COPY_AND_ASSIGN(HAbove);
3275};
3276
3277class HAboveOrEqual : public HCondition {
3278 public:
3279 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3280 : HCondition(first, second, dex_pc) {}
3281
3282 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003283 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003284 }
3285 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003286 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3287 }
3288 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3289 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3290 LOG(FATAL) << DebugName() << " is not defined for float values";
3291 UNREACHABLE();
3292 }
3293 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3294 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3295 LOG(FATAL) << DebugName() << " is not defined for double values";
3296 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003297 }
3298
3299 DECLARE_INSTRUCTION(AboveOrEqual);
3300
3301 IfCondition GetCondition() const OVERRIDE {
3302 return kCondAE;
3303 }
3304
3305 IfCondition GetOppositeCondition() const OVERRIDE {
3306 return kCondB;
3307 }
3308
3309 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 template <typename T> bool Compute(T x, T y) const {
3311 return MakeUnsigned(x) >= MakeUnsigned(y);
3312 }
Aart Bike9f37602015-10-09 11:15:55 -07003313
3314 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3315};
Dave Allison20dfc792014-06-16 20:44:29 -07003316
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003317// Instruction to check how two inputs compare to each other.
3318// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3319class HCompare : public HBinaryOperation {
3320 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003321 HCompare(Primitive::Type type,
3322 HInstruction* first,
3323 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003324 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003325 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003326 : HBinaryOperation(Primitive::kPrimInt,
3327 first,
3328 second,
3329 SideEffectsForArchRuntimeCalls(type),
3330 dex_pc),
3331 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003332 DCHECK_EQ(type, first->GetType());
3333 DCHECK_EQ(type, second->GetType());
3334 }
3335
Roland Levillain9867bc72015-08-05 10:21:34 +01003336 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003337 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3338
3339 template <typename T>
3340 int32_t ComputeFP(T x, T y) const {
3341 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3342 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3343 // Handle the bias.
3344 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3345 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003346
Roland Levillain9867bc72015-08-05 10:21:34 +01003347 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003348 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3349 // reach this code path when processing a freshly built HIR
3350 // graph. However HCompare integer instructions can be synthesized
3351 // by the instruction simplifier to implement IntegerCompare and
3352 // IntegerSignum intrinsics, so we have to handle this case.
3353 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003354 }
3355 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3357 }
3358 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3359 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3360 }
3361 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3362 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003363 }
3364
Calin Juravleddb7df22014-11-25 20:56:51 +00003365 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3366 return bias_ == other->AsCompare()->bias_;
3367 }
3368
Mark Mendellc4701932015-04-10 13:18:51 -04003369 ComparisonBias GetBias() const { return bias_; }
3370
Roland Levillain31dd3d62016-02-16 12:21:02 +00003371 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
3372 // Only meaninfgul for floating-point comparisons.
3373 bool IsGtBias() const {
3374 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3375 return bias_ == ComparisonBias::kGtBias;
3376 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003377
3378 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3379 // MIPS64 uses a runtime call for FP comparisons.
3380 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3381 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003382
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003383 DECLARE_INSTRUCTION(Compare);
3384
Roland Levillain31dd3d62016-02-16 12:21:02 +00003385 protected:
3386 // Return an integer constant containing the result of a comparison evaluated at compile time.
3387 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3388 DCHECK(value == -1 || value == 0 || value == 1) << value;
3389 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3390 }
3391
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003392 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003393 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003394
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003395 DISALLOW_COPY_AND_ASSIGN(HCompare);
3396};
3397
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003398// A local in the graph. Corresponds to a Dex register.
3399class HLocal : public HTemplateInstruction<0> {
3400 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003401 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003402 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003404 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003405
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003406 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003407
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003408 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003409 // The Dex register number.
3410 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003411
3412 DISALLOW_COPY_AND_ASSIGN(HLocal);
3413};
3414
3415// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003416class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003417 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003418 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3419 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003420 SetRawInputAt(0, local);
3421 }
3422
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003423 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3424
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003425 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003426
3427 private:
3428 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3429};
3430
3431// Store a value in a given local. This instruction has two inputs: the value
3432// and the local.
3433class HStoreLocal : public HTemplateInstruction<2> {
3434 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003435 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3436 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003437 SetRawInputAt(0, local);
3438 SetRawInputAt(1, value);
3439 }
3440
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003441 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3442
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003443 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003444
3445 private:
3446 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3447};
3448
David Brazdil6de19382016-01-08 17:37:10 +00003449class HNewInstance : public HExpression<2> {
3450 public:
3451 HNewInstance(HInstruction* cls,
3452 HCurrentMethod* current_method,
3453 uint32_t dex_pc,
3454 uint16_t type_index,
3455 const DexFile& dex_file,
3456 bool can_throw,
3457 bool finalizable,
3458 QuickEntrypointEnum entrypoint)
3459 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3460 type_index_(type_index),
3461 dex_file_(dex_file),
3462 can_throw_(can_throw),
3463 finalizable_(finalizable),
3464 entrypoint_(entrypoint) {
3465 SetRawInputAt(0, cls);
3466 SetRawInputAt(1, current_method);
3467 }
3468
3469 uint16_t GetTypeIndex() const { return type_index_; }
3470 const DexFile& GetDexFile() const { return dex_file_; }
3471
3472 // Calls runtime so needs an environment.
3473 bool NeedsEnvironment() const OVERRIDE { return true; }
3474
3475 // It may throw when called on type that's not instantiable/accessible.
3476 // It can throw OOME.
3477 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3478 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3479
3480 bool IsFinalizable() const { return finalizable_; }
3481
3482 bool CanBeNull() const OVERRIDE { return false; }
3483
3484 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3485
3486 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3487 entrypoint_ = entrypoint;
3488 }
3489
3490 bool IsStringAlloc() const;
3491
3492 DECLARE_INSTRUCTION(NewInstance);
3493
3494 private:
3495 const uint16_t type_index_;
3496 const DexFile& dex_file_;
3497 const bool can_throw_;
3498 const bool finalizable_;
3499 QuickEntrypointEnum entrypoint_;
3500
3501 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3502};
3503
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003504enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003505#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3506 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003507#include "intrinsics_list.h"
3508 kNone,
3509 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3510#undef INTRINSICS_LIST
3511#undef OPTIMIZING_INTRINSICS
3512};
3513std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3514
Agi Csaki05f20562015-08-19 14:58:14 -07003515enum IntrinsicNeedsEnvironmentOrCache {
3516 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3517 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003518};
3519
Aart Bik5d75afe2015-12-14 11:57:01 -08003520enum IntrinsicSideEffects {
3521 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3522 kReadSideEffects, // Intrinsic may read heap memory.
3523 kWriteSideEffects, // Intrinsic may write heap memory.
3524 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3525};
3526
3527enum IntrinsicExceptions {
3528 kNoThrow, // Intrinsic does not throw any exceptions.
3529 kCanThrow // Intrinsic may throw exceptions.
3530};
3531
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003532class HInvoke : public HInstruction {
3533 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003534 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003535
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003536 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003537
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003538 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003539 SetRawInputAt(index, argument);
3540 }
3541
Roland Levillain3e3d7332015-04-28 11:00:54 +01003542 // Return the number of arguments. This number can be lower than
3543 // the number of inputs returned by InputCount(), as some invoke
3544 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3545 // inputs at the end of their list of inputs.
3546 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003548 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003549
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003550 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003551 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003552
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003553 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3554
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003555 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003556 return intrinsic_;
3557 }
3558
Aart Bik5d75afe2015-12-14 11:57:01 -08003559 void SetIntrinsic(Intrinsics intrinsic,
3560 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3561 IntrinsicSideEffects side_effects,
3562 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003563
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003564 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003565 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003566 }
3567
Aart Bik5d75afe2015-12-14 11:57:01 -08003568 bool CanThrow() const OVERRIDE { return can_throw_; }
3569
3570 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3571
3572 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3573 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3574 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003575
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003576 uint32_t* GetIntrinsicOptimizations() {
3577 return &intrinsic_optimizations_;
3578 }
3579
3580 const uint32_t* GetIntrinsicOptimizations() const {
3581 return &intrinsic_optimizations_;
3582 }
3583
3584 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3585
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003586 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003587
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003588 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003589 HInvoke(ArenaAllocator* arena,
3590 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003591 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003592 Primitive::Type return_type,
3593 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003594 uint32_t dex_method_index,
3595 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003596 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003597 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003598 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003599 inputs_(number_of_arguments + number_of_other_inputs,
3600 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003601 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003602 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003603 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003604 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003605 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003606 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003607 }
3608
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003609 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003610 return inputs_[index];
3611 }
3612
David Brazdil1abb4192015-02-17 18:33:36 +00003613 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003614 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003615 }
3616
Aart Bik5d75afe2015-12-14 11:57:01 -08003617 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3618
Roland Levillain3e3d7332015-04-28 11:00:54 +01003619 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003620 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003621 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003622 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003623 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003624 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003625 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003626
3627 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3628 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003629
3630 private:
3631 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3632};
3633
Calin Juravle175dc732015-08-25 15:42:32 +01003634class HInvokeUnresolved : public HInvoke {
3635 public:
3636 HInvokeUnresolved(ArenaAllocator* arena,
3637 uint32_t number_of_arguments,
3638 Primitive::Type return_type,
3639 uint32_t dex_pc,
3640 uint32_t dex_method_index,
3641 InvokeType invoke_type)
3642 : HInvoke(arena,
3643 number_of_arguments,
3644 0u /* number_of_other_inputs */,
3645 return_type,
3646 dex_pc,
3647 dex_method_index,
3648 invoke_type) {
3649 }
3650
3651 DECLARE_INSTRUCTION(InvokeUnresolved);
3652
3653 private:
3654 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3655};
3656
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003657class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003658 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003659 // Requirements of this method call regarding the class
3660 // initialization (clinit) check of its declaring class.
3661 enum class ClinitCheckRequirement {
3662 kNone, // Class already initialized.
3663 kExplicit, // Static call having explicit clinit check as last input.
3664 kImplicit, // Static call implicitly requiring a clinit check.
3665 };
3666
Vladimir Marko58155012015-08-19 12:49:41 +00003667 // Determines how to load the target ArtMethod*.
3668 enum class MethodLoadKind {
3669 // Use a String init ArtMethod* loaded from Thread entrypoints.
3670 kStringInit,
3671
3672 // Use the method's own ArtMethod* loaded by the register allocator.
3673 kRecursive,
3674
3675 // Use ArtMethod* at a known address, embed the direct address in the code.
3676 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3677 kDirectAddress,
3678
3679 // Use ArtMethod* at an address that will be known at link time, embed the direct
3680 // address in the code. If the image is relocatable, emit .patch_oat entry.
3681 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3682 // the image relocatable or not.
3683 kDirectAddressWithFixup,
3684
3685 // Load from resoved methods array in the dex cache using a PC-relative load.
3686 // Used when we need to use the dex cache, for example for invoke-static that
3687 // may cause class initialization (the entry may point to a resolution method),
3688 // and we know that we can access the dex cache arrays using a PC-relative load.
3689 kDexCachePcRelative,
3690
3691 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3692 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3693 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3694 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3695 kDexCacheViaMethod,
3696 };
3697
3698 // Determines the location of the code pointer.
3699 enum class CodePtrLocation {
3700 // Recursive call, use local PC-relative call instruction.
3701 kCallSelf,
3702
3703 // Use PC-relative call instruction patched at link time.
3704 // Used for calls within an oat file, boot->boot or app->app.
3705 kCallPCRelative,
3706
3707 // Call to a known target address, embed the direct address in code.
3708 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3709 kCallDirect,
3710
3711 // Call to a target address that will be known at link time, embed the direct
3712 // address in code. If the image is relocatable, emit .patch_oat entry.
3713 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3714 // the image relocatable or not.
3715 kCallDirectWithFixup,
3716
3717 // Use code pointer from the ArtMethod*.
3718 // Used when we don't know the target code. This is also the last-resort-kind used when
3719 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3720 kCallArtMethod,
3721 };
3722
3723 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003724 MethodLoadKind method_load_kind;
3725 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003726 // The method load data holds
3727 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3728 // Note that there are multiple string init methods, each having its own offset.
3729 // - the method address for kDirectAddress
3730 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003731 uint64_t method_load_data;
3732 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003733 };
3734
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003735 HInvokeStaticOrDirect(ArenaAllocator* arena,
3736 uint32_t number_of_arguments,
3737 Primitive::Type return_type,
3738 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003739 uint32_t method_index,
3740 MethodReference target_method,
3741 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003742 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003743 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003744 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003745 : HInvoke(arena,
3746 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003747 // There is potentially one extra argument for the HCurrentMethod node, and
3748 // potentially one other if the clinit check is explicit, and potentially
3749 // one other if the method is a string factory.
3750 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3751 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3752 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003753 return_type,
3754 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003755 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003756 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003757 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003758 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003759 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003760 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003761
Vladimir Markodc151b22015-10-15 18:02:30 +01003762 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003763 bool had_current_method_input = HasCurrentMethodInput();
3764 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3765
3766 // Using the current method is the default and once we find a better
3767 // method load kind, we should not go back to using the current method.
3768 DCHECK(had_current_method_input || !needs_current_method_input);
3769
3770 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003771 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3772 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003773 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003774 dispatch_info_ = dispatch_info;
3775 }
3776
Vladimir Markoc53c0792015-11-19 15:48:33 +00003777 void AddSpecialInput(HInstruction* input) {
3778 // We allow only one special input.
3779 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3780 DCHECK(InputCount() == GetSpecialInputIndex() ||
3781 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3782 InsertInputAt(GetSpecialInputIndex(), input);
3783 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003784
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003785 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003786 // We access the method via the dex cache so we can't do an implicit null check.
3787 // TODO: for intrinsics we can generate implicit null checks.
3788 return false;
3789 }
3790
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003791 bool CanBeNull() const OVERRIDE {
3792 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3793 }
3794
Vladimir Markoc53c0792015-11-19 15:48:33 +00003795 // Get the index of the special input, if any.
3796 //
David Brazdil6de19382016-01-08 17:37:10 +00003797 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3798 // method pointer; otherwise there may be one platform-specific special input,
3799 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003800 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003801 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003802
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003803 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3804 void SetOptimizedInvokeType(InvokeType invoke_type) {
3805 optimized_invoke_type_ = invoke_type;
3806 }
3807
Vladimir Marko58155012015-08-19 12:49:41 +00003808 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3809 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3810 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003811 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003812 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003813 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003814 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003815 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3816 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003817 bool HasCurrentMethodInput() const {
3818 // This function can be called only after the invoke has been fully initialized by the builder.
3819 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003820 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003821 return true;
3822 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003823 DCHECK(InputCount() == GetSpecialInputIndex() ||
3824 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003825 return false;
3826 }
3827 }
Vladimir Marko58155012015-08-19 12:49:41 +00003828 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3829 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003830 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003831
3832 int32_t GetStringInitOffset() const {
3833 DCHECK(IsStringInit());
3834 return dispatch_info_.method_load_data;
3835 }
3836
3837 uint64_t GetMethodAddress() const {
3838 DCHECK(HasMethodAddress());
3839 return dispatch_info_.method_load_data;
3840 }
3841
3842 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003843 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003844 return dispatch_info_.method_load_data;
3845 }
3846
3847 uint64_t GetDirectCodePtr() const {
3848 DCHECK(HasDirectCodePtr());
3849 return dispatch_info_.direct_code_ptr;
3850 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003851
Calin Juravle68ad6492015-08-18 17:08:12 +01003852 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3853
Roland Levillain4c0eb422015-04-24 16:43:49 +01003854 // Is this instruction a call to a static method?
3855 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003856 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003857 }
3858
Vladimir Markofbb184a2015-11-13 14:47:00 +00003859 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3860 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3861 // instruction; only relevant for static calls with explicit clinit check.
3862 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003863 DCHECK(IsStaticWithExplicitClinitCheck());
3864 size_t last_input_index = InputCount() - 1;
3865 HInstruction* last_input = InputAt(last_input_index);
3866 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003867 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003868 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003869 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003870 clinit_check_requirement_ = new_requirement;
3871 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003872 }
3873
David Brazdilbc9ab162016-01-20 14:50:19 +00003874 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003875 DCHECK(IsStringInit());
3876 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003877 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003878 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003879 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003880 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003881 }
3882
Roland Levillain4c0eb422015-04-24 16:43:49 +01003883 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003884 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003885 bool IsStaticWithExplicitClinitCheck() const {
3886 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3887 }
3888
3889 // Is this a call to a static method whose declaring class has an
3890 // implicit intialization check requirement?
3891 bool IsStaticWithImplicitClinitCheck() const {
3892 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3893 }
3894
Vladimir Markob554b5a2015-11-06 12:57:55 +00003895 // Does this method load kind need the current method as an input?
3896 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3897 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3898 }
3899
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003900 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003901
Roland Levillain4c0eb422015-04-24 16:43:49 +01003902 protected:
3903 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3904 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3905 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3906 HInstruction* input = input_record.GetInstruction();
3907 // `input` is the last input of a static invoke marked as having
3908 // an explicit clinit check. It must either be:
3909 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3910 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3911 DCHECK(input != nullptr);
3912 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3913 }
3914 return input_record;
3915 }
3916
Vladimir Markoc53c0792015-11-19 15:48:33 +00003917 void InsertInputAt(size_t index, HInstruction* input);
3918 void RemoveInputAt(size_t index);
3919
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003920 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003921 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003922 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003923 // The target method may refer to different dex file or method index than the original
3924 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3925 // in derived class to increase visibility.
3926 MethodReference target_method_;
3927 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003928
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003929 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003930};
Vladimir Markof64242a2015-12-01 14:58:23 +00003931std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003932std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003933
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003934class HInvokeVirtual : public HInvoke {
3935 public:
3936 HInvokeVirtual(ArenaAllocator* arena,
3937 uint32_t number_of_arguments,
3938 Primitive::Type return_type,
3939 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003940 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003941 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003942 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003943 vtable_index_(vtable_index) {}
3944
Calin Juravle641547a2015-04-21 22:08:51 +01003945 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003946 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003947 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003948 }
3949
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003950 uint32_t GetVTableIndex() const { return vtable_index_; }
3951
3952 DECLARE_INSTRUCTION(InvokeVirtual);
3953
3954 private:
3955 const uint32_t vtable_index_;
3956
3957 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3958};
3959
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003960class HInvokeInterface : public HInvoke {
3961 public:
3962 HInvokeInterface(ArenaAllocator* arena,
3963 uint32_t number_of_arguments,
3964 Primitive::Type return_type,
3965 uint32_t dex_pc,
3966 uint32_t dex_method_index,
3967 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003968 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003969 imt_index_(imt_index) {}
3970
Calin Juravle641547a2015-04-21 22:08:51 +01003971 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003972 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003973 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003974 }
3975
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003976 uint32_t GetImtIndex() const { return imt_index_; }
3977 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3978
3979 DECLARE_INSTRUCTION(InvokeInterface);
3980
3981 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003982 const uint32_t imt_index_;
3983
3984 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3985};
3986
Roland Levillain88cb1752014-10-20 16:36:47 +01003987class HNeg : public HUnaryOperation {
3988 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003989 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3990 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003991
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 template <typename T> T Compute(T x) const { return -x; }
3993
3994 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003995 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003996 }
3997 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003998 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003999 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4001 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4002 }
4003 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4004 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4005 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004006
Roland Levillain88cb1752014-10-20 16:36:47 +01004007 DECLARE_INSTRUCTION(Neg);
4008
4009 private:
4010 DISALLOW_COPY_AND_ASSIGN(HNeg);
4011};
4012
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004013class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004014 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004015 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004016 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004017 uint32_t dex_pc,
4018 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004019 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004020 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004021 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004022 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004023 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004024 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004025 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004026 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004027 }
4028
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004029 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004030 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004031
4032 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004033 bool NeedsEnvironment() const OVERRIDE { return true; }
4034
Mingyao Yang0c365e62015-03-31 15:09:29 -07004035 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4036 bool CanThrow() const OVERRIDE { return true; }
4037
Calin Juravle10e244f2015-01-26 18:54:32 +00004038 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004039
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004040 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4041
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004042 DECLARE_INSTRUCTION(NewArray);
4043
4044 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004045 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004046 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004047 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004048
4049 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4050};
4051
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004052class HAdd : public HBinaryOperation {
4053 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004054 HAdd(Primitive::Type result_type,
4055 HInstruction* left,
4056 HInstruction* right,
4057 uint32_t dex_pc = kNoDexPc)
4058 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004059
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004060 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004061
Roland Levillain9867bc72015-08-05 10:21:34 +01004062 template <typename T> T Compute(T x, T y) const { return x + y; }
4063
4064 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 return GetBlock()->GetGraph()->GetIntConstant(
4066 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004067 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004068 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004069 return GetBlock()->GetGraph()->GetLongConstant(
4070 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004071 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004072 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4073 return GetBlock()->GetGraph()->GetFloatConstant(
4074 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4075 }
4076 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4077 return GetBlock()->GetGraph()->GetDoubleConstant(
4078 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4079 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004080
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004081 DECLARE_INSTRUCTION(Add);
4082
4083 private:
4084 DISALLOW_COPY_AND_ASSIGN(HAdd);
4085};
4086
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004087class HSub : public HBinaryOperation {
4088 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004089 HSub(Primitive::Type result_type,
4090 HInstruction* left,
4091 HInstruction* right,
4092 uint32_t dex_pc = kNoDexPc)
4093 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004094
Roland Levillain9867bc72015-08-05 10:21:34 +01004095 template <typename T> T Compute(T x, T y) const { return x - y; }
4096
4097 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004098 return GetBlock()->GetGraph()->GetIntConstant(
4099 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004100 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004101 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 return GetBlock()->GetGraph()->GetLongConstant(
4103 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004104 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004105 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4106 return GetBlock()->GetGraph()->GetFloatConstant(
4107 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4108 }
4109 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4110 return GetBlock()->GetGraph()->GetDoubleConstant(
4111 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4112 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004113
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004114 DECLARE_INSTRUCTION(Sub);
4115
4116 private:
4117 DISALLOW_COPY_AND_ASSIGN(HSub);
4118};
4119
Calin Juravle34bacdf2014-10-07 20:23:36 +01004120class HMul : public HBinaryOperation {
4121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 HMul(Primitive::Type result_type,
4123 HInstruction* left,
4124 HInstruction* right,
4125 uint32_t dex_pc = kNoDexPc)
4126 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004128 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004129
Roland Levillain9867bc72015-08-05 10:21:34 +01004130 template <typename T> T Compute(T x, T y) const { return x * y; }
4131
4132 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 return GetBlock()->GetGraph()->GetIntConstant(
4134 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004135 }
4136 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 return GetBlock()->GetGraph()->GetLongConstant(
4138 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004139 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004140 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4141 return GetBlock()->GetGraph()->GetFloatConstant(
4142 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4143 }
4144 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4145 return GetBlock()->GetGraph()->GetDoubleConstant(
4146 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4147 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004148
4149 DECLARE_INSTRUCTION(Mul);
4150
4151 private:
4152 DISALLOW_COPY_AND_ASSIGN(HMul);
4153};
4154
Calin Juravle7c4954d2014-10-28 16:57:40 +00004155class HDiv : public HBinaryOperation {
4156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 HDiv(Primitive::Type result_type,
4158 HInstruction* left,
4159 HInstruction* right,
4160 uint32_t dex_pc)
4161 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004162
Roland Levillain9867bc72015-08-05 10:21:34 +01004163 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004164 T ComputeIntegral(T x, T y) const {
4165 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 // Our graph structure ensures we never have 0 for `y` during
4167 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004168 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004169 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004170 return (y == -1) ? -x : x / y;
4171 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004172
Roland Levillain31dd3d62016-02-16 12:21:02 +00004173 template <typename T>
4174 T ComputeFP(T x, T y) const {
4175 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4176 return x / y;
4177 }
4178
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004180 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004181 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004182 }
4183 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004185 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4186 }
4187 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4188 return GetBlock()->GetGraph()->GetFloatConstant(
4189 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4190 }
4191 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4192 return GetBlock()->GetGraph()->GetDoubleConstant(
4193 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004194 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004195
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004196 static SideEffects SideEffectsForArchRuntimeCalls() {
4197 // The generated code can use a runtime call.
4198 return SideEffects::CanTriggerGC();
4199 }
4200
Calin Juravle7c4954d2014-10-28 16:57:40 +00004201 DECLARE_INSTRUCTION(Div);
4202
4203 private:
4204 DISALLOW_COPY_AND_ASSIGN(HDiv);
4205};
4206
Calin Juravlebacfec32014-11-14 15:54:36 +00004207class HRem : public HBinaryOperation {
4208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004209 HRem(Primitive::Type result_type,
4210 HInstruction* left,
4211 HInstruction* right,
4212 uint32_t dex_pc)
4213 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004214
Roland Levillain9867bc72015-08-05 10:21:34 +01004215 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004216 T ComputeIntegral(T x, T y) const {
4217 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 // Our graph structure ensures we never have 0 for `y` during
4219 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004220 DCHECK_NE(y, 0);
4221 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4222 return (y == -1) ? 0 : x % y;
4223 }
4224
Roland Levillain31dd3d62016-02-16 12:21:02 +00004225 template <typename T>
4226 T ComputeFP(T x, T y) const {
4227 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4228 return std::fmod(x, y);
4229 }
4230
Roland Levillain9867bc72015-08-05 10:21:34 +01004231 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004233 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004234 }
4235 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004236 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004237 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004238 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004239 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4240 return GetBlock()->GetGraph()->GetFloatConstant(
4241 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4242 }
4243 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4244 return GetBlock()->GetGraph()->GetDoubleConstant(
4245 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4246 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004247
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004248 static SideEffects SideEffectsForArchRuntimeCalls() {
4249 return SideEffects::CanTriggerGC();
4250 }
4251
Calin Juravlebacfec32014-11-14 15:54:36 +00004252 DECLARE_INSTRUCTION(Rem);
4253
4254 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004255 DISALLOW_COPY_AND_ASSIGN(HRem);
4256};
4257
Calin Juravled0d48522014-11-04 16:40:20 +00004258class HDivZeroCheck : public HExpression<1> {
4259 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004260 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4261 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004262 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004263 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004264 SetRawInputAt(0, value);
4265 }
4266
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004267 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4268
Calin Juravled0d48522014-11-04 16:40:20 +00004269 bool CanBeMoved() const OVERRIDE { return true; }
4270
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004271 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004272 return true;
4273 }
4274
4275 bool NeedsEnvironment() const OVERRIDE { return true; }
4276 bool CanThrow() const OVERRIDE { return true; }
4277
Calin Juravled0d48522014-11-04 16:40:20 +00004278 DECLARE_INSTRUCTION(DivZeroCheck);
4279
4280 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004281 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4282};
4283
Calin Juravle9aec02f2014-11-18 23:06:35 +00004284class HShl : public HBinaryOperation {
4285 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004286 HShl(Primitive::Type result_type,
4287 HInstruction* left,
4288 HInstruction* right,
4289 uint32_t dex_pc = kNoDexPc)
4290 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004291
Roland Levillain9867bc72015-08-05 10:21:34 +01004292 template <typename T, typename U, typename V>
4293 T Compute(T x, U y, V max_shift_value) const {
4294 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4295 "V is not the unsigned integer type corresponding to T");
4296 return x << (y & max_shift_value);
4297 }
4298
4299 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4300 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004301 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004302 }
4303 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4304 // case is handled as `x << static_cast<int>(y)`.
4305 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4306 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004308 }
4309 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4310 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004312 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004313 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4314 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4315 LOG(FATAL) << DebugName() << " is not defined for float values";
4316 UNREACHABLE();
4317 }
4318 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4319 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4320 LOG(FATAL) << DebugName() << " is not defined for double values";
4321 UNREACHABLE();
4322 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004323
4324 DECLARE_INSTRUCTION(Shl);
4325
4326 private:
4327 DISALLOW_COPY_AND_ASSIGN(HShl);
4328};
4329
4330class HShr : public HBinaryOperation {
4331 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004332 HShr(Primitive::Type result_type,
4333 HInstruction* left,
4334 HInstruction* right,
4335 uint32_t dex_pc = kNoDexPc)
4336 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004337
Roland Levillain9867bc72015-08-05 10:21:34 +01004338 template <typename T, typename U, typename V>
4339 T Compute(T x, U y, V max_shift_value) const {
4340 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4341 "V is not the unsigned integer type corresponding to T");
4342 return x >> (y & max_shift_value);
4343 }
4344
4345 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4346 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004348 }
4349 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4350 // case is handled as `x >> static_cast<int>(y)`.
4351 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4352 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004354 }
4355 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4356 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004357 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004358 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004359 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4360 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4361 LOG(FATAL) << DebugName() << " is not defined for float values";
4362 UNREACHABLE();
4363 }
4364 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4365 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4366 LOG(FATAL) << DebugName() << " is not defined for double values";
4367 UNREACHABLE();
4368 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004369
4370 DECLARE_INSTRUCTION(Shr);
4371
4372 private:
4373 DISALLOW_COPY_AND_ASSIGN(HShr);
4374};
4375
4376class HUShr : public HBinaryOperation {
4377 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 HUShr(Primitive::Type result_type,
4379 HInstruction* left,
4380 HInstruction* right,
4381 uint32_t dex_pc = kNoDexPc)
4382 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004383
Roland Levillain9867bc72015-08-05 10:21:34 +01004384 template <typename T, typename U, typename V>
4385 T Compute(T x, U y, V max_shift_value) const {
4386 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4387 "V is not the unsigned integer type corresponding to T");
4388 V ux = static_cast<V>(x);
4389 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004390 }
4391
Roland Levillain9867bc72015-08-05 10:21:34 +01004392 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4393 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004394 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004395 }
4396 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4397 // case is handled as `x >>> static_cast<int>(y)`.
4398 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4399 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 }
4402 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4403 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004404 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004405 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4407 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4408 LOG(FATAL) << DebugName() << " is not defined for float values";
4409 UNREACHABLE();
4410 }
4411 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4412 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4413 LOG(FATAL) << DebugName() << " is not defined for double values";
4414 UNREACHABLE();
4415 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004416
4417 DECLARE_INSTRUCTION(UShr);
4418
4419 private:
4420 DISALLOW_COPY_AND_ASSIGN(HUShr);
4421};
4422
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004423class HAnd : public HBinaryOperation {
4424 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 HAnd(Primitive::Type result_type,
4426 HInstruction* left,
4427 HInstruction* right,
4428 uint32_t dex_pc = kNoDexPc)
4429 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004430
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004431 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004432
Roland Levillain9867bc72015-08-05 10:21:34 +01004433 template <typename T, typename U>
4434 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4435
4436 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004437 return GetBlock()->GetGraph()->GetIntConstant(
4438 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004439 }
4440 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 return GetBlock()->GetGraph()->GetLongConstant(
4442 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 }
4444 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004445 return GetBlock()->GetGraph()->GetLongConstant(
4446 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004447 }
4448 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 return GetBlock()->GetGraph()->GetLongConstant(
4450 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004451 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004452 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4453 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4454 LOG(FATAL) << DebugName() << " is not defined for float values";
4455 UNREACHABLE();
4456 }
4457 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4458 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4459 LOG(FATAL) << DebugName() << " is not defined for double values";
4460 UNREACHABLE();
4461 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004462
4463 DECLARE_INSTRUCTION(And);
4464
4465 private:
4466 DISALLOW_COPY_AND_ASSIGN(HAnd);
4467};
4468
4469class HOr : public HBinaryOperation {
4470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004471 HOr(Primitive::Type result_type,
4472 HInstruction* left,
4473 HInstruction* right,
4474 uint32_t dex_pc = kNoDexPc)
4475 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004476
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004477 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004478
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 template <typename T, typename U>
4480 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4481
4482 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004483 return GetBlock()->GetGraph()->GetIntConstant(
4484 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004485 }
4486 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004487 return GetBlock()->GetGraph()->GetLongConstant(
4488 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004489 }
4490 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 return GetBlock()->GetGraph()->GetLongConstant(
4492 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004493 }
4494 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 return GetBlock()->GetGraph()->GetLongConstant(
4496 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004497 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004498 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4499 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4500 LOG(FATAL) << DebugName() << " is not defined for float values";
4501 UNREACHABLE();
4502 }
4503 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4504 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4505 LOG(FATAL) << DebugName() << " is not defined for double values";
4506 UNREACHABLE();
4507 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004508
4509 DECLARE_INSTRUCTION(Or);
4510
4511 private:
4512 DISALLOW_COPY_AND_ASSIGN(HOr);
4513};
4514
4515class HXor : public HBinaryOperation {
4516 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004517 HXor(Primitive::Type result_type,
4518 HInstruction* left,
4519 HInstruction* right,
4520 uint32_t dex_pc = kNoDexPc)
4521 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004522
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004523 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004524
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 template <typename T, typename U>
4526 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4527
4528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004529 return GetBlock()->GetGraph()->GetIntConstant(
4530 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004531 }
4532 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004533 return GetBlock()->GetGraph()->GetLongConstant(
4534 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004535 }
4536 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004537 return GetBlock()->GetGraph()->GetLongConstant(
4538 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004539 }
4540 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 return GetBlock()->GetGraph()->GetLongConstant(
4542 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004543 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004544 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4545 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4546 LOG(FATAL) << DebugName() << " is not defined for float values";
4547 UNREACHABLE();
4548 }
4549 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4550 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4551 LOG(FATAL) << DebugName() << " is not defined for double values";
4552 UNREACHABLE();
4553 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004554
4555 DECLARE_INSTRUCTION(Xor);
4556
4557 private:
4558 DISALLOW_COPY_AND_ASSIGN(HXor);
4559};
4560
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004561class HRor : public HBinaryOperation {
4562 public:
4563 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4564 : HBinaryOperation(result_type, value, distance) {}
4565
4566 template <typename T, typename U, typename V>
4567 T Compute(T x, U y, V max_shift_value) const {
4568 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4569 "V is not the unsigned integer type corresponding to T");
4570 V ux = static_cast<V>(x);
4571 if ((y & max_shift_value) == 0) {
4572 return static_cast<T>(ux);
4573 } else {
4574 const V reg_bits = sizeof(T) * 8;
4575 return static_cast<T>(ux >> (y & max_shift_value)) |
4576 (x << (reg_bits - (y & max_shift_value)));
4577 }
4578 }
4579
4580 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4581 return GetBlock()->GetGraph()->GetIntConstant(
4582 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4583 }
4584 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4585 return GetBlock()->GetGraph()->GetLongConstant(
4586 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4587 }
4588 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4589 return GetBlock()->GetGraph()->GetLongConstant(
4590 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4591 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004592 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4593 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4594 LOG(FATAL) << DebugName() << " is not defined for float values";
4595 UNREACHABLE();
4596 }
4597 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4598 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4599 LOG(FATAL) << DebugName() << " is not defined for double values";
4600 UNREACHABLE();
4601 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004602
4603 DECLARE_INSTRUCTION(Ror);
4604
4605 private:
4606 DISALLOW_COPY_AND_ASSIGN(HRor);
4607};
4608
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004609// The value of a parameter in this method. Its location depends on
4610// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004611class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004612 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004613 HParameterValue(const DexFile& dex_file,
4614 uint16_t type_index,
4615 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 Primitive::Type parameter_type,
4617 bool is_this = false)
4618 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004619 dex_file_(dex_file),
4620 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004621 index_(index),
4622 is_this_(is_this),
4623 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004624
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004625 const DexFile& GetDexFile() const { return dex_file_; }
4626 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004627 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004628 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004629
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004630 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4631 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004632
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004633 DECLARE_INSTRUCTION(ParameterValue);
4634
4635 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004636 const DexFile& dex_file_;
4637 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004638 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004639 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004640 const uint8_t index_;
4641
Calin Juravle10e244f2015-01-26 18:54:32 +00004642 // Whether or not the parameter value corresponds to 'this' argument.
4643 const bool is_this_;
4644
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004645 bool can_be_null_;
4646
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004647 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4648};
4649
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004650class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4653 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004654
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004655 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004656 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004657 return true;
4658 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004659
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 template <typename T> T Compute(T x) const { return ~x; }
4661
4662 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004664 }
4665 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004666 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004667 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004668 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4669 LOG(FATAL) << DebugName() << " is not defined for float values";
4670 UNREACHABLE();
4671 }
4672 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4673 LOG(FATAL) << DebugName() << " is not defined for double values";
4674 UNREACHABLE();
4675 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004676
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004677 DECLARE_INSTRUCTION(Not);
4678
4679 private:
4680 DISALLOW_COPY_AND_ASSIGN(HNot);
4681};
4682
David Brazdil66d126e2015-04-03 16:02:44 +01004683class HBooleanNot : public HUnaryOperation {
4684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004685 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4686 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004687
4688 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004689 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004690 return true;
4691 }
4692
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004695 return !x;
4696 }
4697
Roland Levillain9867bc72015-08-05 10:21:34 +01004698 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004699 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 }
4701 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4702 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004703 UNREACHABLE();
4704 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004705 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4706 LOG(FATAL) << DebugName() << " is not defined for float values";
4707 UNREACHABLE();
4708 }
4709 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4710 LOG(FATAL) << DebugName() << " is not defined for double values";
4711 UNREACHABLE();
4712 }
David Brazdil66d126e2015-04-03 16:02:44 +01004713
4714 DECLARE_INSTRUCTION(BooleanNot);
4715
4716 private:
4717 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4718};
4719
Roland Levillaindff1f282014-11-05 14:15:05 +00004720class HTypeConversion : public HExpression<1> {
4721 public:
4722 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004723 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004724 : HExpression(result_type,
4725 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4726 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004727 SetRawInputAt(0, input);
4728 DCHECK_NE(input->GetType(), result_type);
4729 }
4730
4731 HInstruction* GetInput() const { return InputAt(0); }
4732 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4733 Primitive::Type GetResultType() const { return GetType(); }
4734
4735 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004736 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004737
Mark Mendelle82549b2015-05-06 10:55:34 -04004738 // Try to statically evaluate the conversion and return a HConstant
4739 // containing the result. If the input cannot be converted, return nullptr.
4740 HConstant* TryStaticEvaluation() const;
4741
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004742 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4743 Primitive::Type result_type) {
4744 // Some architectures may not require the 'GC' side effects, but at this point
4745 // in the compilation process we do not know what architecture we will
4746 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004747 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4748 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004749 return SideEffects::CanTriggerGC();
4750 }
4751 return SideEffects::None();
4752 }
4753
Roland Levillaindff1f282014-11-05 14:15:05 +00004754 DECLARE_INSTRUCTION(TypeConversion);
4755
4756 private:
4757 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4758};
4759
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004760static constexpr uint32_t kNoRegNumber = -1;
4761
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004762class HPhi : public HInstruction {
4763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004764 HPhi(ArenaAllocator* arena,
4765 uint32_t reg_number,
4766 size_t number_of_inputs,
4767 Primitive::Type type,
4768 uint32_t dex_pc = kNoDexPc)
4769 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004770 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004771 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004772 type_(ToPhiType(type)),
4773 // Phis are constructed live and marked dead if conflicting or unused.
4774 // Individual steps of SsaBuilder should assume that if a phi has been
4775 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4776 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004777 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004778 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004779 }
4780
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004781 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4782 static Primitive::Type ToPhiType(Primitive::Type type) {
4783 switch (type) {
4784 case Primitive::kPrimBoolean:
4785 case Primitive::kPrimByte:
4786 case Primitive::kPrimShort:
4787 case Primitive::kPrimChar:
4788 return Primitive::kPrimInt;
4789 default:
4790 return type;
4791 }
4792 }
4793
David Brazdilffee3d32015-07-06 11:48:53 +01004794 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4795
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004796 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004797
4798 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004799 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004800
Calin Juravle10e244f2015-01-26 18:54:32 +00004801 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004802 void SetType(Primitive::Type new_type) {
4803 // Make sure that only valid type changes occur. The following are allowed:
4804 // (1) int -> float/ref (primitive type propagation),
4805 // (2) long -> double (primitive type propagation).
4806 DCHECK(type_ == new_type ||
4807 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4808 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4809 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4810 type_ = new_type;
4811 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004812
Calin Juravle10e244f2015-01-26 18:54:32 +00004813 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4814 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4815
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004816 uint32_t GetRegNumber() const { return reg_number_; }
4817
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004818 void SetDead() { is_live_ = false; }
4819 void SetLive() { is_live_ = true; }
4820 bool IsDead() const { return !is_live_; }
4821 bool IsLive() const { return is_live_; }
4822
David Brazdil77a48ae2015-09-15 12:34:04 +00004823 bool IsVRegEquivalentOf(HInstruction* other) const {
4824 return other != nullptr
4825 && other->IsPhi()
4826 && other->AsPhi()->GetBlock() == GetBlock()
4827 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4828 }
4829
Calin Juravlea4f88312015-04-16 12:57:19 +01004830 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4831 // An equivalent phi is a phi having the same dex register and type.
4832 // It assumes that phis with the same dex register are adjacent.
4833 HPhi* GetNextEquivalentPhiWithSameType() {
4834 HInstruction* next = GetNext();
4835 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4836 if (next->GetType() == GetType()) {
4837 return next->AsPhi();
4838 }
4839 next = next->GetNext();
4840 }
4841 return nullptr;
4842 }
4843
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004844 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004845
David Brazdil1abb4192015-02-17 18:33:36 +00004846 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004847 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004848 return inputs_[index];
4849 }
David Brazdil1abb4192015-02-17 18:33:36 +00004850
4851 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004852 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004853 }
4854
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004855 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004856 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004857 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004858 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004859 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004860 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004861
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004862 DISALLOW_COPY_AND_ASSIGN(HPhi);
4863};
4864
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004865class HNullCheck : public HExpression<1> {
4866 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004867 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4868 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004869 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004870 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004871 SetRawInputAt(0, value);
4872 }
4873
Calin Juravle10e244f2015-01-26 18:54:32 +00004874 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004875 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004876 return true;
4877 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004878
Calin Juravle10e244f2015-01-26 18:54:32 +00004879 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004880
Calin Juravle10e244f2015-01-26 18:54:32 +00004881 bool CanThrow() const OVERRIDE { return true; }
4882
4883 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004884
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004885
4886 DECLARE_INSTRUCTION(NullCheck);
4887
4888 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004889 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4890};
4891
4892class FieldInfo : public ValueObject {
4893 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004894 FieldInfo(MemberOffset field_offset,
4895 Primitive::Type field_type,
4896 bool is_volatile,
4897 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004898 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004899 const DexFile& dex_file,
4900 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004901 : field_offset_(field_offset),
4902 field_type_(field_type),
4903 is_volatile_(is_volatile),
4904 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004905 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004906 dex_file_(dex_file),
4907 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004908
4909 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004910 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004911 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004912 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004913 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004914 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004915 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004916
4917 private:
4918 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004919 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004920 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004921 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004922 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004923 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004924 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004925};
4926
4927class HInstanceFieldGet : public HExpression<1> {
4928 public:
4929 HInstanceFieldGet(HInstruction* value,
4930 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004931 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004932 bool is_volatile,
4933 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004934 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004935 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004936 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004937 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004938 : HExpression(field_type,
4939 SideEffects::FieldReadOfType(field_type, is_volatile),
4940 dex_pc),
4941 field_info_(field_offset,
4942 field_type,
4943 is_volatile,
4944 field_idx,
4945 declaring_class_def_index,
4946 dex_file,
4947 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948 SetRawInputAt(0, value);
4949 }
4950
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004951 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004952
4953 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4954 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4955 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004956 }
4957
Calin Juravle641547a2015-04-21 22:08:51 +01004958 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4959 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004960 }
4961
4962 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004963 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4964 }
4965
Calin Juravle52c48962014-12-16 17:02:57 +00004966 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004967 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004968 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004969 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004970
4971 DECLARE_INSTRUCTION(InstanceFieldGet);
4972
4973 private:
4974 const FieldInfo field_info_;
4975
4976 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4977};
4978
4979class HInstanceFieldSet : public HTemplateInstruction<2> {
4980 public:
4981 HInstanceFieldSet(HInstruction* object,
4982 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004983 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004984 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004985 bool is_volatile,
4986 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004987 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004988 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004989 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004990 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004991 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4992 dex_pc),
4993 field_info_(field_offset,
4994 field_type,
4995 is_volatile,
4996 field_idx,
4997 declaring_class_def_index,
4998 dex_file,
4999 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005000 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001 SetRawInputAt(0, object);
5002 SetRawInputAt(1, value);
5003 }
5004
Calin Juravle641547a2015-04-21 22:08:51 +01005005 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5006 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005007 }
5008
Calin Juravle52c48962014-12-16 17:02:57 +00005009 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005010 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005011 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005012 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005013 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005014 bool GetValueCanBeNull() const { return value_can_be_null_; }
5015 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005016
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017 DECLARE_INSTRUCTION(InstanceFieldSet);
5018
5019 private:
5020 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005021 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005022
5023 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5024};
5025
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026class HArrayGet : public HExpression<2> {
5027 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005028 HArrayGet(HInstruction* array,
5029 HInstruction* index,
5030 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005031 uint32_t dex_pc,
5032 SideEffects additional_side_effects = SideEffects::None())
5033 : HExpression(type,
5034 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005035 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 SetRawInputAt(0, array);
5037 SetRawInputAt(1, index);
5038 }
5039
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005040 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005041 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005042 return true;
5043 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005044 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005045 // TODO: We can be smarter here.
5046 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5047 // which generates the implicit null check. There are cases when these can be removed
5048 // to produce better code. If we ever add optimizations to do so we should allow an
5049 // implicit check here (as long as the address falls in the first page).
5050 return false;
5051 }
5052
David Brazdil4833f5a2015-12-16 10:37:39 +00005053 bool IsEquivalentOf(HArrayGet* other) const {
5054 bool result = (GetDexPc() == other->GetDexPc());
5055 if (kIsDebugBuild && result) {
5056 DCHECK_EQ(GetBlock(), other->GetBlock());
5057 DCHECK_EQ(GetArray(), other->GetArray());
5058 DCHECK_EQ(GetIndex(), other->GetIndex());
5059 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005060 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005061 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005062 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5063 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005064 }
5065 }
5066 return result;
5067 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005068
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005069 HInstruction* GetArray() const { return InputAt(0); }
5070 HInstruction* GetIndex() const { return InputAt(1); }
5071
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072 DECLARE_INSTRUCTION(ArrayGet);
5073
5074 private:
5075 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5076};
5077
5078class HArraySet : public HTemplateInstruction<3> {
5079 public:
5080 HArraySet(HInstruction* array,
5081 HInstruction* index,
5082 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005083 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005084 uint32_t dex_pc,
5085 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005086 : HTemplateInstruction(
5087 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005088 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5089 additional_side_effects),
5090 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005091 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005092 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005093 value_can_be_null_(true),
5094 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005095 SetRawInputAt(0, array);
5096 SetRawInputAt(1, index);
5097 SetRawInputAt(2, value);
5098 }
5099
Calin Juravle77520bc2015-01-12 18:45:46 +00005100 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005101 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005102 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103 }
5104
Mingyao Yang81014cb2015-06-02 03:16:27 -07005105 // Can throw ArrayStoreException.
5106 bool CanThrow() const OVERRIDE { return needs_type_check_; }
5107
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005108 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005109 // TODO: Same as for ArrayGet.
5110 return false;
5111 }
5112
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005113 void ClearNeedsTypeCheck() {
5114 needs_type_check_ = false;
5115 }
5116
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005117 void ClearValueCanBeNull() {
5118 value_can_be_null_ = false;
5119 }
5120
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005121 void SetStaticTypeOfArrayIsObjectArray() {
5122 static_type_of_array_is_object_array_ = true;
5123 }
5124
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005125 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005126 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005127 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005128
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005129 HInstruction* GetArray() const { return InputAt(0); }
5130 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005131 HInstruction* GetValue() const { return InputAt(2); }
5132
5133 Primitive::Type GetComponentType() const {
5134 // The Dex format does not type floating point index operations. Since the
5135 // `expected_component_type_` is set during building and can therefore not
5136 // be correct, we also check what is the value type. If it is a floating
5137 // point type, we must use that type.
5138 Primitive::Type value_type = GetValue()->GetType();
5139 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5140 ? value_type
5141 : expected_component_type_;
5142 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005143
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005144 Primitive::Type GetRawExpectedComponentType() const {
5145 return expected_component_type_;
5146 }
5147
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005148 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5149 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5150 }
5151
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 DECLARE_INSTRUCTION(ArraySet);
5153
5154 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005155 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005156 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005157 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005158 // Cached information for the reference_type_info_ so that codegen
5159 // does not need to inspect the static type.
5160 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161
5162 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5163};
5164
5165class HArrayLength : public HExpression<1> {
5166 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005167 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005168 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005169 // Note that arrays do not change length, so the instruction does not
5170 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005171 SetRawInputAt(0, array);
5172 }
5173
Calin Juravle77520bc2015-01-12 18:45:46 +00005174 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005175 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005176 return true;
5177 }
Calin Juravle641547a2015-04-21 22:08:51 +01005178 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5179 return obj == InputAt(0);
5180 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005181
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182 DECLARE_INSTRUCTION(ArrayLength);
5183
5184 private:
5185 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5186};
5187
5188class HBoundsCheck : public HExpression<2> {
5189 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005190 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5191 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005193 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005194 DCHECK(index->GetType() == Primitive::kPrimInt);
5195 SetRawInputAt(0, index);
5196 SetRawInputAt(1, length);
5197 }
5198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005199 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005200 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005201 return true;
5202 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005203
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005204 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005206 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005207
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005208 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209
5210 DECLARE_INSTRUCTION(BoundsCheck);
5211
5212 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005213 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5214};
5215
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005216class HSuspendCheck : public HTemplateInstruction<0> {
5217 public:
5218 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005219 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005220
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005221 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005222 return true;
5223 }
5224
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005225 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5226 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005227
5228 DECLARE_INSTRUCTION(SuspendCheck);
5229
5230 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005231 // Only used for code generation, in order to share the same slow path between back edges
5232 // of a same loop.
5233 SlowPathCode* slow_path_;
5234
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005235 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5236};
5237
David Srbecky0cf44932015-12-09 14:09:59 +00005238// Pseudo-instruction which provides the native debugger with mapping information.
5239// It ensures that we can generate line number and local variables at this point.
5240class HNativeDebugInfo : public HTemplateInstruction<0> {
5241 public:
5242 explicit HNativeDebugInfo(uint32_t dex_pc)
5243 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5244
5245 bool NeedsEnvironment() const OVERRIDE {
5246 return true;
5247 }
5248
5249 DECLARE_INSTRUCTION(NativeDebugInfo);
5250
5251 private:
5252 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5253};
5254
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005255/**
5256 * Instruction to load a Class object.
5257 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005258class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005259 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005260 HLoadClass(HCurrentMethod* current_method,
5261 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005262 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005263 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005264 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005265 bool needs_access_check,
5266 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005267 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005268 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005269 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005270 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005271 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005272 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005273 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005274 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005275 // Referrers class should not need access check. We never inline unverified
5276 // methods so we can't possibly end up in this situation.
5277 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005278 SetRawInputAt(0, current_method);
5279 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005280
5281 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005282
5283 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005284 // Note that we don't need to test for generate_clinit_check_.
5285 // Whether or not we need to generate the clinit check is processed in
5286 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005287 return other->AsLoadClass()->type_index_ == type_index_ &&
5288 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005289 }
5290
5291 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5292
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005293 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005294 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005295 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005296
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005297 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005298 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005299 }
5300
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005301 bool MustGenerateClinitCheck() const {
5302 return generate_clinit_check_;
5303 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005304
Calin Juravle0ba218d2015-05-19 18:46:01 +01005305 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005306 // The entrypoint the code generator is going to call does not do
5307 // clinit of the class.
5308 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005309 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005310 }
5311
5312 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005313 return MustGenerateClinitCheck() ||
5314 (!is_referrers_class_ && !is_in_dex_cache_) ||
5315 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005316 }
5317
5318 bool NeedsAccessCheck() const {
5319 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005320 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005321
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005322 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005323 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005324 }
5325
Calin Juravleacf735c2015-02-12 15:25:22 +00005326 ReferenceTypeInfo GetLoadedClassRTI() {
5327 return loaded_class_rti_;
5328 }
5329
5330 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5331 // Make sure we only set exact types (the loaded class should never be merged).
5332 DCHECK(rti.IsExact());
5333 loaded_class_rti_ = rti;
5334 }
5335
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005336 const DexFile& GetDexFile() { return dex_file_; }
5337
Vladimir Markodc151b22015-10-15 18:02:30 +01005338 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005339
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005340 static SideEffects SideEffectsForArchRuntimeCalls() {
5341 return SideEffects::CanTriggerGC();
5342 }
5343
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005344 bool IsInDexCache() const { return is_in_dex_cache_; }
5345
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005346 DECLARE_INSTRUCTION(LoadClass);
5347
5348 private:
5349 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005350 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005351 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005352 // Whether this instruction must generate the initialization check.
5353 // Used for code generation.
5354 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005355 const bool needs_access_check_;
5356 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005357
Calin Juravleacf735c2015-02-12 15:25:22 +00005358 ReferenceTypeInfo loaded_class_rti_;
5359
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005360 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5361};
5362
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005363class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005364 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005365 HLoadString(HCurrentMethod* current_method,
5366 uint32_t string_index,
5367 uint32_t dex_pc,
5368 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005369 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005370 string_index_(string_index),
5371 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005372 SetRawInputAt(0, current_method);
5373 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005374
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005375 bool CanBeMoved() const OVERRIDE { return true; }
5376
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005377 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5378 return other->AsLoadString()->string_index_ == string_index_;
5379 }
5380
5381 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5382
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005383 uint32_t GetStringIndex() const { return string_index_; }
5384
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005385 // Will call the runtime if the string is not already in the dex cache.
5386 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5387
Vladimir Markodc151b22015-10-15 18:02:30 +01005388 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005389 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005390 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005391 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005392
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005393 static SideEffects SideEffectsForArchRuntimeCalls() {
5394 return SideEffects::CanTriggerGC();
5395 }
5396
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005397 DECLARE_INSTRUCTION(LoadString);
5398
5399 private:
5400 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005401 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005402
5403 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5404};
5405
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005406/**
5407 * Performs an initialization check on its Class object input.
5408 */
5409class HClinitCheck : public HExpression<1> {
5410 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005411 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005412 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005413 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005414 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5415 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416 SetRawInputAt(0, constant);
5417 }
5418
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005419 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005420 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005421 return true;
5422 }
5423
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005424 bool NeedsEnvironment() const OVERRIDE {
5425 // May call runtime to initialize the class.
5426 return true;
5427 }
5428
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005429 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005430
5431 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5432
5433 DECLARE_INSTRUCTION(ClinitCheck);
5434
5435 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005436 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5437};
5438
5439class HStaticFieldGet : public HExpression<1> {
5440 public:
5441 HStaticFieldGet(HInstruction* cls,
5442 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005443 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005444 bool is_volatile,
5445 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005446 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005447 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005448 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005449 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005450 : HExpression(field_type,
5451 SideEffects::FieldReadOfType(field_type, is_volatile),
5452 dex_pc),
5453 field_info_(field_offset,
5454 field_type,
5455 is_volatile,
5456 field_idx,
5457 declaring_class_def_index,
5458 dex_file,
5459 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005460 SetRawInputAt(0, cls);
5461 }
5462
Calin Juravle52c48962014-12-16 17:02:57 +00005463
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005464 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005465
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005466 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005467 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5468 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005469 }
5470
5471 size_t ComputeHashCode() const OVERRIDE {
5472 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5473 }
5474
Calin Juravle52c48962014-12-16 17:02:57 +00005475 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005476 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5477 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005478 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005479
5480 DECLARE_INSTRUCTION(StaticFieldGet);
5481
5482 private:
5483 const FieldInfo field_info_;
5484
5485 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5486};
5487
5488class HStaticFieldSet : public HTemplateInstruction<2> {
5489 public:
5490 HStaticFieldSet(HInstruction* cls,
5491 HInstruction* value,
5492 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005493 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005494 bool is_volatile,
5495 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005496 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005497 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005498 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005499 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005500 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5501 dex_pc),
5502 field_info_(field_offset,
5503 field_type,
5504 is_volatile,
5505 field_idx,
5506 declaring_class_def_index,
5507 dex_file,
5508 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005509 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005510 SetRawInputAt(0, cls);
5511 SetRawInputAt(1, value);
5512 }
5513
Calin Juravle52c48962014-12-16 17:02:57 +00005514 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005515 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5516 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005517 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005518
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005519 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005520 bool GetValueCanBeNull() const { return value_can_be_null_; }
5521 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005522
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005523 DECLARE_INSTRUCTION(StaticFieldSet);
5524
5525 private:
5526 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005527 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005528
5529 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5530};
5531
Calin Juravlee460d1d2015-09-29 04:52:17 +01005532class HUnresolvedInstanceFieldGet : public HExpression<1> {
5533 public:
5534 HUnresolvedInstanceFieldGet(HInstruction* obj,
5535 Primitive::Type field_type,
5536 uint32_t field_index,
5537 uint32_t dex_pc)
5538 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5539 field_index_(field_index) {
5540 SetRawInputAt(0, obj);
5541 }
5542
5543 bool NeedsEnvironment() const OVERRIDE { return true; }
5544 bool CanThrow() const OVERRIDE { return true; }
5545
5546 Primitive::Type GetFieldType() const { return GetType(); }
5547 uint32_t GetFieldIndex() const { return field_index_; }
5548
5549 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5550
5551 private:
5552 const uint32_t field_index_;
5553
5554 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5555};
5556
5557class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5558 public:
5559 HUnresolvedInstanceFieldSet(HInstruction* obj,
5560 HInstruction* value,
5561 Primitive::Type field_type,
5562 uint32_t field_index,
5563 uint32_t dex_pc)
5564 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5565 field_type_(field_type),
5566 field_index_(field_index) {
5567 DCHECK_EQ(field_type, value->GetType());
5568 SetRawInputAt(0, obj);
5569 SetRawInputAt(1, value);
5570 }
5571
5572 bool NeedsEnvironment() const OVERRIDE { return true; }
5573 bool CanThrow() const OVERRIDE { return true; }
5574
5575 Primitive::Type GetFieldType() const { return field_type_; }
5576 uint32_t GetFieldIndex() const { return field_index_; }
5577
5578 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5579
5580 private:
5581 const Primitive::Type field_type_;
5582 const uint32_t field_index_;
5583
5584 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5585};
5586
5587class HUnresolvedStaticFieldGet : public HExpression<0> {
5588 public:
5589 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5590 uint32_t field_index,
5591 uint32_t dex_pc)
5592 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5593 field_index_(field_index) {
5594 }
5595
5596 bool NeedsEnvironment() const OVERRIDE { return true; }
5597 bool CanThrow() const OVERRIDE { return true; }
5598
5599 Primitive::Type GetFieldType() const { return GetType(); }
5600 uint32_t GetFieldIndex() const { return field_index_; }
5601
5602 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5603
5604 private:
5605 const uint32_t field_index_;
5606
5607 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5608};
5609
5610class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5611 public:
5612 HUnresolvedStaticFieldSet(HInstruction* value,
5613 Primitive::Type field_type,
5614 uint32_t field_index,
5615 uint32_t dex_pc)
5616 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5617 field_type_(field_type),
5618 field_index_(field_index) {
5619 DCHECK_EQ(field_type, value->GetType());
5620 SetRawInputAt(0, value);
5621 }
5622
5623 bool NeedsEnvironment() const OVERRIDE { return true; }
5624 bool CanThrow() const OVERRIDE { return true; }
5625
5626 Primitive::Type GetFieldType() const { return field_type_; }
5627 uint32_t GetFieldIndex() const { return field_index_; }
5628
5629 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5630
5631 private:
5632 const Primitive::Type field_type_;
5633 const uint32_t field_index_;
5634
5635 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5636};
5637
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005638// Implement the move-exception DEX instruction.
5639class HLoadException : public HExpression<0> {
5640 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005641 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5642 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005643
David Brazdilbbd733e2015-08-18 17:48:17 +01005644 bool CanBeNull() const OVERRIDE { return false; }
5645
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005646 DECLARE_INSTRUCTION(LoadException);
5647
5648 private:
5649 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5650};
5651
David Brazdilcb1c0552015-08-04 16:22:25 +01005652// Implicit part of move-exception which clears thread-local exception storage.
5653// Must not be removed because the runtime expects the TLS to get cleared.
5654class HClearException : public HTemplateInstruction<0> {
5655 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005656 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5657 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005658
5659 DECLARE_INSTRUCTION(ClearException);
5660
5661 private:
5662 DISALLOW_COPY_AND_ASSIGN(HClearException);
5663};
5664
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005665class HThrow : public HTemplateInstruction<1> {
5666 public:
5667 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005668 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005669 SetRawInputAt(0, exception);
5670 }
5671
5672 bool IsControlFlow() const OVERRIDE { return true; }
5673
5674 bool NeedsEnvironment() const OVERRIDE { return true; }
5675
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005676 bool CanThrow() const OVERRIDE { return true; }
5677
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005678
5679 DECLARE_INSTRUCTION(Throw);
5680
5681 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005682 DISALLOW_COPY_AND_ASSIGN(HThrow);
5683};
5684
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005685/**
5686 * Implementation strategies for the code generator of a HInstanceOf
5687 * or `HCheckCast`.
5688 */
5689enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005690 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005691 kExactCheck, // Can do a single class compare.
5692 kClassHierarchyCheck, // Can just walk the super class chain.
5693 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5694 kInterfaceCheck, // No optimization yet when checking against an interface.
5695 kArrayObjectCheck, // Can just check if the array is not primitive.
5696 kArrayCheck // No optimization yet when checking against a generic array.
5697};
5698
Roland Levillain86503782016-02-11 19:07:30 +00005699std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5700
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005701class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005702 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005703 HInstanceOf(HInstruction* object,
5704 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005706 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005707 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005709 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005710 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005711 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005712 SetRawInputAt(0, object);
5713 SetRawInputAt(1, constant);
5714 }
5715
5716 bool CanBeMoved() const OVERRIDE { return true; }
5717
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005718 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005719 return true;
5720 }
5721
5722 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005723 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005724 }
5725
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005726 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5727
5728 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005729
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005730 // Used only in code generation.
5731 bool MustDoNullCheck() const { return must_do_null_check_; }
5732 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5733
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005734 static bool CanCallRuntime(TypeCheckKind check_kind) {
5735 // Mips currently does runtime calls for any other checks.
5736 return check_kind != TypeCheckKind::kExactCheck;
5737 }
5738
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005739 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005740 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005741 }
5742
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005743 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005744
5745 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005746 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005747 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005748
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005749 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5750};
5751
Calin Juravleb1498f62015-02-16 13:13:29 +00005752class HBoundType : public HExpression<1> {
5753 public:
David Brazdilf5552582015-12-27 13:36:12 +00005754 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005755 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005756 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5757 upper_can_be_null_(true),
5758 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005759 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005760 SetRawInputAt(0, input);
5761 }
5762
David Brazdilf5552582015-12-27 13:36:12 +00005763 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005764 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5765 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005766 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005767
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005768 void SetCanBeNull(bool can_be_null) {
5769 DCHECK(upper_can_be_null_ || !can_be_null);
5770 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005771 }
5772
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005773 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5774
Calin Juravleb1498f62015-02-16 13:13:29 +00005775 DECLARE_INSTRUCTION(BoundType);
5776
5777 private:
5778 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005779 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5780 // It is used to bound the type in cases like:
5781 // if (x instanceof ClassX) {
5782 // // uper_bound_ will be ClassX
5783 // }
David Brazdilf5552582015-12-27 13:36:12 +00005784 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005785 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5786 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005787 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005788 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005789
5790 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5791};
5792
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005793class HCheckCast : public HTemplateInstruction<2> {
5794 public:
5795 HCheckCast(HInstruction* object,
5796 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005798 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005799 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005800 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005801 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005802 SetRawInputAt(0, object);
5803 SetRawInputAt(1, constant);
5804 }
5805
5806 bool CanBeMoved() const OVERRIDE { return true; }
5807
5808 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5809 return true;
5810 }
5811
5812 bool NeedsEnvironment() const OVERRIDE {
5813 // Instruction may throw a CheckCastError.
5814 return true;
5815 }
5816
5817 bool CanThrow() const OVERRIDE { return true; }
5818
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005819 bool MustDoNullCheck() const { return must_do_null_check_; }
5820 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005821 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005822
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005823 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005824
5825 DECLARE_INSTRUCTION(CheckCast);
5826
5827 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005828 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005829 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005830
5831 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005832};
5833
Calin Juravle27df7582015-04-17 19:12:31 +01005834class HMemoryBarrier : public HTemplateInstruction<0> {
5835 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005836 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005837 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005838 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005839 barrier_kind_(barrier_kind) {}
5840
5841 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5842
5843 DECLARE_INSTRUCTION(MemoryBarrier);
5844
5845 private:
5846 const MemBarrierKind barrier_kind_;
5847
5848 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5849};
5850
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005851class HMonitorOperation : public HTemplateInstruction<1> {
5852 public:
5853 enum OperationKind {
5854 kEnter,
5855 kExit,
5856 };
5857
5858 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005859 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005860 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5861 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005862 SetRawInputAt(0, object);
5863 }
5864
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005865 // Instruction may go into runtime, so we need an environment.
5866 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005867
5868 bool CanThrow() const OVERRIDE {
5869 // Verifier guarantees that monitor-exit cannot throw.
5870 // This is important because it allows the HGraphBuilder to remove
5871 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5872 return IsEnter();
5873 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005874
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005875
5876 bool IsEnter() const { return kind_ == kEnter; }
5877
5878 DECLARE_INSTRUCTION(MonitorOperation);
5879
Calin Juravle52c48962014-12-16 17:02:57 +00005880 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005881 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005882
5883 private:
5884 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5885};
5886
David Brazdil74eb1b22015-12-14 11:44:01 +00005887class HSelect : public HExpression<3> {
5888 public:
5889 HSelect(HInstruction* condition,
5890 HInstruction* true_value,
5891 HInstruction* false_value,
5892 uint32_t dex_pc)
5893 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5894 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5895
5896 // First input must be `true_value` or `false_value` to allow codegens to
5897 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5898 // that architectures which implement HSelect as a conditional move also
5899 // will not need to invert the condition.
5900 SetRawInputAt(0, false_value);
5901 SetRawInputAt(1, true_value);
5902 SetRawInputAt(2, condition);
5903 }
5904
5905 HInstruction* GetFalseValue() const { return InputAt(0); }
5906 HInstruction* GetTrueValue() const { return InputAt(1); }
5907 HInstruction* GetCondition() const { return InputAt(2); }
5908
5909 bool CanBeMoved() const OVERRIDE { return true; }
5910 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5911
5912 bool CanBeNull() const OVERRIDE {
5913 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5914 }
5915
5916 DECLARE_INSTRUCTION(Select);
5917
5918 private:
5919 DISALLOW_COPY_AND_ASSIGN(HSelect);
5920};
5921
Vladimir Markof9f64412015-09-02 14:05:49 +01005922class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005923 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005924 MoveOperands(Location source,
5925 Location destination,
5926 Primitive::Type type,
5927 HInstruction* instruction)
5928 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005929
5930 Location GetSource() const { return source_; }
5931 Location GetDestination() const { return destination_; }
5932
5933 void SetSource(Location value) { source_ = value; }
5934 void SetDestination(Location value) { destination_ = value; }
5935
5936 // The parallel move resolver marks moves as "in-progress" by clearing the
5937 // destination (but not the source).
5938 Location MarkPending() {
5939 DCHECK(!IsPending());
5940 Location dest = destination_;
5941 destination_ = Location::NoLocation();
5942 return dest;
5943 }
5944
5945 void ClearPending(Location dest) {
5946 DCHECK(IsPending());
5947 destination_ = dest;
5948 }
5949
5950 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005951 DCHECK(source_.IsValid() || destination_.IsInvalid());
5952 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005953 }
5954
5955 // True if this blocks a move from the given location.
5956 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005957 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005958 }
5959
5960 // A move is redundant if it's been eliminated, if its source and
5961 // destination are the same, or if its destination is unneeded.
5962 bool IsRedundant() const {
5963 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5964 }
5965
5966 // We clear both operands to indicate move that's been eliminated.
5967 void Eliminate() {
5968 source_ = destination_ = Location::NoLocation();
5969 }
5970
5971 bool IsEliminated() const {
5972 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5973 return source_.IsInvalid();
5974 }
5975
Alexey Frunze4dda3372015-06-01 18:31:49 -07005976 Primitive::Type GetType() const { return type_; }
5977
Nicolas Geoffray90218252015-04-15 11:56:51 +01005978 bool Is64BitMove() const {
5979 return Primitive::Is64BitType(type_);
5980 }
5981
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005982 HInstruction* GetInstruction() const { return instruction_; }
5983
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005984 private:
5985 Location source_;
5986 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005987 // The type this move is for.
5988 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005989 // The instruction this move is assocatied with. Null when this move is
5990 // for moving an input in the expected locations of user (including a phi user).
5991 // This is only used in debug mode, to ensure we do not connect interval siblings
5992 // in the same parallel move.
5993 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005994};
5995
Roland Levillainc9285912015-12-18 10:38:42 +00005996std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5997
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005998static constexpr size_t kDefaultNumberOfMoves = 4;
5999
6000class HParallelMove : public HTemplateInstruction<0> {
6001 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006002 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006003 : HTemplateInstruction(SideEffects::None(), dex_pc),
6004 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6005 moves_.reserve(kDefaultNumberOfMoves);
6006 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006007
Nicolas Geoffray90218252015-04-15 11:56:51 +01006008 void AddMove(Location source,
6009 Location destination,
6010 Primitive::Type type,
6011 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006012 DCHECK(source.IsValid());
6013 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006014 if (kIsDebugBuild) {
6015 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006016 for (const MoveOperands& move : moves_) {
6017 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006018 // Special case the situation where the move is for the spill slot
6019 // of the instruction.
6020 if ((GetPrevious() == instruction)
6021 || ((GetPrevious() == nullptr)
6022 && instruction->IsPhi()
6023 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006024 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006025 << "Doing parallel moves for the same instruction.";
6026 } else {
6027 DCHECK(false) << "Doing parallel moves for the same instruction.";
6028 }
6029 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006030 }
6031 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006032 for (const MoveOperands& move : moves_) {
6033 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006034 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006035 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006036 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006037 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006038 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006039 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006040 }
6041
Vladimir Marko225b6462015-09-28 12:17:40 +01006042 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006043 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006044 }
6045
Vladimir Marko225b6462015-09-28 12:17:40 +01006046 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006047
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006048 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006049
6050 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006051 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006052
6053 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6054};
6055
Mark Mendell0616ae02015-04-17 12:49:27 -04006056} // namespace art
6057
Vladimir Markob4536b72015-11-24 13:45:23 +00006058#ifdef ART_ENABLE_CODEGEN_arm
6059#include "nodes_arm.h"
6060#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006061#ifdef ART_ENABLE_CODEGEN_arm64
6062#include "nodes_arm64.h"
6063#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006064#ifdef ART_ENABLE_CODEGEN_x86
6065#include "nodes_x86.h"
6066#endif
6067
6068namespace art {
6069
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006070class HGraphVisitor : public ValueObject {
6071 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006072 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6073 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006074
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006075 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006076 virtual void VisitBasicBlock(HBasicBlock* block);
6077
Roland Levillain633021e2014-10-01 14:12:25 +01006078 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006079 void VisitInsertionOrder();
6080
Roland Levillain633021e2014-10-01 14:12:25 +01006081 // Visit the graph following dominator tree reverse post-order.
6082 void VisitReversePostOrder();
6083
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006084 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006085
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006086 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006087#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006088 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6089
6090 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6091
6092#undef DECLARE_VISIT_INSTRUCTION
6093
6094 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006095 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006096
6097 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6098};
6099
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006100class HGraphDelegateVisitor : public HGraphVisitor {
6101 public:
6102 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6103 virtual ~HGraphDelegateVisitor() {}
6104
6105 // Visit functions that delegate to to super class.
6106#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006107 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006108
6109 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6110
6111#undef DECLARE_VISIT_INSTRUCTION
6112
6113 private:
6114 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6115};
6116
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006117class HInsertionOrderIterator : public ValueObject {
6118 public:
6119 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6120
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006121 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006122 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006123 void Advance() { ++index_; }
6124
6125 private:
6126 const HGraph& graph_;
6127 size_t index_;
6128
6129 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6130};
6131
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006132class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006133 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006134 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6135 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006136 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006137 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006138
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006139 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6140 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006141 void Advance() { ++index_; }
6142
6143 private:
6144 const HGraph& graph_;
6145 size_t index_;
6146
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006147 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006148};
6149
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006150class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006151 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006152 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006153 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006154 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006155 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006156 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006157
6158 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006159 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006160 void Advance() { --index_; }
6161
6162 private:
6163 const HGraph& graph_;
6164 size_t index_;
6165
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006166 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006167};
6168
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006169class HLinearPostOrderIterator : public ValueObject {
6170 public:
6171 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006172 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006173
6174 bool Done() const { return index_ == 0; }
6175
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006176 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006177
6178 void Advance() {
6179 --index_;
6180 DCHECK_GE(index_, 0U);
6181 }
6182
6183 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006184 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006185 size_t index_;
6186
6187 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6188};
6189
6190class HLinearOrderIterator : public ValueObject {
6191 public:
6192 explicit HLinearOrderIterator(const HGraph& graph)
6193 : order_(graph.GetLinearOrder()), index_(0) {}
6194
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006195 bool Done() const { return index_ == order_.size(); }
6196 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006197 void Advance() { ++index_; }
6198
6199 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006200 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006201 size_t index_;
6202
6203 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6204};
6205
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006206// Iterator over the blocks that art part of the loop. Includes blocks part
6207// of an inner loop. The order in which the blocks are iterated is on their
6208// block id.
6209class HBlocksInLoopIterator : public ValueObject {
6210 public:
6211 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6212 : blocks_in_loop_(info.GetBlocks()),
6213 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6214 index_(0) {
6215 if (!blocks_in_loop_.IsBitSet(index_)) {
6216 Advance();
6217 }
6218 }
6219
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006220 bool Done() const { return index_ == blocks_.size(); }
6221 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006222 void Advance() {
6223 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006224 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006225 if (blocks_in_loop_.IsBitSet(index_)) {
6226 break;
6227 }
6228 }
6229 }
6230
6231 private:
6232 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006233 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006234 size_t index_;
6235
6236 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6237};
6238
Mingyao Yang3584bce2015-05-19 16:01:59 -07006239// Iterator over the blocks that art part of the loop. Includes blocks part
6240// of an inner loop. The order in which the blocks are iterated is reverse
6241// post order.
6242class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6243 public:
6244 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6245 : blocks_in_loop_(info.GetBlocks()),
6246 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6247 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006248 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006249 Advance();
6250 }
6251 }
6252
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006253 bool Done() const { return index_ == blocks_.size(); }
6254 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006255 void Advance() {
6256 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006257 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6258 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006259 break;
6260 }
6261 }
6262 }
6263
6264 private:
6265 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006266 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006267 size_t index_;
6268
6269 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6270};
6271
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006272inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006273 if (constant->IsIntConstant()) {
6274 return constant->AsIntConstant()->GetValue();
6275 } else if (constant->IsLongConstant()) {
6276 return constant->AsLongConstant()->GetValue();
6277 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006278 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006279 return 0;
6280 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006281}
6282
Vladimir Marko58155012015-08-19 12:49:41 +00006283inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6284 // For the purposes of the compiler, the dex files must actually be the same object
6285 // if we want to safely treat them as the same. This is especially important for JIT
6286 // as custom class loaders can open the same underlying file (or memory) multiple
6287 // times and provide different class resolution but no two class loaders should ever
6288 // use the same DexFile object - doing so is an unsupported hack that can lead to
6289 // all sorts of weird failures.
6290 return &lhs == &rhs;
6291}
6292
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006293#define INSTRUCTION_TYPE_CHECK(type, super) \
6294 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6295 inline const H##type* HInstruction::As##type() const { \
6296 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6297 } \
6298 inline H##type* HInstruction::As##type() { \
6299 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6300 }
6301
6302 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6303#undef INSTRUCTION_TYPE_CHECK
6304
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006305class SwitchTable : public ValueObject {
6306 public:
6307 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6308 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6309 int32_t table_offset = instruction.VRegB_31t();
6310 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6311 if (sparse) {
6312 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6313 } else {
6314 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6315 }
6316 num_entries_ = table[1];
6317 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6318 }
6319
6320 uint16_t GetNumEntries() const {
6321 return num_entries_;
6322 }
6323
6324 void CheckIndex(size_t index) const {
6325 if (sparse_) {
6326 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6327 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6328 } else {
6329 // In a packed table, we have the starting key and num_entries_ values.
6330 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6331 }
6332 }
6333
6334 int32_t GetEntryAt(size_t index) const {
6335 CheckIndex(index);
6336 return values_[index];
6337 }
6338
6339 uint32_t GetDexPcForIndex(size_t index) const {
6340 CheckIndex(index);
6341 return dex_pc_ +
6342 (reinterpret_cast<const int16_t*>(values_ + index) -
6343 reinterpret_cast<const int16_t*>(&instruction_));
6344 }
6345
6346 // Index of the first value in the table.
6347 size_t GetFirstValueIndex() const {
6348 if (sparse_) {
6349 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6350 return num_entries_;
6351 } else {
6352 // In a packed table, we have the starting key and num_entries_ values.
6353 return 1;
6354 }
6355 }
6356
6357 private:
6358 const Instruction& instruction_;
6359 const uint32_t dex_pc_;
6360
6361 // Whether this is a sparse-switch table (or a packed-switch one).
6362 const bool sparse_;
6363
6364 // This can't be const as it needs to be computed off of the given instruction, and complicated
6365 // expressions in the initializer list seemed very ugly.
6366 uint16_t num_entries_;
6367
6368 const int32_t* values_;
6369
6370 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6371};
6372
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006373} // namespace art
6374
6375#endif // ART_COMPILER_OPTIMIZING_NODES_H_