blob: f2db33086c42a025d3f69cc6cf4053f7734768ae [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
Roland Levillain4c0eb422015-04-24 16:43:49 +01003184 // Is this instruction a call to a static method?
3185 bool IsStatic() const {
3186 return GetInvokeType() == kStatic;
3187 }
3188
Roland Levillain3e3d7332015-04-28 11:00:54 +01003189 // Remove the art::HLoadClass instruction set as last input by
3190 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3191 // the initial art::HClinitCheck instruction (only relevant for
3192 // static calls with explicit clinit check).
3193 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003194 DCHECK(IsStaticWithExplicitClinitCheck());
3195 size_t last_input_index = InputCount() - 1;
3196 HInstruction* last_input = InputAt(last_input_index);
3197 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003198 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003199 RemoveAsUserOfInput(last_input_index);
3200 inputs_.DeleteAt(last_input_index);
3201 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3202 DCHECK(IsStaticWithImplicitClinitCheck());
3203 }
3204
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003205 bool IsStringFactoryFor(HFakeString* str) const {
3206 if (!IsStringInit()) return false;
3207 // +1 for the current method.
3208 if (InputCount() == (number_of_arguments_ + 1)) return false;
3209 return InputAt(InputCount() - 1)->AsFakeString() == str;
3210 }
3211
3212 void RemoveFakeStringArgumentAsLastInput() {
3213 DCHECK(IsStringInit());
3214 size_t last_input_index = InputCount() - 1;
3215 HInstruction* last_input = InputAt(last_input_index);
3216 DCHECK(last_input != nullptr);
3217 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3218 RemoveAsUserOfInput(last_input_index);
3219 inputs_.DeleteAt(last_input_index);
3220 }
3221
Roland Levillain4c0eb422015-04-24 16:43:49 +01003222 // Is this a call to a static method whose declaring class has an
3223 // explicit intialization check in the graph?
3224 bool IsStaticWithExplicitClinitCheck() const {
3225 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3226 }
3227
3228 // Is this a call to a static method whose declaring class has an
3229 // implicit intialization check requirement?
3230 bool IsStaticWithImplicitClinitCheck() const {
3231 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3232 }
3233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003234 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003235
Roland Levillain4c0eb422015-04-24 16:43:49 +01003236 protected:
3237 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3238 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3239 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3240 HInstruction* input = input_record.GetInstruction();
3241 // `input` is the last input of a static invoke marked as having
3242 // an explicit clinit check. It must either be:
3243 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3244 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3245 DCHECK(input != nullptr);
3246 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3247 }
3248 return input_record;
3249 }
3250
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003251 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003252 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003253 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003254 // The target method may refer to different dex file or method index than the original
3255 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3256 // in derived class to increase visibility.
3257 MethodReference target_method_;
3258 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003260 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003261};
3262
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003263class HInvokeVirtual : public HInvoke {
3264 public:
3265 HInvokeVirtual(ArenaAllocator* arena,
3266 uint32_t number_of_arguments,
3267 Primitive::Type return_type,
3268 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003270 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003271 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003272 vtable_index_(vtable_index) {}
3273
Calin Juravle641547a2015-04-21 22:08:51 +01003274 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003275 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003276 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003277 }
3278
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003279 uint32_t GetVTableIndex() const { return vtable_index_; }
3280
3281 DECLARE_INSTRUCTION(InvokeVirtual);
3282
3283 private:
3284 const uint32_t vtable_index_;
3285
3286 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3287};
3288
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003289class HInvokeInterface : public HInvoke {
3290 public:
3291 HInvokeInterface(ArenaAllocator* arena,
3292 uint32_t number_of_arguments,
3293 Primitive::Type return_type,
3294 uint32_t dex_pc,
3295 uint32_t dex_method_index,
3296 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003297 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003298 imt_index_(imt_index) {}
3299
Calin Juravle641547a2015-04-21 22:08:51 +01003300 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003301 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003302 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003303 }
3304
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003305 uint32_t GetImtIndex() const { return imt_index_; }
3306 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3307
3308 DECLARE_INSTRUCTION(InvokeInterface);
3309
3310 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003311 const uint32_t imt_index_;
3312
3313 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3314};
3315
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003316class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003317 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003318 HNewInstance(HCurrentMethod* current_method,
3319 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003320 uint16_t type_index,
3321 const DexFile& dex_file,
3322 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003323 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003324 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003325 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003326 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003327 entrypoint_(entrypoint) {
3328 SetRawInputAt(0, current_method);
3329 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003330
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003331 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003332 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003333 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003334
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003335 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003336 bool NeedsEnvironment() const OVERRIDE { return true; }
3337 // It may throw when called on:
3338 // - interfaces
3339 // - abstract/innaccessible/unknown classes
3340 // TODO: optimize when possible.
3341 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003342
Calin Juravle10e244f2015-01-26 18:54:32 +00003343 bool CanBeNull() const OVERRIDE { return false; }
3344
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003345 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003347 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003348
3349 private:
3350 const uint32_t dex_pc_;
3351 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003352 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003353 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003354
3355 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3356};
3357
Roland Levillain88cb1752014-10-20 16:36:47 +01003358class HNeg : public HUnaryOperation {
3359 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003360 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003361 : HUnaryOperation(result_type, input) {}
3362
Roland Levillain9867bc72015-08-05 10:21:34 +01003363 template <typename T> T Compute(T x) const { return -x; }
3364
3365 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3366 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3367 }
3368 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3369 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3370 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003371
Roland Levillain88cb1752014-10-20 16:36:47 +01003372 DECLARE_INSTRUCTION(Neg);
3373
3374 private:
3375 DISALLOW_COPY_AND_ASSIGN(HNeg);
3376};
3377
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003378class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003379 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003380 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003381 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003382 uint32_t dex_pc,
3383 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003384 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003385 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003386 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003387 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003388 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003389 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003390 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003391 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003392 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003393 }
3394
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003395 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003396 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003397 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003398
3399 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003400 bool NeedsEnvironment() const OVERRIDE { return true; }
3401
Mingyao Yang0c365e62015-03-31 15:09:29 -07003402 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3403 bool CanThrow() const OVERRIDE { return true; }
3404
Calin Juravle10e244f2015-01-26 18:54:32 +00003405 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003406
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003407 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3408
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003409 DECLARE_INSTRUCTION(NewArray);
3410
3411 private:
3412 const uint32_t dex_pc_;
3413 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003414 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003415 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003416
3417 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3418};
3419
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003420class HAdd : public HBinaryOperation {
3421 public:
3422 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3423 : HBinaryOperation(result_type, left, right) {}
3424
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003425 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003426
Roland Levillain9867bc72015-08-05 10:21:34 +01003427 template <typename T> T Compute(T x, T y) const { return x + y; }
3428
3429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3430 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003431 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003432 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3433 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003434 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003435
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003436 DECLARE_INSTRUCTION(Add);
3437
3438 private:
3439 DISALLOW_COPY_AND_ASSIGN(HAdd);
3440};
3441
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003442class HSub : public HBinaryOperation {
3443 public:
3444 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3445 : HBinaryOperation(result_type, left, right) {}
3446
Roland Levillain9867bc72015-08-05 10:21:34 +01003447 template <typename T> T Compute(T x, T y) const { return x - y; }
3448
3449 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3450 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003451 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003452 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3453 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003454 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003455
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003456 DECLARE_INSTRUCTION(Sub);
3457
3458 private:
3459 DISALLOW_COPY_AND_ASSIGN(HSub);
3460};
3461
Calin Juravle34bacdf2014-10-07 20:23:36 +01003462class HMul : public HBinaryOperation {
3463 public:
3464 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3465 : HBinaryOperation(result_type, left, right) {}
3466
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003467 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003468
Roland Levillain9867bc72015-08-05 10:21:34 +01003469 template <typename T> T Compute(T x, T y) const { return x * y; }
3470
3471 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3472 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3473 }
3474 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3475 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3476 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003477
3478 DECLARE_INSTRUCTION(Mul);
3479
3480 private:
3481 DISALLOW_COPY_AND_ASSIGN(HMul);
3482};
3483
Calin Juravle7c4954d2014-10-28 16:57:40 +00003484class HDiv : public HBinaryOperation {
3485 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003486 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003487 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3488 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003489
Roland Levillain9867bc72015-08-05 10:21:34 +01003490 template <typename T>
3491 T Compute(T x, T y) const {
3492 // Our graph structure ensures we never have 0 for `y` during
3493 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003494 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003495 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003496 return (y == -1) ? -x : x / y;
3497 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003498
Roland Levillain9867bc72015-08-05 10:21:34 +01003499 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3500 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3501 }
3502 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3503 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003504 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003505
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003506 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003507
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003508 static SideEffects SideEffectsForArchRuntimeCalls() {
3509 // The generated code can use a runtime call.
3510 return SideEffects::CanTriggerGC();
3511 }
3512
Calin Juravle7c4954d2014-10-28 16:57:40 +00003513 DECLARE_INSTRUCTION(Div);
3514
3515 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003516 const uint32_t dex_pc_;
3517
Calin Juravle7c4954d2014-10-28 16:57:40 +00003518 DISALLOW_COPY_AND_ASSIGN(HDiv);
3519};
3520
Calin Juravlebacfec32014-11-14 15:54:36 +00003521class HRem : public HBinaryOperation {
3522 public:
3523 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003524 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3525 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003526
Roland Levillain9867bc72015-08-05 10:21:34 +01003527 template <typename T>
3528 T Compute(T x, T y) const {
3529 // Our graph structure ensures we never have 0 for `y` during
3530 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003531 DCHECK_NE(y, 0);
3532 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3533 return (y == -1) ? 0 : x % y;
3534 }
3535
Roland Levillain9867bc72015-08-05 10:21:34 +01003536 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3537 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3538 }
3539 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3540 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003541 }
3542
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003543 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003544
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003545 static SideEffects SideEffectsForArchRuntimeCalls() {
3546 return SideEffects::CanTriggerGC();
3547 }
3548
Calin Juravlebacfec32014-11-14 15:54:36 +00003549 DECLARE_INSTRUCTION(Rem);
3550
3551 private:
3552 const uint32_t dex_pc_;
3553
3554 DISALLOW_COPY_AND_ASSIGN(HRem);
3555};
3556
Calin Juravled0d48522014-11-04 16:40:20 +00003557class HDivZeroCheck : public HExpression<1> {
3558 public:
3559 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3560 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3561 SetRawInputAt(0, value);
3562 }
3563
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003564 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3565
Calin Juravled0d48522014-11-04 16:40:20 +00003566 bool CanBeMoved() const OVERRIDE { return true; }
3567
3568 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3569 UNUSED(other);
3570 return true;
3571 }
3572
3573 bool NeedsEnvironment() const OVERRIDE { return true; }
3574 bool CanThrow() const OVERRIDE { return true; }
3575
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003576 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003577
3578 DECLARE_INSTRUCTION(DivZeroCheck);
3579
3580 private:
3581 const uint32_t dex_pc_;
3582
3583 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3584};
3585
Calin Juravle9aec02f2014-11-18 23:06:35 +00003586class HShl : public HBinaryOperation {
3587 public:
3588 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3589 : HBinaryOperation(result_type, left, right) {}
3590
Roland Levillain9867bc72015-08-05 10:21:34 +01003591 template <typename T, typename U, typename V>
3592 T Compute(T x, U y, V max_shift_value) const {
3593 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3594 "V is not the unsigned integer type corresponding to T");
3595 return x << (y & max_shift_value);
3596 }
3597
3598 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3599 return GetBlock()->GetGraph()->GetIntConstant(
3600 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3601 }
3602 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3603 // case is handled as `x << static_cast<int>(y)`.
3604 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3605 return GetBlock()->GetGraph()->GetLongConstant(
3606 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3607 }
3608 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3609 return GetBlock()->GetGraph()->GetLongConstant(
3610 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3611 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003612
3613 DECLARE_INSTRUCTION(Shl);
3614
3615 private:
3616 DISALLOW_COPY_AND_ASSIGN(HShl);
3617};
3618
3619class HShr : public HBinaryOperation {
3620 public:
3621 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3622 : HBinaryOperation(result_type, left, right) {}
3623
Roland Levillain9867bc72015-08-05 10:21:34 +01003624 template <typename T, typename U, typename V>
3625 T Compute(T x, U y, V max_shift_value) const {
3626 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3627 "V is not the unsigned integer type corresponding to T");
3628 return x >> (y & max_shift_value);
3629 }
3630
3631 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3632 return GetBlock()->GetGraph()->GetIntConstant(
3633 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3634 }
3635 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3636 // case is handled as `x >> static_cast<int>(y)`.
3637 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3638 return GetBlock()->GetGraph()->GetLongConstant(
3639 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3640 }
3641 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3642 return GetBlock()->GetGraph()->GetLongConstant(
3643 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3644 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003645
3646 DECLARE_INSTRUCTION(Shr);
3647
3648 private:
3649 DISALLOW_COPY_AND_ASSIGN(HShr);
3650};
3651
3652class HUShr : public HBinaryOperation {
3653 public:
3654 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3655 : HBinaryOperation(result_type, left, right) {}
3656
Roland Levillain9867bc72015-08-05 10:21:34 +01003657 template <typename T, typename U, typename V>
3658 T Compute(T x, U y, V max_shift_value) const {
3659 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3660 "V is not the unsigned integer type corresponding to T");
3661 V ux = static_cast<V>(x);
3662 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663 }
3664
Roland Levillain9867bc72015-08-05 10:21:34 +01003665 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3666 return GetBlock()->GetGraph()->GetIntConstant(
3667 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3668 }
3669 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3670 // case is handled as `x >>> static_cast<int>(y)`.
3671 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3672 return GetBlock()->GetGraph()->GetLongConstant(
3673 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3674 }
3675 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3676 return GetBlock()->GetGraph()->GetLongConstant(
3677 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003678 }
3679
3680 DECLARE_INSTRUCTION(UShr);
3681
3682 private:
3683 DISALLOW_COPY_AND_ASSIGN(HUShr);
3684};
3685
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003686class HAnd : public HBinaryOperation {
3687 public:
3688 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3689 : HBinaryOperation(result_type, left, right) {}
3690
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003691 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003692
Roland Levillain9867bc72015-08-05 10:21:34 +01003693 template <typename T, typename U>
3694 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3695
3696 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3697 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3698 }
3699 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3700 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3701 }
3702 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3703 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3704 }
3705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3706 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3707 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003708
3709 DECLARE_INSTRUCTION(And);
3710
3711 private:
3712 DISALLOW_COPY_AND_ASSIGN(HAnd);
3713};
3714
3715class HOr : public HBinaryOperation {
3716 public:
3717 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3718 : HBinaryOperation(result_type, left, right) {}
3719
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003720 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003721
Roland Levillain9867bc72015-08-05 10:21:34 +01003722 template <typename T, typename U>
3723 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3724
3725 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3726 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3727 }
3728 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3729 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3730 }
3731 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3732 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3733 }
3734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3735 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3736 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003737
3738 DECLARE_INSTRUCTION(Or);
3739
3740 private:
3741 DISALLOW_COPY_AND_ASSIGN(HOr);
3742};
3743
3744class HXor : public HBinaryOperation {
3745 public:
3746 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3747 : HBinaryOperation(result_type, left, right) {}
3748
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003749 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003750
Roland Levillain9867bc72015-08-05 10:21:34 +01003751 template <typename T, typename U>
3752 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3753
3754 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3755 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3756 }
3757 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3758 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3759 }
3760 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3761 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3762 }
3763 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3764 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3765 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003766
3767 DECLARE_INSTRUCTION(Xor);
3768
3769 private:
3770 DISALLOW_COPY_AND_ASSIGN(HXor);
3771};
3772
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003773// The value of a parameter in this method. Its location depends on
3774// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003775class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003776 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003777 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003778 : HExpression(parameter_type, SideEffects::None()),
3779 index_(index),
3780 is_this_(is_this),
3781 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003782
3783 uint8_t GetIndex() const { return index_; }
3784
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003785 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3786 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003787
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003788 bool IsThis() const { return is_this_; }
3789
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003790 DECLARE_INSTRUCTION(ParameterValue);
3791
3792 private:
3793 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003794 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003795 const uint8_t index_;
3796
Calin Juravle10e244f2015-01-26 18:54:32 +00003797 // Whether or not the parameter value corresponds to 'this' argument.
3798 const bool is_this_;
3799
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003800 bool can_be_null_;
3801
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003802 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3803};
3804
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003805class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003806 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003807 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003808 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003810 bool CanBeMoved() const OVERRIDE { return true; }
3811 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003812 UNUSED(other);
3813 return true;
3814 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003815
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 template <typename T> T Compute(T x) const { return ~x; }
3817
3818 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3819 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3820 }
3821 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3822 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3823 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003824
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003825 DECLARE_INSTRUCTION(Not);
3826
3827 private:
3828 DISALLOW_COPY_AND_ASSIGN(HNot);
3829};
3830
David Brazdil66d126e2015-04-03 16:02:44 +01003831class HBooleanNot : public HUnaryOperation {
3832 public:
3833 explicit HBooleanNot(HInstruction* input)
3834 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3835
3836 bool CanBeMoved() const OVERRIDE { return true; }
3837 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3838 UNUSED(other);
3839 return true;
3840 }
3841
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003843 DCHECK(IsUint<1>(x));
3844 return !x;
3845 }
3846
Roland Levillain9867bc72015-08-05 10:21:34 +01003847 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3848 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3849 }
3850 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3851 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003852 UNREACHABLE();
3853 }
3854
3855 DECLARE_INSTRUCTION(BooleanNot);
3856
3857 private:
3858 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3859};
3860
Roland Levillaindff1f282014-11-05 14:15:05 +00003861class HTypeConversion : public HExpression<1> {
3862 public:
3863 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003864 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003865 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3866 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003867 SetRawInputAt(0, input);
3868 DCHECK_NE(input->GetType(), result_type);
3869 }
3870
3871 HInstruction* GetInput() const { return InputAt(0); }
3872 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3873 Primitive::Type GetResultType() const { return GetType(); }
3874
Roland Levillain624279f2014-12-04 11:54:28 +00003875 // Required by the x86 and ARM code generators when producing calls
3876 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003877 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003878
Roland Levillaindff1f282014-11-05 14:15:05 +00003879 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003880 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003881
Mark Mendelle82549b2015-05-06 10:55:34 -04003882 // Try to statically evaluate the conversion and return a HConstant
3883 // containing the result. If the input cannot be converted, return nullptr.
3884 HConstant* TryStaticEvaluation() const;
3885
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003886 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3887 Primitive::Type result_type) {
3888 // Some architectures may not require the 'GC' side effects, but at this point
3889 // in the compilation process we do not know what architecture we will
3890 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003891 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3892 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003893 return SideEffects::CanTriggerGC();
3894 }
3895 return SideEffects::None();
3896 }
3897
Roland Levillaindff1f282014-11-05 14:15:05 +00003898 DECLARE_INSTRUCTION(TypeConversion);
3899
3900 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003901 const uint32_t dex_pc_;
3902
Roland Levillaindff1f282014-11-05 14:15:05 +00003903 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3904};
3905
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003906static constexpr uint32_t kNoRegNumber = -1;
3907
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003908class HPhi : public HInstruction {
3909 public:
3910 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003911 : HInstruction(SideEffects::None()),
3912 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003913 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003914 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003915 is_live_(false),
3916 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003917 inputs_.SetSize(number_of_inputs);
3918 }
3919
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003920 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3921 static Primitive::Type ToPhiType(Primitive::Type type) {
3922 switch (type) {
3923 case Primitive::kPrimBoolean:
3924 case Primitive::kPrimByte:
3925 case Primitive::kPrimShort:
3926 case Primitive::kPrimChar:
3927 return Primitive::kPrimInt;
3928 default:
3929 return type;
3930 }
3931 }
3932
David Brazdilffee3d32015-07-06 11:48:53 +01003933 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3934
Calin Juravle10e244f2015-01-26 18:54:32 +00003935 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003936
3937 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003938 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003939
Calin Juravle10e244f2015-01-26 18:54:32 +00003940 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003941 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003942
Calin Juravle10e244f2015-01-26 18:54:32 +00003943 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3944 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3945
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003946 uint32_t GetRegNumber() const { return reg_number_; }
3947
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003948 void SetDead() { is_live_ = false; }
3949 void SetLive() { is_live_ = true; }
3950 bool IsDead() const { return !is_live_; }
3951 bool IsLive() const { return is_live_; }
3952
Calin Juravlea4f88312015-04-16 12:57:19 +01003953 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3954 // An equivalent phi is a phi having the same dex register and type.
3955 // It assumes that phis with the same dex register are adjacent.
3956 HPhi* GetNextEquivalentPhiWithSameType() {
3957 HInstruction* next = GetNext();
3958 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3959 if (next->GetType() == GetType()) {
3960 return next->AsPhi();
3961 }
3962 next = next->GetNext();
3963 }
3964 return nullptr;
3965 }
3966
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003967 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003968
David Brazdil1abb4192015-02-17 18:33:36 +00003969 protected:
3970 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3971
3972 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3973 inputs_.Put(index, input);
3974 }
3975
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003976 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003977 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003978 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003979 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003980 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003981 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003982
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003983 DISALLOW_COPY_AND_ASSIGN(HPhi);
3984};
3985
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003986class HNullCheck : public HExpression<1> {
3987 public:
3988 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003989 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003990 SetRawInputAt(0, value);
3991 }
3992
Calin Juravle10e244f2015-01-26 18:54:32 +00003993 bool CanBeMoved() const OVERRIDE { return true; }
3994 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003995 UNUSED(other);
3996 return true;
3997 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003998
Calin Juravle10e244f2015-01-26 18:54:32 +00003999 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004000
Calin Juravle10e244f2015-01-26 18:54:32 +00004001 bool CanThrow() const OVERRIDE { return true; }
4002
4003 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004004
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004005 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004006
4007 DECLARE_INSTRUCTION(NullCheck);
4008
4009 private:
4010 const uint32_t dex_pc_;
4011
4012 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4013};
4014
4015class FieldInfo : public ValueObject {
4016 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004017 FieldInfo(MemberOffset field_offset,
4018 Primitive::Type field_type,
4019 bool is_volatile,
4020 uint32_t index,
4021 const DexFile& dex_file)
4022 : field_offset_(field_offset),
4023 field_type_(field_type),
4024 is_volatile_(is_volatile),
4025 index_(index),
4026 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004027
4028 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004029 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004030 uint32_t GetFieldIndex() const { return index_; }
4031 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004032 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004033
4034 private:
4035 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004036 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004037 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004038 uint32_t index_;
4039 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004040};
4041
4042class HInstanceFieldGet : public HExpression<1> {
4043 public:
4044 HInstanceFieldGet(HInstruction* value,
4045 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004046 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004047 bool is_volatile,
4048 uint32_t field_idx,
4049 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004050 : HExpression(
4051 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004052 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004053 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004054 SetRawInputAt(0, value);
4055 }
4056
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004057 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004058
4059 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4060 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4061 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004062 }
4063
Calin Juravle641547a2015-04-21 22:08:51 +01004064 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4065 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004066 }
4067
4068 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004069 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4070 }
4071
Calin Juravle52c48962014-12-16 17:02:57 +00004072 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004073 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004074 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004075 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004076
4077 DECLARE_INSTRUCTION(InstanceFieldGet);
4078
4079 private:
4080 const FieldInfo field_info_;
4081
4082 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4083};
4084
4085class HInstanceFieldSet : public HTemplateInstruction<2> {
4086 public:
4087 HInstanceFieldSet(HInstruction* object,
4088 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004089 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004090 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004091 bool is_volatile,
4092 uint32_t field_idx,
4093 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004094 : HTemplateInstruction(
4095 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004096 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004097 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004098 SetRawInputAt(0, object);
4099 SetRawInputAt(1, value);
4100 }
4101
Calin Juravle641547a2015-04-21 22:08:51 +01004102 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4103 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004104 }
4105
Calin Juravle52c48962014-12-16 17:02:57 +00004106 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004108 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004109 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004110 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004111 bool GetValueCanBeNull() const { return value_can_be_null_; }
4112 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004113
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004114 DECLARE_INSTRUCTION(InstanceFieldSet);
4115
4116 private:
4117 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004118 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004119
4120 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4121};
4122
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004123class HArrayGet : public HExpression<2> {
4124 public:
4125 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004126 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004127 SetRawInputAt(0, array);
4128 SetRawInputAt(1, index);
4129 }
4130
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004131 bool CanBeMoved() const OVERRIDE { return true; }
4132 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004133 UNUSED(other);
4134 return true;
4135 }
Calin Juravle641547a2015-04-21 22:08:51 +01004136 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4137 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004138 // TODO: We can be smarter here.
4139 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4140 // which generates the implicit null check. There are cases when these can be removed
4141 // to produce better code. If we ever add optimizations to do so we should allow an
4142 // implicit check here (as long as the address falls in the first page).
4143 return false;
4144 }
4145
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004146 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004147
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004148 HInstruction* GetArray() const { return InputAt(0); }
4149 HInstruction* GetIndex() const { return InputAt(1); }
4150
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151 DECLARE_INSTRUCTION(ArrayGet);
4152
4153 private:
4154 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4155};
4156
4157class HArraySet : public HTemplateInstruction<3> {
4158 public:
4159 HArraySet(HInstruction* array,
4160 HInstruction* index,
4161 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004162 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004163 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004164 : HTemplateInstruction(
4165 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4166 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004167 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004168 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004169 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4170 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171 SetRawInputAt(0, array);
4172 SetRawInputAt(1, index);
4173 SetRawInputAt(2, value);
4174 }
4175
Calin Juravle77520bc2015-01-12 18:45:46 +00004176 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004177 // We currently always call a runtime method to catch array store
4178 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004179 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 }
4181
Mingyao Yang81014cb2015-06-02 03:16:27 -07004182 // Can throw ArrayStoreException.
4183 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4184
Calin Juravle641547a2015-04-21 22:08:51 +01004185 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4186 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004187 // TODO: Same as for ArrayGet.
4188 return false;
4189 }
4190
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004191 void ClearNeedsTypeCheck() {
4192 needs_type_check_ = false;
4193 }
4194
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004195 void ClearValueCanBeNull() {
4196 value_can_be_null_ = false;
4197 }
4198
4199 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004200 bool NeedsTypeCheck() const { return needs_type_check_; }
4201
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004202 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004203
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004204 HInstruction* GetArray() const { return InputAt(0); }
4205 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004206 HInstruction* GetValue() const { return InputAt(2); }
4207
4208 Primitive::Type GetComponentType() const {
4209 // The Dex format does not type floating point index operations. Since the
4210 // `expected_component_type_` is set during building and can therefore not
4211 // be correct, we also check what is the value type. If it is a floating
4212 // point type, we must use that type.
4213 Primitive::Type value_type = GetValue()->GetType();
4214 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4215 ? value_type
4216 : expected_component_type_;
4217 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004218
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004219 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4220 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4221 }
4222
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223 DECLARE_INSTRUCTION(ArraySet);
4224
4225 private:
4226 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004227 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004228 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004229 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004230
4231 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4232};
4233
4234class HArrayLength : public HExpression<1> {
4235 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004236 explicit HArrayLength(HInstruction* array)
4237 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4238 // Note that arrays do not change length, so the instruction does not
4239 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 SetRawInputAt(0, array);
4241 }
4242
Calin Juravle77520bc2015-01-12 18:45:46 +00004243 bool CanBeMoved() const OVERRIDE { return true; }
4244 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004245 UNUSED(other);
4246 return true;
4247 }
Calin Juravle641547a2015-04-21 22:08:51 +01004248 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4249 return obj == InputAt(0);
4250 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004251
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004252 DECLARE_INSTRUCTION(ArrayLength);
4253
4254 private:
4255 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4256};
4257
4258class HBoundsCheck : public HExpression<2> {
4259 public:
4260 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004261 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004262 DCHECK(index->GetType() == Primitive::kPrimInt);
4263 SetRawInputAt(0, index);
4264 SetRawInputAt(1, length);
4265 }
4266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004267 bool CanBeMoved() const OVERRIDE { return true; }
4268 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004269 UNUSED(other);
4270 return true;
4271 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004273 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004275 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004276
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004277 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004278
4279 DECLARE_INSTRUCTION(BoundsCheck);
4280
4281 private:
4282 const uint32_t dex_pc_;
4283
4284 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4285};
4286
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004287/**
4288 * Some DEX instructions are folded into multiple HInstructions that need
4289 * to stay live until the last HInstruction. This class
4290 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004291 * HInstruction stays live. `index` represents the stack location index of the
4292 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004293 */
4294class HTemporary : public HTemplateInstruction<0> {
4295 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004296 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297
4298 size_t GetIndex() const { return index_; }
4299
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004300 Primitive::Type GetType() const OVERRIDE {
4301 // The previous instruction is the one that will be stored in the temporary location.
4302 DCHECK(GetPrevious() != nullptr);
4303 return GetPrevious()->GetType();
4304 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004305
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 DECLARE_INSTRUCTION(Temporary);
4307
4308 private:
4309 const size_t index_;
4310
4311 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4312};
4313
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004314class HSuspendCheck : public HTemplateInstruction<0> {
4315 public:
4316 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004317 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004318
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004319 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004320 return true;
4321 }
4322
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004323 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004324 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4325 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004326
4327 DECLARE_INSTRUCTION(SuspendCheck);
4328
4329 private:
4330 const uint32_t dex_pc_;
4331
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004332 // Only used for code generation, in order to share the same slow path between back edges
4333 // of a same loop.
4334 SlowPathCode* slow_path_;
4335
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004336 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4337};
4338
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004339/**
4340 * Instruction to load a Class object.
4341 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004342class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004343 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004344 HLoadClass(HCurrentMethod* current_method,
4345 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004346 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004347 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004348 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004349 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004350 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004351 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004352 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004353 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004354 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004355 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004356 SetRawInputAt(0, current_method);
4357 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004358
4359 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004360
4361 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4362 return other->AsLoadClass()->type_index_ == type_index_;
4363 }
4364
4365 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4366
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004367 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004368 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004369 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004370 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004371
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004372 bool NeedsEnvironment() const OVERRIDE {
4373 // Will call runtime and load the class if the class is not loaded yet.
4374 // TODO: finer grain decision.
4375 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004376 }
4377
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004378 bool MustGenerateClinitCheck() const {
4379 return generate_clinit_check_;
4380 }
4381
Calin Juravle0ba218d2015-05-19 18:46:01 +01004382 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4383 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004384 }
4385
4386 bool CanCallRuntime() const {
4387 return MustGenerateClinitCheck() || !is_referrers_class_;
4388 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004389
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004390 bool CanThrow() const OVERRIDE {
4391 // May call runtime and and therefore can throw.
4392 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004393 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004394 }
4395
Calin Juravleacf735c2015-02-12 15:25:22 +00004396 ReferenceTypeInfo GetLoadedClassRTI() {
4397 return loaded_class_rti_;
4398 }
4399
4400 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4401 // Make sure we only set exact types (the loaded class should never be merged).
4402 DCHECK(rti.IsExact());
4403 loaded_class_rti_ = rti;
4404 }
4405
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004406 const DexFile& GetDexFile() { return dex_file_; }
4407
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004408 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4409
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004410 static SideEffects SideEffectsForArchRuntimeCalls() {
4411 return SideEffects::CanTriggerGC();
4412 }
4413
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004414 DECLARE_INSTRUCTION(LoadClass);
4415
4416 private:
4417 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004418 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004419 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004420 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004421 // Whether this instruction must generate the initialization check.
4422 // Used for code generation.
4423 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004424
Calin Juravleacf735c2015-02-12 15:25:22 +00004425 ReferenceTypeInfo loaded_class_rti_;
4426
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004427 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4428};
4429
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004430class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004431 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004432 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004433 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004434 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004435 dex_pc_(dex_pc) {
4436 SetRawInputAt(0, current_method);
4437 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004438
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004439 bool CanBeMoved() const OVERRIDE { return true; }
4440
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004441 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4442 return other->AsLoadString()->string_index_ == string_index_;
4443 }
4444
4445 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4446
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004447 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004448 uint32_t GetStringIndex() const { return string_index_; }
4449
4450 // TODO: Can we deopt or debug when we resolve a string?
4451 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004452 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004453 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004454
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004455 static SideEffects SideEffectsForArchRuntimeCalls() {
4456 return SideEffects::CanTriggerGC();
4457 }
4458
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004459 DECLARE_INSTRUCTION(LoadString);
4460
4461 private:
4462 const uint32_t string_index_;
4463 const uint32_t dex_pc_;
4464
4465 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4466};
4467
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004468/**
4469 * Performs an initialization check on its Class object input.
4470 */
4471class HClinitCheck : public HExpression<1> {
4472 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004473 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004474 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004475 Primitive::kPrimNot,
4476 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004477 dex_pc_(dex_pc) {
4478 SetRawInputAt(0, constant);
4479 }
4480
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004481 bool CanBeMoved() const OVERRIDE { return true; }
4482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4483 UNUSED(other);
4484 return true;
4485 }
4486
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004487 bool NeedsEnvironment() const OVERRIDE {
4488 // May call runtime to initialize the class.
4489 return true;
4490 }
4491
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004492 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004493
4494 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4495
4496 DECLARE_INSTRUCTION(ClinitCheck);
4497
4498 private:
4499 const uint32_t dex_pc_;
4500
4501 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4502};
4503
4504class HStaticFieldGet : public HExpression<1> {
4505 public:
4506 HStaticFieldGet(HInstruction* cls,
4507 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004508 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004509 bool is_volatile,
4510 uint32_t field_idx,
4511 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004512 : HExpression(
4513 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004514 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004515 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004516 SetRawInputAt(0, cls);
4517 }
4518
Calin Juravle52c48962014-12-16 17:02:57 +00004519
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004520 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004521
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004523 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4524 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004525 }
4526
4527 size_t ComputeHashCode() const OVERRIDE {
4528 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4529 }
4530
Calin Juravle52c48962014-12-16 17:02:57 +00004531 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004532 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4533 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004534 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004535
4536 DECLARE_INSTRUCTION(StaticFieldGet);
4537
4538 private:
4539 const FieldInfo field_info_;
4540
4541 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4542};
4543
4544class HStaticFieldSet : public HTemplateInstruction<2> {
4545 public:
4546 HStaticFieldSet(HInstruction* cls,
4547 HInstruction* value,
4548 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004549 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004550 bool is_volatile,
4551 uint32_t field_idx,
4552 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004553 : HTemplateInstruction(
4554 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004555 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004556 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004557 SetRawInputAt(0, cls);
4558 SetRawInputAt(1, value);
4559 }
4560
Calin Juravle52c48962014-12-16 17:02:57 +00004561 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004562 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4563 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004564 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004565
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004566 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004567 bool GetValueCanBeNull() const { return value_can_be_null_; }
4568 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004569
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004570 DECLARE_INSTRUCTION(StaticFieldSet);
4571
4572 private:
4573 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004574 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004575
4576 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4577};
4578
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004579// Implement the move-exception DEX instruction.
4580class HLoadException : public HExpression<0> {
4581 public:
4582 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4583
David Brazdilbbd733e2015-08-18 17:48:17 +01004584 bool CanBeNull() const OVERRIDE { return false; }
4585
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004586 DECLARE_INSTRUCTION(LoadException);
4587
4588 private:
4589 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4590};
4591
David Brazdilcb1c0552015-08-04 16:22:25 +01004592// Implicit part of move-exception which clears thread-local exception storage.
4593// Must not be removed because the runtime expects the TLS to get cleared.
4594class HClearException : public HTemplateInstruction<0> {
4595 public:
4596 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4597
4598 DECLARE_INSTRUCTION(ClearException);
4599
4600 private:
4601 DISALLOW_COPY_AND_ASSIGN(HClearException);
4602};
4603
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004604class HThrow : public HTemplateInstruction<1> {
4605 public:
4606 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004607 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004608 SetRawInputAt(0, exception);
4609 }
4610
4611 bool IsControlFlow() const OVERRIDE { return true; }
4612
4613 bool NeedsEnvironment() const OVERRIDE { return true; }
4614
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004615 bool CanThrow() const OVERRIDE { return true; }
4616
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004617 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004618
4619 DECLARE_INSTRUCTION(Throw);
4620
4621 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004622 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004623
4624 DISALLOW_COPY_AND_ASSIGN(HThrow);
4625};
4626
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004627class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004628 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004629 HInstanceOf(HInstruction* object,
4630 HLoadClass* constant,
4631 bool class_is_final,
4632 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004633 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004634 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004635 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004636 dex_pc_(dex_pc) {
4637 SetRawInputAt(0, object);
4638 SetRawInputAt(1, constant);
4639 }
4640
4641 bool CanBeMoved() const OVERRIDE { return true; }
4642
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004643 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004644 return true;
4645 }
4646
4647 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004648 return false;
4649 }
4650
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004651 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004652
4653 bool IsClassFinal() const { return class_is_final_; }
4654
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004655 // Used only in code generation.
4656 bool MustDoNullCheck() const { return must_do_null_check_; }
4657 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4658
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004659 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4660 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4661 }
4662
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004663 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004664
4665 private:
4666 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004667 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004668 const uint32_t dex_pc_;
4669
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004670 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4671};
4672
Calin Juravleb1498f62015-02-16 13:13:29 +00004673class HBoundType : public HExpression<1> {
4674 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004675 // Constructs an HBoundType with the given upper_bound.
4676 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004677 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004678 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004679 upper_bound_(upper_bound),
4680 upper_can_be_null_(upper_can_be_null),
4681 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004682 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004683 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004684 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004685 }
4686
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004687 // GetUpper* should only be used in reference type propagation.
4688 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4689 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004690
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004691 void SetCanBeNull(bool can_be_null) {
4692 DCHECK(upper_can_be_null_ || !can_be_null);
4693 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004694 }
4695
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004696 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4697
Calin Juravleb1498f62015-02-16 13:13:29 +00004698 DECLARE_INSTRUCTION(BoundType);
4699
4700 private:
4701 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004702 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4703 // It is used to bound the type in cases like:
4704 // if (x instanceof ClassX) {
4705 // // uper_bound_ will be ClassX
4706 // }
4707 const ReferenceTypeInfo upper_bound_;
4708 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4709 // is false then can_be_null_ cannot be true).
4710 const bool upper_can_be_null_;
4711 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004712
4713 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4714};
4715
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004716class HCheckCast : public HTemplateInstruction<2> {
4717 public:
4718 HCheckCast(HInstruction* object,
4719 HLoadClass* constant,
4720 bool class_is_final,
4721 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004722 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004723 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004724 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004725 dex_pc_(dex_pc) {
4726 SetRawInputAt(0, object);
4727 SetRawInputAt(1, constant);
4728 }
4729
4730 bool CanBeMoved() const OVERRIDE { return true; }
4731
4732 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4733 return true;
4734 }
4735
4736 bool NeedsEnvironment() const OVERRIDE {
4737 // Instruction may throw a CheckCastError.
4738 return true;
4739 }
4740
4741 bool CanThrow() const OVERRIDE { return true; }
4742
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004743 bool MustDoNullCheck() const { return must_do_null_check_; }
4744 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4745
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004746 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004747
4748 bool IsClassFinal() const { return class_is_final_; }
4749
4750 DECLARE_INSTRUCTION(CheckCast);
4751
4752 private:
4753 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004754 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004755 const uint32_t dex_pc_;
4756
4757 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004758};
4759
Calin Juravle27df7582015-04-17 19:12:31 +01004760class HMemoryBarrier : public HTemplateInstruction<0> {
4761 public:
4762 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004763 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004764 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004765 barrier_kind_(barrier_kind) {}
4766
4767 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4768
4769 DECLARE_INSTRUCTION(MemoryBarrier);
4770
4771 private:
4772 const MemBarrierKind barrier_kind_;
4773
4774 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4775};
4776
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004777class HMonitorOperation : public HTemplateInstruction<1> {
4778 public:
4779 enum OperationKind {
4780 kEnter,
4781 kExit,
4782 };
4783
4784 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004785 : HTemplateInstruction(
4786 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004787 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004788 SetRawInputAt(0, object);
4789 }
4790
4791 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004792 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4793
4794 bool CanThrow() const OVERRIDE {
4795 // Verifier guarantees that monitor-exit cannot throw.
4796 // This is important because it allows the HGraphBuilder to remove
4797 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4798 return IsEnter();
4799 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004800
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004801 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004802
4803 bool IsEnter() const { return kind_ == kEnter; }
4804
4805 DECLARE_INSTRUCTION(MonitorOperation);
4806
Calin Juravle52c48962014-12-16 17:02:57 +00004807 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004808 const OperationKind kind_;
4809 const uint32_t dex_pc_;
4810
4811 private:
4812 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4813};
4814
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004815/**
4816 * A HInstruction used as a marker for the replacement of new + <init>
4817 * of a String to a call to a StringFactory. Only baseline will see
4818 * the node at code generation, where it will be be treated as null.
4819 * When compiling non-baseline, `HFakeString` instructions are being removed
4820 * in the instruction simplifier.
4821 */
4822class HFakeString : public HTemplateInstruction<0> {
4823 public:
4824 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4825
4826 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4827
4828 DECLARE_INSTRUCTION(FakeString);
4829
4830 private:
4831 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4832};
4833
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004834class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004835 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004836 MoveOperands(Location source,
4837 Location destination,
4838 Primitive::Type type,
4839 HInstruction* instruction)
4840 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004841
4842 Location GetSource() const { return source_; }
4843 Location GetDestination() const { return destination_; }
4844
4845 void SetSource(Location value) { source_ = value; }
4846 void SetDestination(Location value) { destination_ = value; }
4847
4848 // The parallel move resolver marks moves as "in-progress" by clearing the
4849 // destination (but not the source).
4850 Location MarkPending() {
4851 DCHECK(!IsPending());
4852 Location dest = destination_;
4853 destination_ = Location::NoLocation();
4854 return dest;
4855 }
4856
4857 void ClearPending(Location dest) {
4858 DCHECK(IsPending());
4859 destination_ = dest;
4860 }
4861
4862 bool IsPending() const {
4863 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4864 return destination_.IsInvalid() && !source_.IsInvalid();
4865 }
4866
4867 // True if this blocks a move from the given location.
4868 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004869 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004870 }
4871
4872 // A move is redundant if it's been eliminated, if its source and
4873 // destination are the same, or if its destination is unneeded.
4874 bool IsRedundant() const {
4875 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4876 }
4877
4878 // We clear both operands to indicate move that's been eliminated.
4879 void Eliminate() {
4880 source_ = destination_ = Location::NoLocation();
4881 }
4882
4883 bool IsEliminated() const {
4884 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4885 return source_.IsInvalid();
4886 }
4887
Alexey Frunze4dda3372015-06-01 18:31:49 -07004888 Primitive::Type GetType() const { return type_; }
4889
Nicolas Geoffray90218252015-04-15 11:56:51 +01004890 bool Is64BitMove() const {
4891 return Primitive::Is64BitType(type_);
4892 }
4893
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004894 HInstruction* GetInstruction() const { return instruction_; }
4895
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004896 private:
4897 Location source_;
4898 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004899 // The type this move is for.
4900 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004901 // The instruction this move is assocatied with. Null when this move is
4902 // for moving an input in the expected locations of user (including a phi user).
4903 // This is only used in debug mode, to ensure we do not connect interval siblings
4904 // in the same parallel move.
4905 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004906};
4907
4908static constexpr size_t kDefaultNumberOfMoves = 4;
4909
4910class HParallelMove : public HTemplateInstruction<0> {
4911 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004912 explicit HParallelMove(ArenaAllocator* arena)
4913 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004914
Nicolas Geoffray90218252015-04-15 11:56:51 +01004915 void AddMove(Location source,
4916 Location destination,
4917 Primitive::Type type,
4918 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004919 DCHECK(source.IsValid());
4920 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004921 if (kIsDebugBuild) {
4922 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004923 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004924 if (moves_.Get(i).GetInstruction() == instruction) {
4925 // Special case the situation where the move is for the spill slot
4926 // of the instruction.
4927 if ((GetPrevious() == instruction)
4928 || ((GetPrevious() == nullptr)
4929 && instruction->IsPhi()
4930 && instruction->GetBlock() == GetBlock())) {
4931 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4932 << "Doing parallel moves for the same instruction.";
4933 } else {
4934 DCHECK(false) << "Doing parallel moves for the same instruction.";
4935 }
4936 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004937 }
4938 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004939 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004940 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004941 << "Overlapped destination for two moves in a parallel move: "
4942 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4943 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004944 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004945 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004946 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004947 }
4948
4949 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004950 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004951 }
4952
4953 size_t NumMoves() const { return moves_.Size(); }
4954
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004955 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004956
4957 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004958 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004959
4960 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4961};
4962
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004963class HGraphVisitor : public ValueObject {
4964 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004965 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4966 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004967
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004968 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004969 virtual void VisitBasicBlock(HBasicBlock* block);
4970
Roland Levillain633021e2014-10-01 14:12:25 +01004971 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004972 void VisitInsertionOrder();
4973
Roland Levillain633021e2014-10-01 14:12:25 +01004974 // Visit the graph following dominator tree reverse post-order.
4975 void VisitReversePostOrder();
4976
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004977 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004978
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004979 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004980#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004981 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4982
4983 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4984
4985#undef DECLARE_VISIT_INSTRUCTION
4986
4987 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004988 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004989
4990 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4991};
4992
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004993class HGraphDelegateVisitor : public HGraphVisitor {
4994 public:
4995 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4996 virtual ~HGraphDelegateVisitor() {}
4997
4998 // Visit functions that delegate to to super class.
4999#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005000 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005001
5002 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5003
5004#undef DECLARE_VISIT_INSTRUCTION
5005
5006 private:
5007 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5008};
5009
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005010class HInsertionOrderIterator : public ValueObject {
5011 public:
5012 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5013
5014 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
5015 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
5016 void Advance() { ++index_; }
5017
5018 private:
5019 const HGraph& graph_;
5020 size_t index_;
5021
5022 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5023};
5024
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005025class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005026 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005027 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5028 // Check that reverse post order of the graph has been built.
5029 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5030 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005031
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005032 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
5033 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005034 void Advance() { ++index_; }
5035
5036 private:
5037 const HGraph& graph_;
5038 size_t index_;
5039
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005040 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005041};
5042
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005043class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005044 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005045 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00005046 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
5047 // Check that reverse post order of the graph has been built.
5048 DCHECK(!graph.GetReversePostOrder().IsEmpty());
5049 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005050
5051 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005052 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005053 void Advance() { --index_; }
5054
5055 private:
5056 const HGraph& graph_;
5057 size_t index_;
5058
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005059 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005060};
5061
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005062class HLinearPostOrderIterator : public ValueObject {
5063 public:
5064 explicit HLinearPostOrderIterator(const HGraph& graph)
5065 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
5066
5067 bool Done() const { return index_ == 0; }
5068
5069 HBasicBlock* Current() const { return order_.Get(index_ -1); }
5070
5071 void Advance() {
5072 --index_;
5073 DCHECK_GE(index_, 0U);
5074 }
5075
5076 private:
5077 const GrowableArray<HBasicBlock*>& order_;
5078 size_t index_;
5079
5080 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5081};
5082
5083class HLinearOrderIterator : public ValueObject {
5084 public:
5085 explicit HLinearOrderIterator(const HGraph& graph)
5086 : order_(graph.GetLinearOrder()), index_(0) {}
5087
5088 bool Done() const { return index_ == order_.Size(); }
5089 HBasicBlock* Current() const { return order_.Get(index_); }
5090 void Advance() { ++index_; }
5091
5092 private:
5093 const GrowableArray<HBasicBlock*>& order_;
5094 size_t index_;
5095
5096 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5097};
5098
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005099// Iterator over the blocks that art part of the loop. Includes blocks part
5100// of an inner loop. The order in which the blocks are iterated is on their
5101// block id.
5102class HBlocksInLoopIterator : public ValueObject {
5103 public:
5104 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5105 : blocks_in_loop_(info.GetBlocks()),
5106 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5107 index_(0) {
5108 if (!blocks_in_loop_.IsBitSet(index_)) {
5109 Advance();
5110 }
5111 }
5112
5113 bool Done() const { return index_ == blocks_.Size(); }
5114 HBasicBlock* Current() const { return blocks_.Get(index_); }
5115 void Advance() {
5116 ++index_;
5117 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5118 if (blocks_in_loop_.IsBitSet(index_)) {
5119 break;
5120 }
5121 }
5122 }
5123
5124 private:
5125 const BitVector& blocks_in_loop_;
5126 const GrowableArray<HBasicBlock*>& blocks_;
5127 size_t index_;
5128
5129 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5130};
5131
Mingyao Yang3584bce2015-05-19 16:01:59 -07005132// Iterator over the blocks that art part of the loop. Includes blocks part
5133// of an inner loop. The order in which the blocks are iterated is reverse
5134// post order.
5135class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5136 public:
5137 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5138 : blocks_in_loop_(info.GetBlocks()),
5139 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5140 index_(0) {
5141 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5142 Advance();
5143 }
5144 }
5145
5146 bool Done() const { return index_ == blocks_.Size(); }
5147 HBasicBlock* Current() const { return blocks_.Get(index_); }
5148 void Advance() {
5149 ++index_;
5150 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5151 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5152 break;
5153 }
5154 }
5155 }
5156
5157 private:
5158 const BitVector& blocks_in_loop_;
5159 const GrowableArray<HBasicBlock*>& blocks_;
5160 size_t index_;
5161
5162 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5163};
5164
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005165inline int64_t Int64FromConstant(HConstant* constant) {
5166 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5167 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5168 : constant->AsLongConstant()->GetValue();
5169}
5170
Vladimir Marko58155012015-08-19 12:49:41 +00005171inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5172 // For the purposes of the compiler, the dex files must actually be the same object
5173 // if we want to safely treat them as the same. This is especially important for JIT
5174 // as custom class loaders can open the same underlying file (or memory) multiple
5175 // times and provide different class resolution but no two class loaders should ever
5176 // use the same DexFile object - doing so is an unsupported hack that can lead to
5177 // all sorts of weird failures.
5178 return &lhs == &rhs;
5179}
5180
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005181} // namespace art
5182
5183#endif // ART_COMPILER_OPTIMIZING_NODES_H_