blob: afbc490150f877dbd3fe355256451fe1b10cc06d [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
Calin Juravleacf735c2015-02-12 15:25:22 +000019#include "mirror/class-inl.h"
20#include "scoped_thread_state_change.h"
21
Nicolas Geoffray3c049742014-09-24 18:10:46 +010022namespace art {
23
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000024class InstructionSimplifierVisitor : public HGraphVisitor {
25 public:
Calin Juravleacf735c2015-02-12 15:25:22 +000026 InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
Alexandre Rames188d4312015-04-09 18:30:21 +010027 : HGraphVisitor(graph),
28 stats_(stats) {}
29
30 void Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000031
32 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010033 void RecordSimplification() {
34 simplification_occurred_ = true;
35 simplifications_at_current_position_++;
36 if (stats_) {
37 stats_->RecordStat(kInstructionSimplifications);
38 }
39 }
40
41 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000042 void VisitShift(HBinaryOperation* shift);
43
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000044 void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
45 void VisitEqual(HEqual* equal) OVERRIDE;
46 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000047 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000048 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080049 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000050 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000051 void VisitAdd(HAdd* instruction) OVERRIDE;
52 void VisitAnd(HAnd* instruction) OVERRIDE;
53 void VisitDiv(HDiv* instruction) OVERRIDE;
54 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010055 void VisitNeg(HNeg* instruction) OVERRIDE;
56 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000057 void VisitOr(HOr* instruction) OVERRIDE;
58 void VisitShl(HShl* instruction) OVERRIDE;
59 void VisitShr(HShr* instruction) OVERRIDE;
60 void VisitSub(HSub* instruction) OVERRIDE;
61 void VisitUShr(HUShr* instruction) OVERRIDE;
62 void VisitXor(HXor* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000063
64 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010065 bool simplification_occurred_ = false;
66 int simplifications_at_current_position_ = 0;
67 // We ensure we do not loop infinitely. The value is a finger in the air guess
68 // that should allow enough simplification.
69 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000070};
71
Nicolas Geoffray3c049742014-09-24 18:10:46 +010072void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000073 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010074 visitor.Run();
75}
76
77void InstructionSimplifierVisitor::Run() {
78 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
79 // The simplification of an instruction to another instruction may yield
80 // possibilities for other simplifications. So although we perform a reverse
81 // post order visit, we sometimes need to revisit an instruction index.
82 simplification_occurred_ = false;
83 VisitBasicBlock(it.Current());
84 if (simplification_occurred_ &&
85 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
86 // New simplifications may be applicable to the instruction at the
87 // current index, so don't advance the iterator.
88 continue;
89 }
90 if (simplifications_at_current_position_ >= kMaxSamePositionSimplifications) {
91 LOG(WARNING) << "Too many simplifications (" << simplifications_at_current_position_
92 << ") occurred at the current position.";
93 }
94 simplifications_at_current_position_ = 0;
95 it.Advance();
96 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +010097}
98
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000099namespace {
100
101bool AreAllBitsSet(HConstant* constant) {
102 return Int64FromConstant(constant) == -1;
103}
104
105} // namespace
106
Alexandre Rames188d4312015-04-09 18:30:21 +0100107// Returns true if the code was simplified to use only one negation operation
108// after the binary operation instead of one on each of the inputs.
109bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
110 DCHECK(binop->IsAdd() || binop->IsSub());
111 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
112 HNeg* left_neg = binop->GetLeft()->AsNeg();
113 HNeg* right_neg = binop->GetRight()->AsNeg();
114 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
115 !right_neg->HasOnlyOneNonEnvironmentUse()) {
116 return false;
117 }
118 // Replace code looking like
119 // NEG tmp1, a
120 // NEG tmp2, b
121 // ADD dst, tmp1, tmp2
122 // with
123 // ADD tmp, a, b
124 // NEG dst, tmp
125 binop->ReplaceInput(left_neg->GetInput(), 0);
126 binop->ReplaceInput(right_neg->GetInput(), 1);
127 left_neg->GetBlock()->RemoveInstruction(left_neg);
128 right_neg->GetBlock()->RemoveInstruction(right_neg);
129 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
130 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
131 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
132 RecordSimplification();
133 return true;
134}
135
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000136void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
137 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
138 HConstant* input_cst = instruction->GetConstantRight();
139 HInstruction* input_other = instruction->GetLeastConstantLeft();
140
141 if ((input_cst != nullptr) && input_cst->IsZero()) {
142 // Replace code looking like
143 // SHL dst, src, 0
144 // with
145 // src
146 instruction->ReplaceWith(input_other);
147 instruction->GetBlock()->RemoveInstruction(instruction);
148 }
149}
150
Calin Juravle10e244f2015-01-26 18:54:32 +0000151void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
152 HInstruction* obj = null_check->InputAt(0);
153 if (!obj->CanBeNull()) {
154 null_check->ReplaceWith(obj);
155 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000156 if (stats_ != nullptr) {
157 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
158 }
159 }
160}
161
162void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
163 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
164 if (!load_class->IsResolved()) {
165 // If the class couldn't be resolve it's not safe to compare against it. It's
166 // default type would be Top which might be wider that the actual class type
167 // and thus producing wrong results.
168 return;
169 }
170 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
171 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
172 ScopedObjectAccess soa(Thread::Current());
173 if (class_rti.IsSupertypeOf(obj_rti)) {
174 check_cast->GetBlock()->RemoveInstruction(check_cast);
175 if (stats_ != nullptr) {
176 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
177 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000178 }
179}
180
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000181void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100182 HBasicBlock* block = check->GetBlock();
183 // Currently always keep the suspend check at entry.
184 if (block->IsEntryBlock()) return;
185
186 // Currently always keep suspend checks at loop entry.
187 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
188 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
189 return;
190 }
191
192 // Remove the suspend check that was added at build time for the baseline
193 // compiler.
194 block->RemoveInstruction(check);
195}
196
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000197void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100198 HInstruction* input1 = equal->InputAt(0);
199 HInstruction* input2 = equal->InputAt(1);
200 if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) {
201 if (input2->AsIntConstant()->GetValue() == 1) {
202 // Replace (bool_value == 1) with bool_value
203 equal->ReplaceWith(equal->InputAt(0));
204 equal->GetBlock()->RemoveInstruction(equal);
205 } else {
Nicolas Geoffrayfa93b502015-01-21 15:44:16 +0000206 // We should replace (bool_value == 0) with !bool_value, but we unfortunately
207 // do not have such instruction.
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100208 DCHECK_EQ(input2->AsIntConstant()->GetValue(), 0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100209 }
210 }
211}
212
Mingyao Yang0304e182015-01-30 16:41:29 -0800213void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
214 HInstruction* input = instruction->InputAt(0);
215 // If the array is a NewArray with constant size, replace the array length
216 // with the constant instruction. This helps the bounds check elimination phase.
217 if (input->IsNewArray()) {
218 input = input->InputAt(0);
219 if (input->IsIntConstant()) {
220 instruction->ReplaceWith(input);
221 }
222 }
223}
224
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000225void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000226 HInstruction* value = instruction->GetValue();
227 if (value->GetType() != Primitive::kPrimNot) return;
228
229 if (value->IsArrayGet()) {
230 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
231 // If the code is just swapping elements in the array, no need for a type check.
232 instruction->ClearNeedsTypeCheck();
233 }
234 }
235}
236
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000237void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
238 if (instruction->GetResultType() == instruction->GetInputType()) {
239 // Remove the instruction if it's converting to the same type.
240 instruction->ReplaceWith(instruction->GetInput());
241 instruction->GetBlock()->RemoveInstruction(instruction);
242 }
243}
244
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000245void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
246 HConstant* input_cst = instruction->GetConstantRight();
247 HInstruction* input_other = instruction->GetLeastConstantLeft();
248 if ((input_cst != nullptr) && input_cst->IsZero()) {
249 // Replace code looking like
250 // ADD dst, src, 0
251 // with
252 // src
253 instruction->ReplaceWith(input_other);
254 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Rames188d4312015-04-09 18:30:21 +0100255 return;
256 }
257
258 HInstruction* left = instruction->GetLeft();
259 HInstruction* right = instruction->GetRight();
260 bool left_is_neg = left->IsNeg();
261 bool right_is_neg = right->IsNeg();
262
263 if (left_is_neg && right_is_neg) {
264 if (TryMoveNegOnInputsAfterBinop(instruction)) {
265 return;
266 }
267 }
268
269 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
270 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
271 // Replace code looking like
272 // NEG tmp, b
273 // ADD dst, a, tmp
274 // with
275 // SUB dst, a, b
276 // We do not perform the optimization if the input negation has environment
277 // uses or multiple non-environment uses as it could lead to worse code. In
278 // particular, we do not want the live range of `b` to be extended if we are
279 // not sure the initial 'NEG' instruction can be removed.
280 HInstruction* other = left_is_neg ? right : left;
281 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
282 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
283 RecordSimplification();
284 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000285 }
286}
287
288void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
289 HConstant* input_cst = instruction->GetConstantRight();
290 HInstruction* input_other = instruction->GetLeastConstantLeft();
291
292 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
293 // Replace code looking like
294 // AND dst, src, 0xFFF...FF
295 // with
296 // src
297 instruction->ReplaceWith(input_other);
298 instruction->GetBlock()->RemoveInstruction(instruction);
299 return;
300 }
301
302 // We assume that GVN has run before, so we only perform a pointer comparison.
303 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100304 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000305 if (instruction->GetLeft() == instruction->GetRight()) {
306 // Replace code looking like
307 // AND dst, src, src
308 // with
309 // src
310 instruction->ReplaceWith(instruction->GetLeft());
311 instruction->GetBlock()->RemoveInstruction(instruction);
312 }
313}
314
315void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
316 HConstant* input_cst = instruction->GetConstantRight();
317 HInstruction* input_other = instruction->GetLeastConstantLeft();
318 Primitive::Type type = instruction->GetType();
319
320 if ((input_cst != nullptr) && input_cst->IsOne()) {
321 // Replace code looking like
322 // DIV dst, src, 1
323 // with
324 // src
325 instruction->ReplaceWith(input_other);
326 instruction->GetBlock()->RemoveInstruction(instruction);
327 return;
328 }
329
330 if ((input_cst != nullptr) && input_cst->IsMinusOne() &&
331 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
332 // Replace code looking like
333 // DIV dst, src, -1
334 // with
335 // NEG dst, src
336 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
337 instruction, (new (GetGraph()->GetArena()) HNeg(type, input_other)));
Alexandre Rames188d4312015-04-09 18:30:21 +0100338 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000339 }
340}
341
342void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
343 HConstant* input_cst = instruction->GetConstantRight();
344 HInstruction* input_other = instruction->GetLeastConstantLeft();
345 Primitive::Type type = instruction->GetType();
346 HBasicBlock* block = instruction->GetBlock();
347 ArenaAllocator* allocator = GetGraph()->GetArena();
348
349 if (input_cst == nullptr) {
350 return;
351 }
352
353 if (input_cst->IsOne()) {
354 // Replace code looking like
355 // MUL dst, src, 1
356 // with
357 // src
358 instruction->ReplaceWith(input_other);
359 instruction->GetBlock()->RemoveInstruction(instruction);
360 return;
361 }
362
363 if (input_cst->IsMinusOne() &&
364 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
365 // Replace code looking like
366 // MUL dst, src, -1
367 // with
368 // NEG dst, src
369 HNeg* neg = new (allocator) HNeg(type, input_other);
370 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100371 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000372 return;
373 }
374
375 if (Primitive::IsFloatingPointType(type) &&
376 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
377 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
378 // Replace code looking like
379 // FP_MUL dst, src, 2.0
380 // with
381 // FP_ADD dst, src, src
382 // The 'int' and 'long' cases are handled below.
383 block->ReplaceAndRemoveInstructionWith(instruction,
384 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100385 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000386 return;
387 }
388
389 if (Primitive::IsIntOrLongType(type)) {
390 int64_t factor = Int64FromConstant(input_cst);
391 // We expect the `0` case to have been handled in the constant folding pass.
392 DCHECK_NE(factor, 0);
393 if (IsPowerOfTwo(factor)) {
394 // Replace code looking like
395 // MUL dst, src, pow_of_2
396 // with
397 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000398 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000399 HShl* shl = new(allocator) HShl(type, input_other, shift);
400 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100401 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000402 }
403 }
404}
405
Alexandre Rames188d4312015-04-09 18:30:21 +0100406void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
407 HInstruction* input = instruction->GetInput();
408 if (input->IsNeg()) {
409 // Replace code looking like
410 // NEG tmp, src
411 // NEG dst, tmp
412 // with
413 // src
414 HNeg* previous_neg = input->AsNeg();
415 instruction->ReplaceWith(previous_neg->GetInput());
416 instruction->GetBlock()->RemoveInstruction(instruction);
417 // We perform the optimization even if the input negation has environment
418 // uses since it allows removing the current instruction. But we only delete
419 // the input negation only if it is does not have any uses left.
420 if (!previous_neg->HasUses()) {
421 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
422 }
423 RecordSimplification();
424 return;
425 }
426
427 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse()) {
428 // Replace code looking like
429 // SUB tmp, a, b
430 // NEG dst, tmp
431 // with
432 // SUB dst, b, a
433 // We do not perform the optimization if the input subtraction has
434 // environment uses or multiple non-environment uses as it could lead to
435 // worse code. In particular, we do not want the live ranges of `a` and `b`
436 // to be extended if we are not sure the initial 'SUB' instruction can be
437 // removed.
438 HSub* sub = input->AsSub();
439 HSub* new_sub =
440 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
441 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
442 if (!sub->HasUses()) {
443 sub->GetBlock()->RemoveInstruction(sub);
444 }
445 RecordSimplification();
446 }
447}
448
449void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
450 HInstruction* input = instruction->GetInput();
451 if (input->IsNot()) {
452 // Replace code looking like
453 // NOT tmp, src
454 // NOT dst, tmp
455 // with
456 // src
457 // We perform the optimization even if the input negation has environment
458 // uses since it allows removing the current instruction. But we only delete
459 // the input negation only if it is does not have any uses left.
460 HNot* previous_not = input->AsNot();
461 instruction->ReplaceWith(previous_not->GetInput());
462 instruction->GetBlock()->RemoveInstruction(instruction);
463 if (!previous_not->HasUses()) {
464 previous_not->GetBlock()->RemoveInstruction(previous_not);
465 }
466 RecordSimplification();
467 }
468}
469
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000470void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
471 HConstant* input_cst = instruction->GetConstantRight();
472 HInstruction* input_other = instruction->GetLeastConstantLeft();
473
474 if ((input_cst != nullptr) && input_cst->IsZero()) {
475 // Replace code looking like
476 // OR dst, src, 0
477 // with
478 // src
479 instruction->ReplaceWith(input_other);
480 instruction->GetBlock()->RemoveInstruction(instruction);
481 return;
482 }
483
484 // We assume that GVN has run before, so we only perform a pointer comparison.
485 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100486 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000487 if (instruction->GetLeft() == instruction->GetRight()) {
488 // Replace code looking like
489 // OR dst, src, src
490 // with
491 // src
492 instruction->ReplaceWith(instruction->GetLeft());
493 instruction->GetBlock()->RemoveInstruction(instruction);
494 }
495}
496
497void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
498 VisitShift(instruction);
499}
500
501void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
502 VisitShift(instruction);
503}
504
505void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
506 HConstant* input_cst = instruction->GetConstantRight();
507 HInstruction* input_other = instruction->GetLeastConstantLeft();
508
509 if ((input_cst != nullptr) && input_cst->IsZero()) {
510 // Replace code looking like
511 // SUB dst, src, 0
512 // with
513 // src
514 instruction->ReplaceWith(input_other);
515 instruction->GetBlock()->RemoveInstruction(instruction);
516 return;
517 }
518
519 Primitive::Type type = instruction->GetType();
520 if (!Primitive::IsIntegralType(type)) {
521 return;
522 }
523
524 HBasicBlock* block = instruction->GetBlock();
525 ArenaAllocator* allocator = GetGraph()->GetArena();
526
Alexandre Rames188d4312015-04-09 18:30:21 +0100527 HInstruction* left = instruction->GetLeft();
528 HInstruction* right = instruction->GetRight();
529 if (left->IsConstant()) {
530 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000531 // Replace code looking like
532 // SUB dst, 0, src
533 // with
534 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100535 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000536 // `x` is `0.0`, the former expression yields `0.0`, while the later
537 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100538 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000539 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100540 RecordSimplification();
541 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000542 }
543 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100544
545 if (left->IsNeg() && right->IsNeg()) {
546 if (TryMoveNegOnInputsAfterBinop(instruction)) {
547 return;
548 }
549 }
550
551 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
552 // Replace code looking like
553 // NEG tmp, b
554 // SUB dst, a, tmp
555 // with
556 // ADD dst, a, b
557 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
558 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
559 RecordSimplification();
560 right->GetBlock()->RemoveInstruction(right);
561 return;
562 }
563
564 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
565 // Replace code looking like
566 // NEG tmp, a
567 // SUB dst, tmp, b
568 // with
569 // ADD tmp, a, b
570 // NEG dst, tmp
571 // The second version is not intrinsically better, but enables more
572 // transformations.
573 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
574 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
575 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
576 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
577 instruction->ReplaceWith(neg);
578 instruction->GetBlock()->RemoveInstruction(instruction);
579 RecordSimplification();
580 left->GetBlock()->RemoveInstruction(left);
581 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000582}
583
584void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
585 VisitShift(instruction);
586}
587
588void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
589 HConstant* input_cst = instruction->GetConstantRight();
590 HInstruction* input_other = instruction->GetLeastConstantLeft();
591
592 if ((input_cst != nullptr) && input_cst->IsZero()) {
593 // Replace code looking like
594 // XOR dst, src, 0
595 // with
596 // src
597 instruction->ReplaceWith(input_other);
598 instruction->GetBlock()->RemoveInstruction(instruction);
599 return;
600 }
601
602 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
603 // Replace code looking like
604 // XOR dst, src, 0xFFF...FF
605 // with
606 // NOT dst, src
607 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
608 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100609 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000610 return;
611 }
612}
613
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100614} // namespace art