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