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 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 19 | #include "mirror/class-inl.h" |
| 20 | #include "scoped_thread_state_change.h" |
| 21 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 24 | class InstructionSimplifierVisitor : public HGraphVisitor { |
| 25 | public: |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 26 | InstructionSimplifierVisitor(HGraph* graph, OptimizingCompilerStats* stats) |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 27 | : HGraphVisitor(graph), |
| 28 | stats_(stats) {} |
| 29 | |
| 30 | void Run(); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 31 | |
| 32 | private: |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 33 | 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 Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 42 | void VisitShift(HBinaryOperation* shift); |
| 43 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 44 | void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE; |
| 45 | void VisitEqual(HEqual* equal) OVERRIDE; |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 46 | void VisitNotEqual(HNotEqual* equal) OVERRIDE; |
| 47 | void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 48 | void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE; |
| 49 | void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 50 | void VisitArraySet(HArraySet* equal) OVERRIDE; |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 51 | void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 52 | void VisitNullCheck(HNullCheck* instruction) OVERRIDE; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 53 | void VisitArrayLength(HArrayLength* instruction) OVERRIDE; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 54 | void VisitCheckCast(HCheckCast* instruction) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 55 | void VisitAdd(HAdd* instruction) OVERRIDE; |
| 56 | void VisitAnd(HAnd* instruction) OVERRIDE; |
| 57 | void VisitDiv(HDiv* instruction) OVERRIDE; |
| 58 | void VisitMul(HMul* instruction) OVERRIDE; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 59 | void VisitNeg(HNeg* instruction) OVERRIDE; |
| 60 | void VisitNot(HNot* instruction) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 61 | void VisitOr(HOr* instruction) OVERRIDE; |
| 62 | void VisitShl(HShl* instruction) OVERRIDE; |
| 63 | void VisitShr(HShr* instruction) OVERRIDE; |
| 64 | void VisitSub(HSub* instruction) OVERRIDE; |
| 65 | void VisitUShr(HUShr* instruction) OVERRIDE; |
| 66 | void VisitXor(HXor* instruction) OVERRIDE; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 67 | void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE; |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 68 | bool IsDominatedByInputNullCheck(HInstruction* instr); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 69 | |
| 70 | OptimizingCompilerStats* stats_; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 71 | bool simplification_occurred_ = false; |
| 72 | int simplifications_at_current_position_ = 0; |
| 73 | // We ensure we do not loop infinitely. The value is a finger in the air guess |
| 74 | // that should allow enough simplification. |
| 75 | static constexpr int kMaxSamePositionSimplifications = 10; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 78 | void InstructionSimplifier::Run() { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 79 | InstructionSimplifierVisitor visitor(graph_, stats_); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 80 | visitor.Run(); |
| 81 | } |
| 82 | |
| 83 | void InstructionSimplifierVisitor::Run() { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 84 | // Iterate in reverse post order to open up more simplifications to users |
| 85 | // of instructions that got simplified. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 86 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) { |
| 87 | // The simplification of an instruction to another instruction may yield |
| 88 | // possibilities for other simplifications. So although we perform a reverse |
| 89 | // post order visit, we sometimes need to revisit an instruction index. |
| 90 | simplification_occurred_ = false; |
| 91 | VisitBasicBlock(it.Current()); |
| 92 | if (simplification_occurred_ && |
| 93 | (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) { |
| 94 | // New simplifications may be applicable to the instruction at the |
| 95 | // current index, so don't advance the iterator. |
| 96 | continue; |
| 97 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 98 | simplifications_at_current_position_ = 0; |
| 99 | it.Advance(); |
| 100 | } |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 103 | namespace { |
| 104 | |
| 105 | bool AreAllBitsSet(HConstant* constant) { |
| 106 | return Int64FromConstant(constant) == -1; |
| 107 | } |
| 108 | |
| 109 | } // namespace |
| 110 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 111 | // Returns true if the code was simplified to use only one negation operation |
| 112 | // after the binary operation instead of one on each of the inputs. |
| 113 | bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) { |
| 114 | DCHECK(binop->IsAdd() || binop->IsSub()); |
| 115 | DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg()); |
| 116 | HNeg* left_neg = binop->GetLeft()->AsNeg(); |
| 117 | HNeg* right_neg = binop->GetRight()->AsNeg(); |
| 118 | if (!left_neg->HasOnlyOneNonEnvironmentUse() || |
| 119 | !right_neg->HasOnlyOneNonEnvironmentUse()) { |
| 120 | return false; |
| 121 | } |
| 122 | // Replace code looking like |
| 123 | // NEG tmp1, a |
| 124 | // NEG tmp2, b |
| 125 | // ADD dst, tmp1, tmp2 |
| 126 | // with |
| 127 | // ADD tmp, a, b |
| 128 | // NEG dst, tmp |
| 129 | binop->ReplaceInput(left_neg->GetInput(), 0); |
| 130 | binop->ReplaceInput(right_neg->GetInput(), 1); |
| 131 | left_neg->GetBlock()->RemoveInstruction(left_neg); |
| 132 | right_neg->GetBlock()->RemoveInstruction(right_neg); |
| 133 | HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop); |
| 134 | binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext()); |
| 135 | binop->ReplaceWithExceptInReplacementAtIndex(neg, 0); |
| 136 | RecordSimplification(); |
| 137 | return true; |
| 138 | } |
| 139 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 140 | void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) { |
| 141 | DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); |
| 142 | HConstant* input_cst = instruction->GetConstantRight(); |
| 143 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 144 | |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 145 | if (input_cst != nullptr) { |
| 146 | if (input_cst->IsZero()) { |
| 147 | // Replace code looking like |
| 148 | // SHL dst, src, 0 |
| 149 | // with |
| 150 | // src |
| 151 | instruction->ReplaceWith(input_other); |
| 152 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 153 | } else if (instruction->IsShl() && input_cst->IsOne()) { |
| 154 | // Replace Shl looking like |
| 155 | // SHL dst, src, 1 |
| 156 | // with |
| 157 | // ADD dst, src, src |
| 158 | HAdd *add = new(GetGraph()->GetArena()) HAdd(instruction->GetType(), |
| 159 | input_other, |
| 160 | input_other); |
| 161 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); |
| 162 | RecordSimplification(); |
| 163 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 167 | void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) { |
| 168 | HInstruction* obj = null_check->InputAt(0); |
| 169 | if (!obj->CanBeNull()) { |
| 170 | null_check->ReplaceWith(obj); |
| 171 | null_check->GetBlock()->RemoveInstruction(null_check); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 172 | if (stats_ != nullptr) { |
| 173 | stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 178 | bool InstructionSimplifierVisitor::IsDominatedByInputNullCheck(HInstruction* instr) { |
| 179 | HInstruction* input = instr->InputAt(0); |
| 180 | for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) { |
| 181 | HInstruction* use = it.Current()->GetUser(); |
| 182 | if (use->IsNullCheck() && use->StrictlyDominates(instr)) { |
| 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 189 | void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) { |
| 190 | HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass(); |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 191 | if (!check_cast->InputAt(0)->CanBeNull() || IsDominatedByInputNullCheck(check_cast)) { |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 192 | check_cast->ClearMustDoNullCheck(); |
| 193 | } |
| 194 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 195 | if (!load_class->IsResolved()) { |
| 196 | // If the class couldn't be resolve it's not safe to compare against it. It's |
| 197 | // default type would be Top which might be wider that the actual class type |
| 198 | // and thus producing wrong results. |
| 199 | return; |
| 200 | } |
| 201 | ReferenceTypeInfo obj_rti = check_cast->InputAt(0)->GetReferenceTypeInfo(); |
| 202 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
| 203 | ScopedObjectAccess soa(Thread::Current()); |
| 204 | if (class_rti.IsSupertypeOf(obj_rti)) { |
| 205 | check_cast->GetBlock()->RemoveInstruction(check_cast); |
| 206 | if (stats_ != nullptr) { |
| 207 | stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast); |
| 208 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 212 | void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) { |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 213 | if (!instruction->InputAt(0)->CanBeNull() || IsDominatedByInputNullCheck(instruction)) { |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 214 | instruction->ClearMustDoNullCheck(); |
| 215 | } |
| 216 | } |
| 217 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 218 | void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 219 | if ((instruction->GetValue()->GetType() == Primitive::kPrimNot) |
| 220 | && !instruction->GetValue()->CanBeNull()) { |
| 221 | instruction->ClearValueCanBeNull(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) { |
| 226 | if ((instruction->GetValue()->GetType() == Primitive::kPrimNot) |
| 227 | && !instruction->GetValue()->CanBeNull()) { |
| 228 | instruction->ClearValueCanBeNull(); |
| 229 | } |
| 230 | } |
| 231 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 232 | void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 233 | HBasicBlock* block = check->GetBlock(); |
| 234 | // Currently always keep the suspend check at entry. |
| 235 | if (block->IsEntryBlock()) return; |
| 236 | |
| 237 | // Currently always keep suspend checks at loop entry. |
| 238 | if (block->IsLoopHeader() && block->GetFirstInstruction() == check) { |
| 239 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // Remove the suspend check that was added at build time for the baseline |
| 244 | // compiler. |
| 245 | block->RemoveInstruction(check); |
| 246 | } |
| 247 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 248 | void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 249 | HInstruction* input_const = equal->GetConstantRight(); |
| 250 | if (input_const != nullptr) { |
| 251 | HInstruction* input_value = equal->GetLeastConstantLeft(); |
| 252 | if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { |
| 253 | HBasicBlock* block = equal->GetBlock(); |
| 254 | if (input_const->AsIntConstant()->IsOne()) { |
| 255 | // Replace (bool_value == true) with bool_value |
| 256 | equal->ReplaceWith(input_value); |
| 257 | block->RemoveInstruction(equal); |
| 258 | RecordSimplification(); |
| 259 | } else { |
| 260 | // Replace (bool_value == false) with !bool_value |
| 261 | DCHECK(input_const->AsIntConstant()->IsZero()); |
| 262 | block->ReplaceAndRemoveInstructionWith( |
| 263 | equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value)); |
| 264 | RecordSimplification(); |
| 265 | } |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 270 | void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) { |
| 271 | HInstruction* input_const = not_equal->GetConstantRight(); |
| 272 | if (input_const != nullptr) { |
| 273 | HInstruction* input_value = not_equal->GetLeastConstantLeft(); |
| 274 | if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { |
| 275 | HBasicBlock* block = not_equal->GetBlock(); |
| 276 | if (input_const->AsIntConstant()->IsOne()) { |
| 277 | // Replace (bool_value != true) with !bool_value |
| 278 | block->ReplaceAndRemoveInstructionWith( |
| 279 | not_equal, new (block->GetGraph()->GetArena()) HBooleanNot(input_value)); |
| 280 | RecordSimplification(); |
| 281 | } else { |
| 282 | // Replace (bool_value != false) with bool_value |
| 283 | DCHECK(input_const->AsIntConstant()->IsZero()); |
| 284 | not_equal->ReplaceWith(input_value); |
| 285 | block->RemoveInstruction(not_equal); |
| 286 | RecordSimplification(); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) { |
| 293 | HInstruction* parent = bool_not->InputAt(0); |
| 294 | if (parent->IsBooleanNot()) { |
| 295 | HInstruction* value = parent->InputAt(0); |
| 296 | // Replace (!(!bool_value)) with bool_value |
| 297 | bool_not->ReplaceWith(value); |
| 298 | bool_not->GetBlock()->RemoveInstruction(bool_not); |
| 299 | // It is possible that `parent` is dead at this point but we leave |
| 300 | // its removal to DCE for simplicity. |
| 301 | RecordSimplification(); |
| 302 | } |
| 303 | } |
| 304 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 305 | void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) { |
| 306 | HInstruction* input = instruction->InputAt(0); |
| 307 | // If the array is a NewArray with constant size, replace the array length |
| 308 | // with the constant instruction. This helps the bounds check elimination phase. |
| 309 | if (input->IsNewArray()) { |
| 310 | input = input->InputAt(0); |
| 311 | if (input->IsIntConstant()) { |
| 312 | instruction->ReplaceWith(input); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 317 | void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 318 | HInstruction* value = instruction->GetValue(); |
| 319 | if (value->GetType() != Primitive::kPrimNot) return; |
| 320 | |
| 321 | if (value->IsArrayGet()) { |
| 322 | if (value->AsArrayGet()->GetArray() == instruction->GetArray()) { |
| 323 | // If the code is just swapping elements in the array, no need for a type check. |
| 324 | instruction->ClearNeedsTypeCheck(); |
| 325 | } |
| 326 | } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 327 | |
| 328 | if (!value->CanBeNull()) { |
| 329 | instruction->ClearValueCanBeNull(); |
| 330 | } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 333 | void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) { |
| 334 | if (instruction->GetResultType() == instruction->GetInputType()) { |
| 335 | // Remove the instruction if it's converting to the same type. |
| 336 | instruction->ReplaceWith(instruction->GetInput()); |
| 337 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 338 | } |
| 339 | } |
| 340 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 341 | void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) { |
| 342 | HConstant* input_cst = instruction->GetConstantRight(); |
| 343 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 344 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 345 | // Replace code looking like |
| 346 | // ADD dst, src, 0 |
| 347 | // with |
| 348 | // src |
| 349 | instruction->ReplaceWith(input_other); |
| 350 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
| 354 | HInstruction* left = instruction->GetLeft(); |
| 355 | HInstruction* right = instruction->GetRight(); |
| 356 | bool left_is_neg = left->IsNeg(); |
| 357 | bool right_is_neg = right->IsNeg(); |
| 358 | |
| 359 | if (left_is_neg && right_is_neg) { |
| 360 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 361 | return; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg(); |
| 366 | if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) { |
| 367 | // Replace code looking like |
| 368 | // NEG tmp, b |
| 369 | // ADD dst, a, tmp |
| 370 | // with |
| 371 | // SUB dst, a, b |
| 372 | // We do not perform the optimization if the input negation has environment |
| 373 | // uses or multiple non-environment uses as it could lead to worse code. In |
| 374 | // particular, we do not want the live range of `b` to be extended if we are |
| 375 | // not sure the initial 'NEG' instruction can be removed. |
| 376 | HInstruction* other = left_is_neg ? right : left; |
| 377 | HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput()); |
| 378 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub); |
| 379 | RecordSimplification(); |
| 380 | neg->GetBlock()->RemoveInstruction(neg); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) { |
| 385 | HConstant* input_cst = instruction->GetConstantRight(); |
| 386 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 387 | |
| 388 | if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) { |
| 389 | // Replace code looking like |
| 390 | // AND dst, src, 0xFFF...FF |
| 391 | // with |
| 392 | // src |
| 393 | instruction->ReplaceWith(input_other); |
| 394 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 399 | // 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] | 400 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 401 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 402 | // Replace code looking like |
| 403 | // AND dst, src, src |
| 404 | // with |
| 405 | // src |
| 406 | instruction->ReplaceWith(instruction->GetLeft()); |
| 407 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) { |
| 412 | HConstant* input_cst = instruction->GetConstantRight(); |
| 413 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 414 | Primitive::Type type = instruction->GetType(); |
| 415 | |
| 416 | if ((input_cst != nullptr) && input_cst->IsOne()) { |
| 417 | // Replace code looking like |
| 418 | // DIV dst, src, 1 |
| 419 | // with |
| 420 | // src |
| 421 | instruction->ReplaceWith(input_other); |
| 422 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 423 | return; |
| 424 | } |
| 425 | |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 426 | if ((input_cst != nullptr) && input_cst->IsMinusOne()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 427 | // Replace code looking like |
| 428 | // DIV dst, src, -1 |
| 429 | // with |
| 430 | // NEG dst, src |
| 431 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 432 | instruction, new (GetGraph()->GetArena()) HNeg(type, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 433 | RecordSimplification(); |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
| 437 | if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) { |
| 438 | // Try replacing code looking like |
| 439 | // DIV dst, src, constant |
| 440 | // with |
| 441 | // MUL dst, src, 1 / constant |
| 442 | HConstant* reciprocal = nullptr; |
| 443 | if (type == Primitive::Primitive::kPrimDouble) { |
| 444 | double value = input_cst->AsDoubleConstant()->GetValue(); |
| 445 | if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) { |
| 446 | reciprocal = GetGraph()->GetDoubleConstant(1.0 / value); |
| 447 | } |
| 448 | } else { |
| 449 | DCHECK_EQ(type, Primitive::kPrimFloat); |
| 450 | float value = input_cst->AsFloatConstant()->GetValue(); |
| 451 | if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) { |
| 452 | reciprocal = GetGraph()->GetFloatConstant(1.0f / value); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (reciprocal != nullptr) { |
| 457 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
| 458 | instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal)); |
| 459 | RecordSimplification(); |
| 460 | return; |
| 461 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | void InstructionSimplifierVisitor::VisitMul(HMul* instruction) { |
| 466 | HConstant* input_cst = instruction->GetConstantRight(); |
| 467 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 468 | Primitive::Type type = instruction->GetType(); |
| 469 | HBasicBlock* block = instruction->GetBlock(); |
| 470 | ArenaAllocator* allocator = GetGraph()->GetArena(); |
| 471 | |
| 472 | if (input_cst == nullptr) { |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | if (input_cst->IsOne()) { |
| 477 | // Replace code looking like |
| 478 | // MUL dst, src, 1 |
| 479 | // with |
| 480 | // src |
| 481 | instruction->ReplaceWith(input_other); |
| 482 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | if (input_cst->IsMinusOne() && |
| 487 | (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) { |
| 488 | // Replace code looking like |
| 489 | // MUL dst, src, -1 |
| 490 | // with |
| 491 | // NEG dst, src |
| 492 | HNeg* neg = new (allocator) HNeg(type, input_other); |
| 493 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 494 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | if (Primitive::IsFloatingPointType(type) && |
| 499 | ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) || |
| 500 | (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) { |
| 501 | // Replace code looking like |
| 502 | // FP_MUL dst, src, 2.0 |
| 503 | // with |
| 504 | // FP_ADD dst, src, src |
| 505 | // The 'int' and 'long' cases are handled below. |
| 506 | block->ReplaceAndRemoveInstructionWith(instruction, |
| 507 | new (allocator) HAdd(type, input_other, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 508 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | |
| 512 | if (Primitive::IsIntOrLongType(type)) { |
| 513 | int64_t factor = Int64FromConstant(input_cst); |
Serguei Katkov | 5384919 | 2015-04-20 14:22:27 +0600 | [diff] [blame] | 514 | // Even though constant propagation also takes care of the zero case, other |
| 515 | // optimizations can lead to having a zero multiplication. |
| 516 | if (factor == 0) { |
| 517 | // Replace code looking like |
| 518 | // MUL dst, src, 0 |
| 519 | // with |
| 520 | // 0 |
| 521 | instruction->ReplaceWith(input_cst); |
| 522 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 523 | } else if (IsPowerOfTwo(factor)) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 524 | // Replace code looking like |
| 525 | // MUL dst, src, pow_of_2 |
| 526 | // with |
| 527 | // SHL dst, src, log2(pow_of_2) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 528 | HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor)); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 529 | HShl* shl = new(allocator) HShl(type, input_other, shift); |
| 530 | block->ReplaceAndRemoveInstructionWith(instruction, shl); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 531 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 536 | void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) { |
| 537 | HInstruction* input = instruction->GetInput(); |
| 538 | if (input->IsNeg()) { |
| 539 | // Replace code looking like |
| 540 | // NEG tmp, src |
| 541 | // NEG dst, tmp |
| 542 | // with |
| 543 | // src |
| 544 | HNeg* previous_neg = input->AsNeg(); |
| 545 | instruction->ReplaceWith(previous_neg->GetInput()); |
| 546 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 547 | // We perform the optimization even if the input negation has environment |
| 548 | // uses since it allows removing the current instruction. But we only delete |
| 549 | // the input negation only if it is does not have any uses left. |
| 550 | if (!previous_neg->HasUses()) { |
| 551 | previous_neg->GetBlock()->RemoveInstruction(previous_neg); |
| 552 | } |
| 553 | RecordSimplification(); |
| 554 | return; |
| 555 | } |
| 556 | |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 557 | if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() && |
| 558 | !Primitive::IsFloatingPointType(input->GetType())) { |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 559 | // Replace code looking like |
| 560 | // SUB tmp, a, b |
| 561 | // NEG dst, tmp |
| 562 | // with |
| 563 | // SUB dst, b, a |
| 564 | // We do not perform the optimization if the input subtraction has |
| 565 | // environment uses or multiple non-environment uses as it could lead to |
| 566 | // worse code. In particular, we do not want the live ranges of `a` and `b` |
| 567 | // to be extended if we are not sure the initial 'SUB' instruction can be |
| 568 | // removed. |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 569 | // 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] | 570 | HSub* sub = input->AsSub(); |
| 571 | HSub* new_sub = |
| 572 | new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft()); |
| 573 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub); |
| 574 | if (!sub->HasUses()) { |
| 575 | sub->GetBlock()->RemoveInstruction(sub); |
| 576 | } |
| 577 | RecordSimplification(); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void InstructionSimplifierVisitor::VisitNot(HNot* instruction) { |
| 582 | HInstruction* input = instruction->GetInput(); |
| 583 | if (input->IsNot()) { |
| 584 | // Replace code looking like |
| 585 | // NOT tmp, src |
| 586 | // NOT dst, tmp |
| 587 | // with |
| 588 | // src |
| 589 | // We perform the optimization even if the input negation has environment |
| 590 | // uses since it allows removing the current instruction. But we only delete |
| 591 | // the input negation only if it is does not have any uses left. |
| 592 | HNot* previous_not = input->AsNot(); |
| 593 | instruction->ReplaceWith(previous_not->GetInput()); |
| 594 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 595 | if (!previous_not->HasUses()) { |
| 596 | previous_not->GetBlock()->RemoveInstruction(previous_not); |
| 597 | } |
| 598 | RecordSimplification(); |
| 599 | } |
| 600 | } |
| 601 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 602 | void InstructionSimplifierVisitor::VisitOr(HOr* instruction) { |
| 603 | HConstant* input_cst = instruction->GetConstantRight(); |
| 604 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 605 | |
| 606 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 607 | // Replace code looking like |
| 608 | // OR dst, src, 0 |
| 609 | // with |
| 610 | // src |
| 611 | instruction->ReplaceWith(input_other); |
| 612 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 617 | // 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] | 618 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 619 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 620 | // Replace code looking like |
| 621 | // OR dst, src, src |
| 622 | // with |
| 623 | // src |
| 624 | instruction->ReplaceWith(instruction->GetLeft()); |
| 625 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | void InstructionSimplifierVisitor::VisitShl(HShl* instruction) { |
| 630 | VisitShift(instruction); |
| 631 | } |
| 632 | |
| 633 | void InstructionSimplifierVisitor::VisitShr(HShr* instruction) { |
| 634 | VisitShift(instruction); |
| 635 | } |
| 636 | |
| 637 | void InstructionSimplifierVisitor::VisitSub(HSub* instruction) { |
| 638 | HConstant* input_cst = instruction->GetConstantRight(); |
| 639 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 640 | |
| 641 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 642 | // Replace code looking like |
| 643 | // SUB dst, src, 0 |
| 644 | // with |
| 645 | // src |
| 646 | instruction->ReplaceWith(input_other); |
| 647 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | Primitive::Type type = instruction->GetType(); |
| 652 | if (!Primitive::IsIntegralType(type)) { |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | HBasicBlock* block = instruction->GetBlock(); |
| 657 | ArenaAllocator* allocator = GetGraph()->GetArena(); |
| 658 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 659 | HInstruction* left = instruction->GetLeft(); |
| 660 | HInstruction* right = instruction->GetRight(); |
| 661 | if (left->IsConstant()) { |
| 662 | if (Int64FromConstant(left->AsConstant()) == 0) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 663 | // Replace code looking like |
| 664 | // SUB dst, 0, src |
| 665 | // with |
| 666 | // NEG dst, src |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 667 | // 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] | 668 | // `x` is `0.0`, the former expression yields `0.0`, while the later |
| 669 | // yields `-0.0`. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 670 | HNeg* neg = new (allocator) HNeg(type, right); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 671 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 672 | RecordSimplification(); |
| 673 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 676 | |
| 677 | if (left->IsNeg() && right->IsNeg()) { |
| 678 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 679 | return; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) { |
| 684 | // Replace code looking like |
| 685 | // NEG tmp, b |
| 686 | // SUB dst, a, tmp |
| 687 | // with |
| 688 | // ADD dst, a, b |
| 689 | HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput()); |
| 690 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); |
| 691 | RecordSimplification(); |
| 692 | right->GetBlock()->RemoveInstruction(right); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) { |
| 697 | // Replace code looking like |
| 698 | // NEG tmp, a |
| 699 | // SUB dst, tmp, b |
| 700 | // with |
| 701 | // ADD tmp, a, b |
| 702 | // NEG dst, tmp |
| 703 | // The second version is not intrinsically better, but enables more |
| 704 | // transformations. |
| 705 | HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right); |
| 706 | instruction->GetBlock()->InsertInstructionBefore(add, instruction); |
| 707 | HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add); |
| 708 | instruction->GetBlock()->InsertInstructionBefore(neg, instruction); |
| 709 | instruction->ReplaceWith(neg); |
| 710 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 711 | RecordSimplification(); |
| 712 | left->GetBlock()->RemoveInstruction(left); |
| 713 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) { |
| 717 | VisitShift(instruction); |
| 718 | } |
| 719 | |
| 720 | void InstructionSimplifierVisitor::VisitXor(HXor* instruction) { |
| 721 | HConstant* input_cst = instruction->GetConstantRight(); |
| 722 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 723 | |
| 724 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 725 | // Replace code looking like |
| 726 | // XOR dst, src, 0 |
| 727 | // with |
| 728 | // src |
| 729 | instruction->ReplaceWith(input_other); |
| 730 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) { |
| 735 | // Replace code looking like |
| 736 | // XOR dst, src, 0xFFF...FF |
| 737 | // with |
| 738 | // NOT dst, src |
| 739 | HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other); |
| 740 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 741 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 742 | return; |
| 743 | } |
| 744 | } |
| 745 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 746 | } // namespace art |