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