blob: 2ed2d9ab20ab9e3fdb01cc28d4870dad7e8fc7b5 [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
Roland Levillain9867bc72015-08-05 10:21:34 +010020#include <type_traits>
21
David Brazdil8d5b8b22015-03-24 10:51:52 +000022#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080023#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010024#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "handle.h"
27#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000028#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010029#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000030#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000031#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000034#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035#include "utils/growable_array.h"
36
37namespace art {
38
David Brazdil1abb4192015-02-17 18:33:36 +000039class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010041class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000042class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010043class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010044class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000046class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000049class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000050class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000052class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010054class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010055class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000057class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010058class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
61static const int kDefaultNumberOfBlocks = 8;
62static const int kDefaultNumberOfSuccessors = 2;
63static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010064static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010065static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000066static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Calin Juravle9aec02f2014-11-18 23:06:35 +000068static constexpr uint32_t kMaxIntShiftValue = 0x1f;
69static constexpr uint64_t kMaxLongShiftValue = 0x3f;
70
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010071static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
72
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010073static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
74
Dave Allison20dfc792014-06-16 20:44:29 -070075enum IfCondition {
76 kCondEQ,
77 kCondNE,
78 kCondLT,
79 kCondLE,
80 kCondGT,
81 kCondGE,
82};
83
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010084class HInstructionList {
85 public:
86 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
87
88 void AddInstruction(HInstruction* instruction);
89 void RemoveInstruction(HInstruction* instruction);
90
David Brazdilc3d743f2015-04-22 13:40:50 +010091 // Insert `instruction` before/after an existing instruction `cursor`.
92 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
93 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
94
Roland Levillain6b469232014-09-25 10:10:38 +010095 // Return true if this list contains `instruction`.
96 bool Contains(HInstruction* instruction) const;
97
Roland Levillainccc07a92014-09-16 14:48:16 +010098 // Return true if `instruction1` is found before `instruction2` in
99 // this instruction list and false otherwise. Abort if none
100 // of these instructions is found.
101 bool FoundBefore(const HInstruction* instruction1,
102 const HInstruction* instruction2) const;
103
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000104 bool IsEmpty() const { return first_instruction_ == nullptr; }
105 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
106
107 // Update the block of all instructions to be `block`.
108 void SetBlockOfInstructions(HBasicBlock* block) const;
109
110 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
111 void Add(const HInstructionList& instruction_list);
112
David Brazdil2d7352b2015-04-20 14:52:42 +0100113 // Return the number of instructions in the list. This is an expensive operation.
114 size_t CountSize() const;
115
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116 private:
117 HInstruction* first_instruction_;
118 HInstruction* last_instruction_;
119
120 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000121 friend class HGraph;
122 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100123 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100124 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125
126 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
127};
128
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000129// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700130class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000131 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100132 HGraph(ArenaAllocator* arena,
133 const DexFile& dex_file,
134 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100135 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100137 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100138 bool debuggable = false,
139 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000141 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100142 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100143 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700144 entry_block_(nullptr),
145 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100146 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100147 number_of_vregs_(0),
148 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000149 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400150 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000151 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000152 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100153 dex_file_(dex_file),
154 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100155 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100156 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100157 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000159 cached_null_constant_(nullptr),
160 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000161 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
162 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100163 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
164 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000166 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100168 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000169
David Brazdil69ba7b72015-06-23 18:27:30 +0100170 bool IsInSsaForm() const { return in_ssa_form_; }
171
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172 HBasicBlock* GetEntryBlock() const { return entry_block_; }
173 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100174 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000176 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
177 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000178
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000179 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 // Try building the SSA form of this graph, with dominance computation and loop
182 // recognition. Returns whether it was successful in doing all these steps.
183 bool TryBuildingSsa() {
184 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000185 // The SSA builder requires loops to all be natural. Specifically, the dead phi
186 // elimination phase checks the consistency of the graph when doing a post-order
187 // visit for eliminating dead phis: a dead phi can only have loop header phi
188 // users remaining when being visited.
189 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100190 // Precompute per-block try membership before entering the SSA builder,
191 // which needs the information to build catch block phis from values of
192 // locals at throwing instructions inside try blocks.
193 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100195 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000196 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000197 }
198
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100199 void ComputeDominanceInformation();
200 void ClearDominanceInformation();
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000203 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100204 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100205 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000207 // Analyze all natural loops in this graph. Returns false if one
208 // loop is not natural, that is the header does not dominate the
209 // back edge.
210 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100211
David Brazdilffee3d32015-07-06 11:48:53 +0100212 // Iterate over blocks to compute try block membership. Needs reverse post
213 // order and loop information.
214 void ComputeTryBlockInformation();
215
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000217 // Returns the instruction used to replace the invoke expression or null if the
218 // invoke is for a void method.
219 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220
Mingyao Yang3584bce2015-05-19 16:01:59 -0700221 // Need to add a couple of blocks to test if the loop body is entered and
222 // put deoptimization instructions, etc.
223 void TransformLoopHeaderForBCE(HBasicBlock* header);
224
David Brazdil2d7352b2015-04-20 14:52:42 +0100225 // Removes `block` from the graph.
226 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000227
David Brazdilfc6a86a2015-06-26 10:33:45 +0000228 // Splits the edge between `block` and `successor` while preserving the
229 // indices in the predecessor/successor lists. If there are multiple edges
230 // between the blocks, the lowest indices are used.
231 // Returns the new block which is empty and has the same dex pc as `successor`.
232 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
233
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
235 void SimplifyLoop(HBasicBlock* header);
236
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000237 int32_t GetNextInstructionId() {
238 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 return current_instruction_id_++;
240 }
241
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000242 int32_t GetCurrentInstructionId() const {
243 return current_instruction_id_;
244 }
245
246 void SetCurrentInstructionId(int32_t id) {
247 current_instruction_id_ = id;
248 }
249
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100250 uint16_t GetMaximumNumberOfOutVRegs() const {
251 return maximum_number_of_out_vregs_;
252 }
253
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000254 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
255 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100256 }
257
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100258 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
259 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
260 }
261
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000262 void UpdateTemporariesVRegSlots(size_t slots) {
263 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100264 }
265
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000266 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100267 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000268 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100269 }
270
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100271 void SetNumberOfVRegs(uint16_t number_of_vregs) {
272 number_of_vregs_ = number_of_vregs;
273 }
274
275 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100276 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100277 return number_of_vregs_;
278 }
279
280 void SetNumberOfInVRegs(uint16_t value) {
281 number_of_in_vregs_ = value;
282 }
283
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100284 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100285 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100286 return number_of_vregs_ - number_of_in_vregs_;
287 }
288
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100289 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
290 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100291 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100292
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100293 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
294 return linear_order_;
295 }
296
Mark Mendell1152c922015-04-24 17:06:35 -0400297 bool HasBoundsChecks() const {
298 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800299 }
300
Mark Mendell1152c922015-04-24 17:06:35 -0400301 void SetHasBoundsChecks(bool value) {
302 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800303 }
304
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100305 bool ShouldGenerateConstructorBarrier() const {
306 return should_generate_constructor_barrier_;
307 }
308
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000309 bool IsDebuggable() const { return debuggable_; }
310
David Brazdil8d5b8b22015-03-24 10:51:52 +0000311 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000312 // already, it is created and inserted into the graph. This method is only for
313 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000314 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000315
316 // TODO: This is problematic for the consistency of reference type propagation
317 // because it can be created anytime after the pass and thus it will be left
318 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000319 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000320
David Brazdil8d5b8b22015-03-24 10:51:52 +0000321 HIntConstant* GetIntConstant(int32_t value) {
322 return CreateConstant(value, &cached_int_constants_);
323 }
324 HLongConstant* GetLongConstant(int64_t value) {
325 return CreateConstant(value, &cached_long_constants_);
326 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000327 HFloatConstant* GetFloatConstant(float value) {
328 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
329 }
330 HDoubleConstant* GetDoubleConstant(double value) {
331 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
332 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000333
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100334 HCurrentMethod* GetCurrentMethod();
335
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000336 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100337
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 const DexFile& GetDexFile() const {
339 return dex_file_;
340 }
341
342 uint32_t GetMethodIdx() const {
343 return method_idx_;
344 }
345
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100346 InvokeType GetInvokeType() const {
347 return invoke_type_;
348 }
349
Mark Mendellc4701932015-04-10 13:18:51 -0400350 InstructionSet GetInstructionSet() const {
351 return instruction_set_;
352 }
353
David Brazdilbbd733e2015-08-18 17:48:17 +0100354 // TODO: Remove once the full compilation pipeline is enabled for try/catch.
355 bool HasTryCatch() const;
356
David Brazdil2d7352b2015-04-20 14:52:42 +0100357 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 void VisitBlockForDominatorTree(HBasicBlock* block,
359 HBasicBlock* predecessor,
360 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100361 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000362 void VisitBlockForBackEdges(HBasicBlock* block,
363 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100364 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000365 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100366 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000367
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000368 template <class InstructionType, typename ValueType>
369 InstructionType* CreateConstant(ValueType value,
370 ArenaSafeMap<ValueType, InstructionType*>* cache) {
371 // Try to find an existing constant of the given value.
372 InstructionType* constant = nullptr;
373 auto cached_constant = cache->find(value);
374 if (cached_constant != cache->end()) {
375 constant = cached_constant->second;
376 }
377
378 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100379 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000380 if (constant == nullptr || constant->GetBlock() == nullptr) {
381 constant = new (arena_) InstructionType(value);
382 cache->Overwrite(value, constant);
383 InsertConstant(constant);
384 }
385 return constant;
386 }
387
David Brazdil8d5b8b22015-03-24 10:51:52 +0000388 void InsertConstant(HConstant* instruction);
389
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000390 // Cache a float constant into the graph. This method should only be
391 // called by the SsaBuilder when creating "equivalent" instructions.
392 void CacheFloatConstant(HFloatConstant* constant);
393
394 // See CacheFloatConstant comment.
395 void CacheDoubleConstant(HDoubleConstant* constant);
396
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000397 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000398
399 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000400 GrowableArray<HBasicBlock*> blocks_;
401
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100402 // List of blocks to perform a reverse post order tree traversal.
403 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000404
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100405 // List of blocks to perform a linear order tree traversal.
406 GrowableArray<HBasicBlock*> linear_order_;
407
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000408 HBasicBlock* entry_block_;
409 HBasicBlock* exit_block_;
410
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100411 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100412 uint16_t maximum_number_of_out_vregs_;
413
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100414 // The number of virtual registers in this method. Contains the parameters.
415 uint16_t number_of_vregs_;
416
417 // The number of virtual registers used by parameters of this method.
418 uint16_t number_of_in_vregs_;
419
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000420 // Number of vreg size slots that the temporaries use (used in baseline compiler).
421 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100422
Mark Mendell1152c922015-04-24 17:06:35 -0400423 // Has bounds checks. We can totally skip BCE if it's false.
424 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800425
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000426 // Indicates whether the graph should be compiled in a way that
427 // ensures full debuggability. If false, we can apply more
428 // aggressive optimizations that may limit the level of debugging.
429 const bool debuggable_;
430
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000431 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000432 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000433
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100434 // The dex file from which the method is from.
435 const DexFile& dex_file_;
436
437 // The method index in the dex file.
438 const uint32_t method_idx_;
439
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100440 // If inlined, this encodes how the callee is being invoked.
441 const InvokeType invoke_type_;
442
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100443 // Whether the graph has been transformed to SSA form. Only used
444 // in debug mode to ensure we are not using properties only valid
445 // for non-SSA form (like the number of temporaries).
446 bool in_ssa_form_;
447
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100448 const bool should_generate_constructor_barrier_;
449
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 const InstructionSet instruction_set_;
451
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000452 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000453 HNullConstant* cached_null_constant_;
454 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000455 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000456 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000457 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000458
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100459 HCurrentMethod* cached_current_method_;
460
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100462 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000463 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000464 DISALLOW_COPY_AND_ASSIGN(HGraph);
465};
466
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700467class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000468 public:
469 HLoopInformation(HBasicBlock* header, HGraph* graph)
470 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100471 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100472 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100473 // Make bit vector growable, as the number of blocks may change.
474 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100475
476 HBasicBlock* GetHeader() const {
477 return header_;
478 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000479
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000480 void SetHeader(HBasicBlock* block) {
481 header_ = block;
482 }
483
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100484 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
485 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
486 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
487
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 void AddBackEdge(HBasicBlock* back_edge) {
489 back_edges_.Add(back_edge);
490 }
491
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492 void RemoveBackEdge(HBasicBlock* back_edge) {
493 back_edges_.Delete(back_edge);
494 }
495
David Brazdil46e2a392015-03-16 17:31:52 +0000496 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100497 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000498 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100499 }
500 return false;
501 }
502
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000503 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000504 return back_edges_.Size();
505 }
506
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100508
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
510 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100511 }
512
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100513 // Returns the lifetime position of the back edge that has the
514 // greatest lifetime position.
515 size_t GetLifetimeEnd() const;
516
517 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
518 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
519 if (back_edges_.Get(i) == existing) {
520 back_edges_.Put(i, new_back_edge);
521 return;
522 }
523 }
524 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100525 }
526
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100527 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100528 // that is the header dominates the back edge.
529 bool Populate();
530
David Brazdila4b8c212015-05-07 09:59:30 +0100531 // Reanalyzes the loop by removing loop info from its blocks and re-running
532 // Populate(). If there are no back edges left, the loop info is completely
533 // removed as well as its SuspendCheck instruction. It must be run on nested
534 // inner loops first.
535 void Update();
536
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // Returns whether this loop information contains `block`.
538 // Note that this loop information *must* be populated before entering this function.
539 bool Contains(const HBasicBlock& block) const;
540
541 // Returns whether this loop information is an inner loop of `other`.
542 // Note that `other` *must* be populated before entering this function.
543 bool IsIn(const HLoopInformation& other) const;
544
545 const ArenaBitVector& GetBlocks() const { return blocks_; }
546
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000547 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000548 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000549
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 // Internal recursive implementation of `Populate`.
552 void PopulateRecursive(HBasicBlock* block);
553
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000554 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100557 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558
559 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
560};
561
David Brazdilec16f792015-08-19 15:04:01 +0100562// Stores try/catch information for basic blocks.
563// Note that HGraph is constructed so that catch blocks cannot simultaneously
564// be try blocks.
565class TryCatchInformation : public ArenaObject<kArenaAllocMisc> {
566 public:
567 // Try block information constructor.
568 explicit TryCatchInformation(const HTryBoundary& try_entry)
569 : try_entry_(&try_entry),
570 catch_dex_file_(nullptr),
571 catch_type_index_(DexFile::kDexNoIndex16) {
572 DCHECK(try_entry_ != nullptr);
573 }
574
575 // Catch block information constructor.
576 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
577 : try_entry_(nullptr),
578 catch_dex_file_(&dex_file),
579 catch_type_index_(catch_type_index) {}
580
581 bool IsTryBlock() const { return try_entry_ != nullptr; }
582
583 const HTryBoundary& GetTryEntry() const {
584 DCHECK(IsTryBlock());
585 return *try_entry_;
586 }
587
588 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
589
590 bool IsCatchAllTypeIndex() const {
591 DCHECK(IsCatchBlock());
592 return catch_type_index_ == DexFile::kDexNoIndex16;
593 }
594
595 uint16_t GetCatchTypeIndex() const {
596 DCHECK(IsCatchBlock());
597 return catch_type_index_;
598 }
599
600 const DexFile& GetCatchDexFile() const {
601 DCHECK(IsCatchBlock());
602 return *catch_dex_file_;
603 }
604
605 private:
606 // One of possibly several TryBoundary instructions entering the block's try.
607 // Only set for try blocks.
608 const HTryBoundary* try_entry_;
609
610 // Exception type information. Only set for catch blocks.
611 const DexFile* catch_dex_file_;
612 const uint16_t catch_type_index_;
613};
614
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100615static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100616static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100617
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000618// A block in a method. Contains the list of instructions represented
619// as a double linked list. Each block knows its predecessors and
620// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100621
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700622class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000623 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100624 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000626 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
627 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000628 loop_information_(nullptr),
629 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100630 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100631 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100632 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100633 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000634 lifetime_end_(kNoLifetime),
David Brazdilec16f792015-08-19 15:04:01 +0100635 try_catch_information_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100637 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
638 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639 }
640
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100641 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
642 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 }
644
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100645 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
646 return dominated_blocks_;
647 }
648
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100649 bool IsEntryBlock() const {
650 return graph_->GetEntryBlock() == this;
651 }
652
653 bool IsExitBlock() const {
654 return graph_->GetExitBlock() == this;
655 }
656
David Brazdil46e2a392015-03-16 17:31:52 +0000657 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000658 bool IsSingleTryBoundary() const;
659
660 // Returns true if this block emits nothing but a jump.
661 bool IsSingleJump() const {
662 HLoopInformation* loop_info = GetLoopInformation();
663 return (IsSingleGoto() || IsSingleTryBoundary())
664 // Back edges generate a suspend check.
665 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
666 }
David Brazdil46e2a392015-03-16 17:31:52 +0000667
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668 void AddBackEdge(HBasicBlock* back_edge) {
669 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000670 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000671 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100672 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000673 loop_information_->AddBackEdge(back_edge);
674 }
675
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000676 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000677 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000678
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000679 int GetBlockId() const { return block_id_; }
680 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000681
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000682 HBasicBlock* GetDominator() const { return dominator_; }
683 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100684 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100685 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000686 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
687 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
688 if (dominated_blocks_.Get(i) == existing) {
689 dominated_blocks_.Put(i, new_block);
690 return;
691 }
692 }
693 LOG(FATAL) << "Unreachable";
694 UNREACHABLE();
695 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100696 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697
698 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100699 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000700 }
701
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
703 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100704 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100705 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100706 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
707 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000708
709 void AddSuccessor(HBasicBlock* block) {
710 successors_.Add(block);
711 block->predecessors_.Add(this);
712 }
713
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100714 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
715 size_t successor_index = GetSuccessorIndexOf(existing);
716 DCHECK_NE(successor_index, static_cast<size_t>(-1));
717 existing->RemovePredecessor(this);
718 new_block->predecessors_.Add(this);
719 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000720 }
721
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000722 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
723 size_t predecessor_index = GetPredecessorIndexOf(existing);
724 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
725 existing->RemoveSuccessor(this);
726 new_block->successors_.Add(this);
727 predecessors_.Put(predecessor_index, new_block);
728 }
729
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100730 // Insert `this` between `predecessor` and `successor. This method
731 // preserves the indicies, and will update the first edge found between
732 // `predecessor` and `successor`.
733 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
734 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
735 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
736 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
737 DCHECK_NE(successor_index, static_cast<size_t>(-1));
738 successor->predecessors_.Put(predecessor_index, this);
739 predecessor->successors_.Put(successor_index, this);
740 successors_.Add(successor);
741 predecessors_.Add(predecessor);
742 }
743
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100744 void RemovePredecessor(HBasicBlock* block) {
745 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100746 }
747
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000748 void RemoveSuccessor(HBasicBlock* block) {
749 successors_.Delete(block);
750 }
751
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100752 void ClearAllPredecessors() {
753 predecessors_.Reset();
754 }
755
756 void AddPredecessor(HBasicBlock* block) {
757 predecessors_.Add(block);
758 block->successors_.Add(this);
759 }
760
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100761 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100762 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100763 HBasicBlock* temp = predecessors_.Get(0);
764 predecessors_.Put(0, predecessors_.Get(1));
765 predecessors_.Put(1, temp);
766 }
767
David Brazdil769c9e52015-04-27 13:54:09 +0100768 void SwapSuccessors() {
769 DCHECK_EQ(successors_.Size(), 2u);
770 HBasicBlock* temp = successors_.Get(0);
771 successors_.Put(0, successors_.Get(1));
772 successors_.Put(1, temp);
773 }
774
David Brazdilfc6a86a2015-06-26 10:33:45 +0000775 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100776 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
777 if (predecessors_.Get(i) == predecessor) {
778 return i;
779 }
780 }
781 return -1;
782 }
783
David Brazdilfc6a86a2015-06-26 10:33:45 +0000784 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100785 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
786 if (successors_.Get(i) == successor) {
787 return i;
788 }
789 }
790 return -1;
791 }
792
David Brazdilfc6a86a2015-06-26 10:33:45 +0000793 HBasicBlock* GetSinglePredecessor() const {
794 DCHECK_EQ(GetPredecessors().Size(), 1u);
795 return GetPredecessors().Get(0);
796 }
797
798 HBasicBlock* GetSingleSuccessor() const {
799 DCHECK_EQ(GetSuccessors().Size(), 1u);
800 return GetSuccessors().Get(0);
801 }
802
803 // Returns whether the first occurrence of `predecessor` in the list of
804 // predecessors is at index `idx`.
805 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
806 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
807 return GetPredecessorIndexOf(predecessor) == idx;
808 }
809
David Brazdilffee3d32015-07-06 11:48:53 +0100810 // Returns the number of non-exceptional successors. SsaChecker ensures that
811 // these are stored at the beginning of the successor list.
812 size_t NumberOfNormalSuccessors() const {
813 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
814 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000815
816 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100817 // created, latter block. Note that this method will add the block to the
818 // graph, create a Goto at the end of the former block and will create an edge
819 // between the blocks. It will not, however, update the reverse post order or
820 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000821 HBasicBlock* SplitBefore(HInstruction* cursor);
822
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000823 // Split the block into two blocks just after `cursor`. Returns the newly
824 // created block. Note that this method just updates raw block information,
825 // like predecessors, successors, dominators, and instruction list. It does not
826 // update the graph, reverse post order, loop information, nor make sure the
827 // blocks are consistent (for example ending with a control flow instruction).
828 HBasicBlock* SplitAfter(HInstruction* cursor);
829
830 // Merge `other` at the end of `this`. Successors and dominated blocks of
831 // `other` are changed to be successors and dominated blocks of `this`. Note
832 // that this method does not update the graph, reverse post order, loop
833 // information, nor make sure the blocks are consistent (for example ending
834 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100835 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000836
837 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
838 // of `this` are moved to `other`.
839 // Note that this method does not update the graph, reverse post order, loop
840 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000841 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000842 void ReplaceWith(HBasicBlock* other);
843
David Brazdil2d7352b2015-04-20 14:52:42 +0100844 // Merge `other` at the end of `this`. This method updates loops, reverse post
845 // order, links to predecessors, successors, dominators and deletes the block
846 // from the graph. The two blocks must be successive, i.e. `this` the only
847 // predecessor of `other` and vice versa.
848 void MergeWith(HBasicBlock* other);
849
850 // Disconnects `this` from all its predecessors, successors and dominator,
851 // removes it from all loops it is included in and eventually from the graph.
852 // The block must not dominate any other block. Predecessors and successors
853 // are safely updated.
854 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000855
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000856 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100857 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100858 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100859 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100860 // Replace instruction `initial` with `replacement` within this block.
861 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
862 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100863 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100864 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000865 // RemoveInstruction and RemovePhi delete a given instruction from the respective
866 // instruction list. With 'ensure_safety' set to true, it verifies that the
867 // instruction is not in use and removes it from the use lists of its inputs.
868 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
869 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100870 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100871
872 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100873 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100874 }
875
Roland Levillain6b879dd2014-09-22 17:13:44 +0100876 bool IsLoopPreHeaderFirstPredecessor() const {
877 DCHECK(IsLoopHeader());
878 DCHECK(!GetPredecessors().IsEmpty());
879 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
880 }
881
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 HLoopInformation* GetLoopInformation() const {
883 return loop_information_;
884 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000886 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100887 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000888 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100889 void SetInLoop(HLoopInformation* info) {
890 if (IsLoopHeader()) {
891 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100892 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100893 loop_information_ = info;
894 } else if (loop_information_->Contains(*info->GetHeader())) {
895 // Block is currently part of an outer loop. Make it part of this inner loop.
896 // Note that a non loop header having a loop information means this loop information
897 // has already been populated
898 loop_information_ = info;
899 } else {
900 // Block is part of an inner loop. Do not update the loop information.
901 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
902 // at this point, because this method is being called while populating `info`.
903 }
904 }
905
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 // Raw update of the loop information.
907 void SetLoopInformation(HLoopInformation* info) {
908 loop_information_ = info;
909 }
910
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100911 bool IsInLoop() const { return loop_information_ != nullptr; }
912
David Brazdilec16f792015-08-19 15:04:01 +0100913 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
914
915 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
916 try_catch_information_ = try_catch_information;
917 }
918
919 bool IsTryBlock() const {
920 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
921 }
922
923 bool IsCatchBlock() const {
924 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
925 }
David Brazdilffee3d32015-07-06 11:48:53 +0100926
927 // Returns the try entry that this block's successors should have. They will
928 // be in the same try, unless the block ends in a try boundary. In that case,
929 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100930 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100931
David Brazdila4b8c212015-05-07 09:59:30 +0100932 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 bool Dominates(HBasicBlock* block) const;
934
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100935 size_t GetLifetimeStart() const { return lifetime_start_; }
936 size_t GetLifetimeEnd() const { return lifetime_end_; }
937
938 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
939 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
940
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100941 uint32_t GetDexPc() const { return dex_pc_; }
942
David Brazdil8d5b8b22015-03-24 10:51:52 +0000943 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000944 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100945 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000946 bool HasSinglePhi() const;
947
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000949 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000950 GrowableArray<HBasicBlock*> predecessors_;
951 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100952 HInstructionList instructions_;
953 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000954 HLoopInformation* loop_information_;
955 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100956 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100958 // The dex program counter of the first instruction of this block.
959 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100960 size_t lifetime_start_;
961 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100962 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100963
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000964 friend class HGraph;
965 friend class HInstruction;
966
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
968};
969
David Brazdilb2bd1c52015-03-25 11:17:37 +0000970// Iterates over the LoopInformation of all loops which contain 'block'
971// from the innermost to the outermost.
972class HLoopInformationOutwardIterator : public ValueObject {
973 public:
974 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
975 : current_(block.GetLoopInformation()) {}
976
977 bool Done() const { return current_ == nullptr; }
978
979 void Advance() {
980 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100981 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000982 }
983
984 HLoopInformation* Current() const {
985 DCHECK(!Done());
986 return current_;
987 }
988
989 private:
990 HLoopInformation* current_;
991
992 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
993};
994
Alexandre Ramesef20f712015-06-09 10:29:30 +0100995#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100996 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000997 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000998 M(ArrayGet, Instruction) \
999 M(ArrayLength, Instruction) \
1000 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001001 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001003 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001004 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001005 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001006 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001007 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001008 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001009 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001010 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001011 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001012 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001013 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001014 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001015 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001016 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(FloatConstant, Constant) \
1018 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001019 M(GreaterThan, Condition) \
1020 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(InstanceFieldGet, Instruction) \
1023 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001024 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001025 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001026 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001027 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001028 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001029 M(LessThan, Condition) \
1030 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001031 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001032 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001034 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(Local, Instruction) \
1036 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001037 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001038 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(Mul, BinaryOperation) \
1040 M(Neg, UnaryOperation) \
1041 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001043 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001044 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001045 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001047 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001051 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001052 M(Return, Instruction) \
1053 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001054 M(Shl, BinaryOperation) \
1055 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001056 M(StaticFieldGet, Instruction) \
1057 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(StoreLocal, Instruction) \
1059 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001061 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001062 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001063 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001064 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001065 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001066 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067
Alexandre Ramesef20f712015-06-09 10:29:30 +01001068#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1069
1070#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1071
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001072#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1073
Alexandre Ramesef20f712015-06-09 10:29:30 +01001074#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1075
1076#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1077
1078#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1079 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1080 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1081 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001082 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001083 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1084 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1085
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001086#define FOR_EACH_INSTRUCTION(M) \
1087 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1088 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001089 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001090 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001091 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001092
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001093#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001094FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1095#undef FORWARD_DECLARATION
1096
Roland Levillainccc07a92014-09-16 14:48:16 +01001097#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001098 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1099 const char* DebugName() const OVERRIDE { return #type; } \
1100 const H##type* As##type() const OVERRIDE { return this; } \
1101 H##type* As##type() OVERRIDE { return this; } \
1102 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001103 return other->Is##type(); \
1104 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001105 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001106
David Brazdiled596192015-01-23 10:39:45 +00001107template <typename T> class HUseList;
1108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001109template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001110class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001111 public:
David Brazdiled596192015-01-23 10:39:45 +00001112 HUseListNode* GetPrevious() const { return prev_; }
1113 HUseListNode* GetNext() const { return next_; }
1114 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001115 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001116 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001118 private:
David Brazdiled596192015-01-23 10:39:45 +00001119 HUseListNode(T user, size_t index)
1120 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1121
1122 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001123 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001124 HUseListNode<T>* prev_;
1125 HUseListNode<T>* next_;
1126
1127 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001128
1129 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1130};
1131
David Brazdiled596192015-01-23 10:39:45 +00001132template <typename T>
1133class HUseList : public ValueObject {
1134 public:
1135 HUseList() : first_(nullptr) {}
1136
1137 void Clear() {
1138 first_ = nullptr;
1139 }
1140
1141 // Adds a new entry at the beginning of the use list and returns
1142 // the newly created node.
1143 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001144 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001145 if (IsEmpty()) {
1146 first_ = new_node;
1147 } else {
1148 first_->prev_ = new_node;
1149 new_node->next_ = first_;
1150 first_ = new_node;
1151 }
1152 return new_node;
1153 }
1154
1155 HUseListNode<T>* GetFirst() const {
1156 return first_;
1157 }
1158
1159 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001160 DCHECK(node != nullptr);
1161 DCHECK(Contains(node));
1162
David Brazdiled596192015-01-23 10:39:45 +00001163 if (node->prev_ != nullptr) {
1164 node->prev_->next_ = node->next_;
1165 }
1166 if (node->next_ != nullptr) {
1167 node->next_->prev_ = node->prev_;
1168 }
1169 if (node == first_) {
1170 first_ = node->next_;
1171 }
1172 }
1173
David Brazdil1abb4192015-02-17 18:33:36 +00001174 bool Contains(const HUseListNode<T>* node) const {
1175 if (node == nullptr) {
1176 return false;
1177 }
1178 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1179 if (current == node) {
1180 return true;
1181 }
1182 }
1183 return false;
1184 }
1185
David Brazdiled596192015-01-23 10:39:45 +00001186 bool IsEmpty() const {
1187 return first_ == nullptr;
1188 }
1189
1190 bool HasOnlyOneUse() const {
1191 return first_ != nullptr && first_->next_ == nullptr;
1192 }
1193
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001194 size_t SizeSlow() const {
1195 size_t count = 0;
1196 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1197 ++count;
1198 }
1199 return count;
1200 }
1201
David Brazdiled596192015-01-23 10:39:45 +00001202 private:
1203 HUseListNode<T>* first_;
1204};
1205
1206template<typename T>
1207class HUseIterator : public ValueObject {
1208 public:
1209 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1210
1211 bool Done() const { return current_ == nullptr; }
1212
1213 void Advance() {
1214 DCHECK(!Done());
1215 current_ = current_->GetNext();
1216 }
1217
1218 HUseListNode<T>* Current() const {
1219 DCHECK(!Done());
1220 return current_;
1221 }
1222
1223 private:
1224 HUseListNode<T>* current_;
1225
1226 friend class HValue;
1227};
1228
David Brazdil1abb4192015-02-17 18:33:36 +00001229// This class is used by HEnvironment and HInstruction classes to record the
1230// instructions they use and pointers to the corresponding HUseListNodes kept
1231// by the used instructions.
1232template <typename T>
1233class HUserRecord : public ValueObject {
1234 public:
1235 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1236 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1237
1238 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1239 : instruction_(old_record.instruction_), use_node_(use_node) {
1240 DCHECK(instruction_ != nullptr);
1241 DCHECK(use_node_ != nullptr);
1242 DCHECK(old_record.use_node_ == nullptr);
1243 }
1244
1245 HInstruction* GetInstruction() const { return instruction_; }
1246 HUseListNode<T>* GetUseNode() const { return use_node_; }
1247
1248 private:
1249 // Instruction used by the user.
1250 HInstruction* instruction_;
1251
1252 // Corresponding entry in the use list kept by 'instruction_'.
1253 HUseListNode<T>* use_node_;
1254};
1255
Aart Bik854a02b2015-07-14 16:07:00 -07001256/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001257 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001258 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001259 * For write/read dependences on fields/arrays, the dependence analysis uses
1260 * type disambiguation (e.g. a float field write cannot modify the value of an
1261 * integer field read) and the access type (e.g. a reference array write cannot
1262 * modify the value of a reference field read [although it may modify the
1263 * reference fetch prior to reading the field, which is represented by its own
1264 * write/read dependence]). The analysis makes conservative points-to
1265 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1266 * the same, and any reference read depends on any reference read without
1267 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001268 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001269 * The internal representation uses 38-bit and is described in the table below.
1270 * The first line indicates the side effect, and for field/array accesses the
1271 * second line indicates the type of the access (in the order of the
1272 * Primitive::Type enum).
1273 * The two numbered lines below indicate the bit position in the bitfield (read
1274 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001275 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001276 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1277 * +-------------+---------+---------+--------------+---------+---------+
1278 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1279 * | 3 |333333322|222222221| 1 |111111110|000000000|
1280 * | 7 |654321098|765432109| 8 |765432109|876543210|
1281 *
1282 * Note that, to ease the implementation, 'changes' bits are least significant
1283 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001284 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001285class SideEffects : public ValueObject {
1286 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001287 SideEffects() : flags_(0) {}
1288
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001289 static SideEffects None() {
1290 return SideEffects(0);
1291 }
1292
1293 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001294 return SideEffects(kAllChangeBits | kAllDependOnBits);
1295 }
1296
1297 static SideEffects AllChanges() {
1298 return SideEffects(kAllChangeBits);
1299 }
1300
1301 static SideEffects AllDependencies() {
1302 return SideEffects(kAllDependOnBits);
1303 }
1304
1305 static SideEffects AllExceptGCDependency() {
1306 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1307 }
1308
1309 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001310 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001311 }
1312
Aart Bik34c3ba92015-07-20 14:08:59 -07001313 static SideEffects AllWrites() {
1314 return SideEffects(kAllWrites);
1315 }
1316
1317 static SideEffects AllReads() {
1318 return SideEffects(kAllReads);
1319 }
1320
1321 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1322 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001323 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001324 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001325 }
1326
Aart Bik854a02b2015-07-14 16:07:00 -07001327 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1328 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 }
1330
Aart Bik34c3ba92015-07-20 14:08:59 -07001331 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1332 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001333 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001334 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001335 }
1336
1337 static SideEffects ArrayReadOfType(Primitive::Type type) {
1338 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1339 }
1340
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001341 static SideEffects CanTriggerGC() {
1342 return SideEffects(1ULL << kCanTriggerGCBit);
1343 }
1344
1345 static SideEffects DependsOnGC() {
1346 return SideEffects(1ULL << kDependsOnGCBit);
1347 }
1348
Aart Bik854a02b2015-07-14 16:07:00 -07001349 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001350 SideEffects Union(SideEffects other) const {
1351 return SideEffects(flags_ | other.flags_);
1352 }
1353
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001354 SideEffects Exclusion(SideEffects other) const {
1355 return SideEffects(flags_ & ~other.flags_);
1356 }
1357
1358 bool Includes(SideEffects other) const {
1359 return (other.flags_ & flags_) == other.flags_;
1360 }
1361
1362 bool HasSideEffects() const {
1363 return (flags_ & kAllChangeBits);
1364 }
1365
1366 bool HasDependencies() const {
1367 return (flags_ & kAllDependOnBits);
1368 }
1369
1370 // Returns true if there are no side effects or dependencies.
1371 bool DoesNothing() const {
1372 return flags_ == 0;
1373 }
1374
Aart Bik854a02b2015-07-14 16:07:00 -07001375 // Returns true if something is written.
1376 bool DoesAnyWrite() const {
1377 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001378 }
1379
Aart Bik854a02b2015-07-14 16:07:00 -07001380 // Returns true if something is read.
1381 bool DoesAnyRead() const {
1382 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001383 }
1384
Aart Bik854a02b2015-07-14 16:07:00 -07001385 // Returns true if potentially everything is written and read
1386 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001387 bool DoesAllReadWrite() const {
1388 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1389 }
1390
Aart Bik854a02b2015-07-14 16:07:00 -07001391 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001392 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001393 }
1394
1395 // Returns true if this may read something written by other.
1396 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001397 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1398 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001399 }
1400
1401 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001403 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001404 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 for (int s = kLastBit; s >= 0; s--) {
1406 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1407 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1408 // This is a bit for the GC side effect.
1409 if (current_bit_is_set) {
1410 flags += "GC";
1411 }
Aart Bik854a02b2015-07-14 16:07:00 -07001412 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001413 } else {
1414 // This is a bit for the array/field analysis.
1415 // The underscore character stands for the 'can trigger GC' bit.
1416 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1417 if (current_bit_is_set) {
1418 flags += kDebug[s];
1419 }
1420 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1421 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1422 flags += "|";
1423 }
1424 }
Aart Bik854a02b2015-07-14 16:07:00 -07001425 }
1426 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001427 }
1428
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001429 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001431 private:
1432 static constexpr int kFieldArrayAnalysisBits = 9;
1433
1434 static constexpr int kFieldWriteOffset = 0;
1435 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1436 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1437 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1438
1439 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1440
1441 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1442 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1443 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1444 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1445
1446 static constexpr int kLastBit = kDependsOnGCBit;
1447 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1448
1449 // Aliases.
1450
1451 static_assert(kChangeBits == kDependOnBits,
1452 "the 'change' bits should match the 'depend on' bits.");
1453
1454 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1455 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1456 static constexpr uint64_t kAllWrites =
1457 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1458 static constexpr uint64_t kAllReads =
1459 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001460
Aart Bik854a02b2015-07-14 16:07:00 -07001461 // Work around the fact that HIR aliases I/F and J/D.
1462 // TODO: remove this interceptor once HIR types are clean
1463 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1464 switch (type) {
1465 case Primitive::kPrimInt:
1466 case Primitive::kPrimFloat:
1467 return TypeFlag(Primitive::kPrimInt, offset) |
1468 TypeFlag(Primitive::kPrimFloat, offset);
1469 case Primitive::kPrimLong:
1470 case Primitive::kPrimDouble:
1471 return TypeFlag(Primitive::kPrimLong, offset) |
1472 TypeFlag(Primitive::kPrimDouble, offset);
1473 default:
1474 return TypeFlag(type, offset);
1475 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001476 }
1477
Aart Bik854a02b2015-07-14 16:07:00 -07001478 // Translates type to bit flag.
1479 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1480 CHECK_NE(type, Primitive::kPrimVoid);
1481 const uint64_t one = 1;
1482 const int shift = type; // 0-based consecutive enum
1483 DCHECK_LE(kFieldWriteOffset, shift);
1484 DCHECK_LT(shift, kArrayWriteOffset);
1485 return one << (type + offset);
1486 }
1487
1488 // Private constructor on direct flags value.
1489 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1490
1491 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492};
1493
David Brazdiled596192015-01-23 10:39:45 +00001494// A HEnvironment object contains the values of virtual registers at a given location.
1495class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1496 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001497 HEnvironment(ArenaAllocator* arena,
1498 size_t number_of_vregs,
1499 const DexFile& dex_file,
1500 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001501 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001502 InvokeType invoke_type,
1503 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001504 : vregs_(arena, number_of_vregs),
1505 locations_(arena, number_of_vregs),
1506 parent_(nullptr),
1507 dex_file_(dex_file),
1508 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001509 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001510 invoke_type_(invoke_type),
1511 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001512 vregs_.SetSize(number_of_vregs);
1513 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001514 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001515 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001516
1517 locations_.SetSize(number_of_vregs);
1518 for (size_t i = 0; i < number_of_vregs; ++i) {
1519 locations_.Put(i, Location());
1520 }
1521 }
1522
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001523 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001524 : HEnvironment(arena,
1525 to_copy.Size(),
1526 to_copy.GetDexFile(),
1527 to_copy.GetMethodIdx(),
1528 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001529 to_copy.GetInvokeType(),
1530 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001531
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001532 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001533 if (parent_ != nullptr) {
1534 parent_->SetAndCopyParentChain(allocator, parent);
1535 } else {
1536 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1537 parent_->CopyFrom(parent);
1538 if (parent->GetParent() != nullptr) {
1539 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1540 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001541 }
David Brazdiled596192015-01-23 10:39:45 +00001542 }
1543
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001544 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1545 void CopyFrom(HEnvironment* environment);
1546
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001547 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1548 // input to the loop phi instead. This is for inserting instructions that
1549 // require an environment (like HDeoptimization) in the loop pre-header.
1550 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001551
1552 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001553 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001554 }
1555
1556 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001557 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001558 }
1559
David Brazdil1abb4192015-02-17 18:33:36 +00001560 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001561
1562 size_t Size() const { return vregs_.Size(); }
1563
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001564 HEnvironment* GetParent() const { return parent_; }
1565
1566 void SetLocationAt(size_t index, Location location) {
1567 locations_.Put(index, location);
1568 }
1569
1570 Location GetLocationAt(size_t index) const {
1571 return locations_.Get(index);
1572 }
1573
1574 uint32_t GetDexPc() const {
1575 return dex_pc_;
1576 }
1577
1578 uint32_t GetMethodIdx() const {
1579 return method_idx_;
1580 }
1581
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001582 InvokeType GetInvokeType() const {
1583 return invoke_type_;
1584 }
1585
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001586 const DexFile& GetDexFile() const {
1587 return dex_file_;
1588 }
1589
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001590 HInstruction* GetHolder() const {
1591 return holder_;
1592 }
1593
David Brazdiled596192015-01-23 10:39:45 +00001594 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001595 // Record instructions' use entries of this environment for constant-time removal.
1596 // It should only be called by HInstruction when a new environment use is added.
1597 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1598 DCHECK(env_use->GetUser() == this);
1599 size_t index = env_use->GetIndex();
1600 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1601 }
David Brazdiled596192015-01-23 10:39:45 +00001602
David Brazdil1abb4192015-02-17 18:33:36 +00001603 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001604 GrowableArray<Location> locations_;
1605 HEnvironment* parent_;
1606 const DexFile& dex_file_;
1607 const uint32_t method_idx_;
1608 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001609 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001610
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001611 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001612 HInstruction* const holder_;
1613
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001614 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001615
1616 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1617};
1618
Calin Juravleacf735c2015-02-12 15:25:22 +00001619class ReferenceTypeInfo : ValueObject {
1620 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001621 typedef Handle<mirror::Class> TypeHandle;
1622
Calin Juravle2e768302015-07-28 14:41:11 +00001623 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1624 // The constructor will check that the type_handle is valid.
1625 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001626 }
1627
Calin Juravle2e768302015-07-28 14:41:11 +00001628 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1629
1630 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1631 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001632 }
1633
Calin Juravle2e768302015-07-28 14:41:11 +00001634 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1635 return IsValidHandle(type_handle_);
1636 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001637 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001638
1639 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1640 DCHECK(IsValid());
1641 return GetTypeHandle()->IsObjectClass();
1642 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001643 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001644 DCHECK(IsValid());
1645 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001646 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001647
1648 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1649
Mathieu Chartier90443472015-07-16 20:32:27 -07001650 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001651 DCHECK(IsValid());
1652 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001653 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1654 }
1655
1656 // Returns true if the type information provide the same amount of details.
1657 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001658 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001659 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001660 if (!IsValid() && !rti.IsValid()) {
1661 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001662 return true;
1663 }
Calin Juravle2e768302015-07-28 14:41:11 +00001664 if (!IsValid() || !rti.IsValid()) {
1665 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001666 return false;
1667 }
Calin Juravle2e768302015-07-28 14:41:11 +00001668 return IsExact() == rti.IsExact()
1669 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001670 }
1671
1672 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001673 ReferenceTypeInfo();
1674 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001675
Calin Juravleacf735c2015-02-12 15:25:22 +00001676 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001677 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001678 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001679 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001680 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001681};
1682
1683std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1684
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001685class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001686 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001688 : previous_(nullptr),
1689 next_(nullptr),
1690 block_(nullptr),
1691 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001692 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001693 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001694 locations_(nullptr),
1695 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001697 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001698 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001699
Dave Allison20dfc792014-06-16 20:44:29 -07001700 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001701
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001702#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001703 enum InstructionKind {
1704 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1705 };
1706#undef DECLARE_KIND
1707
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001708 HInstruction* GetNext() const { return next_; }
1709 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001710
Calin Juravle77520bc2015-01-12 18:45:46 +00001711 HInstruction* GetNextDisregardingMoves() const;
1712 HInstruction* GetPreviousDisregardingMoves() const;
1713
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001714 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001715 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001716 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001717 bool IsInBlock() const { return block_ != nullptr; }
1718 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001719 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001720
Roland Levillain6b879dd2014-09-22 17:13:44 +01001721 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001722 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001723
1724 virtual void Accept(HGraphVisitor* visitor) = 0;
1725 virtual const char* DebugName() const = 0;
1726
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001727 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001728 void SetRawInputAt(size_t index, HInstruction* input) {
1729 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1730 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001732 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 virtual uint32_t GetDexPc() const {
1734 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1735 " does not need an environment";
1736 UNREACHABLE();
1737 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001738 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001739
Roland Levillaine161a2a2014-10-03 12:45:18 +01001740 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001741 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001742
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001743 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001744 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001745
Calin Juravle10e244f2015-01-26 18:54:32 +00001746 // Does not apply for all instructions, but having this at top level greatly
1747 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001748 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001749 virtual bool CanBeNull() const {
1750 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1751 return true;
1752 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001753
Calin Juravle641547a2015-04-21 22:08:51 +01001754 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1755 UNUSED(obj);
1756 return false;
1757 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001758
Calin Juravle2e768302015-07-28 14:41:11 +00001759 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001760
Calin Juravle61d544b2015-02-23 16:46:57 +00001761 ReferenceTypeInfo GetReferenceTypeInfo() const {
1762 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1763 return reference_type_info_;
1764 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001765
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001766 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001767 DCHECK(user != nullptr);
1768 HUseListNode<HInstruction*>* use =
1769 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1770 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001771 }
1772
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001773 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001774 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001775 HUseListNode<HEnvironment*>* env_use =
1776 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1777 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001778 }
1779
David Brazdil1abb4192015-02-17 18:33:36 +00001780 void RemoveAsUserOfInput(size_t input) {
1781 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1782 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1783 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001784
David Brazdil1abb4192015-02-17 18:33:36 +00001785 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1786 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001787
David Brazdiled596192015-01-23 10:39:45 +00001788 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1789 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001790 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001791 bool HasOnlyOneNonEnvironmentUse() const {
1792 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1793 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794
Roland Levillain6c82d402014-10-13 16:10:27 +01001795 // Does this instruction strictly dominate `other_instruction`?
1796 // Returns false if this instruction and `other_instruction` are the same.
1797 // Aborts if this instruction and `other_instruction` are both phis.
1798 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001799
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001800 int GetId() const { return id_; }
1801 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001802
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001803 int GetSsaIndex() const { return ssa_index_; }
1804 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1805 bool HasSsaIndex() const { return ssa_index_ != -1; }
1806
1807 bool HasEnvironment() const { return environment_ != nullptr; }
1808 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001809 // Set the `environment_` field. Raw because this method does not
1810 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001811 void SetRawEnvironment(HEnvironment* environment) {
1812 DCHECK(environment_ == nullptr);
1813 DCHECK_EQ(environment->GetHolder(), this);
1814 environment_ = environment;
1815 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001816
1817 // Set the environment of this instruction, copying it from `environment`. While
1818 // copying, the uses lists are being updated.
1819 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001820 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001821 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001822 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001823 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001824 if (environment->GetParent() != nullptr) {
1825 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1826 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001827 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001828
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001829 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1830 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001831 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001832 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001833 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001834 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001835 if (environment->GetParent() != nullptr) {
1836 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1837 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001838 }
1839
Nicolas Geoffray39468442014-09-02 15:17:15 +01001840 // Returns the number of entries in the environment. Typically, that is the
1841 // number of dex registers in a method. It could be more in case of inlining.
1842 size_t EnvironmentSize() const;
1843
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001844 LocationSummary* GetLocations() const { return locations_; }
1845 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001848 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001849
Alexandre Rames188d4312015-04-09 18:30:21 +01001850 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1851 // uses of this instruction by `other` are *not* updated.
1852 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1853 ReplaceWith(other);
1854 other->ReplaceInput(this, use_index);
1855 }
1856
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001857 // Move `this` instruction before `cursor`.
1858 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001859
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001860#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001861 bool Is##type() const { return (As##type() != nullptr); } \
1862 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001863 virtual H##type* As##type() { return nullptr; }
1864
1865 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1866#undef INSTRUCTION_TYPE_CHECK
1867
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001868 // Returns whether the instruction can be moved within the graph.
1869 virtual bool CanBeMoved() const { return false; }
1870
1871 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001872 virtual bool InstructionTypeEquals(HInstruction* other) const {
1873 UNUSED(other);
1874 return false;
1875 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876
1877 // Returns whether any data encoded in the two instructions is equal.
1878 // This method does not look at the inputs. Both instructions must be
1879 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001880 virtual bool InstructionDataEquals(HInstruction* other) const {
1881 UNUSED(other);
1882 return false;
1883 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001884
1885 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001886 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001887 // 2) Their inputs are identical.
1888 bool Equals(HInstruction* other) const;
1889
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001890 virtual InstructionKind GetKind() const = 0;
1891
1892 virtual size_t ComputeHashCode() const {
1893 size_t result = GetKind();
1894 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1895 result = (result * 31) + InputAt(i)->GetId();
1896 }
1897 return result;
1898 }
1899
1900 SideEffects GetSideEffects() const { return side_effects_; }
1901
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001902 size_t GetLifetimePosition() const { return lifetime_position_; }
1903 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1904 LiveInterval* GetLiveInterval() const { return live_interval_; }
1905 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1906 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1907
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001908 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1909
1910 // Returns whether the code generation of the instruction will require to have access
1911 // to the current method. Such instructions are:
1912 // (1): Instructions that require an environment, as calling the runtime requires
1913 // to walk the stack and have the current method stored at a specific stack address.
1914 // (2): Object literals like classes and strings, that are loaded from the dex cache
1915 // fields of the current method.
1916 bool NeedsCurrentMethod() const {
1917 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1918 }
1919
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001920 virtual bool NeedsDexCache() const { return false; }
1921
Mark Mendellc4701932015-04-10 13:18:51 -04001922 // Does this instruction have any use in an environment before
1923 // control flow hits 'other'?
1924 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1925
1926 // Remove all references to environment uses of this instruction.
1927 // The caller must ensure that this is safe to do.
1928 void RemoveEnvironmentUsers();
1929
David Brazdil1abb4192015-02-17 18:33:36 +00001930 protected:
1931 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1932 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1933
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001934 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001935 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1936
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937 HInstruction* previous_;
1938 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001939 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001940
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001941 // An instruction gets an id when it is added to the graph.
1942 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001943 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001944 int id_;
1945
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001946 // When doing liveness analysis, instructions that have uses get an SSA index.
1947 int ssa_index_;
1948
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001949 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001950 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001951
1952 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001953 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954
Nicolas Geoffray39468442014-09-02 15:17:15 +01001955 // The environment associated with this instruction. Not null if the instruction
1956 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001959 // Set by the code generator.
1960 LocationSummary* locations_;
1961
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001962 // Set by the liveness analysis.
1963 LiveInterval* live_interval_;
1964
1965 // Set by the liveness analysis, this is the position in a linear
1966 // order of blocks where this instruction's live interval start.
1967 size_t lifetime_position_;
1968
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001969 const SideEffects side_effects_;
1970
Calin Juravleacf735c2015-02-12 15:25:22 +00001971 // TODO: for primitive types this should be marked as invalid.
1972 ReferenceTypeInfo reference_type_info_;
1973
David Brazdil1abb4192015-02-17 18:33:36 +00001974 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001975 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001976 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001977 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001978 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001979
1980 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1981};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001982std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001983
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001984class HInputIterator : public ValueObject {
1985 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001986 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001987
1988 bool Done() const { return index_ == instruction_->InputCount(); }
1989 HInstruction* Current() const { return instruction_->InputAt(index_); }
1990 void Advance() { index_++; }
1991
1992 private:
1993 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001994 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995
1996 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1997};
1998
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001999class HInstructionIterator : public ValueObject {
2000 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002001 explicit HInstructionIterator(const HInstructionList& instructions)
2002 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002003 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002004 }
2005
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002006 bool Done() const { return instruction_ == nullptr; }
2007 HInstruction* Current() const { return instruction_; }
2008 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002009 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002010 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002011 }
2012
2013 private:
2014 HInstruction* instruction_;
2015 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002016
2017 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018};
2019
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002020class HBackwardInstructionIterator : public ValueObject {
2021 public:
2022 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2023 : instruction_(instructions.last_instruction_) {
2024 next_ = Done() ? nullptr : instruction_->GetPrevious();
2025 }
2026
2027 bool Done() const { return instruction_ == nullptr; }
2028 HInstruction* Current() const { return instruction_; }
2029 void Advance() {
2030 instruction_ = next_;
2031 next_ = Done() ? nullptr : instruction_->GetPrevious();
2032 }
2033
2034 private:
2035 HInstruction* instruction_;
2036 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002037
2038 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002039};
2040
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002041// An embedded container with N elements of type T. Used (with partial
2042// specialization for N=0) because embedded arrays cannot have size 0.
2043template<typename T, intptr_t N>
2044class EmbeddedArray {
2045 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002046 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002047
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002048 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002049
2050 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002051 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002052 return elements_[i];
2053 }
2054
2055 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002056 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057 return elements_[i];
2058 }
2059
2060 const T& At(intptr_t i) const {
2061 return (*this)[i];
2062 }
2063
2064 void SetAt(intptr_t i, const T& val) {
2065 (*this)[i] = val;
2066 }
2067
2068 private:
2069 T elements_[N];
2070};
2071
2072template<typename T>
2073class EmbeddedArray<T, 0> {
2074 public:
2075 intptr_t length() const { return 0; }
2076 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002077 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002078 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002079 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002080 }
2081 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002082 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002084 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085 }
2086};
2087
2088template<intptr_t N>
2089class HTemplateInstruction: public HInstruction {
2090 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002091 HTemplateInstruction<N>(SideEffects side_effects)
2092 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002093 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002095 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002097 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00002098 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
2099
2100 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
2101 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002102 }
2103
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002104 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002105 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106
2107 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002108};
2109
Dave Allison20dfc792014-06-16 20:44:29 -07002110template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002111class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002112 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002113 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2114 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002115 virtual ~HExpression() {}
2116
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002117 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002118
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002119 protected:
2120 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002121};
2122
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002123// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2124// instruction that branches to the exit block.
2125class HReturnVoid : public HTemplateInstruction<0> {
2126 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002127 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002128
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002129 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002130
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002131 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002132
2133 private:
2134 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2135};
2136
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002137// Represents dex's RETURN opcodes. A HReturn is a control flow
2138// instruction that branches to the exit block.
2139class HReturn : public HTemplateInstruction<1> {
2140 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002141 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002142 SetRawInputAt(0, value);
2143 }
2144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002145 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002146
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002147 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002148
2149 private:
2150 DISALLOW_COPY_AND_ASSIGN(HReturn);
2151};
2152
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002153// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002154// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155// exit block.
2156class HExit : public HTemplateInstruction<0> {
2157 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002158 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002159
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002160 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002162 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002163
2164 private:
2165 DISALLOW_COPY_AND_ASSIGN(HExit);
2166};
2167
2168// Jumps from one block to another.
2169class HGoto : public HTemplateInstruction<0> {
2170 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002171 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2172
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002173 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002175 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002176 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002177 }
2178
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002179 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180
2181 private:
2182 DISALLOW_COPY_AND_ASSIGN(HGoto);
2183};
2184
Roland Levillain9867bc72015-08-05 10:21:34 +01002185class HConstant : public HExpression<0> {
2186 public:
2187 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2188
2189 bool CanBeMoved() const OVERRIDE { return true; }
2190
2191 virtual bool IsMinusOne() const { return false; }
2192 virtual bool IsZero() const { return false; }
2193 virtual bool IsOne() const { return false; }
2194
2195 DECLARE_INSTRUCTION(Constant);
2196
2197 private:
2198 DISALLOW_COPY_AND_ASSIGN(HConstant);
2199};
2200
2201class HNullConstant : public HConstant {
2202 public:
2203 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2204 return true;
2205 }
2206
2207 size_t ComputeHashCode() const OVERRIDE { return 0; }
2208
2209 DECLARE_INSTRUCTION(NullConstant);
2210
2211 private:
2212 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2213
2214 friend class HGraph;
2215 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2216};
2217
2218// Constants of the type int. Those can be from Dex instructions, or
2219// synthesized (for example with the if-eqz instruction).
2220class HIntConstant : public HConstant {
2221 public:
2222 int32_t GetValue() const { return value_; }
2223
2224 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2225 DCHECK(other->IsIntConstant());
2226 return other->AsIntConstant()->value_ == value_;
2227 }
2228
2229 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2230
2231 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2232 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2233 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2234
2235 DECLARE_INSTRUCTION(IntConstant);
2236
2237 private:
2238 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2239 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2240
2241 const int32_t value_;
2242
2243 friend class HGraph;
2244 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2245 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2246 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2247};
2248
2249class HLongConstant : public HConstant {
2250 public:
2251 int64_t GetValue() const { return value_; }
2252
2253 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2254 DCHECK(other->IsLongConstant());
2255 return other->AsLongConstant()->value_ == value_;
2256 }
2257
2258 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2259
2260 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2261 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2262 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2263
2264 DECLARE_INSTRUCTION(LongConstant);
2265
2266 private:
2267 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2268
2269 const int64_t value_;
2270
2271 friend class HGraph;
2272 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2273};
Dave Allison20dfc792014-06-16 20:44:29 -07002274
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002275// Conditional branch. A block ending with an HIf instruction must have
2276// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002277class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002278 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280 SetRawInputAt(0, input);
2281 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002282
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002283 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002284
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002285 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002286 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002287 }
2288
2289 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002290 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002291 }
2292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002293 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002294
2295 private:
2296 DISALLOW_COPY_AND_ASSIGN(HIf);
2297};
2298
David Brazdilfc6a86a2015-06-26 10:33:45 +00002299
2300// Abstract instruction which marks the beginning and/or end of a try block and
2301// links it to the respective exception handlers. Behaves the same as a Goto in
2302// non-exceptional control flow.
2303// Normal-flow successor is stored at index zero, exception handlers under
2304// higher indices in no particular order.
2305class HTryBoundary : public HTemplateInstruction<0> {
2306 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002307 enum BoundaryKind {
2308 kEntry,
2309 kExit,
2310 };
2311
2312 explicit HTryBoundary(BoundaryKind kind)
2313 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002314
2315 bool IsControlFlow() const OVERRIDE { return true; }
2316
2317 // Returns the block's non-exceptional successor (index zero).
2318 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2319
2320 // Returns whether `handler` is among its exception handlers (non-zero index
2321 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002322 bool HasExceptionHandler(const HBasicBlock& handler) const {
2323 DCHECK(handler.IsCatchBlock());
2324 return GetBlock()->GetSuccessors().Contains(
2325 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002326 }
2327
2328 // If not present already, adds `handler` to its block's list of exception
2329 // handlers.
2330 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002331 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002332 GetBlock()->AddSuccessor(handler);
2333 }
2334 }
2335
David Brazdil56e1acc2015-06-30 15:41:36 +01002336 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002337
David Brazdilffee3d32015-07-06 11:48:53 +01002338 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2339
David Brazdilfc6a86a2015-06-26 10:33:45 +00002340 DECLARE_INSTRUCTION(TryBoundary);
2341
2342 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002343 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002344
2345 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2346};
2347
David Brazdilffee3d32015-07-06 11:48:53 +01002348// Iterator over exception handlers of a given HTryBoundary, i.e. over
2349// exceptional successors of its basic block.
2350class HExceptionHandlerIterator : public ValueObject {
2351 public:
2352 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2353 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2354
2355 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2356 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2357 size_t CurrentSuccessorIndex() const { return index_; }
2358 void Advance() { ++index_; }
2359
2360 private:
2361 const HBasicBlock& block_;
2362 size_t index_;
2363
2364 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2365};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002366
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002367// Deoptimize to interpreter, upon checking a condition.
2368class HDeoptimize : public HTemplateInstruction<1> {
2369 public:
2370 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2371 : HTemplateInstruction(SideEffects::None()),
2372 dex_pc_(dex_pc) {
2373 SetRawInputAt(0, cond);
2374 }
2375
2376 bool NeedsEnvironment() const OVERRIDE { return true; }
2377 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002378 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002379
2380 DECLARE_INSTRUCTION(Deoptimize);
2381
2382 private:
2383 uint32_t dex_pc_;
2384
2385 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2386};
2387
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002388// Represents the ArtMethod that was passed as a first argument to
2389// the method. It is used by instructions that depend on it, like
2390// instructions that work with the dex cache.
2391class HCurrentMethod : public HExpression<0> {
2392 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002393 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002394
2395 DECLARE_INSTRUCTION(CurrentMethod);
2396
2397 private:
2398 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2399};
2400
Roland Levillain88cb1752014-10-20 16:36:47 +01002401class HUnaryOperation : public HExpression<1> {
2402 public:
2403 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2404 : HExpression(result_type, SideEffects::None()) {
2405 SetRawInputAt(0, input);
2406 }
2407
2408 HInstruction* GetInput() const { return InputAt(0); }
2409 Primitive::Type GetResultType() const { return GetType(); }
2410
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002411 bool CanBeMoved() const OVERRIDE { return true; }
2412 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002413 UNUSED(other);
2414 return true;
2415 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002416
Roland Levillain9240d6a2014-10-20 16:47:04 +01002417 // Try to statically evaluate `operation` and return a HConstant
2418 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002419 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002420 HConstant* TryStaticEvaluation() const;
2421
2422 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002423 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2424 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002425
Roland Levillain88cb1752014-10-20 16:36:47 +01002426 DECLARE_INSTRUCTION(UnaryOperation);
2427
2428 private:
2429 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2430};
2431
Dave Allison20dfc792014-06-16 20:44:29 -07002432class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002433 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002434 HBinaryOperation(Primitive::Type result_type,
2435 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002436 HInstruction* right,
2437 SideEffects side_effects = SideEffects::None())
2438 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002439 SetRawInputAt(0, left);
2440 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002441 }
2442
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002443 HInstruction* GetLeft() const { return InputAt(0); }
2444 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002445 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002446
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002447 virtual bool IsCommutative() const { return false; }
2448
2449 // Put constant on the right.
2450 // Returns whether order is changed.
2451 bool OrderInputsWithConstantOnTheRight() {
2452 HInstruction* left = InputAt(0);
2453 HInstruction* right = InputAt(1);
2454 if (left->IsConstant() && !right->IsConstant()) {
2455 ReplaceInput(right, 0);
2456 ReplaceInput(left, 1);
2457 return true;
2458 }
2459 return false;
2460 }
2461
2462 // Order inputs by instruction id, but favor constant on the right side.
2463 // This helps GVN for commutative ops.
2464 void OrderInputs() {
2465 DCHECK(IsCommutative());
2466 HInstruction* left = InputAt(0);
2467 HInstruction* right = InputAt(1);
2468 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2469 return;
2470 }
2471 if (OrderInputsWithConstantOnTheRight()) {
2472 return;
2473 }
2474 // Order according to instruction id.
2475 if (left->GetId() > right->GetId()) {
2476 ReplaceInput(right, 0);
2477 ReplaceInput(left, 1);
2478 }
2479 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002481 bool CanBeMoved() const OVERRIDE { return true; }
2482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002483 UNUSED(other);
2484 return true;
2485 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002486
Roland Levillain9240d6a2014-10-20 16:47:04 +01002487 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002488 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002489 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002490 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002491
2492 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002493 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2494 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2495 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2496 HLongConstant* y ATTRIBUTE_UNUSED) const {
2497 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2498 return nullptr;
2499 }
2500 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2501 HIntConstant* y ATTRIBUTE_UNUSED) const {
2502 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2503 return nullptr;
2504 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002505
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002506 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002507 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002508 HConstant* GetConstantRight() const;
2509
2510 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002511 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002512 HInstruction* GetLeastConstantLeft() const;
2513
Roland Levillainccc07a92014-09-16 14:48:16 +01002514 DECLARE_INSTRUCTION(BinaryOperation);
2515
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002516 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002517 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2518};
2519
Mark Mendellc4701932015-04-10 13:18:51 -04002520// The comparison bias applies for floating point operations and indicates how NaN
2521// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002522enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002523 kNoBias, // bias is not applicable (i.e. for long operation)
2524 kGtBias, // return 1 for NaN comparisons
2525 kLtBias, // return -1 for NaN comparisons
2526};
2527
Dave Allison20dfc792014-06-16 20:44:29 -07002528class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002529 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002530 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002531 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002532 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002533 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002535 bool NeedsMaterialization() const { return needs_materialization_; }
2536 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002537
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002538 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002539 // `instruction`, and disregard moves in between.
2540 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002541
Dave Allison20dfc792014-06-16 20:44:29 -07002542 DECLARE_INSTRUCTION(Condition);
2543
2544 virtual IfCondition GetCondition() const = 0;
2545
Mark Mendellc4701932015-04-10 13:18:51 -04002546 virtual IfCondition GetOppositeCondition() const = 0;
2547
Roland Levillain4fa13f62015-07-06 18:11:54 +01002548 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002549
2550 void SetBias(ComparisonBias bias) { bias_ = bias; }
2551
2552 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2553 return bias_ == other->AsCondition()->bias_;
2554 }
2555
Roland Levillain4fa13f62015-07-06 18:11:54 +01002556 bool IsFPConditionTrueIfNaN() const {
2557 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2558 IfCondition if_cond = GetCondition();
2559 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2560 }
2561
2562 bool IsFPConditionFalseIfNaN() const {
2563 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2564 IfCondition if_cond = GetCondition();
2565 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2566 }
2567
Dave Allison20dfc792014-06-16 20:44:29 -07002568 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002569 // For register allocation purposes, returns whether this instruction needs to be
2570 // materialized (that is, not just be in the processor flags).
2571 bool needs_materialization_;
2572
Mark Mendellc4701932015-04-10 13:18:51 -04002573 // Needed if we merge a HCompare into a HCondition.
2574 ComparisonBias bias_;
2575
Dave Allison20dfc792014-06-16 20:44:29 -07002576 DISALLOW_COPY_AND_ASSIGN(HCondition);
2577};
2578
2579// Instruction to check if two inputs are equal to each other.
2580class HEqual : public HCondition {
2581 public:
2582 HEqual(HInstruction* first, HInstruction* second)
2583 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002584
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002585 bool IsCommutative() const OVERRIDE { return true; }
2586
Roland Levillain9867bc72015-08-05 10:21:34 +01002587 template <typename T> bool Compute(T x, T y) const { return x == y; }
2588
2589 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2590 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002591 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002592 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2593 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002594 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002595
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002596 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002598 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002599 return kCondEQ;
2600 }
2601
Mark Mendellc4701932015-04-10 13:18:51 -04002602 IfCondition GetOppositeCondition() const OVERRIDE {
2603 return kCondNE;
2604 }
2605
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002606 private:
2607 DISALLOW_COPY_AND_ASSIGN(HEqual);
2608};
2609
Dave Allison20dfc792014-06-16 20:44:29 -07002610class HNotEqual : public HCondition {
2611 public:
2612 HNotEqual(HInstruction* first, HInstruction* second)
2613 : HCondition(first, second) {}
2614
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002615 bool IsCommutative() const OVERRIDE { return true; }
2616
Roland Levillain9867bc72015-08-05 10:21:34 +01002617 template <typename T> bool Compute(T x, T y) const { return x != y; }
2618
2619 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2620 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002621 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002622 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2623 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002624 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002625
Dave Allison20dfc792014-06-16 20:44:29 -07002626 DECLARE_INSTRUCTION(NotEqual);
2627
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002628 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002629 return kCondNE;
2630 }
2631
Mark Mendellc4701932015-04-10 13:18:51 -04002632 IfCondition GetOppositeCondition() const OVERRIDE {
2633 return kCondEQ;
2634 }
2635
Dave Allison20dfc792014-06-16 20:44:29 -07002636 private:
2637 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2638};
2639
2640class HLessThan : public HCondition {
2641 public:
2642 HLessThan(HInstruction* first, HInstruction* second)
2643 : HCondition(first, second) {}
2644
Roland Levillain9867bc72015-08-05 10:21:34 +01002645 template <typename T> bool Compute(T x, T y) const { return x < y; }
2646
2647 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2648 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002649 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002650 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2651 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002652 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002653
Dave Allison20dfc792014-06-16 20:44:29 -07002654 DECLARE_INSTRUCTION(LessThan);
2655
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002656 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002657 return kCondLT;
2658 }
2659
Mark Mendellc4701932015-04-10 13:18:51 -04002660 IfCondition GetOppositeCondition() const OVERRIDE {
2661 return kCondGE;
2662 }
2663
Dave Allison20dfc792014-06-16 20:44:29 -07002664 private:
2665 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2666};
2667
2668class HLessThanOrEqual : public HCondition {
2669 public:
2670 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2671 : HCondition(first, second) {}
2672
Roland Levillain9867bc72015-08-05 10:21:34 +01002673 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2674
2675 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2676 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002677 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002678 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2679 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002680 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002681
Dave Allison20dfc792014-06-16 20:44:29 -07002682 DECLARE_INSTRUCTION(LessThanOrEqual);
2683
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002684 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002685 return kCondLE;
2686 }
2687
Mark Mendellc4701932015-04-10 13:18:51 -04002688 IfCondition GetOppositeCondition() const OVERRIDE {
2689 return kCondGT;
2690 }
2691
Dave Allison20dfc792014-06-16 20:44:29 -07002692 private:
2693 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2694};
2695
2696class HGreaterThan : public HCondition {
2697 public:
2698 HGreaterThan(HInstruction* first, HInstruction* second)
2699 : HCondition(first, second) {}
2700
Roland Levillain9867bc72015-08-05 10:21:34 +01002701 template <typename T> bool Compute(T x, T y) const { return x > y; }
2702
2703 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2704 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002705 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2707 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002708 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002709
Dave Allison20dfc792014-06-16 20:44:29 -07002710 DECLARE_INSTRUCTION(GreaterThan);
2711
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002712 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002713 return kCondGT;
2714 }
2715
Mark Mendellc4701932015-04-10 13:18:51 -04002716 IfCondition GetOppositeCondition() const OVERRIDE {
2717 return kCondLE;
2718 }
2719
Dave Allison20dfc792014-06-16 20:44:29 -07002720 private:
2721 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2722};
2723
2724class HGreaterThanOrEqual : public HCondition {
2725 public:
2726 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2727 : HCondition(first, second) {}
2728
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2730
2731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2732 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002733 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2735 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002736 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002737
Dave Allison20dfc792014-06-16 20:44:29 -07002738 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2739
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002740 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002741 return kCondGE;
2742 }
2743
Mark Mendellc4701932015-04-10 13:18:51 -04002744 IfCondition GetOppositeCondition() const OVERRIDE {
2745 return kCondLT;
2746 }
2747
Dave Allison20dfc792014-06-16 20:44:29 -07002748 private:
2749 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2750};
2751
2752
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002753// Instruction to check how two inputs compare to each other.
2754// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2755class HCompare : public HBinaryOperation {
2756 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002757 HCompare(Primitive::Type type,
2758 HInstruction* first,
2759 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002760 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002761 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002762 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2763 bias_(bias),
2764 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002765 DCHECK_EQ(type, first->GetType());
2766 DCHECK_EQ(type, second->GetType());
2767 }
2768
Roland Levillain9867bc72015-08-05 10:21:34 +01002769 template <typename T>
2770 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002771
Roland Levillain9867bc72015-08-05 10:21:34 +01002772 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2773 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2774 }
2775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2776 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002777 }
2778
Calin Juravleddb7df22014-11-25 20:56:51 +00002779 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2780 return bias_ == other->AsCompare()->bias_;
2781 }
2782
Mark Mendellc4701932015-04-10 13:18:51 -04002783 ComparisonBias GetBias() const { return bias_; }
2784
Roland Levillain4fa13f62015-07-06 18:11:54 +01002785 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002786
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002787 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2788
2789 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2790 // MIPS64 uses a runtime call for FP comparisons.
2791 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2792 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002793
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002794 DECLARE_INSTRUCTION(Compare);
2795
2796 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002797 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002798 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002799
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002800 DISALLOW_COPY_AND_ASSIGN(HCompare);
2801};
2802
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002803// A local in the graph. Corresponds to a Dex register.
2804class HLocal : public HTemplateInstruction<0> {
2805 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002806 explicit HLocal(uint16_t reg_number)
2807 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002809 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002810
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002811 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002812
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002813 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002814 // The Dex register number.
2815 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002816
2817 DISALLOW_COPY_AND_ASSIGN(HLocal);
2818};
2819
2820// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002821class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002822 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002823 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002824 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002825 SetRawInputAt(0, local);
2826 }
2827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002828 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002830 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002831
2832 private:
2833 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2834};
2835
2836// Store a value in a given local. This instruction has two inputs: the value
2837// and the local.
2838class HStoreLocal : public HTemplateInstruction<2> {
2839 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002840 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002841 SetRawInputAt(0, local);
2842 SetRawInputAt(1, value);
2843 }
2844
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002845 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2846
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002847 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002848
2849 private:
2850 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2851};
2852
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002853class HFloatConstant : public HConstant {
2854 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002855 float GetValue() const { return value_; }
2856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002857 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002858 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002859 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2860 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002861 }
2862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002863 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002864
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002865 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002866 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002867 }
2868 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002869 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002870 }
2871 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002872 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2873 }
2874 bool IsNaN() const {
2875 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002876 }
2877
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878 DECLARE_INSTRUCTION(FloatConstant);
2879
2880 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002881 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002882 explicit HFloatConstant(int32_t value)
2883 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002884
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002885 const float value_;
2886
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002887 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002888 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002889 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002890 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2891};
2892
2893class HDoubleConstant : public HConstant {
2894 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002895 double GetValue() const { return value_; }
2896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002897 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002898 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002899 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2900 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901 }
2902
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002903 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002904
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002905 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002906 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002907 }
2908 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002909 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002910 }
2911 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002912 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2913 }
2914 bool IsNaN() const {
2915 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002916 }
2917
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002918 DECLARE_INSTRUCTION(DoubleConstant);
2919
2920 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002921 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002922 explicit HDoubleConstant(int64_t value)
2923 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002924
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002925 const double value_;
2926
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002927 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002928 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002929 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002930 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2931};
2932
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002933enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002934#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002935#include "intrinsics_list.h"
2936 kNone,
2937 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2938#undef INTRINSICS_LIST
2939#undef OPTIMIZING_INTRINSICS
2940};
2941std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2942
Agi Csaki05f20562015-08-19 14:58:14 -07002943enum IntrinsicNeedsEnvironmentOrCache {
2944 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
2945 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07002946};
2947
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002948class HInvoke : public HInstruction {
2949 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002950 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002951
2952 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2953 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07002954 bool NeedsEnvironment() const OVERRIDE {
2955 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
2956 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002957
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002958 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002959 SetRawInputAt(index, argument);
2960 }
2961
Roland Levillain3e3d7332015-04-28 11:00:54 +01002962 // Return the number of arguments. This number can be lower than
2963 // the number of inputs returned by InputCount(), as some invoke
2964 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2965 // inputs at the end of their list of inputs.
2966 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2967
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002968 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002969
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002970 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002971
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002972 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002973 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002974
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002975 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2976
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002977 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002978 return intrinsic_;
2979 }
2980
Agi Csaki05f20562015-08-19 14:58:14 -07002981 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002982 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07002983 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002984 }
2985
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002986 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002987 return GetEnvironment()->GetParent() != nullptr;
2988 }
2989
2990 bool CanThrow() const OVERRIDE { return true; }
2991
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002992 DECLARE_INSTRUCTION(Invoke);
2993
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002994 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002995 HInvoke(ArenaAllocator* arena,
2996 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002997 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002998 Primitive::Type return_type,
2999 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003000 uint32_t dex_method_index,
3001 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003002 : HInstruction(
3003 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003004 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003005 inputs_(arena, number_of_arguments),
3006 return_type_(return_type),
3007 dex_pc_(dex_pc),
3008 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003009 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003010 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003011 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003012 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
3013 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003014 }
3015
David Brazdil1abb4192015-02-17 18:33:36 +00003016 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3017 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3018 inputs_.Put(index, input);
3019 }
3020
Roland Levillain3e3d7332015-04-28 11:00:54 +01003021 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003022 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003024 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003025 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003026 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003027 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003028 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003029
3030 private:
3031 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3032};
3033
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003034class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003035 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003036 // Requirements of this method call regarding the class
3037 // initialization (clinit) check of its declaring class.
3038 enum class ClinitCheckRequirement {
3039 kNone, // Class already initialized.
3040 kExplicit, // Static call having explicit clinit check as last input.
3041 kImplicit, // Static call implicitly requiring a clinit check.
3042 };
3043
Vladimir Marko58155012015-08-19 12:49:41 +00003044 // Determines how to load the target ArtMethod*.
3045 enum class MethodLoadKind {
3046 // Use a String init ArtMethod* loaded from Thread entrypoints.
3047 kStringInit,
3048
3049 // Use the method's own ArtMethod* loaded by the register allocator.
3050 kRecursive,
3051
3052 // Use ArtMethod* at a known address, embed the direct address in the code.
3053 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3054 kDirectAddress,
3055
3056 // Use ArtMethod* at an address that will be known at link time, embed the direct
3057 // address in the code. If the image is relocatable, emit .patch_oat entry.
3058 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3059 // the image relocatable or not.
3060 kDirectAddressWithFixup,
3061
3062 // Load from resoved methods array in the dex cache using a PC-relative load.
3063 // Used when we need to use the dex cache, for example for invoke-static that
3064 // may cause class initialization (the entry may point to a resolution method),
3065 // and we know that we can access the dex cache arrays using a PC-relative load.
3066 kDexCachePcRelative,
3067
3068 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3069 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3070 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3071 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3072 kDexCacheViaMethod,
3073 };
3074
3075 // Determines the location of the code pointer.
3076 enum class CodePtrLocation {
3077 // Recursive call, use local PC-relative call instruction.
3078 kCallSelf,
3079
3080 // Use PC-relative call instruction patched at link time.
3081 // Used for calls within an oat file, boot->boot or app->app.
3082 kCallPCRelative,
3083
3084 // Call to a known target address, embed the direct address in code.
3085 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3086 kCallDirect,
3087
3088 // Call to a target address that will be known at link time, embed the direct
3089 // address in code. If the image is relocatable, emit .patch_oat entry.
3090 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3091 // the image relocatable or not.
3092 kCallDirectWithFixup,
3093
3094 // Use code pointer from the ArtMethod*.
3095 // Used when we don't know the target code. This is also the last-resort-kind used when
3096 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3097 kCallArtMethod,
3098 };
3099
3100 struct DispatchInfo {
3101 const MethodLoadKind method_load_kind;
3102 const CodePtrLocation code_ptr_location;
3103 // The method load data holds
3104 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3105 // Note that there are multiple string init methods, each having its own offset.
3106 // - the method address for kDirectAddress
3107 // - the dex cache arrays offset for kDexCachePcRel.
3108 const uint64_t method_load_data;
3109 const uint64_t direct_code_ptr;
3110 };
3111
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003112 HInvokeStaticOrDirect(ArenaAllocator* arena,
3113 uint32_t number_of_arguments,
3114 Primitive::Type return_type,
3115 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003116 uint32_t method_index,
3117 MethodReference target_method,
3118 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003119 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003120 InvokeType invoke_type,
3121 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003122 : HInvoke(arena,
3123 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003124 // There is one extra argument for the HCurrentMethod node, and
3125 // potentially one other if the clinit check is explicit, and one other
3126 // if the method is a string factory.
3127 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003128 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003129 return_type,
3130 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003131 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003132 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003133 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003134 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003135 target_method_(target_method),
3136 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003137
Calin Juravle641547a2015-04-21 22:08:51 +01003138 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3139 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003140 // We access the method via the dex cache so we can't do an implicit null check.
3141 // TODO: for intrinsics we can generate implicit null checks.
3142 return false;
3143 }
3144
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003145 bool CanBeNull() const OVERRIDE {
3146 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3147 }
3148
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003149 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003150 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3151 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3152 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003153 bool NeedsDexCache() const OVERRIDE {
3154 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3155 return !IsRecursive() && !IsStringInit();
3156 }
Vladimir Marko58155012015-08-19 12:49:41 +00003157 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003158 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003159 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3160 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3161 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3162 MethodReference GetTargetMethod() const { return target_method_; }
3163
3164 int32_t GetStringInitOffset() const {
3165 DCHECK(IsStringInit());
3166 return dispatch_info_.method_load_data;
3167 }
3168
3169 uint64_t GetMethodAddress() const {
3170 DCHECK(HasMethodAddress());
3171 return dispatch_info_.method_load_data;
3172 }
3173
3174 uint32_t GetDexCacheArrayOffset() const {
3175 DCHECK(HasPcRelDexCache());
3176 return dispatch_info_.method_load_data;
3177 }
3178
3179 uint64_t GetDirectCodePtr() const {
3180 DCHECK(HasDirectCodePtr());
3181 return dispatch_info_.direct_code_ptr;
3182 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003183
Calin Juravle68ad6492015-08-18 17:08:12 +01003184 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3185
Roland Levillain4c0eb422015-04-24 16:43:49 +01003186 // Is this instruction a call to a static method?
3187 bool IsStatic() const {
3188 return GetInvokeType() == kStatic;
3189 }
3190
Roland Levillain3e3d7332015-04-28 11:00:54 +01003191 // Remove the art::HLoadClass instruction set as last input by
3192 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3193 // the initial art::HClinitCheck instruction (only relevant for
3194 // static calls with explicit clinit check).
3195 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003196 DCHECK(IsStaticWithExplicitClinitCheck());
3197 size_t last_input_index = InputCount() - 1;
3198 HInstruction* last_input = InputAt(last_input_index);
3199 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003200 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003201 RemoveAsUserOfInput(last_input_index);
3202 inputs_.DeleteAt(last_input_index);
3203 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3204 DCHECK(IsStaticWithImplicitClinitCheck());
3205 }
3206
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003207 bool IsStringFactoryFor(HFakeString* str) const {
3208 if (!IsStringInit()) return false;
3209 // +1 for the current method.
3210 if (InputCount() == (number_of_arguments_ + 1)) return false;
3211 return InputAt(InputCount() - 1)->AsFakeString() == str;
3212 }
3213
3214 void RemoveFakeStringArgumentAsLastInput() {
3215 DCHECK(IsStringInit());
3216 size_t last_input_index = InputCount() - 1;
3217 HInstruction* last_input = InputAt(last_input_index);
3218 DCHECK(last_input != nullptr);
3219 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3220 RemoveAsUserOfInput(last_input_index);
3221 inputs_.DeleteAt(last_input_index);
3222 }
3223
Roland Levillain4c0eb422015-04-24 16:43:49 +01003224 // Is this a call to a static method whose declaring class has an
3225 // explicit intialization check in the graph?
3226 bool IsStaticWithExplicitClinitCheck() const {
3227 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3228 }
3229
3230 // Is this a call to a static method whose declaring class has an
3231 // implicit intialization check requirement?
3232 bool IsStaticWithImplicitClinitCheck() const {
3233 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3234 }
3235
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003236 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003237
Roland Levillain4c0eb422015-04-24 16:43:49 +01003238 protected:
3239 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3240 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3241 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3242 HInstruction* input = input_record.GetInstruction();
3243 // `input` is the last input of a static invoke marked as having
3244 // an explicit clinit check. It must either be:
3245 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3246 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3247 DCHECK(input != nullptr);
3248 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3249 }
3250 return input_record;
3251 }
3252
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003253 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003254 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003255 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003256 // The target method may refer to different dex file or method index than the original
3257 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3258 // in derived class to increase visibility.
3259 MethodReference target_method_;
3260 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003262 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003263};
3264
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003265class HInvokeVirtual : public HInvoke {
3266 public:
3267 HInvokeVirtual(ArenaAllocator* arena,
3268 uint32_t number_of_arguments,
3269 Primitive::Type return_type,
3270 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003271 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003272 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003273 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003274 vtable_index_(vtable_index) {}
3275
Calin Juravle641547a2015-04-21 22:08:51 +01003276 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003277 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003278 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003279 }
3280
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003281 uint32_t GetVTableIndex() const { return vtable_index_; }
3282
3283 DECLARE_INSTRUCTION(InvokeVirtual);
3284
3285 private:
3286 const uint32_t vtable_index_;
3287
3288 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3289};
3290
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003291class HInvokeInterface : public HInvoke {
3292 public:
3293 HInvokeInterface(ArenaAllocator* arena,
3294 uint32_t number_of_arguments,
3295 Primitive::Type return_type,
3296 uint32_t dex_pc,
3297 uint32_t dex_method_index,
3298 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003299 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003300 imt_index_(imt_index) {}
3301
Calin Juravle641547a2015-04-21 22:08:51 +01003302 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003303 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003304 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003305 }
3306
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003307 uint32_t GetImtIndex() const { return imt_index_; }
3308 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3309
3310 DECLARE_INSTRUCTION(InvokeInterface);
3311
3312 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003313 const uint32_t imt_index_;
3314
3315 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3316};
3317
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003318class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003319 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003320 HNewInstance(HCurrentMethod* current_method,
3321 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003322 uint16_t type_index,
3323 const DexFile& dex_file,
3324 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003325 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003326 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003327 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003328 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003329 entrypoint_(entrypoint) {
3330 SetRawInputAt(0, current_method);
3331 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003332
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003333 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003334 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003335 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003336
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003337 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003338 bool NeedsEnvironment() const OVERRIDE { return true; }
3339 // It may throw when called on:
3340 // - interfaces
3341 // - abstract/innaccessible/unknown classes
3342 // TODO: optimize when possible.
3343 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003344
Calin Juravle10e244f2015-01-26 18:54:32 +00003345 bool CanBeNull() const OVERRIDE { return false; }
3346
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003347 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3348
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003349 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003350
3351 private:
3352 const uint32_t dex_pc_;
3353 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003354 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003355 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003356
3357 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3358};
3359
Roland Levillain88cb1752014-10-20 16:36:47 +01003360class HNeg : public HUnaryOperation {
3361 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003362 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003363 : HUnaryOperation(result_type, input) {}
3364
Roland Levillain9867bc72015-08-05 10:21:34 +01003365 template <typename T> T Compute(T x) const { return -x; }
3366
3367 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3368 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3369 }
3370 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3371 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3372 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003373
Roland Levillain88cb1752014-10-20 16:36:47 +01003374 DECLARE_INSTRUCTION(Neg);
3375
3376 private:
3377 DISALLOW_COPY_AND_ASSIGN(HNeg);
3378};
3379
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003380class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003381 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003382 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003383 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003384 uint32_t dex_pc,
3385 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003386 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003387 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003388 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003389 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003390 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003391 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003392 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003393 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003394 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003395 }
3396
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003397 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003398 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003399 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003400
3401 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003402 bool NeedsEnvironment() const OVERRIDE { return true; }
3403
Mingyao Yang0c365e62015-03-31 15:09:29 -07003404 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3405 bool CanThrow() const OVERRIDE { return true; }
3406
Calin Juravle10e244f2015-01-26 18:54:32 +00003407 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003408
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003409 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3410
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003411 DECLARE_INSTRUCTION(NewArray);
3412
3413 private:
3414 const uint32_t dex_pc_;
3415 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003416 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003417 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003418
3419 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3420};
3421
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003422class HAdd : public HBinaryOperation {
3423 public:
3424 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3425 : HBinaryOperation(result_type, left, right) {}
3426
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003427 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003428
Roland Levillain9867bc72015-08-05 10:21:34 +01003429 template <typename T> T Compute(T x, T y) const { return x + y; }
3430
3431 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3432 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003433 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003434 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3435 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003436 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003437
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003438 DECLARE_INSTRUCTION(Add);
3439
3440 private:
3441 DISALLOW_COPY_AND_ASSIGN(HAdd);
3442};
3443
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003444class HSub : public HBinaryOperation {
3445 public:
3446 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3447 : HBinaryOperation(result_type, left, right) {}
3448
Roland Levillain9867bc72015-08-05 10:21:34 +01003449 template <typename T> T Compute(T x, T y) const { return x - y; }
3450
3451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3452 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003453 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003454 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3455 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003456 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003457
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003458 DECLARE_INSTRUCTION(Sub);
3459
3460 private:
3461 DISALLOW_COPY_AND_ASSIGN(HSub);
3462};
3463
Calin Juravle34bacdf2014-10-07 20:23:36 +01003464class HMul : public HBinaryOperation {
3465 public:
3466 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3467 : HBinaryOperation(result_type, left, right) {}
3468
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003469 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003470
Roland Levillain9867bc72015-08-05 10:21:34 +01003471 template <typename T> T Compute(T x, T y) const { return x * y; }
3472
3473 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3474 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3475 }
3476 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3477 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3478 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003479
3480 DECLARE_INSTRUCTION(Mul);
3481
3482 private:
3483 DISALLOW_COPY_AND_ASSIGN(HMul);
3484};
3485
Calin Juravle7c4954d2014-10-28 16:57:40 +00003486class HDiv : public HBinaryOperation {
3487 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003488 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003489 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3490 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003491
Roland Levillain9867bc72015-08-05 10:21:34 +01003492 template <typename T>
3493 T Compute(T x, T y) const {
3494 // Our graph structure ensures we never have 0 for `y` during
3495 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003496 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003497 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003498 return (y == -1) ? -x : x / y;
3499 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003500
Roland Levillain9867bc72015-08-05 10:21:34 +01003501 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3502 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3503 }
3504 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3505 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003506 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003507
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003508 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003509
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003510 static SideEffects SideEffectsForArchRuntimeCalls() {
3511 // The generated code can use a runtime call.
3512 return SideEffects::CanTriggerGC();
3513 }
3514
Calin Juravle7c4954d2014-10-28 16:57:40 +00003515 DECLARE_INSTRUCTION(Div);
3516
3517 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003518 const uint32_t dex_pc_;
3519
Calin Juravle7c4954d2014-10-28 16:57:40 +00003520 DISALLOW_COPY_AND_ASSIGN(HDiv);
3521};
3522
Calin Juravlebacfec32014-11-14 15:54:36 +00003523class HRem : public HBinaryOperation {
3524 public:
3525 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003526 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3527 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003528
Roland Levillain9867bc72015-08-05 10:21:34 +01003529 template <typename T>
3530 T Compute(T x, T y) const {
3531 // Our graph structure ensures we never have 0 for `y` during
3532 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003533 DCHECK_NE(y, 0);
3534 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3535 return (y == -1) ? 0 : x % y;
3536 }
3537
Roland Levillain9867bc72015-08-05 10:21:34 +01003538 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3539 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3540 }
3541 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3542 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 }
3544
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003545 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003547 static SideEffects SideEffectsForArchRuntimeCalls() {
3548 return SideEffects::CanTriggerGC();
3549 }
3550
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 DECLARE_INSTRUCTION(Rem);
3552
3553 private:
3554 const uint32_t dex_pc_;
3555
3556 DISALLOW_COPY_AND_ASSIGN(HRem);
3557};
3558
Calin Juravled0d48522014-11-04 16:40:20 +00003559class HDivZeroCheck : public HExpression<1> {
3560 public:
3561 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3562 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3563 SetRawInputAt(0, value);
3564 }
3565
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003566 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3567
Calin Juravled0d48522014-11-04 16:40:20 +00003568 bool CanBeMoved() const OVERRIDE { return true; }
3569
3570 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3571 UNUSED(other);
3572 return true;
3573 }
3574
3575 bool NeedsEnvironment() const OVERRIDE { return true; }
3576 bool CanThrow() const OVERRIDE { return true; }
3577
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003578 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003579
3580 DECLARE_INSTRUCTION(DivZeroCheck);
3581
3582 private:
3583 const uint32_t dex_pc_;
3584
3585 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3586};
3587
Calin Juravle9aec02f2014-11-18 23:06:35 +00003588class HShl : public HBinaryOperation {
3589 public:
3590 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3591 : HBinaryOperation(result_type, left, right) {}
3592
Roland Levillain9867bc72015-08-05 10:21:34 +01003593 template <typename T, typename U, typename V>
3594 T Compute(T x, U y, V max_shift_value) const {
3595 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3596 "V is not the unsigned integer type corresponding to T");
3597 return x << (y & max_shift_value);
3598 }
3599
3600 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3601 return GetBlock()->GetGraph()->GetIntConstant(
3602 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3603 }
3604 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3605 // case is handled as `x << static_cast<int>(y)`.
3606 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3607 return GetBlock()->GetGraph()->GetLongConstant(
3608 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3609 }
3610 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3611 return GetBlock()->GetGraph()->GetLongConstant(
3612 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3613 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003614
3615 DECLARE_INSTRUCTION(Shl);
3616
3617 private:
3618 DISALLOW_COPY_AND_ASSIGN(HShl);
3619};
3620
3621class HShr : public HBinaryOperation {
3622 public:
3623 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3624 : HBinaryOperation(result_type, left, right) {}
3625
Roland Levillain9867bc72015-08-05 10:21:34 +01003626 template <typename T, typename U, typename V>
3627 T Compute(T x, U y, V max_shift_value) const {
3628 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3629 "V is not the unsigned integer type corresponding to T");
3630 return x >> (y & max_shift_value);
3631 }
3632
3633 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3634 return GetBlock()->GetGraph()->GetIntConstant(
3635 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3636 }
3637 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3638 // case is handled as `x >> static_cast<int>(y)`.
3639 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3640 return GetBlock()->GetGraph()->GetLongConstant(
3641 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3642 }
3643 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3644 return GetBlock()->GetGraph()->GetLongConstant(
3645 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3646 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003647
3648 DECLARE_INSTRUCTION(Shr);
3649
3650 private:
3651 DISALLOW_COPY_AND_ASSIGN(HShr);
3652};
3653
3654class HUShr : public HBinaryOperation {
3655 public:
3656 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3657 : HBinaryOperation(result_type, left, right) {}
3658
Roland Levillain9867bc72015-08-05 10:21:34 +01003659 template <typename T, typename U, typename V>
3660 T Compute(T x, U y, V max_shift_value) const {
3661 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3662 "V is not the unsigned integer type corresponding to T");
3663 V ux = static_cast<V>(x);
3664 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003665 }
3666
Roland Levillain9867bc72015-08-05 10:21:34 +01003667 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3668 return GetBlock()->GetGraph()->GetIntConstant(
3669 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3670 }
3671 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3672 // case is handled as `x >>> static_cast<int>(y)`.
3673 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3674 return GetBlock()->GetGraph()->GetLongConstant(
3675 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3676 }
3677 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3678 return GetBlock()->GetGraph()->GetLongConstant(
3679 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003680 }
3681
3682 DECLARE_INSTRUCTION(UShr);
3683
3684 private:
3685 DISALLOW_COPY_AND_ASSIGN(HUShr);
3686};
3687
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003688class HAnd : public HBinaryOperation {
3689 public:
3690 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3691 : HBinaryOperation(result_type, left, right) {}
3692
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003693 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003694
Roland Levillain9867bc72015-08-05 10:21:34 +01003695 template <typename T, typename U>
3696 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3697
3698 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3699 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3700 }
3701 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3702 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3703 }
3704 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3705 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3706 }
3707 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3708 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3709 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003710
3711 DECLARE_INSTRUCTION(And);
3712
3713 private:
3714 DISALLOW_COPY_AND_ASSIGN(HAnd);
3715};
3716
3717class HOr : public HBinaryOperation {
3718 public:
3719 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3720 : HBinaryOperation(result_type, left, right) {}
3721
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003722 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003723
Roland Levillain9867bc72015-08-05 10:21:34 +01003724 template <typename T, typename U>
3725 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3726
3727 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3728 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3729 }
3730 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3731 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3732 }
3733 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3734 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3735 }
3736 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3737 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3738 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003739
3740 DECLARE_INSTRUCTION(Or);
3741
3742 private:
3743 DISALLOW_COPY_AND_ASSIGN(HOr);
3744};
3745
3746class HXor : public HBinaryOperation {
3747 public:
3748 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3749 : HBinaryOperation(result_type, left, right) {}
3750
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003751 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003752
Roland Levillain9867bc72015-08-05 10:21:34 +01003753 template <typename T, typename U>
3754 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3755
3756 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3757 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3758 }
3759 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3760 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3761 }
3762 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3763 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3764 }
3765 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3766 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3767 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003768
3769 DECLARE_INSTRUCTION(Xor);
3770
3771 private:
3772 DISALLOW_COPY_AND_ASSIGN(HXor);
3773};
3774
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003775// The value of a parameter in this method. Its location depends on
3776// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003777class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003778 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003779 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003780 : HExpression(parameter_type, SideEffects::None()),
3781 index_(index),
3782 is_this_(is_this),
3783 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003784
3785 uint8_t GetIndex() const { return index_; }
3786
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003787 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3788 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003789
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003790 bool IsThis() const { return is_this_; }
3791
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003792 DECLARE_INSTRUCTION(ParameterValue);
3793
3794 private:
3795 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003796 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003797 const uint8_t index_;
3798
Calin Juravle10e244f2015-01-26 18:54:32 +00003799 // Whether or not the parameter value corresponds to 'this' argument.
3800 const bool is_this_;
3801
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003802 bool can_be_null_;
3803
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003804 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3805};
3806
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003807class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003808 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003809 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003810 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003812 bool CanBeMoved() const OVERRIDE { return true; }
3813 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003814 UNUSED(other);
3815 return true;
3816 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003817
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 template <typename T> T Compute(T x) const { return ~x; }
3819
3820 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3821 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3822 }
3823 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3824 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3825 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003826
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003827 DECLARE_INSTRUCTION(Not);
3828
3829 private:
3830 DISALLOW_COPY_AND_ASSIGN(HNot);
3831};
3832
David Brazdil66d126e2015-04-03 16:02:44 +01003833class HBooleanNot : public HUnaryOperation {
3834 public:
3835 explicit HBooleanNot(HInstruction* input)
3836 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3837
3838 bool CanBeMoved() const OVERRIDE { return true; }
3839 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3840 UNUSED(other);
3841 return true;
3842 }
3843
Roland Levillain9867bc72015-08-05 10:21:34 +01003844 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003845 DCHECK(IsUint<1>(x));
3846 return !x;
3847 }
3848
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3850 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3851 }
3852 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3853 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003854 UNREACHABLE();
3855 }
3856
3857 DECLARE_INSTRUCTION(BooleanNot);
3858
3859 private:
3860 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3861};
3862
Roland Levillaindff1f282014-11-05 14:15:05 +00003863class HTypeConversion : public HExpression<1> {
3864 public:
3865 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003866 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003867 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3868 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003869 SetRawInputAt(0, input);
3870 DCHECK_NE(input->GetType(), result_type);
3871 }
3872
3873 HInstruction* GetInput() const { return InputAt(0); }
3874 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3875 Primitive::Type GetResultType() const { return GetType(); }
3876
Roland Levillain624279f2014-12-04 11:54:28 +00003877 // Required by the x86 and ARM code generators when producing calls
3878 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003879 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003880
Roland Levillaindff1f282014-11-05 14:15:05 +00003881 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003882 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003883
Mark Mendelle82549b2015-05-06 10:55:34 -04003884 // Try to statically evaluate the conversion and return a HConstant
3885 // containing the result. If the input cannot be converted, return nullptr.
3886 HConstant* TryStaticEvaluation() const;
3887
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003888 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3889 Primitive::Type result_type) {
3890 // Some architectures may not require the 'GC' side effects, but at this point
3891 // in the compilation process we do not know what architecture we will
3892 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003893 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3894 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003895 return SideEffects::CanTriggerGC();
3896 }
3897 return SideEffects::None();
3898 }
3899
Roland Levillaindff1f282014-11-05 14:15:05 +00003900 DECLARE_INSTRUCTION(TypeConversion);
3901
3902 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003903 const uint32_t dex_pc_;
3904
Roland Levillaindff1f282014-11-05 14:15:05 +00003905 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3906};
3907
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003908static constexpr uint32_t kNoRegNumber = -1;
3909
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003910class HPhi : public HInstruction {
3911 public:
3912 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003913 : HInstruction(SideEffects::None()),
3914 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003915 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003916 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003917 is_live_(false),
3918 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003919 inputs_.SetSize(number_of_inputs);
3920 }
3921
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003922 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3923 static Primitive::Type ToPhiType(Primitive::Type type) {
3924 switch (type) {
3925 case Primitive::kPrimBoolean:
3926 case Primitive::kPrimByte:
3927 case Primitive::kPrimShort:
3928 case Primitive::kPrimChar:
3929 return Primitive::kPrimInt;
3930 default:
3931 return type;
3932 }
3933 }
3934
David Brazdilffee3d32015-07-06 11:48:53 +01003935 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3936
Calin Juravle10e244f2015-01-26 18:54:32 +00003937 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003938
3939 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003940 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003941
Calin Juravle10e244f2015-01-26 18:54:32 +00003942 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003943 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003944
Calin Juravle10e244f2015-01-26 18:54:32 +00003945 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3946 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3947
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003948 uint32_t GetRegNumber() const { return reg_number_; }
3949
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003950 void SetDead() { is_live_ = false; }
3951 void SetLive() { is_live_ = true; }
3952 bool IsDead() const { return !is_live_; }
3953 bool IsLive() const { return is_live_; }
3954
Calin Juravlea4f88312015-04-16 12:57:19 +01003955 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3956 // An equivalent phi is a phi having the same dex register and type.
3957 // It assumes that phis with the same dex register are adjacent.
3958 HPhi* GetNextEquivalentPhiWithSameType() {
3959 HInstruction* next = GetNext();
3960 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3961 if (next->GetType() == GetType()) {
3962 return next->AsPhi();
3963 }
3964 next = next->GetNext();
3965 }
3966 return nullptr;
3967 }
3968
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003969 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003970
David Brazdil1abb4192015-02-17 18:33:36 +00003971 protected:
3972 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3973
3974 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3975 inputs_.Put(index, input);
3976 }
3977
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003978 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003979 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003980 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003981 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003982 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003983 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003984
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003985 DISALLOW_COPY_AND_ASSIGN(HPhi);
3986};
3987
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003988class HNullCheck : public HExpression<1> {
3989 public:
3990 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003991 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003992 SetRawInputAt(0, value);
3993 }
3994
Calin Juravle10e244f2015-01-26 18:54:32 +00003995 bool CanBeMoved() const OVERRIDE { return true; }
3996 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003997 UNUSED(other);
3998 return true;
3999 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004000
Calin Juravle10e244f2015-01-26 18:54:32 +00004001 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004002
Calin Juravle10e244f2015-01-26 18:54:32 +00004003 bool CanThrow() const OVERRIDE { return true; }
4004
4005 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004006
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004007 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008
4009 DECLARE_INSTRUCTION(NullCheck);
4010
4011 private:
4012 const uint32_t dex_pc_;
4013
4014 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4015};
4016
4017class FieldInfo : public ValueObject {
4018 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004019 FieldInfo(MemberOffset field_offset,
4020 Primitive::Type field_type,
4021 bool is_volatile,
4022 uint32_t index,
4023 const DexFile& dex_file)
4024 : field_offset_(field_offset),
4025 field_type_(field_type),
4026 is_volatile_(is_volatile),
4027 index_(index),
4028 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004029
4030 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004031 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004032 uint32_t GetFieldIndex() const { return index_; }
4033 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004034 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004035
4036 private:
4037 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004038 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004039 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004040 uint32_t index_;
4041 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004042};
4043
4044class HInstanceFieldGet : public HExpression<1> {
4045 public:
4046 HInstanceFieldGet(HInstruction* value,
4047 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004048 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004049 bool is_volatile,
4050 uint32_t field_idx,
4051 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004052 : HExpression(
4053 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004054 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004055 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004056 SetRawInputAt(0, value);
4057 }
4058
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004059 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004060
4061 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4062 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4063 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004064 }
4065
Calin Juravle641547a2015-04-21 22:08:51 +01004066 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4067 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004068 }
4069
4070 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004071 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4072 }
4073
Calin Juravle52c48962014-12-16 17:02:57 +00004074 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004075 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004076 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004077 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004078
4079 DECLARE_INSTRUCTION(InstanceFieldGet);
4080
4081 private:
4082 const FieldInfo field_info_;
4083
4084 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4085};
4086
4087class HInstanceFieldSet : public HTemplateInstruction<2> {
4088 public:
4089 HInstanceFieldSet(HInstruction* object,
4090 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004091 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004092 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004093 bool is_volatile,
4094 uint32_t field_idx,
4095 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004096 : HTemplateInstruction(
4097 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004098 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004099 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004100 SetRawInputAt(0, object);
4101 SetRawInputAt(1, value);
4102 }
4103
Calin Juravle641547a2015-04-21 22:08:51 +01004104 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4105 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004106 }
4107
Calin Juravle52c48962014-12-16 17:02:57 +00004108 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004109 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004110 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004111 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004112 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004113 bool GetValueCanBeNull() const { return value_can_be_null_; }
4114 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004115
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004116 DECLARE_INSTRUCTION(InstanceFieldSet);
4117
4118 private:
4119 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004120 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004121
4122 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4123};
4124
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004125class HArrayGet : public HExpression<2> {
4126 public:
4127 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004128 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004129 SetRawInputAt(0, array);
4130 SetRawInputAt(1, index);
4131 }
4132
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004133 bool CanBeMoved() const OVERRIDE { return true; }
4134 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004135 UNUSED(other);
4136 return true;
4137 }
Calin Juravle641547a2015-04-21 22:08:51 +01004138 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4139 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004140 // TODO: We can be smarter here.
4141 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4142 // which generates the implicit null check. There are cases when these can be removed
4143 // to produce better code. If we ever add optimizations to do so we should allow an
4144 // implicit check here (as long as the address falls in the first page).
4145 return false;
4146 }
4147
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004148 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004149
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004150 HInstruction* GetArray() const { return InputAt(0); }
4151 HInstruction* GetIndex() const { return InputAt(1); }
4152
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004153 DECLARE_INSTRUCTION(ArrayGet);
4154
4155 private:
4156 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4157};
4158
4159class HArraySet : public HTemplateInstruction<3> {
4160 public:
4161 HArraySet(HInstruction* array,
4162 HInstruction* index,
4163 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004164 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004165 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004166 : HTemplateInstruction(
4167 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4168 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004169 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004170 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004171 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4172 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004173 SetRawInputAt(0, array);
4174 SetRawInputAt(1, index);
4175 SetRawInputAt(2, value);
4176 }
4177
Calin Juravle77520bc2015-01-12 18:45:46 +00004178 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 // We currently always call a runtime method to catch array store
4180 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004181 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182 }
4183
Mingyao Yang81014cb2015-06-02 03:16:27 -07004184 // Can throw ArrayStoreException.
4185 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4186
Calin Juravle641547a2015-04-21 22:08:51 +01004187 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4188 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004189 // TODO: Same as for ArrayGet.
4190 return false;
4191 }
4192
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004193 void ClearNeedsTypeCheck() {
4194 needs_type_check_ = false;
4195 }
4196
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004197 void ClearValueCanBeNull() {
4198 value_can_be_null_ = false;
4199 }
4200
4201 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004202 bool NeedsTypeCheck() const { return needs_type_check_; }
4203
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004204 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004205
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004206 HInstruction* GetArray() const { return InputAt(0); }
4207 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004208 HInstruction* GetValue() const { return InputAt(2); }
4209
4210 Primitive::Type GetComponentType() const {
4211 // The Dex format does not type floating point index operations. Since the
4212 // `expected_component_type_` is set during building and can therefore not
4213 // be correct, we also check what is the value type. If it is a floating
4214 // point type, we must use that type.
4215 Primitive::Type value_type = GetValue()->GetType();
4216 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4217 ? value_type
4218 : expected_component_type_;
4219 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004220
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004221 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4222 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4223 }
4224
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004225 DECLARE_INSTRUCTION(ArraySet);
4226
4227 private:
4228 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004229 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004230 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004231 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232
4233 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4234};
4235
4236class HArrayLength : public HExpression<1> {
4237 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004238 explicit HArrayLength(HInstruction* array)
4239 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4240 // Note that arrays do not change length, so the instruction does not
4241 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 SetRawInputAt(0, array);
4243 }
4244
Calin Juravle77520bc2015-01-12 18:45:46 +00004245 bool CanBeMoved() const OVERRIDE { return true; }
4246 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004247 UNUSED(other);
4248 return true;
4249 }
Calin Juravle641547a2015-04-21 22:08:51 +01004250 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4251 return obj == InputAt(0);
4252 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004254 DECLARE_INSTRUCTION(ArrayLength);
4255
4256 private:
4257 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4258};
4259
4260class HBoundsCheck : public HExpression<2> {
4261 public:
4262 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004263 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004264 DCHECK(index->GetType() == Primitive::kPrimInt);
4265 SetRawInputAt(0, index);
4266 SetRawInputAt(1, length);
4267 }
4268
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004269 bool CanBeMoved() const OVERRIDE { return true; }
4270 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004271 UNUSED(other);
4272 return true;
4273 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004275 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004277 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004278
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004279 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004280
4281 DECLARE_INSTRUCTION(BoundsCheck);
4282
4283 private:
4284 const uint32_t dex_pc_;
4285
4286 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4287};
4288
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289/**
4290 * Some DEX instructions are folded into multiple HInstructions that need
4291 * to stay live until the last HInstruction. This class
4292 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004293 * HInstruction stays live. `index` represents the stack location index of the
4294 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295 */
4296class HTemporary : public HTemplateInstruction<0> {
4297 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004298 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299
4300 size_t GetIndex() const { return index_; }
4301
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004302 Primitive::Type GetType() const OVERRIDE {
4303 // The previous instruction is the one that will be stored in the temporary location.
4304 DCHECK(GetPrevious() != nullptr);
4305 return GetPrevious()->GetType();
4306 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004307
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308 DECLARE_INSTRUCTION(Temporary);
4309
4310 private:
4311 const size_t index_;
4312
4313 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4314};
4315
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004316class HSuspendCheck : public HTemplateInstruction<0> {
4317 public:
4318 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004319 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004321 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004322 return true;
4323 }
4324
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004325 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004326 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4327 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004328
4329 DECLARE_INSTRUCTION(SuspendCheck);
4330
4331 private:
4332 const uint32_t dex_pc_;
4333
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004334 // Only used for code generation, in order to share the same slow path between back edges
4335 // of a same loop.
4336 SlowPathCode* slow_path_;
4337
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004338 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4339};
4340
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004341/**
4342 * Instruction to load a Class object.
4343 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004344class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004345 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004346 HLoadClass(HCurrentMethod* current_method,
4347 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004348 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004349 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004350 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004351 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004352 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004353 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004354 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004355 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004356 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004357 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004358 SetRawInputAt(0, current_method);
4359 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004360
4361 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004362
4363 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4364 return other->AsLoadClass()->type_index_ == type_index_;
4365 }
4366
4367 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4368
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004369 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004370 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004371 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004372 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004373
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004374 bool NeedsEnvironment() const OVERRIDE {
4375 // Will call runtime and load the class if the class is not loaded yet.
4376 // TODO: finer grain decision.
4377 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004378 }
4379
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004380 bool MustGenerateClinitCheck() const {
4381 return generate_clinit_check_;
4382 }
4383
Calin Juravle0ba218d2015-05-19 18:46:01 +01004384 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4385 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004386 }
4387
4388 bool CanCallRuntime() const {
4389 return MustGenerateClinitCheck() || !is_referrers_class_;
4390 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004391
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004392 bool CanThrow() const OVERRIDE {
4393 // May call runtime and and therefore can throw.
4394 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004395 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004396 }
4397
Calin Juravleacf735c2015-02-12 15:25:22 +00004398 ReferenceTypeInfo GetLoadedClassRTI() {
4399 return loaded_class_rti_;
4400 }
4401
4402 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4403 // Make sure we only set exact types (the loaded class should never be merged).
4404 DCHECK(rti.IsExact());
4405 loaded_class_rti_ = rti;
4406 }
4407
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004408 const DexFile& GetDexFile() { return dex_file_; }
4409
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004410 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4411
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004412 static SideEffects SideEffectsForArchRuntimeCalls() {
4413 return SideEffects::CanTriggerGC();
4414 }
4415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004416 DECLARE_INSTRUCTION(LoadClass);
4417
4418 private:
4419 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004420 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004421 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004422 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004423 // Whether this instruction must generate the initialization check.
4424 // Used for code generation.
4425 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004426
Calin Juravleacf735c2015-02-12 15:25:22 +00004427 ReferenceTypeInfo loaded_class_rti_;
4428
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004429 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4430};
4431
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004432class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004433 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004434 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004435 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004436 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004437 dex_pc_(dex_pc) {
4438 SetRawInputAt(0, current_method);
4439 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004440
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004441 bool CanBeMoved() const OVERRIDE { return true; }
4442
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004443 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4444 return other->AsLoadString()->string_index_ == string_index_;
4445 }
4446
4447 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4448
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004449 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004450 uint32_t GetStringIndex() const { return string_index_; }
4451
4452 // TODO: Can we deopt or debug when we resolve a string?
4453 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004454 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004455 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004456
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004457 static SideEffects SideEffectsForArchRuntimeCalls() {
4458 return SideEffects::CanTriggerGC();
4459 }
4460
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004461 DECLARE_INSTRUCTION(LoadString);
4462
4463 private:
4464 const uint32_t string_index_;
4465 const uint32_t dex_pc_;
4466
4467 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4468};
4469
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004470/**
4471 * Performs an initialization check on its Class object input.
4472 */
4473class HClinitCheck : public HExpression<1> {
4474 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004475 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004476 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004477 Primitive::kPrimNot,
4478 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004479 dex_pc_(dex_pc) {
4480 SetRawInputAt(0, constant);
4481 }
4482
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004483 bool CanBeMoved() const OVERRIDE { return true; }
4484 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4485 UNUSED(other);
4486 return true;
4487 }
4488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004489 bool NeedsEnvironment() const OVERRIDE {
4490 // May call runtime to initialize the class.
4491 return true;
4492 }
4493
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004494 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004495
4496 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4497
4498 DECLARE_INSTRUCTION(ClinitCheck);
4499
4500 private:
4501 const uint32_t dex_pc_;
4502
4503 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4504};
4505
4506class HStaticFieldGet : public HExpression<1> {
4507 public:
4508 HStaticFieldGet(HInstruction* cls,
4509 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004510 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004511 bool is_volatile,
4512 uint32_t field_idx,
4513 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004514 : HExpression(
4515 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004516 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004517 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004518 SetRawInputAt(0, cls);
4519 }
4520
Calin Juravle52c48962014-12-16 17:02:57 +00004521
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004522 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004523
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004524 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004525 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4526 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004527 }
4528
4529 size_t ComputeHashCode() const OVERRIDE {
4530 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4531 }
4532
Calin Juravle52c48962014-12-16 17:02:57 +00004533 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004534 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4535 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004536 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004537
4538 DECLARE_INSTRUCTION(StaticFieldGet);
4539
4540 private:
4541 const FieldInfo field_info_;
4542
4543 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4544};
4545
4546class HStaticFieldSet : public HTemplateInstruction<2> {
4547 public:
4548 HStaticFieldSet(HInstruction* cls,
4549 HInstruction* value,
4550 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004551 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004552 bool is_volatile,
4553 uint32_t field_idx,
4554 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004555 : HTemplateInstruction(
4556 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004557 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004558 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004559 SetRawInputAt(0, cls);
4560 SetRawInputAt(1, value);
4561 }
4562
Calin Juravle52c48962014-12-16 17:02:57 +00004563 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004564 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4565 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004566 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004567
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004568 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004569 bool GetValueCanBeNull() const { return value_can_be_null_; }
4570 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004571
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004572 DECLARE_INSTRUCTION(StaticFieldSet);
4573
4574 private:
4575 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004576 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004577
4578 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4579};
4580
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004581// Implement the move-exception DEX instruction.
4582class HLoadException : public HExpression<0> {
4583 public:
4584 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4585
David Brazdilbbd733e2015-08-18 17:48:17 +01004586 bool CanBeNull() const OVERRIDE { return false; }
4587
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004588 DECLARE_INSTRUCTION(LoadException);
4589
4590 private:
4591 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4592};
4593
David Brazdilcb1c0552015-08-04 16:22:25 +01004594// Implicit part of move-exception which clears thread-local exception storage.
4595// Must not be removed because the runtime expects the TLS to get cleared.
4596class HClearException : public HTemplateInstruction<0> {
4597 public:
4598 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4599
4600 DECLARE_INSTRUCTION(ClearException);
4601
4602 private:
4603 DISALLOW_COPY_AND_ASSIGN(HClearException);
4604};
4605
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004606class HThrow : public HTemplateInstruction<1> {
4607 public:
4608 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004609 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004610 SetRawInputAt(0, exception);
4611 }
4612
4613 bool IsControlFlow() const OVERRIDE { return true; }
4614
4615 bool NeedsEnvironment() const OVERRIDE { return true; }
4616
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004617 bool CanThrow() const OVERRIDE { return true; }
4618
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004619 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004620
4621 DECLARE_INSTRUCTION(Throw);
4622
4623 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004624 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004625
4626 DISALLOW_COPY_AND_ASSIGN(HThrow);
4627};
4628
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004629class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004630 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004631 HInstanceOf(HInstruction* object,
4632 HLoadClass* constant,
4633 bool class_is_final,
4634 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004635 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004636 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004637 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004638 dex_pc_(dex_pc) {
4639 SetRawInputAt(0, object);
4640 SetRawInputAt(1, constant);
4641 }
4642
4643 bool CanBeMoved() const OVERRIDE { return true; }
4644
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004645 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004646 return true;
4647 }
4648
4649 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004650 return false;
4651 }
4652
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004653 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004654
4655 bool IsClassFinal() const { return class_is_final_; }
4656
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004657 // Used only in code generation.
4658 bool MustDoNullCheck() const { return must_do_null_check_; }
4659 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4660
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004661 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4662 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4663 }
4664
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004665 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004666
4667 private:
4668 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004669 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004670 const uint32_t dex_pc_;
4671
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004672 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4673};
4674
Calin Juravleb1498f62015-02-16 13:13:29 +00004675class HBoundType : public HExpression<1> {
4676 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004677 // Constructs an HBoundType with the given upper_bound.
4678 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004679 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004680 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004681 upper_bound_(upper_bound),
4682 upper_can_be_null_(upper_can_be_null),
4683 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004684 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004685 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004686 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004687 }
4688
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004689 // GetUpper* should only be used in reference type propagation.
4690 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4691 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004692
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004693 void SetCanBeNull(bool can_be_null) {
4694 DCHECK(upper_can_be_null_ || !can_be_null);
4695 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004696 }
4697
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004698 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4699
Calin Juravleb1498f62015-02-16 13:13:29 +00004700 DECLARE_INSTRUCTION(BoundType);
4701
4702 private:
4703 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004704 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4705 // It is used to bound the type in cases like:
4706 // if (x instanceof ClassX) {
4707 // // uper_bound_ will be ClassX
4708 // }
4709 const ReferenceTypeInfo upper_bound_;
4710 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4711 // is false then can_be_null_ cannot be true).
4712 const bool upper_can_be_null_;
4713 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004714
4715 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4716};
4717
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004718class HCheckCast : public HTemplateInstruction<2> {
4719 public:
4720 HCheckCast(HInstruction* object,
4721 HLoadClass* constant,
4722 bool class_is_final,
4723 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004724 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004725 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004726 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004727 dex_pc_(dex_pc) {
4728 SetRawInputAt(0, object);
4729 SetRawInputAt(1, constant);
4730 }
4731
4732 bool CanBeMoved() const OVERRIDE { return true; }
4733
4734 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4735 return true;
4736 }
4737
4738 bool NeedsEnvironment() const OVERRIDE {
4739 // Instruction may throw a CheckCastError.
4740 return true;
4741 }
4742
4743 bool CanThrow() const OVERRIDE { return true; }
4744
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004745 bool MustDoNullCheck() const { return must_do_null_check_; }
4746 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4747
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004748 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004749
4750 bool IsClassFinal() const { return class_is_final_; }
4751
4752 DECLARE_INSTRUCTION(CheckCast);
4753
4754 private:
4755 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004756 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004757 const uint32_t dex_pc_;
4758
4759 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004760};
4761
Calin Juravle27df7582015-04-17 19:12:31 +01004762class HMemoryBarrier : public HTemplateInstruction<0> {
4763 public:
4764 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004765 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004766 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004767 barrier_kind_(barrier_kind) {}
4768
4769 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4770
4771 DECLARE_INSTRUCTION(MemoryBarrier);
4772
4773 private:
4774 const MemBarrierKind barrier_kind_;
4775
4776 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4777};
4778
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004779class HMonitorOperation : public HTemplateInstruction<1> {
4780 public:
4781 enum OperationKind {
4782 kEnter,
4783 kExit,
4784 };
4785
4786 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004787 : HTemplateInstruction(
4788 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004789 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004790 SetRawInputAt(0, object);
4791 }
4792
4793 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004794 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4795
4796 bool CanThrow() const OVERRIDE {
4797 // Verifier guarantees that monitor-exit cannot throw.
4798 // This is important because it allows the HGraphBuilder to remove
4799 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4800 return IsEnter();
4801 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004802
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004803 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004804
4805 bool IsEnter() const { return kind_ == kEnter; }
4806
4807 DECLARE_INSTRUCTION(MonitorOperation);
4808
Calin Juravle52c48962014-12-16 17:02:57 +00004809 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004810 const OperationKind kind_;
4811 const uint32_t dex_pc_;
4812
4813 private:
4814 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4815};
4816
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004817/**
4818 * A HInstruction used as a marker for the replacement of new + <init>
4819 * of a String to a call to a StringFactory. Only baseline will see
4820 * the node at code generation, where it will be be treated as null.
4821 * When compiling non-baseline, `HFakeString` instructions are being removed
4822 * in the instruction simplifier.
4823 */
4824class HFakeString : public HTemplateInstruction<0> {
4825 public:
4826 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4827
4828 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4829
4830 DECLARE_INSTRUCTION(FakeString);
4831
4832 private:
4833 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4834};
4835
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004836class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004837 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004838 MoveOperands(Location source,
4839 Location destination,
4840 Primitive::Type type,
4841 HInstruction* instruction)
4842 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004843
4844 Location GetSource() const { return source_; }
4845 Location GetDestination() const { return destination_; }
4846
4847 void SetSource(Location value) { source_ = value; }
4848 void SetDestination(Location value) { destination_ = value; }
4849
4850 // The parallel move resolver marks moves as "in-progress" by clearing the
4851 // destination (but not the source).
4852 Location MarkPending() {
4853 DCHECK(!IsPending());
4854 Location dest = destination_;
4855 destination_ = Location::NoLocation();
4856 return dest;
4857 }
4858
4859 void ClearPending(Location dest) {
4860 DCHECK(IsPending());
4861 destination_ = dest;
4862 }
4863
4864 bool IsPending() const {
4865 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4866 return destination_.IsInvalid() && !source_.IsInvalid();
4867 }
4868
4869 // True if this blocks a move from the given location.
4870 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004871 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004872 }
4873
4874 // A move is redundant if it's been eliminated, if its source and
4875 // destination are the same, or if its destination is unneeded.
4876 bool IsRedundant() const {
4877 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4878 }
4879
4880 // We clear both operands to indicate move that's been eliminated.
4881 void Eliminate() {
4882 source_ = destination_ = Location::NoLocation();
4883 }
4884
4885 bool IsEliminated() const {
4886 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4887 return source_.IsInvalid();
4888 }
4889
Alexey Frunze4dda3372015-06-01 18:31:49 -07004890 Primitive::Type GetType() const { return type_; }
4891
Nicolas Geoffray90218252015-04-15 11:56:51 +01004892 bool Is64BitMove() const {
4893 return Primitive::Is64BitType(type_);
4894 }
4895
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004896 HInstruction* GetInstruction() const { return instruction_; }
4897
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004898 private:
4899 Location source_;
4900 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004901 // The type this move is for.
4902 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004903 // The instruction this move is assocatied with. Null when this move is
4904 // for moving an input in the expected locations of user (including a phi user).
4905 // This is only used in debug mode, to ensure we do not connect interval siblings
4906 // in the same parallel move.
4907 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004908};
4909
4910static constexpr size_t kDefaultNumberOfMoves = 4;
4911
4912class HParallelMove : public HTemplateInstruction<0> {
4913 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004914 explicit HParallelMove(ArenaAllocator* arena)
4915 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004916
Nicolas Geoffray90218252015-04-15 11:56:51 +01004917 void AddMove(Location source,
4918 Location destination,
4919 Primitive::Type type,
4920 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004921 DCHECK(source.IsValid());
4922 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004923 if (kIsDebugBuild) {
4924 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004925 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004926 if (moves_.Get(i).GetInstruction() == instruction) {
4927 // Special case the situation where the move is for the spill slot
4928 // of the instruction.
4929 if ((GetPrevious() == instruction)
4930 || ((GetPrevious() == nullptr)
4931 && instruction->IsPhi()
4932 && instruction->GetBlock() == GetBlock())) {
4933 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4934 << "Doing parallel moves for the same instruction.";
4935 } else {
4936 DCHECK(false) << "Doing parallel moves for the same instruction.";
4937 }
4938 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004939 }
4940 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004941 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004942 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004943 << "Overlapped destination for two moves in a parallel move: "
4944 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4945 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004946 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004947 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004948 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004949 }
4950
4951 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004952 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004953 }
4954
4955 size_t NumMoves() const { return moves_.Size(); }
4956
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004957 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004958
4959 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004960 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004961
4962 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4963};
4964
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004965class HGraphVisitor : public ValueObject {
4966 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004967 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4968 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004969
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004970 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004971 virtual void VisitBasicBlock(HBasicBlock* block);
4972
Roland Levillain633021e2014-10-01 14:12:25 +01004973 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004974 void VisitInsertionOrder();
4975
Roland Levillain633021e2014-10-01 14:12:25 +01004976 // Visit the graph following dominator tree reverse post-order.
4977 void VisitReversePostOrder();
4978
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004979 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004980
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004981 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004982#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004983 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4984
4985 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4986
4987#undef DECLARE_VISIT_INSTRUCTION
4988
4989 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004990 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004991
4992 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4993};
4994
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004995class HGraphDelegateVisitor : public HGraphVisitor {
4996 public:
4997 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4998 virtual ~HGraphDelegateVisitor() {}
4999
5000 // Visit functions that delegate to to super class.
5001#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005002 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005003
5004 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5005
5006#undef DECLARE_VISIT_INSTRUCTION
5007
5008 private:
5009 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5010};
5011
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005012class HInsertionOrderIterator : public ValueObject {
5013 public:
5014 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5015
5016 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5017 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5018 void Advance() { ++index_; }
5019
5020 private:
5021 const HGraph& graph_;
5022 size_t index_;
5023
5024 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5025};
5026
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005027class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005028 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005029 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5030 // Check that reverse post order of the graph has been built.
5031 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5032 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005033
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005034 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5035 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005036 void Advance() { ++index_; }
5037
5038 private:
5039 const HGraph& graph_;
5040 size_t index_;
5041
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005042 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005043};
5044
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005045class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005046 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005047 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005048 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5049 // Check that reverse post order of the graph has been built.
5050 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5051 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005052
5053 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005054 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005055 void Advance() { --index_; }
5056
5057 private:
5058 const HGraph& graph_;
5059 size_t index_;
5060
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005061 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005062};
5063
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005064class HLinearPostOrderIterator : public ValueObject {
5065 public:
5066 explicit HLinearPostOrderIterator(const HGraph& graph)
5067 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5068
5069 bool Done() const { return index_ == 0; }
5070
5071 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5072
5073 void Advance() {
5074 --index_;
5075 DCHECK_GE(index_, 0U);
5076 }
5077
5078 private:
5079 const GrowableArray<HBasicBlock*>& order_;
5080 size_t index_;
5081
5082 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5083};
5084
5085class HLinearOrderIterator : public ValueObject {
5086 public:
5087 explicit HLinearOrderIterator(const HGraph& graph)
5088 : order_(graph.GetLinearOrder()), index_(0) {}
5089
5090 bool Done() const { return index_ == order_.Size(); }
5091 HBasicBlock* Current() const { return order_.Get(index_); }
5092 void Advance() { ++index_; }
5093
5094 private:
5095 const GrowableArray<HBasicBlock*>& order_;
5096 size_t index_;
5097
5098 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5099};
5100
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005101// Iterator over the blocks that art part of the loop. Includes blocks part
5102// of an inner loop. The order in which the blocks are iterated is on their
5103// block id.
5104class HBlocksInLoopIterator : public ValueObject {
5105 public:
5106 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5107 : blocks_in_loop_(info.GetBlocks()),
5108 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5109 index_(0) {
5110 if (!blocks_in_loop_.IsBitSet(index_)) {
5111 Advance();
5112 }
5113 }
5114
5115 bool Done() const { return index_ == blocks_.Size(); }
5116 HBasicBlock* Current() const { return blocks_.Get(index_); }
5117 void Advance() {
5118 ++index_;
5119 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5120 if (blocks_in_loop_.IsBitSet(index_)) {
5121 break;
5122 }
5123 }
5124 }
5125
5126 private:
5127 const BitVector& blocks_in_loop_;
5128 const GrowableArray<HBasicBlock*>& blocks_;
5129 size_t index_;
5130
5131 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5132};
5133
Mingyao Yang3584bce2015-05-19 16:01:59 -07005134// Iterator over the blocks that art part of the loop. Includes blocks part
5135// of an inner loop. The order in which the blocks are iterated is reverse
5136// post order.
5137class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5138 public:
5139 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5140 : blocks_in_loop_(info.GetBlocks()),
5141 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5142 index_(0) {
5143 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5144 Advance();
5145 }
5146 }
5147
5148 bool Done() const { return index_ == blocks_.Size(); }
5149 HBasicBlock* Current() const { return blocks_.Get(index_); }
5150 void Advance() {
5151 ++index_;
5152 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5153 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5154 break;
5155 }
5156 }
5157 }
5158
5159 private:
5160 const BitVector& blocks_in_loop_;
5161 const GrowableArray<HBasicBlock*>& blocks_;
5162 size_t index_;
5163
5164 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5165};
5166
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005167inline int64_t Int64FromConstant(HConstant* constant) {
5168 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5169 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5170 : constant->AsLongConstant()->GetValue();
5171}
5172
Vladimir Marko58155012015-08-19 12:49:41 +00005173inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5174 // For the purposes of the compiler, the dex files must actually be the same object
5175 // if we want to safely treat them as the same. This is especially important for JIT
5176 // as custom class loaders can open the same underlying file (or memory) multiple
5177 // times and provide different class resolution but no two class loaders should ever
5178 // use the same DexFile object - doing so is an unsupported hack that can lead to
5179 // all sorts of weird failures.
5180 return &lhs == &rhs;
5181}
5182
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005183} // namespace art
5184
5185#endif // ART_COMPILER_OPTIMIZING_NODES_H_