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