blob: 86a3ad98b41de501dd07e51c1f85caaffe376e8f [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;
Mark Mendellc4701932015-04-10 13:18:51 -040057 void VisitCondition(HCondition* instruction) OVERRIDE;
58 void VisitGreaterThan(HGreaterThan* condition) OVERRIDE;
59 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE;
60 void VisitLessThan(HLessThan* condition) OVERRIDE;
61 void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000062 void VisitDiv(HDiv* instruction) OVERRIDE;
63 void VisitMul(HMul* instruction) OVERRIDE;
Alexandre Rames188d4312015-04-09 18:30:21 +010064 void VisitNeg(HNeg* instruction) OVERRIDE;
65 void VisitNot(HNot* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000066 void VisitOr(HOr* instruction) OVERRIDE;
67 void VisitShl(HShl* instruction) OVERRIDE;
68 void VisitShr(HShr* instruction) OVERRIDE;
69 void VisitSub(HSub* instruction) OVERRIDE;
70 void VisitUShr(HUShr* instruction) OVERRIDE;
71 void VisitXor(HXor* instruction) OVERRIDE;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +010072 void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010073 void VisitFakeString(HFakeString* fake_string) OVERRIDE;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +010074
75 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +000076
77 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010078 bool simplification_occurred_ = false;
79 int simplifications_at_current_position_ = 0;
80 // We ensure we do not loop infinitely. The value is a finger in the air guess
81 // that should allow enough simplification.
82 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000083};
84
Nicolas Geoffray3c049742014-09-24 18:10:46 +010085void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000086 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010087 visitor.Run();
88}
89
90void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +010091 // Iterate in reverse post order to open up more simplifications to users
92 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +010093 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
94 // The simplification of an instruction to another instruction may yield
95 // possibilities for other simplifications. So although we perform a reverse
96 // post order visit, we sometimes need to revisit an instruction index.
97 simplification_occurred_ = false;
98 VisitBasicBlock(it.Current());
99 if (simplification_occurred_ &&
100 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
101 // New simplifications may be applicable to the instruction at the
102 // current index, so don't advance the iterator.
103 continue;
104 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100105 simplifications_at_current_position_ = 0;
106 it.Advance();
107 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100108}
109
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000110namespace {
111
112bool AreAllBitsSet(HConstant* constant) {
113 return Int64FromConstant(constant) == -1;
114}
115
116} // namespace
117
Alexandre Rames188d4312015-04-09 18:30:21 +0100118// Returns true if the code was simplified to use only one negation operation
119// after the binary operation instead of one on each of the inputs.
120bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
121 DCHECK(binop->IsAdd() || binop->IsSub());
122 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
123 HNeg* left_neg = binop->GetLeft()->AsNeg();
124 HNeg* right_neg = binop->GetRight()->AsNeg();
125 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
126 !right_neg->HasOnlyOneNonEnvironmentUse()) {
127 return false;
128 }
129 // Replace code looking like
130 // NEG tmp1, a
131 // NEG tmp2, b
132 // ADD dst, tmp1, tmp2
133 // with
134 // ADD tmp, a, b
135 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600136 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
137 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
138 // while the later yields `-0.0`.
139 if (!Primitive::IsIntegralType(binop->GetType())) {
140 return false;
141 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100142 binop->ReplaceInput(left_neg->GetInput(), 0);
143 binop->ReplaceInput(right_neg->GetInput(), 1);
144 left_neg->GetBlock()->RemoveInstruction(left_neg);
145 right_neg->GetBlock()->RemoveInstruction(right_neg);
146 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
147 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
148 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
149 RecordSimplification();
150 return true;
151}
152
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000153void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
154 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
155 HConstant* input_cst = instruction->GetConstantRight();
156 HInstruction* input_other = instruction->GetLeastConstantLeft();
157
Mark Mendellba56d062015-05-05 21:34:03 -0400158 if (input_cst != nullptr) {
159 if (input_cst->IsZero()) {
160 // Replace code looking like
161 // SHL dst, src, 0
162 // with
163 // src
164 instruction->ReplaceWith(input_other);
165 instruction->GetBlock()->RemoveInstruction(instruction);
166 } else if (instruction->IsShl() && input_cst->IsOne()) {
167 // Replace Shl looking like
168 // SHL dst, src, 1
169 // with
170 // ADD dst, src, src
171 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
172 input_other,
173 input_other);
174 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
175 RecordSimplification();
176 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000177 }
178}
179
Calin Juravle10e244f2015-01-26 18:54:32 +0000180void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
181 HInstruction* obj = null_check->InputAt(0);
182 if (!obj->CanBeNull()) {
183 null_check->ReplaceWith(obj);
184 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000185 if (stats_ != nullptr) {
186 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
187 }
188 }
189}
190
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100191bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
192 if (!input->CanBeNull()) {
193 return true;
194 }
195
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100196 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
197 HInstruction* use = it.Current()->GetUser();
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100198 if (use->IsNullCheck() && use->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100199 return true;
200 }
201 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100202
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100203 return false;
204}
205
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100206// Returns whether doing a type test between the class of `object` against `klass` has
207// a statically known outcome. The result of the test is stored in `outcome`.
208static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000209 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
210 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
211 ScopedObjectAccess soa(Thread::Current());
212 if (!obj_rti.IsValid()) {
213 // We run the simplifier before the reference type propagation so type info might not be
214 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100215 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000216 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100217
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100218 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle98893e12015-10-02 21:05:03 +0100219 if (!class_rti.IsValid()) {
220 // Happens when the loaded class is unresolved.
221 return false;
222 }
223 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000224 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100225 *outcome = true;
226 return true;
227 } else if (obj_rti.IsExact()) {
228 // The test failed at compile time so will also fail at runtime.
229 *outcome = false;
230 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100231 } else if (!class_rti.IsInterface()
232 && !obj_rti.IsInterface()
233 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100234 // Different type hierarchy. The test will fail.
235 *outcome = false;
236 return true;
237 }
238 return false;
239}
240
241void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
242 HInstruction* object = check_cast->InputAt(0);
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100243 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100244 check_cast->ClearMustDoNullCheck();
245 }
246
247 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000248 check_cast->GetBlock()->RemoveInstruction(check_cast);
249 if (stats_ != nullptr) {
250 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
251 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100252 return;
253 }
254
255 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700256 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
257 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100258 if (outcome) {
259 check_cast->GetBlock()->RemoveInstruction(check_cast);
260 if (stats_ != nullptr) {
261 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
262 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700263 if (!load_class->HasUses()) {
264 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
265 // However, here we know that it cannot because the checkcast was successfull, hence
266 // the class was already loaded.
267 load_class->GetBlock()->RemoveInstruction(load_class);
268 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100269 } else {
270 // Don't do anything for exceptional cases for now. Ideally we should remove
271 // all instructions and blocks this instruction dominates.
272 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000273 }
274}
275
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100276void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100277 HInstruction* object = instruction->InputAt(0);
278 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100279 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100280 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100281 instruction->ClearMustDoNullCheck();
282 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100283
284 HGraph* graph = GetGraph();
285 if (object->IsNullConstant()) {
286 instruction->ReplaceWith(graph->GetIntConstant(0));
287 instruction->GetBlock()->RemoveInstruction(instruction);
288 RecordSimplification();
289 return;
290 }
291
292 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700293 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
294 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100295 if (outcome && can_be_null) {
296 // Type test will succeed, we just need a null test.
297 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
298 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
299 instruction->ReplaceWith(test);
300 } else {
301 // We've statically determined the result of the instanceof.
302 instruction->ReplaceWith(graph->GetIntConstant(outcome));
303 }
304 RecordSimplification();
305 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700306 if (outcome && !load_class->HasUses()) {
307 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
308 // However, here we know that it cannot because the instanceof check was successfull, hence
309 // the class was already loaded.
310 load_class->GetBlock()->RemoveInstruction(load_class);
311 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100312 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100313}
314
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100315void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
316 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100317 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100318 instruction->ClearValueCanBeNull();
319 }
320}
321
322void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
323 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100324 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100325 instruction->ClearValueCanBeNull();
326 }
327}
328
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000329void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100330 HBasicBlock* block = check->GetBlock();
331 // Currently always keep the suspend check at entry.
332 if (block->IsEntryBlock()) return;
333
334 // Currently always keep suspend checks at loop entry.
335 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
336 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
337 return;
338 }
339
340 // Remove the suspend check that was added at build time for the baseline
341 // compiler.
342 block->RemoveInstruction(check);
343}
344
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000345void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100346 HInstruction* input_const = equal->GetConstantRight();
347 if (input_const != nullptr) {
348 HInstruction* input_value = equal->GetLeastConstantLeft();
349 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
350 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100351 // We are comparing the boolean to a constant which is of type int and can
352 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100353 if (input_const->AsIntConstant()->IsOne()) {
354 // Replace (bool_value == true) with bool_value
355 equal->ReplaceWith(input_value);
356 block->RemoveInstruction(equal);
357 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100358 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100359 // Replace (bool_value == false) with !bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100360 block->ReplaceAndRemoveInstructionWith(
361 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
362 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100363 } else {
364 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
365 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
366 block->RemoveInstruction(equal);
367 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100368 }
Mark Mendellc4701932015-04-10 13:18:51 -0400369 } else {
370 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100371 }
Mark Mendellc4701932015-04-10 13:18:51 -0400372 } else {
373 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100374 }
375}
376
David Brazdil0d13fee2015-04-17 14:52:19 +0100377void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
378 HInstruction* input_const = not_equal->GetConstantRight();
379 if (input_const != nullptr) {
380 HInstruction* input_value = not_equal->GetLeastConstantLeft();
381 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
382 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100383 // We are comparing the boolean to a constant which is of type int and can
384 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100385 if (input_const->AsIntConstant()->IsOne()) {
386 // Replace (bool_value != true) with !bool_value
387 block->ReplaceAndRemoveInstructionWith(
388 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
389 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100390 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100391 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100392 not_equal->ReplaceWith(input_value);
393 block->RemoveInstruction(not_equal);
394 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100395 } else {
396 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
397 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
398 block->RemoveInstruction(not_equal);
399 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100400 }
Mark Mendellc4701932015-04-10 13:18:51 -0400401 } else {
402 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100403 }
Mark Mendellc4701932015-04-10 13:18:51 -0400404 } else {
405 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100406 }
407}
408
409void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
410 HInstruction* parent = bool_not->InputAt(0);
411 if (parent->IsBooleanNot()) {
412 HInstruction* value = parent->InputAt(0);
413 // Replace (!(!bool_value)) with bool_value
414 bool_not->ReplaceWith(value);
415 bool_not->GetBlock()->RemoveInstruction(bool_not);
416 // It is possible that `parent` is dead at this point but we leave
417 // its removal to DCE for simplicity.
418 RecordSimplification();
419 }
420}
421
Mingyao Yang0304e182015-01-30 16:41:29 -0800422void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
423 HInstruction* input = instruction->InputAt(0);
424 // If the array is a NewArray with constant size, replace the array length
425 // with the constant instruction. This helps the bounds check elimination phase.
426 if (input->IsNewArray()) {
427 input = input->InputAt(0);
428 if (input->IsIntConstant()) {
429 instruction->ReplaceWith(input);
430 }
431 }
432}
433
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000434void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000435 HInstruction* value = instruction->GetValue();
436 if (value->GetType() != Primitive::kPrimNot) return;
437
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100438 if (CanEnsureNotNullAt(value, instruction)) {
439 instruction->ClearValueCanBeNull();
440 }
441
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000442 if (value->IsArrayGet()) {
443 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
444 // If the code is just swapping elements in the array, no need for a type check.
445 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100446 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000447 }
448 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100449
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100450 if (value->IsNullConstant()) {
451 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100453 }
454
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100455 ScopedObjectAccess soa(Thread::Current());
456 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
457 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
458 if (!array_rti.IsValid()) {
459 return;
460 }
461
462 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
463 instruction->ClearNeedsTypeCheck();
464 return;
465 }
466
467 if (array_rti.IsObjectArray()) {
468 if (array_rti.IsExact()) {
469 instruction->ClearNeedsTypeCheck();
470 return;
471 }
472 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100473 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000474}
475
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000476void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
477 if (instruction->GetResultType() == instruction->GetInputType()) {
478 // Remove the instruction if it's converting to the same type.
479 instruction->ReplaceWith(instruction->GetInput());
480 instruction->GetBlock()->RemoveInstruction(instruction);
481 }
482}
483
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000484void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
485 HConstant* input_cst = instruction->GetConstantRight();
486 HInstruction* input_other = instruction->GetLeastConstantLeft();
487 if ((input_cst != nullptr) && input_cst->IsZero()) {
488 // Replace code looking like
489 // ADD dst, src, 0
490 // with
491 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600492 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
493 // `x` is `-0.0`, the former expression yields `0.0`, while the later
494 // yields `-0.0`.
495 if (Primitive::IsIntegralType(instruction->GetType())) {
496 instruction->ReplaceWith(input_other);
497 instruction->GetBlock()->RemoveInstruction(instruction);
498 return;
499 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100500 }
501
502 HInstruction* left = instruction->GetLeft();
503 HInstruction* right = instruction->GetRight();
504 bool left_is_neg = left->IsNeg();
505 bool right_is_neg = right->IsNeg();
506
507 if (left_is_neg && right_is_neg) {
508 if (TryMoveNegOnInputsAfterBinop(instruction)) {
509 return;
510 }
511 }
512
513 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
514 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
515 // Replace code looking like
516 // NEG tmp, b
517 // ADD dst, a, tmp
518 // with
519 // SUB dst, a, b
520 // We do not perform the optimization if the input negation has environment
521 // uses or multiple non-environment uses as it could lead to worse code. In
522 // particular, we do not want the live range of `b` to be extended if we are
523 // not sure the initial 'NEG' instruction can be removed.
524 HInstruction* other = left_is_neg ? right : left;
525 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
526 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
527 RecordSimplification();
528 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000529 }
530}
531
532void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
533 HConstant* input_cst = instruction->GetConstantRight();
534 HInstruction* input_other = instruction->GetLeastConstantLeft();
535
Vladimir Marko452c1b62015-09-25 14:44:17 +0100536 if (input_cst != nullptr) {
537 int64_t value = Int64FromConstant(input_cst);
538 if (value == -1) {
539 // Replace code looking like
540 // AND dst, src, 0xFFF...FF
541 // with
542 // src
543 instruction->ReplaceWith(input_other);
544 instruction->GetBlock()->RemoveInstruction(instruction);
545 RecordSimplification();
546 return;
547 }
548 // Eliminate And from UShr+And if the And-mask contains all the bits that
549 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
550 // precisely clears the shifted-in sign bits.
551 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
552 size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32;
553 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
554 size_t num_tail_bits_set = CTZ(value + 1);
555 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
556 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
557 instruction->ReplaceWith(input_other);
558 instruction->GetBlock()->RemoveInstruction(instruction);
559 RecordSimplification();
560 return;
561 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
562 input_other->HasOnlyOneNonEnvironmentUse()) {
563 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
564 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
565 HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(),
566 input_other->InputAt(0),
567 input_other->InputAt(1),
568 input_other->GetDexPc());
569 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
570 input_other->GetBlock()->RemoveInstruction(input_other);
571 RecordSimplification();
572 return;
573 }
574 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000575 }
576
577 // We assume that GVN has run before, so we only perform a pointer comparison.
578 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100579 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000580 if (instruction->GetLeft() == instruction->GetRight()) {
581 // Replace code looking like
582 // AND dst, src, src
583 // with
584 // src
585 instruction->ReplaceWith(instruction->GetLeft());
586 instruction->GetBlock()->RemoveInstruction(instruction);
587 }
588}
589
Mark Mendellc4701932015-04-10 13:18:51 -0400590void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
591 VisitCondition(condition);
592}
593
594void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
595 VisitCondition(condition);
596}
597
598void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
599 VisitCondition(condition);
600}
601
602void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
603 VisitCondition(condition);
604}
605
606void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
607 // Try to fold an HCompare into this HCondition.
608
Roland Levillain7f63c522015-07-13 15:54:55 +0000609 // This simplification is currently supported on x86, x86_64, ARM and ARM64.
610 // TODO: Implement it for MIPS64.
Mark Mendellc4701932015-04-10 13:18:51 -0400611 InstructionSet instruction_set = GetGraph()->GetInstructionSet();
Roland Levillain7f63c522015-07-13 15:54:55 +0000612 if (instruction_set == kMips64) {
Mark Mendellc4701932015-04-10 13:18:51 -0400613 return;
614 }
615
616 HInstruction* left = condition->GetLeft();
617 HInstruction* right = condition->GetRight();
618 // We can only replace an HCondition which compares a Compare to 0.
619 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
620 // condition with a long, float or double comparison as input.
621 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
622 // Conversion is not possible.
623 return;
624 }
625
626 // Is the Compare only used for this purpose?
627 if (!left->GetUses().HasOnlyOneUse()) {
628 // Someone else also wants the result of the compare.
629 return;
630 }
631
632 if (!left->GetEnvUses().IsEmpty()) {
633 // There is a reference to the compare result in an environment. Do we really need it?
634 if (GetGraph()->IsDebuggable()) {
635 return;
636 }
637
638 // We have to ensure that there are no deopt points in the sequence.
639 if (left->HasAnyEnvironmentUseBefore(condition)) {
640 return;
641 }
642 }
643
644 // Clean up any environment uses from the HCompare, if any.
645 left->RemoveEnvironmentUsers();
646
647 // We have decided to fold the HCompare into the HCondition. Transfer the information.
648 condition->SetBias(left->AsCompare()->GetBias());
649
650 // Replace the operands of the HCondition.
651 condition->ReplaceInput(left->InputAt(0), 0);
652 condition->ReplaceInput(left->InputAt(1), 1);
653
654 // Remove the HCompare.
655 left->GetBlock()->RemoveInstruction(left);
656
657 RecordSimplification();
658}
659
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000660void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
661 HConstant* input_cst = instruction->GetConstantRight();
662 HInstruction* input_other = instruction->GetLeastConstantLeft();
663 Primitive::Type type = instruction->GetType();
664
665 if ((input_cst != nullptr) && input_cst->IsOne()) {
666 // Replace code looking like
667 // DIV dst, src, 1
668 // with
669 // src
670 instruction->ReplaceWith(input_other);
671 instruction->GetBlock()->RemoveInstruction(instruction);
672 return;
673 }
674
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000675 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000676 // Replace code looking like
677 // DIV dst, src, -1
678 // with
679 // NEG dst, src
680 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000681 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100682 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000683 return;
684 }
685
686 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
687 // Try replacing code looking like
688 // DIV dst, src, constant
689 // with
690 // MUL dst, src, 1 / constant
691 HConstant* reciprocal = nullptr;
692 if (type == Primitive::Primitive::kPrimDouble) {
693 double value = input_cst->AsDoubleConstant()->GetValue();
694 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
695 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
696 }
697 } else {
698 DCHECK_EQ(type, Primitive::kPrimFloat);
699 float value = input_cst->AsFloatConstant()->GetValue();
700 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
701 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
702 }
703 }
704
705 if (reciprocal != nullptr) {
706 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
707 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
708 RecordSimplification();
709 return;
710 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000711 }
712}
713
714void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
715 HConstant* input_cst = instruction->GetConstantRight();
716 HInstruction* input_other = instruction->GetLeastConstantLeft();
717 Primitive::Type type = instruction->GetType();
718 HBasicBlock* block = instruction->GetBlock();
719 ArenaAllocator* allocator = GetGraph()->GetArena();
720
721 if (input_cst == nullptr) {
722 return;
723 }
724
725 if (input_cst->IsOne()) {
726 // Replace code looking like
727 // MUL dst, src, 1
728 // with
729 // src
730 instruction->ReplaceWith(input_other);
731 instruction->GetBlock()->RemoveInstruction(instruction);
732 return;
733 }
734
735 if (input_cst->IsMinusOne() &&
736 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
737 // Replace code looking like
738 // MUL dst, src, -1
739 // with
740 // NEG dst, src
741 HNeg* neg = new (allocator) HNeg(type, input_other);
742 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100743 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000744 return;
745 }
746
747 if (Primitive::IsFloatingPointType(type) &&
748 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
749 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
750 // Replace code looking like
751 // FP_MUL dst, src, 2.0
752 // with
753 // FP_ADD dst, src, src
754 // The 'int' and 'long' cases are handled below.
755 block->ReplaceAndRemoveInstructionWith(instruction,
756 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100757 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000758 return;
759 }
760
761 if (Primitive::IsIntOrLongType(type)) {
762 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600763 // Even though constant propagation also takes care of the zero case, other
764 // optimizations can lead to having a zero multiplication.
765 if (factor == 0) {
766 // Replace code looking like
767 // MUL dst, src, 0
768 // with
769 // 0
770 instruction->ReplaceWith(input_cst);
771 instruction->GetBlock()->RemoveInstruction(instruction);
772 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000773 // Replace code looking like
774 // MUL dst, src, pow_of_2
775 // with
776 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000777 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000778 HShl* shl = new(allocator) HShl(type, input_other, shift);
779 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100780 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000781 }
782 }
783}
784
Alexandre Rames188d4312015-04-09 18:30:21 +0100785void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
786 HInstruction* input = instruction->GetInput();
787 if (input->IsNeg()) {
788 // Replace code looking like
789 // NEG tmp, src
790 // NEG dst, tmp
791 // with
792 // src
793 HNeg* previous_neg = input->AsNeg();
794 instruction->ReplaceWith(previous_neg->GetInput());
795 instruction->GetBlock()->RemoveInstruction(instruction);
796 // We perform the optimization even if the input negation has environment
797 // uses since it allows removing the current instruction. But we only delete
798 // the input negation only if it is does not have any uses left.
799 if (!previous_neg->HasUses()) {
800 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
801 }
802 RecordSimplification();
803 return;
804 }
805
Serguei Katkov339dfc22015-04-20 12:29:32 +0600806 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
807 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100808 // Replace code looking like
809 // SUB tmp, a, b
810 // NEG dst, tmp
811 // with
812 // SUB dst, b, a
813 // We do not perform the optimization if the input subtraction has
814 // environment uses or multiple non-environment uses as it could lead to
815 // worse code. In particular, we do not want the live ranges of `a` and `b`
816 // to be extended if we are not sure the initial 'SUB' instruction can be
817 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600818 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100819 HSub* sub = input->AsSub();
820 HSub* new_sub =
821 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
822 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
823 if (!sub->HasUses()) {
824 sub->GetBlock()->RemoveInstruction(sub);
825 }
826 RecordSimplification();
827 }
828}
829
830void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
831 HInstruction* input = instruction->GetInput();
832 if (input->IsNot()) {
833 // Replace code looking like
834 // NOT tmp, src
835 // NOT dst, tmp
836 // with
837 // src
838 // We perform the optimization even if the input negation has environment
839 // uses since it allows removing the current instruction. But we only delete
840 // the input negation only if it is does not have any uses left.
841 HNot* previous_not = input->AsNot();
842 instruction->ReplaceWith(previous_not->GetInput());
843 instruction->GetBlock()->RemoveInstruction(instruction);
844 if (!previous_not->HasUses()) {
845 previous_not->GetBlock()->RemoveInstruction(previous_not);
846 }
847 RecordSimplification();
848 }
849}
850
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000851void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
852 HConstant* input_cst = instruction->GetConstantRight();
853 HInstruction* input_other = instruction->GetLeastConstantLeft();
854
855 if ((input_cst != nullptr) && input_cst->IsZero()) {
856 // Replace code looking like
857 // OR dst, src, 0
858 // with
859 // src
860 instruction->ReplaceWith(input_other);
861 instruction->GetBlock()->RemoveInstruction(instruction);
862 return;
863 }
864
865 // We assume that GVN has run before, so we only perform a pointer comparison.
866 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100867 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000868 if (instruction->GetLeft() == instruction->GetRight()) {
869 // Replace code looking like
870 // OR dst, src, src
871 // with
872 // src
873 instruction->ReplaceWith(instruction->GetLeft());
874 instruction->GetBlock()->RemoveInstruction(instruction);
875 }
876}
877
878void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
879 VisitShift(instruction);
880}
881
882void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
883 VisitShift(instruction);
884}
885
886void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
887 HConstant* input_cst = instruction->GetConstantRight();
888 HInstruction* input_other = instruction->GetLeastConstantLeft();
889
Serguei Katkov115b53f2015-08-05 17:03:30 +0600890 Primitive::Type type = instruction->GetType();
891 if (Primitive::IsFloatingPointType(type)) {
892 return;
893 }
894
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000895 if ((input_cst != nullptr) && input_cst->IsZero()) {
896 // Replace code looking like
897 // SUB dst, src, 0
898 // with
899 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600900 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
901 // `x` is `-0.0`, the former expression yields `0.0`, while the later
902 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000903 instruction->ReplaceWith(input_other);
904 instruction->GetBlock()->RemoveInstruction(instruction);
905 return;
906 }
907
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000908 HBasicBlock* block = instruction->GetBlock();
909 ArenaAllocator* allocator = GetGraph()->GetArena();
910
Alexandre Rames188d4312015-04-09 18:30:21 +0100911 HInstruction* left = instruction->GetLeft();
912 HInstruction* right = instruction->GetRight();
913 if (left->IsConstant()) {
914 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000915 // Replace code looking like
916 // SUB dst, 0, src
917 // with
918 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100919 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000920 // `x` is `0.0`, the former expression yields `0.0`, while the later
921 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100922 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000923 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100924 RecordSimplification();
925 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000926 }
927 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100928
929 if (left->IsNeg() && right->IsNeg()) {
930 if (TryMoveNegOnInputsAfterBinop(instruction)) {
931 return;
932 }
933 }
934
935 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
936 // Replace code looking like
937 // NEG tmp, b
938 // SUB dst, a, tmp
939 // with
940 // ADD dst, a, b
941 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
942 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
943 RecordSimplification();
944 right->GetBlock()->RemoveInstruction(right);
945 return;
946 }
947
948 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
949 // Replace code looking like
950 // NEG tmp, a
951 // SUB dst, tmp, b
952 // with
953 // ADD tmp, a, b
954 // NEG dst, tmp
955 // The second version is not intrinsically better, but enables more
956 // transformations.
957 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
958 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
959 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
960 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
961 instruction->ReplaceWith(neg);
962 instruction->GetBlock()->RemoveInstruction(instruction);
963 RecordSimplification();
964 left->GetBlock()->RemoveInstruction(left);
965 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000966}
967
968void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
969 VisitShift(instruction);
970}
971
972void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
973 HConstant* input_cst = instruction->GetConstantRight();
974 HInstruction* input_other = instruction->GetLeastConstantLeft();
975
976 if ((input_cst != nullptr) && input_cst->IsZero()) {
977 // Replace code looking like
978 // XOR dst, src, 0
979 // with
980 // src
981 instruction->ReplaceWith(input_other);
982 instruction->GetBlock()->RemoveInstruction(instruction);
983 return;
984 }
985
986 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
987 // Replace code looking like
988 // XOR dst, src, 0xFFF...FF
989 // with
990 // NOT dst, src
991 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
992 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100993 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000994 return;
995 }
996}
997
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100998void InstructionSimplifierVisitor::VisitFakeString(HFakeString* instruction) {
999 HInstruction* actual_string = nullptr;
1000
1001 // Find the string we need to replace this instruction with. The actual string is
1002 // the return value of a StringFactory call.
1003 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
1004 HInstruction* use = it.Current()->GetUser();
1005 if (use->IsInvokeStaticOrDirect()
1006 && use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction)) {
1007 use->AsInvokeStaticOrDirect()->RemoveFakeStringArgumentAsLastInput();
1008 actual_string = use;
1009 break;
1010 }
1011 }
1012
1013 // Check that there is no other instruction that thinks it is the factory for that string.
1014 if (kIsDebugBuild) {
1015 CHECK(actual_string != nullptr);
1016 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
1017 HInstruction* use = it.Current()->GetUser();
1018 if (use->IsInvokeStaticOrDirect()) {
1019 CHECK(!use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction));
1020 }
1021 }
1022 }
1023
1024 // We need to remove any environment uses of the fake string that are not dominated by
1025 // `actual_string` to null.
1026 for (HUseIterator<HEnvironment*> it(instruction->GetEnvUses()); !it.Done(); it.Advance()) {
1027 HEnvironment* environment = it.Current()->GetUser();
1028 if (!actual_string->StrictlyDominates(environment->GetHolder())) {
1029 environment->RemoveAsUserOfInput(it.Current()->GetIndex());
1030 environment->SetRawEnvAt(it.Current()->GetIndex(), nullptr);
1031 }
1032 }
1033
1034 // Only uses dominated by `actual_string` must remain. We can safely replace and remove
1035 // `instruction`.
1036 instruction->ReplaceWith(actual_string);
1037 instruction->GetBlock()->RemoveInstruction(instruction);
1038}
1039
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001040} // namespace art