blob: 768658ee05da4b28121b106c457beac600fefa1b [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 Marko91e11c02015-09-02 17:03:22 +010020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
David Brazdil8d5b8b22015-03-24 10:51:52 +000024#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010026#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000027#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "handle.h"
29#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000030#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010031#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000032#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000033#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010034#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070035#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000036#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037#include "utils/growable_array.h"
38
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
63static const int kDefaultNumberOfBlocks = 8;
64static const int kDefaultNumberOfSuccessors = 2;
65static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010066static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010067static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000068static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069
Calin Juravle9aec02f2014-11-18 23:06:35 +000070static constexpr uint32_t kMaxIntShiftValue = 0x1f;
71static constexpr uint64_t kMaxLongShiftValue = 0x3f;
72
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010073static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
74
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010075static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
76
Dave Allison20dfc792014-06-16 20:44:29 -070077enum IfCondition {
78 kCondEQ,
79 kCondNE,
80 kCondLT,
81 kCondLE,
82 kCondGT,
83 kCondGE,
84};
85
Vladimir Markof9f64412015-09-02 14:05:49 +010086class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010087 public:
88 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
89
90 void AddInstruction(HInstruction* instruction);
91 void RemoveInstruction(HInstruction* instruction);
92
David Brazdilc3d743f2015-04-22 13:40:50 +010093 // Insert `instruction` before/after an existing instruction `cursor`.
94 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
95 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
96
Roland Levillain6b469232014-09-25 10:10:38 +010097 // Return true if this list contains `instruction`.
98 bool Contains(HInstruction* instruction) const;
99
Roland Levillainccc07a92014-09-16 14:48:16 +0100100 // Return true if `instruction1` is found before `instruction2` in
101 // this instruction list and false otherwise. Abort if none
102 // of these instructions is found.
103 bool FoundBefore(const HInstruction* instruction1,
104 const HInstruction* instruction2) const;
105
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000106 bool IsEmpty() const { return first_instruction_ == nullptr; }
107 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
108
109 // Update the block of all instructions to be `block`.
110 void SetBlockOfInstructions(HBasicBlock* block) const;
111
112 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
113 void Add(const HInstructionList& instruction_list);
114
David Brazdil2d7352b2015-04-20 14:52:42 +0100115 // Return the number of instructions in the list. This is an expensive operation.
116 size_t CountSize() const;
117
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118 private:
119 HInstruction* first_instruction_;
120 HInstruction* last_instruction_;
121
122 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000123 friend class HGraph;
124 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100126 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100127
128 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
129};
130
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000131// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100132class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100134 HGraph(ArenaAllocator* arena,
135 const DexFile& dex_file,
136 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100137 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700138 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100139 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100140 bool debuggable = false,
141 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000142 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000143 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100144 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100145 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700146 entry_block_(nullptr),
147 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100148 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100149 number_of_vregs_(0),
150 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000151 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400152 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000153 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000154 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100155 dex_file_(dex_file),
156 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100157 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100158 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100159 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700160 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000161 cached_null_constant_(nullptr),
162 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000163 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
164 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100165 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
166 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100170 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000171
David Brazdil69ba7b72015-06-23 18:27:30 +0100172 bool IsInSsaForm() const { return in_ssa_form_; }
173
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000174 HBasicBlock* GetEntryBlock() const { return entry_block_; }
175 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100176 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000177
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000178 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
179 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000180
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000181 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000183 // Try building the SSA form of this graph, with dominance computation and loop
184 // recognition. Returns whether it was successful in doing all these steps.
185 bool TryBuildingSsa() {
186 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000187 // The SSA builder requires loops to all be natural. Specifically, the dead phi
188 // elimination phase checks the consistency of the graph when doing a post-order
189 // visit for eliminating dead phis: a dead phi can only have loop header phi
190 // users remaining when being visited.
191 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100192 // Precompute per-block try membership before entering the SSA builder,
193 // which needs the information to build catch block phis from values of
194 // locals at throwing instructions inside try blocks.
195 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000196 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100197 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000198 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 }
200
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100201 void ComputeDominanceInformation();
202 void ClearDominanceInformation();
203
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000205 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100206 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100207 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000208
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000209 // Analyze all natural loops in this graph. Returns false if one
210 // loop is not natural, that is the header does not dominate the
211 // back edge.
212 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100213
David Brazdilffee3d32015-07-06 11:48:53 +0100214 // Iterate over blocks to compute try block membership. Needs reverse post
215 // order and loop information.
216 void ComputeTryBlockInformation();
217
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000219 // Returns the instruction used to replace the invoke expression or null if the
220 // invoke is for a void method.
221 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222
Mingyao Yang3584bce2015-05-19 16:01:59 -0700223 // Need to add a couple of blocks to test if the loop body is entered and
224 // put deoptimization instructions, etc.
225 void TransformLoopHeaderForBCE(HBasicBlock* header);
226
David Brazdil2d7352b2015-04-20 14:52:42 +0100227 // Removes `block` from the graph.
228 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000229
David Brazdilfc6a86a2015-06-26 10:33:45 +0000230 // Splits the edge between `block` and `successor` while preserving the
231 // indices in the predecessor/successor lists. If there are multiple edges
232 // between the blocks, the lowest indices are used.
233 // Returns the new block which is empty and has the same dex pc as `successor`.
234 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
235
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100236 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
237 void SimplifyLoop(HBasicBlock* header);
238
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239 int32_t GetNextInstructionId() {
240 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241 return current_instruction_id_++;
242 }
243
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000244 int32_t GetCurrentInstructionId() const {
245 return current_instruction_id_;
246 }
247
248 void SetCurrentInstructionId(int32_t id) {
249 current_instruction_id_ = id;
250 }
251
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100252 uint16_t GetMaximumNumberOfOutVRegs() const {
253 return maximum_number_of_out_vregs_;
254 }
255
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000256 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
257 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100258 }
259
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100260 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
261 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
262 }
263
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000264 void UpdateTemporariesVRegSlots(size_t slots) {
265 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100266 }
267
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000268 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100269 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000270 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100271 }
272
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100273 void SetNumberOfVRegs(uint16_t number_of_vregs) {
274 number_of_vregs_ = number_of_vregs;
275 }
276
277 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100278 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100279 return number_of_vregs_;
280 }
281
282 void SetNumberOfInVRegs(uint16_t value) {
283 number_of_in_vregs_ = value;
284 }
285
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100286 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100287 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100288 return number_of_vregs_ - number_of_in_vregs_;
289 }
290
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
292 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100293 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100294
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100295 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
296 return linear_order_;
297 }
298
Mark Mendell1152c922015-04-24 17:06:35 -0400299 bool HasBoundsChecks() const {
300 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800301 }
302
Mark Mendell1152c922015-04-24 17:06:35 -0400303 void SetHasBoundsChecks(bool value) {
304 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800305 }
306
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100307 bool ShouldGenerateConstructorBarrier() const {
308 return should_generate_constructor_barrier_;
309 }
310
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000311 bool IsDebuggable() const { return debuggable_; }
312
David Brazdil8d5b8b22015-03-24 10:51:52 +0000313 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000314 // already, it is created and inserted into the graph. This method is only for
315 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000316 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000317
318 // TODO: This is problematic for the consistency of reference type propagation
319 // because it can be created anytime after the pass and thus it will be left
320 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000321 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000322
David Brazdil8d5b8b22015-03-24 10:51:52 +0000323 HIntConstant* GetIntConstant(int32_t value) {
324 return CreateConstant(value, &cached_int_constants_);
325 }
326 HLongConstant* GetLongConstant(int64_t value) {
327 return CreateConstant(value, &cached_long_constants_);
328 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000329 HFloatConstant* GetFloatConstant(float value) {
330 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
331 }
332 HDoubleConstant* GetDoubleConstant(double value) {
333 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
334 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000335
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100336 HCurrentMethod* GetCurrentMethod();
337
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100339
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100340 const DexFile& GetDexFile() const {
341 return dex_file_;
342 }
343
344 uint32_t GetMethodIdx() const {
345 return method_idx_;
346 }
347
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100348 InvokeType GetInvokeType() const {
349 return invoke_type_;
350 }
351
Mark Mendellc4701932015-04-10 13:18:51 -0400352 InstructionSet GetInstructionSet() const {
353 return instruction_set_;
354 }
355
David Brazdilbbd733e2015-08-18 17:48:17 +0100356 // TODO: Remove once the full compilation pipeline is enabled for try/catch.
357 bool HasTryCatch() const;
358
David Brazdil2d7352b2015-04-20 14:52:42 +0100359 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360 void VisitBlockForDominatorTree(HBasicBlock* block,
361 HBasicBlock* predecessor,
362 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100363 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000364 void VisitBlockForBackEdges(HBasicBlock* block,
365 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100366 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000367 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100368 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000370 template <class InstructionType, typename ValueType>
371 InstructionType* CreateConstant(ValueType value,
372 ArenaSafeMap<ValueType, InstructionType*>* cache) {
373 // Try to find an existing constant of the given value.
374 InstructionType* constant = nullptr;
375 auto cached_constant = cache->find(value);
376 if (cached_constant != cache->end()) {
377 constant = cached_constant->second;
378 }
379
380 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100381 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000382 if (constant == nullptr || constant->GetBlock() == nullptr) {
383 constant = new (arena_) InstructionType(value);
384 cache->Overwrite(value, constant);
385 InsertConstant(constant);
386 }
387 return constant;
388 }
389
David Brazdil8d5b8b22015-03-24 10:51:52 +0000390 void InsertConstant(HConstant* instruction);
391
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 // Cache a float constant into the graph. This method should only be
393 // called by the SsaBuilder when creating "equivalent" instructions.
394 void CacheFloatConstant(HFloatConstant* constant);
395
396 // See CacheFloatConstant comment.
397 void CacheDoubleConstant(HDoubleConstant* constant);
398
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000400
401 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402 GrowableArray<HBasicBlock*> blocks_;
403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404 // List of blocks to perform a reverse post order tree traversal.
405 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000406
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100407 // List of blocks to perform a linear order tree traversal.
408 GrowableArray<HBasicBlock*> linear_order_;
409
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000410 HBasicBlock* entry_block_;
411 HBasicBlock* exit_block_;
412
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100413 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100414 uint16_t maximum_number_of_out_vregs_;
415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100416 // The number of virtual registers in this method. Contains the parameters.
417 uint16_t number_of_vregs_;
418
419 // The number of virtual registers used by parameters of this method.
420 uint16_t number_of_in_vregs_;
421
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000422 // Number of vreg size slots that the temporaries use (used in baseline compiler).
423 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100424
Mark Mendell1152c922015-04-24 17:06:35 -0400425 // Has bounds checks. We can totally skip BCE if it's false.
426 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800427
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000428 // Indicates whether the graph should be compiled in a way that
429 // ensures full debuggability. If false, we can apply more
430 // aggressive optimizations that may limit the level of debugging.
431 const bool debuggable_;
432
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000433 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000435
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100436 // The dex file from which the method is from.
437 const DexFile& dex_file_;
438
439 // The method index in the dex file.
440 const uint32_t method_idx_;
441
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100442 // If inlined, this encodes how the callee is being invoked.
443 const InvokeType invoke_type_;
444
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100445 // Whether the graph has been transformed to SSA form. Only used
446 // in debug mode to ensure we are not using properties only valid
447 // for non-SSA form (like the number of temporaries).
448 bool in_ssa_form_;
449
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100450 const bool should_generate_constructor_barrier_;
451
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 const InstructionSet instruction_set_;
453
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000454 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 HNullConstant* cached_null_constant_;
456 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000457 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000458 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000459 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000460
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100461 HCurrentMethod* cached_current_method_;
462
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100464 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000465 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 DISALLOW_COPY_AND_ASSIGN(HGraph);
467};
468
Vladimir Markof9f64412015-09-02 14:05:49 +0100469class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000470 public:
471 HLoopInformation(HBasicBlock* header, HGraph* graph)
472 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100473 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100474 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100475 // Make bit vector growable, as the number of blocks may change.
476 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100477
478 HBasicBlock* GetHeader() const {
479 return header_;
480 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000481
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000482 void SetHeader(HBasicBlock* block) {
483 header_ = block;
484 }
485
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100486 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
487 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
488 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
489
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000490 void AddBackEdge(HBasicBlock* back_edge) {
491 back_edges_.Add(back_edge);
492 }
493
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100494 void RemoveBackEdge(HBasicBlock* back_edge) {
495 back_edges_.Delete(back_edge);
496 }
497
David Brazdil46e2a392015-03-16 17:31:52 +0000498 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100499 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000500 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 }
502 return false;
503 }
504
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000505 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 return back_edges_.Size();
507 }
508
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100510
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
512 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100513 }
514
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100515 // Returns the lifetime position of the back edge that has the
516 // greatest lifetime position.
517 size_t GetLifetimeEnd() const;
518
519 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
520 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
521 if (back_edges_.Get(i) == existing) {
522 back_edges_.Put(i, new_back_edge);
523 return;
524 }
525 }
526 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100527 }
528
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100529 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530 // that is the header dominates the back edge.
531 bool Populate();
532
David Brazdila4b8c212015-05-07 09:59:30 +0100533 // Reanalyzes the loop by removing loop info from its blocks and re-running
534 // Populate(). If there are no back edges left, the loop info is completely
535 // removed as well as its SuspendCheck instruction. It must be run on nested
536 // inner loops first.
537 void Update();
538
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100539 // Returns whether this loop information contains `block`.
540 // Note that this loop information *must* be populated before entering this function.
541 bool Contains(const HBasicBlock& block) const;
542
543 // Returns whether this loop information is an inner loop of `other`.
544 // Note that `other` *must* be populated before entering this function.
545 bool IsIn(const HLoopInformation& other) const;
546
547 const ArenaBitVector& GetBlocks() const { return blocks_; }
548
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000549 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000550 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000551
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100553 // Internal recursive implementation of `Populate`.
554 void PopulateRecursive(HBasicBlock* block);
555
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100557 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000560
561 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
562};
563
David Brazdilec16f792015-08-19 15:04:01 +0100564// Stores try/catch information for basic blocks.
565// Note that HGraph is constructed so that catch blocks cannot simultaneously
566// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100567class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100568 public:
569 // Try block information constructor.
570 explicit TryCatchInformation(const HTryBoundary& try_entry)
571 : try_entry_(&try_entry),
572 catch_dex_file_(nullptr),
573 catch_type_index_(DexFile::kDexNoIndex16) {
574 DCHECK(try_entry_ != nullptr);
575 }
576
577 // Catch block information constructor.
578 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
579 : try_entry_(nullptr),
580 catch_dex_file_(&dex_file),
581 catch_type_index_(catch_type_index) {}
582
583 bool IsTryBlock() const { return try_entry_ != nullptr; }
584
585 const HTryBoundary& GetTryEntry() const {
586 DCHECK(IsTryBlock());
587 return *try_entry_;
588 }
589
590 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
591
592 bool IsCatchAllTypeIndex() const {
593 DCHECK(IsCatchBlock());
594 return catch_type_index_ == DexFile::kDexNoIndex16;
595 }
596
597 uint16_t GetCatchTypeIndex() const {
598 DCHECK(IsCatchBlock());
599 return catch_type_index_;
600 }
601
602 const DexFile& GetCatchDexFile() const {
603 DCHECK(IsCatchBlock());
604 return *catch_dex_file_;
605 }
606
607 private:
608 // One of possibly several TryBoundary instructions entering the block's try.
609 // Only set for try blocks.
610 const HTryBoundary* try_entry_;
611
612 // Exception type information. Only set for catch blocks.
613 const DexFile* catch_dex_file_;
614 const uint16_t catch_type_index_;
615};
616
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100617static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100618static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100619
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000620// A block in a method. Contains the list of instructions represented
621// as a double linked list. Each block knows its predecessors and
622// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100623
Vladimir Markof9f64412015-09-02 14:05:49 +0100624class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100626 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627 : graph_(graph),
Vladimir Marko91e11c02015-09-02 17:03:22 +0100628 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
629 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000630 loop_information_(nullptr),
631 dominator_(nullptr),
Vladimir Marko91e11c02015-09-02 17:03:22 +0100632 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100633 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100634 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100635 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000636 lifetime_end_(kNoLifetime),
Vladimir Marko91e11c02015-09-02 17:03:22 +0100637 try_catch_information_(nullptr) {
638 predecessors_.reserve(kDefaultNumberOfPredecessors);
639 successors_.reserve(kDefaultNumberOfSuccessors);
640 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
641 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642
Vladimir Marko91e11c02015-09-02 17:03:22 +0100643 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100644 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 }
646
Vladimir Marko91e11c02015-09-02 17:03:22 +0100647 HBasicBlock* GetPredecessor(size_t pred_idx) const {
648 DCHECK_LT(pred_idx, predecessors_.size());
649 return predecessors_[pred_idx];
650 }
651
652 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654 }
655
Vladimir Marko91e11c02015-09-02 17:03:22 +0100656 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
657 DCHECK_LE(start_from, successors_.size());
658 auto it = std::find(successors_.begin() + start_from, successors_.end(), block);
659 return it != successors_.end();
660 }
661
662 HBasicBlock* GetSuccessor(size_t succ_idx) const {
663 DCHECK_LT(succ_idx, successors_.size());
664 return successors_[succ_idx];
665 }
666
667 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100668 return dominated_blocks_;
669 }
670
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 bool IsEntryBlock() const {
672 return graph_->GetEntryBlock() == this;
673 }
674
675 bool IsExitBlock() const {
676 return graph_->GetExitBlock() == this;
677 }
678
David Brazdil46e2a392015-03-16 17:31:52 +0000679 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000680 bool IsSingleTryBoundary() const;
681
682 // Returns true if this block emits nothing but a jump.
683 bool IsSingleJump() const {
684 HLoopInformation* loop_info = GetLoopInformation();
685 return (IsSingleGoto() || IsSingleTryBoundary())
686 // Back edges generate a suspend check.
687 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
688 }
David Brazdil46e2a392015-03-16 17:31:52 +0000689
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 void AddBackEdge(HBasicBlock* back_edge) {
691 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000692 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 loop_information_->AddBackEdge(back_edge);
696 }
697
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000698 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000699 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000700
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 int GetBlockId() const { return block_id_; }
702 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 HBasicBlock* GetDominator() const { return dominator_; }
705 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko91e11c02015-09-02 17:03:22 +0100706 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
707
708 void RemoveDominatedBlock(HBasicBlock* block) {
709 auto it = std::find(dominated_blocks_.begin(), dominated_blocks_.end(), block);
710 DCHECK(it != dominated_blocks_.end());
711 dominated_blocks_.erase(it);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000712 }
Vladimir Marko91e11c02015-09-02 17:03:22 +0100713
714 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
715 auto it = std::find(dominated_blocks_.begin(), dominated_blocks_.end(), existing);
716 DCHECK(it != dominated_blocks_.end());
717 *it = new_block;
718 }
719
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100720 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000721
722 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100723 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000724 }
725
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
727 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100728 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100729 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100730 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
731 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000732
733 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100734 successors_.push_back(block);
735 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736 }
737
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100738 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
739 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100740 existing->RemovePredecessor(this);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100741 new_block->predecessors_.push_back(this);
742 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 }
744
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000745 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
746 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000747 existing->RemoveSuccessor(this);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100748 new_block->successors_.push_back(this);
749 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000750 }
751
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100752 // Insert `this` between `predecessor` and `successor. This method
753 // preserves the indicies, and will update the first edge found between
754 // `predecessor` and `successor`.
755 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
756 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100757 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100758 successor->predecessors_[predecessor_index] = this;
759 predecessor->successors_[successor_index] = this;
760 successors_.push_back(successor);
761 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100762 }
763
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100764 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100765 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100766 }
767
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000768 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100769 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000770 }
771
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 void ClearAllPredecessors() {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100773 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100774 }
775
776 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100777 predecessors_.push_back(block);
778 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779 }
780
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100781 void SwapPredecessors() {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100782 DCHECK_EQ(predecessors_.size(), 2u);
783 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100784 }
785
David Brazdil769c9e52015-04-27 13:54:09 +0100786 void SwapSuccessors() {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100787 DCHECK_EQ(successors_.size(), 2u);
788 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100789 }
790
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100792 auto it = std::find(predecessors_.begin(), predecessors_.end(), predecessor);
793 DCHECK(it != predecessors_.end());
794 return std::distance(predecessors_.begin(), it);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100798 auto it = std::find(successors_.begin(), successors_.end(), successor);
799 DCHECK(it != successors_.end());
800 return std::distance(successors_.begin(), it);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100801 }
802
David Brazdilfc6a86a2015-06-26 10:33:45 +0000803 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100804 DCHECK_EQ(GetPredecessors().size(), 1u);
805 return GetPredecessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806 }
807
808 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100809 DCHECK_EQ(GetSuccessors().size(), 1u);
810 return GetSuccessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000811 }
812
813 // Returns whether the first occurrence of `predecessor` in the list of
814 // predecessors is at index `idx`.
815 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100816 DCHECK_EQ(GetPredecessor(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000817 return GetPredecessorIndexOf(predecessor) == idx;
818 }
819
David Brazdilffee3d32015-07-06 11:48:53 +0100820 // Returns the number of non-exceptional successors. SsaChecker ensures that
821 // these are stored at the beginning of the successor list.
822 size_t NumberOfNormalSuccessors() const {
Vladimir Marko91e11c02015-09-02 17:03:22 +0100823 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100824 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000825
826 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100827 // created, latter block. Note that this method will add the block to the
828 // graph, create a Goto at the end of the former block and will create an edge
829 // between the blocks. It will not, however, update the reverse post order or
830 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000831 HBasicBlock* SplitBefore(HInstruction* cursor);
832
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000833 // Split the block into two blocks just after `cursor`. Returns the newly
834 // created block. Note that this method just updates raw block information,
835 // like predecessors, successors, dominators, and instruction list. It does not
836 // update the graph, reverse post order, loop information, nor make sure the
837 // blocks are consistent (for example ending with a control flow instruction).
838 HBasicBlock* SplitAfter(HInstruction* cursor);
839
840 // Merge `other` at the end of `this`. Successors and dominated blocks of
841 // `other` are changed to be successors and dominated blocks of `this`. Note
842 // that this method does not update the graph, reverse post order, loop
843 // information, nor make sure the blocks are consistent (for example ending
844 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100845 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846
847 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
848 // of `this` are moved to `other`.
849 // Note that this method does not update the graph, reverse post order, loop
850 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000851 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852 void ReplaceWith(HBasicBlock* other);
853
David Brazdil2d7352b2015-04-20 14:52:42 +0100854 // Merge `other` at the end of `this`. This method updates loops, reverse post
855 // order, links to predecessors, successors, dominators and deletes the block
856 // from the graph. The two blocks must be successive, i.e. `this` the only
857 // predecessor of `other` and vice versa.
858 void MergeWith(HBasicBlock* other);
859
860 // Disconnects `this` from all its predecessors, successors and dominator,
861 // removes it from all loops it is included in and eventually from the graph.
862 // The block must not dominate any other block. Predecessors and successors
863 // are safely updated.
864 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000865
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100867 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100868 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100869 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100870 // Replace instruction `initial` with `replacement` within this block.
871 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
872 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100874 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000875 // RemoveInstruction and RemovePhi delete a given instruction from the respective
876 // instruction list. With 'ensure_safety' set to true, it verifies that the
877 // instruction is not in use and removes it from the use lists of its inputs.
878 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
879 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100880 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881
882 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100883 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 }
885
Roland Levillain6b879dd2014-09-22 17:13:44 +0100886 bool IsLoopPreHeaderFirstPredecessor() const {
887 DCHECK(IsLoopHeader());
Vladimir Marko91e11c02015-09-02 17:03:22 +0100888 return GetPredecessor(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100889 }
890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 HLoopInformation* GetLoopInformation() const {
892 return loop_information_;
893 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000897 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 void SetInLoop(HLoopInformation* info) {
899 if (IsLoopHeader()) {
900 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100901 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 loop_information_ = info;
903 } else if (loop_information_->Contains(*info->GetHeader())) {
904 // Block is currently part of an outer loop. Make it part of this inner loop.
905 // Note that a non loop header having a loop information means this loop information
906 // has already been populated
907 loop_information_ = info;
908 } else {
909 // Block is part of an inner loop. Do not update the loop information.
910 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
911 // at this point, because this method is being called while populating `info`.
912 }
913 }
914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 // Raw update of the loop information.
916 void SetLoopInformation(HLoopInformation* info) {
917 loop_information_ = info;
918 }
919
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100920 bool IsInLoop() const { return loop_information_ != nullptr; }
921
David Brazdilec16f792015-08-19 15:04:01 +0100922 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
923
924 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
925 try_catch_information_ = try_catch_information;
926 }
927
928 bool IsTryBlock() const {
929 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
930 }
931
932 bool IsCatchBlock() const {
933 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
934 }
David Brazdilffee3d32015-07-06 11:48:53 +0100935
936 // Returns the try entry that this block's successors should have. They will
937 // be in the same try, unless the block ends in a try boundary. In that case,
938 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100939 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100940
David Brazdila4b8c212015-05-07 09:59:30 +0100941 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 bool Dominates(HBasicBlock* block) const;
943
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100944 size_t GetLifetimeStart() const { return lifetime_start_; }
945 size_t GetLifetimeEnd() const { return lifetime_end_; }
946
947 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
948 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
949
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100950 uint32_t GetDexPc() const { return dex_pc_; }
951
David Brazdil8d5b8b22015-03-24 10:51:52 +0000952 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000953 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100954 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000955 bool HasSinglePhi() const;
956
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000958 HGraph* graph_;
Vladimir Marko91e11c02015-09-02 17:03:22 +0100959 ArenaVector<HBasicBlock*> predecessors_;
960 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100961 HInstructionList instructions_;
962 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000963 HLoopInformation* loop_information_;
964 HBasicBlock* dominator_;
Vladimir Marko91e11c02015-09-02 17:03:22 +0100965 ArenaVector<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100967 // The dex program counter of the first instruction of this block.
968 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100969 size_t lifetime_start_;
970 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100971 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100972
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000973 friend class HGraph;
974 friend class HInstruction;
975
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
977};
978
David Brazdilb2bd1c52015-03-25 11:17:37 +0000979// Iterates over the LoopInformation of all loops which contain 'block'
980// from the innermost to the outermost.
981class HLoopInformationOutwardIterator : public ValueObject {
982 public:
983 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
984 : current_(block.GetLoopInformation()) {}
985
986 bool Done() const { return current_ == nullptr; }
987
988 void Advance() {
989 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100990 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000991 }
992
993 HLoopInformation* Current() const {
994 DCHECK(!Done());
995 return current_;
996 }
997
998 private:
999 HLoopInformation* current_;
1000
1001 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1002};
1003
Alexandre Ramesef20f712015-06-09 10:29:30 +01001004#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001005 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001006 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001007 M(ArrayGet, Instruction) \
1008 M(ArrayLength, Instruction) \
1009 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001010 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001011 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001012 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001013 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001014 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001015 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001016 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001017 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001018 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001019 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001021 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001023 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001024 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001025 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(FloatConstant, Constant) \
1027 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028 M(GreaterThan, Condition) \
1029 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001030 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001031 M(InstanceFieldGet, Instruction) \
1032 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001033 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001034 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001035 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001036 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001037 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001038 M(LessThan, Condition) \
1039 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001040 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001041 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001043 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044 M(Local, Instruction) \
1045 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001046 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001047 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001048 M(Mul, BinaryOperation) \
1049 M(Neg, UnaryOperation) \
1050 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001051 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001052 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001053 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001054 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001055 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001056 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001060 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(Return, Instruction) \
1062 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001063 M(Shl, BinaryOperation) \
1064 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001065 M(StaticFieldGet, Instruction) \
1066 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001067 M(StoreLocal, Instruction) \
1068 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001069 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001070 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001071 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001072 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001073 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001074 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001075 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001076
Alexandre Ramesef20f712015-06-09 10:29:30 +01001077#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1078
1079#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1080
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001081#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1082
Alexandre Ramesef20f712015-06-09 10:29:30 +01001083#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1084
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:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001697 : previous_(nullptr),
1698 next_(nullptr),
1699 block_(nullptr),
1700 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001701 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001702 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001703 locations_(nullptr),
1704 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001705 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001706 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001707 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001708
Dave Allison20dfc792014-06-16 20:44:29 -07001709 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001710
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001711#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001712 enum InstructionKind {
1713 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1714 };
1715#undef DECLARE_KIND
1716
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001717 HInstruction* GetNext() const { return next_; }
1718 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001719
Calin Juravle77520bc2015-01-12 18:45:46 +00001720 HInstruction* GetNextDisregardingMoves() const;
1721 HInstruction* GetPreviousDisregardingMoves() const;
1722
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001723 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001724 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001725 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001726 bool IsInBlock() const { return block_ != nullptr; }
1727 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001728 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001729
Roland Levillain6b879dd2014-09-22 17:13:44 +01001730 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001731 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732
1733 virtual void Accept(HGraphVisitor* visitor) = 0;
1734 virtual const char* DebugName() const = 0;
1735
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001737 void SetRawInputAt(size_t index, HInstruction* input) {
1738 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1739 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001741 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001742 virtual uint32_t GetDexPc() const {
1743 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1744 " does not need an environment";
1745 UNREACHABLE();
1746 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001747 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001748
Roland Levillaine161a2a2014-10-03 12:45:18 +01001749 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001750 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001751
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001753 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001754
Calin Juravle10e244f2015-01-26 18:54:32 +00001755 // Does not apply for all instructions, but having this at top level greatly
1756 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001757 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001758 virtual bool CanBeNull() const {
1759 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1760 return true;
1761 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001762
Calin Juravle641547a2015-04-21 22:08:51 +01001763 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1764 UNUSED(obj);
1765 return false;
1766 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001767
Calin Juravle2e768302015-07-28 14:41:11 +00001768 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001769
Calin Juravle61d544b2015-02-23 16:46:57 +00001770 ReferenceTypeInfo GetReferenceTypeInfo() const {
1771 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1772 return reference_type_info_;
1773 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001774
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001775 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001776 DCHECK(user != nullptr);
1777 HUseListNode<HInstruction*>* use =
1778 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1779 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001780 }
1781
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001782 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001783 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001784 HUseListNode<HEnvironment*>* env_use =
1785 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1786 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787 }
1788
David Brazdil1abb4192015-02-17 18:33:36 +00001789 void RemoveAsUserOfInput(size_t input) {
1790 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1791 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1792 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001793
David Brazdil1abb4192015-02-17 18:33:36 +00001794 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1795 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796
David Brazdiled596192015-01-23 10:39:45 +00001797 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1798 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001799 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001800 bool HasOnlyOneNonEnvironmentUse() const {
1801 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1802 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001803
Roland Levillain6c82d402014-10-13 16:10:27 +01001804 // Does this instruction strictly dominate `other_instruction`?
1805 // Returns false if this instruction and `other_instruction` are the same.
1806 // Aborts if this instruction and `other_instruction` are both phis.
1807 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001808
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001809 int GetId() const { return id_; }
1810 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001812 int GetSsaIndex() const { return ssa_index_; }
1813 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1814 bool HasSsaIndex() const { return ssa_index_ != -1; }
1815
1816 bool HasEnvironment() const { return environment_ != nullptr; }
1817 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001818 // Set the `environment_` field. Raw because this method does not
1819 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001820 void SetRawEnvironment(HEnvironment* environment) {
1821 DCHECK(environment_ == nullptr);
1822 DCHECK_EQ(environment->GetHolder(), this);
1823 environment_ = environment;
1824 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001825
1826 // Set the environment of this instruction, copying it from `environment`. While
1827 // copying, the uses lists are being updated.
1828 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001829 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001830 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001831 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001832 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001833 if (environment->GetParent() != nullptr) {
1834 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1835 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001836 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001838 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1839 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001840 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001841 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001842 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001843 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001844 if (environment->GetParent() != nullptr) {
1845 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1846 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001847 }
1848
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 // Returns the number of entries in the environment. Typically, that is the
1850 // number of dex registers in a method. It could be more in case of inlining.
1851 size_t EnvironmentSize() const;
1852
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001853 LocationSummary* GetLocations() const { return locations_; }
1854 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001857 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858
Alexandre Rames188d4312015-04-09 18:30:21 +01001859 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1860 // uses of this instruction by `other` are *not* updated.
1861 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1862 ReplaceWith(other);
1863 other->ReplaceInput(this, use_index);
1864 }
1865
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001866 // Move `this` instruction before `cursor`.
1867 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001868
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001869#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001870 bool Is##type() const { return (As##type() != nullptr); } \
1871 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001872 virtual H##type* As##type() { return nullptr; }
1873
1874 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1875#undef INSTRUCTION_TYPE_CHECK
1876
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001877 // Returns whether the instruction can be moved within the graph.
1878 virtual bool CanBeMoved() const { return false; }
1879
1880 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001881 virtual bool InstructionTypeEquals(HInstruction* other) const {
1882 UNUSED(other);
1883 return false;
1884 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001885
1886 // Returns whether any data encoded in the two instructions is equal.
1887 // This method does not look at the inputs. Both instructions must be
1888 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001889 virtual bool InstructionDataEquals(HInstruction* other) const {
1890 UNUSED(other);
1891 return false;
1892 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001893
1894 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001895 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001896 // 2) Their inputs are identical.
1897 bool Equals(HInstruction* other) const;
1898
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001899 virtual InstructionKind GetKind() const = 0;
1900
1901 virtual size_t ComputeHashCode() const {
1902 size_t result = GetKind();
1903 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1904 result = (result * 31) + InputAt(i)->GetId();
1905 }
1906 return result;
1907 }
1908
1909 SideEffects GetSideEffects() const { return side_effects_; }
1910
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001911 size_t GetLifetimePosition() const { return lifetime_position_; }
1912 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1913 LiveInterval* GetLiveInterval() const { return live_interval_; }
1914 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1915 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1916
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001917 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1918
1919 // Returns whether the code generation of the instruction will require to have access
1920 // to the current method. Such instructions are:
1921 // (1): Instructions that require an environment, as calling the runtime requires
1922 // to walk the stack and have the current method stored at a specific stack address.
1923 // (2): Object literals like classes and strings, that are loaded from the dex cache
1924 // fields of the current method.
1925 bool NeedsCurrentMethod() const {
1926 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1927 }
1928
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001929 virtual bool NeedsDexCache() const { return false; }
1930
Mark Mendellc4701932015-04-10 13:18:51 -04001931 // Does this instruction have any use in an environment before
1932 // control flow hits 'other'?
1933 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1934
1935 // Remove all references to environment uses of this instruction.
1936 // The caller must ensure that this is safe to do.
1937 void RemoveEnvironmentUsers();
1938
David Brazdil1abb4192015-02-17 18:33:36 +00001939 protected:
1940 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1941 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1942
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001944 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1945
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001946 HInstruction* previous_;
1947 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001948 HBasicBlock* block_;
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:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002053 HTemplateInstruction<N>(SideEffects side_effects)
2054 : HInstruction(side_effects), 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:
2080 explicit HTemplateInstruction(SideEffects side_effects) : HInstruction(side_effects) {}
2081 virtual ~HTemplateInstruction() {}
2082
2083 size_t InputCount() const OVERRIDE { return 0; }
2084
2085 protected:
2086 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2087 LOG(FATAL) << "Unreachable";
2088 UNREACHABLE();
2089 }
2090
2091 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2092 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2093 LOG(FATAL) << "Unreachable";
2094 UNREACHABLE();
2095 }
2096
2097 private:
2098 friend class SsaBuilder;
2099};
2100
Dave Allison20dfc792014-06-16 20:44:29 -07002101template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002102class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002103 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002104 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2105 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002106 virtual ~HExpression() {}
2107
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002108 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002109
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002110 protected:
2111 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002112};
2113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2115// instruction that branches to the exit block.
2116class HReturnVoid : public HTemplateInstruction<0> {
2117 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002118 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002120 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002122 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002123
2124 private:
2125 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2126};
2127
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002128// Represents dex's RETURN opcodes. A HReturn is a control flow
2129// instruction that branches to the exit block.
2130class HReturn : public HTemplateInstruction<1> {
2131 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002132 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002133 SetRawInputAt(0, value);
2134 }
2135
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002136 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002138 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002139
2140 private:
2141 DISALLOW_COPY_AND_ASSIGN(HReturn);
2142};
2143
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002145// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146// exit block.
2147class HExit : public HTemplateInstruction<0> {
2148 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002149 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002150
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002151 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002153 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154
2155 private:
2156 DISALLOW_COPY_AND_ASSIGN(HExit);
2157};
2158
2159// Jumps from one block to another.
2160class HGoto : public HTemplateInstruction<0> {
2161 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2163
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002164 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002166 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002167 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002168 }
2169
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002170 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171
2172 private:
2173 DISALLOW_COPY_AND_ASSIGN(HGoto);
2174};
2175
Roland Levillain9867bc72015-08-05 10:21:34 +01002176class HConstant : public HExpression<0> {
2177 public:
2178 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2179
2180 bool CanBeMoved() const OVERRIDE { return true; }
2181
2182 virtual bool IsMinusOne() const { return false; }
2183 virtual bool IsZero() const { return false; }
2184 virtual bool IsOne() const { return false; }
2185
2186 DECLARE_INSTRUCTION(Constant);
2187
2188 private:
2189 DISALLOW_COPY_AND_ASSIGN(HConstant);
2190};
2191
2192class HNullConstant : public HConstant {
2193 public:
2194 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2195 return true;
2196 }
2197
2198 size_t ComputeHashCode() const OVERRIDE { return 0; }
2199
2200 DECLARE_INSTRUCTION(NullConstant);
2201
2202 private:
2203 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2204
2205 friend class HGraph;
2206 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2207};
2208
2209// Constants of the type int. Those can be from Dex instructions, or
2210// synthesized (for example with the if-eqz instruction).
2211class HIntConstant : public HConstant {
2212 public:
2213 int32_t GetValue() const { return value_; }
2214
2215 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2216 DCHECK(other->IsIntConstant());
2217 return other->AsIntConstant()->value_ == value_;
2218 }
2219
2220 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2221
2222 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2223 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2224 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2225
2226 DECLARE_INSTRUCTION(IntConstant);
2227
2228 private:
2229 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2230 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2231
2232 const int32_t value_;
2233
2234 friend class HGraph;
2235 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2236 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2237 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2238};
2239
2240class HLongConstant : public HConstant {
2241 public:
2242 int64_t GetValue() const { return value_; }
2243
2244 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2245 DCHECK(other->IsLongConstant());
2246 return other->AsLongConstant()->value_ == value_;
2247 }
2248
2249 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2250
2251 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2252 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2253 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2254
2255 DECLARE_INSTRUCTION(LongConstant);
2256
2257 private:
2258 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2259
2260 const int64_t value_;
2261
2262 friend class HGraph;
2263 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2264};
Dave Allison20dfc792014-06-16 20:44:29 -07002265
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002266// Conditional branch. A block ending with an HIf instruction must have
2267// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002268class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002269 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002270 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271 SetRawInputAt(0, input);
2272 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002273
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002274 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002276 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko91e11c02015-09-02 17:03:22 +01002277 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002278 }
2279
2280 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko91e11c02015-09-02 17:03:22 +01002281 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002282 }
2283
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002284 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002285
2286 private:
2287 DISALLOW_COPY_AND_ASSIGN(HIf);
2288};
2289
David Brazdilfc6a86a2015-06-26 10:33:45 +00002290
2291// Abstract instruction which marks the beginning and/or end of a try block and
2292// links it to the respective exception handlers. Behaves the same as a Goto in
2293// non-exceptional control flow.
2294// Normal-flow successor is stored at index zero, exception handlers under
2295// higher indices in no particular order.
2296class HTryBoundary : public HTemplateInstruction<0> {
2297 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002298 enum BoundaryKind {
2299 kEntry,
2300 kExit,
2301 };
2302
2303 explicit HTryBoundary(BoundaryKind kind)
2304 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002305
2306 bool IsControlFlow() const OVERRIDE { return true; }
2307
2308 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko91e11c02015-09-02 17:03:22 +01002309 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002310
2311 // Returns whether `handler` is among its exception handlers (non-zero index
2312 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002313 bool HasExceptionHandler(const HBasicBlock& handler) const {
2314 DCHECK(handler.IsCatchBlock());
Vladimir Marko91e11c02015-09-02 17:03:22 +01002315 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002316 }
2317
2318 // If not present already, adds `handler` to its block's list of exception
2319 // handlers.
2320 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002321 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002322 GetBlock()->AddSuccessor(handler);
2323 }
2324 }
2325
David Brazdil56e1acc2015-06-30 15:41:36 +01002326 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002327
David Brazdilffee3d32015-07-06 11:48:53 +01002328 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2329
David Brazdilfc6a86a2015-06-26 10:33:45 +00002330 DECLARE_INSTRUCTION(TryBoundary);
2331
2332 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002333 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002334
2335 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2336};
2337
David Brazdilffee3d32015-07-06 11:48:53 +01002338// Iterator over exception handlers of a given HTryBoundary, i.e. over
2339// exceptional successors of its basic block.
2340class HExceptionHandlerIterator : public ValueObject {
2341 public:
2342 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2343 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2344
Vladimir Marko91e11c02015-09-02 17:03:22 +01002345 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2346 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002347 size_t CurrentSuccessorIndex() const { return index_; }
2348 void Advance() { ++index_; }
2349
2350 private:
2351 const HBasicBlock& block_;
2352 size_t index_;
2353
2354 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2355};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002356
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002357// Deoptimize to interpreter, upon checking a condition.
2358class HDeoptimize : public HTemplateInstruction<1> {
2359 public:
2360 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2361 : HTemplateInstruction(SideEffects::None()),
2362 dex_pc_(dex_pc) {
2363 SetRawInputAt(0, cond);
2364 }
2365
2366 bool NeedsEnvironment() const OVERRIDE { return true; }
2367 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002368 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002369
2370 DECLARE_INSTRUCTION(Deoptimize);
2371
2372 private:
2373 uint32_t dex_pc_;
2374
2375 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2376};
2377
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002378// Represents the ArtMethod that was passed as a first argument to
2379// the method. It is used by instructions that depend on it, like
2380// instructions that work with the dex cache.
2381class HCurrentMethod : public HExpression<0> {
2382 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002383 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002384
2385 DECLARE_INSTRUCTION(CurrentMethod);
2386
2387 private:
2388 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2389};
2390
Roland Levillain88cb1752014-10-20 16:36:47 +01002391class HUnaryOperation : public HExpression<1> {
2392 public:
2393 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2394 : HExpression(result_type, SideEffects::None()) {
2395 SetRawInputAt(0, input);
2396 }
2397
2398 HInstruction* GetInput() const { return InputAt(0); }
2399 Primitive::Type GetResultType() const { return GetType(); }
2400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002401 bool CanBeMoved() const OVERRIDE { return true; }
2402 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002403 UNUSED(other);
2404 return true;
2405 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002406
Roland Levillain9240d6a2014-10-20 16:47:04 +01002407 // Try to statically evaluate `operation` and return a HConstant
2408 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002409 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002410 HConstant* TryStaticEvaluation() const;
2411
2412 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002413 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2414 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002415
Roland Levillain88cb1752014-10-20 16:36:47 +01002416 DECLARE_INSTRUCTION(UnaryOperation);
2417
2418 private:
2419 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2420};
2421
Dave Allison20dfc792014-06-16 20:44:29 -07002422class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002423 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002424 HBinaryOperation(Primitive::Type result_type,
2425 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002426 HInstruction* right,
2427 SideEffects side_effects = SideEffects::None())
2428 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002429 SetRawInputAt(0, left);
2430 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002431 }
2432
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002433 HInstruction* GetLeft() const { return InputAt(0); }
2434 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002435 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002436
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002437 virtual bool IsCommutative() const { return false; }
2438
2439 // Put constant on the right.
2440 // Returns whether order is changed.
2441 bool OrderInputsWithConstantOnTheRight() {
2442 HInstruction* left = InputAt(0);
2443 HInstruction* right = InputAt(1);
2444 if (left->IsConstant() && !right->IsConstant()) {
2445 ReplaceInput(right, 0);
2446 ReplaceInput(left, 1);
2447 return true;
2448 }
2449 return false;
2450 }
2451
2452 // Order inputs by instruction id, but favor constant on the right side.
2453 // This helps GVN for commutative ops.
2454 void OrderInputs() {
2455 DCHECK(IsCommutative());
2456 HInstruction* left = InputAt(0);
2457 HInstruction* right = InputAt(1);
2458 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2459 return;
2460 }
2461 if (OrderInputsWithConstantOnTheRight()) {
2462 return;
2463 }
2464 // Order according to instruction id.
2465 if (left->GetId() > right->GetId()) {
2466 ReplaceInput(right, 0);
2467 ReplaceInput(left, 1);
2468 }
2469 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002470
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002471 bool CanBeMoved() const OVERRIDE { return true; }
2472 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002473 UNUSED(other);
2474 return true;
2475 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002476
Roland Levillain9240d6a2014-10-20 16:47:04 +01002477 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002478 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002479 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002480 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002481
2482 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002483 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2484 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2485 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2486 HLongConstant* y ATTRIBUTE_UNUSED) const {
2487 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2488 return nullptr;
2489 }
2490 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2491 HIntConstant* y ATTRIBUTE_UNUSED) const {
2492 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2493 return nullptr;
2494 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002495
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002496 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002497 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002498 HConstant* GetConstantRight() const;
2499
2500 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002501 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002502 HInstruction* GetLeastConstantLeft() const;
2503
Roland Levillainccc07a92014-09-16 14:48:16 +01002504 DECLARE_INSTRUCTION(BinaryOperation);
2505
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002506 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002507 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2508};
2509
Mark Mendellc4701932015-04-10 13:18:51 -04002510// The comparison bias applies for floating point operations and indicates how NaN
2511// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002512enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002513 kNoBias, // bias is not applicable (i.e. for long operation)
2514 kGtBias, // return 1 for NaN comparisons
2515 kLtBias, // return -1 for NaN comparisons
2516};
2517
Dave Allison20dfc792014-06-16 20:44:29 -07002518class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002520 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002521 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002522 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002523 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002524
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002525 bool NeedsMaterialization() const { return needs_materialization_; }
2526 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002527
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002528 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002529 // `instruction`, and disregard moves in between.
2530 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002531
Dave Allison20dfc792014-06-16 20:44:29 -07002532 DECLARE_INSTRUCTION(Condition);
2533
2534 virtual IfCondition GetCondition() const = 0;
2535
Mark Mendellc4701932015-04-10 13:18:51 -04002536 virtual IfCondition GetOppositeCondition() const = 0;
2537
Roland Levillain4fa13f62015-07-06 18:11:54 +01002538 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002539
2540 void SetBias(ComparisonBias bias) { bias_ = bias; }
2541
2542 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2543 return bias_ == other->AsCondition()->bias_;
2544 }
2545
Roland Levillain4fa13f62015-07-06 18:11:54 +01002546 bool IsFPConditionTrueIfNaN() const {
2547 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2548 IfCondition if_cond = GetCondition();
2549 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2550 }
2551
2552 bool IsFPConditionFalseIfNaN() const {
2553 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2554 IfCondition if_cond = GetCondition();
2555 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2556 }
2557
Dave Allison20dfc792014-06-16 20:44:29 -07002558 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002559 // For register allocation purposes, returns whether this instruction needs to be
2560 // materialized (that is, not just be in the processor flags).
2561 bool needs_materialization_;
2562
Mark Mendellc4701932015-04-10 13:18:51 -04002563 // Needed if we merge a HCompare into a HCondition.
2564 ComparisonBias bias_;
2565
Dave Allison20dfc792014-06-16 20:44:29 -07002566 DISALLOW_COPY_AND_ASSIGN(HCondition);
2567};
2568
2569// Instruction to check if two inputs are equal to each other.
2570class HEqual : public HCondition {
2571 public:
2572 HEqual(HInstruction* first, HInstruction* second)
2573 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002574
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002575 bool IsCommutative() const OVERRIDE { return true; }
2576
Roland Levillain9867bc72015-08-05 10:21:34 +01002577 template <typename T> bool Compute(T x, T y) const { return x == y; }
2578
2579 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2580 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002581 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002582 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2583 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002584 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002585
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002586 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002587
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002588 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002589 return kCondEQ;
2590 }
2591
Mark Mendellc4701932015-04-10 13:18:51 -04002592 IfCondition GetOppositeCondition() const OVERRIDE {
2593 return kCondNE;
2594 }
2595
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002596 private:
2597 DISALLOW_COPY_AND_ASSIGN(HEqual);
2598};
2599
Dave Allison20dfc792014-06-16 20:44:29 -07002600class HNotEqual : public HCondition {
2601 public:
2602 HNotEqual(HInstruction* first, HInstruction* second)
2603 : HCondition(first, second) {}
2604
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002605 bool IsCommutative() const OVERRIDE { return true; }
2606
Roland Levillain9867bc72015-08-05 10:21:34 +01002607 template <typename T> bool Compute(T x, T y) const { return x != y; }
2608
2609 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2610 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002611 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002612 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2613 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002614 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002615
Dave Allison20dfc792014-06-16 20:44:29 -07002616 DECLARE_INSTRUCTION(NotEqual);
2617
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002618 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002619 return kCondNE;
2620 }
2621
Mark Mendellc4701932015-04-10 13:18:51 -04002622 IfCondition GetOppositeCondition() const OVERRIDE {
2623 return kCondEQ;
2624 }
2625
Dave Allison20dfc792014-06-16 20:44:29 -07002626 private:
2627 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2628};
2629
2630class HLessThan : public HCondition {
2631 public:
2632 HLessThan(HInstruction* first, HInstruction* second)
2633 : HCondition(first, second) {}
2634
Roland Levillain9867bc72015-08-05 10:21:34 +01002635 template <typename T> bool Compute(T x, T y) const { return x < y; }
2636
2637 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2638 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002639 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002640 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2641 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002642 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002643
Dave Allison20dfc792014-06-16 20:44:29 -07002644 DECLARE_INSTRUCTION(LessThan);
2645
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002646 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002647 return kCondLT;
2648 }
2649
Mark Mendellc4701932015-04-10 13:18:51 -04002650 IfCondition GetOppositeCondition() const OVERRIDE {
2651 return kCondGE;
2652 }
2653
Dave Allison20dfc792014-06-16 20:44:29 -07002654 private:
2655 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2656};
2657
2658class HLessThanOrEqual : public HCondition {
2659 public:
2660 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2661 : HCondition(first, second) {}
2662
Roland Levillain9867bc72015-08-05 10:21:34 +01002663 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2664
2665 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2666 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002667 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002668 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2669 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002670 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002671
Dave Allison20dfc792014-06-16 20:44:29 -07002672 DECLARE_INSTRUCTION(LessThanOrEqual);
2673
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002674 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002675 return kCondLE;
2676 }
2677
Mark Mendellc4701932015-04-10 13:18:51 -04002678 IfCondition GetOppositeCondition() const OVERRIDE {
2679 return kCondGT;
2680 }
2681
Dave Allison20dfc792014-06-16 20:44:29 -07002682 private:
2683 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2684};
2685
2686class HGreaterThan : public HCondition {
2687 public:
2688 HGreaterThan(HInstruction* first, HInstruction* second)
2689 : HCondition(first, second) {}
2690
Roland Levillain9867bc72015-08-05 10:21:34 +01002691 template <typename T> bool Compute(T x, T y) const { return x > y; }
2692
2693 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2694 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002695 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2697 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002699
Dave Allison20dfc792014-06-16 20:44:29 -07002700 DECLARE_INSTRUCTION(GreaterThan);
2701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002703 return kCondGT;
2704 }
2705
Mark Mendellc4701932015-04-10 13:18:51 -04002706 IfCondition GetOppositeCondition() const OVERRIDE {
2707 return kCondLE;
2708 }
2709
Dave Allison20dfc792014-06-16 20:44:29 -07002710 private:
2711 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2712};
2713
2714class HGreaterThanOrEqual : public HCondition {
2715 public:
2716 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2717 : HCondition(first, second) {}
2718
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2720
2721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2722 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002723 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2725 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002726 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002727
Dave Allison20dfc792014-06-16 20:44:29 -07002728 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2729
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002730 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002731 return kCondGE;
2732 }
2733
Mark Mendellc4701932015-04-10 13:18:51 -04002734 IfCondition GetOppositeCondition() const OVERRIDE {
2735 return kCondLT;
2736 }
2737
Dave Allison20dfc792014-06-16 20:44:29 -07002738 private:
2739 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2740};
2741
2742
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002743// Instruction to check how two inputs compare to each other.
2744// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2745class HCompare : public HBinaryOperation {
2746 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002747 HCompare(Primitive::Type type,
2748 HInstruction* first,
2749 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002750 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002751 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002752 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2753 bias_(bias),
2754 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002755 DCHECK_EQ(type, first->GetType());
2756 DCHECK_EQ(type, second->GetType());
2757 }
2758
Roland Levillain9867bc72015-08-05 10:21:34 +01002759 template <typename T>
2760 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002761
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2763 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2764 }
2765 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2766 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002767 }
2768
Calin Juravleddb7df22014-11-25 20:56:51 +00002769 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2770 return bias_ == other->AsCompare()->bias_;
2771 }
2772
Mark Mendellc4701932015-04-10 13:18:51 -04002773 ComparisonBias GetBias() const { return bias_; }
2774
Roland Levillain4fa13f62015-07-06 18:11:54 +01002775 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002776
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002777 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2778
2779 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2780 // MIPS64 uses a runtime call for FP comparisons.
2781 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2782 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002783
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002784 DECLARE_INSTRUCTION(Compare);
2785
2786 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002787 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002788 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002789
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002790 DISALLOW_COPY_AND_ASSIGN(HCompare);
2791};
2792
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002793// A local in the graph. Corresponds to a Dex register.
2794class HLocal : public HTemplateInstruction<0> {
2795 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002796 explicit HLocal(uint16_t reg_number)
2797 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002798
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002799 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002800
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002801 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002802
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002803 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002804 // The Dex register number.
2805 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002806
2807 DISALLOW_COPY_AND_ASSIGN(HLocal);
2808};
2809
2810// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002811class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002812 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002813 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002814 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002815 SetRawInputAt(0, local);
2816 }
2817
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002818 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2819
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002820 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002821
2822 private:
2823 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2824};
2825
2826// Store a value in a given local. This instruction has two inputs: the value
2827// and the local.
2828class HStoreLocal : public HTemplateInstruction<2> {
2829 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002830 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002831 SetRawInputAt(0, local);
2832 SetRawInputAt(1, value);
2833 }
2834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002835 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2836
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002837 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002838
2839 private:
2840 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2841};
2842
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002843class HFloatConstant : public HConstant {
2844 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002845 float GetValue() const { return value_; }
2846
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002847 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002848 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002849 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2850 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002851 }
2852
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002853 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002854
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002855 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002856 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002857 }
2858 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002859 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002860 }
2861 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002862 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2863 }
2864 bool IsNaN() const {
2865 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002866 }
2867
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002868 DECLARE_INSTRUCTION(FloatConstant);
2869
2870 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002871 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002872 explicit HFloatConstant(int32_t value)
2873 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002874
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002875 const float value_;
2876
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002877 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002878 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002879 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002880 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2881};
2882
2883class HDoubleConstant : public HConstant {
2884 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002885 double GetValue() const { return value_; }
2886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002887 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002888 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002889 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2890 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002891 }
2892
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002893 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002894
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002895 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002896 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002897 }
2898 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002899 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002900 }
2901 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002902 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2903 }
2904 bool IsNaN() const {
2905 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002906 }
2907
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002908 DECLARE_INSTRUCTION(DoubleConstant);
2909
2910 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002911 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002912 explicit HDoubleConstant(int64_t value)
2913 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002914
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002915 const double value_;
2916
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002917 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002918 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002919 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002920 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2921};
2922
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002923enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002924#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002925#include "intrinsics_list.h"
2926 kNone,
2927 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2928#undef INTRINSICS_LIST
2929#undef OPTIMIZING_INTRINSICS
2930};
2931std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2932
Agi Csaki05f20562015-08-19 14:58:14 -07002933enum IntrinsicNeedsEnvironmentOrCache {
2934 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2935 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002936};
2937
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002938class HInvoke : public HInstruction {
2939 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002940 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002941
2942 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2943 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002944 bool NeedsEnvironment() const OVERRIDE {
2945 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2946 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002947
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002948 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002949 SetRawInputAt(index, argument);
2950 }
2951
Roland Levillain3e3d7332015-04-28 11:00:54 +01002952 // Return the number of arguments. This number can be lower than
2953 // the number of inputs returned by InputCount(), as some invoke
2954 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2955 // inputs at the end of their list of inputs.
2956 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2957
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002958 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002959
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002960 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002961
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002962 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002963 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002964
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002965 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2966
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002967 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002968 return intrinsic_;
2969 }
2970
Agi Csaki05f20562015-08-19 14:58:14 -07002971 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002972 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07002973 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002974 }
2975
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002976 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002977 return GetEnvironment()->GetParent() != nullptr;
2978 }
2979
2980 bool CanThrow() const OVERRIDE { return true; }
2981
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002982 DECLARE_INSTRUCTION(Invoke);
2983
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002984 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002985 HInvoke(ArenaAllocator* arena,
2986 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002987 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002988 Primitive::Type return_type,
2989 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002990 uint32_t dex_method_index,
2991 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002992 : HInstruction(
2993 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002994 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002995 inputs_(arena, number_of_arguments),
2996 return_type_(return_type),
2997 dex_pc_(dex_pc),
2998 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002999 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003000 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003001 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003002 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
3003 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003004 }
3005
David Brazdil1abb4192015-02-17 18:33:36 +00003006 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3007 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3008 inputs_.Put(index, input);
3009 }
3010
Roland Levillain3e3d7332015-04-28 11:00:54 +01003011 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003012 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003013 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003014 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003015 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003016 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003017 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003018 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003019
3020 private:
3021 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3022};
3023
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003024class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003025 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003026 // Requirements of this method call regarding the class
3027 // initialization (clinit) check of its declaring class.
3028 enum class ClinitCheckRequirement {
3029 kNone, // Class already initialized.
3030 kExplicit, // Static call having explicit clinit check as last input.
3031 kImplicit, // Static call implicitly requiring a clinit check.
3032 };
3033
Vladimir Marko58155012015-08-19 12:49:41 +00003034 // Determines how to load the target ArtMethod*.
3035 enum class MethodLoadKind {
3036 // Use a String init ArtMethod* loaded from Thread entrypoints.
3037 kStringInit,
3038
3039 // Use the method's own ArtMethod* loaded by the register allocator.
3040 kRecursive,
3041
3042 // Use ArtMethod* at a known address, embed the direct address in the code.
3043 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3044 kDirectAddress,
3045
3046 // Use ArtMethod* at an address that will be known at link time, embed the direct
3047 // address in the code. If the image is relocatable, emit .patch_oat entry.
3048 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3049 // the image relocatable or not.
3050 kDirectAddressWithFixup,
3051
3052 // Load from resoved methods array in the dex cache using a PC-relative load.
3053 // Used when we need to use the dex cache, for example for invoke-static that
3054 // may cause class initialization (the entry may point to a resolution method),
3055 // and we know that we can access the dex cache arrays using a PC-relative load.
3056 kDexCachePcRelative,
3057
3058 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3059 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3060 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3061 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3062 kDexCacheViaMethod,
3063 };
3064
3065 // Determines the location of the code pointer.
3066 enum class CodePtrLocation {
3067 // Recursive call, use local PC-relative call instruction.
3068 kCallSelf,
3069
3070 // Use PC-relative call instruction patched at link time.
3071 // Used for calls within an oat file, boot->boot or app->app.
3072 kCallPCRelative,
3073
3074 // Call to a known target address, embed the direct address in code.
3075 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3076 kCallDirect,
3077
3078 // Call to a target address that will be known at link time, embed the direct
3079 // address in code. If the image is relocatable, emit .patch_oat entry.
3080 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3081 // the image relocatable or not.
3082 kCallDirectWithFixup,
3083
3084 // Use code pointer from the ArtMethod*.
3085 // Used when we don't know the target code. This is also the last-resort-kind used when
3086 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3087 kCallArtMethod,
3088 };
3089
3090 struct DispatchInfo {
3091 const MethodLoadKind method_load_kind;
3092 const CodePtrLocation code_ptr_location;
3093 // The method load data holds
3094 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3095 // Note that there are multiple string init methods, each having its own offset.
3096 // - the method address for kDirectAddress
3097 // - the dex cache arrays offset for kDexCachePcRel.
3098 const uint64_t method_load_data;
3099 const uint64_t direct_code_ptr;
3100 };
3101
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003102 HInvokeStaticOrDirect(ArenaAllocator* arena,
3103 uint32_t number_of_arguments,
3104 Primitive::Type return_type,
3105 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003106 uint32_t method_index,
3107 MethodReference target_method,
3108 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003109 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003110 InvokeType invoke_type,
3111 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003112 : HInvoke(arena,
3113 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003114 // There is one extra argument for the HCurrentMethod node, and
3115 // potentially one other if the clinit check is explicit, and one other
3116 // if the method is a string factory.
3117 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003118 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003119 return_type,
3120 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003121 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003122 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003123 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003124 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003125 target_method_(target_method),
3126 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003127
Calin Juravle641547a2015-04-21 22:08:51 +01003128 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3129 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003130 // We access the method via the dex cache so we can't do an implicit null check.
3131 // TODO: for intrinsics we can generate implicit null checks.
3132 return false;
3133 }
3134
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003135 bool CanBeNull() const OVERRIDE {
3136 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3137 }
3138
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003139 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003140 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3141 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3142 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003143 bool NeedsDexCache() const OVERRIDE {
3144 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3145 return !IsRecursive() && !IsStringInit();
3146 }
Vladimir Marko58155012015-08-19 12:49:41 +00003147 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003148 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003149 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3150 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3151 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3152 MethodReference GetTargetMethod() const { return target_method_; }
3153
3154 int32_t GetStringInitOffset() const {
3155 DCHECK(IsStringInit());
3156 return dispatch_info_.method_load_data;
3157 }
3158
3159 uint64_t GetMethodAddress() const {
3160 DCHECK(HasMethodAddress());
3161 return dispatch_info_.method_load_data;
3162 }
3163
3164 uint32_t GetDexCacheArrayOffset() const {
3165 DCHECK(HasPcRelDexCache());
3166 return dispatch_info_.method_load_data;
3167 }
3168
3169 uint64_t GetDirectCodePtr() const {
3170 DCHECK(HasDirectCodePtr());
3171 return dispatch_info_.direct_code_ptr;
3172 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003173
Calin Juravle68ad6492015-08-18 17:08:12 +01003174 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3175
Roland Levillain4c0eb422015-04-24 16:43:49 +01003176 // Is this instruction a call to a static method?
3177 bool IsStatic() const {
3178 return GetInvokeType() == kStatic;
3179 }
3180
Roland Levillain3e3d7332015-04-28 11:00:54 +01003181 // Remove the art::HLoadClass instruction set as last input by
3182 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3183 // the initial art::HClinitCheck instruction (only relevant for
3184 // static calls with explicit clinit check).
3185 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003186 DCHECK(IsStaticWithExplicitClinitCheck());
3187 size_t last_input_index = InputCount() - 1;
3188 HInstruction* last_input = InputAt(last_input_index);
3189 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003190 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003191 RemoveAsUserOfInput(last_input_index);
3192 inputs_.DeleteAt(last_input_index);
3193 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3194 DCHECK(IsStaticWithImplicitClinitCheck());
3195 }
3196
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003197 bool IsStringFactoryFor(HFakeString* str) const {
3198 if (!IsStringInit()) return false;
3199 // +1 for the current method.
3200 if (InputCount() == (number_of_arguments_ + 1)) return false;
3201 return InputAt(InputCount() - 1)->AsFakeString() == str;
3202 }
3203
3204 void RemoveFakeStringArgumentAsLastInput() {
3205 DCHECK(IsStringInit());
3206 size_t last_input_index = InputCount() - 1;
3207 HInstruction* last_input = InputAt(last_input_index);
3208 DCHECK(last_input != nullptr);
3209 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3210 RemoveAsUserOfInput(last_input_index);
3211 inputs_.DeleteAt(last_input_index);
3212 }
3213
Roland Levillain4c0eb422015-04-24 16:43:49 +01003214 // Is this a call to a static method whose declaring class has an
3215 // explicit intialization check in the graph?
3216 bool IsStaticWithExplicitClinitCheck() const {
3217 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3218 }
3219
3220 // Is this a call to a static method whose declaring class has an
3221 // implicit intialization check requirement?
3222 bool IsStaticWithImplicitClinitCheck() const {
3223 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3224 }
3225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003226 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003227
Roland Levillain4c0eb422015-04-24 16:43:49 +01003228 protected:
3229 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3230 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3231 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3232 HInstruction* input = input_record.GetInstruction();
3233 // `input` is the last input of a static invoke marked as having
3234 // an explicit clinit check. It must either be:
3235 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3236 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3237 DCHECK(input != nullptr);
3238 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3239 }
3240 return input_record;
3241 }
3242
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003243 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003244 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003245 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003246 // The target method may refer to different dex file or method index than the original
3247 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3248 // in derived class to increase visibility.
3249 MethodReference target_method_;
3250 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003252 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003253};
3254
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003255class HInvokeVirtual : public HInvoke {
3256 public:
3257 HInvokeVirtual(ArenaAllocator* arena,
3258 uint32_t number_of_arguments,
3259 Primitive::Type return_type,
3260 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003261 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003262 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003263 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003264 vtable_index_(vtable_index) {}
3265
Calin Juravle641547a2015-04-21 22:08:51 +01003266 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003267 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003268 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003269 }
3270
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003271 uint32_t GetVTableIndex() const { return vtable_index_; }
3272
3273 DECLARE_INSTRUCTION(InvokeVirtual);
3274
3275 private:
3276 const uint32_t vtable_index_;
3277
3278 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3279};
3280
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003281class HInvokeInterface : public HInvoke {
3282 public:
3283 HInvokeInterface(ArenaAllocator* arena,
3284 uint32_t number_of_arguments,
3285 Primitive::Type return_type,
3286 uint32_t dex_pc,
3287 uint32_t dex_method_index,
3288 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003289 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003290 imt_index_(imt_index) {}
3291
Calin Juravle641547a2015-04-21 22:08:51 +01003292 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003293 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003294 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003295 }
3296
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003297 uint32_t GetImtIndex() const { return imt_index_; }
3298 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3299
3300 DECLARE_INSTRUCTION(InvokeInterface);
3301
3302 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003303 const uint32_t imt_index_;
3304
3305 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3306};
3307
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003308class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003309 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003310 HNewInstance(HCurrentMethod* current_method,
3311 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003312 uint16_t type_index,
3313 const DexFile& dex_file,
3314 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003315 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003316 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003317 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003318 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003319 entrypoint_(entrypoint) {
3320 SetRawInputAt(0, current_method);
3321 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003322
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003323 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003324 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003325 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003326
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003327 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003328 bool NeedsEnvironment() const OVERRIDE { return true; }
3329 // It may throw when called on:
3330 // - interfaces
3331 // - abstract/innaccessible/unknown classes
3332 // TODO: optimize when possible.
3333 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003334
Calin Juravle10e244f2015-01-26 18:54:32 +00003335 bool CanBeNull() const OVERRIDE { return false; }
3336
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003337 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3338
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003339 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003340
3341 private:
3342 const uint32_t dex_pc_;
3343 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003344 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003345 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003346
3347 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3348};
3349
Roland Levillain88cb1752014-10-20 16:36:47 +01003350class HNeg : public HUnaryOperation {
3351 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003352 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003353 : HUnaryOperation(result_type, input) {}
3354
Roland Levillain9867bc72015-08-05 10:21:34 +01003355 template <typename T> T Compute(T x) const { return -x; }
3356
3357 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3358 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3359 }
3360 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3361 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3362 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003363
Roland Levillain88cb1752014-10-20 16:36:47 +01003364 DECLARE_INSTRUCTION(Neg);
3365
3366 private:
3367 DISALLOW_COPY_AND_ASSIGN(HNeg);
3368};
3369
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003370class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003371 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003372 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003373 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003374 uint32_t dex_pc,
3375 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003376 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003377 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003378 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003379 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003380 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003381 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003382 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003383 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003384 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003385 }
3386
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003387 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003388 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003389 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003390
3391 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003392 bool NeedsEnvironment() const OVERRIDE { return true; }
3393
Mingyao Yang0c365e62015-03-31 15:09:29 -07003394 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3395 bool CanThrow() const OVERRIDE { return true; }
3396
Calin Juravle10e244f2015-01-26 18:54:32 +00003397 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003398
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003399 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3400
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003401 DECLARE_INSTRUCTION(NewArray);
3402
3403 private:
3404 const uint32_t dex_pc_;
3405 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003406 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003407 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003408
3409 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3410};
3411
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003412class HAdd : public HBinaryOperation {
3413 public:
3414 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3415 : HBinaryOperation(result_type, left, right) {}
3416
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003417 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003418
Roland Levillain9867bc72015-08-05 10:21:34 +01003419 template <typename T> T Compute(T x, T y) const { return x + y; }
3420
3421 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3422 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003423 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003424 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3425 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003426 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003427
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003428 DECLARE_INSTRUCTION(Add);
3429
3430 private:
3431 DISALLOW_COPY_AND_ASSIGN(HAdd);
3432};
3433
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003434class HSub : public HBinaryOperation {
3435 public:
3436 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3437 : HBinaryOperation(result_type, left, right) {}
3438
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 {
3442 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003443 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003444 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3445 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003446 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003447
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003448 DECLARE_INSTRUCTION(Sub);
3449
3450 private:
3451 DISALLOW_COPY_AND_ASSIGN(HSub);
3452};
3453
Calin Juravle34bacdf2014-10-07 20:23:36 +01003454class HMul : public HBinaryOperation {
3455 public:
3456 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3457 : HBinaryOperation(result_type, left, right) {}
3458
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003459 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003460
Roland Levillain9867bc72015-08-05 10:21:34 +01003461 template <typename T> T Compute(T x, T y) const { return x * y; }
3462
3463 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3464 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3465 }
3466 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3467 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3468 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003469
3470 DECLARE_INSTRUCTION(Mul);
3471
3472 private:
3473 DISALLOW_COPY_AND_ASSIGN(HMul);
3474};
3475
Calin Juravle7c4954d2014-10-28 16:57:40 +00003476class HDiv : public HBinaryOperation {
3477 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003478 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003479 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3480 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003481
Roland Levillain9867bc72015-08-05 10:21:34 +01003482 template <typename T>
3483 T Compute(T x, T y) const {
3484 // Our graph structure ensures we never have 0 for `y` during
3485 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003486 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003487 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003488 return (y == -1) ? -x : x / y;
3489 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003490
Roland Levillain9867bc72015-08-05 10:21:34 +01003491 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3492 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3493 }
3494 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3495 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003498 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003499
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003500 static SideEffects SideEffectsForArchRuntimeCalls() {
3501 // The generated code can use a runtime call.
3502 return SideEffects::CanTriggerGC();
3503 }
3504
Calin Juravle7c4954d2014-10-28 16:57:40 +00003505 DECLARE_INSTRUCTION(Div);
3506
3507 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003508 const uint32_t dex_pc_;
3509
Calin Juravle7c4954d2014-10-28 16:57:40 +00003510 DISALLOW_COPY_AND_ASSIGN(HDiv);
3511};
3512
Calin Juravlebacfec32014-11-14 15:54:36 +00003513class HRem : public HBinaryOperation {
3514 public:
3515 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003516 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3517 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003518
Roland Levillain9867bc72015-08-05 10:21:34 +01003519 template <typename T>
3520 T Compute(T x, T y) const {
3521 // Our graph structure ensures we never have 0 for `y` during
3522 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003523 DCHECK_NE(y, 0);
3524 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3525 return (y == -1) ? 0 : x % y;
3526 }
3527
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3529 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3530 }
3531 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3532 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003533 }
3534
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003535 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003536
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003537 static SideEffects SideEffectsForArchRuntimeCalls() {
3538 return SideEffects::CanTriggerGC();
3539 }
3540
Calin Juravlebacfec32014-11-14 15:54:36 +00003541 DECLARE_INSTRUCTION(Rem);
3542
3543 private:
3544 const uint32_t dex_pc_;
3545
3546 DISALLOW_COPY_AND_ASSIGN(HRem);
3547};
3548
Calin Juravled0d48522014-11-04 16:40:20 +00003549class HDivZeroCheck : public HExpression<1> {
3550 public:
3551 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3552 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3553 SetRawInputAt(0, value);
3554 }
3555
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003556 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3557
Calin Juravled0d48522014-11-04 16:40:20 +00003558 bool CanBeMoved() const OVERRIDE { return true; }
3559
3560 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3561 UNUSED(other);
3562 return true;
3563 }
3564
3565 bool NeedsEnvironment() const OVERRIDE { return true; }
3566 bool CanThrow() const OVERRIDE { return true; }
3567
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003568 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003569
3570 DECLARE_INSTRUCTION(DivZeroCheck);
3571
3572 private:
3573 const uint32_t dex_pc_;
3574
3575 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3576};
3577
Calin Juravle9aec02f2014-11-18 23:06:35 +00003578class HShl : public HBinaryOperation {
3579 public:
3580 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3581 : HBinaryOperation(result_type, left, right) {}
3582
Roland Levillain9867bc72015-08-05 10:21:34 +01003583 template <typename T, typename U, typename V>
3584 T Compute(T x, U y, V max_shift_value) const {
3585 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3586 "V is not the unsigned integer type corresponding to T");
3587 return x << (y & max_shift_value);
3588 }
3589
3590 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3591 return GetBlock()->GetGraph()->GetIntConstant(
3592 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3593 }
3594 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3595 // case is handled as `x << static_cast<int>(y)`.
3596 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3597 return GetBlock()->GetGraph()->GetLongConstant(
3598 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3599 }
3600 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3601 return GetBlock()->GetGraph()->GetLongConstant(
3602 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3603 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003604
3605 DECLARE_INSTRUCTION(Shl);
3606
3607 private:
3608 DISALLOW_COPY_AND_ASSIGN(HShl);
3609};
3610
3611class HShr : public HBinaryOperation {
3612 public:
3613 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3614 : HBinaryOperation(result_type, left, right) {}
3615
Roland Levillain9867bc72015-08-05 10:21:34 +01003616 template <typename T, typename U, typename V>
3617 T Compute(T x, U y, V max_shift_value) const {
3618 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3619 "V is not the unsigned integer type corresponding to T");
3620 return x >> (y & max_shift_value);
3621 }
3622
3623 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3624 return GetBlock()->GetGraph()->GetIntConstant(
3625 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3626 }
3627 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3628 // case is handled as `x >> static_cast<int>(y)`.
3629 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3630 return GetBlock()->GetGraph()->GetLongConstant(
3631 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3632 }
3633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3634 return GetBlock()->GetGraph()->GetLongConstant(
3635 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3636 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003637
3638 DECLARE_INSTRUCTION(Shr);
3639
3640 private:
3641 DISALLOW_COPY_AND_ASSIGN(HShr);
3642};
3643
3644class HUShr : public HBinaryOperation {
3645 public:
3646 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3647 : HBinaryOperation(result_type, left, right) {}
3648
Roland Levillain9867bc72015-08-05 10:21:34 +01003649 template <typename T, typename U, typename V>
3650 T Compute(T x, U y, V max_shift_value) const {
3651 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3652 "V is not the unsigned integer type corresponding to T");
3653 V ux = static_cast<V>(x);
3654 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003655 }
3656
Roland Levillain9867bc72015-08-05 10:21:34 +01003657 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3658 return GetBlock()->GetGraph()->GetIntConstant(
3659 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3660 }
3661 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3662 // case is handled as `x >>> static_cast<int>(y)`.
3663 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3664 return GetBlock()->GetGraph()->GetLongConstant(
3665 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3666 }
3667 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3668 return GetBlock()->GetGraph()->GetLongConstant(
3669 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003670 }
3671
3672 DECLARE_INSTRUCTION(UShr);
3673
3674 private:
3675 DISALLOW_COPY_AND_ASSIGN(HUShr);
3676};
3677
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003678class HAnd : public HBinaryOperation {
3679 public:
3680 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3681 : HBinaryOperation(result_type, left, right) {}
3682
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003683 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003684
Roland Levillain9867bc72015-08-05 10:21:34 +01003685 template <typename T, typename U>
3686 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3687
3688 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3689 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3690 }
3691 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3692 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3693 }
3694 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3695 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3696 }
3697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3698 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3699 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003700
3701 DECLARE_INSTRUCTION(And);
3702
3703 private:
3704 DISALLOW_COPY_AND_ASSIGN(HAnd);
3705};
3706
3707class HOr : public HBinaryOperation {
3708 public:
3709 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3710 : HBinaryOperation(result_type, left, right) {}
3711
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003712 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003713
Roland Levillain9867bc72015-08-05 10:21:34 +01003714 template <typename T, typename U>
3715 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3716
3717 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3718 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3719 }
3720 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3721 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3722 }
3723 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3724 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3725 }
3726 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3727 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3728 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003729
3730 DECLARE_INSTRUCTION(Or);
3731
3732 private:
3733 DISALLOW_COPY_AND_ASSIGN(HOr);
3734};
3735
3736class HXor : public HBinaryOperation {
3737 public:
3738 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3739 : HBinaryOperation(result_type, left, right) {}
3740
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003741 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003742
Roland Levillain9867bc72015-08-05 10:21:34 +01003743 template <typename T, typename U>
3744 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3745
3746 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3747 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3748 }
3749 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3750 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3751 }
3752 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3753 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3754 }
3755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3756 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3757 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003758
3759 DECLARE_INSTRUCTION(Xor);
3760
3761 private:
3762 DISALLOW_COPY_AND_ASSIGN(HXor);
3763};
3764
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003765// The value of a parameter in this method. Its location depends on
3766// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003767class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003768 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003769 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003770 : HExpression(parameter_type, SideEffects::None()),
3771 index_(index),
3772 is_this_(is_this),
3773 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003774
3775 uint8_t GetIndex() const { return index_; }
3776
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003777 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3778 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003779
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003780 bool IsThis() const { return is_this_; }
3781
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003782 DECLARE_INSTRUCTION(ParameterValue);
3783
3784 private:
3785 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003786 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003787 const uint8_t index_;
3788
Calin Juravle10e244f2015-01-26 18:54:32 +00003789 // Whether or not the parameter value corresponds to 'this' argument.
3790 const bool is_this_;
3791
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003792 bool can_be_null_;
3793
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003794 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3795};
3796
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003797class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003798 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003799 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003800 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003801
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003802 bool CanBeMoved() const OVERRIDE { return true; }
3803 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003804 UNUSED(other);
3805 return true;
3806 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003807
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 template <typename T> T Compute(T x) const { return ~x; }
3809
3810 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3811 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3812 }
3813 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3814 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3815 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003816
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003817 DECLARE_INSTRUCTION(Not);
3818
3819 private:
3820 DISALLOW_COPY_AND_ASSIGN(HNot);
3821};
3822
David Brazdil66d126e2015-04-03 16:02:44 +01003823class HBooleanNot : public HUnaryOperation {
3824 public:
3825 explicit HBooleanNot(HInstruction* input)
3826 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3827
3828 bool CanBeMoved() const OVERRIDE { return true; }
3829 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3830 UNUSED(other);
3831 return true;
3832 }
3833
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003835 DCHECK(IsUint<1>(x));
3836 return !x;
3837 }
3838
Roland Levillain9867bc72015-08-05 10:21:34 +01003839 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3840 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3841 }
3842 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3843 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003844 UNREACHABLE();
3845 }
3846
3847 DECLARE_INSTRUCTION(BooleanNot);
3848
3849 private:
3850 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3851};
3852
Roland Levillaindff1f282014-11-05 14:15:05 +00003853class HTypeConversion : public HExpression<1> {
3854 public:
3855 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003856 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003857 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3858 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003859 SetRawInputAt(0, input);
3860 DCHECK_NE(input->GetType(), result_type);
3861 }
3862
3863 HInstruction* GetInput() const { return InputAt(0); }
3864 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3865 Primitive::Type GetResultType() const { return GetType(); }
3866
Roland Levillain624279f2014-12-04 11:54:28 +00003867 // Required by the x86 and ARM code generators when producing calls
3868 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003869 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003870
Roland Levillaindff1f282014-11-05 14:15:05 +00003871 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003872 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003873
Mark Mendelle82549b2015-05-06 10:55:34 -04003874 // Try to statically evaluate the conversion and return a HConstant
3875 // containing the result. If the input cannot be converted, return nullptr.
3876 HConstant* TryStaticEvaluation() const;
3877
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003878 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3879 Primitive::Type result_type) {
3880 // Some architectures may not require the 'GC' side effects, but at this point
3881 // in the compilation process we do not know what architecture we will
3882 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003883 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3884 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003885 return SideEffects::CanTriggerGC();
3886 }
3887 return SideEffects::None();
3888 }
3889
Roland Levillaindff1f282014-11-05 14:15:05 +00003890 DECLARE_INSTRUCTION(TypeConversion);
3891
3892 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003893 const uint32_t dex_pc_;
3894
Roland Levillaindff1f282014-11-05 14:15:05 +00003895 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3896};
3897
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003898static constexpr uint32_t kNoRegNumber = -1;
3899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003900class HPhi : public HInstruction {
3901 public:
3902 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003903 : HInstruction(SideEffects::None()),
3904 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003905 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003906 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003907 is_live_(false),
3908 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003909 inputs_.SetSize(number_of_inputs);
3910 }
3911
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003912 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3913 static Primitive::Type ToPhiType(Primitive::Type type) {
3914 switch (type) {
3915 case Primitive::kPrimBoolean:
3916 case Primitive::kPrimByte:
3917 case Primitive::kPrimShort:
3918 case Primitive::kPrimChar:
3919 return Primitive::kPrimInt;
3920 default:
3921 return type;
3922 }
3923 }
3924
David Brazdilffee3d32015-07-06 11:48:53 +01003925 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3926
Calin Juravle10e244f2015-01-26 18:54:32 +00003927 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003928
3929 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003930 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003931
Calin Juravle10e244f2015-01-26 18:54:32 +00003932 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003933 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003934
Calin Juravle10e244f2015-01-26 18:54:32 +00003935 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3936 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3937
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003938 uint32_t GetRegNumber() const { return reg_number_; }
3939
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003940 void SetDead() { is_live_ = false; }
3941 void SetLive() { is_live_ = true; }
3942 bool IsDead() const { return !is_live_; }
3943 bool IsLive() const { return is_live_; }
3944
Calin Juravlea4f88312015-04-16 12:57:19 +01003945 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3946 // An equivalent phi is a phi having the same dex register and type.
3947 // It assumes that phis with the same dex register are adjacent.
3948 HPhi* GetNextEquivalentPhiWithSameType() {
3949 HInstruction* next = GetNext();
3950 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3951 if (next->GetType() == GetType()) {
3952 return next->AsPhi();
3953 }
3954 next = next->GetNext();
3955 }
3956 return nullptr;
3957 }
3958
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003959 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003960
David Brazdil1abb4192015-02-17 18:33:36 +00003961 protected:
3962 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3963
3964 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3965 inputs_.Put(index, input);
3966 }
3967
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003968 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003969 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003970 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003971 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003972 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003973 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003974
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003975 DISALLOW_COPY_AND_ASSIGN(HPhi);
3976};
3977
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003978class HNullCheck : public HExpression<1> {
3979 public:
3980 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003981 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003982 SetRawInputAt(0, value);
3983 }
3984
Calin Juravle10e244f2015-01-26 18:54:32 +00003985 bool CanBeMoved() const OVERRIDE { return true; }
3986 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003987 UNUSED(other);
3988 return true;
3989 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003990
Calin Juravle10e244f2015-01-26 18:54:32 +00003991 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003992
Calin Juravle10e244f2015-01-26 18:54:32 +00003993 bool CanThrow() const OVERRIDE { return true; }
3994
3995 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003996
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003997 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003998
3999 DECLARE_INSTRUCTION(NullCheck);
4000
4001 private:
4002 const uint32_t dex_pc_;
4003
4004 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4005};
4006
4007class FieldInfo : public ValueObject {
4008 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004009 FieldInfo(MemberOffset field_offset,
4010 Primitive::Type field_type,
4011 bool is_volatile,
4012 uint32_t index,
4013 const DexFile& dex_file)
4014 : field_offset_(field_offset),
4015 field_type_(field_type),
4016 is_volatile_(is_volatile),
4017 index_(index),
4018 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004019
4020 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004021 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004022 uint32_t GetFieldIndex() const { return index_; }
4023 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004024 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004025
4026 private:
4027 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004028 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004029 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004030 uint32_t index_;
4031 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004032};
4033
4034class HInstanceFieldGet : public HExpression<1> {
4035 public:
4036 HInstanceFieldGet(HInstruction* value,
4037 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004038 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004039 bool is_volatile,
4040 uint32_t field_idx,
4041 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004042 : HExpression(
4043 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004044 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004045 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004046 SetRawInputAt(0, value);
4047 }
4048
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004049 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004050
4051 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4052 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4053 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004054 }
4055
Calin Juravle641547a2015-04-21 22:08:51 +01004056 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4057 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004058 }
4059
4060 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004061 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4062 }
4063
Calin Juravle52c48962014-12-16 17:02:57 +00004064 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004065 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004067 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068
4069 DECLARE_INSTRUCTION(InstanceFieldGet);
4070
4071 private:
4072 const FieldInfo field_info_;
4073
4074 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4075};
4076
4077class HInstanceFieldSet : public HTemplateInstruction<2> {
4078 public:
4079 HInstanceFieldSet(HInstruction* object,
4080 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004081 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004082 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004083 bool is_volatile,
4084 uint32_t field_idx,
4085 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004086 : HTemplateInstruction(
4087 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004088 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004089 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004090 SetRawInputAt(0, object);
4091 SetRawInputAt(1, value);
4092 }
4093
Calin Juravle641547a2015-04-21 22:08:51 +01004094 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4095 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004096 }
4097
Calin Juravle52c48962014-12-16 17:02:57 +00004098 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004099 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004100 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004101 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004102 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004103 bool GetValueCanBeNull() const { return value_can_be_null_; }
4104 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004105
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004106 DECLARE_INSTRUCTION(InstanceFieldSet);
4107
4108 private:
4109 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004110 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111
4112 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4113};
4114
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115class HArrayGet : public HExpression<2> {
4116 public:
4117 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004118 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004119 SetRawInputAt(0, array);
4120 SetRawInputAt(1, index);
4121 }
4122
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004123 bool CanBeMoved() const OVERRIDE { return true; }
4124 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004125 UNUSED(other);
4126 return true;
4127 }
Calin Juravle641547a2015-04-21 22:08:51 +01004128 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4129 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004130 // TODO: We can be smarter here.
4131 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4132 // which generates the implicit null check. There are cases when these can be removed
4133 // to produce better code. If we ever add optimizations to do so we should allow an
4134 // implicit check here (as long as the address falls in the first page).
4135 return false;
4136 }
4137
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004138 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004139
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004140 HInstruction* GetArray() const { return InputAt(0); }
4141 HInstruction* GetIndex() const { return InputAt(1); }
4142
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143 DECLARE_INSTRUCTION(ArrayGet);
4144
4145 private:
4146 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4147};
4148
4149class HArraySet : public HTemplateInstruction<3> {
4150 public:
4151 HArraySet(HInstruction* array,
4152 HInstruction* index,
4153 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004154 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004155 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004156 : HTemplateInstruction(
4157 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4158 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004159 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004160 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004161 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4162 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004163 SetRawInputAt(0, array);
4164 SetRawInputAt(1, index);
4165 SetRawInputAt(2, value);
4166 }
4167
Calin Juravle77520bc2015-01-12 18:45:46 +00004168 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004169 // We currently always call a runtime method to catch array store
4170 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004171 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172 }
4173
Mingyao Yang81014cb2015-06-02 03:16:27 -07004174 // Can throw ArrayStoreException.
4175 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4176
Calin Juravle641547a2015-04-21 22:08:51 +01004177 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4178 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004179 // TODO: Same as for ArrayGet.
4180 return false;
4181 }
4182
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004183 void ClearNeedsTypeCheck() {
4184 needs_type_check_ = false;
4185 }
4186
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004187 void ClearValueCanBeNull() {
4188 value_can_be_null_ = false;
4189 }
4190
4191 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004192 bool NeedsTypeCheck() const { return needs_type_check_; }
4193
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004194 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004195
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004196 HInstruction* GetArray() const { return InputAt(0); }
4197 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004198 HInstruction* GetValue() const { return InputAt(2); }
4199
4200 Primitive::Type GetComponentType() const {
4201 // The Dex format does not type floating point index operations. Since the
4202 // `expected_component_type_` is set during building and can therefore not
4203 // be correct, we also check what is the value type. If it is a floating
4204 // point type, we must use that type.
4205 Primitive::Type value_type = GetValue()->GetType();
4206 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4207 ? value_type
4208 : expected_component_type_;
4209 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004210
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004211 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4212 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4213 }
4214
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004215 DECLARE_INSTRUCTION(ArraySet);
4216
4217 private:
4218 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004219 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004220 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004221 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004222
4223 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4224};
4225
4226class HArrayLength : public HExpression<1> {
4227 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004228 explicit HArrayLength(HInstruction* array)
4229 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4230 // Note that arrays do not change length, so the instruction does not
4231 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 SetRawInputAt(0, array);
4233 }
4234
Calin Juravle77520bc2015-01-12 18:45:46 +00004235 bool CanBeMoved() const OVERRIDE { return true; }
4236 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004237 UNUSED(other);
4238 return true;
4239 }
Calin Juravle641547a2015-04-21 22:08:51 +01004240 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4241 return obj == InputAt(0);
4242 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004243
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004244 DECLARE_INSTRUCTION(ArrayLength);
4245
4246 private:
4247 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4248};
4249
4250class HBoundsCheck : public HExpression<2> {
4251 public:
4252 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004253 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004254 DCHECK(index->GetType() == Primitive::kPrimInt);
4255 SetRawInputAt(0, index);
4256 SetRawInputAt(1, length);
4257 }
4258
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004259 bool CanBeMoved() const OVERRIDE { return true; }
4260 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004261 UNUSED(other);
4262 return true;
4263 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004265 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004267 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004268
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004269 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004270
4271 DECLARE_INSTRUCTION(BoundsCheck);
4272
4273 private:
4274 const uint32_t dex_pc_;
4275
4276 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4277};
4278
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279/**
4280 * Some DEX instructions are folded into multiple HInstructions that need
4281 * to stay live until the last HInstruction. This class
4282 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004283 * HInstruction stays live. `index` represents the stack location index of the
4284 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285 */
4286class HTemporary : public HTemplateInstruction<0> {
4287 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004288 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289
4290 size_t GetIndex() const { return index_; }
4291
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004292 Primitive::Type GetType() const OVERRIDE {
4293 // The previous instruction is the one that will be stored in the temporary location.
4294 DCHECK(GetPrevious() != nullptr);
4295 return GetPrevious()->GetType();
4296 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004297
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004298 DECLARE_INSTRUCTION(Temporary);
4299
4300 private:
4301 const size_t index_;
4302
4303 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4304};
4305
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004306class HSuspendCheck : public HTemplateInstruction<0> {
4307 public:
4308 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004309 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004310
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004311 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004312 return true;
4313 }
4314
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004315 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004316 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4317 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004318
4319 DECLARE_INSTRUCTION(SuspendCheck);
4320
4321 private:
4322 const uint32_t dex_pc_;
4323
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004324 // Only used for code generation, in order to share the same slow path between back edges
4325 // of a same loop.
4326 SlowPathCode* slow_path_;
4327
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004328 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4329};
4330
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004331/**
4332 * Instruction to load a Class object.
4333 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004334class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004335 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004336 HLoadClass(HCurrentMethod* current_method,
4337 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004338 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004339 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004340 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004341 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004342 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004343 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004344 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004345 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004346 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004347 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004348 SetRawInputAt(0, current_method);
4349 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004350
4351 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004352
4353 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4354 return other->AsLoadClass()->type_index_ == type_index_;
4355 }
4356
4357 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4358
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004359 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004360 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004361 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004362 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004363
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004364 bool NeedsEnvironment() const OVERRIDE {
4365 // Will call runtime and load the class if the class is not loaded yet.
4366 // TODO: finer grain decision.
4367 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004368 }
4369
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004370 bool MustGenerateClinitCheck() const {
4371 return generate_clinit_check_;
4372 }
4373
Calin Juravle0ba218d2015-05-19 18:46:01 +01004374 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4375 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004376 }
4377
4378 bool CanCallRuntime() const {
4379 return MustGenerateClinitCheck() || !is_referrers_class_;
4380 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004381
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004382 bool CanThrow() const OVERRIDE {
4383 // May call runtime and and therefore can throw.
4384 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004385 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004386 }
4387
Calin Juravleacf735c2015-02-12 15:25:22 +00004388 ReferenceTypeInfo GetLoadedClassRTI() {
4389 return loaded_class_rti_;
4390 }
4391
4392 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4393 // Make sure we only set exact types (the loaded class should never be merged).
4394 DCHECK(rti.IsExact());
4395 loaded_class_rti_ = rti;
4396 }
4397
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004398 const DexFile& GetDexFile() { return dex_file_; }
4399
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004400 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4401
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004402 static SideEffects SideEffectsForArchRuntimeCalls() {
4403 return SideEffects::CanTriggerGC();
4404 }
4405
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004406 DECLARE_INSTRUCTION(LoadClass);
4407
4408 private:
4409 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004410 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004411 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004412 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004413 // Whether this instruction must generate the initialization check.
4414 // Used for code generation.
4415 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004416
Calin Juravleacf735c2015-02-12 15:25:22 +00004417 ReferenceTypeInfo loaded_class_rti_;
4418
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004419 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4420};
4421
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004422class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004423 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004424 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004425 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004426 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004427 dex_pc_(dex_pc) {
4428 SetRawInputAt(0, current_method);
4429 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004430
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004431 bool CanBeMoved() const OVERRIDE { return true; }
4432
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004433 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4434 return other->AsLoadString()->string_index_ == string_index_;
4435 }
4436
4437 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4438
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004439 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004440 uint32_t GetStringIndex() const { return string_index_; }
4441
4442 // TODO: Can we deopt or debug when we resolve a string?
4443 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004444 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004445 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004446
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004447 static SideEffects SideEffectsForArchRuntimeCalls() {
4448 return SideEffects::CanTriggerGC();
4449 }
4450
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004451 DECLARE_INSTRUCTION(LoadString);
4452
4453 private:
4454 const uint32_t string_index_;
4455 const uint32_t dex_pc_;
4456
4457 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4458};
4459
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004460/**
4461 * Performs an initialization check on its Class object input.
4462 */
4463class HClinitCheck : public HExpression<1> {
4464 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004465 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004466 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004467 Primitive::kPrimNot,
4468 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004469 dex_pc_(dex_pc) {
4470 SetRawInputAt(0, constant);
4471 }
4472
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004473 bool CanBeMoved() const OVERRIDE { return true; }
4474 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4475 UNUSED(other);
4476 return true;
4477 }
4478
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004479 bool NeedsEnvironment() const OVERRIDE {
4480 // May call runtime to initialize the class.
4481 return true;
4482 }
4483
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004484 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004485
4486 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4487
4488 DECLARE_INSTRUCTION(ClinitCheck);
4489
4490 private:
4491 const uint32_t dex_pc_;
4492
4493 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4494};
4495
4496class HStaticFieldGet : public HExpression<1> {
4497 public:
4498 HStaticFieldGet(HInstruction* cls,
4499 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004500 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004501 bool is_volatile,
4502 uint32_t field_idx,
4503 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004504 : HExpression(
4505 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004506 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004507 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004508 SetRawInputAt(0, cls);
4509 }
4510
Calin Juravle52c48962014-12-16 17:02:57 +00004511
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004512 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004513
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004514 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004515 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4516 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004517 }
4518
4519 size_t ComputeHashCode() const OVERRIDE {
4520 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4521 }
4522
Calin Juravle52c48962014-12-16 17:02:57 +00004523 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004524 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4525 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004526 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004527
4528 DECLARE_INSTRUCTION(StaticFieldGet);
4529
4530 private:
4531 const FieldInfo field_info_;
4532
4533 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4534};
4535
4536class HStaticFieldSet : public HTemplateInstruction<2> {
4537 public:
4538 HStaticFieldSet(HInstruction* cls,
4539 HInstruction* value,
4540 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004541 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004542 bool is_volatile,
4543 uint32_t field_idx,
4544 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004545 : HTemplateInstruction(
4546 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004547 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004548 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004549 SetRawInputAt(0, cls);
4550 SetRawInputAt(1, value);
4551 }
4552
Calin Juravle52c48962014-12-16 17:02:57 +00004553 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004554 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4555 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004556 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004557
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004558 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004559 bool GetValueCanBeNull() const { return value_can_be_null_; }
4560 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004561
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004562 DECLARE_INSTRUCTION(StaticFieldSet);
4563
4564 private:
4565 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004566 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004567
4568 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4569};
4570
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004571// Implement the move-exception DEX instruction.
4572class HLoadException : public HExpression<0> {
4573 public:
4574 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4575
David Brazdilbbd733e2015-08-18 17:48:17 +01004576 bool CanBeNull() const OVERRIDE { return false; }
4577
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004578 DECLARE_INSTRUCTION(LoadException);
4579
4580 private:
4581 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4582};
4583
David Brazdilcb1c0552015-08-04 16:22:25 +01004584// Implicit part of move-exception which clears thread-local exception storage.
4585// Must not be removed because the runtime expects the TLS to get cleared.
4586class HClearException : public HTemplateInstruction<0> {
4587 public:
4588 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4589
4590 DECLARE_INSTRUCTION(ClearException);
4591
4592 private:
4593 DISALLOW_COPY_AND_ASSIGN(HClearException);
4594};
4595
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004596class HThrow : public HTemplateInstruction<1> {
4597 public:
4598 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004599 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004600 SetRawInputAt(0, exception);
4601 }
4602
4603 bool IsControlFlow() const OVERRIDE { return true; }
4604
4605 bool NeedsEnvironment() const OVERRIDE { return true; }
4606
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004607 bool CanThrow() const OVERRIDE { return true; }
4608
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004609 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004610
4611 DECLARE_INSTRUCTION(Throw);
4612
4613 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004614 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004615
4616 DISALLOW_COPY_AND_ASSIGN(HThrow);
4617};
4618
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004619class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004620 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004621 HInstanceOf(HInstruction* object,
4622 HLoadClass* constant,
4623 bool class_is_final,
4624 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004625 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004626 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004627 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004628 dex_pc_(dex_pc) {
4629 SetRawInputAt(0, object);
4630 SetRawInputAt(1, constant);
4631 }
4632
4633 bool CanBeMoved() const OVERRIDE { return true; }
4634
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004635 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004636 return true;
4637 }
4638
4639 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004640 return false;
4641 }
4642
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004643 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004644
4645 bool IsClassFinal() const { return class_is_final_; }
4646
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004647 // Used only in code generation.
4648 bool MustDoNullCheck() const { return must_do_null_check_; }
4649 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4650
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004651 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4652 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4653 }
4654
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004655 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004656
4657 private:
4658 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004659 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004660 const uint32_t dex_pc_;
4661
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004662 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4663};
4664
Calin Juravleb1498f62015-02-16 13:13:29 +00004665class HBoundType : public HExpression<1> {
4666 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004667 // Constructs an HBoundType with the given upper_bound.
4668 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004669 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004670 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004671 upper_bound_(upper_bound),
4672 upper_can_be_null_(upper_can_be_null),
4673 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004674 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004675 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004676 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004677 }
4678
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004679 // GetUpper* should only be used in reference type propagation.
4680 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4681 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004682
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004683 void SetCanBeNull(bool can_be_null) {
4684 DCHECK(upper_can_be_null_ || !can_be_null);
4685 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004686 }
4687
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004688 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4689
Calin Juravleb1498f62015-02-16 13:13:29 +00004690 DECLARE_INSTRUCTION(BoundType);
4691
4692 private:
4693 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004694 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4695 // It is used to bound the type in cases like:
4696 // if (x instanceof ClassX) {
4697 // // uper_bound_ will be ClassX
4698 // }
4699 const ReferenceTypeInfo upper_bound_;
4700 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4701 // is false then can_be_null_ cannot be true).
4702 const bool upper_can_be_null_;
4703 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004704
4705 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4706};
4707
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004708class HCheckCast : public HTemplateInstruction<2> {
4709 public:
4710 HCheckCast(HInstruction* object,
4711 HLoadClass* constant,
4712 bool class_is_final,
4713 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004714 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004715 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004716 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004717 dex_pc_(dex_pc) {
4718 SetRawInputAt(0, object);
4719 SetRawInputAt(1, constant);
4720 }
4721
4722 bool CanBeMoved() const OVERRIDE { return true; }
4723
4724 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4725 return true;
4726 }
4727
4728 bool NeedsEnvironment() const OVERRIDE {
4729 // Instruction may throw a CheckCastError.
4730 return true;
4731 }
4732
4733 bool CanThrow() const OVERRIDE { return true; }
4734
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004735 bool MustDoNullCheck() const { return must_do_null_check_; }
4736 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4737
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004738 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004739
4740 bool IsClassFinal() const { return class_is_final_; }
4741
4742 DECLARE_INSTRUCTION(CheckCast);
4743
4744 private:
4745 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004746 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004747 const uint32_t dex_pc_;
4748
4749 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004750};
4751
Calin Juravle27df7582015-04-17 19:12:31 +01004752class HMemoryBarrier : public HTemplateInstruction<0> {
4753 public:
4754 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004755 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004756 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004757 barrier_kind_(barrier_kind) {}
4758
4759 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4760
4761 DECLARE_INSTRUCTION(MemoryBarrier);
4762
4763 private:
4764 const MemBarrierKind barrier_kind_;
4765
4766 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4767};
4768
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004769class HMonitorOperation : public HTemplateInstruction<1> {
4770 public:
4771 enum OperationKind {
4772 kEnter,
4773 kExit,
4774 };
4775
4776 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004777 : HTemplateInstruction(
4778 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004779 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004780 SetRawInputAt(0, object);
4781 }
4782
4783 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004784 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4785
4786 bool CanThrow() const OVERRIDE {
4787 // Verifier guarantees that monitor-exit cannot throw.
4788 // This is important because it allows the HGraphBuilder to remove
4789 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4790 return IsEnter();
4791 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004793 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004794
4795 bool IsEnter() const { return kind_ == kEnter; }
4796
4797 DECLARE_INSTRUCTION(MonitorOperation);
4798
Calin Juravle52c48962014-12-16 17:02:57 +00004799 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004800 const OperationKind kind_;
4801 const uint32_t dex_pc_;
4802
4803 private:
4804 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4805};
4806
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004807/**
4808 * A HInstruction used as a marker for the replacement of new + <init>
4809 * of a String to a call to a StringFactory. Only baseline will see
4810 * the node at code generation, where it will be be treated as null.
4811 * When compiling non-baseline, `HFakeString` instructions are being removed
4812 * in the instruction simplifier.
4813 */
4814class HFakeString : public HTemplateInstruction<0> {
4815 public:
4816 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4817
4818 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4819
4820 DECLARE_INSTRUCTION(FakeString);
4821
4822 private:
4823 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4824};
4825
Vladimir Markof9f64412015-09-02 14:05:49 +01004826class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004827 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004828 MoveOperands(Location source,
4829 Location destination,
4830 Primitive::Type type,
4831 HInstruction* instruction)
4832 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004833
4834 Location GetSource() const { return source_; }
4835 Location GetDestination() const { return destination_; }
4836
4837 void SetSource(Location value) { source_ = value; }
4838 void SetDestination(Location value) { destination_ = value; }
4839
4840 // The parallel move resolver marks moves as "in-progress" by clearing the
4841 // destination (but not the source).
4842 Location MarkPending() {
4843 DCHECK(!IsPending());
4844 Location dest = destination_;
4845 destination_ = Location::NoLocation();
4846 return dest;
4847 }
4848
4849 void ClearPending(Location dest) {
4850 DCHECK(IsPending());
4851 destination_ = dest;
4852 }
4853
4854 bool IsPending() const {
4855 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4856 return destination_.IsInvalid() && !source_.IsInvalid();
4857 }
4858
4859 // True if this blocks a move from the given location.
4860 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004861 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004862 }
4863
4864 // A move is redundant if it's been eliminated, if its source and
4865 // destination are the same, or if its destination is unneeded.
4866 bool IsRedundant() const {
4867 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4868 }
4869
4870 // We clear both operands to indicate move that's been eliminated.
4871 void Eliminate() {
4872 source_ = destination_ = Location::NoLocation();
4873 }
4874
4875 bool IsEliminated() const {
4876 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4877 return source_.IsInvalid();
4878 }
4879
Alexey Frunze4dda3372015-06-01 18:31:49 -07004880 Primitive::Type GetType() const { return type_; }
4881
Nicolas Geoffray90218252015-04-15 11:56:51 +01004882 bool Is64BitMove() const {
4883 return Primitive::Is64BitType(type_);
4884 }
4885
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004886 HInstruction* GetInstruction() const { return instruction_; }
4887
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004888 private:
4889 Location source_;
4890 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004891 // The type this move is for.
4892 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004893 // The instruction this move is assocatied with. Null when this move is
4894 // for moving an input in the expected locations of user (including a phi user).
4895 // This is only used in debug mode, to ensure we do not connect interval siblings
4896 // in the same parallel move.
4897 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004898};
4899
4900static constexpr size_t kDefaultNumberOfMoves = 4;
4901
4902class HParallelMove : public HTemplateInstruction<0> {
4903 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004904 explicit HParallelMove(ArenaAllocator* arena)
4905 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004906
Nicolas Geoffray90218252015-04-15 11:56:51 +01004907 void AddMove(Location source,
4908 Location destination,
4909 Primitive::Type type,
4910 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004911 DCHECK(source.IsValid());
4912 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004913 if (kIsDebugBuild) {
4914 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004915 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004916 if (moves_.Get(i).GetInstruction() == instruction) {
4917 // Special case the situation where the move is for the spill slot
4918 // of the instruction.
4919 if ((GetPrevious() == instruction)
4920 || ((GetPrevious() == nullptr)
4921 && instruction->IsPhi()
4922 && instruction->GetBlock() == GetBlock())) {
4923 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4924 << "Doing parallel moves for the same instruction.";
4925 } else {
4926 DCHECK(false) << "Doing parallel moves for the same instruction.";
4927 }
4928 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004929 }
4930 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004931 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004932 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004933 << "Overlapped destination for two moves in a parallel move: "
4934 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4935 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004936 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004937 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004938 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004939 }
4940
4941 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004942 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004943 }
4944
4945 size_t NumMoves() const { return moves_.Size(); }
4946
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004947 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004948
4949 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004950 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004951
4952 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4953};
4954
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004955class HGraphVisitor : public ValueObject {
4956 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004957 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4958 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004959
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004960 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004961 virtual void VisitBasicBlock(HBasicBlock* block);
4962
Roland Levillain633021e2014-10-01 14:12:25 +01004963 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004964 void VisitInsertionOrder();
4965
Roland Levillain633021e2014-10-01 14:12:25 +01004966 // Visit the graph following dominator tree reverse post-order.
4967 void VisitReversePostOrder();
4968
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004969 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004970
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004971 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004972#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004973 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4974
4975 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4976
4977#undef DECLARE_VISIT_INSTRUCTION
4978
4979 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004980 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004981
4982 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4983};
4984
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004985class HGraphDelegateVisitor : public HGraphVisitor {
4986 public:
4987 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4988 virtual ~HGraphDelegateVisitor() {}
4989
4990 // Visit functions that delegate to to super class.
4991#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004992 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004993
4994 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4995
4996#undef DECLARE_VISIT_INSTRUCTION
4997
4998 private:
4999 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5000};
5001
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005002class HInsertionOrderIterator : public ValueObject {
5003 public:
5004 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5005
5006 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5007 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5008 void Advance() { ++index_; }
5009
5010 private:
5011 const HGraph& graph_;
5012 size_t index_;
5013
5014 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5015};
5016
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005017class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005018 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005019 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5020 // Check that reverse post order of the graph has been built.
5021 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5022 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005023
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005024 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5025 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005026 void Advance() { ++index_; }
5027
5028 private:
5029 const HGraph& graph_;
5030 size_t index_;
5031
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005032 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005033};
5034
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005035class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005036 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005037 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005038 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5039 // Check that reverse post order of the graph has been built.
5040 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5041 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005042
5043 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005044 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005045 void Advance() { --index_; }
5046
5047 private:
5048 const HGraph& graph_;
5049 size_t index_;
5050
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005051 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005052};
5053
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005054class HLinearPostOrderIterator : public ValueObject {
5055 public:
5056 explicit HLinearPostOrderIterator(const HGraph& graph)
5057 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5058
5059 bool Done() const { return index_ == 0; }
5060
5061 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5062
5063 void Advance() {
5064 --index_;
5065 DCHECK_GE(index_, 0U);
5066 }
5067
5068 private:
5069 const GrowableArray<HBasicBlock*>& order_;
5070 size_t index_;
5071
5072 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5073};
5074
5075class HLinearOrderIterator : public ValueObject {
5076 public:
5077 explicit HLinearOrderIterator(const HGraph& graph)
5078 : order_(graph.GetLinearOrder()), index_(0) {}
5079
5080 bool Done() const { return index_ == order_.Size(); }
5081 HBasicBlock* Current() const { return order_.Get(index_); }
5082 void Advance() { ++index_; }
5083
5084 private:
5085 const GrowableArray<HBasicBlock*>& order_;
5086 size_t index_;
5087
5088 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5089};
5090
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005091// Iterator over the blocks that art part of the loop. Includes blocks part
5092// of an inner loop. The order in which the blocks are iterated is on their
5093// block id.
5094class HBlocksInLoopIterator : public ValueObject {
5095 public:
5096 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5097 : blocks_in_loop_(info.GetBlocks()),
5098 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5099 index_(0) {
5100 if (!blocks_in_loop_.IsBitSet(index_)) {
5101 Advance();
5102 }
5103 }
5104
5105 bool Done() const { return index_ == blocks_.Size(); }
5106 HBasicBlock* Current() const { return blocks_.Get(index_); }
5107 void Advance() {
5108 ++index_;
5109 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5110 if (blocks_in_loop_.IsBitSet(index_)) {
5111 break;
5112 }
5113 }
5114 }
5115
5116 private:
5117 const BitVector& blocks_in_loop_;
5118 const GrowableArray<HBasicBlock*>& blocks_;
5119 size_t index_;
5120
5121 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5122};
5123
Mingyao Yang3584bce2015-05-19 16:01:59 -07005124// Iterator over the blocks that art part of the loop. Includes blocks part
5125// of an inner loop. The order in which the blocks are iterated is reverse
5126// post order.
5127class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5128 public:
5129 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5130 : blocks_in_loop_(info.GetBlocks()),
5131 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5132 index_(0) {
5133 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5134 Advance();
5135 }
5136 }
5137
5138 bool Done() const { return index_ == blocks_.Size(); }
5139 HBasicBlock* Current() const { return blocks_.Get(index_); }
5140 void Advance() {
5141 ++index_;
5142 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5143 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5144 break;
5145 }
5146 }
5147 }
5148
5149 private:
5150 const BitVector& blocks_in_loop_;
5151 const GrowableArray<HBasicBlock*>& blocks_;
5152 size_t index_;
5153
5154 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5155};
5156
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005157inline int64_t Int64FromConstant(HConstant* constant) {
5158 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5159 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5160 : constant->AsLongConstant()->GetValue();
5161}
5162
Vladimir Marko58155012015-08-19 12:49:41 +00005163inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5164 // For the purposes of the compiler, the dex files must actually be the same object
5165 // if we want to safely treat them as the same. This is especially important for JIT
5166 // as custom class loaders can open the same underlying file (or memory) multiple
5167 // times and provide different class resolution but no two class loaders should ever
5168 // use the same DexFile object - doing so is an unsupported hack that can lead to
5169 // all sorts of weird failures.
5170 return &lhs == &rhs;
5171}
5172
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005173} // namespace art
5174
5175#endif // ART_COMPILER_OPTIMIZING_NODES_H_