blob: df6e550b4abf814b4fecbe816158feaaf25935f0 [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
135 binop->ReplaceInput(left_neg->GetInput(), 0);
136 binop->ReplaceInput(right_neg->GetInput(), 1);
137 left_neg->GetBlock()->RemoveInstruction(left_neg);
138 right_neg->GetBlock()->RemoveInstruction(right_neg);
139 HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop);
140 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
141 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
142 RecordSimplification();
143 return true;
144}
145
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000146void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
147 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
148 HConstant* input_cst = instruction->GetConstantRight();
149 HInstruction* input_other = instruction->GetLeastConstantLeft();
150
Mark Mendellba56d062015-05-05 21:34:03 -0400151 if (input_cst != nullptr) {
152 if (input_cst->IsZero()) {
153 // Replace code looking like
154 // SHL dst, src, 0
155 // with
156 // src
157 instruction->ReplaceWith(input_other);
158 instruction->GetBlock()->RemoveInstruction(instruction);
159 } else if (instruction->IsShl() && input_cst->IsOne()) {
160 // Replace Shl looking like
161 // SHL dst, src, 1
162 // with
163 // ADD dst, src, src
164 HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(),
165 input_other,
166 input_other);
167 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
168 RecordSimplification();
169 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000170 }
171}
172
Calin Juravle10e244f2015-01-26 18:54:32 +0000173void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
174 HInstruction* obj = null_check->InputAt(0);
175 if (!obj->CanBeNull()) {
176 null_check->ReplaceWith(obj);
177 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000178 if (stats_ != nullptr) {
179 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
180 }
181 }
182}
183
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100184bool InstructionSimplifierVisitor::IsDominatedByInputNullCheck(HInstruction* instr) {
185 HInstruction* input = instr->InputAt(0);
186 for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) {
187 HInstruction* use = it.Current()->GetUser();
188 if (use->IsNullCheck() && use->StrictlyDominates(instr)) {
189 return true;
190 }
191 }
192 return false;
193}
194
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100195// Returns whether doing a type test between the class of `object` against `klass` has
196// a statically known outcome. The result of the test is stored in `outcome`.
197static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000198 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
199 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
200 ScopedObjectAccess soa(Thread::Current());
201 if (!obj_rti.IsValid()) {
202 // We run the simplifier before the reference type propagation so type info might not be
203 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100204 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000205 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100206
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100207 ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI();
Calin Juravle2e768302015-07-28 14:41:11 +0000208 DCHECK(class_rti.IsValid() && class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000209 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100210 *outcome = true;
211 return true;
212 } else if (obj_rti.IsExact()) {
213 // The test failed at compile time so will also fail at runtime.
214 *outcome = false;
215 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100216 } else if (!class_rti.IsInterface()
217 && !obj_rti.IsInterface()
218 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100219 // Different type hierarchy. The test will fail.
220 *outcome = false;
221 return true;
222 }
223 return false;
224}
225
226void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
227 HInstruction* object = check_cast->InputAt(0);
228 if (!object->CanBeNull() || IsDominatedByInputNullCheck(check_cast)) {
229 check_cast->ClearMustDoNullCheck();
230 }
231
232 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000233 check_cast->GetBlock()->RemoveInstruction(check_cast);
234 if (stats_ != nullptr) {
235 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
236 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100237 return;
238 }
239
240 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700241 HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass();
242 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100243 if (outcome) {
244 check_cast->GetBlock()->RemoveInstruction(check_cast);
245 if (stats_ != nullptr) {
246 stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast);
247 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700248 if (!load_class->HasUses()) {
249 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
250 // However, here we know that it cannot because the checkcast was successfull, hence
251 // the class was already loaded.
252 load_class->GetBlock()->RemoveInstruction(load_class);
253 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100254 } else {
255 // Don't do anything for exceptional cases for now. Ideally we should remove
256 // all instructions and blocks this instruction dominates.
257 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000258 }
259}
260
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100261void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100262 HInstruction* object = instruction->InputAt(0);
263 bool can_be_null = true;
264 if (!object->CanBeNull() || IsDominatedByInputNullCheck(instruction)) {
265 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100266 instruction->ClearMustDoNullCheck();
267 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100268
269 HGraph* graph = GetGraph();
270 if (object->IsNullConstant()) {
271 instruction->ReplaceWith(graph->GetIntConstant(0));
272 instruction->GetBlock()->RemoveInstruction(instruction);
273 RecordSimplification();
274 return;
275 }
276
277 bool outcome;
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700278 HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass();
279 if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100280 if (outcome && can_be_null) {
281 // Type test will succeed, we just need a null test.
282 HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object);
283 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
284 instruction->ReplaceWith(test);
285 } else {
286 // We've statically determined the result of the instanceof.
287 instruction->ReplaceWith(graph->GetIntConstant(outcome));
288 }
289 RecordSimplification();
290 instruction->GetBlock()->RemoveInstruction(instruction);
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700291 if (outcome && !load_class->HasUses()) {
292 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
293 // However, here we know that it cannot because the instanceof check was successfull, hence
294 // the class was already loaded.
295 load_class->GetBlock()->RemoveInstruction(load_class);
296 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100297 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100298}
299
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100300void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
301 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
302 && !instruction->GetValue()->CanBeNull()) {
303 instruction->ClearValueCanBeNull();
304 }
305}
306
307void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
308 if ((instruction->GetValue()->GetType() == Primitive::kPrimNot)
309 && !instruction->GetValue()->CanBeNull()) {
310 instruction->ClearValueCanBeNull();
311 }
312}
313
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000314void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100315 HBasicBlock* block = check->GetBlock();
316 // Currently always keep the suspend check at entry.
317 if (block->IsEntryBlock()) return;
318
319 // Currently always keep suspend checks at loop entry.
320 if (block->IsLoopHeader() && block->GetFirstInstruction() == check) {
321 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check);
322 return;
323 }
324
325 // Remove the suspend check that was added at build time for the baseline
326 // compiler.
327 block->RemoveInstruction(check);
328}
329
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000330void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100331 HInstruction* input_const = equal->GetConstantRight();
332 if (input_const != nullptr) {
333 HInstruction* input_value = equal->GetLeastConstantLeft();
334 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
335 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100336 // We are comparing the boolean to a constant which is of type int and can
337 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100338 if (input_const->AsIntConstant()->IsOne()) {
339 // Replace (bool_value == true) with bool_value
340 equal->ReplaceWith(input_value);
341 block->RemoveInstruction(equal);
342 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100343 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100344 // Replace (bool_value == false) with !bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100345 block->ReplaceAndRemoveInstructionWith(
346 equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
347 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100348 } else {
349 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
350 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
351 block->RemoveInstruction(equal);
352 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100353 }
Mark Mendellc4701932015-04-10 13:18:51 -0400354 } else {
355 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100356 }
Mark Mendellc4701932015-04-10 13:18:51 -0400357 } else {
358 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100359 }
360}
361
David Brazdil0d13fee2015-04-17 14:52:19 +0100362void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
363 HInstruction* input_const = not_equal->GetConstantRight();
364 if (input_const != nullptr) {
365 HInstruction* input_value = not_equal->GetLeastConstantLeft();
366 if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) {
367 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100368 // We are comparing the boolean to a constant which is of type int and can
369 // be any constant.
David Brazdil0d13fee2015-04-17 14:52:19 +0100370 if (input_const->AsIntConstant()->IsOne()) {
371 // Replace (bool_value != true) with !bool_value
372 block->ReplaceAndRemoveInstructionWith(
373 not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value));
374 RecordSimplification();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100375 } else if (input_const->AsIntConstant()->IsZero()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100376 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100377 not_equal->ReplaceWith(input_value);
378 block->RemoveInstruction(not_equal);
379 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100380 } else {
381 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
382 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
383 block->RemoveInstruction(not_equal);
384 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100385 }
Mark Mendellc4701932015-04-10 13:18:51 -0400386 } else {
387 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100388 }
Mark Mendellc4701932015-04-10 13:18:51 -0400389 } else {
390 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100391 }
392}
393
394void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
395 HInstruction* parent = bool_not->InputAt(0);
396 if (parent->IsBooleanNot()) {
397 HInstruction* value = parent->InputAt(0);
398 // Replace (!(!bool_value)) with bool_value
399 bool_not->ReplaceWith(value);
400 bool_not->GetBlock()->RemoveInstruction(bool_not);
401 // It is possible that `parent` is dead at this point but we leave
402 // its removal to DCE for simplicity.
403 RecordSimplification();
404 }
405}
406
Mingyao Yang0304e182015-01-30 16:41:29 -0800407void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
408 HInstruction* input = instruction->InputAt(0);
409 // If the array is a NewArray with constant size, replace the array length
410 // with the constant instruction. This helps the bounds check elimination phase.
411 if (input->IsNewArray()) {
412 input = input->InputAt(0);
413 if (input->IsIntConstant()) {
414 instruction->ReplaceWith(input);
415 }
416 }
417}
418
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000419void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000420 HInstruction* value = instruction->GetValue();
421 if (value->GetType() != Primitive::kPrimNot) return;
422
423 if (value->IsArrayGet()) {
424 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
425 // If the code is just swapping elements in the array, no need for a type check.
426 instruction->ClearNeedsTypeCheck();
427 }
428 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100429
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +0100430 if (value->IsNullConstant()) {
431 instruction->ClearNeedsTypeCheck();
432 }
433
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100434 if (!value->CanBeNull()) {
435 instruction->ClearValueCanBeNull();
436 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000437}
438
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +0000439void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
440 if (instruction->GetResultType() == instruction->GetInputType()) {
441 // Remove the instruction if it's converting to the same type.
442 instruction->ReplaceWith(instruction->GetInput());
443 instruction->GetBlock()->RemoveInstruction(instruction);
444 }
445}
446
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000447void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
448 HConstant* input_cst = instruction->GetConstantRight();
449 HInstruction* input_other = instruction->GetLeastConstantLeft();
450 if ((input_cst != nullptr) && input_cst->IsZero()) {
451 // Replace code looking like
452 // ADD dst, src, 0
453 // with
454 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600455 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
456 // `x` is `-0.0`, the former expression yields `0.0`, while the later
457 // yields `-0.0`.
458 if (Primitive::IsIntegralType(instruction->GetType())) {
459 instruction->ReplaceWith(input_other);
460 instruction->GetBlock()->RemoveInstruction(instruction);
461 return;
462 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100463 }
464
465 HInstruction* left = instruction->GetLeft();
466 HInstruction* right = instruction->GetRight();
467 bool left_is_neg = left->IsNeg();
468 bool right_is_neg = right->IsNeg();
469
470 if (left_is_neg && right_is_neg) {
471 if (TryMoveNegOnInputsAfterBinop(instruction)) {
472 return;
473 }
474 }
475
476 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
477 if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) {
478 // Replace code looking like
479 // NEG tmp, b
480 // ADD dst, a, tmp
481 // with
482 // SUB dst, a, b
483 // We do not perform the optimization if the input negation has environment
484 // uses or multiple non-environment uses as it could lead to worse code. In
485 // particular, we do not want the live range of `b` to be extended if we are
486 // not sure the initial 'NEG' instruction can be removed.
487 HInstruction* other = left_is_neg ? right : left;
488 HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput());
489 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
490 RecordSimplification();
491 neg->GetBlock()->RemoveInstruction(neg);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000492 }
493}
494
495void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
496 HConstant* input_cst = instruction->GetConstantRight();
497 HInstruction* input_other = instruction->GetLeastConstantLeft();
498
499 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
500 // Replace code looking like
501 // AND dst, src, 0xFFF...FF
502 // with
503 // src
504 instruction->ReplaceWith(input_other);
505 instruction->GetBlock()->RemoveInstruction(instruction);
506 return;
507 }
508
509 // We assume that GVN has run before, so we only perform a pointer comparison.
510 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100511 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000512 if (instruction->GetLeft() == instruction->GetRight()) {
513 // Replace code looking like
514 // AND dst, src, src
515 // with
516 // src
517 instruction->ReplaceWith(instruction->GetLeft());
518 instruction->GetBlock()->RemoveInstruction(instruction);
519 }
520}
521
Mark Mendellc4701932015-04-10 13:18:51 -0400522void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
523 VisitCondition(condition);
524}
525
526void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
527 VisitCondition(condition);
528}
529
530void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
531 VisitCondition(condition);
532}
533
534void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
535 VisitCondition(condition);
536}
537
538void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
539 // Try to fold an HCompare into this HCondition.
540
Roland Levillain7f63c522015-07-13 15:54:55 +0000541 // This simplification is currently supported on x86, x86_64, ARM and ARM64.
542 // TODO: Implement it for MIPS64.
Mark Mendellc4701932015-04-10 13:18:51 -0400543 InstructionSet instruction_set = GetGraph()->GetInstructionSet();
Roland Levillain7f63c522015-07-13 15:54:55 +0000544 if (instruction_set == kMips64) {
Mark Mendellc4701932015-04-10 13:18:51 -0400545 return;
546 }
547
548 HInstruction* left = condition->GetLeft();
549 HInstruction* right = condition->GetRight();
550 // We can only replace an HCondition which compares a Compare to 0.
551 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
552 // condition with a long, float or double comparison as input.
553 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
554 // Conversion is not possible.
555 return;
556 }
557
558 // Is the Compare only used for this purpose?
559 if (!left->GetUses().HasOnlyOneUse()) {
560 // Someone else also wants the result of the compare.
561 return;
562 }
563
564 if (!left->GetEnvUses().IsEmpty()) {
565 // There is a reference to the compare result in an environment. Do we really need it?
566 if (GetGraph()->IsDebuggable()) {
567 return;
568 }
569
570 // We have to ensure that there are no deopt points in the sequence.
571 if (left->HasAnyEnvironmentUseBefore(condition)) {
572 return;
573 }
574 }
575
576 // Clean up any environment uses from the HCompare, if any.
577 left->RemoveEnvironmentUsers();
578
579 // We have decided to fold the HCompare into the HCondition. Transfer the information.
580 condition->SetBias(left->AsCompare()->GetBias());
581
582 // Replace the operands of the HCondition.
583 condition->ReplaceInput(left->InputAt(0), 0);
584 condition->ReplaceInput(left->InputAt(1), 1);
585
586 // Remove the HCompare.
587 left->GetBlock()->RemoveInstruction(left);
588
589 RecordSimplification();
590}
591
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000592void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
593 HConstant* input_cst = instruction->GetConstantRight();
594 HInstruction* input_other = instruction->GetLeastConstantLeft();
595 Primitive::Type type = instruction->GetType();
596
597 if ((input_cst != nullptr) && input_cst->IsOne()) {
598 // Replace code looking like
599 // DIV dst, src, 1
600 // with
601 // src
602 instruction->ReplaceWith(input_other);
603 instruction->GetBlock()->RemoveInstruction(instruction);
604 return;
605 }
606
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000607 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000608 // Replace code looking like
609 // DIV dst, src, -1
610 // with
611 // NEG dst, src
612 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000613 instruction, new (GetGraph()->GetArena()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100614 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +0000615 return;
616 }
617
618 if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) {
619 // Try replacing code looking like
620 // DIV dst, src, constant
621 // with
622 // MUL dst, src, 1 / constant
623 HConstant* reciprocal = nullptr;
624 if (type == Primitive::Primitive::kPrimDouble) {
625 double value = input_cst->AsDoubleConstant()->GetValue();
626 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
627 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
628 }
629 } else {
630 DCHECK_EQ(type, Primitive::kPrimFloat);
631 float value = input_cst->AsFloatConstant()->GetValue();
632 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
633 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
634 }
635 }
636
637 if (reciprocal != nullptr) {
638 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
639 instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal));
640 RecordSimplification();
641 return;
642 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000643 }
644}
645
646void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
647 HConstant* input_cst = instruction->GetConstantRight();
648 HInstruction* input_other = instruction->GetLeastConstantLeft();
649 Primitive::Type type = instruction->GetType();
650 HBasicBlock* block = instruction->GetBlock();
651 ArenaAllocator* allocator = GetGraph()->GetArena();
652
653 if (input_cst == nullptr) {
654 return;
655 }
656
657 if (input_cst->IsOne()) {
658 // Replace code looking like
659 // MUL dst, src, 1
660 // with
661 // src
662 instruction->ReplaceWith(input_other);
663 instruction->GetBlock()->RemoveInstruction(instruction);
664 return;
665 }
666
667 if (input_cst->IsMinusOne() &&
668 (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) {
669 // Replace code looking like
670 // MUL dst, src, -1
671 // with
672 // NEG dst, src
673 HNeg* neg = new (allocator) HNeg(type, input_other);
674 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100675 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000676 return;
677 }
678
679 if (Primitive::IsFloatingPointType(type) &&
680 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
681 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
682 // Replace code looking like
683 // FP_MUL dst, src, 2.0
684 // with
685 // FP_ADD dst, src, src
686 // The 'int' and 'long' cases are handled below.
687 block->ReplaceAndRemoveInstructionWith(instruction,
688 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +0100689 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000690 return;
691 }
692
693 if (Primitive::IsIntOrLongType(type)) {
694 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +0600695 // Even though constant propagation also takes care of the zero case, other
696 // optimizations can lead to having a zero multiplication.
697 if (factor == 0) {
698 // Replace code looking like
699 // MUL dst, src, 0
700 // with
701 // 0
702 instruction->ReplaceWith(input_cst);
703 instruction->GetBlock()->RemoveInstruction(instruction);
704 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000705 // Replace code looking like
706 // MUL dst, src, pow_of_2
707 // with
708 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000709 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000710 HShl* shl = new(allocator) HShl(type, input_other, shift);
711 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +0100712 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000713 }
714 }
715}
716
Alexandre Rames188d4312015-04-09 18:30:21 +0100717void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
718 HInstruction* input = instruction->GetInput();
719 if (input->IsNeg()) {
720 // Replace code looking like
721 // NEG tmp, src
722 // NEG dst, tmp
723 // with
724 // src
725 HNeg* previous_neg = input->AsNeg();
726 instruction->ReplaceWith(previous_neg->GetInput());
727 instruction->GetBlock()->RemoveInstruction(instruction);
728 // We perform the optimization even if the input negation has environment
729 // uses since it allows removing the current instruction. But we only delete
730 // the input negation only if it is does not have any uses left.
731 if (!previous_neg->HasUses()) {
732 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
733 }
734 RecordSimplification();
735 return;
736 }
737
Serguei Katkov339dfc22015-04-20 12:29:32 +0600738 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
739 !Primitive::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100740 // Replace code looking like
741 // SUB tmp, a, b
742 // NEG dst, tmp
743 // with
744 // SUB dst, b, a
745 // We do not perform the optimization if the input subtraction has
746 // environment uses or multiple non-environment uses as it could lead to
747 // worse code. In particular, we do not want the live ranges of `a` and `b`
748 // to be extended if we are not sure the initial 'SUB' instruction can be
749 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +0600750 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +0100751 HSub* sub = input->AsSub();
752 HSub* new_sub =
753 new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft());
754 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
755 if (!sub->HasUses()) {
756 sub->GetBlock()->RemoveInstruction(sub);
757 }
758 RecordSimplification();
759 }
760}
761
762void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
763 HInstruction* input = instruction->GetInput();
764 if (input->IsNot()) {
765 // Replace code looking like
766 // NOT tmp, src
767 // NOT dst, tmp
768 // with
769 // src
770 // We perform the optimization even if the input negation has environment
771 // uses since it allows removing the current instruction. But we only delete
772 // the input negation only if it is does not have any uses left.
773 HNot* previous_not = input->AsNot();
774 instruction->ReplaceWith(previous_not->GetInput());
775 instruction->GetBlock()->RemoveInstruction(instruction);
776 if (!previous_not->HasUses()) {
777 previous_not->GetBlock()->RemoveInstruction(previous_not);
778 }
779 RecordSimplification();
780 }
781}
782
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000783void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
784 HConstant* input_cst = instruction->GetConstantRight();
785 HInstruction* input_other = instruction->GetLeastConstantLeft();
786
787 if ((input_cst != nullptr) && input_cst->IsZero()) {
788 // Replace code looking like
789 // OR dst, src, 0
790 // with
791 // src
792 instruction->ReplaceWith(input_other);
793 instruction->GetBlock()->RemoveInstruction(instruction);
794 return;
795 }
796
797 // We assume that GVN has run before, so we only perform a pointer comparison.
798 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +0100799 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000800 if (instruction->GetLeft() == instruction->GetRight()) {
801 // Replace code looking like
802 // OR dst, src, src
803 // with
804 // src
805 instruction->ReplaceWith(instruction->GetLeft());
806 instruction->GetBlock()->RemoveInstruction(instruction);
807 }
808}
809
810void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
811 VisitShift(instruction);
812}
813
814void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
815 VisitShift(instruction);
816}
817
818void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
819 HConstant* input_cst = instruction->GetConstantRight();
820 HInstruction* input_other = instruction->GetLeastConstantLeft();
821
Serguei Katkov115b53f2015-08-05 17:03:30 +0600822 Primitive::Type type = instruction->GetType();
823 if (Primitive::IsFloatingPointType(type)) {
824 return;
825 }
826
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000827 if ((input_cst != nullptr) && input_cst->IsZero()) {
828 // Replace code looking like
829 // SUB dst, src, 0
830 // with
831 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +0600832 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
833 // `x` is `-0.0`, the former expression yields `0.0`, while the later
834 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000835 instruction->ReplaceWith(input_other);
836 instruction->GetBlock()->RemoveInstruction(instruction);
837 return;
838 }
839
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000840 HBasicBlock* block = instruction->GetBlock();
841 ArenaAllocator* allocator = GetGraph()->GetArena();
842
Alexandre Rames188d4312015-04-09 18:30:21 +0100843 HInstruction* left = instruction->GetLeft();
844 HInstruction* right = instruction->GetRight();
845 if (left->IsConstant()) {
846 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000847 // Replace code looking like
848 // SUB dst, 0, src
849 // with
850 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +0100851 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000852 // `x` is `0.0`, the former expression yields `0.0`, while the later
853 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +0100854 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000855 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +0100856 RecordSimplification();
857 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000858 }
859 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100860
861 if (left->IsNeg() && right->IsNeg()) {
862 if (TryMoveNegOnInputsAfterBinop(instruction)) {
863 return;
864 }
865 }
866
867 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
868 // Replace code looking like
869 // NEG tmp, b
870 // SUB dst, a, tmp
871 // with
872 // ADD dst, a, b
873 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput());
874 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
875 RecordSimplification();
876 right->GetBlock()->RemoveInstruction(right);
877 return;
878 }
879
880 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
881 // Replace code looking like
882 // NEG tmp, a
883 // SUB dst, tmp, b
884 // with
885 // ADD tmp, a, b
886 // NEG dst, tmp
887 // The second version is not intrinsically better, but enables more
888 // transformations.
889 HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right);
890 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
891 HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add);
892 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
893 instruction->ReplaceWith(neg);
894 instruction->GetBlock()->RemoveInstruction(instruction);
895 RecordSimplification();
896 left->GetBlock()->RemoveInstruction(left);
897 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000898}
899
900void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
901 VisitShift(instruction);
902}
903
904void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
905 HConstant* input_cst = instruction->GetConstantRight();
906 HInstruction* input_other = instruction->GetLeastConstantLeft();
907
908 if ((input_cst != nullptr) && input_cst->IsZero()) {
909 // Replace code looking like
910 // XOR dst, src, 0
911 // with
912 // src
913 instruction->ReplaceWith(input_other);
914 instruction->GetBlock()->RemoveInstruction(instruction);
915 return;
916 }
917
918 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
919 // Replace code looking like
920 // XOR dst, src, 0xFFF...FF
921 // with
922 // NOT dst, src
923 HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other);
924 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +0100925 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000926 return;
927 }
928}
929
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100930void InstructionSimplifierVisitor::VisitFakeString(HFakeString* instruction) {
931 HInstruction* actual_string = nullptr;
932
933 // Find the string we need to replace this instruction with. The actual string is
934 // the return value of a StringFactory call.
935 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
936 HInstruction* use = it.Current()->GetUser();
937 if (use->IsInvokeStaticOrDirect()
938 && use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction)) {
939 use->AsInvokeStaticOrDirect()->RemoveFakeStringArgumentAsLastInput();
940 actual_string = use;
941 break;
942 }
943 }
944
945 // Check that there is no other instruction that thinks it is the factory for that string.
946 if (kIsDebugBuild) {
947 CHECK(actual_string != nullptr);
948 for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) {
949 HInstruction* use = it.Current()->GetUser();
950 if (use->IsInvokeStaticOrDirect()) {
951 CHECK(!use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction));
952 }
953 }
954 }
955
956 // We need to remove any environment uses of the fake string that are not dominated by
957 // `actual_string` to null.
958 for (HUseIterator<HEnvironment*> it(instruction->GetEnvUses()); !it.Done(); it.Advance()) {
959 HEnvironment* environment = it.Current()->GetUser();
960 if (!actual_string->StrictlyDominates(environment->GetHolder())) {
961 environment->RemoveAsUserOfInput(it.Current()->GetIndex());
962 environment->SetRawEnvAt(it.Current()->GetIndex(), nullptr);
963 }
964 }
965
966 // Only uses dominated by `actual_string` must remain. We can safely replace and remove
967 // `instruction`.
968 instruction->ReplaceWith(actual_string);
969 instruction->GetBlock()->RemoveInstruction(instruction);
970}
971
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100972} // namespace art