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