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