blob: 0ac26de67422779bf6a65ecc028cf61109676f32 [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;
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +010074 bool IsDominatedByInputNullCheck(HInstruction* instr);
Calin Juravleacf735c2015-02-12 15:25:22 +000075
76 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +010077 bool simplification_occurred_ = false;
78 int simplifications_at_current_position_ = 0;
79 // We ensure we do not loop infinitely. The value is a finger in the air guess
80 // that should allow enough simplification.
81 static constexpr int kMaxSamePositionSimplifications = 10;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000082};
83
Nicolas Geoffray3c049742014-09-24 18:10:46 +010084void InstructionSimplifier::Run() {
Calin Juravleacf735c2015-02-12 15:25:22 +000085 InstructionSimplifierVisitor visitor(graph_, stats_);
Alexandre Rames188d4312015-04-09 18:30:21 +010086 visitor.Run();
87}
88
89void InstructionSimplifierVisitor::Run() {
Nicolas Geoffray07276db2015-05-18 14:22:09 +010090 // Iterate in reverse post order to open up more simplifications to users
91 // of instructions that got simplified.
Alexandre Rames188d4312015-04-09 18:30:21 +010092 for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) {
93 // The simplification of an instruction to another instruction may yield
94 // possibilities for other simplifications. So although we perform a reverse
95 // post order visit, we sometimes need to revisit an instruction index.
96 simplification_occurred_ = false;
97 VisitBasicBlock(it.Current());
98 if (simplification_occurred_ &&
99 (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) {
100 // New simplifications may be applicable to the instruction at the
101 // current index, so don't advance the iterator.
102 continue;
103 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100104 simplifications_at_current_position_ = 0;
105 it.Advance();
106 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100107}
108
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000109namespace {
110
111bool AreAllBitsSet(HConstant* constant) {
112 return Int64FromConstant(constant) == -1;
113}
114
115} // namespace
116
Alexandre Rames188d4312015-04-09 18:30:21 +0100117// Returns true if the code was simplified to use only one negation operation
118// after the binary operation instead of one on each of the inputs.
119bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
120 DCHECK(binop->IsAdd() || binop->IsSub());
121 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
122 HNeg* left_neg = binop->GetLeft()->AsNeg();
123 HNeg* right_neg = binop->GetRight()->AsNeg();
124 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
125 !right_neg->HasOnlyOneNonEnvironmentUse()) {
126 return false;
127 }
128 // Replace code looking like
129 // NEG tmp1, a
130 // NEG tmp2, b
131 // ADD dst, tmp1, tmp2
132 // with
133 // ADD tmp, a, b
134 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600135 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
136 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
137 // while the later yields `-0.0`.
138 if (!Primitive::IsIntegralType(binop->GetType())) {
139 return false;
140 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100141 binop->ReplaceInput(left_neg->GetInput(), 0);
142 binop->ReplaceInput(right_neg->GetInput(), 1);
143 left_neg->GetBlock()->RemoveInstruction(left_neg);
144 right_neg->GetBlock()->RemoveInstruction(right_neg);
145 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
146 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
147 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
148 RecordSimplification();
149 return true;
150}
151
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000152void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
153 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
154 HConstant* input_cst = instruction->GetConstantRight();
155 HInstruction* input_other = instruction->GetLeastConstantLeft();
156
Mark Mendellba56d062015-05-05 21:34:03 -0400157 if (input_cst != nullptr) {
158 if (input_cst->IsZero()) {
159 // Replace code looking like
160 // SHL dst, src, 0
161 // with
162 // src
163 instruction->ReplaceWith(input_other);
164 instruction->GetBlock()->RemoveInstruction(instruction);
165 } else if (instruction->IsShl() && input_cst->IsOne()) {
166 // Replace Shl looking like
167 // SHL dst, src, 1
168 // with
169 // ADD dst, src, src
170 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
171 input_other,
172 input_other);
173 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
174 RecordSimplification();
175 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000176 }
177}
178
Calin Juravle10e244f2015-01-26 18:54:32 +0000179void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
180 HInstruction* obj = null_check->InputAt(0);
181 if (!obj->CanBeNull()) {
182 null_check->ReplaceWith(obj);
183 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000184 if (stats_ != nullptr) {
185 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
186 }
187 }
188}
189
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100190bool InstructionSimplifierVisitor::IsDominatedByInputNullCheck(HInstruction* instr) {
191 HInstruction* input = instr->InputAt(0);
192 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
193 HInstruction* use = it.Current()->GetUser();
194 if (use->IsNullCheck() && use->StrictlyDominates(instr)) {
195 return true;
196 }
197 }
198 return false;
199}
200
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100201// Returns whether doing a type test between the class of `object` against `klass` has
202// a statically known outcome. The result of the test is stored in `outcome`.
203static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000204 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
205 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
206 ScopedObjectAccess soa(Thread::Current());
207 if (!obj_rti.IsValid()) {
208 // We run the simplifier before the reference type propagation so type info might not be
209 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100210 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000211 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100212
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100213 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle2e768302015-07-28 14:41:11 +0000214 DCHECK(class_rti.IsValid() && class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000215 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100216 *outcome = true;
217 return true;
218 } else if (obj_rti.IsExact()) {
219 // The test failed at compile time so will also fail at runtime.
220 *outcome = false;
221 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100222 } else if (!class_rti.IsInterface()
223 && !obj_rti.IsInterface()
224 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100225 // Different type hierarchy. The test will fail.
226 *outcome = false;
227 return true;
228 }
229 return false;
230}
231
232void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
233 HInstruction* object = check_cast->InputAt(0);
234 if (!object->CanBeNull() || IsDominatedByInputNullCheck(check_cast)) {
235 check_cast->ClearMustDoNullCheck();
236 }
237
238 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000239 check_cast->GetBlock()->RemoveInstruction(check_cast);
240 if (stats_ != nullptr) {
241 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
242 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100243 return;
244 }
245
246 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700247 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
248 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100249 if (outcome) {
250 check_cast->GetBlock()->RemoveInstruction(check_cast);
251 if (stats_ != nullptr) {
252 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
253 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700254 if (!load_class->HasUses()) {
255 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
256 // However, here we know that it cannot because the checkcast was successfull, hence
257 // the class was already loaded.
258 load_class->GetBlock()->RemoveInstruction(load_class);
259 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100260 } else {
261 // Don't do anything for exceptional cases for now. Ideally we should remove
262 // all instructions and blocks this instruction dominates.
263 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000264 }
265}
266
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100267void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100268 HInstruction* object = instruction->InputAt(0);
269 bool can_be_null = true;
270 if (!object->CanBeNull() || IsDominatedByInputNullCheck(instruction)) {
271 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100272 instruction->ClearMustDoNullCheck();
273 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100274
275 HGraph* graph = GetGraph();
276 if (object->IsNullConstant()) {
277 instruction->ReplaceWith(graph->GetIntConstant(0));
278 instruction->GetBlock()->RemoveInstruction(instruction);
279 RecordSimplification();
280 return;
281 }
282
283 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700284 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
285 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100286 if (outcome && can_be_null) {
287 // Type test will succeed, we just need a null test.
288 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
289 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
290 instruction->ReplaceWith(test);
291 } else {
292 // We've statically determined the result of the instanceof.
293 instruction->ReplaceWith(graph->GetIntConstant(outcome));
294 }
295 RecordSimplification();
296 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700297 if (outcome && !load_class->HasUses()) {
298 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
299 // However, here we know that it cannot because the instanceof check was successfull, hence
300 // the class was already loaded.
301 load_class->GetBlock()->RemoveInstruction(load_class);
302 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100303 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100304}
305
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100306void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
307 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
308 && !instruction->GetValue()->CanBeNull()) {
309 instruction->ClearValueCanBeNull();
310 }
311}
312
313void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
314 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
315 && !instruction->GetValue()->CanBeNull()) {
316 instruction->ClearValueCanBeNull();
317 }
318}
319
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000320void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100321 HBasicBlock* block = check->GetBlock();
322 // Currently always keep the suspend check at entry.
323 if (block->IsEntryBlock()) return;
324
325 // Currently always keep suspend checks at loop entry.
326 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
327 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
328 return;
329 }
330
331 // Remove the suspend check that was added at build time for the baseline
332 // compiler.
333 block->RemoveInstruction(check);
334}
335
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000336void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100337 HInstruction* input_const = equal->GetConstantRight();
338 if (input_const != nullptr) {
339 HInstruction* input_value = equal->GetLeastConstantLeft();
340 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
341 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100342 // We are comparing the boolean to a constant which is of type int and can
343 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100344 if (input_const->AsIntConstant()->IsOne()) {
345 // Replace (bool_value == true) with bool_value
346 equal->ReplaceWith(input_value);
347 block->RemoveInstruction(equal);
348 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100349 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100350 // Replace (bool_value == false) with !bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100351 block->ReplaceAndRemoveInstructionWith(
352 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
353 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100354 } else {
355 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
356 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
357 block->RemoveInstruction(equal);
358 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100359 }
Mark Mendellc4701932015-04-10 13:18:51 -0400360 } else {
361 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100362 }
Mark Mendellc4701932015-04-10 13:18:51 -0400363 } else {
364 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100365 }
366}
367
David Brazdil0d13fee2015-04-17 14:52:19 +0100368void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
369 HInstruction* input_const = not_equal->GetConstantRight();
370 if (input_const != nullptr) {
371 HInstruction* input_value = not_equal->GetLeastConstantLeft();
372 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
373 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100374 // We are comparing the boolean to a constant which is of type int and can
375 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100376 if (input_const->AsIntConstant()->IsOne()) {
377 // Replace (bool_value != true) with !bool_value
378 block->ReplaceAndRemoveInstructionWith(
379 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
380 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100381 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100382 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100383 not_equal->ReplaceWith(input_value);
384 block->RemoveInstruction(not_equal);
385 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100386 } else {
387 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
388 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
389 block->RemoveInstruction(not_equal);
390 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100391 }
Mark Mendellc4701932015-04-10 13:18:51 -0400392 } else {
393 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100394 }
Mark Mendellc4701932015-04-10 13:18:51 -0400395 } else {
396 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100397 }
398}
399
400void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
401 HInstruction* parent = bool_not->InputAt(0);
402 if (parent->IsBooleanNot()) {
403 HInstruction* value = parent->InputAt(0);
404 // Replace (!(!bool_value)) with bool_value
405 bool_not->ReplaceWith(value);
406 bool_not->GetBlock()->RemoveInstruction(bool_not);
407 // It is possible that `parent` is dead at this point but we leave
408 // its removal to DCE for simplicity.
409 RecordSimplification();
410 }
411}
412
Mingyao Yang0304e182015-01-30 16:41:29 -0800413void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
414 HInstruction* input = instruction->InputAt(0);
415 // If the array is a NewArray with constant size, replace the array length
416 // with the constant instruction. This helps the bounds check elimination phase.
417 if (input->IsNewArray()) {
418 input = input->InputAt(0);
419 if (input->IsIntConstant()) {
420 instruction->ReplaceWith(input);
421 }
422 }
423}
424
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000425void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000426 HInstruction* value = instruction->GetValue();
427 if (value->GetType() != Primitive::kPrimNot) return;
428
429 if (value->IsArrayGet()) {
430 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
431 // If the code is just swapping elements in the array, no need for a type check.
432 instruction->ClearNeedsTypeCheck();
433 }
434 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100435
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100436 if (value->IsNullConstant()) {
437 instruction->ClearNeedsTypeCheck();
438 }
439
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100440 if (!value->CanBeNull()) {
441 instruction->ClearValueCanBeNull();
442 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000443}
444
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000445void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
446 if (instruction->GetResultType() == instruction->GetInputType()) {
447 // Remove the instruction if it's converting to the same type.
448 instruction->ReplaceWith(instruction->GetInput());
449 instruction->GetBlock()->RemoveInstruction(instruction);
450 }
451}
452
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000453void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
454 HConstant* input_cst = instruction->GetConstantRight();
455 HInstruction* input_other = instruction->GetLeastConstantLeft();
456 if ((input_cst != nullptr) && input_cst->IsZero()) {
457 // Replace code looking like
458 // ADD dst, src, 0
459 // with
460 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600461 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
462 // `x` is `-0.0`, the former expression yields `0.0`, while the later
463 // yields `-0.0`.
464 if (Primitive::IsIntegralType(instruction->GetType())) {
465 instruction->ReplaceWith(input_other);
466 instruction->GetBlock()->RemoveInstruction(instruction);
467 return;
468 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100469 }
470
471 HInstruction* left = instruction->GetLeft();
472 HInstruction* right = instruction->GetRight();
473 bool left_is_neg = left->IsNeg();
474 bool right_is_neg = right->IsNeg();
475
476 if (left_is_neg && right_is_neg) {
477 if (TryMoveNegOnInputsAfterBinop(instruction)) {
478 return;
479 }
480 }
481
482 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
483 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
484 // Replace code looking like
485 // NEG tmp, b
486 // ADD dst, a, tmp
487 // with
488 // SUB dst, a, b
489 // We do not perform the optimization if the input negation has environment
490 // uses or multiple non-environment uses as it could lead to worse code. In
491 // particular, we do not want the live range of `b` to be extended if we are
492 // not sure the initial 'NEG' instruction can be removed.
493 HInstruction* other = left_is_neg ? right : left;
494 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
495 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
496 RecordSimplification();
497 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000498 }
499}
500
501void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
502 HConstant* input_cst = instruction->GetConstantRight();
503 HInstruction* input_other = instruction->GetLeastConstantLeft();
504
505 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
506 // Replace code looking like
507 // AND dst, src, 0xFFF...FF
508 // with
509 // src
510 instruction->ReplaceWith(input_other);
511 instruction->GetBlock()->RemoveInstruction(instruction);
512 return;
513 }
514
515 // We assume that GVN has run before, so we only perform a pointer comparison.
516 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100517 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000518 if (instruction->GetLeft() == instruction->GetRight()) {
519 // Replace code looking like
520 // AND dst, src, src
521 // with
522 // src
523 instruction->ReplaceWith(instruction->GetLeft());
524 instruction->GetBlock()->RemoveInstruction(instruction);
525 }
526}
527
Mark Mendellc4701932015-04-10 13:18:51 -0400528void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
529 VisitCondition(condition);
530}
531
532void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
533 VisitCondition(condition);
534}
535
536void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
537 VisitCondition(condition);
538}
539
540void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
541 VisitCondition(condition);
542}
543
544void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
545 // Try to fold an HCompare into this HCondition.
546
Roland Levillain7f63c522015-07-13 15:54:55 +0000547 // This simplification is currently supported on x86, x86_64, ARM and ARM64.
548 // TODO: Implement it for MIPS64.
Mark Mendellc4701932015-04-10 13:18:51 -0400549 InstructionSet instruction_set = GetGraph()->GetInstructionSet();
Roland Levillain7f63c522015-07-13 15:54:55 +0000550 if (instruction_set == kMips64) {
Mark Mendellc4701932015-04-10 13:18:51 -0400551 return;
552 }
553
554 HInstruction* left = condition->GetLeft();
555 HInstruction* right = condition->GetRight();
556 // We can only replace an HCondition which compares a Compare to 0.
557 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
558 // condition with a long, float or double comparison as input.
559 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
560 // Conversion is not possible.
561 return;
562 }
563
564 // Is the Compare only used for this purpose?
565 if (!left->GetUses().HasOnlyOneUse()) {
566 // Someone else also wants the result of the compare.
567 return;
568 }
569
570 if (!left->GetEnvUses().IsEmpty()) {
571 // There is a reference to the compare result in an environment. Do we really need it?
572 if (GetGraph()->IsDebuggable()) {
573 return;
574 }
575
576 // We have to ensure that there are no deopt points in the sequence.
577 if (left->HasAnyEnvironmentUseBefore(condition)) {
578 return;
579 }
580 }
581
582 // Clean up any environment uses from the HCompare, if any.
583 left->RemoveEnvironmentUsers();
584
585 // We have decided to fold the HCompare into the HCondition. Transfer the information.
586 condition->SetBias(left->AsCompare()->GetBias());
587
588 // Replace the operands of the HCondition.
589 condition->ReplaceInput(left->InputAt(0), 0);
590 condition->ReplaceInput(left->InputAt(1), 1);
591
592 // Remove the HCompare.
593 left->GetBlock()->RemoveInstruction(left);
594
595 RecordSimplification();
596}
597
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000598void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
599 HConstant* input_cst = instruction->GetConstantRight();
600 HInstruction* input_other = instruction->GetLeastConstantLeft();
601 Primitive::Type type = instruction->GetType();
602
603 if ((input_cst != nullptr) && input_cst->IsOne()) {
604 // Replace code looking like
605 // DIV dst, src, 1
606 // with
607 // src
608 instruction->ReplaceWith(input_other);
609 instruction->GetBlock()->RemoveInstruction(instruction);
610 return;
611 }
612
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000613 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000614 // Replace code looking like
615 // DIV dst, src, -1
616 // with
617 // NEG dst, src
618 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000619 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100620 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000621 return;
622 }
623
624 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
625 // Try replacing code looking like
626 // DIV dst, src, constant
627 // with
628 // MUL dst, src, 1 / constant
629 HConstant* reciprocal = nullptr;
630 if (type == Primitive::Primitive::kPrimDouble) {
631 double value = input_cst->AsDoubleConstant()->GetValue();
632 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
633 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
634 }
635 } else {
636 DCHECK_EQ(type, Primitive::kPrimFloat);
637 float value = input_cst->AsFloatConstant()->GetValue();
638 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
639 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
640 }
641 }
642
643 if (reciprocal != nullptr) {
644 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
645 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
646 RecordSimplification();
647 return;
648 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000649 }
650}
651
652void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
653 HConstant* input_cst = instruction->GetConstantRight();
654 HInstruction* input_other = instruction->GetLeastConstantLeft();
655 Primitive::Type type = instruction->GetType();
656 HBasicBlock* block = instruction->GetBlock();
657 ArenaAllocator* allocator = GetGraph()->GetArena();
658
659 if (input_cst == nullptr) {
660 return;
661 }
662
663 if (input_cst->IsOne()) {
664 // Replace code looking like
665 // MUL dst, src, 1
666 // with
667 // src
668 instruction->ReplaceWith(input_other);
669 instruction->GetBlock()->RemoveInstruction(instruction);
670 return;
671 }
672
673 if (input_cst->IsMinusOne() &&
674 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
675 // Replace code looking like
676 // MUL dst, src, -1
677 // with
678 // NEG dst, src
679 HNeg* neg = new (allocator) HNeg(type, input_other);
680 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100681 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000682 return;
683 }
684
685 if (Primitive::IsFloatingPointType(type) &&
686 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
687 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
688 // Replace code looking like
689 // FP_MUL dst, src, 2.0
690 // with
691 // FP_ADD dst, src, src
692 // The 'int' and 'long' cases are handled below.
693 block->ReplaceAndRemoveInstructionWith(instruction,
694 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100695 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000696 return;
697 }
698
699 if (Primitive::IsIntOrLongType(type)) {
700 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600701 // Even though constant propagation also takes care of the zero case, other
702 // optimizations can lead to having a zero multiplication.
703 if (factor == 0) {
704 // Replace code looking like
705 // MUL dst, src, 0
706 // with
707 // 0
708 instruction->ReplaceWith(input_cst);
709 instruction->GetBlock()->RemoveInstruction(instruction);
710 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000711 // Replace code looking like
712 // MUL dst, src, pow_of_2
713 // with
714 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000715 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000716 HShl* shl = new(allocator) HShl(type, input_other, shift);
717 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100718 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000719 }
720 }
721}
722
Alexandre Rames188d4312015-04-09 18:30:21 +0100723void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
724 HInstruction* input = instruction->GetInput();
725 if (input->IsNeg()) {
726 // Replace code looking like
727 // NEG tmp, src
728 // NEG dst, tmp
729 // with
730 // src
731 HNeg* previous_neg = input->AsNeg();
732 instruction->ReplaceWith(previous_neg->GetInput());
733 instruction->GetBlock()->RemoveInstruction(instruction);
734 // We perform the optimization even if the input negation has environment
735 // uses since it allows removing the current instruction. But we only delete
736 // the input negation only if it is does not have any uses left.
737 if (!previous_neg->HasUses()) {
738 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
739 }
740 RecordSimplification();
741 return;
742 }
743
Serguei Katkov339dfc22015-04-20 12:29:32 +0600744 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
745 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100746 // Replace code looking like
747 // SUB tmp, a, b
748 // NEG dst, tmp
749 // with
750 // SUB dst, b, a
751 // We do not perform the optimization if the input subtraction has
752 // environment uses or multiple non-environment uses as it could lead to
753 // worse code. In particular, we do not want the live ranges of `a` and `b`
754 // to be extended if we are not sure the initial 'SUB' instruction can be
755 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600756 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100757 HSub* sub = input->AsSub();
758 HSub* new_sub =
759 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
760 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
761 if (!sub->HasUses()) {
762 sub->GetBlock()->RemoveInstruction(sub);
763 }
764 RecordSimplification();
765 }
766}
767
768void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
769 HInstruction* input = instruction->GetInput();
770 if (input->IsNot()) {
771 // Replace code looking like
772 // NOT tmp, src
773 // NOT dst, tmp
774 // with
775 // src
776 // We perform the optimization even if the input negation has environment
777 // uses since it allows removing the current instruction. But we only delete
778 // the input negation only if it is does not have any uses left.
779 HNot* previous_not = input->AsNot();
780 instruction->ReplaceWith(previous_not->GetInput());
781 instruction->GetBlock()->RemoveInstruction(instruction);
782 if (!previous_not->HasUses()) {
783 previous_not->GetBlock()->RemoveInstruction(previous_not);
784 }
785 RecordSimplification();
786 }
787}
788
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000789void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
790 HConstant* input_cst = instruction->GetConstantRight();
791 HInstruction* input_other = instruction->GetLeastConstantLeft();
792
793 if ((input_cst != nullptr) && input_cst->IsZero()) {
794 // Replace code looking like
795 // OR dst, src, 0
796 // with
797 // src
798 instruction->ReplaceWith(input_other);
799 instruction->GetBlock()->RemoveInstruction(instruction);
800 return;
801 }
802
803 // We assume that GVN has run before, so we only perform a pointer comparison.
804 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100805 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000806 if (instruction->GetLeft() == instruction->GetRight()) {
807 // Replace code looking like
808 // OR dst, src, src
809 // with
810 // src
811 instruction->ReplaceWith(instruction->GetLeft());
812 instruction->GetBlock()->RemoveInstruction(instruction);
813 }
814}
815
816void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
817 VisitShift(instruction);
818}
819
820void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
821 VisitShift(instruction);
822}
823
824void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
825 HConstant* input_cst = instruction->GetConstantRight();
826 HInstruction* input_other = instruction->GetLeastConstantLeft();
827
Serguei Katkov115b53f2015-08-05 17:03:30 +0600828 Primitive::Type type = instruction->GetType();
829 if (Primitive::IsFloatingPointType(type)) {
830 return;
831 }
832
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000833 if ((input_cst != nullptr) && input_cst->IsZero()) {
834 // Replace code looking like
835 // SUB dst, src, 0
836 // with
837 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600838 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
839 // `x` is `-0.0`, the former expression yields `0.0`, while the later
840 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000841 instruction->ReplaceWith(input_other);
842 instruction->GetBlock()->RemoveInstruction(instruction);
843 return;
844 }
845
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000846 HBasicBlock* block = instruction->GetBlock();
847 ArenaAllocator* allocator = GetGraph()->GetArena();
848
Alexandre Rames188d4312015-04-09 18:30:21 +0100849 HInstruction* left = instruction->GetLeft();
850 HInstruction* right = instruction->GetRight();
851 if (left->IsConstant()) {
852 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000853 // Replace code looking like
854 // SUB dst, 0, src
855 // with
856 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100857 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000858 // `x` is `0.0`, the former expression yields `0.0`, while the later
859 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100860 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000861 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100862 RecordSimplification();
863 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000864 }
865 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100866
867 if (left->IsNeg() && right->IsNeg()) {
868 if (TryMoveNegOnInputsAfterBinop(instruction)) {
869 return;
870 }
871 }
872
873 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
874 // Replace code looking like
875 // NEG tmp, b
876 // SUB dst, a, tmp
877 // with
878 // ADD dst, a, b
879 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
880 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
881 RecordSimplification();
882 right->GetBlock()->RemoveInstruction(right);
883 return;
884 }
885
886 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
887 // Replace code looking like
888 // NEG tmp, a
889 // SUB dst, tmp, b
890 // with
891 // ADD tmp, a, b
892 // NEG dst, tmp
893 // The second version is not intrinsically better, but enables more
894 // transformations.
895 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
896 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
897 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
898 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
899 instruction->ReplaceWith(neg);
900 instruction->GetBlock()->RemoveInstruction(instruction);
901 RecordSimplification();
902 left->GetBlock()->RemoveInstruction(left);
903 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000904}
905
906void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
907 VisitShift(instruction);
908}
909
910void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
911 HConstant* input_cst = instruction->GetConstantRight();
912 HInstruction* input_other = instruction->GetLeastConstantLeft();
913
914 if ((input_cst != nullptr) && input_cst->IsZero()) {
915 // Replace code looking like
916 // XOR dst, src, 0
917 // with
918 // src
919 instruction->ReplaceWith(input_other);
920 instruction->GetBlock()->RemoveInstruction(instruction);
921 return;
922 }
923
924 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
925 // Replace code looking like
926 // XOR dst, src, 0xFFF...FF
927 // with
928 // NOT dst, src
929 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
930 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100931 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000932 return;
933 }
934}
935
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100936void InstructionSimplifierVisitor::VisitFakeString(HFakeString* instruction) {
937 HInstruction* actual_string = nullptr;
938
939 // Find the string we need to replace this instruction with. The actual string is
940 // the return value of a StringFactory call.
941 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
942 HInstruction* use = it.Current()->GetUser();
943 if (use->IsInvokeStaticOrDirect()
944 && use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction)) {
945 use->AsInvokeStaticOrDirect()->RemoveFakeStringArgumentAsLastInput();
946 actual_string = use;
947 break;
948 }
949 }
950
951 // Check that there is no other instruction that thinks it is the factory for that string.
952 if (kIsDebugBuild) {
953 CHECK(actual_string != nullptr);
954 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
955 HInstruction* use = it.Current()->GetUser();
956 if (use->IsInvokeStaticOrDirect()) {
957 CHECK(!use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction));
958 }
959 }
960 }
961
962 // We need to remove any environment uses of the fake string that are not dominated by
963 // `actual_string` to null.
964 for (HUseIterator<HEnvironment*> it(instruction->GetEnvUses()); !it.Done(); it.Advance()) {
965 HEnvironment* environment = it.Current()->GetUser();
966 if (!actual_string->StrictlyDominates(environment->GetHolder())) {
967 environment->RemoveAsUserOfInput(it.Current()->GetIndex());
968 environment->SetRawEnvAt(it.Current()->GetIndex(), nullptr);
969 }
970 }
971
972 // Only uses dominated by `actual_string` must remain. We can safely replace and remove
973 // `instruction`.
974 instruction->ReplaceWith(actual_string);
975 instruction->GetBlock()->RemoveInstruction(instruction);
976}
977
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100978} // namespace art