blob: d52a4f7575f27df21c97caeaac83b90d4711218e [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
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
85 kCondEQ,
86 kCondNE,
87 kCondLT,
88 kCondLE,
89 kCondGT,
90 kCondGE,
91};
92
Vladimir Markof9f64412015-09-02 14:05:49 +010093class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010094 public:
95 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
96
97 void AddInstruction(HInstruction* instruction);
98 void RemoveInstruction(HInstruction* instruction);
99
David Brazdilc3d743f2015-04-22 13:40:50 +0100100 // Insert `instruction` before/after an existing instruction `cursor`.
101 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
102 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
103
Roland Levillain6b469232014-09-25 10:10:38 +0100104 // Return true if this list contains `instruction`.
105 bool Contains(HInstruction* instruction) const;
106
Roland Levillainccc07a92014-09-16 14:48:16 +0100107 // Return true if `instruction1` is found before `instruction2` in
108 // this instruction list and false otherwise. Abort if none
109 // of these instructions is found.
110 bool FoundBefore(const HInstruction* instruction1,
111 const HInstruction* instruction2) const;
112
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000113 bool IsEmpty() const { return first_instruction_ == nullptr; }
114 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
115
116 // Update the block of all instructions to be `block`.
117 void SetBlockOfInstructions(HBasicBlock* block) const;
118
119 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
120 void Add(const HInstructionList& instruction_list);
121
David Brazdil2d7352b2015-04-20 14:52:42 +0100122 // Return the number of instructions in the list. This is an expensive operation.
123 size_t CountSize() const;
124
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125 private:
126 HInstruction* first_instruction_;
127 HInstruction* last_instruction_;
128
129 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000130 friend class HGraph;
131 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100133 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134
135 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
136};
137
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100139class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100141 HGraph(ArenaAllocator* arena,
142 const DexFile& dex_file,
143 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100144 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100146 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100147 bool debuggable = false,
148 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100151 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100152 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700153 entry_block_(nullptr),
154 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100155 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100156 number_of_vregs_(0),
157 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000158 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400159 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000160 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000161 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000162 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100163 dex_file_(dex_file),
164 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100165 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100166 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100167 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 cached_null_constant_(nullptr),
170 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000171 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
172 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100173 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
174 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000176 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100177 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100178 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000179
David Brazdil69ba7b72015-06-23 18:27:30 +0100180 bool IsInSsaForm() const { return in_ssa_form_; }
181
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000182 HBasicBlock* GetEntryBlock() const { return entry_block_; }
183 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100184 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000185
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000186 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
187 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000188
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191 // Try building the SSA form of this graph, with dominance computation and loop
192 // recognition. Returns whether it was successful in doing all these steps.
193 bool TryBuildingSsa() {
194 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000195 // The SSA builder requires loops to all be natural. Specifically, the dead phi
196 // elimination phase checks the consistency of the graph when doing a post-order
197 // visit for eliminating dead phis: a dead phi can only have loop header phi
198 // users remaining when being visited.
199 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100200 // Precompute per-block try membership before entering the SSA builder,
201 // which needs the information to build catch block phis from values of
202 // locals at throwing instructions inside try blocks.
203 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100205 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000206 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000207 }
208
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100209 void ComputeDominanceInformation();
210 void ClearDominanceInformation();
211
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000212 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100214 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100215 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000217 // Analyze all natural loops in this graph. Returns false if one
218 // loop is not natural, that is the header does not dominate the
219 // back edge.
220 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221
David Brazdilffee3d32015-07-06 11:48:53 +0100222 // Iterate over blocks to compute try block membership. Needs reverse post
223 // order and loop information.
224 void ComputeTryBlockInformation();
225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000227 // Returns the instruction used to replace the invoke expression or null if the
228 // invoke is for a void method.
229 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000230
Mingyao Yang3584bce2015-05-19 16:01:59 -0700231 // Need to add a couple of blocks to test if the loop body is entered and
232 // put deoptimization instructions, etc.
233 void TransformLoopHeaderForBCE(HBasicBlock* header);
234
David Brazdil2d7352b2015-04-20 14:52:42 +0100235 // Removes `block` from the graph.
236 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000237
David Brazdilfc6a86a2015-06-26 10:33:45 +0000238 // Splits the edge between `block` and `successor` while preserving the
239 // indices in the predecessor/successor lists. If there are multiple edges
240 // between the blocks, the lowest indices are used.
241 // Returns the new block which is empty and has the same dex pc as `successor`.
242 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
245 void SimplifyLoop(HBasicBlock* header);
246
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000247 int32_t GetNextInstructionId() {
248 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000249 return current_instruction_id_++;
250 }
251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 int32_t GetCurrentInstructionId() const {
253 return current_instruction_id_;
254 }
255
256 void SetCurrentInstructionId(int32_t id) {
257 current_instruction_id_ = id;
258 }
259
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100260 uint16_t GetMaximumNumberOfOutVRegs() const {
261 return maximum_number_of_out_vregs_;
262 }
263
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000264 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
265 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100266 }
267
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100268 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
269 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
270 }
271
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000272 void UpdateTemporariesVRegSlots(size_t slots) {
273 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100274 }
275
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000276 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100277 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000278 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100279 }
280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100281 void SetNumberOfVRegs(uint16_t number_of_vregs) {
282 number_of_vregs_ = number_of_vregs;
283 }
284
285 uint16_t GetNumberOfVRegs() const {
286 return number_of_vregs_;
287 }
288
289 void SetNumberOfInVRegs(uint16_t value) {
290 number_of_in_vregs_ = value;
291 }
292
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100293 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100294 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100295 return number_of_vregs_ - number_of_in_vregs_;
296 }
297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
299 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100300 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100301
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100302 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
303 return linear_order_;
304 }
305
Mark Mendell1152c922015-04-24 17:06:35 -0400306 bool HasBoundsChecks() const {
307 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800308 }
309
Mark Mendell1152c922015-04-24 17:06:35 -0400310 void SetHasBoundsChecks(bool value) {
311 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800312 }
313
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100314 bool ShouldGenerateConstructorBarrier() const {
315 return should_generate_constructor_barrier_;
316 }
317
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000318 bool IsDebuggable() const { return debuggable_; }
319
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000321 // already, it is created and inserted into the graph. This method is only for
322 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600323 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000324
325 // TODO: This is problematic for the consistency of reference type propagation
326 // because it can be created anytime after the pass and thus it will be left
327 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600328 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000329
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600330 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
331 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
334 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
337 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000338 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600339 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
340 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000342
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100343 HCurrentMethod* GetCurrentMethod();
344
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100346
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100347 const DexFile& GetDexFile() const {
348 return dex_file_;
349 }
350
351 uint32_t GetMethodIdx() const {
352 return method_idx_;
353 }
354
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100355 InvokeType GetInvokeType() const {
356 return invoke_type_;
357 }
358
Mark Mendellc4701932015-04-10 13:18:51 -0400359 InstructionSet GetInstructionSet() const {
360 return instruction_set_;
361 }
362
David Brazdil77a48ae2015-09-15 12:34:04 +0000363 bool HasTryCatch() const { return has_try_catch_; }
364 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100365
David Brazdil2d7352b2015-04-20 14:52:42 +0100366 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000367 void VisitBlockForDominatorTree(HBasicBlock* block,
368 HBasicBlock* predecessor,
369 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100370 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000371 void VisitBlockForBackEdges(HBasicBlock* block,
372 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100373 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000374 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100375 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000377 template <class InstructionType, typename ValueType>
378 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600379 ArenaSafeMap<ValueType, InstructionType*>* cache,
380 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000381 // Try to find an existing constant of the given value.
382 InstructionType* constant = nullptr;
383 auto cached_constant = cache->find(value);
384 if (cached_constant != cache->end()) {
385 constant = cached_constant->second;
386 }
387
388 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100389 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000390 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600391 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 cache->Overwrite(value, constant);
393 InsertConstant(constant);
394 }
395 return constant;
396 }
397
David Brazdil8d5b8b22015-03-24 10:51:52 +0000398 void InsertConstant(HConstant* instruction);
399
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000400 // Cache a float constant into the graph. This method should only be
401 // called by the SsaBuilder when creating "equivalent" instructions.
402 void CacheFloatConstant(HFloatConstant* constant);
403
404 // See CacheFloatConstant comment.
405 void CacheDoubleConstant(HDoubleConstant* constant);
406
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000408
409 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000410 GrowableArray<HBasicBlock*> blocks_;
411
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412 // List of blocks to perform a reverse post order tree traversal.
413 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000414
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100415 // List of blocks to perform a linear order tree traversal.
416 GrowableArray<HBasicBlock*> linear_order_;
417
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000418 HBasicBlock* entry_block_;
419 HBasicBlock* exit_block_;
420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100422 uint16_t maximum_number_of_out_vregs_;
423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424 // The number of virtual registers in this method. Contains the parameters.
425 uint16_t number_of_vregs_;
426
427 // The number of virtual registers used by parameters of this method.
428 uint16_t number_of_in_vregs_;
429
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000430 // Number of vreg size slots that the temporaries use (used in baseline compiler).
431 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432
Mark Mendell1152c922015-04-24 17:06:35 -0400433 // Has bounds checks. We can totally skip BCE if it's false.
434 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800435
David Brazdil77a48ae2015-09-15 12:34:04 +0000436 // Flag whether there are any try/catch blocks in the graph. We will skip
437 // try/catch-related passes if false.
438 bool has_try_catch_;
439
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000440 // Indicates whether the graph should be compiled in a way that
441 // ensures full debuggability. If false, we can apply more
442 // aggressive optimizations that may limit the level of debugging.
443 const bool debuggable_;
444
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000445 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100448 // The dex file from which the method is from.
449 const DexFile& dex_file_;
450
451 // The method index in the dex file.
452 const uint32_t method_idx_;
453
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100454 // If inlined, this encodes how the callee is being invoked.
455 const InvokeType invoke_type_;
456
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100457 // Whether the graph has been transformed to SSA form. Only used
458 // in debug mode to ensure we are not using properties only valid
459 // for non-SSA form (like the number of temporaries).
460 bool in_ssa_form_;
461
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100462 const bool should_generate_constructor_barrier_;
463
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 const InstructionSet instruction_set_;
465
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000466 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000467 HNullConstant* cached_null_constant_;
468 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000469 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000472
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100473 HCurrentMethod* cached_current_method_;
474
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100476 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000477 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000478 DISALLOW_COPY_AND_ASSIGN(HGraph);
479};
480
Vladimir Markof9f64412015-09-02 14:05:49 +0100481class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 public:
483 HLoopInformation(HBasicBlock* header, HGraph* graph)
484 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100485 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100487 // Make bit vector growable, as the number of blocks may change.
488 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100489
490 HBasicBlock* GetHeader() const {
491 return header_;
492 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000493
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000494 void SetHeader(HBasicBlock* block) {
495 header_ = block;
496 }
497
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100498 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
499 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
500 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
501
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000502 void AddBackEdge(HBasicBlock* back_edge) {
503 back_edges_.Add(back_edge);
504 }
505
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 void RemoveBackEdge(HBasicBlock* back_edge) {
507 back_edges_.Delete(back_edge);
508 }
509
David Brazdil46e2a392015-03-16 17:31:52 +0000510 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000512 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 }
514 return false;
515 }
516
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000517 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518 return back_edges_.Size();
519 }
520
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
524 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100525 }
526
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100527 // Returns the lifetime position of the back edge that has the
528 // greatest lifetime position.
529 size_t GetLifetimeEnd() const;
530
531 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
532 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
533 if (back_edges_.Get(i) == existing) {
534 back_edges_.Put(i, new_back_edge);
535 return;
536 }
537 }
538 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100539 }
540
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100541 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 // that is the header dominates the back edge.
543 bool Populate();
544
David Brazdila4b8c212015-05-07 09:59:30 +0100545 // Reanalyzes the loop by removing loop info from its blocks and re-running
546 // Populate(). If there are no back edges left, the loop info is completely
547 // removed as well as its SuspendCheck instruction. It must be run on nested
548 // inner loops first.
549 void Update();
550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 // Returns whether this loop information contains `block`.
552 // Note that this loop information *must* be populated before entering this function.
553 bool Contains(const HBasicBlock& block) const;
554
555 // Returns whether this loop information is an inner loop of `other`.
556 // Note that `other` *must* be populated before entering this function.
557 bool IsIn(const HLoopInformation& other) const;
558
559 const ArenaBitVector& GetBlocks() const { return blocks_; }
560
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000561 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000562 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000563
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 // Internal recursive implementation of `Populate`.
566 void PopulateRecursive(HBasicBlock* block);
567
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100569 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572
573 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
574};
575
David Brazdilec16f792015-08-19 15:04:01 +0100576// Stores try/catch information for basic blocks.
577// Note that HGraph is constructed so that catch blocks cannot simultaneously
578// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100579class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100580 public:
581 // Try block information constructor.
582 explicit TryCatchInformation(const HTryBoundary& try_entry)
583 : try_entry_(&try_entry),
584 catch_dex_file_(nullptr),
585 catch_type_index_(DexFile::kDexNoIndex16) {
586 DCHECK(try_entry_ != nullptr);
587 }
588
589 // Catch block information constructor.
590 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
591 : try_entry_(nullptr),
592 catch_dex_file_(&dex_file),
593 catch_type_index_(catch_type_index) {}
594
595 bool IsTryBlock() const { return try_entry_ != nullptr; }
596
597 const HTryBoundary& GetTryEntry() const {
598 DCHECK(IsTryBlock());
599 return *try_entry_;
600 }
601
602 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
603
604 bool IsCatchAllTypeIndex() const {
605 DCHECK(IsCatchBlock());
606 return catch_type_index_ == DexFile::kDexNoIndex16;
607 }
608
609 uint16_t GetCatchTypeIndex() const {
610 DCHECK(IsCatchBlock());
611 return catch_type_index_;
612 }
613
614 const DexFile& GetCatchDexFile() const {
615 DCHECK(IsCatchBlock());
616 return *catch_dex_file_;
617 }
618
619 private:
620 // One of possibly several TryBoundary instructions entering the block's try.
621 // Only set for try blocks.
622 const HTryBoundary* try_entry_;
623
624 // Exception type information. Only set for catch blocks.
625 const DexFile* catch_dex_file_;
626 const uint16_t catch_type_index_;
627};
628
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100629static constexpr size_t kNoLifetime = -1;
630
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631// A block in a method. Contains the list of instructions represented
632// as a double linked list. Each block knows its predecessors and
633// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100634
Vladimir Markof9f64412015-09-02 14:05:49 +0100635class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600637 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
640 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000641 loop_information_(nullptr),
642 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000643 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100644 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100645 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100646 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000647 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000648 try_catch_information_(nullptr) {
649 predecessors_.reserve(kDefaultNumberOfPredecessors);
650 successors_.reserve(kDefaultNumberOfSuccessors);
651 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
652 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653
Vladimir Marko60584552015-09-03 13:35:12 +0000654 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656 }
657
Vladimir Marko60584552015-09-03 13:35:12 +0000658 HBasicBlock* GetPredecessor(size_t pred_idx) const {
659 DCHECK_LT(pred_idx, predecessors_.size());
660 return predecessors_[pred_idx];
661 }
662
663 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665 }
666
Vladimir Marko60584552015-09-03 13:35:12 +0000667 HBasicBlock* GetSuccessor(size_t succ_idx) const {
668 DCHECK_LT(succ_idx, successors_.size());
669 return successors_[succ_idx];
670 }
671
672 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
673 return ContainsElement(successors_, block, start_from);
674 }
675
676 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100677 return dominated_blocks_;
678 }
679
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100680 bool IsEntryBlock() const {
681 return graph_->GetEntryBlock() == this;
682 }
683
684 bool IsExitBlock() const {
685 return graph_->GetExitBlock() == this;
686 }
687
David Brazdil46e2a392015-03-16 17:31:52 +0000688 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000689 bool IsSingleTryBoundary() const;
690
691 // Returns true if this block emits nothing but a jump.
692 bool IsSingleJump() const {
693 HLoopInformation* loop_info = GetLoopInformation();
694 return (IsSingleGoto() || IsSingleTryBoundary())
695 // Back edges generate a suspend check.
696 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
697 }
David Brazdil46e2a392015-03-16 17:31:52 +0000698
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 void AddBackEdge(HBasicBlock* back_edge) {
700 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 loop_information_->AddBackEdge(back_edge);
705 }
706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000710 int GetBlockId() const { return block_id_; }
711 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600712 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714 HBasicBlock* GetDominator() const { return dominator_; }
715 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000716 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
717
718 void RemoveDominatedBlock(HBasicBlock* block) {
719 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100720 }
Vladimir Marko60584552015-09-03 13:35:12 +0000721
722 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
723 ReplaceElement(dominated_blocks_, existing, new_block);
724 }
725
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100726 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727
728 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100729 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 }
731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
733 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100734 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100736 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
737 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738
739 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000740 successors_.push_back(block);
741 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 }
743
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100744 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100746 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->predecessors_.push_back(this);
748 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749 }
750
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
752 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000753 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000754 new_block->successors_.push_back(this);
755 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 }
757
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 // Insert `this` between `predecessor` and `successor. This method
759 // preserves the indicies, and will update the first edge found between
760 // `predecessor` and `successor`.
761 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
762 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000764 successor->predecessors_[predecessor_index] = this;
765 predecessor->successors_[successor_index] = this;
766 successors_.push_back(successor);
767 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100768 }
769
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100770 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 }
773
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000774 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776 }
777
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
782 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 predecessors_.push_back(block);
784 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100787 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 DCHECK_EQ(predecessors_.size(), 2u);
789 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100790 }
791
David Brazdil769c9e52015-04-27 13:54:09 +0100792 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 DCHECK_EQ(successors_.size(), 2u);
794 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100799 }
800
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100803 }
804
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 DCHECK_EQ(GetPredecessors().size(), 1u);
807 return GetPredecessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 }
809
810 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetSuccessors().size(), 1u);
812 return GetSuccessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 }
814
815 // Returns whether the first occurrence of `predecessor` in the list of
816 // predecessors is at index `idx`.
817 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000818 DCHECK_EQ(GetPredecessor(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819 return GetPredecessorIndexOf(predecessor) == idx;
820 }
821
David Brazdilffee3d32015-07-06 11:48:53 +0100822 // Returns the number of non-exceptional successors. SsaChecker ensures that
823 // these are stored at the beginning of the successor list.
824 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000825 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100826 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827
828 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100829 // created, latter block. Note that this method will add the block to the
830 // graph, create a Goto at the end of the former block and will create an edge
831 // between the blocks. It will not, however, update the reverse post order or
832 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000833 HBasicBlock* SplitBefore(HInstruction* cursor);
834
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000835 // Split the block into two blocks just after `cursor`. Returns the newly
836 // created block. Note that this method just updates raw block information,
837 // like predecessors, successors, dominators, and instruction list. It does not
838 // update the graph, reverse post order, loop information, nor make sure the
839 // blocks are consistent (for example ending with a control flow instruction).
840 HBasicBlock* SplitAfter(HInstruction* cursor);
841
842 // Merge `other` at the end of `this`. Successors and dominated blocks of
843 // `other` are changed to be successors and dominated blocks of `this`. Note
844 // that this method does not update the graph, reverse post order, loop
845 // information, nor make sure the blocks are consistent (for example ending
846 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100847 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848
849 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
850 // of `this` are moved to `other`.
851 // Note that this method does not update the graph, reverse post order, loop
852 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000853 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000854 void ReplaceWith(HBasicBlock* other);
855
David Brazdil2d7352b2015-04-20 14:52:42 +0100856 // Merge `other` at the end of `this`. This method updates loops, reverse post
857 // order, links to predecessors, successors, dominators and deletes the block
858 // from the graph. The two blocks must be successive, i.e. `this` the only
859 // predecessor of `other` and vice versa.
860 void MergeWith(HBasicBlock* other);
861
862 // Disconnects `this` from all its predecessors, successors and dominator,
863 // removes it from all loops it is included in and eventually from the graph.
864 // The block must not dominate any other block. Predecessors and successors
865 // are safely updated.
866 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000867
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100869 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100870 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100871 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100872 // Replace instruction `initial` with `replacement` within this block.
873 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
874 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100875 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100876 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000877 // RemoveInstruction and RemovePhi delete a given instruction from the respective
878 // instruction list. With 'ensure_safety' set to true, it verifies that the
879 // instruction is not in use and removes it from the use lists of its inputs.
880 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
881 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100882 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883
884 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100885 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 }
887
Roland Levillain6b879dd2014-09-22 17:13:44 +0100888 bool IsLoopPreHeaderFirstPredecessor() const {
889 DCHECK(IsLoopHeader());
Vladimir Marko60584552015-09-03 13:35:12 +0000890 return GetPredecessor(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100891 }
892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100893 HLoopInformation* GetLoopInformation() const {
894 return loop_information_;
895 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000897 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000899 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 void SetInLoop(HLoopInformation* info) {
901 if (IsLoopHeader()) {
902 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100903 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 loop_information_ = info;
905 } else if (loop_information_->Contains(*info->GetHeader())) {
906 // Block is currently part of an outer loop. Make it part of this inner loop.
907 // Note that a non loop header having a loop information means this loop information
908 // has already been populated
909 loop_information_ = info;
910 } else {
911 // Block is part of an inner loop. Do not update the loop information.
912 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
913 // at this point, because this method is being called while populating `info`.
914 }
915 }
916
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000917 // Raw update of the loop information.
918 void SetLoopInformation(HLoopInformation* info) {
919 loop_information_ = info;
920 }
921
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100922 bool IsInLoop() const { return loop_information_ != nullptr; }
923
David Brazdilec16f792015-08-19 15:04:01 +0100924 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
925
926 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
927 try_catch_information_ = try_catch_information;
928 }
929
930 bool IsTryBlock() const {
931 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
932 }
933
934 bool IsCatchBlock() const {
935 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
936 }
David Brazdilffee3d32015-07-06 11:48:53 +0100937
938 // Returns the try entry that this block's successors should have. They will
939 // be in the same try, unless the block ends in a try boundary. In that case,
940 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100941 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100942
David Brazdila4b8c212015-05-07 09:59:30 +0100943 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100944 bool Dominates(HBasicBlock* block) const;
945
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100946 size_t GetLifetimeStart() const { return lifetime_start_; }
947 size_t GetLifetimeEnd() const { return lifetime_end_; }
948
949 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
950 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
951
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100952
David Brazdil8d5b8b22015-03-24 10:51:52 +0000953 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000954 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100955 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000956 bool HasSinglePhi() const;
957
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000959 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000960 ArenaVector<HBasicBlock*> predecessors_;
961 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962 HInstructionList instructions_;
963 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000964 HLoopInformation* loop_information_;
965 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000966 ArenaVector<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100968 // The dex program counter of the first instruction of this block.
969 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100970 size_t lifetime_start_;
971 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100972 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100973
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000974 friend class HGraph;
975 friend class HInstruction;
976
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
978};
979
David Brazdilb2bd1c52015-03-25 11:17:37 +0000980// Iterates over the LoopInformation of all loops which contain 'block'
981// from the innermost to the outermost.
982class HLoopInformationOutwardIterator : public ValueObject {
983 public:
984 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
985 : current_(block.GetLoopInformation()) {}
986
987 bool Done() const { return current_ == nullptr; }
988
989 void Advance() {
990 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100991 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000992 }
993
994 HLoopInformation* Current() const {
995 DCHECK(!Done());
996 return current_;
997 }
998
999 private:
1000 HLoopInformation* current_;
1001
1002 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1003};
1004
Alexandre Ramesef20f712015-06-09 10:29:30 +01001005#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001006 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001007 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(ArrayGet, Instruction) \
1009 M(ArrayLength, Instruction) \
1010 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001011 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001012 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001013 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001014 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001015 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001016 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001019 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001020 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001022 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001024 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001026 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001027 M(FloatConstant, Constant) \
1028 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(GreaterThan, Condition) \
1030 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001032 M(InstanceFieldGet, Instruction) \
1033 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001034 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001036 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001037 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(LessThan, Condition) \
1040 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001041 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001042 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001044 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(Local, Instruction) \
1046 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001047 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001048 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(Mul, BinaryOperation) \
1050 M(Neg, UnaryOperation) \
1051 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001052 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001053 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001055 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001056 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001057 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001059 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001061 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(Return, Instruction) \
1063 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001064 M(Shl, BinaryOperation) \
1065 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001066 M(StaticFieldGet, Instruction) \
1067 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001068 M(StoreLocal, Instruction) \
1069 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001070 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001071 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001072 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001074 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001075 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001076 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077
Alexandre Ramesef20f712015-06-09 10:29:30 +01001078#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1079
1080#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1081
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001082#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1083
Mark Mendell0616ae02015-04-17 12:49:27 -04001084#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1085 M(X86ComputeBaseMethodAddress, Instruction) \
1086 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001087
1088#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1089
1090#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1091 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1092 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1093 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001094 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001095 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1096 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1097
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001098#define FOR_EACH_INSTRUCTION(M) \
1099 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1100 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001101 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001102 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001103 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001104
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001105#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1107#undef FORWARD_DECLARATION
1108
Roland Levillainccc07a92014-09-16 14:48:16 +01001109#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001110 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1111 const char* DebugName() const OVERRIDE { return #type; } \
1112 const H##type* As##type() const OVERRIDE { return this; } \
1113 H##type* As##type() OVERRIDE { return this; } \
1114 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001115 return other->Is##type(); \
1116 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001117 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118
David Brazdiled596192015-01-23 10:39:45 +00001119template <typename T> class HUseList;
1120
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001121template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001122class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001123 public:
David Brazdiled596192015-01-23 10:39:45 +00001124 HUseListNode* GetPrevious() const { return prev_; }
1125 HUseListNode* GetNext() const { return next_; }
1126 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001128 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001130 private:
David Brazdiled596192015-01-23 10:39:45 +00001131 HUseListNode(T user, size_t index)
1132 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1133
1134 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001135 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001136 HUseListNode<T>* prev_;
1137 HUseListNode<T>* next_;
1138
1139 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001140
1141 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1142};
1143
David Brazdiled596192015-01-23 10:39:45 +00001144template <typename T>
1145class HUseList : public ValueObject {
1146 public:
1147 HUseList() : first_(nullptr) {}
1148
1149 void Clear() {
1150 first_ = nullptr;
1151 }
1152
1153 // Adds a new entry at the beginning of the use list and returns
1154 // the newly created node.
1155 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001156 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001157 if (IsEmpty()) {
1158 first_ = new_node;
1159 } else {
1160 first_->prev_ = new_node;
1161 new_node->next_ = first_;
1162 first_ = new_node;
1163 }
1164 return new_node;
1165 }
1166
1167 HUseListNode<T>* GetFirst() const {
1168 return first_;
1169 }
1170
1171 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001172 DCHECK(node != nullptr);
1173 DCHECK(Contains(node));
1174
David Brazdiled596192015-01-23 10:39:45 +00001175 if (node->prev_ != nullptr) {
1176 node->prev_->next_ = node->next_;
1177 }
1178 if (node->next_ != nullptr) {
1179 node->next_->prev_ = node->prev_;
1180 }
1181 if (node == first_) {
1182 first_ = node->next_;
1183 }
1184 }
1185
David Brazdil1abb4192015-02-17 18:33:36 +00001186 bool Contains(const HUseListNode<T>* node) const {
1187 if (node == nullptr) {
1188 return false;
1189 }
1190 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1191 if (current == node) {
1192 return true;
1193 }
1194 }
1195 return false;
1196 }
1197
David Brazdiled596192015-01-23 10:39:45 +00001198 bool IsEmpty() const {
1199 return first_ == nullptr;
1200 }
1201
1202 bool HasOnlyOneUse() const {
1203 return first_ != nullptr && first_->next_ == nullptr;
1204 }
1205
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001206 size_t SizeSlow() const {
1207 size_t count = 0;
1208 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1209 ++count;
1210 }
1211 return count;
1212 }
1213
David Brazdiled596192015-01-23 10:39:45 +00001214 private:
1215 HUseListNode<T>* first_;
1216};
1217
1218template<typename T>
1219class HUseIterator : public ValueObject {
1220 public:
1221 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1222
1223 bool Done() const { return current_ == nullptr; }
1224
1225 void Advance() {
1226 DCHECK(!Done());
1227 current_ = current_->GetNext();
1228 }
1229
1230 HUseListNode<T>* Current() const {
1231 DCHECK(!Done());
1232 return current_;
1233 }
1234
1235 private:
1236 HUseListNode<T>* current_;
1237
1238 friend class HValue;
1239};
1240
David Brazdil1abb4192015-02-17 18:33:36 +00001241// This class is used by HEnvironment and HInstruction classes to record the
1242// instructions they use and pointers to the corresponding HUseListNodes kept
1243// by the used instructions.
1244template <typename T>
1245class HUserRecord : public ValueObject {
1246 public:
1247 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1248 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1249
1250 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1251 : instruction_(old_record.instruction_), use_node_(use_node) {
1252 DCHECK(instruction_ != nullptr);
1253 DCHECK(use_node_ != nullptr);
1254 DCHECK(old_record.use_node_ == nullptr);
1255 }
1256
1257 HInstruction* GetInstruction() const { return instruction_; }
1258 HUseListNode<T>* GetUseNode() const { return use_node_; }
1259
1260 private:
1261 // Instruction used by the user.
1262 HInstruction* instruction_;
1263
1264 // Corresponding entry in the use list kept by 'instruction_'.
1265 HUseListNode<T>* use_node_;
1266};
1267
Aart Bik854a02b2015-07-14 16:07:00 -07001268/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001269 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001270 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001271 * For write/read dependences on fields/arrays, the dependence analysis uses
1272 * type disambiguation (e.g. a float field write cannot modify the value of an
1273 * integer field read) and the access type (e.g. a reference array write cannot
1274 * modify the value of a reference field read [although it may modify the
1275 * reference fetch prior to reading the field, which is represented by its own
1276 * write/read dependence]). The analysis makes conservative points-to
1277 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1278 * the same, and any reference read depends on any reference read without
1279 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001280 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001281 * The internal representation uses 38-bit and is described in the table below.
1282 * The first line indicates the side effect, and for field/array accesses the
1283 * second line indicates the type of the access (in the order of the
1284 * Primitive::Type enum).
1285 * The two numbered lines below indicate the bit position in the bitfield (read
1286 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001287 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001288 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1289 * +-------------+---------+---------+--------------+---------+---------+
1290 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1291 * | 3 |333333322|222222221| 1 |111111110|000000000|
1292 * | 7 |654321098|765432109| 8 |765432109|876543210|
1293 *
1294 * Note that, to ease the implementation, 'changes' bits are least significant
1295 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001296 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001297class SideEffects : public ValueObject {
1298 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001299 SideEffects() : flags_(0) {}
1300
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001301 static SideEffects None() {
1302 return SideEffects(0);
1303 }
1304
1305 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001306 return SideEffects(kAllChangeBits | kAllDependOnBits);
1307 }
1308
1309 static SideEffects AllChanges() {
1310 return SideEffects(kAllChangeBits);
1311 }
1312
1313 static SideEffects AllDependencies() {
1314 return SideEffects(kAllDependOnBits);
1315 }
1316
1317 static SideEffects AllExceptGCDependency() {
1318 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1319 }
1320
1321 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001322 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001323 }
1324
Aart Bik34c3ba92015-07-20 14:08:59 -07001325 static SideEffects AllWrites() {
1326 return SideEffects(kAllWrites);
1327 }
1328
1329 static SideEffects AllReads() {
1330 return SideEffects(kAllReads);
1331 }
1332
1333 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1334 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001335 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001336 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001337 }
1338
Aart Bik854a02b2015-07-14 16:07:00 -07001339 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1340 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001341 }
1342
Aart Bik34c3ba92015-07-20 14:08:59 -07001343 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1344 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001345 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001346 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001347 }
1348
1349 static SideEffects ArrayReadOfType(Primitive::Type type) {
1350 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1351 }
1352
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001353 static SideEffects CanTriggerGC() {
1354 return SideEffects(1ULL << kCanTriggerGCBit);
1355 }
1356
1357 static SideEffects DependsOnGC() {
1358 return SideEffects(1ULL << kDependsOnGCBit);
1359 }
1360
Aart Bik854a02b2015-07-14 16:07:00 -07001361 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001362 SideEffects Union(SideEffects other) const {
1363 return SideEffects(flags_ | other.flags_);
1364 }
1365
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001366 SideEffects Exclusion(SideEffects other) const {
1367 return SideEffects(flags_ & ~other.flags_);
1368 }
1369
1370 bool Includes(SideEffects other) const {
1371 return (other.flags_ & flags_) == other.flags_;
1372 }
1373
1374 bool HasSideEffects() const {
1375 return (flags_ & kAllChangeBits);
1376 }
1377
1378 bool HasDependencies() const {
1379 return (flags_ & kAllDependOnBits);
1380 }
1381
1382 // Returns true if there are no side effects or dependencies.
1383 bool DoesNothing() const {
1384 return flags_ == 0;
1385 }
1386
Aart Bik854a02b2015-07-14 16:07:00 -07001387 // Returns true if something is written.
1388 bool DoesAnyWrite() const {
1389 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001390 }
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392 // Returns true if something is read.
1393 bool DoesAnyRead() const {
1394 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001395 }
1396
Aart Bik854a02b2015-07-14 16:07:00 -07001397 // Returns true if potentially everything is written and read
1398 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001399 bool DoesAllReadWrite() const {
1400 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1401 }
1402
Aart Bik854a02b2015-07-14 16:07:00 -07001403 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001405 }
1406
1407 // Returns true if this may read something written by other.
1408 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001409 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1410 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001411 }
1412
1413 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001415 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001416 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 for (int s = kLastBit; s >= 0; s--) {
1418 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1419 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1420 // This is a bit for the GC side effect.
1421 if (current_bit_is_set) {
1422 flags += "GC";
1423 }
Aart Bik854a02b2015-07-14 16:07:00 -07001424 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001425 } else {
1426 // This is a bit for the array/field analysis.
1427 // The underscore character stands for the 'can trigger GC' bit.
1428 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1429 if (current_bit_is_set) {
1430 flags += kDebug[s];
1431 }
1432 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1433 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1434 flags += "|";
1435 }
1436 }
Aart Bik854a02b2015-07-14 16:07:00 -07001437 }
1438 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001439 }
1440
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001442
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 private:
1444 static constexpr int kFieldArrayAnalysisBits = 9;
1445
1446 static constexpr int kFieldWriteOffset = 0;
1447 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1448 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1449 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1450
1451 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1452
1453 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1454 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1455 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1456 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1457
1458 static constexpr int kLastBit = kDependsOnGCBit;
1459 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1460
1461 // Aliases.
1462
1463 static_assert(kChangeBits == kDependOnBits,
1464 "the 'change' bits should match the 'depend on' bits.");
1465
1466 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1467 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1468 static constexpr uint64_t kAllWrites =
1469 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1470 static constexpr uint64_t kAllReads =
1471 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001472
Aart Bik854a02b2015-07-14 16:07:00 -07001473 // Work around the fact that HIR aliases I/F and J/D.
1474 // TODO: remove this interceptor once HIR types are clean
1475 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1476 switch (type) {
1477 case Primitive::kPrimInt:
1478 case Primitive::kPrimFloat:
1479 return TypeFlag(Primitive::kPrimInt, offset) |
1480 TypeFlag(Primitive::kPrimFloat, offset);
1481 case Primitive::kPrimLong:
1482 case Primitive::kPrimDouble:
1483 return TypeFlag(Primitive::kPrimLong, offset) |
1484 TypeFlag(Primitive::kPrimDouble, offset);
1485 default:
1486 return TypeFlag(type, offset);
1487 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001488 }
1489
Aart Bik854a02b2015-07-14 16:07:00 -07001490 // Translates type to bit flag.
1491 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1492 CHECK_NE(type, Primitive::kPrimVoid);
1493 const uint64_t one = 1;
1494 const int shift = type; // 0-based consecutive enum
1495 DCHECK_LE(kFieldWriteOffset, shift);
1496 DCHECK_LT(shift, kArrayWriteOffset);
1497 return one << (type + offset);
1498 }
1499
1500 // Private constructor on direct flags value.
1501 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1502
1503 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504};
1505
David Brazdiled596192015-01-23 10:39:45 +00001506// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001507class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001508 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001509 HEnvironment(ArenaAllocator* arena,
1510 size_t number_of_vregs,
1511 const DexFile& dex_file,
1512 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001513 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001514 InvokeType invoke_type,
1515 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001516 : vregs_(arena, number_of_vregs),
1517 locations_(arena, number_of_vregs),
1518 parent_(nullptr),
1519 dex_file_(dex_file),
1520 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001521 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001522 invoke_type_(invoke_type),
1523 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001524 vregs_.SetSize(number_of_vregs);
1525 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001526 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001527 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001528
1529 locations_.SetSize(number_of_vregs);
1530 for (size_t i = 0; i < number_of_vregs; ++i) {
1531 locations_.Put(i, Location());
1532 }
1533 }
1534
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001535 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001536 : HEnvironment(arena,
1537 to_copy.Size(),
1538 to_copy.GetDexFile(),
1539 to_copy.GetMethodIdx(),
1540 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001541 to_copy.GetInvokeType(),
1542 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001543
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001545 if (parent_ != nullptr) {
1546 parent_->SetAndCopyParentChain(allocator, parent);
1547 } else {
1548 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1549 parent_->CopyFrom(parent);
1550 if (parent->GetParent() != nullptr) {
1551 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1552 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001553 }
David Brazdiled596192015-01-23 10:39:45 +00001554 }
1555
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001556 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1557 void CopyFrom(HEnvironment* environment);
1558
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001559 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1560 // input to the loop phi instead. This is for inserting instructions that
1561 // require an environment (like HDeoptimization) in the loop pre-header.
1562 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001563
1564 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001565 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001566 }
1567
1568 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001569 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001570 }
1571
David Brazdil1abb4192015-02-17 18:33:36 +00001572 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001573
1574 size_t Size() const { return vregs_.Size(); }
1575
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001576 HEnvironment* GetParent() const { return parent_; }
1577
1578 void SetLocationAt(size_t index, Location location) {
1579 locations_.Put(index, location);
1580 }
1581
1582 Location GetLocationAt(size_t index) const {
1583 return locations_.Get(index);
1584 }
1585
1586 uint32_t GetDexPc() const {
1587 return dex_pc_;
1588 }
1589
1590 uint32_t GetMethodIdx() const {
1591 return method_idx_;
1592 }
1593
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001594 InvokeType GetInvokeType() const {
1595 return invoke_type_;
1596 }
1597
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001598 const DexFile& GetDexFile() const {
1599 return dex_file_;
1600 }
1601
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001602 HInstruction* GetHolder() const {
1603 return holder_;
1604 }
1605
David Brazdiled596192015-01-23 10:39:45 +00001606 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001607 // Record instructions' use entries of this environment for constant-time removal.
1608 // It should only be called by HInstruction when a new environment use is added.
1609 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1610 DCHECK(env_use->GetUser() == this);
1611 size_t index = env_use->GetIndex();
1612 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1613 }
David Brazdiled596192015-01-23 10:39:45 +00001614
David Brazdil1abb4192015-02-17 18:33:36 +00001615 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001616 GrowableArray<Location> locations_;
1617 HEnvironment* parent_;
1618 const DexFile& dex_file_;
1619 const uint32_t method_idx_;
1620 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001621 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001622
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001623 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001624 HInstruction* const holder_;
1625
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001626 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001627
1628 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1629};
1630
Calin Juravleacf735c2015-02-12 15:25:22 +00001631class ReferenceTypeInfo : ValueObject {
1632 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001633 typedef Handle<mirror::Class> TypeHandle;
1634
Calin Juravle2e768302015-07-28 14:41:11 +00001635 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1636 // The constructor will check that the type_handle is valid.
1637 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001638 }
1639
Calin Juravle2e768302015-07-28 14:41:11 +00001640 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1641
1642 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1643 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001644 }
1645
Calin Juravle2e768302015-07-28 14:41:11 +00001646 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1647 return IsValidHandle(type_handle_);
1648 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001649 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001650
1651 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1652 DCHECK(IsValid());
1653 return GetTypeHandle()->IsObjectClass();
1654 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001655 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001656 DCHECK(IsValid());
1657 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001658 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001659
1660 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1661
Mathieu Chartier90443472015-07-16 20:32:27 -07001662 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001663 DCHECK(IsValid());
1664 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001665 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1666 }
1667
1668 // Returns true if the type information provide the same amount of details.
1669 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001670 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001671 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001672 if (!IsValid() && !rti.IsValid()) {
1673 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001674 return true;
1675 }
Calin Juravle2e768302015-07-28 14:41:11 +00001676 if (!IsValid() || !rti.IsValid()) {
1677 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001678 return false;
1679 }
Calin Juravle2e768302015-07-28 14:41:11 +00001680 return IsExact() == rti.IsExact()
1681 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001682 }
1683
1684 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001685 ReferenceTypeInfo();
1686 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001687
Calin Juravleacf735c2015-02-12 15:25:22 +00001688 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001689 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001690 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001691 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001692 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001693};
1694
1695std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1696
Vladimir Markof9f64412015-09-02 14:05:49 +01001697class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001698 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001699 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700 : previous_(nullptr),
1701 next_(nullptr),
1702 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001703 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001704 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001705 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001706 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001707 locations_(nullptr),
1708 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001709 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001710 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001711 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001712
Dave Allison20dfc792014-06-16 20:44:29 -07001713 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001714
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001715#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001716 enum InstructionKind {
1717 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1718 };
1719#undef DECLARE_KIND
1720
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001721 HInstruction* GetNext() const { return next_; }
1722 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001723
Calin Juravle77520bc2015-01-12 18:45:46 +00001724 HInstruction* GetNextDisregardingMoves() const;
1725 HInstruction* GetPreviousDisregardingMoves() const;
1726
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001727 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001728 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001729 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001730 bool IsInBlock() const { return block_ != nullptr; }
1731 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001732 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001733
Roland Levillain6b879dd2014-09-22 17:13:44 +01001734 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001735 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001736
1737 virtual void Accept(HGraphVisitor* visitor) = 0;
1738 virtual const char* DebugName() const = 0;
1739
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001741 void SetRawInputAt(size_t index, HInstruction* input) {
1742 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1743 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001744
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001745 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001746
1747 uint32_t GetDexPc() const { return dex_pc_; }
1748
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001749 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001750
Roland Levillaine161a2a2014-10-03 12:45:18 +01001751 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001752 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001753
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001754 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001755 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001756
Calin Juravle10e244f2015-01-26 18:54:32 +00001757 // Does not apply for all instructions, but having this at top level greatly
1758 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001759 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001760 virtual bool CanBeNull() const {
1761 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1762 return true;
1763 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001764
Calin Juravle641547a2015-04-21 22:08:51 +01001765 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1766 UNUSED(obj);
1767 return false;
1768 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001769
Calin Juravle2e768302015-07-28 14:41:11 +00001770 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001771
Calin Juravle61d544b2015-02-23 16:46:57 +00001772 ReferenceTypeInfo GetReferenceTypeInfo() const {
1773 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1774 return reference_type_info_;
1775 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001776
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001777 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001778 DCHECK(user != nullptr);
1779 HUseListNode<HInstruction*>* use =
1780 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1781 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001782 }
1783
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001784 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001785 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001786 HUseListNode<HEnvironment*>* env_use =
1787 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1788 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001789 }
1790
David Brazdil1abb4192015-02-17 18:33:36 +00001791 void RemoveAsUserOfInput(size_t input) {
1792 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1793 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1794 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795
David Brazdil1abb4192015-02-17 18:33:36 +00001796 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1797 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798
David Brazdiled596192015-01-23 10:39:45 +00001799 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1800 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001801 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001802 bool HasOnlyOneNonEnvironmentUse() const {
1803 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1804 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805
Roland Levillain6c82d402014-10-13 16:10:27 +01001806 // Does this instruction strictly dominate `other_instruction`?
1807 // Returns false if this instruction and `other_instruction` are the same.
1808 // Aborts if this instruction and `other_instruction` are both phis.
1809 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001810
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001811 int GetId() const { return id_; }
1812 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001813
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001814 int GetSsaIndex() const { return ssa_index_; }
1815 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1816 bool HasSsaIndex() const { return ssa_index_ != -1; }
1817
1818 bool HasEnvironment() const { return environment_ != nullptr; }
1819 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001820 // Set the `environment_` field. Raw because this method does not
1821 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001822 void SetRawEnvironment(HEnvironment* environment) {
1823 DCHECK(environment_ == nullptr);
1824 DCHECK_EQ(environment->GetHolder(), this);
1825 environment_ = environment;
1826 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001827
1828 // Set the environment of this instruction, copying it from `environment`. While
1829 // copying, the uses lists are being updated.
1830 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001831 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001832 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001833 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001834 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001835 if (environment->GetParent() != nullptr) {
1836 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1837 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001838 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001839
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001840 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1841 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001842 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001843 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001844 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001845 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001846 if (environment->GetParent() != nullptr) {
1847 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1848 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001849 }
1850
Nicolas Geoffray39468442014-09-02 15:17:15 +01001851 // Returns the number of entries in the environment. Typically, that is the
1852 // number of dex registers in a method. It could be more in case of inlining.
1853 size_t EnvironmentSize() const;
1854
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001855 LocationSummary* GetLocations() const { return locations_; }
1856 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001857
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001859 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860
Alexandre Rames188d4312015-04-09 18:30:21 +01001861 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1862 // uses of this instruction by `other` are *not* updated.
1863 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1864 ReplaceWith(other);
1865 other->ReplaceInput(this, use_index);
1866 }
1867
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001868 // Move `this` instruction before `cursor`.
1869 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001870
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001871#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001872 bool Is##type() const { return (As##type() != nullptr); } \
1873 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001874 virtual H##type* As##type() { return nullptr; }
1875
1876 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1877#undef INSTRUCTION_TYPE_CHECK
1878
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001879 // Returns whether the instruction can be moved within the graph.
1880 virtual bool CanBeMoved() const { return false; }
1881
1882 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001883 virtual bool InstructionTypeEquals(HInstruction* other) const {
1884 UNUSED(other);
1885 return false;
1886 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001887
1888 // Returns whether any data encoded in the two instructions is equal.
1889 // This method does not look at the inputs. Both instructions must be
1890 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001891 virtual bool InstructionDataEquals(HInstruction* other) const {
1892 UNUSED(other);
1893 return false;
1894 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001895
1896 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001897 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001898 // 2) Their inputs are identical.
1899 bool Equals(HInstruction* other) const;
1900
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001901 virtual InstructionKind GetKind() const = 0;
1902
1903 virtual size_t ComputeHashCode() const {
1904 size_t result = GetKind();
1905 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1906 result = (result * 31) + InputAt(i)->GetId();
1907 }
1908 return result;
1909 }
1910
1911 SideEffects GetSideEffects() const { return side_effects_; }
1912
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001913 size_t GetLifetimePosition() const { return lifetime_position_; }
1914 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1915 LiveInterval* GetLiveInterval() const { return live_interval_; }
1916 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1917 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1918
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001919 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1920
1921 // Returns whether the code generation of the instruction will require to have access
1922 // to the current method. Such instructions are:
1923 // (1): Instructions that require an environment, as calling the runtime requires
1924 // to walk the stack and have the current method stored at a specific stack address.
1925 // (2): Object literals like classes and strings, that are loaded from the dex cache
1926 // fields of the current method.
1927 bool NeedsCurrentMethod() const {
1928 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1929 }
1930
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001931 virtual bool NeedsDexCache() const { return false; }
1932
Mark Mendellc4701932015-04-10 13:18:51 -04001933 // Does this instruction have any use in an environment before
1934 // control flow hits 'other'?
1935 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1936
1937 // Remove all references to environment uses of this instruction.
1938 // The caller must ensure that this is safe to do.
1939 void RemoveEnvironmentUsers();
1940
David Brazdil1abb4192015-02-17 18:33:36 +00001941 protected:
1942 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1943 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1944
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001945 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001946 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1947
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948 HInstruction* previous_;
1949 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001950 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001951 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001952
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001953 // An instruction gets an id when it is added to the graph.
1954 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001955 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956 int id_;
1957
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001958 // When doing liveness analysis, instructions that have uses get an SSA index.
1959 int ssa_index_;
1960
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001962 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001963
1964 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001965 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 // The environment associated with this instruction. Not null if the instruction
1968 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001969 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001970
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971 // Set by the code generator.
1972 LocationSummary* locations_;
1973
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001974 // Set by the liveness analysis.
1975 LiveInterval* live_interval_;
1976
1977 // Set by the liveness analysis, this is the position in a linear
1978 // order of blocks where this instruction's live interval start.
1979 size_t lifetime_position_;
1980
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 const SideEffects side_effects_;
1982
Calin Juravleacf735c2015-02-12 15:25:22 +00001983 // TODO: for primitive types this should be marked as invalid.
1984 ReferenceTypeInfo reference_type_info_;
1985
David Brazdil1abb4192015-02-17 18:33:36 +00001986 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001988 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001989 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001991
1992 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1993};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001994std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001995
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996class HInputIterator : public ValueObject {
1997 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001998 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001999
2000 bool Done() const { return index_ == instruction_->InputCount(); }
2001 HInstruction* Current() const { return instruction_->InputAt(index_); }
2002 void Advance() { index_++; }
2003
2004 private:
2005 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002006 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007
2008 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2009};
2010
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002011class HInstructionIterator : public ValueObject {
2012 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002013 explicit HInstructionIterator(const HInstructionList& instructions)
2014 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002015 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016 }
2017
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002018 bool Done() const { return instruction_ == nullptr; }
2019 HInstruction* Current() const { return instruction_; }
2020 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002022 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002023 }
2024
2025 private:
2026 HInstruction* instruction_;
2027 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002028
2029 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002030};
2031
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002032class HBackwardInstructionIterator : public ValueObject {
2033 public:
2034 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2035 : instruction_(instructions.last_instruction_) {
2036 next_ = Done() ? nullptr : instruction_->GetPrevious();
2037 }
2038
2039 bool Done() const { return instruction_ == nullptr; }
2040 HInstruction* Current() const { return instruction_; }
2041 void Advance() {
2042 instruction_ = next_;
2043 next_ = Done() ? nullptr : instruction_->GetPrevious();
2044 }
2045
2046 private:
2047 HInstruction* instruction_;
2048 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002049
2050 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002051};
2052
Vladimir Markof9f64412015-09-02 14:05:49 +01002053template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002054class HTemplateInstruction: public HInstruction {
2055 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002056 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2057 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002058 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002059
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002060 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002061
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002062 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002063 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2064 DCHECK_LT(i, N);
2065 return inputs_[i];
2066 }
David Brazdil1abb4192015-02-17 18:33:36 +00002067
2068 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002069 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002070 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002071 }
2072
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002073 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002074 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002075
2076 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077};
2078
Vladimir Markof9f64412015-09-02 14:05:49 +01002079// HTemplateInstruction specialization for N=0.
2080template<>
2081class HTemplateInstruction<0>: public HInstruction {
2082 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002083 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2084 : HInstruction(side_effects, dex_pc) {}
2085
Vladimir Markof9f64412015-09-02 14:05:49 +01002086 virtual ~HTemplateInstruction() {}
2087
2088 size_t InputCount() const OVERRIDE { return 0; }
2089
2090 protected:
2091 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2092 LOG(FATAL) << "Unreachable";
2093 UNREACHABLE();
2094 }
2095
2096 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2097 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2098 LOG(FATAL) << "Unreachable";
2099 UNREACHABLE();
2100 }
2101
2102 private:
2103 friend class SsaBuilder;
2104};
2105
Dave Allison20dfc792014-06-16 20:44:29 -07002106template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002107class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002108 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002109 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2110 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002111 virtual ~HExpression() {}
2112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002113 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002114
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002115 protected:
2116 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002117};
2118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2120// instruction that branches to the exit block.
2121class HReturnVoid : public HTemplateInstruction<0> {
2122 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002123 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2124 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002125
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002126 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002128 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129
2130 private:
2131 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2132};
2133
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002134// Represents dex's RETURN opcodes. A HReturn is a control flow
2135// instruction that branches to the exit block.
2136class HReturn : public HTemplateInstruction<1> {
2137 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002138 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2139 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002140 SetRawInputAt(0, value);
2141 }
2142
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002143 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002144
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002145 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002146
2147 private:
2148 DISALLOW_COPY_AND_ASSIGN(HReturn);
2149};
2150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002152// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002153// exit block.
2154class HExit : public HTemplateInstruction<0> {
2155 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002156 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002157
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002158 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002160 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161
2162 private:
2163 DISALLOW_COPY_AND_ASSIGN(HExit);
2164};
2165
2166// Jumps from one block to another.
2167class HGoto : public HTemplateInstruction<0> {
2168 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002169 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002170
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002171 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002172
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002173 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002174 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002175 }
2176
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002177 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178
2179 private:
2180 DISALLOW_COPY_AND_ASSIGN(HGoto);
2181};
2182
Roland Levillain9867bc72015-08-05 10:21:34 +01002183class HConstant : public HExpression<0> {
2184 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002185 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2186 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002187
2188 bool CanBeMoved() const OVERRIDE { return true; }
2189
2190 virtual bool IsMinusOne() const { return false; }
2191 virtual bool IsZero() const { return false; }
2192 virtual bool IsOne() const { return false; }
2193
David Brazdil77a48ae2015-09-15 12:34:04 +00002194 virtual uint64_t GetValueAsUint64() const = 0;
2195
Roland Levillain9867bc72015-08-05 10:21:34 +01002196 DECLARE_INSTRUCTION(Constant);
2197
2198 private:
2199 DISALLOW_COPY_AND_ASSIGN(HConstant);
2200};
2201
2202class HNullConstant : public HConstant {
2203 public:
2204 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2205 return true;
2206 }
2207
David Brazdil77a48ae2015-09-15 12:34:04 +00002208 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2209
Roland Levillain9867bc72015-08-05 10:21:34 +01002210 size_t ComputeHashCode() const OVERRIDE { return 0; }
2211
2212 DECLARE_INSTRUCTION(NullConstant);
2213
2214 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002215 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002216
2217 friend class HGraph;
2218 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2219};
2220
2221// Constants of the type int. Those can be from Dex instructions, or
2222// synthesized (for example with the if-eqz instruction).
2223class HIntConstant : public HConstant {
2224 public:
2225 int32_t GetValue() const { return value_; }
2226
David Brazdil77a48ae2015-09-15 12:34:04 +00002227 uint64_t GetValueAsUint64() const OVERRIDE { return static_cast<uint64_t>(value_); }
2228
Roland Levillain9867bc72015-08-05 10:21:34 +01002229 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2230 DCHECK(other->IsIntConstant());
2231 return other->AsIntConstant()->value_ == value_;
2232 }
2233
2234 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2235
2236 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2237 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2238 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2239
2240 DECLARE_INSTRUCTION(IntConstant);
2241
2242 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002243 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2244 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2245 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2246 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002247
2248 const int32_t value_;
2249
2250 friend class HGraph;
2251 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2252 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2253 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2254};
2255
2256class HLongConstant : public HConstant {
2257 public:
2258 int64_t GetValue() const { return value_; }
2259
David Brazdil77a48ae2015-09-15 12:34:04 +00002260 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2261
Roland Levillain9867bc72015-08-05 10:21:34 +01002262 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2263 DCHECK(other->IsLongConstant());
2264 return other->AsLongConstant()->value_ == value_;
2265 }
2266
2267 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2268
2269 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2270 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2271 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2272
2273 DECLARE_INSTRUCTION(LongConstant);
2274
2275 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002276 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2277 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002278
2279 const int64_t value_;
2280
2281 friend class HGraph;
2282 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2283};
Dave Allison20dfc792014-06-16 20:44:29 -07002284
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002285// Conditional branch. A block ending with an HIf instruction must have
2286// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002287class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002288 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002289 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2290 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002291 SetRawInputAt(0, input);
2292 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002293
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002294 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002295
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002296 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002297 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002298 }
2299
2300 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002301 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002302 }
2303
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002304 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002305
2306 private:
2307 DISALLOW_COPY_AND_ASSIGN(HIf);
2308};
2309
David Brazdilfc6a86a2015-06-26 10:33:45 +00002310
2311// Abstract instruction which marks the beginning and/or end of a try block and
2312// links it to the respective exception handlers. Behaves the same as a Goto in
2313// non-exceptional control flow.
2314// Normal-flow successor is stored at index zero, exception handlers under
2315// higher indices in no particular order.
2316class HTryBoundary : public HTemplateInstruction<0> {
2317 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002318 enum BoundaryKind {
2319 kEntry,
2320 kExit,
2321 };
2322
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002323 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2324 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002325
2326 bool IsControlFlow() const OVERRIDE { return true; }
2327
2328 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002329 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002330
2331 // Returns whether `handler` is among its exception handlers (non-zero index
2332 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002333 bool HasExceptionHandler(const HBasicBlock& handler) const {
2334 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002335 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002336 }
2337
2338 // If not present already, adds `handler` to its block's list of exception
2339 // handlers.
2340 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002341 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002342 GetBlock()->AddSuccessor(handler);
2343 }
2344 }
2345
David Brazdil56e1acc2015-06-30 15:41:36 +01002346 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002347
David Brazdilffee3d32015-07-06 11:48:53 +01002348 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2349
David Brazdilfc6a86a2015-06-26 10:33:45 +00002350 DECLARE_INSTRUCTION(TryBoundary);
2351
2352 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002353 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002354
2355 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2356};
2357
David Brazdilffee3d32015-07-06 11:48:53 +01002358// Iterator over exception handlers of a given HTryBoundary, i.e. over
2359// exceptional successors of its basic block.
2360class HExceptionHandlerIterator : public ValueObject {
2361 public:
2362 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2363 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2364
Vladimir Marko60584552015-09-03 13:35:12 +00002365 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2366 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002367 size_t CurrentSuccessorIndex() const { return index_; }
2368 void Advance() { ++index_; }
2369
2370 private:
2371 const HBasicBlock& block_;
2372 size_t index_;
2373
2374 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2375};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002376
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002377// Deoptimize to interpreter, upon checking a condition.
2378class HDeoptimize : public HTemplateInstruction<1> {
2379 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002380 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2381 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002382 SetRawInputAt(0, cond);
2383 }
2384
2385 bool NeedsEnvironment() const OVERRIDE { return true; }
2386 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002387
2388 DECLARE_INSTRUCTION(Deoptimize);
2389
2390 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002391 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2392};
2393
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002394// Represents the ArtMethod that was passed as a first argument to
2395// the method. It is used by instructions that depend on it, like
2396// instructions that work with the dex cache.
2397class HCurrentMethod : public HExpression<0> {
2398 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002399 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2400 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002401
2402 DECLARE_INSTRUCTION(CurrentMethod);
2403
2404 private:
2405 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2406};
2407
Roland Levillain88cb1752014-10-20 16:36:47 +01002408class HUnaryOperation : public HExpression<1> {
2409 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002410 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2411 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002412 SetRawInputAt(0, input);
2413 }
2414
2415 HInstruction* GetInput() const { return InputAt(0); }
2416 Primitive::Type GetResultType() const { return GetType(); }
2417
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002418 bool CanBeMoved() const OVERRIDE { return true; }
2419 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002420 UNUSED(other);
2421 return true;
2422 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002423
Roland Levillain9240d6a2014-10-20 16:47:04 +01002424 // Try to statically evaluate `operation` and return a HConstant
2425 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002426 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002427 HConstant* TryStaticEvaluation() const;
2428
2429 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002430 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2431 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002432
Roland Levillain88cb1752014-10-20 16:36:47 +01002433 DECLARE_INSTRUCTION(UnaryOperation);
2434
2435 private:
2436 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2437};
2438
Dave Allison20dfc792014-06-16 20:44:29 -07002439class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002440 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002441 HBinaryOperation(Primitive::Type result_type,
2442 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002443 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002444 SideEffects side_effects = SideEffects::None(),
2445 uint32_t dex_pc = kNoDexPc)
2446 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002447 SetRawInputAt(0, left);
2448 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002449 }
2450
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002451 HInstruction* GetLeft() const { return InputAt(0); }
2452 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002453 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002454
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002455 virtual bool IsCommutative() const { return false; }
2456
2457 // Put constant on the right.
2458 // Returns whether order is changed.
2459 bool OrderInputsWithConstantOnTheRight() {
2460 HInstruction* left = InputAt(0);
2461 HInstruction* right = InputAt(1);
2462 if (left->IsConstant() && !right->IsConstant()) {
2463 ReplaceInput(right, 0);
2464 ReplaceInput(left, 1);
2465 return true;
2466 }
2467 return false;
2468 }
2469
2470 // Order inputs by instruction id, but favor constant on the right side.
2471 // This helps GVN for commutative ops.
2472 void OrderInputs() {
2473 DCHECK(IsCommutative());
2474 HInstruction* left = InputAt(0);
2475 HInstruction* right = InputAt(1);
2476 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2477 return;
2478 }
2479 if (OrderInputsWithConstantOnTheRight()) {
2480 return;
2481 }
2482 // Order according to instruction id.
2483 if (left->GetId() > right->GetId()) {
2484 ReplaceInput(right, 0);
2485 ReplaceInput(left, 1);
2486 }
2487 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002489 bool CanBeMoved() const OVERRIDE { return true; }
2490 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002491 UNUSED(other);
2492 return true;
2493 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002494
Roland Levillain9240d6a2014-10-20 16:47:04 +01002495 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002496 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002497 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002498 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002499
2500 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002501 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2502 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2503 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2504 HLongConstant* y ATTRIBUTE_UNUSED) const {
2505 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2506 return nullptr;
2507 }
2508 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2509 HIntConstant* y ATTRIBUTE_UNUSED) const {
2510 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2511 return nullptr;
2512 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002513
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002514 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002515 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002516 HConstant* GetConstantRight() const;
2517
2518 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002519 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002520 HInstruction* GetLeastConstantLeft() const;
2521
Roland Levillainccc07a92014-09-16 14:48:16 +01002522 DECLARE_INSTRUCTION(BinaryOperation);
2523
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002524 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002525 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2526};
2527
Mark Mendellc4701932015-04-10 13:18:51 -04002528// The comparison bias applies for floating point operations and indicates how NaN
2529// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002530enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002531 kNoBias, // bias is not applicable (i.e. for long operation)
2532 kGtBias, // return 1 for NaN comparisons
2533 kLtBias, // return -1 for NaN comparisons
2534};
2535
Dave Allison20dfc792014-06-16 20:44:29 -07002536class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002538 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2539 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002540 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002541 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002542
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002543 bool NeedsMaterialization() const { return needs_materialization_; }
2544 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002545
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002546 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002547 // `instruction`, and disregard moves in between.
2548 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002549
Dave Allison20dfc792014-06-16 20:44:29 -07002550 DECLARE_INSTRUCTION(Condition);
2551
2552 virtual IfCondition GetCondition() const = 0;
2553
Mark Mendellc4701932015-04-10 13:18:51 -04002554 virtual IfCondition GetOppositeCondition() const = 0;
2555
Roland Levillain4fa13f62015-07-06 18:11:54 +01002556 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002557
2558 void SetBias(ComparisonBias bias) { bias_ = bias; }
2559
2560 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2561 return bias_ == other->AsCondition()->bias_;
2562 }
2563
Roland Levillain4fa13f62015-07-06 18:11:54 +01002564 bool IsFPConditionTrueIfNaN() const {
2565 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2566 IfCondition if_cond = GetCondition();
2567 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2568 }
2569
2570 bool IsFPConditionFalseIfNaN() const {
2571 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2572 IfCondition if_cond = GetCondition();
2573 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2574 }
2575
Dave Allison20dfc792014-06-16 20:44:29 -07002576 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002577 // For register allocation purposes, returns whether this instruction needs to be
2578 // materialized (that is, not just be in the processor flags).
2579 bool needs_materialization_;
2580
Mark Mendellc4701932015-04-10 13:18:51 -04002581 // Needed if we merge a HCompare into a HCondition.
2582 ComparisonBias bias_;
2583
Dave Allison20dfc792014-06-16 20:44:29 -07002584 DISALLOW_COPY_AND_ASSIGN(HCondition);
2585};
2586
2587// Instruction to check if two inputs are equal to each other.
2588class HEqual : public HCondition {
2589 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002590 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2591 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002592
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002593 bool IsCommutative() const OVERRIDE { return true; }
2594
Roland Levillain9867bc72015-08-05 10:21:34 +01002595 template <typename T> bool Compute(T x, T y) const { return x == y; }
2596
2597 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002598 return GetBlock()->GetGraph()->GetIntConstant(
2599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002600 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002601 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002602 return GetBlock()->GetGraph()->GetIntConstant(
2603 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002604 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002605
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002606 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002607
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002608 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002609 return kCondEQ;
2610 }
2611
Mark Mendellc4701932015-04-10 13:18:51 -04002612 IfCondition GetOppositeCondition() const OVERRIDE {
2613 return kCondNE;
2614 }
2615
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002616 private:
2617 DISALLOW_COPY_AND_ASSIGN(HEqual);
2618};
2619
Dave Allison20dfc792014-06-16 20:44:29 -07002620class HNotEqual : public HCondition {
2621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002622 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2623 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002625 bool IsCommutative() const OVERRIDE { return true; }
2626
Roland Levillain9867bc72015-08-05 10:21:34 +01002627 template <typename T> bool Compute(T x, T y) const { return x != y; }
2628
2629 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002630 return GetBlock()->GetGraph()->GetIntConstant(
2631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002632 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002634 return GetBlock()->GetGraph()->GetIntConstant(
2635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002636 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002637
Dave Allison20dfc792014-06-16 20:44:29 -07002638 DECLARE_INSTRUCTION(NotEqual);
2639
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002640 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002641 return kCondNE;
2642 }
2643
Mark Mendellc4701932015-04-10 13:18:51 -04002644 IfCondition GetOppositeCondition() const OVERRIDE {
2645 return kCondEQ;
2646 }
2647
Dave Allison20dfc792014-06-16 20:44:29 -07002648 private:
2649 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2650};
2651
2652class HLessThan : public HCondition {
2653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2655 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002656
Roland Levillain9867bc72015-08-05 10:21:34 +01002657 template <typename T> bool Compute(T x, T y) const { return x < y; }
2658
2659 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002660 return GetBlock()->GetGraph()->GetIntConstant(
2661 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002662 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002663 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002664 return GetBlock()->GetGraph()->GetIntConstant(
2665 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002666 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002667
Dave Allison20dfc792014-06-16 20:44:29 -07002668 DECLARE_INSTRUCTION(LessThan);
2669
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002670 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002671 return kCondLT;
2672 }
2673
Mark Mendellc4701932015-04-10 13:18:51 -04002674 IfCondition GetOppositeCondition() const OVERRIDE {
2675 return kCondGE;
2676 }
2677
Dave Allison20dfc792014-06-16 20:44:29 -07002678 private:
2679 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2680};
2681
2682class HLessThanOrEqual : public HCondition {
2683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2685 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002686
Roland Levillain9867bc72015-08-05 10:21:34 +01002687 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2688
2689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 return GetBlock()->GetGraph()->GetIntConstant(
2691 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002692 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002694 return GetBlock()->GetGraph()->GetIntConstant(
2695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002697
Dave Allison20dfc792014-06-16 20:44:29 -07002698 DECLARE_INSTRUCTION(LessThanOrEqual);
2699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002700 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002701 return kCondLE;
2702 }
2703
Mark Mendellc4701932015-04-10 13:18:51 -04002704 IfCondition GetOppositeCondition() const OVERRIDE {
2705 return kCondGT;
2706 }
2707
Dave Allison20dfc792014-06-16 20:44:29 -07002708 private:
2709 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2710};
2711
2712class HGreaterThan : public HCondition {
2713 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002714 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2715 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002716
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 template <typename T> bool Compute(T x, T y) const { return x > y; }
2718
2719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 return GetBlock()->GetGraph()->GetIntConstant(
2721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002722 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002723 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002724 return GetBlock()->GetGraph()->GetIntConstant(
2725 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002726 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002727
Dave Allison20dfc792014-06-16 20:44:29 -07002728 DECLARE_INSTRUCTION(GreaterThan);
2729
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002730 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002731 return kCondGT;
2732 }
2733
Mark Mendellc4701932015-04-10 13:18:51 -04002734 IfCondition GetOppositeCondition() const OVERRIDE {
2735 return kCondLE;
2736 }
2737
Dave Allison20dfc792014-06-16 20:44:29 -07002738 private:
2739 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2740};
2741
2742class HGreaterThanOrEqual : public HCondition {
2743 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2745 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002746
Roland Levillain9867bc72015-08-05 10:21:34 +01002747 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2748
2749 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002750 return GetBlock()->GetGraph()->GetIntConstant(
2751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002752 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002753 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002754 return GetBlock()->GetGraph()->GetIntConstant(
2755 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002756 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002757
Dave Allison20dfc792014-06-16 20:44:29 -07002758 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2759
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002760 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002761 return kCondGE;
2762 }
2763
Mark Mendellc4701932015-04-10 13:18:51 -04002764 IfCondition GetOppositeCondition() const OVERRIDE {
2765 return kCondLT;
2766 }
2767
Dave Allison20dfc792014-06-16 20:44:29 -07002768 private:
2769 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2770};
2771
2772
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002773// Instruction to check how two inputs compare to each other.
2774// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2775class HCompare : public HBinaryOperation {
2776 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002777 HCompare(Primitive::Type type,
2778 HInstruction* first,
2779 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002780 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002781 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 : HBinaryOperation(Primitive::kPrimInt,
2783 first,
2784 second,
2785 SideEffectsForArchRuntimeCalls(type),
2786 dex_pc),
2787 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002788 DCHECK_EQ(type, first->GetType());
2789 DCHECK_EQ(type, second->GetType());
2790 }
2791
Roland Levillain9867bc72015-08-05 10:21:34 +01002792 template <typename T>
2793 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002794
Roland Levillain9867bc72015-08-05 10:21:34 +01002795 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 return GetBlock()->GetGraph()->GetIntConstant(
2797 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002798 }
2799 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 return GetBlock()->GetGraph()->GetIntConstant(
2801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002802 }
2803
Calin Juravleddb7df22014-11-25 20:56:51 +00002804 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2805 return bias_ == other->AsCompare()->bias_;
2806 }
2807
Mark Mendellc4701932015-04-10 13:18:51 -04002808 ComparisonBias GetBias() const { return bias_; }
2809
Roland Levillain4fa13f62015-07-06 18:11:54 +01002810 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002811
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002812
2813 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2814 // MIPS64 uses a runtime call for FP comparisons.
2815 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2816 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002817
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002818 DECLARE_INSTRUCTION(Compare);
2819
2820 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002821 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002822
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002823 DISALLOW_COPY_AND_ASSIGN(HCompare);
2824};
2825
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002826// A local in the graph. Corresponds to a Dex register.
2827class HLocal : public HTemplateInstruction<0> {
2828 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002829 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002831
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002832 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002833
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002834 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002835
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002836 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002837 // The Dex register number.
2838 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002839
2840 DISALLOW_COPY_AND_ASSIGN(HLocal);
2841};
2842
2843// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002844class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002845 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2847 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002848 SetRawInputAt(0, local);
2849 }
2850
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002851 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2852
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002853 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002854
2855 private:
2856 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2857};
2858
2859// Store a value in a given local. This instruction has two inputs: the value
2860// and the local.
2861class HStoreLocal : public HTemplateInstruction<2> {
2862 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002863 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2864 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002865 SetRawInputAt(0, local);
2866 SetRawInputAt(1, value);
2867 }
2868
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002869 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2870
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002871 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002872
2873 private:
2874 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2875};
2876
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002877class HFloatConstant : public HConstant {
2878 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002879 float GetValue() const { return value_; }
2880
David Brazdil77a48ae2015-09-15 12:34:04 +00002881 uint64_t GetValueAsUint64() const OVERRIDE {
2882 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2883 }
2884
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002885 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002886 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002887 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002888 }
2889
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002890 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002891
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002892 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002893 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002894 }
2895 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002896 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002897 }
2898 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002899 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2900 }
2901 bool IsNaN() const {
2902 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002903 }
2904
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002905 DECLARE_INSTRUCTION(FloatConstant);
2906
2907 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002908 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2909 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2910 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2911 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002912
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002913 const float value_;
2914
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002915 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002916 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002917 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002918 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2919};
2920
2921class HDoubleConstant : public HConstant {
2922 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002923 double GetValue() const { return value_; }
2924
David Brazdil77a48ae2015-09-15 12:34:04 +00002925 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2926
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002927 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002928 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002929 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002930 }
2931
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002932 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002933
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002934 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002935 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002936 }
2937 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002938 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002939 }
2940 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002941 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2942 }
2943 bool IsNaN() const {
2944 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002945 }
2946
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002947 DECLARE_INSTRUCTION(DoubleConstant);
2948
2949 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002950 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2951 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2952 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2953 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002955 const double value_;
2956
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002957 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002958 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002959 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002960 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2961};
2962
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002963enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002964#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002965#include "intrinsics_list.h"
2966 kNone,
2967 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2968#undef INTRINSICS_LIST
2969#undef OPTIMIZING_INTRINSICS
2970};
2971std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2972
Agi Csaki05f20562015-08-19 14:58:14 -07002973enum IntrinsicNeedsEnvironmentOrCache {
2974 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2975 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002976};
2977
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002978class HInvoke : public HInstruction {
2979 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002980 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002981
2982 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2983 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002984 bool NeedsEnvironment() const OVERRIDE {
2985 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2986 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002987
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002988 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002989 SetRawInputAt(index, argument);
2990 }
2991
Roland Levillain3e3d7332015-04-28 11:00:54 +01002992 // Return the number of arguments. This number can be lower than
2993 // the number of inputs returned by InputCount(), as some invoke
2994 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2995 // inputs at the end of their list of inputs.
2996 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2997
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002998 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002999
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003000
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003001 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003002 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003003
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003004 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3005
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003006 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003007 return intrinsic_;
3008 }
3009
Agi Csaki05f20562015-08-19 14:58:14 -07003010 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003011 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003012 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003013 }
3014
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003015 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003016 return GetEnvironment()->GetParent() != nullptr;
3017 }
3018
3019 bool CanThrow() const OVERRIDE { return true; }
3020
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003021 DECLARE_INSTRUCTION(Invoke);
3022
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003023 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003024 HInvoke(ArenaAllocator* arena,
3025 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003026 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003027 Primitive::Type return_type,
3028 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003029 uint32_t dex_method_index,
3030 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003031 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003032 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003033 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003034 inputs_(arena, number_of_arguments),
3035 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003036 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003037 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003038 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003039 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003040 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
3041 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003042 }
3043
David Brazdil1abb4192015-02-17 18:33:36 +00003044 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3045 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3046 inputs_.Put(index, input);
3047 }
3048
Roland Levillain3e3d7332015-04-28 11:00:54 +01003049 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003050 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003051 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003052 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003053 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003054 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003055 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003056
3057 private:
3058 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3059};
3060
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003061class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003062 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003063 // Requirements of this method call regarding the class
3064 // initialization (clinit) check of its declaring class.
3065 enum class ClinitCheckRequirement {
3066 kNone, // Class already initialized.
3067 kExplicit, // Static call having explicit clinit check as last input.
3068 kImplicit, // Static call implicitly requiring a clinit check.
3069 };
3070
Vladimir Marko58155012015-08-19 12:49:41 +00003071 // Determines how to load the target ArtMethod*.
3072 enum class MethodLoadKind {
3073 // Use a String init ArtMethod* loaded from Thread entrypoints.
3074 kStringInit,
3075
3076 // Use the method's own ArtMethod* loaded by the register allocator.
3077 kRecursive,
3078
3079 // Use ArtMethod* at a known address, embed the direct address in the code.
3080 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3081 kDirectAddress,
3082
3083 // Use ArtMethod* at an address that will be known at link time, embed the direct
3084 // address in the code. If the image is relocatable, emit .patch_oat entry.
3085 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3086 // the image relocatable or not.
3087 kDirectAddressWithFixup,
3088
3089 // Load from resoved methods array in the dex cache using a PC-relative load.
3090 // Used when we need to use the dex cache, for example for invoke-static that
3091 // may cause class initialization (the entry may point to a resolution method),
3092 // and we know that we can access the dex cache arrays using a PC-relative load.
3093 kDexCachePcRelative,
3094
3095 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3096 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3097 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3098 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3099 kDexCacheViaMethod,
3100 };
3101
3102 // Determines the location of the code pointer.
3103 enum class CodePtrLocation {
3104 // Recursive call, use local PC-relative call instruction.
3105 kCallSelf,
3106
3107 // Use PC-relative call instruction patched at link time.
3108 // Used for calls within an oat file, boot->boot or app->app.
3109 kCallPCRelative,
3110
3111 // Call to a known target address, embed the direct address in code.
3112 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3113 kCallDirect,
3114
3115 // Call to a target address that will be known at link time, embed the direct
3116 // address in code. If the image is relocatable, emit .patch_oat entry.
3117 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3118 // the image relocatable or not.
3119 kCallDirectWithFixup,
3120
3121 // Use code pointer from the ArtMethod*.
3122 // Used when we don't know the target code. This is also the last-resort-kind used when
3123 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3124 kCallArtMethod,
3125 };
3126
3127 struct DispatchInfo {
3128 const MethodLoadKind method_load_kind;
3129 const CodePtrLocation code_ptr_location;
3130 // The method load data holds
3131 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3132 // Note that there are multiple string init methods, each having its own offset.
3133 // - the method address for kDirectAddress
3134 // - the dex cache arrays offset for kDexCachePcRel.
3135 const uint64_t method_load_data;
3136 const uint64_t direct_code_ptr;
3137 };
3138
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003139 HInvokeStaticOrDirect(ArenaAllocator* arena,
3140 uint32_t number_of_arguments,
3141 Primitive::Type return_type,
3142 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003143 uint32_t method_index,
3144 MethodReference target_method,
3145 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003146 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003147 InvokeType invoke_type,
3148 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003149 : HInvoke(arena,
3150 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003151 // There is one extra argument for the HCurrentMethod node, and
3152 // potentially one other if the clinit check is explicit, and one other
3153 // if the method is a string factory.
3154 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003155 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003156 return_type,
3157 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003158 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003159 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003160 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003161 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003162 target_method_(target_method),
3163 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003164
Calin Juravle641547a2015-04-21 22:08:51 +01003165 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3166 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003167 // We access the method via the dex cache so we can't do an implicit null check.
3168 // TODO: for intrinsics we can generate implicit null checks.
3169 return false;
3170 }
3171
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003172 bool CanBeNull() const OVERRIDE {
3173 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3174 }
3175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003176 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003177 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3178 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3179 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003180 bool NeedsDexCache() const OVERRIDE {
3181 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3182 return !IsRecursive() && !IsStringInit();
3183 }
Vladimir Marko58155012015-08-19 12:49:41 +00003184 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003185 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003186 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3187 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3188 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3189 MethodReference GetTargetMethod() const { return target_method_; }
3190
3191 int32_t GetStringInitOffset() const {
3192 DCHECK(IsStringInit());
3193 return dispatch_info_.method_load_data;
3194 }
3195
3196 uint64_t GetMethodAddress() const {
3197 DCHECK(HasMethodAddress());
3198 return dispatch_info_.method_load_data;
3199 }
3200
3201 uint32_t GetDexCacheArrayOffset() const {
3202 DCHECK(HasPcRelDexCache());
3203 return dispatch_info_.method_load_data;
3204 }
3205
3206 uint64_t GetDirectCodePtr() const {
3207 DCHECK(HasDirectCodePtr());
3208 return dispatch_info_.direct_code_ptr;
3209 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003210
Calin Juravle68ad6492015-08-18 17:08:12 +01003211 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3212
Roland Levillain4c0eb422015-04-24 16:43:49 +01003213 // Is this instruction a call to a static method?
3214 bool IsStatic() const {
3215 return GetInvokeType() == kStatic;
3216 }
3217
Roland Levillain3e3d7332015-04-28 11:00:54 +01003218 // Remove the art::HLoadClass instruction set as last input by
3219 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3220 // the initial art::HClinitCheck instruction (only relevant for
3221 // static calls with explicit clinit check).
3222 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003223 DCHECK(IsStaticWithExplicitClinitCheck());
3224 size_t last_input_index = InputCount() - 1;
3225 HInstruction* last_input = InputAt(last_input_index);
3226 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003227 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003228 RemoveAsUserOfInput(last_input_index);
3229 inputs_.DeleteAt(last_input_index);
3230 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3231 DCHECK(IsStaticWithImplicitClinitCheck());
3232 }
3233
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003234 bool IsStringFactoryFor(HFakeString* str) const {
3235 if (!IsStringInit()) return false;
3236 // +1 for the current method.
3237 if (InputCount() == (number_of_arguments_ + 1)) return false;
3238 return InputAt(InputCount() - 1)->AsFakeString() == str;
3239 }
3240
3241 void RemoveFakeStringArgumentAsLastInput() {
3242 DCHECK(IsStringInit());
3243 size_t last_input_index = InputCount() - 1;
3244 HInstruction* last_input = InputAt(last_input_index);
3245 DCHECK(last_input != nullptr);
3246 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3247 RemoveAsUserOfInput(last_input_index);
3248 inputs_.DeleteAt(last_input_index);
3249 }
3250
Roland Levillain4c0eb422015-04-24 16:43:49 +01003251 // Is this a call to a static method whose declaring class has an
3252 // explicit intialization check in the graph?
3253 bool IsStaticWithExplicitClinitCheck() const {
3254 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3255 }
3256
3257 // Is this a call to a static method whose declaring class has an
3258 // implicit intialization check requirement?
3259 bool IsStaticWithImplicitClinitCheck() const {
3260 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3261 }
3262
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003263 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003264
Roland Levillain4c0eb422015-04-24 16:43:49 +01003265 protected:
3266 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3267 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3268 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3269 HInstruction* input = input_record.GetInstruction();
3270 // `input` is the last input of a static invoke marked as having
3271 // an explicit clinit check. It must either be:
3272 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3273 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3274 DCHECK(input != nullptr);
3275 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3276 }
3277 return input_record;
3278 }
3279
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003280 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003281 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003282 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003283 // The target method may refer to different dex file or method index than the original
3284 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3285 // in derived class to increase visibility.
3286 MethodReference target_method_;
3287 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003288
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003289 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003290};
3291
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003292class HInvokeVirtual : public HInvoke {
3293 public:
3294 HInvokeVirtual(ArenaAllocator* arena,
3295 uint32_t number_of_arguments,
3296 Primitive::Type return_type,
3297 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003298 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003299 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003300 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003301 vtable_index_(vtable_index) {}
3302
Calin Juravle641547a2015-04-21 22:08:51 +01003303 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003304 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003305 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003306 }
3307
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003308 uint32_t GetVTableIndex() const { return vtable_index_; }
3309
3310 DECLARE_INSTRUCTION(InvokeVirtual);
3311
3312 private:
3313 const uint32_t vtable_index_;
3314
3315 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3316};
3317
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003318class HInvokeInterface : public HInvoke {
3319 public:
3320 HInvokeInterface(ArenaAllocator* arena,
3321 uint32_t number_of_arguments,
3322 Primitive::Type return_type,
3323 uint32_t dex_pc,
3324 uint32_t dex_method_index,
3325 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003326 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003327 imt_index_(imt_index) {}
3328
Calin Juravle641547a2015-04-21 22:08:51 +01003329 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003330 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003331 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003332 }
3333
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003334 uint32_t GetImtIndex() const { return imt_index_; }
3335 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3336
3337 DECLARE_INSTRUCTION(InvokeInterface);
3338
3339 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003340 const uint32_t imt_index_;
3341
3342 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3343};
3344
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003345class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003346 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003347 HNewInstance(HCurrentMethod* current_method,
3348 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003349 uint16_t type_index,
3350 const DexFile& dex_file,
3351 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003352 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003353 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003354 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003355 entrypoint_(entrypoint) {
3356 SetRawInputAt(0, current_method);
3357 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003358
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003359 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003360 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003361
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003362 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003363 bool NeedsEnvironment() const OVERRIDE { return true; }
3364 // It may throw when called on:
3365 // - interfaces
3366 // - abstract/innaccessible/unknown classes
3367 // TODO: optimize when possible.
3368 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003369
Calin Juravle10e244f2015-01-26 18:54:32 +00003370 bool CanBeNull() const OVERRIDE { return false; }
3371
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003372 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003374 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003375
3376 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003377 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003378 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003379 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003380
3381 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3382};
3383
Roland Levillain88cb1752014-10-20 16:36:47 +01003384class HNeg : public HUnaryOperation {
3385 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003386 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3387 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003388
Roland Levillain9867bc72015-08-05 10:21:34 +01003389 template <typename T> T Compute(T x) const { return -x; }
3390
3391 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003392 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003393 }
3394 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003395 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003396 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003397
Roland Levillain88cb1752014-10-20 16:36:47 +01003398 DECLARE_INSTRUCTION(Neg);
3399
3400 private:
3401 DISALLOW_COPY_AND_ASSIGN(HNeg);
3402};
3403
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003404class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003405 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003406 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003407 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003408 uint32_t dex_pc,
3409 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003410 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003411 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003412 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003413 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003414 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003415 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003416 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003417 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003418 }
3419
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003420 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003421 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003422
3423 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003424 bool NeedsEnvironment() const OVERRIDE { return true; }
3425
Mingyao Yang0c365e62015-03-31 15:09:29 -07003426 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3427 bool CanThrow() const OVERRIDE { return true; }
3428
Calin Juravle10e244f2015-01-26 18:54:32 +00003429 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003430
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003431 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3432
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003433 DECLARE_INSTRUCTION(NewArray);
3434
3435 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003436 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003437 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003438 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003439
3440 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3441};
3442
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003443class HAdd : public HBinaryOperation {
3444 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003445 HAdd(Primitive::Type result_type,
3446 HInstruction* left,
3447 HInstruction* right,
3448 uint32_t dex_pc = kNoDexPc)
3449 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003450
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003451 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003452
Roland Levillain9867bc72015-08-05 10:21:34 +01003453 template <typename T> T Compute(T x, T y) const { return x + y; }
3454
3455 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003456 return GetBlock()->GetGraph()->GetIntConstant(
3457 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003458 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003459 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003460 return GetBlock()->GetGraph()->GetLongConstant(
3461 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003462 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003463
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003464 DECLARE_INSTRUCTION(Add);
3465
3466 private:
3467 DISALLOW_COPY_AND_ASSIGN(HAdd);
3468};
3469
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003470class HSub : public HBinaryOperation {
3471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003472 HSub(Primitive::Type result_type,
3473 HInstruction* left,
3474 HInstruction* right,
3475 uint32_t dex_pc = kNoDexPc)
3476 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003477
Roland Levillain9867bc72015-08-05 10:21:34 +01003478 template <typename T> T Compute(T x, T y) const { return x - y; }
3479
3480 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003481 return GetBlock()->GetGraph()->GetIntConstant(
3482 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003483 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003484 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003485 return GetBlock()->GetGraph()->GetLongConstant(
3486 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003487 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003489 DECLARE_INSTRUCTION(Sub);
3490
3491 private:
3492 DISALLOW_COPY_AND_ASSIGN(HSub);
3493};
3494
Calin Juravle34bacdf2014-10-07 20:23:36 +01003495class HMul : public HBinaryOperation {
3496 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003497 HMul(Primitive::Type result_type,
3498 HInstruction* left,
3499 HInstruction* right,
3500 uint32_t dex_pc = kNoDexPc)
3501 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003502
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003503 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003504
Roland Levillain9867bc72015-08-05 10:21:34 +01003505 template <typename T> T Compute(T x, T y) const { return x * y; }
3506
3507 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003508 return GetBlock()->GetGraph()->GetIntConstant(
3509 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003510 }
3511 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003512 return GetBlock()->GetGraph()->GetLongConstant(
3513 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003514 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003515
3516 DECLARE_INSTRUCTION(Mul);
3517
3518 private:
3519 DISALLOW_COPY_AND_ASSIGN(HMul);
3520};
3521
Calin Juravle7c4954d2014-10-28 16:57:40 +00003522class HDiv : public HBinaryOperation {
3523 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003524 HDiv(Primitive::Type result_type,
3525 HInstruction* left,
3526 HInstruction* right,
3527 uint32_t dex_pc)
3528 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003529
Roland Levillain9867bc72015-08-05 10:21:34 +01003530 template <typename T>
3531 T Compute(T x, T y) const {
3532 // Our graph structure ensures we never have 0 for `y` during
3533 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003534 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003535 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003536 return (y == -1) ? -x : x / y;
3537 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003538
Roland Levillain9867bc72015-08-05 10:21:34 +01003539 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003540 return GetBlock()->GetGraph()->GetIntConstant(
3541 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003542 }
3543 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003544 return GetBlock()->GetGraph()->GetLongConstant(
3545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003546 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003547
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003548 static SideEffects SideEffectsForArchRuntimeCalls() {
3549 // The generated code can use a runtime call.
3550 return SideEffects::CanTriggerGC();
3551 }
3552
Calin Juravle7c4954d2014-10-28 16:57:40 +00003553 DECLARE_INSTRUCTION(Div);
3554
3555 private:
3556 DISALLOW_COPY_AND_ASSIGN(HDiv);
3557};
3558
Calin Juravlebacfec32014-11-14 15:54:36 +00003559class HRem : public HBinaryOperation {
3560 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003561 HRem(Primitive::Type result_type,
3562 HInstruction* left,
3563 HInstruction* right,
3564 uint32_t dex_pc)
3565 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003566
Roland Levillain9867bc72015-08-05 10:21:34 +01003567 template <typename T>
3568 T Compute(T x, T y) const {
3569 // Our graph structure ensures we never have 0 for `y` during
3570 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003571 DCHECK_NE(y, 0);
3572 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3573 return (y == -1) ? 0 : x % y;
3574 }
3575
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003577 return GetBlock()->GetGraph()->GetIntConstant(
3578 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003579 }
3580 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003581 return GetBlock()->GetGraph()->GetLongConstant(
3582 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003583 }
3584
Calin Juravlebacfec32014-11-14 15:54:36 +00003585
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003586 static SideEffects SideEffectsForArchRuntimeCalls() {
3587 return SideEffects::CanTriggerGC();
3588 }
3589
Calin Juravlebacfec32014-11-14 15:54:36 +00003590 DECLARE_INSTRUCTION(Rem);
3591
3592 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 DISALLOW_COPY_AND_ASSIGN(HRem);
3594};
3595
Calin Juravled0d48522014-11-04 16:40:20 +00003596class HDivZeroCheck : public HExpression<1> {
3597 public:
3598 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003599 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003600 SetRawInputAt(0, value);
3601 }
3602
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003603 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3604
Calin Juravled0d48522014-11-04 16:40:20 +00003605 bool CanBeMoved() const OVERRIDE { return true; }
3606
3607 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3608 UNUSED(other);
3609 return true;
3610 }
3611
3612 bool NeedsEnvironment() const OVERRIDE { return true; }
3613 bool CanThrow() const OVERRIDE { return true; }
3614
Calin Juravled0d48522014-11-04 16:40:20 +00003615 DECLARE_INSTRUCTION(DivZeroCheck);
3616
3617 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003618 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3619};
3620
Calin Juravle9aec02f2014-11-18 23:06:35 +00003621class HShl : public HBinaryOperation {
3622 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003623 HShl(Primitive::Type result_type,
3624 HInstruction* left,
3625 HInstruction* right,
3626 uint32_t dex_pc = kNoDexPc)
3627 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003628
Roland Levillain9867bc72015-08-05 10:21:34 +01003629 template <typename T, typename U, typename V>
3630 T Compute(T x, U y, V max_shift_value) const {
3631 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3632 "V is not the unsigned integer type corresponding to T");
3633 return x << (y & max_shift_value);
3634 }
3635
3636 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3637 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003638 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003639 }
3640 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3641 // case is handled as `x << static_cast<int>(y)`.
3642 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3643 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003644 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003645 }
3646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3647 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003648 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003649 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003650
3651 DECLARE_INSTRUCTION(Shl);
3652
3653 private:
3654 DISALLOW_COPY_AND_ASSIGN(HShl);
3655};
3656
3657class HShr : public HBinaryOperation {
3658 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003659 HShr(Primitive::Type result_type,
3660 HInstruction* left,
3661 HInstruction* right,
3662 uint32_t dex_pc = kNoDexPc)
3663 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003664
Roland Levillain9867bc72015-08-05 10:21:34 +01003665 template <typename T, typename U, typename V>
3666 T Compute(T x, U y, V max_shift_value) const {
3667 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3668 "V is not the unsigned integer type corresponding to T");
3669 return x >> (y & max_shift_value);
3670 }
3671
3672 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3673 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003674 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003675 }
3676 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3677 // case is handled as `x >> static_cast<int>(y)`.
3678 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3679 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003680 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003681 }
3682 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3683 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003684 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003685 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686
3687 DECLARE_INSTRUCTION(Shr);
3688
3689 private:
3690 DISALLOW_COPY_AND_ASSIGN(HShr);
3691};
3692
3693class HUShr : public HBinaryOperation {
3694 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003695 HUShr(Primitive::Type result_type,
3696 HInstruction* left,
3697 HInstruction* right,
3698 uint32_t dex_pc = kNoDexPc)
3699 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003700
Roland Levillain9867bc72015-08-05 10:21:34 +01003701 template <typename T, typename U, typename V>
3702 T Compute(T x, U y, V max_shift_value) const {
3703 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3704 "V is not the unsigned integer type corresponding to T");
3705 V ux = static_cast<V>(x);
3706 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003707 }
3708
Roland Levillain9867bc72015-08-05 10:21:34 +01003709 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3710 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003711 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003712 }
3713 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3714 // case is handled as `x >>> static_cast<int>(y)`.
3715 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3716 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003717 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003718 }
3719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3720 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003721 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003722 }
3723
3724 DECLARE_INSTRUCTION(UShr);
3725
3726 private:
3727 DISALLOW_COPY_AND_ASSIGN(HUShr);
3728};
3729
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003730class HAnd : public HBinaryOperation {
3731 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 HAnd(Primitive::Type result_type,
3733 HInstruction* left,
3734 HInstruction* right,
3735 uint32_t dex_pc = kNoDexPc)
3736 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003737
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003738 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003739
Roland Levillain9867bc72015-08-05 10:21:34 +01003740 template <typename T, typename U>
3741 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3742
3743 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 return GetBlock()->GetGraph()->GetIntConstant(
3745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003746 }
3747 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003748 return GetBlock()->GetGraph()->GetLongConstant(
3749 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003750 }
3751 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003752 return GetBlock()->GetGraph()->GetLongConstant(
3753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003754 }
3755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003756 return GetBlock()->GetGraph()->GetLongConstant(
3757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003758 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003759
3760 DECLARE_INSTRUCTION(And);
3761
3762 private:
3763 DISALLOW_COPY_AND_ASSIGN(HAnd);
3764};
3765
3766class HOr : public HBinaryOperation {
3767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 HOr(Primitive::Type result_type,
3769 HInstruction* left,
3770 HInstruction* right,
3771 uint32_t dex_pc = kNoDexPc)
3772 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003773
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003774 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003775
Roland Levillain9867bc72015-08-05 10:21:34 +01003776 template <typename T, typename U>
3777 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3778
3779 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003780 return GetBlock()->GetGraph()->GetIntConstant(
3781 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003782 }
3783 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003784 return GetBlock()->GetGraph()->GetLongConstant(
3785 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003786 }
3787 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003788 return GetBlock()->GetGraph()->GetLongConstant(
3789 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003790 }
3791 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003792 return GetBlock()->GetGraph()->GetLongConstant(
3793 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003794 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003795
3796 DECLARE_INSTRUCTION(Or);
3797
3798 private:
3799 DISALLOW_COPY_AND_ASSIGN(HOr);
3800};
3801
3802class HXor : public HBinaryOperation {
3803 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 HXor(Primitive::Type result_type,
3805 HInstruction* left,
3806 HInstruction* right,
3807 uint32_t dex_pc = kNoDexPc)
3808 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003809
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003810 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003811
Roland Levillain9867bc72015-08-05 10:21:34 +01003812 template <typename T, typename U>
3813 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3814
3815 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 return GetBlock()->GetGraph()->GetIntConstant(
3817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 }
3819 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 return GetBlock()->GetGraph()->GetLongConstant(
3821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003822 }
3823 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003824 return GetBlock()->GetGraph()->GetLongConstant(
3825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003826 }
3827 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003828 return GetBlock()->GetGraph()->GetLongConstant(
3829 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003830 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003831
3832 DECLARE_INSTRUCTION(Xor);
3833
3834 private:
3835 DISALLOW_COPY_AND_ASSIGN(HXor);
3836};
3837
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003838// The value of a parameter in this method. Its location depends on
3839// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003840class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003841 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003842 HParameterValue(uint8_t index,
3843 Primitive::Type parameter_type,
3844 bool is_this = false)
3845 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003846 index_(index),
3847 is_this_(is_this),
3848 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003849
3850 uint8_t GetIndex() const { return index_; }
3851
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003852 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3853 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003854
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003855 bool IsThis() const { return is_this_; }
3856
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003857 DECLARE_INSTRUCTION(ParameterValue);
3858
3859 private:
3860 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003861 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003862 const uint8_t index_;
3863
Calin Juravle10e244f2015-01-26 18:54:32 +00003864 // Whether or not the parameter value corresponds to 'this' argument.
3865 const bool is_this_;
3866
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003867 bool can_be_null_;
3868
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003869 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3870};
3871
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003872class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003873 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003874 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3875 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003877 bool CanBeMoved() const OVERRIDE { return true; }
3878 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003879 UNUSED(other);
3880 return true;
3881 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003882
Roland Levillain9867bc72015-08-05 10:21:34 +01003883 template <typename T> T Compute(T x) const { return ~x; }
3884
3885 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003887 }
3888 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003889 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003890 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003891
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003892 DECLARE_INSTRUCTION(Not);
3893
3894 private:
3895 DISALLOW_COPY_AND_ASSIGN(HNot);
3896};
3897
David Brazdil66d126e2015-04-03 16:02:44 +01003898class HBooleanNot : public HUnaryOperation {
3899 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003900 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3901 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003902
3903 bool CanBeMoved() const OVERRIDE { return true; }
3904 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3905 UNUSED(other);
3906 return true;
3907 }
3908
Roland Levillain9867bc72015-08-05 10:21:34 +01003909 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003910 DCHECK(IsUint<1>(x));
3911 return !x;
3912 }
3913
Roland Levillain9867bc72015-08-05 10:21:34 +01003914 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003915 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003916 }
3917 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3918 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003919 UNREACHABLE();
3920 }
3921
3922 DECLARE_INSTRUCTION(BooleanNot);
3923
3924 private:
3925 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3926};
3927
Roland Levillaindff1f282014-11-05 14:15:05 +00003928class HTypeConversion : public HExpression<1> {
3929 public:
3930 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003931 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003932 : HExpression(result_type,
3933 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3934 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003935 SetRawInputAt(0, input);
3936 DCHECK_NE(input->GetType(), result_type);
3937 }
3938
3939 HInstruction* GetInput() const { return InputAt(0); }
3940 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3941 Primitive::Type GetResultType() const { return GetType(); }
3942
Roland Levillain624279f2014-12-04 11:54:28 +00003943 // Required by the x86 and ARM code generators when producing calls
3944 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003945
Roland Levillaindff1f282014-11-05 14:15:05 +00003946 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003947 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003948
Mark Mendelle82549b2015-05-06 10:55:34 -04003949 // Try to statically evaluate the conversion and return a HConstant
3950 // containing the result. If the input cannot be converted, return nullptr.
3951 HConstant* TryStaticEvaluation() const;
3952
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003953 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3954 Primitive::Type result_type) {
3955 // Some architectures may not require the 'GC' side effects, but at this point
3956 // in the compilation process we do not know what architecture we will
3957 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003958 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3959 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003960 return SideEffects::CanTriggerGC();
3961 }
3962 return SideEffects::None();
3963 }
3964
Roland Levillaindff1f282014-11-05 14:15:05 +00003965 DECLARE_INSTRUCTION(TypeConversion);
3966
3967 private:
3968 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3969};
3970
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003971static constexpr uint32_t kNoRegNumber = -1;
3972
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003973class HPhi : public HInstruction {
3974 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 HPhi(ArenaAllocator* arena,
3976 uint32_t reg_number,
3977 size_t number_of_inputs,
3978 Primitive::Type type,
3979 uint32_t dex_pc = kNoDexPc)
3980 : HInstruction(SideEffects::None(), dex_pc),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003981 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003982 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003983 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003984 is_live_(false),
3985 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003986 inputs_.SetSize(number_of_inputs);
3987 }
3988
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003989 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3990 static Primitive::Type ToPhiType(Primitive::Type type) {
3991 switch (type) {
3992 case Primitive::kPrimBoolean:
3993 case Primitive::kPrimByte:
3994 case Primitive::kPrimShort:
3995 case Primitive::kPrimChar:
3996 return Primitive::kPrimInt;
3997 default:
3998 return type;
3999 }
4000 }
4001
David Brazdilffee3d32015-07-06 11:48:53 +01004002 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4003
Calin Juravle10e244f2015-01-26 18:54:32 +00004004 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004005
4006 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004007 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004008
Calin Juravle10e244f2015-01-26 18:54:32 +00004009 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004010 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004011
Calin Juravle10e244f2015-01-26 18:54:32 +00004012 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4013 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4014
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004015 uint32_t GetRegNumber() const { return reg_number_; }
4016
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004017 void SetDead() { is_live_ = false; }
4018 void SetLive() { is_live_ = true; }
4019 bool IsDead() const { return !is_live_; }
4020 bool IsLive() const { return is_live_; }
4021
David Brazdil77a48ae2015-09-15 12:34:04 +00004022 bool IsVRegEquivalentOf(HInstruction* other) const {
4023 return other != nullptr
4024 && other->IsPhi()
4025 && other->AsPhi()->GetBlock() == GetBlock()
4026 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4027 }
4028
Calin Juravlea4f88312015-04-16 12:57:19 +01004029 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4030 // An equivalent phi is a phi having the same dex register and type.
4031 // It assumes that phis with the same dex register are adjacent.
4032 HPhi* GetNextEquivalentPhiWithSameType() {
4033 HInstruction* next = GetNext();
4034 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4035 if (next->GetType() == GetType()) {
4036 return next->AsPhi();
4037 }
4038 next = next->GetNext();
4039 }
4040 return nullptr;
4041 }
4042
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004043 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004044
David Brazdil1abb4192015-02-17 18:33:36 +00004045 protected:
4046 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
4047
4048 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
4049 inputs_.Put(index, input);
4050 }
4051
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004052 private:
David Brazdil1abb4192015-02-17 18:33:36 +00004053 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004054 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004055 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004056 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004057 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004058
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004059 DISALLOW_COPY_AND_ASSIGN(HPhi);
4060};
4061
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004062class HNullCheck : public HExpression<1> {
4063 public:
4064 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004066 SetRawInputAt(0, value);
4067 }
4068
Calin Juravle10e244f2015-01-26 18:54:32 +00004069 bool CanBeMoved() const OVERRIDE { return true; }
4070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004071 UNUSED(other);
4072 return true;
4073 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004074
Calin Juravle10e244f2015-01-26 18:54:32 +00004075 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004076
Calin Juravle10e244f2015-01-26 18:54:32 +00004077 bool CanThrow() const OVERRIDE { return true; }
4078
4079 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004080
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004081
4082 DECLARE_INSTRUCTION(NullCheck);
4083
4084 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004085 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4086};
4087
4088class FieldInfo : public ValueObject {
4089 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004090 FieldInfo(MemberOffset field_offset,
4091 Primitive::Type field_type,
4092 bool is_volatile,
4093 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004094 const DexFile& dex_file,
4095 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004096 : field_offset_(field_offset),
4097 field_type_(field_type),
4098 is_volatile_(is_volatile),
4099 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004100 dex_file_(dex_file),
4101 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102
4103 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004104 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004105 uint32_t GetFieldIndex() const { return index_; }
4106 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004107 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004108 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004109
4110 private:
4111 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004112 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004113 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004114 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004115 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004116 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004117};
4118
4119class HInstanceFieldGet : public HExpression<1> {
4120 public:
4121 HInstanceFieldGet(HInstruction* value,
4122 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004123 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004124 bool is_volatile,
4125 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004126 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 Handle<mirror::DexCache> dex_cache,
4128 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004129 : HExpression(
4130 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004132 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004133 SetRawInputAt(0, value);
4134 }
4135
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004136 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004137
4138 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4139 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4140 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004141 }
4142
Calin Juravle641547a2015-04-21 22:08:51 +01004143 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4144 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004145 }
4146
4147 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004148 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4149 }
4150
Calin Juravle52c48962014-12-16 17:02:57 +00004151 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004152 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004153 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004154 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004155
4156 DECLARE_INSTRUCTION(InstanceFieldGet);
4157
4158 private:
4159 const FieldInfo field_info_;
4160
4161 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4162};
4163
4164class HInstanceFieldSet : public HTemplateInstruction<2> {
4165 public:
4166 HInstanceFieldSet(HInstruction* object,
4167 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004168 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004169 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004170 bool is_volatile,
4171 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004172 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004173 Handle<mirror::DexCache> dex_cache,
4174 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004175 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004176 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004177 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004178 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004179 SetRawInputAt(0, object);
4180 SetRawInputAt(1, value);
4181 }
4182
Calin Juravle641547a2015-04-21 22:08:51 +01004183 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4184 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004185 }
4186
Calin Juravle52c48962014-12-16 17:02:57 +00004187 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004188 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004189 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004190 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004191 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004192 bool GetValueCanBeNull() const { return value_can_be_null_; }
4193 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004194
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004195 DECLARE_INSTRUCTION(InstanceFieldSet);
4196
4197 private:
4198 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004199 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004200
4201 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4202};
4203
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004204class HArrayGet : public HExpression<2> {
4205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 HArrayGet(HInstruction* array,
4207 HInstruction* index,
4208 Primitive::Type type,
4209 uint32_t dex_pc = kNoDexPc)
4210 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004211 SetRawInputAt(0, array);
4212 SetRawInputAt(1, index);
4213 }
4214
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004215 bool CanBeMoved() const OVERRIDE { return true; }
4216 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004217 UNUSED(other);
4218 return true;
4219 }
Calin Juravle641547a2015-04-21 22:08:51 +01004220 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4221 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004222 // TODO: We can be smarter here.
4223 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4224 // which generates the implicit null check. There are cases when these can be removed
4225 // to produce better code. If we ever add optimizations to do so we should allow an
4226 // implicit check here (as long as the address falls in the first page).
4227 return false;
4228 }
4229
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004230 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004231
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004232 HInstruction* GetArray() const { return InputAt(0); }
4233 HInstruction* GetIndex() const { return InputAt(1); }
4234
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004235 DECLARE_INSTRUCTION(ArrayGet);
4236
4237 private:
4238 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4239};
4240
4241class HArraySet : public HTemplateInstruction<3> {
4242 public:
4243 HArraySet(HInstruction* array,
4244 HInstruction* index,
4245 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004246 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004247 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004248 : HTemplateInstruction(
4249 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004250 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004251 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004252 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4253 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004254 SetRawInputAt(0, array);
4255 SetRawInputAt(1, index);
4256 SetRawInputAt(2, value);
4257 }
4258
Calin Juravle77520bc2015-01-12 18:45:46 +00004259 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004260 // We currently always call a runtime method to catch array store
4261 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004262 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263 }
4264
Mingyao Yang81014cb2015-06-02 03:16:27 -07004265 // Can throw ArrayStoreException.
4266 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4267
Calin Juravle641547a2015-04-21 22:08:51 +01004268 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4269 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004270 // TODO: Same as for ArrayGet.
4271 return false;
4272 }
4273
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004274 void ClearNeedsTypeCheck() {
4275 needs_type_check_ = false;
4276 }
4277
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004278 void ClearValueCanBeNull() {
4279 value_can_be_null_ = false;
4280 }
4281
4282 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004283 bool NeedsTypeCheck() const { return needs_type_check_; }
4284
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004285 HInstruction* GetArray() const { return InputAt(0); }
4286 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004287 HInstruction* GetValue() const { return InputAt(2); }
4288
4289 Primitive::Type GetComponentType() const {
4290 // The Dex format does not type floating point index operations. Since the
4291 // `expected_component_type_` is set during building and can therefore not
4292 // be correct, we also check what is the value type. If it is a floating
4293 // point type, we must use that type.
4294 Primitive::Type value_type = GetValue()->GetType();
4295 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4296 ? value_type
4297 : expected_component_type_;
4298 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004299
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004300 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4301 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4302 }
4303
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004304 DECLARE_INSTRUCTION(ArraySet);
4305
4306 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004307 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004308 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004309 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004310
4311 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4312};
4313
4314class HArrayLength : public HExpression<1> {
4315 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4317 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004318 // Note that arrays do not change length, so the instruction does not
4319 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004320 SetRawInputAt(0, array);
4321 }
4322
Calin Juravle77520bc2015-01-12 18:45:46 +00004323 bool CanBeMoved() const OVERRIDE { return true; }
4324 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004325 UNUSED(other);
4326 return true;
4327 }
Calin Juravle641547a2015-04-21 22:08:51 +01004328 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4329 return obj == InputAt(0);
4330 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004331
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004332 DECLARE_INSTRUCTION(ArrayLength);
4333
4334 private:
4335 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4336};
4337
4338class HBoundsCheck : public HExpression<2> {
4339 public:
4340 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004342 DCHECK(index->GetType() == Primitive::kPrimInt);
4343 SetRawInputAt(0, index);
4344 SetRawInputAt(1, length);
4345 }
4346
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004347 bool CanBeMoved() const OVERRIDE { return true; }
4348 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004349 UNUSED(other);
4350 return true;
4351 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004353 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004354
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004355 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004356
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004357
4358 DECLARE_INSTRUCTION(BoundsCheck);
4359
4360 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4362};
4363
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364/**
4365 * Some DEX instructions are folded into multiple HInstructions that need
4366 * to stay live until the last HInstruction. This class
4367 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004368 * HInstruction stays live. `index` represents the stack location index of the
4369 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004370 */
4371class HTemporary : public HTemplateInstruction<0> {
4372 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4374 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375
4376 size_t GetIndex() const { return index_; }
4377
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004378 Primitive::Type GetType() const OVERRIDE {
4379 // The previous instruction is the one that will be stored in the temporary location.
4380 DCHECK(GetPrevious() != nullptr);
4381 return GetPrevious()->GetType();
4382 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004383
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384 DECLARE_INSTRUCTION(Temporary);
4385
4386 private:
4387 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004388 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4389};
4390
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004391class HSuspendCheck : public HTemplateInstruction<0> {
4392 public:
4393 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004394 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004395
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004396 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004397 return true;
4398 }
4399
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004400 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4401 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004402
4403 DECLARE_INSTRUCTION(SuspendCheck);
4404
4405 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004406 // Only used for code generation, in order to share the same slow path between back edges
4407 // of a same loop.
4408 SlowPathCode* slow_path_;
4409
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004410 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4411};
4412
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004413/**
4414 * Instruction to load a Class object.
4415 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004416class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004417 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004418 HLoadClass(HCurrentMethod* current_method,
4419 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004420 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004421 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004422 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004423 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004424 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004425 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004426 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004427 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004428 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004429 SetRawInputAt(0, current_method);
4430 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004431
4432 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004433
4434 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4435 return other->AsLoadClass()->type_index_ == type_index_;
4436 }
4437
4438 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4439
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004440 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004441 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004442 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004443
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004444 bool NeedsEnvironment() const OVERRIDE {
4445 // Will call runtime and load the class if the class is not loaded yet.
4446 // TODO: finer grain decision.
4447 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004448 }
4449
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004450 bool MustGenerateClinitCheck() const {
4451 return generate_clinit_check_;
4452 }
4453
Calin Juravle0ba218d2015-05-19 18:46:01 +01004454 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4455 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004456 }
4457
4458 bool CanCallRuntime() const {
4459 return MustGenerateClinitCheck() || !is_referrers_class_;
4460 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004461
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004462 bool CanThrow() const OVERRIDE {
4463 // May call runtime and and therefore can throw.
4464 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004465 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004466 }
4467
Calin Juravleacf735c2015-02-12 15:25:22 +00004468 ReferenceTypeInfo GetLoadedClassRTI() {
4469 return loaded_class_rti_;
4470 }
4471
4472 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4473 // Make sure we only set exact types (the loaded class should never be merged).
4474 DCHECK(rti.IsExact());
4475 loaded_class_rti_ = rti;
4476 }
4477
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004478 const DexFile& GetDexFile() { return dex_file_; }
4479
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004480 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4481
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004482 static SideEffects SideEffectsForArchRuntimeCalls() {
4483 return SideEffects::CanTriggerGC();
4484 }
4485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004486 DECLARE_INSTRUCTION(LoadClass);
4487
4488 private:
4489 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004490 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004491 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004492 // Whether this instruction must generate the initialization check.
4493 // Used for code generation.
4494 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004495
Calin Juravleacf735c2015-02-12 15:25:22 +00004496 ReferenceTypeInfo loaded_class_rti_;
4497
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004498 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4499};
4500
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004501class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004502 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004503 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004504 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4505 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004506 SetRawInputAt(0, current_method);
4507 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004508
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004509 bool CanBeMoved() const OVERRIDE { return true; }
4510
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004511 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4512 return other->AsLoadString()->string_index_ == string_index_;
4513 }
4514
4515 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4516
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004517 uint32_t GetStringIndex() const { return string_index_; }
4518
4519 // TODO: Can we deopt or debug when we resolve a string?
4520 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004521 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004522 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004523
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004524 static SideEffects SideEffectsForArchRuntimeCalls() {
4525 return SideEffects::CanTriggerGC();
4526 }
4527
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004528 DECLARE_INSTRUCTION(LoadString);
4529
4530 private:
4531 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004532
4533 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4534};
4535
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004536/**
4537 * Performs an initialization check on its Class object input.
4538 */
4539class HClinitCheck : public HExpression<1> {
4540 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004541 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004542 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004543 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4545 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004546 SetRawInputAt(0, constant);
4547 }
4548
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004549 bool CanBeMoved() const OVERRIDE { return true; }
4550 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4551 UNUSED(other);
4552 return true;
4553 }
4554
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004555 bool NeedsEnvironment() const OVERRIDE {
4556 // May call runtime to initialize the class.
4557 return true;
4558 }
4559
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004560
4561 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4562
4563 DECLARE_INSTRUCTION(ClinitCheck);
4564
4565 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004566 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4567};
4568
4569class HStaticFieldGet : public HExpression<1> {
4570 public:
4571 HStaticFieldGet(HInstruction* cls,
4572 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004573 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004574 bool is_volatile,
4575 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004576 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004577 Handle<mirror::DexCache> dex_cache,
4578 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004579 : HExpression(
4580 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004581 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004582 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004583 SetRawInputAt(0, cls);
4584 }
4585
Calin Juravle52c48962014-12-16 17:02:57 +00004586
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004587 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004588
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004589 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004590 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4591 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004592 }
4593
4594 size_t ComputeHashCode() const OVERRIDE {
4595 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4596 }
4597
Calin Juravle52c48962014-12-16 17:02:57 +00004598 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004599 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4600 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004601 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004602
4603 DECLARE_INSTRUCTION(StaticFieldGet);
4604
4605 private:
4606 const FieldInfo field_info_;
4607
4608 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4609};
4610
4611class HStaticFieldSet : public HTemplateInstruction<2> {
4612 public:
4613 HStaticFieldSet(HInstruction* cls,
4614 HInstruction* value,
4615 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004616 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004617 bool is_volatile,
4618 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004619 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004620 Handle<mirror::DexCache> dex_cache,
4621 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004622 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004623 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004624 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004625 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004626 SetRawInputAt(0, cls);
4627 SetRawInputAt(1, value);
4628 }
4629
Calin Juravle52c48962014-12-16 17:02:57 +00004630 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004631 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4632 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004633 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004634
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004635 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004636 bool GetValueCanBeNull() const { return value_can_be_null_; }
4637 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004638
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004639 DECLARE_INSTRUCTION(StaticFieldSet);
4640
4641 private:
4642 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004643 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004644
4645 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4646};
4647
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004648// Implement the move-exception DEX instruction.
4649class HLoadException : public HExpression<0> {
4650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004651 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4652 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004653
David Brazdilbbd733e2015-08-18 17:48:17 +01004654 bool CanBeNull() const OVERRIDE { return false; }
4655
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004656 DECLARE_INSTRUCTION(LoadException);
4657
4658 private:
4659 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4660};
4661
David Brazdilcb1c0552015-08-04 16:22:25 +01004662// Implicit part of move-exception which clears thread-local exception storage.
4663// Must not be removed because the runtime expects the TLS to get cleared.
4664class HClearException : public HTemplateInstruction<0> {
4665 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004666 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4667 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004668
4669 DECLARE_INSTRUCTION(ClearException);
4670
4671 private:
4672 DISALLOW_COPY_AND_ASSIGN(HClearException);
4673};
4674
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004675class HThrow : public HTemplateInstruction<1> {
4676 public:
4677 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004679 SetRawInputAt(0, exception);
4680 }
4681
4682 bool IsControlFlow() const OVERRIDE { return true; }
4683
4684 bool NeedsEnvironment() const OVERRIDE { return true; }
4685
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004686 bool CanThrow() const OVERRIDE { return true; }
4687
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004688
4689 DECLARE_INSTRUCTION(Throw);
4690
4691 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004692 DISALLOW_COPY_AND_ASSIGN(HThrow);
4693};
4694
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004695class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004696 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004697 HInstanceOf(HInstruction* object,
4698 HLoadClass* constant,
4699 bool class_is_final,
4700 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 : HExpression(Primitive::kPrimBoolean,
4702 SideEffectsForArchRuntimeCalls(class_is_final),
4703 dex_pc),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004704 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004706 SetRawInputAt(0, object);
4707 SetRawInputAt(1, constant);
4708 }
4709
4710 bool CanBeMoved() const OVERRIDE { return true; }
4711
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004712 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004713 return true;
4714 }
4715
4716 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004717 return false;
4718 }
4719
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004720 bool IsClassFinal() const { return class_is_final_; }
4721
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004722 // Used only in code generation.
4723 bool MustDoNullCheck() const { return must_do_null_check_; }
4724 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4725
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004726 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4727 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4728 }
4729
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004730 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004731
4732 private:
4733 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004734 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004735
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004736 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4737};
4738
Calin Juravleb1498f62015-02-16 13:13:29 +00004739class HBoundType : public HExpression<1> {
4740 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004741 // Constructs an HBoundType with the given upper_bound.
4742 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004743 HBoundType(HInstruction* input,
4744 ReferenceTypeInfo upper_bound,
4745 bool upper_can_be_null,
4746 uint32_t dex_pc = kNoDexPc)
4747 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004748 upper_bound_(upper_bound),
4749 upper_can_be_null_(upper_can_be_null),
4750 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004751 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004752 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004753 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004754 }
4755
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004756 // GetUpper* should only be used in reference type propagation.
4757 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4758 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004759
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004760 void SetCanBeNull(bool can_be_null) {
4761 DCHECK(upper_can_be_null_ || !can_be_null);
4762 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004763 }
4764
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004765 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4766
Calin Juravleb1498f62015-02-16 13:13:29 +00004767 DECLARE_INSTRUCTION(BoundType);
4768
4769 private:
4770 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004771 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4772 // It is used to bound the type in cases like:
4773 // if (x instanceof ClassX) {
4774 // // uper_bound_ will be ClassX
4775 // }
4776 const ReferenceTypeInfo upper_bound_;
4777 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4778 // is false then can_be_null_ cannot be true).
4779 const bool upper_can_be_null_;
4780 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004781
4782 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4783};
4784
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004785class HCheckCast : public HTemplateInstruction<2> {
4786 public:
4787 HCheckCast(HInstruction* object,
4788 HLoadClass* constant,
4789 bool class_is_final,
4790 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004791 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004792 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004793 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004794 SetRawInputAt(0, object);
4795 SetRawInputAt(1, constant);
4796 }
4797
4798 bool CanBeMoved() const OVERRIDE { return true; }
4799
4800 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4801 return true;
4802 }
4803
4804 bool NeedsEnvironment() const OVERRIDE {
4805 // Instruction may throw a CheckCastError.
4806 return true;
4807 }
4808
4809 bool CanThrow() const OVERRIDE { return true; }
4810
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004811 bool MustDoNullCheck() const { return must_do_null_check_; }
4812 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4813
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004814
4815 bool IsClassFinal() const { return class_is_final_; }
4816
4817 DECLARE_INSTRUCTION(CheckCast);
4818
4819 private:
4820 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004821 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004822
4823 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004824};
4825
Calin Juravle27df7582015-04-17 19:12:31 +01004826class HMemoryBarrier : public HTemplateInstruction<0> {
4827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004828 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004829 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004830 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004831 barrier_kind_(barrier_kind) {}
4832
4833 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4834
4835 DECLARE_INSTRUCTION(MemoryBarrier);
4836
4837 private:
4838 const MemBarrierKind barrier_kind_;
4839
4840 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4841};
4842
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004843class HMonitorOperation : public HTemplateInstruction<1> {
4844 public:
4845 enum OperationKind {
4846 kEnter,
4847 kExit,
4848 };
4849
4850 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004851 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004852 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4853 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004854 SetRawInputAt(0, object);
4855 }
4856
4857 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004858 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4859
4860 bool CanThrow() const OVERRIDE {
4861 // Verifier guarantees that monitor-exit cannot throw.
4862 // This is important because it allows the HGraphBuilder to remove
4863 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4864 return IsEnter();
4865 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004866
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004867
4868 bool IsEnter() const { return kind_ == kEnter; }
4869
4870 DECLARE_INSTRUCTION(MonitorOperation);
4871
Calin Juravle52c48962014-12-16 17:02:57 +00004872 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004873 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004874
4875 private:
4876 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4877};
4878
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004879/**
4880 * A HInstruction used as a marker for the replacement of new + <init>
4881 * of a String to a call to a StringFactory. Only baseline will see
4882 * the node at code generation, where it will be be treated as null.
4883 * When compiling non-baseline, `HFakeString` instructions are being removed
4884 * in the instruction simplifier.
4885 */
4886class HFakeString : public HTemplateInstruction<0> {
4887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4889 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004890
4891 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4892
4893 DECLARE_INSTRUCTION(FakeString);
4894
4895 private:
4896 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4897};
4898
Vladimir Markof9f64412015-09-02 14:05:49 +01004899class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004900 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004901 MoveOperands(Location source,
4902 Location destination,
4903 Primitive::Type type,
4904 HInstruction* instruction)
4905 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004906
4907 Location GetSource() const { return source_; }
4908 Location GetDestination() const { return destination_; }
4909
4910 void SetSource(Location value) { source_ = value; }
4911 void SetDestination(Location value) { destination_ = value; }
4912
4913 // The parallel move resolver marks moves as "in-progress" by clearing the
4914 // destination (but not the source).
4915 Location MarkPending() {
4916 DCHECK(!IsPending());
4917 Location dest = destination_;
4918 destination_ = Location::NoLocation();
4919 return dest;
4920 }
4921
4922 void ClearPending(Location dest) {
4923 DCHECK(IsPending());
4924 destination_ = dest;
4925 }
4926
4927 bool IsPending() const {
4928 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4929 return destination_.IsInvalid() && !source_.IsInvalid();
4930 }
4931
4932 // True if this blocks a move from the given location.
4933 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004934 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004935 }
4936
4937 // A move is redundant if it's been eliminated, if its source and
4938 // destination are the same, or if its destination is unneeded.
4939 bool IsRedundant() const {
4940 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4941 }
4942
4943 // We clear both operands to indicate move that's been eliminated.
4944 void Eliminate() {
4945 source_ = destination_ = Location::NoLocation();
4946 }
4947
4948 bool IsEliminated() const {
4949 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4950 return source_.IsInvalid();
4951 }
4952
Alexey Frunze4dda3372015-06-01 18:31:49 -07004953 Primitive::Type GetType() const { return type_; }
4954
Nicolas Geoffray90218252015-04-15 11:56:51 +01004955 bool Is64BitMove() const {
4956 return Primitive::Is64BitType(type_);
4957 }
4958
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004959 HInstruction* GetInstruction() const { return instruction_; }
4960
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004961 private:
4962 Location source_;
4963 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004964 // The type this move is for.
4965 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004966 // The instruction this move is assocatied with. Null when this move is
4967 // for moving an input in the expected locations of user (including a phi user).
4968 // This is only used in debug mode, to ensure we do not connect interval siblings
4969 // in the same parallel move.
4970 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004971};
4972
4973static constexpr size_t kDefaultNumberOfMoves = 4;
4974
4975class HParallelMove : public HTemplateInstruction<0> {
4976 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
4978 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004979
Nicolas Geoffray90218252015-04-15 11:56:51 +01004980 void AddMove(Location source,
4981 Location destination,
4982 Primitive::Type type,
4983 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004984 DCHECK(source.IsValid());
4985 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004986 if (kIsDebugBuild) {
4987 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004988 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004989 if (moves_.Get(i).GetInstruction() == instruction) {
4990 // Special case the situation where the move is for the spill slot
4991 // of the instruction.
4992 if ((GetPrevious() == instruction)
4993 || ((GetPrevious() == nullptr)
4994 && instruction->IsPhi()
4995 && instruction->GetBlock() == GetBlock())) {
4996 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4997 << "Doing parallel moves for the same instruction.";
4998 } else {
4999 DCHECK(false) << "Doing parallel moves for the same instruction.";
5000 }
5001 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005002 }
5003 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005004 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08005005 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005006 << "Overlapped destination for two moves in a parallel move: "
5007 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
5008 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005009 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005010 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01005011 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005012 }
5013
5014 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005015 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005016 }
5017
5018 size_t NumMoves() const { return moves_.Size(); }
5019
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005020 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005021
5022 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005023 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005024
5025 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5026};
5027
Mark Mendell0616ae02015-04-17 12:49:27 -04005028} // namespace art
5029
5030#ifdef ART_ENABLE_CODEGEN_x86
5031#include "nodes_x86.h"
5032#endif
5033
5034namespace art {
5035
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005036class HGraphVisitor : public ValueObject {
5037 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005038 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5039 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005040
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005041 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005042 virtual void VisitBasicBlock(HBasicBlock* block);
5043
Roland Levillain633021e2014-10-01 14:12:25 +01005044 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005045 void VisitInsertionOrder();
5046
Roland Levillain633021e2014-10-01 14:12:25 +01005047 // Visit the graph following dominator tree reverse post-order.
5048 void VisitReversePostOrder();
5049
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005050 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005051
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005052 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005053#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005054 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5055
5056 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5057
5058#undef DECLARE_VISIT_INSTRUCTION
5059
5060 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005061 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005062
5063 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5064};
5065
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005066class HGraphDelegateVisitor : public HGraphVisitor {
5067 public:
5068 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5069 virtual ~HGraphDelegateVisitor() {}
5070
5071 // Visit functions that delegate to to super class.
5072#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005073 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005074
5075 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5076
5077#undef DECLARE_VISIT_INSTRUCTION
5078
5079 private:
5080 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5081};
5082
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005083class HInsertionOrderIterator : public ValueObject {
5084 public:
5085 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5086
5087 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5088 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5089 void Advance() { ++index_; }
5090
5091 private:
5092 const HGraph& graph_;
5093 size_t index_;
5094
5095 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5096};
5097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005098class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005099 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005100 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5101 // Check that reverse post order of the graph has been built.
5102 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5103 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005104
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005105 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5106 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005107 void Advance() { ++index_; }
5108
5109 private:
5110 const HGraph& graph_;
5111 size_t index_;
5112
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005113 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005114};
5115
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005116class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005117 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005118 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005119 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5120 // Check that reverse post order of the graph has been built.
5121 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5122 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005123
5124 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005125 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005126 void Advance() { --index_; }
5127
5128 private:
5129 const HGraph& graph_;
5130 size_t index_;
5131
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005132 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005133};
5134
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005135class HLinearPostOrderIterator : public ValueObject {
5136 public:
5137 explicit HLinearPostOrderIterator(const HGraph& graph)
5138 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5139
5140 bool Done() const { return index_ == 0; }
5141
5142 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5143
5144 void Advance() {
5145 --index_;
5146 DCHECK_GE(index_, 0U);
5147 }
5148
5149 private:
5150 const GrowableArray<HBasicBlock*>& order_;
5151 size_t index_;
5152
5153 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5154};
5155
5156class HLinearOrderIterator : public ValueObject {
5157 public:
5158 explicit HLinearOrderIterator(const HGraph& graph)
5159 : order_(graph.GetLinearOrder()), index_(0) {}
5160
5161 bool Done() const { return index_ == order_.Size(); }
5162 HBasicBlock* Current() const { return order_.Get(index_); }
5163 void Advance() { ++index_; }
5164
5165 private:
5166 const GrowableArray<HBasicBlock*>& order_;
5167 size_t index_;
5168
5169 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5170};
5171
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005172// Iterator over the blocks that art part of the loop. Includes blocks part
5173// of an inner loop. The order in which the blocks are iterated is on their
5174// block id.
5175class HBlocksInLoopIterator : public ValueObject {
5176 public:
5177 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5178 : blocks_in_loop_(info.GetBlocks()),
5179 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5180 index_(0) {
5181 if (!blocks_in_loop_.IsBitSet(index_)) {
5182 Advance();
5183 }
5184 }
5185
5186 bool Done() const { return index_ == blocks_.Size(); }
5187 HBasicBlock* Current() const { return blocks_.Get(index_); }
5188 void Advance() {
5189 ++index_;
5190 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5191 if (blocks_in_loop_.IsBitSet(index_)) {
5192 break;
5193 }
5194 }
5195 }
5196
5197 private:
5198 const BitVector& blocks_in_loop_;
5199 const GrowableArray<HBasicBlock*>& blocks_;
5200 size_t index_;
5201
5202 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5203};
5204
Mingyao Yang3584bce2015-05-19 16:01:59 -07005205// Iterator over the blocks that art part of the loop. Includes blocks part
5206// of an inner loop. The order in which the blocks are iterated is reverse
5207// post order.
5208class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5209 public:
5210 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5211 : blocks_in_loop_(info.GetBlocks()),
5212 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5213 index_(0) {
5214 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5215 Advance();
5216 }
5217 }
5218
5219 bool Done() const { return index_ == blocks_.Size(); }
5220 HBasicBlock* Current() const { return blocks_.Get(index_); }
5221 void Advance() {
5222 ++index_;
5223 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5224 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5225 break;
5226 }
5227 }
5228 }
5229
5230 private:
5231 const BitVector& blocks_in_loop_;
5232 const GrowableArray<HBasicBlock*>& blocks_;
5233 size_t index_;
5234
5235 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5236};
5237
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005238inline int64_t Int64FromConstant(HConstant* constant) {
5239 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5240 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5241 : constant->AsLongConstant()->GetValue();
5242}
5243
Vladimir Marko58155012015-08-19 12:49:41 +00005244inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5245 // For the purposes of the compiler, the dex files must actually be the same object
5246 // if we want to safely treat them as the same. This is especially important for JIT
5247 // as custom class loaders can open the same underlying file (or memory) multiple
5248 // times and provide different class resolution but no two class loaders should ever
5249 // use the same DexFile object - doing so is an unsupported hack that can lead to
5250 // all sorts of weird failures.
5251 return &lhs == &rhs;
5252}
5253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005254} // namespace art
5255
5256#endif // ART_COMPILER_OPTIMIZING_NODES_H_