Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 19 | #include "intrinsics.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 20 | #include "mirror/class-inl.h" |
| 21 | #include "scoped_thread_state_change.h" |
| 22 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 25 | class InstructionSimplifierVisitor : public HGraphDelegateVisitor { |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 26 | public: |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 27 | InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats) |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 28 | : HGraphDelegateVisitor(graph), |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 29 | stats_(stats) {} |
| 30 | |
| 31 | void Run(); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 32 | |
| 33 | private: |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 34 | void RecordSimplification() { |
| 35 | simplification_occurred_ = true; |
| 36 | simplifications_at_current_position_++; |
| 37 | if (stats_) { |
| 38 | stats_->RecordStat(kInstructionSimplifications); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 43 | void VisitShift(HBinaryOperation* shift); |
| 44 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 45 | void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE; |
| 46 | void VisitEqual(HEqual* equal) OVERRIDE; |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 47 | void VisitNotEqual(HNotEqual* equal) OVERRIDE; |
| 48 | void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 49 | void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE; |
| 50 | void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 51 | void VisitArraySet(HArraySet* equal) OVERRIDE; |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 52 | void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 53 | void VisitNullCheck(HNullCheck* instruction) OVERRIDE; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 54 | void VisitArrayLength(HArrayLength* instruction) OVERRIDE; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 55 | void VisitCheckCast(HCheckCast* instruction) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 56 | void VisitAdd(HAdd* instruction) OVERRIDE; |
| 57 | void VisitAnd(HAnd* instruction) OVERRIDE; |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 58 | void VisitCondition(HCondition* instruction) OVERRIDE; |
| 59 | void VisitGreaterThan(HGreaterThan* condition) OVERRIDE; |
| 60 | void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE; |
| 61 | void VisitLessThan(HLessThan* condition) OVERRIDE; |
| 62 | void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 63 | void VisitDiv(HDiv* instruction) OVERRIDE; |
| 64 | void VisitMul(HMul* instruction) OVERRIDE; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 65 | void VisitNeg(HNeg* instruction) OVERRIDE; |
| 66 | void VisitNot(HNot* instruction) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 67 | void VisitOr(HOr* instruction) OVERRIDE; |
| 68 | void VisitShl(HShl* instruction) OVERRIDE; |
| 69 | void VisitShr(HShr* instruction) OVERRIDE; |
| 70 | void VisitSub(HSub* instruction) OVERRIDE; |
| 71 | void VisitUShr(HUShr* instruction) OVERRIDE; |
| 72 | void VisitXor(HXor* instruction) OVERRIDE; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 73 | void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE; |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 74 | void VisitFakeString(HFakeString* fake_string) OVERRIDE; |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 75 | void VisitInvoke(HInvoke* invoke) OVERRIDE; |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 76 | |
| 77 | bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 78 | |
| 79 | OptimizingCompilerStats* stats_; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 80 | bool simplification_occurred_ = false; |
| 81 | int simplifications_at_current_position_ = 0; |
| 82 | // We ensure we do not loop infinitely. The value is a finger in the air guess |
| 83 | // that should allow enough simplification. |
| 84 | static constexpr int kMaxSamePositionSimplifications = 10; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 87 | void InstructionSimplifier::Run() { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 88 | InstructionSimplifierVisitor visitor(graph_, stats_); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 89 | visitor.Run(); |
| 90 | } |
| 91 | |
| 92 | void InstructionSimplifierVisitor::Run() { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 93 | // Iterate in reverse post order to open up more simplifications to users |
| 94 | // of instructions that got simplified. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 95 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) { |
| 96 | // The simplification of an instruction to another instruction may yield |
| 97 | // possibilities for other simplifications. So although we perform a reverse |
| 98 | // post order visit, we sometimes need to revisit an instruction index. |
| 99 | simplification_occurred_ = false; |
| 100 | VisitBasicBlock(it.Current()); |
| 101 | if (simplification_occurred_ && |
| 102 | (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) { |
| 103 | // New simplifications may be applicable to the instruction at the |
| 104 | // current index, so don't advance the iterator. |
| 105 | continue; |
| 106 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 107 | simplifications_at_current_position_ = 0; |
| 108 | it.Advance(); |
| 109 | } |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 112 | namespace { |
| 113 | |
| 114 | bool AreAllBitsSet(HConstant* constant) { |
| 115 | return Int64FromConstant(constant) == -1; |
| 116 | } |
| 117 | |
| 118 | } // namespace |
| 119 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 120 | // Returns true if the code was simplified to use only one negation operation |
| 121 | // after the binary operation instead of one on each of the inputs. |
| 122 | bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) { |
| 123 | DCHECK(binop->IsAdd() || binop->IsSub()); |
| 124 | DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg()); |
| 125 | HNeg* left_neg = binop->GetLeft()->AsNeg(); |
| 126 | HNeg* right_neg = binop->GetRight()->AsNeg(); |
| 127 | if (!left_neg->HasOnlyOneNonEnvironmentUse() || |
| 128 | !right_neg->HasOnlyOneNonEnvironmentUse()) { |
| 129 | return false; |
| 130 | } |
| 131 | // Replace code looking like |
| 132 | // NEG tmp1, a |
| 133 | // NEG tmp2, b |
| 134 | // ADD dst, tmp1, tmp2 |
| 135 | // with |
| 136 | // ADD tmp, a, b |
| 137 | // NEG dst, tmp |
Serdjuk, Nikolay Y | aae9e66 | 2015-08-21 13:26:34 +0600 | [diff] [blame] | 138 | // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point. |
| 139 | // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`, |
| 140 | // while the later yields `-0.0`. |
| 141 | if (!Primitive::IsIntegralType(binop->GetType())) { |
| 142 | return false; |
| 143 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 144 | binop->ReplaceInput(left_neg->GetInput(), 0); |
| 145 | binop->ReplaceInput(right_neg->GetInput(), 1); |
| 146 | left_neg->GetBlock()->RemoveInstruction(left_neg); |
| 147 | right_neg->GetBlock()->RemoveInstruction(right_neg); |
| 148 | HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop); |
| 149 | binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext()); |
| 150 | binop->ReplaceWithExceptInReplacementAtIndex(neg, 0); |
| 151 | RecordSimplification(); |
| 152 | return true; |
| 153 | } |
| 154 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 155 | void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) { |
| 156 | DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); |
| 157 | HConstant* input_cst = instruction->GetConstantRight(); |
| 158 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 159 | |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 160 | if (input_cst != nullptr) { |
| 161 | if (input_cst->IsZero()) { |
| 162 | // Replace code looking like |
| 163 | // SHL dst, src, 0 |
| 164 | // with |
| 165 | // src |
| 166 | instruction->ReplaceWith(input_other); |
| 167 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 168 | } else if (instruction->IsShl() && input_cst->IsOne()) { |
| 169 | // Replace Shl looking like |
| 170 | // SHL dst, src, 1 |
| 171 | // with |
| 172 | // ADD dst, src, src |
| 173 | HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(), |
| 174 | input_other, |
| 175 | input_other); |
| 176 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); |
| 177 | RecordSimplification(); |
| 178 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 182 | void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) { |
| 183 | HInstruction* obj = null_check->InputAt(0); |
| 184 | if (!obj->CanBeNull()) { |
| 185 | null_check->ReplaceWith(obj); |
| 186 | null_check->GetBlock()->RemoveInstruction(null_check); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 187 | if (stats_ != nullptr) { |
| 188 | stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 193 | bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const { |
| 194 | if (!input->CanBeNull()) { |
| 195 | return true; |
| 196 | } |
| 197 | |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 198 | for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) { |
| 199 | HInstruction* use = it.Current()->GetUser(); |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 200 | if (use->IsNullCheck() && use->StrictlyDominates(at)) { |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | } |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 204 | |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 208 | // Returns whether doing a type test between the class of `object` against `klass` has |
| 209 | // a statically known outcome. The result of the test is stored in `outcome`. |
| 210 | static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 211 | DCHECK(!object->IsNullConstant()) << "Null constants should be special cased"; |
| 212 | ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo(); |
| 213 | ScopedObjectAccess soa(Thread::Current()); |
| 214 | if (!obj_rti.IsValid()) { |
| 215 | // We run the simplifier before the reference type propagation so type info might not be |
| 216 | // available. |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 217 | return false; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 218 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 219 | |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 220 | ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI(); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 221 | if (!class_rti.IsValid()) { |
| 222 | // Happens when the loaded class is unresolved. |
| 223 | return false; |
| 224 | } |
| 225 | DCHECK(class_rti.IsExact()); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 226 | if (class_rti.IsSupertypeOf(obj_rti)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 227 | *outcome = true; |
| 228 | return true; |
| 229 | } else if (obj_rti.IsExact()) { |
| 230 | // The test failed at compile time so will also fail at runtime. |
| 231 | *outcome = false; |
| 232 | return true; |
Nicolas Geoffray | 7cb499b | 2015-06-17 11:35:11 +0100 | [diff] [blame] | 233 | } else if (!class_rti.IsInterface() |
| 234 | && !obj_rti.IsInterface() |
| 235 | && !obj_rti.IsSupertypeOf(class_rti)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 236 | // Different type hierarchy. The test will fail. |
| 237 | *outcome = false; |
| 238 | return true; |
| 239 | } |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) { |
| 244 | HInstruction* object = check_cast->InputAt(0); |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 245 | if (CanEnsureNotNullAt(object, check_cast)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 246 | check_cast->ClearMustDoNullCheck(); |
| 247 | } |
| 248 | |
| 249 | if (object->IsNullConstant()) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 250 | check_cast->GetBlock()->RemoveInstruction(check_cast); |
| 251 | if (stats_ != nullptr) { |
| 252 | stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast); |
| 253 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | |
| 257 | bool outcome; |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 258 | HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass(); |
| 259 | if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 260 | if (outcome) { |
| 261 | check_cast->GetBlock()->RemoveInstruction(check_cast); |
| 262 | if (stats_ != nullptr) { |
| 263 | stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast); |
| 264 | } |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 265 | if (!load_class->HasUses()) { |
| 266 | // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw. |
| 267 | // However, here we know that it cannot because the checkcast was successfull, hence |
| 268 | // the class was already loaded. |
| 269 | load_class->GetBlock()->RemoveInstruction(load_class); |
| 270 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 271 | } else { |
| 272 | // Don't do anything for exceptional cases for now. Ideally we should remove |
| 273 | // all instructions and blocks this instruction dominates. |
| 274 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 278 | void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 279 | HInstruction* object = instruction->InputAt(0); |
| 280 | bool can_be_null = true; |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 281 | if (CanEnsureNotNullAt(object, instruction)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 282 | can_be_null = false; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 283 | instruction->ClearMustDoNullCheck(); |
| 284 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 285 | |
| 286 | HGraph* graph = GetGraph(); |
| 287 | if (object->IsNullConstant()) { |
| 288 | instruction->ReplaceWith(graph->GetIntConstant(0)); |
| 289 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 290 | RecordSimplification(); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | bool outcome; |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 295 | HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass(); |
| 296 | if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 297 | if (outcome && can_be_null) { |
| 298 | // Type test will succeed, we just need a null test. |
| 299 | HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object); |
| 300 | instruction->GetBlock()->InsertInstructionBefore(test, instruction); |
| 301 | instruction->ReplaceWith(test); |
| 302 | } else { |
| 303 | // We've statically determined the result of the instanceof. |
| 304 | instruction->ReplaceWith(graph->GetIntConstant(outcome)); |
| 305 | } |
| 306 | RecordSimplification(); |
| 307 | instruction->GetBlock()->RemoveInstruction(instruction); |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 308 | if (outcome && !load_class->HasUses()) { |
| 309 | // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw. |
| 310 | // However, here we know that it cannot because the instanceof check was successfull, hence |
| 311 | // the class was already loaded. |
| 312 | load_class->GetBlock()->RemoveInstruction(load_class); |
| 313 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 314 | } |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 317 | void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 318 | if ((instruction->GetValue()->GetType() == Primitive::kPrimNot) |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 319 | && CanEnsureNotNullAt(instruction->GetValue(), instruction)) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 320 | instruction->ClearValueCanBeNull(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) { |
| 325 | if ((instruction->GetValue()->GetType() == Primitive::kPrimNot) |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 326 | && CanEnsureNotNullAt(instruction->GetValue(), instruction)) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 327 | instruction->ClearValueCanBeNull(); |
| 328 | } |
| 329 | } |
| 330 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 331 | void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 332 | HBasicBlock* block = check->GetBlock(); |
| 333 | // Currently always keep the suspend check at entry. |
| 334 | if (block->IsEntryBlock()) return; |
| 335 | |
| 336 | // Currently always keep suspend checks at loop entry. |
| 337 | if (block->IsLoopHeader() && block->GetFirstInstruction() == check) { |
| 338 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | // Remove the suspend check that was added at build time for the baseline |
| 343 | // compiler. |
| 344 | block->RemoveInstruction(check); |
| 345 | } |
| 346 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 347 | void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 348 | HInstruction* input_const = equal->GetConstantRight(); |
| 349 | if (input_const != nullptr) { |
| 350 | HInstruction* input_value = equal->GetLeastConstantLeft(); |
| 351 | if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { |
| 352 | HBasicBlock* block = equal->GetBlock(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 353 | // We are comparing the boolean to a constant which is of type int and can |
| 354 | // be any constant. |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 355 | if (input_const->AsIntConstant()->IsOne()) { |
| 356 | // Replace (bool_value == true) with bool_value |
| 357 | equal->ReplaceWith(input_value); |
| 358 | block->RemoveInstruction(equal); |
| 359 | RecordSimplification(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 360 | } else if (input_const->AsIntConstant()->IsZero()) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 361 | // Replace (bool_value == false) with !bool_value |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 362 | block->ReplaceAndRemoveInstructionWith( |
| 363 | equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value)); |
| 364 | RecordSimplification(); |
David Brazdil | 1e9ec05 | 2015-06-22 10:26:45 +0100 | [diff] [blame] | 365 | } else { |
| 366 | // Replace (bool_value == integer_not_zero_nor_one_constant) with false |
| 367 | equal->ReplaceWith(GetGraph()->GetIntConstant(0)); |
| 368 | block->RemoveInstruction(equal); |
| 369 | RecordSimplification(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 370 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 371 | } else { |
| 372 | VisitCondition(equal); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 373 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 374 | } else { |
| 375 | VisitCondition(equal); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 379 | void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) { |
| 380 | HInstruction* input_const = not_equal->GetConstantRight(); |
| 381 | if (input_const != nullptr) { |
| 382 | HInstruction* input_value = not_equal->GetLeastConstantLeft(); |
| 383 | if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { |
| 384 | HBasicBlock* block = not_equal->GetBlock(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 385 | // We are comparing the boolean to a constant which is of type int and can |
| 386 | // be any constant. |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 387 | if (input_const->AsIntConstant()->IsOne()) { |
| 388 | // Replace (bool_value != true) with !bool_value |
| 389 | block->ReplaceAndRemoveInstructionWith( |
| 390 | not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value)); |
| 391 | RecordSimplification(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 392 | } else if (input_const->AsIntConstant()->IsZero()) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 393 | // Replace (bool_value != false) with bool_value |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 394 | not_equal->ReplaceWith(input_value); |
| 395 | block->RemoveInstruction(not_equal); |
| 396 | RecordSimplification(); |
David Brazdil | 1e9ec05 | 2015-06-22 10:26:45 +0100 | [diff] [blame] | 397 | } else { |
| 398 | // Replace (bool_value != integer_not_zero_nor_one_constant) with true |
| 399 | not_equal->ReplaceWith(GetGraph()->GetIntConstant(1)); |
| 400 | block->RemoveInstruction(not_equal); |
| 401 | RecordSimplification(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 402 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 403 | } else { |
| 404 | VisitCondition(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 405 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 406 | } else { |
| 407 | VisitCondition(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) { |
| 412 | HInstruction* parent = bool_not->InputAt(0); |
| 413 | if (parent->IsBooleanNot()) { |
| 414 | HInstruction* value = parent->InputAt(0); |
| 415 | // Replace (!(!bool_value)) with bool_value |
| 416 | bool_not->ReplaceWith(value); |
| 417 | bool_not->GetBlock()->RemoveInstruction(bool_not); |
| 418 | // It is possible that `parent` is dead at this point but we leave |
| 419 | // its removal to DCE for simplicity. |
| 420 | RecordSimplification(); |
| 421 | } |
| 422 | } |
| 423 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 424 | void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) { |
| 425 | HInstruction* input = instruction->InputAt(0); |
| 426 | // If the array is a NewArray with constant size, replace the array length |
| 427 | // with the constant instruction. This helps the bounds check elimination phase. |
| 428 | if (input->IsNewArray()) { |
| 429 | input = input->InputAt(0); |
| 430 | if (input->IsIntConstant()) { |
| 431 | instruction->ReplaceWith(input); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 436 | void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 437 | HInstruction* value = instruction->GetValue(); |
| 438 | if (value->GetType() != Primitive::kPrimNot) return; |
| 439 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 440 | if (CanEnsureNotNullAt(value, instruction)) { |
| 441 | instruction->ClearValueCanBeNull(); |
| 442 | } |
| 443 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 444 | if (value->IsArrayGet()) { |
| 445 | if (value->AsArrayGet()->GetArray() == instruction->GetArray()) { |
| 446 | // If the code is just swapping elements in the array, no need for a type check. |
| 447 | instruction->ClearNeedsTypeCheck(); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 448 | return; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 451 | |
Nicolas Geoffray | 9fdb31e | 2015-07-01 12:56:46 +0100 | [diff] [blame] | 452 | if (value->IsNullConstant()) { |
| 453 | instruction->ClearNeedsTypeCheck(); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 454 | return; |
Nicolas Geoffray | 9fdb31e | 2015-07-01 12:56:46 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 457 | ScopedObjectAccess soa(Thread::Current()); |
| 458 | ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo(); |
| 459 | ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo(); |
| 460 | if (!array_rti.IsValid()) { |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) { |
| 465 | instruction->ClearNeedsTypeCheck(); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | if (array_rti.IsObjectArray()) { |
| 470 | if (array_rti.IsExact()) { |
| 471 | instruction->ClearNeedsTypeCheck(); |
| 472 | return; |
| 473 | } |
| 474 | instruction->SetStaticTypeOfArrayIsObjectArray(); |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 475 | } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 478 | void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) { |
| 479 | if (instruction->GetResultType() == instruction->GetInputType()) { |
| 480 | // Remove the instruction if it's converting to the same type. |
| 481 | instruction->ReplaceWith(instruction->GetInput()); |
| 482 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 483 | } |
| 484 | } |
| 485 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 486 | void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) { |
| 487 | HConstant* input_cst = instruction->GetConstantRight(); |
| 488 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 489 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 490 | // Replace code looking like |
| 491 | // ADD dst, src, 0 |
| 492 | // with |
| 493 | // src |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 494 | // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When |
| 495 | // `x` is `-0.0`, the former expression yields `0.0`, while the later |
| 496 | // yields `-0.0`. |
| 497 | if (Primitive::IsIntegralType(instruction->GetType())) { |
| 498 | instruction->ReplaceWith(input_other); |
| 499 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 500 | return; |
| 501 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | HInstruction* left = instruction->GetLeft(); |
| 505 | HInstruction* right = instruction->GetRight(); |
| 506 | bool left_is_neg = left->IsNeg(); |
| 507 | bool right_is_neg = right->IsNeg(); |
| 508 | |
| 509 | if (left_is_neg && right_is_neg) { |
| 510 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 511 | return; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg(); |
| 516 | if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) { |
| 517 | // Replace code looking like |
| 518 | // NEG tmp, b |
| 519 | // ADD dst, a, tmp |
| 520 | // with |
| 521 | // SUB dst, a, b |
| 522 | // We do not perform the optimization if the input negation has environment |
| 523 | // uses or multiple non-environment uses as it could lead to worse code. In |
| 524 | // particular, we do not want the live range of `b` to be extended if we are |
| 525 | // not sure the initial 'NEG' instruction can be removed. |
| 526 | HInstruction* other = left_is_neg ? right : left; |
| 527 | HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput()); |
| 528 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub); |
| 529 | RecordSimplification(); |
| 530 | neg->GetBlock()->RemoveInstruction(neg); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) { |
| 535 | HConstant* input_cst = instruction->GetConstantRight(); |
| 536 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 537 | |
Vladimir Marko | 452c1b6 | 2015-09-25 14:44:17 +0100 | [diff] [blame] | 538 | if (input_cst != nullptr) { |
| 539 | int64_t value = Int64FromConstant(input_cst); |
| 540 | if (value == -1) { |
| 541 | // Replace code looking like |
| 542 | // AND dst, src, 0xFFF...FF |
| 543 | // with |
| 544 | // src |
| 545 | instruction->ReplaceWith(input_other); |
| 546 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 547 | RecordSimplification(); |
| 548 | return; |
| 549 | } |
| 550 | // Eliminate And from UShr+And if the And-mask contains all the bits that |
| 551 | // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask |
| 552 | // precisely clears the shifted-in sign bits. |
| 553 | if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) { |
| 554 | size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32; |
| 555 | size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1); |
| 556 | size_t num_tail_bits_set = CTZ(value + 1); |
| 557 | if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) { |
| 558 | // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff". |
| 559 | instruction->ReplaceWith(input_other); |
| 560 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 561 | RecordSimplification(); |
| 562 | return; |
| 563 | } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) && |
| 564 | input_other->HasOnlyOneNonEnvironmentUse()) { |
| 565 | DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above. |
| 566 | // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24". |
| 567 | HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(), |
| 568 | input_other->InputAt(0), |
| 569 | input_other->InputAt(1), |
| 570 | input_other->GetDexPc()); |
| 571 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr); |
| 572 | input_other->GetBlock()->RemoveInstruction(input_other); |
| 573 | RecordSimplification(); |
| 574 | return; |
| 575 | } |
| 576 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 580 | // If for some reason the values are equal but the pointers are different, we |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 581 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 582 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 583 | // Replace code looking like |
| 584 | // AND dst, src, src |
| 585 | // with |
| 586 | // src |
| 587 | instruction->ReplaceWith(instruction->GetLeft()); |
| 588 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 589 | } |
| 590 | } |
| 591 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 592 | void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) { |
| 593 | VisitCondition(condition); |
| 594 | } |
| 595 | |
| 596 | void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) { |
| 597 | VisitCondition(condition); |
| 598 | } |
| 599 | |
| 600 | void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) { |
| 601 | VisitCondition(condition); |
| 602 | } |
| 603 | |
| 604 | void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) { |
| 605 | VisitCondition(condition); |
| 606 | } |
| 607 | |
| 608 | void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) { |
| 609 | // Try to fold an HCompare into this HCondition. |
| 610 | |
Roland Levillain | 7f63c52 | 2015-07-13 15:54:55 +0000 | [diff] [blame] | 611 | // This simplification is currently supported on x86, x86_64, ARM and ARM64. |
| 612 | // TODO: Implement it for MIPS64. |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 613 | InstructionSet instruction_set = GetGraph()->GetInstructionSet(); |
Roland Levillain | 7f63c52 | 2015-07-13 15:54:55 +0000 | [diff] [blame] | 614 | if (instruction_set == kMips64) { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
| 618 | HInstruction* left = condition->GetLeft(); |
| 619 | HInstruction* right = condition->GetRight(); |
| 620 | // We can only replace an HCondition which compares a Compare to 0. |
| 621 | // Both 'dx' and 'jack' generate a compare to 0 when compiling a |
| 622 | // condition with a long, float or double comparison as input. |
| 623 | if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) { |
| 624 | // Conversion is not possible. |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | // Is the Compare only used for this purpose? |
| 629 | if (!left->GetUses().HasOnlyOneUse()) { |
| 630 | // Someone else also wants the result of the compare. |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (!left->GetEnvUses().IsEmpty()) { |
| 635 | // There is a reference to the compare result in an environment. Do we really need it? |
| 636 | if (GetGraph()->IsDebuggable()) { |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | // We have to ensure that there are no deopt points in the sequence. |
| 641 | if (left->HasAnyEnvironmentUseBefore(condition)) { |
| 642 | return; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // Clean up any environment uses from the HCompare, if any. |
| 647 | left->RemoveEnvironmentUsers(); |
| 648 | |
| 649 | // We have decided to fold the HCompare into the HCondition. Transfer the information. |
| 650 | condition->SetBias(left->AsCompare()->GetBias()); |
| 651 | |
| 652 | // Replace the operands of the HCondition. |
| 653 | condition->ReplaceInput(left->InputAt(0), 0); |
| 654 | condition->ReplaceInput(left->InputAt(1), 1); |
| 655 | |
| 656 | // Remove the HCompare. |
| 657 | left->GetBlock()->RemoveInstruction(left); |
| 658 | |
| 659 | RecordSimplification(); |
| 660 | } |
| 661 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 662 | void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) { |
| 663 | HConstant* input_cst = instruction->GetConstantRight(); |
| 664 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 665 | Primitive::Type type = instruction->GetType(); |
| 666 | |
| 667 | if ((input_cst != nullptr) && input_cst->IsOne()) { |
| 668 | // Replace code looking like |
| 669 | // DIV dst, src, 1 |
| 670 | // with |
| 671 | // src |
| 672 | instruction->ReplaceWith(input_other); |
| 673 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 674 | return; |
| 675 | } |
| 676 | |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 677 | if ((input_cst != nullptr) && input_cst->IsMinusOne()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 678 | // Replace code looking like |
| 679 | // DIV dst, src, -1 |
| 680 | // with |
| 681 | // NEG dst, src |
| 682 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 683 | instruction, new (GetGraph()->GetArena()) HNeg(type, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 684 | RecordSimplification(); |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 685 | return; |
| 686 | } |
| 687 | |
| 688 | if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) { |
| 689 | // Try replacing code looking like |
| 690 | // DIV dst, src, constant |
| 691 | // with |
| 692 | // MUL dst, src, 1 / constant |
| 693 | HConstant* reciprocal = nullptr; |
| 694 | if (type == Primitive::Primitive::kPrimDouble) { |
| 695 | double value = input_cst->AsDoubleConstant()->GetValue(); |
| 696 | if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) { |
| 697 | reciprocal = GetGraph()->GetDoubleConstant(1.0 / value); |
| 698 | } |
| 699 | } else { |
| 700 | DCHECK_EQ(type, Primitive::kPrimFloat); |
| 701 | float value = input_cst->AsFloatConstant()->GetValue(); |
| 702 | if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) { |
| 703 | reciprocal = GetGraph()->GetFloatConstant(1.0f / value); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (reciprocal != nullptr) { |
| 708 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
| 709 | instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal)); |
| 710 | RecordSimplification(); |
| 711 | return; |
| 712 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | void InstructionSimplifierVisitor::VisitMul(HMul* instruction) { |
| 717 | HConstant* input_cst = instruction->GetConstantRight(); |
| 718 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 719 | Primitive::Type type = instruction->GetType(); |
| 720 | HBasicBlock* block = instruction->GetBlock(); |
| 721 | ArenaAllocator* allocator = GetGraph()->GetArena(); |
| 722 | |
| 723 | if (input_cst == nullptr) { |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | if (input_cst->IsOne()) { |
| 728 | // Replace code looking like |
| 729 | // MUL dst, src, 1 |
| 730 | // with |
| 731 | // src |
| 732 | instruction->ReplaceWith(input_other); |
| 733 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | if (input_cst->IsMinusOne() && |
| 738 | (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) { |
| 739 | // Replace code looking like |
| 740 | // MUL dst, src, -1 |
| 741 | // with |
| 742 | // NEG dst, src |
| 743 | HNeg* neg = new (allocator) HNeg(type, input_other); |
| 744 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 745 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
| 749 | if (Primitive::IsFloatingPointType(type) && |
| 750 | ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) || |
| 751 | (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) { |
| 752 | // Replace code looking like |
| 753 | // FP_MUL dst, src, 2.0 |
| 754 | // with |
| 755 | // FP_ADD dst, src, src |
| 756 | // The 'int' and 'long' cases are handled below. |
| 757 | block->ReplaceAndRemoveInstructionWith(instruction, |
| 758 | new (allocator) HAdd(type, input_other, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 759 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | |
| 763 | if (Primitive::IsIntOrLongType(type)) { |
| 764 | int64_t factor = Int64FromConstant(input_cst); |
Serguei Katkov | 5384919 | 2015-04-20 14:22:27 +0600 | [diff] [blame] | 765 | // Even though constant propagation also takes care of the zero case, other |
| 766 | // optimizations can lead to having a zero multiplication. |
| 767 | if (factor == 0) { |
| 768 | // Replace code looking like |
| 769 | // MUL dst, src, 0 |
| 770 | // with |
| 771 | // 0 |
| 772 | instruction->ReplaceWith(input_cst); |
| 773 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 774 | } else if (IsPowerOfTwo(factor)) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 775 | // Replace code looking like |
| 776 | // MUL dst, src, pow_of_2 |
| 777 | // with |
| 778 | // SHL dst, src, log2(pow_of_2) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 779 | HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor)); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 780 | HShl* shl = new(allocator) HShl(type, input_other, shift); |
| 781 | block->ReplaceAndRemoveInstructionWith(instruction, shl); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 782 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 787 | void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) { |
| 788 | HInstruction* input = instruction->GetInput(); |
| 789 | if (input->IsNeg()) { |
| 790 | // Replace code looking like |
| 791 | // NEG tmp, src |
| 792 | // NEG dst, tmp |
| 793 | // with |
| 794 | // src |
| 795 | HNeg* previous_neg = input->AsNeg(); |
| 796 | instruction->ReplaceWith(previous_neg->GetInput()); |
| 797 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 798 | // We perform the optimization even if the input negation has environment |
| 799 | // uses since it allows removing the current instruction. But we only delete |
| 800 | // the input negation only if it is does not have any uses left. |
| 801 | if (!previous_neg->HasUses()) { |
| 802 | previous_neg->GetBlock()->RemoveInstruction(previous_neg); |
| 803 | } |
| 804 | RecordSimplification(); |
| 805 | return; |
| 806 | } |
| 807 | |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 808 | if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() && |
| 809 | !Primitive::IsFloatingPointType(input->GetType())) { |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 810 | // Replace code looking like |
| 811 | // SUB tmp, a, b |
| 812 | // NEG dst, tmp |
| 813 | // with |
| 814 | // SUB dst, b, a |
| 815 | // We do not perform the optimization if the input subtraction has |
| 816 | // environment uses or multiple non-environment uses as it could lead to |
| 817 | // worse code. In particular, we do not want the live ranges of `a` and `b` |
| 818 | // to be extended if we are not sure the initial 'SUB' instruction can be |
| 819 | // removed. |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 820 | // We do not perform optimization for fp because we could lose the sign of zero. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 821 | HSub* sub = input->AsSub(); |
| 822 | HSub* new_sub = |
| 823 | new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft()); |
| 824 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub); |
| 825 | if (!sub->HasUses()) { |
| 826 | sub->GetBlock()->RemoveInstruction(sub); |
| 827 | } |
| 828 | RecordSimplification(); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | void InstructionSimplifierVisitor::VisitNot(HNot* instruction) { |
| 833 | HInstruction* input = instruction->GetInput(); |
| 834 | if (input->IsNot()) { |
| 835 | // Replace code looking like |
| 836 | // NOT tmp, src |
| 837 | // NOT dst, tmp |
| 838 | // with |
| 839 | // src |
| 840 | // We perform the optimization even if the input negation has environment |
| 841 | // uses since it allows removing the current instruction. But we only delete |
| 842 | // the input negation only if it is does not have any uses left. |
| 843 | HNot* previous_not = input->AsNot(); |
| 844 | instruction->ReplaceWith(previous_not->GetInput()); |
| 845 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 846 | if (!previous_not->HasUses()) { |
| 847 | previous_not->GetBlock()->RemoveInstruction(previous_not); |
| 848 | } |
| 849 | RecordSimplification(); |
| 850 | } |
| 851 | } |
| 852 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 853 | void InstructionSimplifierVisitor::VisitOr(HOr* instruction) { |
| 854 | HConstant* input_cst = instruction->GetConstantRight(); |
| 855 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 856 | |
| 857 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 858 | // Replace code looking like |
| 859 | // OR dst, src, 0 |
| 860 | // with |
| 861 | // src |
| 862 | instruction->ReplaceWith(input_other); |
| 863 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 868 | // If for some reason the values are equal but the pointers are different, we |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 869 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 870 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 871 | // Replace code looking like |
| 872 | // OR dst, src, src |
| 873 | // with |
| 874 | // src |
| 875 | instruction->ReplaceWith(instruction->GetLeft()); |
| 876 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | void InstructionSimplifierVisitor::VisitShl(HShl* instruction) { |
| 881 | VisitShift(instruction); |
| 882 | } |
| 883 | |
| 884 | void InstructionSimplifierVisitor::VisitShr(HShr* instruction) { |
| 885 | VisitShift(instruction); |
| 886 | } |
| 887 | |
| 888 | void InstructionSimplifierVisitor::VisitSub(HSub* instruction) { |
| 889 | HConstant* input_cst = instruction->GetConstantRight(); |
| 890 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 891 | |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 892 | Primitive::Type type = instruction->GetType(); |
| 893 | if (Primitive::IsFloatingPointType(type)) { |
| 894 | return; |
| 895 | } |
| 896 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 897 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 898 | // Replace code looking like |
| 899 | // SUB dst, src, 0 |
| 900 | // with |
| 901 | // src |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 902 | // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When |
| 903 | // `x` is `-0.0`, the former expression yields `0.0`, while the later |
| 904 | // yields `-0.0`. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 905 | instruction->ReplaceWith(input_other); |
| 906 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 907 | return; |
| 908 | } |
| 909 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 910 | HBasicBlock* block = instruction->GetBlock(); |
| 911 | ArenaAllocator* allocator = GetGraph()->GetArena(); |
| 912 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 913 | HInstruction* left = instruction->GetLeft(); |
| 914 | HInstruction* right = instruction->GetRight(); |
| 915 | if (left->IsConstant()) { |
| 916 | if (Int64FromConstant(left->AsConstant()) == 0) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 917 | // Replace code looking like |
| 918 | // SUB dst, 0, src |
| 919 | // with |
| 920 | // NEG dst, src |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 921 | // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 922 | // `x` is `0.0`, the former expression yields `0.0`, while the later |
| 923 | // yields `-0.0`. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 924 | HNeg* neg = new (allocator) HNeg(type, right); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 925 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 926 | RecordSimplification(); |
| 927 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 930 | |
| 931 | if (left->IsNeg() && right->IsNeg()) { |
| 932 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 933 | return; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) { |
| 938 | // Replace code looking like |
| 939 | // NEG tmp, b |
| 940 | // SUB dst, a, tmp |
| 941 | // with |
| 942 | // ADD dst, a, b |
| 943 | HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput()); |
| 944 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); |
| 945 | RecordSimplification(); |
| 946 | right->GetBlock()->RemoveInstruction(right); |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) { |
| 951 | // Replace code looking like |
| 952 | // NEG tmp, a |
| 953 | // SUB dst, tmp, b |
| 954 | // with |
| 955 | // ADD tmp, a, b |
| 956 | // NEG dst, tmp |
| 957 | // The second version is not intrinsically better, but enables more |
| 958 | // transformations. |
| 959 | HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right); |
| 960 | instruction->GetBlock()->InsertInstructionBefore(add, instruction); |
| 961 | HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add); |
| 962 | instruction->GetBlock()->InsertInstructionBefore(neg, instruction); |
| 963 | instruction->ReplaceWith(neg); |
| 964 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 965 | RecordSimplification(); |
| 966 | left->GetBlock()->RemoveInstruction(left); |
| 967 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) { |
| 971 | VisitShift(instruction); |
| 972 | } |
| 973 | |
| 974 | void InstructionSimplifierVisitor::VisitXor(HXor* instruction) { |
| 975 | HConstant* input_cst = instruction->GetConstantRight(); |
| 976 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 977 | |
| 978 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 979 | // Replace code looking like |
| 980 | // XOR dst, src, 0 |
| 981 | // with |
| 982 | // src |
| 983 | instruction->ReplaceWith(input_other); |
| 984 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) { |
| 989 | // Replace code looking like |
| 990 | // XOR dst, src, 0xFFF...FF |
| 991 | // with |
| 992 | // NOT dst, src |
| 993 | HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other); |
| 994 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 995 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 996 | return; |
| 997 | } |
| 998 | } |
| 999 | |
Nicolas Geoffray | 2e7cd75 | 2015-07-10 11:38:52 +0100 | [diff] [blame] | 1000 | void InstructionSimplifierVisitor::VisitFakeString(HFakeString* instruction) { |
| 1001 | HInstruction* actual_string = nullptr; |
| 1002 | |
| 1003 | // Find the string we need to replace this instruction with. The actual string is |
| 1004 | // the return value of a StringFactory call. |
| 1005 | for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
| 1006 | HInstruction* use = it.Current()->GetUser(); |
| 1007 | if (use->IsInvokeStaticOrDirect() |
| 1008 | && use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction)) { |
| 1009 | use->AsInvokeStaticOrDirect()->RemoveFakeStringArgumentAsLastInput(); |
| 1010 | actual_string = use; |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | // Check that there is no other instruction that thinks it is the factory for that string. |
| 1016 | if (kIsDebugBuild) { |
| 1017 | CHECK(actual_string != nullptr); |
| 1018 | for (HUseIterator<HInstruction*> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
| 1019 | HInstruction* use = it.Current()->GetUser(); |
| 1020 | if (use->IsInvokeStaticOrDirect()) { |
| 1021 | CHECK(!use->AsInvokeStaticOrDirect()->IsStringFactoryFor(instruction)); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | // We need to remove any environment uses of the fake string that are not dominated by |
| 1027 | // `actual_string` to null. |
| 1028 | for (HUseIterator<HEnvironment*> it(instruction->GetEnvUses()); !it.Done(); it.Advance()) { |
| 1029 | HEnvironment* environment = it.Current()->GetUser(); |
| 1030 | if (!actual_string->StrictlyDominates(environment->GetHolder())) { |
| 1031 | environment->RemoveAsUserOfInput(it.Current()->GetIndex()); |
| 1032 | environment->SetRawEnvAt(it.Current()->GetIndex(), nullptr); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | // Only uses dominated by `actual_string` must remain. We can safely replace and remove |
| 1037 | // `instruction`. |
| 1038 | instruction->ReplaceWith(actual_string); |
| 1039 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1040 | } |
| 1041 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1042 | void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) { |
| 1043 | if (instruction->GetIntrinsic() == Intrinsics::kStringEquals) { |
| 1044 | HInstruction* argument = instruction->InputAt(1); |
| 1045 | HInstruction* receiver = instruction->InputAt(0); |
| 1046 | if (receiver == argument) { |
| 1047 | // Because String.equals is an instance call, the receiver is |
| 1048 | // a null check if we don't know it's null. The argument however, will |
| 1049 | // be the actual object. So we cannot end up in a situation where both |
| 1050 | // are equal but could be null. |
| 1051 | DCHECK(CanEnsureNotNullAt(argument, instruction)); |
| 1052 | instruction->ReplaceWith(GetGraph()->GetIntConstant(1)); |
| 1053 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1054 | } else { |
| 1055 | StringEqualsOptimizations optimizations(instruction); |
| 1056 | if (CanEnsureNotNullAt(argument, instruction)) { |
| 1057 | optimizations.SetArgumentNotNull(); |
| 1058 | } |
| 1059 | ScopedObjectAccess soa(Thread::Current()); |
| 1060 | if (argument->GetReferenceTypeInfo().IsStringClass()) { |
| 1061 | optimizations.SetArgumentIsString(); |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1067 | } // namespace art |