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