blob: c1e38633fc3b9d8c2eca4a101f88753488b608fc [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);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +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 TryDeMorganNegationFactoring(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;
David Brazdil74eb1b22015-12-14 11:44:01 +000083 void VisitSelect(HSelect* select) OVERRIDE;
84 void VisitIf(HIf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010085 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010086 void VisitInvoke(HInvoke* invoke) OVERRIDE;
Aart Bikbb245d12015-10-19 11:05:03 -070087 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010088
89 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +000090
Scott Wakeling40a04bf2015-12-11 09:50:36 +000091 void SimplifyRotate(HInvoke* invoke, bool is_left);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +010092 void SimplifySystemArrayCopy(HInvoke* invoke);
93 void SimplifyStringEquals(HInvoke* invoke);
94
Calin Juravleacf735c2015-02-12 15:25:22 +000095 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010096 bool simplification_occurred_ = false;
97 int simplifications_at_current_position_ = 0;
98 // We ensure we do not loop infinitely. The value is a finger in the air guess
99 // that should allow enough simplification.
100 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000101};
102
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100103void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +0000104 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +0100105 visitor.Run();
106}
107
108void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100109 // Iterate in reverse post order to open up more simplifications to users
110 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +0100111 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
112 // The simplification of an instruction to another instruction may yield
113 // possibilities for other simplifications. So although we perform a reverse
114 // post order visit, we sometimes need to revisit an instruction index.
115 simplification_occurred_ = false;
116 VisitBasicBlock(it.Current());
117 if (simplification_occurred_ &&
118 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
119 // New simplifications may be applicable to the instruction at the
120 // current index, so don't advance the iterator.
121 continue;
122 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100123 simplifications_at_current_position_ = 0;
124 it.Advance();
125 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100126}
127
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000128namespace {
129
130bool AreAllBitsSet(HConstant* constant) {
131 return Int64FromConstant(constant) == -1;
132}
133
134} // namespace
135
Alexandre Rames188d4312015-04-09 18:30:21 +0100136// Returns true if the code was simplified to use only one negation operation
137// after the binary operation instead of one on each of the inputs.
138bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
139 DCHECK(binop->IsAdd() || binop->IsSub());
140 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
141 HNeg* left_neg = binop->GetLeft()->AsNeg();
142 HNeg* right_neg = binop->GetRight()->AsNeg();
143 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
144 !right_neg->HasOnlyOneNonEnvironmentUse()) {
145 return false;
146 }
147 // Replace code looking like
148 // NEG tmp1, a
149 // NEG tmp2, b
150 // ADD dst, tmp1, tmp2
151 // with
152 // ADD tmp, a, b
153 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600154 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
155 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
156 // while the later yields `-0.0`.
157 if (!Primitive::IsIntegralType(binop->GetType())) {
158 return false;
159 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100160 binop->ReplaceInput(left_neg->GetInput(), 0);
161 binop->ReplaceInput(right_neg->GetInput(), 1);
162 left_neg->GetBlock()->RemoveInstruction(left_neg);
163 right_neg->GetBlock()->RemoveInstruction(right_neg);
164 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
165 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
166 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
167 RecordSimplification();
168 return true;
169}
170
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000171bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
172 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
173 Primitive::Type type = op->GetType();
174 HInstruction* left = op->GetLeft();
175 HInstruction* right = op->GetRight();
176
177 // We can apply De Morgan's laws if both inputs are Not's and are only used
178 // by `op`.
179 if (left->IsNot() &&
180 right->IsNot() &&
181 left->HasOnlyOneNonEnvironmentUse() &&
182 right->HasOnlyOneNonEnvironmentUse()) {
183 // Replace code looking like
184 // NOT nota, a
185 // NOT notb, b
186 // AND dst, nota, notb (respectively OR)
187 // with
188 // OR or, a, b (respectively AND)
189 // NOT dest, or
190 HInstruction* src_left = left->AsNot()->GetInput();
191 HInstruction* src_right = right->AsNot()->GetInput();
192 uint32_t dex_pc = op->GetDexPc();
193
194 // Remove the negations on the inputs.
195 left->ReplaceWith(src_left);
196 right->ReplaceWith(src_right);
197 left->GetBlock()->RemoveInstruction(left);
198 right->GetBlock()->RemoveInstruction(right);
199
200 // Replace the `HAnd` or `HOr`.
201 HBinaryOperation* hbin;
202 if (op->IsAnd()) {
203 hbin = new (GetGraph()->GetArena()) HOr(type, src_left, src_right, dex_pc);
204 } else {
205 hbin = new (GetGraph()->GetArena()) HAnd(type, src_left, src_right, dex_pc);
206 }
207 HNot* hnot = new (GetGraph()->GetArena()) HNot(type, hbin, dex_pc);
208
209 op->GetBlock()->InsertInstructionBefore(hbin, op);
210 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
211
212 RecordSimplification();
213 return true;
214 }
215
216 return false;
217}
218
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000219void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
220 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
221 HConstant* input_cst = instruction->GetConstantRight();
222 HInstruction* input_other = instruction->GetLeastConstantLeft();
223
Mark Mendellba56d062015-05-05 21:34:03 -0400224 if (input_cst != nullptr) {
225 if (input_cst->IsZero()) {
226 // Replace code looking like
227 // SHL dst, src, 0
228 // with
229 // src
230 instruction->ReplaceWith(input_other);
231 instruction->GetBlock()->RemoveInstruction(instruction);
Mark Mendellba56d062015-05-05 21:34:03 -0400232 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000233 }
234}
235
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000236static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
237 return (sub->GetRight() == other &&
238 sub->GetLeft()->IsConstant() &&
239 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
240}
241
242bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
243 HUShr* ushr,
244 HShl* shl) {
245 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
246 HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(),
247 ushr->GetLeft(),
248 ushr->GetRight());
249 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
250 if (!ushr->HasUses()) {
251 ushr->GetBlock()->RemoveInstruction(ushr);
252 }
253 if (!ushr->GetRight()->HasUses()) {
254 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
255 }
256 if (!shl->HasUses()) {
257 shl->GetBlock()->RemoveInstruction(shl);
258 }
259 if (!shl->GetRight()->HasUses()) {
260 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
261 }
262 return true;
263}
264
265// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
266bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000267 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
268 HInstruction* left = op->GetLeft();
269 HInstruction* right = op->GetRight();
270 // If we have an UShr and a Shl (in either order).
271 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
272 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
273 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
274 DCHECK(Primitive::IsIntOrLongType(ushr->GetType()));
275 if (ushr->GetType() == shl->GetType() &&
276 ushr->GetLeft() == shl->GetLeft()) {
277 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
278 // Shift distances are both constant, try replacing with Ror if they
279 // add up to the register size.
280 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
281 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
282 // Shift distances are potentially of the form x and (reg_size - x).
283 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
284 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
285 // Shift distances are potentially of the form d and -d.
286 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
287 }
288 }
289 }
290 return false;
291}
292
293// Try replacing code looking like (x >>> #rdist OP x << #ldist):
294// UShr dst, x, #rdist
295// Shl tmp, x, #ldist
296// OP dst, dst, tmp
297// or like (x >>> #rdist OP x << #-ldist):
298// UShr dst, x, #rdist
299// Shl tmp, x, #-ldist
300// OP dst, dst, tmp
301// with
302// Ror dst, x, #rdist
303bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
304 HUShr* ushr,
305 HShl* shl) {
306 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
307 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
308 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
309 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
310 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
311 ReplaceRotateWithRor(op, ushr, shl);
312 return true;
313 }
314 return false;
315}
316
317// Replace code looking like (x >>> -d OP x << d):
318// Neg neg, d
319// UShr dst, x, neg
320// Shl tmp, x, d
321// OP dst, dst, tmp
322// with
323// Neg neg, d
324// Ror dst, x, neg
325// *** OR ***
326// Replace code looking like (x >>> d OP x << -d):
327// UShr dst, x, d
328// Neg neg, d
329// Shl tmp, x, neg
330// OP dst, dst, tmp
331// with
332// Ror dst, x, d
333bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
334 HUShr* ushr,
335 HShl* shl) {
336 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
337 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
338 bool neg_is_left = shl->GetRight()->IsNeg();
339 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
340 // And the shift distance being negated is the distance being shifted the other way.
341 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
342 ReplaceRotateWithRor(op, ushr, shl);
343 }
344 return false;
345}
346
347// Try replacing code looking like (x >>> d OP x << (#bits - d)):
348// UShr dst, x, d
349// Sub ld, #bits, d
350// Shl tmp, x, ld
351// OP dst, dst, tmp
352// with
353// Ror dst, x, d
354// *** OR ***
355// Replace code looking like (x >>> (#bits - d) OP x << d):
356// Sub rd, #bits, d
357// UShr dst, x, rd
358// Shl tmp, x, d
359// OP dst, dst, tmp
360// with
361// Neg neg, d
362// Ror dst, x, neg
363bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
364 HUShr* ushr,
365 HShl* shl) {
366 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
367 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
368 size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte;
369 HInstruction* shl_shift = shl->GetRight();
370 HInstruction* ushr_shift = ushr->GetRight();
371 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
372 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
373 return ReplaceRotateWithRor(op, ushr, shl);
374 }
375 return false;
376}
377
Calin Juravle10e244f2015-01-26 18:54:32 +0000378void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
379 HInstruction* obj = null_check->InputAt(0);
380 if (!obj->CanBeNull()) {
381 null_check->ReplaceWith(obj);
382 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000383 if (stats_ != nullptr) {
384 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
385 }
386 }
387}
388
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100389bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
390 if (!input->CanBeNull()) {
391 return true;
392 }
393
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100394 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
395 HInstruction* use = it.Current()->GetUser();
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100396 if (use->IsNullCheck() && use->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100397 return true;
398 }
399 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100400
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100401 return false;
402}
403
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100404// Returns whether doing a type test between the class of `object` against `klass` has
405// a statically known outcome. The result of the test is stored in `outcome`.
406static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000407 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
408 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
409 ScopedObjectAccess soa(Thread::Current());
410 if (!obj_rti.IsValid()) {
411 // We run the simplifier before the reference type propagation so type info might not be
412 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100413 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000414 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100415
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100416 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100417 if (!class_rti.IsValid()) {
418 // Happens when the loaded class is unresolved.
419 return false;
420 }
421 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000422 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100423 *outcome = true;
424 return true;
425 } else if (obj_rti.IsExact()) {
426 // The test failed at compile time so will also fail at runtime.
427 *outcome = false;
428 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100429 } else if (!class_rti.IsInterface()
430 && !obj_rti.IsInterface()
431 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100432 // Different type hierarchy. The test will fail.
433 *outcome = false;
434 return true;
435 }
436 return false;
437}
438
439void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
440 HInstruction* object = check_cast->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100441 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
442 if (load_class->NeedsAccessCheck()) {
443 // If we need to perform an access check we cannot remove the instruction.
444 return;
445 }
446
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100447 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100448 check_cast->ClearMustDoNullCheck();
449 }
450
451 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000452 check_cast->GetBlock()->RemoveInstruction(check_cast);
453 if (stats_ != nullptr) {
454 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
455 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100456 return;
457 }
458
459 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700460 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100461 if (outcome) {
462 check_cast->GetBlock()->RemoveInstruction(check_cast);
463 if (stats_ != nullptr) {
464 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
465 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700466 if (!load_class->HasUses()) {
467 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
468 // However, here we know that it cannot because the checkcast was successfull, hence
469 // the class was already loaded.
470 load_class->GetBlock()->RemoveInstruction(load_class);
471 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100472 } else {
473 // Don't do anything for exceptional cases for now. Ideally we should remove
474 // all instructions and blocks this instruction dominates.
475 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000476 }
477}
478
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100479void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100480 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100481 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
482 if (load_class->NeedsAccessCheck()) {
483 // If we need to perform an access check we cannot remove the instruction.
484 return;
485 }
486
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100487 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100488 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100489 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100490 instruction->ClearMustDoNullCheck();
491 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100492
493 HGraph* graph = GetGraph();
494 if (object->IsNullConstant()) {
495 instruction->ReplaceWith(graph->GetIntConstant(0));
496 instruction->GetBlock()->RemoveInstruction(instruction);
497 RecordSimplification();
498 return;
499 }
500
501 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700502 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100503 if (outcome && can_be_null) {
504 // Type test will succeed, we just need a null test.
505 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
506 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
507 instruction->ReplaceWith(test);
508 } else {
509 // We've statically determined the result of the instanceof.
510 instruction->ReplaceWith(graph->GetIntConstant(outcome));
511 }
512 RecordSimplification();
513 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700514 if (outcome && !load_class->HasUses()) {
515 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
516 // However, here we know that it cannot because the instanceof check was successfull, hence
517 // the class was already loaded.
518 load_class->GetBlock()->RemoveInstruction(load_class);
519 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100520 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100521}
522
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100523void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
524 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100525 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100526 instruction->ClearValueCanBeNull();
527 }
528}
529
530void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
531 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100532 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100533 instruction->ClearValueCanBeNull();
534 }
535}
536
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000537void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100538 HBasicBlock* block = check->GetBlock();
539 // Currently always keep the suspend check at entry.
540 if (block->IsEntryBlock()) return;
541
542 // Currently always keep suspend checks at loop entry.
543 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
544 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
545 return;
546 }
547
548 // Remove the suspend check that was added at build time for the baseline
549 // compiler.
550 block->RemoveInstruction(check);
551}
552
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000553void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100554 HInstruction* input_const = equal->GetConstantRight();
555 if (input_const != nullptr) {
556 HInstruction* input_value = equal->GetLeastConstantLeft();
557 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
558 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100559 // We are comparing the boolean to a constant which is of type int and can
560 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100561 if (input_const->AsIntConstant()->IsOne()) {
562 // Replace (bool_value == true) with bool_value
563 equal->ReplaceWith(input_value);
564 block->RemoveInstruction(equal);
565 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100566 } else if (input_const->AsIntConstant()->IsZero()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500567 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
568 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100569 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100570 } else {
571 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
572 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
573 block->RemoveInstruction(equal);
574 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100575 }
Mark Mendellc4701932015-04-10 13:18:51 -0400576 } else {
577 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100578 }
Mark Mendellc4701932015-04-10 13:18:51 -0400579 } else {
580 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100581 }
582}
583
David Brazdil0d13fee2015-04-17 14:52:19 +0100584void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
585 HInstruction* input_const = not_equal->GetConstantRight();
586 if (input_const != nullptr) {
587 HInstruction* input_value = not_equal->GetLeastConstantLeft();
588 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
589 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100590 // We are comparing the boolean to a constant which is of type int and can
591 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100592 if (input_const->AsIntConstant()->IsOne()) {
Mark Mendellf6529172015-11-17 11:16:56 -0500593 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
594 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100595 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100596 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100597 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100598 not_equal->ReplaceWith(input_value);
599 block->RemoveInstruction(not_equal);
600 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100601 } else {
602 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
603 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
604 block->RemoveInstruction(not_equal);
605 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100606 }
Mark Mendellc4701932015-04-10 13:18:51 -0400607 } else {
608 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100609 }
Mark Mendellc4701932015-04-10 13:18:51 -0400610 } else {
611 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100612 }
613}
614
615void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000616 HInstruction* input = bool_not->InputAt(0);
617 HInstruction* replace_with = nullptr;
618
619 if (input->IsIntConstant()) {
620 // Replace !(true/false) with false/true.
621 if (input->AsIntConstant()->IsOne()) {
622 replace_with = GetGraph()->GetIntConstant(0);
623 } else {
624 DCHECK(input->AsIntConstant()->IsZero());
625 replace_with = GetGraph()->GetIntConstant(1);
626 }
627 } else if (input->IsBooleanNot()) {
628 // Replace (!(!bool_value)) with bool_value.
629 replace_with = input->InputAt(0);
630 } else if (input->IsCondition() &&
631 // Don't change FP compares. The definition of compares involving
632 // NaNs forces the compares to be done as written by the user.
633 !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) {
634 // Replace condition with its opposite.
635 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
636 }
637
638 if (replace_with != nullptr) {
639 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100640 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000641 RecordSimplification();
642 }
643}
644
645void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
646 HInstruction* replace_with = nullptr;
647 HInstruction* condition = select->GetCondition();
648 HInstruction* true_value = select->GetTrueValue();
649 HInstruction* false_value = select->GetFalseValue();
650
651 if (condition->IsBooleanNot()) {
652 // Change ((!cond) ? x : y) to (cond ? y : x).
653 condition = condition->InputAt(0);
654 std::swap(true_value, false_value);
655 select->ReplaceInput(false_value, 0);
656 select->ReplaceInput(true_value, 1);
657 select->ReplaceInput(condition, 2);
658 RecordSimplification();
659 }
660
661 if (true_value == false_value) {
662 // Replace (cond ? x : x) with (x).
663 replace_with = true_value;
664 } else if (condition->IsIntConstant()) {
665 if (condition->AsIntConstant()->IsOne()) {
666 // Replace (true ? x : y) with (x).
667 replace_with = true_value;
668 } else {
669 // Replace (false ? x : y) with (y).
670 DCHECK(condition->AsIntConstant()->IsZero());
671 replace_with = false_value;
672 }
673 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
674 if (true_value->AsIntConstant()->IsOne() && false_value->AsIntConstant()->IsZero()) {
675 // Replace (cond ? true : false) with (cond).
676 replace_with = condition;
677 } else if (true_value->AsIntConstant()->IsZero() && false_value->AsIntConstant()->IsOne()) {
678 // Replace (cond ? false : true) with (!cond).
679 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
680 }
681 }
682
683 if (replace_with != nullptr) {
684 select->ReplaceWith(replace_with);
685 select->GetBlock()->RemoveInstruction(select);
686 RecordSimplification();
687 }
688}
689
690void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
691 HInstruction* condition = instruction->InputAt(0);
692 if (condition->IsBooleanNot()) {
693 // Swap successors if input is negated.
694 instruction->ReplaceInput(condition->InputAt(0), 0);
695 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +0100696 RecordSimplification();
697 }
698}
699
Mingyao Yang0304e182015-01-30 16:41:29 -0800700void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
701 HInstruction* input = instruction->InputAt(0);
702 // If the array is a NewArray with constant size, replace the array length
703 // with the constant instruction. This helps the bounds check elimination phase.
704 if (input->IsNewArray()) {
705 input = input->InputAt(0);
706 if (input->IsIntConstant()) {
707 instruction->ReplaceWith(input);
708 }
709 }
710}
711
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000712void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000713 HInstruction* value = instruction->GetValue();
714 if (value->GetType() != Primitive::kPrimNot) return;
715
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100716 if (CanEnsureNotNullAt(value, instruction)) {
717 instruction->ClearValueCanBeNull();
718 }
719
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000720 if (value->IsArrayGet()) {
721 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
722 // If the code is just swapping elements in the array, no need for a type check.
723 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100724 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000725 }
726 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100727
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100728 if (value->IsNullConstant()) {
729 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100730 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100731 }
732
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100733 ScopedObjectAccess soa(Thread::Current());
734 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
735 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
736 if (!array_rti.IsValid()) {
737 return;
738 }
739
740 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
741 instruction->ClearNeedsTypeCheck();
742 return;
743 }
744
745 if (array_rti.IsObjectArray()) {
746 if (array_rti.IsExact()) {
747 instruction->ClearNeedsTypeCheck();
748 return;
749 }
750 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100751 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000752}
753
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000754void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
755 if (instruction->GetResultType() == instruction->GetInputType()) {
756 // Remove the instruction if it's converting to the same type.
757 instruction->ReplaceWith(instruction->GetInput());
758 instruction->GetBlock()->RemoveInstruction(instruction);
759 }
760}
761
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000762void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
763 HConstant* input_cst = instruction->GetConstantRight();
764 HInstruction* input_other = instruction->GetLeastConstantLeft();
765 if ((input_cst != nullptr) && input_cst->IsZero()) {
766 // Replace code looking like
767 // ADD dst, src, 0
768 // with
769 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600770 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
771 // `x` is `-0.0`, the former expression yields `0.0`, while the later
772 // yields `-0.0`.
773 if (Primitive::IsIntegralType(instruction->GetType())) {
774 instruction->ReplaceWith(input_other);
775 instruction->GetBlock()->RemoveInstruction(instruction);
776 return;
777 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100778 }
779
780 HInstruction* left = instruction->GetLeft();
781 HInstruction* right = instruction->GetRight();
782 bool left_is_neg = left->IsNeg();
783 bool right_is_neg = right->IsNeg();
784
785 if (left_is_neg && right_is_neg) {
786 if (TryMoveNegOnInputsAfterBinop(instruction)) {
787 return;
788 }
789 }
790
791 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
792 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
793 // Replace code looking like
794 // NEG tmp, b
795 // ADD dst, a, tmp
796 // with
797 // SUB dst, a, b
798 // We do not perform the optimization if the input negation has environment
799 // uses or multiple non-environment uses as it could lead to worse code. In
800 // particular, we do not want the live range of `b` to be extended if we are
801 // not sure the initial 'NEG' instruction can be removed.
802 HInstruction* other = left_is_neg ? right : left;
803 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
804 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
805 RecordSimplification();
806 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000807 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000808 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000809
810 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000811}
812
813void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
814 HConstant* input_cst = instruction->GetConstantRight();
815 HInstruction* input_other = instruction->GetLeastConstantLeft();
816
Vladimir Marko452c1b62015-09-25 14:44:17 +0100817 if (input_cst != nullptr) {
818 int64_t value = Int64FromConstant(input_cst);
819 if (value == -1) {
820 // Replace code looking like
821 // AND dst, src, 0xFFF...FF
822 // with
823 // src
824 instruction->ReplaceWith(input_other);
825 instruction->GetBlock()->RemoveInstruction(instruction);
826 RecordSimplification();
827 return;
828 }
829 // Eliminate And from UShr+And if the And-mask contains all the bits that
830 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
831 // precisely clears the shifted-in sign bits.
832 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
833 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
834 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
835 size_t num_tail_bits_set = CTZ(value + 1);
836 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
837 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
838 instruction->ReplaceWith(input_other);
839 instruction->GetBlock()->RemoveInstruction(instruction);
840 RecordSimplification();
841 return;
842 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
843 input_other->HasOnlyOneNonEnvironmentUse()) {
844 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
845 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
846 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
847 input_other->InputAt(0),
848 input_other->InputAt(1),
849 input_other->GetDexPc());
850 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
851 input_other->GetBlock()->RemoveInstruction(input_other);
852 RecordSimplification();
853 return;
854 }
855 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000856 }
857
858 // We assume that GVN has run before, so we only perform a pointer comparison.
859 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100860 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000861 if (instruction->GetLeft() == instruction->GetRight()) {
862 // Replace code looking like
863 // AND dst, src, src
864 // with
865 // src
866 instruction->ReplaceWith(instruction->GetLeft());
867 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000868 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000869 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000870
871 TryDeMorganNegationFactoring(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000872}
873
Mark Mendellc4701932015-04-10 13:18:51 -0400874void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
875 VisitCondition(condition);
876}
877
878void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
879 VisitCondition(condition);
880}
881
882void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
883 VisitCondition(condition);
884}
885
886void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
887 VisitCondition(condition);
888}
889
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +0000890// TODO: unsigned comparisons too?
Aart Bike9f37602015-10-09 11:15:55 -0700891
Mark Mendellc4701932015-04-10 13:18:51 -0400892void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +0000893 // Try to fold an HCompare into this HCondition.
Mark Mendellc4701932015-04-10 13:18:51 -0400894
Mark Mendellc4701932015-04-10 13:18:51 -0400895 HInstruction* left = condition->GetLeft();
896 HInstruction* right = condition->GetRight();
897 // We can only replace an HCondition which compares a Compare to 0.
898 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
899 // condition with a long, float or double comparison as input.
900 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
901 // Conversion is not possible.
902 return;
903 }
904
905 // Is the Compare only used for this purpose?
906 if (!left->GetUses().HasOnlyOneUse()) {
907 // Someone else also wants the result of the compare.
908 return;
909 }
910
911 if (!left->GetEnvUses().IsEmpty()) {
912 // There is a reference to the compare result in an environment. Do we really need it?
913 if (GetGraph()->IsDebuggable()) {
914 return;
915 }
916
917 // We have to ensure that there are no deopt points in the sequence.
918 if (left->HasAnyEnvironmentUseBefore(condition)) {
919 return;
920 }
921 }
922
923 // Clean up any environment uses from the HCompare, if any.
924 left->RemoveEnvironmentUsers();
925
926 // We have decided to fold the HCompare into the HCondition. Transfer the information.
927 condition->SetBias(left->AsCompare()->GetBias());
928
929 // Replace the operands of the HCondition.
930 condition->ReplaceInput(left->InputAt(0), 0);
931 condition->ReplaceInput(left->InputAt(1), 1);
932
933 // Remove the HCompare.
934 left->GetBlock()->RemoveInstruction(left);
935
936 RecordSimplification();
937}
938
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000939void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
940 HConstant* input_cst = instruction->GetConstantRight();
941 HInstruction* input_other = instruction->GetLeastConstantLeft();
942 Primitive::Type type = instruction->GetType();
943
944 if ((input_cst != nullptr) && input_cst->IsOne()) {
945 // Replace code looking like
946 // DIV dst, src, 1
947 // with
948 // src
949 instruction->ReplaceWith(input_other);
950 instruction->GetBlock()->RemoveInstruction(instruction);
951 return;
952 }
953
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000954 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000955 // Replace code looking like
956 // DIV dst, src, -1
957 // with
958 // NEG dst, src
959 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000960 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100961 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000962 return;
963 }
964
965 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
966 // Try replacing code looking like
967 // DIV dst, src, constant
968 // with
969 // MUL dst, src, 1 / constant
970 HConstant* reciprocal = nullptr;
971 if (type == Primitive::Primitive::kPrimDouble) {
972 double value = input_cst->AsDoubleConstant()->GetValue();
973 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
974 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
975 }
976 } else {
977 DCHECK_EQ(type, Primitive::kPrimFloat);
978 float value = input_cst->AsFloatConstant()->GetValue();
979 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
980 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
981 }
982 }
983
984 if (reciprocal != nullptr) {
985 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
986 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
987 RecordSimplification();
988 return;
989 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000990 }
991}
992
993void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
994 HConstant* input_cst = instruction->GetConstantRight();
995 HInstruction* input_other = instruction->GetLeastConstantLeft();
996 Primitive::Type type = instruction->GetType();
997 HBasicBlock* block = instruction->GetBlock();
998 ArenaAllocator* allocator = GetGraph()->GetArena();
999
1000 if (input_cst == nullptr) {
1001 return;
1002 }
1003
1004 if (input_cst->IsOne()) {
1005 // Replace code looking like
1006 // MUL dst, src, 1
1007 // with
1008 // src
1009 instruction->ReplaceWith(input_other);
1010 instruction->GetBlock()->RemoveInstruction(instruction);
1011 return;
1012 }
1013
1014 if (input_cst->IsMinusOne() &&
1015 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
1016 // Replace code looking like
1017 // MUL dst, src, -1
1018 // with
1019 // NEG dst, src
1020 HNeg* neg = new (allocator) HNeg(type, input_other);
1021 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001022 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001023 return;
1024 }
1025
1026 if (Primitive::IsFloatingPointType(type) &&
1027 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1028 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1029 // Replace code looking like
1030 // FP_MUL dst, src, 2.0
1031 // with
1032 // FP_ADD dst, src, src
1033 // The 'int' and 'long' cases are handled below.
1034 block->ReplaceAndRemoveInstructionWith(instruction,
1035 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001036 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001037 return;
1038 }
1039
1040 if (Primitive::IsIntOrLongType(type)) {
1041 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001042 // Even though constant propagation also takes care of the zero case, other
1043 // optimizations can lead to having a zero multiplication.
1044 if (factor == 0) {
1045 // Replace code looking like
1046 // MUL dst, src, 0
1047 // with
1048 // 0
1049 instruction->ReplaceWith(input_cst);
1050 instruction->GetBlock()->RemoveInstruction(instruction);
1051 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001052 // Replace code looking like
1053 // MUL dst, src, pow_of_2
1054 // with
1055 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001056 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001057 HShl* shl = new(allocator) HShl(type, input_other, shift);
1058 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001059 RecordSimplification();
Alexandre Rames38db7852015-11-20 15:02:45 +00001060 } else if (IsPowerOfTwo(factor - 1)) {
1061 // Transform code looking like
1062 // MUL dst, src, (2^n + 1)
1063 // into
1064 // SHL tmp, src, n
1065 // ADD dst, src, tmp
1066 HShl* shl = new (allocator) HShl(type,
1067 input_other,
1068 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1069 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1070
1071 block->InsertInstructionBefore(shl, instruction);
1072 block->ReplaceAndRemoveInstructionWith(instruction, add);
1073 RecordSimplification();
1074 } else if (IsPowerOfTwo(factor + 1)) {
1075 // Transform code looking like
1076 // MUL dst, src, (2^n - 1)
1077 // into
1078 // SHL tmp, src, n
1079 // SUB dst, tmp, src
1080 HShl* shl = new (allocator) HShl(type,
1081 input_other,
1082 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1083 HSub* sub = new (allocator) HSub(type, shl, input_other);
1084
1085 block->InsertInstructionBefore(shl, instruction);
1086 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1087 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001088 }
1089 }
1090}
1091
Alexandre Rames188d4312015-04-09 18:30:21 +01001092void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1093 HInstruction* input = instruction->GetInput();
1094 if (input->IsNeg()) {
1095 // Replace code looking like
1096 // NEG tmp, src
1097 // NEG dst, tmp
1098 // with
1099 // src
1100 HNeg* previous_neg = input->AsNeg();
1101 instruction->ReplaceWith(previous_neg->GetInput());
1102 instruction->GetBlock()->RemoveInstruction(instruction);
1103 // We perform the optimization even if the input negation has environment
1104 // uses since it allows removing the current instruction. But we only delete
1105 // the input negation only if it is does not have any uses left.
1106 if (!previous_neg->HasUses()) {
1107 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1108 }
1109 RecordSimplification();
1110 return;
1111 }
1112
Serguei Katkov339dfc22015-04-20 12:29:32 +06001113 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
1114 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001115 // Replace code looking like
1116 // SUB tmp, a, b
1117 // NEG dst, tmp
1118 // with
1119 // SUB dst, b, a
1120 // We do not perform the optimization if the input subtraction has
1121 // environment uses or multiple non-environment uses as it could lead to
1122 // worse code. In particular, we do not want the live ranges of `a` and `b`
1123 // to be extended if we are not sure the initial 'SUB' instruction can be
1124 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001125 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001126 HSub* sub = input->AsSub();
1127 HSub* new_sub =
1128 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
1129 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1130 if (!sub->HasUses()) {
1131 sub->GetBlock()->RemoveInstruction(sub);
1132 }
1133 RecordSimplification();
1134 }
1135}
1136
1137void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1138 HInstruction* input = instruction->GetInput();
1139 if (input->IsNot()) {
1140 // Replace code looking like
1141 // NOT tmp, src
1142 // NOT dst, tmp
1143 // with
1144 // src
1145 // We perform the optimization even if the input negation has environment
1146 // uses since it allows removing the current instruction. But we only delete
1147 // the input negation only if it is does not have any uses left.
1148 HNot* previous_not = input->AsNot();
1149 instruction->ReplaceWith(previous_not->GetInput());
1150 instruction->GetBlock()->RemoveInstruction(instruction);
1151 if (!previous_not->HasUses()) {
1152 previous_not->GetBlock()->RemoveInstruction(previous_not);
1153 }
1154 RecordSimplification();
1155 }
1156}
1157
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001158void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1159 HConstant* input_cst = instruction->GetConstantRight();
1160 HInstruction* input_other = instruction->GetLeastConstantLeft();
1161
1162 if ((input_cst != nullptr) && input_cst->IsZero()) {
1163 // Replace code looking like
1164 // OR dst, src, 0
1165 // with
1166 // src
1167 instruction->ReplaceWith(input_other);
1168 instruction->GetBlock()->RemoveInstruction(instruction);
1169 return;
1170 }
1171
1172 // We assume that GVN has run before, so we only perform a pointer comparison.
1173 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001174 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001175 if (instruction->GetLeft() == instruction->GetRight()) {
1176 // Replace code looking like
1177 // OR dst, src, src
1178 // with
1179 // src
1180 instruction->ReplaceWith(instruction->GetLeft());
1181 instruction->GetBlock()->RemoveInstruction(instruction);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001182 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001183 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001184
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001185 if (TryDeMorganNegationFactoring(instruction)) return;
1186
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001187 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001188}
1189
1190void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1191 VisitShift(instruction);
1192}
1193
1194void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1195 VisitShift(instruction);
1196}
1197
1198void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1199 HConstant* input_cst = instruction->GetConstantRight();
1200 HInstruction* input_other = instruction->GetLeastConstantLeft();
1201
Serguei Katkov115b53f2015-08-05 17:03:30 +06001202 Primitive::Type type = instruction->GetType();
1203 if (Primitive::IsFloatingPointType(type)) {
1204 return;
1205 }
1206
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001207 if ((input_cst != nullptr) && input_cst->IsZero()) {
1208 // Replace code looking like
1209 // SUB dst, src, 0
1210 // with
1211 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001212 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1213 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1214 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001215 instruction->ReplaceWith(input_other);
1216 instruction->GetBlock()->RemoveInstruction(instruction);
1217 return;
1218 }
1219
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001220 HBasicBlock* block = instruction->GetBlock();
1221 ArenaAllocator* allocator = GetGraph()->GetArena();
1222
Alexandre Rames188d4312015-04-09 18:30:21 +01001223 HInstruction* left = instruction->GetLeft();
1224 HInstruction* right = instruction->GetRight();
1225 if (left->IsConstant()) {
1226 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001227 // Replace code looking like
1228 // SUB dst, 0, src
1229 // with
1230 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001231 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001232 // `x` is `0.0`, the former expression yields `0.0`, while the later
1233 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001234 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001235 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001236 RecordSimplification();
1237 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001238 }
1239 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001240
1241 if (left->IsNeg() && right->IsNeg()) {
1242 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1243 return;
1244 }
1245 }
1246
1247 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1248 // Replace code looking like
1249 // NEG tmp, b
1250 // SUB dst, a, tmp
1251 // with
1252 // ADD dst, a, b
1253 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
1254 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
1255 RecordSimplification();
1256 right->GetBlock()->RemoveInstruction(right);
1257 return;
1258 }
1259
1260 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
1261 // Replace code looking like
1262 // NEG tmp, a
1263 // SUB dst, tmp, b
1264 // with
1265 // ADD tmp, a, b
1266 // NEG dst, tmp
1267 // The second version is not intrinsically better, but enables more
1268 // transformations.
1269 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
1270 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
1271 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
1272 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
1273 instruction->ReplaceWith(neg);
1274 instruction->GetBlock()->RemoveInstruction(instruction);
1275 RecordSimplification();
1276 left->GetBlock()->RemoveInstruction(left);
1277 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001278}
1279
1280void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
1281 VisitShift(instruction);
1282}
1283
1284void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
1285 HConstant* input_cst = instruction->GetConstantRight();
1286 HInstruction* input_other = instruction->GetLeastConstantLeft();
1287
1288 if ((input_cst != nullptr) && input_cst->IsZero()) {
1289 // Replace code looking like
1290 // XOR dst, src, 0
1291 // with
1292 // src
1293 instruction->ReplaceWith(input_other);
1294 instruction->GetBlock()->RemoveInstruction(instruction);
1295 return;
1296 }
1297
1298 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
1299 // Replace code looking like
1300 // XOR dst, src, 0xFFF...FF
1301 // with
1302 // NOT dst, src
1303 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
1304 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01001305 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001306 return;
1307 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001308
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001309 HInstruction* left = instruction->GetLeft();
1310 HInstruction* right = instruction->GetRight();
1311 if (left->IsNot() &&
1312 right->IsNot() &&
1313 left->HasOnlyOneNonEnvironmentUse() &&
1314 right->HasOnlyOneNonEnvironmentUse()) {
1315 // Replace code looking like
1316 // NOT nota, a
1317 // NOT notb, b
1318 // XOR dst, nota, notb
1319 // with
1320 // XOR dst, a, b
1321 instruction->ReplaceInput(left->AsNot()->GetInput(), 0);
1322 instruction->ReplaceInput(right->AsNot()->GetInput(), 1);
1323 left->GetBlock()->RemoveInstruction(left);
1324 right->GetBlock()->RemoveInstruction(right);
1325 RecordSimplification();
1326 return;
1327 }
1328
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001329 TryReplaceWithRotate(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001330}
1331
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001332void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
1333 HInstruction* argument = instruction->InputAt(1);
1334 HInstruction* receiver = instruction->InputAt(0);
1335 if (receiver == argument) {
1336 // Because String.equals is an instance call, the receiver is
1337 // a null check if we don't know it's null. The argument however, will
1338 // be the actual object. So we cannot end up in a situation where both
1339 // are equal but could be null.
1340 DCHECK(CanEnsureNotNullAt(argument, instruction));
1341 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
1342 instruction->GetBlock()->RemoveInstruction(instruction);
1343 } else {
1344 StringEqualsOptimizations optimizations(instruction);
1345 if (CanEnsureNotNullAt(argument, instruction)) {
1346 optimizations.SetArgumentNotNull();
1347 }
1348 ScopedObjectAccess soa(Thread::Current());
1349 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
1350 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
1351 optimizations.SetArgumentIsString();
1352 }
1353 }
1354}
1355
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001356void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke, bool is_left) {
1357 DCHECK(invoke->IsInvokeStaticOrDirect());
1358 DCHECK_EQ(invoke->GetOriginalInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001359 HInstruction* value = invoke->InputAt(0);
1360 HInstruction* distance = invoke->InputAt(1);
1361 // Replace the invoke with an HRor.
1362 if (is_left) {
1363 distance = new (GetGraph()->GetArena()) HNeg(distance->GetType(), distance);
1364 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
1365 }
1366 HRor* ror = new (GetGraph()->GetArena()) HRor(value->GetType(), value, distance);
1367 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
1368 // Remove ClinitCheck and LoadClass, if possible.
1369 HInstruction* clinit = invoke->InputAt(invoke->InputCount() - 1);
1370 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
1371 clinit->GetBlock()->RemoveInstruction(clinit);
1372 HInstruction* ldclass = clinit->InputAt(0);
1373 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
1374 ldclass->GetBlock()->RemoveInstruction(ldclass);
1375 }
1376 }
1377}
1378
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001379static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
1380 if (potential_length->IsArrayLength()) {
1381 return potential_length->InputAt(0) == potential_array;
1382 }
1383
1384 if (potential_array->IsNewArray()) {
1385 return potential_array->InputAt(0) == potential_length;
1386 }
1387
1388 return false;
1389}
1390
1391void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
1392 HInstruction* source = instruction->InputAt(0);
1393 HInstruction* destination = instruction->InputAt(2);
1394 HInstruction* count = instruction->InputAt(4);
1395 SystemArrayCopyOptimizations optimizations(instruction);
1396 if (CanEnsureNotNullAt(source, instruction)) {
1397 optimizations.SetSourceIsNotNull();
1398 }
1399 if (CanEnsureNotNullAt(destination, instruction)) {
1400 optimizations.SetDestinationIsNotNull();
1401 }
1402 if (destination == source) {
1403 optimizations.SetDestinationIsSource();
1404 }
1405
1406 if (IsArrayLengthOf(count, source)) {
1407 optimizations.SetCountIsSourceLength();
1408 }
1409
1410 if (IsArrayLengthOf(count, destination)) {
1411 optimizations.SetCountIsDestinationLength();
1412 }
1413
1414 {
1415 ScopedObjectAccess soa(Thread::Current());
1416 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
1417 if (destination_rti.IsValid()) {
1418 if (destination_rti.IsObjectArray()) {
1419 if (destination_rti.IsExact()) {
1420 optimizations.SetDoesNotNeedTypeCheck();
1421 }
1422 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001423 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001424 if (destination_rti.IsPrimitiveArrayClass()) {
1425 optimizations.SetDestinationIsPrimitiveArray();
1426 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
1427 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001428 }
1429 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001430 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
1431 if (source_rti.IsValid()) {
1432 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
1433 optimizations.SetDoesNotNeedTypeCheck();
1434 }
1435 if (source_rti.IsPrimitiveArrayClass()) {
1436 optimizations.SetSourceIsPrimitiveArray();
1437 } else if (source_rti.IsNonPrimitiveArrayClass()) {
1438 optimizations.SetSourceIsNonPrimitiveArray();
1439 }
1440 }
1441 }
1442}
1443
1444void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
1445 if (instruction->GetIntrinsic() == Intrinsics::kStringEquals) {
1446 SimplifyStringEquals(instruction);
1447 } else if (instruction->GetIntrinsic() == Intrinsics::kSystemArrayCopy) {
1448 SimplifySystemArrayCopy(instruction);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001449 } else if (instruction->GetIntrinsic() == Intrinsics::kIntegerRotateRight ||
1450 instruction->GetIntrinsic() == Intrinsics::kLongRotateRight) {
1451 SimplifyRotate(instruction, false);
1452 } else if (instruction->GetIntrinsic() == Intrinsics::kIntegerRotateLeft ||
1453 instruction->GetIntrinsic() == Intrinsics::kLongRotateLeft) {
1454 SimplifyRotate(instruction, true);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001455 }
1456}
1457
Aart Bikbb245d12015-10-19 11:05:03 -07001458void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
1459 HInstruction* cond = deoptimize->InputAt(0);
1460 if (cond->IsConstant()) {
1461 if (cond->AsIntConstant()->IsZero()) {
1462 // Never deopt: instruction can be removed.
1463 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
1464 } else {
1465 // Always deopt.
1466 }
1467 }
1468}
1469
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001470} // namespace art