blob: 98c0eedeb2f2def1d4e45580bbea1990c3ffb46d [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;
Calin Juravleacf735c2015-02-12 15:25:22 +000065
66 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010067 bool simplification_occurred_ = false;
68 int simplifications_at_current_position_ = 0;
69 // We ensure we do not loop infinitely. The value is a finger in the air guess
70 // that should allow enough simplification.
71 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000072};
73
Nicolas Geoffray3c049742014-09-24 18:10:46 +010074void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000075 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010076 visitor.Run();
77}
78
79void InstructionSimplifierVisitor::Run() {
80 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
81 // The simplification of an instruction to another instruction may yield
82 // possibilities for other simplifications. So although we perform a reverse
83 // post order visit, we sometimes need to revisit an instruction index.
84 simplification_occurred_ = false;
85 VisitBasicBlock(it.Current());
86 if (simplification_occurred_ &&
87 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
88 // New simplifications may be applicable to the instruction at the
89 // current index, so don't advance the iterator.
90 continue;
91 }
Alexandre Rames188d4312015-04-09 18:30:21 +010092 simplifications_at_current_position_ = 0;
93 it.Advance();
94 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +010095}
96
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000097namespace {
98
99bool AreAllBitsSet(HConstant* constant) {
100 return Int64FromConstant(constant) == -1;
101}
102
103} // namespace
104
Alexandre Rames188d4312015-04-09 18:30:21 +0100105// Returns true if the code was simplified to use only one negation operation
106// after the binary operation instead of one on each of the inputs.
107bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
108 DCHECK(binop->IsAdd() || binop->IsSub());
109 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
110 HNeg* left_neg = binop->GetLeft()->AsNeg();
111 HNeg* right_neg = binop->GetRight()->AsNeg();
112 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
113 !right_neg->HasOnlyOneNonEnvironmentUse()) {
114 return false;
115 }
116 // Replace code looking like
117 // NEG tmp1, a
118 // NEG tmp2, b
119 // ADD dst, tmp1, tmp2
120 // with
121 // ADD tmp, a, b
122 // NEG dst, tmp
123 binop->ReplaceInput(left_neg->GetInput(), 0);
124 binop->ReplaceInput(right_neg->GetInput(), 1);
125 left_neg->GetBlock()->RemoveInstruction(left_neg);
126 right_neg->GetBlock()->RemoveInstruction(right_neg);
127 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
128 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
129 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
130 RecordSimplification();
131 return true;
132}
133
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000134void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
135 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
136 HConstant* input_cst = instruction->GetConstantRight();
137 HInstruction* input_other = instruction->GetLeastConstantLeft();
138
139 if ((input_cst != nullptr) && input_cst->IsZero()) {
140 // Replace code looking like
141 // SHL dst, src, 0
142 // with
143 // src
144 instruction->ReplaceWith(input_other);
145 instruction->GetBlock()->RemoveInstruction(instruction);
146 }
147}
148
Calin Juravle10e244f2015-01-26 18:54:32 +0000149void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
150 HInstruction* obj = null_check->InputAt(0);
151 if (!obj->CanBeNull()) {
152 null_check->ReplaceWith(obj);
153 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000154 if (stats_ != nullptr) {
155 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
156 }
157 }
158}
159
160void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
161 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
162 if (!load_class->IsResolved()) {
163 // If the class couldn't be resolve it's not safe to compare against it. It's
164 // default type would be Top which might be wider that the actual class type
165 // and thus producing wrong results.
166 return;
167 }
168 ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo();
169 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
170 ScopedObjectAccess soa(Thread::Current());
171 if (class_rti.IsSupertypeOf(obj_rti)) {
172 check_cast->GetBlock()->RemoveInstruction(check_cast);
173 if (stats_ != nullptr) {
174 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
175 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000176 }
177}
178
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000179void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 HBasicBlock* block = check->GetBlock();
181 // Currently always keep the suspend check at entry.
182 if (block->IsEntryBlock()) return;
183
184 // Currently always keep suspend checks at loop entry.
185 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
186 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
187 return;
188 }
189
190 // Remove the suspend check that was added at build time for the baseline
191 // compiler.
192 block->RemoveInstruction(check);
193}
194
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000195void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100196 HInstruction* input_const = equal->GetConstantRight();
197 if (input_const != nullptr) {
198 HInstruction* input_value = equal->GetLeastConstantLeft();
199 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
200 HBasicBlock* block = equal->GetBlock();
201 if (input_const->AsIntConstant()->IsOne()) {
202 // Replace (bool_value == true) with bool_value
203 equal->ReplaceWith(input_value);
204 block->RemoveInstruction(equal);
205 RecordSimplification();
206 } else {
207 // Replace (bool_value == false) with !bool_value
208 DCHECK(input_const->AsIntConstant()->IsZero());
209 block->ReplaceAndRemoveInstructionWith(
210 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
211 RecordSimplification();
212 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100213 }
214 }
215}
216
David Brazdil0d13fee2015-04-17 14:52:19 +0100217void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
218 HInstruction* input_const = not_equal->GetConstantRight();
219 if (input_const != nullptr) {
220 HInstruction* input_value = not_equal->GetLeastConstantLeft();
221 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
222 HBasicBlock* block = not_equal->GetBlock();
223 if (input_const->AsIntConstant()->IsOne()) {
224 // Replace (bool_value != true) with !bool_value
225 block->ReplaceAndRemoveInstructionWith(
226 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
227 RecordSimplification();
228 } else {
229 // Replace (bool_value != false) with bool_value
230 DCHECK(input_const->AsIntConstant()->IsZero());
231 not_equal->ReplaceWith(input_value);
232 block->RemoveInstruction(not_equal);
233 RecordSimplification();
234 }
235 }
236 }
237}
238
239void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
240 HInstruction* parent = bool_not->InputAt(0);
241 if (parent->IsBooleanNot()) {
242 HInstruction* value = parent->InputAt(0);
243 // Replace (!(!bool_value)) with bool_value
244 bool_not->ReplaceWith(value);
245 bool_not->GetBlock()->RemoveInstruction(bool_not);
246 // It is possible that `parent` is dead at this point but we leave
247 // its removal to DCE for simplicity.
248 RecordSimplification();
249 }
250}
251
Mingyao Yang0304e182015-01-30 16:41:29 -0800252void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
253 HInstruction* input = instruction->InputAt(0);
254 // If the array is a NewArray with constant size, replace the array length
255 // with the constant instruction. This helps the bounds check elimination phase.
256 if (input->IsNewArray()) {
257 input = input->InputAt(0);
258 if (input->IsIntConstant()) {
259 instruction->ReplaceWith(input);
260 }
261 }
262}
263
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000264void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000265 HInstruction* value = instruction->GetValue();
266 if (value->GetType() != Primitive::kPrimNot) return;
267
268 if (value->IsArrayGet()) {
269 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
270 // If the code is just swapping elements in the array, no need for a type check.
271 instruction->ClearNeedsTypeCheck();
272 }
273 }
274}
275
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000276void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
277 if (instruction->GetResultType() == instruction->GetInputType()) {
278 // Remove the instruction if it's converting to the same type.
279 instruction->ReplaceWith(instruction->GetInput());
280 instruction->GetBlock()->RemoveInstruction(instruction);
281 }
282}
283
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000284void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
285 HConstant* input_cst = instruction->GetConstantRight();
286 HInstruction* input_other = instruction->GetLeastConstantLeft();
287 if ((input_cst != nullptr) && input_cst->IsZero()) {
288 // Replace code looking like
289 // ADD dst, src, 0
290 // with
291 // src
292 instruction->ReplaceWith(input_other);
293 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Rames188d4312015-04-09 18:30:21 +0100294 return;
295 }
296
297 HInstruction* left = instruction->GetLeft();
298 HInstruction* right = instruction->GetRight();
299 bool left_is_neg = left->IsNeg();
300 bool right_is_neg = right->IsNeg();
301
302 if (left_is_neg && right_is_neg) {
303 if (TryMoveNegOnInputsAfterBinop(instruction)) {
304 return;
305 }
306 }
307
308 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
309 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
310 // Replace code looking like
311 // NEG tmp, b
312 // ADD dst, a, tmp
313 // with
314 // SUB dst, a, b
315 // We do not perform the optimization if the input negation has environment
316 // uses or multiple non-environment uses as it could lead to worse code. In
317 // particular, we do not want the live range of `b` to be extended if we are
318 // not sure the initial 'NEG' instruction can be removed.
319 HInstruction* other = left_is_neg ? right : left;
320 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
321 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
322 RecordSimplification();
323 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000324 }
325}
326
327void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
328 HConstant* input_cst = instruction->GetConstantRight();
329 HInstruction* input_other = instruction->GetLeastConstantLeft();
330
331 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
332 // Replace code looking like
333 // AND dst, src, 0xFFF...FF
334 // with
335 // src
336 instruction->ReplaceWith(input_other);
337 instruction->GetBlock()->RemoveInstruction(instruction);
338 return;
339 }
340
341 // We assume that GVN has run before, so we only perform a pointer comparison.
342 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100343 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000344 if (instruction->GetLeft() == instruction->GetRight()) {
345 // Replace code looking like
346 // AND dst, src, src
347 // with
348 // src
349 instruction->ReplaceWith(instruction->GetLeft());
350 instruction->GetBlock()->RemoveInstruction(instruction);
351 }
352}
353
354void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
355 HConstant* input_cst = instruction->GetConstantRight();
356 HInstruction* input_other = instruction->GetLeastConstantLeft();
357 Primitive::Type type = instruction->GetType();
358
359 if ((input_cst != nullptr) && input_cst->IsOne()) {
360 // Replace code looking like
361 // DIV dst, src, 1
362 // with
363 // src
364 instruction->ReplaceWith(input_other);
365 instruction->GetBlock()->RemoveInstruction(instruction);
366 return;
367 }
368
369 if ((input_cst != nullptr) && input_cst->IsMinusOne() &&
370 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
371 // Replace code looking like
372 // DIV dst, src, -1
373 // with
374 // NEG dst, src
375 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
376 instruction, (new (GetGraph()->GetArena()) HNeg(type, input_other)));
Alexandre Rames188d4312015-04-09 18:30:21 +0100377 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000378 }
379}
380
381void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
382 HConstant* input_cst = instruction->GetConstantRight();
383 HInstruction* input_other = instruction->GetLeastConstantLeft();
384 Primitive::Type type = instruction->GetType();
385 HBasicBlock* block = instruction->GetBlock();
386 ArenaAllocator* allocator = GetGraph()->GetArena();
387
388 if (input_cst == nullptr) {
389 return;
390 }
391
392 if (input_cst->IsOne()) {
393 // Replace code looking like
394 // MUL dst, src, 1
395 // with
396 // src
397 instruction->ReplaceWith(input_other);
398 instruction->GetBlock()->RemoveInstruction(instruction);
399 return;
400 }
401
402 if (input_cst->IsMinusOne() &&
403 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
404 // Replace code looking like
405 // MUL dst, src, -1
406 // with
407 // NEG dst, src
408 HNeg* neg = new (allocator) HNeg(type, input_other);
409 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100410 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000411 return;
412 }
413
414 if (Primitive::IsFloatingPointType(type) &&
415 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
416 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
417 // Replace code looking like
418 // FP_MUL dst, src, 2.0
419 // with
420 // FP_ADD dst, src, src
421 // The 'int' and 'long' cases are handled below.
422 block->ReplaceAndRemoveInstructionWith(instruction,
423 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100424 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000425 return;
426 }
427
428 if (Primitive::IsIntOrLongType(type)) {
429 int64_t factor = Int64FromConstant(input_cst);
430 // We expect the `0` case to have been handled in the constant folding pass.
431 DCHECK_NE(factor, 0);
432 if (IsPowerOfTwo(factor)) {
433 // Replace code looking like
434 // MUL dst, src, pow_of_2
435 // with
436 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000437 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000438 HShl* shl = new(allocator) HShl(type, input_other, shift);
439 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100440 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000441 }
442 }
443}
444
Alexandre Rames188d4312015-04-09 18:30:21 +0100445void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
446 HInstruction* input = instruction->GetInput();
447 if (input->IsNeg()) {
448 // Replace code looking like
449 // NEG tmp, src
450 // NEG dst, tmp
451 // with
452 // src
453 HNeg* previous_neg = input->AsNeg();
454 instruction->ReplaceWith(previous_neg->GetInput());
455 instruction->GetBlock()->RemoveInstruction(instruction);
456 // We perform the optimization even if the input negation has environment
457 // uses since it allows removing the current instruction. But we only delete
458 // the input negation only if it is does not have any uses left.
459 if (!previous_neg->HasUses()) {
460 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
461 }
462 RecordSimplification();
463 return;
464 }
465
Serguei Katkov339dfc22015-04-20 12:29:32 +0600466 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
467 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100468 // Replace code looking like
469 // SUB tmp, a, b
470 // NEG dst, tmp
471 // with
472 // SUB dst, b, a
473 // We do not perform the optimization if the input subtraction has
474 // environment uses or multiple non-environment uses as it could lead to
475 // worse code. In particular, we do not want the live ranges of `a` and `b`
476 // to be extended if we are not sure the initial 'SUB' instruction can be
477 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600478 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100479 HSub* sub = input->AsSub();
480 HSub* new_sub =
481 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
482 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
483 if (!sub->HasUses()) {
484 sub->GetBlock()->RemoveInstruction(sub);
485 }
486 RecordSimplification();
487 }
488}
489
490void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
491 HInstruction* input = instruction->GetInput();
492 if (input->IsNot()) {
493 // Replace code looking like
494 // NOT tmp, src
495 // NOT dst, tmp
496 // with
497 // src
498 // We perform the optimization even if the input negation has environment
499 // uses since it allows removing the current instruction. But we only delete
500 // the input negation only if it is does not have any uses left.
501 HNot* previous_not = input->AsNot();
502 instruction->ReplaceWith(previous_not->GetInput());
503 instruction->GetBlock()->RemoveInstruction(instruction);
504 if (!previous_not->HasUses()) {
505 previous_not->GetBlock()->RemoveInstruction(previous_not);
506 }
507 RecordSimplification();
508 }
509}
510
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000511void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
512 HConstant* input_cst = instruction->GetConstantRight();
513 HInstruction* input_other = instruction->GetLeastConstantLeft();
514
515 if ((input_cst != nullptr) && input_cst->IsZero()) {
516 // Replace code looking like
517 // OR dst, src, 0
518 // with
519 // src
520 instruction->ReplaceWith(input_other);
521 instruction->GetBlock()->RemoveInstruction(instruction);
522 return;
523 }
524
525 // We assume that GVN has run before, so we only perform a pointer comparison.
526 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100527 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000528 if (instruction->GetLeft() == instruction->GetRight()) {
529 // Replace code looking like
530 // OR dst, src, src
531 // with
532 // src
533 instruction->ReplaceWith(instruction->GetLeft());
534 instruction->GetBlock()->RemoveInstruction(instruction);
535 }
536}
537
538void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
539 VisitShift(instruction);
540}
541
542void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
543 VisitShift(instruction);
544}
545
546void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
547 HConstant* input_cst = instruction->GetConstantRight();
548 HInstruction* input_other = instruction->GetLeastConstantLeft();
549
550 if ((input_cst != nullptr) && input_cst->IsZero()) {
551 // Replace code looking like
552 // SUB dst, src, 0
553 // with
554 // src
555 instruction->ReplaceWith(input_other);
556 instruction->GetBlock()->RemoveInstruction(instruction);
557 return;
558 }
559
560 Primitive::Type type = instruction->GetType();
561 if (!Primitive::IsIntegralType(type)) {
562 return;
563 }
564
565 HBasicBlock* block = instruction->GetBlock();
566 ArenaAllocator* allocator = GetGraph()->GetArena();
567
Alexandre Rames188d4312015-04-09 18:30:21 +0100568 HInstruction* left = instruction->GetLeft();
569 HInstruction* right = instruction->GetRight();
570 if (left->IsConstant()) {
571 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000572 // Replace code looking like
573 // SUB dst, 0, src
574 // with
575 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100576 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000577 // `x` is `0.0`, the former expression yields `0.0`, while the later
578 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100579 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000580 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100581 RecordSimplification();
582 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000583 }
584 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100585
586 if (left->IsNeg() && right->IsNeg()) {
587 if (TryMoveNegOnInputsAfterBinop(instruction)) {
588 return;
589 }
590 }
591
592 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
593 // Replace code looking like
594 // NEG tmp, b
595 // SUB dst, a, tmp
596 // with
597 // ADD dst, a, b
598 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
599 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
600 RecordSimplification();
601 right->GetBlock()->RemoveInstruction(right);
602 return;
603 }
604
605 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
606 // Replace code looking like
607 // NEG tmp, a
608 // SUB dst, tmp, b
609 // with
610 // ADD tmp, a, b
611 // NEG dst, tmp
612 // The second version is not intrinsically better, but enables more
613 // transformations.
614 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
615 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
616 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
617 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
618 instruction->ReplaceWith(neg);
619 instruction->GetBlock()->RemoveInstruction(instruction);
620 RecordSimplification();
621 left->GetBlock()->RemoveInstruction(left);
622 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000623}
624
625void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
626 VisitShift(instruction);
627}
628
629void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
630 HConstant* input_cst = instruction->GetConstantRight();
631 HInstruction* input_other = instruction->GetLeastConstantLeft();
632
633 if ((input_cst != nullptr) && input_cst->IsZero()) {
634 // Replace code looking like
635 // XOR dst, src, 0
636 // with
637 // src
638 instruction->ReplaceWith(input_other);
639 instruction->GetBlock()->RemoveInstruction(instruction);
640 return;
641 }
642
643 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
644 // Replace code looking like
645 // XOR dst, src, 0xFFF...FF
646 // with
647 // NOT dst, src
648 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
649 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100650 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000651 return;
652 }
653}
654
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100655} // namespace art