blob: 06a65e32198c60c504d1722abbfc7c8638eb1a11 [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000024#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000026#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010027#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000028#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000029#include "handle.h"
30#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010032#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000033#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000037#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038#include "utils/growable_array.h"
39
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
79
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Dave Allison20dfc792014-06-16 20:44:29 -070082enum IfCondition {
83 kCondEQ,
84 kCondNE,
85 kCondLT,
86 kCondLE,
87 kCondGT,
88 kCondGE,
89};
90
Vladimir Markof9f64412015-09-02 14:05:49 +010091class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010092 public:
93 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
94
95 void AddInstruction(HInstruction* instruction);
96 void RemoveInstruction(HInstruction* instruction);
97
David Brazdilc3d743f2015-04-22 13:40:50 +010098 // Insert `instruction` before/after an existing instruction `cursor`.
99 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
100 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
101
Roland Levillain6b469232014-09-25 10:10:38 +0100102 // Return true if this list contains `instruction`.
103 bool Contains(HInstruction* instruction) const;
104
Roland Levillainccc07a92014-09-16 14:48:16 +0100105 // Return true if `instruction1` is found before `instruction2` in
106 // this instruction list and false otherwise. Abort if none
107 // of these instructions is found.
108 bool FoundBefore(const HInstruction* instruction1,
109 const HInstruction* instruction2) const;
110
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000111 bool IsEmpty() const { return first_instruction_ == nullptr; }
112 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
113
114 // Update the block of all instructions to be `block`.
115 void SetBlockOfInstructions(HBasicBlock* block) const;
116
117 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
118 void Add(const HInstructionList& instruction_list);
119
David Brazdil2d7352b2015-04-20 14:52:42 +0100120 // Return the number of instructions in the list. This is an expensive operation.
121 size_t CountSize() const;
122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100123 private:
124 HInstruction* first_instruction_;
125 HInstruction* last_instruction_;
126
127 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000128 friend class HGraph;
129 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100130 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100131 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132
133 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
134};
135
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100139 HGraph(ArenaAllocator* arena,
140 const DexFile& dex_file,
141 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100142 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100144 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100145 bool debuggable = false,
146 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000148 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100149 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100150 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700151 entry_block_(nullptr),
152 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100153 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 number_of_vregs_(0),
155 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000156 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400157 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000158 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000159 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100160 dex_file_(dex_file),
161 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100162 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100163 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100164 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000166 cached_null_constant_(nullptr),
167 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000168 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
169 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100170 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
171 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000172
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000173 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100174 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100175 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176
David Brazdil69ba7b72015-06-23 18:27:30 +0100177 bool IsInSsaForm() const { return in_ssa_form_; }
178
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000179 HBasicBlock* GetEntryBlock() const { return entry_block_; }
180 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100181 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000182
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000183 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
184 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000185
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000186 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 // Try building the SSA form of this graph, with dominance computation and loop
189 // recognition. Returns whether it was successful in doing all these steps.
190 bool TryBuildingSsa() {
191 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000192 // The SSA builder requires loops to all be natural. Specifically, the dead phi
193 // elimination phase checks the consistency of the graph when doing a post-order
194 // visit for eliminating dead phis: a dead phi can only have loop header phi
195 // users remaining when being visited.
196 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100197 // Precompute per-block try membership before entering the SSA builder,
198 // which needs the information to build catch block phis from values of
199 // locals at throwing instructions inside try blocks.
200 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000201 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100202 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 }
205
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100206 void ComputeDominanceInformation();
207 void ClearDominanceInformation();
208
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000210 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100211 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100212 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000213
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000214 // Analyze all natural loops in this graph. Returns false if one
215 // loop is not natural, that is the header does not dominate the
216 // back edge.
217 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100218
David Brazdilffee3d32015-07-06 11:48:53 +0100219 // Iterate over blocks to compute try block membership. Needs reverse post
220 // order and loop information.
221 void ComputeTryBlockInformation();
222
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000224 // Returns the instruction used to replace the invoke expression or null if the
225 // invoke is for a void method.
226 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227
Mingyao Yang3584bce2015-05-19 16:01:59 -0700228 // Need to add a couple of blocks to test if the loop body is entered and
229 // put deoptimization instructions, etc.
230 void TransformLoopHeaderForBCE(HBasicBlock* header);
231
David Brazdil2d7352b2015-04-20 14:52:42 +0100232 // Removes `block` from the graph.
233 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000234
David Brazdilfc6a86a2015-06-26 10:33:45 +0000235 // Splits the edge between `block` and `successor` while preserving the
236 // indices in the predecessor/successor lists. If there are multiple edges
237 // between the blocks, the lowest indices are used.
238 // Returns the new block which is empty and has the same dex pc as `successor`.
239 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
240
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100241 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
242 void SimplifyLoop(HBasicBlock* header);
243
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000244 int32_t GetNextInstructionId() {
245 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000246 return current_instruction_id_++;
247 }
248
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000249 int32_t GetCurrentInstructionId() const {
250 return current_instruction_id_;
251 }
252
253 void SetCurrentInstructionId(int32_t id) {
254 current_instruction_id_ = id;
255 }
256
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100257 uint16_t GetMaximumNumberOfOutVRegs() const {
258 return maximum_number_of_out_vregs_;
259 }
260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000261 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
262 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100263 }
264
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100265 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
266 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
267 }
268
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000269 void UpdateTemporariesVRegSlots(size_t slots) {
270 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100271 }
272
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000273 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100274 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000275 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100276 }
277
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100278 void SetNumberOfVRegs(uint16_t number_of_vregs) {
279 number_of_vregs_ = number_of_vregs;
280 }
281
282 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100283 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100284 return number_of_vregs_;
285 }
286
287 void SetNumberOfInVRegs(uint16_t value) {
288 number_of_in_vregs_ = value;
289 }
290
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100291 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100292 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100293 return number_of_vregs_ - number_of_in_vregs_;
294 }
295
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100296 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
297 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100298 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100299
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100300 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
301 return linear_order_;
302 }
303
Mark Mendell1152c922015-04-24 17:06:35 -0400304 bool HasBoundsChecks() const {
305 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800306 }
307
Mark Mendell1152c922015-04-24 17:06:35 -0400308 void SetHasBoundsChecks(bool value) {
309 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800310 }
311
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100312 bool ShouldGenerateConstructorBarrier() const {
313 return should_generate_constructor_barrier_;
314 }
315
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000316 bool IsDebuggable() const { return debuggable_; }
317
David Brazdil8d5b8b22015-03-24 10:51:52 +0000318 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000319 // already, it is created and inserted into the graph. This method is only for
320 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000321 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000322
323 // TODO: This is problematic for the consistency of reference type propagation
324 // because it can be created anytime after the pass and thus it will be left
325 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000326 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000327
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 HIntConstant* GetIntConstant(int32_t value) {
329 return CreateConstant(value, &cached_int_constants_);
330 }
331 HLongConstant* GetLongConstant(int64_t value) {
332 return CreateConstant(value, &cached_long_constants_);
333 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000334 HFloatConstant* GetFloatConstant(float value) {
335 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
336 }
337 HDoubleConstant* GetDoubleConstant(double value) {
338 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
339 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000340
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100341 HCurrentMethod* GetCurrentMethod();
342
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100344
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100345 const DexFile& GetDexFile() const {
346 return dex_file_;
347 }
348
349 uint32_t GetMethodIdx() const {
350 return method_idx_;
351 }
352
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100353 InvokeType GetInvokeType() const {
354 return invoke_type_;
355 }
356
Mark Mendellc4701932015-04-10 13:18:51 -0400357 InstructionSet GetInstructionSet() const {
358 return instruction_set_;
359 }
360
David Brazdilbbd733e2015-08-18 17:48:17 +0100361 // TODO: Remove once the full compilation pipeline is enabled for try/catch.
362 bool HasTryCatch() const;
363
David Brazdil2d7352b2015-04-20 14:52:42 +0100364 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 void VisitBlockForDominatorTree(HBasicBlock* block,
366 HBasicBlock* predecessor,
367 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100368 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369 void VisitBlockForBackEdges(HBasicBlock* block,
370 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100371 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000372 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100373 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000375 template <class InstructionType, typename ValueType>
376 InstructionType* CreateConstant(ValueType value,
377 ArenaSafeMap<ValueType, InstructionType*>* cache) {
378 // Try to find an existing constant of the given value.
379 InstructionType* constant = nullptr;
380 auto cached_constant = cache->find(value);
381 if (cached_constant != cache->end()) {
382 constant = cached_constant->second;
383 }
384
385 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100386 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000387 if (constant == nullptr || constant->GetBlock() == nullptr) {
388 constant = new (arena_) InstructionType(value);
389 cache->Overwrite(value, constant);
390 InsertConstant(constant);
391 }
392 return constant;
393 }
394
David Brazdil8d5b8b22015-03-24 10:51:52 +0000395 void InsertConstant(HConstant* instruction);
396
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000397 // Cache a float constant into the graph. This method should only be
398 // called by the SsaBuilder when creating "equivalent" instructions.
399 void CacheFloatConstant(HFloatConstant* constant);
400
401 // See CacheFloatConstant comment.
402 void CacheDoubleConstant(HDoubleConstant* constant);
403
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000405
406 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407 GrowableArray<HBasicBlock*> blocks_;
408
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 // List of blocks to perform a reverse post order tree traversal.
410 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100412 // List of blocks to perform a linear order tree traversal.
413 GrowableArray<HBasicBlock*> linear_order_;
414
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000415 HBasicBlock* entry_block_;
416 HBasicBlock* exit_block_;
417
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100418 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100419 uint16_t maximum_number_of_out_vregs_;
420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421 // The number of virtual registers in this method. Contains the parameters.
422 uint16_t number_of_vregs_;
423
424 // The number of virtual registers used by parameters of this method.
425 uint16_t number_of_in_vregs_;
426
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000427 // Number of vreg size slots that the temporaries use (used in baseline compiler).
428 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100429
Mark Mendell1152c922015-04-24 17:06:35 -0400430 // Has bounds checks. We can totally skip BCE if it's false.
431 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800432
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000433 // Indicates whether the graph should be compiled in a way that
434 // ensures full debuggability. If false, we can apply more
435 // aggressive optimizations that may limit the level of debugging.
436 const bool debuggable_;
437
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000438 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000440
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100441 // The dex file from which the method is from.
442 const DexFile& dex_file_;
443
444 // The method index in the dex file.
445 const uint32_t method_idx_;
446
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100447 // If inlined, this encodes how the callee is being invoked.
448 const InvokeType invoke_type_;
449
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100450 // Whether the graph has been transformed to SSA form. Only used
451 // in debug mode to ensure we are not using properties only valid
452 // for non-SSA form (like the number of temporaries).
453 bool in_ssa_form_;
454
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100455 const bool should_generate_constructor_barrier_;
456
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 const InstructionSet instruction_set_;
458
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000459 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000460 HNullConstant* cached_null_constant_;
461 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000463 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000464 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000465
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100466 HCurrentMethod* cached_current_method_;
467
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100469 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000470 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000471 DISALLOW_COPY_AND_ASSIGN(HGraph);
472};
473
Vladimir Markof9f64412015-09-02 14:05:49 +0100474class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475 public:
476 HLoopInformation(HBasicBlock* header, HGraph* graph)
477 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100479 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100480 // Make bit vector growable, as the number of blocks may change.
481 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100482
483 HBasicBlock* GetHeader() const {
484 return header_;
485 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000486
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000487 void SetHeader(HBasicBlock* block) {
488 header_ = block;
489 }
490
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100491 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
492 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
493 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
494
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000495 void AddBackEdge(HBasicBlock* back_edge) {
496 back_edges_.Add(back_edge);
497 }
498
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100499 void RemoveBackEdge(HBasicBlock* back_edge) {
500 back_edges_.Delete(back_edge);
501 }
502
David Brazdil46e2a392015-03-16 17:31:52 +0000503 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000505 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 }
507 return false;
508 }
509
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000510 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000511 return back_edges_.Size();
512 }
513
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100515
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
517 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100518 }
519
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100520 // Returns the lifetime position of the back edge that has the
521 // greatest lifetime position.
522 size_t GetLifetimeEnd() const;
523
524 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
525 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
526 if (back_edges_.Get(i) == existing) {
527 back_edges_.Put(i, new_back_edge);
528 return;
529 }
530 }
531 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100532 }
533
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100534 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 // that is the header dominates the back edge.
536 bool Populate();
537
David Brazdila4b8c212015-05-07 09:59:30 +0100538 // Reanalyzes the loop by removing loop info from its blocks and re-running
539 // Populate(). If there are no back edges left, the loop info is completely
540 // removed as well as its SuspendCheck instruction. It must be run on nested
541 // inner loops first.
542 void Update();
543
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100544 // Returns whether this loop information contains `block`.
545 // Note that this loop information *must* be populated before entering this function.
546 bool Contains(const HBasicBlock& block) const;
547
548 // Returns whether this loop information is an inner loop of `other`.
549 // Note that `other` *must* be populated before entering this function.
550 bool IsIn(const HLoopInformation& other) const;
551
552 const ArenaBitVector& GetBlocks() const { return blocks_; }
553
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000554 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000555 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000556
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100558 // Internal recursive implementation of `Populate`.
559 void PopulateRecursive(HBasicBlock* block);
560
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100562 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565
566 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
567};
568
David Brazdilec16f792015-08-19 15:04:01 +0100569// Stores try/catch information for basic blocks.
570// Note that HGraph is constructed so that catch blocks cannot simultaneously
571// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100572class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100573 public:
574 // Try block information constructor.
575 explicit TryCatchInformation(const HTryBoundary& try_entry)
576 : try_entry_(&try_entry),
577 catch_dex_file_(nullptr),
578 catch_type_index_(DexFile::kDexNoIndex16) {
579 DCHECK(try_entry_ != nullptr);
580 }
581
582 // Catch block information constructor.
583 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
584 : try_entry_(nullptr),
585 catch_dex_file_(&dex_file),
586 catch_type_index_(catch_type_index) {}
587
588 bool IsTryBlock() const { return try_entry_ != nullptr; }
589
590 const HTryBoundary& GetTryEntry() const {
591 DCHECK(IsTryBlock());
592 return *try_entry_;
593 }
594
595 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
596
597 bool IsCatchAllTypeIndex() const {
598 DCHECK(IsCatchBlock());
599 return catch_type_index_ == DexFile::kDexNoIndex16;
600 }
601
602 uint16_t GetCatchTypeIndex() const {
603 DCHECK(IsCatchBlock());
604 return catch_type_index_;
605 }
606
607 const DexFile& GetCatchDexFile() const {
608 DCHECK(IsCatchBlock());
609 return *catch_dex_file_;
610 }
611
612 private:
613 // One of possibly several TryBoundary instructions entering the block's try.
614 // Only set for try blocks.
615 const HTryBoundary* try_entry_;
616
617 // Exception type information. Only set for catch blocks.
618 const DexFile* catch_dex_file_;
619 const uint16_t catch_type_index_;
620};
621
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100622static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100623static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100624
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625// A block in a method. Contains the list of instructions represented
626// as a double linked list. Each block knows its predecessors and
627// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100628
Vladimir Markof9f64412015-09-02 14:05:49 +0100629class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000630 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100631 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000633 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
634 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635 loop_information_(nullptr),
636 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000637 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100638 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100639 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000641 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000642 try_catch_information_(nullptr) {
643 predecessors_.reserve(kDefaultNumberOfPredecessors);
644 successors_.reserve(kDefaultNumberOfSuccessors);
645 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
646 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000647
Vladimir Marko60584552015-09-03 13:35:12 +0000648 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650 }
651
Vladimir Marko60584552015-09-03 13:35:12 +0000652 HBasicBlock* GetPredecessor(size_t pred_idx) const {
653 DCHECK_LT(pred_idx, predecessors_.size());
654 return predecessors_[pred_idx];
655 }
656
657 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659 }
660
Vladimir Marko60584552015-09-03 13:35:12 +0000661 HBasicBlock* GetSuccessor(size_t succ_idx) const {
662 DCHECK_LT(succ_idx, successors_.size());
663 return successors_[succ_idx];
664 }
665
666 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
667 return ContainsElement(successors_, block, start_from);
668 }
669
670 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100671 return dominated_blocks_;
672 }
673
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100674 bool IsEntryBlock() const {
675 return graph_->GetEntryBlock() == this;
676 }
677
678 bool IsExitBlock() const {
679 return graph_->GetExitBlock() == this;
680 }
681
David Brazdil46e2a392015-03-16 17:31:52 +0000682 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000683 bool IsSingleTryBoundary() const;
684
685 // Returns true if this block emits nothing but a jump.
686 bool IsSingleJump() const {
687 HLoopInformation* loop_info = GetLoopInformation();
688 return (IsSingleGoto() || IsSingleTryBoundary())
689 // Back edges generate a suspend check.
690 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
691 }
David Brazdil46e2a392015-03-16 17:31:52 +0000692
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 void AddBackEdge(HBasicBlock* back_edge) {
694 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000695 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698 loop_information_->AddBackEdge(back_edge);
699 }
700
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000702 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 int GetBlockId() const { return block_id_; }
705 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HBasicBlock* GetDominator() const { return dominator_; }
708 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000709 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
710
711 void RemoveDominatedBlock(HBasicBlock* block) {
712 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100713 }
Vladimir Marko60584552015-09-03 13:35:12 +0000714
715 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
716 ReplaceElement(dominated_blocks_, existing, new_block);
717 }
718
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100719 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000720
721 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100722 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723 }
724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
726 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100727 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100728 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100729 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
730 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000731
732 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000733 successors_.push_back(block);
734 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000735 }
736
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100737 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
738 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100739 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000740 new_block->predecessors_.push_back(this);
741 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000742 }
743
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000744 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000746 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->successors_.push_back(this);
748 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000749 }
750
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100751 // Insert `this` between `predecessor` and `successor. This method
752 // preserves the indicies, and will update the first edge found between
753 // `predecessor` and `successor`.
754 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
755 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100756 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000757 successor->predecessors_[predecessor_index] = this;
758 predecessor->successors_[successor_index] = this;
759 successors_.push_back(successor);
760 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100761 }
762
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100763 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000764 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 }
766
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000768 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 }
770
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000772 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 }
774
775 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000776 predecessors_.push_back(block);
777 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 }
779
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100780 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000781 DCHECK_EQ(predecessors_.size(), 2u);
782 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100783 }
784
David Brazdil769c9e52015-04-27 13:54:09 +0100785 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000786 DCHECK_EQ(successors_.size(), 2u);
787 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100788 }
789
David Brazdilfc6a86a2015-06-26 10:33:45 +0000790 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000791 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100792 }
793
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000795 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100796 }
797
David Brazdilfc6a86a2015-06-26 10:33:45 +0000798 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000799 DCHECK_EQ(GetPredecessors().size(), 1u);
800 return GetPredecessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 }
802
803 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000804 DCHECK_EQ(GetSuccessors().size(), 1u);
805 return GetSuccessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806 }
807
808 // Returns whether the first occurrence of `predecessor` in the list of
809 // predecessors is at index `idx`.
810 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetPredecessor(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000812 return GetPredecessorIndexOf(predecessor) == idx;
813 }
814
David Brazdilffee3d32015-07-06 11:48:53 +0100815 // Returns the number of non-exceptional successors. SsaChecker ensures that
816 // these are stored at the beginning of the successor list.
817 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000818 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100819 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820
821 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100822 // created, latter block. Note that this method will add the block to the
823 // graph, create a Goto at the end of the former block and will create an edge
824 // between the blocks. It will not, however, update the reverse post order or
825 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000826 HBasicBlock* SplitBefore(HInstruction* cursor);
827
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000828 // Split the block into two blocks just after `cursor`. Returns the newly
829 // created block. Note that this method just updates raw block information,
830 // like predecessors, successors, dominators, and instruction list. It does not
831 // update the graph, reverse post order, loop information, nor make sure the
832 // blocks are consistent (for example ending with a control flow instruction).
833 HBasicBlock* SplitAfter(HInstruction* cursor);
834
835 // Merge `other` at the end of `this`. Successors and dominated blocks of
836 // `other` are changed to be successors and dominated blocks of `this`. Note
837 // that this method does not update the graph, reverse post order, loop
838 // information, nor make sure the blocks are consistent (for example ending
839 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100840 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000841
842 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
843 // of `this` are moved to `other`.
844 // Note that this method does not update the graph, reverse post order, loop
845 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000846 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 void ReplaceWith(HBasicBlock* other);
848
David Brazdil2d7352b2015-04-20 14:52:42 +0100849 // Merge `other` at the end of `this`. This method updates loops, reverse post
850 // order, links to predecessors, successors, dominators and deletes the block
851 // from the graph. The two blocks must be successive, i.e. `this` the only
852 // predecessor of `other` and vice versa.
853 void MergeWith(HBasicBlock* other);
854
855 // Disconnects `this` from all its predecessors, successors and dominator,
856 // removes it from all loops it is included in and eventually from the graph.
857 // The block must not dominate any other block. Predecessors and successors
858 // are safely updated.
859 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000860
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000861 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100862 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100863 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100864 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100865 // Replace instruction `initial` with `replacement` within this block.
866 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
867 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100868 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100869 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000870 // RemoveInstruction and RemovePhi delete a given instruction from the respective
871 // instruction list. With 'ensure_safety' set to true, it verifies that the
872 // instruction is not in use and removes it from the use lists of its inputs.
873 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
874 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100875 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100876
877 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100878 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879 }
880
Roland Levillain6b879dd2014-09-22 17:13:44 +0100881 bool IsLoopPreHeaderFirstPredecessor() const {
882 DCHECK(IsLoopHeader());
Vladimir Marko60584552015-09-03 13:35:12 +0000883 return GetPredecessor(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100884 }
885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 HLoopInformation* GetLoopInformation() const {
887 return loop_information_;
888 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000890 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100891 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000892 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100893 void SetInLoop(HLoopInformation* info) {
894 if (IsLoopHeader()) {
895 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100896 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100897 loop_information_ = info;
898 } else if (loop_information_->Contains(*info->GetHeader())) {
899 // Block is currently part of an outer loop. Make it part of this inner loop.
900 // Note that a non loop header having a loop information means this loop information
901 // has already been populated
902 loop_information_ = info;
903 } else {
904 // Block is part of an inner loop. Do not update the loop information.
905 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
906 // at this point, because this method is being called while populating `info`.
907 }
908 }
909
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000910 // Raw update of the loop information.
911 void SetLoopInformation(HLoopInformation* info) {
912 loop_information_ = info;
913 }
914
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100915 bool IsInLoop() const { return loop_information_ != nullptr; }
916
David Brazdilec16f792015-08-19 15:04:01 +0100917 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
918
919 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
920 try_catch_information_ = try_catch_information;
921 }
922
923 bool IsTryBlock() const {
924 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
925 }
926
927 bool IsCatchBlock() const {
928 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
929 }
David Brazdilffee3d32015-07-06 11:48:53 +0100930
931 // Returns the try entry that this block's successors should have. They will
932 // be in the same try, unless the block ends in a try boundary. In that case,
933 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100934 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100935
David Brazdila4b8c212015-05-07 09:59:30 +0100936 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100937 bool Dominates(HBasicBlock* block) const;
938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939 size_t GetLifetimeStart() const { return lifetime_start_; }
940 size_t GetLifetimeEnd() const { return lifetime_end_; }
941
942 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
943 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
944
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100945 uint32_t GetDexPc() const { return dex_pc_; }
946
David Brazdil8d5b8b22015-03-24 10:51:52 +0000947 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000948 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100949 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000950 bool HasSinglePhi() const;
951
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000953 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000954 ArenaVector<HBasicBlock*> predecessors_;
955 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100956 HInstructionList instructions_;
957 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000958 HLoopInformation* loop_information_;
959 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000960 ArenaVector<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000961 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100962 // The dex program counter of the first instruction of this block.
963 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100964 size_t lifetime_start_;
965 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100966 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100967
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000968 friend class HGraph;
969 friend class HInstruction;
970
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
972};
973
David Brazdilb2bd1c52015-03-25 11:17:37 +0000974// Iterates over the LoopInformation of all loops which contain 'block'
975// from the innermost to the outermost.
976class HLoopInformationOutwardIterator : public ValueObject {
977 public:
978 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
979 : current_(block.GetLoopInformation()) {}
980
981 bool Done() const { return current_ == nullptr; }
982
983 void Advance() {
984 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100985 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000986 }
987
988 HLoopInformation* Current() const {
989 DCHECK(!Done());
990 return current_;
991 }
992
993 private:
994 HLoopInformation* current_;
995
996 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
997};
998
Alexandre Ramesef20f712015-06-09 10:29:30 +0100999#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001000 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001001 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(ArrayGet, Instruction) \
1003 M(ArrayLength, Instruction) \
1004 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001005 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001006 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001007 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001008 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001009 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001010 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001011 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001012 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001013 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001014 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001015 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001016 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001019 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001020 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(FloatConstant, Constant) \
1022 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001023 M(GreaterThan, Condition) \
1024 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001025 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(InstanceFieldGet, Instruction) \
1027 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001028 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001030 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001031 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001033 M(LessThan, Condition) \
1034 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001035 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001036 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001037 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001038 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001039 M(Local, Instruction) \
1040 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001041 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001042 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(Mul, BinaryOperation) \
1044 M(Neg, UnaryOperation) \
1045 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001047 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001048 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001049 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001050 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001051 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001052 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001053 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001054 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001055 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(Return, Instruction) \
1057 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001058 M(Shl, BinaryOperation) \
1059 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001060 M(StaticFieldGet, Instruction) \
1061 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(StoreLocal, Instruction) \
1063 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001064 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001065 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001066 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001067 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001068 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001069 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001070 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001071
Alexandre Ramesef20f712015-06-09 10:29:30 +01001072#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1073
1074#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1075
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001076#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1077
Alexandre Ramesef20f712015-06-09 10:29:30 +01001078#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1079
1080#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1081
1082#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1083 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1084 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1085 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001086 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001087 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1088 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1089
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001090#define FOR_EACH_INSTRUCTION(M) \
1091 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1092 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001093 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001094 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001095 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001096
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001097#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001098FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1099#undef FORWARD_DECLARATION
1100
Roland Levillainccc07a92014-09-16 14:48:16 +01001101#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001102 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1103 const char* DebugName() const OVERRIDE { return #type; } \
1104 const H##type* As##type() const OVERRIDE { return this; } \
1105 H##type* As##type() OVERRIDE { return this; } \
1106 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001107 return other->Is##type(); \
1108 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001109 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001110
David Brazdiled596192015-01-23 10:39:45 +00001111template <typename T> class HUseList;
1112
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001114class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001115 public:
David Brazdiled596192015-01-23 10:39:45 +00001116 HUseListNode* GetPrevious() const { return prev_; }
1117 HUseListNode* GetNext() const { return next_; }
1118 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001119 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001120 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001122 private:
David Brazdiled596192015-01-23 10:39:45 +00001123 HUseListNode(T user, size_t index)
1124 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1125
1126 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001127 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001128 HUseListNode<T>* prev_;
1129 HUseListNode<T>* next_;
1130
1131 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001132
1133 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1134};
1135
David Brazdiled596192015-01-23 10:39:45 +00001136template <typename T>
1137class HUseList : public ValueObject {
1138 public:
1139 HUseList() : first_(nullptr) {}
1140
1141 void Clear() {
1142 first_ = nullptr;
1143 }
1144
1145 // Adds a new entry at the beginning of the use list and returns
1146 // the newly created node.
1147 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001148 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001149 if (IsEmpty()) {
1150 first_ = new_node;
1151 } else {
1152 first_->prev_ = new_node;
1153 new_node->next_ = first_;
1154 first_ = new_node;
1155 }
1156 return new_node;
1157 }
1158
1159 HUseListNode<T>* GetFirst() const {
1160 return first_;
1161 }
1162
1163 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001164 DCHECK(node != nullptr);
1165 DCHECK(Contains(node));
1166
David Brazdiled596192015-01-23 10:39:45 +00001167 if (node->prev_ != nullptr) {
1168 node->prev_->next_ = node->next_;
1169 }
1170 if (node->next_ != nullptr) {
1171 node->next_->prev_ = node->prev_;
1172 }
1173 if (node == first_) {
1174 first_ = node->next_;
1175 }
1176 }
1177
David Brazdil1abb4192015-02-17 18:33:36 +00001178 bool Contains(const HUseListNode<T>* node) const {
1179 if (node == nullptr) {
1180 return false;
1181 }
1182 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1183 if (current == node) {
1184 return true;
1185 }
1186 }
1187 return false;
1188 }
1189
David Brazdiled596192015-01-23 10:39:45 +00001190 bool IsEmpty() const {
1191 return first_ == nullptr;
1192 }
1193
1194 bool HasOnlyOneUse() const {
1195 return first_ != nullptr && first_->next_ == nullptr;
1196 }
1197
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001198 size_t SizeSlow() const {
1199 size_t count = 0;
1200 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1201 ++count;
1202 }
1203 return count;
1204 }
1205
David Brazdiled596192015-01-23 10:39:45 +00001206 private:
1207 HUseListNode<T>* first_;
1208};
1209
1210template<typename T>
1211class HUseIterator : public ValueObject {
1212 public:
1213 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1214
1215 bool Done() const { return current_ == nullptr; }
1216
1217 void Advance() {
1218 DCHECK(!Done());
1219 current_ = current_->GetNext();
1220 }
1221
1222 HUseListNode<T>* Current() const {
1223 DCHECK(!Done());
1224 return current_;
1225 }
1226
1227 private:
1228 HUseListNode<T>* current_;
1229
1230 friend class HValue;
1231};
1232
David Brazdil1abb4192015-02-17 18:33:36 +00001233// This class is used by HEnvironment and HInstruction classes to record the
1234// instructions they use and pointers to the corresponding HUseListNodes kept
1235// by the used instructions.
1236template <typename T>
1237class HUserRecord : public ValueObject {
1238 public:
1239 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1240 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1241
1242 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1243 : instruction_(old_record.instruction_), use_node_(use_node) {
1244 DCHECK(instruction_ != nullptr);
1245 DCHECK(use_node_ != nullptr);
1246 DCHECK(old_record.use_node_ == nullptr);
1247 }
1248
1249 HInstruction* GetInstruction() const { return instruction_; }
1250 HUseListNode<T>* GetUseNode() const { return use_node_; }
1251
1252 private:
1253 // Instruction used by the user.
1254 HInstruction* instruction_;
1255
1256 // Corresponding entry in the use list kept by 'instruction_'.
1257 HUseListNode<T>* use_node_;
1258};
1259
Aart Bik854a02b2015-07-14 16:07:00 -07001260/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001261 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001262 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001263 * For write/read dependences on fields/arrays, the dependence analysis uses
1264 * type disambiguation (e.g. a float field write cannot modify the value of an
1265 * integer field read) and the access type (e.g. a reference array write cannot
1266 * modify the value of a reference field read [although it may modify the
1267 * reference fetch prior to reading the field, which is represented by its own
1268 * write/read dependence]). The analysis makes conservative points-to
1269 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1270 * the same, and any reference read depends on any reference read without
1271 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001272 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001273 * The internal representation uses 38-bit and is described in the table below.
1274 * The first line indicates the side effect, and for field/array accesses the
1275 * second line indicates the type of the access (in the order of the
1276 * Primitive::Type enum).
1277 * The two numbered lines below indicate the bit position in the bitfield (read
1278 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001279 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001280 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1281 * +-------------+---------+---------+--------------+---------+---------+
1282 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1283 * | 3 |333333322|222222221| 1 |111111110|000000000|
1284 * | 7 |654321098|765432109| 8 |765432109|876543210|
1285 *
1286 * Note that, to ease the implementation, 'changes' bits are least significant
1287 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001288 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001289class SideEffects : public ValueObject {
1290 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001291 SideEffects() : flags_(0) {}
1292
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293 static SideEffects None() {
1294 return SideEffects(0);
1295 }
1296
1297 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001298 return SideEffects(kAllChangeBits | kAllDependOnBits);
1299 }
1300
1301 static SideEffects AllChanges() {
1302 return SideEffects(kAllChangeBits);
1303 }
1304
1305 static SideEffects AllDependencies() {
1306 return SideEffects(kAllDependOnBits);
1307 }
1308
1309 static SideEffects AllExceptGCDependency() {
1310 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1311 }
1312
1313 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001314 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001315 }
1316
Aart Bik34c3ba92015-07-20 14:08:59 -07001317 static SideEffects AllWrites() {
1318 return SideEffects(kAllWrites);
1319 }
1320
1321 static SideEffects AllReads() {
1322 return SideEffects(kAllReads);
1323 }
1324
1325 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1326 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001327 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001328 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 }
1330
Aart Bik854a02b2015-07-14 16:07:00 -07001331 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1332 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001333 }
1334
Aart Bik34c3ba92015-07-20 14:08:59 -07001335 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1336 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001337 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001338 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001339 }
1340
1341 static SideEffects ArrayReadOfType(Primitive::Type type) {
1342 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1343 }
1344
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001345 static SideEffects CanTriggerGC() {
1346 return SideEffects(1ULL << kCanTriggerGCBit);
1347 }
1348
1349 static SideEffects DependsOnGC() {
1350 return SideEffects(1ULL << kDependsOnGCBit);
1351 }
1352
Aart Bik854a02b2015-07-14 16:07:00 -07001353 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001354 SideEffects Union(SideEffects other) const {
1355 return SideEffects(flags_ | other.flags_);
1356 }
1357
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001358 SideEffects Exclusion(SideEffects other) const {
1359 return SideEffects(flags_ & ~other.flags_);
1360 }
1361
1362 bool Includes(SideEffects other) const {
1363 return (other.flags_ & flags_) == other.flags_;
1364 }
1365
1366 bool HasSideEffects() const {
1367 return (flags_ & kAllChangeBits);
1368 }
1369
1370 bool HasDependencies() const {
1371 return (flags_ & kAllDependOnBits);
1372 }
1373
1374 // Returns true if there are no side effects or dependencies.
1375 bool DoesNothing() const {
1376 return flags_ == 0;
1377 }
1378
Aart Bik854a02b2015-07-14 16:07:00 -07001379 // Returns true if something is written.
1380 bool DoesAnyWrite() const {
1381 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001382 }
1383
Aart Bik854a02b2015-07-14 16:07:00 -07001384 // Returns true if something is read.
1385 bool DoesAnyRead() const {
1386 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001387 }
1388
Aart Bik854a02b2015-07-14 16:07:00 -07001389 // Returns true if potentially everything is written and read
1390 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001391 bool DoesAllReadWrite() const {
1392 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1393 }
1394
Aart Bik854a02b2015-07-14 16:07:00 -07001395 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001396 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001397 }
1398
1399 // Returns true if this may read something written by other.
1400 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001401 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1402 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001403 }
1404
1405 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001407 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001408 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001409 for (int s = kLastBit; s >= 0; s--) {
1410 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1411 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1412 // This is a bit for the GC side effect.
1413 if (current_bit_is_set) {
1414 flags += "GC";
1415 }
Aart Bik854a02b2015-07-14 16:07:00 -07001416 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 } else {
1418 // This is a bit for the array/field analysis.
1419 // The underscore character stands for the 'can trigger GC' bit.
1420 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1421 if (current_bit_is_set) {
1422 flags += kDebug[s];
1423 }
1424 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1425 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1426 flags += "|";
1427 }
1428 }
Aart Bik854a02b2015-07-14 16:07:00 -07001429 }
1430 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001431 }
1432
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001433 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001435 private:
1436 static constexpr int kFieldArrayAnalysisBits = 9;
1437
1438 static constexpr int kFieldWriteOffset = 0;
1439 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1440 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1441 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1442
1443 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1444
1445 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1446 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1447 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1448 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1449
1450 static constexpr int kLastBit = kDependsOnGCBit;
1451 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1452
1453 // Aliases.
1454
1455 static_assert(kChangeBits == kDependOnBits,
1456 "the 'change' bits should match the 'depend on' bits.");
1457
1458 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1459 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1460 static constexpr uint64_t kAllWrites =
1461 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1462 static constexpr uint64_t kAllReads =
1463 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001464
Aart Bik854a02b2015-07-14 16:07:00 -07001465 // Work around the fact that HIR aliases I/F and J/D.
1466 // TODO: remove this interceptor once HIR types are clean
1467 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1468 switch (type) {
1469 case Primitive::kPrimInt:
1470 case Primitive::kPrimFloat:
1471 return TypeFlag(Primitive::kPrimInt, offset) |
1472 TypeFlag(Primitive::kPrimFloat, offset);
1473 case Primitive::kPrimLong:
1474 case Primitive::kPrimDouble:
1475 return TypeFlag(Primitive::kPrimLong, offset) |
1476 TypeFlag(Primitive::kPrimDouble, offset);
1477 default:
1478 return TypeFlag(type, offset);
1479 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001480 }
1481
Aart Bik854a02b2015-07-14 16:07:00 -07001482 // Translates type to bit flag.
1483 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1484 CHECK_NE(type, Primitive::kPrimVoid);
1485 const uint64_t one = 1;
1486 const int shift = type; // 0-based consecutive enum
1487 DCHECK_LE(kFieldWriteOffset, shift);
1488 DCHECK_LT(shift, kArrayWriteOffset);
1489 return one << (type + offset);
1490 }
1491
1492 // Private constructor on direct flags value.
1493 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1494
1495 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496};
1497
David Brazdiled596192015-01-23 10:39:45 +00001498// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001499class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001500 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001501 HEnvironment(ArenaAllocator* arena,
1502 size_t number_of_vregs,
1503 const DexFile& dex_file,
1504 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001505 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001506 InvokeType invoke_type,
1507 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001508 : vregs_(arena, number_of_vregs),
1509 locations_(arena, number_of_vregs),
1510 parent_(nullptr),
1511 dex_file_(dex_file),
1512 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001513 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001514 invoke_type_(invoke_type),
1515 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001516 vregs_.SetSize(number_of_vregs);
1517 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001518 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001519 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001520
1521 locations_.SetSize(number_of_vregs);
1522 for (size_t i = 0; i < number_of_vregs; ++i) {
1523 locations_.Put(i, Location());
1524 }
1525 }
1526
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001527 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001528 : HEnvironment(arena,
1529 to_copy.Size(),
1530 to_copy.GetDexFile(),
1531 to_copy.GetMethodIdx(),
1532 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001533 to_copy.GetInvokeType(),
1534 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001535
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001536 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001537 if (parent_ != nullptr) {
1538 parent_->SetAndCopyParentChain(allocator, parent);
1539 } else {
1540 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1541 parent_->CopyFrom(parent);
1542 if (parent->GetParent() != nullptr) {
1543 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1544 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001545 }
David Brazdiled596192015-01-23 10:39:45 +00001546 }
1547
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001548 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1549 void CopyFrom(HEnvironment* environment);
1550
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001551 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1552 // input to the loop phi instead. This is for inserting instructions that
1553 // require an environment (like HDeoptimization) in the loop pre-header.
1554 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001555
1556 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001557 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001558 }
1559
1560 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001561 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001562 }
1563
David Brazdil1abb4192015-02-17 18:33:36 +00001564 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001565
1566 size_t Size() const { return vregs_.Size(); }
1567
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001568 HEnvironment* GetParent() const { return parent_; }
1569
1570 void SetLocationAt(size_t index, Location location) {
1571 locations_.Put(index, location);
1572 }
1573
1574 Location GetLocationAt(size_t index) const {
1575 return locations_.Get(index);
1576 }
1577
1578 uint32_t GetDexPc() const {
1579 return dex_pc_;
1580 }
1581
1582 uint32_t GetMethodIdx() const {
1583 return method_idx_;
1584 }
1585
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001586 InvokeType GetInvokeType() const {
1587 return invoke_type_;
1588 }
1589
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001590 const DexFile& GetDexFile() const {
1591 return dex_file_;
1592 }
1593
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001594 HInstruction* GetHolder() const {
1595 return holder_;
1596 }
1597
David Brazdiled596192015-01-23 10:39:45 +00001598 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001599 // Record instructions' use entries of this environment for constant-time removal.
1600 // It should only be called by HInstruction when a new environment use is added.
1601 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1602 DCHECK(env_use->GetUser() == this);
1603 size_t index = env_use->GetIndex();
1604 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1605 }
David Brazdiled596192015-01-23 10:39:45 +00001606
David Brazdil1abb4192015-02-17 18:33:36 +00001607 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001608 GrowableArray<Location> locations_;
1609 HEnvironment* parent_;
1610 const DexFile& dex_file_;
1611 const uint32_t method_idx_;
1612 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001613 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001614
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001615 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001616 HInstruction* const holder_;
1617
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001618 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001619
1620 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1621};
1622
Calin Juravleacf735c2015-02-12 15:25:22 +00001623class ReferenceTypeInfo : ValueObject {
1624 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001625 typedef Handle<mirror::Class> TypeHandle;
1626
Calin Juravle2e768302015-07-28 14:41:11 +00001627 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1628 // The constructor will check that the type_handle is valid.
1629 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001630 }
1631
Calin Juravle2e768302015-07-28 14:41:11 +00001632 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1633
1634 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1635 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001636 }
1637
Calin Juravle2e768302015-07-28 14:41:11 +00001638 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1639 return IsValidHandle(type_handle_);
1640 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001641 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001642
1643 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1644 DCHECK(IsValid());
1645 return GetTypeHandle()->IsObjectClass();
1646 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001647 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001648 DCHECK(IsValid());
1649 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001650 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001651
1652 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1653
Mathieu Chartier90443472015-07-16 20:32:27 -07001654 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001655 DCHECK(IsValid());
1656 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001657 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1658 }
1659
1660 // Returns true if the type information provide the same amount of details.
1661 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001662 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001663 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001664 if (!IsValid() && !rti.IsValid()) {
1665 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001666 return true;
1667 }
Calin Juravle2e768302015-07-28 14:41:11 +00001668 if (!IsValid() || !rti.IsValid()) {
1669 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001670 return false;
1671 }
Calin Juravle2e768302015-07-28 14:41:11 +00001672 return IsExact() == rti.IsExact()
1673 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001674 }
1675
1676 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001677 ReferenceTypeInfo();
1678 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001679
Calin Juravleacf735c2015-02-12 15:25:22 +00001680 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001681 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001682 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001683 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001684 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001685};
1686
1687std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1688
Vladimir Markof9f64412015-09-02 14:05:49 +01001689class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001690 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001691 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001692 : previous_(nullptr),
1693 next_(nullptr),
1694 block_(nullptr),
1695 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001696 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001697 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001698 locations_(nullptr),
1699 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001700 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001701 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001702 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001703
Dave Allison20dfc792014-06-16 20:44:29 -07001704 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001705
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001706#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001707 enum InstructionKind {
1708 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1709 };
1710#undef DECLARE_KIND
1711
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001712 HInstruction* GetNext() const { return next_; }
1713 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001714
Calin Juravle77520bc2015-01-12 18:45:46 +00001715 HInstruction* GetNextDisregardingMoves() const;
1716 HInstruction* GetPreviousDisregardingMoves() const;
1717
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001718 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001719 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001720 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001721 bool IsInBlock() const { return block_ != nullptr; }
1722 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001723 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001724
Roland Levillain6b879dd2014-09-22 17:13:44 +01001725 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001726 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001727
1728 virtual void Accept(HGraphVisitor* visitor) = 0;
1729 virtual const char* DebugName() const = 0;
1730
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001731 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001732 void SetRawInputAt(size_t index, HInstruction* input) {
1733 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1734 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001735
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001736 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001737 virtual uint32_t GetDexPc() const {
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +06001738 return kNoDexPc;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001740 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001741
Roland Levillaine161a2a2014-10-03 12:45:18 +01001742 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001743 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001744
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001745 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001746 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001747
Calin Juravle10e244f2015-01-26 18:54:32 +00001748 // Does not apply for all instructions, but having this at top level greatly
1749 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001750 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001751 virtual bool CanBeNull() const {
1752 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1753 return true;
1754 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001755
Calin Juravle641547a2015-04-21 22:08:51 +01001756 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1757 UNUSED(obj);
1758 return false;
1759 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001760
Calin Juravle2e768302015-07-28 14:41:11 +00001761 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001762
Calin Juravle61d544b2015-02-23 16:46:57 +00001763 ReferenceTypeInfo GetReferenceTypeInfo() const {
1764 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1765 return reference_type_info_;
1766 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001767
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001768 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001769 DCHECK(user != nullptr);
1770 HUseListNode<HInstruction*>* use =
1771 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1772 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001773 }
1774
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001775 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001776 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001777 HUseListNode<HEnvironment*>* env_use =
1778 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1779 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001780 }
1781
David Brazdil1abb4192015-02-17 18:33:36 +00001782 void RemoveAsUserOfInput(size_t input) {
1783 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1784 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1785 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786
David Brazdil1abb4192015-02-17 18:33:36 +00001787 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1788 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001789
David Brazdiled596192015-01-23 10:39:45 +00001790 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1791 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001792 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001793 bool HasOnlyOneNonEnvironmentUse() const {
1794 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1795 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796
Roland Levillain6c82d402014-10-13 16:10:27 +01001797 // Does this instruction strictly dominate `other_instruction`?
1798 // Returns false if this instruction and `other_instruction` are the same.
1799 // Aborts if this instruction and `other_instruction` are both phis.
1800 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001801
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001802 int GetId() const { return id_; }
1803 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001804
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001805 int GetSsaIndex() const { return ssa_index_; }
1806 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1807 bool HasSsaIndex() const { return ssa_index_ != -1; }
1808
1809 bool HasEnvironment() const { return environment_ != nullptr; }
1810 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001811 // Set the `environment_` field. Raw because this method does not
1812 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001813 void SetRawEnvironment(HEnvironment* environment) {
1814 DCHECK(environment_ == nullptr);
1815 DCHECK_EQ(environment->GetHolder(), this);
1816 environment_ = environment;
1817 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001818
1819 // Set the environment of this instruction, copying it from `environment`. While
1820 // copying, the uses lists are being updated.
1821 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001822 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001823 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001824 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001825 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001826 if (environment->GetParent() != nullptr) {
1827 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1828 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001829 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001831 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1832 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001833 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001834 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001835 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001836 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 if (environment->GetParent() != nullptr) {
1838 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1839 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001840 }
1841
Nicolas Geoffray39468442014-09-02 15:17:15 +01001842 // Returns the number of entries in the environment. Typically, that is the
1843 // number of dex registers in a method. It could be more in case of inlining.
1844 size_t EnvironmentSize() const;
1845
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001846 LocationSummary* GetLocations() const { return locations_; }
1847 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001849 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001850 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851
Alexandre Rames188d4312015-04-09 18:30:21 +01001852 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1853 // uses of this instruction by `other` are *not* updated.
1854 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1855 ReplaceWith(other);
1856 other->ReplaceInput(this, use_index);
1857 }
1858
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001859 // Move `this` instruction before `cursor`.
1860 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001861
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001862#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001863 bool Is##type() const { return (As##type() != nullptr); } \
1864 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001865 virtual H##type* As##type() { return nullptr; }
1866
1867 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1868#undef INSTRUCTION_TYPE_CHECK
1869
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001870 // Returns whether the instruction can be moved within the graph.
1871 virtual bool CanBeMoved() const { return false; }
1872
1873 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001874 virtual bool InstructionTypeEquals(HInstruction* other) const {
1875 UNUSED(other);
1876 return false;
1877 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001878
1879 // Returns whether any data encoded in the two instructions is equal.
1880 // This method does not look at the inputs. Both instructions must be
1881 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001882 virtual bool InstructionDataEquals(HInstruction* other) const {
1883 UNUSED(other);
1884 return false;
1885 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001886
1887 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001888 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001889 // 2) Their inputs are identical.
1890 bool Equals(HInstruction* other) const;
1891
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001892 virtual InstructionKind GetKind() const = 0;
1893
1894 virtual size_t ComputeHashCode() const {
1895 size_t result = GetKind();
1896 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1897 result = (result * 31) + InputAt(i)->GetId();
1898 }
1899 return result;
1900 }
1901
1902 SideEffects GetSideEffects() const { return side_effects_; }
1903
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001904 size_t GetLifetimePosition() const { return lifetime_position_; }
1905 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1906 LiveInterval* GetLiveInterval() const { return live_interval_; }
1907 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1908 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1909
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001910 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1911
1912 // Returns whether the code generation of the instruction will require to have access
1913 // to the current method. Such instructions are:
1914 // (1): Instructions that require an environment, as calling the runtime requires
1915 // to walk the stack and have the current method stored at a specific stack address.
1916 // (2): Object literals like classes and strings, that are loaded from the dex cache
1917 // fields of the current method.
1918 bool NeedsCurrentMethod() const {
1919 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1920 }
1921
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001922 virtual bool NeedsDexCache() const { return false; }
1923
Mark Mendellc4701932015-04-10 13:18:51 -04001924 // Does this instruction have any use in an environment before
1925 // control flow hits 'other'?
1926 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1927
1928 // Remove all references to environment uses of this instruction.
1929 // The caller must ensure that this is safe to do.
1930 void RemoveEnvironmentUsers();
1931
David Brazdil1abb4192015-02-17 18:33:36 +00001932 protected:
1933 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1934 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1935
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001936 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001937 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1938
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001939 HInstruction* previous_;
1940 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001941 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001942
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943 // An instruction gets an id when it is added to the graph.
1944 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001945 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946 int id_;
1947
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001948 // When doing liveness analysis, instructions that have uses get an SSA index.
1949 int ssa_index_;
1950
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001951 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001952 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001953
1954 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001955 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001956
Nicolas Geoffray39468442014-09-02 15:17:15 +01001957 // The environment associated with this instruction. Not null if the instruction
1958 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001960
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961 // Set by the code generator.
1962 LocationSummary* locations_;
1963
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001964 // Set by the liveness analysis.
1965 LiveInterval* live_interval_;
1966
1967 // Set by the liveness analysis, this is the position in a linear
1968 // order of blocks where this instruction's live interval start.
1969 size_t lifetime_position_;
1970
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001971 const SideEffects side_effects_;
1972
Calin Juravleacf735c2015-02-12 15:25:22 +00001973 // TODO: for primitive types this should be marked as invalid.
1974 ReferenceTypeInfo reference_type_info_;
1975
David Brazdil1abb4192015-02-17 18:33:36 +00001976 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001977 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001978 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001979 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001980 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001981
1982 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1983};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001984std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001986class HInputIterator : public ValueObject {
1987 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001988 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001989
1990 bool Done() const { return index_ == instruction_->InputCount(); }
1991 HInstruction* Current() const { return instruction_->InputAt(index_); }
1992 void Advance() { index_++; }
1993
1994 private:
1995 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001997
1998 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1999};
2000
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002001class HInstructionIterator : public ValueObject {
2002 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003 explicit HInstructionIterator(const HInstructionList& instructions)
2004 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002005 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006 }
2007
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002008 bool Done() const { return instruction_ == nullptr; }
2009 HInstruction* Current() const { return instruction_; }
2010 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002011 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002012 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 }
2014
2015 private:
2016 HInstruction* instruction_;
2017 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002018
2019 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020};
2021
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002022class HBackwardInstructionIterator : public ValueObject {
2023 public:
2024 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2025 : instruction_(instructions.last_instruction_) {
2026 next_ = Done() ? nullptr : instruction_->GetPrevious();
2027 }
2028
2029 bool Done() const { return instruction_ == nullptr; }
2030 HInstruction* Current() const { return instruction_; }
2031 void Advance() {
2032 instruction_ = next_;
2033 next_ = Done() ? nullptr : instruction_->GetPrevious();
2034 }
2035
2036 private:
2037 HInstruction* instruction_;
2038 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002039
2040 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002041};
2042
Vladimir Markof9f64412015-09-02 14:05:49 +01002043template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002044class HTemplateInstruction: public HInstruction {
2045 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002046 HTemplateInstruction<N>(SideEffects side_effects)
2047 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002048 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002049
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002050 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002052 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002053 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2054 DCHECK_LT(i, N);
2055 return inputs_[i];
2056 }
David Brazdil1abb4192015-02-17 18:33:36 +00002057
2058 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002059 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002060 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002061 }
2062
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002063 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002064 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002065
2066 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002067};
2068
Vladimir Markof9f64412015-09-02 14:05:49 +01002069// HTemplateInstruction specialization for N=0.
2070template<>
2071class HTemplateInstruction<0>: public HInstruction {
2072 public:
2073 explicit HTemplateInstruction(SideEffects side_effects) : HInstruction(side_effects) {}
2074 virtual ~HTemplateInstruction() {}
2075
2076 size_t InputCount() const OVERRIDE { return 0; }
2077
2078 protected:
2079 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2080 LOG(FATAL) << "Unreachable";
2081 UNREACHABLE();
2082 }
2083
2084 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2085 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2086 LOG(FATAL) << "Unreachable";
2087 UNREACHABLE();
2088 }
2089
2090 private:
2091 friend class SsaBuilder;
2092};
2093
Dave Allison20dfc792014-06-16 20:44:29 -07002094template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002095class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002096 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002097 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2098 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002099 virtual ~HExpression() {}
2100
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002101 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002102
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002103 protected:
2104 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002105};
2106
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002107// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2108// instruction that branches to the exit block.
2109class HReturnVoid : public HTemplateInstruction<0> {
2110 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002111 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002113 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002115 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002116
2117 private:
2118 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2119};
2120
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121// Represents dex's RETURN opcodes. A HReturn is a control flow
2122// instruction that branches to the exit block.
2123class HReturn : public HTemplateInstruction<1> {
2124 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002126 SetRawInputAt(0, value);
2127 }
2128
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002129 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002130
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002131 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002132
2133 private:
2134 DISALLOW_COPY_AND_ASSIGN(HReturn);
2135};
2136
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002137// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002138// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002139// exit block.
2140class HExit : public HTemplateInstruction<0> {
2141 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002142 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002143
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002144 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002146 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147
2148 private:
2149 DISALLOW_COPY_AND_ASSIGN(HExit);
2150};
2151
2152// Jumps from one block to another.
2153class HGoto : public HTemplateInstruction<0> {
2154 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002155 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2156
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002157 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002158
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002159 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002160 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002161 }
2162
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002163 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
2165 private:
2166 DISALLOW_COPY_AND_ASSIGN(HGoto);
2167};
2168
Roland Levillain9867bc72015-08-05 10:21:34 +01002169class HConstant : public HExpression<0> {
2170 public:
2171 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2172
2173 bool CanBeMoved() const OVERRIDE { return true; }
2174
2175 virtual bool IsMinusOne() const { return false; }
2176 virtual bool IsZero() const { return false; }
2177 virtual bool IsOne() const { return false; }
2178
2179 DECLARE_INSTRUCTION(Constant);
2180
2181 private:
2182 DISALLOW_COPY_AND_ASSIGN(HConstant);
2183};
2184
2185class HNullConstant : public HConstant {
2186 public:
2187 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2188 return true;
2189 }
2190
2191 size_t ComputeHashCode() const OVERRIDE { return 0; }
2192
2193 DECLARE_INSTRUCTION(NullConstant);
2194
2195 private:
2196 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2197
2198 friend class HGraph;
2199 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2200};
2201
2202// Constants of the type int. Those can be from Dex instructions, or
2203// synthesized (for example with the if-eqz instruction).
2204class HIntConstant : public HConstant {
2205 public:
2206 int32_t GetValue() const { return value_; }
2207
2208 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2209 DCHECK(other->IsIntConstant());
2210 return other->AsIntConstant()->value_ == value_;
2211 }
2212
2213 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2214
2215 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2216 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2217 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2218
2219 DECLARE_INSTRUCTION(IntConstant);
2220
2221 private:
2222 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2223 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2224
2225 const int32_t value_;
2226
2227 friend class HGraph;
2228 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2229 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2230 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2231};
2232
2233class HLongConstant : public HConstant {
2234 public:
2235 int64_t GetValue() const { return value_; }
2236
2237 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2238 DCHECK(other->IsLongConstant());
2239 return other->AsLongConstant()->value_ == value_;
2240 }
2241
2242 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2243
2244 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2245 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2246 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2247
2248 DECLARE_INSTRUCTION(LongConstant);
2249
2250 private:
2251 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2252
2253 const int64_t value_;
2254
2255 friend class HGraph;
2256 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2257};
Dave Allison20dfc792014-06-16 20:44:29 -07002258
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002259// Conditional branch. A block ending with an HIf instruction must have
2260// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002261class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002262 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002263 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002264 SetRawInputAt(0, input);
2265 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002266
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002267 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002269 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002270 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002271 }
2272
2273 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002274 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002275 }
2276
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002277 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002278
2279 private:
2280 DISALLOW_COPY_AND_ASSIGN(HIf);
2281};
2282
David Brazdilfc6a86a2015-06-26 10:33:45 +00002283
2284// Abstract instruction which marks the beginning and/or end of a try block and
2285// links it to the respective exception handlers. Behaves the same as a Goto in
2286// non-exceptional control flow.
2287// Normal-flow successor is stored at index zero, exception handlers under
2288// higher indices in no particular order.
2289class HTryBoundary : public HTemplateInstruction<0> {
2290 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002291 enum BoundaryKind {
2292 kEntry,
2293 kExit,
2294 };
2295
2296 explicit HTryBoundary(BoundaryKind kind)
2297 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002298
2299 bool IsControlFlow() const OVERRIDE { return true; }
2300
2301 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002302 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002303
2304 // Returns whether `handler` is among its exception handlers (non-zero index
2305 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002306 bool HasExceptionHandler(const HBasicBlock& handler) const {
2307 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002308 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002309 }
2310
2311 // If not present already, adds `handler` to its block's list of exception
2312 // handlers.
2313 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002314 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002315 GetBlock()->AddSuccessor(handler);
2316 }
2317 }
2318
David Brazdil56e1acc2015-06-30 15:41:36 +01002319 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002320
David Brazdilffee3d32015-07-06 11:48:53 +01002321 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2322
David Brazdilfc6a86a2015-06-26 10:33:45 +00002323 DECLARE_INSTRUCTION(TryBoundary);
2324
2325 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002326 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002327
2328 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2329};
2330
David Brazdilffee3d32015-07-06 11:48:53 +01002331// Iterator over exception handlers of a given HTryBoundary, i.e. over
2332// exceptional successors of its basic block.
2333class HExceptionHandlerIterator : public ValueObject {
2334 public:
2335 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2336 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2337
Vladimir Marko60584552015-09-03 13:35:12 +00002338 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2339 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002340 size_t CurrentSuccessorIndex() const { return index_; }
2341 void Advance() { ++index_; }
2342
2343 private:
2344 const HBasicBlock& block_;
2345 size_t index_;
2346
2347 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2348};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002349
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002350// Deoptimize to interpreter, upon checking a condition.
2351class HDeoptimize : public HTemplateInstruction<1> {
2352 public:
2353 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2354 : HTemplateInstruction(SideEffects::None()),
2355 dex_pc_(dex_pc) {
2356 SetRawInputAt(0, cond);
2357 }
2358
2359 bool NeedsEnvironment() const OVERRIDE { return true; }
2360 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002361 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002362
2363 DECLARE_INSTRUCTION(Deoptimize);
2364
2365 private:
2366 uint32_t dex_pc_;
2367
2368 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2369};
2370
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002371// Represents the ArtMethod that was passed as a first argument to
2372// the method. It is used by instructions that depend on it, like
2373// instructions that work with the dex cache.
2374class HCurrentMethod : public HExpression<0> {
2375 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002376 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002377
2378 DECLARE_INSTRUCTION(CurrentMethod);
2379
2380 private:
2381 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2382};
2383
Roland Levillain88cb1752014-10-20 16:36:47 +01002384class HUnaryOperation : public HExpression<1> {
2385 public:
2386 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2387 : HExpression(result_type, SideEffects::None()) {
2388 SetRawInputAt(0, input);
2389 }
2390
2391 HInstruction* GetInput() const { return InputAt(0); }
2392 Primitive::Type GetResultType() const { return GetType(); }
2393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002394 bool CanBeMoved() const OVERRIDE { return true; }
2395 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002396 UNUSED(other);
2397 return true;
2398 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002399
Roland Levillain9240d6a2014-10-20 16:47:04 +01002400 // Try to statically evaluate `operation` and return a HConstant
2401 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002402 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002403 HConstant* TryStaticEvaluation() const;
2404
2405 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002406 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2407 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002408
Roland Levillain88cb1752014-10-20 16:36:47 +01002409 DECLARE_INSTRUCTION(UnaryOperation);
2410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2413};
2414
Dave Allison20dfc792014-06-16 20:44:29 -07002415class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002416 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002417 HBinaryOperation(Primitive::Type result_type,
2418 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002419 HInstruction* right,
2420 SideEffects side_effects = SideEffects::None())
2421 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002422 SetRawInputAt(0, left);
2423 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002424 }
2425
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002426 HInstruction* GetLeft() const { return InputAt(0); }
2427 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002428 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002429
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002430 virtual bool IsCommutative() const { return false; }
2431
2432 // Put constant on the right.
2433 // Returns whether order is changed.
2434 bool OrderInputsWithConstantOnTheRight() {
2435 HInstruction* left = InputAt(0);
2436 HInstruction* right = InputAt(1);
2437 if (left->IsConstant() && !right->IsConstant()) {
2438 ReplaceInput(right, 0);
2439 ReplaceInput(left, 1);
2440 return true;
2441 }
2442 return false;
2443 }
2444
2445 // Order inputs by instruction id, but favor constant on the right side.
2446 // This helps GVN for commutative ops.
2447 void OrderInputs() {
2448 DCHECK(IsCommutative());
2449 HInstruction* left = InputAt(0);
2450 HInstruction* right = InputAt(1);
2451 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2452 return;
2453 }
2454 if (OrderInputsWithConstantOnTheRight()) {
2455 return;
2456 }
2457 // Order according to instruction id.
2458 if (left->GetId() > right->GetId()) {
2459 ReplaceInput(right, 0);
2460 ReplaceInput(left, 1);
2461 }
2462 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002463
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002464 bool CanBeMoved() const OVERRIDE { return true; }
2465 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002466 UNUSED(other);
2467 return true;
2468 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002469
Roland Levillain9240d6a2014-10-20 16:47:04 +01002470 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002471 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002472 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002473 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002474
2475 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002476 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2477 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2478 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2479 HLongConstant* y ATTRIBUTE_UNUSED) const {
2480 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2481 return nullptr;
2482 }
2483 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2484 HIntConstant* y ATTRIBUTE_UNUSED) const {
2485 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2486 return nullptr;
2487 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002488
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002489 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002490 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002491 HConstant* GetConstantRight() const;
2492
2493 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002494 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002495 HInstruction* GetLeastConstantLeft() const;
2496
Roland Levillainccc07a92014-09-16 14:48:16 +01002497 DECLARE_INSTRUCTION(BinaryOperation);
2498
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002499 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002500 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2501};
2502
Mark Mendellc4701932015-04-10 13:18:51 -04002503// The comparison bias applies for floating point operations and indicates how NaN
2504// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002505enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002506 kNoBias, // bias is not applicable (i.e. for long operation)
2507 kGtBias, // return 1 for NaN comparisons
2508 kLtBias, // return -1 for NaN comparisons
2509};
2510
Dave Allison20dfc792014-06-16 20:44:29 -07002511class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002512 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002513 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002514 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002515 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002516 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002517
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002518 bool NeedsMaterialization() const { return needs_materialization_; }
2519 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002520
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002521 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002522 // `instruction`, and disregard moves in between.
2523 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002524
Dave Allison20dfc792014-06-16 20:44:29 -07002525 DECLARE_INSTRUCTION(Condition);
2526
2527 virtual IfCondition GetCondition() const = 0;
2528
Mark Mendellc4701932015-04-10 13:18:51 -04002529 virtual IfCondition GetOppositeCondition() const = 0;
2530
Roland Levillain4fa13f62015-07-06 18:11:54 +01002531 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002532
2533 void SetBias(ComparisonBias bias) { bias_ = bias; }
2534
2535 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2536 return bias_ == other->AsCondition()->bias_;
2537 }
2538
Roland Levillain4fa13f62015-07-06 18:11:54 +01002539 bool IsFPConditionTrueIfNaN() const {
2540 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2541 IfCondition if_cond = GetCondition();
2542 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2543 }
2544
2545 bool IsFPConditionFalseIfNaN() const {
2546 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2547 IfCondition if_cond = GetCondition();
2548 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2549 }
2550
Dave Allison20dfc792014-06-16 20:44:29 -07002551 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002552 // For register allocation purposes, returns whether this instruction needs to be
2553 // materialized (that is, not just be in the processor flags).
2554 bool needs_materialization_;
2555
Mark Mendellc4701932015-04-10 13:18:51 -04002556 // Needed if we merge a HCompare into a HCondition.
2557 ComparisonBias bias_;
2558
Dave Allison20dfc792014-06-16 20:44:29 -07002559 DISALLOW_COPY_AND_ASSIGN(HCondition);
2560};
2561
2562// Instruction to check if two inputs are equal to each other.
2563class HEqual : public HCondition {
2564 public:
2565 HEqual(HInstruction* first, HInstruction* second)
2566 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002567
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002568 bool IsCommutative() const OVERRIDE { return true; }
2569
Roland Levillain9867bc72015-08-05 10:21:34 +01002570 template <typename T> bool Compute(T x, T y) const { return x == y; }
2571
2572 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2573 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002574 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2576 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002577 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002578
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002579 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002580
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002581 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002582 return kCondEQ;
2583 }
2584
Mark Mendellc4701932015-04-10 13:18:51 -04002585 IfCondition GetOppositeCondition() const OVERRIDE {
2586 return kCondNE;
2587 }
2588
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002589 private:
2590 DISALLOW_COPY_AND_ASSIGN(HEqual);
2591};
2592
Dave Allison20dfc792014-06-16 20:44:29 -07002593class HNotEqual : public HCondition {
2594 public:
2595 HNotEqual(HInstruction* first, HInstruction* second)
2596 : HCondition(first, second) {}
2597
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002598 bool IsCommutative() const OVERRIDE { return true; }
2599
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 template <typename T> bool Compute(T x, T y) const { return x != y; }
2601
2602 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2603 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002604 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002605 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2606 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002607 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002608
Dave Allison20dfc792014-06-16 20:44:29 -07002609 DECLARE_INSTRUCTION(NotEqual);
2610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002611 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002612 return kCondNE;
2613 }
2614
Mark Mendellc4701932015-04-10 13:18:51 -04002615 IfCondition GetOppositeCondition() const OVERRIDE {
2616 return kCondEQ;
2617 }
2618
Dave Allison20dfc792014-06-16 20:44:29 -07002619 private:
2620 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2621};
2622
2623class HLessThan : public HCondition {
2624 public:
2625 HLessThan(HInstruction* first, HInstruction* second)
2626 : HCondition(first, second) {}
2627
Roland Levillain9867bc72015-08-05 10:21:34 +01002628 template <typename T> bool Compute(T x, T y) const { return x < y; }
2629
2630 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2631 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002632 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2634 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002635 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002636
Dave Allison20dfc792014-06-16 20:44:29 -07002637 DECLARE_INSTRUCTION(LessThan);
2638
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002639 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002640 return kCondLT;
2641 }
2642
Mark Mendellc4701932015-04-10 13:18:51 -04002643 IfCondition GetOppositeCondition() const OVERRIDE {
2644 return kCondGE;
2645 }
2646
Dave Allison20dfc792014-06-16 20:44:29 -07002647 private:
2648 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2649};
2650
2651class HLessThanOrEqual : public HCondition {
2652 public:
2653 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2654 : HCondition(first, second) {}
2655
Roland Levillain9867bc72015-08-05 10:21:34 +01002656 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2657
2658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2659 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002660 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002661 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2662 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002663 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002664
Dave Allison20dfc792014-06-16 20:44:29 -07002665 DECLARE_INSTRUCTION(LessThanOrEqual);
2666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002667 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002668 return kCondLE;
2669 }
2670
Mark Mendellc4701932015-04-10 13:18:51 -04002671 IfCondition GetOppositeCondition() const OVERRIDE {
2672 return kCondGT;
2673 }
2674
Dave Allison20dfc792014-06-16 20:44:29 -07002675 private:
2676 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2677};
2678
2679class HGreaterThan : public HCondition {
2680 public:
2681 HGreaterThan(HInstruction* first, HInstruction* second)
2682 : HCondition(first, second) {}
2683
Roland Levillain9867bc72015-08-05 10:21:34 +01002684 template <typename T> bool Compute(T x, T y) const { return x > y; }
2685
2686 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2687 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002688 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2690 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002691 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002692
Dave Allison20dfc792014-06-16 20:44:29 -07002693 DECLARE_INSTRUCTION(GreaterThan);
2694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002695 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002696 return kCondGT;
2697 }
2698
Mark Mendellc4701932015-04-10 13:18:51 -04002699 IfCondition GetOppositeCondition() const OVERRIDE {
2700 return kCondLE;
2701 }
2702
Dave Allison20dfc792014-06-16 20:44:29 -07002703 private:
2704 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2705};
2706
2707class HGreaterThanOrEqual : public HCondition {
2708 public:
2709 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2710 : HCondition(first, second) {}
2711
Roland Levillain9867bc72015-08-05 10:21:34 +01002712 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2713
2714 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2715 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002716 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2718 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002719 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002720
Dave Allison20dfc792014-06-16 20:44:29 -07002721 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2722
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002723 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002724 return kCondGE;
2725 }
2726
Mark Mendellc4701932015-04-10 13:18:51 -04002727 IfCondition GetOppositeCondition() const OVERRIDE {
2728 return kCondLT;
2729 }
2730
Dave Allison20dfc792014-06-16 20:44:29 -07002731 private:
2732 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2733};
2734
2735
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002736// Instruction to check how two inputs compare to each other.
2737// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2738class HCompare : public HBinaryOperation {
2739 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002740 HCompare(Primitive::Type type,
2741 HInstruction* first,
2742 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002743 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002744 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002745 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2746 bias_(bias),
2747 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002748 DCHECK_EQ(type, first->GetType());
2749 DCHECK_EQ(type, second->GetType());
2750 }
2751
Roland Levillain9867bc72015-08-05 10:21:34 +01002752 template <typename T>
2753 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002754
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2756 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2757 }
2758 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2759 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002760 }
2761
Calin Juravleddb7df22014-11-25 20:56:51 +00002762 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2763 return bias_ == other->AsCompare()->bias_;
2764 }
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766 ComparisonBias GetBias() const { return bias_; }
2767
Roland Levillain4fa13f62015-07-06 18:11:54 +01002768 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002769
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002770 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2771
2772 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2773 // MIPS64 uses a runtime call for FP comparisons.
2774 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2775 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002776
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002777 DECLARE_INSTRUCTION(Compare);
2778
2779 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002780 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002781 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002782
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002783 DISALLOW_COPY_AND_ASSIGN(HCompare);
2784};
2785
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002786// A local in the graph. Corresponds to a Dex register.
2787class HLocal : public HTemplateInstruction<0> {
2788 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002789 explicit HLocal(uint16_t reg_number)
2790 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002791
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002792 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002793
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002794 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002795
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002796 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002797 // The Dex register number.
2798 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002799
2800 DISALLOW_COPY_AND_ASSIGN(HLocal);
2801};
2802
2803// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002804class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002805 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002806 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002807 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002808 SetRawInputAt(0, local);
2809 }
2810
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002811 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2812
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002813 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002814
2815 private:
2816 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2817};
2818
2819// Store a value in a given local. This instruction has two inputs: the value
2820// and the local.
2821class HStoreLocal : public HTemplateInstruction<2> {
2822 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002823 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002824 SetRawInputAt(0, local);
2825 SetRawInputAt(1, value);
2826 }
2827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002828 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002830 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002831
2832 private:
2833 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2834};
2835
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002836class HFloatConstant : public HConstant {
2837 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002838 float GetValue() const { return value_; }
2839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002840 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002841 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002842 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2843 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002844 }
2845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002847
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002848 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002849 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002850 }
2851 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002852 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002853 }
2854 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002855 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2856 }
2857 bool IsNaN() const {
2858 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002859 }
2860
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002861 DECLARE_INSTRUCTION(FloatConstant);
2862
2863 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002864 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002865 explicit HFloatConstant(int32_t value)
2866 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002867
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002868 const float value_;
2869
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002870 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002871 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002872 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002873 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2874};
2875
2876class HDoubleConstant : public HConstant {
2877 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878 double GetValue() const { return value_; }
2879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002880 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002881 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002882 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2883 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002884 }
2885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002886 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002887
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002888 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002889 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002890 }
2891 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002892 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002893 }
2894 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002895 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2896 }
2897 bool IsNaN() const {
2898 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002899 }
2900
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901 DECLARE_INSTRUCTION(DoubleConstant);
2902
2903 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002904 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002905 explicit HDoubleConstant(int64_t value)
2906 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002907
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002908 const double value_;
2909
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002910 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002911 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002912 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002913 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2914};
2915
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002916enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002917#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002918#include "intrinsics_list.h"
2919 kNone,
2920 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2921#undef INTRINSICS_LIST
2922#undef OPTIMIZING_INTRINSICS
2923};
2924std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2925
Agi Csaki05f20562015-08-19 14:58:14 -07002926enum IntrinsicNeedsEnvironmentOrCache {
2927 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2928 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002929};
2930
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002931class HInvoke : public HInstruction {
2932 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002933 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002934
2935 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2936 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002937 bool NeedsEnvironment() const OVERRIDE {
2938 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2939 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002940
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002941 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002942 SetRawInputAt(index, argument);
2943 }
2944
Roland Levillain3e3d7332015-04-28 11:00:54 +01002945 // Return the number of arguments. This number can be lower than
2946 // the number of inputs returned by InputCount(), as some invoke
2947 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2948 // inputs at the end of their list of inputs.
2949 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002951 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002952
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002953 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002954
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002955 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002956 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002957
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002958 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2959
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002960 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002961 return intrinsic_;
2962 }
2963
Agi Csaki05f20562015-08-19 14:58:14 -07002964 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002965 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07002966 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002967 }
2968
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002969 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002970 return GetEnvironment()->GetParent() != nullptr;
2971 }
2972
2973 bool CanThrow() const OVERRIDE { return true; }
2974
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002975 DECLARE_INSTRUCTION(Invoke);
2976
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002977 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002978 HInvoke(ArenaAllocator* arena,
2979 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002980 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002981 Primitive::Type return_type,
2982 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002983 uint32_t dex_method_index,
2984 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002985 : HInstruction(
2986 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002987 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002988 inputs_(arena, number_of_arguments),
2989 return_type_(return_type),
2990 dex_pc_(dex_pc),
2991 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002992 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07002993 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07002994 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002995 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2996 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002997 }
2998
David Brazdil1abb4192015-02-17 18:33:36 +00002999 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3000 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3001 inputs_.Put(index, input);
3002 }
3003
Roland Levillain3e3d7332015-04-28 11:00:54 +01003004 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003005 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003006 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003007 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003008 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003009 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003010 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003011 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003012
3013 private:
3014 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3015};
3016
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003017class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003018 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003019 // Requirements of this method call regarding the class
3020 // initialization (clinit) check of its declaring class.
3021 enum class ClinitCheckRequirement {
3022 kNone, // Class already initialized.
3023 kExplicit, // Static call having explicit clinit check as last input.
3024 kImplicit, // Static call implicitly requiring a clinit check.
3025 };
3026
Vladimir Marko58155012015-08-19 12:49:41 +00003027 // Determines how to load the target ArtMethod*.
3028 enum class MethodLoadKind {
3029 // Use a String init ArtMethod* loaded from Thread entrypoints.
3030 kStringInit,
3031
3032 // Use the method's own ArtMethod* loaded by the register allocator.
3033 kRecursive,
3034
3035 // Use ArtMethod* at a known address, embed the direct address in the code.
3036 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3037 kDirectAddress,
3038
3039 // Use ArtMethod* at an address that will be known at link time, embed the direct
3040 // address in the code. If the image is relocatable, emit .patch_oat entry.
3041 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3042 // the image relocatable or not.
3043 kDirectAddressWithFixup,
3044
3045 // Load from resoved methods array in the dex cache using a PC-relative load.
3046 // Used when we need to use the dex cache, for example for invoke-static that
3047 // may cause class initialization (the entry may point to a resolution method),
3048 // and we know that we can access the dex cache arrays using a PC-relative load.
3049 kDexCachePcRelative,
3050
3051 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3052 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3053 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3054 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3055 kDexCacheViaMethod,
3056 };
3057
3058 // Determines the location of the code pointer.
3059 enum class CodePtrLocation {
3060 // Recursive call, use local PC-relative call instruction.
3061 kCallSelf,
3062
3063 // Use PC-relative call instruction patched at link time.
3064 // Used for calls within an oat file, boot->boot or app->app.
3065 kCallPCRelative,
3066
3067 // Call to a known target address, embed the direct address in code.
3068 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3069 kCallDirect,
3070
3071 // Call to a target address that will be known at link time, embed the direct
3072 // address in code. If the image is relocatable, emit .patch_oat entry.
3073 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3074 // the image relocatable or not.
3075 kCallDirectWithFixup,
3076
3077 // Use code pointer from the ArtMethod*.
3078 // Used when we don't know the target code. This is also the last-resort-kind used when
3079 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3080 kCallArtMethod,
3081 };
3082
3083 struct DispatchInfo {
3084 const MethodLoadKind method_load_kind;
3085 const CodePtrLocation code_ptr_location;
3086 // The method load data holds
3087 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3088 // Note that there are multiple string init methods, each having its own offset.
3089 // - the method address for kDirectAddress
3090 // - the dex cache arrays offset for kDexCachePcRel.
3091 const uint64_t method_load_data;
3092 const uint64_t direct_code_ptr;
3093 };
3094
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003095 HInvokeStaticOrDirect(ArenaAllocator* arena,
3096 uint32_t number_of_arguments,
3097 Primitive::Type return_type,
3098 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003099 uint32_t method_index,
3100 MethodReference target_method,
3101 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003102 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003103 InvokeType invoke_type,
3104 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003105 : HInvoke(arena,
3106 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003107 // There is one extra argument for the HCurrentMethod node, and
3108 // potentially one other if the clinit check is explicit, and one other
3109 // if the method is a string factory.
3110 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003111 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003112 return_type,
3113 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003114 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003115 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003116 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003117 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003118 target_method_(target_method),
3119 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003120
Calin Juravle641547a2015-04-21 22:08:51 +01003121 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3122 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003123 // We access the method via the dex cache so we can't do an implicit null check.
3124 // TODO: for intrinsics we can generate implicit null checks.
3125 return false;
3126 }
3127
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003128 bool CanBeNull() const OVERRIDE {
3129 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3130 }
3131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003132 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003133 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3134 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3135 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003136 bool NeedsDexCache() const OVERRIDE {
3137 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3138 return !IsRecursive() && !IsStringInit();
3139 }
Vladimir Marko58155012015-08-19 12:49:41 +00003140 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003141 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003142 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3143 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3144 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3145 MethodReference GetTargetMethod() const { return target_method_; }
3146
3147 int32_t GetStringInitOffset() const {
3148 DCHECK(IsStringInit());
3149 return dispatch_info_.method_load_data;
3150 }
3151
3152 uint64_t GetMethodAddress() const {
3153 DCHECK(HasMethodAddress());
3154 return dispatch_info_.method_load_data;
3155 }
3156
3157 uint32_t GetDexCacheArrayOffset() const {
3158 DCHECK(HasPcRelDexCache());
3159 return dispatch_info_.method_load_data;
3160 }
3161
3162 uint64_t GetDirectCodePtr() const {
3163 DCHECK(HasDirectCodePtr());
3164 return dispatch_info_.direct_code_ptr;
3165 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003166
Calin Juravle68ad6492015-08-18 17:08:12 +01003167 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3168
Roland Levillain4c0eb422015-04-24 16:43:49 +01003169 // Is this instruction a call to a static method?
3170 bool IsStatic() const {
3171 return GetInvokeType() == kStatic;
3172 }
3173
Roland Levillain3e3d7332015-04-28 11:00:54 +01003174 // Remove the art::HLoadClass instruction set as last input by
3175 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3176 // the initial art::HClinitCheck instruction (only relevant for
3177 // static calls with explicit clinit check).
3178 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003179 DCHECK(IsStaticWithExplicitClinitCheck());
3180 size_t last_input_index = InputCount() - 1;
3181 HInstruction* last_input = InputAt(last_input_index);
3182 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003183 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003184 RemoveAsUserOfInput(last_input_index);
3185 inputs_.DeleteAt(last_input_index);
3186 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3187 DCHECK(IsStaticWithImplicitClinitCheck());
3188 }
3189
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003190 bool IsStringFactoryFor(HFakeString* str) const {
3191 if (!IsStringInit()) return false;
3192 // +1 for the current method.
3193 if (InputCount() == (number_of_arguments_ + 1)) return false;
3194 return InputAt(InputCount() - 1)->AsFakeString() == str;
3195 }
3196
3197 void RemoveFakeStringArgumentAsLastInput() {
3198 DCHECK(IsStringInit());
3199 size_t last_input_index = InputCount() - 1;
3200 HInstruction* last_input = InputAt(last_input_index);
3201 DCHECK(last_input != nullptr);
3202 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3203 RemoveAsUserOfInput(last_input_index);
3204 inputs_.DeleteAt(last_input_index);
3205 }
3206
Roland Levillain4c0eb422015-04-24 16:43:49 +01003207 // Is this a call to a static method whose declaring class has an
3208 // explicit intialization check in the graph?
3209 bool IsStaticWithExplicitClinitCheck() const {
3210 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3211 }
3212
3213 // Is this a call to a static method whose declaring class has an
3214 // implicit intialization check requirement?
3215 bool IsStaticWithImplicitClinitCheck() const {
3216 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3217 }
3218
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003219 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003220
Roland Levillain4c0eb422015-04-24 16:43:49 +01003221 protected:
3222 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3223 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3224 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3225 HInstruction* input = input_record.GetInstruction();
3226 // `input` is the last input of a static invoke marked as having
3227 // an explicit clinit check. It must either be:
3228 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3229 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3230 DCHECK(input != nullptr);
3231 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3232 }
3233 return input_record;
3234 }
3235
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003236 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003237 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003238 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003239 // The target method may refer to different dex file or method index than the original
3240 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3241 // in derived class to increase visibility.
3242 MethodReference target_method_;
3243 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003245 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003246};
3247
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003248class HInvokeVirtual : public HInvoke {
3249 public:
3250 HInvokeVirtual(ArenaAllocator* arena,
3251 uint32_t number_of_arguments,
3252 Primitive::Type return_type,
3253 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003254 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003255 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003256 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003257 vtable_index_(vtable_index) {}
3258
Calin Juravle641547a2015-04-21 22:08:51 +01003259 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003260 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003261 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003262 }
3263
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003264 uint32_t GetVTableIndex() const { return vtable_index_; }
3265
3266 DECLARE_INSTRUCTION(InvokeVirtual);
3267
3268 private:
3269 const uint32_t vtable_index_;
3270
3271 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3272};
3273
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003274class HInvokeInterface : public HInvoke {
3275 public:
3276 HInvokeInterface(ArenaAllocator* arena,
3277 uint32_t number_of_arguments,
3278 Primitive::Type return_type,
3279 uint32_t dex_pc,
3280 uint32_t dex_method_index,
3281 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003282 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003283 imt_index_(imt_index) {}
3284
Calin Juravle641547a2015-04-21 22:08:51 +01003285 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003286 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003287 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003288 }
3289
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003290 uint32_t GetImtIndex() const { return imt_index_; }
3291 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3292
3293 DECLARE_INSTRUCTION(InvokeInterface);
3294
3295 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003296 const uint32_t imt_index_;
3297
3298 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3299};
3300
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003301class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003302 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003303 HNewInstance(HCurrentMethod* current_method,
3304 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003305 uint16_t type_index,
3306 const DexFile& dex_file,
3307 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003308 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003309 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003310 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003311 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003312 entrypoint_(entrypoint) {
3313 SetRawInputAt(0, current_method);
3314 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003315
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003316 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003317 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003318 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003319
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003320 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003321 bool NeedsEnvironment() const OVERRIDE { return true; }
3322 // It may throw when called on:
3323 // - interfaces
3324 // - abstract/innaccessible/unknown classes
3325 // TODO: optimize when possible.
3326 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003327
Calin Juravle10e244f2015-01-26 18:54:32 +00003328 bool CanBeNull() const OVERRIDE { return false; }
3329
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003330 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3331
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003332 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003333
3334 private:
3335 const uint32_t dex_pc_;
3336 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003337 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003338 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003339
3340 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3341};
3342
Roland Levillain88cb1752014-10-20 16:36:47 +01003343class HNeg : public HUnaryOperation {
3344 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003345 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003346 : HUnaryOperation(result_type, input) {}
3347
Roland Levillain9867bc72015-08-05 10:21:34 +01003348 template <typename T> T Compute(T x) const { return -x; }
3349
3350 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3351 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3352 }
3353 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3354 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3355 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003356
Roland Levillain88cb1752014-10-20 16:36:47 +01003357 DECLARE_INSTRUCTION(Neg);
3358
3359 private:
3360 DISALLOW_COPY_AND_ASSIGN(HNeg);
3361};
3362
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003363class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003364 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003365 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003366 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003367 uint32_t dex_pc,
3368 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003369 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003370 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003371 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003372 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003373 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003374 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003375 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003376 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003377 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003378 }
3379
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003380 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003381 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003382 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003383
3384 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003385 bool NeedsEnvironment() const OVERRIDE { return true; }
3386
Mingyao Yang0c365e62015-03-31 15:09:29 -07003387 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3388 bool CanThrow() const OVERRIDE { return true; }
3389
Calin Juravle10e244f2015-01-26 18:54:32 +00003390 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003391
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003392 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3393
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003394 DECLARE_INSTRUCTION(NewArray);
3395
3396 private:
3397 const uint32_t dex_pc_;
3398 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003399 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003400 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003401
3402 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3403};
3404
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003405class HAdd : public HBinaryOperation {
3406 public:
3407 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3408 : HBinaryOperation(result_type, left, right) {}
3409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003410 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003411
Roland Levillain9867bc72015-08-05 10:21:34 +01003412 template <typename T> T Compute(T x, T y) const { return x + y; }
3413
3414 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3415 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003416 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003417 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3418 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003419 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003420
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003421 DECLARE_INSTRUCTION(Add);
3422
3423 private:
3424 DISALLOW_COPY_AND_ASSIGN(HAdd);
3425};
3426
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003427class HSub : public HBinaryOperation {
3428 public:
3429 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3430 : HBinaryOperation(result_type, left, right) {}
3431
Roland Levillain9867bc72015-08-05 10:21:34 +01003432 template <typename T> T Compute(T x, T y) const { return x - y; }
3433
3434 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3435 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003436 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003437 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3438 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003439 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003440
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003441 DECLARE_INSTRUCTION(Sub);
3442
3443 private:
3444 DISALLOW_COPY_AND_ASSIGN(HSub);
3445};
3446
Calin Juravle34bacdf2014-10-07 20:23:36 +01003447class HMul : public HBinaryOperation {
3448 public:
3449 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3450 : HBinaryOperation(result_type, left, right) {}
3451
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003452 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003453
Roland Levillain9867bc72015-08-05 10:21:34 +01003454 template <typename T> T Compute(T x, T y) const { return x * y; }
3455
3456 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3457 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3458 }
3459 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3460 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3461 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003462
3463 DECLARE_INSTRUCTION(Mul);
3464
3465 private:
3466 DISALLOW_COPY_AND_ASSIGN(HMul);
3467};
3468
Calin Juravle7c4954d2014-10-28 16:57:40 +00003469class HDiv : public HBinaryOperation {
3470 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003471 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003472 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3473 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003474
Roland Levillain9867bc72015-08-05 10:21:34 +01003475 template <typename T>
3476 T Compute(T x, T y) const {
3477 // Our graph structure ensures we never have 0 for `y` during
3478 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003479 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003480 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003481 return (y == -1) ? -x : x / y;
3482 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003483
Roland Levillain9867bc72015-08-05 10:21:34 +01003484 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3485 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3486 }
3487 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3488 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003489 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003490
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003491 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003492
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003493 static SideEffects SideEffectsForArchRuntimeCalls() {
3494 // The generated code can use a runtime call.
3495 return SideEffects::CanTriggerGC();
3496 }
3497
Calin Juravle7c4954d2014-10-28 16:57:40 +00003498 DECLARE_INSTRUCTION(Div);
3499
3500 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003501 const uint32_t dex_pc_;
3502
Calin Juravle7c4954d2014-10-28 16:57:40 +00003503 DISALLOW_COPY_AND_ASSIGN(HDiv);
3504};
3505
Calin Juravlebacfec32014-11-14 15:54:36 +00003506class HRem : public HBinaryOperation {
3507 public:
3508 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003509 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3510 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003511
Roland Levillain9867bc72015-08-05 10:21:34 +01003512 template <typename T>
3513 T Compute(T x, T y) const {
3514 // Our graph structure ensures we never have 0 for `y` during
3515 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003516 DCHECK_NE(y, 0);
3517 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3518 return (y == -1) ? 0 : x % y;
3519 }
3520
Roland Levillain9867bc72015-08-05 10:21:34 +01003521 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3522 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3523 }
3524 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3525 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003526 }
3527
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003528 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003529
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003530 static SideEffects SideEffectsForArchRuntimeCalls() {
3531 return SideEffects::CanTriggerGC();
3532 }
3533
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 DECLARE_INSTRUCTION(Rem);
3535
3536 private:
3537 const uint32_t dex_pc_;
3538
3539 DISALLOW_COPY_AND_ASSIGN(HRem);
3540};
3541
Calin Juravled0d48522014-11-04 16:40:20 +00003542class HDivZeroCheck : public HExpression<1> {
3543 public:
3544 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3545 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3546 SetRawInputAt(0, value);
3547 }
3548
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003549 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3550
Calin Juravled0d48522014-11-04 16:40:20 +00003551 bool CanBeMoved() const OVERRIDE { return true; }
3552
3553 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3554 UNUSED(other);
3555 return true;
3556 }
3557
3558 bool NeedsEnvironment() const OVERRIDE { return true; }
3559 bool CanThrow() const OVERRIDE { return true; }
3560
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003561 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003562
3563 DECLARE_INSTRUCTION(DivZeroCheck);
3564
3565 private:
3566 const uint32_t dex_pc_;
3567
3568 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3569};
3570
Calin Juravle9aec02f2014-11-18 23:06:35 +00003571class HShl : public HBinaryOperation {
3572 public:
3573 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3574 : HBinaryOperation(result_type, left, right) {}
3575
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 template <typename T, typename U, typename V>
3577 T Compute(T x, U y, V max_shift_value) const {
3578 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3579 "V is not the unsigned integer type corresponding to T");
3580 return x << (y & max_shift_value);
3581 }
3582
3583 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3584 return GetBlock()->GetGraph()->GetIntConstant(
3585 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3586 }
3587 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3588 // case is handled as `x << static_cast<int>(y)`.
3589 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3590 return GetBlock()->GetGraph()->GetLongConstant(
3591 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3592 }
3593 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3594 return GetBlock()->GetGraph()->GetLongConstant(
3595 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3596 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003597
3598 DECLARE_INSTRUCTION(Shl);
3599
3600 private:
3601 DISALLOW_COPY_AND_ASSIGN(HShl);
3602};
3603
3604class HShr : public HBinaryOperation {
3605 public:
3606 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3607 : HBinaryOperation(result_type, left, right) {}
3608
Roland Levillain9867bc72015-08-05 10:21:34 +01003609 template <typename T, typename U, typename V>
3610 T Compute(T x, U y, V max_shift_value) const {
3611 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3612 "V is not the unsigned integer type corresponding to T");
3613 return x >> (y & max_shift_value);
3614 }
3615
3616 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3617 return GetBlock()->GetGraph()->GetIntConstant(
3618 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3619 }
3620 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3621 // case is handled as `x >> static_cast<int>(y)`.
3622 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3623 return GetBlock()->GetGraph()->GetLongConstant(
3624 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3625 }
3626 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3627 return GetBlock()->GetGraph()->GetLongConstant(
3628 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3629 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003630
3631 DECLARE_INSTRUCTION(Shr);
3632
3633 private:
3634 DISALLOW_COPY_AND_ASSIGN(HShr);
3635};
3636
3637class HUShr : public HBinaryOperation {
3638 public:
3639 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3640 : HBinaryOperation(result_type, left, right) {}
3641
Roland Levillain9867bc72015-08-05 10:21:34 +01003642 template <typename T, typename U, typename V>
3643 T Compute(T x, U y, V max_shift_value) const {
3644 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3645 "V is not the unsigned integer type corresponding to T");
3646 V ux = static_cast<V>(x);
3647 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003648 }
3649
Roland Levillain9867bc72015-08-05 10:21:34 +01003650 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3651 return GetBlock()->GetGraph()->GetIntConstant(
3652 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3653 }
3654 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3655 // case is handled as `x >>> static_cast<int>(y)`.
3656 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3657 return GetBlock()->GetGraph()->GetLongConstant(
3658 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3659 }
3660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3661 return GetBlock()->GetGraph()->GetLongConstant(
3662 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663 }
3664
3665 DECLARE_INSTRUCTION(UShr);
3666
3667 private:
3668 DISALLOW_COPY_AND_ASSIGN(HUShr);
3669};
3670
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003671class HAnd : public HBinaryOperation {
3672 public:
3673 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3674 : HBinaryOperation(result_type, left, right) {}
3675
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003676 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003677
Roland Levillain9867bc72015-08-05 10:21:34 +01003678 template <typename T, typename U>
3679 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3680
3681 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3682 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3683 }
3684 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3685 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3686 }
3687 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3688 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3689 }
3690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3691 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3692 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003693
3694 DECLARE_INSTRUCTION(And);
3695
3696 private:
3697 DISALLOW_COPY_AND_ASSIGN(HAnd);
3698};
3699
3700class HOr : public HBinaryOperation {
3701 public:
3702 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3703 : HBinaryOperation(result_type, left, right) {}
3704
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003705 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003706
Roland Levillain9867bc72015-08-05 10:21:34 +01003707 template <typename T, typename U>
3708 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3709
3710 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3711 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3712 }
3713 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3714 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3715 }
3716 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3717 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3718 }
3719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3720 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3721 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003722
3723 DECLARE_INSTRUCTION(Or);
3724
3725 private:
3726 DISALLOW_COPY_AND_ASSIGN(HOr);
3727};
3728
3729class HXor : public HBinaryOperation {
3730 public:
3731 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3732 : HBinaryOperation(result_type, left, right) {}
3733
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003734 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003735
Roland Levillain9867bc72015-08-05 10:21:34 +01003736 template <typename T, typename U>
3737 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3738
3739 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3740 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3741 }
3742 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3743 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3744 }
3745 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3746 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3747 }
3748 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3749 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3750 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003751
3752 DECLARE_INSTRUCTION(Xor);
3753
3754 private:
3755 DISALLOW_COPY_AND_ASSIGN(HXor);
3756};
3757
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003758// The value of a parameter in this method. Its location depends on
3759// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003760class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003761 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003762 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003763 : HExpression(parameter_type, SideEffects::None()),
3764 index_(index),
3765 is_this_(is_this),
3766 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003767
3768 uint8_t GetIndex() const { return index_; }
3769
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003770 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3771 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003772
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003773 bool IsThis() const { return is_this_; }
3774
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003775 DECLARE_INSTRUCTION(ParameterValue);
3776
3777 private:
3778 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003779 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003780 const uint8_t index_;
3781
Calin Juravle10e244f2015-01-26 18:54:32 +00003782 // Whether or not the parameter value corresponds to 'this' argument.
3783 const bool is_this_;
3784
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003785 bool can_be_null_;
3786
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003787 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3788};
3789
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003790class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003791 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003792 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003793 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003795 bool CanBeMoved() const OVERRIDE { return true; }
3796 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003797 UNUSED(other);
3798 return true;
3799 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003800
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 template <typename T> T Compute(T x) const { return ~x; }
3802
3803 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3804 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3805 }
3806 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3807 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3808 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003809
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003810 DECLARE_INSTRUCTION(Not);
3811
3812 private:
3813 DISALLOW_COPY_AND_ASSIGN(HNot);
3814};
3815
David Brazdil66d126e2015-04-03 16:02:44 +01003816class HBooleanNot : public HUnaryOperation {
3817 public:
3818 explicit HBooleanNot(HInstruction* input)
3819 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3820
3821 bool CanBeMoved() const OVERRIDE { return true; }
3822 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3823 UNUSED(other);
3824 return true;
3825 }
3826
Roland Levillain9867bc72015-08-05 10:21:34 +01003827 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003828 DCHECK(IsUint<1>(x));
3829 return !x;
3830 }
3831
Roland Levillain9867bc72015-08-05 10:21:34 +01003832 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3833 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3834 }
3835 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3836 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003837 UNREACHABLE();
3838 }
3839
3840 DECLARE_INSTRUCTION(BooleanNot);
3841
3842 private:
3843 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3844};
3845
Roland Levillaindff1f282014-11-05 14:15:05 +00003846class HTypeConversion : public HExpression<1> {
3847 public:
3848 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003849 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003850 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3851 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003852 SetRawInputAt(0, input);
3853 DCHECK_NE(input->GetType(), result_type);
3854 }
3855
3856 HInstruction* GetInput() const { return InputAt(0); }
3857 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3858 Primitive::Type GetResultType() const { return GetType(); }
3859
Roland Levillain624279f2014-12-04 11:54:28 +00003860 // Required by the x86 and ARM code generators when producing calls
3861 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003862 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003863
Roland Levillaindff1f282014-11-05 14:15:05 +00003864 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003865 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003866
Mark Mendelle82549b2015-05-06 10:55:34 -04003867 // Try to statically evaluate the conversion and return a HConstant
3868 // containing the result. If the input cannot be converted, return nullptr.
3869 HConstant* TryStaticEvaluation() const;
3870
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003871 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3872 Primitive::Type result_type) {
3873 // Some architectures may not require the 'GC' side effects, but at this point
3874 // in the compilation process we do not know what architecture we will
3875 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003876 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3877 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003878 return SideEffects::CanTriggerGC();
3879 }
3880 return SideEffects::None();
3881 }
3882
Roland Levillaindff1f282014-11-05 14:15:05 +00003883 DECLARE_INSTRUCTION(TypeConversion);
3884
3885 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003886 const uint32_t dex_pc_;
3887
Roland Levillaindff1f282014-11-05 14:15:05 +00003888 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3889};
3890
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003891static constexpr uint32_t kNoRegNumber = -1;
3892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003893class HPhi : public HInstruction {
3894 public:
3895 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003896 : HInstruction(SideEffects::None()),
3897 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003898 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003899 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003900 is_live_(false),
3901 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003902 inputs_.SetSize(number_of_inputs);
3903 }
3904
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003905 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3906 static Primitive::Type ToPhiType(Primitive::Type type) {
3907 switch (type) {
3908 case Primitive::kPrimBoolean:
3909 case Primitive::kPrimByte:
3910 case Primitive::kPrimShort:
3911 case Primitive::kPrimChar:
3912 return Primitive::kPrimInt;
3913 default:
3914 return type;
3915 }
3916 }
3917
David Brazdilffee3d32015-07-06 11:48:53 +01003918 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3919
Calin Juravle10e244f2015-01-26 18:54:32 +00003920 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003921
3922 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003923 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003924
Calin Juravle10e244f2015-01-26 18:54:32 +00003925 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003926 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003927
Calin Juravle10e244f2015-01-26 18:54:32 +00003928 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3929 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3930
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003931 uint32_t GetRegNumber() const { return reg_number_; }
3932
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003933 void SetDead() { is_live_ = false; }
3934 void SetLive() { is_live_ = true; }
3935 bool IsDead() const { return !is_live_; }
3936 bool IsLive() const { return is_live_; }
3937
Calin Juravlea4f88312015-04-16 12:57:19 +01003938 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3939 // An equivalent phi is a phi having the same dex register and type.
3940 // It assumes that phis with the same dex register are adjacent.
3941 HPhi* GetNextEquivalentPhiWithSameType() {
3942 HInstruction* next = GetNext();
3943 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3944 if (next->GetType() == GetType()) {
3945 return next->AsPhi();
3946 }
3947 next = next->GetNext();
3948 }
3949 return nullptr;
3950 }
3951
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003952 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003953
David Brazdil1abb4192015-02-17 18:33:36 +00003954 protected:
3955 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3956
3957 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3958 inputs_.Put(index, input);
3959 }
3960
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003961 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003962 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003963 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003964 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003965 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003966 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003967
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003968 DISALLOW_COPY_AND_ASSIGN(HPhi);
3969};
3970
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971class HNullCheck : public HExpression<1> {
3972 public:
3973 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003974 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003975 SetRawInputAt(0, value);
3976 }
3977
Calin Juravle10e244f2015-01-26 18:54:32 +00003978 bool CanBeMoved() const OVERRIDE { return true; }
3979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003980 UNUSED(other);
3981 return true;
3982 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003983
Calin Juravle10e244f2015-01-26 18:54:32 +00003984 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003985
Calin Juravle10e244f2015-01-26 18:54:32 +00003986 bool CanThrow() const OVERRIDE { return true; }
3987
3988 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003989
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003990 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003991
3992 DECLARE_INSTRUCTION(NullCheck);
3993
3994 private:
3995 const uint32_t dex_pc_;
3996
3997 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3998};
3999
4000class FieldInfo : public ValueObject {
4001 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004002 FieldInfo(MemberOffset field_offset,
4003 Primitive::Type field_type,
4004 bool is_volatile,
4005 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004006 const DexFile& dex_file,
4007 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004008 : field_offset_(field_offset),
4009 field_type_(field_type),
4010 is_volatile_(is_volatile),
4011 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004012 dex_file_(dex_file),
4013 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004014
4015 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004016 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004017 uint32_t GetFieldIndex() const { return index_; }
4018 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004019 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004020 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004021
4022 private:
4023 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004024 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004025 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004026 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004027 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004028 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004029};
4030
4031class HInstanceFieldGet : public HExpression<1> {
4032 public:
4033 HInstanceFieldGet(HInstruction* value,
4034 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004035 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004036 bool is_volatile,
4037 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004038 const DexFile& dex_file,
4039 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004040 : HExpression(
4041 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004042 SideEffects::FieldReadOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004043 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004044 SetRawInputAt(0, value);
4045 }
4046
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004047 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004048
4049 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4050 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4051 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004052 }
4053
Calin Juravle641547a2015-04-21 22:08:51 +01004054 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4055 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004056 }
4057
4058 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004059 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4060 }
4061
Calin Juravle52c48962014-12-16 17:02:57 +00004062 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004063 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004064 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004065 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004066
4067 DECLARE_INSTRUCTION(InstanceFieldGet);
4068
4069 private:
4070 const FieldInfo field_info_;
4071
4072 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4073};
4074
4075class HInstanceFieldSet : public HTemplateInstruction<2> {
4076 public:
4077 HInstanceFieldSet(HInstruction* object,
4078 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004079 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004080 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004081 bool is_volatile,
4082 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004083 const DexFile& dex_file,
4084 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004085 : HTemplateInstruction(
4086 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004087 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004088 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004089 SetRawInputAt(0, object);
4090 SetRawInputAt(1, value);
4091 }
4092
Calin Juravle641547a2015-04-21 22:08:51 +01004093 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4094 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004095 }
4096
Calin Juravle52c48962014-12-16 17:02:57 +00004097 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004098 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004099 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004100 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004101 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004102 bool GetValueCanBeNull() const { return value_can_be_null_; }
4103 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004104
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004105 DECLARE_INSTRUCTION(InstanceFieldSet);
4106
4107 private:
4108 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004109 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004110
4111 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4112};
4113
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004114class HArrayGet : public HExpression<2> {
4115 public:
4116 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004117 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004118 SetRawInputAt(0, array);
4119 SetRawInputAt(1, index);
4120 }
4121
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004122 bool CanBeMoved() const OVERRIDE { return true; }
4123 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004124 UNUSED(other);
4125 return true;
4126 }
Calin Juravle641547a2015-04-21 22:08:51 +01004127 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4128 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004129 // TODO: We can be smarter here.
4130 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4131 // which generates the implicit null check. There are cases when these can be removed
4132 // to produce better code. If we ever add optimizations to do so we should allow an
4133 // implicit check here (as long as the address falls in the first page).
4134 return false;
4135 }
4136
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004137 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004138
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004139 HInstruction* GetArray() const { return InputAt(0); }
4140 HInstruction* GetIndex() const { return InputAt(1); }
4141
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004142 DECLARE_INSTRUCTION(ArrayGet);
4143
4144 private:
4145 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4146};
4147
4148class HArraySet : public HTemplateInstruction<3> {
4149 public:
4150 HArraySet(HInstruction* array,
4151 HInstruction* index,
4152 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004153 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004154 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004155 : HTemplateInstruction(
4156 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4157 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004158 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004159 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004160 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4161 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004162 SetRawInputAt(0, array);
4163 SetRawInputAt(1, index);
4164 SetRawInputAt(2, value);
4165 }
4166
Calin Juravle77520bc2015-01-12 18:45:46 +00004167 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 // We currently always call a runtime method to catch array store
4169 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004170 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171 }
4172
Mingyao Yang81014cb2015-06-02 03:16:27 -07004173 // Can throw ArrayStoreException.
4174 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4175
Calin Juravle641547a2015-04-21 22:08:51 +01004176 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4177 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004178 // TODO: Same as for ArrayGet.
4179 return false;
4180 }
4181
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004182 void ClearNeedsTypeCheck() {
4183 needs_type_check_ = false;
4184 }
4185
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004186 void ClearValueCanBeNull() {
4187 value_can_be_null_ = false;
4188 }
4189
4190 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004191 bool NeedsTypeCheck() const { return needs_type_check_; }
4192
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004193 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004194
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004195 HInstruction* GetArray() const { return InputAt(0); }
4196 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004197 HInstruction* GetValue() const { return InputAt(2); }
4198
4199 Primitive::Type GetComponentType() const {
4200 // The Dex format does not type floating point index operations. Since the
4201 // `expected_component_type_` is set during building and can therefore not
4202 // be correct, we also check what is the value type. If it is a floating
4203 // point type, we must use that type.
4204 Primitive::Type value_type = GetValue()->GetType();
4205 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4206 ? value_type
4207 : expected_component_type_;
4208 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004209
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004210 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4211 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4212 }
4213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 DECLARE_INSTRUCTION(ArraySet);
4215
4216 private:
4217 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004218 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004219 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004220 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004221
4222 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4223};
4224
4225class HArrayLength : public HExpression<1> {
4226 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004227 explicit HArrayLength(HInstruction* array)
4228 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4229 // Note that arrays do not change length, so the instruction does not
4230 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004231 SetRawInputAt(0, array);
4232 }
4233
Calin Juravle77520bc2015-01-12 18:45:46 +00004234 bool CanBeMoved() const OVERRIDE { return true; }
4235 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004236 UNUSED(other);
4237 return true;
4238 }
Calin Juravle641547a2015-04-21 22:08:51 +01004239 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4240 return obj == InputAt(0);
4241 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004242
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004243 DECLARE_INSTRUCTION(ArrayLength);
4244
4245 private:
4246 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4247};
4248
4249class HBoundsCheck : public HExpression<2> {
4250 public:
4251 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004252 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004253 DCHECK(index->GetType() == Primitive::kPrimInt);
4254 SetRawInputAt(0, index);
4255 SetRawInputAt(1, length);
4256 }
4257
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004258 bool CanBeMoved() const OVERRIDE { return true; }
4259 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004260 UNUSED(other);
4261 return true;
4262 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004263
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004264 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004266 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004267
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004268 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004269
4270 DECLARE_INSTRUCTION(BoundsCheck);
4271
4272 private:
4273 const uint32_t dex_pc_;
4274
4275 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4276};
4277
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278/**
4279 * Some DEX instructions are folded into multiple HInstructions that need
4280 * to stay live until the last HInstruction. This class
4281 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004282 * HInstruction stays live. `index` represents the stack location index of the
4283 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284 */
4285class HTemporary : public HTemplateInstruction<0> {
4286 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004287 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288
4289 size_t GetIndex() const { return index_; }
4290
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004291 Primitive::Type GetType() const OVERRIDE {
4292 // The previous instruction is the one that will be stored in the temporary location.
4293 DCHECK(GetPrevious() != nullptr);
4294 return GetPrevious()->GetType();
4295 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004296
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297 DECLARE_INSTRUCTION(Temporary);
4298
4299 private:
4300 const size_t index_;
4301
4302 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4303};
4304
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004305class HSuspendCheck : public HTemplateInstruction<0> {
4306 public:
4307 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004308 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004309
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004310 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004311 return true;
4312 }
4313
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004314 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004315 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4316 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004317
4318 DECLARE_INSTRUCTION(SuspendCheck);
4319
4320 private:
4321 const uint32_t dex_pc_;
4322
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004323 // Only used for code generation, in order to share the same slow path between back edges
4324 // of a same loop.
4325 SlowPathCode* slow_path_;
4326
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004327 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4328};
4329
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004330/**
4331 * Instruction to load a Class object.
4332 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004333class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004334 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004335 HLoadClass(HCurrentMethod* current_method,
4336 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004337 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004338 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004339 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004340 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004341 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004342 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004343 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004344 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004345 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004346 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004347 SetRawInputAt(0, current_method);
4348 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004349
4350 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004351
4352 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4353 return other->AsLoadClass()->type_index_ == type_index_;
4354 }
4355
4356 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4357
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004358 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004359 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004360 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004361 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004362
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004363 bool NeedsEnvironment() const OVERRIDE {
4364 // Will call runtime and load the class if the class is not loaded yet.
4365 // TODO: finer grain decision.
4366 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004367 }
4368
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004369 bool MustGenerateClinitCheck() const {
4370 return generate_clinit_check_;
4371 }
4372
Calin Juravle0ba218d2015-05-19 18:46:01 +01004373 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4374 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004375 }
4376
4377 bool CanCallRuntime() const {
4378 return MustGenerateClinitCheck() || !is_referrers_class_;
4379 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004380
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004381 bool CanThrow() const OVERRIDE {
4382 // May call runtime and and therefore can throw.
4383 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004384 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004385 }
4386
Calin Juravleacf735c2015-02-12 15:25:22 +00004387 ReferenceTypeInfo GetLoadedClassRTI() {
4388 return loaded_class_rti_;
4389 }
4390
4391 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4392 // Make sure we only set exact types (the loaded class should never be merged).
4393 DCHECK(rti.IsExact());
4394 loaded_class_rti_ = rti;
4395 }
4396
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004397 const DexFile& GetDexFile() { return dex_file_; }
4398
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004399 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4400
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004401 static SideEffects SideEffectsForArchRuntimeCalls() {
4402 return SideEffects::CanTriggerGC();
4403 }
4404
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004405 DECLARE_INSTRUCTION(LoadClass);
4406
4407 private:
4408 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004409 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004410 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004411 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004412 // Whether this instruction must generate the initialization check.
4413 // Used for code generation.
4414 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004415
Calin Juravleacf735c2015-02-12 15:25:22 +00004416 ReferenceTypeInfo loaded_class_rti_;
4417
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004418 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4419};
4420
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004421class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004422 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004423 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004424 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004425 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004426 dex_pc_(dex_pc) {
4427 SetRawInputAt(0, current_method);
4428 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004429
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004430 bool CanBeMoved() const OVERRIDE { return true; }
4431
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004432 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4433 return other->AsLoadString()->string_index_ == string_index_;
4434 }
4435
4436 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4437
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004438 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004439 uint32_t GetStringIndex() const { return string_index_; }
4440
4441 // TODO: Can we deopt or debug when we resolve a string?
4442 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004443 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004444 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004445
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004446 static SideEffects SideEffectsForArchRuntimeCalls() {
4447 return SideEffects::CanTriggerGC();
4448 }
4449
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004450 DECLARE_INSTRUCTION(LoadString);
4451
4452 private:
4453 const uint32_t string_index_;
4454 const uint32_t dex_pc_;
4455
4456 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4457};
4458
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004459/**
4460 * Performs an initialization check on its Class object input.
4461 */
4462class HClinitCheck : public HExpression<1> {
4463 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004464 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004465 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004466 Primitive::kPrimNot,
4467 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004468 dex_pc_(dex_pc) {
4469 SetRawInputAt(0, constant);
4470 }
4471
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004472 bool CanBeMoved() const OVERRIDE { return true; }
4473 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4474 UNUSED(other);
4475 return true;
4476 }
4477
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004478 bool NeedsEnvironment() const OVERRIDE {
4479 // May call runtime to initialize the class.
4480 return true;
4481 }
4482
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004483 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004484
4485 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4486
4487 DECLARE_INSTRUCTION(ClinitCheck);
4488
4489 private:
4490 const uint32_t dex_pc_;
4491
4492 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4493};
4494
4495class HStaticFieldGet : public HExpression<1> {
4496 public:
4497 HStaticFieldGet(HInstruction* cls,
4498 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004499 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004500 bool is_volatile,
4501 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004502 const DexFile& dex_file,
4503 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004504 : HExpression(
4505 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004506 SideEffects::FieldReadOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004507 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004508 SetRawInputAt(0, cls);
4509 }
4510
Calin Juravle52c48962014-12-16 17:02:57 +00004511
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004512 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004513
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004514 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004515 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4516 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004517 }
4518
4519 size_t ComputeHashCode() const OVERRIDE {
4520 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4521 }
4522
Calin Juravle52c48962014-12-16 17:02:57 +00004523 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004524 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4525 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004526 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004527
4528 DECLARE_INSTRUCTION(StaticFieldGet);
4529
4530 private:
4531 const FieldInfo field_info_;
4532
4533 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4534};
4535
4536class HStaticFieldSet : public HTemplateInstruction<2> {
4537 public:
4538 HStaticFieldSet(HInstruction* cls,
4539 HInstruction* value,
4540 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004541 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004542 bool is_volatile,
4543 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004544 const DexFile& dex_file,
4545 Handle<mirror::DexCache> dex_cache)
Aart Bik34c3ba92015-07-20 14:08:59 -07004546 : HTemplateInstruction(
4547 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004548 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004549 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004550 SetRawInputAt(0, cls);
4551 SetRawInputAt(1, value);
4552 }
4553
Calin Juravle52c48962014-12-16 17:02:57 +00004554 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004555 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4556 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004557 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004558
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004559 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004560 bool GetValueCanBeNull() const { return value_can_be_null_; }
4561 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004562
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004563 DECLARE_INSTRUCTION(StaticFieldSet);
4564
4565 private:
4566 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004567 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004568
4569 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4570};
4571
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004572// Implement the move-exception DEX instruction.
4573class HLoadException : public HExpression<0> {
4574 public:
4575 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4576
David Brazdilbbd733e2015-08-18 17:48:17 +01004577 bool CanBeNull() const OVERRIDE { return false; }
4578
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004579 DECLARE_INSTRUCTION(LoadException);
4580
4581 private:
4582 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4583};
4584
David Brazdilcb1c0552015-08-04 16:22:25 +01004585// Implicit part of move-exception which clears thread-local exception storage.
4586// Must not be removed because the runtime expects the TLS to get cleared.
4587class HClearException : public HTemplateInstruction<0> {
4588 public:
4589 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4590
4591 DECLARE_INSTRUCTION(ClearException);
4592
4593 private:
4594 DISALLOW_COPY_AND_ASSIGN(HClearException);
4595};
4596
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004597class HThrow : public HTemplateInstruction<1> {
4598 public:
4599 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004600 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004601 SetRawInputAt(0, exception);
4602 }
4603
4604 bool IsControlFlow() const OVERRIDE { return true; }
4605
4606 bool NeedsEnvironment() const OVERRIDE { return true; }
4607
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004608 bool CanThrow() const OVERRIDE { return true; }
4609
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004610 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004611
4612 DECLARE_INSTRUCTION(Throw);
4613
4614 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004615 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004616
4617 DISALLOW_COPY_AND_ASSIGN(HThrow);
4618};
4619
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004620class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004621 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004622 HInstanceOf(HInstruction* object,
4623 HLoadClass* constant,
4624 bool class_is_final,
4625 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004626 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004627 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004628 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004629 dex_pc_(dex_pc) {
4630 SetRawInputAt(0, object);
4631 SetRawInputAt(1, constant);
4632 }
4633
4634 bool CanBeMoved() const OVERRIDE { return true; }
4635
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004636 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004637 return true;
4638 }
4639
4640 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004641 return false;
4642 }
4643
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004644 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004645
4646 bool IsClassFinal() const { return class_is_final_; }
4647
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004648 // Used only in code generation.
4649 bool MustDoNullCheck() const { return must_do_null_check_; }
4650 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4651
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004652 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4653 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4654 }
4655
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004656 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004657
4658 private:
4659 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004660 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004661 const uint32_t dex_pc_;
4662
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004663 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4664};
4665
Calin Juravleb1498f62015-02-16 13:13:29 +00004666class HBoundType : public HExpression<1> {
4667 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004668 // Constructs an HBoundType with the given upper_bound.
4669 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004670 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004671 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004672 upper_bound_(upper_bound),
4673 upper_can_be_null_(upper_can_be_null),
4674 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004675 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004676 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004677 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004678 }
4679
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004680 // GetUpper* should only be used in reference type propagation.
4681 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4682 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004683
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004684 void SetCanBeNull(bool can_be_null) {
4685 DCHECK(upper_can_be_null_ || !can_be_null);
4686 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004687 }
4688
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004689 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4690
Calin Juravleb1498f62015-02-16 13:13:29 +00004691 DECLARE_INSTRUCTION(BoundType);
4692
4693 private:
4694 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004695 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4696 // It is used to bound the type in cases like:
4697 // if (x instanceof ClassX) {
4698 // // uper_bound_ will be ClassX
4699 // }
4700 const ReferenceTypeInfo upper_bound_;
4701 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4702 // is false then can_be_null_ cannot be true).
4703 const bool upper_can_be_null_;
4704 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004705
4706 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4707};
4708
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004709class HCheckCast : public HTemplateInstruction<2> {
4710 public:
4711 HCheckCast(HInstruction* object,
4712 HLoadClass* constant,
4713 bool class_is_final,
4714 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004715 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004716 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004717 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004718 dex_pc_(dex_pc) {
4719 SetRawInputAt(0, object);
4720 SetRawInputAt(1, constant);
4721 }
4722
4723 bool CanBeMoved() const OVERRIDE { return true; }
4724
4725 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4726 return true;
4727 }
4728
4729 bool NeedsEnvironment() const OVERRIDE {
4730 // Instruction may throw a CheckCastError.
4731 return true;
4732 }
4733
4734 bool CanThrow() const OVERRIDE { return true; }
4735
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004736 bool MustDoNullCheck() const { return must_do_null_check_; }
4737 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4738
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004739 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004740
4741 bool IsClassFinal() const { return class_is_final_; }
4742
4743 DECLARE_INSTRUCTION(CheckCast);
4744
4745 private:
4746 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004747 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004748 const uint32_t dex_pc_;
4749
4750 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004751};
4752
Calin Juravle27df7582015-04-17 19:12:31 +01004753class HMemoryBarrier : public HTemplateInstruction<0> {
4754 public:
4755 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004756 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004757 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004758 barrier_kind_(barrier_kind) {}
4759
4760 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4761
4762 DECLARE_INSTRUCTION(MemoryBarrier);
4763
4764 private:
4765 const MemBarrierKind barrier_kind_;
4766
4767 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4768};
4769
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004770class HMonitorOperation : public HTemplateInstruction<1> {
4771 public:
4772 enum OperationKind {
4773 kEnter,
4774 kExit,
4775 };
4776
4777 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004778 : HTemplateInstruction(
4779 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004780 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004781 SetRawInputAt(0, object);
4782 }
4783
4784 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004785 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4786
4787 bool CanThrow() const OVERRIDE {
4788 // Verifier guarantees that monitor-exit cannot throw.
4789 // This is important because it allows the HGraphBuilder to remove
4790 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4791 return IsEnter();
4792 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004793
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004794 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004795
4796 bool IsEnter() const { return kind_ == kEnter; }
4797
4798 DECLARE_INSTRUCTION(MonitorOperation);
4799
Calin Juravle52c48962014-12-16 17:02:57 +00004800 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004801 const OperationKind kind_;
4802 const uint32_t dex_pc_;
4803
4804 private:
4805 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4806};
4807
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004808/**
4809 * A HInstruction used as a marker for the replacement of new + <init>
4810 * of a String to a call to a StringFactory. Only baseline will see
4811 * the node at code generation, where it will be be treated as null.
4812 * When compiling non-baseline, `HFakeString` instructions are being removed
4813 * in the instruction simplifier.
4814 */
4815class HFakeString : public HTemplateInstruction<0> {
4816 public:
4817 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4818
4819 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4820
4821 DECLARE_INSTRUCTION(FakeString);
4822
4823 private:
4824 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4825};
4826
Vladimir Markof9f64412015-09-02 14:05:49 +01004827class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004828 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004829 MoveOperands(Location source,
4830 Location destination,
4831 Primitive::Type type,
4832 HInstruction* instruction)
4833 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004834
4835 Location GetSource() const { return source_; }
4836 Location GetDestination() const { return destination_; }
4837
4838 void SetSource(Location value) { source_ = value; }
4839 void SetDestination(Location value) { destination_ = value; }
4840
4841 // The parallel move resolver marks moves as "in-progress" by clearing the
4842 // destination (but not the source).
4843 Location MarkPending() {
4844 DCHECK(!IsPending());
4845 Location dest = destination_;
4846 destination_ = Location::NoLocation();
4847 return dest;
4848 }
4849
4850 void ClearPending(Location dest) {
4851 DCHECK(IsPending());
4852 destination_ = dest;
4853 }
4854
4855 bool IsPending() const {
4856 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4857 return destination_.IsInvalid() && !source_.IsInvalid();
4858 }
4859
4860 // True if this blocks a move from the given location.
4861 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004862 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004863 }
4864
4865 // A move is redundant if it's been eliminated, if its source and
4866 // destination are the same, or if its destination is unneeded.
4867 bool IsRedundant() const {
4868 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4869 }
4870
4871 // We clear both operands to indicate move that's been eliminated.
4872 void Eliminate() {
4873 source_ = destination_ = Location::NoLocation();
4874 }
4875
4876 bool IsEliminated() const {
4877 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4878 return source_.IsInvalid();
4879 }
4880
Alexey Frunze4dda3372015-06-01 18:31:49 -07004881 Primitive::Type GetType() const { return type_; }
4882
Nicolas Geoffray90218252015-04-15 11:56:51 +01004883 bool Is64BitMove() const {
4884 return Primitive::Is64BitType(type_);
4885 }
4886
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004887 HInstruction* GetInstruction() const { return instruction_; }
4888
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004889 private:
4890 Location source_;
4891 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004892 // The type this move is for.
4893 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004894 // The instruction this move is assocatied with. Null when this move is
4895 // for moving an input in the expected locations of user (including a phi user).
4896 // This is only used in debug mode, to ensure we do not connect interval siblings
4897 // in the same parallel move.
4898 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004899};
4900
4901static constexpr size_t kDefaultNumberOfMoves = 4;
4902
4903class HParallelMove : public HTemplateInstruction<0> {
4904 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004905 explicit HParallelMove(ArenaAllocator* arena)
4906 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004907
Nicolas Geoffray90218252015-04-15 11:56:51 +01004908 void AddMove(Location source,
4909 Location destination,
4910 Primitive::Type type,
4911 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004912 DCHECK(source.IsValid());
4913 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004914 if (kIsDebugBuild) {
4915 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004916 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004917 if (moves_.Get(i).GetInstruction() == instruction) {
4918 // Special case the situation where the move is for the spill slot
4919 // of the instruction.
4920 if ((GetPrevious() == instruction)
4921 || ((GetPrevious() == nullptr)
4922 && instruction->IsPhi()
4923 && instruction->GetBlock() == GetBlock())) {
4924 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4925 << "Doing parallel moves for the same instruction.";
4926 } else {
4927 DCHECK(false) << "Doing parallel moves for the same instruction.";
4928 }
4929 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004930 }
4931 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004932 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004933 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004934 << "Overlapped destination for two moves in a parallel move: "
4935 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4936 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004937 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004938 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004939 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004940 }
4941
4942 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004943 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004944 }
4945
4946 size_t NumMoves() const { return moves_.Size(); }
4947
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004948 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004949
4950 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004951 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004952
4953 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4954};
4955
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004956class HGraphVisitor : public ValueObject {
4957 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004958 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4959 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004960
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004961 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004962 virtual void VisitBasicBlock(HBasicBlock* block);
4963
Roland Levillain633021e2014-10-01 14:12:25 +01004964 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004965 void VisitInsertionOrder();
4966
Roland Levillain633021e2014-10-01 14:12:25 +01004967 // Visit the graph following dominator tree reverse post-order.
4968 void VisitReversePostOrder();
4969
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004970 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004971
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004972 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004973#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004974 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4975
4976 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4977
4978#undef DECLARE_VISIT_INSTRUCTION
4979
4980 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004981 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004982
4983 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4984};
4985
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004986class HGraphDelegateVisitor : public HGraphVisitor {
4987 public:
4988 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4989 virtual ~HGraphDelegateVisitor() {}
4990
4991 // Visit functions that delegate to to super class.
4992#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004993 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004994
4995 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4996
4997#undef DECLARE_VISIT_INSTRUCTION
4998
4999 private:
5000 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5001};
5002
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005003class HInsertionOrderIterator : public ValueObject {
5004 public:
5005 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5006
5007 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5008 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5009 void Advance() { ++index_; }
5010
5011 private:
5012 const HGraph& graph_;
5013 size_t index_;
5014
5015 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5016};
5017
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005018class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005019 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005020 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5021 // Check that reverse post order of the graph has been built.
5022 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5023 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005024
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005025 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5026 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005027 void Advance() { ++index_; }
5028
5029 private:
5030 const HGraph& graph_;
5031 size_t index_;
5032
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005033 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005034};
5035
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005036class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005037 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005038 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005039 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5040 // Check that reverse post order of the graph has been built.
5041 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5042 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005043
5044 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005045 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005046 void Advance() { --index_; }
5047
5048 private:
5049 const HGraph& graph_;
5050 size_t index_;
5051
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005052 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005053};
5054
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005055class HLinearPostOrderIterator : public ValueObject {
5056 public:
5057 explicit HLinearPostOrderIterator(const HGraph& graph)
5058 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5059
5060 bool Done() const { return index_ == 0; }
5061
5062 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5063
5064 void Advance() {
5065 --index_;
5066 DCHECK_GE(index_, 0U);
5067 }
5068
5069 private:
5070 const GrowableArray<HBasicBlock*>& order_;
5071 size_t index_;
5072
5073 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5074};
5075
5076class HLinearOrderIterator : public ValueObject {
5077 public:
5078 explicit HLinearOrderIterator(const HGraph& graph)
5079 : order_(graph.GetLinearOrder()), index_(0) {}
5080
5081 bool Done() const { return index_ == order_.Size(); }
5082 HBasicBlock* Current() const { return order_.Get(index_); }
5083 void Advance() { ++index_; }
5084
5085 private:
5086 const GrowableArray<HBasicBlock*>& order_;
5087 size_t index_;
5088
5089 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5090};
5091
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005092// Iterator over the blocks that art part of the loop. Includes blocks part
5093// of an inner loop. The order in which the blocks are iterated is on their
5094// block id.
5095class HBlocksInLoopIterator : public ValueObject {
5096 public:
5097 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5098 : blocks_in_loop_(info.GetBlocks()),
5099 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5100 index_(0) {
5101 if (!blocks_in_loop_.IsBitSet(index_)) {
5102 Advance();
5103 }
5104 }
5105
5106 bool Done() const { return index_ == blocks_.Size(); }
5107 HBasicBlock* Current() const { return blocks_.Get(index_); }
5108 void Advance() {
5109 ++index_;
5110 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5111 if (blocks_in_loop_.IsBitSet(index_)) {
5112 break;
5113 }
5114 }
5115 }
5116
5117 private:
5118 const BitVector& blocks_in_loop_;
5119 const GrowableArray<HBasicBlock*>& blocks_;
5120 size_t index_;
5121
5122 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5123};
5124
Mingyao Yang3584bce2015-05-19 16:01:59 -07005125// Iterator over the blocks that art part of the loop. Includes blocks part
5126// of an inner loop. The order in which the blocks are iterated is reverse
5127// post order.
5128class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5129 public:
5130 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5131 : blocks_in_loop_(info.GetBlocks()),
5132 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5133 index_(0) {
5134 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5135 Advance();
5136 }
5137 }
5138
5139 bool Done() const { return index_ == blocks_.Size(); }
5140 HBasicBlock* Current() const { return blocks_.Get(index_); }
5141 void Advance() {
5142 ++index_;
5143 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5144 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5145 break;
5146 }
5147 }
5148 }
5149
5150 private:
5151 const BitVector& blocks_in_loop_;
5152 const GrowableArray<HBasicBlock*>& blocks_;
5153 size_t index_;
5154
5155 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5156};
5157
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005158inline int64_t Int64FromConstant(HConstant* constant) {
5159 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5160 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5161 : constant->AsLongConstant()->GetValue();
5162}
5163
Vladimir Marko58155012015-08-19 12:49:41 +00005164inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5165 // For the purposes of the compiler, the dex files must actually be the same object
5166 // if we want to safely treat them as the same. This is especially important for JIT
5167 // as custom class loaders can open the same underlying file (or memory) multiple
5168 // times and provide different class resolution but no two class loaders should ever
5169 // use the same DexFile object - doing so is an unsupported hack that can lead to
5170 // all sorts of weird failures.
5171 return &lhs == &rhs;
5172}
5173
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005174} // namespace art
5175
5176#endif // ART_COMPILER_OPTIMIZING_NODES_H_