blob: 3f66c66ce0718b60ede7961f37be0bbbb4f8820f [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
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#include "instruction_simplifier.h"
18
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010019#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include "mirror/class-inl.h"
21#include "scoped_thread_state_change.h"
22
Nicolas Geoffray3c049742014-09-24 18:10:46 +010023namespace art {
24
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010025class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000026 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000027 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010028 : HGraphDelegateVisitor(graph),
Alexandre Rames188d4312015-04-09 18:30:21 +010029 stats_(stats) {}
30
31 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000032
33 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010034 void RecordSimplification() {
35 simplification_occurred_ = true;
36 simplifications_at_current_position_++;
37 if (stats_) {
38 stats_->RecordStat(kInstructionSimplifications);
39 }
40 }
41
Scott Wakeling40a04bf2015-12-11 09:50:36 +000042 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
43 bool TryReplaceWithRotate(HBinaryOperation* instruction);
44 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
45 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
46 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
47
Alexandre Rames188d4312015-04-09 18:30:21 +010048 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Kevin Brodsky96798492016-01-15 09:49:20 +000049 // `op` should be either HOr or HAnd.
50 // De Morgan's laws:
51 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
52 bool TryApplyDeMorgan(HBinaryOperation* op);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000053 void VisitShift(HBinaryOperation* shift);
54
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000055 void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
56 void VisitEqual(HEqual* equal) OVERRIDE;
David Brazdil0d13fee2015-04-17 14:52:19 +010057 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
58 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray07276db2015-05-18 14:22:09 +010059 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
60 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000061 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000062 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000063 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080064 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000065 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000066 void VisitAdd(HAdd* instruction) OVERRIDE;
67 void VisitAnd(HAnd* instruction) OVERRIDE;
Mark Mendellc4701932015-04-10 13:18:51 -040068 void VisitCondition(HCondition* instruction) OVERRIDE;
69 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
70 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
71 void VisitLessThan(HLessThan* condition) OVERRIDE;
72 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000073 void VisitDiv(HDiv* instruction) OVERRIDE;
74 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010075 void VisitNeg(HNeg* instruction) OVERRIDE;
76 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000077 void VisitOr(HOr* instruction) OVERRIDE;
78 void VisitShl(HShl* instruction) OVERRIDE;
79 void VisitShr(HShr* instruction) OVERRIDE;
80 void VisitSub(HSub* instruction) OVERRIDE;
81 void VisitUShr(HUShr* instruction) OVERRIDE;
82 void VisitXor(HXor* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010083 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010084 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -070085 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010086
87 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +000088
Scott Wakeling40a04bf2015-12-11 09:50:36 +000089 void SimplifyRotate(HInvoke* invoke, bool is_left);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +010090 void SimplifySystemArrayCopy(HInvoke* invoke);
91 void SimplifyStringEquals(HInvoke* invoke);
92
Calin Juravleacf735c2015-02-12 15:25:22 +000093 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010094 bool simplification_occurred_ = false;
95 int simplifications_at_current_position_ = 0;
96 // We ensure we do not loop infinitely. The value is a finger in the air guess
97 // that should allow enough simplification.
98 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000099};
100
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100101void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000102 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100103 visitor.Run();
104}
105
106void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100107 // Iterate in reverse post order to open up more simplifications to users
108 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +0100109 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
110 // The simplification of an instruction to another instruction may yield
111 // possibilities for other simplifications. So although we perform a reverse
112 // post order visit, we sometimes need to revisit an instruction index.
113 simplification_occurred_ = false;
114 VisitBasicBlock(it.Current());
115 if (simplification_occurred_ &&
116 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
117 // New simplifications may be applicable to the instruction at the
118 // current index, so don't advance the iterator.
119 continue;
120 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100121 simplifications_at_current_position_ = 0;
122 it.Advance();
123 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100124}
125
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000126namespace {
127
128bool AreAllBitsSet(HConstant* constant) {
129 return Int64FromConstant(constant) == -1;
130}
131
132} // namespace
133
Alexandre Rames188d4312015-04-09 18:30:21 +0100134// Returns true if the code was simplified to use only one negation operation
135// after the binary operation instead of one on each of the inputs.
136bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
137 DCHECK(binop->IsAdd() || binop->IsSub());
138 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
139 HNeg* left_neg = binop->GetLeft()->AsNeg();
140 HNeg* right_neg = binop->GetRight()->AsNeg();
141 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
142 !right_neg->HasOnlyOneNonEnvironmentUse()) {
143 return false;
144 }
145 // Replace code looking like
146 // NEG tmp1, a
147 // NEG tmp2, b
148 // ADD dst, tmp1, tmp2
149 // with
150 // ADD tmp, a, b
151 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600152 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
153 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
154 // while the later yields `-0.0`.
155 if (!Primitive::IsIntegralType(binop->GetType())) {
156 return false;
157 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100158 binop->ReplaceInput(left_neg->GetInput(), 0);
159 binop->ReplaceInput(right_neg->GetInput(), 1);
160 left_neg->GetBlock()->RemoveInstruction(left_neg);
161 right_neg->GetBlock()->RemoveInstruction(right_neg);
162 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
163 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
164 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
165 RecordSimplification();
166 return true;
167}
168
Kevin Brodsky96798492016-01-15 09:49:20 +0000169bool InstructionSimplifierVisitor::TryApplyDeMorgan(HBinaryOperation* op) {
170 DCHECK(op->IsAnd() || op->IsOr());
171 Primitive::Type type = op->GetType();
172 HInstruction* left = op->GetLeft();
173 HInstruction* right = op->GetRight();
174
175 // We can apply De Morgan's laws if both inputs are Not's and are only used
176 // by `op`.
177 if (left->IsNot() &&
178 right->IsNot() &&
179 left->HasOnlyOneNonEnvironmentUse() &&
180 right->HasOnlyOneNonEnvironmentUse()) {
181 // Replace code looking like
182 // NOT nota, a
183 // NOT notb, b
184 // AND dst, nota, notb (respectively OR)
185 // with
186 // OR or, a, b (respectively AND)
187 // NOT dest, or
188 HInstruction* src_left = left->AsNot()->GetInput();
189 HInstruction* src_right = right->AsNot()->GetInput();
190 uint32_t dex_pc = op->GetDexPc();
191
192 HBinaryOperation* hbin;
193 if (op->IsAnd()) {
194 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
195 } else {
196 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
197 }
198 // Step 1: replace left Not by new binary operation (this is arbitrary,
199 // the right one could have been used instead).
200 // SL SR SL SR
201 // | | | / |
202 // Not Not NewBin Not
203 // \ / -> \ /
204 // OldBin OldBin
205 // | |
206 left->GetBlock()->ReplaceAndRemoveInstructionWith(left, hbin);
207
208 HNot* hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
209 // Step 2: replace old binary operation by a new Not, and remove
210 // the now disconnected right Not.
211 // SL SR SL SR
212 // | / | | / |
213 // NewBin Not NewBin Not [removed]
214 // \ / -> \ x
215 // OldBin Not
216 // | |
217 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
218 right->GetBlock()->RemoveInstruction(right);
219
220 RecordSimplification();
221 return true;
222 }
223
224 return false;
225}
226
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000227void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
228 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
229 HConstant* input_cst = instruction->GetConstantRight();
230 HInstruction* input_other = instruction->GetLeastConstantLeft();
231
Mark Mendellba56d062015-05-05 21:34:03 -0400232 if (input_cst != nullptr) {
233 if (input_cst->IsZero()) {
234 // Replace code looking like
235 // SHL dst, src, 0
236 // with
237 // src
238 instruction->ReplaceWith(input_other);
239 instruction->GetBlock()->RemoveInstruction(instruction);
Mark Mendellba56d062015-05-05 21:34:03 -0400240 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000241 }
242}
243
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000244static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
245 return (sub->GetRight() == other &&
246 sub->GetLeft()->IsConstant() &&
247 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
248}
249
250bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
251 HUShr* ushr,
252 HShl* shl) {
253 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
254 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(),
255 ushr->GetLeft(),
256 ushr->GetRight());
257 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
258 if (!ushr->HasUses()) {
259 ushr->GetBlock()->RemoveInstruction(ushr);
260 }
261 if (!ushr->GetRight()->HasUses()) {
262 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
263 }
264 if (!shl->HasUses()) {
265 shl->GetBlock()->RemoveInstruction(shl);
266 }
267 if (!shl->GetRight()->HasUses()) {
268 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
269 }
270 return true;
271}
272
273// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
274bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000275 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
276 HInstruction* left = op->GetLeft();
277 HInstruction* right = op->GetRight();
278 // If we have an UShr and a Shl (in either order).
279 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
280 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
281 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
282 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
283 if (ushr->GetType() == shl->GetType() &&
284 ushr->GetLeft() == shl->GetLeft()) {
285 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
286 // Shift distances are both constant, try replacing with Ror if they
287 // add up to the register size.
288 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
289 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
290 // Shift distances are potentially of the form x and (reg_size - x).
291 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
292 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
293 // Shift distances are potentially of the form d and -d.
294 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
295 }
296 }
297 }
298 return false;
299}
300
301// Try replacing code looking like (x >>> #rdist OP x << #ldist):
302// UShr dst, x, #rdist
303// Shl tmp, x, #ldist
304// OP dst, dst, tmp
305// or like (x >>> #rdist OP x << #-ldist):
306// UShr dst, x, #rdist
307// Shl tmp, x, #-ldist
308// OP dst, dst, tmp
309// with
310// Ror dst, x, #rdist
311bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
312 HUShr* ushr,
313 HShl* shl) {
314 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
315 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
316 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
317 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
318 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
319 ReplaceRotateWithRor(op, ushr, shl);
320 return true;
321 }
322 return false;
323}
324
325// Replace code looking like (x >>> -d OP x << d):
326// Neg neg, d
327// UShr dst, x, neg
328// Shl tmp, x, d
329// OP dst, dst, tmp
330// with
331// Neg neg, d
332// Ror dst, x, neg
333// *** OR ***
334// Replace code looking like (x >>> d OP x << -d):
335// UShr dst, x, d
336// Neg neg, d
337// Shl tmp, x, neg
338// OP dst, dst, tmp
339// with
340// Ror dst, x, d
341bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
342 HUShr* ushr,
343 HShl* shl) {
344 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
345 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
346 bool neg_is_left = shl->GetRight()->IsNeg();
347 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
348 // And the shift distance being negated is the distance being shifted the other way.
349 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
350 ReplaceRotateWithRor(op, ushr, shl);
351 }
352 return false;
353}
354
355// Try replacing code looking like (x >>> d OP x << (#bits - d)):
356// UShr dst, x, d
357// Sub ld, #bits, d
358// Shl tmp, x, ld
359// OP dst, dst, tmp
360// with
361// Ror dst, x, d
362// *** OR ***
363// Replace code looking like (x >>> (#bits - d) OP x << d):
364// Sub rd, #bits, d
365// UShr dst, x, rd
366// Shl tmp, x, d
367// OP dst, dst, tmp
368// with
369// Neg neg, d
370// Ror dst, x, neg
371bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
372 HUShr* ushr,
373 HShl* shl) {
374 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
375 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
376 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
377 HInstruction* shl_shift = shl->GetRight();
378 HInstruction* ushr_shift = ushr->GetRight();
379 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
380 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
381 return ReplaceRotateWithRor(op, ushr, shl);
382 }
383 return false;
384}
385
Calin Juravle10e244f2015-01-26 18:54:32 +0000386void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
387 HInstruction* obj = null_check->InputAt(0);
388 if (!obj->CanBeNull()) {
389 null_check->ReplaceWith(obj);
390 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000391 if (stats_ != nullptr) {
392 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
393 }
394 }
395}
396
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100397bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
398 if (!input->CanBeNull()) {
399 return true;
400 }
401
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100402 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
403 HInstruction* use = it.Current()->GetUser();
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100404 if (use->IsNullCheck() && use->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100405 return true;
406 }
407 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100408
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100409 return false;
410}
411
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100412// Returns whether doing a type test between the class of `object` against `klass` has
413// a statically known outcome. The result of the test is stored in `outcome`.
414static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000415 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
416 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
417 ScopedObjectAccess soa(Thread::Current());
418 if (!obj_rti.IsValid()) {
419 // We run the simplifier before the reference type propagation so type info might not be
420 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100421 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000422 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100423
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100424 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100425 if (!class_rti.IsValid()) {
426 // Happens when the loaded class is unresolved.
427 return false;
428 }
429 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000430 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100431 *outcome = true;
432 return true;
433 } else if (obj_rti.IsExact()) {
434 // The test failed at compile time so will also fail at runtime.
435 *outcome = false;
436 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100437 } else if (!class_rti.IsInterface()
438 && !obj_rti.IsInterface()
439 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100440 // Different type hierarchy. The test will fail.
441 *outcome = false;
442 return true;
443 }
444 return false;
445}
446
447void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
448 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100449 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
450 if (load_class->NeedsAccessCheck()) {
451 // If we need to perform an access check we cannot remove the instruction.
452 return;
453 }
454
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100455 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100456 check_cast->ClearMustDoNullCheck();
457 }
458
459 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000460 check_cast->GetBlock()->RemoveInstruction(check_cast);
461 if (stats_ != nullptr) {
462 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
463 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100464 return;
465 }
466
467 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700468 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100469 if (outcome) {
470 check_cast->GetBlock()->RemoveInstruction(check_cast);
471 if (stats_ != nullptr) {
472 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
473 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700474 if (!load_class->HasUses()) {
475 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
476 // However, here we know that it cannot because the checkcast was successfull, hence
477 // the class was already loaded.
478 load_class->GetBlock()->RemoveInstruction(load_class);
479 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100480 } else {
481 // Don't do anything for exceptional cases for now. Ideally we should remove
482 // all instructions and blocks this instruction dominates.
483 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000484 }
485}
486
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100487void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100488 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100489 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
490 if (load_class->NeedsAccessCheck()) {
491 // If we need to perform an access check we cannot remove the instruction.
492 return;
493 }
494
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100495 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100496 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100497 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100498 instruction->ClearMustDoNullCheck();
499 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100500
501 HGraph* graph = GetGraph();
502 if (object->IsNullConstant()) {
503 instruction->ReplaceWith(graph->GetIntConstant(0));
504 instruction->GetBlock()->RemoveInstruction(instruction);
505 RecordSimplification();
506 return;
507 }
508
509 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700510 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100511 if (outcome && can_be_null) {
512 // Type test will succeed, we just need a null test.
513 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
514 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
515 instruction->ReplaceWith(test);
516 } else {
517 // We've statically determined the result of the instanceof.
518 instruction->ReplaceWith(graph->GetIntConstant(outcome));
519 }
520 RecordSimplification();
521 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700522 if (outcome && !load_class->HasUses()) {
523 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
524 // However, here we know that it cannot because the instanceof check was successfull, hence
525 // the class was already loaded.
526 load_class->GetBlock()->RemoveInstruction(load_class);
527 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100528 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100529}
530
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100531void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
532 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100533 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100534 instruction->ClearValueCanBeNull();
535 }
536}
537
538void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
539 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100540 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100541 instruction->ClearValueCanBeNull();
542 }
543}
544
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000545void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100546 HBasicBlock* block = check->GetBlock();
547 // Currently always keep the suspend check at entry.
548 if (block->IsEntryBlock()) return;
549
550 // Currently always keep suspend checks at loop entry.
551 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
552 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
553 return;
554 }
555
556 // Remove the suspend check that was added at build time for the baseline
557 // compiler.
558 block->RemoveInstruction(check);
559}
560
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000561void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100562 HInstruction* input_const = equal->GetConstantRight();
563 if (input_const != nullptr) {
564 HInstruction* input_value = equal->GetLeastConstantLeft();
565 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
566 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100567 // We are comparing the boolean to a constant which is of type int and can
568 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100569 if (input_const->AsIntConstant()->IsOne()) {
570 // Replace (bool_value == true) with bool_value
571 equal->ReplaceWith(input_value);
572 block->RemoveInstruction(equal);
573 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100574 } else if (input_const->AsIntConstant()->IsZero()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500575 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
576 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100577 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100578 } else {
579 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
580 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
581 block->RemoveInstruction(equal);
582 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100583 }
Mark Mendellc4701932015-04-10 13:18:51 -0400584 } else {
585 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100586 }
Mark Mendellc4701932015-04-10 13:18:51 -0400587 } else {
588 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100589 }
590}
591
David Brazdil0d13fee2015-04-17 14:52:19 +0100592void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
593 HInstruction* input_const = not_equal->GetConstantRight();
594 if (input_const != nullptr) {
595 HInstruction* input_value = not_equal->GetLeastConstantLeft();
596 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
597 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100598 // We are comparing the boolean to a constant which is of type int and can
599 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100600 if (input_const->AsIntConstant()->IsOne()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500601 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
602 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100603 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100604 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100605 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100606 not_equal->ReplaceWith(input_value);
607 block->RemoveInstruction(not_equal);
608 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100609 } else {
610 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
611 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
612 block->RemoveInstruction(not_equal);
613 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100614 }
Mark Mendellc4701932015-04-10 13:18:51 -0400615 } else {
616 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100617 }
Mark Mendellc4701932015-04-10 13:18:51 -0400618 } else {
619 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100620 }
621}
622
623void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
624 HInstruction* parent = bool_not->InputAt(0);
625 if (parent->IsBooleanNot()) {
626 HInstruction* value = parent->InputAt(0);
627 // Replace (!(!bool_value)) with bool_value
628 bool_not->ReplaceWith(value);
629 bool_not->GetBlock()->RemoveInstruction(bool_not);
630 // It is possible that `parent` is dead at this point but we leave
631 // its removal to DCE for simplicity.
632 RecordSimplification();
633 }
634}
635
Mingyao Yang0304e182015-01-30 16:41:29 -0800636void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
637 HInstruction* input = instruction->InputAt(0);
638 // If the array is a NewArray with constant size, replace the array length
639 // with the constant instruction. This helps the bounds check elimination phase.
640 if (input->IsNewArray()) {
641 input = input->InputAt(0);
642 if (input->IsIntConstant()) {
643 instruction->ReplaceWith(input);
644 }
645 }
646}
647
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000648void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000649 HInstruction* value = instruction->GetValue();
650 if (value->GetType() != Primitive::kPrimNot) return;
651
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100652 if (CanEnsureNotNullAt(value, instruction)) {
653 instruction->ClearValueCanBeNull();
654 }
655
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000656 if (value->IsArrayGet()) {
657 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
658 // If the code is just swapping elements in the array, no need for a type check.
659 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100660 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000661 }
662 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100663
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100664 if (value->IsNullConstant()) {
665 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100666 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100667 }
668
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100669 ScopedObjectAccess soa(Thread::Current());
670 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
671 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
672 if (!array_rti.IsValid()) {
673 return;
674 }
675
676 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
677 instruction->ClearNeedsTypeCheck();
678 return;
679 }
680
681 if (array_rti.IsObjectArray()) {
682 if (array_rti.IsExact()) {
683 instruction->ClearNeedsTypeCheck();
684 return;
685 }
686 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100687 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000688}
689
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000690void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
691 if (instruction->GetResultType() == instruction->GetInputType()) {
692 // Remove the instruction if it's converting to the same type.
693 instruction->ReplaceWith(instruction->GetInput());
694 instruction->GetBlock()->RemoveInstruction(instruction);
695 }
696}
697
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000698void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
699 HConstant* input_cst = instruction->GetConstantRight();
700 HInstruction* input_other = instruction->GetLeastConstantLeft();
701 if ((input_cst != nullptr) && input_cst->IsZero()) {
702 // Replace code looking like
703 // ADD dst, src, 0
704 // with
705 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600706 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
707 // `x` is `-0.0`, the former expression yields `0.0`, while the later
708 // yields `-0.0`.
709 if (Primitive::IsIntegralType(instruction->GetType())) {
710 instruction->ReplaceWith(input_other);
711 instruction->GetBlock()->RemoveInstruction(instruction);
712 return;
713 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100714 }
715
716 HInstruction* left = instruction->GetLeft();
717 HInstruction* right = instruction->GetRight();
718 bool left_is_neg = left->IsNeg();
719 bool right_is_neg = right->IsNeg();
720
721 if (left_is_neg && right_is_neg) {
722 if (TryMoveNegOnInputsAfterBinop(instruction)) {
723 return;
724 }
725 }
726
727 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
728 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
729 // Replace code looking like
730 // NEG tmp, b
731 // ADD dst, a, tmp
732 // with
733 // SUB dst, a, b
734 // We do not perform the optimization if the input negation has environment
735 // uses or multiple non-environment uses as it could lead to worse code. In
736 // particular, we do not want the live range of `b` to be extended if we are
737 // not sure the initial 'NEG' instruction can be removed.
738 HInstruction* other = left_is_neg ? right : left;
739 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
740 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
741 RecordSimplification();
742 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000743 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000744 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000745
746 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000747}
748
749void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
750 HConstant* input_cst = instruction->GetConstantRight();
751 HInstruction* input_other = instruction->GetLeastConstantLeft();
752
Vladimir Marko452c1b62015-09-25 14:44:17 +0100753 if (input_cst != nullptr) {
754 int64_t value = Int64FromConstant(input_cst);
755 if (value == -1) {
756 // Replace code looking like
757 // AND dst, src, 0xFFF...FF
758 // with
759 // src
760 instruction->ReplaceWith(input_other);
761 instruction->GetBlock()->RemoveInstruction(instruction);
762 RecordSimplification();
763 return;
764 }
765 // Eliminate And from UShr+And if the And-mask contains all the bits that
766 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
767 // precisely clears the shifted-in sign bits.
768 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
769 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
770 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
771 size_t num_tail_bits_set = CTZ(value + 1);
772 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
773 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
774 instruction->ReplaceWith(input_other);
775 instruction->GetBlock()->RemoveInstruction(instruction);
776 RecordSimplification();
777 return;
778 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
779 input_other->HasOnlyOneNonEnvironmentUse()) {
780 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
781 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
782 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
783 input_other->InputAt(0),
784 input_other->InputAt(1),
785 input_other->GetDexPc());
786 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
787 input_other->GetBlock()->RemoveInstruction(input_other);
788 RecordSimplification();
789 return;
790 }
791 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000792 }
793
794 // We assume that GVN has run before, so we only perform a pointer comparison.
795 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100796 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000797 if (instruction->GetLeft() == instruction->GetRight()) {
798 // Replace code looking like
799 // AND dst, src, src
800 // with
801 // src
802 instruction->ReplaceWith(instruction->GetLeft());
803 instruction->GetBlock()->RemoveInstruction(instruction);
Kevin Brodsky96798492016-01-15 09:49:20 +0000804 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000805 }
Kevin Brodsky96798492016-01-15 09:49:20 +0000806
807 TryApplyDeMorgan(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000808}
809
Mark Mendellc4701932015-04-10 13:18:51 -0400810void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
811 VisitCondition(condition);
812}
813
814void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
815 VisitCondition(condition);
816}
817
818void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
819 VisitCondition(condition);
820}
821
822void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
823 VisitCondition(condition);
824}
825
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +0000826// TODO: unsigned comparisons too?
Aart Bike9f37602015-10-09 11:15:55 -0700827
Mark Mendellc4701932015-04-10 13:18:51 -0400828void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +0000829 // Try to fold an HCompare into this HCondition.
Mark Mendellc4701932015-04-10 13:18:51 -0400830
Mark Mendellc4701932015-04-10 13:18:51 -0400831 HInstruction* left = condition->GetLeft();
832 HInstruction* right = condition->GetRight();
833 // We can only replace an HCondition which compares a Compare to 0.
834 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
835 // condition with a long, float or double comparison as input.
836 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
837 // Conversion is not possible.
838 return;
839 }
840
841 // Is the Compare only used for this purpose?
842 if (!left->GetUses().HasOnlyOneUse()) {
843 // Someone else also wants the result of the compare.
844 return;
845 }
846
847 if (!left->GetEnvUses().IsEmpty()) {
848 // There is a reference to the compare result in an environment. Do we really need it?
849 if (GetGraph()->IsDebuggable()) {
850 return;
851 }
852
853 // We have to ensure that there are no deopt points in the sequence.
854 if (left->HasAnyEnvironmentUseBefore(condition)) {
855 return;
856 }
857 }
858
859 // Clean up any environment uses from the HCompare, if any.
860 left->RemoveEnvironmentUsers();
861
862 // We have decided to fold the HCompare into the HCondition. Transfer the information.
863 condition->SetBias(left->AsCompare()->GetBias());
864
865 // Replace the operands of the HCondition.
866 condition->ReplaceInput(left->InputAt(0), 0);
867 condition->ReplaceInput(left->InputAt(1), 1);
868
869 // Remove the HCompare.
870 left->GetBlock()->RemoveInstruction(left);
871
872 RecordSimplification();
873}
874
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000875void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
876 HConstant* input_cst = instruction->GetConstantRight();
877 HInstruction* input_other = instruction->GetLeastConstantLeft();
878 Primitive::Type type = instruction->GetType();
879
880 if ((input_cst != nullptr) && input_cst->IsOne()) {
881 // Replace code looking like
882 // DIV dst, src, 1
883 // with
884 // src
885 instruction->ReplaceWith(input_other);
886 instruction->GetBlock()->RemoveInstruction(instruction);
887 return;
888 }
889
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000890 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000891 // Replace code looking like
892 // DIV dst, src, -1
893 // with
894 // NEG dst, src
895 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000896 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100897 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000898 return;
899 }
900
901 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
902 // Try replacing code looking like
903 // DIV dst, src, constant
904 // with
905 // MUL dst, src, 1 / constant
906 HConstant* reciprocal = nullptr;
907 if (type == Primitive::Primitive::kPrimDouble) {
908 double value = input_cst->AsDoubleConstant()->GetValue();
909 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
910 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
911 }
912 } else {
913 DCHECK_EQ(type, Primitive::kPrimFloat);
914 float value = input_cst->AsFloatConstant()->GetValue();
915 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
916 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
917 }
918 }
919
920 if (reciprocal != nullptr) {
921 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
922 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
923 RecordSimplification();
924 return;
925 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000926 }
927}
928
929void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
930 HConstant* input_cst = instruction->GetConstantRight();
931 HInstruction* input_other = instruction->GetLeastConstantLeft();
932 Primitive::Type type = instruction->GetType();
933 HBasicBlock* block = instruction->GetBlock();
934 ArenaAllocator* allocator = GetGraph()->GetArena();
935
936 if (input_cst == nullptr) {
937 return;
938 }
939
940 if (input_cst->IsOne()) {
941 // Replace code looking like
942 // MUL dst, src, 1
943 // with
944 // src
945 instruction->ReplaceWith(input_other);
946 instruction->GetBlock()->RemoveInstruction(instruction);
947 return;
948 }
949
950 if (input_cst->IsMinusOne() &&
951 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
952 // Replace code looking like
953 // MUL dst, src, -1
954 // with
955 // NEG dst, src
956 HNeg* neg = new (allocator) HNeg(type, input_other);
957 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100958 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000959 return;
960 }
961
962 if (Primitive::IsFloatingPointType(type) &&
963 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
964 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
965 // Replace code looking like
966 // FP_MUL dst, src, 2.0
967 // with
968 // FP_ADD dst, src, src
969 // The 'int' and 'long' cases are handled below.
970 block->ReplaceAndRemoveInstructionWith(instruction,
971 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100972 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000973 return;
974 }
975
976 if (Primitive::IsIntOrLongType(type)) {
977 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600978 // Even though constant propagation also takes care of the zero case, other
979 // optimizations can lead to having a zero multiplication.
980 if (factor == 0) {
981 // Replace code looking like
982 // MUL dst, src, 0
983 // with
984 // 0
985 instruction->ReplaceWith(input_cst);
986 instruction->GetBlock()->RemoveInstruction(instruction);
987 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000988 // Replace code looking like
989 // MUL dst, src, pow_of_2
990 // with
991 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000992 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000993 HShl* shl = new(allocator) HShl(type, input_other, shift);
994 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100995 RecordSimplification();
Alexandre Rames38db7852015-11-20 15:02:45 +0000996 } else if (IsPowerOfTwo(factor - 1)) {
997 // Transform code looking like
998 // MUL dst, src, (2^n + 1)
999 // into
1000 // SHL tmp, src, n
1001 // ADD dst, src, tmp
1002 HShl* shl = new (allocator) HShl(type,
1003 input_other,
1004 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1005 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1006
1007 block->InsertInstructionBefore(shl, instruction);
1008 block->ReplaceAndRemoveInstructionWith(instruction, add);
1009 RecordSimplification();
1010 } else if (IsPowerOfTwo(factor + 1)) {
1011 // Transform code looking like
1012 // MUL dst, src, (2^n - 1)
1013 // into
1014 // SHL tmp, src, n
1015 // SUB dst, tmp, src
1016 HShl* shl = new (allocator) HShl(type,
1017 input_other,
1018 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1019 HSub* sub = new (allocator) HSub(type, shl, input_other);
1020
1021 block->InsertInstructionBefore(shl, instruction);
1022 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1023 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001024 }
1025 }
1026}
1027
Alexandre Rames188d4312015-04-09 18:30:21 +01001028void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1029 HInstruction* input = instruction->GetInput();
1030 if (input->IsNeg()) {
1031 // Replace code looking like
1032 // NEG tmp, src
1033 // NEG dst, tmp
1034 // with
1035 // src
1036 HNeg* previous_neg = input->AsNeg();
1037 instruction->ReplaceWith(previous_neg->GetInput());
1038 instruction->GetBlock()->RemoveInstruction(instruction);
1039 // We perform the optimization even if the input negation has environment
1040 // uses since it allows removing the current instruction. But we only delete
1041 // the input negation only if it is does not have any uses left.
1042 if (!previous_neg->HasUses()) {
1043 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1044 }
1045 RecordSimplification();
1046 return;
1047 }
1048
Serguei Katkov339dfc22015-04-20 12:29:32 +06001049 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1050 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001051 // Replace code looking like
1052 // SUB tmp, a, b
1053 // NEG dst, tmp
1054 // with
1055 // SUB dst, b, a
1056 // We do not perform the optimization if the input subtraction has
1057 // environment uses or multiple non-environment uses as it could lead to
1058 // worse code. In particular, we do not want the live ranges of `a` and `b`
1059 // to be extended if we are not sure the initial 'SUB' instruction can be
1060 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001061 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001062 HSub* sub = input->AsSub();
1063 HSub* new_sub =
1064 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1065 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1066 if (!sub->HasUses()) {
1067 sub->GetBlock()->RemoveInstruction(sub);
1068 }
1069 RecordSimplification();
1070 }
1071}
1072
1073void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1074 HInstruction* input = instruction->GetInput();
1075 if (input->IsNot()) {
1076 // Replace code looking like
1077 // NOT tmp, src
1078 // NOT dst, tmp
1079 // with
1080 // src
1081 // We perform the optimization even if the input negation has environment
1082 // uses since it allows removing the current instruction. But we only delete
1083 // the input negation only if it is does not have any uses left.
1084 HNot* previous_not = input->AsNot();
1085 instruction->ReplaceWith(previous_not->GetInput());
1086 instruction->GetBlock()->RemoveInstruction(instruction);
1087 if (!previous_not->HasUses()) {
1088 previous_not->GetBlock()->RemoveInstruction(previous_not);
1089 }
1090 RecordSimplification();
1091 }
1092}
1093
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001094void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1095 HConstant* input_cst = instruction->GetConstantRight();
1096 HInstruction* input_other = instruction->GetLeastConstantLeft();
1097
1098 if ((input_cst != nullptr) && input_cst->IsZero()) {
1099 // Replace code looking like
1100 // OR dst, src, 0
1101 // with
1102 // src
1103 instruction->ReplaceWith(input_other);
1104 instruction->GetBlock()->RemoveInstruction(instruction);
1105 return;
1106 }
1107
1108 // We assume that GVN has run before, so we only perform a pointer comparison.
1109 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001110 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001111 if (instruction->GetLeft() == instruction->GetRight()) {
1112 // Replace code looking like
1113 // OR dst, src, src
1114 // with
1115 // src
1116 instruction->ReplaceWith(instruction->GetLeft());
1117 instruction->GetBlock()->RemoveInstruction(instruction);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001118 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001119 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001120
Kevin Brodsky96798492016-01-15 09:49:20 +00001121 if (TryApplyDeMorgan(instruction)) return;
1122
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001123 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001124}
1125
1126void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1127 VisitShift(instruction);
1128}
1129
1130void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1131 VisitShift(instruction);
1132}
1133
1134void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1135 HConstant* input_cst = instruction->GetConstantRight();
1136 HInstruction* input_other = instruction->GetLeastConstantLeft();
1137
Serguei Katkov115b53f2015-08-05 17:03:30 +06001138 Primitive::Type type = instruction->GetType();
1139 if (Primitive::IsFloatingPointType(type)) {
1140 return;
1141 }
1142
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001143 if ((input_cst != nullptr) && input_cst->IsZero()) {
1144 // Replace code looking like
1145 // SUB dst, src, 0
1146 // with
1147 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001148 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1149 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1150 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001151 instruction->ReplaceWith(input_other);
1152 instruction->GetBlock()->RemoveInstruction(instruction);
1153 return;
1154 }
1155
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001156 HBasicBlock* block = instruction->GetBlock();
1157 ArenaAllocator* allocator = GetGraph()->GetArena();
1158
Alexandre Rames188d4312015-04-09 18:30:21 +01001159 HInstruction* left = instruction->GetLeft();
1160 HInstruction* right = instruction->GetRight();
1161 if (left->IsConstant()) {
1162 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001163 // Replace code looking like
1164 // SUB dst, 0, src
1165 // with
1166 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001167 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001168 // `x` is `0.0`, the former expression yields `0.0`, while the later
1169 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001170 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001171 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001172 RecordSimplification();
1173 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001174 }
1175 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001176
1177 if (left->IsNeg() && right->IsNeg()) {
1178 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1179 return;
1180 }
1181 }
1182
1183 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1184 // Replace code looking like
1185 // NEG tmp, b
1186 // SUB dst, a, tmp
1187 // with
1188 // ADD dst, a, b
1189 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1190 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1191 RecordSimplification();
1192 right->GetBlock()->RemoveInstruction(right);
1193 return;
1194 }
1195
1196 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1197 // Replace code looking like
1198 // NEG tmp, a
1199 // SUB dst, tmp, b
1200 // with
1201 // ADD tmp, a, b
1202 // NEG dst, tmp
1203 // The second version is not intrinsically better, but enables more
1204 // transformations.
1205 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1206 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1207 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1208 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1209 instruction->ReplaceWith(neg);
1210 instruction->GetBlock()->RemoveInstruction(instruction);
1211 RecordSimplification();
1212 left->GetBlock()->RemoveInstruction(left);
1213 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001214}
1215
1216void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1217 VisitShift(instruction);
1218}
1219
1220void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1221 HConstant* input_cst = instruction->GetConstantRight();
1222 HInstruction* input_other = instruction->GetLeastConstantLeft();
1223
1224 if ((input_cst != nullptr) && input_cst->IsZero()) {
1225 // Replace code looking like
1226 // XOR dst, src, 0
1227 // with
1228 // src
1229 instruction->ReplaceWith(input_other);
1230 instruction->GetBlock()->RemoveInstruction(instruction);
1231 return;
1232 }
1233
1234 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1235 // Replace code looking like
1236 // XOR dst, src, 0xFFF...FF
1237 // with
1238 // NOT dst, src
1239 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1240 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001241 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001242 return;
1243 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001244
Kevin Brodsky96798492016-01-15 09:49:20 +00001245 HInstruction* left = instruction->GetLeft();
1246 HInstruction* right = instruction->GetRight();
1247 if (left->IsNot() &&
1248 right->IsNot() &&
1249 left->HasOnlyOneNonEnvironmentUse() &&
1250 right->HasOnlyOneNonEnvironmentUse()) {
1251 // Replace code looking like
1252 // NOT nota, a
1253 // NOT notb, b
1254 // XOR dst, nota, notb
1255 // with
1256 // XOR dst, a, b
1257 instruction->ReplaceInput(left->AsNot()->GetInput(), 0);
1258 instruction->ReplaceInput(right->AsNot()->GetInput(), 1);
1259 left->GetBlock()->RemoveInstruction(left);
1260 right->GetBlock()->RemoveInstruction(right);
1261 RecordSimplification();
1262 return;
1263 }
1264
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001265 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001266}
1267
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001268void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1269 HInstruction* argument = instruction->InputAt(1);
1270 HInstruction* receiver = instruction->InputAt(0);
1271 if (receiver == argument) {
1272 // Because String.equals is an instance call, the receiver is
1273 // a null check if we don't know it's null. The argument however, will
1274 // be the actual object. So we cannot end up in a situation where both
1275 // are equal but could be null.
1276 DCHECK(CanEnsureNotNullAt(argument, instruction));
1277 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1278 instruction->GetBlock()->RemoveInstruction(instruction);
1279 } else {
1280 StringEqualsOptimizations optimizations(instruction);
1281 if (CanEnsureNotNullAt(argument, instruction)) {
1282 optimizations.SetArgumentNotNull();
1283 }
1284 ScopedObjectAccess soa(Thread::Current());
1285 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1286 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1287 optimizations.SetArgumentIsString();
1288 }
1289 }
1290}
1291
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001292void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke, bool is_left) {
1293 DCHECK(invoke->IsInvokeStaticOrDirect());
1294 DCHECK_EQ(invoke->GetOriginalInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001295 HInstruction* value = invoke->InputAt(0);
1296 HInstruction* distance = invoke->InputAt(1);
1297 // Replace the invoke with an HRor.
1298 if (is_left) {
1299 distance = new (GetGraph()->GetArena()) HNeg(distance->GetType(), distance);
1300 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1301 }
1302 HRor* ror = new (GetGraph()->GetArena()) HRor(value->GetType(), value, distance);
1303 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1304 // Remove ClinitCheck and LoadClass, if possible.
1305 HInstruction* clinit = invoke->InputAt(invoke->InputCount() - 1);
1306 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1307 clinit->GetBlock()->RemoveInstruction(clinit);
1308 HInstruction* ldclass = clinit->InputAt(0);
1309 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1310 ldclass->GetBlock()->RemoveInstruction(ldclass);
1311 }
1312 }
1313}
1314
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001315static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1316 if (potential_length->IsArrayLength()) {
1317 return potential_length->InputAt(0) == potential_array;
1318 }
1319
1320 if (potential_array->IsNewArray()) {
1321 return potential_array->InputAt(0) == potential_length;
1322 }
1323
1324 return false;
1325}
1326
1327void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1328 HInstruction* source = instruction->InputAt(0);
1329 HInstruction* destination = instruction->InputAt(2);
1330 HInstruction* count = instruction->InputAt(4);
1331 SystemArrayCopyOptimizations optimizations(instruction);
1332 if (CanEnsureNotNullAt(source, instruction)) {
1333 optimizations.SetSourceIsNotNull();
1334 }
1335 if (CanEnsureNotNullAt(destination, instruction)) {
1336 optimizations.SetDestinationIsNotNull();
1337 }
1338 if (destination == source) {
1339 optimizations.SetDestinationIsSource();
1340 }
1341
1342 if (IsArrayLengthOf(count, source)) {
1343 optimizations.SetCountIsSourceLength();
1344 }
1345
1346 if (IsArrayLengthOf(count, destination)) {
1347 optimizations.SetCountIsDestinationLength();
1348 }
1349
1350 {
1351 ScopedObjectAccess soa(Thread::Current());
1352 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1353 if (destination_rti.IsValid()) {
1354 if (destination_rti.IsObjectArray()) {
1355 if (destination_rti.IsExact()) {
1356 optimizations.SetDoesNotNeedTypeCheck();
1357 }
1358 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001359 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001360 if (destination_rti.IsPrimitiveArrayClass()) {
1361 optimizations.SetDestinationIsPrimitiveArray();
1362 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1363 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001364 }
1365 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001366 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1367 if (source_rti.IsValid()) {
1368 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1369 optimizations.SetDoesNotNeedTypeCheck();
1370 }
1371 if (source_rti.IsPrimitiveArrayClass()) {
1372 optimizations.SetSourceIsPrimitiveArray();
1373 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1374 optimizations.SetSourceIsNonPrimitiveArray();
1375 }
1376 }
1377 }
1378}
1379
1380void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
1381 if (instruction->GetIntrinsic() == Intrinsics::kStringEquals) {
1382 SimplifyStringEquals(instruction);
1383 } else if (instruction->GetIntrinsic() == Intrinsics::kSystemArrayCopy) {
1384 SimplifySystemArrayCopy(instruction);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001385 } else if (instruction->GetIntrinsic() == Intrinsics::kIntegerRotateRight ||
1386 instruction->GetIntrinsic() == Intrinsics::kLongRotateRight) {
1387 SimplifyRotate(instruction, false);
1388 } else if (instruction->GetIntrinsic() == Intrinsics::kIntegerRotateLeft ||
1389 instruction->GetIntrinsic() == Intrinsics::kLongRotateLeft) {
1390 SimplifyRotate(instruction, true);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001391 }
1392}
1393
Aart Bikbb245d12015-10-19 11:05:03 -07001394void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1395 HInstruction* cond = deoptimize->InputAt(0);
1396 if (cond->IsConstant()) {
1397 if (cond->AsIntConstant()->IsZero()) {
1398 // Never deopt: instruction can be removed.
1399 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1400 } else {
1401 // Always deopt.
1402 }
1403 }
1404}
1405
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001406} // namespace art