blob: 2df7c166d84f26300f8852edef92dbe436c0deee [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
140 if ((input_cst != nullptr) && input_cst->IsZero()) {
141 // Replace code looking like
142 // SHL dst, src, 0
143 // with
144 // src
145 instruction->ReplaceWith(input_other);
146 instruction->GetBlock()->RemoveInstruction(instruction);
147 }
148}
149
Calin Juravle10e244f2015-01-26 18:54:32 +0000150void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
151 HInstruction* obj = null_check->InputAt(0);
152 if (!obj->CanBeNull()) {
153 null_check->ReplaceWith(obj);
154 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000155 if (stats_ != nullptr) {
156 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
157 }
158 }
159}
160
161void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
162 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100163 if (!check_cast->InputAt(0)->CanBeNull()) {
164 check_cast->ClearMustDoNullCheck();
165 }
166
Calin Juravleacf735c2015-02-12 15:25:22 +0000167 if (!load_class->IsResolved()) {
168 // If the class couldn't be resolve it's not safe to compare against it. It's
169 // default type would be Top which might be wider that the actual class type
170 // and thus producing wrong results.
171 return;
172 }
173 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
174 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
175 ScopedObjectAccess soa(Thread::Current());
176 if (class_rti.IsSupertypeOf(obj_rti)) {
177 check_cast->GetBlock()->RemoveInstruction(check_cast);
178 if (stats_ != nullptr) {
179 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
180 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000181 }
182}
183
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100184void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
185 if (!instruction->InputAt(0)->CanBeNull()) {
186 instruction->ClearMustDoNullCheck();
187 }
188}
189
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000190void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 HBasicBlock* block = check->GetBlock();
192 // Currently always keep the suspend check at entry.
193 if (block->IsEntryBlock()) return;
194
195 // Currently always keep suspend checks at loop entry.
196 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
197 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
198 return;
199 }
200
201 // Remove the suspend check that was added at build time for the baseline
202 // compiler.
203 block->RemoveInstruction(check);
204}
205
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000206void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100207 HInstruction* input_const = equal->GetConstantRight();
208 if (input_const != nullptr) {
209 HInstruction* input_value = equal->GetLeastConstantLeft();
210 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
211 HBasicBlock* block = equal->GetBlock();
212 if (input_const->AsIntConstant()->IsOne()) {
213 // Replace (bool_value == true) with bool_value
214 equal->ReplaceWith(input_value);
215 block->RemoveInstruction(equal);
216 RecordSimplification();
217 } else {
218 // Replace (bool_value == false) with !bool_value
219 DCHECK(input_const->AsIntConstant()->IsZero());
220 block->ReplaceAndRemoveInstructionWith(
221 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
222 RecordSimplification();
223 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100224 }
225 }
226}
227
David Brazdil0d13fee2015-04-17 14:52:19 +0100228void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
229 HInstruction* input_const = not_equal->GetConstantRight();
230 if (input_const != nullptr) {
231 HInstruction* input_value = not_equal->GetLeastConstantLeft();
232 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
233 HBasicBlock* block = not_equal->GetBlock();
234 if (input_const->AsIntConstant()->IsOne()) {
235 // Replace (bool_value != true) with !bool_value
236 block->ReplaceAndRemoveInstructionWith(
237 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
238 RecordSimplification();
239 } else {
240 // Replace (bool_value != false) with bool_value
241 DCHECK(input_const->AsIntConstant()->IsZero());
242 not_equal->ReplaceWith(input_value);
243 block->RemoveInstruction(not_equal);
244 RecordSimplification();
245 }
246 }
247 }
248}
249
250void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
251 HInstruction* parent = bool_not->InputAt(0);
252 if (parent->IsBooleanNot()) {
253 HInstruction* value = parent->InputAt(0);
254 // Replace (!(!bool_value)) with bool_value
255 bool_not->ReplaceWith(value);
256 bool_not->GetBlock()->RemoveInstruction(bool_not);
257 // It is possible that `parent` is dead at this point but we leave
258 // its removal to DCE for simplicity.
259 RecordSimplification();
260 }
261}
262
Mingyao Yang0304e182015-01-30 16:41:29 -0800263void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
264 HInstruction* input = instruction->InputAt(0);
265 // If the array is a NewArray with constant size, replace the array length
266 // with the constant instruction. This helps the bounds check elimination phase.
267 if (input->IsNewArray()) {
268 input = input->InputAt(0);
269 if (input->IsIntConstant()) {
270 instruction->ReplaceWith(input);
271 }
272 }
273}
274
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000275void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000276 HInstruction* value = instruction->GetValue();
277 if (value->GetType() != Primitive::kPrimNot) return;
278
279 if (value->IsArrayGet()) {
280 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
281 // If the code is just swapping elements in the array, no need for a type check.
282 instruction->ClearNeedsTypeCheck();
283 }
284 }
285}
286
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000287void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
288 if (instruction->GetResultType() == instruction->GetInputType()) {
289 // Remove the instruction if it's converting to the same type.
290 instruction->ReplaceWith(instruction->GetInput());
291 instruction->GetBlock()->RemoveInstruction(instruction);
292 }
293}
294
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000295void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
296 HConstant* input_cst = instruction->GetConstantRight();
297 HInstruction* input_other = instruction->GetLeastConstantLeft();
298 if ((input_cst != nullptr) && input_cst->IsZero()) {
299 // Replace code looking like
300 // ADD dst, src, 0
301 // with
302 // src
303 instruction->ReplaceWith(input_other);
304 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Rames188d4312015-04-09 18:30:21 +0100305 return;
306 }
307
308 HInstruction* left = instruction->GetLeft();
309 HInstruction* right = instruction->GetRight();
310 bool left_is_neg = left->IsNeg();
311 bool right_is_neg = right->IsNeg();
312
313 if (left_is_neg && right_is_neg) {
314 if (TryMoveNegOnInputsAfterBinop(instruction)) {
315 return;
316 }
317 }
318
319 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
320 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
321 // Replace code looking like
322 // NEG tmp, b
323 // ADD dst, a, tmp
324 // with
325 // SUB dst, a, b
326 // We do not perform the optimization if the input negation has environment
327 // uses or multiple non-environment uses as it could lead to worse code. In
328 // particular, we do not want the live range of `b` to be extended if we are
329 // not sure the initial 'NEG' instruction can be removed.
330 HInstruction* other = left_is_neg ? right : left;
331 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
332 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
333 RecordSimplification();
334 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000335 }
336}
337
338void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
339 HConstant* input_cst = instruction->GetConstantRight();
340 HInstruction* input_other = instruction->GetLeastConstantLeft();
341
342 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
343 // Replace code looking like
344 // AND dst, src, 0xFFF...FF
345 // with
346 // src
347 instruction->ReplaceWith(input_other);
348 instruction->GetBlock()->RemoveInstruction(instruction);
349 return;
350 }
351
352 // We assume that GVN has run before, so we only perform a pointer comparison.
353 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100354 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000355 if (instruction->GetLeft() == instruction->GetRight()) {
356 // Replace code looking like
357 // AND dst, src, src
358 // with
359 // src
360 instruction->ReplaceWith(instruction->GetLeft());
361 instruction->GetBlock()->RemoveInstruction(instruction);
362 }
363}
364
365void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
366 HConstant* input_cst = instruction->GetConstantRight();
367 HInstruction* input_other = instruction->GetLeastConstantLeft();
368 Primitive::Type type = instruction->GetType();
369
370 if ((input_cst != nullptr) && input_cst->IsOne()) {
371 // Replace code looking like
372 // DIV dst, src, 1
373 // with
374 // src
375 instruction->ReplaceWith(input_other);
376 instruction->GetBlock()->RemoveInstruction(instruction);
377 return;
378 }
379
380 if ((input_cst != nullptr) && input_cst->IsMinusOne() &&
381 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
382 // Replace code looking like
383 // DIV dst, src, -1
384 // with
385 // NEG dst, src
386 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
387 instruction, (new (GetGraph()->GetArena()) HNeg(type, input_other)));
Alexandre Rames188d4312015-04-09 18:30:21 +0100388 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000389 }
390}
391
392void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
393 HConstant* input_cst = instruction->GetConstantRight();
394 HInstruction* input_other = instruction->GetLeastConstantLeft();
395 Primitive::Type type = instruction->GetType();
396 HBasicBlock* block = instruction->GetBlock();
397 ArenaAllocator* allocator = GetGraph()->GetArena();
398
399 if (input_cst == nullptr) {
400 return;
401 }
402
403 if (input_cst->IsOne()) {
404 // Replace code looking like
405 // MUL dst, src, 1
406 // with
407 // src
408 instruction->ReplaceWith(input_other);
409 instruction->GetBlock()->RemoveInstruction(instruction);
410 return;
411 }
412
413 if (input_cst->IsMinusOne() &&
414 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
415 // Replace code looking like
416 // MUL dst, src, -1
417 // with
418 // NEG dst, src
419 HNeg* neg = new (allocator) HNeg(type, input_other);
420 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100421 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000422 return;
423 }
424
425 if (Primitive::IsFloatingPointType(type) &&
426 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
427 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
428 // Replace code looking like
429 // FP_MUL dst, src, 2.0
430 // with
431 // FP_ADD dst, src, src
432 // The 'int' and 'long' cases are handled below.
433 block->ReplaceAndRemoveInstructionWith(instruction,
434 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100435 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000436 return;
437 }
438
439 if (Primitive::IsIntOrLongType(type)) {
440 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600441 // Even though constant propagation also takes care of the zero case, other
442 // optimizations can lead to having a zero multiplication.
443 if (factor == 0) {
444 // Replace code looking like
445 // MUL dst, src, 0
446 // with
447 // 0
448 instruction->ReplaceWith(input_cst);
449 instruction->GetBlock()->RemoveInstruction(instruction);
450 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000451 // Replace code looking like
452 // MUL dst, src, pow_of_2
453 // with
454 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000456 HShl* shl = new(allocator) HShl(type, input_other, shift);
457 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100458 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000459 }
460 }
461}
462
Alexandre Rames188d4312015-04-09 18:30:21 +0100463void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
464 HInstruction* input = instruction->GetInput();
465 if (input->IsNeg()) {
466 // Replace code looking like
467 // NEG tmp, src
468 // NEG dst, tmp
469 // with
470 // src
471 HNeg* previous_neg = input->AsNeg();
472 instruction->ReplaceWith(previous_neg->GetInput());
473 instruction->GetBlock()->RemoveInstruction(instruction);
474 // We perform the optimization even if the input negation has environment
475 // uses since it allows removing the current instruction. But we only delete
476 // the input negation only if it is does not have any uses left.
477 if (!previous_neg->HasUses()) {
478 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
479 }
480 RecordSimplification();
481 return;
482 }
483
Serguei Katkov339dfc22015-04-20 12:29:32 +0600484 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
485 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100486 // Replace code looking like
487 // SUB tmp, a, b
488 // NEG dst, tmp
489 // with
490 // SUB dst, b, a
491 // We do not perform the optimization if the input subtraction has
492 // environment uses or multiple non-environment uses as it could lead to
493 // worse code. In particular, we do not want the live ranges of `a` and `b`
494 // to be extended if we are not sure the initial 'SUB' instruction can be
495 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600496 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100497 HSub* sub = input->AsSub();
498 HSub* new_sub =
499 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
500 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
501 if (!sub->HasUses()) {
502 sub->GetBlock()->RemoveInstruction(sub);
503 }
504 RecordSimplification();
505 }
506}
507
508void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
509 HInstruction* input = instruction->GetInput();
510 if (input->IsNot()) {
511 // Replace code looking like
512 // NOT tmp, src
513 // NOT dst, tmp
514 // with
515 // src
516 // We perform the optimization even if the input negation has environment
517 // uses since it allows removing the current instruction. But we only delete
518 // the input negation only if it is does not have any uses left.
519 HNot* previous_not = input->AsNot();
520 instruction->ReplaceWith(previous_not->GetInput());
521 instruction->GetBlock()->RemoveInstruction(instruction);
522 if (!previous_not->HasUses()) {
523 previous_not->GetBlock()->RemoveInstruction(previous_not);
524 }
525 RecordSimplification();
526 }
527}
528
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000529void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
530 HConstant* input_cst = instruction->GetConstantRight();
531 HInstruction* input_other = instruction->GetLeastConstantLeft();
532
533 if ((input_cst != nullptr) && input_cst->IsZero()) {
534 // Replace code looking like
535 // OR dst, src, 0
536 // with
537 // src
538 instruction->ReplaceWith(input_other);
539 instruction->GetBlock()->RemoveInstruction(instruction);
540 return;
541 }
542
543 // We assume that GVN has run before, so we only perform a pointer comparison.
544 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100545 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000546 if (instruction->GetLeft() == instruction->GetRight()) {
547 // Replace code looking like
548 // OR dst, src, src
549 // with
550 // src
551 instruction->ReplaceWith(instruction->GetLeft());
552 instruction->GetBlock()->RemoveInstruction(instruction);
553 }
554}
555
556void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
557 VisitShift(instruction);
558}
559
560void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
561 VisitShift(instruction);
562}
563
564void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
565 HConstant* input_cst = instruction->GetConstantRight();
566 HInstruction* input_other = instruction->GetLeastConstantLeft();
567
568 if ((input_cst != nullptr) && input_cst->IsZero()) {
569 // Replace code looking like
570 // SUB dst, src, 0
571 // with
572 // src
573 instruction->ReplaceWith(input_other);
574 instruction->GetBlock()->RemoveInstruction(instruction);
575 return;
576 }
577
578 Primitive::Type type = instruction->GetType();
579 if (!Primitive::IsIntegralType(type)) {
580 return;
581 }
582
583 HBasicBlock* block = instruction->GetBlock();
584 ArenaAllocator* allocator = GetGraph()->GetArena();
585
Alexandre Rames188d4312015-04-09 18:30:21 +0100586 HInstruction* left = instruction->GetLeft();
587 HInstruction* right = instruction->GetRight();
588 if (left->IsConstant()) {
589 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000590 // Replace code looking like
591 // SUB dst, 0, src
592 // with
593 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100594 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000595 // `x` is `0.0`, the former expression yields `0.0`, while the later
596 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100597 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000598 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100599 RecordSimplification();
600 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000601 }
602 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100603
604 if (left->IsNeg() && right->IsNeg()) {
605 if (TryMoveNegOnInputsAfterBinop(instruction)) {
606 return;
607 }
608 }
609
610 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
611 // Replace code looking like
612 // NEG tmp, b
613 // SUB dst, a, tmp
614 // with
615 // ADD dst, a, b
616 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
617 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
618 RecordSimplification();
619 right->GetBlock()->RemoveInstruction(right);
620 return;
621 }
622
623 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
624 // Replace code looking like
625 // NEG tmp, a
626 // SUB dst, tmp, b
627 // with
628 // ADD tmp, a, b
629 // NEG dst, tmp
630 // The second version is not intrinsically better, but enables more
631 // transformations.
632 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
633 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
634 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
635 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
636 instruction->ReplaceWith(neg);
637 instruction->GetBlock()->RemoveInstruction(instruction);
638 RecordSimplification();
639 left->GetBlock()->RemoveInstruction(left);
640 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000641}
642
643void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
644 VisitShift(instruction);
645}
646
647void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
648 HConstant* input_cst = instruction->GetConstantRight();
649 HInstruction* input_other = instruction->GetLeastConstantLeft();
650
651 if ((input_cst != nullptr) && input_cst->IsZero()) {
652 // Replace code looking like
653 // XOR dst, src, 0
654 // with
655 // src
656 instruction->ReplaceWith(input_other);
657 instruction->GetBlock()->RemoveInstruction(instruction);
658 return;
659 }
660
661 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
662 // Replace code looking like
663 // XOR dst, src, 0xFFF...FF
664 // with
665 // NOT dst, src
666 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
667 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100668 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000669 return;
670 }
671}
672
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100673} // namespace art