blob: 86c4a98deb9dc3fa99a46e2b1f2ab6a24765930f [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;
David Brazdil0d13fee2015-04-17 14:52:19 +010046 void VisitNotEqual(HNotEqual* equal) OVERRIDE;
47 void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000048 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000049 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000050 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080051 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000052 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000053 void VisitAdd(HAdd* instruction) OVERRIDE;
54 void VisitAnd(HAnd* instruction) OVERRIDE;
55 void VisitDiv(HDiv* instruction) OVERRIDE;
56 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010057 void VisitNeg(HNeg* instruction) OVERRIDE;
58 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000059 void VisitOr(HOr* instruction) OVERRIDE;
60 void VisitShl(HShl* instruction) OVERRIDE;
61 void VisitShr(HShr* instruction) OVERRIDE;
62 void VisitSub(HSub* instruction) OVERRIDE;
63 void VisitUShr(HUShr* instruction) OVERRIDE;
64 void VisitXor(HXor* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010065 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +010066 bool IsDominatedByInputNullCheck(HInstruction* instr);
Calin Juravleacf735c2015-02-12 15:25:22 +000067
68 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010069 bool simplification_occurred_ = false;
70 int simplifications_at_current_position_ = 0;
71 // We ensure we do not loop infinitely. The value is a finger in the air guess
72 // that should allow enough simplification.
73 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000074};
75
Nicolas Geoffray3c049742014-09-24 18:10:46 +010076void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000077 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010078 visitor.Run();
79}
80
81void InstructionSimplifierVisitor::Run() {
82 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
83 // The simplification of an instruction to another instruction may yield
84 // possibilities for other simplifications. So although we perform a reverse
85 // post order visit, we sometimes need to revisit an instruction index.
86 simplification_occurred_ = false;
87 VisitBasicBlock(it.Current());
88 if (simplification_occurred_ &&
89 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
90 // New simplifications may be applicable to the instruction at the
91 // current index, so don't advance the iterator.
92 continue;
93 }
Alexandre Rames188d4312015-04-09 18:30:21 +010094 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
Mark Mendellba56d062015-05-05 21:34:03 -0400141 if (input_cst != nullptr) {
142 if (input_cst->IsZero()) {
143 // Replace code looking like
144 // SHL dst, src, 0
145 // with
146 // src
147 instruction->ReplaceWith(input_other);
148 instruction->GetBlock()->RemoveInstruction(instruction);
149 } else if (instruction->IsShl() && input_cst->IsOne()) {
150 // Replace Shl looking like
151 // SHL dst, src, 1
152 // with
153 // ADD dst, src, src
154 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
155 input_other,
156 input_other);
157 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
158 RecordSimplification();
159 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000160 }
161}
162
Calin Juravle10e244f2015-01-26 18:54:32 +0000163void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
164 HInstruction* obj = null_check->InputAt(0);
165 if (!obj->CanBeNull()) {
166 null_check->ReplaceWith(obj);
167 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000168 if (stats_ != nullptr) {
169 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
170 }
171 }
172}
173
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100174bool InstructionSimplifierVisitor::IsDominatedByInputNullCheck(HInstruction* instr) {
175 HInstruction* input = instr->InputAt(0);
176 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
177 HInstruction* use = it.Current()->GetUser();
178 if (use->IsNullCheck() && use->StrictlyDominates(instr)) {
179 return true;
180 }
181 }
182 return false;
183}
184
Calin Juravleacf735c2015-02-12 15:25:22 +0000185void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
186 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100187 if (!check_cast->InputAt(0)->CanBeNull() || IsDominatedByInputNullCheck(check_cast)) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100188 check_cast->ClearMustDoNullCheck();
189 }
190
Calin Juravleacf735c2015-02-12 15:25:22 +0000191 if (!load_class->IsResolved()) {
192 // If the class couldn't be resolve it's not safe to compare against it. It's
193 // default type would be Top which might be wider that the actual class type
194 // and thus producing wrong results.
195 return;
196 }
197 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
198 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
199 ScopedObjectAccess soa(Thread::Current());
200 if (class_rti.IsSupertypeOf(obj_rti)) {
201 check_cast->GetBlock()->RemoveInstruction(check_cast);
202 if (stats_ != nullptr) {
203 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
204 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000205 }
206}
207
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100208void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100209 if (!instruction->InputAt(0)->CanBeNull() || IsDominatedByInputNullCheck(instruction)) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100210 instruction->ClearMustDoNullCheck();
211 }
212}
213
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000214void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 HBasicBlock* block = check->GetBlock();
216 // Currently always keep the suspend check at entry.
217 if (block->IsEntryBlock()) return;
218
219 // Currently always keep suspend checks at loop entry.
220 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
221 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
222 return;
223 }
224
225 // Remove the suspend check that was added at build time for the baseline
226 // compiler.
227 block->RemoveInstruction(check);
228}
229
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000230void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100231 HInstruction* input_const = equal->GetConstantRight();
232 if (input_const != nullptr) {
233 HInstruction* input_value = equal->GetLeastConstantLeft();
234 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
235 HBasicBlock* block = equal->GetBlock();
236 if (input_const->AsIntConstant()->IsOne()) {
237 // Replace (bool_value == true) with bool_value
238 equal->ReplaceWith(input_value);
239 block->RemoveInstruction(equal);
240 RecordSimplification();
241 } else {
242 // Replace (bool_value == false) with !bool_value
243 DCHECK(input_const->AsIntConstant()->IsZero());
244 block->ReplaceAndRemoveInstructionWith(
245 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
246 RecordSimplification();
247 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100248 }
249 }
250}
251
David Brazdil0d13fee2015-04-17 14:52:19 +0100252void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
253 HInstruction* input_const = not_equal->GetConstantRight();
254 if (input_const != nullptr) {
255 HInstruction* input_value = not_equal->GetLeastConstantLeft();
256 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
257 HBasicBlock* block = not_equal->GetBlock();
258 if (input_const->AsIntConstant()->IsOne()) {
259 // Replace (bool_value != true) with !bool_value
260 block->ReplaceAndRemoveInstructionWith(
261 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
262 RecordSimplification();
263 } else {
264 // Replace (bool_value != false) with bool_value
265 DCHECK(input_const->AsIntConstant()->IsZero());
266 not_equal->ReplaceWith(input_value);
267 block->RemoveInstruction(not_equal);
268 RecordSimplification();
269 }
270 }
271 }
272}
273
274void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
275 HInstruction* parent = bool_not->InputAt(0);
276 if (parent->IsBooleanNot()) {
277 HInstruction* value = parent->InputAt(0);
278 // Replace (!(!bool_value)) with bool_value
279 bool_not->ReplaceWith(value);
280 bool_not->GetBlock()->RemoveInstruction(bool_not);
281 // It is possible that `parent` is dead at this point but we leave
282 // its removal to DCE for simplicity.
283 RecordSimplification();
284 }
285}
286
Mingyao Yang0304e182015-01-30 16:41:29 -0800287void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
288 HInstruction* input = instruction->InputAt(0);
289 // If the array is a NewArray with constant size, replace the array length
290 // with the constant instruction. This helps the bounds check elimination phase.
291 if (input->IsNewArray()) {
292 input = input->InputAt(0);
293 if (input->IsIntConstant()) {
294 instruction->ReplaceWith(input);
295 }
296 }
297}
298
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000299void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000300 HInstruction* value = instruction->GetValue();
301 if (value->GetType() != Primitive::kPrimNot) return;
302
303 if (value->IsArrayGet()) {
304 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
305 // If the code is just swapping elements in the array, no need for a type check.
306 instruction->ClearNeedsTypeCheck();
307 }
308 }
309}
310
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000311void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
312 if (instruction->GetResultType() == instruction->GetInputType()) {
313 // Remove the instruction if it's converting to the same type.
314 instruction->ReplaceWith(instruction->GetInput());
315 instruction->GetBlock()->RemoveInstruction(instruction);
316 }
317}
318
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000319void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
320 HConstant* input_cst = instruction->GetConstantRight();
321 HInstruction* input_other = instruction->GetLeastConstantLeft();
322 if ((input_cst != nullptr) && input_cst->IsZero()) {
323 // Replace code looking like
324 // ADD dst, src, 0
325 // with
326 // src
327 instruction->ReplaceWith(input_other);
328 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Rames188d4312015-04-09 18:30:21 +0100329 return;
330 }
331
332 HInstruction* left = instruction->GetLeft();
333 HInstruction* right = instruction->GetRight();
334 bool left_is_neg = left->IsNeg();
335 bool right_is_neg = right->IsNeg();
336
337 if (left_is_neg && right_is_neg) {
338 if (TryMoveNegOnInputsAfterBinop(instruction)) {
339 return;
340 }
341 }
342
343 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
344 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
345 // Replace code looking like
346 // NEG tmp, b
347 // ADD dst, a, tmp
348 // with
349 // SUB dst, a, b
350 // We do not perform the optimization if the input negation has environment
351 // uses or multiple non-environment uses as it could lead to worse code. In
352 // particular, we do not want the live range of `b` to be extended if we are
353 // not sure the initial 'NEG' instruction can be removed.
354 HInstruction* other = left_is_neg ? right : left;
355 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
356 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
357 RecordSimplification();
358 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000359 }
360}
361
362void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
363 HConstant* input_cst = instruction->GetConstantRight();
364 HInstruction* input_other = instruction->GetLeastConstantLeft();
365
366 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
367 // Replace code looking like
368 // AND dst, src, 0xFFF...FF
369 // with
370 // src
371 instruction->ReplaceWith(input_other);
372 instruction->GetBlock()->RemoveInstruction(instruction);
373 return;
374 }
375
376 // We assume that GVN has run before, so we only perform a pointer comparison.
377 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100378 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000379 if (instruction->GetLeft() == instruction->GetRight()) {
380 // Replace code looking like
381 // AND dst, src, src
382 // with
383 // src
384 instruction->ReplaceWith(instruction->GetLeft());
385 instruction->GetBlock()->RemoveInstruction(instruction);
386 }
387}
388
389void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
390 HConstant* input_cst = instruction->GetConstantRight();
391 HInstruction* input_other = instruction->GetLeastConstantLeft();
392 Primitive::Type type = instruction->GetType();
393
394 if ((input_cst != nullptr) && input_cst->IsOne()) {
395 // Replace code looking like
396 // DIV dst, src, 1
397 // with
398 // src
399 instruction->ReplaceWith(input_other);
400 instruction->GetBlock()->RemoveInstruction(instruction);
401 return;
402 }
403
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000404 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000405 // Replace code looking like
406 // DIV dst, src, -1
407 // with
408 // NEG dst, src
409 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000410 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100411 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000412 return;
413 }
414
415 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
416 // Try replacing code looking like
417 // DIV dst, src, constant
418 // with
419 // MUL dst, src, 1 / constant
420 HConstant* reciprocal = nullptr;
421 if (type == Primitive::Primitive::kPrimDouble) {
422 double value = input_cst->AsDoubleConstant()->GetValue();
423 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
424 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
425 }
426 } else {
427 DCHECK_EQ(type, Primitive::kPrimFloat);
428 float value = input_cst->AsFloatConstant()->GetValue();
429 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
430 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
431 }
432 }
433
434 if (reciprocal != nullptr) {
435 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
436 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
437 RecordSimplification();
438 return;
439 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000440 }
441}
442
443void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
444 HConstant* input_cst = instruction->GetConstantRight();
445 HInstruction* input_other = instruction->GetLeastConstantLeft();
446 Primitive::Type type = instruction->GetType();
447 HBasicBlock* block = instruction->GetBlock();
448 ArenaAllocator* allocator = GetGraph()->GetArena();
449
450 if (input_cst == nullptr) {
451 return;
452 }
453
454 if (input_cst->IsOne()) {
455 // Replace code looking like
456 // MUL dst, src, 1
457 // with
458 // src
459 instruction->ReplaceWith(input_other);
460 instruction->GetBlock()->RemoveInstruction(instruction);
461 return;
462 }
463
464 if (input_cst->IsMinusOne() &&
465 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
466 // Replace code looking like
467 // MUL dst, src, -1
468 // with
469 // NEG dst, src
470 HNeg* neg = new (allocator) HNeg(type, input_other);
471 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100472 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000473 return;
474 }
475
476 if (Primitive::IsFloatingPointType(type) &&
477 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
478 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
479 // Replace code looking like
480 // FP_MUL dst, src, 2.0
481 // with
482 // FP_ADD dst, src, src
483 // The 'int' and 'long' cases are handled below.
484 block->ReplaceAndRemoveInstructionWith(instruction,
485 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100486 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000487 return;
488 }
489
490 if (Primitive::IsIntOrLongType(type)) {
491 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600492 // Even though constant propagation also takes care of the zero case, other
493 // optimizations can lead to having a zero multiplication.
494 if (factor == 0) {
495 // Replace code looking like
496 // MUL dst, src, 0
497 // with
498 // 0
499 instruction->ReplaceWith(input_cst);
500 instruction->GetBlock()->RemoveInstruction(instruction);
501 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000502 // Replace code looking like
503 // MUL dst, src, pow_of_2
504 // with
505 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000506 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000507 HShl* shl = new(allocator) HShl(type, input_other, shift);
508 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100509 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000510 }
511 }
512}
513
Alexandre Rames188d4312015-04-09 18:30:21 +0100514void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
515 HInstruction* input = instruction->GetInput();
516 if (input->IsNeg()) {
517 // Replace code looking like
518 // NEG tmp, src
519 // NEG dst, tmp
520 // with
521 // src
522 HNeg* previous_neg = input->AsNeg();
523 instruction->ReplaceWith(previous_neg->GetInput());
524 instruction->GetBlock()->RemoveInstruction(instruction);
525 // We perform the optimization even if the input negation has environment
526 // uses since it allows removing the current instruction. But we only delete
527 // the input negation only if it is does not have any uses left.
528 if (!previous_neg->HasUses()) {
529 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
530 }
531 RecordSimplification();
532 return;
533 }
534
Serguei Katkov339dfc22015-04-20 12:29:32 +0600535 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
536 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100537 // Replace code looking like
538 // SUB tmp, a, b
539 // NEG dst, tmp
540 // with
541 // SUB dst, b, a
542 // We do not perform the optimization if the input subtraction has
543 // environment uses or multiple non-environment uses as it could lead to
544 // worse code. In particular, we do not want the live ranges of `a` and `b`
545 // to be extended if we are not sure the initial 'SUB' instruction can be
546 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600547 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100548 HSub* sub = input->AsSub();
549 HSub* new_sub =
550 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
551 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
552 if (!sub->HasUses()) {
553 sub->GetBlock()->RemoveInstruction(sub);
554 }
555 RecordSimplification();
556 }
557}
558
559void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
560 HInstruction* input = instruction->GetInput();
561 if (input->IsNot()) {
562 // Replace code looking like
563 // NOT tmp, src
564 // NOT dst, tmp
565 // with
566 // src
567 // We perform the optimization even if the input negation has environment
568 // uses since it allows removing the current instruction. But we only delete
569 // the input negation only if it is does not have any uses left.
570 HNot* previous_not = input->AsNot();
571 instruction->ReplaceWith(previous_not->GetInput());
572 instruction->GetBlock()->RemoveInstruction(instruction);
573 if (!previous_not->HasUses()) {
574 previous_not->GetBlock()->RemoveInstruction(previous_not);
575 }
576 RecordSimplification();
577 }
578}
579
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000580void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
581 HConstant* input_cst = instruction->GetConstantRight();
582 HInstruction* input_other = instruction->GetLeastConstantLeft();
583
584 if ((input_cst != nullptr) && input_cst->IsZero()) {
585 // Replace code looking like
586 // OR dst, src, 0
587 // with
588 // src
589 instruction->ReplaceWith(input_other);
590 instruction->GetBlock()->RemoveInstruction(instruction);
591 return;
592 }
593
594 // We assume that GVN has run before, so we only perform a pointer comparison.
595 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100596 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000597 if (instruction->GetLeft() == instruction->GetRight()) {
598 // Replace code looking like
599 // OR dst, src, src
600 // with
601 // src
602 instruction->ReplaceWith(instruction->GetLeft());
603 instruction->GetBlock()->RemoveInstruction(instruction);
604 }
605}
606
607void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
608 VisitShift(instruction);
609}
610
611void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
612 VisitShift(instruction);
613}
614
615void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
616 HConstant* input_cst = instruction->GetConstantRight();
617 HInstruction* input_other = instruction->GetLeastConstantLeft();
618
619 if ((input_cst != nullptr) && input_cst->IsZero()) {
620 // Replace code looking like
621 // SUB dst, src, 0
622 // with
623 // src
624 instruction->ReplaceWith(input_other);
625 instruction->GetBlock()->RemoveInstruction(instruction);
626 return;
627 }
628
629 Primitive::Type type = instruction->GetType();
630 if (!Primitive::IsIntegralType(type)) {
631 return;
632 }
633
634 HBasicBlock* block = instruction->GetBlock();
635 ArenaAllocator* allocator = GetGraph()->GetArena();
636
Alexandre Rames188d4312015-04-09 18:30:21 +0100637 HInstruction* left = instruction->GetLeft();
638 HInstruction* right = instruction->GetRight();
639 if (left->IsConstant()) {
640 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000641 // Replace code looking like
642 // SUB dst, 0, src
643 // with
644 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100645 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000646 // `x` is `0.0`, the former expression yields `0.0`, while the later
647 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100648 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000649 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100650 RecordSimplification();
651 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000652 }
653 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100654
655 if (left->IsNeg() && right->IsNeg()) {
656 if (TryMoveNegOnInputsAfterBinop(instruction)) {
657 return;
658 }
659 }
660
661 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
662 // Replace code looking like
663 // NEG tmp, b
664 // SUB dst, a, tmp
665 // with
666 // ADD dst, a, b
667 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
668 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
669 RecordSimplification();
670 right->GetBlock()->RemoveInstruction(right);
671 return;
672 }
673
674 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
675 // Replace code looking like
676 // NEG tmp, a
677 // SUB dst, tmp, b
678 // with
679 // ADD tmp, a, b
680 // NEG dst, tmp
681 // The second version is not intrinsically better, but enables more
682 // transformations.
683 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
684 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
685 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
686 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
687 instruction->ReplaceWith(neg);
688 instruction->GetBlock()->RemoveInstruction(instruction);
689 RecordSimplification();
690 left->GetBlock()->RemoveInstruction(left);
691 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000692}
693
694void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
695 VisitShift(instruction);
696}
697
698void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
699 HConstant* input_cst = instruction->GetConstantRight();
700 HInstruction* input_other = instruction->GetLeastConstantLeft();
701
702 if ((input_cst != nullptr) && input_cst->IsZero()) {
703 // Replace code looking like
704 // XOR dst, src, 0
705 // with
706 // src
707 instruction->ReplaceWith(input_other);
708 instruction->GetBlock()->RemoveInstruction(instruction);
709 return;
710 }
711
712 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
713 // Replace code looking like
714 // XOR dst, src, 0xFFF...FF
715 // with
716 // NOT dst, src
717 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
718 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100719 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000720 return;
721 }
722}
723
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100724} // namespace art