1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "instruction_simplifier.h"
#include "mirror/class-inl.h"
#include "scoped_thread_state_change.h"
namespace art {
class InstructionSimplifierVisitor : public HGraphVisitor {
public:
InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats)
: HGraphVisitor(graph),
stats_(stats) {}
void Run();
private:
void RecordSimplification() {
simplification_occurred_ = true;
simplifications_at_current_position_++;
if (stats_) {
stats_->RecordStat(kInstructionSimplifications);
}
}
bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
void VisitShift(HBinaryOperation* shift);
void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE;
void VisitEqual(HEqual* equal) OVERRIDE;
void VisitNotEqual(HNotEqual* equal) OVERRIDE;
void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
void VisitArraySet(HArraySet* equal) OVERRIDE;
void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
void VisitAdd(HAdd* instruction) OVERRIDE;
void VisitAnd(HAnd* instruction) OVERRIDE;
void VisitDiv(HDiv* instruction) OVERRIDE;
void VisitMul(HMul* instruction) OVERRIDE;
void VisitNeg(HNeg* instruction) OVERRIDE;
void VisitNot(HNot* instruction) OVERRIDE;
void VisitOr(HOr* instruction) OVERRIDE;
void VisitShl(HShl* instruction) OVERRIDE;
void VisitShr(HShr* instruction) OVERRIDE;
void VisitSub(HSub* instruction) OVERRIDE;
void VisitUShr(HUShr* instruction) OVERRIDE;
void VisitXor(HXor* instruction) OVERRIDE;
void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
OptimizingCompilerStats* stats_;
bool simplification_occurred_ = false;
int simplifications_at_current_position_ = 0;
// We ensure we do not loop infinitely. The value is a finger in the air guess
// that should allow enough simplification.
static constexpr int kMaxSamePositionSimplifications = 10;
};
void InstructionSimplifier::Run() {
InstructionSimplifierVisitor visitor(graph_, stats_);
visitor.Run();
}
void InstructionSimplifierVisitor::Run() {
for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
// The simplification of an instruction to another instruction may yield
// possibilities for other simplifications. So although we perform a reverse
// post order visit, we sometimes need to revisit an instruction index.
simplification_occurred_ = false;
VisitBasicBlock(it.Current());
if (simplification_occurred_ &&
(simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
// New simplifications may be applicable to the instruction at the
// current index, so don't advance the iterator.
continue;
}
simplifications_at_current_position_ = 0;
it.Advance();
}
}
namespace {
bool AreAllBitsSet(HConstant* constant) {
return Int64FromConstant(constant) == -1;
}
} // namespace
// Returns true if the code was simplified to use only one negation operation
// after the binary operation instead of one on each of the inputs.
bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
DCHECK(binop->IsAdd() || binop->IsSub());
DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
HNeg* left_neg = binop->GetLeft()->AsNeg();
HNeg* right_neg = binop->GetRight()->AsNeg();
if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
!right_neg->HasOnlyOneNonEnvironmentUse()) {
return false;
}
// Replace code looking like
// NEG tmp1, a
// NEG tmp2, b
// ADD dst, tmp1, tmp2
// with
// ADD tmp, a, b
// NEG dst, tmp
binop->ReplaceInput(left_neg->GetInput(), 0);
binop->ReplaceInput(right_neg->GetInput(), 1);
left_neg->GetBlock()->RemoveInstruction(left_neg);
right_neg->GetBlock()->RemoveInstruction(right_neg);
HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
RecordSimplification();
return true;
}
void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if (input_cst != nullptr) {
if (input_cst->IsZero()) {
// Replace code looking like
// SHL dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
} else if (instruction->IsShl() && input_cst->IsOne()) {
// Replace Shl looking like
// SHL dst, src, 1
// with
// ADD dst, src, src
HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
input_other,
input_other);
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
RecordSimplification();
}
}
}
void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
HInstruction* obj = null_check->InputAt(0);
if (!obj->CanBeNull()) {
null_check->ReplaceWith(obj);
null_check->GetBlock()->RemoveInstruction(null_check);
if (stats_ != nullptr) {
stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
}
}
}
void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
if (!check_cast->InputAt(0)->CanBeNull()) {
check_cast->ClearMustDoNullCheck();
}
if (!load_class->IsResolved()) {
// If the class couldn't be resolve it's not safe to compare against it. It's
// default type would be Top which might be wider that the actual class type
// and thus producing wrong results.
return;
}
ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
ScopedObjectAccess soa(Thread::Current());
if (class_rti.IsSupertypeOf(obj_rti)) {
check_cast->GetBlock()->RemoveInstruction(check_cast);
if (stats_ != nullptr) {
stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
}
}
}
void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
if (!instruction->InputAt(0)->CanBeNull()) {
instruction->ClearMustDoNullCheck();
}
}
void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
HBasicBlock* block = check->GetBlock();
// Currently always keep the suspend check at entry.
if (block->IsEntryBlock()) return;
// Currently always keep suspend checks at loop entry.
if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
return;
}
// Remove the suspend check that was added at build time for the baseline
// compiler.
block->RemoveInstruction(check);
}
void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
HInstruction* input_const = equal->GetConstantRight();
if (input_const != nullptr) {
HInstruction* input_value = equal->GetLeastConstantLeft();
if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
HBasicBlock* block = equal->GetBlock();
if (input_const->AsIntConstant()->IsOne()) {
// Replace (bool_value == true) with bool_value
equal->ReplaceWith(input_value);
block->RemoveInstruction(equal);
RecordSimplification();
} else {
// Replace (bool_value == false) with !bool_value
DCHECK(input_const->AsIntConstant()->IsZero());
block->ReplaceAndRemoveInstructionWith(
equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
RecordSimplification();
}
}
}
}
void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
HInstruction* input_const = not_equal->GetConstantRight();
if (input_const != nullptr) {
HInstruction* input_value = not_equal->GetLeastConstantLeft();
if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
HBasicBlock* block = not_equal->GetBlock();
if (input_const->AsIntConstant()->IsOne()) {
// Replace (bool_value != true) with !bool_value
block->ReplaceAndRemoveInstructionWith(
not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
RecordSimplification();
} else {
// Replace (bool_value != false) with bool_value
DCHECK(input_const->AsIntConstant()->IsZero());
not_equal->ReplaceWith(input_value);
block->RemoveInstruction(not_equal);
RecordSimplification();
}
}
}
}
void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
HInstruction* parent = bool_not->InputAt(0);
if (parent->IsBooleanNot()) {
HInstruction* value = parent->InputAt(0);
// Replace (!(!bool_value)) with bool_value
bool_not->ReplaceWith(value);
bool_not->GetBlock()->RemoveInstruction(bool_not);
// It is possible that `parent` is dead at this point but we leave
// its removal to DCE for simplicity.
RecordSimplification();
}
}
void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
HInstruction* input = instruction->InputAt(0);
// If the array is a NewArray with constant size, replace the array length
// with the constant instruction. This helps the bounds check elimination phase.
if (input->IsNewArray()) {
input = input->InputAt(0);
if (input->IsIntConstant()) {
instruction->ReplaceWith(input);
}
}
}
void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
HInstruction* value = instruction->GetValue();
if (value->GetType() != Primitive::kPrimNot) return;
if (value->IsArrayGet()) {
if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
// If the code is just swapping elements in the array, no need for a type check.
instruction->ClearNeedsTypeCheck();
}
}
}
void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
if (instruction->GetResultType() == instruction->GetInputType()) {
// Remove the instruction if it's converting to the same type.
instruction->ReplaceWith(instruction->GetInput());
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// ADD dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
HInstruction* left = instruction->GetLeft();
HInstruction* right = instruction->GetRight();
bool left_is_neg = left->IsNeg();
bool right_is_neg = right->IsNeg();
if (left_is_neg && right_is_neg) {
if (TryMoveNegOnInputsAfterBinop(instruction)) {
return;
}
}
HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
// Replace code looking like
// NEG tmp, b
// ADD dst, a, tmp
// with
// SUB dst, a, b
// We do not perform the optimization if the input negation has environment
// uses or multiple non-environment uses as it could lead to worse code. In
// particular, we do not want the live range of `b` to be extended if we are
// not sure the initial 'NEG' instruction can be removed.
HInstruction* other = left_is_neg ? right : left;
HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
RecordSimplification();
neg->GetBlock()->RemoveInstruction(neg);
}
}
void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
// Replace code looking like
// AND dst, src, 0xFFF...FF
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
// We assume that GVN has run before, so we only perform a pointer comparison.
// If for some reason the values are equal but the pointers are different, we
// are still correct and only miss an optimization opportunity.
if (instruction->GetLeft() == instruction->GetRight()) {
// Replace code looking like
// AND dst, src, src
// with
// src
instruction->ReplaceWith(instruction->GetLeft());
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
Primitive::Type type = instruction->GetType();
if ((input_cst != nullptr) && input_cst->IsOne()) {
// Replace code looking like
// DIV dst, src, 1
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
// Replace code looking like
// DIV dst, src, -1
// with
// NEG dst, src
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
RecordSimplification();
return;
}
if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
// Try replacing code looking like
// DIV dst, src, constant
// with
// MUL dst, src, 1 / constant
HConstant* reciprocal = nullptr;
if (type == Primitive::Primitive::kPrimDouble) {
double value = input_cst->AsDoubleConstant()->GetValue();
if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
}
} else {
DCHECK_EQ(type, Primitive::kPrimFloat);
float value = input_cst->AsFloatConstant()->GetValue();
if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
}
}
if (reciprocal != nullptr) {
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
RecordSimplification();
return;
}
}
}
void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
Primitive::Type type = instruction->GetType();
HBasicBlock* block = instruction->GetBlock();
ArenaAllocator* allocator = GetGraph()->GetArena();
if (input_cst == nullptr) {
return;
}
if (input_cst->IsOne()) {
// Replace code looking like
// MUL dst, src, 1
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
if (input_cst->IsMinusOne() &&
(Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
// Replace code looking like
// MUL dst, src, -1
// with
// NEG dst, src
HNeg* neg = new (allocator) HNeg(type, input_other);
block->ReplaceAndRemoveInstructionWith(instruction, neg);
RecordSimplification();
return;
}
if (Primitive::IsFloatingPointType(type) &&
((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
(input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
// Replace code looking like
// FP_MUL dst, src, 2.0
// with
// FP_ADD dst, src, src
// The 'int' and 'long' cases are handled below.
block->ReplaceAndRemoveInstructionWith(instruction,
new (allocator) HAdd(type, input_other, input_other));
RecordSimplification();
return;
}
if (Primitive::IsIntOrLongType(type)) {
int64_t factor = Int64FromConstant(input_cst);
// Even though constant propagation also takes care of the zero case, other
// optimizations can lead to having a zero multiplication.
if (factor == 0) {
// Replace code looking like
// MUL dst, src, 0
// with
// 0
instruction->ReplaceWith(input_cst);
instruction->GetBlock()->RemoveInstruction(instruction);
} else if (IsPowerOfTwo(factor)) {
// Replace code looking like
// MUL dst, src, pow_of_2
// with
// SHL dst, src, log2(pow_of_2)
HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
HShl* shl = new(allocator) HShl(type, input_other, shift);
block->ReplaceAndRemoveInstructionWith(instruction, shl);
RecordSimplification();
}
}
}
void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
HInstruction* input = instruction->GetInput();
if (input->IsNeg()) {
// Replace code looking like
// NEG tmp, src
// NEG dst, tmp
// with
// src
HNeg* previous_neg = input->AsNeg();
instruction->ReplaceWith(previous_neg->GetInput());
instruction->GetBlock()->RemoveInstruction(instruction);
// We perform the optimization even if the input negation has environment
// uses since it allows removing the current instruction. But we only delete
// the input negation only if it is does not have any uses left.
if (!previous_neg->HasUses()) {
previous_neg->GetBlock()->RemoveInstruction(previous_neg);
}
RecordSimplification();
return;
}
if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
!Primitive::IsFloatingPointType(input->GetType())) {
// Replace code looking like
// SUB tmp, a, b
// NEG dst, tmp
// with
// SUB dst, b, a
// We do not perform the optimization if the input subtraction has
// environment uses or multiple non-environment uses as it could lead to
// worse code. In particular, we do not want the live ranges of `a` and `b`
// to be extended if we are not sure the initial 'SUB' instruction can be
// removed.
// We do not perform optimization for fp because we could lose the sign of zero.
HSub* sub = input->AsSub();
HSub* new_sub =
new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
if (!sub->HasUses()) {
sub->GetBlock()->RemoveInstruction(sub);
}
RecordSimplification();
}
}
void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
HInstruction* input = instruction->GetInput();
if (input->IsNot()) {
// Replace code looking like
// NOT tmp, src
// NOT dst, tmp
// with
// src
// We perform the optimization even if the input negation has environment
// uses since it allows removing the current instruction. But we only delete
// the input negation only if it is does not have any uses left.
HNot* previous_not = input->AsNot();
instruction->ReplaceWith(previous_not->GetInput());
instruction->GetBlock()->RemoveInstruction(instruction);
if (!previous_not->HasUses()) {
previous_not->GetBlock()->RemoveInstruction(previous_not);
}
RecordSimplification();
}
}
void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// OR dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
// We assume that GVN has run before, so we only perform a pointer comparison.
// If for some reason the values are equal but the pointers are different, we
// are still correct and only miss an optimization opportunity.
if (instruction->GetLeft() == instruction->GetRight()) {
// Replace code looking like
// OR dst, src, src
// with
// src
instruction->ReplaceWith(instruction->GetLeft());
instruction->GetBlock()->RemoveInstruction(instruction);
}
}
void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
VisitShift(instruction);
}
void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
VisitShift(instruction);
}
void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// SUB dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
Primitive::Type type = instruction->GetType();
if (!Primitive::IsIntegralType(type)) {
return;
}
HBasicBlock* block = instruction->GetBlock();
ArenaAllocator* allocator = GetGraph()->GetArena();
HInstruction* left = instruction->GetLeft();
HInstruction* right = instruction->GetRight();
if (left->IsConstant()) {
if (Int64FromConstant(left->AsConstant()) == 0) {
// Replace code looking like
// SUB dst, 0, src
// with
// NEG dst, src
// Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
// `x` is `0.0`, the former expression yields `0.0`, while the later
// yields `-0.0`.
HNeg* neg = new (allocator) HNeg(type, right);
block->ReplaceAndRemoveInstructionWith(instruction, neg);
RecordSimplification();
return;
}
}
if (left->IsNeg() && right->IsNeg()) {
if (TryMoveNegOnInputsAfterBinop(instruction)) {
return;
}
}
if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
// Replace code looking like
// NEG tmp, b
// SUB dst, a, tmp
// with
// ADD dst, a, b
HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
RecordSimplification();
right->GetBlock()->RemoveInstruction(right);
return;
}
if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
// Replace code looking like
// NEG tmp, a
// SUB dst, tmp, b
// with
// ADD tmp, a, b
// NEG dst, tmp
// The second version is not intrinsically better, but enables more
// transformations.
HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
instruction->GetBlock()->InsertInstructionBefore(add, instruction);
HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
instruction->ReplaceWith(neg);
instruction->GetBlock()->RemoveInstruction(instruction);
RecordSimplification();
left->GetBlock()->RemoveInstruction(left);
}
}
void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
VisitShift(instruction);
}
void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
HConstant* input_cst = instruction->GetConstantRight();
HInstruction* input_other = instruction->GetLeastConstantLeft();
if ((input_cst != nullptr) && input_cst->IsZero()) {
// Replace code looking like
// XOR dst, src, 0
// with
// src
instruction->ReplaceWith(input_other);
instruction->GetBlock()->RemoveInstruction(instruction);
return;
}
if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
// Replace code looking like
// XOR dst, src, 0xFFF...FF
// with
// NOT dst, src
HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
RecordSimplification();
return;
}
}
} // namespace art
|