blob: 9cc5cad55ac5a4448f3ad2307e210a48d1c9aa65 [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 Markob4eb1b12018-05-24 11:09:38 +010021#include "class_root.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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "sharpening.h"
Vladimir Marko552a1342017-10-31 10:56:47 +000028#include "string_builder_append.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000029
Nicolas Geoffray3c049742014-09-24 18:10:46 +010030namespace art {
31
Artem Serovcced8ba2017-07-19 18:18:09 +010032// Whether to run an exhaustive test of individual HInstructions cloning when each instruction
33// is replaced with its copy if it is clonable.
34static constexpr bool kTestInstructionClonerExhaustively = false;
35
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010036class InstructionSimplifierVisitor : public HGraphDelegateVisitor {
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000037 public:
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000038 InstructionSimplifierVisitor(HGraph* graph,
39 CodeGenerator* codegen,
40 OptimizingCompilerStats* stats)
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +010041 : HGraphDelegateVisitor(graph),
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000042 codegen_(codegen),
Alexandre Rames188d4312015-04-09 18:30:21 +010043 stats_(stats) {}
44
Aart Bik24773202018-04-26 10:28:51 -070045 bool Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000046
47 private:
Alexandre Rames188d4312015-04-09 18:30:21 +010048 void RecordSimplification() {
49 simplification_occurred_ = true;
50 simplifications_at_current_position_++;
Vladimir Markocd09e1f2017-11-24 15:02:40 +000051 MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSimplifications);
Alexandre Rames188d4312015-04-09 18:30:21 +010052 }
53
Scott Wakeling40a04bf2015-12-11 09:50:36 +000054 bool ReplaceRotateWithRor(HBinaryOperation* op, HUShr* ushr, HShl* shl);
55 bool TryReplaceWithRotate(HBinaryOperation* instruction);
56 bool TryReplaceWithRotateConstantPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
57 bool TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
58 bool TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op, HUShr* ushr, HShl* shl);
59
Alexandre Rames188d4312015-04-09 18:30:21 +010060 bool TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +000061 // `op` should be either HOr or HAnd.
62 // De Morgan's laws:
63 // ~a & ~b = ~(a | b) and ~a | ~b = ~(a & b)
64 bool TryDeMorganNegationFactoring(HBinaryOperation* op);
Anton Kirilove14dc862016-05-13 17:56:15 +010065 bool TryHandleAssociativeAndCommutativeOperation(HBinaryOperation* instruction);
66 bool TrySubtractionChainSimplification(HBinaryOperation* instruction);
Lena Djokicbc5460b2017-07-20 16:07:36 +020067 bool TryCombineVecMultiplyAccumulate(HVecMul* mul);
Anton Kirilove14dc862016-05-13 17:56:15 +010068
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000069 void VisitShift(HBinaryOperation* shift);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010070 void VisitEqual(HEqual* equal) override;
71 void VisitNotEqual(HNotEqual* equal) override;
72 void VisitBooleanNot(HBooleanNot* bool_not) override;
73 void VisitInstanceFieldSet(HInstanceFieldSet* equal) override;
74 void VisitStaticFieldSet(HStaticFieldSet* equal) override;
75 void VisitArraySet(HArraySet* equal) override;
76 void VisitTypeConversion(HTypeConversion* instruction) override;
77 void VisitNullCheck(HNullCheck* instruction) override;
78 void VisitArrayLength(HArrayLength* instruction) override;
79 void VisitCheckCast(HCheckCast* instruction) override;
80 void VisitAbs(HAbs* instruction) override;
81 void VisitAdd(HAdd* instruction) override;
82 void VisitAnd(HAnd* instruction) override;
83 void VisitCondition(HCondition* instruction) override;
84 void VisitGreaterThan(HGreaterThan* condition) override;
85 void VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) override;
86 void VisitLessThan(HLessThan* condition) override;
87 void VisitLessThanOrEqual(HLessThanOrEqual* condition) override;
88 void VisitBelow(HBelow* condition) override;
89 void VisitBelowOrEqual(HBelowOrEqual* condition) override;
90 void VisitAbove(HAbove* condition) override;
91 void VisitAboveOrEqual(HAboveOrEqual* condition) override;
92 void VisitDiv(HDiv* instruction) override;
93 void VisitMul(HMul* instruction) override;
94 void VisitNeg(HNeg* instruction) override;
95 void VisitNot(HNot* instruction) override;
96 void VisitOr(HOr* instruction) override;
97 void VisitShl(HShl* instruction) override;
98 void VisitShr(HShr* instruction) override;
99 void VisitSub(HSub* instruction) override;
100 void VisitUShr(HUShr* instruction) override;
101 void VisitXor(HXor* instruction) override;
102 void VisitSelect(HSelect* select) override;
103 void VisitIf(HIf* instruction) override;
104 void VisitInstanceOf(HInstanceOf* instruction) override;
105 void VisitInvoke(HInvoke* invoke) override;
106 void VisitDeoptimize(HDeoptimize* deoptimize) override;
107 void VisitVecMul(HVecMul* instruction) override;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100108
109 bool CanEnsureNotNullAt(HInstruction* instr, HInstruction* at) const;
Calin Juravleacf735c2015-02-12 15:25:22 +0000110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100111 void SimplifyRotate(HInvoke* invoke, bool is_left, DataType::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100112 void SimplifySystemArrayCopy(HInvoke* invoke);
113 void SimplifyStringEquals(HInvoke* invoke);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100114 void SimplifyCompare(HInvoke* invoke, bool is_signum, DataType::Type type);
Aart Bik75a38b22016-02-17 10:41:50 -0800115 void SimplifyIsNaN(HInvoke* invoke);
Aart Bik2a6aad92016-02-25 11:32:32 -0800116 void SimplifyFP2Int(HInvoke* invoke);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100117 void SimplifyStringCharAt(HInvoke* invoke);
Vladimir Markodce016e2016-04-28 13:10:02 +0100118 void SimplifyStringIsEmptyOrLength(HInvoke* invoke);
Vladimir Marko6fa44042018-03-19 18:42:49 +0000119 void SimplifyStringIndexOf(HInvoke* invoke);
Aart Bikff7d89c2016-11-07 08:49:28 -0800120 void SimplifyNPEOnArgN(HInvoke* invoke, size_t);
Aart Bik71bf7b42016-11-16 10:17:46 -0800121 void SimplifyReturnThis(HInvoke* invoke);
122 void SimplifyAllocationIntrinsic(HInvoke* invoke);
Aart Bik11932592016-03-08 12:42:25 -0800123 void SimplifyMemBarrier(HInvoke* invoke, MemBarrierKind barrier_kind);
Aart Bik1f8d51b2018-02-15 10:42:37 -0800124 void SimplifyMin(HInvoke* invoke, DataType::Type type);
125 void SimplifyMax(HInvoke* invoke, DataType::Type type);
Aart Bik3dad3412018-02-28 12:01:46 -0800126 void SimplifyAbs(HInvoke* invoke, DataType::Type type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100127
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000128 CodeGenerator* codegen_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000129 OptimizingCompilerStats* stats_;
Alexandre Rames188d4312015-04-09 18:30:21 +0100130 bool simplification_occurred_ = false;
131 int simplifications_at_current_position_ = 0;
Aart Bik2767f4b2016-10-28 15:03:53 -0700132 // We ensure we do not loop infinitely. The value should not be too high, since that
133 // would allow looping around the same basic block too many times. The value should
134 // not be too low either, however, since we want to allow revisiting a basic block
135 // with many statements and simplifications at least once.
136 static constexpr int kMaxSamePositionSimplifications = 50;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000137};
138
Aart Bik24773202018-04-26 10:28:51 -0700139bool InstructionSimplifier::Run() {
Artem Serovcced8ba2017-07-19 18:18:09 +0100140 if (kTestInstructionClonerExhaustively) {
141 CloneAndReplaceInstructionVisitor visitor(graph_);
142 visitor.VisitReversePostOrder();
143 }
144
Vladimir Markobb089b62018-06-28 17:30:16 +0100145 InstructionSimplifierVisitor visitor(graph_, codegen_, stats_);
Aart Bik24773202018-04-26 10:28:51 -0700146 return visitor.Run();
Alexandre Rames188d4312015-04-09 18:30:21 +0100147}
148
Aart Bik24773202018-04-26 10:28:51 -0700149bool InstructionSimplifierVisitor::Run() {
150 bool didSimplify = false;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100151 // Iterate in reverse post order to open up more simplifications to users
152 // of instructions that got simplified.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100153 for (HBasicBlock* block : GetGraph()->GetReversePostOrder()) {
Alexandre Rames188d4312015-04-09 18:30:21 +0100154 // The simplification of an instruction to another instruction may yield
155 // possibilities for other simplifications. So although we perform a reverse
156 // post order visit, we sometimes need to revisit an instruction index.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100157 do {
158 simplification_occurred_ = false;
159 VisitBasicBlock(block);
Aart Bik24773202018-04-26 10:28:51 -0700160 if (simplification_occurred_) {
161 didSimplify = true;
162 }
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100163 } while (simplification_occurred_ &&
164 (simplifications_at_current_position_ < kMaxSamePositionSimplifications));
Alexandre Rames188d4312015-04-09 18:30:21 +0100165 simplifications_at_current_position_ = 0;
Alexandre Rames188d4312015-04-09 18:30:21 +0100166 }
Aart Bik24773202018-04-26 10:28:51 -0700167 return didSimplify;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100168}
169
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000170namespace {
171
172bool AreAllBitsSet(HConstant* constant) {
173 return Int64FromConstant(constant) == -1;
174}
175
176} // namespace
177
Alexandre Rames188d4312015-04-09 18:30:21 +0100178// Returns true if the code was simplified to use only one negation operation
179// after the binary operation instead of one on each of the inputs.
180bool InstructionSimplifierVisitor::TryMoveNegOnInputsAfterBinop(HBinaryOperation* binop) {
181 DCHECK(binop->IsAdd() || binop->IsSub());
182 DCHECK(binop->GetLeft()->IsNeg() && binop->GetRight()->IsNeg());
183 HNeg* left_neg = binop->GetLeft()->AsNeg();
184 HNeg* right_neg = binop->GetRight()->AsNeg();
185 if (!left_neg->HasOnlyOneNonEnvironmentUse() ||
186 !right_neg->HasOnlyOneNonEnvironmentUse()) {
187 return false;
188 }
189 // Replace code looking like
190 // NEG tmp1, a
191 // NEG tmp2, b
192 // ADD dst, tmp1, tmp2
193 // with
194 // ADD tmp, a, b
195 // NEG dst, tmp
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600196 // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
197 // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
198 // while the later yields `-0.0`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100199 if (!DataType::IsIntegralType(binop->GetType())) {
Serdjuk, Nikolay Yaae9e662015-08-21 13:26:34 +0600200 return false;
201 }
Alexandre Rames188d4312015-04-09 18:30:21 +0100202 binop->ReplaceInput(left_neg->GetInput(), 0);
203 binop->ReplaceInput(right_neg->GetInput(), 1);
204 left_neg->GetBlock()->RemoveInstruction(left_neg);
205 right_neg->GetBlock()->RemoveInstruction(right_neg);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100206 HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(binop->GetType(), binop);
Alexandre Rames188d4312015-04-09 18:30:21 +0100207 binop->GetBlock()->InsertInstructionBefore(neg, binop->GetNext());
208 binop->ReplaceWithExceptInReplacementAtIndex(neg, 0);
209 RecordSimplification();
210 return true;
211}
212
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000213bool InstructionSimplifierVisitor::TryDeMorganNegationFactoring(HBinaryOperation* op) {
214 DCHECK(op->IsAnd() || op->IsOr()) << op->DebugName();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100215 DataType::Type type = op->GetType();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000216 HInstruction* left = op->GetLeft();
217 HInstruction* right = op->GetRight();
218
219 // We can apply De Morgan's laws if both inputs are Not's and are only used
220 // by `op`.
Alexandre Rames9f980252016-02-05 14:00:28 +0000221 if (((left->IsNot() && right->IsNot()) ||
222 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000223 left->HasOnlyOneNonEnvironmentUse() &&
224 right->HasOnlyOneNonEnvironmentUse()) {
225 // Replace code looking like
226 // NOT nota, a
227 // NOT notb, b
228 // AND dst, nota, notb (respectively OR)
229 // with
230 // OR or, a, b (respectively AND)
231 // NOT dest, or
Alexandre Rames9f980252016-02-05 14:00:28 +0000232 HInstruction* src_left = left->InputAt(0);
233 HInstruction* src_right = right->InputAt(0);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000234 uint32_t dex_pc = op->GetDexPc();
235
236 // Remove the negations on the inputs.
237 left->ReplaceWith(src_left);
238 right->ReplaceWith(src_right);
239 left->GetBlock()->RemoveInstruction(left);
240 right->GetBlock()->RemoveInstruction(right);
241
242 // Replace the `HAnd` or `HOr`.
243 HBinaryOperation* hbin;
244 if (op->IsAnd()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100245 hbin = new (GetGraph()->GetAllocator()) HOr(type, src_left, src_right, dex_pc);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000246 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100247 hbin = new (GetGraph()->GetAllocator()) HAnd(type, src_left, src_right, dex_pc);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000248 }
Alexandre Rames9f980252016-02-05 14:00:28 +0000249 HInstruction* hnot;
250 if (left->IsBooleanNot()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100251 hnot = new (GetGraph()->GetAllocator()) HBooleanNot(hbin, dex_pc);
Alexandre Rames9f980252016-02-05 14:00:28 +0000252 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100253 hnot = new (GetGraph()->GetAllocator()) HNot(type, hbin, dex_pc);
Alexandre Rames9f980252016-02-05 14:00:28 +0000254 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +0000255
256 op->GetBlock()->InsertInstructionBefore(hbin, op);
257 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, hnot);
258
259 RecordSimplification();
260 return true;
261 }
262
263 return false;
264}
265
Lena Djokicbc5460b2017-07-20 16:07:36 +0200266bool InstructionSimplifierVisitor::TryCombineVecMultiplyAccumulate(HVecMul* mul) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100267 DataType::Type type = mul->GetPackedType();
Lena Djokicbc5460b2017-07-20 16:07:36 +0200268 InstructionSet isa = codegen_->GetInstructionSet();
269 switch (isa) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000270 case InstructionSet::kArm64:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100271 if (!(type == DataType::Type::kUint8 ||
272 type == DataType::Type::kInt8 ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100273 type == DataType::Type::kUint16 ||
274 type == DataType::Type::kInt16 ||
275 type == DataType::Type::kInt32)) {
Lena Djokicbc5460b2017-07-20 16:07:36 +0200276 return false;
277 }
278 break;
Vladimir Marko33bff252017-11-01 14:35:42 +0000279 case InstructionSet::kMips:
280 case InstructionSet::kMips64:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100281 if (!(type == DataType::Type::kUint8 ||
282 type == DataType::Type::kInt8 ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100283 type == DataType::Type::kUint16 ||
284 type == DataType::Type::kInt16 ||
285 type == DataType::Type::kInt32 ||
286 type == DataType::Type::kInt64)) {
Lena Djokicbc5460b2017-07-20 16:07:36 +0200287 return false;
288 }
289 break;
290 default:
291 return false;
292 }
293
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100294 ArenaAllocator* allocator = mul->GetBlock()->GetGraph()->GetAllocator();
Lena Djokicbc5460b2017-07-20 16:07:36 +0200295
296 if (mul->HasOnlyOneNonEnvironmentUse()) {
297 HInstruction* use = mul->GetUses().front().GetUser();
298 if (use->IsVecAdd() || use->IsVecSub()) {
299 // Replace code looking like
300 // VECMUL tmp, x, y
301 // VECADD/SUB dst, acc, tmp
302 // with
303 // VECMULACC dst, acc, x, y
304 // Note that we do not want to (unconditionally) perform the merge when the
305 // multiplication has multiple uses and it can be merged in all of them.
306 // Multiple uses could happen on the same control-flow path, and we would
307 // then increase the amount of work. In the future we could try to evaluate
308 // whether all uses are on different control-flow paths (using dominance and
309 // reverse-dominance information) and only perform the merge when they are.
310 HInstruction* accumulator = nullptr;
311 HVecBinaryOperation* binop = use->AsVecBinaryOperation();
312 HInstruction* binop_left = binop->GetLeft();
313 HInstruction* binop_right = binop->GetRight();
314 // This is always true since the `HVecMul` has only one use (which is checked above).
315 DCHECK_NE(binop_left, binop_right);
316 if (binop_right == mul) {
317 accumulator = binop_left;
318 } else if (use->IsVecAdd()) {
319 DCHECK_EQ(binop_left, mul);
320 accumulator = binop_right;
321 }
322
323 HInstruction::InstructionKind kind =
324 use->IsVecAdd() ? HInstruction::kAdd : HInstruction::kSub;
325 if (accumulator != nullptr) {
326 HVecMultiplyAccumulate* mulacc =
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100327 new (allocator) HVecMultiplyAccumulate(allocator,
328 kind,
329 accumulator,
330 mul->GetLeft(),
331 mul->GetRight(),
332 binop->GetPackedType(),
333 binop->GetVectorLength(),
334 binop->GetDexPc());
Lena Djokicbc5460b2017-07-20 16:07:36 +0200335
336 binop->GetBlock()->ReplaceAndRemoveInstructionWith(binop, mulacc);
337 DCHECK(!mul->HasUses());
338 mul->GetBlock()->RemoveInstruction(mul);
339 return true;
340 }
341 }
342 }
343
344 return false;
345}
346
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000347void InstructionSimplifierVisitor::VisitShift(HBinaryOperation* instruction) {
348 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
Alexandre Rames50518442016-06-27 11:39:19 +0100349 HInstruction* shift_amount = instruction->GetRight();
350 HInstruction* value = instruction->GetLeft();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 int64_t implicit_mask = (value->GetType() == DataType::Type::kInt64)
Alexandre Rames50518442016-06-27 11:39:19 +0100353 ? kMaxLongShiftDistance
354 : kMaxIntShiftDistance;
355
356 if (shift_amount->IsConstant()) {
357 int64_t cst = Int64FromConstant(shift_amount->AsConstant());
Aart Bik50e20d52017-05-05 14:07:29 -0700358 int64_t masked_cst = cst & implicit_mask;
359 if (masked_cst == 0) {
Mark Mendellba56d062015-05-05 21:34:03 -0400360 // Replace code looking like
Alexandre Rames50518442016-06-27 11:39:19 +0100361 // SHL dst, value, 0
Mark Mendellba56d062015-05-05 21:34:03 -0400362 // with
Alexandre Rames50518442016-06-27 11:39:19 +0100363 // value
364 instruction->ReplaceWith(value);
Mark Mendellba56d062015-05-05 21:34:03 -0400365 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100366 RecordSimplification();
Alexandre Rames50518442016-06-27 11:39:19 +0100367 return;
Aart Bik50e20d52017-05-05 14:07:29 -0700368 } else if (masked_cst != cst) {
369 // Replace code looking like
370 // SHL dst, value, cst
371 // where cst exceeds maximum distance with the equivalent
372 // SHL dst, value, cst & implicit_mask
373 // (as defined by shift semantics). This ensures other
374 // optimizations do not need to special case for such situations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100375 DCHECK_EQ(shift_amount->GetType(), DataType::Type::kInt32);
Andreas Gampe3db70682018-12-26 15:12:03 -0800376 instruction->ReplaceInput(GetGraph()->GetIntConstant(masked_cst), /* index= */ 1);
Aart Bik50e20d52017-05-05 14:07:29 -0700377 RecordSimplification();
378 return;
Alexandre Rames50518442016-06-27 11:39:19 +0100379 }
380 }
381
382 // Shift operations implicitly mask the shift amount according to the type width. Get rid of
Vladimir Marko7033d492017-09-28 16:32:24 +0100383 // unnecessary And/Or/Xor/Add/Sub/TypeConversion operations on the shift amount that do not
384 // affect the relevant bits.
Alexandre Rames50518442016-06-27 11:39:19 +0100385 // Replace code looking like
Vladimir Marko7033d492017-09-28 16:32:24 +0100386 // AND adjusted_shift, shift, <superset of implicit mask>
387 // [OR/XOR/ADD/SUB adjusted_shift, shift, <value not overlapping with implicit mask>]
388 // [<conversion-from-integral-non-64-bit-type> adjusted_shift, shift]
389 // SHL dst, value, adjusted_shift
Alexandre Rames50518442016-06-27 11:39:19 +0100390 // with
391 // SHL dst, value, shift
Vladimir Marko7033d492017-09-28 16:32:24 +0100392 if (shift_amount->IsAnd() ||
393 shift_amount->IsOr() ||
394 shift_amount->IsXor() ||
395 shift_amount->IsAdd() ||
396 shift_amount->IsSub()) {
397 int64_t required_result = shift_amount->IsAnd() ? implicit_mask : 0;
398 HBinaryOperation* bin_op = shift_amount->AsBinaryOperation();
399 HConstant* mask = bin_op->GetConstantRight();
400 if (mask != nullptr && (Int64FromConstant(mask) & implicit_mask) == required_result) {
401 instruction->ReplaceInput(bin_op->GetLeastConstantLeft(), 1);
Alexandre Rames50518442016-06-27 11:39:19 +0100402 RecordSimplification();
Vladimir Marko7033d492017-09-28 16:32:24 +0100403 return;
404 }
405 } else if (shift_amount->IsTypeConversion()) {
406 DCHECK_NE(shift_amount->GetType(), DataType::Type::kBool); // We never convert to bool.
407 DataType::Type source_type = shift_amount->InputAt(0)->GetType();
408 // Non-integral and 64-bit source types require an explicit type conversion.
409 if (DataType::IsIntegralType(source_type) && !DataType::Is64BitType(source_type)) {
410 instruction->ReplaceInput(shift_amount->AsTypeConversion()->GetInput(), 1);
411 RecordSimplification();
412 return;
Mark Mendellba56d062015-05-05 21:34:03 -0400413 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000414 }
415}
416
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000417static bool IsSubRegBitsMinusOther(HSub* sub, size_t reg_bits, HInstruction* other) {
418 return (sub->GetRight() == other &&
419 sub->GetLeft()->IsConstant() &&
420 (Int64FromConstant(sub->GetLeft()->AsConstant()) & (reg_bits - 1)) == 0);
421}
422
423bool InstructionSimplifierVisitor::ReplaceRotateWithRor(HBinaryOperation* op,
424 HUShr* ushr,
425 HShl* shl) {
Roland Levillain22c49222016-03-18 14:04:28 +0000426 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr()) << op->DebugName();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100427 HRor* ror =
428 new (GetGraph()->GetAllocator()) HRor(ushr->GetType(), ushr->GetLeft(), ushr->GetRight());
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000429 op->GetBlock()->ReplaceAndRemoveInstructionWith(op, ror);
430 if (!ushr->HasUses()) {
431 ushr->GetBlock()->RemoveInstruction(ushr);
432 }
433 if (!ushr->GetRight()->HasUses()) {
434 ushr->GetRight()->GetBlock()->RemoveInstruction(ushr->GetRight());
435 }
436 if (!shl->HasUses()) {
437 shl->GetBlock()->RemoveInstruction(shl);
438 }
439 if (!shl->GetRight()->HasUses()) {
440 shl->GetRight()->GetBlock()->RemoveInstruction(shl->GetRight());
441 }
Alexandre Ramesc5809c32016-05-25 15:01:06 +0100442 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000443 return true;
444}
445
446// Try to replace a binary operation flanked by one UShr and one Shl with a bitfield rotation.
447bool InstructionSimplifierVisitor::TryReplaceWithRotate(HBinaryOperation* op) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000448 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
449 HInstruction* left = op->GetLeft();
450 HInstruction* right = op->GetRight();
451 // If we have an UShr and a Shl (in either order).
452 if ((left->IsUShr() && right->IsShl()) || (left->IsShl() && right->IsUShr())) {
453 HUShr* ushr = left->IsUShr() ? left->AsUShr() : right->AsUShr();
454 HShl* shl = left->IsShl() ? left->AsShl() : right->AsShl();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100455 DCHECK(DataType::IsIntOrLongType(ushr->GetType()));
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000456 if (ushr->GetType() == shl->GetType() &&
457 ushr->GetLeft() == shl->GetLeft()) {
458 if (ushr->GetRight()->IsConstant() && shl->GetRight()->IsConstant()) {
459 // Shift distances are both constant, try replacing with Ror if they
460 // add up to the register size.
461 return TryReplaceWithRotateConstantPattern(op, ushr, shl);
462 } else if (ushr->GetRight()->IsSub() || shl->GetRight()->IsSub()) {
463 // Shift distances are potentially of the form x and (reg_size - x).
464 return TryReplaceWithRotateRegisterSubPattern(op, ushr, shl);
465 } else if (ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg()) {
466 // Shift distances are potentially of the form d and -d.
467 return TryReplaceWithRotateRegisterNegPattern(op, ushr, shl);
468 }
469 }
470 }
471 return false;
472}
473
474// Try replacing code looking like (x >>> #rdist OP x << #ldist):
475// UShr dst, x, #rdist
476// Shl tmp, x, #ldist
477// OP dst, dst, tmp
478// or like (x >>> #rdist OP x << #-ldist):
479// UShr dst, x, #rdist
480// Shl tmp, x, #-ldist
481// OP dst, dst, tmp
482// with
483// Ror dst, x, #rdist
484bool InstructionSimplifierVisitor::TryReplaceWithRotateConstantPattern(HBinaryOperation* op,
485 HUShr* ushr,
486 HShl* shl) {
487 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100488 size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000489 size_t rdist = Int64FromConstant(ushr->GetRight()->AsConstant());
490 size_t ldist = Int64FromConstant(shl->GetRight()->AsConstant());
491 if (((ldist + rdist) & (reg_bits - 1)) == 0) {
492 ReplaceRotateWithRor(op, ushr, shl);
493 return true;
494 }
495 return false;
496}
497
498// Replace code looking like (x >>> -d OP x << d):
499// Neg neg, d
500// UShr dst, x, neg
501// Shl tmp, x, d
502// OP dst, dst, tmp
503// with
504// Neg neg, d
505// Ror dst, x, neg
506// *** OR ***
507// Replace code looking like (x >>> d OP x << -d):
508// UShr dst, x, d
509// Neg neg, d
510// Shl tmp, x, neg
511// OP dst, dst, tmp
512// with
513// Ror dst, x, d
514bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterNegPattern(HBinaryOperation* op,
515 HUShr* ushr,
516 HShl* shl) {
517 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
518 DCHECK(ushr->GetRight()->IsNeg() || shl->GetRight()->IsNeg());
519 bool neg_is_left = shl->GetRight()->IsNeg();
520 HNeg* neg = neg_is_left ? shl->GetRight()->AsNeg() : ushr->GetRight()->AsNeg();
521 // And the shift distance being negated is the distance being shifted the other way.
522 if (neg->InputAt(0) == (neg_is_left ? ushr->GetRight() : shl->GetRight())) {
523 ReplaceRotateWithRor(op, ushr, shl);
524 }
525 return false;
526}
527
528// Try replacing code looking like (x >>> d OP x << (#bits - d)):
529// UShr dst, x, d
530// Sub ld, #bits, d
531// Shl tmp, x, ld
532// OP dst, dst, tmp
533// with
534// Ror dst, x, d
535// *** OR ***
536// Replace code looking like (x >>> (#bits - d) OP x << d):
537// Sub rd, #bits, d
538// UShr dst, x, rd
539// Shl tmp, x, d
540// OP dst, dst, tmp
541// with
542// Neg neg, d
543// Ror dst, x, neg
544bool InstructionSimplifierVisitor::TryReplaceWithRotateRegisterSubPattern(HBinaryOperation* op,
545 HUShr* ushr,
546 HShl* shl) {
547 DCHECK(op->IsAdd() || op->IsXor() || op->IsOr());
548 DCHECK(ushr->GetRight()->IsSub() || shl->GetRight()->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100549 size_t reg_bits = DataType::Size(ushr->GetType()) * kBitsPerByte;
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000550 HInstruction* shl_shift = shl->GetRight();
551 HInstruction* ushr_shift = ushr->GetRight();
552 if ((shl_shift->IsSub() && IsSubRegBitsMinusOther(shl_shift->AsSub(), reg_bits, ushr_shift)) ||
553 (ushr_shift->IsSub() && IsSubRegBitsMinusOther(ushr_shift->AsSub(), reg_bits, shl_shift))) {
554 return ReplaceRotateWithRor(op, ushr, shl);
555 }
556 return false;
557}
558
Calin Juravle10e244f2015-01-26 18:54:32 +0000559void InstructionSimplifierVisitor::VisitNullCheck(HNullCheck* null_check) {
560 HInstruction* obj = null_check->InputAt(0);
561 if (!obj->CanBeNull()) {
562 null_check->ReplaceWith(obj);
563 null_check->GetBlock()->RemoveInstruction(null_check);
Calin Juravleacf735c2015-02-12 15:25:22 +0000564 if (stats_ != nullptr) {
565 stats_->RecordStat(MethodCompilationStat::kRemovedNullCheck);
566 }
567 }
568}
569
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100570bool InstructionSimplifierVisitor::CanEnsureNotNullAt(HInstruction* input, HInstruction* at) const {
571 if (!input->CanBeNull()) {
572 return true;
573 }
574
Vladimir Marko46817b82016-03-29 12:21:58 +0100575 for (const HUseListNode<HInstruction*>& use : input->GetUses()) {
576 HInstruction* user = use.GetUser();
577 if (user->IsNullCheck() && user->StrictlyDominates(at)) {
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100578 return true;
579 }
580 }
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100581
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +0100582 return false;
583}
584
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100585// Returns whether doing a type test between the class of `object` against `klass` has
586// a statically known outcome. The result of the test is stored in `outcome`.
Vladimir Marko175e7862018-03-27 09:03:13 +0000587static bool TypeCheckHasKnownOutcome(ReferenceTypeInfo class_rti,
588 HInstruction* object,
589 /*out*/bool* outcome) {
Calin Juravle2e768302015-07-28 14:41:11 +0000590 DCHECK(!object->IsNullConstant()) << "Null constants should be special cased";
591 ReferenceTypeInfo obj_rti = object->GetReferenceTypeInfo();
592 ScopedObjectAccess soa(Thread::Current());
593 if (!obj_rti.IsValid()) {
594 // We run the simplifier before the reference type propagation so type info might not be
595 // available.
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100596 return false;
Calin Juravleacf735c2015-02-12 15:25:22 +0000597 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100598
Calin Juravle98893e12015-10-02 21:05:03 +0100599 if (!class_rti.IsValid()) {
600 // Happens when the loaded class is unresolved.
601 return false;
602 }
603 DCHECK(class_rti.IsExact());
Calin Juravleacf735c2015-02-12 15:25:22 +0000604 if (class_rti.IsSupertypeOf(obj_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100605 *outcome = true;
606 return true;
607 } else if (obj_rti.IsExact()) {
608 // The test failed at compile time so will also fail at runtime.
609 *outcome = false;
610 return true;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100611 } else if (!class_rti.IsInterface()
612 && !obj_rti.IsInterface()
613 && !obj_rti.IsSupertypeOf(class_rti)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100614 // Different type hierarchy. The test will fail.
615 *outcome = false;
616 return true;
617 }
618 return false;
619}
620
621void InstructionSimplifierVisitor::VisitCheckCast(HCheckCast* check_cast) {
622 HInstruction* object = check_cast->InputAt(0);
Vladimir Marko175e7862018-03-27 09:03:13 +0000623 if (check_cast->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck &&
624 check_cast->GetTargetClass()->NeedsAccessCheck()) {
Calin Juravlee53fb552015-10-07 17:51:52 +0100625 // If we need to perform an access check we cannot remove the instruction.
626 return;
627 }
628
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100629 if (CanEnsureNotNullAt(object, check_cast)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100630 check_cast->ClearMustDoNullCheck();
631 }
632
633 if (object->IsNullConstant()) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000634 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700635 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100636 return;
637 }
638
Roland Levillain05e34f42018-05-24 13:19:05 +0000639 // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder
640 // the return value check with the `outcome` check, b/27651442.
Vladimir Markoa65ed302016-03-14 21:21:29 +0000641 bool outcome = false;
Vladimir Marko175e7862018-03-27 09:03:13 +0000642 if (TypeCheckHasKnownOutcome(check_cast->GetTargetClassRTI(), object, &outcome)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100643 if (outcome) {
644 check_cast->GetBlock()->RemoveInstruction(check_cast);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700645 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedCheckedCast);
Vladimir Marko175e7862018-03-27 09:03:13 +0000646 if (check_cast->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) {
647 HLoadClass* load_class = check_cast->GetTargetClass();
648 if (!load_class->HasUses()) {
649 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
650 // However, here we know that it cannot because the checkcast was successfull, hence
651 // the class was already loaded.
652 load_class->GetBlock()->RemoveInstruction(load_class);
653 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700654 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100655 } else {
656 // Don't do anything for exceptional cases for now. Ideally we should remove
657 // all instructions and blocks this instruction dominates.
658 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000659 }
660}
661
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100662void InstructionSimplifierVisitor::VisitInstanceOf(HInstanceOf* instruction) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100663 HInstruction* object = instruction->InputAt(0);
Vladimir Marko175e7862018-03-27 09:03:13 +0000664 if (instruction->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck &&
665 instruction->GetTargetClass()->NeedsAccessCheck()) {
Calin Juravlee53fb552015-10-07 17:51:52 +0100666 // If we need to perform an access check we cannot remove the instruction.
667 return;
668 }
669
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100670 bool can_be_null = true;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100671 if (CanEnsureNotNullAt(object, instruction)) {
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100672 can_be_null = false;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100673 instruction->ClearMustDoNullCheck();
674 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100675
676 HGraph* graph = GetGraph();
677 if (object->IsNullConstant()) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000678 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100679 instruction->ReplaceWith(graph->GetIntConstant(0));
680 instruction->GetBlock()->RemoveInstruction(instruction);
681 RecordSimplification();
682 return;
683 }
684
Roland Levillain05e34f42018-05-24 13:19:05 +0000685 // Historical note: The `outcome` was initialized to please Valgrind - the compiler can reorder
686 // the return value check with the `outcome` check, b/27651442.
Vladimir Marko24bd8952016-03-15 10:40:33 +0000687 bool outcome = false;
Vladimir Marko175e7862018-03-27 09:03:13 +0000688 if (TypeCheckHasKnownOutcome(instruction->GetTargetClassRTI(), object, &outcome)) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000689 MaybeRecordStat(stats_, MethodCompilationStat::kRemovedInstanceOf);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100690 if (outcome && can_be_null) {
691 // Type test will succeed, we just need a null test.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100692 HNotEqual* test = new (graph->GetAllocator()) HNotEqual(graph->GetNullConstant(), object);
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100693 instruction->GetBlock()->InsertInstructionBefore(test, instruction);
694 instruction->ReplaceWith(test);
695 } else {
696 // We've statically determined the result of the instanceof.
697 instruction->ReplaceWith(graph->GetIntConstant(outcome));
698 }
699 RecordSimplification();
700 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Marko175e7862018-03-27 09:03:13 +0000701 if (outcome && instruction->GetTypeCheckKind() != TypeCheckKind::kBitstringCheck) {
702 HLoadClass* load_class = instruction->GetTargetClass();
703 if (!load_class->HasUses()) {
704 // We cannot rely on DCE to remove the class because the `HLoadClass` thinks it can throw.
705 // However, here we know that it cannot because the instanceof check was successfull, hence
706 // the class was already loaded.
707 load_class->GetBlock()->RemoveInstruction(load_class);
708 }
Nicolas Geoffrayefa84682015-08-12 18:28:14 -0700709 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +0100710 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +0100711}
712
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100713void InstructionSimplifierVisitor::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100714 if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100715 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100716 instruction->ClearValueCanBeNull();
717 }
718}
719
720void InstructionSimplifierVisitor::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100721 if ((instruction->GetValue()->GetType() == DataType::Type::kReference)
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100722 && CanEnsureNotNullAt(instruction->GetValue(), instruction)) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100723 instruction->ClearValueCanBeNull();
724 }
725}
726
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100727static HCondition* GetOppositeConditionSwapOps(ArenaAllocator* allocator, HInstruction* cond) {
Anton Shaminbdd79352016-02-15 12:48:36 +0600728 HInstruction *lhs = cond->InputAt(0);
729 HInstruction *rhs = cond->InputAt(1);
730 switch (cond->GetKind()) {
731 case HInstruction::kEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100732 return new (allocator) HEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600733 case HInstruction::kNotEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100734 return new (allocator) HNotEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600735 case HInstruction::kLessThan:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100736 return new (allocator) HGreaterThan(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600737 case HInstruction::kLessThanOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100738 return new (allocator) HGreaterThanOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600739 case HInstruction::kGreaterThan:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100740 return new (allocator) HLessThan(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600741 case HInstruction::kGreaterThanOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100742 return new (allocator) HLessThanOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600743 case HInstruction::kBelow:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100744 return new (allocator) HAbove(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600745 case HInstruction::kBelowOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100746 return new (allocator) HAboveOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600747 case HInstruction::kAbove:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100748 return new (allocator) HBelow(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600749 case HInstruction::kAboveOrEqual:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100750 return new (allocator) HBelowOrEqual(rhs, lhs);
Anton Shaminbdd79352016-02-15 12:48:36 +0600751 default:
752 LOG(FATAL) << "Unknown ConditionType " << cond->GetKind();
Elliott Hughesc1896c92018-11-29 11:33:18 -0800753 UNREACHABLE();
Anton Shaminbdd79352016-02-15 12:48:36 +0600754 }
Anton Shaminbdd79352016-02-15 12:48:36 +0600755}
756
Aart Bik2767f4b2016-10-28 15:03:53 -0700757static bool CmpHasBoolType(HInstruction* input, HInstruction* cmp) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100758 if (input->GetType() == DataType::Type::kBool) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700759 return true; // input has direct boolean type
760 } else if (cmp->GetUses().HasExactlyOneElement()) {
761 // Comparison also has boolean type if both its input and the instruction
762 // itself feed into the same phi node.
763 HInstruction* user = cmp->GetUses().front().GetUser();
764 return user->IsPhi() && user->HasInput(input) && user->HasInput(cmp);
765 }
766 return false;
767}
768
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000769void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100770 HInstruction* input_const = equal->GetConstantRight();
771 if (input_const != nullptr) {
772 HInstruction* input_value = equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700773 if (CmpHasBoolType(input_value, equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100774 HBasicBlock* block = equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100775 // We are comparing the boolean to a constant which is of type int and can
776 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000777 if (input_const->AsIntConstant()->IsTrue()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100778 // Replace (bool_value == true) with bool_value
779 equal->ReplaceWith(input_value);
780 block->RemoveInstruction(equal);
781 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000782 } else if (input_const->AsIntConstant()->IsFalse()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700783 // Replace (bool_value == false) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500784 equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, equal));
785 block->RemoveInstruction(equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100786 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100787 } else {
788 // Replace (bool_value == integer_not_zero_nor_one_constant) with false
789 equal->ReplaceWith(GetGraph()->GetIntConstant(0));
790 block->RemoveInstruction(equal);
791 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100792 }
Mark Mendellc4701932015-04-10 13:18:51 -0400793 } else {
794 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100795 }
Mark Mendellc4701932015-04-10 13:18:51 -0400796 } else {
797 VisitCondition(equal);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100798 }
799}
800
David Brazdil0d13fee2015-04-17 14:52:19 +0100801void InstructionSimplifierVisitor::VisitNotEqual(HNotEqual* not_equal) {
802 HInstruction* input_const = not_equal->GetConstantRight();
803 if (input_const != nullptr) {
804 HInstruction* input_value = not_equal->GetLeastConstantLeft();
Aart Bik2767f4b2016-10-28 15:03:53 -0700805 if (CmpHasBoolType(input_value, not_equal) && input_const->IsIntConstant()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100806 HBasicBlock* block = not_equal->GetBlock();
Nicolas Geoffray3c4ab802015-06-19 11:42:07 +0100807 // We are comparing the boolean to a constant which is of type int and can
808 // be any constant.
Roland Levillain1a653882016-03-18 18:05:57 +0000809 if (input_const->AsIntConstant()->IsTrue()) {
Aart Bik2767f4b2016-10-28 15:03:53 -0700810 // Replace (bool_value != true) with !bool_value
Mark Mendellf6529172015-11-17 11:16:56 -0500811 not_equal->ReplaceWith(GetGraph()->InsertOppositeCondition(input_value, not_equal));
812 block->RemoveInstruction(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100813 RecordSimplification();
Roland Levillain1a653882016-03-18 18:05:57 +0000814 } else if (input_const->AsIntConstant()->IsFalse()) {
David Brazdil0d13fee2015-04-17 14:52:19 +0100815 // Replace (bool_value != false) with bool_value
David Brazdil0d13fee2015-04-17 14:52:19 +0100816 not_equal->ReplaceWith(input_value);
817 block->RemoveInstruction(not_equal);
818 RecordSimplification();
David Brazdil1e9ec052015-06-22 10:26:45 +0100819 } else {
820 // Replace (bool_value != integer_not_zero_nor_one_constant) with true
821 not_equal->ReplaceWith(GetGraph()->GetIntConstant(1));
822 block->RemoveInstruction(not_equal);
823 RecordSimplification();
David Brazdil0d13fee2015-04-17 14:52:19 +0100824 }
Mark Mendellc4701932015-04-10 13:18:51 -0400825 } else {
826 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100827 }
Mark Mendellc4701932015-04-10 13:18:51 -0400828 } else {
829 VisitCondition(not_equal);
David Brazdil0d13fee2015-04-17 14:52:19 +0100830 }
831}
832
833void InstructionSimplifierVisitor::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000834 HInstruction* input = bool_not->InputAt(0);
835 HInstruction* replace_with = nullptr;
836
837 if (input->IsIntConstant()) {
838 // Replace !(true/false) with false/true.
Roland Levillain1a653882016-03-18 18:05:57 +0000839 if (input->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000840 replace_with = GetGraph()->GetIntConstant(0);
841 } else {
Roland Levillain1a653882016-03-18 18:05:57 +0000842 DCHECK(input->AsIntConstant()->IsFalse()) << input->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000843 replace_with = GetGraph()->GetIntConstant(1);
844 }
845 } else if (input->IsBooleanNot()) {
846 // Replace (!(!bool_value)) with bool_value.
847 replace_with = input->InputAt(0);
848 } else if (input->IsCondition() &&
849 // Don't change FP compares. The definition of compares involving
850 // NaNs forces the compares to be done as written by the user.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100851 !DataType::IsFloatingPointType(input->InputAt(0)->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000852 // Replace condition with its opposite.
853 replace_with = GetGraph()->InsertOppositeCondition(input->AsCondition(), bool_not);
854 }
855
856 if (replace_with != nullptr) {
857 bool_not->ReplaceWith(replace_with);
David Brazdil0d13fee2015-04-17 14:52:19 +0100858 bool_not->GetBlock()->RemoveInstruction(bool_not);
David Brazdil74eb1b22015-12-14 11:44:01 +0000859 RecordSimplification();
860 }
861}
862
Aart Bik4f7dd342017-09-12 13:12:57 -0700863// Constructs a new ABS(x) node in the HIR.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100864static HInstruction* NewIntegralAbs(ArenaAllocator* allocator,
865 HInstruction* x,
866 HInstruction* cursor) {
Aart Bik2286da22018-03-22 10:50:22 -0700867 DataType::Type type = DataType::Kind(x->GetType());
Aart Bik3dad3412018-02-28 12:01:46 -0800868 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Aart Bik142b9132018-03-14 15:12:59 -0700869 HAbs* abs = new (allocator) HAbs(type, x, cursor->GetDexPc());
Aart Bik3dad3412018-02-28 12:01:46 -0800870 cursor->GetBlock()->InsertInstructionBefore(abs, cursor);
871 return abs;
Aart Bik4f7dd342017-09-12 13:12:57 -0700872}
873
Aart Bik142b9132018-03-14 15:12:59 -0700874// Constructs a new MIN/MAX(x, y) node in the HIR.
875static HInstruction* NewIntegralMinMax(ArenaAllocator* allocator,
876 HInstruction* x,
877 HInstruction* y,
878 HInstruction* cursor,
879 bool is_min) {
Aart Bik2286da22018-03-22 10:50:22 -0700880 DataType::Type type = DataType::Kind(x->GetType());
Aart Bik142b9132018-03-14 15:12:59 -0700881 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
882 HBinaryOperation* minmax = nullptr;
883 if (is_min) {
884 minmax = new (allocator) HMin(type, x, y, cursor->GetDexPc());
885 } else {
886 minmax = new (allocator) HMax(type, x, y, cursor->GetDexPc());
887 }
888 cursor->GetBlock()->InsertInstructionBefore(minmax, cursor);
889 return minmax;
890}
891
Aart Bik4f7dd342017-09-12 13:12:57 -0700892// Returns true if operands a and b consists of widening type conversions
893// (either explicit or implicit) to the given to_type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100894static bool AreLowerPrecisionArgs(DataType::Type to_type, HInstruction* a, HInstruction* b) {
Aart Bik4f7dd342017-09-12 13:12:57 -0700895 if (a->IsTypeConversion() && a->GetType() == to_type) {
896 a = a->InputAt(0);
897 }
898 if (b->IsTypeConversion() && b->GetType() == to_type) {
899 b = b->InputAt(0);
900 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100901 DataType::Type type1 = a->GetType();
902 DataType::Type type2 = b->GetType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100903 return (type1 == DataType::Type::kUint8 && type2 == DataType::Type::kUint8) ||
904 (type1 == DataType::Type::kInt8 && type2 == DataType::Type::kInt8) ||
905 (type1 == DataType::Type::kInt16 && type2 == DataType::Type::kInt16) ||
906 (type1 == DataType::Type::kUint16 && type2 == DataType::Type::kUint16) ||
907 (type1 == DataType::Type::kInt32 && type2 == DataType::Type::kInt32 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100908 to_type == DataType::Type::kInt64);
Aart Bik4f7dd342017-09-12 13:12:57 -0700909}
910
Aart Bik1d746de2018-03-28 16:30:02 -0700911// Returns an acceptable substitution for "a" on the select
912// construct "a <cmp> b ? c : .." during MIN/MAX recognition.
913static HInstruction* AllowInMinMax(IfCondition cmp,
914 HInstruction* a,
915 HInstruction* b,
916 HInstruction* c) {
917 int64_t value = 0;
918 if (IsInt64AndGet(b, /*out*/ &value) &&
919 (((cmp == kCondLT || cmp == kCondLE) && c->IsMax()) ||
920 ((cmp == kCondGT || cmp == kCondGE) && c->IsMin()))) {
921 HConstant* other = c->AsBinaryOperation()->GetConstantRight();
922 if (other != nullptr && a == c->AsBinaryOperation()->GetLeastConstantLeft()) {
923 int64_t other_value = Int64FromConstant(other);
924 bool is_max = (cmp == kCondLT || cmp == kCondLE);
925 // Allow the max for a < 100 ? max(a, -100) : ..
926 // or the min for a > -100 ? min(a, 100) : ..
927 if (is_max ? (value >= other_value) : (value <= other_value)) {
928 return c;
929 }
930 }
931 }
932 return nullptr;
933}
934
David Brazdil74eb1b22015-12-14 11:44:01 +0000935void InstructionSimplifierVisitor::VisitSelect(HSelect* select) {
936 HInstruction* replace_with = nullptr;
937 HInstruction* condition = select->GetCondition();
938 HInstruction* true_value = select->GetTrueValue();
939 HInstruction* false_value = select->GetFalseValue();
940
941 if (condition->IsBooleanNot()) {
942 // Change ((!cond) ? x : y) to (cond ? y : x).
943 condition = condition->InputAt(0);
944 std::swap(true_value, false_value);
945 select->ReplaceInput(false_value, 0);
946 select->ReplaceInput(true_value, 1);
947 select->ReplaceInput(condition, 2);
948 RecordSimplification();
949 }
950
951 if (true_value == false_value) {
952 // Replace (cond ? x : x) with (x).
953 replace_with = true_value;
954 } else if (condition->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000955 if (condition->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000956 // Replace (true ? x : y) with (x).
957 replace_with = true_value;
958 } else {
959 // Replace (false ? x : y) with (y).
Roland Levillain1a653882016-03-18 18:05:57 +0000960 DCHECK(condition->AsIntConstant()->IsFalse()) << condition->AsIntConstant()->GetValue();
David Brazdil74eb1b22015-12-14 11:44:01 +0000961 replace_with = false_value;
962 }
963 } else if (true_value->IsIntConstant() && false_value->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +0000964 if (true_value->AsIntConstant()->IsTrue() && false_value->AsIntConstant()->IsFalse()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000965 // Replace (cond ? true : false) with (cond).
966 replace_with = condition;
Roland Levillain1a653882016-03-18 18:05:57 +0000967 } else if (true_value->AsIntConstant()->IsFalse() && false_value->AsIntConstant()->IsTrue()) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000968 // Replace (cond ? false : true) with (!cond).
969 replace_with = GetGraph()->InsertOppositeCondition(condition, select);
970 }
Aart Bik4f7dd342017-09-12 13:12:57 -0700971 } else if (condition->IsCondition()) {
972 IfCondition cmp = condition->AsCondition()->GetCondition();
973 HInstruction* a = condition->InputAt(0);
974 HInstruction* b = condition->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 DataType::Type t_type = true_value->GetType();
976 DataType::Type f_type = false_value->GetType();
Aart Bik4f7dd342017-09-12 13:12:57 -0700977 // Here we have a <cmp> b ? true_value : false_value.
Aart Bik1d746de2018-03-28 16:30:02 -0700978 // Test if both values are compatible integral types (resulting MIN/MAX/ABS
979 // type will be int or long, like the condition). Replacements are general,
980 // but assume conditions prefer constants on the right.
Aart Bik2286da22018-03-22 10:50:22 -0700981 if (DataType::IsIntegralType(t_type) && DataType::Kind(t_type) == DataType::Kind(f_type)) {
Aart Bik1d746de2018-03-28 16:30:02 -0700982 // Allow a < 100 ? max(a, -100) : ..
983 // or a > -100 ? min(a, 100) : ..
984 // to use min/max instead of a to detect nested min/max expressions.
985 HInstruction* new_a = AllowInMinMax(cmp, a, b, true_value);
986 if (new_a != nullptr) {
987 a = new_a;
988 }
Aart Bik142b9132018-03-14 15:12:59 -0700989 // Try to replace typical integral MIN/MAX/ABS constructs.
990 if ((cmp == kCondLT || cmp == kCondLE || cmp == kCondGT || cmp == kCondGE) &&
991 ((a == true_value && b == false_value) ||
992 (b == true_value && a == false_value))) {
993 // Found a < b ? a : b (MIN) or a < b ? b : a (MAX)
994 // or a > b ? a : b (MAX) or a > b ? b : a (MIN).
995 bool is_min = (cmp == kCondLT || cmp == kCondLE) == (a == true_value);
996 replace_with = NewIntegralMinMax(GetGraph()->GetAllocator(), a, b, select, is_min);
Aart Bik1d746de2018-03-28 16:30:02 -0700997 } else if (((cmp == kCondLT || cmp == kCondLE) && true_value->IsNeg()) ||
998 ((cmp == kCondGT || cmp == kCondGE) && false_value->IsNeg())) {
999 bool negLeft = (cmp == kCondLT || cmp == kCondLE);
1000 HInstruction* the_negated = negLeft ? true_value->InputAt(0) : false_value->InputAt(0);
1001 HInstruction* not_negated = negLeft ? false_value : true_value;
1002 if (a == the_negated && a == not_negated && IsInt64Value(b, 0)) {
1003 // Found a < 0 ? -a : a
1004 // or a > 0 ? a : -a
1005 // which can be replaced by ABS(a).
1006 replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), a, select);
Aart Bik4f7dd342017-09-12 13:12:57 -07001007 }
1008 } else if (true_value->IsSub() && false_value->IsSub()) {
1009 HInstruction* true_sub1 = true_value->InputAt(0);
1010 HInstruction* true_sub2 = true_value->InputAt(1);
1011 HInstruction* false_sub1 = false_value->InputAt(0);
1012 HInstruction* false_sub2 = false_value->InputAt(1);
1013 if ((((cmp == kCondGT || cmp == kCondGE) &&
1014 (a == true_sub1 && b == true_sub2 && a == false_sub2 && b == false_sub1)) ||
1015 ((cmp == kCondLT || cmp == kCondLE) &&
1016 (a == true_sub2 && b == true_sub1 && a == false_sub1 && b == false_sub2))) &&
1017 AreLowerPrecisionArgs(t_type, a, b)) {
Aart Bik1d746de2018-03-28 16:30:02 -07001018 // Found a > b ? a - b : b - a
1019 // or a < b ? b - a : a - b
Aart Bik4f7dd342017-09-12 13:12:57 -07001020 // which can be replaced by ABS(a - b) for lower precision operands a, b.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 replace_with = NewIntegralAbs(GetGraph()->GetAllocator(), true_value, select);
Aart Bik4f7dd342017-09-12 13:12:57 -07001022 }
1023 }
1024 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001025 }
1026
1027 if (replace_with != nullptr) {
1028 select->ReplaceWith(replace_with);
1029 select->GetBlock()->RemoveInstruction(select);
1030 RecordSimplification();
1031 }
1032}
1033
1034void InstructionSimplifierVisitor::VisitIf(HIf* instruction) {
1035 HInstruction* condition = instruction->InputAt(0);
1036 if (condition->IsBooleanNot()) {
1037 // Swap successors if input is negated.
1038 instruction->ReplaceInput(condition->InputAt(0), 0);
1039 instruction->GetBlock()->SwapSuccessors();
David Brazdil0d13fee2015-04-17 14:52:19 +01001040 RecordSimplification();
1041 }
1042}
1043
Mingyao Yang0304e182015-01-30 16:41:29 -08001044void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) {
1045 HInstruction* input = instruction->InputAt(0);
1046 // If the array is a NewArray with constant size, replace the array length
1047 // with the constant instruction. This helps the bounds check elimination phase.
1048 if (input->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001049 input = input->AsNewArray()->GetLength();
Mingyao Yang0304e182015-01-30 16:41:29 -08001050 if (input->IsIntConstant()) {
1051 instruction->ReplaceWith(input);
1052 }
1053 }
1054}
1055
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +00001056void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001057 HInstruction* value = instruction->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001058 if (value->GetType() != DataType::Type::kReference) {
1059 return;
1060 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001061
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001062 if (CanEnsureNotNullAt(value, instruction)) {
1063 instruction->ClearValueCanBeNull();
1064 }
1065
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001066 if (value->IsArrayGet()) {
1067 if (value->AsArrayGet()->GetArray() == instruction->GetArray()) {
1068 // If the code is just swapping elements in the array, no need for a type check.
1069 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001070 return;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001071 }
1072 }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001073
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +01001074 if (value->IsNullConstant()) {
1075 instruction->ClearNeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001076 return;
Nicolas Geoffray9fdb31e2015-07-01 12:56:46 +01001077 }
1078
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001079 ScopedObjectAccess soa(Thread::Current());
1080 ReferenceTypeInfo array_rti = instruction->GetArray()->GetReferenceTypeInfo();
1081 ReferenceTypeInfo value_rti = value->GetReferenceTypeInfo();
1082 if (!array_rti.IsValid()) {
1083 return;
1084 }
1085
1086 if (value_rti.IsValid() && array_rti.CanArrayHold(value_rti)) {
1087 instruction->ClearNeedsTypeCheck();
1088 return;
1089 }
1090
1091 if (array_rti.IsObjectArray()) {
1092 if (array_rti.IsExact()) {
1093 instruction->ClearNeedsTypeCheck();
1094 return;
1095 }
1096 instruction->SetStaticTypeOfArrayIsObjectArray();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001097 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001098}
1099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001100static bool IsTypeConversionLossless(DataType::Type input_type, DataType::Type result_type) {
Aart Bikdab69072017-10-23 13:30:39 -07001101 // Make sure all implicit conversions have been simplified and no new ones have been introduced.
1102 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
1103 << input_type << "," << result_type;
Vladimir Markob52bbde2016-02-12 12:06:05 +00001104 // The conversion to a larger type is loss-less with the exception of two cases,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001105 // - conversion to the unsigned type Uint16, where we may lose some bits, and
Vladimir Markob52bbde2016-02-12 12:06:05 +00001106 // - conversion from float to long, the only FP to integral conversion with smaller FP type.
1107 // For integral to FP conversions this holds because the FP mantissa is large enough.
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001108 // Note: The size check excludes Uint8 as the result type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001109 return DataType::Size(result_type) > DataType::Size(input_type) &&
1110 result_type != DataType::Type::kUint16 &&
1111 !(result_type == DataType::Type::kInt64 && input_type == DataType::Type::kFloat32);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001112}
1113
Vladimir Marko61b92282017-10-11 13:23:17 +01001114static inline bool TryReplaceFieldOrArrayGetType(HInstruction* maybe_get, DataType::Type new_type) {
1115 if (maybe_get->IsInstanceFieldGet()) {
1116 maybe_get->AsInstanceFieldGet()->SetType(new_type);
1117 return true;
1118 } else if (maybe_get->IsStaticFieldGet()) {
1119 maybe_get->AsStaticFieldGet()->SetType(new_type);
1120 return true;
1121 } else if (maybe_get->IsArrayGet() && !maybe_get->AsArrayGet()->IsStringCharAt()) {
1122 maybe_get->AsArrayGet()->SetType(new_type);
1123 return true;
1124 } else {
1125 return false;
1126 }
1127}
1128
Mingyao Yang3bcb7512017-11-16 15:40:46 -08001129// The type conversion is only used for storing into a field/element of the
1130// same/narrower size.
1131static bool IsTypeConversionForStoringIntoNoWiderFieldOnly(HTypeConversion* type_conversion) {
1132 if (type_conversion->HasEnvironmentUses()) {
1133 return false;
1134 }
1135 DataType::Type input_type = type_conversion->GetInputType();
1136 DataType::Type result_type = type_conversion->GetResultType();
1137 if (!DataType::IsIntegralType(input_type) ||
1138 !DataType::IsIntegralType(result_type) ||
1139 input_type == DataType::Type::kInt64 ||
1140 result_type == DataType::Type::kInt64) {
1141 // Type conversion is needed if non-integer types are involved, or 64-bit
1142 // types are involved, which may use different number of registers.
1143 return false;
1144 }
1145 if (DataType::Size(input_type) >= DataType::Size(result_type)) {
1146 // Type conversion is not necessary when storing to a field/element of the
1147 // same/smaller size.
1148 } else {
1149 // We do not handle this case here.
1150 return false;
1151 }
1152
1153 // Check if the converted value is only used for storing into heap.
1154 for (const HUseListNode<HInstruction*>& use : type_conversion->GetUses()) {
1155 HInstruction* instruction = use.GetUser();
1156 if (instruction->IsInstanceFieldSet() &&
1157 instruction->AsInstanceFieldSet()->GetFieldType() == result_type) {
1158 DCHECK_EQ(instruction->AsInstanceFieldSet()->GetValue(), type_conversion);
1159 continue;
1160 }
1161 if (instruction->IsStaticFieldSet() &&
1162 instruction->AsStaticFieldSet()->GetFieldType() == result_type) {
1163 DCHECK_EQ(instruction->AsStaticFieldSet()->GetValue(), type_conversion);
1164 continue;
1165 }
1166 if (instruction->IsArraySet() &&
1167 instruction->AsArraySet()->GetComponentType() == result_type &&
1168 // not index use.
1169 instruction->AsArraySet()->GetIndex() != type_conversion) {
1170 DCHECK_EQ(instruction->AsArraySet()->GetValue(), type_conversion);
1171 continue;
1172 }
1173 // The use is not as a store value, or the field/element type is not the
1174 // same as the result_type, keep the type conversion.
1175 return false;
1176 }
1177 // Codegen automatically handles the type conversion during the store.
1178 return true;
1179}
1180
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001181void InstructionSimplifierVisitor::VisitTypeConversion(HTypeConversion* instruction) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001182 HInstruction* input = instruction->GetInput();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001183 DataType::Type input_type = input->GetType();
1184 DataType::Type result_type = instruction->GetResultType();
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01001185 if (instruction->IsImplicitConversion()) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001186 instruction->ReplaceWith(input);
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001187 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001188 RecordSimplification();
1189 return;
1190 }
1191
1192 if (input->IsTypeConversion()) {
1193 HTypeConversion* input_conversion = input->AsTypeConversion();
1194 HInstruction* original_input = input_conversion->GetInput();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001195 DataType::Type original_type = original_input->GetType();
Vladimir Markob52bbde2016-02-12 12:06:05 +00001196
1197 // When the first conversion is lossless, a direct conversion from the original type
1198 // to the final type yields the same result, even for a lossy second conversion, for
1199 // example float->double->int or int->double->float.
1200 bool is_first_conversion_lossless = IsTypeConversionLossless(original_type, input_type);
1201
1202 // For integral conversions, see if the first conversion loses only bits that the second
1203 // doesn't need, i.e. the final type is no wider than the intermediate. If so, direct
1204 // conversion yields the same result, for example long->int->short or int->char->short.
1205 bool integral_conversions_with_non_widening_second =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 DataType::IsIntegralType(input_type) &&
1207 DataType::IsIntegralType(original_type) &&
1208 DataType::IsIntegralType(result_type) &&
1209 DataType::Size(result_type) <= DataType::Size(input_type);
Vladimir Markob52bbde2016-02-12 12:06:05 +00001210
1211 if (is_first_conversion_lossless || integral_conversions_with_non_widening_second) {
1212 // If the merged conversion is implicit, do the simplification unconditionally.
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001213 if (DataType::IsTypeConversionImplicit(original_type, result_type)) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00001214 instruction->ReplaceWith(original_input);
1215 instruction->GetBlock()->RemoveInstruction(instruction);
1216 if (!input_conversion->HasUses()) {
1217 // Don't wait for DCE.
1218 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1219 }
1220 RecordSimplification();
1221 return;
1222 }
1223 // Otherwise simplify only if the first conversion has no other use.
1224 if (input_conversion->HasOnlyOneNonEnvironmentUse()) {
1225 input_conversion->ReplaceWith(original_input);
1226 input_conversion->GetBlock()->RemoveInstruction(input_conversion);
1227 RecordSimplification();
1228 return;
1229 }
1230 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001231 } else if (input->IsAnd() && DataType::IsIntegralType(result_type)) {
1232 DCHECK(DataType::IsIntegralType(input_type));
Vladimir Marko8428bd32016-02-12 16:53:57 +00001233 HAnd* input_and = input->AsAnd();
1234 HConstant* constant = input_and->GetConstantRight();
1235 if (constant != nullptr) {
1236 int64_t value = Int64FromConstant(constant);
1237 DCHECK_NE(value, -1); // "& -1" would have been optimized away in VisitAnd().
1238 size_t trailing_ones = CTZ(~static_cast<uint64_t>(value));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239 if (trailing_ones >= kBitsPerByte * DataType::Size(result_type)) {
Vladimir Marko8428bd32016-02-12 16:53:57 +00001240 // The `HAnd` is useless, for example in `(byte) (x & 0xff)`, get rid of it.
Vladimir Marko625090f2016-03-14 18:00:05 +00001241 HInstruction* original_input = input_and->GetLeastConstantLeft();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001242 if (DataType::IsTypeConversionImplicit(original_input->GetType(), result_type)) {
Vladimir Marko625090f2016-03-14 18:00:05 +00001243 instruction->ReplaceWith(original_input);
1244 instruction->GetBlock()->RemoveInstruction(instruction);
1245 RecordSimplification();
1246 return;
1247 } else if (input->HasOnlyOneNonEnvironmentUse()) {
1248 input_and->ReplaceWith(original_input);
1249 input_and->GetBlock()->RemoveInstruction(input_and);
1250 RecordSimplification();
1251 return;
1252 }
Vladimir Marko8428bd32016-02-12 16:53:57 +00001253 }
1254 }
Vladimir Marko61b92282017-10-11 13:23:17 +01001255 } else if (input->HasOnlyOneNonEnvironmentUse() &&
1256 ((input_type == DataType::Type::kInt8 && result_type == DataType::Type::kUint8) ||
1257 (input_type == DataType::Type::kUint8 && result_type == DataType::Type::kInt8) ||
1258 (input_type == DataType::Type::kInt16 && result_type == DataType::Type::kUint16) ||
1259 (input_type == DataType::Type::kUint16 && result_type == DataType::Type::kInt16))) {
1260 // Try to modify the type of the load to `result_type` and remove the explicit type conversion.
1261 if (TryReplaceFieldOrArrayGetType(input, result_type)) {
1262 instruction->ReplaceWith(input);
1263 instruction->GetBlock()->RemoveInstruction(instruction);
1264 RecordSimplification();
1265 return;
1266 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001267 }
Mingyao Yang3bcb7512017-11-16 15:40:46 -08001268
1269 if (IsTypeConversionForStoringIntoNoWiderFieldOnly(instruction)) {
1270 instruction->ReplaceWith(input);
1271 instruction->GetBlock()->RemoveInstruction(instruction);
1272 RecordSimplification();
1273 return;
1274 }
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001275}
1276
Aart Bikc6eec4b2018-03-29 17:22:00 -07001277void InstructionSimplifierVisitor::VisitAbs(HAbs* instruction) {
1278 HInstruction* input = instruction->GetInput();
1279 if (DataType::IsZeroExtension(input->GetType(), instruction->GetResultType())) {
1280 // Zero extension from narrow to wide can never set sign bit in the wider
1281 // operand, making the subsequent Abs redundant (e.g., abs(b & 0xff) for byte b).
1282 instruction->ReplaceWith(input);
1283 instruction->GetBlock()->RemoveInstruction(instruction);
1284 RecordSimplification();
1285 }
1286}
1287
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001288void InstructionSimplifierVisitor::VisitAdd(HAdd* instruction) {
1289 HConstant* input_cst = instruction->GetConstantRight();
1290 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001291 bool integral_type = DataType::IsIntegralType(instruction->GetType());
Roland Levillain1a653882016-03-18 18:05:57 +00001292 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001293 // Replace code looking like
1294 // ADD dst, src, 0
1295 // with
1296 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001297 // Note that we cannot optimize `x + 0.0` to `x` for floating-point. When
1298 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1299 // yields `-0.0`.
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001300 if (integral_type) {
Serguei Katkov115b53f2015-08-05 17:03:30 +06001301 instruction->ReplaceWith(input_other);
1302 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001303 RecordSimplification();
Serguei Katkov115b53f2015-08-05 17:03:30 +06001304 return;
1305 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001306 }
1307
1308 HInstruction* left = instruction->GetLeft();
1309 HInstruction* right = instruction->GetRight();
1310 bool left_is_neg = left->IsNeg();
1311 bool right_is_neg = right->IsNeg();
1312
1313 if (left_is_neg && right_is_neg) {
1314 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1315 return;
1316 }
1317 }
1318
1319 HNeg* neg = left_is_neg ? left->AsNeg() : right->AsNeg();
Andreas Gampe654698d2018-09-20 13:34:35 -07001320 if (left_is_neg != right_is_neg && neg->HasOnlyOneNonEnvironmentUse()) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001321 // Replace code looking like
1322 // NEG tmp, b
1323 // ADD dst, a, tmp
1324 // with
1325 // SUB dst, a, b
1326 // We do not perform the optimization if the input negation has environment
1327 // uses or multiple non-environment uses as it could lead to worse code. In
1328 // particular, we do not want the live range of `b` to be extended if we are
1329 // not sure the initial 'NEG' instruction can be removed.
1330 HInstruction* other = left_is_neg ? right : left;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001331 HSub* sub =
1332 new(GetGraph()->GetAllocator()) HSub(instruction->GetType(), other, neg->GetInput());
Alexandre Rames188d4312015-04-09 18:30:21 +01001333 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, sub);
1334 RecordSimplification();
1335 neg->GetBlock()->RemoveInstruction(neg);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001336 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001337 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001338
Anton Kirilove14dc862016-05-13 17:56:15 +01001339 if (TryReplaceWithRotate(instruction)) {
1340 return;
1341 }
1342
1343 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1344 // so no need to return.
1345 TryHandleAssociativeAndCommutativeOperation(instruction);
1346
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001347 if ((left->IsSub() || right->IsSub()) &&
Anton Kirilove14dc862016-05-13 17:56:15 +01001348 TrySubtractionChainSimplification(instruction)) {
1349 return;
1350 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06001351
1352 if (integral_type) {
1353 // Replace code patterns looking like
1354 // SUB dst1, x, y SUB dst1, x, y
1355 // ADD dst2, dst1, y ADD dst2, y, dst1
1356 // with
1357 // SUB dst1, x, y
1358 // ADD instruction is not needed in this case, we may use
1359 // one of inputs of SUB instead.
1360 if (left->IsSub() && left->InputAt(1) == right) {
1361 instruction->ReplaceWith(left->InputAt(0));
1362 RecordSimplification();
1363 instruction->GetBlock()->RemoveInstruction(instruction);
1364 return;
1365 } else if (right->IsSub() && right->InputAt(1) == left) {
1366 instruction->ReplaceWith(right->InputAt(0));
1367 RecordSimplification();
1368 instruction->GetBlock()->RemoveInstruction(instruction);
1369 return;
1370 }
1371 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001372}
1373
1374void InstructionSimplifierVisitor::VisitAnd(HAnd* instruction) {
Vladimir Marko61b92282017-10-11 13:23:17 +01001375 DCHECK(DataType::IsIntegralType(instruction->GetType()));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001376 HConstant* input_cst = instruction->GetConstantRight();
1377 HInstruction* input_other = instruction->GetLeastConstantLeft();
1378
Vladimir Marko452c1b62015-09-25 14:44:17 +01001379 if (input_cst != nullptr) {
1380 int64_t value = Int64FromConstant(input_cst);
Aart Bikdab69072017-10-23 13:30:39 -07001381 if (value == -1 ||
1382 // Similar cases under zero extension.
1383 (DataType::IsUnsignedType(input_other->GetType()) &&
1384 ((DataType::MaxValueOfIntegralType(input_other->GetType()) & ~value) == 0))) {
Vladimir Marko452c1b62015-09-25 14:44:17 +01001385 // Replace code looking like
1386 // AND dst, src, 0xFFF...FF
1387 // with
1388 // src
1389 instruction->ReplaceWith(input_other);
1390 instruction->GetBlock()->RemoveInstruction(instruction);
1391 RecordSimplification();
1392 return;
1393 }
Vladimir Markoc8fb2112017-10-03 11:37:52 +01001394 if (input_other->IsTypeConversion() &&
1395 input_other->GetType() == DataType::Type::kInt64 &&
1396 DataType::IsIntegralType(input_other->InputAt(0)->GetType()) &&
1397 IsInt<32>(value) &&
1398 input_other->HasOnlyOneNonEnvironmentUse()) {
1399 // The AND can be reordered before the TypeConversion. Replace
1400 // LongConstant cst, <32-bit-constant-sign-extended-to-64-bits>
1401 // TypeConversion<Int64> tmp, src
1402 // AND dst, tmp, cst
1403 // with
1404 // IntConstant cst, <32-bit-constant>
1405 // AND tmp, src, cst
1406 // TypeConversion<Int64> dst, tmp
1407 // This helps 32-bit targets and does not hurt 64-bit targets.
1408 // This also simplifies detection of other patterns, such as Uint8 loads.
1409 HInstruction* new_and_input = input_other->InputAt(0);
1410 // Implicit conversion Int64->Int64 would have been removed previously.
1411 DCHECK_NE(new_and_input->GetType(), DataType::Type::kInt64);
1412 HConstant* new_const = GetGraph()->GetConstant(DataType::Type::kInt32, value);
1413 HAnd* new_and =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001414 new (GetGraph()->GetAllocator()) HAnd(DataType::Type::kInt32, new_and_input, new_const);
Vladimir Markoc8fb2112017-10-03 11:37:52 +01001415 instruction->GetBlock()->InsertInstructionBefore(new_and, instruction);
1416 HTypeConversion* new_conversion =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001417 new (GetGraph()->GetAllocator()) HTypeConversion(DataType::Type::kInt64, new_and);
Vladimir Markoc8fb2112017-10-03 11:37:52 +01001418 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_conversion);
1419 input_other->GetBlock()->RemoveInstruction(input_other);
1420 RecordSimplification();
1421 // Try to process the new And now, do not wait for the next round of simplifications.
1422 instruction = new_and;
1423 input_other = new_and_input;
1424 }
Vladimir Marko452c1b62015-09-25 14:44:17 +01001425 // Eliminate And from UShr+And if the And-mask contains all the bits that
1426 // can be non-zero after UShr. Transform Shr+And to UShr if the And-mask
1427 // precisely clears the shifted-in sign bits.
1428 if ((input_other->IsUShr() || input_other->IsShr()) && input_other->InputAt(1)->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001429 size_t reg_bits = (instruction->GetResultType() == DataType::Type::kInt64) ? 64 : 32;
Vladimir Marko452c1b62015-09-25 14:44:17 +01001430 size_t shift = Int64FromConstant(input_other->InputAt(1)->AsConstant()) & (reg_bits - 1);
1431 size_t num_tail_bits_set = CTZ(value + 1);
1432 if ((num_tail_bits_set >= reg_bits - shift) && input_other->IsUShr()) {
1433 // This AND clears only bits known to be clear, for example "(x >>> 24) & 0xff".
1434 instruction->ReplaceWith(input_other);
1435 instruction->GetBlock()->RemoveInstruction(instruction);
1436 RecordSimplification();
1437 return;
1438 } else if ((num_tail_bits_set == reg_bits - shift) && IsPowerOfTwo(value + 1) &&
1439 input_other->HasOnlyOneNonEnvironmentUse()) {
1440 DCHECK(input_other->IsShr()); // For UShr, we would have taken the branch above.
1441 // Replace SHR+AND with USHR, for example "(x >> 24) & 0xff" -> "x >>> 24".
Vladimir Markoca6fff82017-10-03 14:49:14 +01001442 HUShr* ushr = new (GetGraph()->GetAllocator()) HUShr(instruction->GetType(),
Vladimir Marko69d310e2017-10-09 14:12:23 +01001443 input_other->InputAt(0),
1444 input_other->InputAt(1),
1445 input_other->GetDexPc());
Vladimir Marko452c1b62015-09-25 14:44:17 +01001446 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, ushr);
1447 input_other->GetBlock()->RemoveInstruction(input_other);
1448 RecordSimplification();
1449 return;
1450 }
1451 }
Vladimir Marko61b92282017-10-11 13:23:17 +01001452 if ((value == 0xff || value == 0xffff) && instruction->GetType() != DataType::Type::kInt64) {
1453 // Transform AND to a type conversion to Uint8/Uint16. If `input_other` is a field
1454 // or array Get with only a single use, short-circuit the subsequent simplification
1455 // of the Get+TypeConversion and change the Get's type to `new_type` instead.
1456 DataType::Type new_type = (value == 0xff) ? DataType::Type::kUint8 : DataType::Type::kUint16;
1457 DataType::Type find_type = (value == 0xff) ? DataType::Type::kInt8 : DataType::Type::kInt16;
1458 if (input_other->GetType() == find_type &&
1459 input_other->HasOnlyOneNonEnvironmentUse() &&
1460 TryReplaceFieldOrArrayGetType(input_other, new_type)) {
1461 instruction->ReplaceWith(input_other);
1462 instruction->GetBlock()->RemoveInstruction(instruction);
Aart Bikdab69072017-10-23 13:30:39 -07001463 } else if (DataType::IsTypeConversionImplicit(input_other->GetType(), new_type)) {
1464 instruction->ReplaceWith(input_other);
1465 instruction->GetBlock()->RemoveInstruction(instruction);
Vladimir Marko61b92282017-10-11 13:23:17 +01001466 } else {
1467 HTypeConversion* type_conversion = new (GetGraph()->GetAllocator()) HTypeConversion(
1468 new_type, input_other, instruction->GetDexPc());
1469 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, type_conversion);
1470 }
1471 RecordSimplification();
1472 return;
1473 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001474 }
1475
1476 // We assume that GVN has run before, so we only perform a pointer comparison.
1477 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001478 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001479 if (instruction->GetLeft() == instruction->GetRight()) {
1480 // Replace code looking like
1481 // AND dst, src, src
1482 // with
1483 // src
1484 instruction->ReplaceWith(instruction->GetLeft());
1485 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001486 RecordSimplification();
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001487 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001488 }
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001489
Anton Kirilove14dc862016-05-13 17:56:15 +01001490 if (TryDeMorganNegationFactoring(instruction)) {
1491 return;
1492 }
1493
1494 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1495 // so no need to return.
1496 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001497}
1498
Mark Mendellc4701932015-04-10 13:18:51 -04001499void InstructionSimplifierVisitor::VisitGreaterThan(HGreaterThan* condition) {
1500 VisitCondition(condition);
1501}
1502
1503void InstructionSimplifierVisitor::VisitGreaterThanOrEqual(HGreaterThanOrEqual* condition) {
1504 VisitCondition(condition);
1505}
1506
1507void InstructionSimplifierVisitor::VisitLessThan(HLessThan* condition) {
1508 VisitCondition(condition);
1509}
1510
1511void InstructionSimplifierVisitor::VisitLessThanOrEqual(HLessThanOrEqual* condition) {
1512 VisitCondition(condition);
1513}
1514
Anton Shaminbdd79352016-02-15 12:48:36 +06001515void InstructionSimplifierVisitor::VisitBelow(HBelow* condition) {
1516 VisitCondition(condition);
1517}
1518
1519void InstructionSimplifierVisitor::VisitBelowOrEqual(HBelowOrEqual* condition) {
1520 VisitCondition(condition);
1521}
1522
1523void InstructionSimplifierVisitor::VisitAbove(HAbove* condition) {
1524 VisitCondition(condition);
1525}
1526
1527void InstructionSimplifierVisitor::VisitAboveOrEqual(HAboveOrEqual* condition) {
1528 VisitCondition(condition);
1529}
Aart Bike9f37602015-10-09 11:15:55 -07001530
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001531// Recognize the following pattern:
1532// obj.getClass() ==/!= Foo.class
1533// And replace it with a constant value if the type of `obj` is statically known.
1534static bool RecognizeAndSimplifyClassCheck(HCondition* condition) {
1535 HInstruction* input_one = condition->InputAt(0);
1536 HInstruction* input_two = condition->InputAt(1);
1537 HLoadClass* load_class = input_one->IsLoadClass()
1538 ? input_one->AsLoadClass()
1539 : input_two->AsLoadClass();
1540 if (load_class == nullptr) {
1541 return false;
1542 }
1543
1544 ReferenceTypeInfo class_rti = load_class->GetLoadedClassRTI();
1545 if (!class_rti.IsValid()) {
1546 // Unresolved class.
1547 return false;
1548 }
1549
1550 HInstanceFieldGet* field_get = (load_class == input_one)
1551 ? input_two->AsInstanceFieldGet()
1552 : input_one->AsInstanceFieldGet();
1553 if (field_get == nullptr) {
1554 return false;
1555 }
1556
1557 HInstruction* receiver = field_get->InputAt(0);
1558 ReferenceTypeInfo receiver_type = receiver->GetReferenceTypeInfo();
1559 if (!receiver_type.IsExact()) {
1560 return false;
1561 }
1562
1563 {
1564 ScopedObjectAccess soa(Thread::Current());
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001565 ArtField* field = GetClassRoot<mirror::Object>()->GetInstanceField(0);
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001566 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
1567 if (field_get->GetFieldInfo().GetField() != field) {
1568 return false;
1569 }
1570
1571 // We can replace the compare.
1572 int value = 0;
1573 if (receiver_type.IsEqual(class_rti)) {
1574 value = condition->IsEqual() ? 1 : 0;
1575 } else {
1576 value = condition->IsNotEqual() ? 1 : 0;
1577 }
1578 condition->ReplaceWith(condition->GetBlock()->GetGraph()->GetIntConstant(value));
1579 return true;
1580 }
1581}
1582
Mark Mendellc4701932015-04-10 13:18:51 -04001583void InstructionSimplifierVisitor::VisitCondition(HCondition* condition) {
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001584 if (condition->IsEqual() || condition->IsNotEqual()) {
1585 if (RecognizeAndSimplifyClassCheck(condition)) {
1586 return;
1587 }
1588 }
1589
Anton Shaminbdd79352016-02-15 12:48:36 +06001590 // Reverse condition if left is constant. Our code generators prefer constant
1591 // on the right hand side.
1592 if (condition->GetLeft()->IsConstant() && !condition->GetRight()->IsConstant()) {
1593 HBasicBlock* block = condition->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001594 HCondition* replacement =
1595 GetOppositeConditionSwapOps(block->GetGraph()->GetAllocator(), condition);
Anton Shaminbdd79352016-02-15 12:48:36 +06001596 // If it is a fp we must set the opposite bias.
1597 if (replacement != nullptr) {
1598 if (condition->IsLtBias()) {
1599 replacement->SetBias(ComparisonBias::kGtBias);
1600 } else if (condition->IsGtBias()) {
1601 replacement->SetBias(ComparisonBias::kLtBias);
1602 }
1603 block->ReplaceAndRemoveInstructionWith(condition, replacement);
1604 RecordSimplification();
1605
1606 condition = replacement;
1607 }
1608 }
Mark Mendellc4701932015-04-10 13:18:51 -04001609
Mark Mendellc4701932015-04-10 13:18:51 -04001610 HInstruction* left = condition->GetLeft();
1611 HInstruction* right = condition->GetRight();
Anton Shaminbdd79352016-02-15 12:48:36 +06001612
1613 // Try to fold an HCompare into this HCondition.
1614
Mark Mendellc4701932015-04-10 13:18:51 -04001615 // We can only replace an HCondition which compares a Compare to 0.
1616 // Both 'dx' and 'jack' generate a compare to 0 when compiling a
1617 // condition with a long, float or double comparison as input.
1618 if (!left->IsCompare() || !right->IsConstant() || right->AsIntConstant()->GetValue() != 0) {
1619 // Conversion is not possible.
1620 return;
1621 }
1622
1623 // Is the Compare only used for this purpose?
Vladimir Marko46817b82016-03-29 12:21:58 +01001624 if (!left->GetUses().HasExactlyOneElement()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001625 // Someone else also wants the result of the compare.
1626 return;
1627 }
1628
Vladimir Marko46817b82016-03-29 12:21:58 +01001629 if (!left->GetEnvUses().empty()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001630 // There is a reference to the compare result in an environment. Do we really need it?
1631 if (GetGraph()->IsDebuggable()) {
1632 return;
1633 }
1634
1635 // We have to ensure that there are no deopt points in the sequence.
1636 if (left->HasAnyEnvironmentUseBefore(condition)) {
1637 return;
1638 }
1639 }
1640
1641 // Clean up any environment uses from the HCompare, if any.
1642 left->RemoveEnvironmentUsers();
1643
1644 // We have decided to fold the HCompare into the HCondition. Transfer the information.
1645 condition->SetBias(left->AsCompare()->GetBias());
1646
1647 // Replace the operands of the HCondition.
1648 condition->ReplaceInput(left->InputAt(0), 0);
1649 condition->ReplaceInput(left->InputAt(1), 1);
1650
1651 // Remove the HCompare.
1652 left->GetBlock()->RemoveInstruction(left);
1653
1654 RecordSimplification();
1655}
1656
Andreas Gampe9186ced2016-12-12 14:28:21 -08001657// Return whether x / divisor == x * (1.0f / divisor), for every float x.
1658static constexpr bool CanDivideByReciprocalMultiplyFloat(int32_t divisor) {
1659 // True, if the most significant bits of divisor are 0.
1660 return ((divisor & 0x7fffff) == 0);
1661}
1662
1663// Return whether x / divisor == x * (1.0 / divisor), for every double x.
1664static constexpr bool CanDivideByReciprocalMultiplyDouble(int64_t divisor) {
1665 // True, if the most significant bits of divisor are 0.
1666 return ((divisor & ((UINT64_C(1) << 52) - 1)) == 0);
1667}
1668
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001669void InstructionSimplifierVisitor::VisitDiv(HDiv* instruction) {
1670 HConstant* input_cst = instruction->GetConstantRight();
1671 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001673
1674 if ((input_cst != nullptr) && input_cst->IsOne()) {
1675 // Replace code looking like
1676 // DIV dst, src, 1
1677 // with
1678 // src
1679 instruction->ReplaceWith(input_other);
1680 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001681 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001682 return;
1683 }
1684
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001685 if ((input_cst != nullptr) && input_cst->IsMinusOne()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001686 // Replace code looking like
1687 // DIV dst, src, -1
1688 // with
1689 // NEG dst, src
1690 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001691 instruction, new (GetGraph()->GetAllocator()) HNeg(type, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001692 RecordSimplification();
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001693 return;
1694 }
1695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 if ((input_cst != nullptr) && DataType::IsFloatingPointType(type)) {
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001697 // Try replacing code looking like
1698 // DIV dst, src, constant
1699 // with
1700 // MUL dst, src, 1 / constant
1701 HConstant* reciprocal = nullptr;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 if (type == DataType::Type::kFloat64) {
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001703 double value = input_cst->AsDoubleConstant()->GetValue();
1704 if (CanDivideByReciprocalMultiplyDouble(bit_cast<int64_t, double>(value))) {
1705 reciprocal = GetGraph()->GetDoubleConstant(1.0 / value);
1706 }
1707 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 DCHECK_EQ(type, DataType::Type::kFloat32);
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001709 float value = input_cst->AsFloatConstant()->GetValue();
1710 if (CanDivideByReciprocalMultiplyFloat(bit_cast<int32_t, float>(value))) {
1711 reciprocal = GetGraph()->GetFloatConstant(1.0f / value);
1712 }
1713 }
1714
1715 if (reciprocal != nullptr) {
1716 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001717 instruction, new (GetGraph()->GetAllocator()) HMul(type, input_other, reciprocal));
Nicolas Geoffray0d221842015-04-27 08:53:46 +00001718 RecordSimplification();
1719 return;
1720 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001721 }
1722}
1723
1724void InstructionSimplifierVisitor::VisitMul(HMul* instruction) {
1725 HConstant* input_cst = instruction->GetConstantRight();
1726 HInstruction* input_other = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001727 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001728 HBasicBlock* block = instruction->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001729 ArenaAllocator* allocator = GetGraph()->GetAllocator();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001730
1731 if (input_cst == nullptr) {
1732 return;
1733 }
1734
1735 if (input_cst->IsOne()) {
1736 // Replace code looking like
1737 // MUL dst, src, 1
1738 // with
1739 // src
1740 instruction->ReplaceWith(input_other);
1741 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001742 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001743 return;
1744 }
1745
1746 if (input_cst->IsMinusOne() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747 (DataType::IsFloatingPointType(type) || DataType::IsIntOrLongType(type))) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001748 // Replace code looking like
1749 // MUL dst, src, -1
1750 // with
1751 // NEG dst, src
1752 HNeg* neg = new (allocator) HNeg(type, input_other);
1753 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001754 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001755 return;
1756 }
1757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 if (DataType::IsFloatingPointType(type) &&
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001759 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->GetValue() == 2.0f) ||
1760 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->GetValue() == 2.0))) {
1761 // Replace code looking like
1762 // FP_MUL dst, src, 2.0
1763 // with
1764 // FP_ADD dst, src, src
1765 // The 'int' and 'long' cases are handled below.
1766 block->ReplaceAndRemoveInstructionWith(instruction,
1767 new (allocator) HAdd(type, input_other, input_other));
Alexandre Rames188d4312015-04-09 18:30:21 +01001768 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001769 return;
1770 }
1771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001772 if (DataType::IsIntOrLongType(type)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001773 int64_t factor = Int64FromConstant(input_cst);
Serguei Katkov53849192015-04-20 14:22:27 +06001774 // Even though constant propagation also takes care of the zero case, other
1775 // optimizations can lead to having a zero multiplication.
1776 if (factor == 0) {
1777 // Replace code looking like
1778 // MUL dst, src, 0
1779 // with
1780 // 0
1781 instruction->ReplaceWith(input_cst);
1782 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001783 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001784 return;
Serguei Katkov53849192015-04-20 14:22:27 +06001785 } else if (IsPowerOfTwo(factor)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001786 // Replace code looking like
1787 // MUL dst, src, pow_of_2
1788 // with
1789 // SHL dst, src, log2(pow_of_2)
David Brazdil8d5b8b22015-03-24 10:51:52 +00001790 HIntConstant* shift = GetGraph()->GetIntConstant(WhichPowerOf2(factor));
Roland Levillain22c49222016-03-18 14:04:28 +00001791 HShl* shl = new (allocator) HShl(type, input_other, shift);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001792 block->ReplaceAndRemoveInstructionWith(instruction, shl);
Alexandre Rames188d4312015-04-09 18:30:21 +01001793 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001794 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001795 } else if (IsPowerOfTwo(factor - 1)) {
1796 // Transform code looking like
1797 // MUL dst, src, (2^n + 1)
1798 // into
1799 // SHL tmp, src, n
1800 // ADD dst, src, tmp
1801 HShl* shl = new (allocator) HShl(type,
1802 input_other,
1803 GetGraph()->GetIntConstant(WhichPowerOf2(factor - 1)));
1804 HAdd* add = new (allocator) HAdd(type, input_other, shl);
1805
1806 block->InsertInstructionBefore(shl, instruction);
1807 block->ReplaceAndRemoveInstructionWith(instruction, add);
1808 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001809 return;
Alexandre Rames38db7852015-11-20 15:02:45 +00001810 } else if (IsPowerOfTwo(factor + 1)) {
1811 // Transform code looking like
1812 // MUL dst, src, (2^n - 1)
1813 // into
1814 // SHL tmp, src, n
1815 // SUB dst, tmp, src
1816 HShl* shl = new (allocator) HShl(type,
1817 input_other,
1818 GetGraph()->GetIntConstant(WhichPowerOf2(factor + 1)));
1819 HSub* sub = new (allocator) HSub(type, shl, input_other);
1820
1821 block->InsertInstructionBefore(shl, instruction);
1822 block->ReplaceAndRemoveInstructionWith(instruction, sub);
1823 RecordSimplification();
Anton Kirilove14dc862016-05-13 17:56:15 +01001824 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001825 }
1826 }
Anton Kirilove14dc862016-05-13 17:56:15 +01001827
1828 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1829 // so no need to return.
1830 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001831}
1832
Alexandre Rames188d4312015-04-09 18:30:21 +01001833void InstructionSimplifierVisitor::VisitNeg(HNeg* instruction) {
1834 HInstruction* input = instruction->GetInput();
1835 if (input->IsNeg()) {
1836 // Replace code looking like
1837 // NEG tmp, src
1838 // NEG dst, tmp
1839 // with
1840 // src
1841 HNeg* previous_neg = input->AsNeg();
1842 instruction->ReplaceWith(previous_neg->GetInput());
1843 instruction->GetBlock()->RemoveInstruction(instruction);
1844 // We perform the optimization even if the input negation has environment
1845 // uses since it allows removing the current instruction. But we only delete
1846 // the input negation only if it is does not have any uses left.
1847 if (!previous_neg->HasUses()) {
1848 previous_neg->GetBlock()->RemoveInstruction(previous_neg);
1849 }
1850 RecordSimplification();
1851 return;
1852 }
1853
Serguei Katkov339dfc22015-04-20 12:29:32 +06001854 if (input->IsSub() && input->HasOnlyOneNonEnvironmentUse() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001855 !DataType::IsFloatingPointType(input->GetType())) {
Alexandre Rames188d4312015-04-09 18:30:21 +01001856 // Replace code looking like
1857 // SUB tmp, a, b
1858 // NEG dst, tmp
1859 // with
1860 // SUB dst, b, a
1861 // We do not perform the optimization if the input subtraction has
1862 // environment uses or multiple non-environment uses as it could lead to
1863 // worse code. In particular, we do not want the live ranges of `a` and `b`
1864 // to be extended if we are not sure the initial 'SUB' instruction can be
1865 // removed.
Serguei Katkov339dfc22015-04-20 12:29:32 +06001866 // We do not perform optimization for fp because we could lose the sign of zero.
Alexandre Rames188d4312015-04-09 18:30:21 +01001867 HSub* sub = input->AsSub();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 HSub* new_sub = new (GetGraph()->GetAllocator()) HSub(
1869 instruction->GetType(), sub->GetRight(), sub->GetLeft());
Alexandre Rames188d4312015-04-09 18:30:21 +01001870 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, new_sub);
1871 if (!sub->HasUses()) {
1872 sub->GetBlock()->RemoveInstruction(sub);
1873 }
1874 RecordSimplification();
1875 }
1876}
1877
1878void InstructionSimplifierVisitor::VisitNot(HNot* instruction) {
1879 HInstruction* input = instruction->GetInput();
1880 if (input->IsNot()) {
1881 // Replace code looking like
1882 // NOT tmp, src
1883 // NOT dst, tmp
1884 // with
1885 // src
1886 // We perform the optimization even if the input negation has environment
1887 // uses since it allows removing the current instruction. But we only delete
1888 // the input negation only if it is does not have any uses left.
1889 HNot* previous_not = input->AsNot();
1890 instruction->ReplaceWith(previous_not->GetInput());
1891 instruction->GetBlock()->RemoveInstruction(instruction);
1892 if (!previous_not->HasUses()) {
1893 previous_not->GetBlock()->RemoveInstruction(previous_not);
1894 }
1895 RecordSimplification();
1896 }
1897}
1898
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001899void InstructionSimplifierVisitor::VisitOr(HOr* instruction) {
1900 HConstant* input_cst = instruction->GetConstantRight();
1901 HInstruction* input_other = instruction->GetLeastConstantLeft();
1902
Roland Levillain1a653882016-03-18 18:05:57 +00001903 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001904 // Replace code looking like
1905 // OR dst, src, 0
1906 // with
1907 // src
1908 instruction->ReplaceWith(input_other);
1909 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001910 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001911 return;
1912 }
1913
1914 // We assume that GVN has run before, so we only perform a pointer comparison.
1915 // If for some reason the values are equal but the pointers are different, we
Alexandre Rames188d4312015-04-09 18:30:21 +01001916 // are still correct and only miss an optimization opportunity.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001917 if (instruction->GetLeft() == instruction->GetRight()) {
1918 // Replace code looking like
1919 // OR dst, src, src
1920 // with
1921 // src
1922 instruction->ReplaceWith(instruction->GetLeft());
1923 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001924 RecordSimplification();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001925 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001926 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001927
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00001928 if (TryDeMorganNegationFactoring(instruction)) return;
1929
Anton Kirilove14dc862016-05-13 17:56:15 +01001930 if (TryReplaceWithRotate(instruction)) {
1931 return;
1932 }
1933
1934 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
1935 // so no need to return.
1936 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001937}
1938
1939void InstructionSimplifierVisitor::VisitShl(HShl* instruction) {
1940 VisitShift(instruction);
1941}
1942
1943void InstructionSimplifierVisitor::VisitShr(HShr* instruction) {
1944 VisitShift(instruction);
1945}
1946
1947void InstructionSimplifierVisitor::VisitSub(HSub* instruction) {
1948 HConstant* input_cst = instruction->GetConstantRight();
1949 HInstruction* input_other = instruction->GetLeastConstantLeft();
1950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 DataType::Type type = instruction->GetType();
1952 if (DataType::IsFloatingPointType(type)) {
Serguei Katkov115b53f2015-08-05 17:03:30 +06001953 return;
1954 }
1955
Roland Levillain1a653882016-03-18 18:05:57 +00001956 if ((input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001957 // Replace code looking like
1958 // SUB dst, src, 0
1959 // with
1960 // src
Serguei Katkov115b53f2015-08-05 17:03:30 +06001961 // Note that we cannot optimize `x - 0.0` to `x` for floating-point. When
1962 // `x` is `-0.0`, the former expression yields `0.0`, while the later
1963 // yields `-0.0`.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001964 instruction->ReplaceWith(input_other);
1965 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01001966 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001967 return;
1968 }
1969
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001970 HBasicBlock* block = instruction->GetBlock();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001971 ArenaAllocator* allocator = GetGraph()->GetAllocator();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001972
Alexandre Rames188d4312015-04-09 18:30:21 +01001973 HInstruction* left = instruction->GetLeft();
1974 HInstruction* right = instruction->GetRight();
1975 if (left->IsConstant()) {
1976 if (Int64FromConstant(left->AsConstant()) == 0) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001977 // Replace code looking like
1978 // SUB dst, 0, src
1979 // with
1980 // NEG dst, src
Alexandre Rames188d4312015-04-09 18:30:21 +01001981 // Note that we cannot optimize `0.0 - x` to `-x` for floating-point. When
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001982 // `x` is `0.0`, the former expression yields `0.0`, while the later
1983 // yields `-0.0`.
Alexandre Rames188d4312015-04-09 18:30:21 +01001984 HNeg* neg = new (allocator) HNeg(type, right);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001985 block->ReplaceAndRemoveInstructionWith(instruction, neg);
Alexandre Rames188d4312015-04-09 18:30:21 +01001986 RecordSimplification();
1987 return;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001988 }
1989 }
Alexandre Rames188d4312015-04-09 18:30:21 +01001990
1991 if (left->IsNeg() && right->IsNeg()) {
1992 if (TryMoveNegOnInputsAfterBinop(instruction)) {
1993 return;
1994 }
1995 }
1996
1997 if (right->IsNeg() && right->HasOnlyOneNonEnvironmentUse()) {
1998 // Replace code looking like
1999 // NEG tmp, b
2000 // SUB dst, a, tmp
2001 // with
2002 // ADD dst, a, b
Vladimir Markoca6fff82017-10-03 14:49:14 +01002003 HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left, right->AsNeg()->GetInput());
Alexandre Rames188d4312015-04-09 18:30:21 +01002004 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, add);
2005 RecordSimplification();
2006 right->GetBlock()->RemoveInstruction(right);
2007 return;
2008 }
2009
2010 if (left->IsNeg() && left->HasOnlyOneNonEnvironmentUse()) {
2011 // Replace code looking like
2012 // NEG tmp, a
2013 // SUB dst, tmp, b
2014 // with
2015 // ADD tmp, a, b
2016 // NEG dst, tmp
2017 // The second version is not intrinsically better, but enables more
2018 // transformations.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002019 HAdd* add = new(GetGraph()->GetAllocator()) HAdd(type, left->AsNeg()->GetInput(), right);
Alexandre Rames188d4312015-04-09 18:30:21 +01002020 instruction->GetBlock()->InsertInstructionBefore(add, instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002021 HNeg* neg = new (GetGraph()->GetAllocator()) HNeg(instruction->GetType(), add);
Alexandre Rames188d4312015-04-09 18:30:21 +01002022 instruction->GetBlock()->InsertInstructionBefore(neg, instruction);
2023 instruction->ReplaceWith(neg);
2024 instruction->GetBlock()->RemoveInstruction(instruction);
2025 RecordSimplification();
2026 left->GetBlock()->RemoveInstruction(left);
Anton Kirilove14dc862016-05-13 17:56:15 +01002027 return;
2028 }
2029
2030 if (TrySubtractionChainSimplification(instruction)) {
2031 return;
Alexandre Rames188d4312015-04-09 18:30:21 +01002032 }
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06002033
2034 if (left->IsAdd()) {
2035 // Replace code patterns looking like
2036 // ADD dst1, x, y ADD dst1, x, y
2037 // SUB dst2, dst1, y SUB dst2, dst1, x
2038 // with
2039 // ADD dst1, x, y
2040 // SUB instruction is not needed in this case, we may use
2041 // one of inputs of ADD instead.
2042 // It is applicable to integral types only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 DCHECK(DataType::IsIntegralType(type));
Maxim Kazantsevd3278bd2016-07-12 15:55:33 +06002044 if (left->InputAt(1) == right) {
2045 instruction->ReplaceWith(left->InputAt(0));
2046 RecordSimplification();
2047 instruction->GetBlock()->RemoveInstruction(instruction);
2048 return;
2049 } else if (left->InputAt(0) == right) {
2050 instruction->ReplaceWith(left->InputAt(1));
2051 RecordSimplification();
2052 instruction->GetBlock()->RemoveInstruction(instruction);
2053 return;
2054 }
2055 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002056}
2057
2058void InstructionSimplifierVisitor::VisitUShr(HUShr* instruction) {
2059 VisitShift(instruction);
2060}
2061
2062void InstructionSimplifierVisitor::VisitXor(HXor* instruction) {
2063 HConstant* input_cst = instruction->GetConstantRight();
2064 HInstruction* input_other = instruction->GetLeastConstantLeft();
2065
Roland Levillain1a653882016-03-18 18:05:57 +00002066 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002067 // Replace code looking like
2068 // XOR dst, src, 0
2069 // with
2070 // src
2071 instruction->ReplaceWith(input_other);
2072 instruction->GetBlock()->RemoveInstruction(instruction);
Alexandre Ramesc5809c32016-05-25 15:01:06 +01002073 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002074 return;
2075 }
2076
Sebastien Hertz9837caf2016-08-01 11:09:50 +02002077 if ((input_cst != nullptr) && input_cst->IsOne()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002078 && input_other->GetType() == DataType::Type::kBool) {
Sebastien Hertz9837caf2016-08-01 11:09:50 +02002079 // Replace code looking like
2080 // XOR dst, src, 1
2081 // with
2082 // BOOLEAN_NOT dst, src
Vladimir Markoca6fff82017-10-03 14:49:14 +01002083 HBooleanNot* boolean_not = new (GetGraph()->GetAllocator()) HBooleanNot(input_other);
Sebastien Hertz9837caf2016-08-01 11:09:50 +02002084 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, boolean_not);
2085 RecordSimplification();
2086 return;
2087 }
2088
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002089 if ((input_cst != nullptr) && AreAllBitsSet(input_cst)) {
2090 // Replace code looking like
2091 // XOR dst, src, 0xFFF...FF
2092 // with
2093 // NOT dst, src
Vladimir Markoca6fff82017-10-03 14:49:14 +01002094 HNot* bitwise_not = new (GetGraph()->GetAllocator()) HNot(instruction->GetType(), input_other);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002095 instruction->GetBlock()->ReplaceAndRemoveInstructionWith(instruction, bitwise_not);
Alexandre Rames188d4312015-04-09 18:30:21 +01002096 RecordSimplification();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002097 return;
2098 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002099
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00002100 HInstruction* left = instruction->GetLeft();
2101 HInstruction* right = instruction->GetRight();
Alexandre Rames9f980252016-02-05 14:00:28 +00002102 if (((left->IsNot() && right->IsNot()) ||
2103 (left->IsBooleanNot() && right->IsBooleanNot())) &&
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00002104 left->HasOnlyOneNonEnvironmentUse() &&
2105 right->HasOnlyOneNonEnvironmentUse()) {
2106 // Replace code looking like
2107 // NOT nota, a
2108 // NOT notb, b
2109 // XOR dst, nota, notb
2110 // with
2111 // XOR dst, a, b
Alexandre Rames9f980252016-02-05 14:00:28 +00002112 instruction->ReplaceInput(left->InputAt(0), 0);
2113 instruction->ReplaceInput(right->InputAt(0), 1);
Alexandre Ramesca0e3a02016-02-03 10:54:07 +00002114 left->GetBlock()->RemoveInstruction(left);
2115 right->GetBlock()->RemoveInstruction(right);
2116 RecordSimplification();
2117 return;
2118 }
2119
Anton Kirilove14dc862016-05-13 17:56:15 +01002120 if (TryReplaceWithRotate(instruction)) {
2121 return;
2122 }
2123
2124 // TryHandleAssociativeAndCommutativeOperation() does not remove its input,
2125 // so no need to return.
2126 TryHandleAssociativeAndCommutativeOperation(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002127}
2128
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002129void InstructionSimplifierVisitor::SimplifyStringEquals(HInvoke* instruction) {
2130 HInstruction* argument = instruction->InputAt(1);
2131 HInstruction* receiver = instruction->InputAt(0);
2132 if (receiver == argument) {
2133 // Because String.equals is an instance call, the receiver is
2134 // a null check if we don't know it's null. The argument however, will
2135 // be the actual object. So we cannot end up in a situation where both
2136 // are equal but could be null.
2137 DCHECK(CanEnsureNotNullAt(argument, instruction));
2138 instruction->ReplaceWith(GetGraph()->GetIntConstant(1));
2139 instruction->GetBlock()->RemoveInstruction(instruction);
2140 } else {
2141 StringEqualsOptimizations optimizations(instruction);
2142 if (CanEnsureNotNullAt(argument, instruction)) {
2143 optimizations.SetArgumentNotNull();
2144 }
2145 ScopedObjectAccess soa(Thread::Current());
2146 ReferenceTypeInfo argument_rti = argument->GetReferenceTypeInfo();
2147 if (argument_rti.IsValid() && argument_rti.IsStringClass()) {
2148 optimizations.SetArgumentIsString();
2149 }
2150 }
2151}
2152
Roland Levillain22c49222016-03-18 14:04:28 +00002153void InstructionSimplifierVisitor::SimplifyRotate(HInvoke* invoke,
2154 bool is_left,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002155 DataType::Type type) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002156 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01002157 DCHECK_EQ(invoke->GetInvokeType(), InvokeType::kStatic);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002158 HInstruction* value = invoke->InputAt(0);
2159 HInstruction* distance = invoke->InputAt(1);
2160 // Replace the invoke with an HRor.
2161 if (is_left) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00002162 // Unconditionally set the type of the negated distance to `int`,
2163 // as shift and rotate operations expect a 32-bit (or narrower)
2164 // value for their distance input.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002165 distance = new (GetGraph()->GetAllocator()) HNeg(DataType::Type::kInt32, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002166 invoke->GetBlock()->InsertInstructionBefore(distance, invoke);
2167 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002168 HRor* ror = new (GetGraph()->GetAllocator()) HRor(type, value, distance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002169 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, ror);
2170 // Remove ClinitCheck and LoadClass, if possible.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002171 HInstruction* clinit = invoke->GetInputs().back();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002172 if (clinit->IsClinitCheck() && !clinit->HasUses()) {
2173 clinit->GetBlock()->RemoveInstruction(clinit);
2174 HInstruction* ldclass = clinit->InputAt(0);
2175 if (ldclass->IsLoadClass() && !ldclass->HasUses()) {
2176 ldclass->GetBlock()->RemoveInstruction(ldclass);
2177 }
2178 }
2179}
2180
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002181static bool IsArrayLengthOf(HInstruction* potential_length, HInstruction* potential_array) {
2182 if (potential_length->IsArrayLength()) {
2183 return potential_length->InputAt(0) == potential_array;
2184 }
2185
2186 if (potential_array->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00002187 return potential_array->AsNewArray()->GetLength() == potential_length;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002188 }
2189
2190 return false;
2191}
2192
2193void InstructionSimplifierVisitor::SimplifySystemArrayCopy(HInvoke* instruction) {
2194 HInstruction* source = instruction->InputAt(0);
2195 HInstruction* destination = instruction->InputAt(2);
2196 HInstruction* count = instruction->InputAt(4);
2197 SystemArrayCopyOptimizations optimizations(instruction);
2198 if (CanEnsureNotNullAt(source, instruction)) {
2199 optimizations.SetSourceIsNotNull();
2200 }
2201 if (CanEnsureNotNullAt(destination, instruction)) {
2202 optimizations.SetDestinationIsNotNull();
2203 }
2204 if (destination == source) {
2205 optimizations.SetDestinationIsSource();
2206 }
2207
2208 if (IsArrayLengthOf(count, source)) {
2209 optimizations.SetCountIsSourceLength();
2210 }
2211
2212 if (IsArrayLengthOf(count, destination)) {
2213 optimizations.SetCountIsDestinationLength();
2214 }
2215
2216 {
2217 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 DataType::Type source_component_type = DataType::Type::kVoid;
2219 DataType::Type destination_component_type = DataType::Type::kVoid;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002220 ReferenceTypeInfo destination_rti = destination->GetReferenceTypeInfo();
2221 if (destination_rti.IsValid()) {
2222 if (destination_rti.IsObjectArray()) {
2223 if (destination_rti.IsExact()) {
2224 optimizations.SetDoesNotNeedTypeCheck();
2225 }
2226 optimizations.SetDestinationIsTypedObjectArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002227 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002228 if (destination_rti.IsPrimitiveArrayClass()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002229 destination_component_type = DataTypeFromPrimitive(
2230 destination_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002231 optimizations.SetDestinationIsPrimitiveArray();
2232 } else if (destination_rti.IsNonPrimitiveArrayClass()) {
2233 optimizations.SetDestinationIsNonPrimitiveArray();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002234 }
2235 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002236 ReferenceTypeInfo source_rti = source->GetReferenceTypeInfo();
2237 if (source_rti.IsValid()) {
2238 if (destination_rti.IsValid() && destination_rti.CanArrayHoldValuesOf(source_rti)) {
2239 optimizations.SetDoesNotNeedTypeCheck();
2240 }
2241 if (source_rti.IsPrimitiveArrayClass()) {
2242 optimizations.SetSourceIsPrimitiveArray();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 source_component_type = DataTypeFromPrimitive(
2244 source_rti.GetTypeHandle()->GetComponentType()->GetPrimitiveType());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002245 } else if (source_rti.IsNonPrimitiveArrayClass()) {
2246 optimizations.SetSourceIsNonPrimitiveArray();
2247 }
2248 }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002249 // For primitive arrays, use their optimized ArtMethod implementations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 if ((source_component_type != DataType::Type::kVoid) &&
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002251 (source_component_type == destination_component_type)) {
2252 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2253 PointerSize image_size = class_linker->GetImagePointerSize();
2254 HInvokeStaticOrDirect* invoke = instruction->AsInvokeStaticOrDirect();
Vladimir Markod93e3742018-07-18 10:58:13 +01002255 ObjPtr<mirror::Class> system = invoke->GetResolvedMethod()->GetDeclaringClass();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002256 ArtMethod* method = nullptr;
2257 switch (source_component_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 case DataType::Type::kBool:
Vladimir Markoba118822017-06-12 15:41:56 +01002259 method = system->FindClassMethod("arraycopy", "([ZI[ZII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002260 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002261 case DataType::Type::kInt8:
Vladimir Markoba118822017-06-12 15:41:56 +01002262 method = system->FindClassMethod("arraycopy", "([BI[BII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002263 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 case DataType::Type::kUint16:
Vladimir Markoba118822017-06-12 15:41:56 +01002265 method = system->FindClassMethod("arraycopy", "([CI[CII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002266 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267 case DataType::Type::kInt16:
Vladimir Markoba118822017-06-12 15:41:56 +01002268 method = system->FindClassMethod("arraycopy", "([SI[SII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002269 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 case DataType::Type::kInt32:
Vladimir Markoba118822017-06-12 15:41:56 +01002271 method = system->FindClassMethod("arraycopy", "([II[III)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002272 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 case DataType::Type::kFloat32:
Vladimir Markoba118822017-06-12 15:41:56 +01002274 method = system->FindClassMethod("arraycopy", "([FI[FII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002275 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 case DataType::Type::kInt64:
Vladimir Markoba118822017-06-12 15:41:56 +01002277 method = system->FindClassMethod("arraycopy", "([JI[JII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002278 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 case DataType::Type::kFloat64:
Vladimir Markoba118822017-06-12 15:41:56 +01002280 method = system->FindClassMethod("arraycopy", "([DI[DII)V", image_size);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002281 break;
2282 default:
2283 LOG(FATAL) << "Unreachable";
2284 }
2285 DCHECK(method != nullptr);
Vladimir Markoba118822017-06-12 15:41:56 +01002286 DCHECK(method->IsStatic());
2287 DCHECK(method->GetDeclaringClass() == system);
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002288 invoke->SetResolvedMethod(method);
2289 // Sharpen the new invoke. Note that we do not update the dex method index of
2290 // the invoke, as we would need to look it up in the current dex file, and it
2291 // is unlikely that it exists. The most usual situation for such typed
2292 // arraycopy methods is a direct pointer to the boot image.
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01002293 invoke->SetDispatchInfo(HSharpening::SharpenInvokeStaticOrDirect(method, codegen_));
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002294 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002295 }
2296}
2297
Roland Levillaina5c4a402016-03-15 15:02:50 +00002298void InstructionSimplifierVisitor::SimplifyCompare(HInvoke* invoke,
2299 bool is_signum,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 DataType::Type type) {
Aart Bika19616e2016-02-01 18:57:58 -08002301 DCHECK(invoke->IsInvokeStaticOrDirect());
2302 uint32_t dex_pc = invoke->GetDexPc();
2303 HInstruction* left = invoke->InputAt(0);
2304 HInstruction* right;
Aart Bika19616e2016-02-01 18:57:58 -08002305 if (!is_signum) {
2306 right = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 } else if (type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08002308 right = GetGraph()->GetLongConstant(0);
2309 } else {
2310 right = GetGraph()->GetIntConstant(0);
2311 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002312 HCompare* compare = new (GetGraph()->GetAllocator())
Aart Bika19616e2016-02-01 18:57:58 -08002313 HCompare(type, left, right, ComparisonBias::kNoBias, dex_pc);
2314 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, compare);
2315}
2316
Aart Bik75a38b22016-02-17 10:41:50 -08002317void InstructionSimplifierVisitor::SimplifyIsNaN(HInvoke* invoke) {
2318 DCHECK(invoke->IsInvokeStaticOrDirect());
2319 uint32_t dex_pc = invoke->GetDexPc();
2320 // IsNaN(x) is the same as x != x.
2321 HInstruction* x = invoke->InputAt(0);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002322 HCondition* condition = new (GetGraph()->GetAllocator()) HNotEqual(x, x, dex_pc);
Aart Bik8ffc1fa2016-02-17 15:13:56 -08002323 condition->SetBias(ComparisonBias::kLtBias);
Aart Bik75a38b22016-02-17 10:41:50 -08002324 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, condition);
2325}
2326
Aart Bik2a6aad92016-02-25 11:32:32 -08002327void InstructionSimplifierVisitor::SimplifyFP2Int(HInvoke* invoke) {
2328 DCHECK(invoke->IsInvokeStaticOrDirect());
2329 uint32_t dex_pc = invoke->GetDexPc();
2330 HInstruction* x = invoke->InputAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 DataType::Type type = x->GetType();
Aart Bik2a6aad92016-02-25 11:32:32 -08002332 // Set proper bit pattern for NaN and replace intrinsic with raw version.
2333 HInstruction* nan;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002334 if (type == DataType::Type::kFloat64) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002335 nan = GetGraph()->GetLongConstant(0x7ff8000000000000L);
2336 invoke->SetIntrinsic(Intrinsics::kDoubleDoubleToRawLongBits,
2337 kNeedsEnvironmentOrCache,
2338 kNoSideEffects,
2339 kNoThrow);
2340 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 DCHECK_EQ(type, DataType::Type::kFloat32);
Aart Bik2a6aad92016-02-25 11:32:32 -08002342 nan = GetGraph()->GetIntConstant(0x7fc00000);
2343 invoke->SetIntrinsic(Intrinsics::kFloatFloatToRawIntBits,
2344 kNeedsEnvironmentOrCache,
2345 kNoSideEffects,
2346 kNoThrow);
2347 }
2348 // Test IsNaN(x), which is the same as x != x.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002349 HCondition* condition = new (GetGraph()->GetAllocator()) HNotEqual(x, x, dex_pc);
Aart Bik2a6aad92016-02-25 11:32:32 -08002350 condition->SetBias(ComparisonBias::kLtBias);
2351 invoke->GetBlock()->InsertInstructionBefore(condition, invoke->GetNext());
2352 // Select between the two.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002353 HInstruction* select = new (GetGraph()->GetAllocator()) HSelect(condition, nan, invoke, dex_pc);
Aart Bik2a6aad92016-02-25 11:32:32 -08002354 invoke->GetBlock()->InsertInstructionBefore(select, condition->GetNext());
2355 invoke->ReplaceWithExceptInReplacementAtIndex(select, 0); // false at index 0
2356}
2357
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002358void InstructionSimplifierVisitor::SimplifyStringCharAt(HInvoke* invoke) {
2359 HInstruction* str = invoke->InputAt(0);
2360 HInstruction* index = invoke->InputAt(1);
2361 uint32_t dex_pc = invoke->GetDexPc();
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002362 ArenaAllocator* allocator = GetGraph()->GetAllocator();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002363 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2364 // so create the HArrayLength, HBoundsCheck and HArrayGet.
Andreas Gampe3db70682018-12-26 15:12:03 -08002365 HArrayLength* length = new (allocator) HArrayLength(str, dex_pc, /* is_string_length= */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002366 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002367 HBoundsCheck* bounds_check = new (allocator) HBoundsCheck(
Andreas Gampe3db70682018-12-26 15:12:03 -08002368 index, length, dex_pc, /* is_string_char_at= */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002369 invoke->GetBlock()->InsertInstructionBefore(bounds_check, invoke);
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002370 HArrayGet* array_get = new (allocator) HArrayGet(str,
2371 bounds_check,
2372 DataType::Type::kUint16,
2373 SideEffects::None(), // Strings are immutable.
2374 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002375 /* is_string_char_at= */ true);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002376 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, array_get);
2377 bounds_check->CopyEnvironmentFrom(invoke->GetEnvironment());
2378 GetGraph()->SetHasBoundsChecks(true);
2379}
2380
Vladimir Markodce016e2016-04-28 13:10:02 +01002381void InstructionSimplifierVisitor::SimplifyStringIsEmptyOrLength(HInvoke* invoke) {
2382 HInstruction* str = invoke->InputAt(0);
2383 uint32_t dex_pc = invoke->GetDexPc();
2384 // We treat String as an array to allow DCE and BCE to seamlessly work on strings,
2385 // so create the HArrayLength.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002386 HArrayLength* length =
Andreas Gampe3db70682018-12-26 15:12:03 -08002387 new (GetGraph()->GetAllocator()) HArrayLength(str, dex_pc, /* is_string_length= */ true);
Vladimir Markodce016e2016-04-28 13:10:02 +01002388 HInstruction* replacement;
2389 if (invoke->GetIntrinsic() == Intrinsics::kStringIsEmpty) {
2390 // For String.isEmpty(), create the `HEqual` representing the `length == 0`.
2391 invoke->GetBlock()->InsertInstructionBefore(length, invoke);
2392 HIntConstant* zero = GetGraph()->GetIntConstant(0);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002393 HEqual* equal = new (GetGraph()->GetAllocator()) HEqual(length, zero, dex_pc);
Vladimir Markodce016e2016-04-28 13:10:02 +01002394 replacement = equal;
2395 } else {
2396 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringLength);
2397 replacement = length;
2398 }
2399 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, replacement);
2400}
2401
Vladimir Marko6fa44042018-03-19 18:42:49 +00002402void InstructionSimplifierVisitor::SimplifyStringIndexOf(HInvoke* invoke) {
2403 DCHECK(invoke->GetIntrinsic() == Intrinsics::kStringIndexOf ||
2404 invoke->GetIntrinsic() == Intrinsics::kStringIndexOfAfter);
2405 if (invoke->InputAt(0)->IsLoadString()) {
2406 HLoadString* load_string = invoke->InputAt(0)->AsLoadString();
2407 const DexFile& dex_file = load_string->GetDexFile();
2408 uint32_t utf16_length;
2409 const char* data =
2410 dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), &utf16_length);
2411 if (utf16_length == 0) {
2412 invoke->ReplaceWith(GetGraph()->GetIntConstant(-1));
2413 invoke->GetBlock()->RemoveInstruction(invoke);
2414 RecordSimplification();
2415 return;
2416 }
2417 if (utf16_length == 1 && invoke->GetIntrinsic() == Intrinsics::kStringIndexOf) {
2418 // Simplify to HSelect(HEquals(., load_string.charAt(0)), 0, -1).
2419 // If the sought character is supplementary, this gives the correct result, i.e. -1.
2420 uint32_t c = GetUtf16FromUtf8(&data);
2421 DCHECK_EQ(GetTrailingUtf16Char(c), 0u);
2422 DCHECK_EQ(GetLeadingUtf16Char(c), c);
2423 uint32_t dex_pc = invoke->GetDexPc();
2424 ArenaAllocator* allocator = GetGraph()->GetAllocator();
2425 HEqual* equal =
2426 new (allocator) HEqual(invoke->InputAt(1), GetGraph()->GetIntConstant(c), dex_pc);
2427 invoke->GetBlock()->InsertInstructionBefore(equal, invoke);
2428 HSelect* result = new (allocator) HSelect(equal,
2429 GetGraph()->GetIntConstant(0),
2430 GetGraph()->GetIntConstant(-1),
2431 dex_pc);
2432 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, result);
2433 RecordSimplification();
2434 return;
2435 }
2436 }
2437}
2438
Aart Bikff7d89c2016-11-07 08:49:28 -08002439// This method should only be used on intrinsics whose sole way of throwing an
2440// exception is raising a NPE when the nth argument is null. If that argument
2441// is provably non-null, we can clear the flag.
2442void InstructionSimplifierVisitor::SimplifyNPEOnArgN(HInvoke* invoke, size_t n) {
2443 HInstruction* arg = invoke->InputAt(n);
Aart Bik71bf7b42016-11-16 10:17:46 -08002444 if (invoke->CanThrow() && !arg->CanBeNull()) {
Aart Bikff7d89c2016-11-07 08:49:28 -08002445 invoke->SetCanThrow(false);
2446 }
2447}
2448
Aart Bik71bf7b42016-11-16 10:17:46 -08002449// Methods that return "this" can replace the returned value with the receiver.
2450void InstructionSimplifierVisitor::SimplifyReturnThis(HInvoke* invoke) {
2451 if (invoke->HasUses()) {
2452 HInstruction* receiver = invoke->InputAt(0);
2453 invoke->ReplaceWith(receiver);
2454 RecordSimplification();
2455 }
2456}
2457
2458// Helper method for StringBuffer escape analysis.
2459static bool NoEscapeForStringBufferReference(HInstruction* reference, HInstruction* user) {
2460 if (user->IsInvokeStaticOrDirect()) {
2461 // Any constructor on StringBuffer is okay.
Aart Bikab2270f2016-12-15 09:36:31 -08002462 return user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2463 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
Aart Bik71bf7b42016-11-16 10:17:46 -08002464 user->InputAt(0) == reference;
2465 } else if (user->IsInvokeVirtual()) {
2466 switch (user->AsInvokeVirtual()->GetIntrinsic()) {
2467 case Intrinsics::kStringBufferLength:
2468 case Intrinsics::kStringBufferToString:
2469 DCHECK_EQ(user->InputAt(0), reference);
2470 return true;
2471 case Intrinsics::kStringBufferAppend:
2472 // Returns "this", so only okay if no further uses.
2473 DCHECK_EQ(user->InputAt(0), reference);
2474 DCHECK_NE(user->InputAt(1), reference);
2475 return !user->HasUses();
2476 default:
2477 break;
2478 }
2479 }
2480 return false;
2481}
2482
Vladimir Marko552a1342017-10-31 10:56:47 +00002483static bool TryReplaceStringBuilderAppend(HInvoke* invoke) {
2484 DCHECK_EQ(invoke->GetIntrinsic(), Intrinsics::kStringBuilderToString);
2485 if (invoke->CanThrowIntoCatchBlock()) {
2486 return false;
2487 }
2488
2489 HBasicBlock* block = invoke->GetBlock();
2490 HInstruction* sb = invoke->InputAt(0);
2491
2492 // We support only a new StringBuilder, otherwise we cannot ensure that
2493 // the StringBuilder data does not need to be populated for other users.
2494 if (!sb->IsNewInstance()) {
2495 return false;
2496 }
2497
2498 // For now, we support only single-block recognition.
2499 // (Ternary operators feeding the append could be implemented.)
2500 for (const HUseListNode<HInstruction*>& use : sb->GetUses()) {
2501 if (use.GetUser()->GetBlock() != block) {
2502 return false;
2503 }
2504 }
2505
2506 // Collect args and check for unexpected uses.
2507 // We expect one call to a constructor with no arguments, one constructor fence (unless
2508 // eliminated), some number of append calls and one call to StringBuilder.toString().
2509 bool seen_constructor = false;
2510 bool seen_constructor_fence = false;
2511 bool seen_to_string = false;
2512 uint32_t format = 0u;
2513 uint32_t num_args = 0u;
2514 HInstruction* args[StringBuilderAppend::kMaxArgs]; // Added in reverse order.
2515 for (const HUseListNode<HInstruction*>& use : sb->GetUses()) {
2516 // The append pattern uses the StringBuilder only as the first argument.
2517 if (use.GetIndex() != 0u) {
2518 return false;
2519 }
2520 // We see the uses in reverse order because they are inserted at the front
2521 // of the singly-linked list, so the StringBuilder.append() must come first.
2522 HInstruction* user = use.GetUser();
2523 if (!seen_to_string) {
2524 if (user->IsInvokeVirtual() &&
2525 user->AsInvokeVirtual()->GetIntrinsic() == Intrinsics::kStringBuilderToString) {
2526 seen_to_string = true;
2527 continue;
2528 } else {
2529 return false;
2530 }
2531 }
2532 // Then we should see the arguments.
2533 if (user->IsInvokeVirtual()) {
2534 HInvokeVirtual* as_invoke_virtual = user->AsInvokeVirtual();
2535 DCHECK(!seen_constructor);
2536 DCHECK(!seen_constructor_fence);
2537 StringBuilderAppend::Argument arg;
2538 switch (as_invoke_virtual->GetIntrinsic()) {
2539 case Intrinsics::kStringBuilderAppendObject:
2540 // TODO: Unimplemented, needs to call String.valueOf().
2541 return false;
2542 case Intrinsics::kStringBuilderAppendString:
2543 arg = StringBuilderAppend::Argument::kString;
2544 break;
2545 case Intrinsics::kStringBuilderAppendCharArray:
2546 // TODO: Unimplemented, StringBuilder.append(char[]) can throw NPE and we would
2547 // not have the correct stack trace for it.
2548 return false;
2549 case Intrinsics::kStringBuilderAppendBoolean:
2550 arg = StringBuilderAppend::Argument::kBoolean;
2551 break;
2552 case Intrinsics::kStringBuilderAppendChar:
2553 arg = StringBuilderAppend::Argument::kChar;
2554 break;
2555 case Intrinsics::kStringBuilderAppendInt:
2556 arg = StringBuilderAppend::Argument::kInt;
2557 break;
2558 case Intrinsics::kStringBuilderAppendLong:
2559 arg = StringBuilderAppend::Argument::kLong;
2560 break;
2561 case Intrinsics::kStringBuilderAppendCharSequence: {
2562 ReferenceTypeInfo rti = user->AsInvokeVirtual()->InputAt(1)->GetReferenceTypeInfo();
2563 if (!rti.IsValid()) {
2564 return false;
2565 }
2566 ScopedObjectAccess soa(Thread::Current());
2567 Handle<mirror::Class> input_type = rti.GetTypeHandle();
2568 DCHECK(input_type != nullptr);
2569 if (input_type.Get() == GetClassRoot<mirror::String>()) {
2570 arg = StringBuilderAppend::Argument::kString;
2571 } else {
2572 // TODO: Check and implement for StringBuilder. We could find the StringBuilder's
2573 // internal char[] inconsistent with the length, or the string compression
2574 // of the result could be compromised with a concurrent modification, and
2575 // we would need to throw appropriate exceptions.
2576 return false;
2577 }
2578 break;
2579 }
2580 case Intrinsics::kStringBuilderAppendFloat:
2581 case Intrinsics::kStringBuilderAppendDouble:
2582 // TODO: Unimplemented, needs to call FloatingDecimal.getBinaryToASCIIConverter().
2583 return false;
2584 default: {
2585 return false;
2586 }
2587 }
2588 // Uses of the append return value should have been replaced with the first input.
2589 DCHECK(!as_invoke_virtual->HasUses());
2590 DCHECK(!as_invoke_virtual->HasEnvironmentUses());
2591 if (num_args == StringBuilderAppend::kMaxArgs) {
2592 return false;
2593 }
2594 format = (format << StringBuilderAppend::kBitsPerArg) | static_cast<uint32_t>(arg);
2595 args[num_args] = as_invoke_virtual->InputAt(1u);
2596 ++num_args;
2597 } else if (user->IsInvokeStaticOrDirect() &&
2598 user->AsInvokeStaticOrDirect()->GetResolvedMethod() != nullptr &&
2599 user->AsInvokeStaticOrDirect()->GetResolvedMethod()->IsConstructor() &&
2600 user->AsInvokeStaticOrDirect()->GetNumberOfArguments() == 1u) {
2601 // After arguments, we should see the constructor.
2602 // We accept only the constructor with no extra arguments.
2603 DCHECK(!seen_constructor);
2604 DCHECK(!seen_constructor_fence);
2605 seen_constructor = true;
2606 } else if (user->IsConstructorFence()) {
2607 // The last use we see is the constructor fence.
2608 DCHECK(seen_constructor);
2609 DCHECK(!seen_constructor_fence);
2610 seen_constructor_fence = true;
2611 } else {
2612 return false;
2613 }
2614 }
2615
2616 if (num_args == 0u) {
2617 return false;
2618 }
2619
2620 // Check environment uses.
2621 for (const HUseListNode<HEnvironment*>& use : sb->GetEnvUses()) {
2622 HInstruction* holder = use.GetUser()->GetHolder();
2623 if (holder->GetBlock() != block) {
2624 return false;
2625 }
2626 // Accept only calls on the StringBuilder (which shall all be removed).
2627 // TODO: Carve-out for const-string? Or rely on environment pruning (to be implemented)?
2628 if (holder->InputCount() == 0 || holder->InputAt(0) != sb) {
2629 return false;
2630 }
2631 }
2632
2633 // Create replacement instruction.
2634 HIntConstant* fmt = block->GetGraph()->GetIntConstant(static_cast<int32_t>(format));
2635 ArenaAllocator* allocator = block->GetGraph()->GetAllocator();
2636 HStringBuilderAppend* append =
2637 new (allocator) HStringBuilderAppend(fmt, num_args, allocator, invoke->GetDexPc());
2638 append->SetReferenceTypeInfo(invoke->GetReferenceTypeInfo());
2639 for (size_t i = 0; i != num_args; ++i) {
2640 append->SetArgumentAt(i, args[num_args - 1u - i]);
2641 }
2642 block->InsertInstructionBefore(append, invoke);
2643 invoke->ReplaceWith(append);
2644 // Copy environment, except for the StringBuilder uses.
2645 for (size_t i = 0, size = invoke->GetEnvironment()->Size(); i != size; ++i) {
2646 if (invoke->GetEnvironment()->GetInstructionAt(i) == sb) {
2647 invoke->GetEnvironment()->RemoveAsUserOfInput(i);
2648 invoke->GetEnvironment()->SetRawEnvAt(i, nullptr);
2649 }
2650 }
2651 append->CopyEnvironmentFrom(invoke->GetEnvironment());
2652 // Remove the old instruction.
2653 block->RemoveInstruction(invoke);
2654 // Remove the StringBuilder's uses and StringBuilder.
2655 while (sb->HasNonEnvironmentUses()) {
2656 block->RemoveInstruction(sb->GetUses().front().GetUser());
2657 }
2658 DCHECK(!sb->HasEnvironmentUses());
2659 block->RemoveInstruction(sb);
2660 return true;
2661}
2662
Aart Bik71bf7b42016-11-16 10:17:46 -08002663// Certain allocation intrinsics are not removed by dead code elimination
2664// because of potentially throwing an OOM exception or other side effects.
2665// This method removes such intrinsics when special circumstances allow.
2666void InstructionSimplifierVisitor::SimplifyAllocationIntrinsic(HInvoke* invoke) {
2667 if (!invoke->HasUses()) {
2668 // Instruction has no uses. If unsynchronized, we can remove right away, safely ignoring
2669 // the potential OOM of course. Otherwise, we must ensure the receiver object of this
2670 // call does not escape since only thread-local synchronization may be removed.
2671 bool is_synchronized = invoke->GetIntrinsic() == Intrinsics::kStringBufferToString;
2672 HInstruction* receiver = invoke->InputAt(0);
2673 if (!is_synchronized || DoesNotEscape(receiver, NoEscapeForStringBufferReference)) {
2674 invoke->GetBlock()->RemoveInstruction(invoke);
2675 RecordSimplification();
2676 }
Vladimir Marko552a1342017-10-31 10:56:47 +00002677 } else if (invoke->GetIntrinsic() == Intrinsics::kStringBuilderToString &&
2678 TryReplaceStringBuilderAppend(invoke)) {
2679 RecordSimplification();
Aart Bik71bf7b42016-11-16 10:17:46 -08002680 }
2681}
2682
Vladimir Markoca6fff82017-10-03 14:49:14 +01002683void InstructionSimplifierVisitor::SimplifyMemBarrier(HInvoke* invoke,
2684 MemBarrierKind barrier_kind) {
Aart Bik11932592016-03-08 12:42:25 -08002685 uint32_t dex_pc = invoke->GetDexPc();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002686 HMemoryBarrier* mem_barrier =
2687 new (GetGraph()->GetAllocator()) HMemoryBarrier(barrier_kind, dex_pc);
Aart Bik11932592016-03-08 12:42:25 -08002688 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, mem_barrier);
2689}
2690
Aart Bik1f8d51b2018-02-15 10:42:37 -08002691void InstructionSimplifierVisitor::SimplifyMin(HInvoke* invoke, DataType::Type type) {
2692 DCHECK(invoke->IsInvokeStaticOrDirect());
2693 HMin* min = new (GetGraph()->GetAllocator())
2694 HMin(type, invoke->InputAt(0), invoke->InputAt(1), invoke->GetDexPc());
2695 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, min);
2696}
2697
2698void InstructionSimplifierVisitor::SimplifyMax(HInvoke* invoke, DataType::Type type) {
2699 DCHECK(invoke->IsInvokeStaticOrDirect());
2700 HMax* max = new (GetGraph()->GetAllocator())
2701 HMax(type, invoke->InputAt(0), invoke->InputAt(1), invoke->GetDexPc());
2702 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, max);
2703}
2704
Aart Bik3dad3412018-02-28 12:01:46 -08002705void InstructionSimplifierVisitor::SimplifyAbs(HInvoke* invoke, DataType::Type type) {
2706 DCHECK(invoke->IsInvokeStaticOrDirect());
2707 HAbs* abs = new (GetGraph()->GetAllocator())
2708 HAbs(type, invoke->InputAt(0), invoke->GetDexPc());
2709 invoke->GetBlock()->ReplaceAndRemoveInstructionWith(invoke, abs);
2710}
2711
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01002712void InstructionSimplifierVisitor::VisitInvoke(HInvoke* instruction) {
Aart Bik2a6aad92016-02-25 11:32:32 -08002713 switch (instruction->GetIntrinsic()) {
2714 case Intrinsics::kStringEquals:
2715 SimplifyStringEquals(instruction);
2716 break;
2717 case Intrinsics::kSystemArrayCopy:
2718 SimplifySystemArrayCopy(instruction);
2719 break;
2720 case Intrinsics::kIntegerRotateRight:
Andreas Gampe3db70682018-12-26 15:12:03 -08002721 SimplifyRotate(instruction, /* is_left= */ false, DataType::Type::kInt32);
Roland Levillain22c49222016-03-18 14:04:28 +00002722 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002723 case Intrinsics::kLongRotateRight:
Andreas Gampe3db70682018-12-26 15:12:03 -08002724 SimplifyRotate(instruction, /* is_left= */ false, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002725 break;
2726 case Intrinsics::kIntegerRotateLeft:
Andreas Gampe3db70682018-12-26 15:12:03 -08002727 SimplifyRotate(instruction, /* is_left= */ true, DataType::Type::kInt32);
Roland Levillain22c49222016-03-18 14:04:28 +00002728 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002729 case Intrinsics::kLongRotateLeft:
Andreas Gampe3db70682018-12-26 15:12:03 -08002730 SimplifyRotate(instruction, /* is_left= */ true, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002731 break;
2732 case Intrinsics::kIntegerCompare:
Andreas Gampe3db70682018-12-26 15:12:03 -08002733 SimplifyCompare(instruction, /* is_signum= */ false, DataType::Type::kInt32);
Roland Levillaina5c4a402016-03-15 15:02:50 +00002734 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002735 case Intrinsics::kLongCompare:
Andreas Gampe3db70682018-12-26 15:12:03 -08002736 SimplifyCompare(instruction, /* is_signum= */ false, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002737 break;
2738 case Intrinsics::kIntegerSignum:
Andreas Gampe3db70682018-12-26 15:12:03 -08002739 SimplifyCompare(instruction, /* is_signum= */ true, DataType::Type::kInt32);
Roland Levillaina5c4a402016-03-15 15:02:50 +00002740 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002741 case Intrinsics::kLongSignum:
Andreas Gampe3db70682018-12-26 15:12:03 -08002742 SimplifyCompare(instruction, /* is_signum= */ true, DataType::Type::kInt64);
Aart Bik2a6aad92016-02-25 11:32:32 -08002743 break;
2744 case Intrinsics::kFloatIsNaN:
2745 case Intrinsics::kDoubleIsNaN:
2746 SimplifyIsNaN(instruction);
2747 break;
2748 case Intrinsics::kFloatFloatToIntBits:
2749 case Intrinsics::kDoubleDoubleToLongBits:
2750 SimplifyFP2Int(instruction);
2751 break;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002752 case Intrinsics::kStringCharAt:
2753 SimplifyStringCharAt(instruction);
2754 break;
Vladimir Markodce016e2016-04-28 13:10:02 +01002755 case Intrinsics::kStringIsEmpty:
2756 case Intrinsics::kStringLength:
2757 SimplifyStringIsEmptyOrLength(instruction);
2758 break;
Vladimir Marko6fa44042018-03-19 18:42:49 +00002759 case Intrinsics::kStringIndexOf:
2760 case Intrinsics::kStringIndexOfAfter:
2761 SimplifyStringIndexOf(instruction);
2762 break;
Aart Bikff7d89c2016-11-07 08:49:28 -08002763 case Intrinsics::kStringStringIndexOf:
2764 case Intrinsics::kStringStringIndexOfAfter:
2765 SimplifyNPEOnArgN(instruction, 1); // 0th has own NullCheck
2766 break;
Aart Bik71bf7b42016-11-16 10:17:46 -08002767 case Intrinsics::kStringBufferAppend:
Vladimir Markod4561172017-10-30 17:48:25 +00002768 case Intrinsics::kStringBuilderAppendObject:
2769 case Intrinsics::kStringBuilderAppendString:
2770 case Intrinsics::kStringBuilderAppendCharSequence:
2771 case Intrinsics::kStringBuilderAppendCharArray:
2772 case Intrinsics::kStringBuilderAppendBoolean:
2773 case Intrinsics::kStringBuilderAppendChar:
2774 case Intrinsics::kStringBuilderAppendInt:
2775 case Intrinsics::kStringBuilderAppendLong:
2776 case Intrinsics::kStringBuilderAppendFloat:
2777 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08002778 SimplifyReturnThis(instruction);
2779 break;
2780 case Intrinsics::kStringBufferToString:
2781 case Intrinsics::kStringBuilderToString:
2782 SimplifyAllocationIntrinsic(instruction);
2783 break;
Aart Bik11932592016-03-08 12:42:25 -08002784 case Intrinsics::kUnsafeLoadFence:
2785 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
2786 break;
2787 case Intrinsics::kUnsafeStoreFence:
2788 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
2789 break;
2790 case Intrinsics::kUnsafeFullFence:
2791 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
2792 break;
Orion Hodson4a4610a2017-09-28 16:57:55 +01002793 case Intrinsics::kVarHandleFullFence:
2794 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyAny);
2795 break;
2796 case Intrinsics::kVarHandleAcquireFence:
2797 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
2798 break;
2799 case Intrinsics::kVarHandleReleaseFence:
2800 SimplifyMemBarrier(instruction, MemBarrierKind::kAnyStore);
2801 break;
2802 case Intrinsics::kVarHandleLoadLoadFence:
2803 SimplifyMemBarrier(instruction, MemBarrierKind::kLoadAny);
2804 break;
2805 case Intrinsics::kVarHandleStoreStoreFence:
2806 SimplifyMemBarrier(instruction, MemBarrierKind::kStoreStore);
2807 break;
Aart Bik1f8d51b2018-02-15 10:42:37 -08002808 case Intrinsics::kMathMinIntInt:
2809 SimplifyMin(instruction, DataType::Type::kInt32);
2810 break;
2811 case Intrinsics::kMathMinLongLong:
2812 SimplifyMin(instruction, DataType::Type::kInt64);
2813 break;
2814 case Intrinsics::kMathMinFloatFloat:
2815 SimplifyMin(instruction, DataType::Type::kFloat32);
2816 break;
2817 case Intrinsics::kMathMinDoubleDouble:
2818 SimplifyMin(instruction, DataType::Type::kFloat64);
2819 break;
2820 case Intrinsics::kMathMaxIntInt:
2821 SimplifyMax(instruction, DataType::Type::kInt32);
2822 break;
2823 case Intrinsics::kMathMaxLongLong:
2824 SimplifyMax(instruction, DataType::Type::kInt64);
2825 break;
2826 case Intrinsics::kMathMaxFloatFloat:
2827 SimplifyMax(instruction, DataType::Type::kFloat32);
2828 break;
2829 case Intrinsics::kMathMaxDoubleDouble:
2830 SimplifyMax(instruction, DataType::Type::kFloat64);
2831 break;
Aart Bik3dad3412018-02-28 12:01:46 -08002832 case Intrinsics::kMathAbsInt:
2833 SimplifyAbs(instruction, DataType::Type::kInt32);
2834 break;
2835 case Intrinsics::kMathAbsLong:
2836 SimplifyAbs(instruction, DataType::Type::kInt64);
2837 break;
2838 case Intrinsics::kMathAbsFloat:
2839 SimplifyAbs(instruction, DataType::Type::kFloat32);
2840 break;
2841 case Intrinsics::kMathAbsDouble:
2842 SimplifyAbs(instruction, DataType::Type::kFloat64);
2843 break;
Aart Bik2a6aad92016-02-25 11:32:32 -08002844 default:
2845 break;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01002846 }
2847}
2848
Aart Bikbb245d12015-10-19 11:05:03 -07002849void InstructionSimplifierVisitor::VisitDeoptimize(HDeoptimize* deoptimize) {
2850 HInstruction* cond = deoptimize->InputAt(0);
2851 if (cond->IsConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002852 if (cond->AsIntConstant()->IsFalse()) {
Aart Bikbb245d12015-10-19 11:05:03 -07002853 // Never deopt: instruction can be removed.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002854 if (deoptimize->GuardsAnInput()) {
2855 deoptimize->ReplaceWith(deoptimize->GuardedInput());
2856 }
Aart Bikbb245d12015-10-19 11:05:03 -07002857 deoptimize->GetBlock()->RemoveInstruction(deoptimize);
2858 } else {
2859 // Always deopt.
2860 }
2861 }
2862}
2863
Anton Kirilove14dc862016-05-13 17:56:15 +01002864// Replace code looking like
2865// OP y, x, const1
2866// OP z, y, const2
2867// with
2868// OP z, x, const3
2869// where OP is both an associative and a commutative operation.
2870bool InstructionSimplifierVisitor::TryHandleAssociativeAndCommutativeOperation(
2871 HBinaryOperation* instruction) {
2872 DCHECK(instruction->IsCommutative());
2873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 if (!DataType::IsIntegralType(instruction->GetType())) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002875 return false;
2876 }
2877
2878 HInstruction* left = instruction->GetLeft();
2879 HInstruction* right = instruction->GetRight();
2880 // Variable names as described above.
2881 HConstant* const2;
2882 HBinaryOperation* y;
2883
Vladimir Marko0dcccd82018-05-04 13:32:25 +01002884 if (instruction->GetKind() == left->GetKind() && right->IsConstant()) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002885 const2 = right->AsConstant();
2886 y = left->AsBinaryOperation();
Vladimir Marko0dcccd82018-05-04 13:32:25 +01002887 } else if (left->IsConstant() && instruction->GetKind() == right->GetKind()) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002888 const2 = left->AsConstant();
2889 y = right->AsBinaryOperation();
2890 } else {
2891 // The node does not match the pattern.
2892 return false;
2893 }
2894
2895 // If `y` has more than one use, we do not perform the optimization
2896 // because it might increase code size (e.g. if the new constant is
2897 // no longer encodable as an immediate operand in the target ISA).
2898 if (!y->HasOnlyOneNonEnvironmentUse()) {
2899 return false;
2900 }
2901
2902 // GetConstantRight() can return both left and right constants
2903 // for commutative operations.
2904 HConstant* const1 = y->GetConstantRight();
2905 if (const1 == nullptr) {
2906 return false;
2907 }
2908
2909 instruction->ReplaceInput(const1, 0);
2910 instruction->ReplaceInput(const2, 1);
2911 HConstant* const3 = instruction->TryStaticEvaluation();
2912 DCHECK(const3 != nullptr);
2913 instruction->ReplaceInput(y->GetLeastConstantLeft(), 0);
2914 instruction->ReplaceInput(const3, 1);
2915 RecordSimplification();
2916 return true;
2917}
2918
2919static HBinaryOperation* AsAddOrSub(HInstruction* binop) {
2920 return (binop->IsAdd() || binop->IsSub()) ? binop->AsBinaryOperation() : nullptr;
2921}
2922
2923// Helper function that performs addition statically, considering the result type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002924static int64_t ComputeAddition(DataType::Type type, int64_t x, int64_t y) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002925 // Use the Compute() method for consistency with TryStaticEvaluation().
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 if (type == DataType::Type::kInt32) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002927 return HAdd::Compute<int32_t>(x, y);
2928 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 DCHECK_EQ(type, DataType::Type::kInt64);
Anton Kirilove14dc862016-05-13 17:56:15 +01002930 return HAdd::Compute<int64_t>(x, y);
2931 }
2932}
2933
2934// Helper function that handles the child classes of HConstant
2935// and returns an integer with the appropriate sign.
2936static int64_t GetValue(HConstant* constant, bool is_negated) {
2937 int64_t ret = Int64FromConstant(constant);
2938 return is_negated ? -ret : ret;
2939}
2940
2941// Replace code looking like
2942// OP1 y, x, const1
2943// OP2 z, y, const2
2944// with
2945// OP3 z, x, const3
2946// where OPx is either ADD or SUB, and at least one of OP{1,2} is SUB.
2947bool InstructionSimplifierVisitor::TrySubtractionChainSimplification(
2948 HBinaryOperation* instruction) {
2949 DCHECK(instruction->IsAdd() || instruction->IsSub()) << instruction->DebugName();
2950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002951 DataType::Type type = instruction->GetType();
2952 if (!DataType::IsIntegralType(type)) {
Anton Kirilove14dc862016-05-13 17:56:15 +01002953 return false;
2954 }
2955
2956 HInstruction* left = instruction->GetLeft();
2957 HInstruction* right = instruction->GetRight();
2958 // Variable names as described above.
2959 HConstant* const2 = right->IsConstant() ? right->AsConstant() : left->AsConstant();
2960 if (const2 == nullptr) {
2961 return false;
2962 }
2963
2964 HBinaryOperation* y = (AsAddOrSub(left) != nullptr)
2965 ? left->AsBinaryOperation()
2966 : AsAddOrSub(right);
2967 // If y has more than one use, we do not perform the optimization because
2968 // it might increase code size (e.g. if the new constant is no longer
2969 // encodable as an immediate operand in the target ISA).
2970 if ((y == nullptr) || !y->HasOnlyOneNonEnvironmentUse()) {
2971 return false;
2972 }
2973
2974 left = y->GetLeft();
2975 HConstant* const1 = left->IsConstant() ? left->AsConstant() : y->GetRight()->AsConstant();
2976 if (const1 == nullptr) {
2977 return false;
2978 }
2979
2980 HInstruction* x = (const1 == left) ? y->GetRight() : left;
2981 // If both inputs are constants, let the constant folding pass deal with it.
2982 if (x->IsConstant()) {
2983 return false;
2984 }
2985
2986 bool is_const2_negated = (const2 == right) && instruction->IsSub();
2987 int64_t const2_val = GetValue(const2, is_const2_negated);
2988 bool is_y_negated = (y == right) && instruction->IsSub();
2989 right = y->GetRight();
2990 bool is_const1_negated = is_y_negated ^ ((const1 == right) && y->IsSub());
2991 int64_t const1_val = GetValue(const1, is_const1_negated);
2992 bool is_x_negated = is_y_negated ^ ((x == right) && y->IsSub());
2993 int64_t const3_val = ComputeAddition(type, const1_val, const2_val);
2994 HBasicBlock* block = instruction->GetBlock();
2995 HConstant* const3 = block->GetGraph()->GetConstant(type, const3_val);
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002996 ArenaAllocator* allocator = instruction->GetAllocator();
Anton Kirilove14dc862016-05-13 17:56:15 +01002997 HInstruction* z;
2998
2999 if (is_x_negated) {
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003000 z = new (allocator) HSub(type, const3, x, instruction->GetDexPc());
Anton Kirilove14dc862016-05-13 17:56:15 +01003001 } else {
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003002 z = new (allocator) HAdd(type, x, const3, instruction->GetDexPc());
Anton Kirilove14dc862016-05-13 17:56:15 +01003003 }
3004
3005 block->ReplaceAndRemoveInstructionWith(instruction, z);
3006 RecordSimplification();
3007 return true;
3008}
3009
Lena Djokicbc5460b2017-07-20 16:07:36 +02003010void InstructionSimplifierVisitor::VisitVecMul(HVecMul* instruction) {
3011 if (TryCombineVecMultiplyAccumulate(instruction)) {
3012 RecordSimplification();
3013 }
3014}
3015
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003016} // namespace art