blob: 23a432ef9d2977a73503f73e33334098ecef8245 [file] [log] [blame]
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001/*
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 Gampec6ea7d02017-02-01 16:46:28 -080019#include "art_method-inl.h"
20#include "class_linker-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010021#include "class_root-inl.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010022#include "data_type-inl.h"
Aart Bik71bf7b42016-11-16 10:17:46 -080023#include "escape.h"
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010024#include "intrinsics.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000025#include "mirror/class-inl.h"
Alex Light3a73ffb2021-01-25 14:11:05 +000026#include "optimizing/nodes.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "sharpening.h"
Vladimir Marko552a1342017-10-31 10:56:47 +000029#include "string_builder_append.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030
Vladimir Marko0a516052019-10-14 13:00:44 +000031namespace art {
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032
Artem Serovcced8ba2017-07-19 18:18:09 +010033// Whether to run an exhaustive test of individual HInstructions cloning when each instruction
34// is replaced with its copy if it is clonable.
35static constexpr bool kTestInstructionClonerExhaustively = false;
36
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010037class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000038 public:
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000039 InstructionSimplifierVisitor(HGraph* graph,
40 CodeGenerator* codegen,
Evgeny Astigeevich6587d912020-06-12 10:51:43 +010041 OptimizingCompilerStats* stats,
42 bool be_loop_friendly)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010043 : HGraphDelegateVisitor(graph),
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000044 codegen_(codegen),
Evgeny Astigeevich6587d912020-06-12 10:51:43 +010045 stats_(stats),
46 be_loop_friendly_(be_loop_friendly) {}
Alexandre Rames188d4312015-04-09 18:30:21 +010047
Aart Bik24773202018-04-26 10:28:51 -070048 bool Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000049
50 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010051 void RecordSimplification() {
52 simplification_occurred_ = true;
53 simplifications_at_current_position_++;
Vladimir Markocd09e1f2017-11-24 15:02:40 +000054 MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplifications);
Alexandre Rames188d4312015-04-09 18:30:21 +010055 }
56
Scott Wakeling40a04bf2015-12-11 09:50:36 +000057 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
58 bool TryReplaceWithRotate(HBinaryOperation* instruction);
59 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
60 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
61 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
62
Alexandre Rames188d4312015-04-09 18:30:21 +010063 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000064 // `op` should be either HOr or HAnd.
65 // De Morgan's laws:
66 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
67 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010068 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
69 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
Lena Djokicbc5460b2017-07-20 16:07:36 +020070 bool TryCombineVecMultiplyAccumulate(HVecMul* mul);
Evgeny Astigeevich6587d912020-06-12 10:51:43 +010071 void TryToReuseDiv(HRem* rem);
Anton Kirilove14dc862016-05-13 17:56:15 +010072
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000073 void VisitShift(HBinaryOperation* shift);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010074 void VisitEqual(HEqual* equal) override;
75 void VisitNotEqual(HNotEqual* equal) override;
76 void VisitBooleanNot(HBooleanNot* bool_not) override;
77 void VisitInstanceFieldSet(HInstanceFieldSet* equal) override;
78 void VisitStaticFieldSet(HStaticFieldSet* equal) override;
79 void VisitArraySet(HArraySet* equal) override;
80 void VisitTypeConversion(HTypeConversion* instruction) override;
81 void VisitNullCheck(HNullCheck* instruction) override;
82 void VisitArrayLength(HArrayLength* instruction) override;
83 void VisitCheckCast(HCheckCast* instruction) override;
84 void VisitAbs(HAbs* instruction) override;
85 void VisitAdd(HAdd* instruction) override;
86 void VisitAnd(HAnd* instruction) override;
87 void VisitCondition(HCondition* instruction) override;
88 void VisitGreaterThan(HGreaterThan* condition) override;
89 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) override;
90 void VisitLessThan(HLessThan* condition) override;
91 void VisitLessThanOrEqual(HLessThanOrEqual* condition) override;
92 void VisitBelow(HBelow* condition) override;
93 void VisitBelowOrEqual(HBelowOrEqual* condition) override;
94 void VisitAbove(HAbove* condition) override;
95 void VisitAboveOrEqual(HAboveOrEqual* condition) override;
96 void VisitDiv(HDiv* instruction) override;
Evgeny Astigeevich6587d912020-06-12 10:51:43 +010097 void VisitRem(HRem* instruction) override;
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010098 void VisitMul(HMul* instruction) override;
99 void VisitNeg(HNeg* instruction) override;
100 void VisitNot(HNot* instruction) override;
101 void VisitOr(HOr* instruction) override;
102 void VisitShl(HShl* instruction) override;
103 void VisitShr(HShr* instruction) override;
104 void VisitSub(HSub* instruction) override;
105 void VisitUShr(HUShr* instruction) override;
106 void VisitXor(HXor* instruction) override;
107 void VisitSelect(HSelect* select) override;
108 void VisitIf(HIf* instruction) override;
109 void VisitInstanceOf(HInstanceOf* instruction) override;
110 void VisitInvoke(HInvoke* invoke) override;
111 void VisitDeoptimize(HDeoptimize* deoptimize) override;
112 void VisitVecMul(HVecMul* instruction) override;
Alex Light3a73ffb2021-01-25 14:11:05 +0000113 void VisitPredicatedInstanceFieldGet(HPredicatedInstanceFieldGet* instruction) override;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100114
115 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000116
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100117 void SimplifySystemArrayCopy(HInvoke* invoke);
118 void SimplifyStringEquals(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800119 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100120 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Marko5f846072020-04-09 13:20:11 +0100121 void SimplifyStringLength(HInvoke* invoke);
Vladimir Marko6fa44042018-03-19 18:42:49 +0000122 void SimplifyStringIndexOf(HInvoke* invoke);
Aart Bikff7d89c2016-11-07 08:49:28 -0800123 void SimplifyNPEOnArgN(HInvoke* invoke, size_t);
Aart Bik71bf7b42016-11-16 10:17:46 -0800124 void SimplifyReturnThis(HInvoke* invoke);
125 void SimplifyAllocationIntrinsic(HInvoke* invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100126
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000127 CodeGenerator* codegen_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000128 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100129 bool simplification_occurred_ = false;
130 int simplifications_at_current_position_ = 0;
Evgeny Astigeevich6587d912020-06-12 10:51:43 +0100131 // Prohibit optimizations which can affect HInductionVarAnalysis/HLoopOptimization
132 // and prevent loop optimizations:
133 // true - avoid such optimizations.
134 // false - allow such optimizations.
135 // Checked by the following optimizations:
136 // - TryToReuseDiv: simplification of Div+Rem into Div+Mul+Sub.
137 bool be_loop_friendly_;
Aart Bik2767f4b2016-10-28 15:03:53 -0700138 // We ensure we do not loop infinitely. The value should not be too high, since that
139 // would allow looping around the same basic block too many times. The value should
140 // not be too low either, however, since we want to allow revisiting a basic block
141 // with many statements and simplifications at least once.
142 static constexpr int kMaxSamePositionSimplifications = 50;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000143};
144
Aart Bik24773202018-04-26 10:28:51 -0700145bool InstructionSimplifier::Run() {
Artem Serovcced8ba2017-07-19 18:18:09 +0100146 if (kTestInstructionClonerExhaustively) {
147 CloneAndReplaceInstructionVisitor visitor(graph_);
148 visitor.VisitReversePostOrder();
149 }
150
Evgeny Astigeevich6587d912020-06-12 10:51:43 +0100151 bool be_loop_friendly = (use_all_optimizations_ == false);
152
153 InstructionSimplifierVisitor visitor(graph_, codegen_, stats_, be_loop_friendly);
Aart Bik24773202018-04-26 10:28:51 -0700154 return visitor.Run();
Alexandre Rames188d4312015-04-09 18:30:21 +0100155}
156
Aart Bik24773202018-04-26 10:28:51 -0700157bool InstructionSimplifierVisitor::Run() {
158 bool didSimplify = false;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100159 // Iterate in reverse post order to open up more simplifications to users
160 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100161 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100162 // The simplification of an instruction to another instruction may yield
163 // possibilities for other simplifications. So although we perform a reverse
164 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100165 do {
166 simplification_occurred_ = false;
167 VisitBasicBlock(block);
Aart Bik24773202018-04-26 10:28:51 -0700168 if (simplification_occurred_) {
169 didSimplify = true;
170 }
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100171 } while (simplification_occurred_ &&
172 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100173 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100174 }
Aart Bik24773202018-04-26 10:28:51 -0700175 return didSimplify;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176}
177
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000178namespace {
179
180bool AreAllBitsSet(HConstant* constant) {
181 return Int64FromConstant(constant) == -1;
182}
183
184} // namespace
185
Alexandre Rames188d4312015-04-09 18:30:21 +0100186// Returns true if the code was simplified to use only one negation operation
187// after the binary operation instead of one on each of the inputs.
188bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
189 DCHECK(binop->IsAdd() || binop->IsSub());
190 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
191 HNeg* left_neg = binop->GetLeft()->AsNeg();
192 HNeg* right_neg = binop->GetRight()->AsNeg();
193 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
194 !right_neg->HasOnlyOneNonEnvironmentUse()) {
195 return false;
196 }
197 // Replace code looking like
198 // NEG tmp1, a
199 // NEG tmp2, b
200 // ADD dst, tmp1, tmp2
201 // with
202 // ADD tmp, a, b
203 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600204 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
205 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
206 // while the later yields `-0.0`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100207 if (!DataType::IsIntegralType(binop->GetType())) {
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600208 return false;
209 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100210 binop->ReplaceInput(left_neg->GetInput(), 0);
211 binop->ReplaceInput(right_neg->GetInput(), 1);
212 left_neg->GetBlock()->RemoveInstruction(left_neg);
213 right_neg->GetBlock()->RemoveInstruction(right_neg);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100214 HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(binop->GetType(), binop);
Alexandre Rames188d4312015-04-09 18:30:21 +0100215 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
216 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
217 RecordSimplification();
218 return true;
219}
220
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000221bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
222 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100223 DataType::Type type = op->GetType();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000224 HInstruction* left = op->GetLeft();
225 HInstruction* right = op->GetRight();
226
227 // We can apply De Morgan's laws if both inputs are Not's and are only used
228 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000229 if (((left->IsNot() && right->IsNot()) ||
230 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000231 left->HasOnlyOneNonEnvironmentUse() &&
232 right->HasOnlyOneNonEnvironmentUse()) {
233 // Replace code looking like
234 // NOT nota, a
235 // NOT notb, b
236 // AND dst, nota, notb (respectively OR)
237 // with
238 // OR or, a, b (respectively AND)
239 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000240 HInstruction* src_left = left->InputAt(0);
241 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000242 uint32_t dex_pc = op->GetDexPc();
243
244 // Remove the negations on the inputs.
245 left->ReplaceWith(src_left);
246 right->ReplaceWith(src_right);
247 left->GetBlock()->RemoveInstruction(left);
248 right->GetBlock()->RemoveInstruction(right);
249
250 // Replace the `HAnd` or `HOr`.
251 HBinaryOperation* hbin;
252 if (op->IsAnd()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100253 hbin = new (GetGraph()->GetAllocator()) HOr(type, src_left, src_right, dex_pc);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000254 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100255 hbin = new (GetGraph()->GetAllocator()) HAnd(type, src_left, src_right, dex_pc);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000256 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000257 HInstruction* hnot;
258 if (left->IsBooleanNot()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100259 hnot = new (GetGraph()->GetAllocator()) HBooleanNot(hbin, dex_pc);
Alexandre Rames9f980252016-02-05 14:00:28 +0000260 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100261 hnot = new (GetGraph()->GetAllocator()) HNot(type, hbin, dex_pc);
Alexandre Rames9f980252016-02-05 14:00:28 +0000262 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000263
264 op->GetBlock()->InsertInstructionBefore(hbin, op);
265 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
266
267 RecordSimplification();
268 return true;
269 }
270
271 return false;
272}
273
Lena Djokicbc5460b2017-07-20 16:07:36 +0200274bool InstructionSimplifierVisitor::TryCombineVecMultiplyAccumulate(HVecMul* mul) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100275 DataType::Type type = mul->GetPackedType();
Lena Djokicbc5460b2017-07-20 16:07:36 +0200276 InstructionSet isa = codegen_->GetInstructionSet();
277 switch (isa) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000278 case InstructionSet::kArm64:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100279 if (!(type == DataType::Type::kUint8 ||
280 type == DataType::Type::kInt8 ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100281 type == DataType::Type::kUint16 ||
282 type == DataType::Type::kInt16 ||
283 type == DataType::Type::kInt32)) {
Lena Djokicbc5460b2017-07-20 16:07:36 +0200284 return false;
285 }
286 break;
Lena Djokicbc5460b2017-07-20 16:07:36 +0200287 default:
288 return false;
289 }
290
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100291 ArenaAllocator* allocator = mul->GetBlock()->GetGraph()->GetAllocator();
Artem Serov8ba4de12019-12-04 21:10:23 +0000292 if (!mul->HasOnlyOneNonEnvironmentUse()) {
293 return false;
294 }
295 HInstruction* binop = mul->GetUses().front().GetUser();
296 if (!binop->IsVecAdd() && !binop->IsVecSub()) {
297 return false;
Lena Djokicbc5460b2017-07-20 16:07:36 +0200298 }
299
Artem Serov8ba4de12019-12-04 21:10:23 +0000300 // Replace code looking like
301 // VECMUL tmp, x, y
302 // VECADD/SUB dst, acc, tmp
303 // with
304 // VECMULACC dst, acc, x, y
305 // Note that we do not want to (unconditionally) perform the merge when the
306 // multiplication has multiple uses and it can be merged in all of them.
307 // Multiple uses could happen on the same control-flow path, and we would
308 // then increase the amount of work. In the future we could try to evaluate
309 // whether all uses are on different control-flow paths (using dominance and
310 // reverse-dominance information) and only perform the merge when they are.
311 HInstruction* accumulator = nullptr;
312 HVecBinaryOperation* vec_binop = binop->AsVecBinaryOperation();
313 HInstruction* binop_left = vec_binop->GetLeft();
314 HInstruction* binop_right = vec_binop->GetRight();
315 // This is always true since the `HVecMul` has only one use (which is checked above).
316 DCHECK_NE(binop_left, binop_right);
317 if (binop_right == mul) {
318 accumulator = binop_left;
319 } else {
320 DCHECK_EQ(binop_left, mul);
321 // Only addition is commutative.
322 if (!binop->IsVecAdd()) {
323 return false;
324 }
325 accumulator = binop_right;
326 }
327
328 DCHECK(accumulator != nullptr);
329 HInstruction::InstructionKind kind =
330 binop->IsVecAdd() ? HInstruction::kAdd : HInstruction::kSub;
331
332 bool predicated_simd = vec_binop->IsPredicated();
333 if (predicated_simd && !HVecOperation::HaveSamePredicate(vec_binop, mul)) {
334 return false;
335 }
336
337 HVecMultiplyAccumulate* mulacc =
338 new (allocator) HVecMultiplyAccumulate(allocator,
339 kind,
340 accumulator,
341 mul->GetLeft(),
342 mul->GetRight(),
343 vec_binop->GetPackedType(),
344 vec_binop->GetVectorLength(),
345 vec_binop->GetDexPc());
346
347
348
349 vec_binop->GetBlock()->ReplaceAndRemoveInstructionWith(vec_binop, mulacc);
350 if (predicated_simd) {
351 mulacc->SetGoverningPredicate(vec_binop->GetGoverningPredicate(),
352 vec_binop->GetPredicationKind());
353 }
354
355 DCHECK(!mul->HasUses());
356 mul->GetBlock()->RemoveInstruction(mul);
357 return true;
Lena Djokicbc5460b2017-07-20 16:07:36 +0200358}
359
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000360void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
361 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100362 HInstruction* shift_amount = instruction->GetRight();
363 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100365 int64_t implicit_mask = (value->GetType() == DataType::Type::kInt64)
Alexandre Rames50518442016-06-27 11:39:19 +0100366 ? kMaxLongShiftDistance
367 : kMaxIntShiftDistance;
368
369 if (shift_amount->IsConstant()) {
370 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
Aart Bik50e20d52017-05-05 14:07:29 -0700371 int64_t masked_cst = cst & implicit_mask;
372 if (masked_cst == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400373 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100374 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400375 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100376 // value
377 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400378 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100379 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100380 return;
Aart Bik50e20d52017-05-05 14:07:29 -0700381 } else if (masked_cst != cst) {
382 // Replace code looking like
383 // SHL dst, value, cst
384 // where cst exceeds maximum distance with the equivalent
385 // SHL dst, value, cst & implicit_mask
386 // (as defined by shift semantics). This ensures other
387 // optimizations do not need to special case for such situations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100388 DCHECK_EQ(shift_amount->GetType(), DataType::Type::kInt32);
Andreas Gampe3db70682018-12-26 15:12:03 -0800389 instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index= */ 1);
Aart Bik50e20d52017-05-05 14:07:29 -0700390 RecordSimplification();
391 return;
Alexandre Rames50518442016-06-27 11:39:19 +0100392 }
393 }
394
395 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
Vladimir Marko7033d492017-09-28 16:32:24 +0100396 // unnecessary And/Or/Xor/Add/Sub/TypeConversion operations on the shift amount that do not
397 // affect the relevant bits.
Alexandre Rames50518442016-06-27 11:39:19 +0100398 // Replace code looking like
Vladimir Marko7033d492017-09-28 16:32:24 +0100399 // AND adjusted_shift, shift, <superset of implicit mask>
400 // [OR/XOR/ADD/SUB adjusted_shift, shift, <value not overlapping with implicit mask>]
401 // [<conversion-from-integral-non-64-bit-type> adjusted_shift, shift]
402 // SHL dst, value, adjusted_shift
Alexandre Rames50518442016-06-27 11:39:19 +0100403 // with
404 // SHL dst, value, shift
Vladimir Marko7033d492017-09-28 16:32:24 +0100405 if (shift_amount->IsAnd() ||
406 shift_amount->IsOr() ||
407 shift_amount->IsXor() ||
408 shift_amount->IsAdd() ||
409 shift_amount->IsSub()) {
410 int64_t required_result = shift_amount->IsAnd() ? implicit_mask : 0;
411 HBinaryOperation* bin_op = shift_amount->AsBinaryOperation();
412 HConstant* mask = bin_op->GetConstantRight();
413 if (mask != nullptr && (Int64FromConstant(mask) & implicit_mask) == required_result) {
414 instruction->ReplaceInput(bin_op->GetLeastConstantLeft(), 1);
Alexandre Rames50518442016-06-27 11:39:19 +0100415 RecordSimplification();
Vladimir Marko7033d492017-09-28 16:32:24 +0100416 return;
417 }
418 } else if (shift_amount->IsTypeConversion()) {
419 DCHECK_NE(shift_amount->GetType(), DataType::Type::kBool); // We never convert to bool.
420 DataType::Type source_type = shift_amount->InputAt(0)->GetType();
421 // Non-integral and 64-bit source types require an explicit type conversion.
422 if (DataType::IsIntegralType(source_type) && !DataType::Is64BitType(source_type)) {
423 instruction->ReplaceInput(shift_amount->AsTypeConversion()->GetInput(), 1);
424 RecordSimplification();
425 return;
Mark Mendellba56d062015-05-05 21:34:03 -0400426 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000427 }
428}
429
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000430static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
431 return (sub->GetRight() == other &&
432 sub->GetLeft()->IsConstant() &&
433 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
434}
435
436bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
437 HUShr* ushr,
438 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000439 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100440 HRor* ror =
441 new (GetGraph()->GetAllocator()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000442 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
443 if (!ushr->HasUses()) {
444 ushr->GetBlock()->RemoveInstruction(ushr);
445 }
446 if (!ushr->GetRight()->HasUses()) {
447 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
448 }
449 if (!shl->HasUses()) {
450 shl->GetBlock()->RemoveInstruction(shl);
451 }
452 if (!shl->GetRight()->HasUses()) {
453 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
454 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100455 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000456 return true;
457}
458
459// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
460bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000461 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
462 HInstruction* left = op->GetLeft();
463 HInstruction* right = op->GetRight();
464 // If we have an UShr and a Shl (in either order).
465 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
466 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
467 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100468 DCHECK(DataType::IsIntOrLongType(ushr->GetType()));
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000469 if (ushr->GetType() == shl->GetType() &&
470 ushr->GetLeft() == shl->GetLeft()) {
471 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
472 // Shift distances are both constant, try replacing with Ror if they
473 // add up to the register size.
474 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
475 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
476 // Shift distances are potentially of the form x and (reg_size - x).
477 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
478 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
479 // Shift distances are potentially of the form d and -d.
480 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
481 }
482 }
483 }
484 return false;
485}
486
487// Try replacing code looking like (x >>> #rdist OP x << #ldist):
488// UShr dst, x, #rdist
489// Shl tmp, x, #ldist
490// OP dst, dst, tmp
491// or like (x >>> #rdist OP x << #-ldist):
492// UShr dst, x, #rdist
493// Shl tmp, x, #-ldist
494// OP dst, dst, tmp
495// with
496// Ror dst, x, #rdist
497bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
498 HUShr* ushr,
499 HShl* shl) {
500 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100501 size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000502 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
503 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
504 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
505 ReplaceRotateWithRor(op, ushr, shl);
506 return true;
507 }
508 return false;
509}
510
511// Replace code looking like (x >>> -d OP x << d):
512// Neg neg, d
513// UShr dst, x, neg
514// Shl tmp, x, d
515// OP dst, dst, tmp
516// with
517// Neg neg, d
518// Ror dst, x, neg
519// *** OR ***
520// Replace code looking like (x >>> d OP x << -d):
521// UShr dst, x, d
522// Neg neg, d
523// Shl tmp, x, neg
524// OP dst, dst, tmp
525// with
526// Ror dst, x, d
527bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
528 HUShr* ushr,
529 HShl* shl) {
530 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
531 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
532 bool neg_is_left = shl->GetRight()->IsNeg();
533 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
534 // And the shift distance being negated is the distance being shifted the other way.
535 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
536 ReplaceRotateWithRor(op, ushr, shl);
537 }
538 return false;
539}
540
541// Try replacing code looking like (x >>> d OP x << (#bits - d)):
542// UShr dst, x, d
543// Sub ld, #bits, d
544// Shl tmp, x, ld
545// OP dst, dst, tmp
546// with
547// Ror dst, x, d
548// *** OR ***
549// Replace code looking like (x >>> (#bits - d) OP x << d):
550// Sub rd, #bits, d
551// UShr dst, x, rd
552// Shl tmp, x, d
553// OP dst, dst, tmp
554// with
555// Neg neg, d
556// Ror dst, x, neg
557bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
558 HUShr* ushr,
559 HShl* shl) {
560 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
561 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000563 HInstruction* shl_shift = shl->GetRight();
564 HInstruction* ushr_shift = ushr->GetRight();
565 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
566 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
567 return ReplaceRotateWithRor(op, ushr, shl);
568 }
569 return false;
570}
571
Calin Juravle10e244f2015-01-26 18:54:32 +0000572void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
573 HInstruction* obj = null_check->InputAt(0);
574 if (!obj->CanBeNull()) {
575 null_check->ReplaceWith(obj);
576 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000577 if (stats_ != nullptr) {
578 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
579 }
580 }
581}
582
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100583bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
584 if (!input->CanBeNull()) {
585 return true;
586 }
587
Vladimir Marko46817b82016-03-29 12:21:58 +0100588 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
589 HInstruction* user = use.GetUser();
590 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100591 return true;
592 }
593 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100594
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100595 return false;
596}
597
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100598// Returns whether doing a type test between the class of `object` against `klass` has
599// a statically known outcome. The result of the test is stored in `outcome`.
Vladimir Marko175e7862018-03-27 09:03:13 +0000600static bool TypeCheckHasKnownOutcome(ReferenceTypeInfo class_rti,
601 HInstruction* object,
602 /*out*/bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000603 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
604 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
605 ScopedObjectAccess soa(Thread::Current());
606 if (!obj_rti.IsValid()) {
607 // We run the simplifier before the reference type propagation so type info might not be
608 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100609 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000610 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100611
Calin Juravle98893e12015-10-02 21:05:03 +0100612 if (!class_rti.IsValid()) {
613 // Happens when the loaded class is unresolved.
Alex Lightbb550e42021-04-21 17:04:13 -0700614 if (obj_rti.IsExact()) {
615 // outcome == 'true' && obj_rti is valid implies that class_rti is valid.
616 // Since that's a contradiction we must not pass this check.
617 *outcome = false;
618 return true;
619 } else {
620 // We aren't able to say anything in particular since we don't know the
621 // exact type of the object.
622 return false;
623 }
Calin Juravle98893e12015-10-02 21:05:03 +0100624 }
625 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000626 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100627 *outcome = true;
628 return true;
629 } else if (obj_rti.IsExact()) {
630 // The test failed at compile time so will also fail at runtime.
631 *outcome = false;
632 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100633 } else if (!class_rti.IsInterface()
634 && !obj_rti.IsInterface()
635 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100636 // Different type hierarchy. The test will fail.
637 *outcome = false;
638 return true;
639 }
640 return false;
641}
642
643void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
644 HInstruction* object = check_cast->InputAt(0);
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100645 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100646 check_cast->ClearMustDoNullCheck();
647 }
648
649 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000650 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700651 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100652 return;
653 }
654
Alex Lightbb550e42021-04-21 17:04:13 -0700655 // Minor correctness check.
656 DCHECK(check_cast->GetTargetClass()->StrictlyDominates(check_cast))
657 << "Illegal graph!\n"
658 << check_cast->DumpWithArgs();
659
Roland Levillain05e34f42018-05-24 13:19:05 +0000660 // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder
661 // the return value check with the `outcome` check, b/27651442.
Vladimir Markoa65ed302016-03-14 21:21:29 +0000662 bool outcome = false;
Vladimir Marko175e7862018-03-27 09:03:13 +0000663 if (TypeCheckHasKnownOutcome(check_cast->GetTargetClassRTI(), object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100664 if (outcome) {
665 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700666 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Vladimir Marko175e7862018-03-27 09:03:13 +0000667 if (check_cast->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) {
668 HLoadClass* load_class = check_cast->GetTargetClass();
Alex Lightbb550e42021-04-21 17:04:13 -0700669 if (!load_class->HasUses() && !load_class->NeedsAccessCheck()) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000670 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
Alex Lightbb550e42021-04-21 17:04:13 -0700671 // However, here we know that it cannot because the checkcast was successful, hence
Vladimir Marko175e7862018-03-27 09:03:13 +0000672 // the class was already loaded.
673 load_class->GetBlock()->RemoveInstruction(load_class);
674 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700675 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100676 } else {
Alex Lightbb550e42021-04-21 17:04:13 -0700677 // TODO Don't do anything for exceptional cases for now. Ideally we should
678 // remove all instructions and blocks this instruction dominates and
679 // replace it with a manual throw.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100680 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000681 }
682}
683
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100684void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100685 HInstruction* object = instruction->InputAt(0);
Calin Juravlee53fb552015-10-07 17:51:52 +0100686
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100687 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100688 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100689 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100690 instruction->ClearMustDoNullCheck();
691 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100692
693 HGraph* graph = GetGraph();
694 if (object->IsNullConstant()) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000695 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100696 instruction->ReplaceWith(graph->GetIntConstant(0));
697 instruction->GetBlock()->RemoveInstruction(instruction);
698 RecordSimplification();
699 return;
700 }
701
Alex Lightbb550e42021-04-21 17:04:13 -0700702 // Minor correctness check.
703 DCHECK(instruction->GetTargetClass()->StrictlyDominates(instruction))
704 << "Illegal graph!\n"
705 << instruction->DumpWithArgs();
706
Roland Levillain05e34f42018-05-24 13:19:05 +0000707 // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder
708 // the return value check with the `outcome` check, b/27651442.
Vladimir Marko24bd8952016-03-15 10:40:33 +0000709 bool outcome = false;
Vladimir Marko175e7862018-03-27 09:03:13 +0000710 if (TypeCheckHasKnownOutcome(instruction->GetTargetClassRTI(), object, &outcome)) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000711 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100712 if (outcome && can_be_null) {
713 // Type test will succeed, we just need a null test.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100714 HNotEqual* test = new (graph->GetAllocator()) HNotEqual(graph->GetNullConstant(), object);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100715 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
716 instruction->ReplaceWith(test);
717 } else {
718 // We've statically determined the result of the instanceof.
719 instruction->ReplaceWith(graph->GetIntConstant(outcome));
720 }
721 RecordSimplification();
722 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Marko175e7862018-03-27 09:03:13 +0000723 if (outcome && instruction->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) {
724 HLoadClass* load_class = instruction->GetTargetClass();
Alex Lightbb550e42021-04-21 17:04:13 -0700725 if (!load_class->HasUses() && !load_class->NeedsAccessCheck()) {
726 // We cannot rely on DCE to remove the class because the `HLoadClass`
727 // thinks it can throw. However, here we know that it cannot because the
728 // instanceof check was successful and we don't need to check the
729 // access, hence the class was already loaded.
Vladimir Marko175e7862018-03-27 09:03:13 +0000730 load_class->GetBlock()->RemoveInstruction(load_class);
731 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700732 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100733 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100734}
735
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100736void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100737 if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100738 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100739 instruction->ClearValueCanBeNull();
740 }
741}
742
743void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100744 if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100745 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100746 instruction->ClearValueCanBeNull();
747 }
748}
749
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100750static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* allocator, HInstruction* cond) {
Anton Shaminbdd79352016-02-15 12:48:36 +0600751 HInstruction *lhs = cond->InputAt(0);
752 HInstruction *rhs = cond->InputAt(1);
753 switch (cond->GetKind()) {
754 case HInstruction::kEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100755 return new (allocator) HEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600756 case HInstruction::kNotEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100757 return new (allocator) HNotEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600758 case HInstruction::kLessThan:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100759 return new (allocator) HGreaterThan(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600760 case HInstruction::kLessThanOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100761 return new (allocator) HGreaterThanOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600762 case HInstruction::kGreaterThan:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100763 return new (allocator) HLessThan(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600764 case HInstruction::kGreaterThanOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100765 return new (allocator) HLessThanOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600766 case HInstruction::kBelow:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100767 return new (allocator) HAbove(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600768 case HInstruction::kBelowOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100769 return new (allocator) HAboveOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600770 case HInstruction::kAbove:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100771 return new (allocator) HBelow(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600772 case HInstruction::kAboveOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100773 return new (allocator) HBelowOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600774 default:
775 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
Elliott Hughesc1896c92018-11-29 11:33:18 -0800776 UNREACHABLE();
Anton Shaminbdd79352016-02-15 12:48:36 +0600777 }
Anton Shaminbdd79352016-02-15 12:48:36 +0600778}
779
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000780void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100781 HInstruction* input_const = equal->GetConstantRight();
782 if (input_const != nullptr) {
783 HInstruction* input_value = equal->GetLeastConstantLeft();
Nicolas Geoffrayeb104c82019-06-03 17:58:34 +0100784 if ((input_value->GetType() == DataType::Type::kBool) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100785 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100786 // We are comparing the boolean to a constant which is of type int and can
787 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000788 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100789 // Replace (bool_value == true) with bool_value
790 equal->ReplaceWith(input_value);
791 block->RemoveInstruction(equal);
792 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000793 } else if (input_const->AsIntConstant()->IsFalse()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700794 // Replace (bool_value == false) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500795 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
796 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100797 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100798 } else {
799 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
800 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
801 block->RemoveInstruction(equal);
802 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100803 }
Mark Mendellc4701932015-04-10 13:18:51 -0400804 } else {
805 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100806 }
Mark Mendellc4701932015-04-10 13:18:51 -0400807 } else {
808 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100809 }
810}
811
David Brazdil0d13fee2015-04-17 14:52:19 +0100812void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
813 HInstruction* input_const = not_equal->GetConstantRight();
814 if (input_const != nullptr) {
815 HInstruction* input_value = not_equal->GetLeastConstantLeft();
Nicolas Geoffrayeb104c82019-06-03 17:58:34 +0100816 if ((input_value->GetType() == DataType::Type::kBool) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100817 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100818 // We are comparing the boolean to a constant which is of type int and can
819 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000820 if (input_const->AsIntConstant()->IsTrue()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700821 // Replace (bool_value != true) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500822 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
823 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100824 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000825 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100826 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100827 not_equal->ReplaceWith(input_value);
828 block->RemoveInstruction(not_equal);
829 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100830 } else {
831 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
832 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
833 block->RemoveInstruction(not_equal);
834 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100835 }
Mark Mendellc4701932015-04-10 13:18:51 -0400836 } else {
837 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100838 }
Mark Mendellc4701932015-04-10 13:18:51 -0400839 } else {
840 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100841 }
842}
843
844void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000845 HInstruction* input = bool_not->InputAt(0);
846 HInstruction* replace_with = nullptr;
847
848 if (input->IsIntConstant()) {
849 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000850 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000851 replace_with = GetGraph()->GetIntConstant(0);
852 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000853 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000854 replace_with = GetGraph()->GetIntConstant(1);
855 }
856 } else if (input->IsBooleanNot()) {
857 // Replace (!(!bool_value)) with bool_value.
858 replace_with = input->InputAt(0);
859 } else if (input->IsCondition() &&
860 // Don't change FP compares. The definition of compares involving
861 // NaNs forces the compares to be done as written by the user.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100862 !DataType::IsFloatingPointType(input->InputAt(0)->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000863 // Replace condition with its opposite.
864 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
865 }
866
867 if (replace_with != nullptr) {
868 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100869 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000870 RecordSimplification();
871 }
872}
873
Aart Bik4f7dd342017-09-12 13:12:57 -0700874// Constructs a new ABS(x) node in the HIR.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100875static HInstruction* NewIntegralAbs(ArenaAllocator* allocator,
876 HInstruction* x,
877 HInstruction* cursor) {
Aart Bik2286da22018-03-22 10:50:22 -0700878 DataType::Type type = DataType::Kind(x->GetType());
Aart Bik3dad3412018-02-28 12:01:46 -0800879 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Aart Bik142b9132018-03-14 15:12:59 -0700880 HAbs* abs = new (allocator) HAbs(type, x, cursor->GetDexPc());
Aart Bik3dad3412018-02-28 12:01:46 -0800881 cursor->GetBlock()->InsertInstructionBefore(abs, cursor);
882 return abs;
Aart Bik4f7dd342017-09-12 13:12:57 -0700883}
884
Aart Bik142b9132018-03-14 15:12:59 -0700885// Constructs a new MIN/MAX(x, y) node in the HIR.
886static HInstruction* NewIntegralMinMax(ArenaAllocator* allocator,
887 HInstruction* x,
888 HInstruction* y,
889 HInstruction* cursor,
890 bool is_min) {
Aart Bik2286da22018-03-22 10:50:22 -0700891 DataType::Type type = DataType::Kind(x->GetType());
Aart Bik142b9132018-03-14 15:12:59 -0700892 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
893 HBinaryOperation* minmax = nullptr;
894 if (is_min) {
895 minmax = new (allocator) HMin(type, x, y, cursor->GetDexPc());
896 } else {
897 minmax = new (allocator) HMax(type, x, y, cursor->GetDexPc());
898 }
899 cursor->GetBlock()->InsertInstructionBefore(minmax, cursor);
900 return minmax;
901}
902
Aart Bik4f7dd342017-09-12 13:12:57 -0700903// Returns true if operands a and b consists of widening type conversions
904// (either explicit or implicit) to the given to_type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905static bool AreLowerPrecisionArgs(DataType::Type to_type, HInstruction* a, HInstruction* b) {
Aart Bik4f7dd342017-09-12 13:12:57 -0700906 if (a->IsTypeConversion() && a->GetType() == to_type) {
907 a = a->InputAt(0);
908 }
909 if (b->IsTypeConversion() && b->GetType() == to_type) {
910 b = b->InputAt(0);
911 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100912 DataType::Type type1 = a->GetType();
913 DataType::Type type2 = b->GetType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100914 return (type1 == DataType::Type::kUint8 && type2 == DataType::Type::kUint8) ||
915 (type1 == DataType::Type::kInt8 && type2 == DataType::Type::kInt8) ||
916 (type1 == DataType::Type::kInt16 && type2 == DataType::Type::kInt16) ||
917 (type1 == DataType::Type::kUint16 && type2 == DataType::Type::kUint16) ||
918 (type1 == DataType::Type::kInt32 && type2 == DataType::Type::kInt32 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100919 to_type == DataType::Type::kInt64);
Aart Bik4f7dd342017-09-12 13:12:57 -0700920}
921
Aart Bik1d746de2018-03-28 16:30:02 -0700922// Returns an acceptable substitution for "a" on the select
923// construct "a <cmp> b ? c : .." during MIN/MAX recognition.
924static HInstruction* AllowInMinMax(IfCondition cmp,
925 HInstruction* a,
926 HInstruction* b,
927 HInstruction* c) {
928 int64_t value = 0;
929 if (IsInt64AndGet(b, /*out*/ &value) &&
930 (((cmp == kCondLT || cmp == kCondLE) && c->IsMax()) ||
931 ((cmp == kCondGT || cmp == kCondGE) && c->IsMin()))) {
932 HConstant* other = c->AsBinaryOperation()->GetConstantRight();
933 if (other != nullptr && a == c->AsBinaryOperation()->GetLeastConstantLeft()) {
934 int64_t other_value = Int64FromConstant(other);
935 bool is_max = (cmp == kCondLT || cmp == kCondLE);
936 // Allow the max for a < 100 ? max(a, -100) : ..
937 // or the min for a > -100 ? min(a, 100) : ..
938 if (is_max ? (value >= other_value) : (value <= other_value)) {
939 return c;
940 }
941 }
942 }
943 return nullptr;
944}
945
Alex Light3a73ffb2021-01-25 14:11:05 +0000946// TODO This should really be done by LSE itself since there is significantly
947// more information available there.
948void InstructionSimplifierVisitor::VisitPredicatedInstanceFieldGet(
949 HPredicatedInstanceFieldGet* pred_get) {
950 HInstruction* target = pred_get->GetTarget();
951 HInstruction* default_val = pred_get->GetDefaultValue();
952 // TODO Technically we could end up with a case where the target isn't a phi
953 // (allowing us to eliminate the instruction and replace with either a
954 // InstanceFieldGet or the default) but due to the ordering of compilation
955 // passes this can't happen in ART.
956 if (!target->IsPhi() || !default_val->IsPhi() || default_val->GetBlock() != target->GetBlock()) {
957 // Already reduced the target or the phi selection will differ between the
958 // target and default.
959 return;
960 }
961 DCHECK_EQ(default_val->InputCount(), target->InputCount());
962 // In the same block both phis only one non-null we can remove the phi from default_val.
963 HInstruction* single_value = nullptr;
964 auto inputs = target->GetInputs();
965 for (auto [input, idx] : ZipCount(MakeIterationRange(inputs))) {
966 if (input->CanBeNull()) {
967 if (single_value == nullptr) {
968 single_value = default_val->InputAt(idx);
969 } else if (single_value != default_val->InputAt(idx) &&
970 !single_value->Equals(default_val->InputAt(idx))) {
Alex Light74328052021-03-29 18:11:23 -0700971 // Multiple values are associated with potential nulls, can't combine.
Alex Light3a73ffb2021-01-25 14:11:05 +0000972 return;
973 }
974 }
975 }
Alex Light74328052021-03-29 18:11:23 -0700976 if (single_value == nullptr) {
977 // None of the inputs can be null so we can just remove the predicatedness
978 // of this instruction.
979 DCHECK(std::none_of(inputs.cbegin(), inputs.cend(), [](const HInstruction* input) {
980 return input->CanBeNull();
981 }));
982 HInstruction* replace_with = new (GetGraph()->GetAllocator())
983 HInstanceFieldGet(pred_get->GetTarget(),
984 pred_get->GetFieldInfo().GetField(),
985 pred_get->GetFieldType(),
986 pred_get->GetFieldOffset(),
987 pred_get->IsVolatile(),
988 pred_get->GetFieldInfo().GetFieldIndex(),
989 pred_get->GetFieldInfo().GetDeclaringClassDefIndex(),
990 pred_get->GetFieldInfo().GetDexFile(),
991 pred_get->GetDexPc());
992 if (pred_get->GetType() == DataType::Type::kReference) {
993 replace_with->SetReferenceTypeInfo(pred_get->GetReferenceTypeInfo());
994 }
995 pred_get->GetBlock()->InsertInstructionBefore(replace_with, pred_get);
996 pred_get->ReplaceWith(replace_with);
997 pred_get->GetBlock()->RemoveInstruction(pred_get);
998 RecordSimplification();
999 } else if (single_value->StrictlyDominates(pred_get)) {
Alex Light3a73ffb2021-01-25 14:11:05 +00001000 // Combine all the maybe null values into one.
1001 pred_get->ReplaceInput(single_value, 0);
Alex Light74328052021-03-29 18:11:23 -07001002 RecordSimplification();
Alex Light3a73ffb2021-01-25 14:11:05 +00001003 }
1004}
1005
David Brazdil74eb1b22015-12-14 11:44:01 +00001006void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
1007 HInstruction* replace_with = nullptr;
1008 HInstruction* condition = select->GetCondition();
1009 HInstruction* true_value = select->GetTrueValue();
1010 HInstruction* false_value = select->GetFalseValue();
1011
1012 if (condition->IsBooleanNot()) {
1013 // Change ((!cond) ? x : y) to (cond ? y : x).
1014 condition = condition->InputAt(0);
1015 std::swap(true_value, false_value);
1016 select->ReplaceInput(false_value, 0);
1017 select->ReplaceInput(true_value, 1);
1018 select->ReplaceInput(condition, 2);
1019 RecordSimplification();
1020 }
1021
1022 if (true_value == false_value) {
1023 // Replace (cond ? x : x) with (x).
1024 replace_with = true_value;
1025 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001026 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001027 // Replace (true ? x : y) with (x).
1028 replace_with = true_value;
1029 } else {
1030 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +00001031 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +00001032 replace_with = false_value;
1033 }
1034 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001035 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001036 // Replace (cond ? true : false) with (cond).
1037 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +00001038 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001039 // Replace (cond ? false : true) with (!cond).
1040 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
1041 }
Aart Bik4f7dd342017-09-12 13:12:57 -07001042 } else if (condition->IsCondition()) {
1043 IfCondition cmp = condition->AsCondition()->GetCondition();
1044 HInstruction* a = condition->InputAt(0);
1045 HInstruction* b = condition->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001046 DataType::Type t_type = true_value->GetType();
1047 DataType::Type f_type = false_value->GetType();
Aart Bik4f7dd342017-09-12 13:12:57 -07001048 // Here we have a <cmp> b ? true_value : false_value.
Aart Bik1d746de2018-03-28 16:30:02 -07001049 // Test if both values are compatible integral types (resulting MIN/MAX/ABS
1050 // type will be int or long, like the condition). Replacements are general,
1051 // but assume conditions prefer constants on the right.
Aart Bik2286da22018-03-22 10:50:22 -07001052 if (DataType::IsIntegralType(t_type) && DataType::Kind(t_type) == DataType::Kind(f_type)) {
Aart Bik1d746de2018-03-28 16:30:02 -07001053 // Allow a < 100 ? max(a, -100) : ..
1054 // or a > -100 ? min(a, 100) : ..
1055 // to use min/max instead of a to detect nested min/max expressions.
1056 HInstruction* new_a = AllowInMinMax(cmp, a, b, true_value);
1057 if (new_a != nullptr) {
1058 a = new_a;
1059 }
Aart Bik142b9132018-03-14 15:12:59 -07001060 // Try to replace typical integral MIN/MAX/ABS constructs.
1061 if ((cmp == kCondLT || cmp == kCondLE || cmp == kCondGT || cmp == kCondGE) &&
1062 ((a == true_value && b == false_value) ||
1063 (b == true_value && a == false_value))) {
1064 // Found a < b ? a : b (MIN) or a < b ? b : a (MAX)
1065 // or a > b ? a : b (MAX) or a > b ? b : a (MIN).
1066 bool is_min = (cmp == kCondLT || cmp == kCondLE) == (a == true_value);
1067 replace_with = NewIntegralMinMax(GetGraph()->GetAllocator(), a, b, select, is_min);
Aart Bik1d746de2018-03-28 16:30:02 -07001068 } else if (((cmp == kCondLT || cmp == kCondLE) && true_value->IsNeg()) ||
1069 ((cmp == kCondGT || cmp == kCondGE) && false_value->IsNeg())) {
1070 bool negLeft = (cmp == kCondLT || cmp == kCondLE);
1071 HInstruction* the_negated = negLeft ? true_value->InputAt(0) : false_value->InputAt(0);
1072 HInstruction* not_negated = negLeft ? false_value : true_value;
1073 if (a == the_negated && a == not_negated && IsInt64Value(b, 0)) {
1074 // Found a < 0 ? -a : a
1075 // or a > 0 ? a : -a
1076 // which can be replaced by ABS(a).
1077 replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), a, select);
Aart Bik4f7dd342017-09-12 13:12:57 -07001078 }
1079 } else if (true_value->IsSub() && false_value->IsSub()) {
1080 HInstruction* true_sub1 = true_value->InputAt(0);
1081 HInstruction* true_sub2 = true_value->InputAt(1);
1082 HInstruction* false_sub1 = false_value->InputAt(0);
1083 HInstruction* false_sub2 = false_value->InputAt(1);
1084 if ((((cmp == kCondGT || cmp == kCondGE) &&
1085 (a == true_sub1 && b == true_sub2 && a == false_sub2 && b == false_sub1)) ||
1086 ((cmp == kCondLT || cmp == kCondLE) &&
1087 (a == true_sub2 && b == true_sub1 && a == false_sub1 && b == false_sub2))) &&
1088 AreLowerPrecisionArgs(t_type, a, b)) {
Aart Bik1d746de2018-03-28 16:30:02 -07001089 // Found a > b ? a - b : b - a
1090 // or a < b ? b - a : a - b
Aart Bik4f7dd342017-09-12 13:12:57 -07001091 // which can be replaced by ABS(a - b) for lower precision operands a, b.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001092 replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), true_value, select);
Aart Bik4f7dd342017-09-12 13:12:57 -07001093 }
1094 }
1095 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001096 }
1097
1098 if (replace_with != nullptr) {
1099 select->ReplaceWith(replace_with);
1100 select->GetBlock()->RemoveInstruction(select);
1101 RecordSimplification();
1102 }
1103}
1104
1105void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
1106 HInstruction* condition = instruction->InputAt(0);
1107 if (condition->IsBooleanNot()) {
1108 // Swap successors if input is negated.
1109 instruction->ReplaceInput(condition->InputAt(0), 0);
1110 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +01001111 RecordSimplification();
1112 }
1113}
1114
Mingyao Yang0304e182015-01-30 16:41:29 -08001115void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
1116 HInstruction* input = instruction->InputAt(0);
1117 // If the array is a NewArray with constant size, replace the array length
1118 // with the constant instruction. This helps the bounds check elimination phase.
1119 if (input->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001120 input = input->AsNewArray()->GetLength();
Mingyao Yang0304e182015-01-30 16:41:29 -08001121 if (input->IsIntConstant()) {
1122 instruction->ReplaceWith(input);
1123 }
1124 }
1125}
1126
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +00001127void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001128 HInstruction* value = instruction->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001129 if (value->GetType() != DataType::Type::kReference) {
1130 return;
1131 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001132
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001133 if (CanEnsureNotNullAt(value, instruction)) {
1134 instruction->ClearValueCanBeNull();
1135 }
1136
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001137 if (value->IsArrayGet()) {
1138 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
1139 // If the code is just swapping elements in the array, no need for a type check.
1140 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001141 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001142 }
1143 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001144
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +01001145 if (value->IsNullConstant()) {
1146 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001147 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +01001148 }
1149
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001150 ScopedObjectAccess soa(Thread::Current());
1151 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
1152 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
1153 if (!array_rti.IsValid()) {
1154 return;
1155 }
1156
1157 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
1158 instruction->ClearNeedsTypeCheck();
1159 return;
1160 }
1161
1162 if (array_rti.IsObjectArray()) {
1163 if (array_rti.IsExact()) {
1164 instruction->ClearNeedsTypeCheck();
1165 return;
1166 }
1167 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001168 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001169}
1170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001171static bool IsTypeConversionLossless(DataType::Type input_type, DataType::Type result_type) {
Aart Bikdab69072017-10-23 13:30:39 -07001172 // Make sure all implicit conversions have been simplified and no new ones have been introduced.
1173 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
1174 << input_type << "," << result_type;
Vladimir Markob52bbde2016-02-12 12:06:05 +00001175 // The conversion to a larger type is loss-less with the exception of two cases,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001176 // - conversion to the unsigned type Uint16, where we may lose some bits, and
Vladimir Markob52bbde2016-02-12 12:06:05 +00001177 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
1178 // For integral to FP conversions this holds because the FP mantissa is large enough.
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001179 // Note: The size check excludes Uint8 as the result type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001180 return DataType::Size(result_type) > DataType::Size(input_type) &&
1181 result_type != DataType::Type::kUint16 &&
1182 !(result_type == DataType::Type::kInt64 && input_type == DataType::Type::kFloat32);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001183}
1184
Vladimir Marko61b92282017-10-11 13:23:17 +01001185static inline bool TryReplaceFieldOrArrayGetType(HInstruction* maybe_get, DataType::Type new_type) {
1186 if (maybe_get->IsInstanceFieldGet()) {
1187 maybe_get->AsInstanceFieldGet()->SetType(new_type);
1188 return true;
Alex Light3a73ffb2021-01-25 14:11:05 +00001189 } else if (maybe_get->IsPredicatedInstanceFieldGet()) {
1190 maybe_get->AsPredicatedInstanceFieldGet()->SetType(new_type);
1191 return true;
Vladimir Marko61b92282017-10-11 13:23:17 +01001192 } else if (maybe_get->IsStaticFieldGet()) {
1193 maybe_get->AsStaticFieldGet()->SetType(new_type);
1194 return true;
1195 } else if (maybe_get->IsArrayGet() && !maybe_get->AsArrayGet()->IsStringCharAt()) {
1196 maybe_get->AsArrayGet()->SetType(new_type);
1197 return true;
1198 } else {
1199 return false;
1200 }
1201}
1202
Mingyao Yang3bcb7512017-11-16 15:40:46 -08001203// The type conversion is only used for storing into a field/element of the
1204// same/narrower size.
1205static bool IsTypeConversionForStoringIntoNoWiderFieldOnly(HTypeConversion* type_conversion) {
1206 if (type_conversion->HasEnvironmentUses()) {
1207 return false;
1208 }
1209 DataType::Type input_type = type_conversion->GetInputType();
1210 DataType::Type result_type = type_conversion->GetResultType();
1211 if (!DataType::IsIntegralType(input_type) ||
1212 !DataType::IsIntegralType(result_type) ||
1213 input_type == DataType::Type::kInt64 ||
1214 result_type == DataType::Type::kInt64) {
1215 // Type conversion is needed if non-integer types are involved, or 64-bit
1216 // types are involved, which may use different number of registers.
1217 return false;
1218 }
1219 if (DataType::Size(input_type) >= DataType::Size(result_type)) {
1220 // Type conversion is not necessary when storing to a field/element of the
1221 // same/smaller size.
1222 } else {
1223 // We do not handle this case here.
1224 return false;
1225 }
1226
1227 // Check if the converted value is only used for storing into heap.
1228 for (const HUseListNode<HInstruction*>& use : type_conversion->GetUses()) {
1229 HInstruction* instruction = use.GetUser();
1230 if (instruction->IsInstanceFieldSet() &&
1231 instruction->AsInstanceFieldSet()->GetFieldType() == result_type) {
1232 DCHECK_EQ(instruction->AsInstanceFieldSet()->GetValue(), type_conversion);
1233 continue;
1234 }
1235 if (instruction->IsStaticFieldSet() &&
1236 instruction->AsStaticFieldSet()->GetFieldType() == result_type) {
1237 DCHECK_EQ(instruction->AsStaticFieldSet()->GetValue(), type_conversion);
1238 continue;
1239 }
1240 if (instruction->IsArraySet() &&
1241 instruction->AsArraySet()->GetComponentType() == result_type &&
1242 // not index use.
1243 instruction->AsArraySet()->GetIndex() != type_conversion) {
1244 DCHECK_EQ(instruction->AsArraySet()->GetValue(), type_conversion);
1245 continue;
1246 }
1247 // The use is not as a store value, or the field/element type is not the
1248 // same as the result_type, keep the type conversion.
1249 return false;
1250 }
1251 // Codegen automatically handles the type conversion during the store.
1252 return true;
1253}
1254
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001255void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001256 HInstruction* input = instruction->GetInput();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001257 DataType::Type input_type = input->GetType();
1258 DataType::Type result_type = instruction->GetResultType();
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01001259 if (instruction->IsImplicitConversion()) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001260 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001261 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001262 RecordSimplification();
1263 return;
1264 }
1265
1266 if (input->IsTypeConversion()) {
1267 HTypeConversion* input_conversion = input->AsTypeConversion();
1268 HInstruction* original_input = input_conversion->GetInput();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 DataType::Type original_type = original_input->GetType();
Vladimir Markob52bbde2016-02-12 12:06:05 +00001270
1271 // When the first conversion is lossless, a direct conversion from the original type
1272 // to the final type yields the same result, even for a lossy second conversion, for
1273 // example float->double->int or int->double->float.
1274 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
1275
1276 // For integral conversions, see if the first conversion loses only bits that the second
1277 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
1278 // conversion yields the same result, for example long->int->short or int->char->short.
1279 bool integral_conversions_with_non_widening_second =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001280 DataType::IsIntegralType(input_type) &&
1281 DataType::IsIntegralType(original_type) &&
1282 DataType::IsIntegralType(result_type) &&
1283 DataType::Size(result_type) <= DataType::Size(input_type);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001284
1285 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
1286 // If the merged conversion is implicit, do the simplification unconditionally.
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001287 if (DataType::IsTypeConversionImplicit(original_type, result_type)) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001288 instruction->ReplaceWith(original_input);
1289 instruction->GetBlock()->RemoveInstruction(instruction);
1290 if (!input_conversion->HasUses()) {
1291 // Don't wait for DCE.
1292 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1293 }
1294 RecordSimplification();
1295 return;
1296 }
1297 // Otherwise simplify only if the first conversion has no other use.
1298 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
1299 input_conversion->ReplaceWith(original_input);
1300 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1301 RecordSimplification();
1302 return;
1303 }
1304 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001305 } else if (input->IsAnd() && DataType::IsIntegralType(result_type)) {
1306 DCHECK(DataType::IsIntegralType(input_type));
Vladimir Marko8428bd32016-02-12 16:53:57 +00001307 HAnd* input_and = input->AsAnd();
1308 HConstant* constant = input_and->GetConstantRight();
1309 if (constant != nullptr) {
1310 int64_t value = Int64FromConstant(constant);
1311 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
1312 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001313 if (trailing_ones >= kBitsPerByte * DataType::Size(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +00001314 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +00001315 HInstruction* original_input = input_and->GetLeastConstantLeft();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001316 if (DataType::IsTypeConversionImplicit(original_input->GetType(), result_type)) {
Vladimir Marko625090f2016-03-14 18:00:05 +00001317 instruction->ReplaceWith(original_input);
1318 instruction->GetBlock()->RemoveInstruction(instruction);
1319 RecordSimplification();
1320 return;
1321 } else if (input->HasOnlyOneNonEnvironmentUse()) {
1322 input_and->ReplaceWith(original_input);
1323 input_and->GetBlock()->RemoveInstruction(input_and);
1324 RecordSimplification();
1325 return;
1326 }
Vladimir Marko8428bd32016-02-12 16:53:57 +00001327 }
1328 }
Vladimir Marko61b92282017-10-11 13:23:17 +01001329 } else if (input->HasOnlyOneNonEnvironmentUse() &&
1330 ((input_type == DataType::Type::kInt8 && result_type == DataType::Type::kUint8) ||
1331 (input_type == DataType::Type::kUint8 && result_type == DataType::Type::kInt8) ||
1332 (input_type == DataType::Type::kInt16 && result_type == DataType::Type::kUint16) ||
1333 (input_type == DataType::Type::kUint16 && result_type == DataType::Type::kInt16))) {
1334 // Try to modify the type of the load to `result_type` and remove the explicit type conversion.
1335 if (TryReplaceFieldOrArrayGetType(input, result_type)) {
1336 instruction->ReplaceWith(input);
1337 instruction->GetBlock()->RemoveInstruction(instruction);
1338 RecordSimplification();
1339 return;
1340 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001341 }
Mingyao Yang3bcb7512017-11-16 15:40:46 -08001342
1343 if (IsTypeConversionForStoringIntoNoWiderFieldOnly(instruction)) {
1344 instruction->ReplaceWith(input);
1345 instruction->GetBlock()->RemoveInstruction(instruction);
1346 RecordSimplification();
1347 return;
1348 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001349}
1350
Aart Bikc6eec4b2018-03-29 17:22:00 -07001351void InstructionSimplifierVisitor::VisitAbs(HAbs* instruction) {
1352 HInstruction* input = instruction->GetInput();
1353 if (DataType::IsZeroExtension(input->GetType(), instruction->GetResultType())) {
1354 // Zero extension from narrow to wide can never set sign bit in the wider
1355 // operand, making the subsequent Abs redundant (e.g., abs(b & 0xff) for byte b).
1356 instruction->ReplaceWith(input);
1357 instruction->GetBlock()->RemoveInstruction(instruction);
1358 RecordSimplification();
1359 }
1360}
1361
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001362void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
1363 HConstant* input_cst = instruction->GetConstantRight();
1364 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001365 bool integral_type = DataType::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +00001366 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001367 // Replace code looking like
1368 // ADD dst, src, 0
1369 // with
1370 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001371 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
1372 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1373 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001374 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +06001375 instruction->ReplaceWith(input_other);
1376 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001377 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +06001378 return;
1379 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001380 }
1381
1382 HInstruction* left = instruction->GetLeft();
1383 HInstruction* right = instruction->GetRight();
1384 bool left_is_neg = left->IsNeg();
1385 bool right_is_neg = right->IsNeg();
1386
1387 if (left_is_neg && right_is_neg) {
1388 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1389 return;
1390 }
1391 }
1392
1393 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
Andreas Gampe654698d2018-09-20 13:34:35 -07001394 if (left_is_neg != right_is_neg && neg->HasOnlyOneNonEnvironmentUse()) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001395 // Replace code looking like
1396 // NEG tmp, b
1397 // ADD dst, a, tmp
1398 // with
1399 // SUB dst, a, b
1400 // We do not perform the optimization if the input negation has environment
1401 // uses or multiple non-environment uses as it could lead to worse code. In
1402 // particular, we do not want the live range of `b` to be extended if we are
1403 // not sure the initial 'NEG' instruction can be removed.
1404 HInstruction* other = left_is_neg ? right : left;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001405 HSub* sub =
1406 new(GetGraph()->GetAllocator()) HSub(instruction->GetType(), other, neg->GetInput());
Alexandre Rames188d4312015-04-09 18:30:21 +01001407 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
1408 RecordSimplification();
1409 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001410 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001411 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001412
Anton Kirilove14dc862016-05-13 17:56:15 +01001413 if (TryReplaceWithRotate(instruction)) {
1414 return;
1415 }
1416
1417 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1418 // so no need to return.
1419 TryHandleAssociativeAndCommutativeOperation(instruction);
1420
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001421 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +01001422 TrySubtractionChainSimplification(instruction)) {
1423 return;
1424 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001425
1426 if (integral_type) {
1427 // Replace code patterns looking like
1428 // SUB dst1, x, y SUB dst1, x, y
1429 // ADD dst2, dst1, y ADD dst2, y, dst1
1430 // with
1431 // SUB dst1, x, y
1432 // ADD instruction is not needed in this case, we may use
1433 // one of inputs of SUB instead.
1434 if (left->IsSub() && left->InputAt(1) == right) {
1435 instruction->ReplaceWith(left->InputAt(0));
1436 RecordSimplification();
1437 instruction->GetBlock()->RemoveInstruction(instruction);
1438 return;
1439 } else if (right->IsSub() && right->InputAt(1) == left) {
1440 instruction->ReplaceWith(right->InputAt(0));
1441 RecordSimplification();
1442 instruction->GetBlock()->RemoveInstruction(instruction);
1443 return;
1444 }
1445 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001446}
1447
1448void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
Vladimir Marko61b92282017-10-11 13:23:17 +01001449 DCHECK(DataType::IsIntegralType(instruction->GetType()));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001450 HConstant* input_cst = instruction->GetConstantRight();
1451 HInstruction* input_other = instruction->GetLeastConstantLeft();
1452
Vladimir Marko452c1b62015-09-25 14:44:17 +01001453 if (input_cst != nullptr) {
1454 int64_t value = Int64FromConstant(input_cst);
Aart Bikdab69072017-10-23 13:30:39 -07001455 if (value == -1 ||
1456 // Similar cases under zero extension.
1457 (DataType::IsUnsignedType(input_other->GetType()) &&
1458 ((DataType::MaxValueOfIntegralType(input_other->GetType()) & ~value) == 0))) {
Vladimir Marko452c1b62015-09-25 14:44:17 +01001459 // Replace code looking like
1460 // AND dst, src, 0xFFF...FF
1461 // with
1462 // src
1463 instruction->ReplaceWith(input_other);
1464 instruction->GetBlock()->RemoveInstruction(instruction);
1465 RecordSimplification();
1466 return;
1467 }
Vladimir Markoc8fb2112017-10-03 11:37:52 +01001468 if (input_other->IsTypeConversion() &&
1469 input_other->GetType() == DataType::Type::kInt64 &&
1470 DataType::IsIntegralType(input_other->InputAt(0)->GetType()) &&
1471 IsInt<32>(value) &&
1472 input_other->HasOnlyOneNonEnvironmentUse()) {
1473 // The AND can be reordered before the TypeConversion. Replace
1474 // LongConstant cst, <32-bit-constant-sign-extended-to-64-bits>
1475 // TypeConversion<Int64> tmp, src
1476 // AND dst, tmp, cst
1477 // with
1478 // IntConstant cst, <32-bit-constant>
1479 // AND tmp, src, cst
1480 // TypeConversion<Int64> dst, tmp
1481 // This helps 32-bit targets and does not hurt 64-bit targets.
1482 // This also simplifies detection of other patterns, such as Uint8 loads.
1483 HInstruction* new_and_input = input_other->InputAt(0);
1484 // Implicit conversion Int64->Int64 would have been removed previously.
1485 DCHECK_NE(new_and_input->GetType(), DataType::Type::kInt64);
1486 HConstant* new_const = GetGraph()->GetConstant(DataType::Type::kInt32, value);
1487 HAnd* new_and =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001488 new (GetGraph()->GetAllocator()) HAnd(DataType::Type::kInt32, new_and_input, new_const);
Vladimir Markoc8fb2112017-10-03 11:37:52 +01001489 instruction->GetBlock()->InsertInstructionBefore(new_and, instruction);
1490 HTypeConversion* new_conversion =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001491 new (GetGraph()->GetAllocator()) HTypeConversion(DataType::Type::kInt64, new_and);
Vladimir Markoc8fb2112017-10-03 11:37:52 +01001492 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_conversion);
1493 input_other->GetBlock()->RemoveInstruction(input_other);
1494 RecordSimplification();
1495 // Try to process the new And now, do not wait for the next round of simplifications.
1496 instruction = new_and;
1497 input_other = new_and_input;
1498 }
Vladimir Marko452c1b62015-09-25 14:44:17 +01001499 // Eliminate And from UShr+And if the And-mask contains all the bits that
1500 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1501 // precisely clears the shifted-in sign bits.
1502 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001503 size_t reg_bits = (instruction->GetResultType() == DataType::Type::kInt64) ? 64 : 32;
Vladimir Marko452c1b62015-09-25 14:44:17 +01001504 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1505 size_t num_tail_bits_set = CTZ(value + 1);
1506 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1507 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1508 instruction->ReplaceWith(input_other);
1509 instruction->GetBlock()->RemoveInstruction(instruction);
1510 RecordSimplification();
1511 return;
1512 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1513 input_other->HasOnlyOneNonEnvironmentUse()) {
1514 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1515 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
Vladimir Markoca6fff82017-10-03 14:49:14 +01001516 HUShr* ushr = new (GetGraph()->GetAllocator()) HUShr(instruction->GetType(),
Vladimir Marko69d310e2017-10-09 14:12:23 +01001517 input_other->InputAt(0),
1518 input_other->InputAt(1),
1519 input_other->GetDexPc());
Vladimir Marko452c1b62015-09-25 14:44:17 +01001520 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1521 input_other->GetBlock()->RemoveInstruction(input_other);
1522 RecordSimplification();
1523 return;
1524 }
1525 }
Vladimir Marko61b92282017-10-11 13:23:17 +01001526 if ((value == 0xff || value == 0xffff) && instruction->GetType() != DataType::Type::kInt64) {
1527 // Transform AND to a type conversion to Uint8/Uint16. If `input_other` is a field
1528 // or array Get with only a single use, short-circuit the subsequent simplification
1529 // of the Get+TypeConversion and change the Get's type to `new_type` instead.
1530 DataType::Type new_type = (value == 0xff) ? DataType::Type::kUint8 : DataType::Type::kUint16;
1531 DataType::Type find_type = (value == 0xff) ? DataType::Type::kInt8 : DataType::Type::kInt16;
1532 if (input_other->GetType() == find_type &&
1533 input_other->HasOnlyOneNonEnvironmentUse() &&
1534 TryReplaceFieldOrArrayGetType(input_other, new_type)) {
1535 instruction->ReplaceWith(input_other);
1536 instruction->GetBlock()->RemoveInstruction(instruction);
Aart Bikdab69072017-10-23 13:30:39 -07001537 } else if (DataType::IsTypeConversionImplicit(input_other->GetType(), new_type)) {
1538 instruction->ReplaceWith(input_other);
1539 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Marko61b92282017-10-11 13:23:17 +01001540 } else {
1541 HTypeConversion* type_conversion = new (GetGraph()->GetAllocator()) HTypeConversion(
1542 new_type, input_other, instruction->GetDexPc());
1543 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, type_conversion);
1544 }
1545 RecordSimplification();
1546 return;
1547 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001548 }
1549
1550 // We assume that GVN has run before, so we only perform a pointer comparison.
1551 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001552 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001553 if (instruction->GetLeft() == instruction->GetRight()) {
1554 // Replace code looking like
1555 // AND dst, src, src
1556 // with
1557 // src
1558 instruction->ReplaceWith(instruction->GetLeft());
1559 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001560 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001561 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001562 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001563
Anton Kirilove14dc862016-05-13 17:56:15 +01001564 if (TryDeMorganNegationFactoring(instruction)) {
1565 return;
1566 }
1567
1568 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1569 // so no need to return.
1570 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001571}
1572
Mark Mendellc4701932015-04-10 13:18:51 -04001573void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1574 VisitCondition(condition);
1575}
1576
1577void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1578 VisitCondition(condition);
1579}
1580
1581void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1582 VisitCondition(condition);
1583}
1584
1585void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1586 VisitCondition(condition);
1587}
1588
Anton Shaminbdd79352016-02-15 12:48:36 +06001589void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1590 VisitCondition(condition);
1591}
1592
1593void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1594 VisitCondition(condition);
1595}
1596
1597void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1598 VisitCondition(condition);
1599}
1600
1601void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1602 VisitCondition(condition);
1603}
Aart Bike9f37602015-10-09 11:15:55 -07001604
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001605// Recognize the following pattern:
1606// obj.getClass() ==/!= Foo.class
1607// And replace it with a constant value if the type of `obj` is statically known.
1608static bool RecognizeAndSimplifyClassCheck(HCondition* condition) {
1609 HInstruction* input_one = condition->InputAt(0);
1610 HInstruction* input_two = condition->InputAt(1);
1611 HLoadClass* load_class = input_one->IsLoadClass()
1612 ? input_one->AsLoadClass()
1613 : input_two->AsLoadClass();
1614 if (load_class == nullptr) {
1615 return false;
1616 }
1617
1618 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
1619 if (!class_rti.IsValid()) {
1620 // Unresolved class.
1621 return false;
1622 }
1623
1624 HInstanceFieldGet* field_get = (load_class == input_one)
1625 ? input_two->AsInstanceFieldGet()
1626 : input_one->AsInstanceFieldGet();
1627 if (field_get == nullptr) {
1628 return false;
1629 }
1630
1631 HInstruction* receiver = field_get->InputAt(0);
1632 ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
1633 if (!receiver_type.IsExact()) {
1634 return false;
1635 }
1636
1637 {
1638 ScopedObjectAccess soa(Thread::Current());
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001639 ArtField* field = GetClassRoot<mirror::Object>()->GetInstanceField(0);
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001640 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
1641 if (field_get->GetFieldInfo().GetField() != field) {
1642 return false;
1643 }
1644
1645 // We can replace the compare.
1646 int value = 0;
1647 if (receiver_type.IsEqual(class_rti)) {
1648 value = condition->IsEqual() ? 1 : 0;
1649 } else {
1650 value = condition->IsNotEqual() ? 1 : 0;
1651 }
1652 condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1653 return true;
1654 }
1655}
1656
Mark Mendellc4701932015-04-10 13:18:51 -04001657void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001658 if (condition->IsEqual() || condition->IsNotEqual()) {
1659 if (RecognizeAndSimplifyClassCheck(condition)) {
1660 return;
1661 }
1662 }
1663
Anton Shaminbdd79352016-02-15 12:48:36 +06001664 // Reverse condition if left is constant. Our code generators prefer constant
1665 // on the right hand side.
1666 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1667 HBasicBlock* block = condition->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001668 HCondition* replacement =
1669 GetOppositeConditionSwapOps(block->GetGraph()->GetAllocator(), condition);
Anton Shaminbdd79352016-02-15 12:48:36 +06001670 // If it is a fp we must set the opposite bias.
1671 if (replacement != nullptr) {
1672 if (condition->IsLtBias()) {
1673 replacement->SetBias(ComparisonBias::kGtBias);
1674 } else if (condition->IsGtBias()) {
1675 replacement->SetBias(ComparisonBias::kLtBias);
1676 }
1677 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1678 RecordSimplification();
1679
1680 condition = replacement;
1681 }
1682 }
Mark Mendellc4701932015-04-10 13:18:51 -04001683
Mark Mendellc4701932015-04-10 13:18:51 -04001684 HInstruction* left = condition->GetLeft();
1685 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001686
1687 // Try to fold an HCompare into this HCondition.
1688
Mark Mendellc4701932015-04-10 13:18:51 -04001689 // We can only replace an HCondition which compares a Compare to 0.
1690 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1691 // condition with a long, float or double comparison as input.
1692 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1693 // Conversion is not possible.
1694 return;
1695 }
1696
1697 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001698 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001699 // Someone else also wants the result of the compare.
1700 return;
1701 }
1702
Vladimir Marko46817b82016-03-29 12:21:58 +01001703 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001704 // There is a reference to the compare result in an environment. Do we really need it?
1705 if (GetGraph()->IsDebuggable()) {
1706 return;
1707 }
1708
1709 // We have to ensure that there are no deopt points in the sequence.
1710 if (left->HasAnyEnvironmentUseBefore(condition)) {
1711 return;
1712 }
1713 }
1714
1715 // Clean up any environment uses from the HCompare, if any.
1716 left->RemoveEnvironmentUsers();
1717
1718 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1719 condition->SetBias(left->AsCompare()->GetBias());
1720
1721 // Replace the operands of the HCondition.
1722 condition->ReplaceInput(left->InputAt(0), 0);
1723 condition->ReplaceInput(left->InputAt(1), 1);
1724
1725 // Remove the HCompare.
1726 left->GetBlock()->RemoveInstruction(left);
1727
1728 RecordSimplification();
1729}
1730
Andreas Gampe9186ced2016-12-12 14:28:21 -08001731// Return whether x / divisor == x * (1.0f / divisor), for every float x.
1732static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
1733 // True, if the most significant bits of divisor are 0.
1734 return ((divisor & 0x7fffff) == 0);
1735}
1736
1737// Return whether x / divisor == x * (1.0 / divisor), for every double x.
1738static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
1739 // True, if the most significant bits of divisor are 0.
1740 return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
1741}
1742
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001743void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1744 HConstant* input_cst = instruction->GetConstantRight();
1745 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001747
1748 if ((input_cst != nullptr) && input_cst->IsOne()) {
1749 // Replace code looking like
1750 // DIV dst, src, 1
1751 // with
1752 // src
1753 instruction->ReplaceWith(input_other);
1754 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001755 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001756 return;
1757 }
1758
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001759 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001760 // Replace code looking like
1761 // DIV dst, src, -1
1762 // with
1763 // NEG dst, src
1764 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001765 instruction, new (GetGraph()->GetAllocator()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001766 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001767 return;
1768 }
1769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001770 if ((input_cst != nullptr) && DataType::IsFloatingPointType(type)) {
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001771 // Try replacing code looking like
1772 // DIV dst, src, constant
1773 // with
1774 // MUL dst, src, 1 / constant
1775 HConstant* reciprocal = nullptr;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001776 if (type == DataType::Type::kFloat64) {
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001777 double value = input_cst->AsDoubleConstant()->GetValue();
1778 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1779 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1780 }
1781 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001782 DCHECK_EQ(type, DataType::Type::kFloat32);
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001783 float value = input_cst->AsFloatConstant()->GetValue();
1784 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1785 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1786 }
1787 }
1788
1789 if (reciprocal != nullptr) {
1790 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001791 instruction, new (GetGraph()->GetAllocator()) HMul(type, input_other, reciprocal));
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001792 RecordSimplification();
1793 return;
1794 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001795 }
1796}
1797
Evgeny Astigeevich6587d912020-06-12 10:51:43 +01001798
1799// Search HDiv having the specified dividend and divisor which is in the specified basic block.
1800// Return nullptr if nothing has been found.
1801static HInstruction* FindDivWithInputsInBasicBlock(HInstruction* dividend,
1802 HInstruction* divisor,
1803 HBasicBlock* basic_block) {
1804 for (const HUseListNode<HInstruction*>& use : dividend->GetUses()) {
1805 HInstruction* user = use.GetUser();
1806 if (user->GetBlock() == basic_block && user->IsDiv() && user->InputAt(1) == divisor) {
1807 return user;
1808 }
1809 }
1810 return nullptr;
1811}
1812
1813// If there is Div with the same inputs as Rem and in the same basic block, it can be reused.
1814// Rem is replaced with Mul+Sub which use the found Div.
1815void InstructionSimplifierVisitor::TryToReuseDiv(HRem* rem) {
1816 // As the optimization replaces Rem with Mul+Sub they prevent some loop optimizations
1817 // if the Rem is in a loop.
1818 // Check if it is allowed to optimize such Rems.
1819 if (rem->IsInLoop() && be_loop_friendly_) {
1820 return;
1821 }
1822 DataType::Type type = rem->GetResultType();
1823 if (!DataType::IsIntOrLongType(type)) {
1824 return;
1825 }
1826
1827 HBasicBlock* basic_block = rem->GetBlock();
1828 HInstruction* dividend = rem->GetLeft();
1829 HInstruction* divisor = rem->GetRight();
1830
1831 if (divisor->IsConstant()) {
1832 HConstant* input_cst = rem->GetConstantRight();
1833 DCHECK(input_cst->IsIntConstant() || input_cst->IsLongConstant());
1834 int64_t cst_value = Int64FromConstant(input_cst);
1835 if (cst_value == std::numeric_limits<int64_t>::min() || IsPowerOfTwo(std::abs(cst_value))) {
1836 // Such cases are usually handled in the code generator because they don't need Div at all.
1837 return;
1838 }
1839 }
1840
1841 HInstruction* quotient = FindDivWithInputsInBasicBlock(dividend, divisor, basic_block);
1842 if (quotient == nullptr) {
1843 return;
1844 }
1845 if (!quotient->StrictlyDominates(rem)) {
1846 quotient->MoveBefore(rem);
1847 }
1848
1849 ArenaAllocator* allocator = GetGraph()->GetAllocator();
1850 HInstruction* mul = new (allocator) HMul(type, quotient, divisor);
1851 basic_block->InsertInstructionBefore(mul, rem);
1852 HInstruction* sub = new (allocator) HSub(type, dividend, mul);
1853 basic_block->InsertInstructionBefore(sub, rem);
1854 rem->ReplaceWith(sub);
1855 basic_block->RemoveInstruction(rem);
1856 RecordSimplification();
1857}
1858
1859void InstructionSimplifierVisitor::VisitRem(HRem* rem) {
1860 TryToReuseDiv(rem);
1861}
1862
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001863void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1864 HConstant* input_cst = instruction->GetConstantRight();
1865 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001866 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001867 HBasicBlock* block = instruction->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 ArenaAllocator* allocator = GetGraph()->GetAllocator();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001869
1870 if (input_cst == nullptr) {
1871 return;
1872 }
1873
1874 if (input_cst->IsOne()) {
1875 // Replace code looking like
1876 // MUL dst, src, 1
1877 // with
1878 // src
1879 instruction->ReplaceWith(input_other);
1880 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001881 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001882 return;
1883 }
1884
1885 if (input_cst->IsMinusOne() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 (DataType::IsFloatingPointType(type) || DataType::IsIntOrLongType(type))) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001887 // Replace code looking like
1888 // MUL dst, src, -1
1889 // with
1890 // NEG dst, src
1891 HNeg* neg = new (allocator) HNeg(type, input_other);
1892 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001893 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001894 return;
1895 }
1896
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001897 if (DataType::IsFloatingPointType(type) &&
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001898 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1899 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1900 // Replace code looking like
1901 // FP_MUL dst, src, 2.0
1902 // with
1903 // FP_ADD dst, src, src
1904 // The 'int' and 'long' cases are handled below.
1905 block->ReplaceAndRemoveInstructionWith(instruction,
1906 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001907 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001908 return;
1909 }
1910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001911 if (DataType::IsIntOrLongType(type)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001912 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001913 // Even though constant propagation also takes care of the zero case, other
1914 // optimizations can lead to having a zero multiplication.
1915 if (factor == 0) {
1916 // Replace code looking like
1917 // MUL dst, src, 0
1918 // with
1919 // 0
1920 instruction->ReplaceWith(input_cst);
1921 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001922 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001923 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001924 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001925 // Replace code looking like
1926 // MUL dst, src, pow_of_2
1927 // with
1928 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001929 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001930 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001931 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001932 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001933 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001934 } else if (IsPowerOfTwo(factor - 1)) {
1935 // Transform code looking like
1936 // MUL dst, src, (2^n + 1)
1937 // into
1938 // SHL tmp, src, n
1939 // ADD dst, src, tmp
1940 HShl* shl = new (allocator) HShl(type,
1941 input_other,
1942 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1943 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1944
1945 block->InsertInstructionBefore(shl, instruction);
1946 block->ReplaceAndRemoveInstructionWith(instruction, add);
1947 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001948 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001949 } else if (IsPowerOfTwo(factor + 1)) {
1950 // Transform code looking like
1951 // MUL dst, src, (2^n - 1)
1952 // into
1953 // SHL tmp, src, n
1954 // SUB dst, tmp, src
1955 HShl* shl = new (allocator) HShl(type,
1956 input_other,
1957 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1958 HSub* sub = new (allocator) HSub(type, shl, input_other);
1959
1960 block->InsertInstructionBefore(shl, instruction);
1961 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1962 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001963 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001964 }
1965 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001966
1967 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1968 // so no need to return.
1969 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001970}
1971
Alexandre Rames188d4312015-04-09 18:30:21 +01001972void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1973 HInstruction* input = instruction->GetInput();
1974 if (input->IsNeg()) {
1975 // Replace code looking like
1976 // NEG tmp, src
1977 // NEG dst, tmp
1978 // with
1979 // src
1980 HNeg* previous_neg = input->AsNeg();
1981 instruction->ReplaceWith(previous_neg->GetInput());
1982 instruction->GetBlock()->RemoveInstruction(instruction);
1983 // We perform the optimization even if the input negation has environment
1984 // uses since it allows removing the current instruction. But we only delete
1985 // the input negation only if it is does not have any uses left.
1986 if (!previous_neg->HasUses()) {
1987 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1988 }
1989 RecordSimplification();
1990 return;
1991 }
1992
Serguei Katkov339dfc22015-04-20 12:29:32 +06001993 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001994 !DataType::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001995 // Replace code looking like
1996 // SUB tmp, a, b
1997 // NEG dst, tmp
1998 // with
1999 // SUB dst, b, a
2000 // We do not perform the optimization if the input subtraction has
2001 // environment uses or multiple non-environment uses as it could lead to
2002 // worse code. In particular, we do not want the live ranges of `a` and `b`
2003 // to be extended if we are not sure the initial 'SUB' instruction can be
2004 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06002005 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01002006 HSub* sub = input->AsSub();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002007 HSub* new_sub = new (GetGraph()->GetAllocator()) HSub(
2008 instruction->GetType(), sub->GetRight(), sub->GetLeft());
Alexandre Rames188d4312015-04-09 18:30:21 +01002009 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
2010 if (!sub->HasUses()) {
2011 sub->GetBlock()->RemoveInstruction(sub);
2012 }
2013 RecordSimplification();
2014 }
2015}
2016
2017void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
2018 HInstruction* input = instruction->GetInput();
2019 if (input->IsNot()) {
2020 // Replace code looking like
2021 // NOT tmp, src
2022 // NOT dst, tmp
2023 // with
2024 // src
2025 // We perform the optimization even if the input negation has environment
2026 // uses since it allows removing the current instruction. But we only delete
2027 // the input negation only if it is does not have any uses left.
2028 HNot* previous_not = input->AsNot();
2029 instruction->ReplaceWith(previous_not->GetInput());
2030 instruction->GetBlock()->RemoveInstruction(instruction);
2031 if (!previous_not->HasUses()) {
2032 previous_not->GetBlock()->RemoveInstruction(previous_not);
2033 }
2034 RecordSimplification();
2035 }
2036}
2037
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002038void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
2039 HConstant* input_cst = instruction->GetConstantRight();
2040 HInstruction* input_other = instruction->GetLeastConstantLeft();
2041
Roland Levillain1a653882016-03-18 18:05:57 +00002042 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002043 // Replace code looking like
2044 // OR dst, src, 0
2045 // with
2046 // src
2047 instruction->ReplaceWith(input_other);
2048 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01002049 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002050 return;
2051 }
2052
2053 // We assume that GVN has run before, so we only perform a pointer comparison.
2054 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01002055 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002056 if (instruction->GetLeft() == instruction->GetRight()) {
2057 // Replace code looking like
2058 // OR dst, src, src
2059 // with
2060 // src
2061 instruction->ReplaceWith(instruction->GetLeft());
2062 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01002063 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002064 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002065 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002066
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00002067 if (TryDeMorganNegationFactoring(instruction)) return;
2068
Anton Kirilove14dc862016-05-13 17:56:15 +01002069 if (TryReplaceWithRotate(instruction)) {
2070 return;
2071 }
2072
2073 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2074 // so no need to return.
2075 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002076}
2077
2078void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
2079 VisitShift(instruction);
2080}
2081
2082void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
2083 VisitShift(instruction);
2084}
2085
2086void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
2087 HConstant* input_cst = instruction->GetConstantRight();
2088 HInstruction* input_other = instruction->GetLeastConstantLeft();
2089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 DataType::Type type = instruction->GetType();
2091 if (DataType::IsFloatingPointType(type)) {
Serguei Katkov115b53f2015-08-05 17:03:30 +06002092 return;
2093 }
2094
Roland Levillain1a653882016-03-18 18:05:57 +00002095 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002096 // Replace code looking like
2097 // SUB dst, src, 0
2098 // with
2099 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06002100 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
2101 // `x` is `-0.0`, the former expression yields `0.0`, while the later
2102 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002103 instruction->ReplaceWith(input_other);
2104 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01002105 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002106 return;
2107 }
2108
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002109 HBasicBlock* block = instruction->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002110 ArenaAllocator* allocator = GetGraph()->GetAllocator();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002111
Alexandre Rames188d4312015-04-09 18:30:21 +01002112 HInstruction* left = instruction->GetLeft();
2113 HInstruction* right = instruction->GetRight();
2114 if (left->IsConstant()) {
2115 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002116 // Replace code looking like
2117 // SUB dst, 0, src
2118 // with
2119 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01002120 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002121 // `x` is `0.0`, the former expression yields `0.0`, while the later
2122 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01002123 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002124 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01002125 RecordSimplification();
2126 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002127 }
2128 }
Alexandre Rames188d4312015-04-09 18:30:21 +01002129
2130 if (left->IsNeg() && right->IsNeg()) {
2131 if (TryMoveNegOnInputsAfterBinop(instruction)) {
2132 return;
2133 }
2134 }
2135
2136 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
2137 // Replace code looking like
2138 // NEG tmp, b
2139 // SUB dst, a, tmp
2140 // with
2141 // ADD dst, a, b
Vladimir Markoca6fff82017-10-03 14:49:14 +01002142 HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left, right->AsNeg()->GetInput());
Alexandre Rames188d4312015-04-09 18:30:21 +01002143 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
2144 RecordSimplification();
2145 right->GetBlock()->RemoveInstruction(right);
2146 return;
2147 }
2148
2149 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
2150 // Replace code looking like
2151 // NEG tmp, a
2152 // SUB dst, tmp, b
2153 // with
2154 // ADD tmp, a, b
2155 // NEG dst, tmp
2156 // The second version is not intrinsically better, but enables more
2157 // transformations.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002158 HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left->AsNeg()->GetInput(), right);
Alexandre Rames188d4312015-04-09 18:30:21 +01002159 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002160 HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(instruction->GetType(), add);
Alexandre Rames188d4312015-04-09 18:30:21 +01002161 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
2162 instruction->ReplaceWith(neg);
2163 instruction->GetBlock()->RemoveInstruction(instruction);
2164 RecordSimplification();
2165 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01002166 return;
2167 }
2168
2169 if (TrySubtractionChainSimplification(instruction)) {
2170 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01002171 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06002172
2173 if (left->IsAdd()) {
2174 // Replace code patterns looking like
2175 // ADD dst1, x, y ADD dst1, x, y
2176 // SUB dst2, dst1, y SUB dst2, dst1, x
2177 // with
2178 // ADD dst1, x, y
2179 // SUB instruction is not needed in this case, we may use
2180 // one of inputs of ADD instead.
2181 // It is applicable to integral types only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002182 DCHECK(DataType::IsIntegralType(type));
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06002183 if (left->InputAt(1) == right) {
2184 instruction->ReplaceWith(left->InputAt(0));
2185 RecordSimplification();
2186 instruction->GetBlock()->RemoveInstruction(instruction);
2187 return;
2188 } else if (left->InputAt(0) == right) {
2189 instruction->ReplaceWith(left->InputAt(1));
2190 RecordSimplification();
2191 instruction->GetBlock()->RemoveInstruction(instruction);
2192 return;
2193 }
2194 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002195}
2196
2197void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
2198 VisitShift(instruction);
2199}
2200
2201void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
2202 HConstant* input_cst = instruction->GetConstantRight();
2203 HInstruction* input_other = instruction->GetLeastConstantLeft();
2204
Roland Levillain1a653882016-03-18 18:05:57 +00002205 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002206 // Replace code looking like
2207 // XOR dst, src, 0
2208 // with
2209 // src
2210 instruction->ReplaceWith(input_other);
2211 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01002212 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002213 return;
2214 }
2215
Sebastien Hertz9837caf2016-08-01 11:09:50 +02002216 if ((input_cst != nullptr) && input_cst->IsOne()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002217 && input_other->GetType() == DataType::Type::kBool) {
Sebastien Hertz9837caf2016-08-01 11:09:50 +02002218 // Replace code looking like
2219 // XOR dst, src, 1
2220 // with
2221 // BOOLEAN_NOT dst, src
Vladimir Markoca6fff82017-10-03 14:49:14 +01002222 HBooleanNot* boolean_not = new (GetGraph()->GetAllocator()) HBooleanNot(input_other);
Sebastien Hertz9837caf2016-08-01 11:09:50 +02002223 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
2224 RecordSimplification();
2225 return;
2226 }
2227
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002228 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
2229 // Replace code looking like
2230 // XOR dst, src, 0xFFF...FF
2231 // with
2232 // NOT dst, src
Vladimir Markoca6fff82017-10-03 14:49:14 +01002233 HNot* bitwise_not = new (GetGraph()->GetAllocator()) HNot(instruction->GetType(), input_other);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002234 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01002235 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002236 return;
2237 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002238
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00002239 HInstruction* left = instruction->GetLeft();
2240 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00002241 if (((left->IsNot() && right->IsNot()) ||
2242 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00002243 left->HasOnlyOneNonEnvironmentUse() &&
2244 right->HasOnlyOneNonEnvironmentUse()) {
2245 // Replace code looking like
2246 // NOT nota, a
2247 // NOT notb, b
2248 // XOR dst, nota, notb
2249 // with
2250 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00002251 instruction->ReplaceInput(left->InputAt(0), 0);
2252 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00002253 left->GetBlock()->RemoveInstruction(left);
2254 right->GetBlock()->RemoveInstruction(right);
2255 RecordSimplification();
2256 return;
2257 }
2258
Anton Kirilove14dc862016-05-13 17:56:15 +01002259 if (TryReplaceWithRotate(instruction)) {
2260 return;
2261 }
2262
2263 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2264 // so no need to return.
2265 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002266}
2267
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002268void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
2269 HInstruction* argument = instruction->InputAt(1);
2270 HInstruction* receiver = instruction->InputAt(0);
2271 if (receiver == argument) {
2272 // Because String.equals is an instance call, the receiver is
2273 // a null check if we don't know it's null. The argument however, will
2274 // be the actual object. So we cannot end up in a situation where both
2275 // are equal but could be null.
2276 DCHECK(CanEnsureNotNullAt(argument, instruction));
2277 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
2278 instruction->GetBlock()->RemoveInstruction(instruction);
2279 } else {
2280 StringEqualsOptimizations optimizations(instruction);
2281 if (CanEnsureNotNullAt(argument, instruction)) {
2282 optimizations.SetArgumentNotNull();
2283 }
2284 ScopedObjectAccess soa(Thread::Current());
2285 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
2286 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
2287 optimizations.SetArgumentIsString();
2288 }
2289 }
2290}
2291
2292static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
2293 if (potential_length->IsArrayLength()) {
2294 return potential_length->InputAt(0) == potential_array;
2295 }
2296
2297 if (potential_array->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00002298 return potential_array->AsNewArray()->GetLength() == potential_length;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002299 }
2300
2301 return false;
2302}
2303
2304void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
2305 HInstruction* source = instruction->InputAt(0);
2306 HInstruction* destination = instruction->InputAt(2);
2307 HInstruction* count = instruction->InputAt(4);
2308 SystemArrayCopyOptimizations optimizations(instruction);
2309 if (CanEnsureNotNullAt(source, instruction)) {
2310 optimizations.SetSourceIsNotNull();
2311 }
2312 if (CanEnsureNotNullAt(destination, instruction)) {
2313 optimizations.SetDestinationIsNotNull();
2314 }
2315 if (destination == source) {
2316 optimizations.SetDestinationIsSource();
2317 }
2318
2319 if (IsArrayLengthOf(count, source)) {
2320 optimizations.SetCountIsSourceLength();
2321 }
2322
2323 if (IsArrayLengthOf(count, destination)) {
2324 optimizations.SetCountIsDestinationLength();
2325 }
2326
2327 {
2328 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002329 DataType::Type source_component_type = DataType::Type::kVoid;
2330 DataType::Type destination_component_type = DataType::Type::kVoid;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002331 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
2332 if (destination_rti.IsValid()) {
2333 if (destination_rti.IsObjectArray()) {
2334 if (destination_rti.IsExact()) {
2335 optimizations.SetDoesNotNeedTypeCheck();
2336 }
2337 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002338 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002339 if (destination_rti.IsPrimitiveArrayClass()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 destination_component_type = DataTypeFromPrimitive(
2341 destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002342 optimizations.SetDestinationIsPrimitiveArray();
2343 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
2344 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002345 }
2346 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002347 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
2348 if (source_rti.IsValid()) {
2349 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
2350 optimizations.SetDoesNotNeedTypeCheck();
2351 }
2352 if (source_rti.IsPrimitiveArrayClass()) {
2353 optimizations.SetSourceIsPrimitiveArray();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 source_component_type = DataTypeFromPrimitive(
2355 source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002356 } else if (source_rti.IsNonPrimitiveArrayClass()) {
2357 optimizations.SetSourceIsNonPrimitiveArray();
2358 }
2359 }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002360 // For primitive arrays, use their optimized ArtMethod implementations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 if ((source_component_type != DataType::Type::kVoid) &&
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002362 (source_component_type == destination_component_type)) {
2363 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2364 PointerSize image_size = class_linker->GetImagePointerSize();
2365 HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
Vladimir Markod93e3742018-07-18 10:58:13 +01002366 ObjPtr<mirror::Class> system = invoke->GetResolvedMethod()->GetDeclaringClass();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002367 ArtMethod* method = nullptr;
2368 switch (source_component_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002369 case DataType::Type::kBool:
Vladimir Markoba118822017-06-12 15:41:56 +01002370 method = system->FindClassMethod("arraycopy", "([ZI[ZII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002371 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kInt8:
Vladimir Markoba118822017-06-12 15:41:56 +01002373 method = system->FindClassMethod("arraycopy", "([BI[BII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002374 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 case DataType::Type::kUint16:
Vladimir Markoba118822017-06-12 15:41:56 +01002376 method = system->FindClassMethod("arraycopy", "([CI[CII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002377 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002378 case DataType::Type::kInt16:
Vladimir Markoba118822017-06-12 15:41:56 +01002379 method = system->FindClassMethod("arraycopy", "([SI[SII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002380 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 case DataType::Type::kInt32:
Vladimir Markoba118822017-06-12 15:41:56 +01002382 method = system->FindClassMethod("arraycopy", "([II[III)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002383 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kFloat32:
Vladimir Markoba118822017-06-12 15:41:56 +01002385 method = system->FindClassMethod("arraycopy", "([FI[FII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002386 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 case DataType::Type::kInt64:
Vladimir Markoba118822017-06-12 15:41:56 +01002388 method = system->FindClassMethod("arraycopy", "([JI[JII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002389 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002390 case DataType::Type::kFloat64:
Vladimir Markoba118822017-06-12 15:41:56 +01002391 method = system->FindClassMethod("arraycopy", "([DI[DII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002392 break;
2393 default:
2394 LOG(FATAL) << "Unreachable";
2395 }
2396 DCHECK(method != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01002397 DCHECK(method->IsStatic());
2398 DCHECK(method->GetDeclaringClass() == system);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002399 invoke->SetResolvedMethod(method);
2400 // Sharpen the new invoke. Note that we do not update the dex method index of
2401 // the invoke, as we would need to look it up in the current dex file, and it
2402 // is unlikely that it exists. The most usual situation for such typed
2403 // arraycopy methods is a direct pointer to the boot image.
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +00002404 invoke->SetDispatchInfo(HSharpening::SharpenLoadMethod(
2405 method,
2406 /* has_method_id= */ true,
2407 /* for_interface_call= */ false,
2408 codegen_));
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002409 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002410 }
2411}
2412
Aart Bik2a6aad92016-02-25 11:32:32 -08002413void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
2414 DCHECK(invoke->IsInvokeStaticOrDirect());
2415 uint32_t dex_pc = invoke->GetDexPc();
2416 HInstruction* x = invoke->InputAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 DataType::Type type = x->GetType();
Aart Bik2a6aad92016-02-25 11:32:32 -08002418 // Set proper bit pattern for NaN and replace intrinsic with raw version.
2419 HInstruction* nan;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 if (type == DataType::Type::kFloat64) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002421 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
2422 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
Nicolas Geoffray8f2eb252020-11-06 13:39:54 +00002423 kNeedsEnvironment,
Aart Bik2a6aad92016-02-25 11:32:32 -08002424 kNoSideEffects,
2425 kNoThrow);
2426 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 DCHECK_EQ(type, DataType::Type::kFloat32);
Aart Bik2a6aad92016-02-25 11:32:32 -08002428 nan = GetGraph()->GetIntConstant(0x7fc00000);
2429 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
Nicolas Geoffray8f2eb252020-11-06 13:39:54 +00002430 kNeedsEnvironment,
Aart Bik2a6aad92016-02-25 11:32:32 -08002431 kNoSideEffects,
2432 kNoThrow);
2433 }
2434 // Test IsNaN(x), which is the same as x != x.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002435 HCondition* condition = new (GetGraph()->GetAllocator()) HNotEqual(x, x, dex_pc);
Aart Bik2a6aad92016-02-25 11:32:32 -08002436 condition->SetBias(ComparisonBias::kLtBias);
2437 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
2438 // Select between the two.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002439 HInstruction* select = new (GetGraph()->GetAllocator()) HSelect(condition, nan, invoke, dex_pc);
Aart Bik2a6aad92016-02-25 11:32:32 -08002440 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
2441 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
2442}
2443
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002444void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
2445 HInstruction* str = invoke->InputAt(0);
2446 HInstruction* index = invoke->InputAt(1);
2447 uint32_t dex_pc = invoke->GetDexPc();
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002448 ArenaAllocator* allocator = GetGraph()->GetAllocator();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002449 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2450 // so create the HArrayLength, HBoundsCheck and HArrayGet.
Andreas Gampe3db70682018-12-26 15:12:03 -08002451 HArrayLength* length = new (allocator) HArrayLength(str, dex_pc, /* is_string_length= */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002452 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002453 HBoundsCheck* bounds_check = new (allocator) HBoundsCheck(
Andreas Gampe3db70682018-12-26 15:12:03 -08002454 index, length, dex_pc, /* is_string_char_at= */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002455 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002456 HArrayGet* array_get = new (allocator) HArrayGet(str,
2457 bounds_check,
2458 DataType::Type::kUint16,
2459 SideEffects::None(), // Strings are immutable.
2460 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002461 /* is_string_char_at= */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002462 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
2463 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
2464 GetGraph()->SetHasBoundsChecks(true);
2465}
2466
Vladimir Marko5f846072020-04-09 13:20:11 +01002467void InstructionSimplifierVisitor::SimplifyStringLength(HInvoke* invoke) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002468 HInstruction* str = invoke->InputAt(0);
2469 uint32_t dex_pc = invoke->GetDexPc();
2470 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2471 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002472 HArrayLength* length =
Andreas Gampe3db70682018-12-26 15:12:03 -08002473 new (GetGraph()->GetAllocator()) HArrayLength(str, dex_pc, /* is_string_length= */ true);
Vladimir Marko5f846072020-04-09 13:20:11 +01002474 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, length);
Vladimir Markodce016e2016-04-28 13:10:02 +01002475}
2476
Vladimir Marko6fa44042018-03-19 18:42:49 +00002477void InstructionSimplifierVisitor::SimplifyStringIndexOf(HInvoke* invoke) {
2478 DCHECK(invoke->GetIntrinsic() == Intrinsics::kStringIndexOf ||
2479 invoke->GetIntrinsic() == Intrinsics::kStringIndexOfAfter);
2480 if (invoke->InputAt(0)->IsLoadString()) {
2481 HLoadString* load_string = invoke->InputAt(0)->AsLoadString();
2482 const DexFile& dex_file = load_string->GetDexFile();
2483 uint32_t utf16_length;
2484 const char* data =
2485 dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), &utf16_length);
2486 if (utf16_length == 0) {
2487 invoke->ReplaceWith(GetGraph()->GetIntConstant(-1));
2488 invoke->GetBlock()->RemoveInstruction(invoke);
2489 RecordSimplification();
2490 return;
2491 }
2492 if (utf16_length == 1 && invoke->GetIntrinsic() == Intrinsics::kStringIndexOf) {
2493 // Simplify to HSelect(HEquals(., load_string.charAt(0)), 0, -1).
2494 // If the sought character is supplementary, this gives the correct result, i.e. -1.
2495 uint32_t c = GetUtf16FromUtf8(&data);
2496 DCHECK_EQ(GetTrailingUtf16Char(c), 0u);
2497 DCHECK_EQ(GetLeadingUtf16Char(c), c);
2498 uint32_t dex_pc = invoke->GetDexPc();
2499 ArenaAllocator* allocator = GetGraph()->GetAllocator();
2500 HEqual* equal =
2501 new (allocator) HEqual(invoke->InputAt(1), GetGraph()->GetIntConstant(c), dex_pc);
2502 invoke->GetBlock()->InsertInstructionBefore(equal, invoke);
2503 HSelect* result = new (allocator) HSelect(equal,
2504 GetGraph()->GetIntConstant(0),
2505 GetGraph()->GetIntConstant(-1),
2506 dex_pc);
2507 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, result);
2508 RecordSimplification();
2509 return;
2510 }
2511 }
2512}
2513
Aart Bikff7d89c2016-11-07 08:49:28 -08002514// This method should only be used on intrinsics whose sole way of throwing an
2515// exception is raising a NPE when the nth argument is null. If that argument
2516// is provably non-null, we can clear the flag.
2517void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2518 HInstruction* arg = invoke->InputAt(n);
Aart Bik71bf7b42016-11-16 10:17:46 -08002519 if (invoke->CanThrow() && !arg->CanBeNull()) {
Aart Bikff7d89c2016-11-07 08:49:28 -08002520 invoke->SetCanThrow(false);
2521 }
2522}
2523
Aart Bik71bf7b42016-11-16 10:17:46 -08002524// Methods that return "this" can replace the returned value with the receiver.
2525void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2526 if (invoke->HasUses()) {
2527 HInstruction* receiver = invoke->InputAt(0);
2528 invoke->ReplaceWith(receiver);
2529 RecordSimplification();
2530 }
2531}
2532
2533// Helper method for StringBuffer escape analysis.
2534static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2535 if (user->IsInvokeStaticOrDirect()) {
2536 // Any constructor on StringBuffer is okay.
Aart Bikab2270f2016-12-15 09:36:31 -08002537 return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2538 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
Aart Bik71bf7b42016-11-16 10:17:46 -08002539 user->InputAt(0) == reference;
2540 } else if (user->IsInvokeVirtual()) {
2541 switch (user->AsInvokeVirtual()->GetIntrinsic()) {
2542 case Intrinsics::kStringBufferLength:
2543 case Intrinsics::kStringBufferToString:
2544 DCHECK_EQ(user->InputAt(0), reference);
2545 return true;
2546 case Intrinsics::kStringBufferAppend:
2547 // Returns "this", so only okay if no further uses.
2548 DCHECK_EQ(user->InputAt(0), reference);
2549 DCHECK_NE(user->InputAt(1), reference);
2550 return !user->HasUses();
2551 default:
2552 break;
2553 }
2554 }
2555 return false;
2556}
2557
Vladimir Marko552a1342017-10-31 10:56:47 +00002558static bool TryReplaceStringBuilderAppend(HInvoke* invoke) {
2559 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringBuilderToString);
2560 if (invoke->CanThrowIntoCatchBlock()) {
2561 return false;
2562 }
2563
2564 HBasicBlock* block = invoke->GetBlock();
2565 HInstruction* sb = invoke->InputAt(0);
2566
2567 // We support only a new StringBuilder, otherwise we cannot ensure that
2568 // the StringBuilder data does not need to be populated for other users.
2569 if (!sb->IsNewInstance()) {
2570 return false;
2571 }
2572
2573 // For now, we support only single-block recognition.
2574 // (Ternary operators feeding the append could be implemented.)
2575 for (const HUseListNode<HInstruction*>& use : sb->GetUses()) {
2576 if (use.GetUser()->GetBlock() != block) {
2577 return false;
2578 }
Vladimir Markoa18f5ae2019-12-13 12:53:39 +00002579 // The append pattern uses the StringBuilder only as the first argument.
2580 if (use.GetIndex() != 0u) {
2581 return false;
2582 }
Vladimir Marko552a1342017-10-31 10:56:47 +00002583 }
2584
2585 // Collect args and check for unexpected uses.
2586 // We expect one call to a constructor with no arguments, one constructor fence (unless
2587 // eliminated), some number of append calls and one call to StringBuilder.toString().
2588 bool seen_constructor = false;
2589 bool seen_constructor_fence = false;
2590 bool seen_to_string = false;
2591 uint32_t format = 0u;
2592 uint32_t num_args = 0u;
2593 HInstruction* args[StringBuilderAppend::kMaxArgs]; // Added in reverse order.
Vladimir Markoa18f5ae2019-12-13 12:53:39 +00002594 for (HBackwardInstructionIterator iter(block->GetInstructions()); !iter.Done(); iter.Advance()) {
2595 HInstruction* user = iter.Current();
2596 // Instructions of interest apply to `sb`, skip those that do not involve `sb`.
2597 if (user->InputCount() == 0u || user->InputAt(0u) != sb) {
2598 continue;
Vladimir Marko552a1342017-10-31 10:56:47 +00002599 }
Vladimir Markoa18f5ae2019-12-13 12:53:39 +00002600 // We visit the uses in reverse order, so the StringBuilder.toString() must come first.
Vladimir Marko552a1342017-10-31 10:56:47 +00002601 if (!seen_to_string) {
Vladimir Markoa18f5ae2019-12-13 12:53:39 +00002602 if (user == invoke) {
Vladimir Marko552a1342017-10-31 10:56:47 +00002603 seen_to_string = true;
2604 continue;
2605 } else {
2606 return false;
2607 }
2608 }
2609 // Then we should see the arguments.
2610 if (user->IsInvokeVirtual()) {
2611 HInvokeVirtual* as_invoke_virtual = user->AsInvokeVirtual();
2612 DCHECK(!seen_constructor);
2613 DCHECK(!seen_constructor_fence);
2614 StringBuilderAppend::Argument arg;
2615 switch (as_invoke_virtual->GetIntrinsic()) {
2616 case Intrinsics::kStringBuilderAppendObject:
2617 // TODO: Unimplemented, needs to call String.valueOf().
2618 return false;
2619 case Intrinsics::kStringBuilderAppendString:
2620 arg = StringBuilderAppend::Argument::kString;
2621 break;
2622 case Intrinsics::kStringBuilderAppendCharArray:
2623 // TODO: Unimplemented, StringBuilder.append(char[]) can throw NPE and we would
2624 // not have the correct stack trace for it.
2625 return false;
2626 case Intrinsics::kStringBuilderAppendBoolean:
2627 arg = StringBuilderAppend::Argument::kBoolean;
2628 break;
2629 case Intrinsics::kStringBuilderAppendChar:
2630 arg = StringBuilderAppend::Argument::kChar;
2631 break;
2632 case Intrinsics::kStringBuilderAppendInt:
2633 arg = StringBuilderAppend::Argument::kInt;
2634 break;
2635 case Intrinsics::kStringBuilderAppendLong:
2636 arg = StringBuilderAppend::Argument::kLong;
2637 break;
2638 case Intrinsics::kStringBuilderAppendCharSequence: {
2639 ReferenceTypeInfo rti = user->AsInvokeVirtual()->InputAt(1)->GetReferenceTypeInfo();
2640 if (!rti.IsValid()) {
2641 return false;
2642 }
2643 ScopedObjectAccess soa(Thread::Current());
2644 Handle<mirror::Class> input_type = rti.GetTypeHandle();
2645 DCHECK(input_type != nullptr);
2646 if (input_type.Get() == GetClassRoot<mirror::String>()) {
2647 arg = StringBuilderAppend::Argument::kString;
2648 } else {
2649 // TODO: Check and implement for StringBuilder. We could find the StringBuilder's
2650 // internal char[] inconsistent with the length, or the string compression
2651 // of the result could be compromised with a concurrent modification, and
2652 // we would need to throw appropriate exceptions.
2653 return false;
2654 }
2655 break;
2656 }
2657 case Intrinsics::kStringBuilderAppendFloat:
2658 case Intrinsics::kStringBuilderAppendDouble:
2659 // TODO: Unimplemented, needs to call FloatingDecimal.getBinaryToASCIIConverter().
2660 return false;
2661 default: {
2662 return false;
2663 }
2664 }
2665 // Uses of the append return value should have been replaced with the first input.
2666 DCHECK(!as_invoke_virtual->HasUses());
2667 DCHECK(!as_invoke_virtual->HasEnvironmentUses());
2668 if (num_args == StringBuilderAppend::kMaxArgs) {
2669 return false;
2670 }
2671 format = (format << StringBuilderAppend::kBitsPerArg) | static_cast<uint32_t>(arg);
2672 args[num_args] = as_invoke_virtual->InputAt(1u);
2673 ++num_args;
2674 } else if (user->IsInvokeStaticOrDirect() &&
2675 user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2676 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
2677 user->AsInvokeStaticOrDirect()->GetNumberOfArguments() == 1u) {
2678 // After arguments, we should see the constructor.
2679 // We accept only the constructor with no extra arguments.
2680 DCHECK(!seen_constructor);
2681 DCHECK(!seen_constructor_fence);
2682 seen_constructor = true;
2683 } else if (user->IsConstructorFence()) {
2684 // The last use we see is the constructor fence.
2685 DCHECK(seen_constructor);
2686 DCHECK(!seen_constructor_fence);
2687 seen_constructor_fence = true;
2688 } else {
2689 return false;
2690 }
2691 }
2692
2693 if (num_args == 0u) {
2694 return false;
2695 }
2696
2697 // Check environment uses.
2698 for (const HUseListNode<HEnvironment*>& use : sb->GetEnvUses()) {
2699 HInstruction* holder = use.GetUser()->GetHolder();
2700 if (holder->GetBlock() != block) {
2701 return false;
2702 }
2703 // Accept only calls on the StringBuilder (which shall all be removed).
2704 // TODO: Carve-out for const-string? Or rely on environment pruning (to be implemented)?
2705 if (holder->InputCount() == 0 || holder->InputAt(0) != sb) {
2706 return false;
2707 }
2708 }
2709
2710 // Create replacement instruction.
2711 HIntConstant* fmt = block->GetGraph()->GetIntConstant(static_cast<int32_t>(format));
2712 ArenaAllocator* allocator = block->GetGraph()->GetAllocator();
2713 HStringBuilderAppend* append =
2714 new (allocator) HStringBuilderAppend(fmt, num_args, allocator, invoke->GetDexPc());
2715 append->SetReferenceTypeInfo(invoke->GetReferenceTypeInfo());
2716 for (size_t i = 0; i != num_args; ++i) {
2717 append->SetArgumentAt(i, args[num_args - 1u - i]);
2718 }
2719 block->InsertInstructionBefore(append, invoke);
Vladimir Markob1fe5e12020-03-10 14:30:49 +00002720 DCHECK(!invoke->CanBeNull());
2721 DCHECK(!append->CanBeNull());
Vladimir Marko552a1342017-10-31 10:56:47 +00002722 invoke->ReplaceWith(append);
2723 // Copy environment, except for the StringBuilder uses.
Vladimir Marko56f13322019-11-15 14:07:19 +00002724 for (HEnvironment* env = invoke->GetEnvironment(); env != nullptr; env = env->GetParent()) {
2725 for (size_t i = 0, size = env->Size(); i != size; ++i) {
2726 if (env->GetInstructionAt(i) == sb) {
2727 env->RemoveAsUserOfInput(i);
2728 env->SetRawEnvAt(i, /*instruction=*/ nullptr);
2729 }
Vladimir Marko552a1342017-10-31 10:56:47 +00002730 }
2731 }
2732 append->CopyEnvironmentFrom(invoke->GetEnvironment());
2733 // Remove the old instruction.
2734 block->RemoveInstruction(invoke);
2735 // Remove the StringBuilder's uses and StringBuilder.
2736 while (sb->HasNonEnvironmentUses()) {
2737 block->RemoveInstruction(sb->GetUses().front().GetUser());
2738 }
2739 DCHECK(!sb->HasEnvironmentUses());
2740 block->RemoveInstruction(sb);
2741 return true;
2742}
2743
Aart Bik71bf7b42016-11-16 10:17:46 -08002744// Certain allocation intrinsics are not removed by dead code elimination
2745// because of potentially throwing an OOM exception or other side effects.
2746// This method removes such intrinsics when special circumstances allow.
2747void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
2748 if (!invoke->HasUses()) {
2749 // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
2750 // the potential OOM of course. Otherwise, we must ensure the receiver object of this
2751 // call does not escape since only thread-local synchronization may be removed.
2752 bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
2753 HInstruction* receiver = invoke->InputAt(0);
2754 if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
2755 invoke->GetBlock()->RemoveInstruction(invoke);
2756 RecordSimplification();
2757 }
Vladimir Marko552a1342017-10-31 10:56:47 +00002758 } else if (invoke->GetIntrinsic() == Intrinsics::kStringBuilderToString &&
2759 TryReplaceStringBuilderAppend(invoke)) {
2760 RecordSimplification();
Aart Bik71bf7b42016-11-16 10:17:46 -08002761 }
2762}
2763
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002764void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002765 switch (instruction->GetIntrinsic()) {
2766 case Intrinsics::kStringEquals:
2767 SimplifyStringEquals(instruction);
2768 break;
2769 case Intrinsics::kSystemArrayCopy:
2770 SimplifySystemArrayCopy(instruction);
2771 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002772 case Intrinsics::kFloatFloatToIntBits:
2773 case Intrinsics::kDoubleDoubleToLongBits:
2774 SimplifyFP2Int(instruction);
2775 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002776 case Intrinsics::kStringCharAt:
Vladimir Marko5f846072020-04-09 13:20:11 +01002777 // Instruction builder creates intermediate representation directly
2778 // but the inliner can sharpen CharSequence.charAt() to String.charAt().
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002779 SimplifyStringCharAt(instruction);
2780 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01002781 case Intrinsics::kStringLength:
Vladimir Marko5f846072020-04-09 13:20:11 +01002782 // Instruction builder creates intermediate representation directly
2783 // but the inliner can sharpen CharSequence.length() to String.length().
2784 SimplifyStringLength(instruction);
Vladimir Markodce016e2016-04-28 13:10:02 +01002785 break;
Vladimir Marko6fa44042018-03-19 18:42:49 +00002786 case Intrinsics::kStringIndexOf:
2787 case Intrinsics::kStringIndexOfAfter:
2788 SimplifyStringIndexOf(instruction);
2789 break;
Aart Bikff7d89c2016-11-07 08:49:28 -08002790 case Intrinsics::kStringStringIndexOf:
2791 case Intrinsics::kStringStringIndexOfAfter:
2792 SimplifyNPEOnArgN(instruction, 1); // 0th has own NullCheck
2793 break;
Aart Bik71bf7b42016-11-16 10:17:46 -08002794 case Intrinsics::kStringBufferAppend:
Vladimir Markod4561172017-10-30 17:48:25 +00002795 case Intrinsics::kStringBuilderAppendObject:
2796 case Intrinsics::kStringBuilderAppendString:
2797 case Intrinsics::kStringBuilderAppendCharSequence:
2798 case Intrinsics::kStringBuilderAppendCharArray:
2799 case Intrinsics::kStringBuilderAppendBoolean:
2800 case Intrinsics::kStringBuilderAppendChar:
2801 case Intrinsics::kStringBuilderAppendInt:
2802 case Intrinsics::kStringBuilderAppendLong:
2803 case Intrinsics::kStringBuilderAppendFloat:
2804 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08002805 SimplifyReturnThis(instruction);
2806 break;
2807 case Intrinsics::kStringBufferToString:
2808 case Intrinsics::kStringBuilderToString:
2809 SimplifyAllocationIntrinsic(instruction);
2810 break;
Vladimir Marko5f846072020-04-09 13:20:11 +01002811 case Intrinsics::kIntegerRotateRight:
2812 case Intrinsics::kLongRotateRight:
2813 case Intrinsics::kIntegerRotateLeft:
2814 case Intrinsics::kLongRotateLeft:
2815 case Intrinsics::kIntegerCompare:
2816 case Intrinsics::kLongCompare:
2817 case Intrinsics::kIntegerSignum:
2818 case Intrinsics::kLongSignum:
2819 case Intrinsics::kFloatIsNaN:
2820 case Intrinsics::kDoubleIsNaN:
2821 case Intrinsics::kStringIsEmpty:
Aart Bik11932592016-03-08 12:42:25 -08002822 case Intrinsics::kUnsafeLoadFence:
Aart Bik11932592016-03-08 12:42:25 -08002823 case Intrinsics::kUnsafeStoreFence:
Aart Bik11932592016-03-08 12:42:25 -08002824 case Intrinsics::kUnsafeFullFence:
Orion Hodson4a4610a2017-09-28 16:57:55 +01002825 case Intrinsics::kVarHandleFullFence:
Orion Hodson4a4610a2017-09-28 16:57:55 +01002826 case Intrinsics::kVarHandleAcquireFence:
Orion Hodson4a4610a2017-09-28 16:57:55 +01002827 case Intrinsics::kVarHandleReleaseFence:
Orion Hodson4a4610a2017-09-28 16:57:55 +01002828 case Intrinsics::kVarHandleLoadLoadFence:
Orion Hodson4a4610a2017-09-28 16:57:55 +01002829 case Intrinsics::kVarHandleStoreStoreFence:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002830 case Intrinsics::kMathMinIntInt:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002831 case Intrinsics::kMathMinLongLong:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002832 case Intrinsics::kMathMinFloatFloat:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002833 case Intrinsics::kMathMinDoubleDouble:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002834 case Intrinsics::kMathMaxIntInt:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002835 case Intrinsics::kMathMaxLongLong:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002836 case Intrinsics::kMathMaxFloatFloat:
Aart Bik1f8d51b2018-02-15 10:42:37 -08002837 case Intrinsics::kMathMaxDoubleDouble:
Aart Bik3dad3412018-02-28 12:01:46 -08002838 case Intrinsics::kMathAbsInt:
Aart Bik3dad3412018-02-28 12:01:46 -08002839 case Intrinsics::kMathAbsLong:
Aart Bik3dad3412018-02-28 12:01:46 -08002840 case Intrinsics::kMathAbsFloat:
Aart Bik3dad3412018-02-28 12:01:46 -08002841 case Intrinsics::kMathAbsDouble:
Vladimir Marko5f846072020-04-09 13:20:11 +01002842 // These are replaced by intermediate representation in the instruction builder.
2843 LOG(FATAL) << "Unexpected " << static_cast<Intrinsics>(instruction->GetIntrinsic());
2844 UNREACHABLE();
Aart Bik2a6aad92016-02-25 11:32:32 -08002845 default:
2846 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002847 }
2848}
2849
Aart Bikbb245d12015-10-19 11:05:03 -07002850void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
2851 HInstruction* cond = deoptimize->InputAt(0);
2852 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002853 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07002854 // Never deopt: instruction can be removed.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002855 if (deoptimize->GuardsAnInput()) {
2856 deoptimize->ReplaceWith(deoptimize->GuardedInput());
2857 }
Aart Bikbb245d12015-10-19 11:05:03 -07002858 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
2859 } else {
2860 // Always deopt.
2861 }
2862 }
2863}
2864
Anton Kirilove14dc862016-05-13 17:56:15 +01002865// Replace code looking like
2866// OP y, x, const1
2867// OP z, y, const2
2868// with
2869// OP z, x, const3
2870// where OP is both an associative and a commutative operation.
2871bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
2872 HBinaryOperation* instruction) {
2873 DCHECK(instruction->IsCommutative());
2874
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 if (!DataType::IsIntegralType(instruction->GetType())) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002876 return false;
2877 }
2878
2879 HInstruction* left = instruction->GetLeft();
2880 HInstruction* right = instruction->GetRight();
2881 // Variable names as described above.
2882 HConstant* const2;
2883 HBinaryOperation* y;
2884
Vladimir Marko0dcccd82018-05-04 13:32:25 +01002885 if (instruction->GetKind() == left->GetKind() && right->IsConstant()) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002886 const2 = right->AsConstant();
2887 y = left->AsBinaryOperation();
Vladimir Marko0dcccd82018-05-04 13:32:25 +01002888 } else if (left->IsConstant() && instruction->GetKind() == right->GetKind()) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002889 const2 = left->AsConstant();
2890 y = right->AsBinaryOperation();
2891 } else {
2892 // The node does not match the pattern.
2893 return false;
2894 }
2895
2896 // If `y` has more than one use, we do not perform the optimization
2897 // because it might increase code size (e.g. if the new constant is
2898 // no longer encodable as an immediate operand in the target ISA).
2899 if (!y->HasOnlyOneNonEnvironmentUse()) {
2900 return false;
2901 }
2902
2903 // GetConstantRight() can return both left and right constants
2904 // for commutative operations.
2905 HConstant* const1 = y->GetConstantRight();
2906 if (const1 == nullptr) {
2907 return false;
2908 }
2909
2910 instruction->ReplaceInput(const1, 0);
2911 instruction->ReplaceInput(const2, 1);
2912 HConstant* const3 = instruction->TryStaticEvaluation();
2913 DCHECK(const3 != nullptr);
2914 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
2915 instruction->ReplaceInput(const3, 1);
2916 RecordSimplification();
2917 return true;
2918}
2919
2920static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
2921 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
2922}
2923
2924// Helper function that performs addition statically, considering the result type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002925static int64_t ComputeAddition(DataType::Type type, int64_t x, int64_t y) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002926 // Use the Compute() method for consistency with TryStaticEvaluation().
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 if (type == DataType::Type::kInt32) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002928 return HAdd::Compute<int32_t>(x, y);
2929 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 DCHECK_EQ(type, DataType::Type::kInt64);
Anton Kirilove14dc862016-05-13 17:56:15 +01002931 return HAdd::Compute<int64_t>(x, y);
2932 }
2933}
2934
2935// Helper function that handles the child classes of HConstant
2936// and returns an integer with the appropriate sign.
2937static int64_t GetValue(HConstant* constant, bool is_negated) {
2938 int64_t ret = Int64FromConstant(constant);
2939 return is_negated ? -ret : ret;
2940}
2941
2942// Replace code looking like
2943// OP1 y, x, const1
2944// OP2 z, y, const2
2945// with
2946// OP3 z, x, const3
2947// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2948bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2949 HBinaryOperation* instruction) {
2950 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 DataType::Type type = instruction->GetType();
2953 if (!DataType::IsIntegralType(type)) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002954 return false;
2955 }
2956
2957 HInstruction* left = instruction->GetLeft();
2958 HInstruction* right = instruction->GetRight();
2959 // Variable names as described above.
2960 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2961 if (const2 == nullptr) {
2962 return false;
2963 }
2964
2965 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2966 ? left->AsBinaryOperation()
2967 : AsAddOrSub(right);
2968 // If y has more than one use, we do not perform the optimization because
2969 // it might increase code size (e.g. if the new constant is no longer
2970 // encodable as an immediate operand in the target ISA).
2971 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2972 return false;
2973 }
2974
2975 left = y->GetLeft();
2976 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2977 if (const1 == nullptr) {
2978 return false;
2979 }
2980
2981 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2982 // If both inputs are constants, let the constant folding pass deal with it.
2983 if (x->IsConstant()) {
2984 return false;
2985 }
2986
2987 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2988 int64_t const2_val = GetValue(const2, is_const2_negated);
2989 bool is_y_negated = (y == right) && instruction->IsSub();
2990 right = y->GetRight();
2991 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2992 int64_t const1_val = GetValue(const1, is_const1_negated);
2993 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2994 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2995 HBasicBlock* block = instruction->GetBlock();
2996 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002997 ArenaAllocator* allocator = instruction->GetAllocator();
Anton Kirilove14dc862016-05-13 17:56:15 +01002998 HInstruction* z;
2999
3000 if (is_x_negated) {
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003001 z = new (allocator) HSub(type, const3, x, instruction->GetDexPc());
Anton Kirilove14dc862016-05-13 17:56:15 +01003002 } else {
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003003 z = new (allocator) HAdd(type, x, const3, instruction->GetDexPc());
Anton Kirilove14dc862016-05-13 17:56:15 +01003004 }
3005
3006 block->ReplaceAndRemoveInstructionWith(instruction, z);
3007 RecordSimplification();
3008 return true;
3009}
3010
Lena Djokicbc5460b2017-07-20 16:07:36 +02003011void InstructionSimplifierVisitor::VisitVecMul(HVecMul* instruction) {
3012 if (TryCombineVecMultiplyAccumulate(instruction)) {
3013 RecordSimplification();
3014 }
3015}
3016
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003017} // namespace art