blob: 0adb9311587aa20d83534061aebc41b20eab94ec [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 Geoffray07276db2015-05-18 14:22:09 +010048 void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE;
49 void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000050 void VisitArraySet(HArraySet* equal) OVERRIDE;
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +000051 void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE;
Calin Juravle10e244f2015-01-26 18:54:32 +000052 void VisitNullCheck(HNullCheck* instruction) OVERRIDE;
Mingyao Yang0304e182015-01-30 16:41:29 -080053 void VisitArrayLength(HArrayLength* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000054 void VisitCheckCast(HCheckCast* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000055 void VisitAdd(HAdd* instruction) OVERRIDE;
56 void VisitAnd(HAnd* instruction) OVERRIDE;
57 void VisitDiv(HDiv* instruction) OVERRIDE;
58 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010059 void VisitNeg(HNeg* instruction) OVERRIDE;
60 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000061 void VisitOr(HOr* instruction) OVERRIDE;
62 void VisitShl(HShl* instruction) OVERRIDE;
63 void VisitShr(HShr* instruction) OVERRIDE;
64 void VisitSub(HSub* instruction) OVERRIDE;
65 void VisitUShr(HUShr* instruction) OVERRIDE;
66 void VisitXor(HXor* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010067 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Calin Juravleacf735c2015-02-12 15:25:22 +000068
69 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010070 bool simplification_occurred_ = false;
71 int simplifications_at_current_position_ = 0;
72 // We ensure we do not loop infinitely. The value is a finger in the air guess
73 // that should allow enough simplification.
74 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000075};
76
Nicolas Geoffray3c049742014-09-24 18:10:46 +010077void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000078 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010079 visitor.Run();
80}
81
82void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +010083 // Iterate in reverse post order to open up more simplifications to users
84 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +010085 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
86 // The simplification of an instruction to another instruction may yield
87 // possibilities for other simplifications. So although we perform a reverse
88 // post order visit, we sometimes need to revisit an instruction index.
89 simplification_occurred_ = false;
90 VisitBasicBlock(it.Current());
91 if (simplification_occurred_ &&
92 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
93 // New simplifications may be applicable to the instruction at the
94 // current index, so don't advance the iterator.
95 continue;
96 }
Alexandre Rames188d4312015-04-09 18:30:21 +010097 simplifications_at_current_position_ = 0;
98 it.Advance();
99 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100100}
101
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000102namespace {
103
104bool AreAllBitsSet(HConstant* constant) {
105 return Int64FromConstant(constant) == -1;
106}
107
108} // namespace
109
Alexandre Rames188d4312015-04-09 18:30:21 +0100110// Returns true if the code was simplified to use only one negation operation
111// after the binary operation instead of one on each of the inputs.
112bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
113 DCHECK(binop->IsAdd() || binop->IsSub());
114 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
115 HNeg* left_neg = binop->GetLeft()->AsNeg();
116 HNeg* right_neg = binop->GetRight()->AsNeg();
117 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
118 !right_neg->HasOnlyOneNonEnvironmentUse()) {
119 return false;
120 }
121 // Replace code looking like
122 // NEG tmp1, a
123 // NEG tmp2, b
124 // ADD dst, tmp1, tmp2
125 // with
126 // ADD tmp, a, b
127 // NEG dst, tmp
128 binop->ReplaceInput(left_neg->GetInput(), 0);
129 binop->ReplaceInput(right_neg->GetInput(), 1);
130 left_neg->GetBlock()->RemoveInstruction(left_neg);
131 right_neg->GetBlock()->RemoveInstruction(right_neg);
132 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
133 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
134 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
135 RecordSimplification();
136 return true;
137}
138
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000139void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
140 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
141 HConstant* input_cst = instruction->GetConstantRight();
142 HInstruction* input_other = instruction->GetLeastConstantLeft();
143
Mark Mendellba56d062015-05-05 21:34:03 -0400144 if (input_cst != nullptr) {
145 if (input_cst->IsZero()) {
146 // Replace code looking like
147 // SHL dst, src, 0
148 // with
149 // src
150 instruction->ReplaceWith(input_other);
151 instruction->GetBlock()->RemoveInstruction(instruction);
152 } else if (instruction->IsShl() && input_cst->IsOne()) {
153 // Replace Shl looking like
154 // SHL dst, src, 1
155 // with
156 // ADD dst, src, src
157 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
158 input_other,
159 input_other);
160 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
161 RecordSimplification();
162 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000163 }
164}
165
Calin Juravle10e244f2015-01-26 18:54:32 +0000166void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
167 HInstruction* obj = null_check->InputAt(0);
168 if (!obj->CanBeNull()) {
169 null_check->ReplaceWith(obj);
170 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000171 if (stats_ != nullptr) {
172 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
173 }
174 }
175}
176
177void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
178 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100179 if (!check_cast->InputAt(0)->CanBeNull()) {
180 check_cast->ClearMustDoNullCheck();
181 }
182
Calin Juravleacf735c2015-02-12 15:25:22 +0000183 if (!load_class->IsResolved()) {
184 // If the class couldn't be resolve it's not safe to compare against it. It's
185 // default type would be Top which might be wider that the actual class type
186 // and thus producing wrong results.
187 return;
188 }
189 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
190 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
191 ScopedObjectAccess soa(Thread::Current());
192 if (class_rti.IsSupertypeOf(obj_rti)) {
193 check_cast->GetBlock()->RemoveInstruction(check_cast);
194 if (stats_ != nullptr) {
195 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
196 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000197 }
198}
199
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100200void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
201 if (!instruction->InputAt(0)->CanBeNull()) {
202 instruction->ClearMustDoNullCheck();
203 }
204}
205
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100206void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
207 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
208 && !instruction->GetValue()->CanBeNull()) {
209 instruction->ClearValueCanBeNull();
210 }
211}
212
213void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
214 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
215 && !instruction->GetValue()->CanBeNull()) {
216 instruction->ClearValueCanBeNull();
217 }
218}
219
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000220void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 HBasicBlock* block = check->GetBlock();
222 // Currently always keep the suspend check at entry.
223 if (block->IsEntryBlock()) return;
224
225 // Currently always keep suspend checks at loop entry.
226 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
227 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
228 return;
229 }
230
231 // Remove the suspend check that was added at build time for the baseline
232 // compiler.
233 block->RemoveInstruction(check);
234}
235
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000236void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100237 HInstruction* input_const = equal->GetConstantRight();
238 if (input_const != nullptr) {
239 HInstruction* input_value = equal->GetLeastConstantLeft();
240 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
241 HBasicBlock* block = equal->GetBlock();
242 if (input_const->AsIntConstant()->IsOne()) {
243 // Replace (bool_value == true) with bool_value
244 equal->ReplaceWith(input_value);
245 block->RemoveInstruction(equal);
246 RecordSimplification();
247 } else {
248 // Replace (bool_value == false) with !bool_value
249 DCHECK(input_const->AsIntConstant()->IsZero());
250 block->ReplaceAndRemoveInstructionWith(
251 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
252 RecordSimplification();
253 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100254 }
255 }
256}
257
David Brazdil0d13fee2015-04-17 14:52:19 +0100258void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
259 HInstruction* input_const = not_equal->GetConstantRight();
260 if (input_const != nullptr) {
261 HInstruction* input_value = not_equal->GetLeastConstantLeft();
262 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
263 HBasicBlock* block = not_equal->GetBlock();
264 if (input_const->AsIntConstant()->IsOne()) {
265 // Replace (bool_value != true) with !bool_value
266 block->ReplaceAndRemoveInstructionWith(
267 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
268 RecordSimplification();
269 } else {
270 // Replace (bool_value != false) with bool_value
271 DCHECK(input_const->AsIntConstant()->IsZero());
272 not_equal->ReplaceWith(input_value);
273 block->RemoveInstruction(not_equal);
274 RecordSimplification();
275 }
276 }
277 }
278}
279
280void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
281 HInstruction* parent = bool_not->InputAt(0);
282 if (parent->IsBooleanNot()) {
283 HInstruction* value = parent->InputAt(0);
284 // Replace (!(!bool_value)) with bool_value
285 bool_not->ReplaceWith(value);
286 bool_not->GetBlock()->RemoveInstruction(bool_not);
287 // It is possible that `parent` is dead at this point but we leave
288 // its removal to DCE for simplicity.
289 RecordSimplification();
290 }
291}
292
Mingyao Yang0304e182015-01-30 16:41:29 -0800293void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
294 HInstruction* input = instruction->InputAt(0);
295 // If the array is a NewArray with constant size, replace the array length
296 // with the constant instruction. This helps the bounds check elimination phase.
297 if (input->IsNewArray()) {
298 input = input->InputAt(0);
299 if (input->IsIntConstant()) {
300 instruction->ReplaceWith(input);
301 }
302 }
303}
304
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000305void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000306 HInstruction* value = instruction->GetValue();
307 if (value->GetType() != Primitive::kPrimNot) return;
308
309 if (value->IsArrayGet()) {
310 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
311 // If the code is just swapping elements in the array, no need for a type check.
312 instruction->ClearNeedsTypeCheck();
313 }
314 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100315
316 if (!value->CanBeNull()) {
317 instruction->ClearValueCanBeNull();
318 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000319}
320
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000321void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
322 if (instruction->GetResultType() == instruction->GetInputType()) {
323 // Remove the instruction if it's converting to the same type.
324 instruction->ReplaceWith(instruction->GetInput());
325 instruction->GetBlock()->RemoveInstruction(instruction);
326 }
327}
328
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000329void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
330 HConstant* input_cst = instruction->GetConstantRight();
331 HInstruction* input_other = instruction->GetLeastConstantLeft();
332 if ((input_cst != nullptr) && input_cst->IsZero()) {
333 // Replace code looking like
334 // ADD dst, src, 0
335 // with
336 // src
337 instruction->ReplaceWith(input_other);
338 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Rames188d4312015-04-09 18:30:21 +0100339 return;
340 }
341
342 HInstruction* left = instruction->GetLeft();
343 HInstruction* right = instruction->GetRight();
344 bool left_is_neg = left->IsNeg();
345 bool right_is_neg = right->IsNeg();
346
347 if (left_is_neg && right_is_neg) {
348 if (TryMoveNegOnInputsAfterBinop(instruction)) {
349 return;
350 }
351 }
352
353 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
354 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
355 // Replace code looking like
356 // NEG tmp, b
357 // ADD dst, a, tmp
358 // with
359 // SUB dst, a, b
360 // We do not perform the optimization if the input negation has environment
361 // uses or multiple non-environment uses as it could lead to worse code. In
362 // particular, we do not want the live range of `b` to be extended if we are
363 // not sure the initial 'NEG' instruction can be removed.
364 HInstruction* other = left_is_neg ? right : left;
365 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
366 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
367 RecordSimplification();
368 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000369 }
370}
371
372void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
373 HConstant* input_cst = instruction->GetConstantRight();
374 HInstruction* input_other = instruction->GetLeastConstantLeft();
375
376 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
377 // Replace code looking like
378 // AND dst, src, 0xFFF...FF
379 // with
380 // src
381 instruction->ReplaceWith(input_other);
382 instruction->GetBlock()->RemoveInstruction(instruction);
383 return;
384 }
385
386 // We assume that GVN has run before, so we only perform a pointer comparison.
387 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100388 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000389 if (instruction->GetLeft() == instruction->GetRight()) {
390 // Replace code looking like
391 // AND dst, src, src
392 // with
393 // src
394 instruction->ReplaceWith(instruction->GetLeft());
395 instruction->GetBlock()->RemoveInstruction(instruction);
396 }
397}
398
399void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
400 HConstant* input_cst = instruction->GetConstantRight();
401 HInstruction* input_other = instruction->GetLeastConstantLeft();
402 Primitive::Type type = instruction->GetType();
403
404 if ((input_cst != nullptr) && input_cst->IsOne()) {
405 // Replace code looking like
406 // DIV dst, src, 1
407 // with
408 // src
409 instruction->ReplaceWith(input_other);
410 instruction->GetBlock()->RemoveInstruction(instruction);
411 return;
412 }
413
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000414 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000415 // Replace code looking like
416 // DIV dst, src, -1
417 // with
418 // NEG dst, src
419 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000420 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100421 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000422 return;
423 }
424
425 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
426 // Try replacing code looking like
427 // DIV dst, src, constant
428 // with
429 // MUL dst, src, 1 / constant
430 HConstant* reciprocal = nullptr;
431 if (type == Primitive::Primitive::kPrimDouble) {
432 double value = input_cst->AsDoubleConstant()->GetValue();
433 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
434 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
435 }
436 } else {
437 DCHECK_EQ(type, Primitive::kPrimFloat);
438 float value = input_cst->AsFloatConstant()->GetValue();
439 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
440 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
441 }
442 }
443
444 if (reciprocal != nullptr) {
445 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
446 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
447 RecordSimplification();
448 return;
449 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000450 }
451}
452
453void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
454 HConstant* input_cst = instruction->GetConstantRight();
455 HInstruction* input_other = instruction->GetLeastConstantLeft();
456 Primitive::Type type = instruction->GetType();
457 HBasicBlock* block = instruction->GetBlock();
458 ArenaAllocator* allocator = GetGraph()->GetArena();
459
460 if (input_cst == nullptr) {
461 return;
462 }
463
464 if (input_cst->IsOne()) {
465 // Replace code looking like
466 // MUL dst, src, 1
467 // with
468 // src
469 instruction->ReplaceWith(input_other);
470 instruction->GetBlock()->RemoveInstruction(instruction);
471 return;
472 }
473
474 if (input_cst->IsMinusOne() &&
475 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
476 // Replace code looking like
477 // MUL dst, src, -1
478 // with
479 // NEG dst, src
480 HNeg* neg = new (allocator) HNeg(type, input_other);
481 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100482 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000483 return;
484 }
485
486 if (Primitive::IsFloatingPointType(type) &&
487 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
488 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
489 // Replace code looking like
490 // FP_MUL dst, src, 2.0
491 // with
492 // FP_ADD dst, src, src
493 // The 'int' and 'long' cases are handled below.
494 block->ReplaceAndRemoveInstructionWith(instruction,
495 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100496 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000497 return;
498 }
499
500 if (Primitive::IsIntOrLongType(type)) {
501 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600502 // Even though constant propagation also takes care of the zero case, other
503 // optimizations can lead to having a zero multiplication.
504 if (factor == 0) {
505 // Replace code looking like
506 // MUL dst, src, 0
507 // with
508 // 0
509 instruction->ReplaceWith(input_cst);
510 instruction->GetBlock()->RemoveInstruction(instruction);
511 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000512 // Replace code looking like
513 // MUL dst, src, pow_of_2
514 // with
515 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000517 HShl* shl = new(allocator) HShl(type, input_other, shift);
518 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100519 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000520 }
521 }
522}
523
Alexandre Rames188d4312015-04-09 18:30:21 +0100524void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
525 HInstruction* input = instruction->GetInput();
526 if (input->IsNeg()) {
527 // Replace code looking like
528 // NEG tmp, src
529 // NEG dst, tmp
530 // with
531 // src
532 HNeg* previous_neg = input->AsNeg();
533 instruction->ReplaceWith(previous_neg->GetInput());
534 instruction->GetBlock()->RemoveInstruction(instruction);
535 // We perform the optimization even if the input negation has environment
536 // uses since it allows removing the current instruction. But we only delete
537 // the input negation only if it is does not have any uses left.
538 if (!previous_neg->HasUses()) {
539 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
540 }
541 RecordSimplification();
542 return;
543 }
544
Serguei Katkov339dfc22015-04-20 12:29:32 +0600545 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
546 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100547 // Replace code looking like
548 // SUB tmp, a, b
549 // NEG dst, tmp
550 // with
551 // SUB dst, b, a
552 // We do not perform the optimization if the input subtraction has
553 // environment uses or multiple non-environment uses as it could lead to
554 // worse code. In particular, we do not want the live ranges of `a` and `b`
555 // to be extended if we are not sure the initial 'SUB' instruction can be
556 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600557 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100558 HSub* sub = input->AsSub();
559 HSub* new_sub =
560 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
561 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
562 if (!sub->HasUses()) {
563 sub->GetBlock()->RemoveInstruction(sub);
564 }
565 RecordSimplification();
566 }
567}
568
569void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
570 HInstruction* input = instruction->GetInput();
571 if (input->IsNot()) {
572 // Replace code looking like
573 // NOT tmp, src
574 // NOT dst, tmp
575 // with
576 // src
577 // We perform the optimization even if the input negation has environment
578 // uses since it allows removing the current instruction. But we only delete
579 // the input negation only if it is does not have any uses left.
580 HNot* previous_not = input->AsNot();
581 instruction->ReplaceWith(previous_not->GetInput());
582 instruction->GetBlock()->RemoveInstruction(instruction);
583 if (!previous_not->HasUses()) {
584 previous_not->GetBlock()->RemoveInstruction(previous_not);
585 }
586 RecordSimplification();
587 }
588}
589
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000590void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
591 HConstant* input_cst = instruction->GetConstantRight();
592 HInstruction* input_other = instruction->GetLeastConstantLeft();
593
594 if ((input_cst != nullptr) && input_cst->IsZero()) {
595 // Replace code looking like
596 // OR dst, src, 0
597 // with
598 // src
599 instruction->ReplaceWith(input_other);
600 instruction->GetBlock()->RemoveInstruction(instruction);
601 return;
602 }
603
604 // We assume that GVN has run before, so we only perform a pointer comparison.
605 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100606 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000607 if (instruction->GetLeft() == instruction->GetRight()) {
608 // Replace code looking like
609 // OR dst, src, src
610 // with
611 // src
612 instruction->ReplaceWith(instruction->GetLeft());
613 instruction->GetBlock()->RemoveInstruction(instruction);
614 }
615}
616
617void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
618 VisitShift(instruction);
619}
620
621void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
622 VisitShift(instruction);
623}
624
625void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
626 HConstant* input_cst = instruction->GetConstantRight();
627 HInstruction* input_other = instruction->GetLeastConstantLeft();
628
629 if ((input_cst != nullptr) && input_cst->IsZero()) {
630 // Replace code looking like
631 // SUB dst, src, 0
632 // with
633 // src
634 instruction->ReplaceWith(input_other);
635 instruction->GetBlock()->RemoveInstruction(instruction);
636 return;
637 }
638
639 Primitive::Type type = instruction->GetType();
640 if (!Primitive::IsIntegralType(type)) {
641 return;
642 }
643
644 HBasicBlock* block = instruction->GetBlock();
645 ArenaAllocator* allocator = GetGraph()->GetArena();
646
Alexandre Rames188d4312015-04-09 18:30:21 +0100647 HInstruction* left = instruction->GetLeft();
648 HInstruction* right = instruction->GetRight();
649 if (left->IsConstant()) {
650 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000651 // Replace code looking like
652 // SUB dst, 0, src
653 // with
654 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100655 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000656 // `x` is `0.0`, the former expression yields `0.0`, while the later
657 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100658 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000659 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100660 RecordSimplification();
661 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000662 }
663 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100664
665 if (left->IsNeg() && right->IsNeg()) {
666 if (TryMoveNegOnInputsAfterBinop(instruction)) {
667 return;
668 }
669 }
670
671 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
672 // Replace code looking like
673 // NEG tmp, b
674 // SUB dst, a, tmp
675 // with
676 // ADD dst, a, b
677 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
678 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
679 RecordSimplification();
680 right->GetBlock()->RemoveInstruction(right);
681 return;
682 }
683
684 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
685 // Replace code looking like
686 // NEG tmp, a
687 // SUB dst, tmp, b
688 // with
689 // ADD tmp, a, b
690 // NEG dst, tmp
691 // The second version is not intrinsically better, but enables more
692 // transformations.
693 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
694 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
695 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
696 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
697 instruction->ReplaceWith(neg);
698 instruction->GetBlock()->RemoveInstruction(instruction);
699 RecordSimplification();
700 left->GetBlock()->RemoveInstruction(left);
701 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000702}
703
704void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
705 VisitShift(instruction);
706}
707
708void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
709 HConstant* input_cst = instruction->GetConstantRight();
710 HInstruction* input_other = instruction->GetLeastConstantLeft();
711
712 if ((input_cst != nullptr) && input_cst->IsZero()) {
713 // Replace code looking like
714 // XOR dst, src, 0
715 // with
716 // src
717 instruction->ReplaceWith(input_other);
718 instruction->GetBlock()->RemoveInstruction(instruction);
719 return;
720 }
721
722 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
723 // Replace code looking like
724 // XOR dst, src, 0xFFF...FF
725 // with
726 // NOT dst, src
727 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
728 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100729 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000730 return;
731 }
732}
733
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100734} // namespace art