blob: 23d605b7b5ee0d13fecdaba30fa2448ba5108fec [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 Markof9f64412015-09-02 14:05:49 +010020#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010021#include <type_traits>
22
David Brazdil8d5b8b22015-03-24 10:51:52 +000023#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080024#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010025#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000026#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000027#include "handle.h"
28#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000029#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010030#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000031#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000032#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010033#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070034#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000035#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036#include "utils/growable_array.h"
37
38namespace art {
39
David Brazdil1abb4192015-02-17 18:33:36 +000040class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010042class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000043class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010044class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010045class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000047class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000050class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000051class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000053class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010055class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010056class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010057class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000058class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010059class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000061
Mathieu Chartier736b5602015-09-02 14:54:11 -070062namespace mirror {
63class DexCache;
64} // namespace mirror
65
Nicolas Geoffray818f2102014-02-18 16:43:35 +000066static const int kDefaultNumberOfBlocks = 8;
67static const int kDefaultNumberOfSuccessors = 2;
68static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010069static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010070static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000071static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072
Calin Juravle9aec02f2014-11-18 23:06:35 +000073static constexpr uint32_t kMaxIntShiftValue = 0x1f;
74static constexpr uint64_t kMaxLongShiftValue = 0x3f;
75
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010076static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
77
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010078static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
79
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060080static constexpr uint32_t kNoDexPc = -1;
81
Dave Allison20dfc792014-06-16 20:44:29 -070082enum IfCondition {
83 kCondEQ,
84 kCondNE,
85 kCondLT,
86 kCondLE,
87 kCondGT,
88 kCondGE,
89};
90
Vladimir Markof9f64412015-09-02 14:05:49 +010091class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010092 public:
93 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
94
95 void AddInstruction(HInstruction* instruction);
96 void RemoveInstruction(HInstruction* instruction);
97
David Brazdilc3d743f2015-04-22 13:40:50 +010098 // Insert `instruction` before/after an existing instruction `cursor`.
99 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
100 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
101
Roland Levillain6b469232014-09-25 10:10:38 +0100102 // Return true if this list contains `instruction`.
103 bool Contains(HInstruction* instruction) const;
104
Roland Levillainccc07a92014-09-16 14:48:16 +0100105 // Return true if `instruction1` is found before `instruction2` in
106 // this instruction list and false otherwise. Abort if none
107 // of these instructions is found.
108 bool FoundBefore(const HInstruction* instruction1,
109 const HInstruction* instruction2) const;
110
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000111 bool IsEmpty() const { return first_instruction_ == nullptr; }
112 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
113
114 // Update the block of all instructions to be `block`.
115 void SetBlockOfInstructions(HBasicBlock* block) const;
116
117 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
118 void Add(const HInstructionList& instruction_list);
119
David Brazdil2d7352b2015-04-20 14:52:42 +0100120 // Return the number of instructions in the list. This is an expensive operation.
121 size_t CountSize() const;
122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100123 private:
124 HInstruction* first_instruction_;
125 HInstruction* last_instruction_;
126
127 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000128 friend class HGraph;
129 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100130 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100131 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132
133 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
134};
135
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100139 HGraph(ArenaAllocator* arena,
140 const DexFile& dex_file,
141 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100142 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100144 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100145 bool debuggable = false,
146 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000148 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100149 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100150 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700151 entry_block_(nullptr),
152 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100153 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100154 number_of_vregs_(0),
155 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000156 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400157 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000158 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000159 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100160 dex_file_(dex_file),
161 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100162 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100163 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100164 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000166 cached_null_constant_(nullptr),
167 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000168 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
169 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100170 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
171 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000172
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000173 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100174 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100175 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176
David Brazdil69ba7b72015-06-23 18:27:30 +0100177 bool IsInSsaForm() const { return in_ssa_form_; }
178
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000179 HBasicBlock* GetEntryBlock() const { return entry_block_; }
180 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100181 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000182
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000183 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
184 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000185
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000186 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 // Try building the SSA form of this graph, with dominance computation and loop
189 // recognition. Returns whether it was successful in doing all these steps.
190 bool TryBuildingSsa() {
191 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000192 // The SSA builder requires loops to all be natural. Specifically, the dead phi
193 // elimination phase checks the consistency of the graph when doing a post-order
194 // visit for eliminating dead phis: a dead phi can only have loop header phi
195 // users remaining when being visited.
196 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100197 // Precompute per-block try membership before entering the SSA builder,
198 // which needs the information to build catch block phis from values of
199 // locals at throwing instructions inside try blocks.
200 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000201 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100202 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 }
205
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100206 void ComputeDominanceInformation();
207 void ClearDominanceInformation();
208
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000210 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100211 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100212 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000213
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000214 // Analyze all natural loops in this graph. Returns false if one
215 // loop is not natural, that is the header does not dominate the
216 // back edge.
217 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100218
David Brazdilffee3d32015-07-06 11:48:53 +0100219 // Iterate over blocks to compute try block membership. Needs reverse post
220 // order and loop information.
221 void ComputeTryBlockInformation();
222
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000224 // Returns the instruction used to replace the invoke expression or null if the
225 // invoke is for a void method.
226 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227
Mingyao Yang3584bce2015-05-19 16:01:59 -0700228 // Need to add a couple of blocks to test if the loop body is entered and
229 // put deoptimization instructions, etc.
230 void TransformLoopHeaderForBCE(HBasicBlock* header);
231
David Brazdil2d7352b2015-04-20 14:52:42 +0100232 // Removes `block` from the graph.
233 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000234
David Brazdilfc6a86a2015-06-26 10:33:45 +0000235 // Splits the edge between `block` and `successor` while preserving the
236 // indices in the predecessor/successor lists. If there are multiple edges
237 // between the blocks, the lowest indices are used.
238 // Returns the new block which is empty and has the same dex pc as `successor`.
239 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
240
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100241 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
242 void SimplifyLoop(HBasicBlock* header);
243
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000244 int32_t GetNextInstructionId() {
245 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000246 return current_instruction_id_++;
247 }
248
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000249 int32_t GetCurrentInstructionId() const {
250 return current_instruction_id_;
251 }
252
253 void SetCurrentInstructionId(int32_t id) {
254 current_instruction_id_ = id;
255 }
256
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100257 uint16_t GetMaximumNumberOfOutVRegs() const {
258 return maximum_number_of_out_vregs_;
259 }
260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000261 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
262 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100263 }
264
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100265 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
266 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
267 }
268
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000269 void UpdateTemporariesVRegSlots(size_t slots) {
270 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100271 }
272
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000273 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100274 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000275 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100276 }
277
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100278 void SetNumberOfVRegs(uint16_t number_of_vregs) {
279 number_of_vregs_ = number_of_vregs;
280 }
281
282 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100283 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100284 return number_of_vregs_;
285 }
286
287 void SetNumberOfInVRegs(uint16_t value) {
288 number_of_in_vregs_ = value;
289 }
290
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100291 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100292 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100293 return number_of_vregs_ - number_of_in_vregs_;
294 }
295
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100296 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
297 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100298 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100299
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100300 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
301 return linear_order_;
302 }
303
Mark Mendell1152c922015-04-24 17:06:35 -0400304 bool HasBoundsChecks() const {
305 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800306 }
307
Mark Mendell1152c922015-04-24 17:06:35 -0400308 void SetHasBoundsChecks(bool value) {
309 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800310 }
311
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100312 bool ShouldGenerateConstructorBarrier() const {
313 return should_generate_constructor_barrier_;
314 }
315
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000316 bool IsDebuggable() const { return debuggable_; }
317
David Brazdil8d5b8b22015-03-24 10:51:52 +0000318 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000319 // already, it is created and inserted into the graph. This method is only for
320 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600321 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000322
323 // TODO: This is problematic for the consistency of reference type propagation
324 // because it can be created anytime after the pass and thus it will be left
325 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600326 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000327
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600328 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
329 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000330 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600331 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
332 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000333 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600334 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
335 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000336 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600337 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
338 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000339 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000340
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100341 HCurrentMethod* GetCurrentMethod();
342
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100344
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100345 const DexFile& GetDexFile() const {
346 return dex_file_;
347 }
348
349 uint32_t GetMethodIdx() const {
350 return method_idx_;
351 }
352
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100353 InvokeType GetInvokeType() const {
354 return invoke_type_;
355 }
356
Mark Mendellc4701932015-04-10 13:18:51 -0400357 InstructionSet GetInstructionSet() const {
358 return instruction_set_;
359 }
360
David Brazdilbbd733e2015-08-18 17:48:17 +0100361 // TODO: Remove once the full compilation pipeline is enabled for try/catch.
362 bool HasTryCatch() const;
363
David Brazdil2d7352b2015-04-20 14:52:42 +0100364 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 void VisitBlockForDominatorTree(HBasicBlock* block,
366 HBasicBlock* predecessor,
367 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100368 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369 void VisitBlockForBackEdges(HBasicBlock* block,
370 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100371 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000372 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100373 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000375 template <class InstructionType, typename ValueType>
376 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600377 ArenaSafeMap<ValueType, InstructionType*>* cache,
378 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000379 // Try to find an existing constant of the given value.
380 InstructionType* constant = nullptr;
381 auto cached_constant = cache->find(value);
382 if (cached_constant != cache->end()) {
383 constant = cached_constant->second;
384 }
385
386 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100387 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000388 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600389 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000390 cache->Overwrite(value, constant);
391 InsertConstant(constant);
392 }
393 return constant;
394 }
395
David Brazdil8d5b8b22015-03-24 10:51:52 +0000396 void InsertConstant(HConstant* instruction);
397
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000398 // Cache a float constant into the graph. This method should only be
399 // called by the SsaBuilder when creating "equivalent" instructions.
400 void CacheFloatConstant(HFloatConstant* constant);
401
402 // See CacheFloatConstant comment.
403 void CacheDoubleConstant(HDoubleConstant* constant);
404
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000405 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000406
407 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408 GrowableArray<HBasicBlock*> blocks_;
409
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 // List of blocks to perform a reverse post order tree traversal.
411 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000412
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100413 // List of blocks to perform a linear order tree traversal.
414 GrowableArray<HBasicBlock*> linear_order_;
415
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000416 HBasicBlock* entry_block_;
417 HBasicBlock* exit_block_;
418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100419 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100420 uint16_t maximum_number_of_out_vregs_;
421
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100422 // The number of virtual registers in this method. Contains the parameters.
423 uint16_t number_of_vregs_;
424
425 // The number of virtual registers used by parameters of this method.
426 uint16_t number_of_in_vregs_;
427
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000428 // Number of vreg size slots that the temporaries use (used in baseline compiler).
429 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430
Mark Mendell1152c922015-04-24 17:06:35 -0400431 // Has bounds checks. We can totally skip BCE if it's false.
432 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800433
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000434 // Indicates whether the graph should be compiled in a way that
435 // ensures full debuggability. If false, we can apply more
436 // aggressive optimizations that may limit the level of debugging.
437 const bool debuggable_;
438
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000439 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100442 // The dex file from which the method is from.
443 const DexFile& dex_file_;
444
445 // The method index in the dex file.
446 const uint32_t method_idx_;
447
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100448 // If inlined, this encodes how the callee is being invoked.
449 const InvokeType invoke_type_;
450
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100451 // Whether the graph has been transformed to SSA form. Only used
452 // in debug mode to ensure we are not using properties only valid
453 // for non-SSA form (like the number of temporaries).
454 bool in_ssa_form_;
455
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100456 const bool should_generate_constructor_barrier_;
457
Mathieu Chartiere401d142015-04-22 13:56:20 -0700458 const InstructionSet instruction_set_;
459
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000460 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000461 HNullConstant* cached_null_constant_;
462 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000464 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000465 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000466
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100467 HCurrentMethod* cached_current_method_;
468
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000469 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100470 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000471 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472 DISALLOW_COPY_AND_ASSIGN(HGraph);
473};
474
Vladimir Markof9f64412015-09-02 14:05:49 +0100475class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000476 public:
477 HLoopInformation(HBasicBlock* header, HGraph* graph)
478 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100479 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100481 // Make bit vector growable, as the number of blocks may change.
482 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100483
484 HBasicBlock* GetHeader() const {
485 return header_;
486 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000487
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000488 void SetHeader(HBasicBlock* block) {
489 header_ = block;
490 }
491
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100492 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
493 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
494 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
495
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000496 void AddBackEdge(HBasicBlock* back_edge) {
497 back_edges_.Add(back_edge);
498 }
499
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100500 void RemoveBackEdge(HBasicBlock* back_edge) {
501 back_edges_.Delete(back_edge);
502 }
503
David Brazdil46e2a392015-03-16 17:31:52 +0000504 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100505 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000506 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 }
508 return false;
509 }
510
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000511 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512 return back_edges_.Size();
513 }
514
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100516
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
518 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100519 }
520
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100521 // Returns the lifetime position of the back edge that has the
522 // greatest lifetime position.
523 size_t GetLifetimeEnd() const;
524
525 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
526 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
527 if (back_edges_.Get(i) == existing) {
528 back_edges_.Put(i, new_back_edge);
529 return;
530 }
531 }
532 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100533 }
534
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100535 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100536 // that is the header dominates the back edge.
537 bool Populate();
538
David Brazdila4b8c212015-05-07 09:59:30 +0100539 // Reanalyzes the loop by removing loop info from its blocks and re-running
540 // Populate(). If there are no back edges left, the loop info is completely
541 // removed as well as its SuspendCheck instruction. It must be run on nested
542 // inner loops first.
543 void Update();
544
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // Returns whether this loop information contains `block`.
546 // Note that this loop information *must* be populated before entering this function.
547 bool Contains(const HBasicBlock& block) const;
548
549 // Returns whether this loop information is an inner loop of `other`.
550 // Note that `other` *must* be populated before entering this function.
551 bool IsIn(const HLoopInformation& other) const;
552
553 const ArenaBitVector& GetBlocks() const { return blocks_; }
554
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000555 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000556 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000557
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 // Internal recursive implementation of `Populate`.
560 void PopulateRecursive(HBasicBlock* block);
561
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100563 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566
567 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
568};
569
David Brazdilec16f792015-08-19 15:04:01 +0100570// Stores try/catch information for basic blocks.
571// Note that HGraph is constructed so that catch blocks cannot simultaneously
572// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100573class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100574 public:
575 // Try block information constructor.
576 explicit TryCatchInformation(const HTryBoundary& try_entry)
577 : try_entry_(&try_entry),
578 catch_dex_file_(nullptr),
579 catch_type_index_(DexFile::kDexNoIndex16) {
580 DCHECK(try_entry_ != nullptr);
581 }
582
583 // Catch block information constructor.
584 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
585 : try_entry_(nullptr),
586 catch_dex_file_(&dex_file),
587 catch_type_index_(catch_type_index) {}
588
589 bool IsTryBlock() const { return try_entry_ != nullptr; }
590
591 const HTryBoundary& GetTryEntry() const {
592 DCHECK(IsTryBlock());
593 return *try_entry_;
594 }
595
596 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
597
598 bool IsCatchAllTypeIndex() const {
599 DCHECK(IsCatchBlock());
600 return catch_type_index_ == DexFile::kDexNoIndex16;
601 }
602
603 uint16_t GetCatchTypeIndex() const {
604 DCHECK(IsCatchBlock());
605 return catch_type_index_;
606 }
607
608 const DexFile& GetCatchDexFile() const {
609 DCHECK(IsCatchBlock());
610 return *catch_dex_file_;
611 }
612
613 private:
614 // One of possibly several TryBoundary instructions entering the block's try.
615 // Only set for try blocks.
616 const HTryBoundary* try_entry_;
617
618 // Exception type information. Only set for catch blocks.
619 const DexFile* catch_dex_file_;
620 const uint16_t catch_type_index_;
621};
622
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100623static constexpr size_t kNoLifetime = -1;
624
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625// A block in a method. Contains the list of instructions represented
626// as a double linked list. Each block knows its predecessors and
627// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100628
Vladimir Markof9f64412015-09-02 14:05:49 +0100629class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600631 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 : graph_(graph),
Vladimir Marko145acc52015-09-03 13:33:25 +0000633 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
634 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635 loop_information_(nullptr),
636 dominator_(nullptr),
Vladimir Marko145acc52015-09-03 13:33:25 +0000637 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100638 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100639 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000641 lifetime_end_(kNoLifetime),
Vladimir Marko145acc52015-09-03 13:33:25 +0000642 try_catch_information_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643
Vladimir Marko145acc52015-09-03 13:33:25 +0000644 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100645 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000646 }
647
Vladimir Marko145acc52015-09-03 13:33:25 +0000648 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650 }
651
Vladimir Marko145acc52015-09-03 13:33:25 +0000652 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100653 return dominated_blocks_;
654 }
655
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100656 bool IsEntryBlock() const {
657 return graph_->GetEntryBlock() == this;
658 }
659
660 bool IsExitBlock() const {
661 return graph_->GetExitBlock() == this;
662 }
663
David Brazdil46e2a392015-03-16 17:31:52 +0000664 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000665 bool IsSingleTryBoundary() const;
666
667 // Returns true if this block emits nothing but a jump.
668 bool IsSingleJump() const {
669 HLoopInformation* loop_info = GetLoopInformation();
670 return (IsSingleGoto() || IsSingleTryBoundary())
671 // Back edges generate a suspend check.
672 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
673 }
David Brazdil46e2a392015-03-16 17:31:52 +0000674
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000675 void AddBackEdge(HBasicBlock* back_edge) {
676 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000677 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000678 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680 loop_information_->AddBackEdge(back_edge);
681 }
682
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000683 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000684 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000685
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000686 int GetBlockId() const { return block_id_; }
687 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600688 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000689
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000690 HBasicBlock* GetDominator() const { return dominator_; }
691 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko145acc52015-09-03 13:33:25 +0000692 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
693 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Vladimir Marko91e11c02015-09-02 17:03:22 +0100694 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000695 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
696 if (dominated_blocks_.Get(i) == existing) {
697 dominated_blocks_.Put(i, new_block);
698 return;
699 }
700 }
701 LOG(FATAL) << "Unreachable";
702 UNREACHABLE();
Vladimir Marko91e11c02015-09-02 17:03:22 +0100703 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100704 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000705
706 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100707 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000708 }
709
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
711 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100712 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100713 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100714 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
715 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000716
717 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000718 successors_.Add(block);
719 block->predecessors_.Add(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720 }
721
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100722 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
723 size_t successor_index = GetSuccessorIndexOf(existing);
Vladimir Marko145acc52015-09-03 13:33:25 +0000724 DCHECK_NE(successor_index, static_cast<size_t>(-1));
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100725 existing->RemovePredecessor(this);
Vladimir Marko145acc52015-09-03 13:33:25 +0000726 new_block->predecessors_.Add(this);
727 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000728 }
729
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000730 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
731 size_t predecessor_index = GetPredecessorIndexOf(existing);
Vladimir Marko145acc52015-09-03 13:33:25 +0000732 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000733 existing->RemoveSuccessor(this);
Vladimir Marko145acc52015-09-03 13:33:25 +0000734 new_block->successors_.Add(this);
735 predecessors_.Put(predecessor_index, new_block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000736 }
737
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100738 // Insert `this` between `predecessor` and `successor. This method
739 // preserves the indicies, and will update the first edge found between
740 // `predecessor` and `successor`.
741 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
742 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Vladimir Marko145acc52015-09-03 13:33:25 +0000743 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100744 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko145acc52015-09-03 13:33:25 +0000745 DCHECK_NE(successor_index, static_cast<size_t>(-1));
746 successor->predecessors_.Put(predecessor_index, this);
747 predecessor->successors_.Put(successor_index, this);
748 successors_.Add(successor);
749 predecessors_.Add(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100750 }
751
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100752 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000753 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100754 }
755
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000757 successors_.Delete(block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000758 }
759
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100760 void ClearAllPredecessors() {
Vladimir Marko145acc52015-09-03 13:33:25 +0000761 predecessors_.Reset();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 }
763
764 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko145acc52015-09-03 13:33:25 +0000765 predecessors_.Add(block);
766 block->successors_.Add(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767 }
768
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100769 void SwapPredecessors() {
Vladimir Marko145acc52015-09-03 13:33:25 +0000770 DCHECK_EQ(predecessors_.Size(), 2u);
771 HBasicBlock* temp = predecessors_.Get(0);
772 predecessors_.Put(0, predecessors_.Get(1));
773 predecessors_.Put(1, temp);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100774 }
775
David Brazdil769c9e52015-04-27 13:54:09 +0100776 void SwapSuccessors() {
Vladimir Marko145acc52015-09-03 13:33:25 +0000777 DCHECK_EQ(successors_.Size(), 2u);
778 HBasicBlock* temp = successors_.Get(0);
779 successors_.Put(0, successors_.Get(1));
780 successors_.Put(1, temp);
David Brazdil769c9e52015-04-27 13:54:09 +0100781 }
782
David Brazdilfc6a86a2015-06-26 10:33:45 +0000783 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000784 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
785 if (predecessors_.Get(i) == predecessor) {
786 return i;
787 }
788 }
789 return -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790 }
791
David Brazdilfc6a86a2015-06-26 10:33:45 +0000792 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000793 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
794 if (successors_.Get(i) == successor) {
795 return i;
796 }
797 }
798 return -1;
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100799 }
800
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000802 DCHECK_EQ(GetPredecessors().Size(), 1u);
803 return GetPredecessors().Get(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000804 }
805
806 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000807 DCHECK_EQ(GetSuccessors().Size(), 1u);
808 return GetSuccessors().Get(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000809 }
810
811 // Returns whether the first occurrence of `predecessor` in the list of
812 // predecessors is at index `idx`.
813 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000814 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000815 return GetPredecessorIndexOf(predecessor) == idx;
816 }
817
David Brazdilffee3d32015-07-06 11:48:53 +0100818 // Returns the number of non-exceptional successors. SsaChecker ensures that
819 // these are stored at the beginning of the successor list.
820 size_t NumberOfNormalSuccessors() const {
Vladimir Marko145acc52015-09-03 13:33:25 +0000821 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
David Brazdilffee3d32015-07-06 11:48:53 +0100822 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000823
824 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100825 // created, latter block. Note that this method will add the block to the
826 // graph, create a Goto at the end of the former block and will create an edge
827 // between the blocks. It will not, however, update the reverse post order or
828 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000829 HBasicBlock* SplitBefore(HInstruction* cursor);
830
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000831 // Split the block into two blocks just after `cursor`. Returns the newly
832 // created block. Note that this method just updates raw block information,
833 // like predecessors, successors, dominators, and instruction list. It does not
834 // update the graph, reverse post order, loop information, nor make sure the
835 // blocks are consistent (for example ending with a control flow instruction).
836 HBasicBlock* SplitAfter(HInstruction* cursor);
837
838 // Merge `other` at the end of `this`. Successors and dominated blocks of
839 // `other` are changed to be successors and dominated blocks of `this`. Note
840 // that this method does not update the graph, reverse post order, loop
841 // information, nor make sure the blocks are consistent (for example ending
842 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100843 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000844
845 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
846 // of `this` are moved to `other`.
847 // Note that this method does not update the graph, reverse post order, loop
848 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000849 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850 void ReplaceWith(HBasicBlock* other);
851
David Brazdil2d7352b2015-04-20 14:52:42 +0100852 // Merge `other` at the end of `this`. This method updates loops, reverse post
853 // order, links to predecessors, successors, dominators and deletes the block
854 // from the graph. The two blocks must be successive, i.e. `this` the only
855 // predecessor of `other` and vice versa.
856 void MergeWith(HBasicBlock* other);
857
858 // Disconnects `this` from all its predecessors, successors and dominator,
859 // removes it from all loops it is included in and eventually from the graph.
860 // The block must not dominate any other block. Predecessors and successors
861 // are safely updated.
862 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000863
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100865 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100866 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100867 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100868 // Replace instruction `initial` with `replacement` within this block.
869 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
870 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100871 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100872 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000873 // RemoveInstruction and RemovePhi delete a given instruction from the respective
874 // instruction list. With 'ensure_safety' set to true, it verifies that the
875 // instruction is not in use and removes it from the use lists of its inputs.
876 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
877 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100878 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879
880 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100881 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 }
883
Roland Levillain6b879dd2014-09-22 17:13:44 +0100884 bool IsLoopPreHeaderFirstPredecessor() const {
885 DCHECK(IsLoopHeader());
Vladimir Marko145acc52015-09-03 13:33:25 +0000886 DCHECK(!GetPredecessors().IsEmpty());
887 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100888 }
889
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100890 HLoopInformation* GetLoopInformation() const {
891 return loop_information_;
892 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100895 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100897 void SetInLoop(HLoopInformation* info) {
898 if (IsLoopHeader()) {
899 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100900 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 loop_information_ = info;
902 } else if (loop_information_->Contains(*info->GetHeader())) {
903 // Block is currently part of an outer loop. Make it part of this inner loop.
904 // Note that a non loop header having a loop information means this loop information
905 // has already been populated
906 loop_information_ = info;
907 } else {
908 // Block is part of an inner loop. Do not update the loop information.
909 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
910 // at this point, because this method is being called while populating `info`.
911 }
912 }
913
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000914 // Raw update of the loop information.
915 void SetLoopInformation(HLoopInformation* info) {
916 loop_information_ = info;
917 }
918
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100919 bool IsInLoop() const { return loop_information_ != nullptr; }
920
David Brazdilec16f792015-08-19 15:04:01 +0100921 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
922
923 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
924 try_catch_information_ = try_catch_information;
925 }
926
927 bool IsTryBlock() const {
928 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
929 }
930
931 bool IsCatchBlock() const {
932 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
933 }
David Brazdilffee3d32015-07-06 11:48:53 +0100934
935 // Returns the try entry that this block's successors should have. They will
936 // be in the same try, unless the block ends in a try boundary. In that case,
937 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100938 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100939
David Brazdila4b8c212015-05-07 09:59:30 +0100940 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100941 bool Dominates(HBasicBlock* block) const;
942
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100943 size_t GetLifetimeStart() const { return lifetime_start_; }
944 size_t GetLifetimeEnd() const { return lifetime_end_; }
945
946 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
947 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
948
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100949
David Brazdil8d5b8b22015-03-24 10:51:52 +0000950 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000951 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100952 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000953 bool HasSinglePhi() const;
954
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000955 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000956 HGraph* graph_;
Vladimir Marko145acc52015-09-03 13:33:25 +0000957 GrowableArray<HBasicBlock*> predecessors_;
958 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100959 HInstructionList instructions_;
960 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000961 HLoopInformation* loop_information_;
962 HBasicBlock* dominator_;
Vladimir Marko145acc52015-09-03 13:33:25 +0000963 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100965 // The dex program counter of the first instruction of this block.
966 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100967 size_t lifetime_start_;
968 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100969 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100970
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000971 friend class HGraph;
972 friend class HInstruction;
973
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
975};
976
David Brazdilb2bd1c52015-03-25 11:17:37 +0000977// Iterates over the LoopInformation of all loops which contain 'block'
978// from the innermost to the outermost.
979class HLoopInformationOutwardIterator : public ValueObject {
980 public:
981 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
982 : current_(block.GetLoopInformation()) {}
983
984 bool Done() const { return current_ == nullptr; }
985
986 void Advance() {
987 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100988 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000989 }
990
991 HLoopInformation* Current() const {
992 DCHECK(!Done());
993 return current_;
994 }
995
996 private:
997 HLoopInformation* current_;
998
999 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1000};
1001
Alexandre Ramesef20f712015-06-09 10:29:30 +01001002#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001003 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001004 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001005 M(ArrayGet, Instruction) \
1006 M(ArrayLength, Instruction) \
1007 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001008 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001010 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001011 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001012 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001013 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001014 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001016 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001017 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001019 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001023 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(FloatConstant, Constant) \
1025 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026 M(GreaterThan, Condition) \
1027 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001029 M(InstanceFieldGet, Instruction) \
1030 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001031 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001033 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001034 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(LessThan, Condition) \
1037 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001038 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001039 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001041 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(Local, Instruction) \
1043 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001044 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001045 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(Mul, BinaryOperation) \
1047 M(Neg, UnaryOperation) \
1048 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001050 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001052 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001053 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001054 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001055 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001056 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001058 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(Return, Instruction) \
1060 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001061 M(Shl, BinaryOperation) \
1062 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001063 M(StaticFieldGet, Instruction) \
1064 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001065 M(StoreLocal, Instruction) \
1066 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001067 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001068 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001069 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001070 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001071 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001072 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001073 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001074
Alexandre Ramesef20f712015-06-09 10:29:30 +01001075#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1076
1077#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1078
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001079#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1080
Mark Mendell0616ae02015-04-17 12:49:27 -04001081#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1082 M(X86ComputeBaseMethodAddress, Instruction) \
1083 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001084
1085#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1086
1087#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1088 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1089 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1090 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001091 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001092 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1093 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1094
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001095#define FOR_EACH_INSTRUCTION(M) \
1096 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1097 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001098 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001099 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001100 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001101
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001102#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001103FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1104#undef FORWARD_DECLARATION
1105
Roland Levillainccc07a92014-09-16 14:48:16 +01001106#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001107 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1108 const char* DebugName() const OVERRIDE { return #type; } \
1109 const H##type* As##type() const OVERRIDE { return this; } \
1110 H##type* As##type() OVERRIDE { return this; } \
1111 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001112 return other->Is##type(); \
1113 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001114 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115
David Brazdiled596192015-01-23 10:39:45 +00001116template <typename T> class HUseList;
1117
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001119class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001120 public:
David Brazdiled596192015-01-23 10:39:45 +00001121 HUseListNode* GetPrevious() const { return prev_; }
1122 HUseListNode* GetNext() const { return next_; }
1123 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001124 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001125 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001126
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001127 private:
David Brazdiled596192015-01-23 10:39:45 +00001128 HUseListNode(T user, size_t index)
1129 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1130
1131 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001132 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001133 HUseListNode<T>* prev_;
1134 HUseListNode<T>* next_;
1135
1136 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001137
1138 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1139};
1140
David Brazdiled596192015-01-23 10:39:45 +00001141template <typename T>
1142class HUseList : public ValueObject {
1143 public:
1144 HUseList() : first_(nullptr) {}
1145
1146 void Clear() {
1147 first_ = nullptr;
1148 }
1149
1150 // Adds a new entry at the beginning of the use list and returns
1151 // the newly created node.
1152 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001153 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001154 if (IsEmpty()) {
1155 first_ = new_node;
1156 } else {
1157 first_->prev_ = new_node;
1158 new_node->next_ = first_;
1159 first_ = new_node;
1160 }
1161 return new_node;
1162 }
1163
1164 HUseListNode<T>* GetFirst() const {
1165 return first_;
1166 }
1167
1168 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001169 DCHECK(node != nullptr);
1170 DCHECK(Contains(node));
1171
David Brazdiled596192015-01-23 10:39:45 +00001172 if (node->prev_ != nullptr) {
1173 node->prev_->next_ = node->next_;
1174 }
1175 if (node->next_ != nullptr) {
1176 node->next_->prev_ = node->prev_;
1177 }
1178 if (node == first_) {
1179 first_ = node->next_;
1180 }
1181 }
1182
David Brazdil1abb4192015-02-17 18:33:36 +00001183 bool Contains(const HUseListNode<T>* node) const {
1184 if (node == nullptr) {
1185 return false;
1186 }
1187 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1188 if (current == node) {
1189 return true;
1190 }
1191 }
1192 return false;
1193 }
1194
David Brazdiled596192015-01-23 10:39:45 +00001195 bool IsEmpty() const {
1196 return first_ == nullptr;
1197 }
1198
1199 bool HasOnlyOneUse() const {
1200 return first_ != nullptr && first_->next_ == nullptr;
1201 }
1202
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001203 size_t SizeSlow() const {
1204 size_t count = 0;
1205 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1206 ++count;
1207 }
1208 return count;
1209 }
1210
David Brazdiled596192015-01-23 10:39:45 +00001211 private:
1212 HUseListNode<T>* first_;
1213};
1214
1215template<typename T>
1216class HUseIterator : public ValueObject {
1217 public:
1218 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1219
1220 bool Done() const { return current_ == nullptr; }
1221
1222 void Advance() {
1223 DCHECK(!Done());
1224 current_ = current_->GetNext();
1225 }
1226
1227 HUseListNode<T>* Current() const {
1228 DCHECK(!Done());
1229 return current_;
1230 }
1231
1232 private:
1233 HUseListNode<T>* current_;
1234
1235 friend class HValue;
1236};
1237
David Brazdil1abb4192015-02-17 18:33:36 +00001238// This class is used by HEnvironment and HInstruction classes to record the
1239// instructions they use and pointers to the corresponding HUseListNodes kept
1240// by the used instructions.
1241template <typename T>
1242class HUserRecord : public ValueObject {
1243 public:
1244 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1245 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1246
1247 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1248 : instruction_(old_record.instruction_), use_node_(use_node) {
1249 DCHECK(instruction_ != nullptr);
1250 DCHECK(use_node_ != nullptr);
1251 DCHECK(old_record.use_node_ == nullptr);
1252 }
1253
1254 HInstruction* GetInstruction() const { return instruction_; }
1255 HUseListNode<T>* GetUseNode() const { return use_node_; }
1256
1257 private:
1258 // Instruction used by the user.
1259 HInstruction* instruction_;
1260
1261 // Corresponding entry in the use list kept by 'instruction_'.
1262 HUseListNode<T>* use_node_;
1263};
1264
Aart Bik854a02b2015-07-14 16:07:00 -07001265/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001266 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001267 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001268 * For write/read dependences on fields/arrays, the dependence analysis uses
1269 * type disambiguation (e.g. a float field write cannot modify the value of an
1270 * integer field read) and the access type (e.g. a reference array write cannot
1271 * modify the value of a reference field read [although it may modify the
1272 * reference fetch prior to reading the field, which is represented by its own
1273 * write/read dependence]). The analysis makes conservative points-to
1274 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1275 * the same, and any reference read depends on any reference read without
1276 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001277 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001278 * The internal representation uses 38-bit and is described in the table below.
1279 * The first line indicates the side effect, and for field/array accesses the
1280 * second line indicates the type of the access (in the order of the
1281 * Primitive::Type enum).
1282 * The two numbered lines below indicate the bit position in the bitfield (read
1283 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001284 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001285 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1286 * +-------------+---------+---------+--------------+---------+---------+
1287 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1288 * | 3 |333333322|222222221| 1 |111111110|000000000|
1289 * | 7 |654321098|765432109| 8 |765432109|876543210|
1290 *
1291 * Note that, to ease the implementation, 'changes' bits are least significant
1292 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001293 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001294class SideEffects : public ValueObject {
1295 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001296 SideEffects() : flags_(0) {}
1297
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001298 static SideEffects None() {
1299 return SideEffects(0);
1300 }
1301
1302 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001303 return SideEffects(kAllChangeBits | kAllDependOnBits);
1304 }
1305
1306 static SideEffects AllChanges() {
1307 return SideEffects(kAllChangeBits);
1308 }
1309
1310 static SideEffects AllDependencies() {
1311 return SideEffects(kAllDependOnBits);
1312 }
1313
1314 static SideEffects AllExceptGCDependency() {
1315 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1316 }
1317
1318 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001319 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001320 }
1321
Aart Bik34c3ba92015-07-20 14:08:59 -07001322 static SideEffects AllWrites() {
1323 return SideEffects(kAllWrites);
1324 }
1325
1326 static SideEffects AllReads() {
1327 return SideEffects(kAllReads);
1328 }
1329
1330 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1331 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001332 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001333 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001334 }
1335
Aart Bik854a02b2015-07-14 16:07:00 -07001336 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1337 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001338 }
1339
Aart Bik34c3ba92015-07-20 14:08:59 -07001340 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1341 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001342 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001343 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001344 }
1345
1346 static SideEffects ArrayReadOfType(Primitive::Type type) {
1347 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1348 }
1349
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001350 static SideEffects CanTriggerGC() {
1351 return SideEffects(1ULL << kCanTriggerGCBit);
1352 }
1353
1354 static SideEffects DependsOnGC() {
1355 return SideEffects(1ULL << kDependsOnGCBit);
1356 }
1357
Aart Bik854a02b2015-07-14 16:07:00 -07001358 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001359 SideEffects Union(SideEffects other) const {
1360 return SideEffects(flags_ | other.flags_);
1361 }
1362
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001363 SideEffects Exclusion(SideEffects other) const {
1364 return SideEffects(flags_ & ~other.flags_);
1365 }
1366
1367 bool Includes(SideEffects other) const {
1368 return (other.flags_ & flags_) == other.flags_;
1369 }
1370
1371 bool HasSideEffects() const {
1372 return (flags_ & kAllChangeBits);
1373 }
1374
1375 bool HasDependencies() const {
1376 return (flags_ & kAllDependOnBits);
1377 }
1378
1379 // Returns true if there are no side effects or dependencies.
1380 bool DoesNothing() const {
1381 return flags_ == 0;
1382 }
1383
Aart Bik854a02b2015-07-14 16:07:00 -07001384 // Returns true if something is written.
1385 bool DoesAnyWrite() const {
1386 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001387 }
1388
Aart Bik854a02b2015-07-14 16:07:00 -07001389 // Returns true if something is read.
1390 bool DoesAnyRead() const {
1391 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001392 }
1393
Aart Bik854a02b2015-07-14 16:07:00 -07001394 // Returns true if potentially everything is written and read
1395 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001396 bool DoesAllReadWrite() const {
1397 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1398 }
1399
Aart Bik854a02b2015-07-14 16:07:00 -07001400 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001401 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001402 }
1403
1404 // Returns true if this may read something written by other.
1405 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1407 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001408 }
1409
1410 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001411 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001412 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001413 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 for (int s = kLastBit; s >= 0; s--) {
1415 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1416 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1417 // This is a bit for the GC side effect.
1418 if (current_bit_is_set) {
1419 flags += "GC";
1420 }
Aart Bik854a02b2015-07-14 16:07:00 -07001421 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001422 } else {
1423 // This is a bit for the array/field analysis.
1424 // The underscore character stands for the 'can trigger GC' bit.
1425 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1426 if (current_bit_is_set) {
1427 flags += kDebug[s];
1428 }
1429 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1430 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1431 flags += "|";
1432 }
1433 }
Aart Bik854a02b2015-07-14 16:07:00 -07001434 }
1435 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001436 }
1437
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001438 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001439
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001440 private:
1441 static constexpr int kFieldArrayAnalysisBits = 9;
1442
1443 static constexpr int kFieldWriteOffset = 0;
1444 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1445 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1446 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1447
1448 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1449
1450 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1451 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1452 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1453 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1454
1455 static constexpr int kLastBit = kDependsOnGCBit;
1456 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1457
1458 // Aliases.
1459
1460 static_assert(kChangeBits == kDependOnBits,
1461 "the 'change' bits should match the 'depend on' bits.");
1462
1463 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1464 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1465 static constexpr uint64_t kAllWrites =
1466 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1467 static constexpr uint64_t kAllReads =
1468 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001469
Aart Bik854a02b2015-07-14 16:07:00 -07001470 // Work around the fact that HIR aliases I/F and J/D.
1471 // TODO: remove this interceptor once HIR types are clean
1472 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1473 switch (type) {
1474 case Primitive::kPrimInt:
1475 case Primitive::kPrimFloat:
1476 return TypeFlag(Primitive::kPrimInt, offset) |
1477 TypeFlag(Primitive::kPrimFloat, offset);
1478 case Primitive::kPrimLong:
1479 case Primitive::kPrimDouble:
1480 return TypeFlag(Primitive::kPrimLong, offset) |
1481 TypeFlag(Primitive::kPrimDouble, offset);
1482 default:
1483 return TypeFlag(type, offset);
1484 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001485 }
1486
Aart Bik854a02b2015-07-14 16:07:00 -07001487 // Translates type to bit flag.
1488 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1489 CHECK_NE(type, Primitive::kPrimVoid);
1490 const uint64_t one = 1;
1491 const int shift = type; // 0-based consecutive enum
1492 DCHECK_LE(kFieldWriteOffset, shift);
1493 DCHECK_LT(shift, kArrayWriteOffset);
1494 return one << (type + offset);
1495 }
1496
1497 // Private constructor on direct flags value.
1498 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1499
1500 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501};
1502
David Brazdiled596192015-01-23 10:39:45 +00001503// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001504class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001505 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001506 HEnvironment(ArenaAllocator* arena,
1507 size_t number_of_vregs,
1508 const DexFile& dex_file,
1509 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001510 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001511 InvokeType invoke_type,
1512 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001513 : vregs_(arena, number_of_vregs),
1514 locations_(arena, number_of_vregs),
1515 parent_(nullptr),
1516 dex_file_(dex_file),
1517 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001518 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001519 invoke_type_(invoke_type),
1520 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001521 vregs_.SetSize(number_of_vregs);
1522 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001523 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001524 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001525
1526 locations_.SetSize(number_of_vregs);
1527 for (size_t i = 0; i < number_of_vregs; ++i) {
1528 locations_.Put(i, Location());
1529 }
1530 }
1531
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001532 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001533 : HEnvironment(arena,
1534 to_copy.Size(),
1535 to_copy.GetDexFile(),
1536 to_copy.GetMethodIdx(),
1537 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001538 to_copy.GetInvokeType(),
1539 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001540
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001541 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001542 if (parent_ != nullptr) {
1543 parent_->SetAndCopyParentChain(allocator, parent);
1544 } else {
1545 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1546 parent_->CopyFrom(parent);
1547 if (parent->GetParent() != nullptr) {
1548 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1549 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001550 }
David Brazdiled596192015-01-23 10:39:45 +00001551 }
1552
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001553 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1554 void CopyFrom(HEnvironment* environment);
1555
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001556 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1557 // input to the loop phi instead. This is for inserting instructions that
1558 // require an environment (like HDeoptimization) in the loop pre-header.
1559 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001560
1561 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001562 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001563 }
1564
1565 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001566 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001567 }
1568
David Brazdil1abb4192015-02-17 18:33:36 +00001569 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001570
1571 size_t Size() const { return vregs_.Size(); }
1572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001573 HEnvironment* GetParent() const { return parent_; }
1574
1575 void SetLocationAt(size_t index, Location location) {
1576 locations_.Put(index, location);
1577 }
1578
1579 Location GetLocationAt(size_t index) const {
1580 return locations_.Get(index);
1581 }
1582
1583 uint32_t GetDexPc() const {
1584 return dex_pc_;
1585 }
1586
1587 uint32_t GetMethodIdx() const {
1588 return method_idx_;
1589 }
1590
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001591 InvokeType GetInvokeType() const {
1592 return invoke_type_;
1593 }
1594
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001595 const DexFile& GetDexFile() const {
1596 return dex_file_;
1597 }
1598
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001599 HInstruction* GetHolder() const {
1600 return holder_;
1601 }
1602
David Brazdiled596192015-01-23 10:39:45 +00001603 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001604 // Record instructions' use entries of this environment for constant-time removal.
1605 // It should only be called by HInstruction when a new environment use is added.
1606 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1607 DCHECK(env_use->GetUser() == this);
1608 size_t index = env_use->GetIndex();
1609 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1610 }
David Brazdiled596192015-01-23 10:39:45 +00001611
David Brazdil1abb4192015-02-17 18:33:36 +00001612 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001613 GrowableArray<Location> locations_;
1614 HEnvironment* parent_;
1615 const DexFile& dex_file_;
1616 const uint32_t method_idx_;
1617 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001618 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001619
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001620 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001621 HInstruction* const holder_;
1622
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001623 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001624
1625 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1626};
1627
Calin Juravleacf735c2015-02-12 15:25:22 +00001628class ReferenceTypeInfo : ValueObject {
1629 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001630 typedef Handle<mirror::Class> TypeHandle;
1631
Calin Juravle2e768302015-07-28 14:41:11 +00001632 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1633 // The constructor will check that the type_handle is valid.
1634 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001635 }
1636
Calin Juravle2e768302015-07-28 14:41:11 +00001637 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1638
1639 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1640 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001641 }
1642
Calin Juravle2e768302015-07-28 14:41:11 +00001643 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1644 return IsValidHandle(type_handle_);
1645 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001646 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001647
1648 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1649 DCHECK(IsValid());
1650 return GetTypeHandle()->IsObjectClass();
1651 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001652 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001653 DCHECK(IsValid());
1654 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001655 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001656
1657 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1658
Mathieu Chartier90443472015-07-16 20:32:27 -07001659 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001660 DCHECK(IsValid());
1661 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001662 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1663 }
1664
1665 // Returns true if the type information provide the same amount of details.
1666 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001667 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001668 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001669 if (!IsValid() && !rti.IsValid()) {
1670 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001671 return true;
1672 }
Calin Juravle2e768302015-07-28 14:41:11 +00001673 if (!IsValid() || !rti.IsValid()) {
1674 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001675 return false;
1676 }
Calin Juravle2e768302015-07-28 14:41:11 +00001677 return IsExact() == rti.IsExact()
1678 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001679 }
1680
1681 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001682 ReferenceTypeInfo();
1683 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001684
Calin Juravleacf735c2015-02-12 15:25:22 +00001685 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001686 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001687 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001688 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001689 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001690};
1691
1692std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1693
Vladimir Markof9f64412015-09-02 14:05:49 +01001694class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001695 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001696 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001697 : previous_(nullptr),
1698 next_(nullptr),
1699 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001700 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001701 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001702 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001703 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001704 locations_(nullptr),
1705 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001706 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001707 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001708 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001709
Dave Allison20dfc792014-06-16 20:44:29 -07001710 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001712#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001713 enum InstructionKind {
1714 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1715 };
1716#undef DECLARE_KIND
1717
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001718 HInstruction* GetNext() const { return next_; }
1719 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001720
Calin Juravle77520bc2015-01-12 18:45:46 +00001721 HInstruction* GetNextDisregardingMoves() const;
1722 HInstruction* GetPreviousDisregardingMoves() const;
1723
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001724 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001725 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001726 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001727 bool IsInBlock() const { return block_ != nullptr; }
1728 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001729 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001730
Roland Levillain6b879dd2014-09-22 17:13:44 +01001731 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001732 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001733
1734 virtual void Accept(HGraphVisitor* visitor) = 0;
1735 virtual const char* DebugName() const = 0;
1736
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001737 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001738 void SetRawInputAt(size_t index, HInstruction* input) {
1739 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1740 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001741
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001742 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001743
1744 uint32_t GetDexPc() const { return dex_pc_; }
1745
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001746 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001747
Roland Levillaine161a2a2014-10-03 12:45:18 +01001748 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001749 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001750
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001752 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001753
Calin Juravle10e244f2015-01-26 18:54:32 +00001754 // Does not apply for all instructions, but having this at top level greatly
1755 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001756 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001757 virtual bool CanBeNull() const {
1758 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1759 return true;
1760 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001761
Calin Juravle641547a2015-04-21 22:08:51 +01001762 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1763 UNUSED(obj);
1764 return false;
1765 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001766
Calin Juravle2e768302015-07-28 14:41:11 +00001767 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001768
Calin Juravle61d544b2015-02-23 16:46:57 +00001769 ReferenceTypeInfo GetReferenceTypeInfo() const {
1770 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1771 return reference_type_info_;
1772 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001773
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001774 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001775 DCHECK(user != nullptr);
1776 HUseListNode<HInstruction*>* use =
1777 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1778 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001779 }
1780
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001781 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001782 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001783 HUseListNode<HEnvironment*>* env_use =
1784 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1785 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786 }
1787
David Brazdil1abb4192015-02-17 18:33:36 +00001788 void RemoveAsUserOfInput(size_t input) {
1789 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1790 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1791 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792
David Brazdil1abb4192015-02-17 18:33:36 +00001793 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1794 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795
David Brazdiled596192015-01-23 10:39:45 +00001796 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1797 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001798 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001799 bool HasOnlyOneNonEnvironmentUse() const {
1800 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1801 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001802
Roland Levillain6c82d402014-10-13 16:10:27 +01001803 // Does this instruction strictly dominate `other_instruction`?
1804 // Returns false if this instruction and `other_instruction` are the same.
1805 // Aborts if this instruction and `other_instruction` are both phis.
1806 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001807
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001808 int GetId() const { return id_; }
1809 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001810
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001811 int GetSsaIndex() const { return ssa_index_; }
1812 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1813 bool HasSsaIndex() const { return ssa_index_ != -1; }
1814
1815 bool HasEnvironment() const { return environment_ != nullptr; }
1816 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001817 // Set the `environment_` field. Raw because this method does not
1818 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001819 void SetRawEnvironment(HEnvironment* environment) {
1820 DCHECK(environment_ == nullptr);
1821 DCHECK_EQ(environment->GetHolder(), this);
1822 environment_ = environment;
1823 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001824
1825 // Set the environment of this instruction, copying it from `environment`. While
1826 // copying, the uses lists are being updated.
1827 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001828 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001829 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001830 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001831 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001832 if (environment->GetParent() != nullptr) {
1833 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1834 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001835 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001837 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1838 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001839 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001840 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001841 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001842 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001843 if (environment->GetParent() != nullptr) {
1844 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1845 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001846 }
1847
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 // Returns the number of entries in the environment. Typically, that is the
1849 // number of dex registers in a method. It could be more in case of inlining.
1850 size_t EnvironmentSize() const;
1851
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001852 LocationSummary* GetLocations() const { return locations_; }
1853 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001854
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001856 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857
Alexandre Rames188d4312015-04-09 18:30:21 +01001858 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1859 // uses of this instruction by `other` are *not* updated.
1860 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1861 ReplaceWith(other);
1862 other->ReplaceInput(this, use_index);
1863 }
1864
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001865 // Move `this` instruction before `cursor`.
1866 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001867
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001868#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001869 bool Is##type() const { return (As##type() != nullptr); } \
1870 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001871 virtual H##type* As##type() { return nullptr; }
1872
1873 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1874#undef INSTRUCTION_TYPE_CHECK
1875
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876 // Returns whether the instruction can be moved within the graph.
1877 virtual bool CanBeMoved() const { return false; }
1878
1879 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001880 virtual bool InstructionTypeEquals(HInstruction* other) const {
1881 UNUSED(other);
1882 return false;
1883 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001884
1885 // Returns whether any data encoded in the two instructions is equal.
1886 // This method does not look at the inputs. Both instructions must be
1887 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001888 virtual bool InstructionDataEquals(HInstruction* other) const {
1889 UNUSED(other);
1890 return false;
1891 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001892
1893 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001894 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001895 // 2) Their inputs are identical.
1896 bool Equals(HInstruction* other) const;
1897
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001898 virtual InstructionKind GetKind() const = 0;
1899
1900 virtual size_t ComputeHashCode() const {
1901 size_t result = GetKind();
1902 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1903 result = (result * 31) + InputAt(i)->GetId();
1904 }
1905 return result;
1906 }
1907
1908 SideEffects GetSideEffects() const { return side_effects_; }
1909
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001910 size_t GetLifetimePosition() const { return lifetime_position_; }
1911 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1912 LiveInterval* GetLiveInterval() const { return live_interval_; }
1913 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1914 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1915
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001916 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1917
1918 // Returns whether the code generation of the instruction will require to have access
1919 // to the current method. Such instructions are:
1920 // (1): Instructions that require an environment, as calling the runtime requires
1921 // to walk the stack and have the current method stored at a specific stack address.
1922 // (2): Object literals like classes and strings, that are loaded from the dex cache
1923 // fields of the current method.
1924 bool NeedsCurrentMethod() const {
1925 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1926 }
1927
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001928 virtual bool NeedsDexCache() const { return false; }
1929
Mark Mendellc4701932015-04-10 13:18:51 -04001930 // Does this instruction have any use in an environment before
1931 // control flow hits 'other'?
1932 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1933
1934 // Remove all references to environment uses of this instruction.
1935 // The caller must ensure that this is safe to do.
1936 void RemoveEnvironmentUsers();
1937
David Brazdil1abb4192015-02-17 18:33:36 +00001938 protected:
1939 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1940 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1941
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001942 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001943 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1944
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001945 HInstruction* previous_;
1946 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001947 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001948 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001950 // An instruction gets an id when it is added to the graph.
1951 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001952 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001953 int id_;
1954
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001955 // When doing liveness analysis, instructions that have uses get an SSA index.
1956 int ssa_index_;
1957
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001958 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001959 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960
1961 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001962 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001963
Nicolas Geoffray39468442014-09-02 15:17:15 +01001964 // The environment associated with this instruction. Not null if the instruction
1965 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001968 // Set by the code generator.
1969 LocationSummary* locations_;
1970
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001971 // Set by the liveness analysis.
1972 LiveInterval* live_interval_;
1973
1974 // Set by the liveness analysis, this is the position in a linear
1975 // order of blocks where this instruction's live interval start.
1976 size_t lifetime_position_;
1977
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001978 const SideEffects side_effects_;
1979
Calin Juravleacf735c2015-02-12 15:25:22 +00001980 // TODO: for primitive types this should be marked as invalid.
1981 ReferenceTypeInfo reference_type_info_;
1982
David Brazdil1abb4192015-02-17 18:33:36 +00001983 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001984 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001985 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001987 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988
1989 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1990};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001991std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001992
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993class HInputIterator : public ValueObject {
1994 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001995 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996
1997 bool Done() const { return index_ == instruction_->InputCount(); }
1998 HInstruction* Current() const { return instruction_->InputAt(index_); }
1999 void Advance() { index_++; }
2000
2001 private:
2002 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004
2005 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2006};
2007
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002008class HInstructionIterator : public ValueObject {
2009 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002010 explicit HInstructionIterator(const HInstructionList& instructions)
2011 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002012 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 }
2014
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002015 bool Done() const { return instruction_ == nullptr; }
2016 HInstruction* Current() const { return instruction_; }
2017 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002019 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020 }
2021
2022 private:
2023 HInstruction* instruction_;
2024 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002025
2026 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002027};
2028
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002029class HBackwardInstructionIterator : public ValueObject {
2030 public:
2031 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2032 : instruction_(instructions.last_instruction_) {
2033 next_ = Done() ? nullptr : instruction_->GetPrevious();
2034 }
2035
2036 bool Done() const { return instruction_ == nullptr; }
2037 HInstruction* Current() const { return instruction_; }
2038 void Advance() {
2039 instruction_ = next_;
2040 next_ = Done() ? nullptr : instruction_->GetPrevious();
2041 }
2042
2043 private:
2044 HInstruction* instruction_;
2045 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002046
2047 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002048};
2049
Vladimir Markof9f64412015-09-02 14:05:49 +01002050template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002051class HTemplateInstruction: public HInstruction {
2052 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002053 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2054 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002055 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002056
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002057 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002060 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2061 DCHECK_LT(i, N);
2062 return inputs_[i];
2063 }
David Brazdil1abb4192015-02-17 18:33:36 +00002064
2065 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002066 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002067 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068 }
2069
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002071 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072
2073 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074};
2075
Vladimir Markof9f64412015-09-02 14:05:49 +01002076// HTemplateInstruction specialization for N=0.
2077template<>
2078class HTemplateInstruction<0>: public HInstruction {
2079 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002080 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2081 : HInstruction(side_effects, dex_pc) {}
2082
Vladimir Markof9f64412015-09-02 14:05:49 +01002083 virtual ~HTemplateInstruction() {}
2084
2085 size_t InputCount() const OVERRIDE { return 0; }
2086
2087 protected:
2088 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2089 LOG(FATAL) << "Unreachable";
2090 UNREACHABLE();
2091 }
2092
2093 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2094 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2095 LOG(FATAL) << "Unreachable";
2096 UNREACHABLE();
2097 }
2098
2099 private:
2100 friend class SsaBuilder;
2101};
2102
Dave Allison20dfc792014-06-16 20:44:29 -07002103template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002104class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002106 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2107 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002108 virtual ~HExpression() {}
2109
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002110 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002111
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002112 protected:
2113 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002114};
2115
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002116// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2117// instruction that branches to the exit block.
2118class HReturnVoid : public HTemplateInstruction<0> {
2119 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002120 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2121 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002122
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002123 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002125 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126
2127 private:
2128 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2129};
2130
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002131// Represents dex's RETURN opcodes. A HReturn is a control flow
2132// instruction that branches to the exit block.
2133class HReturn : public HTemplateInstruction<1> {
2134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002135 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2136 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002137 SetRawInputAt(0, value);
2138 }
2139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002140 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002142 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002143
2144 private:
2145 DISALLOW_COPY_AND_ASSIGN(HReturn);
2146};
2147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002148// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002149// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150// exit block.
2151class HExit : public HTemplateInstruction<0> {
2152 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002153 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002154
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002155 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002157 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002158
2159 private:
2160 DISALLOW_COPY_AND_ASSIGN(HExit);
2161};
2162
2163// Jumps from one block to another.
2164class HGoto : public HTemplateInstruction<0> {
2165 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002166 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002168 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002170 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002171 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002172 }
2173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002174 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175
2176 private:
2177 DISALLOW_COPY_AND_ASSIGN(HGoto);
2178};
2179
Roland Levillain9867bc72015-08-05 10:21:34 +01002180class HConstant : public HExpression<0> {
2181 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002182 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2183 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002184
2185 bool CanBeMoved() const OVERRIDE { return true; }
2186
2187 virtual bool IsMinusOne() const { return false; }
2188 virtual bool IsZero() const { return false; }
2189 virtual bool IsOne() const { return false; }
2190
2191 DECLARE_INSTRUCTION(Constant);
2192
2193 private:
2194 DISALLOW_COPY_AND_ASSIGN(HConstant);
2195};
2196
2197class HNullConstant : public HConstant {
2198 public:
2199 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2200 return true;
2201 }
2202
2203 size_t ComputeHashCode() const OVERRIDE { return 0; }
2204
2205 DECLARE_INSTRUCTION(NullConstant);
2206
2207 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002209
2210 friend class HGraph;
2211 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2212};
2213
2214// Constants of the type int. Those can be from Dex instructions, or
2215// synthesized (for example with the if-eqz instruction).
2216class HIntConstant : public HConstant {
2217 public:
2218 int32_t GetValue() const { return value_; }
2219
2220 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2221 DCHECK(other->IsIntConstant());
2222 return other->AsIntConstant()->value_ == value_;
2223 }
2224
2225 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2226
2227 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2228 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2229 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2230
2231 DECLARE_INSTRUCTION(IntConstant);
2232
2233 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002234 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2235 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2236 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2237 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002238
2239 const int32_t value_;
2240
2241 friend class HGraph;
2242 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2243 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2244 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2245};
2246
2247class HLongConstant : public HConstant {
2248 public:
2249 int64_t GetValue() const { return value_; }
2250
2251 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2252 DCHECK(other->IsLongConstant());
2253 return other->AsLongConstant()->value_ == value_;
2254 }
2255
2256 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2257
2258 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2259 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2260 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2261
2262 DECLARE_INSTRUCTION(LongConstant);
2263
2264 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002265 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2266 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002267
2268 const int64_t value_;
2269
2270 friend class HGraph;
2271 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2272};
Dave Allison20dfc792014-06-16 20:44:29 -07002273
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002274// Conditional branch. A block ending with an HIf instruction must have
2275// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002276class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2279 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280 SetRawInputAt(0, input);
2281 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002282
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002283 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002284
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002285 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +00002286 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002287 }
2288
2289 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko145acc52015-09-03 13:33:25 +00002290 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002291 }
2292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002293 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002294
2295 private:
2296 DISALLOW_COPY_AND_ASSIGN(HIf);
2297};
2298
David Brazdilfc6a86a2015-06-26 10:33:45 +00002299
2300// Abstract instruction which marks the beginning and/or end of a try block and
2301// links it to the respective exception handlers. Behaves the same as a Goto in
2302// non-exceptional control flow.
2303// Normal-flow successor is stored at index zero, exception handlers under
2304// higher indices in no particular order.
2305class HTryBoundary : public HTemplateInstruction<0> {
2306 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002307 enum BoundaryKind {
2308 kEntry,
2309 kExit,
2310 };
2311
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2313 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002314
2315 bool IsControlFlow() const OVERRIDE { return true; }
2316
2317 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko145acc52015-09-03 13:33:25 +00002318 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002319
2320 // Returns whether `handler` is among its exception handlers (non-zero index
2321 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002322 bool HasExceptionHandler(const HBasicBlock& handler) const {
2323 DCHECK(handler.IsCatchBlock());
Vladimir Marko145acc52015-09-03 13:33:25 +00002324 return GetBlock()->GetSuccessors().Contains(
2325 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002326 }
2327
2328 // If not present already, adds `handler` to its block's list of exception
2329 // handlers.
2330 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002331 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002332 GetBlock()->AddSuccessor(handler);
2333 }
2334 }
2335
David Brazdil56e1acc2015-06-30 15:41:36 +01002336 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002337
David Brazdilffee3d32015-07-06 11:48:53 +01002338 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2339
David Brazdilfc6a86a2015-06-26 10:33:45 +00002340 DECLARE_INSTRUCTION(TryBoundary);
2341
2342 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002343 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002344
2345 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2346};
2347
David Brazdilffee3d32015-07-06 11:48:53 +01002348// Iterator over exception handlers of a given HTryBoundary, i.e. over
2349// exceptional successors of its basic block.
2350class HExceptionHandlerIterator : public ValueObject {
2351 public:
2352 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2353 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2354
Vladimir Marko145acc52015-09-03 13:33:25 +00002355 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2356 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002357 size_t CurrentSuccessorIndex() const { return index_; }
2358 void Advance() { ++index_; }
2359
2360 private:
2361 const HBasicBlock& block_;
2362 size_t index_;
2363
2364 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2365};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002366
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002367// Deoptimize to interpreter, upon checking a condition.
2368class HDeoptimize : public HTemplateInstruction<1> {
2369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002370 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2371 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002372 SetRawInputAt(0, cond);
2373 }
2374
2375 bool NeedsEnvironment() const OVERRIDE { return true; }
2376 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002377
2378 DECLARE_INSTRUCTION(Deoptimize);
2379
2380 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002381 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2382};
2383
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002384// Represents the ArtMethod that was passed as a first argument to
2385// the method. It is used by instructions that depend on it, like
2386// instructions that work with the dex cache.
2387class HCurrentMethod : public HExpression<0> {
2388 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002389 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2390 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002391
2392 DECLARE_INSTRUCTION(CurrentMethod);
2393
2394 private:
2395 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2396};
2397
Roland Levillain88cb1752014-10-20 16:36:47 +01002398class HUnaryOperation : public HExpression<1> {
2399 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002400 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2401 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002402 SetRawInputAt(0, input);
2403 }
2404
2405 HInstruction* GetInput() const { return InputAt(0); }
2406 Primitive::Type GetResultType() const { return GetType(); }
2407
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002408 bool CanBeMoved() const OVERRIDE { return true; }
2409 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002410 UNUSED(other);
2411 return true;
2412 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002413
Roland Levillain9240d6a2014-10-20 16:47:04 +01002414 // Try to statically evaluate `operation` and return a HConstant
2415 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002416 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002417 HConstant* TryStaticEvaluation() const;
2418
2419 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002420 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2421 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002422
Roland Levillain88cb1752014-10-20 16:36:47 +01002423 DECLARE_INSTRUCTION(UnaryOperation);
2424
2425 private:
2426 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2427};
2428
Dave Allison20dfc792014-06-16 20:44:29 -07002429class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002430 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002431 HBinaryOperation(Primitive::Type result_type,
2432 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002433 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002434 SideEffects side_effects = SideEffects::None(),
2435 uint32_t dex_pc = kNoDexPc)
2436 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002437 SetRawInputAt(0, left);
2438 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002439 }
2440
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002441 HInstruction* GetLeft() const { return InputAt(0); }
2442 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002443 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002444
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002445 virtual bool IsCommutative() const { return false; }
2446
2447 // Put constant on the right.
2448 // Returns whether order is changed.
2449 bool OrderInputsWithConstantOnTheRight() {
2450 HInstruction* left = InputAt(0);
2451 HInstruction* right = InputAt(1);
2452 if (left->IsConstant() && !right->IsConstant()) {
2453 ReplaceInput(right, 0);
2454 ReplaceInput(left, 1);
2455 return true;
2456 }
2457 return false;
2458 }
2459
2460 // Order inputs by instruction id, but favor constant on the right side.
2461 // This helps GVN for commutative ops.
2462 void OrderInputs() {
2463 DCHECK(IsCommutative());
2464 HInstruction* left = InputAt(0);
2465 HInstruction* right = InputAt(1);
2466 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2467 return;
2468 }
2469 if (OrderInputsWithConstantOnTheRight()) {
2470 return;
2471 }
2472 // Order according to instruction id.
2473 if (left->GetId() > right->GetId()) {
2474 ReplaceInput(right, 0);
2475 ReplaceInput(left, 1);
2476 }
2477 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002478
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002479 bool CanBeMoved() const OVERRIDE { return true; }
2480 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002481 UNUSED(other);
2482 return true;
2483 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002484
Roland Levillain9240d6a2014-10-20 16:47:04 +01002485 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002486 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002487 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002488 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002489
2490 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002491 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2492 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2493 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2494 HLongConstant* y ATTRIBUTE_UNUSED) const {
2495 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2496 return nullptr;
2497 }
2498 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2499 HIntConstant* y ATTRIBUTE_UNUSED) const {
2500 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2501 return nullptr;
2502 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002503
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002504 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002505 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002506 HConstant* GetConstantRight() const;
2507
2508 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002509 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002510 HInstruction* GetLeastConstantLeft() const;
2511
Roland Levillainccc07a92014-09-16 14:48:16 +01002512 DECLARE_INSTRUCTION(BinaryOperation);
2513
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002514 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002515 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2516};
2517
Mark Mendellc4701932015-04-10 13:18:51 -04002518// The comparison bias applies for floating point operations and indicates how NaN
2519// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002520enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002521 kNoBias, // bias is not applicable (i.e. for long operation)
2522 kGtBias, // return 1 for NaN comparisons
2523 kLtBias, // return -1 for NaN comparisons
2524};
2525
Dave Allison20dfc792014-06-16 20:44:29 -07002526class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2529 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002530 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002531 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002532
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002533 bool NeedsMaterialization() const { return needs_materialization_; }
2534 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002535
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002536 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002537 // `instruction`, and disregard moves in between.
2538 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002539
Dave Allison20dfc792014-06-16 20:44:29 -07002540 DECLARE_INSTRUCTION(Condition);
2541
2542 virtual IfCondition GetCondition() const = 0;
2543
Mark Mendellc4701932015-04-10 13:18:51 -04002544 virtual IfCondition GetOppositeCondition() const = 0;
2545
Roland Levillain4fa13f62015-07-06 18:11:54 +01002546 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002547
2548 void SetBias(ComparisonBias bias) { bias_ = bias; }
2549
2550 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2551 return bias_ == other->AsCondition()->bias_;
2552 }
2553
Roland Levillain4fa13f62015-07-06 18:11:54 +01002554 bool IsFPConditionTrueIfNaN() const {
2555 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2556 IfCondition if_cond = GetCondition();
2557 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2558 }
2559
2560 bool IsFPConditionFalseIfNaN() const {
2561 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2562 IfCondition if_cond = GetCondition();
2563 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2564 }
2565
Dave Allison20dfc792014-06-16 20:44:29 -07002566 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002567 // For register allocation purposes, returns whether this instruction needs to be
2568 // materialized (that is, not just be in the processor flags).
2569 bool needs_materialization_;
2570
Mark Mendellc4701932015-04-10 13:18:51 -04002571 // Needed if we merge a HCompare into a HCondition.
2572 ComparisonBias bias_;
2573
Dave Allison20dfc792014-06-16 20:44:29 -07002574 DISALLOW_COPY_AND_ASSIGN(HCondition);
2575};
2576
2577// Instruction to check if two inputs are equal to each other.
2578class HEqual : public HCondition {
2579 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002580 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2581 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002582
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002583 bool IsCommutative() const OVERRIDE { return true; }
2584
Roland Levillain9867bc72015-08-05 10:21:34 +01002585 template <typename T> bool Compute(T x, T y) const { return x == y; }
2586
2587 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002588 return GetBlock()->GetGraph()->GetIntConstant(
2589 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002590 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002591 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002592 return GetBlock()->GetGraph()->GetIntConstant(
2593 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002594 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002595
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002596 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002598 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002599 return kCondEQ;
2600 }
2601
Mark Mendellc4701932015-04-10 13:18:51 -04002602 IfCondition GetOppositeCondition() const OVERRIDE {
2603 return kCondNE;
2604 }
2605
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002606 private:
2607 DISALLOW_COPY_AND_ASSIGN(HEqual);
2608};
2609
Dave Allison20dfc792014-06-16 20:44:29 -07002610class HNotEqual : public HCondition {
2611 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002612 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2613 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002614
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002615 bool IsCommutative() const OVERRIDE { return true; }
2616
Roland Levillain9867bc72015-08-05 10:21:34 +01002617 template <typename T> bool Compute(T x, T y) const { return x != y; }
2618
2619 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002620 return GetBlock()->GetGraph()->GetIntConstant(
2621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002622 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002623 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002624 return GetBlock()->GetGraph()->GetIntConstant(
2625 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002626 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002627
Dave Allison20dfc792014-06-16 20:44:29 -07002628 DECLARE_INSTRUCTION(NotEqual);
2629
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002630 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002631 return kCondNE;
2632 }
2633
Mark Mendellc4701932015-04-10 13:18:51 -04002634 IfCondition GetOppositeCondition() const OVERRIDE {
2635 return kCondEQ;
2636 }
2637
Dave Allison20dfc792014-06-16 20:44:29 -07002638 private:
2639 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2640};
2641
2642class HLessThan : public HCondition {
2643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002644 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2645 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002646
Roland Levillain9867bc72015-08-05 10:21:34 +01002647 template <typename T> bool Compute(T x, T y) const { return x < y; }
2648
2649 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002650 return GetBlock()->GetGraph()->GetIntConstant(
2651 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002652 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002653 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 return GetBlock()->GetGraph()->GetIntConstant(
2655 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002656 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002657
Dave Allison20dfc792014-06-16 20:44:29 -07002658 DECLARE_INSTRUCTION(LessThan);
2659
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002660 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002661 return kCondLT;
2662 }
2663
Mark Mendellc4701932015-04-10 13:18:51 -04002664 IfCondition GetOppositeCondition() const OVERRIDE {
2665 return kCondGE;
2666 }
2667
Dave Allison20dfc792014-06-16 20:44:29 -07002668 private:
2669 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2670};
2671
2672class HLessThanOrEqual : public HCondition {
2673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002674 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2675 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002676
Roland Levillain9867bc72015-08-05 10:21:34 +01002677 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2678
2679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002680 return GetBlock()->GetGraph()->GetIntConstant(
2681 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002682 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 return GetBlock()->GetGraph()->GetIntConstant(
2685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002686 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002687
Dave Allison20dfc792014-06-16 20:44:29 -07002688 DECLARE_INSTRUCTION(LessThanOrEqual);
2689
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002690 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002691 return kCondLE;
2692 }
2693
Mark Mendellc4701932015-04-10 13:18:51 -04002694 IfCondition GetOppositeCondition() const OVERRIDE {
2695 return kCondGT;
2696 }
2697
Dave Allison20dfc792014-06-16 20:44:29 -07002698 private:
2699 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2700};
2701
2702class HGreaterThan : public HCondition {
2703 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002704 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2705 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002706
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 template <typename T> bool Compute(T x, T y) const { return x > y; }
2708
2709 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 return GetBlock()->GetGraph()->GetIntConstant(
2711 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002712 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002714 return GetBlock()->GetGraph()->GetIntConstant(
2715 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002716 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002717
Dave Allison20dfc792014-06-16 20:44:29 -07002718 DECLARE_INSTRUCTION(GreaterThan);
2719
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002720 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002721 return kCondGT;
2722 }
2723
Mark Mendellc4701932015-04-10 13:18:51 -04002724 IfCondition GetOppositeCondition() const OVERRIDE {
2725 return kCondLE;
2726 }
2727
Dave Allison20dfc792014-06-16 20:44:29 -07002728 private:
2729 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2730};
2731
2732class HGreaterThanOrEqual : public HCondition {
2733 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002734 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2735 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002736
Roland Levillain9867bc72015-08-05 10:21:34 +01002737 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2738
2739 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 return GetBlock()->GetGraph()->GetIntConstant(
2741 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002742 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002743 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 return GetBlock()->GetGraph()->GetIntConstant(
2745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002746 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002747
Dave Allison20dfc792014-06-16 20:44:29 -07002748 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2749
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002750 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002751 return kCondGE;
2752 }
2753
Mark Mendellc4701932015-04-10 13:18:51 -04002754 IfCondition GetOppositeCondition() const OVERRIDE {
2755 return kCondLT;
2756 }
2757
Dave Allison20dfc792014-06-16 20:44:29 -07002758 private:
2759 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2760};
2761
2762
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002763// Instruction to check how two inputs compare to each other.
2764// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2765class HCompare : public HBinaryOperation {
2766 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002767 HCompare(Primitive::Type type,
2768 HInstruction* first,
2769 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002770 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002771 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002772 : HBinaryOperation(Primitive::kPrimInt,
2773 first,
2774 second,
2775 SideEffectsForArchRuntimeCalls(type),
2776 dex_pc),
2777 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002778 DCHECK_EQ(type, first->GetType());
2779 DCHECK_EQ(type, second->GetType());
2780 }
2781
Roland Levillain9867bc72015-08-05 10:21:34 +01002782 template <typename T>
2783 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002784
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002788 }
2789 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 return GetBlock()->GetGraph()->GetIntConstant(
2791 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002792 }
2793
Calin Juravleddb7df22014-11-25 20:56:51 +00002794 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2795 return bias_ == other->AsCompare()->bias_;
2796 }
2797
Mark Mendellc4701932015-04-10 13:18:51 -04002798 ComparisonBias GetBias() const { return bias_; }
2799
Roland Levillain4fa13f62015-07-06 18:11:54 +01002800 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002801
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002802
2803 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2804 // MIPS64 uses a runtime call for FP comparisons.
2805 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2806 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002807
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002808 DECLARE_INSTRUCTION(Compare);
2809
2810 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002811 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002812
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002813 DISALLOW_COPY_AND_ASSIGN(HCompare);
2814};
2815
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002816// A local in the graph. Corresponds to a Dex register.
2817class HLocal : public HTemplateInstruction<0> {
2818 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002819 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002822 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002823
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002824 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002825
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002826 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002827 // The Dex register number.
2828 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002829
2830 DISALLOW_COPY_AND_ASSIGN(HLocal);
2831};
2832
2833// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002834class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002835 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002836 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2837 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002838 SetRawInputAt(0, local);
2839 }
2840
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002841 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2842
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002843 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002844
2845 private:
2846 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2847};
2848
2849// Store a value in a given local. This instruction has two inputs: the value
2850// and the local.
2851class HStoreLocal : public HTemplateInstruction<2> {
2852 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2854 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002855 SetRawInputAt(0, local);
2856 SetRawInputAt(1, value);
2857 }
2858
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002859 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2860
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002861 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002862
2863 private:
2864 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2865};
2866
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002867class HFloatConstant : public HConstant {
2868 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002869 float GetValue() const { return value_; }
2870
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002871 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002872 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002873 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2874 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002875 }
2876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002877 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002879 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002880 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002881 }
2882 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002883 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002884 }
2885 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002886 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2887 }
2888 bool IsNaN() const {
2889 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002890 }
2891
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002892 DECLARE_INSTRUCTION(FloatConstant);
2893
2894 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002895 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2896 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2897 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2898 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002899
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002900 const float value_;
2901
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002902 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002903 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002904 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002905 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2906};
2907
2908class HDoubleConstant : public HConstant {
2909 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910 double GetValue() const { return value_; }
2911
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002912 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002914 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2915 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002916 }
2917
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002918 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002919
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002920 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002921 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002922 }
2923 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002924 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002925 }
2926 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002927 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2928 }
2929 bool IsNaN() const {
2930 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002931 }
2932
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002933 DECLARE_INSTRUCTION(DoubleConstant);
2934
2935 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002936 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2937 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2938 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2939 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002940
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002941 const double value_;
2942
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002943 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002944 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002945 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002946 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2947};
2948
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002949enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002950#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002951#include "intrinsics_list.h"
2952 kNone,
2953 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2954#undef INTRINSICS_LIST
2955#undef OPTIMIZING_INTRINSICS
2956};
2957std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2958
Agi Csaki05f20562015-08-19 14:58:14 -07002959enum IntrinsicNeedsEnvironmentOrCache {
2960 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2961 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002962};
2963
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002964class HInvoke : public HInstruction {
2965 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002966 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002967
2968 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2969 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002970 bool NeedsEnvironment() const OVERRIDE {
2971 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2972 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002973
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002974 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002975 SetRawInputAt(index, argument);
2976 }
2977
Roland Levillain3e3d7332015-04-28 11:00:54 +01002978 // Return the number of arguments. This number can be lower than
2979 // the number of inputs returned by InputCount(), as some invoke
2980 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2981 // inputs at the end of their list of inputs.
2982 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2983
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002984 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002985
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002986
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002987 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002988 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002989
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002990 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2991
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002992 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002993 return intrinsic_;
2994 }
2995
Agi Csaki05f20562015-08-19 14:58:14 -07002996 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002997 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07002998 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002999 }
3000
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003001 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003002 return GetEnvironment()->GetParent() != nullptr;
3003 }
3004
3005 bool CanThrow() const OVERRIDE { return true; }
3006
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003007 DECLARE_INSTRUCTION(Invoke);
3008
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003009 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003010 HInvoke(ArenaAllocator* arena,
3011 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003012 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003013 Primitive::Type return_type,
3014 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003015 uint32_t dex_method_index,
3016 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003017 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003018 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003019 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003020 inputs_(arena, number_of_arguments),
3021 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003022 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003023 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003024 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003025 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003026 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
3027 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003028 }
3029
David Brazdil1abb4192015-02-17 18:33:36 +00003030 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3031 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3032 inputs_.Put(index, input);
3033 }
3034
Roland Levillain3e3d7332015-04-28 11:00:54 +01003035 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003036 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003037 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003038 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003039 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003040 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003041 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003042
3043 private:
3044 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3045};
3046
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003047class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003048 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003049 // Requirements of this method call regarding the class
3050 // initialization (clinit) check of its declaring class.
3051 enum class ClinitCheckRequirement {
3052 kNone, // Class already initialized.
3053 kExplicit, // Static call having explicit clinit check as last input.
3054 kImplicit, // Static call implicitly requiring a clinit check.
3055 };
3056
Vladimir Marko58155012015-08-19 12:49:41 +00003057 // Determines how to load the target ArtMethod*.
3058 enum class MethodLoadKind {
3059 // Use a String init ArtMethod* loaded from Thread entrypoints.
3060 kStringInit,
3061
3062 // Use the method's own ArtMethod* loaded by the register allocator.
3063 kRecursive,
3064
3065 // Use ArtMethod* at a known address, embed the direct address in the code.
3066 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3067 kDirectAddress,
3068
3069 // Use ArtMethod* at an address that will be known at link time, embed the direct
3070 // address in the code. If the image is relocatable, emit .patch_oat entry.
3071 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3072 // the image relocatable or not.
3073 kDirectAddressWithFixup,
3074
3075 // Load from resoved methods array in the dex cache using a PC-relative load.
3076 // Used when we need to use the dex cache, for example for invoke-static that
3077 // may cause class initialization (the entry may point to a resolution method),
3078 // and we know that we can access the dex cache arrays using a PC-relative load.
3079 kDexCachePcRelative,
3080
3081 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3082 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3083 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3084 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3085 kDexCacheViaMethod,
3086 };
3087
3088 // Determines the location of the code pointer.
3089 enum class CodePtrLocation {
3090 // Recursive call, use local PC-relative call instruction.
3091 kCallSelf,
3092
3093 // Use PC-relative call instruction patched at link time.
3094 // Used for calls within an oat file, boot->boot or app->app.
3095 kCallPCRelative,
3096
3097 // Call to a known target address, embed the direct address in code.
3098 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3099 kCallDirect,
3100
3101 // Call to a target address that will be known at link time, embed the direct
3102 // address in code. If the image is relocatable, emit .patch_oat entry.
3103 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3104 // the image relocatable or not.
3105 kCallDirectWithFixup,
3106
3107 // Use code pointer from the ArtMethod*.
3108 // Used when we don't know the target code. This is also the last-resort-kind used when
3109 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3110 kCallArtMethod,
3111 };
3112
3113 struct DispatchInfo {
3114 const MethodLoadKind method_load_kind;
3115 const CodePtrLocation code_ptr_location;
3116 // The method load data holds
3117 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3118 // Note that there are multiple string init methods, each having its own offset.
3119 // - the method address for kDirectAddress
3120 // - the dex cache arrays offset for kDexCachePcRel.
3121 const uint64_t method_load_data;
3122 const uint64_t direct_code_ptr;
3123 };
3124
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003125 HInvokeStaticOrDirect(ArenaAllocator* arena,
3126 uint32_t number_of_arguments,
3127 Primitive::Type return_type,
3128 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003129 uint32_t method_index,
3130 MethodReference target_method,
3131 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003132 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003133 InvokeType invoke_type,
3134 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003135 : HInvoke(arena,
3136 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003137 // There is one extra argument for the HCurrentMethod node, and
3138 // potentially one other if the clinit check is explicit, and one other
3139 // if the method is a string factory.
3140 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003141 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003142 return_type,
3143 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003144 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003145 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003146 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003147 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003148 target_method_(target_method),
3149 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003150
Calin Juravle641547a2015-04-21 22:08:51 +01003151 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3152 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003153 // We access the method via the dex cache so we can't do an implicit null check.
3154 // TODO: for intrinsics we can generate implicit null checks.
3155 return false;
3156 }
3157
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003158 bool CanBeNull() const OVERRIDE {
3159 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3160 }
3161
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003162 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003163 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3164 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3165 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003166 bool NeedsDexCache() const OVERRIDE {
3167 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3168 return !IsRecursive() && !IsStringInit();
3169 }
Vladimir Marko58155012015-08-19 12:49:41 +00003170 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003171 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003172 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3173 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3174 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3175 MethodReference GetTargetMethod() const { return target_method_; }
3176
3177 int32_t GetStringInitOffset() const {
3178 DCHECK(IsStringInit());
3179 return dispatch_info_.method_load_data;
3180 }
3181
3182 uint64_t GetMethodAddress() const {
3183 DCHECK(HasMethodAddress());
3184 return dispatch_info_.method_load_data;
3185 }
3186
3187 uint32_t GetDexCacheArrayOffset() const {
3188 DCHECK(HasPcRelDexCache());
3189 return dispatch_info_.method_load_data;
3190 }
3191
3192 uint64_t GetDirectCodePtr() const {
3193 DCHECK(HasDirectCodePtr());
3194 return dispatch_info_.direct_code_ptr;
3195 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003196
Calin Juravle68ad6492015-08-18 17:08:12 +01003197 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3198
Roland Levillain4c0eb422015-04-24 16:43:49 +01003199 // Is this instruction a call to a static method?
3200 bool IsStatic() const {
3201 return GetInvokeType() == kStatic;
3202 }
3203
Roland Levillain3e3d7332015-04-28 11:00:54 +01003204 // Remove the art::HLoadClass instruction set as last input by
3205 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3206 // the initial art::HClinitCheck instruction (only relevant for
3207 // static calls with explicit clinit check).
3208 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003209 DCHECK(IsStaticWithExplicitClinitCheck());
3210 size_t last_input_index = InputCount() - 1;
3211 HInstruction* last_input = InputAt(last_input_index);
3212 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003213 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003214 RemoveAsUserOfInput(last_input_index);
3215 inputs_.DeleteAt(last_input_index);
3216 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3217 DCHECK(IsStaticWithImplicitClinitCheck());
3218 }
3219
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003220 bool IsStringFactoryFor(HFakeString* str) const {
3221 if (!IsStringInit()) return false;
3222 // +1 for the current method.
3223 if (InputCount() == (number_of_arguments_ + 1)) return false;
3224 return InputAt(InputCount() - 1)->AsFakeString() == str;
3225 }
3226
3227 void RemoveFakeStringArgumentAsLastInput() {
3228 DCHECK(IsStringInit());
3229 size_t last_input_index = InputCount() - 1;
3230 HInstruction* last_input = InputAt(last_input_index);
3231 DCHECK(last_input != nullptr);
3232 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3233 RemoveAsUserOfInput(last_input_index);
3234 inputs_.DeleteAt(last_input_index);
3235 }
3236
Roland Levillain4c0eb422015-04-24 16:43:49 +01003237 // Is this a call to a static method whose declaring class has an
3238 // explicit intialization check in the graph?
3239 bool IsStaticWithExplicitClinitCheck() const {
3240 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3241 }
3242
3243 // Is this a call to a static method whose declaring class has an
3244 // implicit intialization check requirement?
3245 bool IsStaticWithImplicitClinitCheck() const {
3246 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3247 }
3248
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003249 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003250
Roland Levillain4c0eb422015-04-24 16:43:49 +01003251 protected:
3252 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3253 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3254 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3255 HInstruction* input = input_record.GetInstruction();
3256 // `input` is the last input of a static invoke marked as having
3257 // an explicit clinit check. It must either be:
3258 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3259 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3260 DCHECK(input != nullptr);
3261 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3262 }
3263 return input_record;
3264 }
3265
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003266 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003267 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003268 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003269 // The target method may refer to different dex file or method index than the original
3270 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3271 // in derived class to increase visibility.
3272 MethodReference target_method_;
3273 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003274
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003275 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003276};
3277
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003278class HInvokeVirtual : public HInvoke {
3279 public:
3280 HInvokeVirtual(ArenaAllocator* arena,
3281 uint32_t number_of_arguments,
3282 Primitive::Type return_type,
3283 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003284 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003285 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003286 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003287 vtable_index_(vtable_index) {}
3288
Calin Juravle641547a2015-04-21 22:08:51 +01003289 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003290 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003291 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003292 }
3293
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003294 uint32_t GetVTableIndex() const { return vtable_index_; }
3295
3296 DECLARE_INSTRUCTION(InvokeVirtual);
3297
3298 private:
3299 const uint32_t vtable_index_;
3300
3301 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3302};
3303
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003304class HInvokeInterface : public HInvoke {
3305 public:
3306 HInvokeInterface(ArenaAllocator* arena,
3307 uint32_t number_of_arguments,
3308 Primitive::Type return_type,
3309 uint32_t dex_pc,
3310 uint32_t dex_method_index,
3311 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003312 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003313 imt_index_(imt_index) {}
3314
Calin Juravle641547a2015-04-21 22:08:51 +01003315 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003316 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003317 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003318 }
3319
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003320 uint32_t GetImtIndex() const { return imt_index_; }
3321 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3322
3323 DECLARE_INSTRUCTION(InvokeInterface);
3324
3325 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003326 const uint32_t imt_index_;
3327
3328 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3329};
3330
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003331class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003332 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003333 HNewInstance(HCurrentMethod* current_method,
3334 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003335 uint16_t type_index,
3336 const DexFile& dex_file,
3337 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003338 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003339 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003340 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003341 entrypoint_(entrypoint) {
3342 SetRawInputAt(0, current_method);
3343 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003344
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003345 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003346 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003347
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003348 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003349 bool NeedsEnvironment() const OVERRIDE { return true; }
3350 // It may throw when called on:
3351 // - interfaces
3352 // - abstract/innaccessible/unknown classes
3353 // TODO: optimize when possible.
3354 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003355
Calin Juravle10e244f2015-01-26 18:54:32 +00003356 bool CanBeNull() const OVERRIDE { return false; }
3357
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003358 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3359
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003360 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003361
3362 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003363 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003364 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003365 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003366
3367 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3368};
3369
Roland Levillain88cb1752014-10-20 16:36:47 +01003370class HNeg : public HUnaryOperation {
3371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003372 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3373 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003374
Roland Levillain9867bc72015-08-05 10:21:34 +01003375 template <typename T> T Compute(T x) const { return -x; }
3376
3377 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003378 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003379 }
3380 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003381 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003382 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003383
Roland Levillain88cb1752014-10-20 16:36:47 +01003384 DECLARE_INSTRUCTION(Neg);
3385
3386 private:
3387 DISALLOW_COPY_AND_ASSIGN(HNeg);
3388};
3389
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003390class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003391 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003392 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003393 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003394 uint32_t dex_pc,
3395 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003396 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003397 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003398 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003399 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003400 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003401 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003402 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003403 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003404 }
3405
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003406 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003407 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003408
3409 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003410 bool NeedsEnvironment() const OVERRIDE { return true; }
3411
Mingyao Yang0c365e62015-03-31 15:09:29 -07003412 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3413 bool CanThrow() const OVERRIDE { return true; }
3414
Calin Juravle10e244f2015-01-26 18:54:32 +00003415 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003416
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003417 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3418
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003419 DECLARE_INSTRUCTION(NewArray);
3420
3421 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003422 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003423 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003424 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003425
3426 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3427};
3428
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003429class HAdd : public HBinaryOperation {
3430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003431 HAdd(Primitive::Type result_type,
3432 HInstruction* left,
3433 HInstruction* right,
3434 uint32_t dex_pc = kNoDexPc)
3435 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003436
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003437 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003438
Roland Levillain9867bc72015-08-05 10:21:34 +01003439 template <typename T> T Compute(T x, T y) const { return x + y; }
3440
3441 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003442 return GetBlock()->GetGraph()->GetIntConstant(
3443 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003444 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003445 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003446 return GetBlock()->GetGraph()->GetLongConstant(
3447 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003448 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003449
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003450 DECLARE_INSTRUCTION(Add);
3451
3452 private:
3453 DISALLOW_COPY_AND_ASSIGN(HAdd);
3454};
3455
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003456class HSub : public HBinaryOperation {
3457 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003458 HSub(Primitive::Type result_type,
3459 HInstruction* left,
3460 HInstruction* right,
3461 uint32_t dex_pc = kNoDexPc)
3462 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003463
Roland Levillain9867bc72015-08-05 10:21:34 +01003464 template <typename T> T Compute(T x, T y) const { return x - y; }
3465
3466 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003467 return GetBlock()->GetGraph()->GetIntConstant(
3468 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003469 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003471 return GetBlock()->GetGraph()->GetLongConstant(
3472 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003473 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003475 DECLARE_INSTRUCTION(Sub);
3476
3477 private:
3478 DISALLOW_COPY_AND_ASSIGN(HSub);
3479};
3480
Calin Juravle34bacdf2014-10-07 20:23:36 +01003481class HMul : public HBinaryOperation {
3482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003483 HMul(Primitive::Type result_type,
3484 HInstruction* left,
3485 HInstruction* right,
3486 uint32_t dex_pc = kNoDexPc)
3487 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003489 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003490
Roland Levillain9867bc72015-08-05 10:21:34 +01003491 template <typename T> T Compute(T x, T y) const { return x * y; }
3492
3493 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003494 return GetBlock()->GetGraph()->GetIntConstant(
3495 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003496 }
3497 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003498 return GetBlock()->GetGraph()->GetLongConstant(
3499 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003500 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003501
3502 DECLARE_INSTRUCTION(Mul);
3503
3504 private:
3505 DISALLOW_COPY_AND_ASSIGN(HMul);
3506};
3507
Calin Juravle7c4954d2014-10-28 16:57:40 +00003508class HDiv : public HBinaryOperation {
3509 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003510 HDiv(Primitive::Type result_type,
3511 HInstruction* left,
3512 HInstruction* right,
3513 uint32_t dex_pc)
3514 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003515
Roland Levillain9867bc72015-08-05 10:21:34 +01003516 template <typename T>
3517 T Compute(T x, T y) const {
3518 // Our graph structure ensures we never have 0 for `y` during
3519 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003520 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003521 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003522 return (y == -1) ? -x : x / y;
3523 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003524
Roland Levillain9867bc72015-08-05 10:21:34 +01003525 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003526 return GetBlock()->GetGraph()->GetIntConstant(
3527 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 }
3529 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003530 return GetBlock()->GetGraph()->GetLongConstant(
3531 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003532 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003533
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003534 static SideEffects SideEffectsForArchRuntimeCalls() {
3535 // The generated code can use a runtime call.
3536 return SideEffects::CanTriggerGC();
3537 }
3538
Calin Juravle7c4954d2014-10-28 16:57:40 +00003539 DECLARE_INSTRUCTION(Div);
3540
3541 private:
3542 DISALLOW_COPY_AND_ASSIGN(HDiv);
3543};
3544
Calin Juravlebacfec32014-11-14 15:54:36 +00003545class HRem : public HBinaryOperation {
3546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003547 HRem(Primitive::Type result_type,
3548 HInstruction* left,
3549 HInstruction* right,
3550 uint32_t dex_pc)
3551 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003552
Roland Levillain9867bc72015-08-05 10:21:34 +01003553 template <typename T>
3554 T Compute(T x, T y) const {
3555 // Our graph structure ensures we never have 0 for `y` during
3556 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003557 DCHECK_NE(y, 0);
3558 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3559 return (y == -1) ? 0 : x % y;
3560 }
3561
Roland Levillain9867bc72015-08-05 10:21:34 +01003562 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003563 return GetBlock()->GetGraph()->GetIntConstant(
3564 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 }
3566 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003567 return GetBlock()->GetGraph()->GetLongConstant(
3568 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003569 }
3570
Calin Juravlebacfec32014-11-14 15:54:36 +00003571
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003572 static SideEffects SideEffectsForArchRuntimeCalls() {
3573 return SideEffects::CanTriggerGC();
3574 }
3575
Calin Juravlebacfec32014-11-14 15:54:36 +00003576 DECLARE_INSTRUCTION(Rem);
3577
3578 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003579 DISALLOW_COPY_AND_ASSIGN(HRem);
3580};
3581
Calin Juravled0d48522014-11-04 16:40:20 +00003582class HDivZeroCheck : public HExpression<1> {
3583 public:
3584 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003585 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003586 SetRawInputAt(0, value);
3587 }
3588
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003589 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3590
Calin Juravled0d48522014-11-04 16:40:20 +00003591 bool CanBeMoved() const OVERRIDE { return true; }
3592
3593 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3594 UNUSED(other);
3595 return true;
3596 }
3597
3598 bool NeedsEnvironment() const OVERRIDE { return true; }
3599 bool CanThrow() const OVERRIDE { return true; }
3600
Calin Juravled0d48522014-11-04 16:40:20 +00003601 DECLARE_INSTRUCTION(DivZeroCheck);
3602
3603 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003604 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3605};
3606
Calin Juravle9aec02f2014-11-18 23:06:35 +00003607class HShl : public HBinaryOperation {
3608 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003609 HShl(Primitive::Type result_type,
3610 HInstruction* left,
3611 HInstruction* right,
3612 uint32_t dex_pc = kNoDexPc)
3613 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003614
Roland Levillain9867bc72015-08-05 10:21:34 +01003615 template <typename T, typename U, typename V>
3616 T Compute(T x, U y, V max_shift_value) const {
3617 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3618 "V is not the unsigned integer type corresponding to T");
3619 return x << (y & max_shift_value);
3620 }
3621
3622 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3623 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003624 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003625 }
3626 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3627 // case is handled as `x << static_cast<int>(y)`.
3628 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3629 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003630 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003631 }
3632 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3633 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003634 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003635 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003636
3637 DECLARE_INSTRUCTION(Shl);
3638
3639 private:
3640 DISALLOW_COPY_AND_ASSIGN(HShl);
3641};
3642
3643class HShr : public HBinaryOperation {
3644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003645 HShr(Primitive::Type result_type,
3646 HInstruction* left,
3647 HInstruction* right,
3648 uint32_t dex_pc = kNoDexPc)
3649 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003650
Roland Levillain9867bc72015-08-05 10:21:34 +01003651 template <typename T, typename U, typename V>
3652 T Compute(T x, U y, V max_shift_value) const {
3653 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3654 "V is not the unsigned integer type corresponding to T");
3655 return x >> (y & max_shift_value);
3656 }
3657
3658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3659 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003660 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003661 }
3662 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3663 // case is handled as `x >> static_cast<int>(y)`.
3664 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3665 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003666 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003667 }
3668 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3669 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003670 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003671 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003672
3673 DECLARE_INSTRUCTION(Shr);
3674
3675 private:
3676 DISALLOW_COPY_AND_ASSIGN(HShr);
3677};
3678
3679class HUShr : public HBinaryOperation {
3680 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003681 HUShr(Primitive::Type result_type,
3682 HInstruction* left,
3683 HInstruction* right,
3684 uint32_t dex_pc = kNoDexPc)
3685 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686
Roland Levillain9867bc72015-08-05 10:21:34 +01003687 template <typename T, typename U, typename V>
3688 T Compute(T x, U y, V max_shift_value) const {
3689 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3690 "V is not the unsigned integer type corresponding to T");
3691 V ux = static_cast<V>(x);
3692 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003693 }
3694
Roland Levillain9867bc72015-08-05 10:21:34 +01003695 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3696 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003697 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003698 }
3699 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3700 // case is handled as `x >>> static_cast<int>(y)`.
3701 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3702 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003703 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003704 }
3705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3706 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003707 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 }
3709
3710 DECLARE_INSTRUCTION(UShr);
3711
3712 private:
3713 DISALLOW_COPY_AND_ASSIGN(HUShr);
3714};
3715
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003716class HAnd : public HBinaryOperation {
3717 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003718 HAnd(Primitive::Type result_type,
3719 HInstruction* left,
3720 HInstruction* right,
3721 uint32_t dex_pc = kNoDexPc)
3722 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003723
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003724 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003725
Roland Levillain9867bc72015-08-05 10:21:34 +01003726 template <typename T, typename U>
3727 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3728
3729 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003730 return GetBlock()->GetGraph()->GetIntConstant(
3731 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003732 }
3733 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003734 return GetBlock()->GetGraph()->GetLongConstant(
3735 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003736 }
3737 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003738 return GetBlock()->GetGraph()->GetLongConstant(
3739 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003740 }
3741 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003742 return GetBlock()->GetGraph()->GetLongConstant(
3743 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003745
3746 DECLARE_INSTRUCTION(And);
3747
3748 private:
3749 DISALLOW_COPY_AND_ASSIGN(HAnd);
3750};
3751
3752class HOr : public HBinaryOperation {
3753 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003754 HOr(Primitive::Type result_type,
3755 HInstruction* left,
3756 HInstruction* right,
3757 uint32_t dex_pc = kNoDexPc)
3758 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003759
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003760 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003761
Roland Levillain9867bc72015-08-05 10:21:34 +01003762 template <typename T, typename U>
3763 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3764
3765 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003766 return GetBlock()->GetGraph()->GetIntConstant(
3767 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003768 }
3769 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003770 return GetBlock()->GetGraph()->GetLongConstant(
3771 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003772 }
3773 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003774 return GetBlock()->GetGraph()->GetLongConstant(
3775 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003776 }
3777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003778 return GetBlock()->GetGraph()->GetLongConstant(
3779 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003780 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003781
3782 DECLARE_INSTRUCTION(Or);
3783
3784 private:
3785 DISALLOW_COPY_AND_ASSIGN(HOr);
3786};
3787
3788class HXor : public HBinaryOperation {
3789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003790 HXor(Primitive::Type result_type,
3791 HInstruction* left,
3792 HInstruction* right,
3793 uint32_t dex_pc = kNoDexPc)
3794 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003795
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003796 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003797
Roland Levillain9867bc72015-08-05 10:21:34 +01003798 template <typename T, typename U>
3799 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3800
3801 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003802 return GetBlock()->GetGraph()->GetIntConstant(
3803 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003804 }
3805 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003806 return GetBlock()->GetGraph()->GetLongConstant(
3807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 }
3809 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003810 return GetBlock()->GetGraph()->GetLongConstant(
3811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003812 }
3813 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003814 return GetBlock()->GetGraph()->GetLongConstant(
3815 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003817
3818 DECLARE_INSTRUCTION(Xor);
3819
3820 private:
3821 DISALLOW_COPY_AND_ASSIGN(HXor);
3822};
3823
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003824// The value of a parameter in this method. Its location depends on
3825// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003826class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003828 HParameterValue(uint8_t index,
3829 Primitive::Type parameter_type,
3830 bool is_this = false)
3831 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003832 index_(index),
3833 is_this_(is_this),
3834 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003835
3836 uint8_t GetIndex() const { return index_; }
3837
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003838 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3839 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003840
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003841 bool IsThis() const { return is_this_; }
3842
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003843 DECLARE_INSTRUCTION(ParameterValue);
3844
3845 private:
3846 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003847 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003848 const uint8_t index_;
3849
Calin Juravle10e244f2015-01-26 18:54:32 +00003850 // Whether or not the parameter value corresponds to 'this' argument.
3851 const bool is_this_;
3852
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003853 bool can_be_null_;
3854
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003855 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3856};
3857
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003858class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003859 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003860 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3861 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003863 bool CanBeMoved() const OVERRIDE { return true; }
3864 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003865 UNUSED(other);
3866 return true;
3867 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003868
Roland Levillain9867bc72015-08-05 10:21:34 +01003869 template <typename T> T Compute(T x) const { return ~x; }
3870
3871 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003873 }
3874 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003875 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003876 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003877
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003878 DECLARE_INSTRUCTION(Not);
3879
3880 private:
3881 DISALLOW_COPY_AND_ASSIGN(HNot);
3882};
3883
David Brazdil66d126e2015-04-03 16:02:44 +01003884class HBooleanNot : public HUnaryOperation {
3885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3887 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003888
3889 bool CanBeMoved() const OVERRIDE { return true; }
3890 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3891 UNUSED(other);
3892 return true;
3893 }
3894
Roland Levillain9867bc72015-08-05 10:21:34 +01003895 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003896 DCHECK(IsUint<1>(x));
3897 return !x;
3898 }
3899
Roland Levillain9867bc72015-08-05 10:21:34 +01003900 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003901 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003902 }
3903 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3904 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003905 UNREACHABLE();
3906 }
3907
3908 DECLARE_INSTRUCTION(BooleanNot);
3909
3910 private:
3911 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3912};
3913
Roland Levillaindff1f282014-11-05 14:15:05 +00003914class HTypeConversion : public HExpression<1> {
3915 public:
3916 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003917 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 : HExpression(result_type,
3919 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3920 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003921 SetRawInputAt(0, input);
3922 DCHECK_NE(input->GetType(), result_type);
3923 }
3924
3925 HInstruction* GetInput() const { return InputAt(0); }
3926 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3927 Primitive::Type GetResultType() const { return GetType(); }
3928
Roland Levillain624279f2014-12-04 11:54:28 +00003929 // Required by the x86 and ARM code generators when producing calls
3930 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003931
Roland Levillaindff1f282014-11-05 14:15:05 +00003932 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003933 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003934
Mark Mendelle82549b2015-05-06 10:55:34 -04003935 // Try to statically evaluate the conversion and return a HConstant
3936 // containing the result. If the input cannot be converted, return nullptr.
3937 HConstant* TryStaticEvaluation() const;
3938
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003939 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3940 Primitive::Type result_type) {
3941 // Some architectures may not require the 'GC' side effects, but at this point
3942 // in the compilation process we do not know what architecture we will
3943 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003944 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3945 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003946 return SideEffects::CanTriggerGC();
3947 }
3948 return SideEffects::None();
3949 }
3950
Roland Levillaindff1f282014-11-05 14:15:05 +00003951 DECLARE_INSTRUCTION(TypeConversion);
3952
3953 private:
3954 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3955};
3956
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003957static constexpr uint32_t kNoRegNumber = -1;
3958
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003959class HPhi : public HInstruction {
3960 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003961 HPhi(ArenaAllocator* arena,
3962 uint32_t reg_number,
3963 size_t number_of_inputs,
3964 Primitive::Type type,
3965 uint32_t dex_pc = kNoDexPc)
3966 : HInstruction(SideEffects::None(), dex_pc),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003967 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003968 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003969 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003970 is_live_(false),
3971 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003972 inputs_.SetSize(number_of_inputs);
3973 }
3974
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003975 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3976 static Primitive::Type ToPhiType(Primitive::Type type) {
3977 switch (type) {
3978 case Primitive::kPrimBoolean:
3979 case Primitive::kPrimByte:
3980 case Primitive::kPrimShort:
3981 case Primitive::kPrimChar:
3982 return Primitive::kPrimInt;
3983 default:
3984 return type;
3985 }
3986 }
3987
David Brazdilffee3d32015-07-06 11:48:53 +01003988 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3989
Calin Juravle10e244f2015-01-26 18:54:32 +00003990 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003991
3992 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003993 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003994
Calin Juravle10e244f2015-01-26 18:54:32 +00003995 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003996 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003997
Calin Juravle10e244f2015-01-26 18:54:32 +00003998 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3999 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4000
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004001 uint32_t GetRegNumber() const { return reg_number_; }
4002
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004003 void SetDead() { is_live_ = false; }
4004 void SetLive() { is_live_ = true; }
4005 bool IsDead() const { return !is_live_; }
4006 bool IsLive() const { return is_live_; }
4007
Calin Juravlea4f88312015-04-16 12:57:19 +01004008 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4009 // An equivalent phi is a phi having the same dex register and type.
4010 // It assumes that phis with the same dex register are adjacent.
4011 HPhi* GetNextEquivalentPhiWithSameType() {
4012 HInstruction* next = GetNext();
4013 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4014 if (next->GetType() == GetType()) {
4015 return next->AsPhi();
4016 }
4017 next = next->GetNext();
4018 }
4019 return nullptr;
4020 }
4021
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004022 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004023
David Brazdil1abb4192015-02-17 18:33:36 +00004024 protected:
4025 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
4026
4027 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
4028 inputs_.Put(index, input);
4029 }
4030
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004031 private:
David Brazdil1abb4192015-02-17 18:33:36 +00004032 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004033 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004034 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004035 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004036 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004037
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004038 DISALLOW_COPY_AND_ASSIGN(HPhi);
4039};
4040
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004041class HNullCheck : public HExpression<1> {
4042 public:
4043 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004044 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004045 SetRawInputAt(0, value);
4046 }
4047
Calin Juravle10e244f2015-01-26 18:54:32 +00004048 bool CanBeMoved() const OVERRIDE { return true; }
4049 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004050 UNUSED(other);
4051 return true;
4052 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004053
Calin Juravle10e244f2015-01-26 18:54:32 +00004054 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004055
Calin Juravle10e244f2015-01-26 18:54:32 +00004056 bool CanThrow() const OVERRIDE { return true; }
4057
4058 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004059
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004060
4061 DECLARE_INSTRUCTION(NullCheck);
4062
4063 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004064 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4065};
4066
4067class FieldInfo : public ValueObject {
4068 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004069 FieldInfo(MemberOffset field_offset,
4070 Primitive::Type field_type,
4071 bool is_volatile,
4072 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004073 const DexFile& dex_file,
4074 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004075 : field_offset_(field_offset),
4076 field_type_(field_type),
4077 is_volatile_(is_volatile),
4078 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004079 dex_file_(dex_file),
4080 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004081
4082 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004083 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004084 uint32_t GetFieldIndex() const { return index_; }
4085 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004086 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004087 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004088
4089 private:
4090 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004091 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004092 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004093 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004094 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004095 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004096};
4097
4098class HInstanceFieldGet : public HExpression<1> {
4099 public:
4100 HInstanceFieldGet(HInstruction* value,
4101 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004102 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004103 bool is_volatile,
4104 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004105 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 Handle<mirror::DexCache> dex_cache,
4107 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004108 : HExpression(
4109 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004110 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004111 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004112 SetRawInputAt(0, value);
4113 }
4114
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004115 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004116
4117 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4118 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4119 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004120 }
4121
Calin Juravle641547a2015-04-21 22:08:51 +01004122 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4123 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004124 }
4125
4126 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004127 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4128 }
4129
Calin Juravle52c48962014-12-16 17:02:57 +00004130 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004132 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004133 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004134
4135 DECLARE_INSTRUCTION(InstanceFieldGet);
4136
4137 private:
4138 const FieldInfo field_info_;
4139
4140 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4141};
4142
4143class HInstanceFieldSet : public HTemplateInstruction<2> {
4144 public:
4145 HInstanceFieldSet(HInstruction* object,
4146 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004147 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004148 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004149 bool is_volatile,
4150 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004151 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004152 Handle<mirror::DexCache> dex_cache,
4153 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004154 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004155 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004156 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004157 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004158 SetRawInputAt(0, object);
4159 SetRawInputAt(1, value);
4160 }
4161
Calin Juravle641547a2015-04-21 22:08:51 +01004162 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4163 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004164 }
4165
Calin Juravle52c48962014-12-16 17:02:57 +00004166 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004167 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004168 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004169 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004170 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004171 bool GetValueCanBeNull() const { return value_can_be_null_; }
4172 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004173
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174 DECLARE_INSTRUCTION(InstanceFieldSet);
4175
4176 private:
4177 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004178 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004179
4180 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4181};
4182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004183class HArrayGet : public HExpression<2> {
4184 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004185 HArrayGet(HInstruction* array,
4186 HInstruction* index,
4187 Primitive::Type type,
4188 uint32_t dex_pc = kNoDexPc)
4189 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004190 SetRawInputAt(0, array);
4191 SetRawInputAt(1, index);
4192 }
4193
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004194 bool CanBeMoved() const OVERRIDE { return true; }
4195 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004196 UNUSED(other);
4197 return true;
4198 }
Calin Juravle641547a2015-04-21 22:08:51 +01004199 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4200 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004201 // TODO: We can be smarter here.
4202 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4203 // which generates the implicit null check. There are cases when these can be removed
4204 // to produce better code. If we ever add optimizations to do so we should allow an
4205 // implicit check here (as long as the address falls in the first page).
4206 return false;
4207 }
4208
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004209 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004210
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004211 HInstruction* GetArray() const { return InputAt(0); }
4212 HInstruction* GetIndex() const { return InputAt(1); }
4213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 DECLARE_INSTRUCTION(ArrayGet);
4215
4216 private:
4217 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4218};
4219
4220class HArraySet : public HTemplateInstruction<3> {
4221 public:
4222 HArraySet(HInstruction* array,
4223 HInstruction* index,
4224 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004225 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004226 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004227 : HTemplateInstruction(
4228 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004230 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004231 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4232 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233 SetRawInputAt(0, array);
4234 SetRawInputAt(1, index);
4235 SetRawInputAt(2, value);
4236 }
4237
Calin Juravle77520bc2015-01-12 18:45:46 +00004238 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004239 // We currently always call a runtime method to catch array store
4240 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004241 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 }
4243
Mingyao Yang81014cb2015-06-02 03:16:27 -07004244 // Can throw ArrayStoreException.
4245 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4246
Calin Juravle641547a2015-04-21 22:08:51 +01004247 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4248 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004249 // TODO: Same as for ArrayGet.
4250 return false;
4251 }
4252
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004253 void ClearNeedsTypeCheck() {
4254 needs_type_check_ = false;
4255 }
4256
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004257 void ClearValueCanBeNull() {
4258 value_can_be_null_ = false;
4259 }
4260
4261 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004262 bool NeedsTypeCheck() const { return needs_type_check_; }
4263
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004264 HInstruction* GetArray() const { return InputAt(0); }
4265 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004266 HInstruction* GetValue() const { return InputAt(2); }
4267
4268 Primitive::Type GetComponentType() const {
4269 // The Dex format does not type floating point index operations. Since the
4270 // `expected_component_type_` is set during building and can therefore not
4271 // be correct, we also check what is the value type. If it is a floating
4272 // point type, we must use that type.
4273 Primitive::Type value_type = GetValue()->GetType();
4274 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4275 ? value_type
4276 : expected_component_type_;
4277 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004278
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004279 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4280 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4281 }
4282
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004283 DECLARE_INSTRUCTION(ArraySet);
4284
4285 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004286 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004287 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004288 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289
4290 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4291};
4292
4293class HArrayLength : public HExpression<1> {
4294 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4296 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004297 // Note that arrays do not change length, so the instruction does not
4298 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004299 SetRawInputAt(0, array);
4300 }
4301
Calin Juravle77520bc2015-01-12 18:45:46 +00004302 bool CanBeMoved() const OVERRIDE { return true; }
4303 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004304 UNUSED(other);
4305 return true;
4306 }
Calin Juravle641547a2015-04-21 22:08:51 +01004307 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4308 return obj == InputAt(0);
4309 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004310
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 DECLARE_INSTRUCTION(ArrayLength);
4312
4313 private:
4314 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4315};
4316
4317class HBoundsCheck : public HExpression<2> {
4318 public:
4319 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004321 DCHECK(index->GetType() == Primitive::kPrimInt);
4322 SetRawInputAt(0, index);
4323 SetRawInputAt(1, length);
4324 }
4325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004326 bool CanBeMoved() const OVERRIDE { return true; }
4327 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004328 UNUSED(other);
4329 return true;
4330 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004332 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004334 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004335
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004336
4337 DECLARE_INSTRUCTION(BoundsCheck);
4338
4339 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4341};
4342
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004343/**
4344 * Some DEX instructions are folded into multiple HInstructions that need
4345 * to stay live until the last HInstruction. This class
4346 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004347 * HInstruction stays live. `index` represents the stack location index of the
4348 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004349 */
4350class HTemporary : public HTemplateInstruction<0> {
4351 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4353 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354
4355 size_t GetIndex() const { return index_; }
4356
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004357 Primitive::Type GetType() const OVERRIDE {
4358 // The previous instruction is the one that will be stored in the temporary location.
4359 DCHECK(GetPrevious() != nullptr);
4360 return GetPrevious()->GetType();
4361 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004362
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363 DECLARE_INSTRUCTION(Temporary);
4364
4365 private:
4366 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4368};
4369
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004370class HSuspendCheck : public HTemplateInstruction<0> {
4371 public:
4372 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004374
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004375 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004376 return true;
4377 }
4378
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004379 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4380 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004381
4382 DECLARE_INSTRUCTION(SuspendCheck);
4383
4384 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004385 // Only used for code generation, in order to share the same slow path between back edges
4386 // of a same loop.
4387 SlowPathCode* slow_path_;
4388
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004389 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4390};
4391
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004392/**
4393 * Instruction to load a Class object.
4394 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004395class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004396 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004397 HLoadClass(HCurrentMethod* current_method,
4398 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004399 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004400 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004401 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004402 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004403 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004404 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004405 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004406 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004407 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004408 SetRawInputAt(0, current_method);
4409 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004410
4411 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004412
4413 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4414 return other->AsLoadClass()->type_index_ == type_index_;
4415 }
4416
4417 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4418
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004419 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004420 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004421 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004422
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004423 bool NeedsEnvironment() const OVERRIDE {
4424 // Will call runtime and load the class if the class is not loaded yet.
4425 // TODO: finer grain decision.
4426 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004427 }
4428
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004429 bool MustGenerateClinitCheck() const {
4430 return generate_clinit_check_;
4431 }
4432
Calin Juravle0ba218d2015-05-19 18:46:01 +01004433 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4434 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004435 }
4436
4437 bool CanCallRuntime() const {
4438 return MustGenerateClinitCheck() || !is_referrers_class_;
4439 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004440
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004441 bool CanThrow() const OVERRIDE {
4442 // May call runtime and and therefore can throw.
4443 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004444 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004445 }
4446
Calin Juravleacf735c2015-02-12 15:25:22 +00004447 ReferenceTypeInfo GetLoadedClassRTI() {
4448 return loaded_class_rti_;
4449 }
4450
4451 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4452 // Make sure we only set exact types (the loaded class should never be merged).
4453 DCHECK(rti.IsExact());
4454 loaded_class_rti_ = rti;
4455 }
4456
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004457 const DexFile& GetDexFile() { return dex_file_; }
4458
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004459 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4460
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004461 static SideEffects SideEffectsForArchRuntimeCalls() {
4462 return SideEffects::CanTriggerGC();
4463 }
4464
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004465 DECLARE_INSTRUCTION(LoadClass);
4466
4467 private:
4468 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004469 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004470 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004471 // Whether this instruction must generate the initialization check.
4472 // Used for code generation.
4473 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004474
Calin Juravleacf735c2015-02-12 15:25:22 +00004475 ReferenceTypeInfo loaded_class_rti_;
4476
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004477 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4478};
4479
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004480class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004481 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004482 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004483 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4484 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004485 SetRawInputAt(0, current_method);
4486 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004487
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004488 bool CanBeMoved() const OVERRIDE { return true; }
4489
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004490 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4491 return other->AsLoadString()->string_index_ == string_index_;
4492 }
4493
4494 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4495
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004496 uint32_t GetStringIndex() const { return string_index_; }
4497
4498 // TODO: Can we deopt or debug when we resolve a string?
4499 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004500 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004501 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004502
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004503 static SideEffects SideEffectsForArchRuntimeCalls() {
4504 return SideEffects::CanTriggerGC();
4505 }
4506
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004507 DECLARE_INSTRUCTION(LoadString);
4508
4509 private:
4510 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004511
4512 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4513};
4514
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004515/**
4516 * Performs an initialization check on its Class object input.
4517 */
4518class HClinitCheck : public HExpression<1> {
4519 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004520 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004521 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004522 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004523 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4524 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004525 SetRawInputAt(0, constant);
4526 }
4527
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004528 bool CanBeMoved() const OVERRIDE { return true; }
4529 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4530 UNUSED(other);
4531 return true;
4532 }
4533
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004534 bool NeedsEnvironment() const OVERRIDE {
4535 // May call runtime to initialize the class.
4536 return true;
4537 }
4538
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004539
4540 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4541
4542 DECLARE_INSTRUCTION(ClinitCheck);
4543
4544 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004545 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4546};
4547
4548class HStaticFieldGet : public HExpression<1> {
4549 public:
4550 HStaticFieldGet(HInstruction* cls,
4551 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004552 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004553 bool is_volatile,
4554 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004555 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004556 Handle<mirror::DexCache> dex_cache,
4557 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004558 : HExpression(
4559 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004560 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004561 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004562 SetRawInputAt(0, cls);
4563 }
4564
Calin Juravle52c48962014-12-16 17:02:57 +00004565
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004566 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004567
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004568 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004569 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4570 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004571 }
4572
4573 size_t ComputeHashCode() const OVERRIDE {
4574 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4575 }
4576
Calin Juravle52c48962014-12-16 17:02:57 +00004577 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004578 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4579 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004580 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004581
4582 DECLARE_INSTRUCTION(StaticFieldGet);
4583
4584 private:
4585 const FieldInfo field_info_;
4586
4587 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4588};
4589
4590class HStaticFieldSet : public HTemplateInstruction<2> {
4591 public:
4592 HStaticFieldSet(HInstruction* cls,
4593 HInstruction* value,
4594 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004595 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004596 bool is_volatile,
4597 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004598 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004599 Handle<mirror::DexCache> dex_cache,
4600 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004601 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004603 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004604 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004605 SetRawInputAt(0, cls);
4606 SetRawInputAt(1, value);
4607 }
4608
Calin Juravle52c48962014-12-16 17:02:57 +00004609 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004610 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4611 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004612 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004613
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004614 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004615 bool GetValueCanBeNull() const { return value_can_be_null_; }
4616 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004617
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004618 DECLARE_INSTRUCTION(StaticFieldSet);
4619
4620 private:
4621 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004622 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004623
4624 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4625};
4626
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004627// Implement the move-exception DEX instruction.
4628class HLoadException : public HExpression<0> {
4629 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4631 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004632
David Brazdilbbd733e2015-08-18 17:48:17 +01004633 bool CanBeNull() const OVERRIDE { return false; }
4634
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004635 DECLARE_INSTRUCTION(LoadException);
4636
4637 private:
4638 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4639};
4640
David Brazdilcb1c0552015-08-04 16:22:25 +01004641// Implicit part of move-exception which clears thread-local exception storage.
4642// Must not be removed because the runtime expects the TLS to get cleared.
4643class HClearException : public HTemplateInstruction<0> {
4644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004645 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4646 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004647
4648 DECLARE_INSTRUCTION(ClearException);
4649
4650 private:
4651 DISALLOW_COPY_AND_ASSIGN(HClearException);
4652};
4653
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004654class HThrow : public HTemplateInstruction<1> {
4655 public:
4656 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004657 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004658 SetRawInputAt(0, exception);
4659 }
4660
4661 bool IsControlFlow() const OVERRIDE { return true; }
4662
4663 bool NeedsEnvironment() const OVERRIDE { return true; }
4664
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004665 bool CanThrow() const OVERRIDE { return true; }
4666
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004667
4668 DECLARE_INSTRUCTION(Throw);
4669
4670 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004671 DISALLOW_COPY_AND_ASSIGN(HThrow);
4672};
4673
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004674class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004675 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004676 HInstanceOf(HInstruction* object,
4677 HLoadClass* constant,
4678 bool class_is_final,
4679 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 : HExpression(Primitive::kPrimBoolean,
4681 SideEffectsForArchRuntimeCalls(class_is_final),
4682 dex_pc),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004683 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004685 SetRawInputAt(0, object);
4686 SetRawInputAt(1, constant);
4687 }
4688
4689 bool CanBeMoved() const OVERRIDE { return true; }
4690
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004691 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004692 return true;
4693 }
4694
4695 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004696 return false;
4697 }
4698
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004699 bool IsClassFinal() const { return class_is_final_; }
4700
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004701 // Used only in code generation.
4702 bool MustDoNullCheck() const { return must_do_null_check_; }
4703 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4704
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004705 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4706 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4707 }
4708
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004709 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004710
4711 private:
4712 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004713 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004714
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004715 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4716};
4717
Calin Juravleb1498f62015-02-16 13:13:29 +00004718class HBoundType : public HExpression<1> {
4719 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004720 // Constructs an HBoundType with the given upper_bound.
4721 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004722 HBoundType(HInstruction* input,
4723 ReferenceTypeInfo upper_bound,
4724 bool upper_can_be_null,
4725 uint32_t dex_pc = kNoDexPc)
4726 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004727 upper_bound_(upper_bound),
4728 upper_can_be_null_(upper_can_be_null),
4729 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004730 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004731 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004732 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004733 }
4734
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004735 // GetUpper* should only be used in reference type propagation.
4736 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4737 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004738
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004739 void SetCanBeNull(bool can_be_null) {
4740 DCHECK(upper_can_be_null_ || !can_be_null);
4741 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004742 }
4743
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004744 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4745
Calin Juravleb1498f62015-02-16 13:13:29 +00004746 DECLARE_INSTRUCTION(BoundType);
4747
4748 private:
4749 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004750 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4751 // It is used to bound the type in cases like:
4752 // if (x instanceof ClassX) {
4753 // // uper_bound_ will be ClassX
4754 // }
4755 const ReferenceTypeInfo upper_bound_;
4756 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4757 // is false then can_be_null_ cannot be true).
4758 const bool upper_can_be_null_;
4759 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004760
4761 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4762};
4763
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004764class HCheckCast : public HTemplateInstruction<2> {
4765 public:
4766 HCheckCast(HInstruction* object,
4767 HLoadClass* constant,
4768 bool class_is_final,
4769 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004770 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004771 class_is_final_(class_is_final),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004772 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004773 SetRawInputAt(0, object);
4774 SetRawInputAt(1, constant);
4775 }
4776
4777 bool CanBeMoved() const OVERRIDE { return true; }
4778
4779 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4780 return true;
4781 }
4782
4783 bool NeedsEnvironment() const OVERRIDE {
4784 // Instruction may throw a CheckCastError.
4785 return true;
4786 }
4787
4788 bool CanThrow() const OVERRIDE { return true; }
4789
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004790 bool MustDoNullCheck() const { return must_do_null_check_; }
4791 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4792
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004793
4794 bool IsClassFinal() const { return class_is_final_; }
4795
4796 DECLARE_INSTRUCTION(CheckCast);
4797
4798 private:
4799 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004800 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004801
4802 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004803};
4804
Calin Juravle27df7582015-04-17 19:12:31 +01004805class HMemoryBarrier : public HTemplateInstruction<0> {
4806 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004808 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004809 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004810 barrier_kind_(barrier_kind) {}
4811
4812 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4813
4814 DECLARE_INSTRUCTION(MemoryBarrier);
4815
4816 private:
4817 const MemBarrierKind barrier_kind_;
4818
4819 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4820};
4821
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004822class HMonitorOperation : public HTemplateInstruction<1> {
4823 public:
4824 enum OperationKind {
4825 kEnter,
4826 kExit,
4827 };
4828
4829 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004830 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004831 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4832 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004833 SetRawInputAt(0, object);
4834 }
4835
4836 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004837 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4838
4839 bool CanThrow() const OVERRIDE {
4840 // Verifier guarantees that monitor-exit cannot throw.
4841 // This is important because it allows the HGraphBuilder to remove
4842 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4843 return IsEnter();
4844 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004845
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004846
4847 bool IsEnter() const { return kind_ == kEnter; }
4848
4849 DECLARE_INSTRUCTION(MonitorOperation);
4850
Calin Juravle52c48962014-12-16 17:02:57 +00004851 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004852 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004853
4854 private:
4855 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4856};
4857
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004858/**
4859 * A HInstruction used as a marker for the replacement of new + <init>
4860 * of a String to a call to a StringFactory. Only baseline will see
4861 * the node at code generation, where it will be be treated as null.
4862 * When compiling non-baseline, `HFakeString` instructions are being removed
4863 * in the instruction simplifier.
4864 */
4865class HFakeString : public HTemplateInstruction<0> {
4866 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004867 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4868 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004869
4870 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4871
4872 DECLARE_INSTRUCTION(FakeString);
4873
4874 private:
4875 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4876};
4877
Vladimir Markof9f64412015-09-02 14:05:49 +01004878class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004879 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004880 MoveOperands(Location source,
4881 Location destination,
4882 Primitive::Type type,
4883 HInstruction* instruction)
4884 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004885
4886 Location GetSource() const { return source_; }
4887 Location GetDestination() const { return destination_; }
4888
4889 void SetSource(Location value) { source_ = value; }
4890 void SetDestination(Location value) { destination_ = value; }
4891
4892 // The parallel move resolver marks moves as "in-progress" by clearing the
4893 // destination (but not the source).
4894 Location MarkPending() {
4895 DCHECK(!IsPending());
4896 Location dest = destination_;
4897 destination_ = Location::NoLocation();
4898 return dest;
4899 }
4900
4901 void ClearPending(Location dest) {
4902 DCHECK(IsPending());
4903 destination_ = dest;
4904 }
4905
4906 bool IsPending() const {
4907 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4908 return destination_.IsInvalid() && !source_.IsInvalid();
4909 }
4910
4911 // True if this blocks a move from the given location.
4912 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004913 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004914 }
4915
4916 // A move is redundant if it's been eliminated, if its source and
4917 // destination are the same, or if its destination is unneeded.
4918 bool IsRedundant() const {
4919 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4920 }
4921
4922 // We clear both operands to indicate move that's been eliminated.
4923 void Eliminate() {
4924 source_ = destination_ = Location::NoLocation();
4925 }
4926
4927 bool IsEliminated() const {
4928 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4929 return source_.IsInvalid();
4930 }
4931
Alexey Frunze4dda3372015-06-01 18:31:49 -07004932 Primitive::Type GetType() const { return type_; }
4933
Nicolas Geoffray90218252015-04-15 11:56:51 +01004934 bool Is64BitMove() const {
4935 return Primitive::Is64BitType(type_);
4936 }
4937
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004938 HInstruction* GetInstruction() const { return instruction_; }
4939
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004940 private:
4941 Location source_;
4942 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004943 // The type this move is for.
4944 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004945 // The instruction this move is assocatied with. Null when this move is
4946 // for moving an input in the expected locations of user (including a phi user).
4947 // This is only used in debug mode, to ensure we do not connect interval siblings
4948 // in the same parallel move.
4949 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004950};
4951
4952static constexpr size_t kDefaultNumberOfMoves = 4;
4953
4954class HParallelMove : public HTemplateInstruction<0> {
4955 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
4957 : HTemplateInstruction(SideEffects::None(), dex_pc), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004958
Nicolas Geoffray90218252015-04-15 11:56:51 +01004959 void AddMove(Location source,
4960 Location destination,
4961 Primitive::Type type,
4962 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004963 DCHECK(source.IsValid());
4964 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004965 if (kIsDebugBuild) {
4966 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004967 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004968 if (moves_.Get(i).GetInstruction() == instruction) {
4969 // Special case the situation where the move is for the spill slot
4970 // of the instruction.
4971 if ((GetPrevious() == instruction)
4972 || ((GetPrevious() == nullptr)
4973 && instruction->IsPhi()
4974 && instruction->GetBlock() == GetBlock())) {
4975 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4976 << "Doing parallel moves for the same instruction.";
4977 } else {
4978 DCHECK(false) << "Doing parallel moves for the same instruction.";
4979 }
4980 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004981 }
4982 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004983 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004984 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004985 << "Overlapped destination for two moves in a parallel move: "
4986 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4987 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004988 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004989 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004990 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004991 }
4992
4993 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004994 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004995 }
4996
4997 size_t NumMoves() const { return moves_.Size(); }
4998
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004999 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005000
5001 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005002 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005003
5004 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5005};
5006
Mark Mendell0616ae02015-04-17 12:49:27 -04005007} // namespace art
5008
5009#ifdef ART_ENABLE_CODEGEN_x86
5010#include "nodes_x86.h"
5011#endif
5012
5013namespace art {
5014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005015class HGraphVisitor : public ValueObject {
5016 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005017 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5018 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005019
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005020 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005021 virtual void VisitBasicBlock(HBasicBlock* block);
5022
Roland Levillain633021e2014-10-01 14:12:25 +01005023 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005024 void VisitInsertionOrder();
5025
Roland Levillain633021e2014-10-01 14:12:25 +01005026 // Visit the graph following dominator tree reverse post-order.
5027 void VisitReversePostOrder();
5028
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005029 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005030
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005031 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005032#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005033 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5034
5035 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5036
5037#undef DECLARE_VISIT_INSTRUCTION
5038
5039 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005040 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005041
5042 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5043};
5044
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005045class HGraphDelegateVisitor : public HGraphVisitor {
5046 public:
5047 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5048 virtual ~HGraphDelegateVisitor() {}
5049
5050 // Visit functions that delegate to to super class.
5051#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005052 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005053
5054 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5055
5056#undef DECLARE_VISIT_INSTRUCTION
5057
5058 private:
5059 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5060};
5061
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005062class HInsertionOrderIterator : public ValueObject {
5063 public:
5064 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5065
5066 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5067 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5068 void Advance() { ++index_; }
5069
5070 private:
5071 const HGraph& graph_;
5072 size_t index_;
5073
5074 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5075};
5076
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005077class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005078 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005079 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5080 // Check that reverse post order of the graph has been built.
5081 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5082 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005083
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005084 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5085 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005086 void Advance() { ++index_; }
5087
5088 private:
5089 const HGraph& graph_;
5090 size_t index_;
5091
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005092 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005093};
5094
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005095class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005096 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005097 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005098 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5099 // Check that reverse post order of the graph has been built.
5100 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5101 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005102
5103 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005104 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005105 void Advance() { --index_; }
5106
5107 private:
5108 const HGraph& graph_;
5109 size_t index_;
5110
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005111 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005112};
5113
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005114class HLinearPostOrderIterator : public ValueObject {
5115 public:
5116 explicit HLinearPostOrderIterator(const HGraph& graph)
5117 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5118
5119 bool Done() const { return index_ == 0; }
5120
5121 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5122
5123 void Advance() {
5124 --index_;
5125 DCHECK_GE(index_, 0U);
5126 }
5127
5128 private:
5129 const GrowableArray<HBasicBlock*>& order_;
5130 size_t index_;
5131
5132 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5133};
5134
5135class HLinearOrderIterator : public ValueObject {
5136 public:
5137 explicit HLinearOrderIterator(const HGraph& graph)
5138 : order_(graph.GetLinearOrder()), index_(0) {}
5139
5140 bool Done() const { return index_ == order_.Size(); }
5141 HBasicBlock* Current() const { return order_.Get(index_); }
5142 void Advance() { ++index_; }
5143
5144 private:
5145 const GrowableArray<HBasicBlock*>& order_;
5146 size_t index_;
5147
5148 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5149};
5150
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005151// Iterator over the blocks that art part of the loop. Includes blocks part
5152// of an inner loop. The order in which the blocks are iterated is on their
5153// block id.
5154class HBlocksInLoopIterator : public ValueObject {
5155 public:
5156 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5157 : blocks_in_loop_(info.GetBlocks()),
5158 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5159 index_(0) {
5160 if (!blocks_in_loop_.IsBitSet(index_)) {
5161 Advance();
5162 }
5163 }
5164
5165 bool Done() const { return index_ == blocks_.Size(); }
5166 HBasicBlock* Current() const { return blocks_.Get(index_); }
5167 void Advance() {
5168 ++index_;
5169 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5170 if (blocks_in_loop_.IsBitSet(index_)) {
5171 break;
5172 }
5173 }
5174 }
5175
5176 private:
5177 const BitVector& blocks_in_loop_;
5178 const GrowableArray<HBasicBlock*>& blocks_;
5179 size_t index_;
5180
5181 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5182};
5183
Mingyao Yang3584bce2015-05-19 16:01:59 -07005184// Iterator over the blocks that art part of the loop. Includes blocks part
5185// of an inner loop. The order in which the blocks are iterated is reverse
5186// post order.
5187class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5188 public:
5189 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5190 : blocks_in_loop_(info.GetBlocks()),
5191 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5192 index_(0) {
5193 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5194 Advance();
5195 }
5196 }
5197
5198 bool Done() const { return index_ == blocks_.Size(); }
5199 HBasicBlock* Current() const { return blocks_.Get(index_); }
5200 void Advance() {
5201 ++index_;
5202 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5203 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5204 break;
5205 }
5206 }
5207 }
5208
5209 private:
5210 const BitVector& blocks_in_loop_;
5211 const GrowableArray<HBasicBlock*>& blocks_;
5212 size_t index_;
5213
5214 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5215};
5216
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005217inline int64_t Int64FromConstant(HConstant* constant) {
5218 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5219 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5220 : constant->AsLongConstant()->GetValue();
5221}
5222
Vladimir Marko58155012015-08-19 12:49:41 +00005223inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5224 // For the purposes of the compiler, the dex files must actually be the same object
5225 // if we want to safely treat them as the same. This is especially important for JIT
5226 // as custom class loaders can open the same underlying file (or memory) multiple
5227 // times and provide different class resolution but no two class loaders should ever
5228 // use the same DexFile object - doing so is an unsupported hack that can lead to
5229 // all sorts of weird failures.
5230 return &lhs == &rhs;
5231}
5232
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005233} // namespace art
5234
5235#endif // ART_COMPILER_OPTIMIZING_NODES_H_