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