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 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 42 | bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 43 | bool TryReplaceWithRotate(HBinaryOperation* instruction); |
| 44 | bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 45 | bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 46 | bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 47 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 48 | bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 49 | void VisitShift(HBinaryOperation* shift); |
| 50 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 51 | void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE; |
| 52 | void VisitEqual(HEqual* equal) OVERRIDE; |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 53 | void VisitNotEqual(HNotEqual* equal) OVERRIDE; |
| 54 | void VisitBooleanNot(HBooleanNot* bool_not) OVERRIDE; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 55 | void VisitInstanceFieldSet(HInstanceFieldSet* equal) OVERRIDE; |
| 56 | void VisitStaticFieldSet(HStaticFieldSet* equal) OVERRIDE; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 57 | void VisitArraySet(HArraySet* equal) OVERRIDE; |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 58 | void VisitTypeConversion(HTypeConversion* instruction) OVERRIDE; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 59 | void VisitNullCheck(HNullCheck* instruction) OVERRIDE; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 60 | void VisitArrayLength(HArrayLength* instruction) OVERRIDE; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 61 | void VisitCheckCast(HCheckCast* instruction) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 62 | void VisitAdd(HAdd* instruction) OVERRIDE; |
| 63 | void VisitAnd(HAnd* instruction) OVERRIDE; |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 64 | void VisitCondition(HCondition* instruction) OVERRIDE; |
| 65 | void VisitGreaterThan(HGreaterThan* condition) OVERRIDE; |
| 66 | void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) OVERRIDE; |
| 67 | void VisitLessThan(HLessThan* condition) OVERRIDE; |
| 68 | void VisitLessThanOrEqual(HLessThanOrEqual* condition) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 69 | void VisitDiv(HDiv* instruction) OVERRIDE; |
| 70 | void VisitMul(HMul* instruction) OVERRIDE; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 71 | void VisitNeg(HNeg* instruction) OVERRIDE; |
| 72 | void VisitNot(HNot* instruction) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 73 | void VisitOr(HOr* instruction) OVERRIDE; |
| 74 | void VisitShl(HShl* instruction) OVERRIDE; |
| 75 | void VisitShr(HShr* instruction) OVERRIDE; |
| 76 | void VisitSub(HSub* instruction) OVERRIDE; |
| 77 | void VisitUShr(HUShr* instruction) OVERRIDE; |
| 78 | void VisitXor(HXor* instruction) OVERRIDE; |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame^] | 79 | void VisitSelect(HSelect* select) OVERRIDE; |
| 80 | void VisitIf(HIf* instruction) OVERRIDE; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 81 | void VisitInstanceOf(HInstanceOf* instruction) OVERRIDE; |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 82 | void VisitInvoke(HInvoke* invoke) OVERRIDE; |
Aart Bik | bb245d1 | 2015-10-19 11:05:03 -0700 | [diff] [blame] | 83 | void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE; |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 84 | |
| 85 | bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 86 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 87 | void SimplifyRotate(HInvoke* invoke, bool is_left); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 88 | void SimplifySystemArrayCopy(HInvoke* invoke); |
| 89 | void SimplifyStringEquals(HInvoke* invoke); |
| 90 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 91 | OptimizingCompilerStats* stats_; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 92 | bool simplification_occurred_ = false; |
| 93 | int simplifications_at_current_position_ = 0; |
| 94 | // We ensure we do not loop infinitely. The value is a finger in the air guess |
| 95 | // that should allow enough simplification. |
| 96 | static constexpr int kMaxSamePositionSimplifications = 10; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 99 | void InstructionSimplifier::Run() { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 100 | InstructionSimplifierVisitor visitor(graph_, stats_); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 101 | visitor.Run(); |
| 102 | } |
| 103 | |
| 104 | void InstructionSimplifierVisitor::Run() { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 105 | // Iterate in reverse post order to open up more simplifications to users |
| 106 | // of instructions that got simplified. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 107 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done();) { |
| 108 | // The simplification of an instruction to another instruction may yield |
| 109 | // possibilities for other simplifications. So although we perform a reverse |
| 110 | // post order visit, we sometimes need to revisit an instruction index. |
| 111 | simplification_occurred_ = false; |
| 112 | VisitBasicBlock(it.Current()); |
| 113 | if (simplification_occurred_ && |
| 114 | (simplifications_at_current_position_ < kMaxSamePositionSimplifications)) { |
| 115 | // New simplifications may be applicable to the instruction at the |
| 116 | // current index, so don't advance the iterator. |
| 117 | continue; |
| 118 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 119 | simplifications_at_current_position_ = 0; |
| 120 | it.Advance(); |
| 121 | } |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 124 | namespace { |
| 125 | |
| 126 | bool AreAllBitsSet(HConstant* constant) { |
| 127 | return Int64FromConstant(constant) == -1; |
| 128 | } |
| 129 | |
| 130 | } // namespace |
| 131 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 132 | // Returns true if the code was simplified to use only one negation operation |
| 133 | // after the binary operation instead of one on each of the inputs. |
| 134 | bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) { |
| 135 | DCHECK(binop->IsAdd() || binop->IsSub()); |
| 136 | DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg()); |
| 137 | HNeg* left_neg = binop->GetLeft()->AsNeg(); |
| 138 | HNeg* right_neg = binop->GetRight()->AsNeg(); |
| 139 | if (!left_neg->HasOnlyOneNonEnvironmentUse() || |
| 140 | !right_neg->HasOnlyOneNonEnvironmentUse()) { |
| 141 | return false; |
| 142 | } |
| 143 | // Replace code looking like |
| 144 | // NEG tmp1, a |
| 145 | // NEG tmp2, b |
| 146 | // ADD dst, tmp1, tmp2 |
| 147 | // with |
| 148 | // ADD tmp, a, b |
| 149 | // NEG dst, tmp |
Serdjuk, Nikolay Y | aae9e66 | 2015-08-21 13:26:34 +0600 | [diff] [blame] | 150 | // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point. |
| 151 | // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`, |
| 152 | // while the later yields `-0.0`. |
| 153 | if (!Primitive::IsIntegralType(binop->GetType())) { |
| 154 | return false; |
| 155 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 156 | binop->ReplaceInput(left_neg->GetInput(), 0); |
| 157 | binop->ReplaceInput(right_neg->GetInput(), 1); |
| 158 | left_neg->GetBlock()->RemoveInstruction(left_neg); |
| 159 | right_neg->GetBlock()->RemoveInstruction(right_neg); |
| 160 | HNeg* neg = new (GetGraph()->GetArena()) HNeg(binop->GetType(), binop); |
| 161 | binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext()); |
| 162 | binop->ReplaceWithExceptInReplacementAtIndex(neg, 0); |
| 163 | RecordSimplification(); |
| 164 | return true; |
| 165 | } |
| 166 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 167 | void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) { |
| 168 | DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); |
| 169 | HConstant* input_cst = instruction->GetConstantRight(); |
| 170 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 171 | |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 172 | if (input_cst != nullptr) { |
| 173 | if (input_cst->IsZero()) { |
| 174 | // Replace code looking like |
| 175 | // SHL dst, src, 0 |
| 176 | // with |
| 177 | // src |
| 178 | instruction->ReplaceWith(input_other); |
| 179 | instruction->GetBlock()->RemoveInstruction(instruction); |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 180 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 184 | static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) { |
| 185 | return (sub->GetRight() == other && |
| 186 | sub->GetLeft()->IsConstant() && |
| 187 | (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0); |
| 188 | } |
| 189 | |
| 190 | bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op, |
| 191 | HUShr* ushr, |
| 192 | HShl* shl) { |
| 193 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 194 | HRor* ror = new (GetGraph()->GetArena()) HRor(ushr->GetType(), |
| 195 | ushr->GetLeft(), |
| 196 | ushr->GetRight()); |
| 197 | op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror); |
| 198 | if (!ushr->HasUses()) { |
| 199 | ushr->GetBlock()->RemoveInstruction(ushr); |
| 200 | } |
| 201 | if (!ushr->GetRight()->HasUses()) { |
| 202 | ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight()); |
| 203 | } |
| 204 | if (!shl->HasUses()) { |
| 205 | shl->GetBlock()->RemoveInstruction(shl); |
| 206 | } |
| 207 | if (!shl->GetRight()->HasUses()) { |
| 208 | shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight()); |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | // Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation. |
| 214 | bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) { |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 215 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 216 | HInstruction* left = op->GetLeft(); |
| 217 | HInstruction* right = op->GetRight(); |
| 218 | // If we have an UShr and a Shl (in either order). |
| 219 | if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) { |
| 220 | HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr(); |
| 221 | HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl(); |
| 222 | DCHECK(Primitive::IsIntOrLongType(ushr->GetType())); |
| 223 | if (ushr->GetType() == shl->GetType() && |
| 224 | ushr->GetLeft() == shl->GetLeft()) { |
| 225 | if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) { |
| 226 | // Shift distances are both constant, try replacing with Ror if they |
| 227 | // add up to the register size. |
| 228 | return TryReplaceWithRotateConstantPattern(op, ushr, shl); |
| 229 | } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) { |
| 230 | // Shift distances are potentially of the form x and (reg_size - x). |
| 231 | return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl); |
| 232 | } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) { |
| 233 | // Shift distances are potentially of the form d and -d. |
| 234 | return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // Try replacing code looking like (x >>> #rdist OP x << #ldist): |
| 242 | // UShr dst, x, #rdist |
| 243 | // Shl tmp, x, #ldist |
| 244 | // OP dst, dst, tmp |
| 245 | // or like (x >>> #rdist OP x << #-ldist): |
| 246 | // UShr dst, x, #rdist |
| 247 | // Shl tmp, x, #-ldist |
| 248 | // OP dst, dst, tmp |
| 249 | // with |
| 250 | // Ror dst, x, #rdist |
| 251 | bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op, |
| 252 | HUShr* ushr, |
| 253 | HShl* shl) { |
| 254 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 255 | size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte; |
| 256 | size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant()); |
| 257 | size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant()); |
| 258 | if (((ldist + rdist) & (reg_bits - 1)) == 0) { |
| 259 | ReplaceRotateWithRor(op, ushr, shl); |
| 260 | return true; |
| 261 | } |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | // Replace code looking like (x >>> -d OP x << d): |
| 266 | // Neg neg, d |
| 267 | // UShr dst, x, neg |
| 268 | // Shl tmp, x, d |
| 269 | // OP dst, dst, tmp |
| 270 | // with |
| 271 | // Neg neg, d |
| 272 | // Ror dst, x, neg |
| 273 | // *** OR *** |
| 274 | // Replace code looking like (x >>> d OP x << -d): |
| 275 | // UShr dst, x, d |
| 276 | // Neg neg, d |
| 277 | // Shl tmp, x, neg |
| 278 | // OP dst, dst, tmp |
| 279 | // with |
| 280 | // Ror dst, x, d |
| 281 | bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, |
| 282 | HUShr* ushr, |
| 283 | HShl* shl) { |
| 284 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 285 | DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()); |
| 286 | bool neg_is_left = shl->GetRight()->IsNeg(); |
| 287 | HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg(); |
| 288 | // And the shift distance being negated is the distance being shifted the other way. |
| 289 | if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) { |
| 290 | ReplaceRotateWithRor(op, ushr, shl); |
| 291 | } |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | // Try replacing code looking like (x >>> d OP x << (#bits - d)): |
| 296 | // UShr dst, x, d |
| 297 | // Sub ld, #bits, d |
| 298 | // Shl tmp, x, ld |
| 299 | // OP dst, dst, tmp |
| 300 | // with |
| 301 | // Ror dst, x, d |
| 302 | // *** OR *** |
| 303 | // Replace code looking like (x >>> (#bits - d) OP x << d): |
| 304 | // Sub rd, #bits, d |
| 305 | // UShr dst, x, rd |
| 306 | // Shl tmp, x, d |
| 307 | // OP dst, dst, tmp |
| 308 | // with |
| 309 | // Neg neg, d |
| 310 | // Ror dst, x, neg |
| 311 | bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, |
| 312 | HUShr* ushr, |
| 313 | HShl* shl) { |
| 314 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 315 | DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()); |
| 316 | size_t reg_bits = Primitive::ComponentSize(ushr->GetType()) * kBitsPerByte; |
| 317 | HInstruction* shl_shift = shl->GetRight(); |
| 318 | HInstruction* ushr_shift = ushr->GetRight(); |
| 319 | if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) || |
| 320 | (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) { |
| 321 | return ReplaceRotateWithRor(op, ushr, shl); |
| 322 | } |
| 323 | return false; |
| 324 | } |
| 325 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 326 | void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) { |
| 327 | HInstruction* obj = null_check->InputAt(0); |
| 328 | if (!obj->CanBeNull()) { |
| 329 | null_check->ReplaceWith(obj); |
| 330 | null_check->GetBlock()->RemoveInstruction(null_check); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 331 | if (stats_ != nullptr) { |
| 332 | stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 337 | bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const { |
| 338 | if (!input->CanBeNull()) { |
| 339 | return true; |
| 340 | } |
| 341 | |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 342 | for (HUseIterator<HInstruction*> it(input->GetUses()); !it.Done(); it.Advance()) { |
| 343 | HInstruction* use = it.Current()->GetUser(); |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 344 | if (use->IsNullCheck() && use->StrictlyDominates(at)) { |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | } |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 348 | |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 352 | // Returns whether doing a type test between the class of `object` against `klass` has |
| 353 | // a statically known outcome. The result of the test is stored in `outcome`. |
| 354 | static bool TypeCheckHasKnownOutcome(HLoadClass* klass, HInstruction* object, bool* outcome) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 355 | DCHECK(!object->IsNullConstant()) << "Null constants should be special cased"; |
| 356 | ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo(); |
| 357 | ScopedObjectAccess soa(Thread::Current()); |
| 358 | if (!obj_rti.IsValid()) { |
| 359 | // We run the simplifier before the reference type propagation so type info might not be |
| 360 | // available. |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 361 | return false; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 362 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 363 | |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 364 | ReferenceTypeInfo class_rti = klass->GetLoadedClassRTI(); |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 365 | if (!class_rti.IsValid()) { |
| 366 | // Happens when the loaded class is unresolved. |
| 367 | return false; |
| 368 | } |
| 369 | DCHECK(class_rti.IsExact()); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 370 | if (class_rti.IsSupertypeOf(obj_rti)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 371 | *outcome = true; |
| 372 | return true; |
| 373 | } else if (obj_rti.IsExact()) { |
| 374 | // The test failed at compile time so will also fail at runtime. |
| 375 | *outcome = false; |
| 376 | return true; |
Nicolas Geoffray | 7cb499b | 2015-06-17 11:35:11 +0100 | [diff] [blame] | 377 | } else if (!class_rti.IsInterface() |
| 378 | && !obj_rti.IsInterface() |
| 379 | && !obj_rti.IsSupertypeOf(class_rti)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 380 | // Different type hierarchy. The test will fail. |
| 381 | *outcome = false; |
| 382 | return true; |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) { |
| 388 | HInstruction* object = check_cast->InputAt(0); |
Calin Juravle | e53fb55 | 2015-10-07 17:51:52 +0100 | [diff] [blame] | 389 | HLoadClass* load_class = check_cast->InputAt(1)->AsLoadClass(); |
| 390 | if (load_class->NeedsAccessCheck()) { |
| 391 | // If we need to perform an access check we cannot remove the instruction. |
| 392 | return; |
| 393 | } |
| 394 | |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 395 | if (CanEnsureNotNullAt(object, check_cast)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 396 | check_cast->ClearMustDoNullCheck(); |
| 397 | } |
| 398 | |
| 399 | if (object->IsNullConstant()) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 400 | check_cast->GetBlock()->RemoveInstruction(check_cast); |
| 401 | if (stats_ != nullptr) { |
| 402 | stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast); |
| 403 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | |
| 407 | bool outcome; |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 408 | if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 409 | if (outcome) { |
| 410 | check_cast->GetBlock()->RemoveInstruction(check_cast); |
| 411 | if (stats_ != nullptr) { |
| 412 | stats_->RecordStat(MethodCompilationStat::kRemovedCheckedCast); |
| 413 | } |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 414 | if (!load_class->HasUses()) { |
| 415 | // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw. |
| 416 | // However, here we know that it cannot because the checkcast was successfull, hence |
| 417 | // the class was already loaded. |
| 418 | load_class->GetBlock()->RemoveInstruction(load_class); |
| 419 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 420 | } else { |
| 421 | // Don't do anything for exceptional cases for now. Ideally we should remove |
| 422 | // all instructions and blocks this instruction dominates. |
| 423 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 427 | void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 428 | HInstruction* object = instruction->InputAt(0); |
Calin Juravle | e53fb55 | 2015-10-07 17:51:52 +0100 | [diff] [blame] | 429 | HLoadClass* load_class = instruction->InputAt(1)->AsLoadClass(); |
| 430 | if (load_class->NeedsAccessCheck()) { |
| 431 | // If we need to perform an access check we cannot remove the instruction. |
| 432 | return; |
| 433 | } |
| 434 | |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 435 | bool can_be_null = true; |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 436 | if (CanEnsureNotNullAt(object, instruction)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 437 | can_be_null = false; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 438 | instruction->ClearMustDoNullCheck(); |
| 439 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 440 | |
| 441 | HGraph* graph = GetGraph(); |
| 442 | if (object->IsNullConstant()) { |
| 443 | instruction->ReplaceWith(graph->GetIntConstant(0)); |
| 444 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 445 | RecordSimplification(); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | bool outcome; |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 450 | if (TypeCheckHasKnownOutcome(load_class, object, &outcome)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 451 | if (outcome && can_be_null) { |
| 452 | // Type test will succeed, we just need a null test. |
| 453 | HNotEqual* test = new (graph->GetArena()) HNotEqual(graph->GetNullConstant(), object); |
| 454 | instruction->GetBlock()->InsertInstructionBefore(test, instruction); |
| 455 | instruction->ReplaceWith(test); |
| 456 | } else { |
| 457 | // We've statically determined the result of the instanceof. |
| 458 | instruction->ReplaceWith(graph->GetIntConstant(outcome)); |
| 459 | } |
| 460 | RecordSimplification(); |
| 461 | instruction->GetBlock()->RemoveInstruction(instruction); |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 462 | if (outcome && !load_class->HasUses()) { |
| 463 | // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw. |
| 464 | // However, here we know that it cannot because the instanceof check was successfull, hence |
| 465 | // the class was already loaded. |
| 466 | load_class->GetBlock()->RemoveInstruction(load_class); |
| 467 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 468 | } |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 471 | void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 472 | if ((instruction->GetValue()->GetType() == Primitive::kPrimNot) |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 473 | && CanEnsureNotNullAt(instruction->GetValue(), instruction)) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 474 | instruction->ClearValueCanBeNull(); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) { |
| 479 | if ((instruction->GetValue()->GetType() == Primitive::kPrimNot) |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 480 | && CanEnsureNotNullAt(instruction->GetValue(), instruction)) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 481 | instruction->ClearValueCanBeNull(); |
| 482 | } |
| 483 | } |
| 484 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 485 | void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 486 | HBasicBlock* block = check->GetBlock(); |
| 487 | // Currently always keep the suspend check at entry. |
| 488 | if (block->IsEntryBlock()) return; |
| 489 | |
| 490 | // Currently always keep suspend checks at loop entry. |
| 491 | if (block->IsLoopHeader() && block->GetFirstInstruction() == check) { |
| 492 | DCHECK(block->GetLoopInformation()->GetSuspendCheck() == check); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | // Remove the suspend check that was added at build time for the baseline |
| 497 | // compiler. |
| 498 | block->RemoveInstruction(check); |
| 499 | } |
| 500 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 501 | void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 502 | HInstruction* input_const = equal->GetConstantRight(); |
| 503 | if (input_const != nullptr) { |
| 504 | HInstruction* input_value = equal->GetLeastConstantLeft(); |
| 505 | if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { |
| 506 | HBasicBlock* block = equal->GetBlock(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 507 | // We are comparing the boolean to a constant which is of type int and can |
| 508 | // be any constant. |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 509 | if (input_const->AsIntConstant()->IsOne()) { |
| 510 | // Replace (bool_value == true) with bool_value |
| 511 | equal->ReplaceWith(input_value); |
| 512 | block->RemoveInstruction(equal); |
| 513 | RecordSimplification(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 514 | } else if (input_const->AsIntConstant()->IsZero()) { |
Mark Mendell | f652917 | 2015-11-17 11:16:56 -0500 | [diff] [blame] | 515 | equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal)); |
| 516 | block->RemoveInstruction(equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 517 | RecordSimplification(); |
David Brazdil | 1e9ec05 | 2015-06-22 10:26:45 +0100 | [diff] [blame] | 518 | } else { |
| 519 | // Replace (bool_value == integer_not_zero_nor_one_constant) with false |
| 520 | equal->ReplaceWith(GetGraph()->GetIntConstant(0)); |
| 521 | block->RemoveInstruction(equal); |
| 522 | RecordSimplification(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 523 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 524 | } else { |
| 525 | VisitCondition(equal); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 526 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 527 | } else { |
| 528 | VisitCondition(equal); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 532 | void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) { |
| 533 | HInstruction* input_const = not_equal->GetConstantRight(); |
| 534 | if (input_const != nullptr) { |
| 535 | HInstruction* input_value = not_equal->GetLeastConstantLeft(); |
| 536 | if (input_value->GetType() == Primitive::kPrimBoolean && input_const->IsIntConstant()) { |
| 537 | HBasicBlock* block = not_equal->GetBlock(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 538 | // We are comparing the boolean to a constant which is of type int and can |
| 539 | // be any constant. |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 540 | if (input_const->AsIntConstant()->IsOne()) { |
Mark Mendell | f652917 | 2015-11-17 11:16:56 -0500 | [diff] [blame] | 541 | not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal)); |
| 542 | block->RemoveInstruction(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 543 | RecordSimplification(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 544 | } else if (input_const->AsIntConstant()->IsZero()) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 545 | // Replace (bool_value != false) with bool_value |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 546 | not_equal->ReplaceWith(input_value); |
| 547 | block->RemoveInstruction(not_equal); |
| 548 | RecordSimplification(); |
David Brazdil | 1e9ec05 | 2015-06-22 10:26:45 +0100 | [diff] [blame] | 549 | } else { |
| 550 | // Replace (bool_value != integer_not_zero_nor_one_constant) with true |
| 551 | not_equal->ReplaceWith(GetGraph()->GetIntConstant(1)); |
| 552 | block->RemoveInstruction(not_equal); |
| 553 | RecordSimplification(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 554 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 555 | } else { |
| 556 | VisitCondition(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 557 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 558 | } else { |
| 559 | VisitCondition(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame^] | 564 | HInstruction* input = bool_not->InputAt(0); |
| 565 | HInstruction* replace_with = nullptr; |
| 566 | |
| 567 | if (input->IsIntConstant()) { |
| 568 | // Replace !(true/false) with false/true. |
| 569 | if (input->AsIntConstant()->IsOne()) { |
| 570 | replace_with = GetGraph()->GetIntConstant(0); |
| 571 | } else { |
| 572 | DCHECK(input->AsIntConstant()->IsZero()); |
| 573 | replace_with = GetGraph()->GetIntConstant(1); |
| 574 | } |
| 575 | } else if (input->IsBooleanNot()) { |
| 576 | // Replace (!(!bool_value)) with bool_value. |
| 577 | replace_with = input->InputAt(0); |
| 578 | } else if (input->IsCondition() && |
| 579 | // Don't change FP compares. The definition of compares involving |
| 580 | // NaNs forces the compares to be done as written by the user. |
| 581 | !Primitive::IsFloatingPointType(input->InputAt(0)->GetType())) { |
| 582 | // Replace condition with its opposite. |
| 583 | replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not); |
| 584 | } |
| 585 | |
| 586 | if (replace_with != nullptr) { |
| 587 | bool_not->ReplaceWith(replace_with); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 588 | bool_not->GetBlock()->RemoveInstruction(bool_not); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame^] | 589 | RecordSimplification(); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void InstructionSimplifierVisitor::VisitSelect(HSelect* select) { |
| 594 | HInstruction* replace_with = nullptr; |
| 595 | HInstruction* condition = select->GetCondition(); |
| 596 | HInstruction* true_value = select->GetTrueValue(); |
| 597 | HInstruction* false_value = select->GetFalseValue(); |
| 598 | |
| 599 | if (condition->IsBooleanNot()) { |
| 600 | // Change ((!cond) ? x : y) to (cond ? y : x). |
| 601 | condition = condition->InputAt(0); |
| 602 | std::swap(true_value, false_value); |
| 603 | select->ReplaceInput(false_value, 0); |
| 604 | select->ReplaceInput(true_value, 1); |
| 605 | select->ReplaceInput(condition, 2); |
| 606 | RecordSimplification(); |
| 607 | } |
| 608 | |
| 609 | if (true_value == false_value) { |
| 610 | // Replace (cond ? x : x) with (x). |
| 611 | replace_with = true_value; |
| 612 | } else if (condition->IsIntConstant()) { |
| 613 | if (condition->AsIntConstant()->IsOne()) { |
| 614 | // Replace (true ? x : y) with (x). |
| 615 | replace_with = true_value; |
| 616 | } else { |
| 617 | // Replace (false ? x : y) with (y). |
| 618 | DCHECK(condition->AsIntConstant()->IsZero()); |
| 619 | replace_with = false_value; |
| 620 | } |
| 621 | } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) { |
| 622 | if (true_value->AsIntConstant()->IsOne() && false_value->AsIntConstant()->IsZero()) { |
| 623 | // Replace (cond ? true : false) with (cond). |
| 624 | replace_with = condition; |
| 625 | } else if (true_value->AsIntConstant()->IsZero() && false_value->AsIntConstant()->IsOne()) { |
| 626 | // Replace (cond ? false : true) with (!cond). |
| 627 | replace_with = GetGraph()->InsertOppositeCondition(condition, select); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | if (replace_with != nullptr) { |
| 632 | select->ReplaceWith(replace_with); |
| 633 | select->GetBlock()->RemoveInstruction(select); |
| 634 | RecordSimplification(); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | void InstructionSimplifierVisitor::VisitIf(HIf* instruction) { |
| 639 | HInstruction* condition = instruction->InputAt(0); |
| 640 | if (condition->IsBooleanNot()) { |
| 641 | // Swap successors if input is negated. |
| 642 | instruction->ReplaceInput(condition->InputAt(0), 0); |
| 643 | instruction->GetBlock()->SwapSuccessors(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 644 | RecordSimplification(); |
| 645 | } |
| 646 | } |
| 647 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 648 | void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) { |
| 649 | HInstruction* input = instruction->InputAt(0); |
| 650 | // If the array is a NewArray with constant size, replace the array length |
| 651 | // with the constant instruction. This helps the bounds check elimination phase. |
| 652 | if (input->IsNewArray()) { |
| 653 | input = input->InputAt(0); |
| 654 | if (input->IsIntConstant()) { |
| 655 | instruction->ReplaceWith(input); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 660 | void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 661 | HInstruction* value = instruction->GetValue(); |
| 662 | if (value->GetType() != Primitive::kPrimNot) return; |
| 663 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 664 | if (CanEnsureNotNullAt(value, instruction)) { |
| 665 | instruction->ClearValueCanBeNull(); |
| 666 | } |
| 667 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 668 | if (value->IsArrayGet()) { |
| 669 | if (value->AsArrayGet()->GetArray() == instruction->GetArray()) { |
| 670 | // If the code is just swapping elements in the array, no need for a type check. |
| 671 | instruction->ClearNeedsTypeCheck(); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 672 | return; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 675 | |
Nicolas Geoffray | 9fdb31e | 2015-07-01 12:56:46 +0100 | [diff] [blame] | 676 | if (value->IsNullConstant()) { |
| 677 | instruction->ClearNeedsTypeCheck(); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 678 | return; |
Nicolas Geoffray | 9fdb31e | 2015-07-01 12:56:46 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 681 | ScopedObjectAccess soa(Thread::Current()); |
| 682 | ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo(); |
| 683 | ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo(); |
| 684 | if (!array_rti.IsValid()) { |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) { |
| 689 | instruction->ClearNeedsTypeCheck(); |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | if (array_rti.IsObjectArray()) { |
| 694 | if (array_rti.IsExact()) { |
| 695 | instruction->ClearNeedsTypeCheck(); |
| 696 | return; |
| 697 | } |
| 698 | instruction->SetStaticTypeOfArrayIsObjectArray(); |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 699 | } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 702 | void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) { |
| 703 | if (instruction->GetResultType() == instruction->GetInputType()) { |
| 704 | // Remove the instruction if it's converting to the same type. |
| 705 | instruction->ReplaceWith(instruction->GetInput()); |
| 706 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 707 | } |
| 708 | } |
| 709 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 710 | void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) { |
| 711 | HConstant* input_cst = instruction->GetConstantRight(); |
| 712 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 713 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 714 | // Replace code looking like |
| 715 | // ADD dst, src, 0 |
| 716 | // with |
| 717 | // src |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 718 | // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When |
| 719 | // `x` is `-0.0`, the former expression yields `0.0`, while the later |
| 720 | // yields `-0.0`. |
| 721 | if (Primitive::IsIntegralType(instruction->GetType())) { |
| 722 | instruction->ReplaceWith(input_other); |
| 723 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 724 | return; |
| 725 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | HInstruction* left = instruction->GetLeft(); |
| 729 | HInstruction* right = instruction->GetRight(); |
| 730 | bool left_is_neg = left->IsNeg(); |
| 731 | bool right_is_neg = right->IsNeg(); |
| 732 | |
| 733 | if (left_is_neg && right_is_neg) { |
| 734 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 735 | return; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg(); |
| 740 | if ((left_is_neg ^ right_is_neg) && neg->HasOnlyOneNonEnvironmentUse()) { |
| 741 | // Replace code looking like |
| 742 | // NEG tmp, b |
| 743 | // ADD dst, a, tmp |
| 744 | // with |
| 745 | // SUB dst, a, b |
| 746 | // We do not perform the optimization if the input negation has environment |
| 747 | // uses or multiple non-environment uses as it could lead to worse code. In |
| 748 | // particular, we do not want the live range of `b` to be extended if we are |
| 749 | // not sure the initial 'NEG' instruction can be removed. |
| 750 | HInstruction* other = left_is_neg ? right : left; |
| 751 | HSub* sub = new(GetGraph()->GetArena()) HSub(instruction->GetType(), other, neg->GetInput()); |
| 752 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub); |
| 753 | RecordSimplification(); |
| 754 | neg->GetBlock()->RemoveInstruction(neg); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 755 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 756 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 757 | |
| 758 | TryReplaceWithRotate(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) { |
| 762 | HConstant* input_cst = instruction->GetConstantRight(); |
| 763 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 764 | |
Vladimir Marko | 452c1b6 | 2015-09-25 14:44:17 +0100 | [diff] [blame] | 765 | if (input_cst != nullptr) { |
| 766 | int64_t value = Int64FromConstant(input_cst); |
| 767 | if (value == -1) { |
| 768 | // Replace code looking like |
| 769 | // AND dst, src, 0xFFF...FF |
| 770 | // with |
| 771 | // src |
| 772 | instruction->ReplaceWith(input_other); |
| 773 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 774 | RecordSimplification(); |
| 775 | return; |
| 776 | } |
| 777 | // Eliminate And from UShr+And if the And-mask contains all the bits that |
| 778 | // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask |
| 779 | // precisely clears the shifted-in sign bits. |
| 780 | if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) { |
| 781 | size_t reg_bits = (instruction->GetResultType() == Primitive::kPrimLong) ? 64 : 32; |
| 782 | size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1); |
| 783 | size_t num_tail_bits_set = CTZ(value + 1); |
| 784 | if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) { |
| 785 | // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff". |
| 786 | instruction->ReplaceWith(input_other); |
| 787 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 788 | RecordSimplification(); |
| 789 | return; |
| 790 | } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) && |
| 791 | input_other->HasOnlyOneNonEnvironmentUse()) { |
| 792 | DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above. |
| 793 | // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24". |
| 794 | HUShr* ushr = new (GetGraph()->GetArena()) HUShr(instruction->GetType(), |
| 795 | input_other->InputAt(0), |
| 796 | input_other->InputAt(1), |
| 797 | input_other->GetDexPc()); |
| 798 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr); |
| 799 | input_other->GetBlock()->RemoveInstruction(input_other); |
| 800 | RecordSimplification(); |
| 801 | return; |
| 802 | } |
| 803 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 807 | // 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] | 808 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 809 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 810 | // Replace code looking like |
| 811 | // AND dst, src, src |
| 812 | // with |
| 813 | // src |
| 814 | instruction->ReplaceWith(instruction->GetLeft()); |
| 815 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 816 | } |
| 817 | } |
| 818 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 819 | void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) { |
| 820 | VisitCondition(condition); |
| 821 | } |
| 822 | |
| 823 | void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) { |
| 824 | VisitCondition(condition); |
| 825 | } |
| 826 | |
| 827 | void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) { |
| 828 | VisitCondition(condition); |
| 829 | } |
| 830 | |
| 831 | void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) { |
| 832 | VisitCondition(condition); |
| 833 | } |
| 834 | |
Nicolas Geoffray | d4aee94 | 2016-01-22 12:35:26 +0000 | [diff] [blame] | 835 | // TODO: unsigned comparisons too? |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 836 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 837 | void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) { |
Nicolas Geoffray | d4aee94 | 2016-01-22 12:35:26 +0000 | [diff] [blame] | 838 | // Try to fold an HCompare into this HCondition. |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 839 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 840 | HInstruction* left = condition->GetLeft(); |
| 841 | HInstruction* right = condition->GetRight(); |
| 842 | // We can only replace an HCondition which compares a Compare to 0. |
| 843 | // Both 'dx' and 'jack' generate a compare to 0 when compiling a |
| 844 | // condition with a long, float or double comparison as input. |
| 845 | if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) { |
| 846 | // Conversion is not possible. |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | // Is the Compare only used for this purpose? |
| 851 | if (!left->GetUses().HasOnlyOneUse()) { |
| 852 | // Someone else also wants the result of the compare. |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | if (!left->GetEnvUses().IsEmpty()) { |
| 857 | // There is a reference to the compare result in an environment. Do we really need it? |
| 858 | if (GetGraph()->IsDebuggable()) { |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | // We have to ensure that there are no deopt points in the sequence. |
| 863 | if (left->HasAnyEnvironmentUseBefore(condition)) { |
| 864 | return; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | // Clean up any environment uses from the HCompare, if any. |
| 869 | left->RemoveEnvironmentUsers(); |
| 870 | |
| 871 | // We have decided to fold the HCompare into the HCondition. Transfer the information. |
| 872 | condition->SetBias(left->AsCompare()->GetBias()); |
| 873 | |
| 874 | // Replace the operands of the HCondition. |
| 875 | condition->ReplaceInput(left->InputAt(0), 0); |
| 876 | condition->ReplaceInput(left->InputAt(1), 1); |
| 877 | |
| 878 | // Remove the HCompare. |
| 879 | left->GetBlock()->RemoveInstruction(left); |
| 880 | |
| 881 | RecordSimplification(); |
| 882 | } |
| 883 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 884 | void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) { |
| 885 | HConstant* input_cst = instruction->GetConstantRight(); |
| 886 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 887 | Primitive::Type type = instruction->GetType(); |
| 888 | |
| 889 | if ((input_cst != nullptr) && input_cst->IsOne()) { |
| 890 | // Replace code looking like |
| 891 | // DIV dst, src, 1 |
| 892 | // with |
| 893 | // src |
| 894 | instruction->ReplaceWith(input_other); |
| 895 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 896 | return; |
| 897 | } |
| 898 | |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 899 | if ((input_cst != nullptr) && input_cst->IsMinusOne()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 900 | // Replace code looking like |
| 901 | // DIV dst, src, -1 |
| 902 | // with |
| 903 | // NEG dst, src |
| 904 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 905 | instruction, new (GetGraph()->GetArena()) HNeg(type, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 906 | RecordSimplification(); |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 907 | return; |
| 908 | } |
| 909 | |
| 910 | if ((input_cst != nullptr) && Primitive::IsFloatingPointType(type)) { |
| 911 | // Try replacing code looking like |
| 912 | // DIV dst, src, constant |
| 913 | // with |
| 914 | // MUL dst, src, 1 / constant |
| 915 | HConstant* reciprocal = nullptr; |
| 916 | if (type == Primitive::Primitive::kPrimDouble) { |
| 917 | double value = input_cst->AsDoubleConstant()->GetValue(); |
| 918 | if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) { |
| 919 | reciprocal = GetGraph()->GetDoubleConstant(1.0 / value); |
| 920 | } |
| 921 | } else { |
| 922 | DCHECK_EQ(type, Primitive::kPrimFloat); |
| 923 | float value = input_cst->AsFloatConstant()->GetValue(); |
| 924 | if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) { |
| 925 | reciprocal = GetGraph()->GetFloatConstant(1.0f / value); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | if (reciprocal != nullptr) { |
| 930 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
| 931 | instruction, new (GetGraph()->GetArena()) HMul(type, input_other, reciprocal)); |
| 932 | RecordSimplification(); |
| 933 | return; |
| 934 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
| 938 | void InstructionSimplifierVisitor::VisitMul(HMul* instruction) { |
| 939 | HConstant* input_cst = instruction->GetConstantRight(); |
| 940 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 941 | Primitive::Type type = instruction->GetType(); |
| 942 | HBasicBlock* block = instruction->GetBlock(); |
| 943 | ArenaAllocator* allocator = GetGraph()->GetArena(); |
| 944 | |
| 945 | if (input_cst == nullptr) { |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | if (input_cst->IsOne()) { |
| 950 | // Replace code looking like |
| 951 | // MUL dst, src, 1 |
| 952 | // with |
| 953 | // src |
| 954 | instruction->ReplaceWith(input_other); |
| 955 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | if (input_cst->IsMinusOne() && |
| 960 | (Primitive::IsFloatingPointType(type) || Primitive::IsIntOrLongType(type))) { |
| 961 | // Replace code looking like |
| 962 | // MUL dst, src, -1 |
| 963 | // with |
| 964 | // NEG dst, src |
| 965 | HNeg* neg = new (allocator) HNeg(type, input_other); |
| 966 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 967 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 968 | return; |
| 969 | } |
| 970 | |
| 971 | if (Primitive::IsFloatingPointType(type) && |
| 972 | ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) || |
| 973 | (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) { |
| 974 | // Replace code looking like |
| 975 | // FP_MUL dst, src, 2.0 |
| 976 | // with |
| 977 | // FP_ADD dst, src, src |
| 978 | // The 'int' and 'long' cases are handled below. |
| 979 | block->ReplaceAndRemoveInstructionWith(instruction, |
| 980 | new (allocator) HAdd(type, input_other, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 981 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 982 | return; |
| 983 | } |
| 984 | |
| 985 | if (Primitive::IsIntOrLongType(type)) { |
| 986 | int64_t factor = Int64FromConstant(input_cst); |
Serguei Katkov | 5384919 | 2015-04-20 14:22:27 +0600 | [diff] [blame] | 987 | // Even though constant propagation also takes care of the zero case, other |
| 988 | // optimizations can lead to having a zero multiplication. |
| 989 | if (factor == 0) { |
| 990 | // Replace code looking like |
| 991 | // MUL dst, src, 0 |
| 992 | // with |
| 993 | // 0 |
| 994 | instruction->ReplaceWith(input_cst); |
| 995 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 996 | } else if (IsPowerOfTwo(factor)) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 997 | // Replace code looking like |
| 998 | // MUL dst, src, pow_of_2 |
| 999 | // with |
| 1000 | // SHL dst, src, log2(pow_of_2) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 1001 | HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor)); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1002 | HShl* shl = new(allocator) HShl(type, input_other, shift); |
| 1003 | block->ReplaceAndRemoveInstructionWith(instruction, shl); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1004 | RecordSimplification(); |
Alexandre Rames | 38db785 | 2015-11-20 15:02:45 +0000 | [diff] [blame] | 1005 | } else if (IsPowerOfTwo(factor - 1)) { |
| 1006 | // Transform code looking like |
| 1007 | // MUL dst, src, (2^n + 1) |
| 1008 | // into |
| 1009 | // SHL tmp, src, n |
| 1010 | // ADD dst, src, tmp |
| 1011 | HShl* shl = new (allocator) HShl(type, |
| 1012 | input_other, |
| 1013 | GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1))); |
| 1014 | HAdd* add = new (allocator) HAdd(type, input_other, shl); |
| 1015 | |
| 1016 | block->InsertInstructionBefore(shl, instruction); |
| 1017 | block->ReplaceAndRemoveInstructionWith(instruction, add); |
| 1018 | RecordSimplification(); |
| 1019 | } else if (IsPowerOfTwo(factor + 1)) { |
| 1020 | // Transform code looking like |
| 1021 | // MUL dst, src, (2^n - 1) |
| 1022 | // into |
| 1023 | // SHL tmp, src, n |
| 1024 | // SUB dst, tmp, src |
| 1025 | HShl* shl = new (allocator) HShl(type, |
| 1026 | input_other, |
| 1027 | GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1))); |
| 1028 | HSub* sub = new (allocator) HSub(type, shl, input_other); |
| 1029 | |
| 1030 | block->InsertInstructionBefore(shl, instruction); |
| 1031 | block->ReplaceAndRemoveInstructionWith(instruction, sub); |
| 1032 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1037 | void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) { |
| 1038 | HInstruction* input = instruction->GetInput(); |
| 1039 | if (input->IsNeg()) { |
| 1040 | // Replace code looking like |
| 1041 | // NEG tmp, src |
| 1042 | // NEG dst, tmp |
| 1043 | // with |
| 1044 | // src |
| 1045 | HNeg* previous_neg = input->AsNeg(); |
| 1046 | instruction->ReplaceWith(previous_neg->GetInput()); |
| 1047 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1048 | // We perform the optimization even if the input negation has environment |
| 1049 | // uses since it allows removing the current instruction. But we only delete |
| 1050 | // the input negation only if it is does not have any uses left. |
| 1051 | if (!previous_neg->HasUses()) { |
| 1052 | previous_neg->GetBlock()->RemoveInstruction(previous_neg); |
| 1053 | } |
| 1054 | RecordSimplification(); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 1058 | if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() && |
| 1059 | !Primitive::IsFloatingPointType(input->GetType())) { |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1060 | // Replace code looking like |
| 1061 | // SUB tmp, a, b |
| 1062 | // NEG dst, tmp |
| 1063 | // with |
| 1064 | // SUB dst, b, a |
| 1065 | // We do not perform the optimization if the input subtraction has |
| 1066 | // environment uses or multiple non-environment uses as it could lead to |
| 1067 | // worse code. In particular, we do not want the live ranges of `a` and `b` |
| 1068 | // to be extended if we are not sure the initial 'SUB' instruction can be |
| 1069 | // removed. |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 1070 | // 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] | 1071 | HSub* sub = input->AsSub(); |
| 1072 | HSub* new_sub = |
| 1073 | new (GetGraph()->GetArena()) HSub(instruction->GetType(), sub->GetRight(), sub->GetLeft()); |
| 1074 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub); |
| 1075 | if (!sub->HasUses()) { |
| 1076 | sub->GetBlock()->RemoveInstruction(sub); |
| 1077 | } |
| 1078 | RecordSimplification(); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | void InstructionSimplifierVisitor::VisitNot(HNot* instruction) { |
| 1083 | HInstruction* input = instruction->GetInput(); |
| 1084 | if (input->IsNot()) { |
| 1085 | // Replace code looking like |
| 1086 | // NOT tmp, src |
| 1087 | // NOT dst, tmp |
| 1088 | // with |
| 1089 | // src |
| 1090 | // We perform the optimization even if the input negation has environment |
| 1091 | // uses since it allows removing the current instruction. But we only delete |
| 1092 | // the input negation only if it is does not have any uses left. |
| 1093 | HNot* previous_not = input->AsNot(); |
| 1094 | instruction->ReplaceWith(previous_not->GetInput()); |
| 1095 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1096 | if (!previous_not->HasUses()) { |
| 1097 | previous_not->GetBlock()->RemoveInstruction(previous_not); |
| 1098 | } |
| 1099 | RecordSimplification(); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1103 | void InstructionSimplifierVisitor::VisitOr(HOr* instruction) { |
| 1104 | HConstant* input_cst = instruction->GetConstantRight(); |
| 1105 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 1106 | |
| 1107 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 1108 | // Replace code looking like |
| 1109 | // OR dst, src, 0 |
| 1110 | // with |
| 1111 | // src |
| 1112 | instruction->ReplaceWith(input_other); |
| 1113 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1114 | return; |
| 1115 | } |
| 1116 | |
| 1117 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 1118 | // 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] | 1119 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1120 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 1121 | // Replace code looking like |
| 1122 | // OR dst, src, src |
| 1123 | // with |
| 1124 | // src |
| 1125 | instruction->ReplaceWith(instruction->GetLeft()); |
| 1126 | instruction->GetBlock()->RemoveInstruction(instruction); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1127 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1128 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1129 | |
| 1130 | TryReplaceWithRotate(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | void InstructionSimplifierVisitor::VisitShl(HShl* instruction) { |
| 1134 | VisitShift(instruction); |
| 1135 | } |
| 1136 | |
| 1137 | void InstructionSimplifierVisitor::VisitShr(HShr* instruction) { |
| 1138 | VisitShift(instruction); |
| 1139 | } |
| 1140 | |
| 1141 | void InstructionSimplifierVisitor::VisitSub(HSub* instruction) { |
| 1142 | HConstant* input_cst = instruction->GetConstantRight(); |
| 1143 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 1144 | |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 1145 | Primitive::Type type = instruction->GetType(); |
| 1146 | if (Primitive::IsFloatingPointType(type)) { |
| 1147 | return; |
| 1148 | } |
| 1149 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1150 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 1151 | // Replace code looking like |
| 1152 | // SUB dst, src, 0 |
| 1153 | // with |
| 1154 | // src |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 1155 | // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When |
| 1156 | // `x` is `-0.0`, the former expression yields `0.0`, while the later |
| 1157 | // yields `-0.0`. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1158 | instruction->ReplaceWith(input_other); |
| 1159 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1160 | return; |
| 1161 | } |
| 1162 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1163 | HBasicBlock* block = instruction->GetBlock(); |
| 1164 | ArenaAllocator* allocator = GetGraph()->GetArena(); |
| 1165 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1166 | HInstruction* left = instruction->GetLeft(); |
| 1167 | HInstruction* right = instruction->GetRight(); |
| 1168 | if (left->IsConstant()) { |
| 1169 | if (Int64FromConstant(left->AsConstant()) == 0) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1170 | // Replace code looking like |
| 1171 | // SUB dst, 0, src |
| 1172 | // with |
| 1173 | // NEG dst, src |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1174 | // 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] | 1175 | // `x` is `0.0`, the former expression yields `0.0`, while the later |
| 1176 | // yields `-0.0`. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1177 | HNeg* neg = new (allocator) HNeg(type, right); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1178 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1179 | RecordSimplification(); |
| 1180 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1181 | } |
| 1182 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1183 | |
| 1184 | if (left->IsNeg() && right->IsNeg()) { |
| 1185 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 1186 | return; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) { |
| 1191 | // Replace code looking like |
| 1192 | // NEG tmp, b |
| 1193 | // SUB dst, a, tmp |
| 1194 | // with |
| 1195 | // ADD dst, a, b |
| 1196 | HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left, right->AsNeg()->GetInput()); |
| 1197 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); |
| 1198 | RecordSimplification(); |
| 1199 | right->GetBlock()->RemoveInstruction(right); |
| 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) { |
| 1204 | // Replace code looking like |
| 1205 | // NEG tmp, a |
| 1206 | // SUB dst, tmp, b |
| 1207 | // with |
| 1208 | // ADD tmp, a, b |
| 1209 | // NEG dst, tmp |
| 1210 | // The second version is not intrinsically better, but enables more |
| 1211 | // transformations. |
| 1212 | HAdd* add = new(GetGraph()->GetArena()) HAdd(type, left->AsNeg()->GetInput(), right); |
| 1213 | instruction->GetBlock()->InsertInstructionBefore(add, instruction); |
| 1214 | HNeg* neg = new (GetGraph()->GetArena()) HNeg(instruction->GetType(), add); |
| 1215 | instruction->GetBlock()->InsertInstructionBefore(neg, instruction); |
| 1216 | instruction->ReplaceWith(neg); |
| 1217 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1218 | RecordSimplification(); |
| 1219 | left->GetBlock()->RemoveInstruction(left); |
| 1220 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) { |
| 1224 | VisitShift(instruction); |
| 1225 | } |
| 1226 | |
| 1227 | void InstructionSimplifierVisitor::VisitXor(HXor* instruction) { |
| 1228 | HConstant* input_cst = instruction->GetConstantRight(); |
| 1229 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 1230 | |
| 1231 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 1232 | // Replace code looking like |
| 1233 | // XOR dst, src, 0 |
| 1234 | // with |
| 1235 | // src |
| 1236 | instruction->ReplaceWith(input_other); |
| 1237 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1238 | return; |
| 1239 | } |
| 1240 | |
| 1241 | if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) { |
| 1242 | // Replace code looking like |
| 1243 | // XOR dst, src, 0xFFF...FF |
| 1244 | // with |
| 1245 | // NOT dst, src |
| 1246 | HNot* bitwise_not = new (GetGraph()->GetArena()) HNot(instruction->GetType(), input_other); |
| 1247 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1248 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1249 | return; |
| 1250 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1251 | |
| 1252 | TryReplaceWithRotate(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1255 | void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) { |
| 1256 | HInstruction* argument = instruction->InputAt(1); |
| 1257 | HInstruction* receiver = instruction->InputAt(0); |
| 1258 | if (receiver == argument) { |
| 1259 | // Because String.equals is an instance call, the receiver is |
| 1260 | // a null check if we don't know it's null. The argument however, will |
| 1261 | // be the actual object. So we cannot end up in a situation where both |
| 1262 | // are equal but could be null. |
| 1263 | DCHECK(CanEnsureNotNullAt(argument, instruction)); |
| 1264 | instruction->ReplaceWith(GetGraph()->GetIntConstant(1)); |
| 1265 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1266 | } else { |
| 1267 | StringEqualsOptimizations optimizations(instruction); |
| 1268 | if (CanEnsureNotNullAt(argument, instruction)) { |
| 1269 | optimizations.SetArgumentNotNull(); |
| 1270 | } |
| 1271 | ScopedObjectAccess soa(Thread::Current()); |
| 1272 | ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo(); |
| 1273 | if (argument_rti.IsValid() && argument_rti.IsStringClass()) { |
| 1274 | optimizations.SetArgumentIsString(); |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1279 | void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke, bool is_left) { |
| 1280 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
| 1281 | DCHECK_EQ(invoke->GetOriginalInvokeType(), InvokeType::kStatic); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1282 | HInstruction* value = invoke->InputAt(0); |
| 1283 | HInstruction* distance = invoke->InputAt(1); |
| 1284 | // Replace the invoke with an HRor. |
| 1285 | if (is_left) { |
| 1286 | distance = new (GetGraph()->GetArena()) HNeg(distance->GetType(), distance); |
| 1287 | invoke->GetBlock()->InsertInstructionBefore(distance, invoke); |
| 1288 | } |
| 1289 | HRor* ror = new (GetGraph()->GetArena()) HRor(value->GetType(), value, distance); |
| 1290 | invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror); |
| 1291 | // Remove ClinitCheck and LoadClass, if possible. |
| 1292 | HInstruction* clinit = invoke->InputAt(invoke->InputCount() - 1); |
| 1293 | if (clinit->IsClinitCheck() && !clinit->HasUses()) { |
| 1294 | clinit->GetBlock()->RemoveInstruction(clinit); |
| 1295 | HInstruction* ldclass = clinit->InputAt(0); |
| 1296 | if (ldclass->IsLoadClass() && !ldclass->HasUses()) { |
| 1297 | ldclass->GetBlock()->RemoveInstruction(ldclass); |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1302 | static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) { |
| 1303 | if (potential_length->IsArrayLength()) { |
| 1304 | return potential_length->InputAt(0) == potential_array; |
| 1305 | } |
| 1306 | |
| 1307 | if (potential_array->IsNewArray()) { |
| 1308 | return potential_array->InputAt(0) == potential_length; |
| 1309 | } |
| 1310 | |
| 1311 | return false; |
| 1312 | } |
| 1313 | |
| 1314 | void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) { |
| 1315 | HInstruction* source = instruction->InputAt(0); |
| 1316 | HInstruction* destination = instruction->InputAt(2); |
| 1317 | HInstruction* count = instruction->InputAt(4); |
| 1318 | SystemArrayCopyOptimizations optimizations(instruction); |
| 1319 | if (CanEnsureNotNullAt(source, instruction)) { |
| 1320 | optimizations.SetSourceIsNotNull(); |
| 1321 | } |
| 1322 | if (CanEnsureNotNullAt(destination, instruction)) { |
| 1323 | optimizations.SetDestinationIsNotNull(); |
| 1324 | } |
| 1325 | if (destination == source) { |
| 1326 | optimizations.SetDestinationIsSource(); |
| 1327 | } |
| 1328 | |
| 1329 | if (IsArrayLengthOf(count, source)) { |
| 1330 | optimizations.SetCountIsSourceLength(); |
| 1331 | } |
| 1332 | |
| 1333 | if (IsArrayLengthOf(count, destination)) { |
| 1334 | optimizations.SetCountIsDestinationLength(); |
| 1335 | } |
| 1336 | |
| 1337 | { |
| 1338 | ScopedObjectAccess soa(Thread::Current()); |
| 1339 | ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo(); |
| 1340 | if (destination_rti.IsValid()) { |
| 1341 | if (destination_rti.IsObjectArray()) { |
| 1342 | if (destination_rti.IsExact()) { |
| 1343 | optimizations.SetDoesNotNeedTypeCheck(); |
| 1344 | } |
| 1345 | optimizations.SetDestinationIsTypedObjectArray(); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1346 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1347 | if (destination_rti.IsPrimitiveArrayClass()) { |
| 1348 | optimizations.SetDestinationIsPrimitiveArray(); |
| 1349 | } else if (destination_rti.IsNonPrimitiveArrayClass()) { |
| 1350 | optimizations.SetDestinationIsNonPrimitiveArray(); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1351 | } |
| 1352 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 1353 | ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo(); |
| 1354 | if (source_rti.IsValid()) { |
| 1355 | if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) { |
| 1356 | optimizations.SetDoesNotNeedTypeCheck(); |
| 1357 | } |
| 1358 | if (source_rti.IsPrimitiveArrayClass()) { |
| 1359 | optimizations.SetSourceIsPrimitiveArray(); |
| 1360 | } else if (source_rti.IsNonPrimitiveArrayClass()) { |
| 1361 | optimizations.SetSourceIsNonPrimitiveArray(); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) { |
| 1368 | if (instruction->GetIntrinsic() == Intrinsics::kStringEquals) { |
| 1369 | SimplifyStringEquals(instruction); |
| 1370 | } else if (instruction->GetIntrinsic() == Intrinsics::kSystemArrayCopy) { |
| 1371 | SimplifySystemArrayCopy(instruction); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1372 | } else if (instruction->GetIntrinsic() == Intrinsics::kIntegerRotateRight || |
| 1373 | instruction->GetIntrinsic() == Intrinsics::kLongRotateRight) { |
| 1374 | SimplifyRotate(instruction, false); |
| 1375 | } else if (instruction->GetIntrinsic() == Intrinsics::kIntegerRotateLeft || |
| 1376 | instruction->GetIntrinsic() == Intrinsics::kLongRotateLeft) { |
| 1377 | SimplifyRotate(instruction, true); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
Aart Bik | bb245d1 | 2015-10-19 11:05:03 -0700 | [diff] [blame] | 1381 | void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) { |
| 1382 | HInstruction* cond = deoptimize->InputAt(0); |
| 1383 | if (cond->IsConstant()) { |
| 1384 | if (cond->AsIntConstant()->IsZero()) { |
| 1385 | // Never deopt: instruction can be removed. |
| 1386 | deoptimize->GetBlock()->RemoveInstruction(deoptimize); |
| 1387 | } else { |
| 1388 | // Always deopt. |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1393 | } // namespace art |