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