blob: 46fad17b8f091fd91cf314d2cc36f02c203dfc97 [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;
Calin Juravleacf735c2015-02-12 15:25:22 +000066
67 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010068 bool simplification_occurred_ = false;
69 int simplifications_at_current_position_ = 0;
70 // We ensure we do not loop infinitely. The value is a finger in the air guess
71 // that should allow enough simplification.
72 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000073};
74
Nicolas Geoffray3c049742014-09-24 18:10:46 +010075void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000076 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010077 visitor.Run();
78}
79
80void InstructionSimplifierVisitor::Run() {
81 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
82 // The simplification of an instruction to another instruction may yield
83 // possibilities for other simplifications. So although we perform a reverse
84 // post order visit, we sometimes need to revisit an instruction index.
85 simplification_occurred_ = false;
86 VisitBasicBlock(it.Current());
87 if (simplification_occurred_ &&
88 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
89 // New simplifications may be applicable to the instruction at the
90 // current index, so don't advance the iterator.
91 continue;
92 }
Alexandre Rames188d4312015-04-09 18:30:21 +010093 simplifications_at_current_position_ = 0;
94 it.Advance();
95 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +010096}
97
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000098namespace {
99
100bool AreAllBitsSet(HConstant* constant) {
101 return Int64FromConstant(constant) == -1;
102}
103
104} // namespace
105
Alexandre Rames188d4312015-04-09 18:30:21 +0100106// Returns true if the code was simplified to use only one negation operation
107// after the binary operation instead of one on each of the inputs.
108bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
109 DCHECK(binop->IsAdd() || binop->IsSub());
110 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
111 HNeg* left_neg = binop->GetLeft()->AsNeg();
112 HNeg* right_neg = binop->GetRight()->AsNeg();
113 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
114 !right_neg->HasOnlyOneNonEnvironmentUse()) {
115 return false;
116 }
117 // Replace code looking like
118 // NEG tmp1, a
119 // NEG tmp2, b
120 // ADD dst, tmp1, tmp2
121 // with
122 // ADD tmp, a, b
123 // NEG dst, tmp
124 binop->ReplaceInput(left_neg->GetInput(), 0);
125 binop->ReplaceInput(right_neg->GetInput(), 1);
126 left_neg->GetBlock()->RemoveInstruction(left_neg);
127 right_neg->GetBlock()->RemoveInstruction(right_neg);
128 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
129 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
130 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
131 RecordSimplification();
132 return true;
133}
134
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000135void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
136 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
137 HConstant* input_cst = instruction->GetConstantRight();
138 HInstruction* input_other = instruction->GetLeastConstantLeft();
139
Mark Mendellba56d062015-05-05 21:34:03 -0400140 if (input_cst != nullptr) {
141 if (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 } else if (instruction->IsShl() && input_cst->IsOne()) {
149 // Replace Shl looking like
150 // SHL dst, src, 1
151 // with
152 // ADD dst, src, src
153 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
154 input_other,
155 input_other);
156 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
157 RecordSimplification();
158 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000159 }
160}
161
Calin Juravle10e244f2015-01-26 18:54:32 +0000162void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
163 HInstruction* obj = null_check->InputAt(0);
164 if (!obj->CanBeNull()) {
165 null_check->ReplaceWith(obj);
166 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000167 if (stats_ != nullptr) {
168 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
169 }
170 }
171}
172
173void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
174 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100175 if (!check_cast->InputAt(0)->CanBeNull()) {
176 check_cast->ClearMustDoNullCheck();
177 }
178
Calin Juravleacf735c2015-02-12 15:25:22 +0000179 if (!load_class->IsResolved()) {
180 // If the class couldn't be resolve it's not safe to compare against it. It's
181 // default type would be Top which might be wider that the actual class type
182 // and thus producing wrong results.
183 return;
184 }
185 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
186 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
187 ScopedObjectAccess soa(Thread::Current());
188 if (class_rti.IsSupertypeOf(obj_rti)) {
189 check_cast->GetBlock()->RemoveInstruction(check_cast);
190 if (stats_ != nullptr) {
191 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
192 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000193 }
194}
195
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100196void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
197 if (!instruction->InputAt(0)->CanBeNull()) {
198 instruction->ClearMustDoNullCheck();
199 }
200}
201
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000202void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100203 HBasicBlock* block = check->GetBlock();
204 // Currently always keep the suspend check at entry.
205 if (block->IsEntryBlock()) return;
206
207 // Currently always keep suspend checks at loop entry.
208 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
209 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
210 return;
211 }
212
213 // Remove the suspend check that was added at build time for the baseline
214 // compiler.
215 block->RemoveInstruction(check);
216}
217
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000218void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100219 HInstruction* input_const = equal->GetConstantRight();
220 if (input_const != nullptr) {
221 HInstruction* input_value = equal->GetLeastConstantLeft();
222 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
223 HBasicBlock* block = equal->GetBlock();
224 if (input_const->AsIntConstant()->IsOne()) {
225 // Replace (bool_value == true) with bool_value
226 equal->ReplaceWith(input_value);
227 block->RemoveInstruction(equal);
228 RecordSimplification();
229 } else {
230 // Replace (bool_value == false) with !bool_value
231 DCHECK(input_const->AsIntConstant()->IsZero());
232 block->ReplaceAndRemoveInstructionWith(
233 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
234 RecordSimplification();
235 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100236 }
237 }
238}
239
David Brazdil0d13fee2015-04-17 14:52:19 +0100240void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
241 HInstruction* input_const = not_equal->GetConstantRight();
242 if (input_const != nullptr) {
243 HInstruction* input_value = not_equal->GetLeastConstantLeft();
244 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
245 HBasicBlock* block = not_equal->GetBlock();
246 if (input_const->AsIntConstant()->IsOne()) {
247 // Replace (bool_value != true) with !bool_value
248 block->ReplaceAndRemoveInstructionWith(
249 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
250 RecordSimplification();
251 } else {
252 // Replace (bool_value != false) with bool_value
253 DCHECK(input_const->AsIntConstant()->IsZero());
254 not_equal->ReplaceWith(input_value);
255 block->RemoveInstruction(not_equal);
256 RecordSimplification();
257 }
258 }
259 }
260}
261
262void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
263 HInstruction* parent = bool_not->InputAt(0);
264 if (parent->IsBooleanNot()) {
265 HInstruction* value = parent->InputAt(0);
266 // Replace (!(!bool_value)) with bool_value
267 bool_not->ReplaceWith(value);
268 bool_not->GetBlock()->RemoveInstruction(bool_not);
269 // It is possible that `parent` is dead at this point but we leave
270 // its removal to DCE for simplicity.
271 RecordSimplification();
272 }
273}
274
Mingyao Yang0304e182015-01-30 16:41:29 -0800275void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
276 HInstruction* input = instruction->InputAt(0);
277 // If the array is a NewArray with constant size, replace the array length
278 // with the constant instruction. This helps the bounds check elimination phase.
279 if (input->IsNewArray()) {
280 input = input->InputAt(0);
281 if (input->IsIntConstant()) {
282 instruction->ReplaceWith(input);
283 }
284 }
285}
286
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000287void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000288 HInstruction* value = instruction->GetValue();
289 if (value->GetType() != Primitive::kPrimNot) return;
290
291 if (value->IsArrayGet()) {
292 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
293 // If the code is just swapping elements in the array, no need for a type check.
294 instruction->ClearNeedsTypeCheck();
295 }
296 }
297}
298
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000299void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
300 if (instruction->GetResultType() == instruction->GetInputType()) {
301 // Remove the instruction if it's converting to the same type.
302 instruction->ReplaceWith(instruction->GetInput());
303 instruction->GetBlock()->RemoveInstruction(instruction);
304 }
305}
306
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000307void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
308 HConstant* input_cst = instruction->GetConstantRight();
309 HInstruction* input_other = instruction->GetLeastConstantLeft();
310 if ((input_cst != nullptr) && input_cst->IsZero()) {
311 // Replace code looking like
312 // ADD dst, src, 0
313 // with
314 // src
315 instruction->ReplaceWith(input_other);
316 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Rames188d4312015-04-09 18:30:21 +0100317 return;
318 }
319
320 HInstruction* left = instruction->GetLeft();
321 HInstruction* right = instruction->GetRight();
322 bool left_is_neg = left->IsNeg();
323 bool right_is_neg = right->IsNeg();
324
325 if (left_is_neg && right_is_neg) {
326 if (TryMoveNegOnInputsAfterBinop(instruction)) {
327 return;
328 }
329 }
330
331 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
332 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
333 // Replace code looking like
334 // NEG tmp, b
335 // ADD dst, a, tmp
336 // with
337 // SUB dst, a, b
338 // We do not perform the optimization if the input negation has environment
339 // uses or multiple non-environment uses as it could lead to worse code. In
340 // particular, we do not want the live range of `b` to be extended if we are
341 // not sure the initial 'NEG' instruction can be removed.
342 HInstruction* other = left_is_neg ? right : left;
343 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
344 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
345 RecordSimplification();
346 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000347 }
348}
349
350void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
351 HConstant* input_cst = instruction->GetConstantRight();
352 HInstruction* input_other = instruction->GetLeastConstantLeft();
353
354 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
355 // Replace code looking like
356 // AND dst, src, 0xFFF...FF
357 // with
358 // src
359 instruction->ReplaceWith(input_other);
360 instruction->GetBlock()->RemoveInstruction(instruction);
361 return;
362 }
363
364 // We assume that GVN has run before, so we only perform a pointer comparison.
365 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100366 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000367 if (instruction->GetLeft() == instruction->GetRight()) {
368 // Replace code looking like
369 // AND dst, src, src
370 // with
371 // src
372 instruction->ReplaceWith(instruction->GetLeft());
373 instruction->GetBlock()->RemoveInstruction(instruction);
374 }
375}
376
377void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
378 HConstant* input_cst = instruction->GetConstantRight();
379 HInstruction* input_other = instruction->GetLeastConstantLeft();
380 Primitive::Type type = instruction->GetType();
381
382 if ((input_cst != nullptr) && input_cst->IsOne()) {
383 // Replace code looking like
384 // DIV dst, src, 1
385 // with
386 // src
387 instruction->ReplaceWith(input_other);
388 instruction->GetBlock()->RemoveInstruction(instruction);
389 return;
390 }
391
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000392 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000393 // Replace code looking like
394 // DIV dst, src, -1
395 // with
396 // NEG dst, src
397 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000398 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100399 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000400 return;
401 }
402
403 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
404 // Try replacing code looking like
405 // DIV dst, src, constant
406 // with
407 // MUL dst, src, 1 / constant
408 HConstant* reciprocal = nullptr;
409 if (type == Primitive::Primitive::kPrimDouble) {
410 double value = input_cst->AsDoubleConstant()->GetValue();
411 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
412 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
413 }
414 } else {
415 DCHECK_EQ(type, Primitive::kPrimFloat);
416 float value = input_cst->AsFloatConstant()->GetValue();
417 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
418 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
419 }
420 }
421
422 if (reciprocal != nullptr) {
423 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
424 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
425 RecordSimplification();
426 return;
427 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000428 }
429}
430
431void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
432 HConstant* input_cst = instruction->GetConstantRight();
433 HInstruction* input_other = instruction->GetLeastConstantLeft();
434 Primitive::Type type = instruction->GetType();
435 HBasicBlock* block = instruction->GetBlock();
436 ArenaAllocator* allocator = GetGraph()->GetArena();
437
438 if (input_cst == nullptr) {
439 return;
440 }
441
442 if (input_cst->IsOne()) {
443 // Replace code looking like
444 // MUL dst, src, 1
445 // with
446 // src
447 instruction->ReplaceWith(input_other);
448 instruction->GetBlock()->RemoveInstruction(instruction);
449 return;
450 }
451
452 if (input_cst->IsMinusOne() &&
453 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
454 // Replace code looking like
455 // MUL dst, src, -1
456 // with
457 // NEG dst, src
458 HNeg* neg = new (allocator) HNeg(type, input_other);
459 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100460 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000461 return;
462 }
463
464 if (Primitive::IsFloatingPointType(type) &&
465 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
466 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
467 // Replace code looking like
468 // FP_MUL dst, src, 2.0
469 // with
470 // FP_ADD dst, src, src
471 // The 'int' and 'long' cases are handled below.
472 block->ReplaceAndRemoveInstructionWith(instruction,
473 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100474 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000475 return;
476 }
477
478 if (Primitive::IsIntOrLongType(type)) {
479 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600480 // Even though constant propagation also takes care of the zero case, other
481 // optimizations can lead to having a zero multiplication.
482 if (factor == 0) {
483 // Replace code looking like
484 // MUL dst, src, 0
485 // with
486 // 0
487 instruction->ReplaceWith(input_cst);
488 instruction->GetBlock()->RemoveInstruction(instruction);
489 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000490 // Replace code looking like
491 // MUL dst, src, pow_of_2
492 // with
493 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000494 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000495 HShl* shl = new(allocator) HShl(type, input_other, shift);
496 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100497 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000498 }
499 }
500}
501
Alexandre Rames188d4312015-04-09 18:30:21 +0100502void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
503 HInstruction* input = instruction->GetInput();
504 if (input->IsNeg()) {
505 // Replace code looking like
506 // NEG tmp, src
507 // NEG dst, tmp
508 // with
509 // src
510 HNeg* previous_neg = input->AsNeg();
511 instruction->ReplaceWith(previous_neg->GetInput());
512 instruction->GetBlock()->RemoveInstruction(instruction);
513 // We perform the optimization even if the input negation has environment
514 // uses since it allows removing the current instruction. But we only delete
515 // the input negation only if it is does not have any uses left.
516 if (!previous_neg->HasUses()) {
517 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
518 }
519 RecordSimplification();
520 return;
521 }
522
Serguei Katkov339dfc22015-04-20 12:29:32 +0600523 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
524 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100525 // Replace code looking like
526 // SUB tmp, a, b
527 // NEG dst, tmp
528 // with
529 // SUB dst, b, a
530 // We do not perform the optimization if the input subtraction has
531 // environment uses or multiple non-environment uses as it could lead to
532 // worse code. In particular, we do not want the live ranges of `a` and `b`
533 // to be extended if we are not sure the initial 'SUB' instruction can be
534 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600535 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100536 HSub* sub = input->AsSub();
537 HSub* new_sub =
538 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
539 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
540 if (!sub->HasUses()) {
541 sub->GetBlock()->RemoveInstruction(sub);
542 }
543 RecordSimplification();
544 }
545}
546
547void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
548 HInstruction* input = instruction->GetInput();
549 if (input->IsNot()) {
550 // Replace code looking like
551 // NOT tmp, src
552 // NOT dst, tmp
553 // with
554 // src
555 // We perform the optimization even if the input negation has environment
556 // uses since it allows removing the current instruction. But we only delete
557 // the input negation only if it is does not have any uses left.
558 HNot* previous_not = input->AsNot();
559 instruction->ReplaceWith(previous_not->GetInput());
560 instruction->GetBlock()->RemoveInstruction(instruction);
561 if (!previous_not->HasUses()) {
562 previous_not->GetBlock()->RemoveInstruction(previous_not);
563 }
564 RecordSimplification();
565 }
566}
567
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000568void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
569 HConstant* input_cst = instruction->GetConstantRight();
570 HInstruction* input_other = instruction->GetLeastConstantLeft();
571
572 if ((input_cst != nullptr) && input_cst->IsZero()) {
573 // Replace code looking like
574 // OR dst, src, 0
575 // with
576 // src
577 instruction->ReplaceWith(input_other);
578 instruction->GetBlock()->RemoveInstruction(instruction);
579 return;
580 }
581
582 // We assume that GVN has run before, so we only perform a pointer comparison.
583 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100584 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000585 if (instruction->GetLeft() == instruction->GetRight()) {
586 // Replace code looking like
587 // OR dst, src, src
588 // with
589 // src
590 instruction->ReplaceWith(instruction->GetLeft());
591 instruction->GetBlock()->RemoveInstruction(instruction);
592 }
593}
594
595void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
596 VisitShift(instruction);
597}
598
599void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
600 VisitShift(instruction);
601}
602
603void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
604 HConstant* input_cst = instruction->GetConstantRight();
605 HInstruction* input_other = instruction->GetLeastConstantLeft();
606
607 if ((input_cst != nullptr) && input_cst->IsZero()) {
608 // Replace code looking like
609 // SUB dst, src, 0
610 // with
611 // src
612 instruction->ReplaceWith(input_other);
613 instruction->GetBlock()->RemoveInstruction(instruction);
614 return;
615 }
616
617 Primitive::Type type = instruction->GetType();
618 if (!Primitive::IsIntegralType(type)) {
619 return;
620 }
621
622 HBasicBlock* block = instruction->GetBlock();
623 ArenaAllocator* allocator = GetGraph()->GetArena();
624
Alexandre Rames188d4312015-04-09 18:30:21 +0100625 HInstruction* left = instruction->GetLeft();
626 HInstruction* right = instruction->GetRight();
627 if (left->IsConstant()) {
628 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000629 // Replace code looking like
630 // SUB dst, 0, src
631 // with
632 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100633 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000634 // `x` is `0.0`, the former expression yields `0.0`, while the later
635 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100636 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000637 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100638 RecordSimplification();
639 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000640 }
641 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100642
643 if (left->IsNeg() && right->IsNeg()) {
644 if (TryMoveNegOnInputsAfterBinop(instruction)) {
645 return;
646 }
647 }
648
649 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
650 // Replace code looking like
651 // NEG tmp, b
652 // SUB dst, a, tmp
653 // with
654 // ADD dst, a, b
655 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
656 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
657 RecordSimplification();
658 right->GetBlock()->RemoveInstruction(right);
659 return;
660 }
661
662 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
663 // Replace code looking like
664 // NEG tmp, a
665 // SUB dst, tmp, b
666 // with
667 // ADD tmp, a, b
668 // NEG dst, tmp
669 // The second version is not intrinsically better, but enables more
670 // transformations.
671 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
672 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
673 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
674 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
675 instruction->ReplaceWith(neg);
676 instruction->GetBlock()->RemoveInstruction(instruction);
677 RecordSimplification();
678 left->GetBlock()->RemoveInstruction(left);
679 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000680}
681
682void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
683 VisitShift(instruction);
684}
685
686void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
687 HConstant* input_cst = instruction->GetConstantRight();
688 HInstruction* input_other = instruction->GetLeastConstantLeft();
689
690 if ((input_cst != nullptr) && input_cst->IsZero()) {
691 // Replace code looking like
692 // XOR dst, src, 0
693 // with
694 // src
695 instruction->ReplaceWith(input_other);
696 instruction->GetBlock()->RemoveInstruction(instruction);
697 return;
698 }
699
700 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
701 // Replace code looking like
702 // XOR dst, src, 0xFFF...FF
703 // with
704 // NOT dst, src
705 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
706 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100707 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000708 return;
709 }
710}
711
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100712} // namespace art