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 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 19 | #include "art_method-inl.h" |
| 20 | #include "class_linker-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 21 | #include "class_root-inl.h" |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 22 | #include "data_type-inl.h" |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 23 | #include "escape.h" |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 24 | #include "intrinsics.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 26 | #include "optimizing/nodes.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 27 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "sharpening.h" |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 29 | #include "string_builder_append.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 30 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 31 | namespace art { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 32 | |
Artem Serov | cced8ba | 2017-07-19 18:18:09 +0100 | [diff] [blame] | 33 | // Whether to run an exhaustive test of individual HInstructions cloning when each instruction |
| 34 | // is replaced with its copy if it is clonable. |
| 35 | static constexpr bool kTestInstructionClonerExhaustively = false; |
| 36 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 37 | class InstructionSimplifierVisitor : public HGraphDelegateVisitor { |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 38 | public: |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 39 | InstructionSimplifierVisitor(HGraph* graph, |
| 40 | CodeGenerator* codegen, |
Evgeny Astigeevich | 6587d91 | 2020-06-12 10:51:43 +0100 | [diff] [blame] | 41 | OptimizingCompilerStats* stats, |
| 42 | bool be_loop_friendly) |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 43 | : HGraphDelegateVisitor(graph), |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 44 | codegen_(codegen), |
Evgeny Astigeevich | 6587d91 | 2020-06-12 10:51:43 +0100 | [diff] [blame] | 45 | stats_(stats), |
| 46 | be_loop_friendly_(be_loop_friendly) {} |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 47 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 48 | bool Run(); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 49 | |
| 50 | private: |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 51 | void RecordSimplification() { |
| 52 | simplification_occurred_ = true; |
| 53 | simplifications_at_current_position_++; |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame] | 54 | MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplifications); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 57 | bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 58 | bool TryReplaceWithRotate(HBinaryOperation* instruction); |
| 59 | bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 60 | bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 61 | bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl); |
| 62 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 63 | bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop); |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 64 | // `op` should be either HOr or HAnd. |
| 65 | // De Morgan's laws: |
| 66 | // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b) |
| 67 | bool TryDeMorganNegationFactoring(HBinaryOperation* op); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 68 | bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction); |
| 69 | bool TrySubtractionChainSimplification(HBinaryOperation* instruction); |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 70 | bool TryCombineVecMultiplyAccumulate(HVecMul* mul); |
Evgeny Astigeevich | 6587d91 | 2020-06-12 10:51:43 +0100 | [diff] [blame] | 71 | void TryToReuseDiv(HRem* rem); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 72 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 73 | void VisitShift(HBinaryOperation* shift); |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 74 | void VisitEqual(HEqual* equal) override; |
| 75 | void VisitNotEqual(HNotEqual* equal) override; |
| 76 | void VisitBooleanNot(HBooleanNot* bool_not) override; |
| 77 | void VisitInstanceFieldSet(HInstanceFieldSet* equal) override; |
| 78 | void VisitStaticFieldSet(HStaticFieldSet* equal) override; |
| 79 | void VisitArraySet(HArraySet* equal) override; |
| 80 | void VisitTypeConversion(HTypeConversion* instruction) override; |
| 81 | void VisitNullCheck(HNullCheck* instruction) override; |
| 82 | void VisitArrayLength(HArrayLength* instruction) override; |
| 83 | void VisitCheckCast(HCheckCast* instruction) override; |
| 84 | void VisitAbs(HAbs* instruction) override; |
| 85 | void VisitAdd(HAdd* instruction) override; |
| 86 | void VisitAnd(HAnd* instruction) override; |
| 87 | void VisitCondition(HCondition* instruction) override; |
| 88 | void VisitGreaterThan(HGreaterThan* condition) override; |
| 89 | void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) override; |
| 90 | void VisitLessThan(HLessThan* condition) override; |
| 91 | void VisitLessThanOrEqual(HLessThanOrEqual* condition) override; |
| 92 | void VisitBelow(HBelow* condition) override; |
| 93 | void VisitBelowOrEqual(HBelowOrEqual* condition) override; |
| 94 | void VisitAbove(HAbove* condition) override; |
| 95 | void VisitAboveOrEqual(HAboveOrEqual* condition) override; |
| 96 | void VisitDiv(HDiv* instruction) override; |
Evgeny Astigeevich | 6587d91 | 2020-06-12 10:51:43 +0100 | [diff] [blame] | 97 | void VisitRem(HRem* instruction) override; |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 98 | void VisitMul(HMul* instruction) override; |
| 99 | void VisitNeg(HNeg* instruction) override; |
| 100 | void VisitNot(HNot* instruction) override; |
| 101 | void VisitOr(HOr* instruction) override; |
| 102 | void VisitShl(HShl* instruction) override; |
| 103 | void VisitShr(HShr* instruction) override; |
| 104 | void VisitSub(HSub* instruction) override; |
| 105 | void VisitUShr(HUShr* instruction) override; |
| 106 | void VisitXor(HXor* instruction) override; |
| 107 | void VisitSelect(HSelect* select) override; |
| 108 | void VisitIf(HIf* instruction) override; |
| 109 | void VisitInstanceOf(HInstanceOf* instruction) override; |
| 110 | void VisitInvoke(HInvoke* invoke) override; |
| 111 | void VisitDeoptimize(HDeoptimize* deoptimize) override; |
| 112 | void VisitVecMul(HVecMul* instruction) override; |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 113 | void VisitPredicatedInstanceFieldGet(HPredicatedInstanceFieldGet* instruction) override; |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 114 | |
| 115 | bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 116 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 117 | void SimplifySystemArrayCopy(HInvoke* invoke); |
| 118 | void SimplifyStringEquals(HInvoke* invoke); |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 119 | void SimplifyFP2Int(HInvoke* invoke); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 120 | void SimplifyStringCharAt(HInvoke* invoke); |
Vladimir Marko | 5f84607 | 2020-04-09 13:20:11 +0100 | [diff] [blame] | 121 | void SimplifyStringLength(HInvoke* invoke); |
Vladimir Marko | 6fa4404 | 2018-03-19 18:42:49 +0000 | [diff] [blame] | 122 | void SimplifyStringIndexOf(HInvoke* invoke); |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 123 | void SimplifyNPEOnArgN(HInvoke* invoke, size_t); |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 124 | void SimplifyReturnThis(HInvoke* invoke); |
| 125 | void SimplifyAllocationIntrinsic(HInvoke* invoke); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 126 | |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 127 | CodeGenerator* codegen_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 128 | OptimizingCompilerStats* stats_; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 129 | bool simplification_occurred_ = false; |
| 130 | int simplifications_at_current_position_ = 0; |
Evgeny Astigeevich | 6587d91 | 2020-06-12 10:51:43 +0100 | [diff] [blame] | 131 | // Prohibit optimizations which can affect HInductionVarAnalysis/HLoopOptimization |
| 132 | // and prevent loop optimizations: |
| 133 | // true - avoid such optimizations. |
| 134 | // false - allow such optimizations. |
| 135 | // Checked by the following optimizations: |
| 136 | // - TryToReuseDiv: simplification of Div+Rem into Div+Mul+Sub. |
| 137 | bool be_loop_friendly_; |
Aart Bik | 2767f4b | 2016-10-28 15:03:53 -0700 | [diff] [blame] | 138 | // We ensure we do not loop infinitely. The value should not be too high, since that |
| 139 | // would allow looping around the same basic block too many times. The value should |
| 140 | // not be too low either, however, since we want to allow revisiting a basic block |
| 141 | // with many statements and simplifications at least once. |
| 142 | static constexpr int kMaxSamePositionSimplifications = 50; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 145 | bool InstructionSimplifier::Run() { |
Artem Serov | cced8ba | 2017-07-19 18:18:09 +0100 | [diff] [blame] | 146 | if (kTestInstructionClonerExhaustively) { |
| 147 | CloneAndReplaceInstructionVisitor visitor(graph_); |
| 148 | visitor.VisitReversePostOrder(); |
| 149 | } |
| 150 | |
Evgeny Astigeevich | 6587d91 | 2020-06-12 10:51:43 +0100 | [diff] [blame] | 151 | bool be_loop_friendly = (use_all_optimizations_ == false); |
| 152 | |
| 153 | InstructionSimplifierVisitor visitor(graph_, codegen_, stats_, be_loop_friendly); |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 154 | return visitor.Run(); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 157 | bool InstructionSimplifierVisitor::Run() { |
| 158 | bool didSimplify = false; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 159 | // Iterate in reverse post order to open up more simplifications to users |
| 160 | // of instructions that got simplified. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 161 | for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) { |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 162 | // The simplification of an instruction to another instruction may yield |
| 163 | // possibilities for other simplifications. So although we perform a reverse |
| 164 | // post order visit, we sometimes need to revisit an instruction index. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 165 | do { |
| 166 | simplification_occurred_ = false; |
| 167 | VisitBasicBlock(block); |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 168 | if (simplification_occurred_) { |
| 169 | didSimplify = true; |
| 170 | } |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 171 | } while (simplification_occurred_ && |
| 172 | (simplifications_at_current_position_ < kMaxSamePositionSimplifications)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 173 | simplifications_at_current_position_ = 0; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 174 | } |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 175 | return didSimplify; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 178 | namespace { |
| 179 | |
| 180 | bool AreAllBitsSet(HConstant* constant) { |
| 181 | return Int64FromConstant(constant) == -1; |
| 182 | } |
| 183 | |
| 184 | } // namespace |
| 185 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 186 | // Returns true if the code was simplified to use only one negation operation |
| 187 | // after the binary operation instead of one on each of the inputs. |
| 188 | bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) { |
| 189 | DCHECK(binop->IsAdd() || binop->IsSub()); |
| 190 | DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg()); |
| 191 | HNeg* left_neg = binop->GetLeft()->AsNeg(); |
| 192 | HNeg* right_neg = binop->GetRight()->AsNeg(); |
| 193 | if (!left_neg->HasOnlyOneNonEnvironmentUse() || |
| 194 | !right_neg->HasOnlyOneNonEnvironmentUse()) { |
| 195 | return false; |
| 196 | } |
| 197 | // Replace code looking like |
| 198 | // NEG tmp1, a |
| 199 | // NEG tmp2, b |
| 200 | // ADD dst, tmp1, tmp2 |
| 201 | // with |
| 202 | // ADD tmp, a, b |
| 203 | // NEG dst, tmp |
Serdjuk, Nikolay Y | aae9e66 | 2015-08-21 13:26:34 +0600 | [diff] [blame] | 204 | // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point. |
| 205 | // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`, |
| 206 | // while the later yields `-0.0`. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 207 | if (!DataType::IsIntegralType(binop->GetType())) { |
Serdjuk, Nikolay Y | aae9e66 | 2015-08-21 13:26:34 +0600 | [diff] [blame] | 208 | return false; |
| 209 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 210 | binop->ReplaceInput(left_neg->GetInput(), 0); |
| 211 | binop->ReplaceInput(right_neg->GetInput(), 1); |
| 212 | left_neg->GetBlock()->RemoveInstruction(left_neg); |
| 213 | right_neg->GetBlock()->RemoveInstruction(right_neg); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 214 | HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(binop->GetType(), binop); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 215 | binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext()); |
| 216 | binop->ReplaceWithExceptInReplacementAtIndex(neg, 0); |
| 217 | RecordSimplification(); |
| 218 | return true; |
| 219 | } |
| 220 | |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 221 | bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) { |
| 222 | DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 223 | DataType::Type type = op->GetType(); |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 224 | HInstruction* left = op->GetLeft(); |
| 225 | HInstruction* right = op->GetRight(); |
| 226 | |
| 227 | // We can apply De Morgan's laws if both inputs are Not's and are only used |
| 228 | // by `op`. |
Alexandre Rames | 9f98025 | 2016-02-05 14:00:28 +0000 | [diff] [blame] | 229 | if (((left->IsNot() && right->IsNot()) || |
| 230 | (left->IsBooleanNot() && right->IsBooleanNot())) && |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 231 | left->HasOnlyOneNonEnvironmentUse() && |
| 232 | right->HasOnlyOneNonEnvironmentUse()) { |
| 233 | // Replace code looking like |
| 234 | // NOT nota, a |
| 235 | // NOT notb, b |
| 236 | // AND dst, nota, notb (respectively OR) |
| 237 | // with |
| 238 | // OR or, a, b (respectively AND) |
| 239 | // NOT dest, or |
Alexandre Rames | 9f98025 | 2016-02-05 14:00:28 +0000 | [diff] [blame] | 240 | HInstruction* src_left = left->InputAt(0); |
| 241 | HInstruction* src_right = right->InputAt(0); |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 242 | uint32_t dex_pc = op->GetDexPc(); |
| 243 | |
| 244 | // Remove the negations on the inputs. |
| 245 | left->ReplaceWith(src_left); |
| 246 | right->ReplaceWith(src_right); |
| 247 | left->GetBlock()->RemoveInstruction(left); |
| 248 | right->GetBlock()->RemoveInstruction(right); |
| 249 | |
| 250 | // Replace the `HAnd` or `HOr`. |
| 251 | HBinaryOperation* hbin; |
| 252 | if (op->IsAnd()) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 253 | hbin = new (GetGraph()->GetAllocator()) HOr(type, src_left, src_right, dex_pc); |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 254 | } else { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 255 | hbin = new (GetGraph()->GetAllocator()) HAnd(type, src_left, src_right, dex_pc); |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 256 | } |
Alexandre Rames | 9f98025 | 2016-02-05 14:00:28 +0000 | [diff] [blame] | 257 | HInstruction* hnot; |
| 258 | if (left->IsBooleanNot()) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 259 | hnot = new (GetGraph()->GetAllocator()) HBooleanNot(hbin, dex_pc); |
Alexandre Rames | 9f98025 | 2016-02-05 14:00:28 +0000 | [diff] [blame] | 260 | } else { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 261 | hnot = new (GetGraph()->GetAllocator()) HNot(type, hbin, dex_pc); |
Alexandre Rames | 9f98025 | 2016-02-05 14:00:28 +0000 | [diff] [blame] | 262 | } |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 263 | |
| 264 | op->GetBlock()->InsertInstructionBefore(hbin, op); |
| 265 | op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot); |
| 266 | |
| 267 | RecordSimplification(); |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | return false; |
| 272 | } |
| 273 | |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 274 | bool InstructionSimplifierVisitor::TryCombineVecMultiplyAccumulate(HVecMul* mul) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 275 | DataType::Type type = mul->GetPackedType(); |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 276 | InstructionSet isa = codegen_->GetInstructionSet(); |
| 277 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 278 | case InstructionSet::kArm64: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 279 | if (!(type == DataType::Type::kUint8 || |
| 280 | type == DataType::Type::kInt8 || |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 281 | type == DataType::Type::kUint16 || |
| 282 | type == DataType::Type::kInt16 || |
| 283 | type == DataType::Type::kInt32)) { |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | break; |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 287 | default: |
| 288 | return false; |
| 289 | } |
| 290 | |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 291 | ArenaAllocator* allocator = mul->GetBlock()->GetGraph()->GetAllocator(); |
Artem Serov | 8ba4de1 | 2019-12-04 21:10:23 +0000 | [diff] [blame] | 292 | if (!mul->HasOnlyOneNonEnvironmentUse()) { |
| 293 | return false; |
| 294 | } |
| 295 | HInstruction* binop = mul->GetUses().front().GetUser(); |
| 296 | if (!binop->IsVecAdd() && !binop->IsVecSub()) { |
| 297 | return false; |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Artem Serov | 8ba4de1 | 2019-12-04 21:10:23 +0000 | [diff] [blame] | 300 | // Replace code looking like |
| 301 | // VECMUL tmp, x, y |
| 302 | // VECADD/SUB dst, acc, tmp |
| 303 | // with |
| 304 | // VECMULACC dst, acc, x, y |
| 305 | // Note that we do not want to (unconditionally) perform the merge when the |
| 306 | // multiplication has multiple uses and it can be merged in all of them. |
| 307 | // Multiple uses could happen on the same control-flow path, and we would |
| 308 | // then increase the amount of work. In the future we could try to evaluate |
| 309 | // whether all uses are on different control-flow paths (using dominance and |
| 310 | // reverse-dominance information) and only perform the merge when they are. |
| 311 | HInstruction* accumulator = nullptr; |
| 312 | HVecBinaryOperation* vec_binop = binop->AsVecBinaryOperation(); |
| 313 | HInstruction* binop_left = vec_binop->GetLeft(); |
| 314 | HInstruction* binop_right = vec_binop->GetRight(); |
| 315 | // This is always true since the `HVecMul` has only one use (which is checked above). |
| 316 | DCHECK_NE(binop_left, binop_right); |
| 317 | if (binop_right == mul) { |
| 318 | accumulator = binop_left; |
| 319 | } else { |
| 320 | DCHECK_EQ(binop_left, mul); |
| 321 | // Only addition is commutative. |
| 322 | if (!binop->IsVecAdd()) { |
| 323 | return false; |
| 324 | } |
| 325 | accumulator = binop_right; |
| 326 | } |
| 327 | |
| 328 | DCHECK(accumulator != nullptr); |
| 329 | HInstruction::InstructionKind kind = |
| 330 | binop->IsVecAdd() ? HInstruction::kAdd : HInstruction::kSub; |
| 331 | |
| 332 | bool predicated_simd = vec_binop->IsPredicated(); |
| 333 | if (predicated_simd && !HVecOperation::HaveSamePredicate(vec_binop, mul)) { |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | HVecMultiplyAccumulate* mulacc = |
| 338 | new (allocator) HVecMultiplyAccumulate(allocator, |
| 339 | kind, |
| 340 | accumulator, |
| 341 | mul->GetLeft(), |
| 342 | mul->GetRight(), |
| 343 | vec_binop->GetPackedType(), |
| 344 | vec_binop->GetVectorLength(), |
| 345 | vec_binop->GetDexPc()); |
| 346 | |
| 347 | |
| 348 | |
| 349 | vec_binop->GetBlock()->ReplaceAndRemoveInstructionWith(vec_binop, mulacc); |
| 350 | if (predicated_simd) { |
| 351 | mulacc->SetGoverningPredicate(vec_binop->GetGoverningPredicate(), |
| 352 | vec_binop->GetPredicationKind()); |
| 353 | } |
| 354 | |
| 355 | DCHECK(!mul->HasUses()); |
| 356 | mul->GetBlock()->RemoveInstruction(mul); |
| 357 | return true; |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 360 | void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) { |
| 361 | DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 362 | HInstruction* shift_amount = instruction->GetRight(); |
| 363 | HInstruction* value = instruction->GetLeft(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 364 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 365 | int64_t implicit_mask = (value->GetType() == DataType::Type::kInt64) |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 366 | ? kMaxLongShiftDistance |
| 367 | : kMaxIntShiftDistance; |
| 368 | |
| 369 | if (shift_amount->IsConstant()) { |
| 370 | int64_t cst = Int64FromConstant(shift_amount->AsConstant()); |
Aart Bik | 50e20d5 | 2017-05-05 14:07:29 -0700 | [diff] [blame] | 371 | int64_t masked_cst = cst & implicit_mask; |
| 372 | if (masked_cst == 0) { |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 373 | // Replace code looking like |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 374 | // SHL dst, value, 0 |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 375 | // with |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 376 | // value |
| 377 | instruction->ReplaceWith(value); |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 378 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 379 | RecordSimplification(); |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 380 | return; |
Aart Bik | 50e20d5 | 2017-05-05 14:07:29 -0700 | [diff] [blame] | 381 | } else if (masked_cst != cst) { |
| 382 | // Replace code looking like |
| 383 | // SHL dst, value, cst |
| 384 | // where cst exceeds maximum distance with the equivalent |
| 385 | // SHL dst, value, cst & implicit_mask |
| 386 | // (as defined by shift semantics). This ensures other |
| 387 | // optimizations do not need to special case for such situations. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 388 | DCHECK_EQ(shift_amount->GetType(), DataType::Type::kInt32); |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 389 | instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index= */ 1); |
Aart Bik | 50e20d5 | 2017-05-05 14:07:29 -0700 | [diff] [blame] | 390 | RecordSimplification(); |
| 391 | return; |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | // Shift operations implicitly mask the shift amount according to the type width. Get rid of |
Vladimir Marko | 7033d49 | 2017-09-28 16:32:24 +0100 | [diff] [blame] | 396 | // unnecessary And/Or/Xor/Add/Sub/TypeConversion operations on the shift amount that do not |
| 397 | // affect the relevant bits. |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 398 | // Replace code looking like |
Vladimir Marko | 7033d49 | 2017-09-28 16:32:24 +0100 | [diff] [blame] | 399 | // AND adjusted_shift, shift, <superset of implicit mask> |
| 400 | // [OR/XOR/ADD/SUB adjusted_shift, shift, <value not overlapping with implicit mask>] |
| 401 | // [<conversion-from-integral-non-64-bit-type> adjusted_shift, shift] |
| 402 | // SHL dst, value, adjusted_shift |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 403 | // with |
| 404 | // SHL dst, value, shift |
Vladimir Marko | 7033d49 | 2017-09-28 16:32:24 +0100 | [diff] [blame] | 405 | if (shift_amount->IsAnd() || |
| 406 | shift_amount->IsOr() || |
| 407 | shift_amount->IsXor() || |
| 408 | shift_amount->IsAdd() || |
| 409 | shift_amount->IsSub()) { |
| 410 | int64_t required_result = shift_amount->IsAnd() ? implicit_mask : 0; |
| 411 | HBinaryOperation* bin_op = shift_amount->AsBinaryOperation(); |
| 412 | HConstant* mask = bin_op->GetConstantRight(); |
| 413 | if (mask != nullptr && (Int64FromConstant(mask) & implicit_mask) == required_result) { |
| 414 | instruction->ReplaceInput(bin_op->GetLeastConstantLeft(), 1); |
Alexandre Rames | 5051844 | 2016-06-27 11:39:19 +0100 | [diff] [blame] | 415 | RecordSimplification(); |
Vladimir Marko | 7033d49 | 2017-09-28 16:32:24 +0100 | [diff] [blame] | 416 | return; |
| 417 | } |
| 418 | } else if (shift_amount->IsTypeConversion()) { |
| 419 | DCHECK_NE(shift_amount->GetType(), DataType::Type::kBool); // We never convert to bool. |
| 420 | DataType::Type source_type = shift_amount->InputAt(0)->GetType(); |
| 421 | // Non-integral and 64-bit source types require an explicit type conversion. |
| 422 | if (DataType::IsIntegralType(source_type) && !DataType::Is64BitType(source_type)) { |
| 423 | instruction->ReplaceInput(shift_amount->AsTypeConversion()->GetInput(), 1); |
| 424 | RecordSimplification(); |
| 425 | return; |
Mark Mendell | ba56d06 | 2015-05-05 21:34:03 -0400 | [diff] [blame] | 426 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 430 | static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) { |
| 431 | return (sub->GetRight() == other && |
| 432 | sub->GetLeft()->IsConstant() && |
| 433 | (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0); |
| 434 | } |
| 435 | |
| 436 | bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op, |
| 437 | HUShr* ushr, |
| 438 | HShl* shl) { |
Roland Levillain | 22c4922 | 2016-03-18 14:04:28 +0000 | [diff] [blame] | 439 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 440 | HRor* ror = |
| 441 | new (GetGraph()->GetAllocator()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight()); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 442 | op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror); |
| 443 | if (!ushr->HasUses()) { |
| 444 | ushr->GetBlock()->RemoveInstruction(ushr); |
| 445 | } |
| 446 | if (!ushr->GetRight()->HasUses()) { |
| 447 | ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight()); |
| 448 | } |
| 449 | if (!shl->HasUses()) { |
| 450 | shl->GetBlock()->RemoveInstruction(shl); |
| 451 | } |
| 452 | if (!shl->GetRight()->HasUses()) { |
| 453 | shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight()); |
| 454 | } |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 455 | RecordSimplification(); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 456 | return true; |
| 457 | } |
| 458 | |
| 459 | // Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation. |
| 460 | bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) { |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 461 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 462 | HInstruction* left = op->GetLeft(); |
| 463 | HInstruction* right = op->GetRight(); |
| 464 | // If we have an UShr and a Shl (in either order). |
| 465 | if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) { |
| 466 | HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr(); |
| 467 | HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 468 | DCHECK(DataType::IsIntOrLongType(ushr->GetType())); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 469 | if (ushr->GetType() == shl->GetType() && |
| 470 | ushr->GetLeft() == shl->GetLeft()) { |
| 471 | if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) { |
| 472 | // Shift distances are both constant, try replacing with Ror if they |
| 473 | // add up to the register size. |
| 474 | return TryReplaceWithRotateConstantPattern(op, ushr, shl); |
| 475 | } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) { |
| 476 | // Shift distances are potentially of the form x and (reg_size - x). |
| 477 | return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl); |
| 478 | } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) { |
| 479 | // Shift distances are potentially of the form d and -d. |
| 480 | return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | // Try replacing code looking like (x >>> #rdist OP x << #ldist): |
| 488 | // UShr dst, x, #rdist |
| 489 | // Shl tmp, x, #ldist |
| 490 | // OP dst, dst, tmp |
| 491 | // or like (x >>> #rdist OP x << #-ldist): |
| 492 | // UShr dst, x, #rdist |
| 493 | // Shl tmp, x, #-ldist |
| 494 | // OP dst, dst, tmp |
| 495 | // with |
| 496 | // Ror dst, x, #rdist |
| 497 | bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op, |
| 498 | HUShr* ushr, |
| 499 | HShl* shl) { |
| 500 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 501 | size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte; |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 502 | size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant()); |
| 503 | size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant()); |
| 504 | if (((ldist + rdist) & (reg_bits - 1)) == 0) { |
| 505 | ReplaceRotateWithRor(op, ushr, shl); |
| 506 | return true; |
| 507 | } |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | // Replace code looking like (x >>> -d OP x << d): |
| 512 | // Neg neg, d |
| 513 | // UShr dst, x, neg |
| 514 | // Shl tmp, x, d |
| 515 | // OP dst, dst, tmp |
| 516 | // with |
| 517 | // Neg neg, d |
| 518 | // Ror dst, x, neg |
| 519 | // *** OR *** |
| 520 | // Replace code looking like (x >>> d OP x << -d): |
| 521 | // UShr dst, x, d |
| 522 | // Neg neg, d |
| 523 | // Shl tmp, x, neg |
| 524 | // OP dst, dst, tmp |
| 525 | // with |
| 526 | // Ror dst, x, d |
| 527 | bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, |
| 528 | HUShr* ushr, |
| 529 | HShl* shl) { |
| 530 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 531 | DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()); |
| 532 | bool neg_is_left = shl->GetRight()->IsNeg(); |
| 533 | HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg(); |
| 534 | // And the shift distance being negated is the distance being shifted the other way. |
| 535 | if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) { |
| 536 | ReplaceRotateWithRor(op, ushr, shl); |
| 537 | } |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | // Try replacing code looking like (x >>> d OP x << (#bits - d)): |
| 542 | // UShr dst, x, d |
| 543 | // Sub ld, #bits, d |
| 544 | // Shl tmp, x, ld |
| 545 | // OP dst, dst, tmp |
| 546 | // with |
| 547 | // Ror dst, x, d |
| 548 | // *** OR *** |
| 549 | // Replace code looking like (x >>> (#bits - d) OP x << d): |
| 550 | // Sub rd, #bits, d |
| 551 | // UShr dst, x, rd |
| 552 | // Shl tmp, x, d |
| 553 | // OP dst, dst, tmp |
| 554 | // with |
| 555 | // Neg neg, d |
| 556 | // Ror dst, x, neg |
| 557 | bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, |
| 558 | HUShr* ushr, |
| 559 | HShl* shl) { |
| 560 | DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()); |
| 561 | DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 562 | size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte; |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 563 | HInstruction* shl_shift = shl->GetRight(); |
| 564 | HInstruction* ushr_shift = ushr->GetRight(); |
| 565 | if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) || |
| 566 | (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) { |
| 567 | return ReplaceRotateWithRor(op, ushr, shl); |
| 568 | } |
| 569 | return false; |
| 570 | } |
| 571 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 572 | void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) { |
| 573 | HInstruction* obj = null_check->InputAt(0); |
| 574 | if (!obj->CanBeNull()) { |
| 575 | null_check->ReplaceWith(obj); |
| 576 | null_check->GetBlock()->RemoveInstruction(null_check); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 577 | if (stats_ != nullptr) { |
| 578 | stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 583 | bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const { |
| 584 | if (!input->CanBeNull()) { |
| 585 | return true; |
| 586 | } |
| 587 | |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 588 | for (const HUseListNode<HInstruction*>& use : input->GetUses()) { |
| 589 | HInstruction* user = use.GetUser(); |
| 590 | if (user->IsNullCheck() && user->StrictlyDominates(at)) { |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | } |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 594 | |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 595 | return false; |
| 596 | } |
| 597 | |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 598 | // Returns whether doing a type test between the class of `object` against `klass` has |
| 599 | // a statically known outcome. The result of the test is stored in `outcome`. |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 600 | static bool TypeCheckHasKnownOutcome(ReferenceTypeInfo class_rti, |
| 601 | HInstruction* object, |
| 602 | /*out*/bool* outcome) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 603 | DCHECK(!object->IsNullConstant()) << "Null constants should be special cased"; |
| 604 | ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo(); |
| 605 | ScopedObjectAccess soa(Thread::Current()); |
| 606 | if (!obj_rti.IsValid()) { |
| 607 | // We run the simplifier before the reference type propagation so type info might not be |
| 608 | // available. |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 609 | return false; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 610 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 611 | |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 612 | if (!class_rti.IsValid()) { |
| 613 | // Happens when the loaded class is unresolved. |
Alex Light | bb550e4 | 2021-04-21 17:04:13 -0700 | [diff] [blame] | 614 | if (obj_rti.IsExact()) { |
| 615 | // outcome == 'true' && obj_rti is valid implies that class_rti is valid. |
| 616 | // Since that's a contradiction we must not pass this check. |
| 617 | *outcome = false; |
| 618 | return true; |
| 619 | } else { |
| 620 | // We aren't able to say anything in particular since we don't know the |
| 621 | // exact type of the object. |
| 622 | return false; |
| 623 | } |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 624 | } |
| 625 | DCHECK(class_rti.IsExact()); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 626 | if (class_rti.IsSupertypeOf(obj_rti)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 627 | *outcome = true; |
| 628 | return true; |
| 629 | } else if (obj_rti.IsExact()) { |
| 630 | // The test failed at compile time so will also fail at runtime. |
| 631 | *outcome = false; |
| 632 | return true; |
Nicolas Geoffray | 7cb499b | 2015-06-17 11:35:11 +0100 | [diff] [blame] | 633 | } else if (!class_rti.IsInterface() |
| 634 | && !obj_rti.IsInterface() |
| 635 | && !obj_rti.IsSupertypeOf(class_rti)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 636 | // Different type hierarchy. The test will fail. |
| 637 | *outcome = false; |
| 638 | return true; |
| 639 | } |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) { |
| 644 | HInstruction* object = check_cast->InputAt(0); |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 645 | if (CanEnsureNotNullAt(object, check_cast)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 646 | check_cast->ClearMustDoNullCheck(); |
| 647 | } |
| 648 | |
| 649 | if (object->IsNullConstant()) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 650 | check_cast->GetBlock()->RemoveInstruction(check_cast); |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 651 | MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast); |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 652 | return; |
| 653 | } |
| 654 | |
Alex Light | bb550e4 | 2021-04-21 17:04:13 -0700 | [diff] [blame] | 655 | // Minor correctness check. |
| 656 | DCHECK(check_cast->GetTargetClass()->StrictlyDominates(check_cast)) |
| 657 | << "Illegal graph!\n" |
| 658 | << check_cast->DumpWithArgs(); |
| 659 | |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 660 | // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder |
| 661 | // the return value check with the `outcome` check, b/27651442. |
Vladimir Marko | a65ed30 | 2016-03-14 21:21:29 +0000 | [diff] [blame] | 662 | bool outcome = false; |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 663 | if (TypeCheckHasKnownOutcome(check_cast->GetTargetClassRTI(), object, &outcome)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 664 | if (outcome) { |
| 665 | check_cast->GetBlock()->RemoveInstruction(check_cast); |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 666 | MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast); |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 667 | if (check_cast->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) { |
| 668 | HLoadClass* load_class = check_cast->GetTargetClass(); |
Alex Light | bb550e4 | 2021-04-21 17:04:13 -0700 | [diff] [blame] | 669 | if (!load_class->HasUses() && !load_class->NeedsAccessCheck()) { |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 670 | // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw. |
Alex Light | bb550e4 | 2021-04-21 17:04:13 -0700 | [diff] [blame] | 671 | // However, here we know that it cannot because the checkcast was successful, hence |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 672 | // the class was already loaded. |
| 673 | load_class->GetBlock()->RemoveInstruction(load_class); |
| 674 | } |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 675 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 676 | } else { |
Alex Light | bb550e4 | 2021-04-21 17:04:13 -0700 | [diff] [blame] | 677 | // TODO Don't do anything for exceptional cases for now. Ideally we should |
| 678 | // remove all instructions and blocks this instruction dominates and |
| 679 | // replace it with a manual throw. |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 680 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 684 | void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 685 | HInstruction* object = instruction->InputAt(0); |
Calin Juravle | e53fb55 | 2015-10-07 17:51:52 +0100 | [diff] [blame] | 686 | |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 687 | bool can_be_null = true; |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 688 | if (CanEnsureNotNullAt(object, instruction)) { |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 689 | can_be_null = false; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 690 | instruction->ClearMustDoNullCheck(); |
| 691 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 692 | |
| 693 | HGraph* graph = GetGraph(); |
| 694 | if (object->IsNullConstant()) { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame] | 695 | MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf); |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 696 | instruction->ReplaceWith(graph->GetIntConstant(0)); |
| 697 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 698 | RecordSimplification(); |
| 699 | return; |
| 700 | } |
| 701 | |
Alex Light | bb550e4 | 2021-04-21 17:04:13 -0700 | [diff] [blame] | 702 | // Minor correctness check. |
| 703 | DCHECK(instruction->GetTargetClass()->StrictlyDominates(instruction)) |
| 704 | << "Illegal graph!\n" |
| 705 | << instruction->DumpWithArgs(); |
| 706 | |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 707 | // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder |
| 708 | // the return value check with the `outcome` check, b/27651442. |
Vladimir Marko | 24bd895 | 2016-03-15 10:40:33 +0000 | [diff] [blame] | 709 | bool outcome = false; |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 710 | if (TypeCheckHasKnownOutcome(instruction->GetTargetClassRTI(), object, &outcome)) { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame] | 711 | MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf); |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 712 | if (outcome && can_be_null) { |
| 713 | // Type test will succeed, we just need a null test. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 714 | HNotEqual* test = new (graph->GetAllocator()) HNotEqual(graph->GetNullConstant(), object); |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 715 | instruction->GetBlock()->InsertInstructionBefore(test, instruction); |
| 716 | instruction->ReplaceWith(test); |
| 717 | } else { |
| 718 | // We've statically determined the result of the instanceof. |
| 719 | instruction->ReplaceWith(graph->GetIntConstant(outcome)); |
| 720 | } |
| 721 | RecordSimplification(); |
| 722 | instruction->GetBlock()->RemoveInstruction(instruction); |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 723 | if (outcome && instruction->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) { |
| 724 | HLoadClass* load_class = instruction->GetTargetClass(); |
Alex Light | bb550e4 | 2021-04-21 17:04:13 -0700 | [diff] [blame] | 725 | if (!load_class->HasUses() && !load_class->NeedsAccessCheck()) { |
| 726 | // We cannot rely on DCE to remove the class because the `HLoadClass` |
| 727 | // thinks it can throw. However, here we know that it cannot because the |
| 728 | // instanceof check was successful and we don't need to check the |
| 729 | // access, hence the class was already loaded. |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 730 | load_class->GetBlock()->RemoveInstruction(load_class); |
| 731 | } |
Nicolas Geoffray | efa8468 | 2015-08-12 18:28:14 -0700 | [diff] [blame] | 732 | } |
Guillaume Sanchez | 222862c | 2015-06-09 18:33:02 +0100 | [diff] [blame] | 733 | } |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 736 | void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 737 | if ((instruction->GetValue()->GetType() == DataType::Type::kReference) |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 738 | && CanEnsureNotNullAt(instruction->GetValue(), instruction)) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 739 | instruction->ClearValueCanBeNull(); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 744 | if ((instruction->GetValue()->GetType() == DataType::Type::kReference) |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 745 | && CanEnsureNotNullAt(instruction->GetValue(), instruction)) { |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 746 | instruction->ClearValueCanBeNull(); |
| 747 | } |
| 748 | } |
| 749 | |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 750 | static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* allocator, HInstruction* cond) { |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 751 | HInstruction *lhs = cond->InputAt(0); |
| 752 | HInstruction *rhs = cond->InputAt(1); |
| 753 | switch (cond->GetKind()) { |
| 754 | case HInstruction::kEqual: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 755 | return new (allocator) HEqual(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 756 | case HInstruction::kNotEqual: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 757 | return new (allocator) HNotEqual(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 758 | case HInstruction::kLessThan: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 759 | return new (allocator) HGreaterThan(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 760 | case HInstruction::kLessThanOrEqual: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 761 | return new (allocator) HGreaterThanOrEqual(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 762 | case HInstruction::kGreaterThan: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 763 | return new (allocator) HLessThan(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 764 | case HInstruction::kGreaterThanOrEqual: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 765 | return new (allocator) HLessThanOrEqual(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 766 | case HInstruction::kBelow: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 767 | return new (allocator) HAbove(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 768 | case HInstruction::kBelowOrEqual: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 769 | return new (allocator) HAboveOrEqual(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 770 | case HInstruction::kAbove: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 771 | return new (allocator) HBelow(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 772 | case HInstruction::kAboveOrEqual: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 773 | return new (allocator) HBelowOrEqual(rhs, lhs); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 774 | default: |
| 775 | LOG(FATAL) << "Unknown ConditionType " << cond->GetKind(); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 776 | UNREACHABLE(); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 777 | } |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 778 | } |
| 779 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 780 | void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 781 | HInstruction* input_const = equal->GetConstantRight(); |
| 782 | if (input_const != nullptr) { |
| 783 | HInstruction* input_value = equal->GetLeastConstantLeft(); |
Nicolas Geoffray | eb104c8 | 2019-06-03 17:58:34 +0100 | [diff] [blame] | 784 | if ((input_value->GetType() == DataType::Type::kBool) && input_const->IsIntConstant()) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 785 | HBasicBlock* block = equal->GetBlock(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 786 | // We are comparing the boolean to a constant which is of type int and can |
| 787 | // be any constant. |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 788 | if (input_const->AsIntConstant()->IsTrue()) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 789 | // Replace (bool_value == true) with bool_value |
| 790 | equal->ReplaceWith(input_value); |
| 791 | block->RemoveInstruction(equal); |
| 792 | RecordSimplification(); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 793 | } else if (input_const->AsIntConstant()->IsFalse()) { |
Aart Bik | 2767f4b | 2016-10-28 15:03:53 -0700 | [diff] [blame] | 794 | // Replace (bool_value == false) with !bool_value |
Mark Mendell | f652917 | 2015-11-17 11:16:56 -0500 | [diff] [blame] | 795 | equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal)); |
| 796 | block->RemoveInstruction(equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 797 | RecordSimplification(); |
David Brazdil | 1e9ec05 | 2015-06-22 10:26:45 +0100 | [diff] [blame] | 798 | } else { |
| 799 | // Replace (bool_value == integer_not_zero_nor_one_constant) with false |
| 800 | equal->ReplaceWith(GetGraph()->GetIntConstant(0)); |
| 801 | block->RemoveInstruction(equal); |
| 802 | RecordSimplification(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 803 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 804 | } else { |
| 805 | VisitCondition(equal); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 806 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 807 | } else { |
| 808 | VisitCondition(equal); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 812 | void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) { |
| 813 | HInstruction* input_const = not_equal->GetConstantRight(); |
| 814 | if (input_const != nullptr) { |
| 815 | HInstruction* input_value = not_equal->GetLeastConstantLeft(); |
Nicolas Geoffray | eb104c8 | 2019-06-03 17:58:34 +0100 | [diff] [blame] | 816 | if ((input_value->GetType() == DataType::Type::kBool) && input_const->IsIntConstant()) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 817 | HBasicBlock* block = not_equal->GetBlock(); |
Nicolas Geoffray | 3c4ab80 | 2015-06-19 11:42:07 +0100 | [diff] [blame] | 818 | // We are comparing the boolean to a constant which is of type int and can |
| 819 | // be any constant. |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 820 | if (input_const->AsIntConstant()->IsTrue()) { |
Aart Bik | 2767f4b | 2016-10-28 15:03:53 -0700 | [diff] [blame] | 821 | // Replace (bool_value != true) with !bool_value |
Mark Mendell | f652917 | 2015-11-17 11:16:56 -0500 | [diff] [blame] | 822 | not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal)); |
| 823 | block->RemoveInstruction(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 824 | RecordSimplification(); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 825 | } else if (input_const->AsIntConstant()->IsFalse()) { |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 826 | // Replace (bool_value != false) with bool_value |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 827 | not_equal->ReplaceWith(input_value); |
| 828 | block->RemoveInstruction(not_equal); |
| 829 | RecordSimplification(); |
David Brazdil | 1e9ec05 | 2015-06-22 10:26:45 +0100 | [diff] [blame] | 830 | } else { |
| 831 | // Replace (bool_value != integer_not_zero_nor_one_constant) with true |
| 832 | not_equal->ReplaceWith(GetGraph()->GetIntConstant(1)); |
| 833 | block->RemoveInstruction(not_equal); |
| 834 | RecordSimplification(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 835 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 836 | } else { |
| 837 | VisitCondition(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 838 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 839 | } else { |
| 840 | VisitCondition(not_equal); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
| 844 | void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 845 | HInstruction* input = bool_not->InputAt(0); |
| 846 | HInstruction* replace_with = nullptr; |
| 847 | |
| 848 | if (input->IsIntConstant()) { |
| 849 | // Replace !(true/false) with false/true. |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 850 | if (input->AsIntConstant()->IsTrue()) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 851 | replace_with = GetGraph()->GetIntConstant(0); |
| 852 | } else { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 853 | DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue(); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 854 | replace_with = GetGraph()->GetIntConstant(1); |
| 855 | } |
| 856 | } else if (input->IsBooleanNot()) { |
| 857 | // Replace (!(!bool_value)) with bool_value. |
| 858 | replace_with = input->InputAt(0); |
| 859 | } else if (input->IsCondition() && |
| 860 | // Don't change FP compares. The definition of compares involving |
| 861 | // NaNs forces the compares to be done as written by the user. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 862 | !DataType::IsFloatingPointType(input->InputAt(0)->GetType())) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 863 | // Replace condition with its opposite. |
| 864 | replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not); |
| 865 | } |
| 866 | |
| 867 | if (replace_with != nullptr) { |
| 868 | bool_not->ReplaceWith(replace_with); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 869 | bool_not->GetBlock()->RemoveInstruction(bool_not); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 870 | RecordSimplification(); |
| 871 | } |
| 872 | } |
| 873 | |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 874 | // Constructs a new ABS(x) node in the HIR. |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 875 | static HInstruction* NewIntegralAbs(ArenaAllocator* allocator, |
| 876 | HInstruction* x, |
| 877 | HInstruction* cursor) { |
Aart Bik | 2286da2 | 2018-03-22 10:50:22 -0700 | [diff] [blame] | 878 | DataType::Type type = DataType::Kind(x->GetType()); |
Aart Bik | 3dad341 | 2018-02-28 12:01:46 -0800 | [diff] [blame] | 879 | DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64); |
Aart Bik | 142b913 | 2018-03-14 15:12:59 -0700 | [diff] [blame] | 880 | HAbs* abs = new (allocator) HAbs(type, x, cursor->GetDexPc()); |
Aart Bik | 3dad341 | 2018-02-28 12:01:46 -0800 | [diff] [blame] | 881 | cursor->GetBlock()->InsertInstructionBefore(abs, cursor); |
| 882 | return abs; |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Aart Bik | 142b913 | 2018-03-14 15:12:59 -0700 | [diff] [blame] | 885 | // Constructs a new MIN/MAX(x, y) node in the HIR. |
| 886 | static HInstruction* NewIntegralMinMax(ArenaAllocator* allocator, |
| 887 | HInstruction* x, |
| 888 | HInstruction* y, |
| 889 | HInstruction* cursor, |
| 890 | bool is_min) { |
Aart Bik | 2286da2 | 2018-03-22 10:50:22 -0700 | [diff] [blame] | 891 | DataType::Type type = DataType::Kind(x->GetType()); |
Aart Bik | 142b913 | 2018-03-14 15:12:59 -0700 | [diff] [blame] | 892 | DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64); |
| 893 | HBinaryOperation* minmax = nullptr; |
| 894 | if (is_min) { |
| 895 | minmax = new (allocator) HMin(type, x, y, cursor->GetDexPc()); |
| 896 | } else { |
| 897 | minmax = new (allocator) HMax(type, x, y, cursor->GetDexPc()); |
| 898 | } |
| 899 | cursor->GetBlock()->InsertInstructionBefore(minmax, cursor); |
| 900 | return minmax; |
| 901 | } |
| 902 | |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 903 | // Returns true if operands a and b consists of widening type conversions |
| 904 | // (either explicit or implicit) to the given to_type. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 905 | static bool AreLowerPrecisionArgs(DataType::Type to_type, HInstruction* a, HInstruction* b) { |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 906 | if (a->IsTypeConversion() && a->GetType() == to_type) { |
| 907 | a = a->InputAt(0); |
| 908 | } |
| 909 | if (b->IsTypeConversion() && b->GetType() == to_type) { |
| 910 | b = b->InputAt(0); |
| 911 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 912 | DataType::Type type1 = a->GetType(); |
| 913 | DataType::Type type2 = b->GetType(); |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 914 | return (type1 == DataType::Type::kUint8 && type2 == DataType::Type::kUint8) || |
| 915 | (type1 == DataType::Type::kInt8 && type2 == DataType::Type::kInt8) || |
| 916 | (type1 == DataType::Type::kInt16 && type2 == DataType::Type::kInt16) || |
| 917 | (type1 == DataType::Type::kUint16 && type2 == DataType::Type::kUint16) || |
| 918 | (type1 == DataType::Type::kInt32 && type2 == DataType::Type::kInt32 && |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 919 | to_type == DataType::Type::kInt64); |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame] | 922 | // Returns an acceptable substitution for "a" on the select |
| 923 | // construct "a <cmp> b ? c : .." during MIN/MAX recognition. |
| 924 | static HInstruction* AllowInMinMax(IfCondition cmp, |
| 925 | HInstruction* a, |
| 926 | HInstruction* b, |
| 927 | HInstruction* c) { |
| 928 | int64_t value = 0; |
| 929 | if (IsInt64AndGet(b, /*out*/ &value) && |
| 930 | (((cmp == kCondLT || cmp == kCondLE) && c->IsMax()) || |
| 931 | ((cmp == kCondGT || cmp == kCondGE) && c->IsMin()))) { |
| 932 | HConstant* other = c->AsBinaryOperation()->GetConstantRight(); |
| 933 | if (other != nullptr && a == c->AsBinaryOperation()->GetLeastConstantLeft()) { |
| 934 | int64_t other_value = Int64FromConstant(other); |
| 935 | bool is_max = (cmp == kCondLT || cmp == kCondLE); |
| 936 | // Allow the max for a < 100 ? max(a, -100) : .. |
| 937 | // or the min for a > -100 ? min(a, 100) : .. |
| 938 | if (is_max ? (value >= other_value) : (value <= other_value)) { |
| 939 | return c; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | return nullptr; |
| 944 | } |
| 945 | |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 946 | // TODO This should really be done by LSE itself since there is significantly |
| 947 | // more information available there. |
| 948 | void InstructionSimplifierVisitor::VisitPredicatedInstanceFieldGet( |
| 949 | HPredicatedInstanceFieldGet* pred_get) { |
| 950 | HInstruction* target = pred_get->GetTarget(); |
| 951 | HInstruction* default_val = pred_get->GetDefaultValue(); |
| 952 | // TODO Technically we could end up with a case where the target isn't a phi |
| 953 | // (allowing us to eliminate the instruction and replace with either a |
| 954 | // InstanceFieldGet or the default) but due to the ordering of compilation |
| 955 | // passes this can't happen in ART. |
| 956 | if (!target->IsPhi() || !default_val->IsPhi() || default_val->GetBlock() != target->GetBlock()) { |
| 957 | // Already reduced the target or the phi selection will differ between the |
| 958 | // target and default. |
| 959 | return; |
| 960 | } |
| 961 | DCHECK_EQ(default_val->InputCount(), target->InputCount()); |
| 962 | // In the same block both phis only one non-null we can remove the phi from default_val. |
| 963 | HInstruction* single_value = nullptr; |
| 964 | auto inputs = target->GetInputs(); |
| 965 | for (auto [input, idx] : ZipCount(MakeIterationRange(inputs))) { |
| 966 | if (input->CanBeNull()) { |
| 967 | if (single_value == nullptr) { |
| 968 | single_value = default_val->InputAt(idx); |
| 969 | } else if (single_value != default_val->InputAt(idx) && |
| 970 | !single_value->Equals(default_val->InputAt(idx))) { |
Alex Light | 7432805 | 2021-03-29 18:11:23 -0700 | [diff] [blame] | 971 | // Multiple values are associated with potential nulls, can't combine. |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 972 | return; |
| 973 | } |
| 974 | } |
| 975 | } |
Alex Light | 7432805 | 2021-03-29 18:11:23 -0700 | [diff] [blame] | 976 | if (single_value == nullptr) { |
| 977 | // None of the inputs can be null so we can just remove the predicatedness |
| 978 | // of this instruction. |
| 979 | DCHECK(std::none_of(inputs.cbegin(), inputs.cend(), [](const HInstruction* input) { |
| 980 | return input->CanBeNull(); |
| 981 | })); |
| 982 | HInstruction* replace_with = new (GetGraph()->GetAllocator()) |
| 983 | HInstanceFieldGet(pred_get->GetTarget(), |
| 984 | pred_get->GetFieldInfo().GetField(), |
| 985 | pred_get->GetFieldType(), |
| 986 | pred_get->GetFieldOffset(), |
| 987 | pred_get->IsVolatile(), |
| 988 | pred_get->GetFieldInfo().GetFieldIndex(), |
| 989 | pred_get->GetFieldInfo().GetDeclaringClassDefIndex(), |
| 990 | pred_get->GetFieldInfo().GetDexFile(), |
| 991 | pred_get->GetDexPc()); |
| 992 | if (pred_get->GetType() == DataType::Type::kReference) { |
| 993 | replace_with->SetReferenceTypeInfo(pred_get->GetReferenceTypeInfo()); |
| 994 | } |
| 995 | pred_get->GetBlock()->InsertInstructionBefore(replace_with, pred_get); |
| 996 | pred_get->ReplaceWith(replace_with); |
| 997 | pred_get->GetBlock()->RemoveInstruction(pred_get); |
| 998 | RecordSimplification(); |
| 999 | } else if (single_value->StrictlyDominates(pred_get)) { |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 1000 | // Combine all the maybe null values into one. |
| 1001 | pred_get->ReplaceInput(single_value, 0); |
Alex Light | 7432805 | 2021-03-29 18:11:23 -0700 | [diff] [blame] | 1002 | RecordSimplification(); |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1006 | void InstructionSimplifierVisitor::VisitSelect(HSelect* select) { |
| 1007 | HInstruction* replace_with = nullptr; |
| 1008 | HInstruction* condition = select->GetCondition(); |
| 1009 | HInstruction* true_value = select->GetTrueValue(); |
| 1010 | HInstruction* false_value = select->GetFalseValue(); |
| 1011 | |
| 1012 | if (condition->IsBooleanNot()) { |
| 1013 | // Change ((!cond) ? x : y) to (cond ? y : x). |
| 1014 | condition = condition->InputAt(0); |
| 1015 | std::swap(true_value, false_value); |
| 1016 | select->ReplaceInput(false_value, 0); |
| 1017 | select->ReplaceInput(true_value, 1); |
| 1018 | select->ReplaceInput(condition, 2); |
| 1019 | RecordSimplification(); |
| 1020 | } |
| 1021 | |
| 1022 | if (true_value == false_value) { |
| 1023 | // Replace (cond ? x : x) with (x). |
| 1024 | replace_with = true_value; |
| 1025 | } else if (condition->IsIntConstant()) { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1026 | if (condition->AsIntConstant()->IsTrue()) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1027 | // Replace (true ? x : y) with (x). |
| 1028 | replace_with = true_value; |
| 1029 | } else { |
| 1030 | // Replace (false ? x : y) with (y). |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1031 | DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue(); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1032 | replace_with = false_value; |
| 1033 | } |
| 1034 | } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1035 | if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1036 | // Replace (cond ? true : false) with (cond). |
| 1037 | replace_with = condition; |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1038 | } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1039 | // Replace (cond ? false : true) with (!cond). |
| 1040 | replace_with = GetGraph()->InsertOppositeCondition(condition, select); |
| 1041 | } |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 1042 | } else if (condition->IsCondition()) { |
| 1043 | IfCondition cmp = condition->AsCondition()->GetCondition(); |
| 1044 | HInstruction* a = condition->InputAt(0); |
| 1045 | HInstruction* b = condition->InputAt(1); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1046 | DataType::Type t_type = true_value->GetType(); |
| 1047 | DataType::Type f_type = false_value->GetType(); |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 1048 | // Here we have a <cmp> b ? true_value : false_value. |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame] | 1049 | // Test if both values are compatible integral types (resulting MIN/MAX/ABS |
| 1050 | // type will be int or long, like the condition). Replacements are general, |
| 1051 | // but assume conditions prefer constants on the right. |
Aart Bik | 2286da2 | 2018-03-22 10:50:22 -0700 | [diff] [blame] | 1052 | if (DataType::IsIntegralType(t_type) && DataType::Kind(t_type) == DataType::Kind(f_type)) { |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame] | 1053 | // Allow a < 100 ? max(a, -100) : .. |
| 1054 | // or a > -100 ? min(a, 100) : .. |
| 1055 | // to use min/max instead of a to detect nested min/max expressions. |
| 1056 | HInstruction* new_a = AllowInMinMax(cmp, a, b, true_value); |
| 1057 | if (new_a != nullptr) { |
| 1058 | a = new_a; |
| 1059 | } |
Aart Bik | 142b913 | 2018-03-14 15:12:59 -0700 | [diff] [blame] | 1060 | // Try to replace typical integral MIN/MAX/ABS constructs. |
| 1061 | if ((cmp == kCondLT || cmp == kCondLE || cmp == kCondGT || cmp == kCondGE) && |
| 1062 | ((a == true_value && b == false_value) || |
| 1063 | (b == true_value && a == false_value))) { |
| 1064 | // Found a < b ? a : b (MIN) or a < b ? b : a (MAX) |
| 1065 | // or a > b ? a : b (MAX) or a > b ? b : a (MIN). |
| 1066 | bool is_min = (cmp == kCondLT || cmp == kCondLE) == (a == true_value); |
| 1067 | replace_with = NewIntegralMinMax(GetGraph()->GetAllocator(), a, b, select, is_min); |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame] | 1068 | } else if (((cmp == kCondLT || cmp == kCondLE) && true_value->IsNeg()) || |
| 1069 | ((cmp == kCondGT || cmp == kCondGE) && false_value->IsNeg())) { |
| 1070 | bool negLeft = (cmp == kCondLT || cmp == kCondLE); |
| 1071 | HInstruction* the_negated = negLeft ? true_value->InputAt(0) : false_value->InputAt(0); |
| 1072 | HInstruction* not_negated = negLeft ? false_value : true_value; |
| 1073 | if (a == the_negated && a == not_negated && IsInt64Value(b, 0)) { |
| 1074 | // Found a < 0 ? -a : a |
| 1075 | // or a > 0 ? a : -a |
| 1076 | // which can be replaced by ABS(a). |
| 1077 | replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), a, select); |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } else if (true_value->IsSub() && false_value->IsSub()) { |
| 1080 | HInstruction* true_sub1 = true_value->InputAt(0); |
| 1081 | HInstruction* true_sub2 = true_value->InputAt(1); |
| 1082 | HInstruction* false_sub1 = false_value->InputAt(0); |
| 1083 | HInstruction* false_sub2 = false_value->InputAt(1); |
| 1084 | if ((((cmp == kCondGT || cmp == kCondGE) && |
| 1085 | (a == true_sub1 && b == true_sub2 && a == false_sub2 && b == false_sub1)) || |
| 1086 | ((cmp == kCondLT || cmp == kCondLE) && |
| 1087 | (a == true_sub2 && b == true_sub1 && a == false_sub1 && b == false_sub2))) && |
| 1088 | AreLowerPrecisionArgs(t_type, a, b)) { |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame] | 1089 | // Found a > b ? a - b : b - a |
| 1090 | // or a < b ? b - a : a - b |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 1091 | // which can be replaced by ABS(a - b) for lower precision operands a, b. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1092 | replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), true_value, select); |
Aart Bik | 4f7dd34 | 2017-09-12 13:12:57 -0700 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | } |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | if (replace_with != nullptr) { |
| 1099 | select->ReplaceWith(replace_with); |
| 1100 | select->GetBlock()->RemoveInstruction(select); |
| 1101 | RecordSimplification(); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void InstructionSimplifierVisitor::VisitIf(HIf* instruction) { |
| 1106 | HInstruction* condition = instruction->InputAt(0); |
| 1107 | if (condition->IsBooleanNot()) { |
| 1108 | // Swap successors if input is negated. |
| 1109 | instruction->ReplaceInput(condition->InputAt(0), 0); |
| 1110 | instruction->GetBlock()->SwapSuccessors(); |
David Brazdil | 0d13fee | 2015-04-17 14:52:19 +0100 | [diff] [blame] | 1111 | RecordSimplification(); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 1115 | void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) { |
| 1116 | HInstruction* input = instruction->InputAt(0); |
| 1117 | // If the array is a NewArray with constant size, replace the array length |
| 1118 | // with the constant instruction. This helps the bounds check elimination phase. |
| 1119 | if (input->IsNewArray()) { |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 1120 | input = input->AsNewArray()->GetLength(); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 1121 | if (input->IsIntConstant()) { |
| 1122 | instruction->ReplaceWith(input); |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 1127 | void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 1128 | HInstruction* value = instruction->GetValue(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1129 | if (value->GetType() != DataType::Type::kReference) { |
| 1130 | return; |
| 1131 | } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 1132 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 1133 | if (CanEnsureNotNullAt(value, instruction)) { |
| 1134 | instruction->ClearValueCanBeNull(); |
| 1135 | } |
| 1136 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 1137 | if (value->IsArrayGet()) { |
| 1138 | if (value->AsArrayGet()->GetArray() == instruction->GetArray()) { |
| 1139 | // If the code is just swapping elements in the array, no need for a type check. |
| 1140 | instruction->ClearNeedsTypeCheck(); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 1141 | return; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 1144 | |
Nicolas Geoffray | 9fdb31e | 2015-07-01 12:56:46 +0100 | [diff] [blame] | 1145 | if (value->IsNullConstant()) { |
| 1146 | instruction->ClearNeedsTypeCheck(); |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 1147 | return; |
Nicolas Geoffray | 9fdb31e | 2015-07-01 12:56:46 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 1150 | ScopedObjectAccess soa(Thread::Current()); |
| 1151 | ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo(); |
| 1152 | ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo(); |
| 1153 | if (!array_rti.IsValid()) { |
| 1154 | return; |
| 1155 | } |
| 1156 | |
| 1157 | if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) { |
| 1158 | instruction->ClearNeedsTypeCheck(); |
| 1159 | return; |
| 1160 | } |
| 1161 | |
| 1162 | if (array_rti.IsObjectArray()) { |
| 1163 | if (array_rti.IsExact()) { |
| 1164 | instruction->ClearNeedsTypeCheck(); |
| 1165 | return; |
| 1166 | } |
| 1167 | instruction->SetStaticTypeOfArrayIsObjectArray(); |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 1168 | } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1171 | static bool IsTypeConversionLossless(DataType::Type input_type, DataType::Type result_type) { |
Aart Bik | dab6907 | 2017-10-23 13:30:39 -0700 | [diff] [blame] | 1172 | // Make sure all implicit conversions have been simplified and no new ones have been introduced. |
| 1173 | DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type)) |
| 1174 | << input_type << "," << result_type; |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1175 | // The conversion to a larger type is loss-less with the exception of two cases, |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 1176 | // - conversion to the unsigned type Uint16, where we may lose some bits, and |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1177 | // - conversion from float to long, the only FP to integral conversion with smaller FP type. |
| 1178 | // For integral to FP conversions this holds because the FP mantissa is large enough. |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 1179 | // Note: The size check excludes Uint8 as the result type. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1180 | return DataType::Size(result_type) > DataType::Size(input_type) && |
| 1181 | result_type != DataType::Type::kUint16 && |
| 1182 | !(result_type == DataType::Type::kInt64 && input_type == DataType::Type::kFloat32); |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Vladimir Marko | 61b9228 | 2017-10-11 13:23:17 +0100 | [diff] [blame] | 1185 | static inline bool TryReplaceFieldOrArrayGetType(HInstruction* maybe_get, DataType::Type new_type) { |
| 1186 | if (maybe_get->IsInstanceFieldGet()) { |
| 1187 | maybe_get->AsInstanceFieldGet()->SetType(new_type); |
| 1188 | return true; |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 1189 | } else if (maybe_get->IsPredicatedInstanceFieldGet()) { |
| 1190 | maybe_get->AsPredicatedInstanceFieldGet()->SetType(new_type); |
| 1191 | return true; |
Vladimir Marko | 61b9228 | 2017-10-11 13:23:17 +0100 | [diff] [blame] | 1192 | } else if (maybe_get->IsStaticFieldGet()) { |
| 1193 | maybe_get->AsStaticFieldGet()->SetType(new_type); |
| 1194 | return true; |
| 1195 | } else if (maybe_get->IsArrayGet() && !maybe_get->AsArrayGet()->IsStringCharAt()) { |
| 1196 | maybe_get->AsArrayGet()->SetType(new_type); |
| 1197 | return true; |
| 1198 | } else { |
| 1199 | return false; |
| 1200 | } |
| 1201 | } |
| 1202 | |
Mingyao Yang | 3bcb751 | 2017-11-16 15:40:46 -0800 | [diff] [blame] | 1203 | // The type conversion is only used for storing into a field/element of the |
| 1204 | // same/narrower size. |
| 1205 | static bool IsTypeConversionForStoringIntoNoWiderFieldOnly(HTypeConversion* type_conversion) { |
| 1206 | if (type_conversion->HasEnvironmentUses()) { |
| 1207 | return false; |
| 1208 | } |
| 1209 | DataType::Type input_type = type_conversion->GetInputType(); |
| 1210 | DataType::Type result_type = type_conversion->GetResultType(); |
| 1211 | if (!DataType::IsIntegralType(input_type) || |
| 1212 | !DataType::IsIntegralType(result_type) || |
| 1213 | input_type == DataType::Type::kInt64 || |
| 1214 | result_type == DataType::Type::kInt64) { |
| 1215 | // Type conversion is needed if non-integer types are involved, or 64-bit |
| 1216 | // types are involved, which may use different number of registers. |
| 1217 | return false; |
| 1218 | } |
| 1219 | if (DataType::Size(input_type) >= DataType::Size(result_type)) { |
| 1220 | // Type conversion is not necessary when storing to a field/element of the |
| 1221 | // same/smaller size. |
| 1222 | } else { |
| 1223 | // We do not handle this case here. |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
| 1227 | // Check if the converted value is only used for storing into heap. |
| 1228 | for (const HUseListNode<HInstruction*>& use : type_conversion->GetUses()) { |
| 1229 | HInstruction* instruction = use.GetUser(); |
| 1230 | if (instruction->IsInstanceFieldSet() && |
| 1231 | instruction->AsInstanceFieldSet()->GetFieldType() == result_type) { |
| 1232 | DCHECK_EQ(instruction->AsInstanceFieldSet()->GetValue(), type_conversion); |
| 1233 | continue; |
| 1234 | } |
| 1235 | if (instruction->IsStaticFieldSet() && |
| 1236 | instruction->AsStaticFieldSet()->GetFieldType() == result_type) { |
| 1237 | DCHECK_EQ(instruction->AsStaticFieldSet()->GetValue(), type_conversion); |
| 1238 | continue; |
| 1239 | } |
| 1240 | if (instruction->IsArraySet() && |
| 1241 | instruction->AsArraySet()->GetComponentType() == result_type && |
| 1242 | // not index use. |
| 1243 | instruction->AsArraySet()->GetIndex() != type_conversion) { |
| 1244 | DCHECK_EQ(instruction->AsArraySet()->GetValue(), type_conversion); |
| 1245 | continue; |
| 1246 | } |
| 1247 | // The use is not as a store value, or the field/element type is not the |
| 1248 | // same as the result_type, keep the type conversion. |
| 1249 | return false; |
| 1250 | } |
| 1251 | // Codegen automatically handles the type conversion during the store. |
| 1252 | return true; |
| 1253 | } |
| 1254 | |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 1255 | void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1256 | HInstruction* input = instruction->GetInput(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1257 | DataType::Type input_type = input->GetType(); |
| 1258 | DataType::Type result_type = instruction->GetResultType(); |
Nicolas Geoffray | acc56ac | 2018-10-09 08:45:24 +0100 | [diff] [blame] | 1259 | if (instruction->IsImplicitConversion()) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1260 | instruction->ReplaceWith(input); |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 1261 | instruction->GetBlock()->RemoveInstruction(instruction); |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1262 | RecordSimplification(); |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | if (input->IsTypeConversion()) { |
| 1267 | HTypeConversion* input_conversion = input->AsTypeConversion(); |
| 1268 | HInstruction* original_input = input_conversion->GetInput(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1269 | DataType::Type original_type = original_input->GetType(); |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1270 | |
| 1271 | // When the first conversion is lossless, a direct conversion from the original type |
| 1272 | // to the final type yields the same result, even for a lossy second conversion, for |
| 1273 | // example float->double->int or int->double->float. |
| 1274 | bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type); |
| 1275 | |
| 1276 | // For integral conversions, see if the first conversion loses only bits that the second |
| 1277 | // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct |
| 1278 | // conversion yields the same result, for example long->int->short or int->char->short. |
| 1279 | bool integral_conversions_with_non_widening_second = |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1280 | DataType::IsIntegralType(input_type) && |
| 1281 | DataType::IsIntegralType(original_type) && |
| 1282 | DataType::IsIntegralType(result_type) && |
| 1283 | DataType::Size(result_type) <= DataType::Size(input_type); |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) { |
| 1286 | // If the merged conversion is implicit, do the simplification unconditionally. |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 1287 | if (DataType::IsTypeConversionImplicit(original_type, result_type)) { |
Vladimir Marko | b52bbde | 2016-02-12 12:06:05 +0000 | [diff] [blame] | 1288 | instruction->ReplaceWith(original_input); |
| 1289 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1290 | if (!input_conversion->HasUses()) { |
| 1291 | // Don't wait for DCE. |
| 1292 | input_conversion->GetBlock()->RemoveInstruction(input_conversion); |
| 1293 | } |
| 1294 | RecordSimplification(); |
| 1295 | return; |
| 1296 | } |
| 1297 | // Otherwise simplify only if the first conversion has no other use. |
| 1298 | if (input_conversion->HasOnlyOneNonEnvironmentUse()) { |
| 1299 | input_conversion->ReplaceWith(original_input); |
| 1300 | input_conversion->GetBlock()->RemoveInstruction(input_conversion); |
| 1301 | RecordSimplification(); |
| 1302 | return; |
| 1303 | } |
| 1304 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1305 | } else if (input->IsAnd() && DataType::IsIntegralType(result_type)) { |
| 1306 | DCHECK(DataType::IsIntegralType(input_type)); |
Vladimir Marko | 8428bd3 | 2016-02-12 16:53:57 +0000 | [diff] [blame] | 1307 | HAnd* input_and = input->AsAnd(); |
| 1308 | HConstant* constant = input_and->GetConstantRight(); |
| 1309 | if (constant != nullptr) { |
| 1310 | int64_t value = Int64FromConstant(constant); |
| 1311 | DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd(). |
| 1312 | size_t trailing_ones = CTZ(~static_cast<uint64_t>(value)); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1313 | if (trailing_ones >= kBitsPerByte * DataType::Size(result_type)) { |
Vladimir Marko | 8428bd3 | 2016-02-12 16:53:57 +0000 | [diff] [blame] | 1314 | // 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] | 1315 | HInstruction* original_input = input_and->GetLeastConstantLeft(); |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 1316 | if (DataType::IsTypeConversionImplicit(original_input->GetType(), result_type)) { |
Vladimir Marko | 625090f | 2016-03-14 18:00:05 +0000 | [diff] [blame] | 1317 | instruction->ReplaceWith(original_input); |
| 1318 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1319 | RecordSimplification(); |
| 1320 | return; |
| 1321 | } else if (input->HasOnlyOneNonEnvironmentUse()) { |
| 1322 | input_and->ReplaceWith(original_input); |
| 1323 | input_and->GetBlock()->RemoveInstruction(input_and); |
| 1324 | RecordSimplification(); |
| 1325 | return; |
| 1326 | } |
Vladimir Marko | 8428bd3 | 2016-02-12 16:53:57 +0000 | [diff] [blame] | 1327 | } |
| 1328 | } |
Vladimir Marko | 61b9228 | 2017-10-11 13:23:17 +0100 | [diff] [blame] | 1329 | } else if (input->HasOnlyOneNonEnvironmentUse() && |
| 1330 | ((input_type == DataType::Type::kInt8 && result_type == DataType::Type::kUint8) || |
| 1331 | (input_type == DataType::Type::kUint8 && result_type == DataType::Type::kInt8) || |
| 1332 | (input_type == DataType::Type::kInt16 && result_type == DataType::Type::kUint16) || |
| 1333 | (input_type == DataType::Type::kUint16 && result_type == DataType::Type::kInt16))) { |
| 1334 | // Try to modify the type of the load to `result_type` and remove the explicit type conversion. |
| 1335 | if (TryReplaceFieldOrArrayGetType(input, result_type)) { |
| 1336 | instruction->ReplaceWith(input); |
| 1337 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1338 | RecordSimplification(); |
| 1339 | return; |
| 1340 | } |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 1341 | } |
Mingyao Yang | 3bcb751 | 2017-11-16 15:40:46 -0800 | [diff] [blame] | 1342 | |
| 1343 | if (IsTypeConversionForStoringIntoNoWiderFieldOnly(instruction)) { |
| 1344 | instruction->ReplaceWith(input); |
| 1345 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1346 | RecordSimplification(); |
| 1347 | return; |
| 1348 | } |
Nicolas Geoffray | 01fcc9e | 2014-12-01 14:16:20 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Aart Bik | c6eec4b | 2018-03-29 17:22:00 -0700 | [diff] [blame] | 1351 | void InstructionSimplifierVisitor::VisitAbs(HAbs* instruction) { |
| 1352 | HInstruction* input = instruction->GetInput(); |
| 1353 | if (DataType::IsZeroExtension(input->GetType(), instruction->GetResultType())) { |
| 1354 | // Zero extension from narrow to wide can never set sign bit in the wider |
| 1355 | // operand, making the subsequent Abs redundant (e.g., abs(b & 0xff) for byte b). |
| 1356 | instruction->ReplaceWith(input); |
| 1357 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1358 | RecordSimplification(); |
| 1359 | } |
| 1360 | } |
| 1361 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1362 | void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) { |
| 1363 | HConstant* input_cst = instruction->GetConstantRight(); |
| 1364 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1365 | bool integral_type = DataType::IsIntegralType(instruction->GetType()); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1366 | if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1367 | // Replace code looking like |
| 1368 | // ADD dst, src, 0 |
| 1369 | // with |
| 1370 | // src |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 1371 | // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When |
| 1372 | // `x` is `-0.0`, the former expression yields `0.0`, while the later |
| 1373 | // yields `-0.0`. |
Maxim Kazantsev | d3278bd | 2016-07-12 15:55:33 +0600 | [diff] [blame] | 1374 | if (integral_type) { |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 1375 | instruction->ReplaceWith(input_other); |
| 1376 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 1377 | RecordSimplification(); |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 1378 | return; |
| 1379 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | HInstruction* left = instruction->GetLeft(); |
| 1383 | HInstruction* right = instruction->GetRight(); |
| 1384 | bool left_is_neg = left->IsNeg(); |
| 1385 | bool right_is_neg = right->IsNeg(); |
| 1386 | |
| 1387 | if (left_is_neg && right_is_neg) { |
| 1388 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 1389 | return; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg(); |
Andreas Gampe | 654698d | 2018-09-20 13:34:35 -0700 | [diff] [blame] | 1394 | if (left_is_neg != right_is_neg && neg->HasOnlyOneNonEnvironmentUse()) { |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1395 | // Replace code looking like |
| 1396 | // NEG tmp, b |
| 1397 | // ADD dst, a, tmp |
| 1398 | // with |
| 1399 | // SUB dst, a, b |
| 1400 | // We do not perform the optimization if the input negation has environment |
| 1401 | // uses or multiple non-environment uses as it could lead to worse code. In |
| 1402 | // particular, we do not want the live range of `b` to be extended if we are |
| 1403 | // not sure the initial 'NEG' instruction can be removed. |
| 1404 | HInstruction* other = left_is_neg ? right : left; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1405 | HSub* sub = |
| 1406 | new(GetGraph()->GetAllocator()) HSub(instruction->GetType(), other, neg->GetInput()); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1407 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub); |
| 1408 | RecordSimplification(); |
| 1409 | neg->GetBlock()->RemoveInstruction(neg); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1410 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1411 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1412 | |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1413 | if (TryReplaceWithRotate(instruction)) { |
| 1414 | return; |
| 1415 | } |
| 1416 | |
| 1417 | // TryHandleAssociativeAndCommutativeOperation() does not remove its input, |
| 1418 | // so no need to return. |
| 1419 | TryHandleAssociativeAndCommutativeOperation(instruction); |
| 1420 | |
Maxim Kazantsev | d3278bd | 2016-07-12 15:55:33 +0600 | [diff] [blame] | 1421 | if ((left->IsSub() || right->IsSub()) && |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1422 | TrySubtractionChainSimplification(instruction)) { |
| 1423 | return; |
| 1424 | } |
Maxim Kazantsev | d3278bd | 2016-07-12 15:55:33 +0600 | [diff] [blame] | 1425 | |
| 1426 | if (integral_type) { |
| 1427 | // Replace code patterns looking like |
| 1428 | // SUB dst1, x, y SUB dst1, x, y |
| 1429 | // ADD dst2, dst1, y ADD dst2, y, dst1 |
| 1430 | // with |
| 1431 | // SUB dst1, x, y |
| 1432 | // ADD instruction is not needed in this case, we may use |
| 1433 | // one of inputs of SUB instead. |
| 1434 | if (left->IsSub() && left->InputAt(1) == right) { |
| 1435 | instruction->ReplaceWith(left->InputAt(0)); |
| 1436 | RecordSimplification(); |
| 1437 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1438 | return; |
| 1439 | } else if (right->IsSub() && right->InputAt(1) == left) { |
| 1440 | instruction->ReplaceWith(right->InputAt(0)); |
| 1441 | RecordSimplification(); |
| 1442 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1443 | return; |
| 1444 | } |
| 1445 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) { |
Vladimir Marko | 61b9228 | 2017-10-11 13:23:17 +0100 | [diff] [blame] | 1449 | DCHECK(DataType::IsIntegralType(instruction->GetType())); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1450 | HConstant* input_cst = instruction->GetConstantRight(); |
| 1451 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 1452 | |
Vladimir Marko | 452c1b6 | 2015-09-25 14:44:17 +0100 | [diff] [blame] | 1453 | if (input_cst != nullptr) { |
| 1454 | int64_t value = Int64FromConstant(input_cst); |
Aart Bik | dab6907 | 2017-10-23 13:30:39 -0700 | [diff] [blame] | 1455 | if (value == -1 || |
| 1456 | // Similar cases under zero extension. |
| 1457 | (DataType::IsUnsignedType(input_other->GetType()) && |
| 1458 | ((DataType::MaxValueOfIntegralType(input_other->GetType()) & ~value) == 0))) { |
Vladimir Marko | 452c1b6 | 2015-09-25 14:44:17 +0100 | [diff] [blame] | 1459 | // Replace code looking like |
| 1460 | // AND dst, src, 0xFFF...FF |
| 1461 | // with |
| 1462 | // src |
| 1463 | instruction->ReplaceWith(input_other); |
| 1464 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1465 | RecordSimplification(); |
| 1466 | return; |
| 1467 | } |
Vladimir Marko | c8fb211 | 2017-10-03 11:37:52 +0100 | [diff] [blame] | 1468 | if (input_other->IsTypeConversion() && |
| 1469 | input_other->GetType() == DataType::Type::kInt64 && |
| 1470 | DataType::IsIntegralType(input_other->InputAt(0)->GetType()) && |
| 1471 | IsInt<32>(value) && |
| 1472 | input_other->HasOnlyOneNonEnvironmentUse()) { |
| 1473 | // The AND can be reordered before the TypeConversion. Replace |
| 1474 | // LongConstant cst, <32-bit-constant-sign-extended-to-64-bits> |
| 1475 | // TypeConversion<Int64> tmp, src |
| 1476 | // AND dst, tmp, cst |
| 1477 | // with |
| 1478 | // IntConstant cst, <32-bit-constant> |
| 1479 | // AND tmp, src, cst |
| 1480 | // TypeConversion<Int64> dst, tmp |
| 1481 | // This helps 32-bit targets and does not hurt 64-bit targets. |
| 1482 | // This also simplifies detection of other patterns, such as Uint8 loads. |
| 1483 | HInstruction* new_and_input = input_other->InputAt(0); |
| 1484 | // Implicit conversion Int64->Int64 would have been removed previously. |
| 1485 | DCHECK_NE(new_and_input->GetType(), DataType::Type::kInt64); |
| 1486 | HConstant* new_const = GetGraph()->GetConstant(DataType::Type::kInt32, value); |
| 1487 | HAnd* new_and = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1488 | new (GetGraph()->GetAllocator()) HAnd(DataType::Type::kInt32, new_and_input, new_const); |
Vladimir Marko | c8fb211 | 2017-10-03 11:37:52 +0100 | [diff] [blame] | 1489 | instruction->GetBlock()->InsertInstructionBefore(new_and, instruction); |
| 1490 | HTypeConversion* new_conversion = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1491 | new (GetGraph()->GetAllocator()) HTypeConversion(DataType::Type::kInt64, new_and); |
Vladimir Marko | c8fb211 | 2017-10-03 11:37:52 +0100 | [diff] [blame] | 1492 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_conversion); |
| 1493 | input_other->GetBlock()->RemoveInstruction(input_other); |
| 1494 | RecordSimplification(); |
| 1495 | // Try to process the new And now, do not wait for the next round of simplifications. |
| 1496 | instruction = new_and; |
| 1497 | input_other = new_and_input; |
| 1498 | } |
Vladimir Marko | 452c1b6 | 2015-09-25 14:44:17 +0100 | [diff] [blame] | 1499 | // Eliminate And from UShr+And if the And-mask contains all the bits that |
| 1500 | // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask |
| 1501 | // precisely clears the shifted-in sign bits. |
| 1502 | if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1503 | size_t reg_bits = (instruction->GetResultType() == DataType::Type::kInt64) ? 64 : 32; |
Vladimir Marko | 452c1b6 | 2015-09-25 14:44:17 +0100 | [diff] [blame] | 1504 | size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1); |
| 1505 | size_t num_tail_bits_set = CTZ(value + 1); |
| 1506 | if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) { |
| 1507 | // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff". |
| 1508 | instruction->ReplaceWith(input_other); |
| 1509 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1510 | RecordSimplification(); |
| 1511 | return; |
| 1512 | } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) && |
| 1513 | input_other->HasOnlyOneNonEnvironmentUse()) { |
| 1514 | DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above. |
| 1515 | // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24". |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1516 | HUShr* ushr = new (GetGraph()->GetAllocator()) HUShr(instruction->GetType(), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 1517 | input_other->InputAt(0), |
| 1518 | input_other->InputAt(1), |
| 1519 | input_other->GetDexPc()); |
Vladimir Marko | 452c1b6 | 2015-09-25 14:44:17 +0100 | [diff] [blame] | 1520 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr); |
| 1521 | input_other->GetBlock()->RemoveInstruction(input_other); |
| 1522 | RecordSimplification(); |
| 1523 | return; |
| 1524 | } |
| 1525 | } |
Vladimir Marko | 61b9228 | 2017-10-11 13:23:17 +0100 | [diff] [blame] | 1526 | if ((value == 0xff || value == 0xffff) && instruction->GetType() != DataType::Type::kInt64) { |
| 1527 | // Transform AND to a type conversion to Uint8/Uint16. If `input_other` is a field |
| 1528 | // or array Get with only a single use, short-circuit the subsequent simplification |
| 1529 | // of the Get+TypeConversion and change the Get's type to `new_type` instead. |
| 1530 | DataType::Type new_type = (value == 0xff) ? DataType::Type::kUint8 : DataType::Type::kUint16; |
| 1531 | DataType::Type find_type = (value == 0xff) ? DataType::Type::kInt8 : DataType::Type::kInt16; |
| 1532 | if (input_other->GetType() == find_type && |
| 1533 | input_other->HasOnlyOneNonEnvironmentUse() && |
| 1534 | TryReplaceFieldOrArrayGetType(input_other, new_type)) { |
| 1535 | instruction->ReplaceWith(input_other); |
| 1536 | instruction->GetBlock()->RemoveInstruction(instruction); |
Aart Bik | dab6907 | 2017-10-23 13:30:39 -0700 | [diff] [blame] | 1537 | } else if (DataType::IsTypeConversionImplicit(input_other->GetType(), new_type)) { |
| 1538 | instruction->ReplaceWith(input_other); |
| 1539 | instruction->GetBlock()->RemoveInstruction(instruction); |
Vladimir Marko | 61b9228 | 2017-10-11 13:23:17 +0100 | [diff] [blame] | 1540 | } else { |
| 1541 | HTypeConversion* type_conversion = new (GetGraph()->GetAllocator()) HTypeConversion( |
| 1542 | new_type, input_other, instruction->GetDexPc()); |
| 1543 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, type_conversion); |
| 1544 | } |
| 1545 | RecordSimplification(); |
| 1546 | return; |
| 1547 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 1551 | // 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] | 1552 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1553 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 1554 | // Replace code looking like |
| 1555 | // AND dst, src, src |
| 1556 | // with |
| 1557 | // src |
| 1558 | instruction->ReplaceWith(instruction->GetLeft()); |
| 1559 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 1560 | RecordSimplification(); |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 1561 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1562 | } |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 1563 | |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1564 | if (TryDeMorganNegationFactoring(instruction)) { |
| 1565 | return; |
| 1566 | } |
| 1567 | |
| 1568 | // TryHandleAssociativeAndCommutativeOperation() does not remove its input, |
| 1569 | // so no need to return. |
| 1570 | TryHandleAssociativeAndCommutativeOperation(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1573 | void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) { |
| 1574 | VisitCondition(condition); |
| 1575 | } |
| 1576 | |
| 1577 | void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) { |
| 1578 | VisitCondition(condition); |
| 1579 | } |
| 1580 | |
| 1581 | void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) { |
| 1582 | VisitCondition(condition); |
| 1583 | } |
| 1584 | |
| 1585 | void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) { |
| 1586 | VisitCondition(condition); |
| 1587 | } |
| 1588 | |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 1589 | void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) { |
| 1590 | VisitCondition(condition); |
| 1591 | } |
| 1592 | |
| 1593 | void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) { |
| 1594 | VisitCondition(condition); |
| 1595 | } |
| 1596 | |
| 1597 | void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) { |
| 1598 | VisitCondition(condition); |
| 1599 | } |
| 1600 | |
| 1601 | void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) { |
| 1602 | VisitCondition(condition); |
| 1603 | } |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 1604 | |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1605 | // Recognize the following pattern: |
| 1606 | // obj.getClass() ==/!= Foo.class |
| 1607 | // And replace it with a constant value if the type of `obj` is statically known. |
| 1608 | static bool RecognizeAndSimplifyClassCheck(HCondition* condition) { |
| 1609 | HInstruction* input_one = condition->InputAt(0); |
| 1610 | HInstruction* input_two = condition->InputAt(1); |
| 1611 | HLoadClass* load_class = input_one->IsLoadClass() |
| 1612 | ? input_one->AsLoadClass() |
| 1613 | : input_two->AsLoadClass(); |
| 1614 | if (load_class == nullptr) { |
| 1615 | return false; |
| 1616 | } |
| 1617 | |
| 1618 | ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI(); |
| 1619 | if (!class_rti.IsValid()) { |
| 1620 | // Unresolved class. |
| 1621 | return false; |
| 1622 | } |
| 1623 | |
| 1624 | HInstanceFieldGet* field_get = (load_class == input_one) |
| 1625 | ? input_two->AsInstanceFieldGet() |
| 1626 | : input_one->AsInstanceFieldGet(); |
| 1627 | if (field_get == nullptr) { |
| 1628 | return false; |
| 1629 | } |
| 1630 | |
| 1631 | HInstruction* receiver = field_get->InputAt(0); |
| 1632 | ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo(); |
| 1633 | if (!receiver_type.IsExact()) { |
| 1634 | return false; |
| 1635 | } |
| 1636 | |
| 1637 | { |
| 1638 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1639 | ArtField* field = GetClassRoot<mirror::Object>()->GetInstanceField(0); |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1640 | DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_"); |
| 1641 | if (field_get->GetFieldInfo().GetField() != field) { |
| 1642 | return false; |
| 1643 | } |
| 1644 | |
| 1645 | // We can replace the compare. |
| 1646 | int value = 0; |
| 1647 | if (receiver_type.IsEqual(class_rti)) { |
| 1648 | value = condition->IsEqual() ? 1 : 0; |
| 1649 | } else { |
| 1650 | value = condition->IsNotEqual() ? 1 : 0; |
| 1651 | } |
| 1652 | condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value)); |
| 1653 | return true; |
| 1654 | } |
| 1655 | } |
| 1656 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1657 | void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) { |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1658 | if (condition->IsEqual() || condition->IsNotEqual()) { |
| 1659 | if (RecognizeAndSimplifyClassCheck(condition)) { |
| 1660 | return; |
| 1661 | } |
| 1662 | } |
| 1663 | |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 1664 | // Reverse condition if left is constant. Our code generators prefer constant |
| 1665 | // on the right hand side. |
| 1666 | if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) { |
| 1667 | HBasicBlock* block = condition->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1668 | HCondition* replacement = |
| 1669 | GetOppositeConditionSwapOps(block->GetGraph()->GetAllocator(), condition); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 1670 | // If it is a fp we must set the opposite bias. |
| 1671 | if (replacement != nullptr) { |
| 1672 | if (condition->IsLtBias()) { |
| 1673 | replacement->SetBias(ComparisonBias::kGtBias); |
| 1674 | } else if (condition->IsGtBias()) { |
| 1675 | replacement->SetBias(ComparisonBias::kLtBias); |
| 1676 | } |
| 1677 | block->ReplaceAndRemoveInstructionWith(condition, replacement); |
| 1678 | RecordSimplification(); |
| 1679 | |
| 1680 | condition = replacement; |
| 1681 | } |
| 1682 | } |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1683 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1684 | HInstruction* left = condition->GetLeft(); |
| 1685 | HInstruction* right = condition->GetRight(); |
Anton Shamin | bdd7935 | 2016-02-15 12:48:36 +0600 | [diff] [blame] | 1686 | |
| 1687 | // Try to fold an HCompare into this HCondition. |
| 1688 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1689 | // We can only replace an HCondition which compares a Compare to 0. |
| 1690 | // Both 'dx' and 'jack' generate a compare to 0 when compiling a |
| 1691 | // condition with a long, float or double comparison as input. |
| 1692 | if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) { |
| 1693 | // Conversion is not possible. |
| 1694 | return; |
| 1695 | } |
| 1696 | |
| 1697 | // Is the Compare only used for this purpose? |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1698 | if (!left->GetUses().HasExactlyOneElement()) { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1699 | // Someone else also wants the result of the compare. |
| 1700 | return; |
| 1701 | } |
| 1702 | |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1703 | if (!left->GetEnvUses().empty()) { |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1704 | // There is a reference to the compare result in an environment. Do we really need it? |
| 1705 | if (GetGraph()->IsDebuggable()) { |
| 1706 | return; |
| 1707 | } |
| 1708 | |
| 1709 | // We have to ensure that there are no deopt points in the sequence. |
| 1710 | if (left->HasAnyEnvironmentUseBefore(condition)) { |
| 1711 | return; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | // Clean up any environment uses from the HCompare, if any. |
| 1716 | left->RemoveEnvironmentUsers(); |
| 1717 | |
| 1718 | // We have decided to fold the HCompare into the HCondition. Transfer the information. |
| 1719 | condition->SetBias(left->AsCompare()->GetBias()); |
| 1720 | |
| 1721 | // Replace the operands of the HCondition. |
| 1722 | condition->ReplaceInput(left->InputAt(0), 0); |
| 1723 | condition->ReplaceInput(left->InputAt(1), 1); |
| 1724 | |
| 1725 | // Remove the HCompare. |
| 1726 | left->GetBlock()->RemoveInstruction(left); |
| 1727 | |
| 1728 | RecordSimplification(); |
| 1729 | } |
| 1730 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1731 | // Return whether x / divisor == x * (1.0f / divisor), for every float x. |
| 1732 | static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) { |
| 1733 | // True, if the most significant bits of divisor are 0. |
| 1734 | return ((divisor & 0x7fffff) == 0); |
| 1735 | } |
| 1736 | |
| 1737 | // Return whether x / divisor == x * (1.0 / divisor), for every double x. |
| 1738 | static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) { |
| 1739 | // True, if the most significant bits of divisor are 0. |
| 1740 | return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0); |
| 1741 | } |
| 1742 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1743 | void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) { |
| 1744 | HConstant* input_cst = instruction->GetConstantRight(); |
| 1745 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1746 | DataType::Type type = instruction->GetType(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1747 | |
| 1748 | if ((input_cst != nullptr) && input_cst->IsOne()) { |
| 1749 | // Replace code looking like |
| 1750 | // DIV dst, src, 1 |
| 1751 | // with |
| 1752 | // src |
| 1753 | instruction->ReplaceWith(input_other); |
| 1754 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 1755 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1756 | return; |
| 1757 | } |
| 1758 | |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1759 | if ((input_cst != nullptr) && input_cst->IsMinusOne()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1760 | // Replace code looking like |
| 1761 | // DIV dst, src, -1 |
| 1762 | // with |
| 1763 | // NEG dst, src |
| 1764 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1765 | instruction, new (GetGraph()->GetAllocator()) HNeg(type, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1766 | RecordSimplification(); |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1767 | return; |
| 1768 | } |
| 1769 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1770 | if ((input_cst != nullptr) && DataType::IsFloatingPointType(type)) { |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1771 | // Try replacing code looking like |
| 1772 | // DIV dst, src, constant |
| 1773 | // with |
| 1774 | // MUL dst, src, 1 / constant |
| 1775 | HConstant* reciprocal = nullptr; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1776 | if (type == DataType::Type::kFloat64) { |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1777 | double value = input_cst->AsDoubleConstant()->GetValue(); |
| 1778 | if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) { |
| 1779 | reciprocal = GetGraph()->GetDoubleConstant(1.0 / value); |
| 1780 | } |
| 1781 | } else { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1782 | DCHECK_EQ(type, DataType::Type::kFloat32); |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1783 | float value = input_cst->AsFloatConstant()->GetValue(); |
| 1784 | if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) { |
| 1785 | reciprocal = GetGraph()->GetFloatConstant(1.0f / value); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | if (reciprocal != nullptr) { |
| 1790 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith( |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1791 | instruction, new (GetGraph()->GetAllocator()) HMul(type, input_other, reciprocal)); |
Nicolas Geoffray | 0d22184 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1792 | RecordSimplification(); |
| 1793 | return; |
| 1794 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
Evgeny Astigeevich | 6587d91 | 2020-06-12 10:51:43 +0100 | [diff] [blame] | 1798 | |
| 1799 | // Search HDiv having the specified dividend and divisor which is in the specified basic block. |
| 1800 | // Return nullptr if nothing has been found. |
| 1801 | static HInstruction* FindDivWithInputsInBasicBlock(HInstruction* dividend, |
| 1802 | HInstruction* divisor, |
| 1803 | HBasicBlock* basic_block) { |
| 1804 | for (const HUseListNode<HInstruction*>& use : dividend->GetUses()) { |
| 1805 | HInstruction* user = use.GetUser(); |
| 1806 | if (user->GetBlock() == basic_block && user->IsDiv() && user->InputAt(1) == divisor) { |
| 1807 | return user; |
| 1808 | } |
| 1809 | } |
| 1810 | return nullptr; |
| 1811 | } |
| 1812 | |
| 1813 | // If there is Div with the same inputs as Rem and in the same basic block, it can be reused. |
| 1814 | // Rem is replaced with Mul+Sub which use the found Div. |
| 1815 | void InstructionSimplifierVisitor::TryToReuseDiv(HRem* rem) { |
| 1816 | // As the optimization replaces Rem with Mul+Sub they prevent some loop optimizations |
| 1817 | // if the Rem is in a loop. |
| 1818 | // Check if it is allowed to optimize such Rems. |
| 1819 | if (rem->IsInLoop() && be_loop_friendly_) { |
| 1820 | return; |
| 1821 | } |
| 1822 | DataType::Type type = rem->GetResultType(); |
| 1823 | if (!DataType::IsIntOrLongType(type)) { |
| 1824 | return; |
| 1825 | } |
| 1826 | |
| 1827 | HBasicBlock* basic_block = rem->GetBlock(); |
| 1828 | HInstruction* dividend = rem->GetLeft(); |
| 1829 | HInstruction* divisor = rem->GetRight(); |
| 1830 | |
| 1831 | if (divisor->IsConstant()) { |
| 1832 | HConstant* input_cst = rem->GetConstantRight(); |
| 1833 | DCHECK(input_cst->IsIntConstant() || input_cst->IsLongConstant()); |
| 1834 | int64_t cst_value = Int64FromConstant(input_cst); |
| 1835 | if (cst_value == std::numeric_limits<int64_t>::min() || IsPowerOfTwo(std::abs(cst_value))) { |
| 1836 | // Such cases are usually handled in the code generator because they don't need Div at all. |
| 1837 | return; |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | HInstruction* quotient = FindDivWithInputsInBasicBlock(dividend, divisor, basic_block); |
| 1842 | if (quotient == nullptr) { |
| 1843 | return; |
| 1844 | } |
| 1845 | if (!quotient->StrictlyDominates(rem)) { |
| 1846 | quotient->MoveBefore(rem); |
| 1847 | } |
| 1848 | |
| 1849 | ArenaAllocator* allocator = GetGraph()->GetAllocator(); |
| 1850 | HInstruction* mul = new (allocator) HMul(type, quotient, divisor); |
| 1851 | basic_block->InsertInstructionBefore(mul, rem); |
| 1852 | HInstruction* sub = new (allocator) HSub(type, dividend, mul); |
| 1853 | basic_block->InsertInstructionBefore(sub, rem); |
| 1854 | rem->ReplaceWith(sub); |
| 1855 | basic_block->RemoveInstruction(rem); |
| 1856 | RecordSimplification(); |
| 1857 | } |
| 1858 | |
| 1859 | void InstructionSimplifierVisitor::VisitRem(HRem* rem) { |
| 1860 | TryToReuseDiv(rem); |
| 1861 | } |
| 1862 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1863 | void InstructionSimplifierVisitor::VisitMul(HMul* instruction) { |
| 1864 | HConstant* input_cst = instruction->GetConstantRight(); |
| 1865 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1866 | DataType::Type type = instruction->GetType(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1867 | HBasicBlock* block = instruction->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1868 | ArenaAllocator* allocator = GetGraph()->GetAllocator(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1869 | |
| 1870 | if (input_cst == nullptr) { |
| 1871 | return; |
| 1872 | } |
| 1873 | |
| 1874 | if (input_cst->IsOne()) { |
| 1875 | // Replace code looking like |
| 1876 | // MUL dst, src, 1 |
| 1877 | // with |
| 1878 | // src |
| 1879 | instruction->ReplaceWith(input_other); |
| 1880 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 1881 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1882 | return; |
| 1883 | } |
| 1884 | |
| 1885 | if (input_cst->IsMinusOne() && |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1886 | (DataType::IsFloatingPointType(type) || DataType::IsIntOrLongType(type))) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1887 | // Replace code looking like |
| 1888 | // MUL dst, src, -1 |
| 1889 | // with |
| 1890 | // NEG dst, src |
| 1891 | HNeg* neg = new (allocator) HNeg(type, input_other); |
| 1892 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1893 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1894 | return; |
| 1895 | } |
| 1896 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1897 | if (DataType::IsFloatingPointType(type) && |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1898 | ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) || |
| 1899 | (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) { |
| 1900 | // Replace code looking like |
| 1901 | // FP_MUL dst, src, 2.0 |
| 1902 | // with |
| 1903 | // FP_ADD dst, src, src |
| 1904 | // The 'int' and 'long' cases are handled below. |
| 1905 | block->ReplaceAndRemoveInstructionWith(instruction, |
| 1906 | new (allocator) HAdd(type, input_other, input_other)); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1907 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1908 | return; |
| 1909 | } |
| 1910 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1911 | if (DataType::IsIntOrLongType(type)) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1912 | int64_t factor = Int64FromConstant(input_cst); |
Serguei Katkov | 5384919 | 2015-04-20 14:22:27 +0600 | [diff] [blame] | 1913 | // Even though constant propagation also takes care of the zero case, other |
| 1914 | // optimizations can lead to having a zero multiplication. |
| 1915 | if (factor == 0) { |
| 1916 | // Replace code looking like |
| 1917 | // MUL dst, src, 0 |
| 1918 | // with |
| 1919 | // 0 |
| 1920 | instruction->ReplaceWith(input_cst); |
| 1921 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 1922 | RecordSimplification(); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1923 | return; |
Serguei Katkov | 5384919 | 2015-04-20 14:22:27 +0600 | [diff] [blame] | 1924 | } else if (IsPowerOfTwo(factor)) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1925 | // Replace code looking like |
| 1926 | // MUL dst, src, pow_of_2 |
| 1927 | // with |
| 1928 | // SHL dst, src, log2(pow_of_2) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 1929 | HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor)); |
Roland Levillain | 22c4922 | 2016-03-18 14:04:28 +0000 | [diff] [blame] | 1930 | HShl* shl = new (allocator) HShl(type, input_other, shift); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1931 | block->ReplaceAndRemoveInstructionWith(instruction, shl); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1932 | RecordSimplification(); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1933 | return; |
Alexandre Rames | 38db785 | 2015-11-20 15:02:45 +0000 | [diff] [blame] | 1934 | } else if (IsPowerOfTwo(factor - 1)) { |
| 1935 | // Transform code looking like |
| 1936 | // MUL dst, src, (2^n + 1) |
| 1937 | // into |
| 1938 | // SHL tmp, src, n |
| 1939 | // ADD dst, src, tmp |
| 1940 | HShl* shl = new (allocator) HShl(type, |
| 1941 | input_other, |
| 1942 | GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1))); |
| 1943 | HAdd* add = new (allocator) HAdd(type, input_other, shl); |
| 1944 | |
| 1945 | block->InsertInstructionBefore(shl, instruction); |
| 1946 | block->ReplaceAndRemoveInstructionWith(instruction, add); |
| 1947 | RecordSimplification(); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1948 | return; |
Alexandre Rames | 38db785 | 2015-11-20 15:02:45 +0000 | [diff] [blame] | 1949 | } else if (IsPowerOfTwo(factor + 1)) { |
| 1950 | // Transform code looking like |
| 1951 | // MUL dst, src, (2^n - 1) |
| 1952 | // into |
| 1953 | // SHL tmp, src, n |
| 1954 | // SUB dst, tmp, src |
| 1955 | HShl* shl = new (allocator) HShl(type, |
| 1956 | input_other, |
| 1957 | GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1))); |
| 1958 | HSub* sub = new (allocator) HSub(type, shl, input_other); |
| 1959 | |
| 1960 | block->InsertInstructionBefore(shl, instruction); |
| 1961 | block->ReplaceAndRemoveInstructionWith(instruction, sub); |
| 1962 | RecordSimplification(); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1963 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 1966 | |
| 1967 | // TryHandleAssociativeAndCommutativeOperation() does not remove its input, |
| 1968 | // so no need to return. |
| 1969 | TryHandleAssociativeAndCommutativeOperation(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1972 | void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) { |
| 1973 | HInstruction* input = instruction->GetInput(); |
| 1974 | if (input->IsNeg()) { |
| 1975 | // Replace code looking like |
| 1976 | // NEG tmp, src |
| 1977 | // NEG dst, tmp |
| 1978 | // with |
| 1979 | // src |
| 1980 | HNeg* previous_neg = input->AsNeg(); |
| 1981 | instruction->ReplaceWith(previous_neg->GetInput()); |
| 1982 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1983 | // We perform the optimization even if the input negation has environment |
| 1984 | // uses since it allows removing the current instruction. But we only delete |
| 1985 | // the input negation only if it is does not have any uses left. |
| 1986 | if (!previous_neg->HasUses()) { |
| 1987 | previous_neg->GetBlock()->RemoveInstruction(previous_neg); |
| 1988 | } |
| 1989 | RecordSimplification(); |
| 1990 | return; |
| 1991 | } |
| 1992 | |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 1993 | if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() && |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1994 | !DataType::IsFloatingPointType(input->GetType())) { |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1995 | // Replace code looking like |
| 1996 | // SUB tmp, a, b |
| 1997 | // NEG dst, tmp |
| 1998 | // with |
| 1999 | // SUB dst, b, a |
| 2000 | // We do not perform the optimization if the input subtraction has |
| 2001 | // environment uses or multiple non-environment uses as it could lead to |
| 2002 | // worse code. In particular, we do not want the live ranges of `a` and `b` |
| 2003 | // to be extended if we are not sure the initial 'SUB' instruction can be |
| 2004 | // removed. |
Serguei Katkov | 339dfc2 | 2015-04-20 12:29:32 +0600 | [diff] [blame] | 2005 | // 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] | 2006 | HSub* sub = input->AsSub(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2007 | HSub* new_sub = new (GetGraph()->GetAllocator()) HSub( |
| 2008 | instruction->GetType(), sub->GetRight(), sub->GetLeft()); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2009 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub); |
| 2010 | if (!sub->HasUses()) { |
| 2011 | sub->GetBlock()->RemoveInstruction(sub); |
| 2012 | } |
| 2013 | RecordSimplification(); |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | void InstructionSimplifierVisitor::VisitNot(HNot* instruction) { |
| 2018 | HInstruction* input = instruction->GetInput(); |
| 2019 | if (input->IsNot()) { |
| 2020 | // Replace code looking like |
| 2021 | // NOT tmp, src |
| 2022 | // NOT dst, tmp |
| 2023 | // with |
| 2024 | // src |
| 2025 | // We perform the optimization even if the input negation has environment |
| 2026 | // uses since it allows removing the current instruction. But we only delete |
| 2027 | // the input negation only if it is does not have any uses left. |
| 2028 | HNot* previous_not = input->AsNot(); |
| 2029 | instruction->ReplaceWith(previous_not->GetInput()); |
| 2030 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 2031 | if (!previous_not->HasUses()) { |
| 2032 | previous_not->GetBlock()->RemoveInstruction(previous_not); |
| 2033 | } |
| 2034 | RecordSimplification(); |
| 2035 | } |
| 2036 | } |
| 2037 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2038 | void InstructionSimplifierVisitor::VisitOr(HOr* instruction) { |
| 2039 | HConstant* input_cst = instruction->GetConstantRight(); |
| 2040 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 2041 | |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 2042 | if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2043 | // Replace code looking like |
| 2044 | // OR dst, src, 0 |
| 2045 | // with |
| 2046 | // src |
| 2047 | instruction->ReplaceWith(input_other); |
| 2048 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 2049 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2050 | return; |
| 2051 | } |
| 2052 | |
| 2053 | // We assume that GVN has run before, so we only perform a pointer comparison. |
| 2054 | // 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] | 2055 | // are still correct and only miss an optimization opportunity. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2056 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 2057 | // Replace code looking like |
| 2058 | // OR dst, src, src |
| 2059 | // with |
| 2060 | // src |
| 2061 | instruction->ReplaceWith(instruction->GetLeft()); |
| 2062 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 2063 | RecordSimplification(); |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 2064 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2065 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 2066 | |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 2067 | if (TryDeMorganNegationFactoring(instruction)) return; |
| 2068 | |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2069 | if (TryReplaceWithRotate(instruction)) { |
| 2070 | return; |
| 2071 | } |
| 2072 | |
| 2073 | // TryHandleAssociativeAndCommutativeOperation() does not remove its input, |
| 2074 | // so no need to return. |
| 2075 | TryHandleAssociativeAndCommutativeOperation(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | void InstructionSimplifierVisitor::VisitShl(HShl* instruction) { |
| 2079 | VisitShift(instruction); |
| 2080 | } |
| 2081 | |
| 2082 | void InstructionSimplifierVisitor::VisitShr(HShr* instruction) { |
| 2083 | VisitShift(instruction); |
| 2084 | } |
| 2085 | |
| 2086 | void InstructionSimplifierVisitor::VisitSub(HSub* instruction) { |
| 2087 | HConstant* input_cst = instruction->GetConstantRight(); |
| 2088 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 2089 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2090 | DataType::Type type = instruction->GetType(); |
| 2091 | if (DataType::IsFloatingPointType(type)) { |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 2092 | return; |
| 2093 | } |
| 2094 | |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 2095 | if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2096 | // Replace code looking like |
| 2097 | // SUB dst, src, 0 |
| 2098 | // with |
| 2099 | // src |
Serguei Katkov | 115b53f | 2015-08-05 17:03:30 +0600 | [diff] [blame] | 2100 | // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When |
| 2101 | // `x` is `-0.0`, the former expression yields `0.0`, while the later |
| 2102 | // yields `-0.0`. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2103 | instruction->ReplaceWith(input_other); |
| 2104 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 2105 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2106 | return; |
| 2107 | } |
| 2108 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2109 | HBasicBlock* block = instruction->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2110 | ArenaAllocator* allocator = GetGraph()->GetAllocator(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2111 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2112 | HInstruction* left = instruction->GetLeft(); |
| 2113 | HInstruction* right = instruction->GetRight(); |
| 2114 | if (left->IsConstant()) { |
| 2115 | if (Int64FromConstant(left->AsConstant()) == 0) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2116 | // Replace code looking like |
| 2117 | // SUB dst, 0, src |
| 2118 | // with |
| 2119 | // NEG dst, src |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2120 | // 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] | 2121 | // `x` is `0.0`, the former expression yields `0.0`, while the later |
| 2122 | // yields `-0.0`. |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2123 | HNeg* neg = new (allocator) HNeg(type, right); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2124 | block->ReplaceAndRemoveInstructionWith(instruction, neg); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2125 | RecordSimplification(); |
| 2126 | return; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2127 | } |
| 2128 | } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2129 | |
| 2130 | if (left->IsNeg() && right->IsNeg()) { |
| 2131 | if (TryMoveNegOnInputsAfterBinop(instruction)) { |
| 2132 | return; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) { |
| 2137 | // Replace code looking like |
| 2138 | // NEG tmp, b |
| 2139 | // SUB dst, a, tmp |
| 2140 | // with |
| 2141 | // ADD dst, a, b |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2142 | HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left, right->AsNeg()->GetInput()); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2143 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add); |
| 2144 | RecordSimplification(); |
| 2145 | right->GetBlock()->RemoveInstruction(right); |
| 2146 | return; |
| 2147 | } |
| 2148 | |
| 2149 | if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) { |
| 2150 | // Replace code looking like |
| 2151 | // NEG tmp, a |
| 2152 | // SUB dst, tmp, b |
| 2153 | // with |
| 2154 | // ADD tmp, a, b |
| 2155 | // NEG dst, tmp |
| 2156 | // The second version is not intrinsically better, but enables more |
| 2157 | // transformations. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2158 | HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left->AsNeg()->GetInput(), right); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2159 | instruction->GetBlock()->InsertInstructionBefore(add, instruction); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2160 | HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(instruction->GetType(), add); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2161 | instruction->GetBlock()->InsertInstructionBefore(neg, instruction); |
| 2162 | instruction->ReplaceWith(neg); |
| 2163 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 2164 | RecordSimplification(); |
| 2165 | left->GetBlock()->RemoveInstruction(left); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2166 | return; |
| 2167 | } |
| 2168 | |
| 2169 | if (TrySubtractionChainSimplification(instruction)) { |
| 2170 | return; |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2171 | } |
Maxim Kazantsev | d3278bd | 2016-07-12 15:55:33 +0600 | [diff] [blame] | 2172 | |
| 2173 | if (left->IsAdd()) { |
| 2174 | // Replace code patterns looking like |
| 2175 | // ADD dst1, x, y ADD dst1, x, y |
| 2176 | // SUB dst2, dst1, y SUB dst2, dst1, x |
| 2177 | // with |
| 2178 | // ADD dst1, x, y |
| 2179 | // SUB instruction is not needed in this case, we may use |
| 2180 | // one of inputs of ADD instead. |
| 2181 | // It is applicable to integral types only. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2182 | DCHECK(DataType::IsIntegralType(type)); |
Maxim Kazantsev | d3278bd | 2016-07-12 15:55:33 +0600 | [diff] [blame] | 2183 | if (left->InputAt(1) == right) { |
| 2184 | instruction->ReplaceWith(left->InputAt(0)); |
| 2185 | RecordSimplification(); |
| 2186 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 2187 | return; |
| 2188 | } else if (left->InputAt(0) == right) { |
| 2189 | instruction->ReplaceWith(left->InputAt(1)); |
| 2190 | RecordSimplification(); |
| 2191 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 2192 | return; |
| 2193 | } |
| 2194 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) { |
| 2198 | VisitShift(instruction); |
| 2199 | } |
| 2200 | |
| 2201 | void InstructionSimplifierVisitor::VisitXor(HXor* instruction) { |
| 2202 | HConstant* input_cst = instruction->GetConstantRight(); |
| 2203 | HInstruction* input_other = instruction->GetLeastConstantLeft(); |
| 2204 | |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 2205 | if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2206 | // Replace code looking like |
| 2207 | // XOR dst, src, 0 |
| 2208 | // with |
| 2209 | // src |
| 2210 | instruction->ReplaceWith(input_other); |
| 2211 | instruction->GetBlock()->RemoveInstruction(instruction); |
Alexandre Rames | c5809c3 | 2016-05-25 15:01:06 +0100 | [diff] [blame] | 2212 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2213 | return; |
| 2214 | } |
| 2215 | |
Sebastien Hertz | 9837caf | 2016-08-01 11:09:50 +0200 | [diff] [blame] | 2216 | if ((input_cst != nullptr) && input_cst->IsOne() |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2217 | && input_other->GetType() == DataType::Type::kBool) { |
Sebastien Hertz | 9837caf | 2016-08-01 11:09:50 +0200 | [diff] [blame] | 2218 | // Replace code looking like |
| 2219 | // XOR dst, src, 1 |
| 2220 | // with |
| 2221 | // BOOLEAN_NOT dst, src |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2222 | HBooleanNot* boolean_not = new (GetGraph()->GetAllocator()) HBooleanNot(input_other); |
Sebastien Hertz | 9837caf | 2016-08-01 11:09:50 +0200 | [diff] [blame] | 2223 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not); |
| 2224 | RecordSimplification(); |
| 2225 | return; |
| 2226 | } |
| 2227 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2228 | if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) { |
| 2229 | // Replace code looking like |
| 2230 | // XOR dst, src, 0xFFF...FF |
| 2231 | // with |
| 2232 | // NOT dst, src |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2233 | HNot* bitwise_not = new (GetGraph()->GetAllocator()) HNot(instruction->GetType(), input_other); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2234 | instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not); |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 2235 | RecordSimplification(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2236 | return; |
| 2237 | } |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 2238 | |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 2239 | HInstruction* left = instruction->GetLeft(); |
| 2240 | HInstruction* right = instruction->GetRight(); |
Alexandre Rames | 9f98025 | 2016-02-05 14:00:28 +0000 | [diff] [blame] | 2241 | if (((left->IsNot() && right->IsNot()) || |
| 2242 | (left->IsBooleanNot() && right->IsBooleanNot())) && |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 2243 | left->HasOnlyOneNonEnvironmentUse() && |
| 2244 | right->HasOnlyOneNonEnvironmentUse()) { |
| 2245 | // Replace code looking like |
| 2246 | // NOT nota, a |
| 2247 | // NOT notb, b |
| 2248 | // XOR dst, nota, notb |
| 2249 | // with |
| 2250 | // XOR dst, a, b |
Alexandre Rames | 9f98025 | 2016-02-05 14:00:28 +0000 | [diff] [blame] | 2251 | instruction->ReplaceInput(left->InputAt(0), 0); |
| 2252 | instruction->ReplaceInput(right->InputAt(0), 1); |
Alexandre Rames | ca0e3a0 | 2016-02-03 10:54:07 +0000 | [diff] [blame] | 2253 | left->GetBlock()->RemoveInstruction(left); |
| 2254 | right->GetBlock()->RemoveInstruction(right); |
| 2255 | RecordSimplification(); |
| 2256 | return; |
| 2257 | } |
| 2258 | |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2259 | if (TryReplaceWithRotate(instruction)) { |
| 2260 | return; |
| 2261 | } |
| 2262 | |
| 2263 | // TryHandleAssociativeAndCommutativeOperation() does not remove its input, |
| 2264 | // so no need to return. |
| 2265 | TryHandleAssociativeAndCommutativeOperation(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2268 | void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) { |
| 2269 | HInstruction* argument = instruction->InputAt(1); |
| 2270 | HInstruction* receiver = instruction->InputAt(0); |
| 2271 | if (receiver == argument) { |
| 2272 | // Because String.equals is an instance call, the receiver is |
| 2273 | // a null check if we don't know it's null. The argument however, will |
| 2274 | // be the actual object. So we cannot end up in a situation where both |
| 2275 | // are equal but could be null. |
| 2276 | DCHECK(CanEnsureNotNullAt(argument, instruction)); |
| 2277 | instruction->ReplaceWith(GetGraph()->GetIntConstant(1)); |
| 2278 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 2279 | } else { |
| 2280 | StringEqualsOptimizations optimizations(instruction); |
| 2281 | if (CanEnsureNotNullAt(argument, instruction)) { |
| 2282 | optimizations.SetArgumentNotNull(); |
| 2283 | } |
| 2284 | ScopedObjectAccess soa(Thread::Current()); |
| 2285 | ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo(); |
| 2286 | if (argument_rti.IsValid() && argument_rti.IsStringClass()) { |
| 2287 | optimizations.SetArgumentIsString(); |
| 2288 | } |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) { |
| 2293 | if (potential_length->IsArrayLength()) { |
| 2294 | return potential_length->InputAt(0) == potential_array; |
| 2295 | } |
| 2296 | |
| 2297 | if (potential_array->IsNewArray()) { |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 2298 | return potential_array->AsNewArray()->GetLength() == potential_length; |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | return false; |
| 2302 | } |
| 2303 | |
| 2304 | void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) { |
| 2305 | HInstruction* source = instruction->InputAt(0); |
| 2306 | HInstruction* destination = instruction->InputAt(2); |
| 2307 | HInstruction* count = instruction->InputAt(4); |
| 2308 | SystemArrayCopyOptimizations optimizations(instruction); |
| 2309 | if (CanEnsureNotNullAt(source, instruction)) { |
| 2310 | optimizations.SetSourceIsNotNull(); |
| 2311 | } |
| 2312 | if (CanEnsureNotNullAt(destination, instruction)) { |
| 2313 | optimizations.SetDestinationIsNotNull(); |
| 2314 | } |
| 2315 | if (destination == source) { |
| 2316 | optimizations.SetDestinationIsSource(); |
| 2317 | } |
| 2318 | |
| 2319 | if (IsArrayLengthOf(count, source)) { |
| 2320 | optimizations.SetCountIsSourceLength(); |
| 2321 | } |
| 2322 | |
| 2323 | if (IsArrayLengthOf(count, destination)) { |
| 2324 | optimizations.SetCountIsDestinationLength(); |
| 2325 | } |
| 2326 | |
| 2327 | { |
| 2328 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2329 | DataType::Type source_component_type = DataType::Type::kVoid; |
| 2330 | DataType::Type destination_component_type = DataType::Type::kVoid; |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2331 | ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo(); |
| 2332 | if (destination_rti.IsValid()) { |
| 2333 | if (destination_rti.IsObjectArray()) { |
| 2334 | if (destination_rti.IsExact()) { |
| 2335 | optimizations.SetDoesNotNeedTypeCheck(); |
| 2336 | } |
| 2337 | optimizations.SetDestinationIsTypedObjectArray(); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 2338 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2339 | if (destination_rti.IsPrimitiveArrayClass()) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2340 | destination_component_type = DataTypeFromPrimitive( |
| 2341 | destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2342 | optimizations.SetDestinationIsPrimitiveArray(); |
| 2343 | } else if (destination_rti.IsNonPrimitiveArrayClass()) { |
| 2344 | optimizations.SetDestinationIsNonPrimitiveArray(); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 2345 | } |
| 2346 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2347 | ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo(); |
| 2348 | if (source_rti.IsValid()) { |
| 2349 | if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) { |
| 2350 | optimizations.SetDoesNotNeedTypeCheck(); |
| 2351 | } |
| 2352 | if (source_rti.IsPrimitiveArrayClass()) { |
| 2353 | optimizations.SetSourceIsPrimitiveArray(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2354 | source_component_type = DataTypeFromPrimitive( |
| 2355 | source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType()); |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2356 | } else if (source_rti.IsNonPrimitiveArrayClass()) { |
| 2357 | optimizations.SetSourceIsNonPrimitiveArray(); |
| 2358 | } |
| 2359 | } |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2360 | // For primitive arrays, use their optimized ArtMethod implementations. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2361 | if ((source_component_type != DataType::Type::kVoid) && |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2362 | (source_component_type == destination_component_type)) { |
| 2363 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 2364 | PointerSize image_size = class_linker->GetImagePointerSize(); |
| 2365 | HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect(); |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 2366 | ObjPtr<mirror::Class> system = invoke->GetResolvedMethod()->GetDeclaringClass(); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2367 | ArtMethod* method = nullptr; |
| 2368 | switch (source_component_type) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2369 | case DataType::Type::kBool: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2370 | method = system->FindClassMethod("arraycopy", "([ZI[ZII)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2371 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2372 | case DataType::Type::kInt8: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2373 | method = system->FindClassMethod("arraycopy", "([BI[BII)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2374 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2375 | case DataType::Type::kUint16: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2376 | method = system->FindClassMethod("arraycopy", "([CI[CII)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2377 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2378 | case DataType::Type::kInt16: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2379 | method = system->FindClassMethod("arraycopy", "([SI[SII)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2380 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2381 | case DataType::Type::kInt32: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2382 | method = system->FindClassMethod("arraycopy", "([II[III)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2383 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2384 | case DataType::Type::kFloat32: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2385 | method = system->FindClassMethod("arraycopy", "([FI[FII)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2386 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2387 | case DataType::Type::kInt64: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2388 | method = system->FindClassMethod("arraycopy", "([JI[JII)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2389 | break; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2390 | case DataType::Type::kFloat64: |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2391 | method = system->FindClassMethod("arraycopy", "([DI[DII)V", image_size); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2392 | break; |
| 2393 | default: |
| 2394 | LOG(FATAL) << "Unreachable"; |
| 2395 | } |
| 2396 | DCHECK(method != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2397 | DCHECK(method->IsStatic()); |
| 2398 | DCHECK(method->GetDeclaringClass() == system); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2399 | invoke->SetResolvedMethod(method); |
| 2400 | // Sharpen the new invoke. Note that we do not update the dex method index of |
| 2401 | // the invoke, as we would need to look it up in the current dex file, and it |
| 2402 | // is unlikely that it exists. The most usual situation for such typed |
| 2403 | // arraycopy methods is a direct pointer to the boot image. |
Nicolas Geoffray | d5a8695 | 2021-01-19 10:35:54 +0000 | [diff] [blame] | 2404 | invoke->SetDispatchInfo(HSharpening::SharpenLoadMethod( |
| 2405 | method, |
| 2406 | /* has_method_id= */ true, |
| 2407 | /* for_interface_call= */ false, |
| 2408 | codegen_)); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 2409 | } |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2413 | void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) { |
| 2414 | DCHECK(invoke->IsInvokeStaticOrDirect()); |
| 2415 | uint32_t dex_pc = invoke->GetDexPc(); |
| 2416 | HInstruction* x = invoke->InputAt(0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2417 | DataType::Type type = x->GetType(); |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2418 | // Set proper bit pattern for NaN and replace intrinsic with raw version. |
| 2419 | HInstruction* nan; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2420 | if (type == DataType::Type::kFloat64) { |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2421 | nan = GetGraph()->GetLongConstant(0x7ff8000000000000L); |
| 2422 | invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits, |
Nicolas Geoffray | 8f2eb25 | 2020-11-06 13:39:54 +0000 | [diff] [blame] | 2423 | kNeedsEnvironment, |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2424 | kNoSideEffects, |
| 2425 | kNoThrow); |
| 2426 | } else { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2427 | DCHECK_EQ(type, DataType::Type::kFloat32); |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2428 | nan = GetGraph()->GetIntConstant(0x7fc00000); |
| 2429 | invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits, |
Nicolas Geoffray | 8f2eb25 | 2020-11-06 13:39:54 +0000 | [diff] [blame] | 2430 | kNeedsEnvironment, |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2431 | kNoSideEffects, |
| 2432 | kNoThrow); |
| 2433 | } |
| 2434 | // Test IsNaN(x), which is the same as x != x. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2435 | HCondition* condition = new (GetGraph()->GetAllocator()) HNotEqual(x, x, dex_pc); |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2436 | condition->SetBias(ComparisonBias::kLtBias); |
| 2437 | invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext()); |
| 2438 | // Select between the two. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 2439 | HInstruction* select = new (GetGraph()->GetAllocator()) HSelect(condition, nan, invoke, dex_pc); |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2440 | invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext()); |
| 2441 | invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0 |
| 2442 | } |
| 2443 | |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2444 | void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) { |
| 2445 | HInstruction* str = invoke->InputAt(0); |
| 2446 | HInstruction* index = invoke->InputAt(1); |
| 2447 | uint32_t dex_pc = invoke->GetDexPc(); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 2448 | ArenaAllocator* allocator = GetGraph()->GetAllocator(); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2449 | // We treat String as an array to allow DCE and BCE to seamlessly work on strings, |
| 2450 | // so create the HArrayLength, HBoundsCheck and HArrayGet. |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2451 | HArrayLength* length = new (allocator) HArrayLength(str, dex_pc, /* is_string_length= */ true); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2452 | invoke->GetBlock()->InsertInstructionBefore(length, invoke); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 2453 | HBoundsCheck* bounds_check = new (allocator) HBoundsCheck( |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2454 | index, length, dex_pc, /* is_string_char_at= */ true); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2455 | invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 2456 | HArrayGet* array_get = new (allocator) HArrayGet(str, |
| 2457 | bounds_check, |
| 2458 | DataType::Type::kUint16, |
| 2459 | SideEffects::None(), // Strings are immutable. |
| 2460 | dex_pc, |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2461 | /* is_string_char_at= */ true); |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2462 | invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get); |
| 2463 | bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment()); |
| 2464 | GetGraph()->SetHasBoundsChecks(true); |
| 2465 | } |
| 2466 | |
Vladimir Marko | 5f84607 | 2020-04-09 13:20:11 +0100 | [diff] [blame] | 2467 | void InstructionSimplifierVisitor::SimplifyStringLength(HInvoke* invoke) { |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 2468 | HInstruction* str = invoke->InputAt(0); |
| 2469 | uint32_t dex_pc = invoke->GetDexPc(); |
| 2470 | // We treat String as an array to allow DCE and BCE to seamlessly work on strings, |
| 2471 | // so create the HArrayLength. |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2472 | HArrayLength* length = |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 2473 | new (GetGraph()->GetAllocator()) HArrayLength(str, dex_pc, /* is_string_length= */ true); |
Vladimir Marko | 5f84607 | 2020-04-09 13:20:11 +0100 | [diff] [blame] | 2474 | invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, length); |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
Vladimir Marko | 6fa4404 | 2018-03-19 18:42:49 +0000 | [diff] [blame] | 2477 | void InstructionSimplifierVisitor::SimplifyStringIndexOf(HInvoke* invoke) { |
| 2478 | DCHECK(invoke->GetIntrinsic() == Intrinsics::kStringIndexOf || |
| 2479 | invoke->GetIntrinsic() == Intrinsics::kStringIndexOfAfter); |
| 2480 | if (invoke->InputAt(0)->IsLoadString()) { |
| 2481 | HLoadString* load_string = invoke->InputAt(0)->AsLoadString(); |
| 2482 | const DexFile& dex_file = load_string->GetDexFile(); |
| 2483 | uint32_t utf16_length; |
| 2484 | const char* data = |
| 2485 | dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), &utf16_length); |
| 2486 | if (utf16_length == 0) { |
| 2487 | invoke->ReplaceWith(GetGraph()->GetIntConstant(-1)); |
| 2488 | invoke->GetBlock()->RemoveInstruction(invoke); |
| 2489 | RecordSimplification(); |
| 2490 | return; |
| 2491 | } |
| 2492 | if (utf16_length == 1 && invoke->GetIntrinsic() == Intrinsics::kStringIndexOf) { |
| 2493 | // Simplify to HSelect(HEquals(., load_string.charAt(0)), 0, -1). |
| 2494 | // If the sought character is supplementary, this gives the correct result, i.e. -1. |
| 2495 | uint32_t c = GetUtf16FromUtf8(&data); |
| 2496 | DCHECK_EQ(GetTrailingUtf16Char(c), 0u); |
| 2497 | DCHECK_EQ(GetLeadingUtf16Char(c), c); |
| 2498 | uint32_t dex_pc = invoke->GetDexPc(); |
| 2499 | ArenaAllocator* allocator = GetGraph()->GetAllocator(); |
| 2500 | HEqual* equal = |
| 2501 | new (allocator) HEqual(invoke->InputAt(1), GetGraph()->GetIntConstant(c), dex_pc); |
| 2502 | invoke->GetBlock()->InsertInstructionBefore(equal, invoke); |
| 2503 | HSelect* result = new (allocator) HSelect(equal, |
| 2504 | GetGraph()->GetIntConstant(0), |
| 2505 | GetGraph()->GetIntConstant(-1), |
| 2506 | dex_pc); |
| 2507 | invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, result); |
| 2508 | RecordSimplification(); |
| 2509 | return; |
| 2510 | } |
| 2511 | } |
| 2512 | } |
| 2513 | |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 2514 | // This method should only be used on intrinsics whose sole way of throwing an |
| 2515 | // exception is raising a NPE when the nth argument is null. If that argument |
| 2516 | // is provably non-null, we can clear the flag. |
| 2517 | void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) { |
| 2518 | HInstruction* arg = invoke->InputAt(n); |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2519 | if (invoke->CanThrow() && !arg->CanBeNull()) { |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 2520 | invoke->SetCanThrow(false); |
| 2521 | } |
| 2522 | } |
| 2523 | |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2524 | // Methods that return "this" can replace the returned value with the receiver. |
| 2525 | void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) { |
| 2526 | if (invoke->HasUses()) { |
| 2527 | HInstruction* receiver = invoke->InputAt(0); |
| 2528 | invoke->ReplaceWith(receiver); |
| 2529 | RecordSimplification(); |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | // Helper method for StringBuffer escape analysis. |
| 2534 | static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) { |
| 2535 | if (user->IsInvokeStaticOrDirect()) { |
| 2536 | // Any constructor on StringBuffer is okay. |
Aart Bik | ab2270f | 2016-12-15 09:36:31 -0800 | [diff] [blame] | 2537 | return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr && |
| 2538 | user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() && |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2539 | user->InputAt(0) == reference; |
| 2540 | } else if (user->IsInvokeVirtual()) { |
| 2541 | switch (user->AsInvokeVirtual()->GetIntrinsic()) { |
| 2542 | case Intrinsics::kStringBufferLength: |
| 2543 | case Intrinsics::kStringBufferToString: |
| 2544 | DCHECK_EQ(user->InputAt(0), reference); |
| 2545 | return true; |
| 2546 | case Intrinsics::kStringBufferAppend: |
| 2547 | // Returns "this", so only okay if no further uses. |
| 2548 | DCHECK_EQ(user->InputAt(0), reference); |
| 2549 | DCHECK_NE(user->InputAt(1), reference); |
| 2550 | return !user->HasUses(); |
| 2551 | default: |
| 2552 | break; |
| 2553 | } |
| 2554 | } |
| 2555 | return false; |
| 2556 | } |
| 2557 | |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2558 | static bool TryReplaceStringBuilderAppend(HInvoke* invoke) { |
| 2559 | DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringBuilderToString); |
| 2560 | if (invoke->CanThrowIntoCatchBlock()) { |
| 2561 | return false; |
| 2562 | } |
| 2563 | |
| 2564 | HBasicBlock* block = invoke->GetBlock(); |
| 2565 | HInstruction* sb = invoke->InputAt(0); |
| 2566 | |
| 2567 | // We support only a new StringBuilder, otherwise we cannot ensure that |
| 2568 | // the StringBuilder data does not need to be populated for other users. |
| 2569 | if (!sb->IsNewInstance()) { |
| 2570 | return false; |
| 2571 | } |
| 2572 | |
| 2573 | // For now, we support only single-block recognition. |
| 2574 | // (Ternary operators feeding the append could be implemented.) |
| 2575 | for (const HUseListNode<HInstruction*>& use : sb->GetUses()) { |
| 2576 | if (use.GetUser()->GetBlock() != block) { |
| 2577 | return false; |
| 2578 | } |
Vladimir Marko | a18f5ae | 2019-12-13 12:53:39 +0000 | [diff] [blame] | 2579 | // The append pattern uses the StringBuilder only as the first argument. |
| 2580 | if (use.GetIndex() != 0u) { |
| 2581 | return false; |
| 2582 | } |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | // Collect args and check for unexpected uses. |
| 2586 | // We expect one call to a constructor with no arguments, one constructor fence (unless |
| 2587 | // eliminated), some number of append calls and one call to StringBuilder.toString(). |
| 2588 | bool seen_constructor = false; |
| 2589 | bool seen_constructor_fence = false; |
| 2590 | bool seen_to_string = false; |
| 2591 | uint32_t format = 0u; |
| 2592 | uint32_t num_args = 0u; |
| 2593 | HInstruction* args[StringBuilderAppend::kMaxArgs]; // Added in reverse order. |
Vladimir Marko | a18f5ae | 2019-12-13 12:53:39 +0000 | [diff] [blame] | 2594 | for (HBackwardInstructionIterator iter(block->GetInstructions()); !iter.Done(); iter.Advance()) { |
| 2595 | HInstruction* user = iter.Current(); |
| 2596 | // Instructions of interest apply to `sb`, skip those that do not involve `sb`. |
| 2597 | if (user->InputCount() == 0u || user->InputAt(0u) != sb) { |
| 2598 | continue; |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2599 | } |
Vladimir Marko | a18f5ae | 2019-12-13 12:53:39 +0000 | [diff] [blame] | 2600 | // We visit the uses in reverse order, so the StringBuilder.toString() must come first. |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2601 | if (!seen_to_string) { |
Vladimir Marko | a18f5ae | 2019-12-13 12:53:39 +0000 | [diff] [blame] | 2602 | if (user == invoke) { |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2603 | seen_to_string = true; |
| 2604 | continue; |
| 2605 | } else { |
| 2606 | return false; |
| 2607 | } |
| 2608 | } |
| 2609 | // Then we should see the arguments. |
| 2610 | if (user->IsInvokeVirtual()) { |
| 2611 | HInvokeVirtual* as_invoke_virtual = user->AsInvokeVirtual(); |
| 2612 | DCHECK(!seen_constructor); |
| 2613 | DCHECK(!seen_constructor_fence); |
| 2614 | StringBuilderAppend::Argument arg; |
| 2615 | switch (as_invoke_virtual->GetIntrinsic()) { |
| 2616 | case Intrinsics::kStringBuilderAppendObject: |
| 2617 | // TODO: Unimplemented, needs to call String.valueOf(). |
| 2618 | return false; |
| 2619 | case Intrinsics::kStringBuilderAppendString: |
| 2620 | arg = StringBuilderAppend::Argument::kString; |
| 2621 | break; |
| 2622 | case Intrinsics::kStringBuilderAppendCharArray: |
| 2623 | // TODO: Unimplemented, StringBuilder.append(char[]) can throw NPE and we would |
| 2624 | // not have the correct stack trace for it. |
| 2625 | return false; |
| 2626 | case Intrinsics::kStringBuilderAppendBoolean: |
| 2627 | arg = StringBuilderAppend::Argument::kBoolean; |
| 2628 | break; |
| 2629 | case Intrinsics::kStringBuilderAppendChar: |
| 2630 | arg = StringBuilderAppend::Argument::kChar; |
| 2631 | break; |
| 2632 | case Intrinsics::kStringBuilderAppendInt: |
| 2633 | arg = StringBuilderAppend::Argument::kInt; |
| 2634 | break; |
| 2635 | case Intrinsics::kStringBuilderAppendLong: |
| 2636 | arg = StringBuilderAppend::Argument::kLong; |
| 2637 | break; |
| 2638 | case Intrinsics::kStringBuilderAppendCharSequence: { |
| 2639 | ReferenceTypeInfo rti = user->AsInvokeVirtual()->InputAt(1)->GetReferenceTypeInfo(); |
| 2640 | if (!rti.IsValid()) { |
| 2641 | return false; |
| 2642 | } |
| 2643 | ScopedObjectAccess soa(Thread::Current()); |
| 2644 | Handle<mirror::Class> input_type = rti.GetTypeHandle(); |
| 2645 | DCHECK(input_type != nullptr); |
| 2646 | if (input_type.Get() == GetClassRoot<mirror::String>()) { |
| 2647 | arg = StringBuilderAppend::Argument::kString; |
| 2648 | } else { |
| 2649 | // TODO: Check and implement for StringBuilder. We could find the StringBuilder's |
| 2650 | // internal char[] inconsistent with the length, or the string compression |
| 2651 | // of the result could be compromised with a concurrent modification, and |
| 2652 | // we would need to throw appropriate exceptions. |
| 2653 | return false; |
| 2654 | } |
| 2655 | break; |
| 2656 | } |
| 2657 | case Intrinsics::kStringBuilderAppendFloat: |
| 2658 | case Intrinsics::kStringBuilderAppendDouble: |
| 2659 | // TODO: Unimplemented, needs to call FloatingDecimal.getBinaryToASCIIConverter(). |
| 2660 | return false; |
| 2661 | default: { |
| 2662 | return false; |
| 2663 | } |
| 2664 | } |
| 2665 | // Uses of the append return value should have been replaced with the first input. |
| 2666 | DCHECK(!as_invoke_virtual->HasUses()); |
| 2667 | DCHECK(!as_invoke_virtual->HasEnvironmentUses()); |
| 2668 | if (num_args == StringBuilderAppend::kMaxArgs) { |
| 2669 | return false; |
| 2670 | } |
| 2671 | format = (format << StringBuilderAppend::kBitsPerArg) | static_cast<uint32_t>(arg); |
| 2672 | args[num_args] = as_invoke_virtual->InputAt(1u); |
| 2673 | ++num_args; |
| 2674 | } else if (user->IsInvokeStaticOrDirect() && |
| 2675 | user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr && |
| 2676 | user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() && |
| 2677 | user->AsInvokeStaticOrDirect()->GetNumberOfArguments() == 1u) { |
| 2678 | // After arguments, we should see the constructor. |
| 2679 | // We accept only the constructor with no extra arguments. |
| 2680 | DCHECK(!seen_constructor); |
| 2681 | DCHECK(!seen_constructor_fence); |
| 2682 | seen_constructor = true; |
| 2683 | } else if (user->IsConstructorFence()) { |
| 2684 | // The last use we see is the constructor fence. |
| 2685 | DCHECK(seen_constructor); |
| 2686 | DCHECK(!seen_constructor_fence); |
| 2687 | seen_constructor_fence = true; |
| 2688 | } else { |
| 2689 | return false; |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | if (num_args == 0u) { |
| 2694 | return false; |
| 2695 | } |
| 2696 | |
| 2697 | // Check environment uses. |
| 2698 | for (const HUseListNode<HEnvironment*>& use : sb->GetEnvUses()) { |
| 2699 | HInstruction* holder = use.GetUser()->GetHolder(); |
| 2700 | if (holder->GetBlock() != block) { |
| 2701 | return false; |
| 2702 | } |
| 2703 | // Accept only calls on the StringBuilder (which shall all be removed). |
| 2704 | // TODO: Carve-out for const-string? Or rely on environment pruning (to be implemented)? |
| 2705 | if (holder->InputCount() == 0 || holder->InputAt(0) != sb) { |
| 2706 | return false; |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | // Create replacement instruction. |
| 2711 | HIntConstant* fmt = block->GetGraph()->GetIntConstant(static_cast<int32_t>(format)); |
| 2712 | ArenaAllocator* allocator = block->GetGraph()->GetAllocator(); |
| 2713 | HStringBuilderAppend* append = |
| 2714 | new (allocator) HStringBuilderAppend(fmt, num_args, allocator, invoke->GetDexPc()); |
| 2715 | append->SetReferenceTypeInfo(invoke->GetReferenceTypeInfo()); |
| 2716 | for (size_t i = 0; i != num_args; ++i) { |
| 2717 | append->SetArgumentAt(i, args[num_args - 1u - i]); |
| 2718 | } |
| 2719 | block->InsertInstructionBefore(append, invoke); |
Vladimir Marko | b1fe5e1 | 2020-03-10 14:30:49 +0000 | [diff] [blame] | 2720 | DCHECK(!invoke->CanBeNull()); |
| 2721 | DCHECK(!append->CanBeNull()); |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2722 | invoke->ReplaceWith(append); |
| 2723 | // Copy environment, except for the StringBuilder uses. |
Vladimir Marko | 56f1332 | 2019-11-15 14:07:19 +0000 | [diff] [blame] | 2724 | for (HEnvironment* env = invoke->GetEnvironment(); env != nullptr; env = env->GetParent()) { |
| 2725 | for (size_t i = 0, size = env->Size(); i != size; ++i) { |
| 2726 | if (env->GetInstructionAt(i) == sb) { |
| 2727 | env->RemoveAsUserOfInput(i); |
| 2728 | env->SetRawEnvAt(i, /*instruction=*/ nullptr); |
| 2729 | } |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2730 | } |
| 2731 | } |
| 2732 | append->CopyEnvironmentFrom(invoke->GetEnvironment()); |
| 2733 | // Remove the old instruction. |
| 2734 | block->RemoveInstruction(invoke); |
| 2735 | // Remove the StringBuilder's uses and StringBuilder. |
| 2736 | while (sb->HasNonEnvironmentUses()) { |
| 2737 | block->RemoveInstruction(sb->GetUses().front().GetUser()); |
| 2738 | } |
| 2739 | DCHECK(!sb->HasEnvironmentUses()); |
| 2740 | block->RemoveInstruction(sb); |
| 2741 | return true; |
| 2742 | } |
| 2743 | |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2744 | // Certain allocation intrinsics are not removed by dead code elimination |
| 2745 | // because of potentially throwing an OOM exception or other side effects. |
| 2746 | // This method removes such intrinsics when special circumstances allow. |
| 2747 | void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) { |
| 2748 | if (!invoke->HasUses()) { |
| 2749 | // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring |
| 2750 | // the potential OOM of course. Otherwise, we must ensure the receiver object of this |
| 2751 | // call does not escape since only thread-local synchronization may be removed. |
| 2752 | bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString; |
| 2753 | HInstruction* receiver = invoke->InputAt(0); |
| 2754 | if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) { |
| 2755 | invoke->GetBlock()->RemoveInstruction(invoke); |
| 2756 | RecordSimplification(); |
| 2757 | } |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 2758 | } else if (invoke->GetIntrinsic() == Intrinsics::kStringBuilderToString && |
| 2759 | TryReplaceStringBuilderAppend(invoke)) { |
| 2760 | RecordSimplification(); |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2761 | } |
| 2762 | } |
| 2763 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 2764 | void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) { |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2765 | switch (instruction->GetIntrinsic()) { |
| 2766 | case Intrinsics::kStringEquals: |
| 2767 | SimplifyStringEquals(instruction); |
| 2768 | break; |
| 2769 | case Intrinsics::kSystemArrayCopy: |
| 2770 | SimplifySystemArrayCopy(instruction); |
| 2771 | break; |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2772 | case Intrinsics::kFloatFloatToIntBits: |
| 2773 | case Intrinsics::kDoubleDoubleToLongBits: |
| 2774 | SimplifyFP2Int(instruction); |
| 2775 | break; |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2776 | case Intrinsics::kStringCharAt: |
Vladimir Marko | 5f84607 | 2020-04-09 13:20:11 +0100 | [diff] [blame] | 2777 | // Instruction builder creates intermediate representation directly |
| 2778 | // but the inliner can sharpen CharSequence.charAt() to String.charAt(). |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 2779 | SimplifyStringCharAt(instruction); |
| 2780 | break; |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 2781 | case Intrinsics::kStringLength: |
Vladimir Marko | 5f84607 | 2020-04-09 13:20:11 +0100 | [diff] [blame] | 2782 | // Instruction builder creates intermediate representation directly |
| 2783 | // but the inliner can sharpen CharSequence.length() to String.length(). |
| 2784 | SimplifyStringLength(instruction); |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 2785 | break; |
Vladimir Marko | 6fa4404 | 2018-03-19 18:42:49 +0000 | [diff] [blame] | 2786 | case Intrinsics::kStringIndexOf: |
| 2787 | case Intrinsics::kStringIndexOfAfter: |
| 2788 | SimplifyStringIndexOf(instruction); |
| 2789 | break; |
Aart Bik | ff7d89c | 2016-11-07 08:49:28 -0800 | [diff] [blame] | 2790 | case Intrinsics::kStringStringIndexOf: |
| 2791 | case Intrinsics::kStringStringIndexOfAfter: |
| 2792 | SimplifyNPEOnArgN(instruction, 1); // 0th has own NullCheck |
| 2793 | break; |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2794 | case Intrinsics::kStringBufferAppend: |
Vladimir Marko | d456117 | 2017-10-30 17:48:25 +0000 | [diff] [blame] | 2795 | case Intrinsics::kStringBuilderAppendObject: |
| 2796 | case Intrinsics::kStringBuilderAppendString: |
| 2797 | case Intrinsics::kStringBuilderAppendCharSequence: |
| 2798 | case Intrinsics::kStringBuilderAppendCharArray: |
| 2799 | case Intrinsics::kStringBuilderAppendBoolean: |
| 2800 | case Intrinsics::kStringBuilderAppendChar: |
| 2801 | case Intrinsics::kStringBuilderAppendInt: |
| 2802 | case Intrinsics::kStringBuilderAppendLong: |
| 2803 | case Intrinsics::kStringBuilderAppendFloat: |
| 2804 | case Intrinsics::kStringBuilderAppendDouble: |
Aart Bik | 71bf7b4 | 2016-11-16 10:17:46 -0800 | [diff] [blame] | 2805 | SimplifyReturnThis(instruction); |
| 2806 | break; |
| 2807 | case Intrinsics::kStringBufferToString: |
| 2808 | case Intrinsics::kStringBuilderToString: |
| 2809 | SimplifyAllocationIntrinsic(instruction); |
| 2810 | break; |
Vladimir Marko | 5f84607 | 2020-04-09 13:20:11 +0100 | [diff] [blame] | 2811 | case Intrinsics::kIntegerRotateRight: |
| 2812 | case Intrinsics::kLongRotateRight: |
| 2813 | case Intrinsics::kIntegerRotateLeft: |
| 2814 | case Intrinsics::kLongRotateLeft: |
| 2815 | case Intrinsics::kIntegerCompare: |
| 2816 | case Intrinsics::kLongCompare: |
| 2817 | case Intrinsics::kIntegerSignum: |
| 2818 | case Intrinsics::kLongSignum: |
| 2819 | case Intrinsics::kFloatIsNaN: |
| 2820 | case Intrinsics::kDoubleIsNaN: |
| 2821 | case Intrinsics::kStringIsEmpty: |
Aart Bik | 1193259 | 2016-03-08 12:42:25 -0800 | [diff] [blame] | 2822 | case Intrinsics::kUnsafeLoadFence: |
Aart Bik | 1193259 | 2016-03-08 12:42:25 -0800 | [diff] [blame] | 2823 | case Intrinsics::kUnsafeStoreFence: |
Aart Bik | 1193259 | 2016-03-08 12:42:25 -0800 | [diff] [blame] | 2824 | case Intrinsics::kUnsafeFullFence: |
Orion Hodson | 4a4610a | 2017-09-28 16:57:55 +0100 | [diff] [blame] | 2825 | case Intrinsics::kVarHandleFullFence: |
Orion Hodson | 4a4610a | 2017-09-28 16:57:55 +0100 | [diff] [blame] | 2826 | case Intrinsics::kVarHandleAcquireFence: |
Orion Hodson | 4a4610a | 2017-09-28 16:57:55 +0100 | [diff] [blame] | 2827 | case Intrinsics::kVarHandleReleaseFence: |
Orion Hodson | 4a4610a | 2017-09-28 16:57:55 +0100 | [diff] [blame] | 2828 | case Intrinsics::kVarHandleLoadLoadFence: |
Orion Hodson | 4a4610a | 2017-09-28 16:57:55 +0100 | [diff] [blame] | 2829 | case Intrinsics::kVarHandleStoreStoreFence: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2830 | case Intrinsics::kMathMinIntInt: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2831 | case Intrinsics::kMathMinLongLong: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2832 | case Intrinsics::kMathMinFloatFloat: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2833 | case Intrinsics::kMathMinDoubleDouble: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2834 | case Intrinsics::kMathMaxIntInt: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2835 | case Intrinsics::kMathMaxLongLong: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2836 | case Intrinsics::kMathMaxFloatFloat: |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 2837 | case Intrinsics::kMathMaxDoubleDouble: |
Aart Bik | 3dad341 | 2018-02-28 12:01:46 -0800 | [diff] [blame] | 2838 | case Intrinsics::kMathAbsInt: |
Aart Bik | 3dad341 | 2018-02-28 12:01:46 -0800 | [diff] [blame] | 2839 | case Intrinsics::kMathAbsLong: |
Aart Bik | 3dad341 | 2018-02-28 12:01:46 -0800 | [diff] [blame] | 2840 | case Intrinsics::kMathAbsFloat: |
Aart Bik | 3dad341 | 2018-02-28 12:01:46 -0800 | [diff] [blame] | 2841 | case Intrinsics::kMathAbsDouble: |
Vladimir Marko | 5f84607 | 2020-04-09 13:20:11 +0100 | [diff] [blame] | 2842 | // These are replaced by intermediate representation in the instruction builder. |
| 2843 | LOG(FATAL) << "Unexpected " << static_cast<Intrinsics>(instruction->GetIntrinsic()); |
| 2844 | UNREACHABLE(); |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 2845 | default: |
| 2846 | break; |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | |
Aart Bik | bb245d1 | 2015-10-19 11:05:03 -0700 | [diff] [blame] | 2850 | void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) { |
| 2851 | HInstruction* cond = deoptimize->InputAt(0); |
| 2852 | if (cond->IsConstant()) { |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 2853 | if (cond->AsIntConstant()->IsFalse()) { |
Aart Bik | bb245d1 | 2015-10-19 11:05:03 -0700 | [diff] [blame] | 2854 | // Never deopt: instruction can be removed. |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 2855 | if (deoptimize->GuardsAnInput()) { |
| 2856 | deoptimize->ReplaceWith(deoptimize->GuardedInput()); |
| 2857 | } |
Aart Bik | bb245d1 | 2015-10-19 11:05:03 -0700 | [diff] [blame] | 2858 | deoptimize->GetBlock()->RemoveInstruction(deoptimize); |
| 2859 | } else { |
| 2860 | // Always deopt. |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2865 | // Replace code looking like |
| 2866 | // OP y, x, const1 |
| 2867 | // OP z, y, const2 |
| 2868 | // with |
| 2869 | // OP z, x, const3 |
| 2870 | // where OP is both an associative and a commutative operation. |
| 2871 | bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation( |
| 2872 | HBinaryOperation* instruction) { |
| 2873 | DCHECK(instruction->IsCommutative()); |
| 2874 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2875 | if (!DataType::IsIntegralType(instruction->GetType())) { |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2876 | return false; |
| 2877 | } |
| 2878 | |
| 2879 | HInstruction* left = instruction->GetLeft(); |
| 2880 | HInstruction* right = instruction->GetRight(); |
| 2881 | // Variable names as described above. |
| 2882 | HConstant* const2; |
| 2883 | HBinaryOperation* y; |
| 2884 | |
Vladimir Marko | 0dcccd8 | 2018-05-04 13:32:25 +0100 | [diff] [blame] | 2885 | if (instruction->GetKind() == left->GetKind() && right->IsConstant()) { |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2886 | const2 = right->AsConstant(); |
| 2887 | y = left->AsBinaryOperation(); |
Vladimir Marko | 0dcccd8 | 2018-05-04 13:32:25 +0100 | [diff] [blame] | 2888 | } else if (left->IsConstant() && instruction->GetKind() == right->GetKind()) { |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2889 | const2 = left->AsConstant(); |
| 2890 | y = right->AsBinaryOperation(); |
| 2891 | } else { |
| 2892 | // The node does not match the pattern. |
| 2893 | return false; |
| 2894 | } |
| 2895 | |
| 2896 | // If `y` has more than one use, we do not perform the optimization |
| 2897 | // because it might increase code size (e.g. if the new constant is |
| 2898 | // no longer encodable as an immediate operand in the target ISA). |
| 2899 | if (!y->HasOnlyOneNonEnvironmentUse()) { |
| 2900 | return false; |
| 2901 | } |
| 2902 | |
| 2903 | // GetConstantRight() can return both left and right constants |
| 2904 | // for commutative operations. |
| 2905 | HConstant* const1 = y->GetConstantRight(); |
| 2906 | if (const1 == nullptr) { |
| 2907 | return false; |
| 2908 | } |
| 2909 | |
| 2910 | instruction->ReplaceInput(const1, 0); |
| 2911 | instruction->ReplaceInput(const2, 1); |
| 2912 | HConstant* const3 = instruction->TryStaticEvaluation(); |
| 2913 | DCHECK(const3 != nullptr); |
| 2914 | instruction->ReplaceInput(y->GetLeastConstantLeft(), 0); |
| 2915 | instruction->ReplaceInput(const3, 1); |
| 2916 | RecordSimplification(); |
| 2917 | return true; |
| 2918 | } |
| 2919 | |
| 2920 | static HBinaryOperation* AsAddOrSub(HInstruction* binop) { |
| 2921 | return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr; |
| 2922 | } |
| 2923 | |
| 2924 | // Helper function that performs addition statically, considering the result type. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2925 | static int64_t ComputeAddition(DataType::Type type, int64_t x, int64_t y) { |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2926 | // Use the Compute() method for consistency with TryStaticEvaluation(). |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2927 | if (type == DataType::Type::kInt32) { |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2928 | return HAdd::Compute<int32_t>(x, y); |
| 2929 | } else { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2930 | DCHECK_EQ(type, DataType::Type::kInt64); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2931 | return HAdd::Compute<int64_t>(x, y); |
| 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | // Helper function that handles the child classes of HConstant |
| 2936 | // and returns an integer with the appropriate sign. |
| 2937 | static int64_t GetValue(HConstant* constant, bool is_negated) { |
| 2938 | int64_t ret = Int64FromConstant(constant); |
| 2939 | return is_negated ? -ret : ret; |
| 2940 | } |
| 2941 | |
| 2942 | // Replace code looking like |
| 2943 | // OP1 y, x, const1 |
| 2944 | // OP2 z, y, const2 |
| 2945 | // with |
| 2946 | // OP3 z, x, const3 |
| 2947 | // where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB. |
| 2948 | bool InstructionSimplifierVisitor::TrySubtractionChainSimplification( |
| 2949 | HBinaryOperation* instruction) { |
| 2950 | DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName(); |
| 2951 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 2952 | DataType::Type type = instruction->GetType(); |
| 2953 | if (!DataType::IsIntegralType(type)) { |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2954 | return false; |
| 2955 | } |
| 2956 | |
| 2957 | HInstruction* left = instruction->GetLeft(); |
| 2958 | HInstruction* right = instruction->GetRight(); |
| 2959 | // Variable names as described above. |
| 2960 | HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant(); |
| 2961 | if (const2 == nullptr) { |
| 2962 | return false; |
| 2963 | } |
| 2964 | |
| 2965 | HBinaryOperation* y = (AsAddOrSub(left) != nullptr) |
| 2966 | ? left->AsBinaryOperation() |
| 2967 | : AsAddOrSub(right); |
| 2968 | // If y has more than one use, we do not perform the optimization because |
| 2969 | // it might increase code size (e.g. if the new constant is no longer |
| 2970 | // encodable as an immediate operand in the target ISA). |
| 2971 | if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) { |
| 2972 | return false; |
| 2973 | } |
| 2974 | |
| 2975 | left = y->GetLeft(); |
| 2976 | HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant(); |
| 2977 | if (const1 == nullptr) { |
| 2978 | return false; |
| 2979 | } |
| 2980 | |
| 2981 | HInstruction* x = (const1 == left) ? y->GetRight() : left; |
| 2982 | // If both inputs are constants, let the constant folding pass deal with it. |
| 2983 | if (x->IsConstant()) { |
| 2984 | return false; |
| 2985 | } |
| 2986 | |
| 2987 | bool is_const2_negated = (const2 == right) && instruction->IsSub(); |
| 2988 | int64_t const2_val = GetValue(const2, is_const2_negated); |
| 2989 | bool is_y_negated = (y == right) && instruction->IsSub(); |
| 2990 | right = y->GetRight(); |
| 2991 | bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub()); |
| 2992 | int64_t const1_val = GetValue(const1, is_const1_negated); |
| 2993 | bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub()); |
| 2994 | int64_t const3_val = ComputeAddition(type, const1_val, const2_val); |
| 2995 | HBasicBlock* block = instruction->GetBlock(); |
| 2996 | HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 2997 | ArenaAllocator* allocator = instruction->GetAllocator(); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 2998 | HInstruction* z; |
| 2999 | |
| 3000 | if (is_x_negated) { |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 3001 | z = new (allocator) HSub(type, const3, x, instruction->GetDexPc()); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 3002 | } else { |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 3003 | z = new (allocator) HAdd(type, x, const3, instruction->GetDexPc()); |
Anton Kirilov | e14dc86 | 2016-05-13 17:56:15 +0100 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | block->ReplaceAndRemoveInstructionWith(instruction, z); |
| 3007 | RecordSimplification(); |
| 3008 | return true; |
| 3009 | } |
| 3010 | |
Lena Djokic | bc5460b | 2017-07-20 16:07:36 +0200 | [diff] [blame] | 3011 | void InstructionSimplifierVisitor::VisitVecMul(HVecMul* instruction) { |
| 3012 | if (TryCombineVecMultiplyAccumulate(instruction)) { |
| 3013 | RecordSimplification(); |
| 3014 | } |
| 3015 | } |
| 3016 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 3017 | } // namespace art |