1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "loop_optimization.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "arch/arm/instruction_set_features_arm.h"
20*795d594fSAndroid Build Coastguard Worker #include "arch/arm64/instruction_set_features_arm64.h"
21*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
22*795d594fSAndroid Build Coastguard Worker #include "arch/x86/instruction_set_features_x86.h"
23*795d594fSAndroid Build Coastguard Worker #include "arch/x86_64/instruction_set_features_x86_64.h"
24*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
25*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
26*795d594fSAndroid Build Coastguard Worker #include "linear_order.h"
27*795d594fSAndroid Build Coastguard Worker #include "mirror/array-inl.h"
28*795d594fSAndroid Build Coastguard Worker #include "mirror/string.h"
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker // Enables vectorization (SIMDization) in the loop optimizer.
33*795d594fSAndroid Build Coastguard Worker static constexpr bool kEnableVectorization = true;
34*795d594fSAndroid Build Coastguard Worker
35*795d594fSAndroid Build Coastguard Worker //
36*795d594fSAndroid Build Coastguard Worker // Static helpers.
37*795d594fSAndroid Build Coastguard Worker //
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard Worker // Base alignment for arrays/strings guaranteed by the Android runtime.
BaseAlignment()40*795d594fSAndroid Build Coastguard Worker static uint32_t BaseAlignment() {
41*795d594fSAndroid Build Coastguard Worker return kObjectAlignment;
42*795d594fSAndroid Build Coastguard Worker }
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker // Hidden offset for arrays/strings guaranteed by the Android runtime.
HiddenOffset(DataType::Type type,bool is_string_char_at)45*795d594fSAndroid Build Coastguard Worker static uint32_t HiddenOffset(DataType::Type type, bool is_string_char_at) {
46*795d594fSAndroid Build Coastguard Worker return is_string_char_at
47*795d594fSAndroid Build Coastguard Worker ? mirror::String::ValueOffset().Uint32Value()
48*795d594fSAndroid Build Coastguard Worker : mirror::Array::DataOffset(DataType::Size(type)).Uint32Value();
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker // Remove the instruction from the graph. A bit more elaborate than the usual
52*795d594fSAndroid Build Coastguard Worker // instruction removal, since there may be a cycle in the use structure.
RemoveFromCycle(HInstruction * instruction)53*795d594fSAndroid Build Coastguard Worker static void RemoveFromCycle(HInstruction* instruction) {
54*795d594fSAndroid Build Coastguard Worker instruction->RemoveAsUserOfAllInputs();
55*795d594fSAndroid Build Coastguard Worker instruction->RemoveEnvironmentUsers();
56*795d594fSAndroid Build Coastguard Worker instruction->GetBlock()->RemoveInstructionOrPhi(instruction, /*ensure_safety=*/ false);
57*795d594fSAndroid Build Coastguard Worker RemoveEnvironmentUses(instruction);
58*795d594fSAndroid Build Coastguard Worker ResetEnvironmentInputRecords(instruction);
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker
61*795d594fSAndroid Build Coastguard Worker // Detect a goto block and sets succ to the single successor.
IsGotoBlock(HBasicBlock * block,HBasicBlock ** succ)62*795d594fSAndroid Build Coastguard Worker static bool IsGotoBlock(HBasicBlock* block, /*out*/ HBasicBlock** succ) {
63*795d594fSAndroid Build Coastguard Worker if (block->GetPredecessors().size() == 1 &&
64*795d594fSAndroid Build Coastguard Worker block->GetSuccessors().size() == 1 &&
65*795d594fSAndroid Build Coastguard Worker block->IsSingleGoto()) {
66*795d594fSAndroid Build Coastguard Worker *succ = block->GetSingleSuccessor();
67*795d594fSAndroid Build Coastguard Worker return true;
68*795d594fSAndroid Build Coastguard Worker }
69*795d594fSAndroid Build Coastguard Worker return false;
70*795d594fSAndroid Build Coastguard Worker }
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker // Detect an early exit loop.
IsEarlyExit(HLoopInformation * loop_info)73*795d594fSAndroid Build Coastguard Worker static bool IsEarlyExit(HLoopInformation* loop_info) {
74*795d594fSAndroid Build Coastguard Worker HBlocksInLoopReversePostOrderIterator it_loop(*loop_info);
75*795d594fSAndroid Build Coastguard Worker for (it_loop.Advance(); !it_loop.Done(); it_loop.Advance()) {
76*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* successor : it_loop.Current()->GetSuccessors()) {
77*795d594fSAndroid Build Coastguard Worker if (!loop_info->Contains(*successor)) {
78*795d594fSAndroid Build Coastguard Worker return true;
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker return false;
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker
85*795d594fSAndroid Build Coastguard Worker // Forward declaration.
86*795d594fSAndroid Build Coastguard Worker static bool IsZeroExtensionAndGet(HInstruction* instruction,
87*795d594fSAndroid Build Coastguard Worker DataType::Type type,
88*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** operand);
89*795d594fSAndroid Build Coastguard Worker
90*795d594fSAndroid Build Coastguard Worker // Detect a sign extension in instruction from the given type.
91*795d594fSAndroid Build Coastguard Worker // Returns the promoted operand on success.
IsSignExtensionAndGet(HInstruction * instruction,DataType::Type type,HInstruction ** operand)92*795d594fSAndroid Build Coastguard Worker static bool IsSignExtensionAndGet(HInstruction* instruction,
93*795d594fSAndroid Build Coastguard Worker DataType::Type type,
94*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** operand) {
95*795d594fSAndroid Build Coastguard Worker // Accept any already wider constant that would be handled properly by sign
96*795d594fSAndroid Build Coastguard Worker // extension when represented in the *width* of the given narrower data type
97*795d594fSAndroid Build Coastguard Worker // (the fact that Uint8/Uint16 normally zero extend does not matter here).
98*795d594fSAndroid Build Coastguard Worker int64_t value = 0;
99*795d594fSAndroid Build Coastguard Worker if (IsInt64AndGet(instruction, /*out*/ &value)) {
100*795d594fSAndroid Build Coastguard Worker switch (type) {
101*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint8:
102*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt8:
103*795d594fSAndroid Build Coastguard Worker if (IsInt<8>(value)) {
104*795d594fSAndroid Build Coastguard Worker *operand = instruction;
105*795d594fSAndroid Build Coastguard Worker return true;
106*795d594fSAndroid Build Coastguard Worker }
107*795d594fSAndroid Build Coastguard Worker return false;
108*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
109*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
110*795d594fSAndroid Build Coastguard Worker if (IsInt<16>(value)) {
111*795d594fSAndroid Build Coastguard Worker *operand = instruction;
112*795d594fSAndroid Build Coastguard Worker return true;
113*795d594fSAndroid Build Coastguard Worker }
114*795d594fSAndroid Build Coastguard Worker return false;
115*795d594fSAndroid Build Coastguard Worker default:
116*795d594fSAndroid Build Coastguard Worker return false;
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker }
119*795d594fSAndroid Build Coastguard Worker // An implicit widening conversion of any signed expression sign-extends.
120*795d594fSAndroid Build Coastguard Worker if (instruction->GetType() == type) {
121*795d594fSAndroid Build Coastguard Worker switch (type) {
122*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt8:
123*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
124*795d594fSAndroid Build Coastguard Worker *operand = instruction;
125*795d594fSAndroid Build Coastguard Worker return true;
126*795d594fSAndroid Build Coastguard Worker default:
127*795d594fSAndroid Build Coastguard Worker return false;
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker // An explicit widening conversion of a signed expression sign-extends.
131*795d594fSAndroid Build Coastguard Worker if (instruction->IsTypeConversion()) {
132*795d594fSAndroid Build Coastguard Worker HInstruction* conv = instruction->InputAt(0);
133*795d594fSAndroid Build Coastguard Worker DataType::Type from = conv->GetType();
134*795d594fSAndroid Build Coastguard Worker switch (instruction->GetType()) {
135*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt32:
136*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt64:
137*795d594fSAndroid Build Coastguard Worker if (type == from && (from == DataType::Type::kInt8 ||
138*795d594fSAndroid Build Coastguard Worker from == DataType::Type::kInt16 ||
139*795d594fSAndroid Build Coastguard Worker from == DataType::Type::kInt32)) {
140*795d594fSAndroid Build Coastguard Worker *operand = conv;
141*795d594fSAndroid Build Coastguard Worker return true;
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker return false;
144*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
145*795d594fSAndroid Build Coastguard Worker return type == DataType::Type::kUint16 &&
146*795d594fSAndroid Build Coastguard Worker from == DataType::Type::kUint16 &&
147*795d594fSAndroid Build Coastguard Worker IsZeroExtensionAndGet(instruction->InputAt(0), type, /*out*/ operand);
148*795d594fSAndroid Build Coastguard Worker default:
149*795d594fSAndroid Build Coastguard Worker return false;
150*795d594fSAndroid Build Coastguard Worker }
151*795d594fSAndroid Build Coastguard Worker }
152*795d594fSAndroid Build Coastguard Worker return false;
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker
155*795d594fSAndroid Build Coastguard Worker // Detect a zero extension in instruction from the given type.
156*795d594fSAndroid Build Coastguard Worker // Returns the promoted operand on success.
IsZeroExtensionAndGet(HInstruction * instruction,DataType::Type type,HInstruction ** operand)157*795d594fSAndroid Build Coastguard Worker static bool IsZeroExtensionAndGet(HInstruction* instruction,
158*795d594fSAndroid Build Coastguard Worker DataType::Type type,
159*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** operand) {
160*795d594fSAndroid Build Coastguard Worker // Accept any already wider constant that would be handled properly by zero
161*795d594fSAndroid Build Coastguard Worker // extension when represented in the *width* of the given narrower data type
162*795d594fSAndroid Build Coastguard Worker // (the fact that Int8/Int16 normally sign extend does not matter here).
163*795d594fSAndroid Build Coastguard Worker int64_t value = 0;
164*795d594fSAndroid Build Coastguard Worker if (IsInt64AndGet(instruction, /*out*/ &value)) {
165*795d594fSAndroid Build Coastguard Worker switch (type) {
166*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint8:
167*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt8:
168*795d594fSAndroid Build Coastguard Worker if (IsUint<8>(value)) {
169*795d594fSAndroid Build Coastguard Worker *operand = instruction;
170*795d594fSAndroid Build Coastguard Worker return true;
171*795d594fSAndroid Build Coastguard Worker }
172*795d594fSAndroid Build Coastguard Worker return false;
173*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
174*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
175*795d594fSAndroid Build Coastguard Worker if (IsUint<16>(value)) {
176*795d594fSAndroid Build Coastguard Worker *operand = instruction;
177*795d594fSAndroid Build Coastguard Worker return true;
178*795d594fSAndroid Build Coastguard Worker }
179*795d594fSAndroid Build Coastguard Worker return false;
180*795d594fSAndroid Build Coastguard Worker default:
181*795d594fSAndroid Build Coastguard Worker return false;
182*795d594fSAndroid Build Coastguard Worker }
183*795d594fSAndroid Build Coastguard Worker }
184*795d594fSAndroid Build Coastguard Worker // An implicit widening conversion of any unsigned expression zero-extends.
185*795d594fSAndroid Build Coastguard Worker if (instruction->GetType() == type) {
186*795d594fSAndroid Build Coastguard Worker switch (type) {
187*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint8:
188*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
189*795d594fSAndroid Build Coastguard Worker *operand = instruction;
190*795d594fSAndroid Build Coastguard Worker return true;
191*795d594fSAndroid Build Coastguard Worker default:
192*795d594fSAndroid Build Coastguard Worker return false;
193*795d594fSAndroid Build Coastguard Worker }
194*795d594fSAndroid Build Coastguard Worker }
195*795d594fSAndroid Build Coastguard Worker // An explicit widening conversion of an unsigned expression zero-extends.
196*795d594fSAndroid Build Coastguard Worker if (instruction->IsTypeConversion()) {
197*795d594fSAndroid Build Coastguard Worker HInstruction* conv = instruction->InputAt(0);
198*795d594fSAndroid Build Coastguard Worker DataType::Type from = conv->GetType();
199*795d594fSAndroid Build Coastguard Worker switch (instruction->GetType()) {
200*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt32:
201*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt64:
202*795d594fSAndroid Build Coastguard Worker if (type == from && from == DataType::Type::kUint16) {
203*795d594fSAndroid Build Coastguard Worker *operand = conv;
204*795d594fSAndroid Build Coastguard Worker return true;
205*795d594fSAndroid Build Coastguard Worker }
206*795d594fSAndroid Build Coastguard Worker return false;
207*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
208*795d594fSAndroid Build Coastguard Worker return type == DataType::Type::kInt16 &&
209*795d594fSAndroid Build Coastguard Worker from == DataType::Type::kInt16 &&
210*795d594fSAndroid Build Coastguard Worker IsSignExtensionAndGet(instruction->InputAt(0), type, /*out*/ operand);
211*795d594fSAndroid Build Coastguard Worker default:
212*795d594fSAndroid Build Coastguard Worker return false;
213*795d594fSAndroid Build Coastguard Worker }
214*795d594fSAndroid Build Coastguard Worker }
215*795d594fSAndroid Build Coastguard Worker return false;
216*795d594fSAndroid Build Coastguard Worker }
217*795d594fSAndroid Build Coastguard Worker
218*795d594fSAndroid Build Coastguard Worker // Detect situations with same-extension narrower operands.
219*795d594fSAndroid Build Coastguard Worker // Returns true on success and sets is_unsigned accordingly.
IsNarrowerOperands(HInstruction * a,HInstruction * b,DataType::Type type,HInstruction ** r,HInstruction ** s,bool * is_unsigned)220*795d594fSAndroid Build Coastguard Worker static bool IsNarrowerOperands(HInstruction* a,
221*795d594fSAndroid Build Coastguard Worker HInstruction* b,
222*795d594fSAndroid Build Coastguard Worker DataType::Type type,
223*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** r,
224*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** s,
225*795d594fSAndroid Build Coastguard Worker /*out*/ bool* is_unsigned) {
226*795d594fSAndroid Build Coastguard Worker DCHECK(a != nullptr && b != nullptr);
227*795d594fSAndroid Build Coastguard Worker // Look for a matching sign extension.
228*795d594fSAndroid Build Coastguard Worker DataType::Type stype = HVecOperation::ToSignedType(type);
229*795d594fSAndroid Build Coastguard Worker if (IsSignExtensionAndGet(a, stype, r) && IsSignExtensionAndGet(b, stype, s)) {
230*795d594fSAndroid Build Coastguard Worker *is_unsigned = false;
231*795d594fSAndroid Build Coastguard Worker return true;
232*795d594fSAndroid Build Coastguard Worker }
233*795d594fSAndroid Build Coastguard Worker // Look for a matching zero extension.
234*795d594fSAndroid Build Coastguard Worker DataType::Type utype = HVecOperation::ToUnsignedType(type);
235*795d594fSAndroid Build Coastguard Worker if (IsZeroExtensionAndGet(a, utype, r) && IsZeroExtensionAndGet(b, utype, s)) {
236*795d594fSAndroid Build Coastguard Worker *is_unsigned = true;
237*795d594fSAndroid Build Coastguard Worker return true;
238*795d594fSAndroid Build Coastguard Worker }
239*795d594fSAndroid Build Coastguard Worker return false;
240*795d594fSAndroid Build Coastguard Worker }
241*795d594fSAndroid Build Coastguard Worker
242*795d594fSAndroid Build Coastguard Worker // As above, single operand.
IsNarrowerOperand(HInstruction * a,DataType::Type type,HInstruction ** r,bool * is_unsigned)243*795d594fSAndroid Build Coastguard Worker static bool IsNarrowerOperand(HInstruction* a,
244*795d594fSAndroid Build Coastguard Worker DataType::Type type,
245*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** r,
246*795d594fSAndroid Build Coastguard Worker /*out*/ bool* is_unsigned) {
247*795d594fSAndroid Build Coastguard Worker DCHECK(a != nullptr);
248*795d594fSAndroid Build Coastguard Worker // Look for a matching sign extension.
249*795d594fSAndroid Build Coastguard Worker DataType::Type stype = HVecOperation::ToSignedType(type);
250*795d594fSAndroid Build Coastguard Worker if (IsSignExtensionAndGet(a, stype, r)) {
251*795d594fSAndroid Build Coastguard Worker *is_unsigned = false;
252*795d594fSAndroid Build Coastguard Worker return true;
253*795d594fSAndroid Build Coastguard Worker }
254*795d594fSAndroid Build Coastguard Worker // Look for a matching zero extension.
255*795d594fSAndroid Build Coastguard Worker DataType::Type utype = HVecOperation::ToUnsignedType(type);
256*795d594fSAndroid Build Coastguard Worker if (IsZeroExtensionAndGet(a, utype, r)) {
257*795d594fSAndroid Build Coastguard Worker *is_unsigned = true;
258*795d594fSAndroid Build Coastguard Worker return true;
259*795d594fSAndroid Build Coastguard Worker }
260*795d594fSAndroid Build Coastguard Worker return false;
261*795d594fSAndroid Build Coastguard Worker }
262*795d594fSAndroid Build Coastguard Worker
263*795d594fSAndroid Build Coastguard Worker // Compute relative vector length based on type difference.
GetOtherVL(DataType::Type other_type,DataType::Type vector_type,uint32_t vl)264*795d594fSAndroid Build Coastguard Worker static uint32_t GetOtherVL(DataType::Type other_type, DataType::Type vector_type, uint32_t vl) {
265*795d594fSAndroid Build Coastguard Worker DCHECK(DataType::IsIntegralType(other_type));
266*795d594fSAndroid Build Coastguard Worker DCHECK(DataType::IsIntegralType(vector_type));
267*795d594fSAndroid Build Coastguard Worker DCHECK_GE(DataType::SizeShift(other_type), DataType::SizeShift(vector_type));
268*795d594fSAndroid Build Coastguard Worker return vl >> (DataType::SizeShift(other_type) - DataType::SizeShift(vector_type));
269*795d594fSAndroid Build Coastguard Worker }
270*795d594fSAndroid Build Coastguard Worker
271*795d594fSAndroid Build Coastguard Worker // Detect up to two added operands a and b and an acccumulated constant c.
IsAddConst(HInstruction * instruction,HInstruction ** a,HInstruction ** b,int64_t * c,int32_t depth=8)272*795d594fSAndroid Build Coastguard Worker static bool IsAddConst(HInstruction* instruction,
273*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** a,
274*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** b,
275*795d594fSAndroid Build Coastguard Worker /*out*/ int64_t* c,
276*795d594fSAndroid Build Coastguard Worker int32_t depth = 8) { // don't search too deep
277*795d594fSAndroid Build Coastguard Worker int64_t value = 0;
278*795d594fSAndroid Build Coastguard Worker // Enter add/sub while still within reasonable depth.
279*795d594fSAndroid Build Coastguard Worker if (depth > 0) {
280*795d594fSAndroid Build Coastguard Worker if (instruction->IsAdd()) {
281*795d594fSAndroid Build Coastguard Worker return IsAddConst(instruction->InputAt(0), a, b, c, depth - 1) &&
282*795d594fSAndroid Build Coastguard Worker IsAddConst(instruction->InputAt(1), a, b, c, depth - 1);
283*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsSub() &&
284*795d594fSAndroid Build Coastguard Worker IsInt64AndGet(instruction->InputAt(1), &value)) {
285*795d594fSAndroid Build Coastguard Worker *c -= value;
286*795d594fSAndroid Build Coastguard Worker return IsAddConst(instruction->InputAt(0), a, b, c, depth - 1);
287*795d594fSAndroid Build Coastguard Worker }
288*795d594fSAndroid Build Coastguard Worker }
289*795d594fSAndroid Build Coastguard Worker // Otherwise, deal with leaf nodes.
290*795d594fSAndroid Build Coastguard Worker if (IsInt64AndGet(instruction, &value)) {
291*795d594fSAndroid Build Coastguard Worker *c += value;
292*795d594fSAndroid Build Coastguard Worker return true;
293*795d594fSAndroid Build Coastguard Worker } else if (*a == nullptr) {
294*795d594fSAndroid Build Coastguard Worker *a = instruction;
295*795d594fSAndroid Build Coastguard Worker return true;
296*795d594fSAndroid Build Coastguard Worker } else if (*b == nullptr) {
297*795d594fSAndroid Build Coastguard Worker *b = instruction;
298*795d594fSAndroid Build Coastguard Worker return true;
299*795d594fSAndroid Build Coastguard Worker }
300*795d594fSAndroid Build Coastguard Worker return false; // too many operands
301*795d594fSAndroid Build Coastguard Worker }
302*795d594fSAndroid Build Coastguard Worker
303*795d594fSAndroid Build Coastguard Worker // Detect a + b + c with optional constant c.
IsAddConst2(HGraph * graph,HInstruction * instruction,HInstruction ** a,HInstruction ** b,int64_t * c)304*795d594fSAndroid Build Coastguard Worker static bool IsAddConst2(HGraph* graph,
305*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
306*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** a,
307*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** b,
308*795d594fSAndroid Build Coastguard Worker /*out*/ int64_t* c) {
309*795d594fSAndroid Build Coastguard Worker // We want an actual add/sub and not the trivial case where {b: 0, c: 0}.
310*795d594fSAndroid Build Coastguard Worker if (IsAddOrSub(instruction) && IsAddConst(instruction, a, b, c) && *a != nullptr) {
311*795d594fSAndroid Build Coastguard Worker if (*b == nullptr) {
312*795d594fSAndroid Build Coastguard Worker // Constant is usually already present, unless accumulated.
313*795d594fSAndroid Build Coastguard Worker *b = graph->GetConstant(instruction->GetType(), (*c));
314*795d594fSAndroid Build Coastguard Worker *c = 0;
315*795d594fSAndroid Build Coastguard Worker }
316*795d594fSAndroid Build Coastguard Worker return true;
317*795d594fSAndroid Build Coastguard Worker }
318*795d594fSAndroid Build Coastguard Worker return false;
319*795d594fSAndroid Build Coastguard Worker }
320*795d594fSAndroid Build Coastguard Worker
321*795d594fSAndroid Build Coastguard Worker // Detect a direct a - b or a hidden a - (-c).
IsSubConst2(HGraph * graph,HInstruction * instruction,HInstruction ** a,HInstruction ** b)322*795d594fSAndroid Build Coastguard Worker static bool IsSubConst2(HGraph* graph,
323*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
324*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** a,
325*795d594fSAndroid Build Coastguard Worker /*out*/ HInstruction** b) {
326*795d594fSAndroid Build Coastguard Worker int64_t c = 0;
327*795d594fSAndroid Build Coastguard Worker if (instruction->IsSub()) {
328*795d594fSAndroid Build Coastguard Worker *a = instruction->InputAt(0);
329*795d594fSAndroid Build Coastguard Worker *b = instruction->InputAt(1);
330*795d594fSAndroid Build Coastguard Worker return true;
331*795d594fSAndroid Build Coastguard Worker } else if (IsAddConst(instruction, a, b, &c) && *a != nullptr && *b == nullptr) {
332*795d594fSAndroid Build Coastguard Worker // Constant for the hidden subtraction.
333*795d594fSAndroid Build Coastguard Worker *b = graph->GetConstant(instruction->GetType(), -c);
334*795d594fSAndroid Build Coastguard Worker return true;
335*795d594fSAndroid Build Coastguard Worker }
336*795d594fSAndroid Build Coastguard Worker return false;
337*795d594fSAndroid Build Coastguard Worker }
338*795d594fSAndroid Build Coastguard Worker
339*795d594fSAndroid Build Coastguard Worker // Detect reductions of the following forms,
340*795d594fSAndroid Build Coastguard Worker // x = x_phi + ..
341*795d594fSAndroid Build Coastguard Worker // x = x_phi - ..
HasReductionFormat(HInstruction * reduction,HInstruction * phi)342*795d594fSAndroid Build Coastguard Worker static bool HasReductionFormat(HInstruction* reduction, HInstruction* phi) {
343*795d594fSAndroid Build Coastguard Worker if (reduction->IsAdd()) {
344*795d594fSAndroid Build Coastguard Worker return (reduction->InputAt(0) == phi && reduction->InputAt(1) != phi) ||
345*795d594fSAndroid Build Coastguard Worker (reduction->InputAt(0) != phi && reduction->InputAt(1) == phi);
346*795d594fSAndroid Build Coastguard Worker } else if (reduction->IsSub()) {
347*795d594fSAndroid Build Coastguard Worker return (reduction->InputAt(0) == phi && reduction->InputAt(1) != phi);
348*795d594fSAndroid Build Coastguard Worker }
349*795d594fSAndroid Build Coastguard Worker return false;
350*795d594fSAndroid Build Coastguard Worker }
351*795d594fSAndroid Build Coastguard Worker
352*795d594fSAndroid Build Coastguard Worker // Translates vector operation to reduction kind.
GetReductionKind(HVecOperation * reduction)353*795d594fSAndroid Build Coastguard Worker static HVecReduce::ReductionKind GetReductionKind(HVecOperation* reduction) {
354*795d594fSAndroid Build Coastguard Worker if (reduction->IsVecAdd() ||
355*795d594fSAndroid Build Coastguard Worker reduction->IsVecSub() ||
356*795d594fSAndroid Build Coastguard Worker reduction->IsVecSADAccumulate() ||
357*795d594fSAndroid Build Coastguard Worker reduction->IsVecDotProd()) {
358*795d594fSAndroid Build Coastguard Worker return HVecReduce::kSum;
359*795d594fSAndroid Build Coastguard Worker }
360*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unsupported SIMD reduction " << reduction->GetId();
361*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
362*795d594fSAndroid Build Coastguard Worker }
363*795d594fSAndroid Build Coastguard Worker
364*795d594fSAndroid Build Coastguard Worker // Test vector restrictions.
HasVectorRestrictions(uint64_t restrictions,uint64_t tested)365*795d594fSAndroid Build Coastguard Worker static bool HasVectorRestrictions(uint64_t restrictions, uint64_t tested) {
366*795d594fSAndroid Build Coastguard Worker return (restrictions & tested) != 0;
367*795d594fSAndroid Build Coastguard Worker }
368*795d594fSAndroid Build Coastguard Worker
369*795d594fSAndroid Build Coastguard Worker // Insert an instruction at the end of the block, with safe checks.
Insert(HBasicBlock * block,HInstruction * instruction)370*795d594fSAndroid Build Coastguard Worker inline HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
371*795d594fSAndroid Build Coastguard Worker DCHECK(block != nullptr);
372*795d594fSAndroid Build Coastguard Worker DCHECK(instruction != nullptr);
373*795d594fSAndroid Build Coastguard Worker block->InsertInstructionBefore(instruction, block->GetLastInstruction());
374*795d594fSAndroid Build Coastguard Worker return instruction;
375*795d594fSAndroid Build Coastguard Worker }
376*795d594fSAndroid Build Coastguard Worker
377*795d594fSAndroid Build Coastguard Worker // Check that instructions from the induction sets are fully removed: have no uses
378*795d594fSAndroid Build Coastguard Worker // and no other instructions use them.
CheckInductionSetFullyRemoved(ScopedArenaSet<HInstruction * > * iset)379*795d594fSAndroid Build Coastguard Worker static bool CheckInductionSetFullyRemoved(ScopedArenaSet<HInstruction*>* iset) {
380*795d594fSAndroid Build Coastguard Worker for (HInstruction* instr : *iset) {
381*795d594fSAndroid Build Coastguard Worker if (instr->GetBlock() != nullptr ||
382*795d594fSAndroid Build Coastguard Worker !instr->GetUses().empty() ||
383*795d594fSAndroid Build Coastguard Worker !instr->GetEnvUses().empty() ||
384*795d594fSAndroid Build Coastguard Worker HasEnvironmentUsedByOthers(instr)) {
385*795d594fSAndroid Build Coastguard Worker return false;
386*795d594fSAndroid Build Coastguard Worker }
387*795d594fSAndroid Build Coastguard Worker }
388*795d594fSAndroid Build Coastguard Worker return true;
389*795d594fSAndroid Build Coastguard Worker }
390*795d594fSAndroid Build Coastguard Worker
391*795d594fSAndroid Build Coastguard Worker // Tries to statically evaluate condition of the specified "HIf" for other condition checks.
TryToEvaluateIfCondition(HIf * instruction,HGraph * graph)392*795d594fSAndroid Build Coastguard Worker static void TryToEvaluateIfCondition(HIf* instruction, HGraph* graph) {
393*795d594fSAndroid Build Coastguard Worker HInstruction* cond = instruction->InputAt(0);
394*795d594fSAndroid Build Coastguard Worker
395*795d594fSAndroid Build Coastguard Worker // If a condition 'cond' is evaluated in an HIf instruction then in the successors of the
396*795d594fSAndroid Build Coastguard Worker // IF_BLOCK we statically know the value of the condition 'cond' (TRUE in TRUE_SUCC, FALSE in
397*795d594fSAndroid Build Coastguard Worker // FALSE_SUCC). Using that we can replace another evaluation (use) EVAL of the same 'cond'
398*795d594fSAndroid Build Coastguard Worker // with TRUE value (FALSE value) if every path from the ENTRY_BLOCK to EVAL_BLOCK contains the
399*795d594fSAndroid Build Coastguard Worker // edge HIF_BLOCK->TRUE_SUCC (HIF_BLOCK->FALSE_SUCC).
400*795d594fSAndroid Build Coastguard Worker // if (cond) { if(cond) {
401*795d594fSAndroid Build Coastguard Worker // if (cond) {} if (1) {}
402*795d594fSAndroid Build Coastguard Worker // } else { =======> } else {
403*795d594fSAndroid Build Coastguard Worker // if (cond) {} if (0) {}
404*795d594fSAndroid Build Coastguard Worker // } }
405*795d594fSAndroid Build Coastguard Worker if (!cond->IsConstant()) {
406*795d594fSAndroid Build Coastguard Worker HBasicBlock* true_succ = instruction->IfTrueSuccessor();
407*795d594fSAndroid Build Coastguard Worker HBasicBlock* false_succ = instruction->IfFalseSuccessor();
408*795d594fSAndroid Build Coastguard Worker
409*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(true_succ->GetPredecessors().size(), 1u);
410*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(false_succ->GetPredecessors().size(), 1u);
411*795d594fSAndroid Build Coastguard Worker
412*795d594fSAndroid Build Coastguard Worker const HUseList<HInstruction*>& uses = cond->GetUses();
413*795d594fSAndroid Build Coastguard Worker for (auto it = uses.begin(), end = uses.end(); it != end; /* ++it below */) {
414*795d594fSAndroid Build Coastguard Worker HInstruction* user = it->GetUser();
415*795d594fSAndroid Build Coastguard Worker size_t index = it->GetIndex();
416*795d594fSAndroid Build Coastguard Worker HBasicBlock* user_block = user->GetBlock();
417*795d594fSAndroid Build Coastguard Worker // Increment `it` now because `*it` may disappear thanks to user->ReplaceInput().
418*795d594fSAndroid Build Coastguard Worker ++it;
419*795d594fSAndroid Build Coastguard Worker if (true_succ->Dominates(user_block)) {
420*795d594fSAndroid Build Coastguard Worker user->ReplaceInput(graph->GetIntConstant(1), index);
421*795d594fSAndroid Build Coastguard Worker } else if (false_succ->Dominates(user_block)) {
422*795d594fSAndroid Build Coastguard Worker user->ReplaceInput(graph->GetIntConstant(0), index);
423*795d594fSAndroid Build Coastguard Worker }
424*795d594fSAndroid Build Coastguard Worker }
425*795d594fSAndroid Build Coastguard Worker }
426*795d594fSAndroid Build Coastguard Worker }
427*795d594fSAndroid Build Coastguard Worker
428*795d594fSAndroid Build Coastguard Worker // Peel the first 'count' iterations of the loop.
PeelByCount(HLoopInformation * loop_info,int count,InductionVarRange * induction_range)429*795d594fSAndroid Build Coastguard Worker static void PeelByCount(HLoopInformation* loop_info,
430*795d594fSAndroid Build Coastguard Worker int count,
431*795d594fSAndroid Build Coastguard Worker InductionVarRange* induction_range) {
432*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < count; i++) {
433*795d594fSAndroid Build Coastguard Worker // Perform peeling.
434*795d594fSAndroid Build Coastguard Worker LoopClonerSimpleHelper helper(loop_info, induction_range);
435*795d594fSAndroid Build Coastguard Worker helper.DoPeeling();
436*795d594fSAndroid Build Coastguard Worker }
437*795d594fSAndroid Build Coastguard Worker }
438*795d594fSAndroid Build Coastguard Worker
439*795d594fSAndroid Build Coastguard Worker // Returns the narrower type out of instructions a and b types.
GetNarrowerType(HInstruction * a,HInstruction * b)440*795d594fSAndroid Build Coastguard Worker static DataType::Type GetNarrowerType(HInstruction* a, HInstruction* b) {
441*795d594fSAndroid Build Coastguard Worker DataType::Type type = a->GetType();
442*795d594fSAndroid Build Coastguard Worker if (DataType::Size(b->GetType()) < DataType::Size(type)) {
443*795d594fSAndroid Build Coastguard Worker type = b->GetType();
444*795d594fSAndroid Build Coastguard Worker }
445*795d594fSAndroid Build Coastguard Worker if (a->IsTypeConversion() &&
446*795d594fSAndroid Build Coastguard Worker DataType::Size(a->InputAt(0)->GetType()) < DataType::Size(type)) {
447*795d594fSAndroid Build Coastguard Worker type = a->InputAt(0)->GetType();
448*795d594fSAndroid Build Coastguard Worker }
449*795d594fSAndroid Build Coastguard Worker if (b->IsTypeConversion() &&
450*795d594fSAndroid Build Coastguard Worker DataType::Size(b->InputAt(0)->GetType()) < DataType::Size(type)) {
451*795d594fSAndroid Build Coastguard Worker type = b->InputAt(0)->GetType();
452*795d594fSAndroid Build Coastguard Worker }
453*795d594fSAndroid Build Coastguard Worker return type;
454*795d594fSAndroid Build Coastguard Worker }
455*795d594fSAndroid Build Coastguard Worker
456*795d594fSAndroid Build Coastguard Worker // Returns whether the loop is of a diamond structure:
457*795d594fSAndroid Build Coastguard Worker //
458*795d594fSAndroid Build Coastguard Worker // header <----------------+
459*795d594fSAndroid Build Coastguard Worker // | |
460*795d594fSAndroid Build Coastguard Worker // diamond_hif |
461*795d594fSAndroid Build Coastguard Worker // / \ |
462*795d594fSAndroid Build Coastguard Worker // diamond_true diamond_false |
463*795d594fSAndroid Build Coastguard Worker // \ / |
464*795d594fSAndroid Build Coastguard Worker // back_edge |
465*795d594fSAndroid Build Coastguard Worker // | |
466*795d594fSAndroid Build Coastguard Worker // +---------------------+
HasLoopDiamondStructure(HLoopInformation * loop_info)467*795d594fSAndroid Build Coastguard Worker static bool HasLoopDiamondStructure(HLoopInformation* loop_info) {
468*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = loop_info->GetHeader();
469*795d594fSAndroid Build Coastguard Worker if (loop_info->NumberOfBackEdges() != 1 || header->GetSuccessors().size() != 2) {
470*795d594fSAndroid Build Coastguard Worker return false;
471*795d594fSAndroid Build Coastguard Worker }
472*795d594fSAndroid Build Coastguard Worker HBasicBlock* header_succ_0 = header->GetSuccessors()[0];
473*795d594fSAndroid Build Coastguard Worker HBasicBlock* header_succ_1 = header->GetSuccessors()[1];
474*795d594fSAndroid Build Coastguard Worker HBasicBlock* diamond_top = loop_info->Contains(*header_succ_0) ?
475*795d594fSAndroid Build Coastguard Worker header_succ_0 :
476*795d594fSAndroid Build Coastguard Worker header_succ_1;
477*795d594fSAndroid Build Coastguard Worker if (!diamond_top->GetLastInstruction()->IsIf()) {
478*795d594fSAndroid Build Coastguard Worker return false;
479*795d594fSAndroid Build Coastguard Worker }
480*795d594fSAndroid Build Coastguard Worker
481*795d594fSAndroid Build Coastguard Worker HIf* diamond_hif = diamond_top->GetLastInstruction()->AsIf();
482*795d594fSAndroid Build Coastguard Worker HBasicBlock* diamond_true = diamond_hif->IfTrueSuccessor();
483*795d594fSAndroid Build Coastguard Worker HBasicBlock* diamond_false = diamond_hif->IfFalseSuccessor();
484*795d594fSAndroid Build Coastguard Worker
485*795d594fSAndroid Build Coastguard Worker if (diamond_true->GetSuccessors().size() != 1 || diamond_false->GetSuccessors().size() != 1) {
486*795d594fSAndroid Build Coastguard Worker return false;
487*795d594fSAndroid Build Coastguard Worker }
488*795d594fSAndroid Build Coastguard Worker
489*795d594fSAndroid Build Coastguard Worker HBasicBlock* back_edge = diamond_true->GetSingleSuccessor();
490*795d594fSAndroid Build Coastguard Worker if (back_edge != diamond_false->GetSingleSuccessor() ||
491*795d594fSAndroid Build Coastguard Worker back_edge != loop_info->GetBackEdges()[0]) {
492*795d594fSAndroid Build Coastguard Worker return false;
493*795d594fSAndroid Build Coastguard Worker }
494*795d594fSAndroid Build Coastguard Worker
495*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(loop_info->GetBlocks().NumSetBits(), 5u);
496*795d594fSAndroid Build Coastguard Worker return true;
497*795d594fSAndroid Build Coastguard Worker }
498*795d594fSAndroid Build Coastguard Worker
IsPredicatedLoopControlFlowSupported(HLoopInformation * loop_info)499*795d594fSAndroid Build Coastguard Worker static bool IsPredicatedLoopControlFlowSupported(HLoopInformation* loop_info) {
500*795d594fSAndroid Build Coastguard Worker size_t num_of_blocks = loop_info->GetBlocks().NumSetBits();
501*795d594fSAndroid Build Coastguard Worker return num_of_blocks == 2 || HasLoopDiamondStructure(loop_info);
502*795d594fSAndroid Build Coastguard Worker }
503*795d594fSAndroid Build Coastguard Worker
504*795d594fSAndroid Build Coastguard Worker //
505*795d594fSAndroid Build Coastguard Worker // Public methods.
506*795d594fSAndroid Build Coastguard Worker //
507*795d594fSAndroid Build Coastguard Worker
HLoopOptimization(HGraph * graph,const CodeGenerator & codegen,HInductionVarAnalysis * induction_analysis,OptimizingCompilerStats * stats,const char * name)508*795d594fSAndroid Build Coastguard Worker HLoopOptimization::HLoopOptimization(HGraph* graph,
509*795d594fSAndroid Build Coastguard Worker const CodeGenerator& codegen,
510*795d594fSAndroid Build Coastguard Worker HInductionVarAnalysis* induction_analysis,
511*795d594fSAndroid Build Coastguard Worker OptimizingCompilerStats* stats,
512*795d594fSAndroid Build Coastguard Worker const char* name)
513*795d594fSAndroid Build Coastguard Worker : HOptimization(graph, name, stats),
514*795d594fSAndroid Build Coastguard Worker compiler_options_(&codegen.GetCompilerOptions()),
515*795d594fSAndroid Build Coastguard Worker simd_register_size_(codegen.GetSIMDRegisterWidth()),
516*795d594fSAndroid Build Coastguard Worker induction_range_(induction_analysis),
517*795d594fSAndroid Build Coastguard Worker loop_allocator_(nullptr),
518*795d594fSAndroid Build Coastguard Worker global_allocator_(graph_->GetAllocator()),
519*795d594fSAndroid Build Coastguard Worker top_loop_(nullptr),
520*795d594fSAndroid Build Coastguard Worker last_loop_(nullptr),
521*795d594fSAndroid Build Coastguard Worker iset_(nullptr),
522*795d594fSAndroid Build Coastguard Worker reductions_(nullptr),
523*795d594fSAndroid Build Coastguard Worker simplified_(false),
524*795d594fSAndroid Build Coastguard Worker predicated_vectorization_mode_(codegen.SupportsPredicatedSIMD()),
525*795d594fSAndroid Build Coastguard Worker vector_length_(0),
526*795d594fSAndroid Build Coastguard Worker vector_refs_(nullptr),
527*795d594fSAndroid Build Coastguard Worker vector_static_peeling_factor_(0),
528*795d594fSAndroid Build Coastguard Worker vector_dynamic_peeling_candidate_(nullptr),
529*795d594fSAndroid Build Coastguard Worker vector_runtime_test_a_(nullptr),
530*795d594fSAndroid Build Coastguard Worker vector_runtime_test_b_(nullptr),
531*795d594fSAndroid Build Coastguard Worker vector_map_(nullptr),
532*795d594fSAndroid Build Coastguard Worker vector_permanent_map_(nullptr),
533*795d594fSAndroid Build Coastguard Worker vector_external_set_(nullptr),
534*795d594fSAndroid Build Coastguard Worker predicate_info_map_(nullptr),
535*795d594fSAndroid Build Coastguard Worker synthesis_mode_(LoopSynthesisMode::kSequential),
536*795d594fSAndroid Build Coastguard Worker vector_preheader_(nullptr),
537*795d594fSAndroid Build Coastguard Worker vector_header_(nullptr),
538*795d594fSAndroid Build Coastguard Worker vector_body_(nullptr),
539*795d594fSAndroid Build Coastguard Worker vector_index_(nullptr),
540*795d594fSAndroid Build Coastguard Worker arch_loop_helper_(ArchNoOptsLoopHelper::Create(codegen, global_allocator_)) {}
541*795d594fSAndroid Build Coastguard Worker
Run()542*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::Run() {
543*795d594fSAndroid Build Coastguard Worker // Skip if there is no loop or the graph has irreducible loops.
544*795d594fSAndroid Build Coastguard Worker // TODO: make this less of a sledgehammer.
545*795d594fSAndroid Build Coastguard Worker if (!graph_->HasLoops() || graph_->HasIrreducibleLoops()) {
546*795d594fSAndroid Build Coastguard Worker return false;
547*795d594fSAndroid Build Coastguard Worker }
548*795d594fSAndroid Build Coastguard Worker
549*795d594fSAndroid Build Coastguard Worker // Phase-local allocator.
550*795d594fSAndroid Build Coastguard Worker ScopedArenaAllocator allocator(graph_->GetArenaStack());
551*795d594fSAndroid Build Coastguard Worker loop_allocator_ = &allocator;
552*795d594fSAndroid Build Coastguard Worker
553*795d594fSAndroid Build Coastguard Worker // Perform loop optimizations.
554*795d594fSAndroid Build Coastguard Worker const bool did_loop_opt = LocalRun();
555*795d594fSAndroid Build Coastguard Worker if (top_loop_ == nullptr) {
556*795d594fSAndroid Build Coastguard Worker graph_->SetHasLoops(false); // no more loops
557*795d594fSAndroid Build Coastguard Worker }
558*795d594fSAndroid Build Coastguard Worker
559*795d594fSAndroid Build Coastguard Worker // Detach allocator.
560*795d594fSAndroid Build Coastguard Worker loop_allocator_ = nullptr;
561*795d594fSAndroid Build Coastguard Worker
562*795d594fSAndroid Build Coastguard Worker return did_loop_opt;
563*795d594fSAndroid Build Coastguard Worker }
564*795d594fSAndroid Build Coastguard Worker
565*795d594fSAndroid Build Coastguard Worker //
566*795d594fSAndroid Build Coastguard Worker // Loop setup and traversal.
567*795d594fSAndroid Build Coastguard Worker //
568*795d594fSAndroid Build Coastguard Worker
LocalRun()569*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::LocalRun() {
570*795d594fSAndroid Build Coastguard Worker // Build the linear order using the phase-local allocator. This step enables building
571*795d594fSAndroid Build Coastguard Worker // a loop hierarchy that properly reflects the outer-inner and previous-next relation.
572*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HBasicBlock*> linear_order(loop_allocator_->Adapter(kArenaAllocLinearOrder));
573*795d594fSAndroid Build Coastguard Worker LinearizeGraph(graph_, &linear_order);
574*795d594fSAndroid Build Coastguard Worker
575*795d594fSAndroid Build Coastguard Worker // Build the loop hierarchy.
576*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* block : linear_order) {
577*795d594fSAndroid Build Coastguard Worker if (block->IsLoopHeader()) {
578*795d594fSAndroid Build Coastguard Worker AddLoop(block->GetLoopInformation());
579*795d594fSAndroid Build Coastguard Worker }
580*795d594fSAndroid Build Coastguard Worker }
581*795d594fSAndroid Build Coastguard Worker DCHECK(top_loop_ != nullptr);
582*795d594fSAndroid Build Coastguard Worker
583*795d594fSAndroid Build Coastguard Worker // Traverse the loop hierarchy inner-to-outer and optimize. Traversal can use
584*795d594fSAndroid Build Coastguard Worker // temporary data structures using the phase-local allocator. All new HIR
585*795d594fSAndroid Build Coastguard Worker // should use the global allocator.
586*795d594fSAndroid Build Coastguard Worker ScopedArenaSet<HInstruction*> iset(loop_allocator_->Adapter(kArenaAllocLoopOptimization));
587*795d594fSAndroid Build Coastguard Worker ScopedArenaSafeMap<HInstruction*, HInstruction*> reds(
588*795d594fSAndroid Build Coastguard Worker std::less<HInstruction*>(), loop_allocator_->Adapter(kArenaAllocLoopOptimization));
589*795d594fSAndroid Build Coastguard Worker ScopedArenaSet<ArrayReference> refs(loop_allocator_->Adapter(kArenaAllocLoopOptimization));
590*795d594fSAndroid Build Coastguard Worker ScopedArenaSafeMap<HInstruction*, HInstruction*> map(
591*795d594fSAndroid Build Coastguard Worker std::less<HInstruction*>(), loop_allocator_->Adapter(kArenaAllocLoopOptimization));
592*795d594fSAndroid Build Coastguard Worker ScopedArenaSafeMap<HInstruction*, HInstruction*> perm(
593*795d594fSAndroid Build Coastguard Worker std::less<HInstruction*>(), loop_allocator_->Adapter(kArenaAllocLoopOptimization));
594*795d594fSAndroid Build Coastguard Worker ScopedArenaSet<HInstruction*> ext_set(loop_allocator_->Adapter(kArenaAllocLoopOptimization));
595*795d594fSAndroid Build Coastguard Worker ScopedArenaSafeMap<HBasicBlock*, BlockPredicateInfo*> pred(
596*795d594fSAndroid Build Coastguard Worker std::less<HBasicBlock*>(), loop_allocator_->Adapter(kArenaAllocLoopOptimization));
597*795d594fSAndroid Build Coastguard Worker // Attach.
598*795d594fSAndroid Build Coastguard Worker iset_ = &iset;
599*795d594fSAndroid Build Coastguard Worker reductions_ = &reds;
600*795d594fSAndroid Build Coastguard Worker vector_refs_ = &refs;
601*795d594fSAndroid Build Coastguard Worker vector_map_ = ↦
602*795d594fSAndroid Build Coastguard Worker vector_permanent_map_ = &perm;
603*795d594fSAndroid Build Coastguard Worker vector_external_set_ = &ext_set;
604*795d594fSAndroid Build Coastguard Worker predicate_info_map_ = &pred;
605*795d594fSAndroid Build Coastguard Worker // Traverse.
606*795d594fSAndroid Build Coastguard Worker const bool did_loop_opt = TraverseLoopsInnerToOuter(top_loop_);
607*795d594fSAndroid Build Coastguard Worker // Detach.
608*795d594fSAndroid Build Coastguard Worker iset_ = nullptr;
609*795d594fSAndroid Build Coastguard Worker reductions_ = nullptr;
610*795d594fSAndroid Build Coastguard Worker vector_refs_ = nullptr;
611*795d594fSAndroid Build Coastguard Worker vector_map_ = nullptr;
612*795d594fSAndroid Build Coastguard Worker vector_permanent_map_ = nullptr;
613*795d594fSAndroid Build Coastguard Worker vector_external_set_ = nullptr;
614*795d594fSAndroid Build Coastguard Worker predicate_info_map_ = nullptr;
615*795d594fSAndroid Build Coastguard Worker
616*795d594fSAndroid Build Coastguard Worker return did_loop_opt;
617*795d594fSAndroid Build Coastguard Worker }
618*795d594fSAndroid Build Coastguard Worker
AddLoop(HLoopInformation * loop_info)619*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::AddLoop(HLoopInformation* loop_info) {
620*795d594fSAndroid Build Coastguard Worker DCHECK(loop_info != nullptr);
621*795d594fSAndroid Build Coastguard Worker LoopNode* node = new (loop_allocator_) LoopNode(loop_info);
622*795d594fSAndroid Build Coastguard Worker if (last_loop_ == nullptr) {
623*795d594fSAndroid Build Coastguard Worker // First loop.
624*795d594fSAndroid Build Coastguard Worker DCHECK(top_loop_ == nullptr);
625*795d594fSAndroid Build Coastguard Worker last_loop_ = top_loop_ = node;
626*795d594fSAndroid Build Coastguard Worker } else if (loop_info->IsIn(*last_loop_->loop_info)) {
627*795d594fSAndroid Build Coastguard Worker // Inner loop.
628*795d594fSAndroid Build Coastguard Worker node->outer = last_loop_;
629*795d594fSAndroid Build Coastguard Worker DCHECK(last_loop_->inner == nullptr);
630*795d594fSAndroid Build Coastguard Worker last_loop_ = last_loop_->inner = node;
631*795d594fSAndroid Build Coastguard Worker } else {
632*795d594fSAndroid Build Coastguard Worker // Subsequent loop.
633*795d594fSAndroid Build Coastguard Worker while (last_loop_->outer != nullptr && !loop_info->IsIn(*last_loop_->outer->loop_info)) {
634*795d594fSAndroid Build Coastguard Worker last_loop_ = last_loop_->outer;
635*795d594fSAndroid Build Coastguard Worker }
636*795d594fSAndroid Build Coastguard Worker node->outer = last_loop_->outer;
637*795d594fSAndroid Build Coastguard Worker node->previous = last_loop_;
638*795d594fSAndroid Build Coastguard Worker DCHECK(last_loop_->next == nullptr);
639*795d594fSAndroid Build Coastguard Worker last_loop_ = last_loop_->next = node;
640*795d594fSAndroid Build Coastguard Worker }
641*795d594fSAndroid Build Coastguard Worker }
642*795d594fSAndroid Build Coastguard Worker
RemoveLoop(LoopNode * node)643*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::RemoveLoop(LoopNode* node) {
644*795d594fSAndroid Build Coastguard Worker DCHECK(node != nullptr);
645*795d594fSAndroid Build Coastguard Worker DCHECK(node->inner == nullptr);
646*795d594fSAndroid Build Coastguard Worker if (node->previous != nullptr) {
647*795d594fSAndroid Build Coastguard Worker // Within sequence.
648*795d594fSAndroid Build Coastguard Worker node->previous->next = node->next;
649*795d594fSAndroid Build Coastguard Worker if (node->next != nullptr) {
650*795d594fSAndroid Build Coastguard Worker node->next->previous = node->previous;
651*795d594fSAndroid Build Coastguard Worker }
652*795d594fSAndroid Build Coastguard Worker } else {
653*795d594fSAndroid Build Coastguard Worker // First of sequence.
654*795d594fSAndroid Build Coastguard Worker if (node->outer != nullptr) {
655*795d594fSAndroid Build Coastguard Worker node->outer->inner = node->next;
656*795d594fSAndroid Build Coastguard Worker } else {
657*795d594fSAndroid Build Coastguard Worker top_loop_ = node->next;
658*795d594fSAndroid Build Coastguard Worker }
659*795d594fSAndroid Build Coastguard Worker if (node->next != nullptr) {
660*795d594fSAndroid Build Coastguard Worker node->next->outer = node->outer;
661*795d594fSAndroid Build Coastguard Worker node->next->previous = nullptr;
662*795d594fSAndroid Build Coastguard Worker }
663*795d594fSAndroid Build Coastguard Worker }
664*795d594fSAndroid Build Coastguard Worker }
665*795d594fSAndroid Build Coastguard Worker
TraverseLoopsInnerToOuter(LoopNode * node)666*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TraverseLoopsInnerToOuter(LoopNode* node) {
667*795d594fSAndroid Build Coastguard Worker bool changed = false;
668*795d594fSAndroid Build Coastguard Worker for ( ; node != nullptr; node = node->next) {
669*795d594fSAndroid Build Coastguard Worker // Visit inner loops first. Recompute induction information for this
670*795d594fSAndroid Build Coastguard Worker // loop if the induction of any inner loop has changed.
671*795d594fSAndroid Build Coastguard Worker if (TraverseLoopsInnerToOuter(node->inner)) {
672*795d594fSAndroid Build Coastguard Worker induction_range_.ReVisit(node->loop_info);
673*795d594fSAndroid Build Coastguard Worker changed = true;
674*795d594fSAndroid Build Coastguard Worker }
675*795d594fSAndroid Build Coastguard Worker
676*795d594fSAndroid Build Coastguard Worker CalculateAndSetTryCatchKind(node);
677*795d594fSAndroid Build Coastguard Worker if (node->try_catch_kind == LoopNode::TryCatchKind::kHasTryCatch) {
678*795d594fSAndroid Build Coastguard Worker // The current optimizations assume that the loops do not contain try/catches.
679*795d594fSAndroid Build Coastguard Worker // TODO(solanes, 227283906): Assess if we can modify them to work with try/catches.
680*795d594fSAndroid Build Coastguard Worker continue;
681*795d594fSAndroid Build Coastguard Worker }
682*795d594fSAndroid Build Coastguard Worker
683*795d594fSAndroid Build Coastguard Worker DCHECK(node->try_catch_kind == LoopNode::TryCatchKind::kNoTryCatch)
684*795d594fSAndroid Build Coastguard Worker << "kind: " << static_cast<int>(node->try_catch_kind)
685*795d594fSAndroid Build Coastguard Worker << ". LoopOptimization requires the loops to not have try catches.";
686*795d594fSAndroid Build Coastguard Worker
687*795d594fSAndroid Build Coastguard Worker // Repeat simplifications in the loop-body until no more changes occur.
688*795d594fSAndroid Build Coastguard Worker // Note that since each simplification consists of eliminating code (without
689*795d594fSAndroid Build Coastguard Worker // introducing new code), this process is always finite.
690*795d594fSAndroid Build Coastguard Worker do {
691*795d594fSAndroid Build Coastguard Worker simplified_ = false;
692*795d594fSAndroid Build Coastguard Worker SimplifyInduction(node);
693*795d594fSAndroid Build Coastguard Worker SimplifyBlocks(node);
694*795d594fSAndroid Build Coastguard Worker changed = simplified_ || changed;
695*795d594fSAndroid Build Coastguard Worker } while (simplified_);
696*795d594fSAndroid Build Coastguard Worker // Optimize inner loop.
697*795d594fSAndroid Build Coastguard Worker if (node->inner == nullptr) {
698*795d594fSAndroid Build Coastguard Worker changed = OptimizeInnerLoop(node) || changed;
699*795d594fSAndroid Build Coastguard Worker }
700*795d594fSAndroid Build Coastguard Worker }
701*795d594fSAndroid Build Coastguard Worker return changed;
702*795d594fSAndroid Build Coastguard Worker }
703*795d594fSAndroid Build Coastguard Worker
CalculateAndSetTryCatchKind(LoopNode * node)704*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::CalculateAndSetTryCatchKind(LoopNode* node) {
705*795d594fSAndroid Build Coastguard Worker DCHECK(node != nullptr);
706*795d594fSAndroid Build Coastguard Worker DCHECK(node->try_catch_kind == LoopNode::TryCatchKind::kUnknown)
707*795d594fSAndroid Build Coastguard Worker << "kind: " << static_cast<int>(node->try_catch_kind)
708*795d594fSAndroid Build Coastguard Worker << ". SetTryCatchKind should be called only once per LoopNode.";
709*795d594fSAndroid Build Coastguard Worker
710*795d594fSAndroid Build Coastguard Worker // If a inner loop has a try catch, then the outer loop has one too (as it contains `inner`).
711*795d594fSAndroid Build Coastguard Worker // Knowing this, we could skip iterating through all of the outer loop's parents with a simple
712*795d594fSAndroid Build Coastguard Worker // check.
713*795d594fSAndroid Build Coastguard Worker for (LoopNode* inner = node->inner; inner != nullptr; inner = inner->next) {
714*795d594fSAndroid Build Coastguard Worker DCHECK(inner->try_catch_kind != LoopNode::TryCatchKind::kUnknown)
715*795d594fSAndroid Build Coastguard Worker << "kind: " << static_cast<int>(inner->try_catch_kind)
716*795d594fSAndroid Build Coastguard Worker << ". Should have updated the inner loop before the outer loop.";
717*795d594fSAndroid Build Coastguard Worker
718*795d594fSAndroid Build Coastguard Worker if (inner->try_catch_kind == LoopNode::TryCatchKind::kHasTryCatch) {
719*795d594fSAndroid Build Coastguard Worker node->try_catch_kind = LoopNode::TryCatchKind::kHasTryCatch;
720*795d594fSAndroid Build Coastguard Worker return;
721*795d594fSAndroid Build Coastguard Worker }
722*795d594fSAndroid Build Coastguard Worker }
723*795d594fSAndroid Build Coastguard Worker
724*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopIterator it_loop(*node->loop_info); !it_loop.Done(); it_loop.Advance()) {
725*795d594fSAndroid Build Coastguard Worker HBasicBlock* block = it_loop.Current();
726*795d594fSAndroid Build Coastguard Worker if (block->GetTryCatchInformation() != nullptr) {
727*795d594fSAndroid Build Coastguard Worker node->try_catch_kind = LoopNode::TryCatchKind::kHasTryCatch;
728*795d594fSAndroid Build Coastguard Worker return;
729*795d594fSAndroid Build Coastguard Worker }
730*795d594fSAndroid Build Coastguard Worker }
731*795d594fSAndroid Build Coastguard Worker
732*795d594fSAndroid Build Coastguard Worker node->try_catch_kind = LoopNode::TryCatchKind::kNoTryCatch;
733*795d594fSAndroid Build Coastguard Worker }
734*795d594fSAndroid Build Coastguard Worker
735*795d594fSAndroid Build Coastguard Worker //
736*795d594fSAndroid Build Coastguard Worker // This optimization applies to loops with plain simple operations
737*795d594fSAndroid Build Coastguard Worker // (I.e. no calls to java code or runtime) with a known small trip_count * instr_count
738*795d594fSAndroid Build Coastguard Worker // value.
739*795d594fSAndroid Build Coastguard Worker //
TryToRemoveSuspendCheckFromLoopHeader(LoopAnalysisInfo * analysis_info,bool generate_code)740*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryToRemoveSuspendCheckFromLoopHeader(LoopAnalysisInfo* analysis_info,
741*795d594fSAndroid Build Coastguard Worker bool generate_code) {
742*795d594fSAndroid Build Coastguard Worker if (!graph_->SuspendChecksAreAllowedToNoOp()) {
743*795d594fSAndroid Build Coastguard Worker return false;
744*795d594fSAndroid Build Coastguard Worker }
745*795d594fSAndroid Build Coastguard Worker
746*795d594fSAndroid Build Coastguard Worker int64_t trip_count = analysis_info->GetTripCount();
747*795d594fSAndroid Build Coastguard Worker
748*795d594fSAndroid Build Coastguard Worker if (trip_count == LoopAnalysisInfo::kUnknownTripCount) {
749*795d594fSAndroid Build Coastguard Worker return false;
750*795d594fSAndroid Build Coastguard Worker }
751*795d594fSAndroid Build Coastguard Worker
752*795d594fSAndroid Build Coastguard Worker int64_t instruction_count = analysis_info->GetNumberOfInstructions();
753*795d594fSAndroid Build Coastguard Worker int64_t total_instruction_count = trip_count * instruction_count;
754*795d594fSAndroid Build Coastguard Worker
755*795d594fSAndroid Build Coastguard Worker // The inclusion of the HasInstructionsPreventingScalarOpts() prevents this
756*795d594fSAndroid Build Coastguard Worker // optimization from being applied to loops that have calls.
757*795d594fSAndroid Build Coastguard Worker bool can_optimize =
758*795d594fSAndroid Build Coastguard Worker total_instruction_count <= HLoopOptimization::kMaxTotalInstRemoveSuspendCheck &&
759*795d594fSAndroid Build Coastguard Worker !analysis_info->HasInstructionsPreventingScalarOpts();
760*795d594fSAndroid Build Coastguard Worker
761*795d594fSAndroid Build Coastguard Worker if (!can_optimize) {
762*795d594fSAndroid Build Coastguard Worker return false;
763*795d594fSAndroid Build Coastguard Worker }
764*795d594fSAndroid Build Coastguard Worker
765*795d594fSAndroid Build Coastguard Worker // If we should do the optimization, disable codegen for the SuspendCheck.
766*795d594fSAndroid Build Coastguard Worker if (generate_code) {
767*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = analysis_info->GetLoopInfo();
768*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = loop_info->GetHeader();
769*795d594fSAndroid Build Coastguard Worker HSuspendCheck* instruction = header->GetLoopInformation()->GetSuspendCheck();
770*795d594fSAndroid Build Coastguard Worker // As other optimizations depend on SuspendCheck
771*795d594fSAndroid Build Coastguard Worker // (e.g: CHAGuardVisitor::HoistGuard), disable its codegen instead of
772*795d594fSAndroid Build Coastguard Worker // removing the SuspendCheck instruction.
773*795d594fSAndroid Build Coastguard Worker instruction->SetIsNoOp(true);
774*795d594fSAndroid Build Coastguard Worker }
775*795d594fSAndroid Build Coastguard Worker
776*795d594fSAndroid Build Coastguard Worker return true;
777*795d594fSAndroid Build Coastguard Worker }
778*795d594fSAndroid Build Coastguard Worker
779*795d594fSAndroid Build Coastguard Worker //
780*795d594fSAndroid Build Coastguard Worker // Optimization.
781*795d594fSAndroid Build Coastguard Worker //
782*795d594fSAndroid Build Coastguard Worker
SimplifyInduction(LoopNode * node)783*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::SimplifyInduction(LoopNode* node) {
784*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = node->loop_info->GetHeader();
785*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader = node->loop_info->GetPreHeader();
786*795d594fSAndroid Build Coastguard Worker // Scan the phis in the header to find opportunities to simplify an induction
787*795d594fSAndroid Build Coastguard Worker // cycle that is only used outside the loop. Replace these uses, if any, with
788*795d594fSAndroid Build Coastguard Worker // the last value and remove the induction cycle.
789*795d594fSAndroid Build Coastguard Worker // Examples: for (int i = 0; x != null; i++) { .... no i .... }
790*795d594fSAndroid Build Coastguard Worker // for (int i = 0; i < 10; i++, k++) { .... no k .... } return k;
791*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(header->GetPhis()); !it.Done(); it.Advance()) {
792*795d594fSAndroid Build Coastguard Worker HPhi* phi = it.Current()->AsPhi();
793*795d594fSAndroid Build Coastguard Worker if (TrySetPhiInduction(phi, /*restrict_uses*/ true) &&
794*795d594fSAndroid Build Coastguard Worker TryAssignLastValue(node->loop_info, phi, preheader, /*collect_loop_uses*/ false)) {
795*795d594fSAndroid Build Coastguard Worker // Note that it's ok to have replaced uses after the loop with the last value, without
796*795d594fSAndroid Build Coastguard Worker // being able to remove the cycle. Environment uses (which are the reason we may not be
797*795d594fSAndroid Build Coastguard Worker // able to remove the cycle) within the loop will still hold the right value. We must
798*795d594fSAndroid Build Coastguard Worker // have tried first, however, to replace outside uses.
799*795d594fSAndroid Build Coastguard Worker if (CanRemoveCycle()) {
800*795d594fSAndroid Build Coastguard Worker simplified_ = true;
801*795d594fSAndroid Build Coastguard Worker for (HInstruction* i : *iset_) {
802*795d594fSAndroid Build Coastguard Worker RemoveFromCycle(i);
803*795d594fSAndroid Build Coastguard Worker }
804*795d594fSAndroid Build Coastguard Worker DCHECK(CheckInductionSetFullyRemoved(iset_));
805*795d594fSAndroid Build Coastguard Worker }
806*795d594fSAndroid Build Coastguard Worker }
807*795d594fSAndroid Build Coastguard Worker }
808*795d594fSAndroid Build Coastguard Worker }
809*795d594fSAndroid Build Coastguard Worker
SimplifyBlocks(LoopNode * node)810*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::SimplifyBlocks(LoopNode* node) {
811*795d594fSAndroid Build Coastguard Worker // Iterate over all basic blocks in the loop-body.
812*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopIterator it(*node->loop_info); !it.Done(); it.Advance()) {
813*795d594fSAndroid Build Coastguard Worker HBasicBlock* block = it.Current();
814*795d594fSAndroid Build Coastguard Worker // Remove dead instructions from the loop-body.
815*795d594fSAndroid Build Coastguard Worker RemoveDeadInstructions(block->GetPhis());
816*795d594fSAndroid Build Coastguard Worker RemoveDeadInstructions(block->GetInstructions());
817*795d594fSAndroid Build Coastguard Worker // Remove trivial control flow blocks from the loop-body.
818*795d594fSAndroid Build Coastguard Worker if (block->GetPredecessors().size() == 1 &&
819*795d594fSAndroid Build Coastguard Worker block->GetSuccessors().size() == 1 &&
820*795d594fSAndroid Build Coastguard Worker block->GetSingleSuccessor()->GetPredecessors().size() == 1) {
821*795d594fSAndroid Build Coastguard Worker simplified_ = true;
822*795d594fSAndroid Build Coastguard Worker block->MergeWith(block->GetSingleSuccessor());
823*795d594fSAndroid Build Coastguard Worker } else if (block->GetSuccessors().size() == 2) {
824*795d594fSAndroid Build Coastguard Worker // Trivial if block can be bypassed to either branch.
825*795d594fSAndroid Build Coastguard Worker HBasicBlock* succ0 = block->GetSuccessors()[0];
826*795d594fSAndroid Build Coastguard Worker HBasicBlock* succ1 = block->GetSuccessors()[1];
827*795d594fSAndroid Build Coastguard Worker HBasicBlock* meet0 = nullptr;
828*795d594fSAndroid Build Coastguard Worker HBasicBlock* meet1 = nullptr;
829*795d594fSAndroid Build Coastguard Worker if (succ0 != succ1 &&
830*795d594fSAndroid Build Coastguard Worker IsGotoBlock(succ0, &meet0) &&
831*795d594fSAndroid Build Coastguard Worker IsGotoBlock(succ1, &meet1) &&
832*795d594fSAndroid Build Coastguard Worker meet0 == meet1 && // meets again
833*795d594fSAndroid Build Coastguard Worker meet0 != block && // no self-loop
834*795d594fSAndroid Build Coastguard Worker meet0->GetPhis().IsEmpty()) { // not used for merging
835*795d594fSAndroid Build Coastguard Worker simplified_ = true;
836*795d594fSAndroid Build Coastguard Worker succ0->DisconnectAndDelete();
837*795d594fSAndroid Build Coastguard Worker if (block->Dominates(meet0)) {
838*795d594fSAndroid Build Coastguard Worker block->RemoveDominatedBlock(meet0);
839*795d594fSAndroid Build Coastguard Worker succ1->AddDominatedBlock(meet0);
840*795d594fSAndroid Build Coastguard Worker meet0->SetDominator(succ1);
841*795d594fSAndroid Build Coastguard Worker }
842*795d594fSAndroid Build Coastguard Worker }
843*795d594fSAndroid Build Coastguard Worker }
844*795d594fSAndroid Build Coastguard Worker }
845*795d594fSAndroid Build Coastguard Worker }
846*795d594fSAndroid Build Coastguard Worker
847*795d594fSAndroid Build Coastguard Worker // Checks whether the loop has exit structure suitable for InnerLoopFinite optimization:
848*795d594fSAndroid Build Coastguard Worker // - has single loop exit.
849*795d594fSAndroid Build Coastguard Worker // - the exit block has only single predecessor - a block inside the loop.
850*795d594fSAndroid Build Coastguard Worker //
851*795d594fSAndroid Build Coastguard Worker // In that case returns single exit basic block (outside the loop); otherwise nullptr.
GetInnerLoopFiniteSingleExit(HLoopInformation * loop_info)852*795d594fSAndroid Build Coastguard Worker static HBasicBlock* GetInnerLoopFiniteSingleExit(HLoopInformation* loop_info) {
853*795d594fSAndroid Build Coastguard Worker HBasicBlock* exit = nullptr;
854*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopIterator block_it(*loop_info);
855*795d594fSAndroid Build Coastguard Worker !block_it.Done();
856*795d594fSAndroid Build Coastguard Worker block_it.Advance()) {
857*795d594fSAndroid Build Coastguard Worker HBasicBlock* block = block_it.Current();
858*795d594fSAndroid Build Coastguard Worker
859*795d594fSAndroid Build Coastguard Worker // Check whether one of the successor is loop exit.
860*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* successor : block->GetSuccessors()) {
861*795d594fSAndroid Build Coastguard Worker if (!loop_info->Contains(*successor)) {
862*795d594fSAndroid Build Coastguard Worker if (exit != nullptr) {
863*795d594fSAndroid Build Coastguard Worker // The loop has more than one exit.
864*795d594fSAndroid Build Coastguard Worker return nullptr;
865*795d594fSAndroid Build Coastguard Worker }
866*795d594fSAndroid Build Coastguard Worker exit = successor;
867*795d594fSAndroid Build Coastguard Worker
868*795d594fSAndroid Build Coastguard Worker // Ensure exit can only be reached by exiting loop.
869*795d594fSAndroid Build Coastguard Worker if (successor->GetPredecessors().size() != 1) {
870*795d594fSAndroid Build Coastguard Worker return nullptr;
871*795d594fSAndroid Build Coastguard Worker }
872*795d594fSAndroid Build Coastguard Worker }
873*795d594fSAndroid Build Coastguard Worker }
874*795d594fSAndroid Build Coastguard Worker }
875*795d594fSAndroid Build Coastguard Worker return exit;
876*795d594fSAndroid Build Coastguard Worker }
877*795d594fSAndroid Build Coastguard Worker
TryOptimizeInnerLoopFinite(LoopNode * node)878*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryOptimizeInnerLoopFinite(LoopNode* node) {
879*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = node->loop_info->GetHeader();
880*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader = node->loop_info->GetPreHeader();
881*795d594fSAndroid Build Coastguard Worker // Ensure loop header logic is finite.
882*795d594fSAndroid Build Coastguard Worker int64_t trip_count = 0;
883*795d594fSAndroid Build Coastguard Worker if (!induction_range_.IsFinite(node->loop_info, &trip_count)) {
884*795d594fSAndroid Build Coastguard Worker return false;
885*795d594fSAndroid Build Coastguard Worker }
886*795d594fSAndroid Build Coastguard Worker // Check loop exits.
887*795d594fSAndroid Build Coastguard Worker HBasicBlock* exit = GetInnerLoopFiniteSingleExit(node->loop_info);
888*795d594fSAndroid Build Coastguard Worker if (exit == nullptr) {
889*795d594fSAndroid Build Coastguard Worker return false;
890*795d594fSAndroid Build Coastguard Worker }
891*795d594fSAndroid Build Coastguard Worker
892*795d594fSAndroid Build Coastguard Worker HBasicBlock* body = (header->GetSuccessors()[0] == exit)
893*795d594fSAndroid Build Coastguard Worker ? header->GetSuccessors()[1]
894*795d594fSAndroid Build Coastguard Worker : header->GetSuccessors()[0];
895*795d594fSAndroid Build Coastguard Worker // Detect either an empty loop (no side effects other than plain iteration) or
896*795d594fSAndroid Build Coastguard Worker // a trivial loop (just iterating once). Replace subsequent index uses, if any,
897*795d594fSAndroid Build Coastguard Worker // with the last value and remove the loop, possibly after unrolling its body.
898*795d594fSAndroid Build Coastguard Worker HPhi* main_phi = nullptr;
899*795d594fSAndroid Build Coastguard Worker size_t num_of_blocks = header->GetLoopInformation()->GetBlocks().NumSetBits();
900*795d594fSAndroid Build Coastguard Worker
901*795d594fSAndroid Build Coastguard Worker if (num_of_blocks == 2 && TrySetSimpleLoopHeader(header, &main_phi)) {
902*795d594fSAndroid Build Coastguard Worker bool is_empty = IsEmptyBody(body);
903*795d594fSAndroid Build Coastguard Worker if (reductions_->empty() && // TODO: possible with some effort
904*795d594fSAndroid Build Coastguard Worker (is_empty || trip_count == 1) &&
905*795d594fSAndroid Build Coastguard Worker TryAssignLastValue(node->loop_info, main_phi, preheader, /*collect_loop_uses*/ true)) {
906*795d594fSAndroid Build Coastguard Worker if (!is_empty) {
907*795d594fSAndroid Build Coastguard Worker // Unroll the loop-body, which sees initial value of the index.
908*795d594fSAndroid Build Coastguard Worker main_phi->ReplaceWith(main_phi->InputAt(0));
909*795d594fSAndroid Build Coastguard Worker preheader->MergeInstructionsWith(body);
910*795d594fSAndroid Build Coastguard Worker }
911*795d594fSAndroid Build Coastguard Worker body->DisconnectAndDelete();
912*795d594fSAndroid Build Coastguard Worker exit->RemovePredecessor(header);
913*795d594fSAndroid Build Coastguard Worker header->RemoveSuccessor(exit);
914*795d594fSAndroid Build Coastguard Worker header->RemoveDominatedBlock(exit);
915*795d594fSAndroid Build Coastguard Worker header->DisconnectAndDelete();
916*795d594fSAndroid Build Coastguard Worker preheader->AddSuccessor(exit);
917*795d594fSAndroid Build Coastguard Worker preheader->AddInstruction(new (global_allocator_) HGoto());
918*795d594fSAndroid Build Coastguard Worker preheader->AddDominatedBlock(exit);
919*795d594fSAndroid Build Coastguard Worker exit->SetDominator(preheader);
920*795d594fSAndroid Build Coastguard Worker RemoveLoop(node); // update hierarchy
921*795d594fSAndroid Build Coastguard Worker return true;
922*795d594fSAndroid Build Coastguard Worker }
923*795d594fSAndroid Build Coastguard Worker }
924*795d594fSAndroid Build Coastguard Worker // Vectorize loop, if possible and valid.
925*795d594fSAndroid Build Coastguard Worker if (!kEnableVectorization ||
926*795d594fSAndroid Build Coastguard Worker // Disable vectorization for debuggable graphs: this is a workaround for the bug
927*795d594fSAndroid Build Coastguard Worker // in 'GenerateNewLoop' which caused the SuspendCheck environment to be invalid.
928*795d594fSAndroid Build Coastguard Worker // TODO: b/138601207, investigate other possible cases with wrong environment values and
929*795d594fSAndroid Build Coastguard Worker // possibly switch back vectorization on for debuggable graphs.
930*795d594fSAndroid Build Coastguard Worker graph_->IsDebuggable()) {
931*795d594fSAndroid Build Coastguard Worker return false;
932*795d594fSAndroid Build Coastguard Worker }
933*795d594fSAndroid Build Coastguard Worker
934*795d594fSAndroid Build Coastguard Worker if (kForceTryPredicatedSIMD && IsInPredicatedVectorizationMode()) {
935*795d594fSAndroid Build Coastguard Worker return TryVectorizePredicated(node, body, exit, main_phi, trip_count);
936*795d594fSAndroid Build Coastguard Worker } else {
937*795d594fSAndroid Build Coastguard Worker return TryVectorizedTraditional(node, body, exit, main_phi, trip_count);
938*795d594fSAndroid Build Coastguard Worker }
939*795d594fSAndroid Build Coastguard Worker }
940*795d594fSAndroid Build Coastguard Worker
TryVectorizePredicated(LoopNode * node,HBasicBlock * body,HBasicBlock * exit,HPhi * main_phi,int64_t trip_count)941*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryVectorizePredicated(LoopNode* node,
942*795d594fSAndroid Build Coastguard Worker HBasicBlock* body,
943*795d594fSAndroid Build Coastguard Worker HBasicBlock* exit,
944*795d594fSAndroid Build Coastguard Worker HPhi* main_phi,
945*795d594fSAndroid Build Coastguard Worker int64_t trip_count) {
946*795d594fSAndroid Build Coastguard Worker if (!IsPredicatedLoopControlFlowSupported(node->loop_info) ||
947*795d594fSAndroid Build Coastguard Worker !ShouldVectorizeCommon(node, main_phi, trip_count)) {
948*795d594fSAndroid Build Coastguard Worker return false;
949*795d594fSAndroid Build Coastguard Worker }
950*795d594fSAndroid Build Coastguard Worker
951*795d594fSAndroid Build Coastguard Worker // Currently we can only generate cleanup loops for loops with 2 basic block.
952*795d594fSAndroid Build Coastguard Worker //
953*795d594fSAndroid Build Coastguard Worker // TODO: Support array disambiguation tests for CF loops.
954*795d594fSAndroid Build Coastguard Worker if (NeedsArrayRefsDisambiguationTest() &&
955*795d594fSAndroid Build Coastguard Worker node->loop_info->GetBlocks().NumSetBits() != 2) {
956*795d594fSAndroid Build Coastguard Worker return false;
957*795d594fSAndroid Build Coastguard Worker }
958*795d594fSAndroid Build Coastguard Worker
959*795d594fSAndroid Build Coastguard Worker VectorizePredicated(node, body, exit);
960*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(stats_, MethodCompilationStat::kLoopVectorized);
961*795d594fSAndroid Build Coastguard Worker graph_->SetHasPredicatedSIMD(true); // flag SIMD usage
962*795d594fSAndroid Build Coastguard Worker return true;
963*795d594fSAndroid Build Coastguard Worker }
964*795d594fSAndroid Build Coastguard Worker
TryVectorizedTraditional(LoopNode * node,HBasicBlock * body,HBasicBlock * exit,HPhi * main_phi,int64_t trip_count)965*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryVectorizedTraditional(LoopNode* node,
966*795d594fSAndroid Build Coastguard Worker HBasicBlock* body,
967*795d594fSAndroid Build Coastguard Worker HBasicBlock* exit,
968*795d594fSAndroid Build Coastguard Worker HPhi* main_phi,
969*795d594fSAndroid Build Coastguard Worker int64_t trip_count) {
970*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = node->loop_info->GetHeader();
971*795d594fSAndroid Build Coastguard Worker size_t num_of_blocks = header->GetLoopInformation()->GetBlocks().NumSetBits();
972*795d594fSAndroid Build Coastguard Worker
973*795d594fSAndroid Build Coastguard Worker if (num_of_blocks != 2 || !ShouldVectorizeCommon(node, main_phi, trip_count)) {
974*795d594fSAndroid Build Coastguard Worker return false;
975*795d594fSAndroid Build Coastguard Worker }
976*795d594fSAndroid Build Coastguard Worker VectorizeTraditional(node, body, exit, trip_count);
977*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(stats_, MethodCompilationStat::kLoopVectorized);
978*795d594fSAndroid Build Coastguard Worker graph_->SetHasTraditionalSIMD(true); // flag SIMD usage
979*795d594fSAndroid Build Coastguard Worker return true;
980*795d594fSAndroid Build Coastguard Worker }
981*795d594fSAndroid Build Coastguard Worker
OptimizeInnerLoop(LoopNode * node)982*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::OptimizeInnerLoop(LoopNode* node) {
983*795d594fSAndroid Build Coastguard Worker return TryOptimizeInnerLoopFinite(node) || TryLoopScalarOpts(node);
984*795d594fSAndroid Build Coastguard Worker }
985*795d594fSAndroid Build Coastguard Worker
986*795d594fSAndroid Build Coastguard Worker //
987*795d594fSAndroid Build Coastguard Worker // Scalar loop peeling and unrolling: generic part methods.
988*795d594fSAndroid Build Coastguard Worker //
989*795d594fSAndroid Build Coastguard Worker
TryUnrollingForBranchPenaltyReduction(LoopAnalysisInfo * analysis_info,bool generate_code)990*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryUnrollingForBranchPenaltyReduction(LoopAnalysisInfo* analysis_info,
991*795d594fSAndroid Build Coastguard Worker bool generate_code) {
992*795d594fSAndroid Build Coastguard Worker if (analysis_info->GetNumberOfExits() > 1) {
993*795d594fSAndroid Build Coastguard Worker return false;
994*795d594fSAndroid Build Coastguard Worker }
995*795d594fSAndroid Build Coastguard Worker
996*795d594fSAndroid Build Coastguard Worker uint32_t unrolling_factor = arch_loop_helper_->GetScalarUnrollingFactor(analysis_info);
997*795d594fSAndroid Build Coastguard Worker if (unrolling_factor == LoopAnalysisInfo::kNoUnrollingFactor) {
998*795d594fSAndroid Build Coastguard Worker return false;
999*795d594fSAndroid Build Coastguard Worker }
1000*795d594fSAndroid Build Coastguard Worker
1001*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1002*795d594fSAndroid Build Coastguard Worker // TODO: support other unrolling factors.
1003*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(unrolling_factor, 2u);
1004*795d594fSAndroid Build Coastguard Worker
1005*795d594fSAndroid Build Coastguard Worker // Perform unrolling.
1006*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = analysis_info->GetLoopInfo();
1007*795d594fSAndroid Build Coastguard Worker LoopClonerSimpleHelper helper(loop_info, &induction_range_);
1008*795d594fSAndroid Build Coastguard Worker helper.DoUnrolling();
1009*795d594fSAndroid Build Coastguard Worker
1010*795d594fSAndroid Build Coastguard Worker // Remove the redundant loop check after unrolling.
1011*795d594fSAndroid Build Coastguard Worker HIf* copy_hif =
1012*795d594fSAndroid Build Coastguard Worker helper.GetBasicBlockMap()->Get(loop_info->GetHeader())->GetLastInstruction()->AsIf();
1013*795d594fSAndroid Build Coastguard Worker int32_t constant = loop_info->Contains(*copy_hif->IfTrueSuccessor()) ? 1 : 0;
1014*795d594fSAndroid Build Coastguard Worker copy_hif->ReplaceInput(graph_->GetIntConstant(constant), 0u);
1015*795d594fSAndroid Build Coastguard Worker }
1016*795d594fSAndroid Build Coastguard Worker return true;
1017*795d594fSAndroid Build Coastguard Worker }
1018*795d594fSAndroid Build Coastguard Worker
TryPeelingForLoopInvariantExitsElimination(LoopAnalysisInfo * analysis_info,bool generate_code)1019*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryPeelingForLoopInvariantExitsElimination(LoopAnalysisInfo* analysis_info,
1020*795d594fSAndroid Build Coastguard Worker bool generate_code) {
1021*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = analysis_info->GetLoopInfo();
1022*795d594fSAndroid Build Coastguard Worker if (!arch_loop_helper_->IsLoopPeelingEnabled()) {
1023*795d594fSAndroid Build Coastguard Worker return false;
1024*795d594fSAndroid Build Coastguard Worker }
1025*795d594fSAndroid Build Coastguard Worker
1026*795d594fSAndroid Build Coastguard Worker if (analysis_info->GetNumberOfInvariantExits() == 0) {
1027*795d594fSAndroid Build Coastguard Worker return false;
1028*795d594fSAndroid Build Coastguard Worker }
1029*795d594fSAndroid Build Coastguard Worker
1030*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1031*795d594fSAndroid Build Coastguard Worker // Perform peeling.
1032*795d594fSAndroid Build Coastguard Worker LoopClonerSimpleHelper helper(loop_info, &induction_range_);
1033*795d594fSAndroid Build Coastguard Worker helper.DoPeeling();
1034*795d594fSAndroid Build Coastguard Worker
1035*795d594fSAndroid Build Coastguard Worker // Statically evaluate loop check after peeling for loop invariant condition.
1036*795d594fSAndroid Build Coastguard Worker const SuperblockCloner::HInstructionMap* hir_map = helper.GetInstructionMap();
1037*795d594fSAndroid Build Coastguard Worker for (auto entry : *hir_map) {
1038*795d594fSAndroid Build Coastguard Worker HInstruction* copy = entry.second;
1039*795d594fSAndroid Build Coastguard Worker if (copy->IsIf()) {
1040*795d594fSAndroid Build Coastguard Worker TryToEvaluateIfCondition(copy->AsIf(), graph_);
1041*795d594fSAndroid Build Coastguard Worker }
1042*795d594fSAndroid Build Coastguard Worker }
1043*795d594fSAndroid Build Coastguard Worker }
1044*795d594fSAndroid Build Coastguard Worker
1045*795d594fSAndroid Build Coastguard Worker return true;
1046*795d594fSAndroid Build Coastguard Worker }
1047*795d594fSAndroid Build Coastguard Worker
TryFullUnrolling(LoopAnalysisInfo * analysis_info,bool generate_code)1048*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryFullUnrolling(LoopAnalysisInfo* analysis_info, bool generate_code) {
1049*795d594fSAndroid Build Coastguard Worker // Fully unroll loops with a known and small trip count.
1050*795d594fSAndroid Build Coastguard Worker int64_t trip_count = analysis_info->GetTripCount();
1051*795d594fSAndroid Build Coastguard Worker if (!arch_loop_helper_->IsLoopPeelingEnabled() ||
1052*795d594fSAndroid Build Coastguard Worker trip_count == LoopAnalysisInfo::kUnknownTripCount ||
1053*795d594fSAndroid Build Coastguard Worker !arch_loop_helper_->IsFullUnrollingBeneficial(analysis_info)) {
1054*795d594fSAndroid Build Coastguard Worker return false;
1055*795d594fSAndroid Build Coastguard Worker }
1056*795d594fSAndroid Build Coastguard Worker
1057*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1058*795d594fSAndroid Build Coastguard Worker // Peeling of the N first iterations (where N equals to the trip count) will effectively
1059*795d594fSAndroid Build Coastguard Worker // eliminate the loop: after peeling we will have N sequential iterations copied into the loop
1060*795d594fSAndroid Build Coastguard Worker // preheader and the original loop. The trip count of this loop will be 0 as the sequential
1061*795d594fSAndroid Build Coastguard Worker // iterations are executed first and there are exactly N of them. Thus we can statically
1062*795d594fSAndroid Build Coastguard Worker // evaluate the loop exit condition to 'false' and fully eliminate it.
1063*795d594fSAndroid Build Coastguard Worker //
1064*795d594fSAndroid Build Coastguard Worker // Here is an example of full unrolling of a loop with a trip count 2:
1065*795d594fSAndroid Build Coastguard Worker //
1066*795d594fSAndroid Build Coastguard Worker // loop_cond_1
1067*795d594fSAndroid Build Coastguard Worker // loop_body_1 <- First iteration.
1068*795d594fSAndroid Build Coastguard Worker // |
1069*795d594fSAndroid Build Coastguard Worker // \ v
1070*795d594fSAndroid Build Coastguard Worker // ==\ loop_cond_2
1071*795d594fSAndroid Build Coastguard Worker // ==/ loop_body_2 <- Second iteration.
1072*795d594fSAndroid Build Coastguard Worker // / |
1073*795d594fSAndroid Build Coastguard Worker // <- v <-
1074*795d594fSAndroid Build Coastguard Worker // loop_cond \ loop_cond \ <- This cond is always false.
1075*795d594fSAndroid Build Coastguard Worker // loop_body _/ loop_body _/
1076*795d594fSAndroid Build Coastguard Worker //
1077*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = analysis_info->GetLoopInfo();
1078*795d594fSAndroid Build Coastguard Worker PeelByCount(loop_info, trip_count, &induction_range_);
1079*795d594fSAndroid Build Coastguard Worker HIf* loop_hif = loop_info->GetHeader()->GetLastInstruction()->AsIf();
1080*795d594fSAndroid Build Coastguard Worker int32_t constant = loop_info->Contains(*loop_hif->IfTrueSuccessor()) ? 0 : 1;
1081*795d594fSAndroid Build Coastguard Worker loop_hif->ReplaceInput(graph_->GetIntConstant(constant), 0u);
1082*795d594fSAndroid Build Coastguard Worker }
1083*795d594fSAndroid Build Coastguard Worker
1084*795d594fSAndroid Build Coastguard Worker return true;
1085*795d594fSAndroid Build Coastguard Worker }
1086*795d594fSAndroid Build Coastguard Worker
TryLoopScalarOpts(LoopNode * node)1087*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryLoopScalarOpts(LoopNode* node) {
1088*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = node->loop_info;
1089*795d594fSAndroid Build Coastguard Worker int64_t trip_count = LoopAnalysis::GetLoopTripCount(loop_info, &induction_range_);
1090*795d594fSAndroid Build Coastguard Worker if (trip_count == 0) {
1091*795d594fSAndroid Build Coastguard Worker // Mark the loop as dead.
1092*795d594fSAndroid Build Coastguard Worker HIf* loop_hif = loop_info->GetHeader()->GetLastInstruction()->AsIf();
1093*795d594fSAndroid Build Coastguard Worker int32_t constant = loop_info->Contains(*loop_hif->IfTrueSuccessor()) ? 0 : 1;
1094*795d594fSAndroid Build Coastguard Worker loop_hif->ReplaceInput(graph_->GetIntConstant(constant), 0u);
1095*795d594fSAndroid Build Coastguard Worker return true;
1096*795d594fSAndroid Build Coastguard Worker }
1097*795d594fSAndroid Build Coastguard Worker
1098*795d594fSAndroid Build Coastguard Worker LoopAnalysisInfo analysis_info(loop_info);
1099*795d594fSAndroid Build Coastguard Worker LoopAnalysis::CalculateLoopBasicProperties(loop_info, &analysis_info, trip_count);
1100*795d594fSAndroid Build Coastguard Worker if (analysis_info.HasInstructionsPreventingScalarOpts() ||
1101*795d594fSAndroid Build Coastguard Worker arch_loop_helper_->IsLoopNonBeneficialForScalarOpts(&analysis_info)) {
1102*795d594fSAndroid Build Coastguard Worker return false;
1103*795d594fSAndroid Build Coastguard Worker }
1104*795d594fSAndroid Build Coastguard Worker
1105*795d594fSAndroid Build Coastguard Worker if (!TryFullUnrolling(&analysis_info, /*generate_code*/ false) &&
1106*795d594fSAndroid Build Coastguard Worker !TryPeelingForLoopInvariantExitsElimination(&analysis_info, /*generate_code*/ false) &&
1107*795d594fSAndroid Build Coastguard Worker !TryUnrollingForBranchPenaltyReduction(&analysis_info, /*generate_code*/ false) &&
1108*795d594fSAndroid Build Coastguard Worker !TryToRemoveSuspendCheckFromLoopHeader(&analysis_info, /*generate_code*/ false)) {
1109*795d594fSAndroid Build Coastguard Worker return false;
1110*795d594fSAndroid Build Coastguard Worker }
1111*795d594fSAndroid Build Coastguard Worker
1112*795d594fSAndroid Build Coastguard Worker // Try the suspend check removal even for non-clonable loops. Also this
1113*795d594fSAndroid Build Coastguard Worker // optimization doesn't interfere with other scalar loop optimizations so it can
1114*795d594fSAndroid Build Coastguard Worker // be done prior to them.
1115*795d594fSAndroid Build Coastguard Worker bool removed_suspend_check = TryToRemoveSuspendCheckFromLoopHeader(&analysis_info);
1116*795d594fSAndroid Build Coastguard Worker
1117*795d594fSAndroid Build Coastguard Worker // Run 'IsLoopClonable' the last as it might be time-consuming.
1118*795d594fSAndroid Build Coastguard Worker if (!LoopClonerHelper::IsLoopClonable(loop_info)) {
1119*795d594fSAndroid Build Coastguard Worker return false;
1120*795d594fSAndroid Build Coastguard Worker }
1121*795d594fSAndroid Build Coastguard Worker
1122*795d594fSAndroid Build Coastguard Worker return TryFullUnrolling(&analysis_info) ||
1123*795d594fSAndroid Build Coastguard Worker TryPeelingForLoopInvariantExitsElimination(&analysis_info) ||
1124*795d594fSAndroid Build Coastguard Worker TryUnrollingForBranchPenaltyReduction(&analysis_info) || removed_suspend_check;
1125*795d594fSAndroid Build Coastguard Worker }
1126*795d594fSAndroid Build Coastguard Worker
1127*795d594fSAndroid Build Coastguard Worker //
1128*795d594fSAndroid Build Coastguard Worker // Loop vectorization. The implementation is based on the book by Aart J.C. Bik:
1129*795d594fSAndroid Build Coastguard Worker // "The Software Vectorization Handbook. Applying Multimedia Extensions for Maximum Performance."
1130*795d594fSAndroid Build Coastguard Worker // Intel Press, June, 2004 (http://www.aartbik.com/).
1131*795d594fSAndroid Build Coastguard Worker //
1132*795d594fSAndroid Build Coastguard Worker
1133*795d594fSAndroid Build Coastguard Worker
CanVectorizeDataFlow(LoopNode * node,HBasicBlock * header,bool collect_alignment_info)1134*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::CanVectorizeDataFlow(LoopNode* node,
1135*795d594fSAndroid Build Coastguard Worker HBasicBlock* header,
1136*795d594fSAndroid Build Coastguard Worker bool collect_alignment_info) {
1137*795d594fSAndroid Build Coastguard Worker // Reset vector bookkeeping.
1138*795d594fSAndroid Build Coastguard Worker vector_length_ = 0;
1139*795d594fSAndroid Build Coastguard Worker vector_refs_->clear();
1140*795d594fSAndroid Build Coastguard Worker vector_static_peeling_factor_ = 0;
1141*795d594fSAndroid Build Coastguard Worker vector_dynamic_peeling_candidate_ = nullptr;
1142*795d594fSAndroid Build Coastguard Worker vector_runtime_test_a_ =
1143*795d594fSAndroid Build Coastguard Worker vector_runtime_test_b_ = nullptr;
1144*795d594fSAndroid Build Coastguard Worker
1145*795d594fSAndroid Build Coastguard Worker // Traverse the data flow of the loop, in the original program order.
1146*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopReversePostOrderIterator block_it(*header->GetLoopInformation());
1147*795d594fSAndroid Build Coastguard Worker !block_it.Done();
1148*795d594fSAndroid Build Coastguard Worker block_it.Advance()) {
1149*795d594fSAndroid Build Coastguard Worker HBasicBlock* block = block_it.Current();
1150*795d594fSAndroid Build Coastguard Worker
1151*795d594fSAndroid Build Coastguard Worker if (block == header) {
1152*795d594fSAndroid Build Coastguard Worker // The header is of a certain structure (TrySetSimpleLoopHeader) and doesn't need to be
1153*795d594fSAndroid Build Coastguard Worker // processed here.
1154*795d594fSAndroid Build Coastguard Worker continue;
1155*795d594fSAndroid Build Coastguard Worker }
1156*795d594fSAndroid Build Coastguard Worker
1157*795d594fSAndroid Build Coastguard Worker // Phis in the loop-body prevent vectorization.
1158*795d594fSAndroid Build Coastguard Worker // TODO: Enable vectorization of CF loops with Phis.
1159*795d594fSAndroid Build Coastguard Worker if (!block->GetPhis().IsEmpty()) {
1160*795d594fSAndroid Build Coastguard Worker return false;
1161*795d594fSAndroid Build Coastguard Worker }
1162*795d594fSAndroid Build Coastguard Worker
1163*795d594fSAndroid Build Coastguard Worker // Scan the loop-body instructions, starting a right-hand-side tree traversal at each
1164*795d594fSAndroid Build Coastguard Worker // left-hand-side occurrence, which allows passing down attributes down the use tree.
1165*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
1166*795d594fSAndroid Build Coastguard Worker if (!VectorizeDef(node, it.Current(), /*generate_code*/ false)) {
1167*795d594fSAndroid Build Coastguard Worker return false; // failure to vectorize a left-hand-side
1168*795d594fSAndroid Build Coastguard Worker }
1169*795d594fSAndroid Build Coastguard Worker }
1170*795d594fSAndroid Build Coastguard Worker }
1171*795d594fSAndroid Build Coastguard Worker
1172*795d594fSAndroid Build Coastguard Worker // Prepare alignment analysis:
1173*795d594fSAndroid Build Coastguard Worker // (1) find desired alignment (SIMD vector size in bytes).
1174*795d594fSAndroid Build Coastguard Worker // (2) initialize static loop peeling votes (peeling factor that will
1175*795d594fSAndroid Build Coastguard Worker // make one particular reference aligned), never to exceed (1).
1176*795d594fSAndroid Build Coastguard Worker // (3) variable to record how many references share same alignment.
1177*795d594fSAndroid Build Coastguard Worker // (4) variable to record suitable candidate for dynamic loop peeling.
1178*795d594fSAndroid Build Coastguard Worker size_t desired_alignment = GetVectorSizeInBytes();
1179*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<uint32_t> peeling_votes(desired_alignment, 0u,
1180*795d594fSAndroid Build Coastguard Worker loop_allocator_->Adapter(kArenaAllocLoopOptimization));
1181*795d594fSAndroid Build Coastguard Worker
1182*795d594fSAndroid Build Coastguard Worker uint32_t max_num_same_alignment = 0;
1183*795d594fSAndroid Build Coastguard Worker const ArrayReference* peeling_candidate = nullptr;
1184*795d594fSAndroid Build Coastguard Worker
1185*795d594fSAndroid Build Coastguard Worker // Data dependence analysis. Find each pair of references with same type, where
1186*795d594fSAndroid Build Coastguard Worker // at least one is a write. Each such pair denotes a possible data dependence.
1187*795d594fSAndroid Build Coastguard Worker // This analysis exploits the property that differently typed arrays cannot be
1188*795d594fSAndroid Build Coastguard Worker // aliased, as well as the property that references either point to the same
1189*795d594fSAndroid Build Coastguard Worker // array or to two completely disjoint arrays, i.e., no partial aliasing.
1190*795d594fSAndroid Build Coastguard Worker // Other than a few simply heuristics, no detailed subscript analysis is done.
1191*795d594fSAndroid Build Coastguard Worker // The scan over references also prepares finding a suitable alignment strategy.
1192*795d594fSAndroid Build Coastguard Worker for (auto i = vector_refs_->begin(); i != vector_refs_->end(); ++i) {
1193*795d594fSAndroid Build Coastguard Worker uint32_t num_same_alignment = 0;
1194*795d594fSAndroid Build Coastguard Worker // Scan over all next references.
1195*795d594fSAndroid Build Coastguard Worker for (auto j = i; ++j != vector_refs_->end(); ) {
1196*795d594fSAndroid Build Coastguard Worker if (i->type == j->type && (i->lhs || j->lhs)) {
1197*795d594fSAndroid Build Coastguard Worker // Found same-typed a[i+x] vs. b[i+y], where at least one is a write.
1198*795d594fSAndroid Build Coastguard Worker HInstruction* a = i->base;
1199*795d594fSAndroid Build Coastguard Worker HInstruction* b = j->base;
1200*795d594fSAndroid Build Coastguard Worker HInstruction* x = i->offset;
1201*795d594fSAndroid Build Coastguard Worker HInstruction* y = j->offset;
1202*795d594fSAndroid Build Coastguard Worker if (a == b) {
1203*795d594fSAndroid Build Coastguard Worker // Found a[i+x] vs. a[i+y]. Accept if x == y (loop-independent data dependence).
1204*795d594fSAndroid Build Coastguard Worker // Conservatively assume a loop-carried data dependence otherwise, and reject.
1205*795d594fSAndroid Build Coastguard Worker if (x != y) {
1206*795d594fSAndroid Build Coastguard Worker return false;
1207*795d594fSAndroid Build Coastguard Worker }
1208*795d594fSAndroid Build Coastguard Worker // Count the number of references that have the same alignment (since
1209*795d594fSAndroid Build Coastguard Worker // base and offset are the same) and where at least one is a write, so
1210*795d594fSAndroid Build Coastguard Worker // e.g. a[i] = a[i] + b[i] counts a[i] but not b[i]).
1211*795d594fSAndroid Build Coastguard Worker num_same_alignment++;
1212*795d594fSAndroid Build Coastguard Worker } else {
1213*795d594fSAndroid Build Coastguard Worker // Found a[i+x] vs. b[i+y]. Accept if x == y (at worst loop-independent data dependence).
1214*795d594fSAndroid Build Coastguard Worker // Conservatively assume a potential loop-carried data dependence otherwise, avoided by
1215*795d594fSAndroid Build Coastguard Worker // generating an explicit a != b disambiguation runtime test on the two references.
1216*795d594fSAndroid Build Coastguard Worker if (x != y) {
1217*795d594fSAndroid Build Coastguard Worker // To avoid excessive overhead, we only accept one a != b test.
1218*795d594fSAndroid Build Coastguard Worker if (vector_runtime_test_a_ == nullptr) {
1219*795d594fSAndroid Build Coastguard Worker // First test found.
1220*795d594fSAndroid Build Coastguard Worker vector_runtime_test_a_ = a;
1221*795d594fSAndroid Build Coastguard Worker vector_runtime_test_b_ = b;
1222*795d594fSAndroid Build Coastguard Worker } else if ((vector_runtime_test_a_ != a || vector_runtime_test_b_ != b) &&
1223*795d594fSAndroid Build Coastguard Worker (vector_runtime_test_a_ != b || vector_runtime_test_b_ != a)) {
1224*795d594fSAndroid Build Coastguard Worker return false; // second test would be needed
1225*795d594fSAndroid Build Coastguard Worker }
1226*795d594fSAndroid Build Coastguard Worker }
1227*795d594fSAndroid Build Coastguard Worker }
1228*795d594fSAndroid Build Coastguard Worker }
1229*795d594fSAndroid Build Coastguard Worker }
1230*795d594fSAndroid Build Coastguard Worker // Update information for finding suitable alignment strategy:
1231*795d594fSAndroid Build Coastguard Worker // (1) update votes for static loop peeling,
1232*795d594fSAndroid Build Coastguard Worker // (2) update suitable candidate for dynamic loop peeling.
1233*795d594fSAndroid Build Coastguard Worker Alignment alignment = ComputeAlignment(i->offset, i->type, i->is_string_char_at);
1234*795d594fSAndroid Build Coastguard Worker if (alignment.Base() >= desired_alignment) {
1235*795d594fSAndroid Build Coastguard Worker // If the array/string object has a known, sufficient alignment, use the
1236*795d594fSAndroid Build Coastguard Worker // initial offset to compute the static loop peeling vote (this always
1237*795d594fSAndroid Build Coastguard Worker // works, since elements have natural alignment).
1238*795d594fSAndroid Build Coastguard Worker uint32_t offset = alignment.Offset() & (desired_alignment - 1u);
1239*795d594fSAndroid Build Coastguard Worker uint32_t vote = (offset == 0)
1240*795d594fSAndroid Build Coastguard Worker ? 0
1241*795d594fSAndroid Build Coastguard Worker : ((desired_alignment - offset) >> DataType::SizeShift(i->type));
1242*795d594fSAndroid Build Coastguard Worker DCHECK_LT(vote, 16u);
1243*795d594fSAndroid Build Coastguard Worker ++peeling_votes[vote];
1244*795d594fSAndroid Build Coastguard Worker } else if (BaseAlignment() >= desired_alignment &&
1245*795d594fSAndroid Build Coastguard Worker num_same_alignment > max_num_same_alignment) {
1246*795d594fSAndroid Build Coastguard Worker // Otherwise, if the array/string object has a known, sufficient alignment
1247*795d594fSAndroid Build Coastguard Worker // for just the base but with an unknown offset, record the candidate with
1248*795d594fSAndroid Build Coastguard Worker // the most occurrences for dynamic loop peeling (again, the peeling always
1249*795d594fSAndroid Build Coastguard Worker // works, since elements have natural alignment).
1250*795d594fSAndroid Build Coastguard Worker max_num_same_alignment = num_same_alignment;
1251*795d594fSAndroid Build Coastguard Worker peeling_candidate = &(*i);
1252*795d594fSAndroid Build Coastguard Worker }
1253*795d594fSAndroid Build Coastguard Worker } // for i
1254*795d594fSAndroid Build Coastguard Worker
1255*795d594fSAndroid Build Coastguard Worker if (collect_alignment_info) {
1256*795d594fSAndroid Build Coastguard Worker // Update the info on alignment strategy.
1257*795d594fSAndroid Build Coastguard Worker SetAlignmentStrategy(peeling_votes, peeling_candidate);
1258*795d594fSAndroid Build Coastguard Worker }
1259*795d594fSAndroid Build Coastguard Worker
1260*795d594fSAndroid Build Coastguard Worker // Success!
1261*795d594fSAndroid Build Coastguard Worker return true;
1262*795d594fSAndroid Build Coastguard Worker }
1263*795d594fSAndroid Build Coastguard Worker
ShouldVectorizeCommon(LoopNode * node,HPhi * main_phi,int64_t trip_count)1264*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::ShouldVectorizeCommon(LoopNode* node,
1265*795d594fSAndroid Build Coastguard Worker HPhi* main_phi,
1266*795d594fSAndroid Build Coastguard Worker int64_t trip_count) {
1267*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = node->loop_info->GetHeader();
1268*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader = node->loop_info->GetPreHeader();
1269*795d594fSAndroid Build Coastguard Worker
1270*795d594fSAndroid Build Coastguard Worker bool enable_alignment_strategies = !IsInPredicatedVectorizationMode();
1271*795d594fSAndroid Build Coastguard Worker if (!TrySetSimpleLoopHeader(header, &main_phi) ||
1272*795d594fSAndroid Build Coastguard Worker !CanVectorizeDataFlow(node, header, enable_alignment_strategies) ||
1273*795d594fSAndroid Build Coastguard Worker !IsVectorizationProfitable(trip_count) ||
1274*795d594fSAndroid Build Coastguard Worker !TryAssignLastValue(node->loop_info, main_phi, preheader, /*collect_loop_uses*/ true)) {
1275*795d594fSAndroid Build Coastguard Worker return false;
1276*795d594fSAndroid Build Coastguard Worker }
1277*795d594fSAndroid Build Coastguard Worker
1278*795d594fSAndroid Build Coastguard Worker return true;
1279*795d594fSAndroid Build Coastguard Worker }
1280*795d594fSAndroid Build Coastguard Worker
VectorizePredicated(LoopNode * node,HBasicBlock * block,HBasicBlock * exit)1281*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::VectorizePredicated(LoopNode* node,
1282*795d594fSAndroid Build Coastguard Worker HBasicBlock* block,
1283*795d594fSAndroid Build Coastguard Worker HBasicBlock* exit) {
1284*795d594fSAndroid Build Coastguard Worker DCHECK(IsInPredicatedVectorizationMode());
1285*795d594fSAndroid Build Coastguard Worker
1286*795d594fSAndroid Build Coastguard Worker vector_external_set_->clear();
1287*795d594fSAndroid Build Coastguard Worker
1288*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = node->loop_info->GetHeader();
1289*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader = node->loop_info->GetPreHeader();
1290*795d594fSAndroid Build Coastguard Worker
1291*795d594fSAndroid Build Coastguard Worker // Adjust vector bookkeeping.
1292*795d594fSAndroid Build Coastguard Worker HPhi* main_phi = nullptr;
1293*795d594fSAndroid Build Coastguard Worker bool is_simple_loop_header = TrySetSimpleLoopHeader(header, &main_phi); // refills sets
1294*795d594fSAndroid Build Coastguard Worker DCHECK(is_simple_loop_header);
1295*795d594fSAndroid Build Coastguard Worker vector_header_ = header;
1296*795d594fSAndroid Build Coastguard Worker vector_body_ = block;
1297*795d594fSAndroid Build Coastguard Worker
1298*795d594fSAndroid Build Coastguard Worker // Loop induction type.
1299*795d594fSAndroid Build Coastguard Worker DataType::Type induc_type = main_phi->GetType();
1300*795d594fSAndroid Build Coastguard Worker DCHECK(induc_type == DataType::Type::kInt32 || induc_type == DataType::Type::kInt64)
1301*795d594fSAndroid Build Coastguard Worker << induc_type;
1302*795d594fSAndroid Build Coastguard Worker
1303*795d594fSAndroid Build Coastguard Worker // Generate loop control:
1304*795d594fSAndroid Build Coastguard Worker // stc = <trip-count>;
1305*795d594fSAndroid Build Coastguard Worker // vtc = <vector trip-count>
1306*795d594fSAndroid Build Coastguard Worker HInstruction* stc = induction_range_.GenerateTripCount(node->loop_info, graph_, preheader);
1307*795d594fSAndroid Build Coastguard Worker HInstruction* vtc = stc;
1308*795d594fSAndroid Build Coastguard Worker vector_index_ = graph_->GetConstant(induc_type, 0);
1309*795d594fSAndroid Build Coastguard Worker bool needs_disambiguation_test = false;
1310*795d594fSAndroid Build Coastguard Worker // Generate runtime disambiguation test:
1311*795d594fSAndroid Build Coastguard Worker // vtc = a != b ? vtc : 0;
1312*795d594fSAndroid Build Coastguard Worker if (NeedsArrayRefsDisambiguationTest()) {
1313*795d594fSAndroid Build Coastguard Worker HInstruction* rt = Insert(
1314*795d594fSAndroid Build Coastguard Worker preheader,
1315*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HNotEqual(vector_runtime_test_a_, vector_runtime_test_b_));
1316*795d594fSAndroid Build Coastguard Worker vtc = Insert(preheader,
1317*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
1318*795d594fSAndroid Build Coastguard Worker HSelect(rt, vtc, graph_->GetConstant(induc_type, 0), kNoDexPc));
1319*795d594fSAndroid Build Coastguard Worker needs_disambiguation_test = true;
1320*795d594fSAndroid Build Coastguard Worker }
1321*795d594fSAndroid Build Coastguard Worker
1322*795d594fSAndroid Build Coastguard Worker // Generate vector loop:
1323*795d594fSAndroid Build Coastguard Worker // for ( ; i < vtc; i += vector_length)
1324*795d594fSAndroid Build Coastguard Worker // <vectorized-loop-body>
1325*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader_for_vector_loop =
1326*795d594fSAndroid Build Coastguard Worker graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit);
1327*795d594fSAndroid Build Coastguard Worker synthesis_mode_ = LoopSynthesisMode::kVector;
1328*795d594fSAndroid Build Coastguard Worker GenerateNewLoopPredicated(node,
1329*795d594fSAndroid Build Coastguard Worker preheader_for_vector_loop,
1330*795d594fSAndroid Build Coastguard Worker vector_index_,
1331*795d594fSAndroid Build Coastguard Worker vtc,
1332*795d594fSAndroid Build Coastguard Worker graph_->GetConstant(induc_type, vector_length_));
1333*795d594fSAndroid Build Coastguard Worker
1334*795d594fSAndroid Build Coastguard Worker // Generate scalar loop, if needed:
1335*795d594fSAndroid Build Coastguard Worker // for ( ; i < stc; i += 1)
1336*795d594fSAndroid Build Coastguard Worker // <loop-body>
1337*795d594fSAndroid Build Coastguard Worker if (needs_disambiguation_test) {
1338*795d594fSAndroid Build Coastguard Worker synthesis_mode_ = LoopSynthesisMode::kSequential;
1339*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader_for_cleanup_loop =
1340*795d594fSAndroid Build Coastguard Worker graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit);
1341*795d594fSAndroid Build Coastguard Worker // Use "Traditional" version for the sequential loop.
1342*795d594fSAndroid Build Coastguard Worker GenerateNewLoopScalarOrTraditional(node,
1343*795d594fSAndroid Build Coastguard Worker preheader_for_cleanup_loop,
1344*795d594fSAndroid Build Coastguard Worker vector_index_,
1345*795d594fSAndroid Build Coastguard Worker stc,
1346*795d594fSAndroid Build Coastguard Worker graph_->GetConstant(induc_type, 1),
1347*795d594fSAndroid Build Coastguard Worker LoopAnalysisInfo::kNoUnrollingFactor);
1348*795d594fSAndroid Build Coastguard Worker }
1349*795d594fSAndroid Build Coastguard Worker
1350*795d594fSAndroid Build Coastguard Worker FinalizeVectorization(node);
1351*795d594fSAndroid Build Coastguard Worker
1352*795d594fSAndroid Build Coastguard Worker // Assign governing predicates for the predicated instructions inserted during vectorization
1353*795d594fSAndroid Build Coastguard Worker // outside the loop.
1354*795d594fSAndroid Build Coastguard Worker for (auto it : *vector_external_set_) {
1355*795d594fSAndroid Build Coastguard Worker DCHECK(it->IsVecOperation());
1356*795d594fSAndroid Build Coastguard Worker HVecOperation* vec_op = it->AsVecOperation();
1357*795d594fSAndroid Build Coastguard Worker
1358*795d594fSAndroid Build Coastguard Worker HVecPredSetAll* set_pred = new (global_allocator_) HVecPredSetAll(global_allocator_,
1359*795d594fSAndroid Build Coastguard Worker graph_->GetIntConstant(1),
1360*795d594fSAndroid Build Coastguard Worker vec_op->GetPackedType(),
1361*795d594fSAndroid Build Coastguard Worker vec_op->GetVectorLength(),
1362*795d594fSAndroid Build Coastguard Worker 0u);
1363*795d594fSAndroid Build Coastguard Worker vec_op->GetBlock()->InsertInstructionBefore(set_pred, vec_op);
1364*795d594fSAndroid Build Coastguard Worker vec_op->SetMergingGoverningPredicate(set_pred);
1365*795d594fSAndroid Build Coastguard Worker }
1366*795d594fSAndroid Build Coastguard Worker }
1367*795d594fSAndroid Build Coastguard Worker
VectorizeTraditional(LoopNode * node,HBasicBlock * block,HBasicBlock * exit,int64_t trip_count)1368*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::VectorizeTraditional(LoopNode* node,
1369*795d594fSAndroid Build Coastguard Worker HBasicBlock* block,
1370*795d594fSAndroid Build Coastguard Worker HBasicBlock* exit,
1371*795d594fSAndroid Build Coastguard Worker int64_t trip_count) {
1372*795d594fSAndroid Build Coastguard Worker DCHECK(!IsInPredicatedVectorizationMode());
1373*795d594fSAndroid Build Coastguard Worker
1374*795d594fSAndroid Build Coastguard Worker vector_external_set_->clear();
1375*795d594fSAndroid Build Coastguard Worker
1376*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = node->loop_info->GetHeader();
1377*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader = node->loop_info->GetPreHeader();
1378*795d594fSAndroid Build Coastguard Worker
1379*795d594fSAndroid Build Coastguard Worker // Pick a loop unrolling factor for the vector loop.
1380*795d594fSAndroid Build Coastguard Worker uint32_t unroll = arch_loop_helper_->GetSIMDUnrollingFactor(
1381*795d594fSAndroid Build Coastguard Worker block, trip_count, MaxNumberPeeled(), vector_length_);
1382*795d594fSAndroid Build Coastguard Worker uint32_t chunk = vector_length_ * unroll;
1383*795d594fSAndroid Build Coastguard Worker
1384*795d594fSAndroid Build Coastguard Worker DCHECK(trip_count == 0 || (trip_count >= MaxNumberPeeled() + chunk));
1385*795d594fSAndroid Build Coastguard Worker
1386*795d594fSAndroid Build Coastguard Worker // A cleanup loop is needed, at least, for any unknown trip count or
1387*795d594fSAndroid Build Coastguard Worker // for a known trip count with remainder iterations after vectorization.
1388*795d594fSAndroid Build Coastguard Worker bool needs_cleanup =
1389*795d594fSAndroid Build Coastguard Worker (trip_count == 0 || ((trip_count - vector_static_peeling_factor_) % chunk) != 0);
1390*795d594fSAndroid Build Coastguard Worker
1391*795d594fSAndroid Build Coastguard Worker // Adjust vector bookkeeping.
1392*795d594fSAndroid Build Coastguard Worker HPhi* main_phi = nullptr;
1393*795d594fSAndroid Build Coastguard Worker bool is_simple_loop_header = TrySetSimpleLoopHeader(header, &main_phi); // refills sets
1394*795d594fSAndroid Build Coastguard Worker DCHECK(is_simple_loop_header);
1395*795d594fSAndroid Build Coastguard Worker vector_header_ = header;
1396*795d594fSAndroid Build Coastguard Worker vector_body_ = block;
1397*795d594fSAndroid Build Coastguard Worker
1398*795d594fSAndroid Build Coastguard Worker // Loop induction type.
1399*795d594fSAndroid Build Coastguard Worker DataType::Type induc_type = main_phi->GetType();
1400*795d594fSAndroid Build Coastguard Worker DCHECK(induc_type == DataType::Type::kInt32 || induc_type == DataType::Type::kInt64)
1401*795d594fSAndroid Build Coastguard Worker << induc_type;
1402*795d594fSAndroid Build Coastguard Worker
1403*795d594fSAndroid Build Coastguard Worker // Generate the trip count for static or dynamic loop peeling, if needed:
1404*795d594fSAndroid Build Coastguard Worker // ptc = <peeling factor>;
1405*795d594fSAndroid Build Coastguard Worker HInstruction* ptc = nullptr;
1406*795d594fSAndroid Build Coastguard Worker if (vector_static_peeling_factor_ != 0) {
1407*795d594fSAndroid Build Coastguard Worker // Static loop peeling for SIMD alignment (using the most suitable
1408*795d594fSAndroid Build Coastguard Worker // fixed peeling factor found during prior alignment analysis).
1409*795d594fSAndroid Build Coastguard Worker DCHECK(vector_dynamic_peeling_candidate_ == nullptr);
1410*795d594fSAndroid Build Coastguard Worker ptc = graph_->GetConstant(induc_type, vector_static_peeling_factor_);
1411*795d594fSAndroid Build Coastguard Worker } else if (vector_dynamic_peeling_candidate_ != nullptr) {
1412*795d594fSAndroid Build Coastguard Worker // Dynamic loop peeling for SIMD alignment (using the most suitable
1413*795d594fSAndroid Build Coastguard Worker // candidate found during prior alignment analysis):
1414*795d594fSAndroid Build Coastguard Worker // rem = offset % ALIGN; // adjusted as #elements
1415*795d594fSAndroid Build Coastguard Worker // ptc = rem == 0 ? 0 : (ALIGN - rem);
1416*795d594fSAndroid Build Coastguard Worker uint32_t shift = DataType::SizeShift(vector_dynamic_peeling_candidate_->type);
1417*795d594fSAndroid Build Coastguard Worker uint32_t align = GetVectorSizeInBytes() >> shift;
1418*795d594fSAndroid Build Coastguard Worker uint32_t hidden_offset = HiddenOffset(vector_dynamic_peeling_candidate_->type,
1419*795d594fSAndroid Build Coastguard Worker vector_dynamic_peeling_candidate_->is_string_char_at);
1420*795d594fSAndroid Build Coastguard Worker HInstruction* adjusted_offset = graph_->GetConstant(induc_type, hidden_offset >> shift);
1421*795d594fSAndroid Build Coastguard Worker HInstruction* offset = Insert(preheader, new (global_allocator_) HAdd(
1422*795d594fSAndroid Build Coastguard Worker induc_type, vector_dynamic_peeling_candidate_->offset, adjusted_offset));
1423*795d594fSAndroid Build Coastguard Worker HInstruction* rem = Insert(preheader, new (global_allocator_) HAnd(
1424*795d594fSAndroid Build Coastguard Worker induc_type, offset, graph_->GetConstant(induc_type, align - 1u)));
1425*795d594fSAndroid Build Coastguard Worker HInstruction* sub = Insert(preheader, new (global_allocator_) HSub(
1426*795d594fSAndroid Build Coastguard Worker induc_type, graph_->GetConstant(induc_type, align), rem));
1427*795d594fSAndroid Build Coastguard Worker HInstruction* cond = Insert(preheader, new (global_allocator_) HEqual(
1428*795d594fSAndroid Build Coastguard Worker rem, graph_->GetConstant(induc_type, 0)));
1429*795d594fSAndroid Build Coastguard Worker ptc = Insert(preheader, new (global_allocator_) HSelect(
1430*795d594fSAndroid Build Coastguard Worker cond, graph_->GetConstant(induc_type, 0), sub, kNoDexPc));
1431*795d594fSAndroid Build Coastguard Worker needs_cleanup = true; // don't know the exact amount
1432*795d594fSAndroid Build Coastguard Worker }
1433*795d594fSAndroid Build Coastguard Worker
1434*795d594fSAndroid Build Coastguard Worker // Generate loop control:
1435*795d594fSAndroid Build Coastguard Worker // stc = <trip-count>;
1436*795d594fSAndroid Build Coastguard Worker // ptc = min(stc, ptc);
1437*795d594fSAndroid Build Coastguard Worker // vtc = stc - (stc - ptc) % chunk;
1438*795d594fSAndroid Build Coastguard Worker // i = 0;
1439*795d594fSAndroid Build Coastguard Worker HInstruction* stc = induction_range_.GenerateTripCount(node->loop_info, graph_, preheader);
1440*795d594fSAndroid Build Coastguard Worker HInstruction* vtc = stc;
1441*795d594fSAndroid Build Coastguard Worker if (needs_cleanup) {
1442*795d594fSAndroid Build Coastguard Worker DCHECK(IsPowerOfTwo(chunk));
1443*795d594fSAndroid Build Coastguard Worker HInstruction* diff = stc;
1444*795d594fSAndroid Build Coastguard Worker if (ptc != nullptr) {
1445*795d594fSAndroid Build Coastguard Worker if (trip_count == 0) {
1446*795d594fSAndroid Build Coastguard Worker HInstruction* cond = Insert(preheader, new (global_allocator_) HAboveOrEqual(stc, ptc));
1447*795d594fSAndroid Build Coastguard Worker ptc = Insert(preheader, new (global_allocator_) HSelect(cond, ptc, stc, kNoDexPc));
1448*795d594fSAndroid Build Coastguard Worker }
1449*795d594fSAndroid Build Coastguard Worker diff = Insert(preheader, new (global_allocator_) HSub(induc_type, stc, ptc));
1450*795d594fSAndroid Build Coastguard Worker }
1451*795d594fSAndroid Build Coastguard Worker HInstruction* rem = Insert(
1452*795d594fSAndroid Build Coastguard Worker preheader, new (global_allocator_) HAnd(induc_type,
1453*795d594fSAndroid Build Coastguard Worker diff,
1454*795d594fSAndroid Build Coastguard Worker graph_->GetConstant(induc_type, chunk - 1)));
1455*795d594fSAndroid Build Coastguard Worker vtc = Insert(preheader, new (global_allocator_) HSub(induc_type, stc, rem));
1456*795d594fSAndroid Build Coastguard Worker }
1457*795d594fSAndroid Build Coastguard Worker vector_index_ = graph_->GetConstant(induc_type, 0);
1458*795d594fSAndroid Build Coastguard Worker
1459*795d594fSAndroid Build Coastguard Worker // Generate runtime disambiguation test:
1460*795d594fSAndroid Build Coastguard Worker // vtc = a != b ? vtc : 0;
1461*795d594fSAndroid Build Coastguard Worker if (NeedsArrayRefsDisambiguationTest()) {
1462*795d594fSAndroid Build Coastguard Worker HInstruction* rt = Insert(
1463*795d594fSAndroid Build Coastguard Worker preheader,
1464*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HNotEqual(vector_runtime_test_a_, vector_runtime_test_b_));
1465*795d594fSAndroid Build Coastguard Worker vtc = Insert(preheader,
1466*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
1467*795d594fSAndroid Build Coastguard Worker HSelect(rt, vtc, graph_->GetConstant(induc_type, 0), kNoDexPc));
1468*795d594fSAndroid Build Coastguard Worker needs_cleanup = true;
1469*795d594fSAndroid Build Coastguard Worker }
1470*795d594fSAndroid Build Coastguard Worker
1471*795d594fSAndroid Build Coastguard Worker // Generate alignment peeling loop, if needed:
1472*795d594fSAndroid Build Coastguard Worker // for ( ; i < ptc; i += 1)
1473*795d594fSAndroid Build Coastguard Worker // <loop-body>
1474*795d594fSAndroid Build Coastguard Worker //
1475*795d594fSAndroid Build Coastguard Worker // NOTE: The alignment forced by the peeling loop is preserved even if data is
1476*795d594fSAndroid Build Coastguard Worker // moved around during suspend checks, since all analysis was based on
1477*795d594fSAndroid Build Coastguard Worker // nothing more than the Android runtime alignment conventions.
1478*795d594fSAndroid Build Coastguard Worker if (ptc != nullptr) {
1479*795d594fSAndroid Build Coastguard Worker synthesis_mode_ = LoopSynthesisMode::kSequential;
1480*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader_for_peeling_loop =
1481*795d594fSAndroid Build Coastguard Worker graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit);
1482*795d594fSAndroid Build Coastguard Worker GenerateNewLoopScalarOrTraditional(node,
1483*795d594fSAndroid Build Coastguard Worker preheader_for_peeling_loop,
1484*795d594fSAndroid Build Coastguard Worker vector_index_,
1485*795d594fSAndroid Build Coastguard Worker ptc,
1486*795d594fSAndroid Build Coastguard Worker graph_->GetConstant(induc_type, 1),
1487*795d594fSAndroid Build Coastguard Worker LoopAnalysisInfo::kNoUnrollingFactor);
1488*795d594fSAndroid Build Coastguard Worker }
1489*795d594fSAndroid Build Coastguard Worker
1490*795d594fSAndroid Build Coastguard Worker // Generate vector loop, possibly further unrolled:
1491*795d594fSAndroid Build Coastguard Worker // for ( ; i < vtc; i += chunk)
1492*795d594fSAndroid Build Coastguard Worker // <vectorized-loop-body>
1493*795d594fSAndroid Build Coastguard Worker synthesis_mode_ = LoopSynthesisMode::kVector;
1494*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader_for_vector_loop =
1495*795d594fSAndroid Build Coastguard Worker graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit);
1496*795d594fSAndroid Build Coastguard Worker GenerateNewLoopScalarOrTraditional(node,
1497*795d594fSAndroid Build Coastguard Worker preheader_for_vector_loop,
1498*795d594fSAndroid Build Coastguard Worker vector_index_,
1499*795d594fSAndroid Build Coastguard Worker vtc,
1500*795d594fSAndroid Build Coastguard Worker graph_->GetConstant(induc_type, vector_length_), // per unroll
1501*795d594fSAndroid Build Coastguard Worker unroll);
1502*795d594fSAndroid Build Coastguard Worker
1503*795d594fSAndroid Build Coastguard Worker // Generate cleanup loop, if needed:
1504*795d594fSAndroid Build Coastguard Worker // for ( ; i < stc; i += 1)
1505*795d594fSAndroid Build Coastguard Worker // <loop-body>
1506*795d594fSAndroid Build Coastguard Worker if (needs_cleanup) {
1507*795d594fSAndroid Build Coastguard Worker synthesis_mode_ = LoopSynthesisMode::kSequential;
1508*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader_for_cleanup_loop =
1509*795d594fSAndroid Build Coastguard Worker graph_->TransformLoopForVectorization(vector_header_, vector_body_, exit);
1510*795d594fSAndroid Build Coastguard Worker GenerateNewLoopScalarOrTraditional(node,
1511*795d594fSAndroid Build Coastguard Worker preheader_for_cleanup_loop,
1512*795d594fSAndroid Build Coastguard Worker vector_index_,
1513*795d594fSAndroid Build Coastguard Worker stc,
1514*795d594fSAndroid Build Coastguard Worker graph_->GetConstant(induc_type, 1),
1515*795d594fSAndroid Build Coastguard Worker LoopAnalysisInfo::kNoUnrollingFactor);
1516*795d594fSAndroid Build Coastguard Worker }
1517*795d594fSAndroid Build Coastguard Worker
1518*795d594fSAndroid Build Coastguard Worker FinalizeVectorization(node);
1519*795d594fSAndroid Build Coastguard Worker }
1520*795d594fSAndroid Build Coastguard Worker
FinalizeVectorization(LoopNode * node)1521*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::FinalizeVectorization(LoopNode* node) {
1522*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = node->loop_info->GetHeader();
1523*795d594fSAndroid Build Coastguard Worker HBasicBlock* preheader = node->loop_info->GetPreHeader();
1524*795d594fSAndroid Build Coastguard Worker HLoopInformation* vloop = vector_header_->GetLoopInformation();
1525*795d594fSAndroid Build Coastguard Worker // Link reductions to their final uses.
1526*795d594fSAndroid Build Coastguard Worker for (auto i = reductions_->begin(); i != reductions_->end(); ++i) {
1527*795d594fSAndroid Build Coastguard Worker if (i->first->IsPhi()) {
1528*795d594fSAndroid Build Coastguard Worker HInstruction* phi = i->first;
1529*795d594fSAndroid Build Coastguard Worker HInstruction* repl = ReduceAndExtractIfNeeded(i->second);
1530*795d594fSAndroid Build Coastguard Worker // Deal with regular uses.
1531*795d594fSAndroid Build Coastguard Worker for (const HUseListNode<HInstruction*>& use : phi->GetUses()) {
1532*795d594fSAndroid Build Coastguard Worker induction_range_.Replace(use.GetUser(), phi, repl); // update induction use
1533*795d594fSAndroid Build Coastguard Worker }
1534*795d594fSAndroid Build Coastguard Worker phi->ReplaceWith(repl);
1535*795d594fSAndroid Build Coastguard Worker }
1536*795d594fSAndroid Build Coastguard Worker }
1537*795d594fSAndroid Build Coastguard Worker
1538*795d594fSAndroid Build Coastguard Worker // Remove the original loop.
1539*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopPostOrderIterator it_loop(*node->loop_info);
1540*795d594fSAndroid Build Coastguard Worker !it_loop.Done();
1541*795d594fSAndroid Build Coastguard Worker it_loop.Advance()) {
1542*795d594fSAndroid Build Coastguard Worker HBasicBlock* cur_block = it_loop.Current();
1543*795d594fSAndroid Build Coastguard Worker if (cur_block == node->loop_info->GetHeader()) {
1544*795d594fSAndroid Build Coastguard Worker continue;
1545*795d594fSAndroid Build Coastguard Worker }
1546*795d594fSAndroid Build Coastguard Worker cur_block->DisconnectAndDelete();
1547*795d594fSAndroid Build Coastguard Worker }
1548*795d594fSAndroid Build Coastguard Worker
1549*795d594fSAndroid Build Coastguard Worker while (!header->GetFirstInstruction()->IsGoto()) {
1550*795d594fSAndroid Build Coastguard Worker header->RemoveInstruction(header->GetFirstInstruction());
1551*795d594fSAndroid Build Coastguard Worker }
1552*795d594fSAndroid Build Coastguard Worker
1553*795d594fSAndroid Build Coastguard Worker // Update loop hierarchy: the old header now resides in the same outer loop
1554*795d594fSAndroid Build Coastguard Worker // as the old preheader. Note that we don't bother putting sequential
1555*795d594fSAndroid Build Coastguard Worker // loops back in the hierarchy at this point.
1556*795d594fSAndroid Build Coastguard Worker header->SetLoopInformation(preheader->GetLoopInformation()); // outward
1557*795d594fSAndroid Build Coastguard Worker node->loop_info = vloop;
1558*795d594fSAndroid Build Coastguard Worker }
1559*795d594fSAndroid Build Coastguard Worker
InitializeForNewLoop(HBasicBlock * new_preheader,HInstruction * lo)1560*795d594fSAndroid Build Coastguard Worker HPhi* HLoopOptimization::InitializeForNewLoop(HBasicBlock* new_preheader, HInstruction* lo) {
1561*795d594fSAndroid Build Coastguard Worker DataType::Type induc_type = lo->GetType();
1562*795d594fSAndroid Build Coastguard Worker // Prepare new loop.
1563*795d594fSAndroid Build Coastguard Worker vector_preheader_ = new_preheader,
1564*795d594fSAndroid Build Coastguard Worker vector_header_ = vector_preheader_->GetSingleSuccessor();
1565*795d594fSAndroid Build Coastguard Worker vector_body_ = vector_header_->GetSuccessors()[1];
1566*795d594fSAndroid Build Coastguard Worker HPhi* phi = new (global_allocator_) HPhi(global_allocator_,
1567*795d594fSAndroid Build Coastguard Worker kNoRegNumber,
1568*795d594fSAndroid Build Coastguard Worker 0,
1569*795d594fSAndroid Build Coastguard Worker HPhi::ToPhiType(induc_type));
1570*795d594fSAndroid Build Coastguard Worker vector_header_->AddPhi(phi);
1571*795d594fSAndroid Build Coastguard Worker vector_index_ = phi;
1572*795d594fSAndroid Build Coastguard Worker vector_permanent_map_->clear();
1573*795d594fSAndroid Build Coastguard Worker predicate_info_map_->clear();
1574*795d594fSAndroid Build Coastguard Worker
1575*795d594fSAndroid Build Coastguard Worker return phi;
1576*795d594fSAndroid Build Coastguard Worker }
1577*795d594fSAndroid Build Coastguard Worker
GenerateNewLoopScalarOrTraditional(LoopNode * node,HBasicBlock * new_preheader,HInstruction * lo,HInstruction * hi,HInstruction * step,uint32_t unroll)1578*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateNewLoopScalarOrTraditional(LoopNode* node,
1579*795d594fSAndroid Build Coastguard Worker HBasicBlock* new_preheader,
1580*795d594fSAndroid Build Coastguard Worker HInstruction* lo,
1581*795d594fSAndroid Build Coastguard Worker HInstruction* hi,
1582*795d594fSAndroid Build Coastguard Worker HInstruction* step,
1583*795d594fSAndroid Build Coastguard Worker uint32_t unroll) {
1584*795d594fSAndroid Build Coastguard Worker DCHECK(unroll == 1 || synthesis_mode_ == LoopSynthesisMode::kVector);
1585*795d594fSAndroid Build Coastguard Worker DataType::Type induc_type = lo->GetType();
1586*795d594fSAndroid Build Coastguard Worker HPhi* phi = InitializeForNewLoop(new_preheader, lo);
1587*795d594fSAndroid Build Coastguard Worker
1588*795d594fSAndroid Build Coastguard Worker // Generate loop exit check.
1589*795d594fSAndroid Build Coastguard Worker HInstruction* cond = new (global_allocator_) HAboveOrEqual(phi, hi);
1590*795d594fSAndroid Build Coastguard Worker vector_header_->AddInstruction(cond);
1591*795d594fSAndroid Build Coastguard Worker vector_header_->AddInstruction(new (global_allocator_) HIf(cond));
1592*795d594fSAndroid Build Coastguard Worker
1593*795d594fSAndroid Build Coastguard Worker for (uint32_t u = 0; u < unroll; u++) {
1594*795d594fSAndroid Build Coastguard Worker GenerateNewLoopBodyOnce(node, induc_type, step);
1595*795d594fSAndroid Build Coastguard Worker }
1596*795d594fSAndroid Build Coastguard Worker
1597*795d594fSAndroid Build Coastguard Worker FinalizePhisForNewLoop(phi, lo);
1598*795d594fSAndroid Build Coastguard Worker }
1599*795d594fSAndroid Build Coastguard Worker
GenerateNewLoopPredicated(LoopNode * node,HBasicBlock * new_preheader,HInstruction * lo,HInstruction * hi,HInstruction * step)1600*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateNewLoopPredicated(LoopNode* node,
1601*795d594fSAndroid Build Coastguard Worker HBasicBlock* new_preheader,
1602*795d594fSAndroid Build Coastguard Worker HInstruction* lo,
1603*795d594fSAndroid Build Coastguard Worker HInstruction* hi,
1604*795d594fSAndroid Build Coastguard Worker HInstruction* step) {
1605*795d594fSAndroid Build Coastguard Worker DCHECK(IsInPredicatedVectorizationMode());
1606*795d594fSAndroid Build Coastguard Worker DCHECK(synthesis_mode_ == LoopSynthesisMode::kVector);
1607*795d594fSAndroid Build Coastguard Worker DataType::Type induc_type = lo->GetType();
1608*795d594fSAndroid Build Coastguard Worker HPhi* phi = InitializeForNewLoop(new_preheader, lo);
1609*795d594fSAndroid Build Coastguard Worker
1610*795d594fSAndroid Build Coastguard Worker // Generate loop exit check.
1611*795d594fSAndroid Build Coastguard Worker HVecPredWhile* pred_while =
1612*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecPredWhile(global_allocator_,
1613*795d594fSAndroid Build Coastguard Worker phi,
1614*795d594fSAndroid Build Coastguard Worker hi,
1615*795d594fSAndroid Build Coastguard Worker HVecPredWhile::CondKind::kLO,
1616*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
1617*795d594fSAndroid Build Coastguard Worker vector_length_,
1618*795d594fSAndroid Build Coastguard Worker 0u);
1619*795d594fSAndroid Build Coastguard Worker
1620*795d594fSAndroid Build Coastguard Worker HInstruction* cond =
1621*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecPredToBoolean(global_allocator_,
1622*795d594fSAndroid Build Coastguard Worker pred_while,
1623*795d594fSAndroid Build Coastguard Worker HVecPredToBoolean::PCondKind::kNFirst,
1624*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32,
1625*795d594fSAndroid Build Coastguard Worker vector_length_,
1626*795d594fSAndroid Build Coastguard Worker 0u);
1627*795d594fSAndroid Build Coastguard Worker
1628*795d594fSAndroid Build Coastguard Worker vector_header_->AddInstruction(pred_while);
1629*795d594fSAndroid Build Coastguard Worker vector_header_->AddInstruction(cond);
1630*795d594fSAndroid Build Coastguard Worker vector_header_->AddInstruction(new (global_allocator_) HIf(cond));
1631*795d594fSAndroid Build Coastguard Worker
1632*795d594fSAndroid Build Coastguard Worker PreparePredicateInfoMap(node);
1633*795d594fSAndroid Build Coastguard Worker GenerateNewLoopBodyOnce(node, induc_type, step);
1634*795d594fSAndroid Build Coastguard Worker InitPredicateInfoMap(node, pred_while);
1635*795d594fSAndroid Build Coastguard Worker
1636*795d594fSAndroid Build Coastguard Worker // Assign governing predicates for instructions in the loop; the traversal order doesn't matter.
1637*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopIterator block_it(*node->loop_info);
1638*795d594fSAndroid Build Coastguard Worker !block_it.Done();
1639*795d594fSAndroid Build Coastguard Worker block_it.Advance()) {
1640*795d594fSAndroid Build Coastguard Worker HBasicBlock* cur_block = block_it.Current();
1641*795d594fSAndroid Build Coastguard Worker
1642*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(cur_block->GetInstructions()); !it.Done(); it.Advance()) {
1643*795d594fSAndroid Build Coastguard Worker auto i = vector_map_->find(it.Current());
1644*795d594fSAndroid Build Coastguard Worker if (i != vector_map_->end()) {
1645*795d594fSAndroid Build Coastguard Worker HInstruction* instr = i->second;
1646*795d594fSAndroid Build Coastguard Worker
1647*795d594fSAndroid Build Coastguard Worker if (!instr->IsVecOperation()) {
1648*795d594fSAndroid Build Coastguard Worker continue;
1649*795d594fSAndroid Build Coastguard Worker }
1650*795d594fSAndroid Build Coastguard Worker // There are cases when a vector instruction, which corresponds to some instruction in the
1651*795d594fSAndroid Build Coastguard Worker // original scalar loop, is located not in the newly created vector loop but
1652*795d594fSAndroid Build Coastguard Worker // in the vector loop preheader (and hence recorded in vector_external_set_).
1653*795d594fSAndroid Build Coastguard Worker //
1654*795d594fSAndroid Build Coastguard Worker // Governing predicates will be set for such instructions separately.
1655*795d594fSAndroid Build Coastguard Worker bool in_vector_loop = vector_header_->GetLoopInformation()->Contains(*instr->GetBlock());
1656*795d594fSAndroid Build Coastguard Worker DCHECK_IMPLIES(!in_vector_loop,
1657*795d594fSAndroid Build Coastguard Worker vector_external_set_->find(instr) != vector_external_set_->end());
1658*795d594fSAndroid Build Coastguard Worker
1659*795d594fSAndroid Build Coastguard Worker if (in_vector_loop &&
1660*795d594fSAndroid Build Coastguard Worker !instr->AsVecOperation()->IsPredicated()) {
1661*795d594fSAndroid Build Coastguard Worker HVecOperation* op = instr->AsVecOperation();
1662*795d594fSAndroid Build Coastguard Worker HVecPredSetOperation* pred = predicate_info_map_->Get(cur_block)->GetControlPredicate();
1663*795d594fSAndroid Build Coastguard Worker op->SetMergingGoverningPredicate(pred);
1664*795d594fSAndroid Build Coastguard Worker }
1665*795d594fSAndroid Build Coastguard Worker }
1666*795d594fSAndroid Build Coastguard Worker }
1667*795d594fSAndroid Build Coastguard Worker }
1668*795d594fSAndroid Build Coastguard Worker
1669*795d594fSAndroid Build Coastguard Worker FinalizePhisForNewLoop(phi, lo);
1670*795d594fSAndroid Build Coastguard Worker }
1671*795d594fSAndroid Build Coastguard Worker
GenerateNewLoopBodyOnce(LoopNode * node,DataType::Type induc_type,HInstruction * step)1672*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateNewLoopBodyOnce(LoopNode* node,
1673*795d594fSAndroid Build Coastguard Worker DataType::Type induc_type,
1674*795d594fSAndroid Build Coastguard Worker HInstruction* step) {
1675*795d594fSAndroid Build Coastguard Worker // Generate instruction map.
1676*795d594fSAndroid Build Coastguard Worker vector_map_->clear();
1677*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = node->loop_info;
1678*795d594fSAndroid Build Coastguard Worker
1679*795d594fSAndroid Build Coastguard Worker // Traverse the data flow of the loop, in the original program order.
1680*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopReversePostOrderIterator block_it(*loop_info);
1681*795d594fSAndroid Build Coastguard Worker !block_it.Done();
1682*795d594fSAndroid Build Coastguard Worker block_it.Advance()) {
1683*795d594fSAndroid Build Coastguard Worker HBasicBlock* cur_block = block_it.Current();
1684*795d594fSAndroid Build Coastguard Worker
1685*795d594fSAndroid Build Coastguard Worker if (cur_block == loop_info->GetHeader()) {
1686*795d594fSAndroid Build Coastguard Worker continue;
1687*795d594fSAndroid Build Coastguard Worker }
1688*795d594fSAndroid Build Coastguard Worker
1689*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(cur_block->GetInstructions()); !it.Done(); it.Advance()) {
1690*795d594fSAndroid Build Coastguard Worker bool vectorized_def = VectorizeDef(node, it.Current(), /*generate_code*/ true);
1691*795d594fSAndroid Build Coastguard Worker DCHECK(vectorized_def);
1692*795d594fSAndroid Build Coastguard Worker }
1693*795d594fSAndroid Build Coastguard Worker }
1694*795d594fSAndroid Build Coastguard Worker
1695*795d594fSAndroid Build Coastguard Worker // Generate body from the instruction map, in the original program order.
1696*795d594fSAndroid Build Coastguard Worker HEnvironment* env = vector_header_->GetFirstInstruction()->GetEnvironment();
1697*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopReversePostOrderIterator block_it(*loop_info);
1698*795d594fSAndroid Build Coastguard Worker !block_it.Done();
1699*795d594fSAndroid Build Coastguard Worker block_it.Advance()) {
1700*795d594fSAndroid Build Coastguard Worker HBasicBlock* cur_block = block_it.Current();
1701*795d594fSAndroid Build Coastguard Worker
1702*795d594fSAndroid Build Coastguard Worker if (cur_block == loop_info->GetHeader()) {
1703*795d594fSAndroid Build Coastguard Worker continue;
1704*795d594fSAndroid Build Coastguard Worker }
1705*795d594fSAndroid Build Coastguard Worker
1706*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(cur_block->GetInstructions()); !it.Done(); it.Advance()) {
1707*795d594fSAndroid Build Coastguard Worker auto i = vector_map_->find(it.Current());
1708*795d594fSAndroid Build Coastguard Worker if (i != vector_map_->end() && !i->second->IsInBlock()) {
1709*795d594fSAndroid Build Coastguard Worker Insert(vector_body_, i->second);
1710*795d594fSAndroid Build Coastguard Worker // Deal with instructions that need an environment, such as the scalar intrinsics.
1711*795d594fSAndroid Build Coastguard Worker if (i->second->NeedsEnvironment()) {
1712*795d594fSAndroid Build Coastguard Worker i->second->CopyEnvironmentFromWithLoopPhiAdjustment(env, vector_header_);
1713*795d594fSAndroid Build Coastguard Worker }
1714*795d594fSAndroid Build Coastguard Worker }
1715*795d594fSAndroid Build Coastguard Worker }
1716*795d594fSAndroid Build Coastguard Worker }
1717*795d594fSAndroid Build Coastguard Worker // Generate the induction.
1718*795d594fSAndroid Build Coastguard Worker vector_index_ = new (global_allocator_) HAdd(induc_type, vector_index_, step);
1719*795d594fSAndroid Build Coastguard Worker Insert(vector_body_, vector_index_);
1720*795d594fSAndroid Build Coastguard Worker }
1721*795d594fSAndroid Build Coastguard Worker
FinalizePhisForNewLoop(HPhi * phi,HInstruction * lo)1722*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::FinalizePhisForNewLoop(HPhi* phi, HInstruction* lo) {
1723*795d594fSAndroid Build Coastguard Worker // Finalize phi inputs for the reductions (if any).
1724*795d594fSAndroid Build Coastguard Worker for (auto i = reductions_->begin(); i != reductions_->end(); ++i) {
1725*795d594fSAndroid Build Coastguard Worker if (!i->first->IsPhi()) {
1726*795d594fSAndroid Build Coastguard Worker DCHECK(i->second->IsPhi());
1727*795d594fSAndroid Build Coastguard Worker GenerateVecReductionPhiInputs(i->second->AsPhi(), i->first);
1728*795d594fSAndroid Build Coastguard Worker }
1729*795d594fSAndroid Build Coastguard Worker }
1730*795d594fSAndroid Build Coastguard Worker // Finalize phi inputs for the loop index.
1731*795d594fSAndroid Build Coastguard Worker phi->AddInput(lo);
1732*795d594fSAndroid Build Coastguard Worker phi->AddInput(vector_index_);
1733*795d594fSAndroid Build Coastguard Worker vector_index_ = phi;
1734*795d594fSAndroid Build Coastguard Worker }
1735*795d594fSAndroid Build Coastguard Worker
VectorizeDef(LoopNode * node,HInstruction * instruction,bool generate_code)1736*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::VectorizeDef(LoopNode* node,
1737*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
1738*795d594fSAndroid Build Coastguard Worker bool generate_code) {
1739*795d594fSAndroid Build Coastguard Worker // Accept a left-hand-side array base[index] for
1740*795d594fSAndroid Build Coastguard Worker // (1) supported vector type,
1741*795d594fSAndroid Build Coastguard Worker // (2) loop-invariant base,
1742*795d594fSAndroid Build Coastguard Worker // (3) unit stride index,
1743*795d594fSAndroid Build Coastguard Worker // (4) vectorizable right-hand-side value.
1744*795d594fSAndroid Build Coastguard Worker uint64_t restrictions = kNone;
1745*795d594fSAndroid Build Coastguard Worker // Don't accept expressions that can throw.
1746*795d594fSAndroid Build Coastguard Worker if (instruction->CanThrow()) {
1747*795d594fSAndroid Build Coastguard Worker return false;
1748*795d594fSAndroid Build Coastguard Worker }
1749*795d594fSAndroid Build Coastguard Worker if (instruction->IsArraySet()) {
1750*795d594fSAndroid Build Coastguard Worker DataType::Type type = instruction->AsArraySet()->GetComponentType();
1751*795d594fSAndroid Build Coastguard Worker HInstruction* base = instruction->InputAt(0);
1752*795d594fSAndroid Build Coastguard Worker HInstruction* index = instruction->InputAt(1);
1753*795d594fSAndroid Build Coastguard Worker HInstruction* value = instruction->InputAt(2);
1754*795d594fSAndroid Build Coastguard Worker HInstruction* offset = nullptr;
1755*795d594fSAndroid Build Coastguard Worker // For narrow types, explicit type conversion may have been
1756*795d594fSAndroid Build Coastguard Worker // optimized way, so set the no hi bits restriction here.
1757*795d594fSAndroid Build Coastguard Worker if (DataType::Size(type) <= 2) {
1758*795d594fSAndroid Build Coastguard Worker restrictions |= kNoHiBits;
1759*795d594fSAndroid Build Coastguard Worker }
1760*795d594fSAndroid Build Coastguard Worker if (TrySetVectorType(type, &restrictions) &&
1761*795d594fSAndroid Build Coastguard Worker node->loop_info->IsDefinedOutOfTheLoop(base) &&
1762*795d594fSAndroid Build Coastguard Worker induction_range_.IsUnitStride(instruction->GetBlock(), index, graph_, &offset) &&
1763*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, value, generate_code, type, restrictions)) {
1764*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1765*795d594fSAndroid Build Coastguard Worker GenerateVecSub(index, offset);
1766*795d594fSAndroid Build Coastguard Worker GenerateVecMem(instruction, vector_map_->Get(index), vector_map_->Get(value), offset, type);
1767*795d594fSAndroid Build Coastguard Worker } else {
1768*795d594fSAndroid Build Coastguard Worker vector_refs_->insert(ArrayReference(base, offset, type, /*lhs*/ true));
1769*795d594fSAndroid Build Coastguard Worker }
1770*795d594fSAndroid Build Coastguard Worker return true;
1771*795d594fSAndroid Build Coastguard Worker }
1772*795d594fSAndroid Build Coastguard Worker return false;
1773*795d594fSAndroid Build Coastguard Worker }
1774*795d594fSAndroid Build Coastguard Worker // Accept a left-hand-side reduction for
1775*795d594fSAndroid Build Coastguard Worker // (1) supported vector type,
1776*795d594fSAndroid Build Coastguard Worker // (2) vectorizable right-hand-side value.
1777*795d594fSAndroid Build Coastguard Worker auto redit = reductions_->find(instruction);
1778*795d594fSAndroid Build Coastguard Worker if (redit != reductions_->end()) {
1779*795d594fSAndroid Build Coastguard Worker DataType::Type type = instruction->GetType();
1780*795d594fSAndroid Build Coastguard Worker // Recognize SAD idiom or direct reduction.
1781*795d594fSAndroid Build Coastguard Worker if (VectorizeSADIdiom(node, instruction, generate_code, type, restrictions) ||
1782*795d594fSAndroid Build Coastguard Worker VectorizeDotProdIdiom(node, instruction, generate_code, type, restrictions) ||
1783*795d594fSAndroid Build Coastguard Worker (TrySetVectorType(type, &restrictions) &&
1784*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, instruction, generate_code, type, restrictions))) {
1785*795d594fSAndroid Build Coastguard Worker DCHECK(!instruction->IsPhi());
1786*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1787*795d594fSAndroid Build Coastguard Worker HInstruction* new_red_vec_op = vector_map_->Get(instruction);
1788*795d594fSAndroid Build Coastguard Worker HInstruction* original_phi = redit->second;
1789*795d594fSAndroid Build Coastguard Worker DCHECK(original_phi->IsPhi());
1790*795d594fSAndroid Build Coastguard Worker vector_permanent_map_->Put(new_red_vec_op, vector_map_->Get(original_phi));
1791*795d594fSAndroid Build Coastguard Worker vector_permanent_map_->Overwrite(original_phi, new_red_vec_op);
1792*795d594fSAndroid Build Coastguard Worker }
1793*795d594fSAndroid Build Coastguard Worker return true;
1794*795d594fSAndroid Build Coastguard Worker }
1795*795d594fSAndroid Build Coastguard Worker return false;
1796*795d594fSAndroid Build Coastguard Worker }
1797*795d594fSAndroid Build Coastguard Worker // Branch back okay.
1798*795d594fSAndroid Build Coastguard Worker if (instruction->IsGoto()) {
1799*795d594fSAndroid Build Coastguard Worker return true;
1800*795d594fSAndroid Build Coastguard Worker }
1801*795d594fSAndroid Build Coastguard Worker
1802*795d594fSAndroid Build Coastguard Worker if (instruction->IsIf()) {
1803*795d594fSAndroid Build Coastguard Worker return VectorizeIfCondition(node, instruction, generate_code, restrictions);
1804*795d594fSAndroid Build Coastguard Worker }
1805*795d594fSAndroid Build Coastguard Worker // Otherwise accept only expressions with no effects outside the immediate loop-body.
1806*795d594fSAndroid Build Coastguard Worker // Note that actual uses are inspected during right-hand-side tree traversal.
1807*795d594fSAndroid Build Coastguard Worker return !IsUsedOutsideLoop(node->loop_info, instruction)
1808*795d594fSAndroid Build Coastguard Worker && !instruction->DoesAnyWrite();
1809*795d594fSAndroid Build Coastguard Worker }
1810*795d594fSAndroid Build Coastguard Worker
VectorizeUse(LoopNode * node,HInstruction * instruction,bool generate_code,DataType::Type type,uint64_t restrictions)1811*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::VectorizeUse(LoopNode* node,
1812*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
1813*795d594fSAndroid Build Coastguard Worker bool generate_code,
1814*795d594fSAndroid Build Coastguard Worker DataType::Type type,
1815*795d594fSAndroid Build Coastguard Worker uint64_t restrictions) {
1816*795d594fSAndroid Build Coastguard Worker // Accept anything for which code has already been generated.
1817*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1818*795d594fSAndroid Build Coastguard Worker if (vector_map_->find(instruction) != vector_map_->end()) {
1819*795d594fSAndroid Build Coastguard Worker return true;
1820*795d594fSAndroid Build Coastguard Worker }
1821*795d594fSAndroid Build Coastguard Worker }
1822*795d594fSAndroid Build Coastguard Worker // Continue the right-hand-side tree traversal, passing in proper
1823*795d594fSAndroid Build Coastguard Worker // types and vector restrictions along the way. During code generation,
1824*795d594fSAndroid Build Coastguard Worker // all new nodes are drawn from the global allocator.
1825*795d594fSAndroid Build Coastguard Worker if (node->loop_info->IsDefinedOutOfTheLoop(instruction)) {
1826*795d594fSAndroid Build Coastguard Worker // Accept invariant use, using scalar expansion.
1827*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1828*795d594fSAndroid Build Coastguard Worker GenerateVecInv(instruction, type);
1829*795d594fSAndroid Build Coastguard Worker }
1830*795d594fSAndroid Build Coastguard Worker return true;
1831*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsArrayGet()) {
1832*795d594fSAndroid Build Coastguard Worker // Deal with vector restrictions.
1833*795d594fSAndroid Build Coastguard Worker bool is_string_char_at = instruction->AsArrayGet()->IsStringCharAt();
1834*795d594fSAndroid Build Coastguard Worker
1835*795d594fSAndroid Build Coastguard Worker if (is_string_char_at && (HasVectorRestrictions(restrictions, kNoStringCharAt))) {
1836*795d594fSAndroid Build Coastguard Worker return false;
1837*795d594fSAndroid Build Coastguard Worker }
1838*795d594fSAndroid Build Coastguard Worker // Accept a right-hand-side array base[index] for
1839*795d594fSAndroid Build Coastguard Worker // (1) matching vector type (exact match or signed/unsigned integral type of the same size),
1840*795d594fSAndroid Build Coastguard Worker // (2) loop-invariant base,
1841*795d594fSAndroid Build Coastguard Worker // (3) unit stride index,
1842*795d594fSAndroid Build Coastguard Worker // (4) vectorizable right-hand-side value.
1843*795d594fSAndroid Build Coastguard Worker HInstruction* base = instruction->InputAt(0);
1844*795d594fSAndroid Build Coastguard Worker HInstruction* index = instruction->InputAt(1);
1845*795d594fSAndroid Build Coastguard Worker HInstruction* offset = nullptr;
1846*795d594fSAndroid Build Coastguard Worker if (HVecOperation::ToSignedType(type) == HVecOperation::ToSignedType(instruction->GetType()) &&
1847*795d594fSAndroid Build Coastguard Worker node->loop_info->IsDefinedOutOfTheLoop(base) &&
1848*795d594fSAndroid Build Coastguard Worker induction_range_.IsUnitStride(instruction->GetBlock(), index, graph_, &offset)) {
1849*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1850*795d594fSAndroid Build Coastguard Worker GenerateVecSub(index, offset);
1851*795d594fSAndroid Build Coastguard Worker GenerateVecMem(instruction, vector_map_->Get(index), nullptr, offset, type);
1852*795d594fSAndroid Build Coastguard Worker } else {
1853*795d594fSAndroid Build Coastguard Worker vector_refs_->insert(ArrayReference(base, offset, type, /*lhs*/ false, is_string_char_at));
1854*795d594fSAndroid Build Coastguard Worker }
1855*795d594fSAndroid Build Coastguard Worker return true;
1856*795d594fSAndroid Build Coastguard Worker }
1857*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsPhi()) {
1858*795d594fSAndroid Build Coastguard Worker // Accept particular phi operations.
1859*795d594fSAndroid Build Coastguard Worker if (reductions_->find(instruction) != reductions_->end()) {
1860*795d594fSAndroid Build Coastguard Worker // Deal with vector restrictions.
1861*795d594fSAndroid Build Coastguard Worker if (HasVectorRestrictions(restrictions, kNoReduction)) {
1862*795d594fSAndroid Build Coastguard Worker return false;
1863*795d594fSAndroid Build Coastguard Worker }
1864*795d594fSAndroid Build Coastguard Worker // Accept a reduction.
1865*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1866*795d594fSAndroid Build Coastguard Worker GenerateVecReductionPhi(instruction->AsPhi());
1867*795d594fSAndroid Build Coastguard Worker }
1868*795d594fSAndroid Build Coastguard Worker return true;
1869*795d594fSAndroid Build Coastguard Worker }
1870*795d594fSAndroid Build Coastguard Worker // TODO: accept right-hand-side induction?
1871*795d594fSAndroid Build Coastguard Worker return false;
1872*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsTypeConversion()) {
1873*795d594fSAndroid Build Coastguard Worker // Accept particular type conversions.
1874*795d594fSAndroid Build Coastguard Worker HTypeConversion* conversion = instruction->AsTypeConversion();
1875*795d594fSAndroid Build Coastguard Worker HInstruction* opa = conversion->InputAt(0);
1876*795d594fSAndroid Build Coastguard Worker DataType::Type from = conversion->GetInputType();
1877*795d594fSAndroid Build Coastguard Worker DataType::Type to = conversion->GetResultType();
1878*795d594fSAndroid Build Coastguard Worker if (DataType::IsIntegralType(from) && DataType::IsIntegralType(to)) {
1879*795d594fSAndroid Build Coastguard Worker uint32_t size_vec = DataType::Size(type);
1880*795d594fSAndroid Build Coastguard Worker uint32_t size_from = DataType::Size(from);
1881*795d594fSAndroid Build Coastguard Worker uint32_t size_to = DataType::Size(to);
1882*795d594fSAndroid Build Coastguard Worker // Accept an integral conversion
1883*795d594fSAndroid Build Coastguard Worker // (1a) narrowing into vector type, "wider" operations cannot bring in higher order bits, or
1884*795d594fSAndroid Build Coastguard Worker // (1b) widening from at least vector type, and
1885*795d594fSAndroid Build Coastguard Worker // (2) vectorizable operand.
1886*795d594fSAndroid Build Coastguard Worker if ((size_to < size_from &&
1887*795d594fSAndroid Build Coastguard Worker size_to == size_vec &&
1888*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, opa, generate_code, type, restrictions | kNoHiBits)) ||
1889*795d594fSAndroid Build Coastguard Worker (size_to >= size_from &&
1890*795d594fSAndroid Build Coastguard Worker size_from >= size_vec &&
1891*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, opa, generate_code, type, restrictions))) {
1892*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1893*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kVector) {
1894*795d594fSAndroid Build Coastguard Worker vector_map_->Put(instruction, vector_map_->Get(opa)); // operand pass-through
1895*795d594fSAndroid Build Coastguard Worker } else {
1896*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(opa), nullptr, type);
1897*795d594fSAndroid Build Coastguard Worker }
1898*795d594fSAndroid Build Coastguard Worker }
1899*795d594fSAndroid Build Coastguard Worker return true;
1900*795d594fSAndroid Build Coastguard Worker }
1901*795d594fSAndroid Build Coastguard Worker } else if (to == DataType::Type::kFloat32 && from == DataType::Type::kInt32) {
1902*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(to, type);
1903*795d594fSAndroid Build Coastguard Worker // Accept int to float conversion for
1904*795d594fSAndroid Build Coastguard Worker // (1) supported int,
1905*795d594fSAndroid Build Coastguard Worker // (2) vectorizable operand.
1906*795d594fSAndroid Build Coastguard Worker if (TrySetVectorType(from, &restrictions) &&
1907*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, opa, generate_code, from, restrictions)) {
1908*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1909*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(opa), nullptr, type);
1910*795d594fSAndroid Build Coastguard Worker }
1911*795d594fSAndroid Build Coastguard Worker return true;
1912*795d594fSAndroid Build Coastguard Worker }
1913*795d594fSAndroid Build Coastguard Worker }
1914*795d594fSAndroid Build Coastguard Worker return false;
1915*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsNeg() || instruction->IsNot() || instruction->IsBooleanNot()) {
1916*795d594fSAndroid Build Coastguard Worker // Accept unary operator for vectorizable operand.
1917*795d594fSAndroid Build Coastguard Worker HInstruction* opa = instruction->InputAt(0);
1918*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, opa, generate_code, type, restrictions)) {
1919*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1920*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(opa), nullptr, type);
1921*795d594fSAndroid Build Coastguard Worker }
1922*795d594fSAndroid Build Coastguard Worker return true;
1923*795d594fSAndroid Build Coastguard Worker }
1924*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsAdd() || instruction->IsSub() ||
1925*795d594fSAndroid Build Coastguard Worker instruction->IsMul() || instruction->IsDiv() ||
1926*795d594fSAndroid Build Coastguard Worker instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1927*795d594fSAndroid Build Coastguard Worker // Deal with vector restrictions.
1928*795d594fSAndroid Build Coastguard Worker if ((instruction->IsMul() && HasVectorRestrictions(restrictions, kNoMul)) ||
1929*795d594fSAndroid Build Coastguard Worker (instruction->IsDiv() && HasVectorRestrictions(restrictions, kNoDiv))) {
1930*795d594fSAndroid Build Coastguard Worker return false;
1931*795d594fSAndroid Build Coastguard Worker }
1932*795d594fSAndroid Build Coastguard Worker // Accept binary operator for vectorizable operands.
1933*795d594fSAndroid Build Coastguard Worker HInstruction* opa = instruction->InputAt(0);
1934*795d594fSAndroid Build Coastguard Worker HInstruction* opb = instruction->InputAt(1);
1935*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, opa, generate_code, type, restrictions) &&
1936*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, opb, generate_code, type, restrictions)) {
1937*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1938*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(opa), vector_map_->Get(opb), type);
1939*795d594fSAndroid Build Coastguard Worker }
1940*795d594fSAndroid Build Coastguard Worker return true;
1941*795d594fSAndroid Build Coastguard Worker }
1942*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()) {
1943*795d594fSAndroid Build Coastguard Worker // Recognize halving add idiom.
1944*795d594fSAndroid Build Coastguard Worker if (VectorizeHalvingAddIdiom(node, instruction, generate_code, type, restrictions)) {
1945*795d594fSAndroid Build Coastguard Worker return true;
1946*795d594fSAndroid Build Coastguard Worker }
1947*795d594fSAndroid Build Coastguard Worker // Deal with vector restrictions.
1948*795d594fSAndroid Build Coastguard Worker HInstruction* opa = instruction->InputAt(0);
1949*795d594fSAndroid Build Coastguard Worker HInstruction* opb = instruction->InputAt(1);
1950*795d594fSAndroid Build Coastguard Worker HInstruction* r = opa;
1951*795d594fSAndroid Build Coastguard Worker bool is_unsigned = false;
1952*795d594fSAndroid Build Coastguard Worker if ((HasVectorRestrictions(restrictions, kNoShift)) ||
1953*795d594fSAndroid Build Coastguard Worker (instruction->IsShr() && HasVectorRestrictions(restrictions, kNoShr))) {
1954*795d594fSAndroid Build Coastguard Worker return false; // unsupported instruction
1955*795d594fSAndroid Build Coastguard Worker } else if (HasVectorRestrictions(restrictions, kNoHiBits)) {
1956*795d594fSAndroid Build Coastguard Worker // Shifts right need extra care to account for higher order bits.
1957*795d594fSAndroid Build Coastguard Worker // TODO: less likely shr/unsigned and ushr/signed can by flipping signess.
1958*795d594fSAndroid Build Coastguard Worker if (instruction->IsShr() &&
1959*795d594fSAndroid Build Coastguard Worker (!IsNarrowerOperand(opa, type, &r, &is_unsigned) || is_unsigned)) {
1960*795d594fSAndroid Build Coastguard Worker return false; // reject, unless all operands are sign-extension narrower
1961*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsUShr() &&
1962*795d594fSAndroid Build Coastguard Worker (!IsNarrowerOperand(opa, type, &r, &is_unsigned) || !is_unsigned)) {
1963*795d594fSAndroid Build Coastguard Worker return false; // reject, unless all operands are zero-extension narrower
1964*795d594fSAndroid Build Coastguard Worker }
1965*795d594fSAndroid Build Coastguard Worker }
1966*795d594fSAndroid Build Coastguard Worker // Accept shift operator for vectorizable/invariant operands.
1967*795d594fSAndroid Build Coastguard Worker // TODO: accept symbolic, albeit loop invariant shift factors.
1968*795d594fSAndroid Build Coastguard Worker DCHECK(r != nullptr);
1969*795d594fSAndroid Build Coastguard Worker if (generate_code && synthesis_mode_ != LoopSynthesisMode::kVector) { // de-idiom
1970*795d594fSAndroid Build Coastguard Worker r = opa;
1971*795d594fSAndroid Build Coastguard Worker }
1972*795d594fSAndroid Build Coastguard Worker int64_t distance = 0;
1973*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, r, generate_code, type, restrictions) &&
1974*795d594fSAndroid Build Coastguard Worker IsInt64AndGet(opb, /*out*/ &distance)) {
1975*795d594fSAndroid Build Coastguard Worker // Restrict shift distance to packed data type width.
1976*795d594fSAndroid Build Coastguard Worker int64_t max_distance = DataType::Size(type) * 8;
1977*795d594fSAndroid Build Coastguard Worker if (0 <= distance && distance < max_distance) {
1978*795d594fSAndroid Build Coastguard Worker if (generate_code) {
1979*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(r), opb, type);
1980*795d594fSAndroid Build Coastguard Worker }
1981*795d594fSAndroid Build Coastguard Worker return true;
1982*795d594fSAndroid Build Coastguard Worker }
1983*795d594fSAndroid Build Coastguard Worker }
1984*795d594fSAndroid Build Coastguard Worker } else if (instruction->IsAbs()) {
1985*795d594fSAndroid Build Coastguard Worker // Deal with vector restrictions.
1986*795d594fSAndroid Build Coastguard Worker HInstruction* opa = instruction->InputAt(0);
1987*795d594fSAndroid Build Coastguard Worker HInstruction* r = opa;
1988*795d594fSAndroid Build Coastguard Worker bool is_unsigned = false;
1989*795d594fSAndroid Build Coastguard Worker if (HasVectorRestrictions(restrictions, kNoAbs)) {
1990*795d594fSAndroid Build Coastguard Worker return false;
1991*795d594fSAndroid Build Coastguard Worker } else if (HasVectorRestrictions(restrictions, kNoHiBits) &&
1992*795d594fSAndroid Build Coastguard Worker (!IsNarrowerOperand(opa, type, &r, &is_unsigned) || is_unsigned)) {
1993*795d594fSAndroid Build Coastguard Worker return false; // reject, unless operand is sign-extension narrower
1994*795d594fSAndroid Build Coastguard Worker }
1995*795d594fSAndroid Build Coastguard Worker // Accept ABS(x) for vectorizable operand.
1996*795d594fSAndroid Build Coastguard Worker DCHECK(r != nullptr);
1997*795d594fSAndroid Build Coastguard Worker if (generate_code && synthesis_mode_ != LoopSynthesisMode::kVector) { // de-idiom
1998*795d594fSAndroid Build Coastguard Worker r = opa;
1999*795d594fSAndroid Build Coastguard Worker }
2000*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, r, generate_code, type, restrictions)) {
2001*795d594fSAndroid Build Coastguard Worker if (generate_code) {
2002*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction,
2003*795d594fSAndroid Build Coastguard Worker vector_map_->Get(r),
2004*795d594fSAndroid Build Coastguard Worker nullptr,
2005*795d594fSAndroid Build Coastguard Worker HVecOperation::ToProperType(type, is_unsigned));
2006*795d594fSAndroid Build Coastguard Worker }
2007*795d594fSAndroid Build Coastguard Worker return true;
2008*795d594fSAndroid Build Coastguard Worker }
2009*795d594fSAndroid Build Coastguard Worker }
2010*795d594fSAndroid Build Coastguard Worker return false;
2011*795d594fSAndroid Build Coastguard Worker }
2012*795d594fSAndroid Build Coastguard Worker
GetVectorSizeInBytes()2013*795d594fSAndroid Build Coastguard Worker uint32_t HLoopOptimization::GetVectorSizeInBytes() {
2014*795d594fSAndroid Build Coastguard Worker return simd_register_size_;
2015*795d594fSAndroid Build Coastguard Worker }
2016*795d594fSAndroid Build Coastguard Worker
TrySetVectorType(DataType::Type type,uint64_t * restrictions)2017*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TrySetVectorType(DataType::Type type, uint64_t* restrictions) {
2018*795d594fSAndroid Build Coastguard Worker const InstructionSetFeatures* features = compiler_options_->GetInstructionSetFeatures();
2019*795d594fSAndroid Build Coastguard Worker switch (compiler_options_->GetInstructionSet()) {
2020*795d594fSAndroid Build Coastguard Worker case InstructionSet::kArm:
2021*795d594fSAndroid Build Coastguard Worker case InstructionSet::kThumb2:
2022*795d594fSAndroid Build Coastguard Worker // Allow vectorization for all ARM devices, because Android assumes that
2023*795d594fSAndroid Build Coastguard Worker // ARM 32-bit always supports advanced SIMD (64-bit SIMD).
2024*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoIfCond;
2025*795d594fSAndroid Build Coastguard Worker switch (type) {
2026*795d594fSAndroid Build Coastguard Worker case DataType::Type::kBool:
2027*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint8:
2028*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt8:
2029*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv | kNoReduction | kNoDotProd;
2030*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 8);
2031*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
2032*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
2033*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv | kNoStringCharAt | kNoReduction | kNoDotProd;
2034*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 4);
2035*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt32:
2036*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv | kNoWideSAD;
2037*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 2);
2038*795d594fSAndroid Build Coastguard Worker default:
2039*795d594fSAndroid Build Coastguard Worker break;
2040*795d594fSAndroid Build Coastguard Worker }
2041*795d594fSAndroid Build Coastguard Worker return false;
2042*795d594fSAndroid Build Coastguard Worker case InstructionSet::kArm64:
2043*795d594fSAndroid Build Coastguard Worker if (IsInPredicatedVectorizationMode()) {
2044*795d594fSAndroid Build Coastguard Worker // SVE vectorization.
2045*795d594fSAndroid Build Coastguard Worker size_t vector_length = simd_register_size_ / DataType::Size(type);
2046*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(simd_register_size_ % DataType::Size(type), 0u);
2047*795d594fSAndroid Build Coastguard Worker switch (type) {
2048*795d594fSAndroid Build Coastguard Worker case DataType::Type::kBool:
2049*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv |
2050*795d594fSAndroid Build Coastguard Worker kNoSignedHAdd |
2051*795d594fSAndroid Build Coastguard Worker kNoUnsignedHAdd |
2052*795d594fSAndroid Build Coastguard Worker kNoUnroundedHAdd |
2053*795d594fSAndroid Build Coastguard Worker kNoSAD |
2054*795d594fSAndroid Build Coastguard Worker kNoIfCond;
2055*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, vector_length);
2056*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint8:
2057*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt8:
2058*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv |
2059*795d594fSAndroid Build Coastguard Worker kNoSignedHAdd |
2060*795d594fSAndroid Build Coastguard Worker kNoUnsignedHAdd |
2061*795d594fSAndroid Build Coastguard Worker kNoUnroundedHAdd |
2062*795d594fSAndroid Build Coastguard Worker kNoSAD;
2063*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, vector_length);
2064*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
2065*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
2066*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv |
2067*795d594fSAndroid Build Coastguard Worker kNoStringCharAt | // TODO: support in predicated mode.
2068*795d594fSAndroid Build Coastguard Worker kNoSignedHAdd |
2069*795d594fSAndroid Build Coastguard Worker kNoUnsignedHAdd |
2070*795d594fSAndroid Build Coastguard Worker kNoUnroundedHAdd |
2071*795d594fSAndroid Build Coastguard Worker kNoSAD |
2072*795d594fSAndroid Build Coastguard Worker kNoDotProd;
2073*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, vector_length);
2074*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt32:
2075*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv | kNoSAD;
2076*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, vector_length);
2077*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt64:
2078*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv | kNoSAD | kNoIfCond;
2079*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, vector_length);
2080*795d594fSAndroid Build Coastguard Worker case DataType::Type::kFloat32:
2081*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoReduction | kNoIfCond;
2082*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, vector_length);
2083*795d594fSAndroid Build Coastguard Worker case DataType::Type::kFloat64:
2084*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoReduction | kNoIfCond;
2085*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, vector_length);
2086*795d594fSAndroid Build Coastguard Worker default:
2087*795d594fSAndroid Build Coastguard Worker break;
2088*795d594fSAndroid Build Coastguard Worker }
2089*795d594fSAndroid Build Coastguard Worker return false;
2090*795d594fSAndroid Build Coastguard Worker } else {
2091*795d594fSAndroid Build Coastguard Worker // Allow vectorization for all ARM devices, because Android assumes that
2092*795d594fSAndroid Build Coastguard Worker // ARMv8 AArch64 always supports advanced SIMD (128-bit SIMD).
2093*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoIfCond;
2094*795d594fSAndroid Build Coastguard Worker switch (type) {
2095*795d594fSAndroid Build Coastguard Worker case DataType::Type::kBool:
2096*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint8:
2097*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt8:
2098*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv;
2099*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 16);
2100*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
2101*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
2102*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv;
2103*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 8);
2104*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt32:
2105*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv;
2106*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 4);
2107*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt64:
2108*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv | kNoMul;
2109*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 2);
2110*795d594fSAndroid Build Coastguard Worker case DataType::Type::kFloat32:
2111*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoReduction;
2112*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 4);
2113*795d594fSAndroid Build Coastguard Worker case DataType::Type::kFloat64:
2114*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoReduction;
2115*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 2);
2116*795d594fSAndroid Build Coastguard Worker default:
2117*795d594fSAndroid Build Coastguard Worker break;
2118*795d594fSAndroid Build Coastguard Worker }
2119*795d594fSAndroid Build Coastguard Worker return false;
2120*795d594fSAndroid Build Coastguard Worker }
2121*795d594fSAndroid Build Coastguard Worker case InstructionSet::kX86:
2122*795d594fSAndroid Build Coastguard Worker case InstructionSet::kX86_64:
2123*795d594fSAndroid Build Coastguard Worker // Allow vectorization for SSE4.1-enabled X86 devices only (128-bit SIMD).
2124*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoIfCond;
2125*795d594fSAndroid Build Coastguard Worker if (features->AsX86InstructionSetFeatures()->HasSSE4_1()) {
2126*795d594fSAndroid Build Coastguard Worker switch (type) {
2127*795d594fSAndroid Build Coastguard Worker case DataType::Type::kBool:
2128*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint8:
2129*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt8:
2130*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoMul |
2131*795d594fSAndroid Build Coastguard Worker kNoDiv |
2132*795d594fSAndroid Build Coastguard Worker kNoShift |
2133*795d594fSAndroid Build Coastguard Worker kNoAbs |
2134*795d594fSAndroid Build Coastguard Worker kNoSignedHAdd |
2135*795d594fSAndroid Build Coastguard Worker kNoUnroundedHAdd |
2136*795d594fSAndroid Build Coastguard Worker kNoSAD |
2137*795d594fSAndroid Build Coastguard Worker kNoDotProd;
2138*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 16);
2139*795d594fSAndroid Build Coastguard Worker case DataType::Type::kUint16:
2140*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv |
2141*795d594fSAndroid Build Coastguard Worker kNoAbs |
2142*795d594fSAndroid Build Coastguard Worker kNoSignedHAdd |
2143*795d594fSAndroid Build Coastguard Worker kNoUnroundedHAdd |
2144*795d594fSAndroid Build Coastguard Worker kNoSAD |
2145*795d594fSAndroid Build Coastguard Worker kNoDotProd;
2146*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 8);
2147*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt16:
2148*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv |
2149*795d594fSAndroid Build Coastguard Worker kNoAbs |
2150*795d594fSAndroid Build Coastguard Worker kNoSignedHAdd |
2151*795d594fSAndroid Build Coastguard Worker kNoUnroundedHAdd |
2152*795d594fSAndroid Build Coastguard Worker kNoSAD;
2153*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 8);
2154*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt32:
2155*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoDiv | kNoSAD;
2156*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 4);
2157*795d594fSAndroid Build Coastguard Worker case DataType::Type::kInt64:
2158*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoMul | kNoDiv | kNoShr | kNoAbs | kNoSAD;
2159*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 2);
2160*795d594fSAndroid Build Coastguard Worker case DataType::Type::kFloat32:
2161*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoReduction;
2162*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 4);
2163*795d594fSAndroid Build Coastguard Worker case DataType::Type::kFloat64:
2164*795d594fSAndroid Build Coastguard Worker *restrictions |= kNoReduction;
2165*795d594fSAndroid Build Coastguard Worker return TrySetVectorLength(type, 2);
2166*795d594fSAndroid Build Coastguard Worker default:
2167*795d594fSAndroid Build Coastguard Worker break;
2168*795d594fSAndroid Build Coastguard Worker } // switch type
2169*795d594fSAndroid Build Coastguard Worker }
2170*795d594fSAndroid Build Coastguard Worker return false;
2171*795d594fSAndroid Build Coastguard Worker default:
2172*795d594fSAndroid Build Coastguard Worker return false;
2173*795d594fSAndroid Build Coastguard Worker } // switch instruction set
2174*795d594fSAndroid Build Coastguard Worker }
2175*795d594fSAndroid Build Coastguard Worker
TrySetVectorLengthImpl(uint32_t length)2176*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TrySetVectorLengthImpl(uint32_t length) {
2177*795d594fSAndroid Build Coastguard Worker DCHECK(IsPowerOfTwo(length) && length >= 2u);
2178*795d594fSAndroid Build Coastguard Worker // First time set?
2179*795d594fSAndroid Build Coastguard Worker if (vector_length_ == 0) {
2180*795d594fSAndroid Build Coastguard Worker vector_length_ = length;
2181*795d594fSAndroid Build Coastguard Worker }
2182*795d594fSAndroid Build Coastguard Worker // Different types are acceptable within a loop-body, as long as all the corresponding vector
2183*795d594fSAndroid Build Coastguard Worker // lengths match exactly to obtain a uniform traversal through the vector iteration space
2184*795d594fSAndroid Build Coastguard Worker // (idiomatic exceptions to this rule can be handled by further unrolling sub-expressions).
2185*795d594fSAndroid Build Coastguard Worker return vector_length_ == length;
2186*795d594fSAndroid Build Coastguard Worker }
2187*795d594fSAndroid Build Coastguard Worker
GenerateVecInv(HInstruction * org,DataType::Type type)2188*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateVecInv(HInstruction* org, DataType::Type type) {
2189*795d594fSAndroid Build Coastguard Worker if (vector_map_->find(org) == vector_map_->end()) {
2190*795d594fSAndroid Build Coastguard Worker // In scalar code, just use a self pass-through for scalar invariants
2191*795d594fSAndroid Build Coastguard Worker // (viz. expression remains itself).
2192*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kSequential) {
2193*795d594fSAndroid Build Coastguard Worker vector_map_->Put(org, org);
2194*795d594fSAndroid Build Coastguard Worker return;
2195*795d594fSAndroid Build Coastguard Worker }
2196*795d594fSAndroid Build Coastguard Worker // In vector code, explicit scalar expansion is needed.
2197*795d594fSAndroid Build Coastguard Worker HInstruction* vector = nullptr;
2198*795d594fSAndroid Build Coastguard Worker auto it = vector_permanent_map_->find(org);
2199*795d594fSAndroid Build Coastguard Worker if (it != vector_permanent_map_->end()) {
2200*795d594fSAndroid Build Coastguard Worker vector = it->second; // reuse during unrolling
2201*795d594fSAndroid Build Coastguard Worker } else {
2202*795d594fSAndroid Build Coastguard Worker // Generates ReplicateScalar( (optional_type_conv) org ).
2203*795d594fSAndroid Build Coastguard Worker HInstruction* input = org;
2204*795d594fSAndroid Build Coastguard Worker DataType::Type input_type = input->GetType();
2205*795d594fSAndroid Build Coastguard Worker if (type != input_type && (type == DataType::Type::kInt64 ||
2206*795d594fSAndroid Build Coastguard Worker input_type == DataType::Type::kInt64)) {
2207*795d594fSAndroid Build Coastguard Worker input = Insert(vector_preheader_,
2208*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HTypeConversion(type, input, kNoDexPc));
2209*795d594fSAndroid Build Coastguard Worker }
2210*795d594fSAndroid Build Coastguard Worker vector = new (global_allocator_)
2211*795d594fSAndroid Build Coastguard Worker HVecReplicateScalar(global_allocator_, input, type, vector_length_, kNoDexPc);
2212*795d594fSAndroid Build Coastguard Worker vector_permanent_map_->Put(org, Insert(vector_preheader_, vector));
2213*795d594fSAndroid Build Coastguard Worker MaybeInsertInVectorExternalSet(vector);
2214*795d594fSAndroid Build Coastguard Worker }
2215*795d594fSAndroid Build Coastguard Worker vector_map_->Put(org, vector);
2216*795d594fSAndroid Build Coastguard Worker }
2217*795d594fSAndroid Build Coastguard Worker }
2218*795d594fSAndroid Build Coastguard Worker
GenerateVecSub(HInstruction * org,HInstruction * offset)2219*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateVecSub(HInstruction* org, HInstruction* offset) {
2220*795d594fSAndroid Build Coastguard Worker if (vector_map_->find(org) == vector_map_->end()) {
2221*795d594fSAndroid Build Coastguard Worker HInstruction* subscript = vector_index_;
2222*795d594fSAndroid Build Coastguard Worker int64_t value = 0;
2223*795d594fSAndroid Build Coastguard Worker if (!IsInt64AndGet(offset, &value) || value != 0) {
2224*795d594fSAndroid Build Coastguard Worker subscript = new (global_allocator_) HAdd(DataType::Type::kInt32, subscript, offset);
2225*795d594fSAndroid Build Coastguard Worker if (org->IsPhi()) {
2226*795d594fSAndroid Build Coastguard Worker Insert(vector_body_, subscript); // lacks layout placeholder
2227*795d594fSAndroid Build Coastguard Worker }
2228*795d594fSAndroid Build Coastguard Worker }
2229*795d594fSAndroid Build Coastguard Worker vector_map_->Put(org, subscript);
2230*795d594fSAndroid Build Coastguard Worker }
2231*795d594fSAndroid Build Coastguard Worker }
2232*795d594fSAndroid Build Coastguard Worker
GenerateVecMem(HInstruction * org,HInstruction * opa,HInstruction * opb,HInstruction * offset,DataType::Type type)2233*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateVecMem(HInstruction* org,
2234*795d594fSAndroid Build Coastguard Worker HInstruction* opa,
2235*795d594fSAndroid Build Coastguard Worker HInstruction* opb,
2236*795d594fSAndroid Build Coastguard Worker HInstruction* offset,
2237*795d594fSAndroid Build Coastguard Worker DataType::Type type) {
2238*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc = org->GetDexPc();
2239*795d594fSAndroid Build Coastguard Worker HInstruction* vector = nullptr;
2240*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kVector) {
2241*795d594fSAndroid Build Coastguard Worker // Vector store or load.
2242*795d594fSAndroid Build Coastguard Worker bool is_string_char_at = false;
2243*795d594fSAndroid Build Coastguard Worker HInstruction* base = org->InputAt(0);
2244*795d594fSAndroid Build Coastguard Worker if (opb != nullptr) {
2245*795d594fSAndroid Build Coastguard Worker vector = new (global_allocator_) HVecStore(
2246*795d594fSAndroid Build Coastguard Worker global_allocator_, base, opa, opb, type, org->GetSideEffects(), vector_length_, dex_pc);
2247*795d594fSAndroid Build Coastguard Worker } else {
2248*795d594fSAndroid Build Coastguard Worker is_string_char_at = org->AsArrayGet()->IsStringCharAt();
2249*795d594fSAndroid Build Coastguard Worker vector = new (global_allocator_) HVecLoad(global_allocator_,
2250*795d594fSAndroid Build Coastguard Worker base,
2251*795d594fSAndroid Build Coastguard Worker opa,
2252*795d594fSAndroid Build Coastguard Worker type,
2253*795d594fSAndroid Build Coastguard Worker org->GetSideEffects(),
2254*795d594fSAndroid Build Coastguard Worker vector_length_,
2255*795d594fSAndroid Build Coastguard Worker is_string_char_at,
2256*795d594fSAndroid Build Coastguard Worker dex_pc);
2257*795d594fSAndroid Build Coastguard Worker }
2258*795d594fSAndroid Build Coastguard Worker // Known (forced/adjusted/original) alignment?
2259*795d594fSAndroid Build Coastguard Worker if (vector_dynamic_peeling_candidate_ != nullptr) {
2260*795d594fSAndroid Build Coastguard Worker if (vector_dynamic_peeling_candidate_->offset == offset && // TODO: diffs too?
2261*795d594fSAndroid Build Coastguard Worker DataType::Size(vector_dynamic_peeling_candidate_->type) == DataType::Size(type) &&
2262*795d594fSAndroid Build Coastguard Worker vector_dynamic_peeling_candidate_->is_string_char_at == is_string_char_at) {
2263*795d594fSAndroid Build Coastguard Worker vector->AsVecMemoryOperation()->SetAlignment( // forced
2264*795d594fSAndroid Build Coastguard Worker Alignment(GetVectorSizeInBytes(), 0));
2265*795d594fSAndroid Build Coastguard Worker }
2266*795d594fSAndroid Build Coastguard Worker } else {
2267*795d594fSAndroid Build Coastguard Worker vector->AsVecMemoryOperation()->SetAlignment( // adjusted/original
2268*795d594fSAndroid Build Coastguard Worker ComputeAlignment(offset, type, is_string_char_at, vector_static_peeling_factor_));
2269*795d594fSAndroid Build Coastguard Worker }
2270*795d594fSAndroid Build Coastguard Worker } else {
2271*795d594fSAndroid Build Coastguard Worker // Scalar store or load.
2272*795d594fSAndroid Build Coastguard Worker DCHECK(synthesis_mode_ == LoopSynthesisMode::kSequential);
2273*795d594fSAndroid Build Coastguard Worker if (opb != nullptr) {
2274*795d594fSAndroid Build Coastguard Worker DataType::Type component_type = org->AsArraySet()->GetComponentType();
2275*795d594fSAndroid Build Coastguard Worker vector = new (global_allocator_) HArraySet(
2276*795d594fSAndroid Build Coastguard Worker org->InputAt(0), opa, opb, component_type, org->GetSideEffects(), dex_pc);
2277*795d594fSAndroid Build Coastguard Worker } else {
2278*795d594fSAndroid Build Coastguard Worker bool is_string_char_at = org->AsArrayGet()->IsStringCharAt();
2279*795d594fSAndroid Build Coastguard Worker vector = new (global_allocator_) HArrayGet(
2280*795d594fSAndroid Build Coastguard Worker org->InputAt(0), opa, org->GetType(), org->GetSideEffects(), dex_pc, is_string_char_at);
2281*795d594fSAndroid Build Coastguard Worker }
2282*795d594fSAndroid Build Coastguard Worker }
2283*795d594fSAndroid Build Coastguard Worker vector_map_->Put(org, vector);
2284*795d594fSAndroid Build Coastguard Worker }
2285*795d594fSAndroid Build Coastguard Worker
GenerateVecReductionPhi(HPhi * orig_phi)2286*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateVecReductionPhi(HPhi* orig_phi) {
2287*795d594fSAndroid Build Coastguard Worker DCHECK(reductions_->find(orig_phi) != reductions_->end());
2288*795d594fSAndroid Build Coastguard Worker DCHECK(reductions_->Get(orig_phi->InputAt(1)) == orig_phi);
2289*795d594fSAndroid Build Coastguard Worker HInstruction* vector = nullptr;
2290*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kSequential) {
2291*795d594fSAndroid Build Coastguard Worker HPhi* new_phi = new (global_allocator_) HPhi(
2292*795d594fSAndroid Build Coastguard Worker global_allocator_, kNoRegNumber, 0, orig_phi->GetType());
2293*795d594fSAndroid Build Coastguard Worker vector_header_->AddPhi(new_phi);
2294*795d594fSAndroid Build Coastguard Worker vector = new_phi;
2295*795d594fSAndroid Build Coastguard Worker } else {
2296*795d594fSAndroid Build Coastguard Worker // Link vector reduction back to prior unrolled update, or a first phi.
2297*795d594fSAndroid Build Coastguard Worker auto it = vector_permanent_map_->find(orig_phi);
2298*795d594fSAndroid Build Coastguard Worker if (it != vector_permanent_map_->end()) {
2299*795d594fSAndroid Build Coastguard Worker vector = it->second;
2300*795d594fSAndroid Build Coastguard Worker } else {
2301*795d594fSAndroid Build Coastguard Worker HPhi* new_phi = new (global_allocator_) HPhi(
2302*795d594fSAndroid Build Coastguard Worker global_allocator_, kNoRegNumber, 0, HVecOperation::kSIMDType);
2303*795d594fSAndroid Build Coastguard Worker vector_header_->AddPhi(new_phi);
2304*795d594fSAndroid Build Coastguard Worker vector = new_phi;
2305*795d594fSAndroid Build Coastguard Worker }
2306*795d594fSAndroid Build Coastguard Worker }
2307*795d594fSAndroid Build Coastguard Worker vector_map_->Put(orig_phi, vector);
2308*795d594fSAndroid Build Coastguard Worker }
2309*795d594fSAndroid Build Coastguard Worker
GenerateVecReductionPhiInputs(HPhi * phi,HInstruction * reduction)2310*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::GenerateVecReductionPhiInputs(HPhi* phi, HInstruction* reduction) {
2311*795d594fSAndroid Build Coastguard Worker HInstruction* new_phi = vector_map_->Get(phi);
2312*795d594fSAndroid Build Coastguard Worker HInstruction* new_init = reductions_->Get(phi);
2313*795d594fSAndroid Build Coastguard Worker HInstruction* new_red = vector_map_->Get(reduction);
2314*795d594fSAndroid Build Coastguard Worker // Link unrolled vector loop back to new phi.
2315*795d594fSAndroid Build Coastguard Worker for (; !new_phi->IsPhi(); new_phi = vector_permanent_map_->Get(new_phi)) {
2316*795d594fSAndroid Build Coastguard Worker DCHECK(new_phi->IsVecOperation());
2317*795d594fSAndroid Build Coastguard Worker }
2318*795d594fSAndroid Build Coastguard Worker // Prepare the new initialization.
2319*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kVector) {
2320*795d594fSAndroid Build Coastguard Worker // Generate a [initial, 0, .., 0] vector for add or
2321*795d594fSAndroid Build Coastguard Worker // a [initial, initial, .., initial] vector for min/max.
2322*795d594fSAndroid Build Coastguard Worker HVecOperation* red_vector = new_red->AsVecOperation();
2323*795d594fSAndroid Build Coastguard Worker HVecReduce::ReductionKind kind = GetReductionKind(red_vector);
2324*795d594fSAndroid Build Coastguard Worker uint32_t vector_length = red_vector->GetVectorLength();
2325*795d594fSAndroid Build Coastguard Worker DataType::Type type = red_vector->GetPackedType();
2326*795d594fSAndroid Build Coastguard Worker if (kind == HVecReduce::ReductionKind::kSum) {
2327*795d594fSAndroid Build Coastguard Worker new_init = Insert(vector_preheader_,
2328*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecSetScalars(global_allocator_,
2329*795d594fSAndroid Build Coastguard Worker &new_init,
2330*795d594fSAndroid Build Coastguard Worker type,
2331*795d594fSAndroid Build Coastguard Worker vector_length,
2332*795d594fSAndroid Build Coastguard Worker 1,
2333*795d594fSAndroid Build Coastguard Worker kNoDexPc));
2334*795d594fSAndroid Build Coastguard Worker } else {
2335*795d594fSAndroid Build Coastguard Worker new_init = Insert(vector_preheader_,
2336*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecReplicateScalar(global_allocator_,
2337*795d594fSAndroid Build Coastguard Worker new_init,
2338*795d594fSAndroid Build Coastguard Worker type,
2339*795d594fSAndroid Build Coastguard Worker vector_length,
2340*795d594fSAndroid Build Coastguard Worker kNoDexPc));
2341*795d594fSAndroid Build Coastguard Worker }
2342*795d594fSAndroid Build Coastguard Worker MaybeInsertInVectorExternalSet(new_init);
2343*795d594fSAndroid Build Coastguard Worker } else {
2344*795d594fSAndroid Build Coastguard Worker new_init = ReduceAndExtractIfNeeded(new_init);
2345*795d594fSAndroid Build Coastguard Worker }
2346*795d594fSAndroid Build Coastguard Worker // Set the phi inputs.
2347*795d594fSAndroid Build Coastguard Worker DCHECK(new_phi->IsPhi());
2348*795d594fSAndroid Build Coastguard Worker new_phi->AsPhi()->AddInput(new_init);
2349*795d594fSAndroid Build Coastguard Worker new_phi->AsPhi()->AddInput(new_red);
2350*795d594fSAndroid Build Coastguard Worker // New feed value for next phi (safe mutation in iteration).
2351*795d594fSAndroid Build Coastguard Worker reductions_->find(phi)->second = new_phi;
2352*795d594fSAndroid Build Coastguard Worker }
2353*795d594fSAndroid Build Coastguard Worker
ReduceAndExtractIfNeeded(HInstruction * instruction)2354*795d594fSAndroid Build Coastguard Worker HInstruction* HLoopOptimization::ReduceAndExtractIfNeeded(HInstruction* instruction) {
2355*795d594fSAndroid Build Coastguard Worker if (instruction->IsPhi()) {
2356*795d594fSAndroid Build Coastguard Worker HInstruction* input = instruction->InputAt(1);
2357*795d594fSAndroid Build Coastguard Worker if (HVecOperation::ReturnsSIMDValue(input)) {
2358*795d594fSAndroid Build Coastguard Worker DCHECK(!input->IsPhi());
2359*795d594fSAndroid Build Coastguard Worker HVecOperation* input_vector = input->AsVecOperation();
2360*795d594fSAndroid Build Coastguard Worker uint32_t vector_length = input_vector->GetVectorLength();
2361*795d594fSAndroid Build Coastguard Worker DataType::Type type = input_vector->GetPackedType();
2362*795d594fSAndroid Build Coastguard Worker HVecReduce::ReductionKind kind = GetReductionKind(input_vector);
2363*795d594fSAndroid Build Coastguard Worker HBasicBlock* exit = instruction->GetBlock()->GetSuccessors()[0];
2364*795d594fSAndroid Build Coastguard Worker // Generate a vector reduction and scalar extract
2365*795d594fSAndroid Build Coastguard Worker // x = REDUCE( [x_1, .., x_n] )
2366*795d594fSAndroid Build Coastguard Worker // y = x_1
2367*795d594fSAndroid Build Coastguard Worker // along the exit of the defining loop.
2368*795d594fSAndroid Build Coastguard Worker HVecReduce* reduce = new (global_allocator_) HVecReduce(
2369*795d594fSAndroid Build Coastguard Worker global_allocator_, instruction, type, vector_length, kind, kNoDexPc);
2370*795d594fSAndroid Build Coastguard Worker exit->InsertInstructionBefore(reduce, exit->GetFirstInstruction());
2371*795d594fSAndroid Build Coastguard Worker MaybeInsertInVectorExternalSet(reduce);
2372*795d594fSAndroid Build Coastguard Worker instruction = new (global_allocator_) HVecExtractScalar(
2373*795d594fSAndroid Build Coastguard Worker global_allocator_, reduce, type, vector_length, 0, kNoDexPc);
2374*795d594fSAndroid Build Coastguard Worker exit->InsertInstructionAfter(instruction, reduce);
2375*795d594fSAndroid Build Coastguard Worker
2376*795d594fSAndroid Build Coastguard Worker MaybeInsertInVectorExternalSet(instruction);
2377*795d594fSAndroid Build Coastguard Worker }
2378*795d594fSAndroid Build Coastguard Worker }
2379*795d594fSAndroid Build Coastguard Worker return instruction;
2380*795d594fSAndroid Build Coastguard Worker }
2381*795d594fSAndroid Build Coastguard Worker
2382*795d594fSAndroid Build Coastguard Worker #define GENERATE_VEC(x, y) \
2383*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kVector) { \
2384*795d594fSAndroid Build Coastguard Worker vector = (x); \
2385*795d594fSAndroid Build Coastguard Worker } else { \
2386*795d594fSAndroid Build Coastguard Worker DCHECK(synthesis_mode_ == LoopSynthesisMode::kSequential); \
2387*795d594fSAndroid Build Coastguard Worker vector = (y); \
2388*795d594fSAndroid Build Coastguard Worker } \
2389*795d594fSAndroid Build Coastguard Worker break;
2390*795d594fSAndroid Build Coastguard Worker
2391*795d594fSAndroid Build Coastguard Worker // Some instructions in the scalar loop body can only occur in loops with control flow; for such
2392*795d594fSAndroid Build Coastguard Worker // loops we don't support clean ups loop (generated via kSequential); see TryVectorizePredicated.
2393*795d594fSAndroid Build Coastguard Worker #define GENERATE_PRED_VEC(x) \
2394*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(synthesis_mode_, LoopSynthesisMode::kVector); \
2395*795d594fSAndroid Build Coastguard Worker vector = (x); \
2396*795d594fSAndroid Build Coastguard Worker break;
2397*795d594fSAndroid Build Coastguard Worker
GenerateVecOp(HInstruction * org,HInstruction * opa,HInstruction * opb,DataType::Type type)2398*795d594fSAndroid Build Coastguard Worker HInstruction* HLoopOptimization::GenerateVecOp(HInstruction* org,
2399*795d594fSAndroid Build Coastguard Worker HInstruction* opa,
2400*795d594fSAndroid Build Coastguard Worker HInstruction* opb,
2401*795d594fSAndroid Build Coastguard Worker DataType::Type type) {
2402*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc = org->GetDexPc();
2403*795d594fSAndroid Build Coastguard Worker HInstruction* vector = nullptr;
2404*795d594fSAndroid Build Coastguard Worker DataType::Type org_type = org->GetType();
2405*795d594fSAndroid Build Coastguard Worker switch (org->GetKind()) {
2406*795d594fSAndroid Build Coastguard Worker case HInstruction::kNeg:
2407*795d594fSAndroid Build Coastguard Worker DCHECK(opb == nullptr);
2408*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2409*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecNeg(global_allocator_, opa, type, vector_length_, dex_pc),
2410*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HNeg(org_type, opa, dex_pc));
2411*795d594fSAndroid Build Coastguard Worker case HInstruction::kNot:
2412*795d594fSAndroid Build Coastguard Worker DCHECK(opb == nullptr);
2413*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2414*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecNot(global_allocator_, opa, type, vector_length_, dex_pc),
2415*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HNot(org_type, opa, dex_pc));
2416*795d594fSAndroid Build Coastguard Worker case HInstruction::kBooleanNot:
2417*795d594fSAndroid Build Coastguard Worker DCHECK(opb == nullptr);
2418*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2419*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecNot(global_allocator_, opa, type, vector_length_, dex_pc),
2420*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HBooleanNot(opa, dex_pc));
2421*795d594fSAndroid Build Coastguard Worker case HInstruction::kTypeConversion:
2422*795d594fSAndroid Build Coastguard Worker DCHECK(opb == nullptr);
2423*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2424*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecCnv(global_allocator_, opa, type, vector_length_, dex_pc),
2425*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HTypeConversion(org_type, opa, dex_pc));
2426*795d594fSAndroid Build Coastguard Worker case HInstruction::kAdd:
2427*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2428*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecAdd(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2429*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HAdd(org_type, opa, opb, dex_pc));
2430*795d594fSAndroid Build Coastguard Worker case HInstruction::kSub:
2431*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2432*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecSub(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2433*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HSub(org_type, opa, opb, dex_pc));
2434*795d594fSAndroid Build Coastguard Worker case HInstruction::kMul:
2435*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2436*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecMul(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2437*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HMul(org_type, opa, opb, dex_pc));
2438*795d594fSAndroid Build Coastguard Worker case HInstruction::kDiv:
2439*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2440*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecDiv(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2441*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HDiv(org_type, opa, opb, dex_pc));
2442*795d594fSAndroid Build Coastguard Worker case HInstruction::kAnd:
2443*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2444*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecAnd(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2445*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HAnd(org_type, opa, opb, dex_pc));
2446*795d594fSAndroid Build Coastguard Worker case HInstruction::kOr:
2447*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2448*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecOr(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2449*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HOr(org_type, opa, opb, dex_pc));
2450*795d594fSAndroid Build Coastguard Worker case HInstruction::kXor:
2451*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2452*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecXor(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2453*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HXor(org_type, opa, opb, dex_pc));
2454*795d594fSAndroid Build Coastguard Worker case HInstruction::kShl:
2455*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2456*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecShl(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2457*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HShl(org_type, opa, opb, dex_pc));
2458*795d594fSAndroid Build Coastguard Worker case HInstruction::kShr:
2459*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2460*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecShr(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2461*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HShr(org_type, opa, opb, dex_pc));
2462*795d594fSAndroid Build Coastguard Worker case HInstruction::kUShr:
2463*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2464*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecUShr(global_allocator_, opa, opb, type, vector_length_, dex_pc),
2465*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HUShr(org_type, opa, opb, dex_pc));
2466*795d594fSAndroid Build Coastguard Worker case HInstruction::kAbs:
2467*795d594fSAndroid Build Coastguard Worker DCHECK(opb == nullptr);
2468*795d594fSAndroid Build Coastguard Worker GENERATE_VEC(
2469*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HVecAbs(global_allocator_, opa, type, vector_length_, dex_pc),
2470*795d594fSAndroid Build Coastguard Worker new (global_allocator_) HAbs(org_type, opa, dex_pc));
2471*795d594fSAndroid Build Coastguard Worker case HInstruction::kEqual:
2472*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2473*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2474*795d594fSAndroid Build Coastguard Worker HVecEqual(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2475*795d594fSAndroid Build Coastguard Worker case HInstruction::kNotEqual:
2476*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2477*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2478*795d594fSAndroid Build Coastguard Worker HVecNotEqual(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2479*795d594fSAndroid Build Coastguard Worker case HInstruction::kLessThan:
2480*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2481*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2482*795d594fSAndroid Build Coastguard Worker HVecLessThan(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2483*795d594fSAndroid Build Coastguard Worker case HInstruction::kLessThanOrEqual:
2484*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2485*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2486*795d594fSAndroid Build Coastguard Worker HVecLessThanOrEqual(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2487*795d594fSAndroid Build Coastguard Worker case HInstruction::kGreaterThan:
2488*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2489*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2490*795d594fSAndroid Build Coastguard Worker HVecGreaterThan(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2491*795d594fSAndroid Build Coastguard Worker case HInstruction::kGreaterThanOrEqual:
2492*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2493*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2494*795d594fSAndroid Build Coastguard Worker HVecGreaterThanOrEqual(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2495*795d594fSAndroid Build Coastguard Worker case HInstruction::kBelow:
2496*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2497*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2498*795d594fSAndroid Build Coastguard Worker HVecBelow(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2499*795d594fSAndroid Build Coastguard Worker case HInstruction::kBelowOrEqual:
2500*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2501*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2502*795d594fSAndroid Build Coastguard Worker HVecBelowOrEqual(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2503*795d594fSAndroid Build Coastguard Worker case HInstruction::kAbove:
2504*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2505*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2506*795d594fSAndroid Build Coastguard Worker HVecAbove(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2507*795d594fSAndroid Build Coastguard Worker case HInstruction::kAboveOrEqual:
2508*795d594fSAndroid Build Coastguard Worker GENERATE_PRED_VEC(
2509*795d594fSAndroid Build Coastguard Worker new (global_allocator_)
2510*795d594fSAndroid Build Coastguard Worker HVecAboveOrEqual(global_allocator_, opa, opb, type, vector_length_, dex_pc));
2511*795d594fSAndroid Build Coastguard Worker default:
2512*795d594fSAndroid Build Coastguard Worker break;
2513*795d594fSAndroid Build Coastguard Worker } // switch
2514*795d594fSAndroid Build Coastguard Worker CHECK(vector != nullptr) << "Unsupported SIMD operator";
2515*795d594fSAndroid Build Coastguard Worker vector_map_->Put(org, vector);
2516*795d594fSAndroid Build Coastguard Worker return vector;
2517*795d594fSAndroid Build Coastguard Worker }
2518*795d594fSAndroid Build Coastguard Worker
2519*795d594fSAndroid Build Coastguard Worker #undef GENERATE_VEC
2520*795d594fSAndroid Build Coastguard Worker
2521*795d594fSAndroid Build Coastguard Worker //
2522*795d594fSAndroid Build Coastguard Worker // Vectorization idioms.
2523*795d594fSAndroid Build Coastguard Worker //
2524*795d594fSAndroid Build Coastguard Worker
2525*795d594fSAndroid Build Coastguard Worker // Method recognizes the following idioms:
2526*795d594fSAndroid Build Coastguard Worker // rounding halving add (a + b + 1) >> 1 for unsigned/signed operands a, b
2527*795d594fSAndroid Build Coastguard Worker // truncated halving add (a + b) >> 1 for unsigned/signed operands a, b
2528*795d594fSAndroid Build Coastguard Worker // Provided that the operands are promoted to a wider form to do the arithmetic and
2529*795d594fSAndroid Build Coastguard Worker // then cast back to narrower form, the idioms can be mapped into efficient SIMD
2530*795d594fSAndroid Build Coastguard Worker // implementation that operates directly in narrower form (plus one extra bit).
2531*795d594fSAndroid Build Coastguard Worker // TODO: current version recognizes implicit byte/short/char widening only;
2532*795d594fSAndroid Build Coastguard Worker // explicit widening from int to long could be added later.
VectorizeHalvingAddIdiom(LoopNode * node,HInstruction * instruction,bool generate_code,DataType::Type type,uint64_t restrictions)2533*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::VectorizeHalvingAddIdiom(LoopNode* node,
2534*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
2535*795d594fSAndroid Build Coastguard Worker bool generate_code,
2536*795d594fSAndroid Build Coastguard Worker DataType::Type type,
2537*795d594fSAndroid Build Coastguard Worker uint64_t restrictions) {
2538*795d594fSAndroid Build Coastguard Worker // Test for top level arithmetic shift right x >> 1 or logical shift right x >>> 1
2539*795d594fSAndroid Build Coastguard Worker // (note whether the sign bit in wider precision is shifted in has no effect
2540*795d594fSAndroid Build Coastguard Worker // on the narrow precision computed by the idiom).
2541*795d594fSAndroid Build Coastguard Worker if ((instruction->IsShr() ||
2542*795d594fSAndroid Build Coastguard Worker instruction->IsUShr()) &&
2543*795d594fSAndroid Build Coastguard Worker IsInt64Value(instruction->InputAt(1), 1)) {
2544*795d594fSAndroid Build Coastguard Worker // Test for (a + b + c) >> 1 for optional constant c.
2545*795d594fSAndroid Build Coastguard Worker HInstruction* a = nullptr;
2546*795d594fSAndroid Build Coastguard Worker HInstruction* b = nullptr;
2547*795d594fSAndroid Build Coastguard Worker int64_t c = 0;
2548*795d594fSAndroid Build Coastguard Worker if (IsAddConst2(graph_, instruction->InputAt(0), /*out*/ &a, /*out*/ &b, /*out*/ &c)) {
2549*795d594fSAndroid Build Coastguard Worker // Accept c == 1 (rounded) or c == 0 (not rounded).
2550*795d594fSAndroid Build Coastguard Worker bool is_rounded = false;
2551*795d594fSAndroid Build Coastguard Worker if (c == 1) {
2552*795d594fSAndroid Build Coastguard Worker is_rounded = true;
2553*795d594fSAndroid Build Coastguard Worker } else if (c != 0) {
2554*795d594fSAndroid Build Coastguard Worker return false;
2555*795d594fSAndroid Build Coastguard Worker }
2556*795d594fSAndroid Build Coastguard Worker // Accept consistent zero or sign extension on operands a and b.
2557*795d594fSAndroid Build Coastguard Worker HInstruction* r = nullptr;
2558*795d594fSAndroid Build Coastguard Worker HInstruction* s = nullptr;
2559*795d594fSAndroid Build Coastguard Worker bool is_unsigned = false;
2560*795d594fSAndroid Build Coastguard Worker if (!IsNarrowerOperands(a, b, type, &r, &s, &is_unsigned)) {
2561*795d594fSAndroid Build Coastguard Worker return false;
2562*795d594fSAndroid Build Coastguard Worker }
2563*795d594fSAndroid Build Coastguard Worker // Deal with vector restrictions.
2564*795d594fSAndroid Build Coastguard Worker if ((is_unsigned && HasVectorRestrictions(restrictions, kNoUnsignedHAdd)) ||
2565*795d594fSAndroid Build Coastguard Worker (!is_unsigned && HasVectorRestrictions(restrictions, kNoSignedHAdd)) ||
2566*795d594fSAndroid Build Coastguard Worker (!is_rounded && HasVectorRestrictions(restrictions, kNoUnroundedHAdd))) {
2567*795d594fSAndroid Build Coastguard Worker return false;
2568*795d594fSAndroid Build Coastguard Worker }
2569*795d594fSAndroid Build Coastguard Worker // Accept recognized halving add for vectorizable operands. Vectorized code uses the
2570*795d594fSAndroid Build Coastguard Worker // shorthand idiomatic operation. Sequential code uses the original scalar expressions.
2571*795d594fSAndroid Build Coastguard Worker DCHECK(r != nullptr && s != nullptr);
2572*795d594fSAndroid Build Coastguard Worker if (generate_code && synthesis_mode_ != LoopSynthesisMode::kVector) { // de-idiom
2573*795d594fSAndroid Build Coastguard Worker r = instruction->InputAt(0);
2574*795d594fSAndroid Build Coastguard Worker s = instruction->InputAt(1);
2575*795d594fSAndroid Build Coastguard Worker }
2576*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, r, generate_code, type, restrictions) &&
2577*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, s, generate_code, type, restrictions)) {
2578*795d594fSAndroid Build Coastguard Worker if (generate_code) {
2579*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kVector) {
2580*795d594fSAndroid Build Coastguard Worker vector_map_->Put(instruction, new (global_allocator_) HVecHalvingAdd(
2581*795d594fSAndroid Build Coastguard Worker global_allocator_,
2582*795d594fSAndroid Build Coastguard Worker vector_map_->Get(r),
2583*795d594fSAndroid Build Coastguard Worker vector_map_->Get(s),
2584*795d594fSAndroid Build Coastguard Worker HVecOperation::ToProperType(type, is_unsigned),
2585*795d594fSAndroid Build Coastguard Worker vector_length_,
2586*795d594fSAndroid Build Coastguard Worker is_rounded,
2587*795d594fSAndroid Build Coastguard Worker kNoDexPc));
2588*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(stats_, MethodCompilationStat::kLoopVectorizedIdiom);
2589*795d594fSAndroid Build Coastguard Worker } else {
2590*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(r), vector_map_->Get(s), type);
2591*795d594fSAndroid Build Coastguard Worker }
2592*795d594fSAndroid Build Coastguard Worker }
2593*795d594fSAndroid Build Coastguard Worker return true;
2594*795d594fSAndroid Build Coastguard Worker }
2595*795d594fSAndroid Build Coastguard Worker }
2596*795d594fSAndroid Build Coastguard Worker }
2597*795d594fSAndroid Build Coastguard Worker return false;
2598*795d594fSAndroid Build Coastguard Worker }
2599*795d594fSAndroid Build Coastguard Worker
2600*795d594fSAndroid Build Coastguard Worker // Method recognizes the following idiom:
2601*795d594fSAndroid Build Coastguard Worker // q += ABS(a - b) for signed operands a, b
2602*795d594fSAndroid Build Coastguard Worker // Provided that the operands have the same type or are promoted to a wider form.
2603*795d594fSAndroid Build Coastguard Worker // Since this may involve a vector length change, the idiom is handled by going directly
2604*795d594fSAndroid Build Coastguard Worker // to a sad-accumulate node (rather than relying combining finer grained nodes later).
2605*795d594fSAndroid Build Coastguard Worker // TODO: unsigned SAD too?
VectorizeSADIdiom(LoopNode * node,HInstruction * instruction,bool generate_code,DataType::Type reduction_type,uint64_t restrictions)2606*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::VectorizeSADIdiom(LoopNode* node,
2607*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
2608*795d594fSAndroid Build Coastguard Worker bool generate_code,
2609*795d594fSAndroid Build Coastguard Worker DataType::Type reduction_type,
2610*795d594fSAndroid Build Coastguard Worker uint64_t restrictions) {
2611*795d594fSAndroid Build Coastguard Worker // Filter integral "q += ABS(a - b);" reduction, where ABS and SUB
2612*795d594fSAndroid Build Coastguard Worker // are done in the same precision (either int or long).
2613*795d594fSAndroid Build Coastguard Worker if (!instruction->IsAdd() ||
2614*795d594fSAndroid Build Coastguard Worker (reduction_type != DataType::Type::kInt32 && reduction_type != DataType::Type::kInt64)) {
2615*795d594fSAndroid Build Coastguard Worker return false;
2616*795d594fSAndroid Build Coastguard Worker }
2617*795d594fSAndroid Build Coastguard Worker HInstruction* acc = instruction->InputAt(0);
2618*795d594fSAndroid Build Coastguard Worker HInstruction* abs = instruction->InputAt(1);
2619*795d594fSAndroid Build Coastguard Worker HInstruction* a = nullptr;
2620*795d594fSAndroid Build Coastguard Worker HInstruction* b = nullptr;
2621*795d594fSAndroid Build Coastguard Worker if (abs->IsAbs() &&
2622*795d594fSAndroid Build Coastguard Worker abs->GetType() == reduction_type &&
2623*795d594fSAndroid Build Coastguard Worker IsSubConst2(graph_, abs->InputAt(0), /*out*/ &a, /*out*/ &b)) {
2624*795d594fSAndroid Build Coastguard Worker DCHECK(a != nullptr && b != nullptr);
2625*795d594fSAndroid Build Coastguard Worker } else {
2626*795d594fSAndroid Build Coastguard Worker return false;
2627*795d594fSAndroid Build Coastguard Worker }
2628*795d594fSAndroid Build Coastguard Worker // Accept same-type or consistent sign extension for narrower-type on operands a and b.
2629*795d594fSAndroid Build Coastguard Worker // The same-type or narrower operands are called r (a or lower) and s (b or lower).
2630*795d594fSAndroid Build Coastguard Worker // We inspect the operands carefully to pick the most suited type.
2631*795d594fSAndroid Build Coastguard Worker HInstruction* r = a;
2632*795d594fSAndroid Build Coastguard Worker HInstruction* s = b;
2633*795d594fSAndroid Build Coastguard Worker bool is_unsigned = false;
2634*795d594fSAndroid Build Coastguard Worker DataType::Type sub_type = GetNarrowerType(a, b);
2635*795d594fSAndroid Build Coastguard Worker if (reduction_type != sub_type &&
2636*795d594fSAndroid Build Coastguard Worker (!IsNarrowerOperands(a, b, sub_type, &r, &s, &is_unsigned) || is_unsigned)) {
2637*795d594fSAndroid Build Coastguard Worker return false;
2638*795d594fSAndroid Build Coastguard Worker }
2639*795d594fSAndroid Build Coastguard Worker // Try same/narrower type and deal with vector restrictions.
2640*795d594fSAndroid Build Coastguard Worker if (!TrySetVectorType(sub_type, &restrictions) ||
2641*795d594fSAndroid Build Coastguard Worker HasVectorRestrictions(restrictions, kNoSAD) ||
2642*795d594fSAndroid Build Coastguard Worker (reduction_type != sub_type && HasVectorRestrictions(restrictions, kNoWideSAD))) {
2643*795d594fSAndroid Build Coastguard Worker return false;
2644*795d594fSAndroid Build Coastguard Worker }
2645*795d594fSAndroid Build Coastguard Worker // Accept SAD idiom for vectorizable operands. Vectorized code uses the shorthand
2646*795d594fSAndroid Build Coastguard Worker // idiomatic operation. Sequential code uses the original scalar expressions.
2647*795d594fSAndroid Build Coastguard Worker DCHECK(r != nullptr && s != nullptr);
2648*795d594fSAndroid Build Coastguard Worker if (generate_code && synthesis_mode_ != LoopSynthesisMode::kVector) { // de-idiom
2649*795d594fSAndroid Build Coastguard Worker r = s = abs->InputAt(0);
2650*795d594fSAndroid Build Coastguard Worker }
2651*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, acc, generate_code, sub_type, restrictions) &&
2652*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, r, generate_code, sub_type, restrictions) &&
2653*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, s, generate_code, sub_type, restrictions)) {
2654*795d594fSAndroid Build Coastguard Worker if (generate_code) {
2655*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kVector) {
2656*795d594fSAndroid Build Coastguard Worker vector_map_->Put(instruction, new (global_allocator_) HVecSADAccumulate(
2657*795d594fSAndroid Build Coastguard Worker global_allocator_,
2658*795d594fSAndroid Build Coastguard Worker vector_map_->Get(acc),
2659*795d594fSAndroid Build Coastguard Worker vector_map_->Get(r),
2660*795d594fSAndroid Build Coastguard Worker vector_map_->Get(s),
2661*795d594fSAndroid Build Coastguard Worker HVecOperation::ToProperType(reduction_type, is_unsigned),
2662*795d594fSAndroid Build Coastguard Worker GetOtherVL(reduction_type, sub_type, vector_length_),
2663*795d594fSAndroid Build Coastguard Worker kNoDexPc));
2664*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(stats_, MethodCompilationStat::kLoopVectorizedIdiom);
2665*795d594fSAndroid Build Coastguard Worker } else {
2666*795d594fSAndroid Build Coastguard Worker // "GenerateVecOp()" must not be called more than once for each original loop body
2667*795d594fSAndroid Build Coastguard Worker // instruction. As the SAD idiom processes both "current" instruction ("instruction")
2668*795d594fSAndroid Build Coastguard Worker // and its ABS input in one go, we must check that for the scalar case the ABS instruction
2669*795d594fSAndroid Build Coastguard Worker // has not yet been processed.
2670*795d594fSAndroid Build Coastguard Worker if (vector_map_->find(abs) == vector_map_->end()) {
2671*795d594fSAndroid Build Coastguard Worker GenerateVecOp(abs, vector_map_->Get(r), nullptr, reduction_type);
2672*795d594fSAndroid Build Coastguard Worker }
2673*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(acc), vector_map_->Get(abs), reduction_type);
2674*795d594fSAndroid Build Coastguard Worker }
2675*795d594fSAndroid Build Coastguard Worker }
2676*795d594fSAndroid Build Coastguard Worker return true;
2677*795d594fSAndroid Build Coastguard Worker }
2678*795d594fSAndroid Build Coastguard Worker return false;
2679*795d594fSAndroid Build Coastguard Worker }
2680*795d594fSAndroid Build Coastguard Worker
2681*795d594fSAndroid Build Coastguard Worker // Method recognises the following dot product idiom:
2682*795d594fSAndroid Build Coastguard Worker // q += a * b for operands a, b whose type is narrower than the reduction one.
2683*795d594fSAndroid Build Coastguard Worker // Provided that the operands have the same type or are promoted to a wider form.
2684*795d594fSAndroid Build Coastguard Worker // Since this may involve a vector length change, the idiom is handled by going directly
2685*795d594fSAndroid Build Coastguard Worker // to a dot product node (rather than relying combining finer grained nodes later).
VectorizeDotProdIdiom(LoopNode * node,HInstruction * instruction,bool generate_code,DataType::Type reduction_type,uint64_t restrictions)2686*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::VectorizeDotProdIdiom(LoopNode* node,
2687*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
2688*795d594fSAndroid Build Coastguard Worker bool generate_code,
2689*795d594fSAndroid Build Coastguard Worker DataType::Type reduction_type,
2690*795d594fSAndroid Build Coastguard Worker uint64_t restrictions) {
2691*795d594fSAndroid Build Coastguard Worker if (!instruction->IsAdd() || reduction_type != DataType::Type::kInt32) {
2692*795d594fSAndroid Build Coastguard Worker return false;
2693*795d594fSAndroid Build Coastguard Worker }
2694*795d594fSAndroid Build Coastguard Worker
2695*795d594fSAndroid Build Coastguard Worker HInstruction* const acc = instruction->InputAt(0);
2696*795d594fSAndroid Build Coastguard Worker HInstruction* const mul = instruction->InputAt(1);
2697*795d594fSAndroid Build Coastguard Worker if (!mul->IsMul() || mul->GetType() != reduction_type) {
2698*795d594fSAndroid Build Coastguard Worker return false;
2699*795d594fSAndroid Build Coastguard Worker }
2700*795d594fSAndroid Build Coastguard Worker
2701*795d594fSAndroid Build Coastguard Worker HInstruction* const mul_left = mul->InputAt(0);
2702*795d594fSAndroid Build Coastguard Worker HInstruction* const mul_right = mul->InputAt(1);
2703*795d594fSAndroid Build Coastguard Worker HInstruction* r = mul_left;
2704*795d594fSAndroid Build Coastguard Worker HInstruction* s = mul_right;
2705*795d594fSAndroid Build Coastguard Worker DataType::Type op_type = GetNarrowerType(mul_left, mul_right);
2706*795d594fSAndroid Build Coastguard Worker bool is_unsigned = false;
2707*795d594fSAndroid Build Coastguard Worker
2708*795d594fSAndroid Build Coastguard Worker if (!IsNarrowerOperands(mul_left, mul_right, op_type, &r, &s, &is_unsigned)) {
2709*795d594fSAndroid Build Coastguard Worker return false;
2710*795d594fSAndroid Build Coastguard Worker }
2711*795d594fSAndroid Build Coastguard Worker op_type = HVecOperation::ToProperType(op_type, is_unsigned);
2712*795d594fSAndroid Build Coastguard Worker
2713*795d594fSAndroid Build Coastguard Worker if (!TrySetVectorType(op_type, &restrictions) ||
2714*795d594fSAndroid Build Coastguard Worker HasVectorRestrictions(restrictions, kNoDotProd)) {
2715*795d594fSAndroid Build Coastguard Worker return false;
2716*795d594fSAndroid Build Coastguard Worker }
2717*795d594fSAndroid Build Coastguard Worker
2718*795d594fSAndroid Build Coastguard Worker DCHECK(r != nullptr && s != nullptr);
2719*795d594fSAndroid Build Coastguard Worker // Accept dot product idiom for vectorizable operands. Vectorized code uses the shorthand
2720*795d594fSAndroid Build Coastguard Worker // idiomatic operation. Sequential code uses the original scalar expressions.
2721*795d594fSAndroid Build Coastguard Worker if (generate_code && synthesis_mode_ != LoopSynthesisMode::kVector) { // de-idiom
2722*795d594fSAndroid Build Coastguard Worker r = mul_left;
2723*795d594fSAndroid Build Coastguard Worker s = mul_right;
2724*795d594fSAndroid Build Coastguard Worker }
2725*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, acc, generate_code, op_type, restrictions) &&
2726*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, r, generate_code, op_type, restrictions) &&
2727*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, s, generate_code, op_type, restrictions)) {
2728*795d594fSAndroid Build Coastguard Worker if (generate_code) {
2729*795d594fSAndroid Build Coastguard Worker if (synthesis_mode_ == LoopSynthesisMode::kVector) {
2730*795d594fSAndroid Build Coastguard Worker vector_map_->Put(instruction, new (global_allocator_) HVecDotProd(
2731*795d594fSAndroid Build Coastguard Worker global_allocator_,
2732*795d594fSAndroid Build Coastguard Worker vector_map_->Get(acc),
2733*795d594fSAndroid Build Coastguard Worker vector_map_->Get(r),
2734*795d594fSAndroid Build Coastguard Worker vector_map_->Get(s),
2735*795d594fSAndroid Build Coastguard Worker reduction_type,
2736*795d594fSAndroid Build Coastguard Worker is_unsigned,
2737*795d594fSAndroid Build Coastguard Worker GetOtherVL(reduction_type, op_type, vector_length_),
2738*795d594fSAndroid Build Coastguard Worker kNoDexPc));
2739*795d594fSAndroid Build Coastguard Worker MaybeRecordStat(stats_, MethodCompilationStat::kLoopVectorizedIdiom);
2740*795d594fSAndroid Build Coastguard Worker } else {
2741*795d594fSAndroid Build Coastguard Worker // "GenerateVecOp()" must not be called more than once for each original loop body
2742*795d594fSAndroid Build Coastguard Worker // instruction. As the DotProd idiom processes both "current" instruction ("instruction")
2743*795d594fSAndroid Build Coastguard Worker // and its MUL input in one go, we must check that for the scalar case the MUL instruction
2744*795d594fSAndroid Build Coastguard Worker // has not yet been processed.
2745*795d594fSAndroid Build Coastguard Worker if (vector_map_->find(mul) == vector_map_->end()) {
2746*795d594fSAndroid Build Coastguard Worker GenerateVecOp(mul, vector_map_->Get(r), vector_map_->Get(s), reduction_type);
2747*795d594fSAndroid Build Coastguard Worker }
2748*795d594fSAndroid Build Coastguard Worker GenerateVecOp(instruction, vector_map_->Get(acc), vector_map_->Get(mul), reduction_type);
2749*795d594fSAndroid Build Coastguard Worker }
2750*795d594fSAndroid Build Coastguard Worker }
2751*795d594fSAndroid Build Coastguard Worker return true;
2752*795d594fSAndroid Build Coastguard Worker }
2753*795d594fSAndroid Build Coastguard Worker return false;
2754*795d594fSAndroid Build Coastguard Worker }
2755*795d594fSAndroid Build Coastguard Worker
VectorizeIfCondition(LoopNode * node,HInstruction * hif,bool generate_code,uint64_t restrictions)2756*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::VectorizeIfCondition(LoopNode* node,
2757*795d594fSAndroid Build Coastguard Worker HInstruction* hif,
2758*795d594fSAndroid Build Coastguard Worker bool generate_code,
2759*795d594fSAndroid Build Coastguard Worker uint64_t restrictions) {
2760*795d594fSAndroid Build Coastguard Worker DCHECK(hif->IsIf());
2761*795d594fSAndroid Build Coastguard Worker HInstruction* if_input = hif->InputAt(0);
2762*795d594fSAndroid Build Coastguard Worker
2763*795d594fSAndroid Build Coastguard Worker if (!if_input->HasOnlyOneNonEnvironmentUse()) {
2764*795d594fSAndroid Build Coastguard Worker // Avoid the complications of the condition used as materialized boolean.
2765*795d594fSAndroid Build Coastguard Worker return false;
2766*795d594fSAndroid Build Coastguard Worker }
2767*795d594fSAndroid Build Coastguard Worker
2768*795d594fSAndroid Build Coastguard Worker if (!if_input->IsCondition()) {
2769*795d594fSAndroid Build Coastguard Worker return false;
2770*795d594fSAndroid Build Coastguard Worker }
2771*795d594fSAndroid Build Coastguard Worker
2772*795d594fSAndroid Build Coastguard Worker HCondition* cond = if_input->AsCondition();
2773*795d594fSAndroid Build Coastguard Worker HInstruction* opa = cond->InputAt(0);
2774*795d594fSAndroid Build Coastguard Worker HInstruction* opb = cond->InputAt(1);
2775*795d594fSAndroid Build Coastguard Worker DataType::Type type = GetNarrowerType(opa, opb);
2776*795d594fSAndroid Build Coastguard Worker
2777*795d594fSAndroid Build Coastguard Worker if (!DataType::IsIntegralType(type)) {
2778*795d594fSAndroid Build Coastguard Worker return false;
2779*795d594fSAndroid Build Coastguard Worker }
2780*795d594fSAndroid Build Coastguard Worker
2781*795d594fSAndroid Build Coastguard Worker bool is_unsigned = false;
2782*795d594fSAndroid Build Coastguard Worker HInstruction* opa_promoted = opa;
2783*795d594fSAndroid Build Coastguard Worker HInstruction* opb_promoted = opb;
2784*795d594fSAndroid Build Coastguard Worker bool is_int_case = DataType::Type::kInt32 == opa->GetType() &&
2785*795d594fSAndroid Build Coastguard Worker DataType::Type::kInt32 == opb->GetType();
2786*795d594fSAndroid Build Coastguard Worker
2787*795d594fSAndroid Build Coastguard Worker // Condition arguments should be either both int32 or consistently extended signed/unsigned
2788*795d594fSAndroid Build Coastguard Worker // narrower operands.
2789*795d594fSAndroid Build Coastguard Worker if (!is_int_case &&
2790*795d594fSAndroid Build Coastguard Worker !IsNarrowerOperands(opa, opb, type, &opa_promoted, &opb_promoted, &is_unsigned)) {
2791*795d594fSAndroid Build Coastguard Worker return false;
2792*795d594fSAndroid Build Coastguard Worker }
2793*795d594fSAndroid Build Coastguard Worker type = HVecOperation::ToProperType(type, is_unsigned);
2794*795d594fSAndroid Build Coastguard Worker
2795*795d594fSAndroid Build Coastguard Worker // For narrow types, explicit type conversion may have been
2796*795d594fSAndroid Build Coastguard Worker // optimized way, so set the no hi bits restriction here.
2797*795d594fSAndroid Build Coastguard Worker if (DataType::Size(type) <= 2) {
2798*795d594fSAndroid Build Coastguard Worker restrictions |= kNoHiBits;
2799*795d594fSAndroid Build Coastguard Worker }
2800*795d594fSAndroid Build Coastguard Worker
2801*795d594fSAndroid Build Coastguard Worker if (!TrySetVectorType(type, &restrictions) ||
2802*795d594fSAndroid Build Coastguard Worker HasVectorRestrictions(restrictions, kNoIfCond)) {
2803*795d594fSAndroid Build Coastguard Worker return false;
2804*795d594fSAndroid Build Coastguard Worker }
2805*795d594fSAndroid Build Coastguard Worker
2806*795d594fSAndroid Build Coastguard Worker if (generate_code && synthesis_mode_ != LoopSynthesisMode::kVector) { // de-idiom
2807*795d594fSAndroid Build Coastguard Worker opa_promoted = opa;
2808*795d594fSAndroid Build Coastguard Worker opb_promoted = opb;
2809*795d594fSAndroid Build Coastguard Worker }
2810*795d594fSAndroid Build Coastguard Worker
2811*795d594fSAndroid Build Coastguard Worker if (VectorizeUse(node, opa_promoted, generate_code, type, restrictions) &&
2812*795d594fSAndroid Build Coastguard Worker VectorizeUse(node, opb_promoted, generate_code, type, restrictions)) {
2813*795d594fSAndroid Build Coastguard Worker if (generate_code) {
2814*795d594fSAndroid Build Coastguard Worker HInstruction* vec_cond = GenerateVecOp(cond,
2815*795d594fSAndroid Build Coastguard Worker vector_map_->Get(opa_promoted),
2816*795d594fSAndroid Build Coastguard Worker vector_map_->Get(opb_promoted),
2817*795d594fSAndroid Build Coastguard Worker type);
2818*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(synthesis_mode_, LoopSynthesisMode::kVector);
2819*795d594fSAndroid Build Coastguard Worker HInstruction* vec_pred_not = new (global_allocator_)
2820*795d594fSAndroid Build Coastguard Worker HVecPredNot(global_allocator_, vec_cond, type, vector_length_, hif->GetDexPc());
2821*795d594fSAndroid Build Coastguard Worker
2822*795d594fSAndroid Build Coastguard Worker vector_map_->Put(hif, vec_pred_not);
2823*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* pred_info = predicate_info_map_->Get(hif->GetBlock());
2824*795d594fSAndroid Build Coastguard Worker pred_info->SetControlFlowInfo(vec_cond->AsVecPredSetOperation(),
2825*795d594fSAndroid Build Coastguard Worker vec_pred_not->AsVecPredSetOperation());
2826*795d594fSAndroid Build Coastguard Worker }
2827*795d594fSAndroid Build Coastguard Worker return true;
2828*795d594fSAndroid Build Coastguard Worker }
2829*795d594fSAndroid Build Coastguard Worker
2830*795d594fSAndroid Build Coastguard Worker return false;
2831*795d594fSAndroid Build Coastguard Worker }
2832*795d594fSAndroid Build Coastguard Worker
2833*795d594fSAndroid Build Coastguard Worker //
2834*795d594fSAndroid Build Coastguard Worker // Vectorization heuristics.
2835*795d594fSAndroid Build Coastguard Worker //
2836*795d594fSAndroid Build Coastguard Worker
ComputeAlignment(HInstruction * offset,DataType::Type type,bool is_string_char_at,uint32_t peeling)2837*795d594fSAndroid Build Coastguard Worker Alignment HLoopOptimization::ComputeAlignment(HInstruction* offset,
2838*795d594fSAndroid Build Coastguard Worker DataType::Type type,
2839*795d594fSAndroid Build Coastguard Worker bool is_string_char_at,
2840*795d594fSAndroid Build Coastguard Worker uint32_t peeling) {
2841*795d594fSAndroid Build Coastguard Worker // Combine the alignment and hidden offset that is guaranteed by
2842*795d594fSAndroid Build Coastguard Worker // the Android runtime with a known starting index adjusted as bytes.
2843*795d594fSAndroid Build Coastguard Worker int64_t value = 0;
2844*795d594fSAndroid Build Coastguard Worker if (IsInt64AndGet(offset, /*out*/ &value)) {
2845*795d594fSAndroid Build Coastguard Worker uint32_t start_offset =
2846*795d594fSAndroid Build Coastguard Worker HiddenOffset(type, is_string_char_at) + (value + peeling) * DataType::Size(type);
2847*795d594fSAndroid Build Coastguard Worker return Alignment(BaseAlignment(), start_offset & (BaseAlignment() - 1u));
2848*795d594fSAndroid Build Coastguard Worker }
2849*795d594fSAndroid Build Coastguard Worker // Otherwise, the Android runtime guarantees at least natural alignment.
2850*795d594fSAndroid Build Coastguard Worker return Alignment(DataType::Size(type), 0);
2851*795d594fSAndroid Build Coastguard Worker }
2852*795d594fSAndroid Build Coastguard Worker
SetAlignmentStrategy(const ScopedArenaVector<uint32_t> & peeling_votes,const ArrayReference * peeling_candidate)2853*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::SetAlignmentStrategy(const ScopedArenaVector<uint32_t>& peeling_votes,
2854*795d594fSAndroid Build Coastguard Worker const ArrayReference* peeling_candidate) {
2855*795d594fSAndroid Build Coastguard Worker // Current heuristic: pick the best static loop peeling factor, if any,
2856*795d594fSAndroid Build Coastguard Worker // or otherwise use dynamic loop peeling on suggested peeling candidate.
2857*795d594fSAndroid Build Coastguard Worker uint32_t max_vote = 0;
2858*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < peeling_votes.size(); i++) {
2859*795d594fSAndroid Build Coastguard Worker if (peeling_votes[i] > max_vote) {
2860*795d594fSAndroid Build Coastguard Worker max_vote = peeling_votes[i];
2861*795d594fSAndroid Build Coastguard Worker vector_static_peeling_factor_ = i;
2862*795d594fSAndroid Build Coastguard Worker }
2863*795d594fSAndroid Build Coastguard Worker }
2864*795d594fSAndroid Build Coastguard Worker if (max_vote == 0) {
2865*795d594fSAndroid Build Coastguard Worker vector_dynamic_peeling_candidate_ = peeling_candidate;
2866*795d594fSAndroid Build Coastguard Worker }
2867*795d594fSAndroid Build Coastguard Worker }
2868*795d594fSAndroid Build Coastguard Worker
MaxNumberPeeled()2869*795d594fSAndroid Build Coastguard Worker uint32_t HLoopOptimization::MaxNumberPeeled() {
2870*795d594fSAndroid Build Coastguard Worker if (vector_dynamic_peeling_candidate_ != nullptr) {
2871*795d594fSAndroid Build Coastguard Worker return vector_length_ - 1u; // worst-case
2872*795d594fSAndroid Build Coastguard Worker }
2873*795d594fSAndroid Build Coastguard Worker return vector_static_peeling_factor_; // known exactly
2874*795d594fSAndroid Build Coastguard Worker }
2875*795d594fSAndroid Build Coastguard Worker
IsVectorizationProfitable(int64_t trip_count)2876*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::IsVectorizationProfitable(int64_t trip_count) {
2877*795d594fSAndroid Build Coastguard Worker // Current heuristic: non-empty body with sufficient number of iterations (if known).
2878*795d594fSAndroid Build Coastguard Worker // TODO: refine by looking at e.g. operation count, alignment, etc.
2879*795d594fSAndroid Build Coastguard Worker // TODO: trip count is really unsigned entity, provided the guarding test
2880*795d594fSAndroid Build Coastguard Worker // is satisfied; deal with this more carefully later
2881*795d594fSAndroid Build Coastguard Worker uint32_t max_peel = MaxNumberPeeled();
2882*795d594fSAndroid Build Coastguard Worker // Peeling is not supported in predicated mode.
2883*795d594fSAndroid Build Coastguard Worker DCHECK_IMPLIES(IsInPredicatedVectorizationMode(), max_peel == 0u);
2884*795d594fSAndroid Build Coastguard Worker if (vector_length_ == 0) {
2885*795d594fSAndroid Build Coastguard Worker return false; // nothing found
2886*795d594fSAndroid Build Coastguard Worker } else if (trip_count < 0) {
2887*795d594fSAndroid Build Coastguard Worker return false; // guard against non-taken/large
2888*795d594fSAndroid Build Coastguard Worker } else if ((0 < trip_count) && (trip_count < (vector_length_ + max_peel))) {
2889*795d594fSAndroid Build Coastguard Worker return false; // insufficient iterations
2890*795d594fSAndroid Build Coastguard Worker }
2891*795d594fSAndroid Build Coastguard Worker return true;
2892*795d594fSAndroid Build Coastguard Worker }
2893*795d594fSAndroid Build Coastguard Worker
2894*795d594fSAndroid Build Coastguard Worker //
2895*795d594fSAndroid Build Coastguard Worker // Helpers.
2896*795d594fSAndroid Build Coastguard Worker //
2897*795d594fSAndroid Build Coastguard Worker
TrySetPhiInduction(HPhi * phi,bool restrict_uses)2898*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TrySetPhiInduction(HPhi* phi, bool restrict_uses) {
2899*795d594fSAndroid Build Coastguard Worker // Start with empty phi induction.
2900*795d594fSAndroid Build Coastguard Worker iset_->clear();
2901*795d594fSAndroid Build Coastguard Worker
2902*795d594fSAndroid Build Coastguard Worker // Special case Phis that have equivalent in a debuggable setup. Our graph checker isn't
2903*795d594fSAndroid Build Coastguard Worker // smart enough to follow strongly connected components (and it's probably not worth
2904*795d594fSAndroid Build Coastguard Worker // it to make it so). See b/33775412.
2905*795d594fSAndroid Build Coastguard Worker if (graph_->IsDebuggable() && phi->HasEquivalentPhi()) {
2906*795d594fSAndroid Build Coastguard Worker return false;
2907*795d594fSAndroid Build Coastguard Worker }
2908*795d594fSAndroid Build Coastguard Worker
2909*795d594fSAndroid Build Coastguard Worker // Lookup phi induction cycle.
2910*795d594fSAndroid Build Coastguard Worker ArenaSet<HInstruction*>* set = induction_range_.LookupCycle(phi);
2911*795d594fSAndroid Build Coastguard Worker if (set != nullptr) {
2912*795d594fSAndroid Build Coastguard Worker for (HInstruction* i : *set) {
2913*795d594fSAndroid Build Coastguard Worker // Check that, other than instructions that are no longer in the graph (removed earlier)
2914*795d594fSAndroid Build Coastguard Worker // each instruction is removable and, when restrict uses are requested, other than for phi,
2915*795d594fSAndroid Build Coastguard Worker // all uses are contained within the cycle.
2916*795d594fSAndroid Build Coastguard Worker if (!i->IsInBlock()) {
2917*795d594fSAndroid Build Coastguard Worker continue;
2918*795d594fSAndroid Build Coastguard Worker } else if (!i->IsRemovable()) {
2919*795d594fSAndroid Build Coastguard Worker return false;
2920*795d594fSAndroid Build Coastguard Worker } else if (i != phi && restrict_uses) {
2921*795d594fSAndroid Build Coastguard Worker // Deal with regular uses.
2922*795d594fSAndroid Build Coastguard Worker for (const HUseListNode<HInstruction*>& use : i->GetUses()) {
2923*795d594fSAndroid Build Coastguard Worker if (set->find(use.GetUser()) == set->end()) {
2924*795d594fSAndroid Build Coastguard Worker return false;
2925*795d594fSAndroid Build Coastguard Worker }
2926*795d594fSAndroid Build Coastguard Worker }
2927*795d594fSAndroid Build Coastguard Worker }
2928*795d594fSAndroid Build Coastguard Worker iset_->insert(i); // copy
2929*795d594fSAndroid Build Coastguard Worker }
2930*795d594fSAndroid Build Coastguard Worker return true;
2931*795d594fSAndroid Build Coastguard Worker }
2932*795d594fSAndroid Build Coastguard Worker return false;
2933*795d594fSAndroid Build Coastguard Worker }
2934*795d594fSAndroid Build Coastguard Worker
TrySetPhiReduction(HPhi * phi)2935*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TrySetPhiReduction(HPhi* phi) {
2936*795d594fSAndroid Build Coastguard Worker DCHECK(phi->IsLoopHeaderPhi());
2937*795d594fSAndroid Build Coastguard Worker // Only unclassified phi cycles are candidates for reductions.
2938*795d594fSAndroid Build Coastguard Worker if (induction_range_.IsClassified(phi)) {
2939*795d594fSAndroid Build Coastguard Worker return false;
2940*795d594fSAndroid Build Coastguard Worker }
2941*795d594fSAndroid Build Coastguard Worker // Accept operations like x = x + .., provided that the phi and the reduction are
2942*795d594fSAndroid Build Coastguard Worker // used exactly once inside the loop, and by each other.
2943*795d594fSAndroid Build Coastguard Worker HInputsRef inputs = phi->GetInputs();
2944*795d594fSAndroid Build Coastguard Worker if (inputs.size() == 2) {
2945*795d594fSAndroid Build Coastguard Worker HInstruction* reduction = inputs[1];
2946*795d594fSAndroid Build Coastguard Worker if (HasReductionFormat(reduction, phi)) {
2947*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = phi->GetBlock()->GetLoopInformation();
2948*795d594fSAndroid Build Coastguard Worker DCHECK(loop_info->Contains(*reduction->GetBlock()));
2949*795d594fSAndroid Build Coastguard Worker const bool single_use_inside_loop =
2950*795d594fSAndroid Build Coastguard Worker // Reduction update only used by phi.
2951*795d594fSAndroid Build Coastguard Worker reduction->GetUses().HasExactlyOneElement() &&
2952*795d594fSAndroid Build Coastguard Worker !reduction->HasEnvironmentUses() &&
2953*795d594fSAndroid Build Coastguard Worker // Reduction update is only use of phi inside the loop.
2954*795d594fSAndroid Build Coastguard Worker std::none_of(phi->GetUses().begin(),
2955*795d594fSAndroid Build Coastguard Worker phi->GetUses().end(),
2956*795d594fSAndroid Build Coastguard Worker [loop_info, reduction](const HUseListNode<HInstruction*>& use) {
2957*795d594fSAndroid Build Coastguard Worker HInstruction* user = use.GetUser();
2958*795d594fSAndroid Build Coastguard Worker return user != reduction && loop_info->Contains(*user->GetBlock());
2959*795d594fSAndroid Build Coastguard Worker });
2960*795d594fSAndroid Build Coastguard Worker if (single_use_inside_loop) {
2961*795d594fSAndroid Build Coastguard Worker // Link reduction back, and start recording feed value.
2962*795d594fSAndroid Build Coastguard Worker reductions_->Put(reduction, phi);
2963*795d594fSAndroid Build Coastguard Worker reductions_->Put(phi, phi->InputAt(0));
2964*795d594fSAndroid Build Coastguard Worker return true;
2965*795d594fSAndroid Build Coastguard Worker }
2966*795d594fSAndroid Build Coastguard Worker }
2967*795d594fSAndroid Build Coastguard Worker }
2968*795d594fSAndroid Build Coastguard Worker return false;
2969*795d594fSAndroid Build Coastguard Worker }
2970*795d594fSAndroid Build Coastguard Worker
TrySetSimpleLoopHeader(HBasicBlock * block,HPhi ** main_phi)2971*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TrySetSimpleLoopHeader(HBasicBlock* block, /*out*/ HPhi** main_phi) {
2972*795d594fSAndroid Build Coastguard Worker // Start with empty phi induction and reductions.
2973*795d594fSAndroid Build Coastguard Worker iset_->clear();
2974*795d594fSAndroid Build Coastguard Worker reductions_->clear();
2975*795d594fSAndroid Build Coastguard Worker
2976*795d594fSAndroid Build Coastguard Worker // Scan the phis to find the following (the induction structure has already
2977*795d594fSAndroid Build Coastguard Worker // been optimized, so we don't need to worry about trivial cases):
2978*795d594fSAndroid Build Coastguard Worker // (1) optional reductions in loop,
2979*795d594fSAndroid Build Coastguard Worker // (2) the main induction, used in loop control.
2980*795d594fSAndroid Build Coastguard Worker HPhi* phi = nullptr;
2981*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
2982*795d594fSAndroid Build Coastguard Worker if (TrySetPhiReduction(it.Current()->AsPhi())) {
2983*795d594fSAndroid Build Coastguard Worker continue;
2984*795d594fSAndroid Build Coastguard Worker } else if (phi == nullptr) {
2985*795d594fSAndroid Build Coastguard Worker // Found the first candidate for main induction.
2986*795d594fSAndroid Build Coastguard Worker phi = it.Current()->AsPhi();
2987*795d594fSAndroid Build Coastguard Worker } else {
2988*795d594fSAndroid Build Coastguard Worker return false;
2989*795d594fSAndroid Build Coastguard Worker }
2990*795d594fSAndroid Build Coastguard Worker }
2991*795d594fSAndroid Build Coastguard Worker
2992*795d594fSAndroid Build Coastguard Worker // Then test for a typical loopheader:
2993*795d594fSAndroid Build Coastguard Worker // s: SuspendCheck
2994*795d594fSAndroid Build Coastguard Worker // c: Condition(phi, bound)
2995*795d594fSAndroid Build Coastguard Worker // i: If(c)
2996*795d594fSAndroid Build Coastguard Worker if (phi != nullptr && TrySetPhiInduction(phi, /*restrict_uses*/ false)) {
2997*795d594fSAndroid Build Coastguard Worker HInstruction* s = block->GetFirstInstruction();
2998*795d594fSAndroid Build Coastguard Worker if (s != nullptr && s->IsSuspendCheck()) {
2999*795d594fSAndroid Build Coastguard Worker HInstruction* c = s->GetNext();
3000*795d594fSAndroid Build Coastguard Worker if (c != nullptr &&
3001*795d594fSAndroid Build Coastguard Worker c->IsCondition() &&
3002*795d594fSAndroid Build Coastguard Worker c->GetUses().HasExactlyOneElement() && // only used for termination
3003*795d594fSAndroid Build Coastguard Worker !c->HasEnvironmentUses()) { // unlikely, but not impossible
3004*795d594fSAndroid Build Coastguard Worker HInstruction* i = c->GetNext();
3005*795d594fSAndroid Build Coastguard Worker if (i != nullptr && i->IsIf() && i->InputAt(0) == c) {
3006*795d594fSAndroid Build Coastguard Worker iset_->insert(c);
3007*795d594fSAndroid Build Coastguard Worker iset_->insert(s);
3008*795d594fSAndroid Build Coastguard Worker *main_phi = phi;
3009*795d594fSAndroid Build Coastguard Worker return true;
3010*795d594fSAndroid Build Coastguard Worker }
3011*795d594fSAndroid Build Coastguard Worker }
3012*795d594fSAndroid Build Coastguard Worker }
3013*795d594fSAndroid Build Coastguard Worker }
3014*795d594fSAndroid Build Coastguard Worker return false;
3015*795d594fSAndroid Build Coastguard Worker }
3016*795d594fSAndroid Build Coastguard Worker
IsEmptyBody(HBasicBlock * block)3017*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::IsEmptyBody(HBasicBlock* block) {
3018*795d594fSAndroid Build Coastguard Worker if (!block->GetPhis().IsEmpty()) {
3019*795d594fSAndroid Build Coastguard Worker return false;
3020*795d594fSAndroid Build Coastguard Worker }
3021*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
3022*795d594fSAndroid Build Coastguard Worker HInstruction* instruction = it.Current();
3023*795d594fSAndroid Build Coastguard Worker if (!instruction->IsGoto() && iset_->find(instruction) == iset_->end()) {
3024*795d594fSAndroid Build Coastguard Worker return false;
3025*795d594fSAndroid Build Coastguard Worker }
3026*795d594fSAndroid Build Coastguard Worker }
3027*795d594fSAndroid Build Coastguard Worker return true;
3028*795d594fSAndroid Build Coastguard Worker }
3029*795d594fSAndroid Build Coastguard Worker
IsUsedOutsideLoop(HLoopInformation * loop_info,HInstruction * instruction)3030*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::IsUsedOutsideLoop(HLoopInformation* loop_info,
3031*795d594fSAndroid Build Coastguard Worker HInstruction* instruction) {
3032*795d594fSAndroid Build Coastguard Worker // Deal with regular uses.
3033*795d594fSAndroid Build Coastguard Worker for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
3034*795d594fSAndroid Build Coastguard Worker if (use.GetUser()->GetBlock()->GetLoopInformation() != loop_info) {
3035*795d594fSAndroid Build Coastguard Worker return true;
3036*795d594fSAndroid Build Coastguard Worker }
3037*795d594fSAndroid Build Coastguard Worker }
3038*795d594fSAndroid Build Coastguard Worker return false;
3039*795d594fSAndroid Build Coastguard Worker }
3040*795d594fSAndroid Build Coastguard Worker
IsOnlyUsedAfterLoop(HLoopInformation * loop_info,HInstruction * instruction,bool collect_loop_uses,uint32_t * use_count)3041*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
3042*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
3043*795d594fSAndroid Build Coastguard Worker bool collect_loop_uses,
3044*795d594fSAndroid Build Coastguard Worker /*out*/ uint32_t* use_count) {
3045*795d594fSAndroid Build Coastguard Worker // Deal with regular uses.
3046*795d594fSAndroid Build Coastguard Worker for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
3047*795d594fSAndroid Build Coastguard Worker HInstruction* user = use.GetUser();
3048*795d594fSAndroid Build Coastguard Worker if (iset_->find(user) == iset_->end()) { // not excluded?
3049*795d594fSAndroid Build Coastguard Worker if (loop_info->Contains(*user->GetBlock())) {
3050*795d594fSAndroid Build Coastguard Worker // If collect_loop_uses is set, simply keep adding those uses to the set.
3051*795d594fSAndroid Build Coastguard Worker // Otherwise, reject uses inside the loop that were not already in the set.
3052*795d594fSAndroid Build Coastguard Worker if (collect_loop_uses) {
3053*795d594fSAndroid Build Coastguard Worker iset_->insert(user);
3054*795d594fSAndroid Build Coastguard Worker continue;
3055*795d594fSAndroid Build Coastguard Worker }
3056*795d594fSAndroid Build Coastguard Worker return false;
3057*795d594fSAndroid Build Coastguard Worker }
3058*795d594fSAndroid Build Coastguard Worker ++*use_count;
3059*795d594fSAndroid Build Coastguard Worker }
3060*795d594fSAndroid Build Coastguard Worker }
3061*795d594fSAndroid Build Coastguard Worker return true;
3062*795d594fSAndroid Build Coastguard Worker }
3063*795d594fSAndroid Build Coastguard Worker
TryReplaceWithLastValue(HLoopInformation * loop_info,HInstruction * instruction,HBasicBlock * block)3064*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryReplaceWithLastValue(HLoopInformation* loop_info,
3065*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
3066*795d594fSAndroid Build Coastguard Worker HBasicBlock* block) {
3067*795d594fSAndroid Build Coastguard Worker // Try to replace outside uses with the last value.
3068*795d594fSAndroid Build Coastguard Worker if (induction_range_.CanGenerateLastValue(instruction)) {
3069*795d594fSAndroid Build Coastguard Worker HInstruction* replacement = induction_range_.GenerateLastValue(instruction, graph_, block);
3070*795d594fSAndroid Build Coastguard Worker // Deal with regular uses.
3071*795d594fSAndroid Build Coastguard Worker const HUseList<HInstruction*>& uses = instruction->GetUses();
3072*795d594fSAndroid Build Coastguard Worker for (auto it = uses.begin(), end = uses.end(); it != end;) {
3073*795d594fSAndroid Build Coastguard Worker HInstruction* user = it->GetUser();
3074*795d594fSAndroid Build Coastguard Worker size_t index = it->GetIndex();
3075*795d594fSAndroid Build Coastguard Worker ++it; // increment before replacing
3076*795d594fSAndroid Build Coastguard Worker if (iset_->find(user) == iset_->end()) { // not excluded?
3077*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
3078*795d594fSAndroid Build Coastguard Worker // We have checked earlier in 'IsOnlyUsedAfterLoop' that the use is after the loop.
3079*795d594fSAndroid Build Coastguard Worker HLoopInformation* other_loop_info = user->GetBlock()->GetLoopInformation();
3080*795d594fSAndroid Build Coastguard Worker CHECK(other_loop_info == nullptr || !other_loop_info->IsIn(*loop_info));
3081*795d594fSAndroid Build Coastguard Worker }
3082*795d594fSAndroid Build Coastguard Worker user->ReplaceInput(replacement, index);
3083*795d594fSAndroid Build Coastguard Worker induction_range_.Replace(user, instruction, replacement); // update induction
3084*795d594fSAndroid Build Coastguard Worker }
3085*795d594fSAndroid Build Coastguard Worker }
3086*795d594fSAndroid Build Coastguard Worker // Deal with environment uses.
3087*795d594fSAndroid Build Coastguard Worker const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses();
3088*795d594fSAndroid Build Coastguard Worker for (auto it = env_uses.begin(), end = env_uses.end(); it != end;) {
3089*795d594fSAndroid Build Coastguard Worker HEnvironment* user = it->GetUser();
3090*795d594fSAndroid Build Coastguard Worker size_t index = it->GetIndex();
3091*795d594fSAndroid Build Coastguard Worker ++it; // increment before replacing
3092*795d594fSAndroid Build Coastguard Worker if (iset_->find(user->GetHolder()) == iset_->end()) { // not excluded?
3093*795d594fSAndroid Build Coastguard Worker // Only update environment uses after the loop.
3094*795d594fSAndroid Build Coastguard Worker HLoopInformation* other_loop_info = user->GetHolder()->GetBlock()->GetLoopInformation();
3095*795d594fSAndroid Build Coastguard Worker if (other_loop_info == nullptr || !other_loop_info->IsIn(*loop_info)) {
3096*795d594fSAndroid Build Coastguard Worker user->RemoveAsUserOfInput(index);
3097*795d594fSAndroid Build Coastguard Worker user->SetRawEnvAt(index, replacement);
3098*795d594fSAndroid Build Coastguard Worker replacement->AddEnvUseAt(user, index);
3099*795d594fSAndroid Build Coastguard Worker }
3100*795d594fSAndroid Build Coastguard Worker }
3101*795d594fSAndroid Build Coastguard Worker }
3102*795d594fSAndroid Build Coastguard Worker return true;
3103*795d594fSAndroid Build Coastguard Worker }
3104*795d594fSAndroid Build Coastguard Worker return false;
3105*795d594fSAndroid Build Coastguard Worker }
3106*795d594fSAndroid Build Coastguard Worker
TryAssignLastValue(HLoopInformation * loop_info,HInstruction * instruction,HBasicBlock * block,bool collect_loop_uses)3107*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::TryAssignLastValue(HLoopInformation* loop_info,
3108*795d594fSAndroid Build Coastguard Worker HInstruction* instruction,
3109*795d594fSAndroid Build Coastguard Worker HBasicBlock* block,
3110*795d594fSAndroid Build Coastguard Worker bool collect_loop_uses) {
3111*795d594fSAndroid Build Coastguard Worker // Assigning the last value is always successful if there are no uses.
3112*795d594fSAndroid Build Coastguard Worker // Otherwise, it succeeds in a no early-exit loop by generating the
3113*795d594fSAndroid Build Coastguard Worker // proper last value assignment.
3114*795d594fSAndroid Build Coastguard Worker uint32_t use_count = 0;
3115*795d594fSAndroid Build Coastguard Worker return IsOnlyUsedAfterLoop(loop_info, instruction, collect_loop_uses, &use_count) &&
3116*795d594fSAndroid Build Coastguard Worker (use_count == 0 ||
3117*795d594fSAndroid Build Coastguard Worker (!IsEarlyExit(loop_info) && TryReplaceWithLastValue(loop_info, instruction, block)));
3118*795d594fSAndroid Build Coastguard Worker }
3119*795d594fSAndroid Build Coastguard Worker
RemoveDeadInstructions(const HInstructionList & list)3120*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::RemoveDeadInstructions(const HInstructionList& list) {
3121*795d594fSAndroid Build Coastguard Worker for (HBackwardInstructionIterator i(list); !i.Done(); i.Advance()) {
3122*795d594fSAndroid Build Coastguard Worker HInstruction* instruction = i.Current();
3123*795d594fSAndroid Build Coastguard Worker if (instruction->IsDeadAndRemovable()) {
3124*795d594fSAndroid Build Coastguard Worker simplified_ = true;
3125*795d594fSAndroid Build Coastguard Worker instruction->GetBlock()->RemoveInstructionOrPhi(instruction);
3126*795d594fSAndroid Build Coastguard Worker }
3127*795d594fSAndroid Build Coastguard Worker }
3128*795d594fSAndroid Build Coastguard Worker }
3129*795d594fSAndroid Build Coastguard Worker
CanRemoveCycle()3130*795d594fSAndroid Build Coastguard Worker bool HLoopOptimization::CanRemoveCycle() {
3131*795d594fSAndroid Build Coastguard Worker for (HInstruction* i : *iset_) {
3132*795d594fSAndroid Build Coastguard Worker // We can never remove instructions that have environment
3133*795d594fSAndroid Build Coastguard Worker // uses when we compile 'debuggable'.
3134*795d594fSAndroid Build Coastguard Worker if (i->HasEnvironmentUses() && graph_->IsDebuggable()) {
3135*795d594fSAndroid Build Coastguard Worker return false;
3136*795d594fSAndroid Build Coastguard Worker }
3137*795d594fSAndroid Build Coastguard Worker // A deoptimization should never have an environment input removed.
3138*795d594fSAndroid Build Coastguard Worker for (const HUseListNode<HEnvironment*>& use : i->GetEnvUses()) {
3139*795d594fSAndroid Build Coastguard Worker if (use.GetUser()->GetHolder()->IsDeoptimize()) {
3140*795d594fSAndroid Build Coastguard Worker return false;
3141*795d594fSAndroid Build Coastguard Worker }
3142*795d594fSAndroid Build Coastguard Worker }
3143*795d594fSAndroid Build Coastguard Worker }
3144*795d594fSAndroid Build Coastguard Worker return true;
3145*795d594fSAndroid Build Coastguard Worker }
3146*795d594fSAndroid Build Coastguard Worker
PreparePredicateInfoMap(LoopNode * node)3147*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::PreparePredicateInfoMap(LoopNode* node) {
3148*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = node->loop_info;
3149*795d594fSAndroid Build Coastguard Worker
3150*795d594fSAndroid Build Coastguard Worker DCHECK(IsPredicatedLoopControlFlowSupported(loop_info));
3151*795d594fSAndroid Build Coastguard Worker
3152*795d594fSAndroid Build Coastguard Worker for (HBlocksInLoopIterator block_it(*loop_info);
3153*795d594fSAndroid Build Coastguard Worker !block_it.Done();
3154*795d594fSAndroid Build Coastguard Worker block_it.Advance()) {
3155*795d594fSAndroid Build Coastguard Worker HBasicBlock* cur_block = block_it.Current();
3156*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* pred_info = new (loop_allocator_) BlockPredicateInfo();
3157*795d594fSAndroid Build Coastguard Worker
3158*795d594fSAndroid Build Coastguard Worker predicate_info_map_->Put(cur_block, pred_info);
3159*795d594fSAndroid Build Coastguard Worker }
3160*795d594fSAndroid Build Coastguard Worker }
3161*795d594fSAndroid Build Coastguard Worker
InitPredicateInfoMap(LoopNode * node,HVecPredSetOperation * loop_main_pred)3162*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::InitPredicateInfoMap(LoopNode* node,
3163*795d594fSAndroid Build Coastguard Worker HVecPredSetOperation* loop_main_pred) {
3164*795d594fSAndroid Build Coastguard Worker HLoopInformation* loop_info = node->loop_info;
3165*795d594fSAndroid Build Coastguard Worker HBasicBlock* header = loop_info->GetHeader();
3166*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* header_info = predicate_info_map_->Get(header);
3167*795d594fSAndroid Build Coastguard Worker // Loop header is a special case; it doesn't have a false predicate because we
3168*795d594fSAndroid Build Coastguard Worker // would just exit the loop then.
3169*795d594fSAndroid Build Coastguard Worker header_info->SetControlFlowInfo(loop_main_pred, loop_main_pred);
3170*795d594fSAndroid Build Coastguard Worker
3171*795d594fSAndroid Build Coastguard Worker size_t blocks_in_loop = header->GetLoopInformation()->GetBlocks().NumSetBits();
3172*795d594fSAndroid Build Coastguard Worker if (blocks_in_loop == 2) {
3173*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* successor : header->GetSuccessors()) {
3174*795d594fSAndroid Build Coastguard Worker if (loop_info->Contains(*successor)) {
3175*795d594fSAndroid Build Coastguard Worker // This is loop second block - body.
3176*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* body_info = predicate_info_map_->Get(successor);
3177*795d594fSAndroid Build Coastguard Worker body_info->SetControlPredicate(loop_main_pred);
3178*795d594fSAndroid Build Coastguard Worker return;
3179*795d594fSAndroid Build Coastguard Worker }
3180*795d594fSAndroid Build Coastguard Worker }
3181*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Unreachable";
3182*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
3183*795d594fSAndroid Build Coastguard Worker }
3184*795d594fSAndroid Build Coastguard Worker
3185*795d594fSAndroid Build Coastguard Worker // TODO: support predicated vectorization of CF loop of more complex structure.
3186*795d594fSAndroid Build Coastguard Worker DCHECK(HasLoopDiamondStructure(loop_info));
3187*795d594fSAndroid Build Coastguard Worker HBasicBlock* header_succ_0 = header->GetSuccessors()[0];
3188*795d594fSAndroid Build Coastguard Worker HBasicBlock* header_succ_1 = header->GetSuccessors()[1];
3189*795d594fSAndroid Build Coastguard Worker HBasicBlock* diamond_top = loop_info->Contains(*header_succ_0) ?
3190*795d594fSAndroid Build Coastguard Worker header_succ_0 :
3191*795d594fSAndroid Build Coastguard Worker header_succ_1;
3192*795d594fSAndroid Build Coastguard Worker
3193*795d594fSAndroid Build Coastguard Worker HIf* diamond_hif = diamond_top->GetLastInstruction()->AsIf();
3194*795d594fSAndroid Build Coastguard Worker HBasicBlock* diamond_true = diamond_hif->IfTrueSuccessor();
3195*795d594fSAndroid Build Coastguard Worker HBasicBlock* diamond_false = diamond_hif->IfFalseSuccessor();
3196*795d594fSAndroid Build Coastguard Worker HBasicBlock* back_edge = diamond_true->GetSingleSuccessor();
3197*795d594fSAndroid Build Coastguard Worker
3198*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* diamond_top_info = predicate_info_map_->Get(diamond_top);
3199*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* diamond_true_info = predicate_info_map_->Get(diamond_true);
3200*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* diamond_false_info = predicate_info_map_->Get(diamond_false);
3201*795d594fSAndroid Build Coastguard Worker BlockPredicateInfo* back_edge_info = predicate_info_map_->Get(back_edge);
3202*795d594fSAndroid Build Coastguard Worker
3203*795d594fSAndroid Build Coastguard Worker diamond_top_info->SetControlPredicate(header_info->GetTruePredicate());
3204*795d594fSAndroid Build Coastguard Worker
3205*795d594fSAndroid Build Coastguard Worker diamond_true_info->SetControlPredicate(diamond_top_info->GetTruePredicate());
3206*795d594fSAndroid Build Coastguard Worker diamond_false_info->SetControlPredicate(diamond_top_info->GetFalsePredicate());
3207*795d594fSAndroid Build Coastguard Worker
3208*795d594fSAndroid Build Coastguard Worker back_edge_info->SetControlPredicate(header_info->GetTruePredicate());
3209*795d594fSAndroid Build Coastguard Worker }
3210*795d594fSAndroid Build Coastguard Worker
MaybeInsertInVectorExternalSet(HInstruction * instruction)3211*795d594fSAndroid Build Coastguard Worker void HLoopOptimization::MaybeInsertInVectorExternalSet(HInstruction* instruction) {
3212*795d594fSAndroid Build Coastguard Worker if (IsInPredicatedVectorizationMode()) {
3213*795d594fSAndroid Build Coastguard Worker vector_external_set_->insert(instruction);
3214*795d594fSAndroid Build Coastguard Worker }
3215*795d594fSAndroid Build Coastguard Worker }
3216*795d594fSAndroid Build Coastguard Worker
operator <<(std::ostream & os,const HLoopOptimization::LoopSynthesisMode & mode)3217*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const HLoopOptimization::LoopSynthesisMode& mode) {
3218*795d594fSAndroid Build Coastguard Worker switch (mode) {
3219*795d594fSAndroid Build Coastguard Worker case HLoopOptimization::LoopSynthesisMode::kSequential:
3220*795d594fSAndroid Build Coastguard Worker return os << "kSequential";
3221*795d594fSAndroid Build Coastguard Worker case HLoopOptimization::LoopSynthesisMode::kVector:
3222*795d594fSAndroid Build Coastguard Worker return os << "kVector";
3223*795d594fSAndroid Build Coastguard Worker }
3224*795d594fSAndroid Build Coastguard Worker return os;
3225*795d594fSAndroid Build Coastguard Worker }
3226*795d594fSAndroid Build Coastguard Worker
3227*795d594fSAndroid Build Coastguard Worker } // namespace art
3228