xref: /aosp_15_r20/art/compiler/optimizing/code_generator_utils.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 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 "code_generator_utils.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
24*795d594fSAndroid Build Coastguard Worker 
CalculateMagicAndShiftForDivRem(int64_t divisor,bool is_long,int64_t * magic,int * shift)25*795d594fSAndroid Build Coastguard Worker void CalculateMagicAndShiftForDivRem(int64_t divisor, bool is_long,
26*795d594fSAndroid Build Coastguard Worker                                      int64_t* magic, int* shift) {
27*795d594fSAndroid Build Coastguard Worker   // It does not make sense to calculate magic and shift for zero divisor.
28*795d594fSAndroid Build Coastguard Worker   DCHECK_NE(divisor, 0);
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker   /* Implementation according to H.S.Warren's "Hacker's Delight" (Addison Wesley, 2002)
31*795d594fSAndroid Build Coastguard Worker    * Chapter 10 and T.Grablund, P.L.Montogomery's "Division by Invariant Integers Using
32*795d594fSAndroid Build Coastguard Worker    * Multiplication" (PLDI 1994).
33*795d594fSAndroid Build Coastguard Worker    * The magic number M and shift S can be calculated in the following way:
34*795d594fSAndroid Build Coastguard Worker    * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
35*795d594fSAndroid Build Coastguard Worker    * where divisor(d) >= 2.
36*795d594fSAndroid Build Coastguard Worker    * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
37*795d594fSAndroid Build Coastguard Worker    * where divisor(d) <= -2.
38*795d594fSAndroid Build Coastguard Worker    * Thus nc can be calculated like:
39*795d594fSAndroid Build Coastguard Worker    * nc = exp + exp % d - 1, where d >= 2 and exp = 2^31 for int or 2^63 for long
40*795d594fSAndroid Build Coastguard Worker    * nc = -exp + (exp + 1) % d, where d >= 2 and exp = 2^31 for int or 2^63 for long
41*795d594fSAndroid Build Coastguard Worker    *
42*795d594fSAndroid Build Coastguard Worker    * So the shift p is the smallest p satisfying
43*795d594fSAndroid Build Coastguard Worker    * 2^p > nc * (d - 2^p % d), where d >= 2
44*795d594fSAndroid Build Coastguard Worker    * 2^p > nc * (d + 2^p % d), where d <= -2.
45*795d594fSAndroid Build Coastguard Worker    *
46*795d594fSAndroid Build Coastguard Worker    * The magic number M is calculated by
47*795d594fSAndroid Build Coastguard Worker    * M = (2^p + d - 2^p % d) / d, where d >= 2
48*795d594fSAndroid Build Coastguard Worker    * M = (2^p - d - 2^p % d) / d, where d <= -2.
49*795d594fSAndroid Build Coastguard Worker    *
50*795d594fSAndroid Build Coastguard Worker    * Notice that p is always bigger than or equal to 32 (resp. 64), so we just return 32 - p
51*795d594fSAndroid Build Coastguard Worker    * (resp. 64 - p) as the shift number S.
52*795d594fSAndroid Build Coastguard Worker    */
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   int64_t p = is_long ? 63 : 31;
55*795d594fSAndroid Build Coastguard Worker   const uint64_t exp = is_long ? (UINT64_C(1) << 63) : (UINT32_C(1) << 31);
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker   // Initialize the computations.
58*795d594fSAndroid Build Coastguard Worker   uint64_t abs_d = (divisor >= 0) ? divisor : -divisor;
59*795d594fSAndroid Build Coastguard Worker   uint64_t sign_bit = is_long ? static_cast<uint64_t>(divisor) >> 63 :
60*795d594fSAndroid Build Coastguard Worker                                 static_cast<uint32_t>(divisor) >> 31;
61*795d594fSAndroid Build Coastguard Worker   uint64_t tmp = exp + sign_bit;
62*795d594fSAndroid Build Coastguard Worker   uint64_t abs_nc = tmp - 1 - (tmp % abs_d);
63*795d594fSAndroid Build Coastguard Worker   uint64_t quotient1 = exp / abs_nc;
64*795d594fSAndroid Build Coastguard Worker   uint64_t remainder1 = exp % abs_nc;
65*795d594fSAndroid Build Coastguard Worker   uint64_t quotient2 = exp / abs_d;
66*795d594fSAndroid Build Coastguard Worker   uint64_t remainder2 = exp % abs_d;
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker   /*
69*795d594fSAndroid Build Coastguard Worker    * To avoid handling both positive and negative divisor, "Hacker's Delight"
70*795d594fSAndroid Build Coastguard Worker    * introduces a method to handle these 2 cases together to avoid duplication.
71*795d594fSAndroid Build Coastguard Worker    */
72*795d594fSAndroid Build Coastguard Worker   uint64_t delta;
73*795d594fSAndroid Build Coastguard Worker   do {
74*795d594fSAndroid Build Coastguard Worker     p++;
75*795d594fSAndroid Build Coastguard Worker     quotient1 = 2 * quotient1;
76*795d594fSAndroid Build Coastguard Worker     remainder1 = 2 * remainder1;
77*795d594fSAndroid Build Coastguard Worker     if (remainder1 >= abs_nc) {
78*795d594fSAndroid Build Coastguard Worker       quotient1++;
79*795d594fSAndroid Build Coastguard Worker       remainder1 = remainder1 - abs_nc;
80*795d594fSAndroid Build Coastguard Worker     }
81*795d594fSAndroid Build Coastguard Worker     quotient2 = 2 * quotient2;
82*795d594fSAndroid Build Coastguard Worker     remainder2 = 2 * remainder2;
83*795d594fSAndroid Build Coastguard Worker     if (remainder2 >= abs_d) {
84*795d594fSAndroid Build Coastguard Worker       quotient2++;
85*795d594fSAndroid Build Coastguard Worker       remainder2 = remainder2 - abs_d;
86*795d594fSAndroid Build Coastguard Worker     }
87*795d594fSAndroid Build Coastguard Worker     delta = abs_d - remainder2;
88*795d594fSAndroid Build Coastguard Worker   } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker   *magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker   if (!is_long) {
93*795d594fSAndroid Build Coastguard Worker     *magic = static_cast<int>(*magic);
94*795d594fSAndroid Build Coastguard Worker   }
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker   *shift = is_long ? p - 64 : p - 32;
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker 
IsBooleanValueOrMaterializedCondition(HInstruction * cond_input)99*795d594fSAndroid Build Coastguard Worker bool IsBooleanValueOrMaterializedCondition(HInstruction* cond_input) {
100*795d594fSAndroid Build Coastguard Worker   return !cond_input->IsCondition() || !cond_input->IsEmittedAtUseSite();
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker // A helper class to group functions analyzing if values are non-negative
104*795d594fSAndroid Build Coastguard Worker // at the point of use. The class keeps some context used by the functions.
105*795d594fSAndroid Build Coastguard Worker // The class is not supposed to be used directly or its instances to be kept.
106*795d594fSAndroid Build Coastguard Worker // The main function using it is HasNonNegativeInputAt.
107*795d594fSAndroid Build Coastguard Worker // If you want to use the class methods you need to become a friend of the class.
108*795d594fSAndroid Build Coastguard Worker class UnsignedUseAnalyzer {
109*795d594fSAndroid Build Coastguard Worker  private:
UnsignedUseAnalyzer(ArenaAllocator * allocator)110*795d594fSAndroid Build Coastguard Worker   explicit UnsignedUseAnalyzer(ArenaAllocator* allocator)
111*795d594fSAndroid Build Coastguard Worker       : seen_values_(allocator->Adapter(kArenaAllocCodeGenerator)) {
112*795d594fSAndroid Build Coastguard Worker   }
113*795d594fSAndroid Build Coastguard Worker 
114*795d594fSAndroid Build Coastguard Worker   bool IsNonNegativeUse(HInstruction* target_user, HInstruction* value);
115*795d594fSAndroid Build Coastguard Worker   bool IsComparedValueNonNegativeInBlock(HInstruction* value,
116*795d594fSAndroid Build Coastguard Worker                                          HCondition* cond,
117*795d594fSAndroid Build Coastguard Worker                                          HBasicBlock* target_block);
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker   ArenaSet<HInstruction*> seen_values_;
120*795d594fSAndroid Build Coastguard Worker 
121*795d594fSAndroid Build Coastguard Worker   friend bool HasNonNegativeInputAt(HInstruction* instr, size_t i);
122*795d594fSAndroid Build Coastguard Worker };
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker // Check that the value compared with a non-negavite value is
125*795d594fSAndroid Build Coastguard Worker // non-negative in the specified basic block.
IsComparedValueNonNegativeInBlock(HInstruction * value,HCondition * cond,HBasicBlock * target_block)126*795d594fSAndroid Build Coastguard Worker bool UnsignedUseAnalyzer::IsComparedValueNonNegativeInBlock(HInstruction* value,
127*795d594fSAndroid Build Coastguard Worker                                                             HCondition* cond,
128*795d594fSAndroid Build Coastguard Worker                                                             HBasicBlock* target_block) {
129*795d594fSAndroid Build Coastguard Worker   DCHECK(cond->HasInput(value));
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker   // To simplify analysis, we require:
132*795d594fSAndroid Build Coastguard Worker   // 1. The condition basic block and target_block to be different.
133*795d594fSAndroid Build Coastguard Worker   // 2. The condition basic block to end with HIf.
134*795d594fSAndroid Build Coastguard Worker   // 3. HIf to use the condition.
135*795d594fSAndroid Build Coastguard Worker   if (cond->GetBlock() == target_block ||
136*795d594fSAndroid Build Coastguard Worker       !cond->GetBlock()->EndsWithIf() ||
137*795d594fSAndroid Build Coastguard Worker       cond->GetBlock()->GetLastInstruction()->InputAt(0) != cond) {
138*795d594fSAndroid Build Coastguard Worker     return false;
139*795d594fSAndroid Build Coastguard Worker   }
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker   // We need to find a successor basic block of HIf for the case when instr is non-negative.
142*795d594fSAndroid Build Coastguard Worker   // If the successor dominates target_block, instructions in target_block see a non-negative value.
143*795d594fSAndroid Build Coastguard Worker   HIf* if_instr = cond->GetBlock()->GetLastInstruction()->AsIf();
144*795d594fSAndroid Build Coastguard Worker   HBasicBlock* successor = nullptr;
145*795d594fSAndroid Build Coastguard Worker   switch (cond->GetCondition()) {
146*795d594fSAndroid Build Coastguard Worker     case kCondGT:
147*795d594fSAndroid Build Coastguard Worker     case kCondGE: {
148*795d594fSAndroid Build Coastguard Worker       if (cond->GetLeft() == value) {
149*795d594fSAndroid Build Coastguard Worker         // The expression is v > A or v >= A.
150*795d594fSAndroid Build Coastguard Worker         // If A is non-negative, we need the true successor.
151*795d594fSAndroid Build Coastguard Worker         if (IsNonNegativeUse(cond, cond->GetRight())) {
152*795d594fSAndroid Build Coastguard Worker           successor = if_instr->IfTrueSuccessor();
153*795d594fSAndroid Build Coastguard Worker         } else {
154*795d594fSAndroid Build Coastguard Worker           return false;
155*795d594fSAndroid Build Coastguard Worker         }
156*795d594fSAndroid Build Coastguard Worker       } else {
157*795d594fSAndroid Build Coastguard Worker         DCHECK_EQ(cond->GetRight(), value);
158*795d594fSAndroid Build Coastguard Worker         // The expression is A > v or A >= v.
159*795d594fSAndroid Build Coastguard Worker         // If A is non-negative, we need the false successor.
160*795d594fSAndroid Build Coastguard Worker         if (IsNonNegativeUse(cond, cond->GetLeft())) {
161*795d594fSAndroid Build Coastguard Worker           successor = if_instr->IfFalseSuccessor();
162*795d594fSAndroid Build Coastguard Worker         } else {
163*795d594fSAndroid Build Coastguard Worker           return false;
164*795d594fSAndroid Build Coastguard Worker         }
165*795d594fSAndroid Build Coastguard Worker       }
166*795d594fSAndroid Build Coastguard Worker       break;
167*795d594fSAndroid Build Coastguard Worker     }
168*795d594fSAndroid Build Coastguard Worker 
169*795d594fSAndroid Build Coastguard Worker     case kCondLT:
170*795d594fSAndroid Build Coastguard Worker     case kCondLE: {
171*795d594fSAndroid Build Coastguard Worker       if (cond->GetLeft() == value) {
172*795d594fSAndroid Build Coastguard Worker         // The expression is v < A or v <= A.
173*795d594fSAndroid Build Coastguard Worker         // If A is non-negative, we need the false successor.
174*795d594fSAndroid Build Coastguard Worker         if (IsNonNegativeUse(cond, cond->GetRight())) {
175*795d594fSAndroid Build Coastguard Worker           successor = if_instr->IfFalseSuccessor();
176*795d594fSAndroid Build Coastguard Worker         } else {
177*795d594fSAndroid Build Coastguard Worker           return false;
178*795d594fSAndroid Build Coastguard Worker         }
179*795d594fSAndroid Build Coastguard Worker       } else {
180*795d594fSAndroid Build Coastguard Worker         DCHECK_EQ(cond->GetRight(), value);
181*795d594fSAndroid Build Coastguard Worker         // The expression is A < v or A <= v.
182*795d594fSAndroid Build Coastguard Worker         // If A is non-negative, we need the true successor.
183*795d594fSAndroid Build Coastguard Worker         if (IsNonNegativeUse(cond, cond->GetLeft())) {
184*795d594fSAndroid Build Coastguard Worker           successor = if_instr->IfTrueSuccessor();
185*795d594fSAndroid Build Coastguard Worker         } else {
186*795d594fSAndroid Build Coastguard Worker           return false;
187*795d594fSAndroid Build Coastguard Worker         }
188*795d594fSAndroid Build Coastguard Worker       }
189*795d594fSAndroid Build Coastguard Worker       break;
190*795d594fSAndroid Build Coastguard Worker     }
191*795d594fSAndroid Build Coastguard Worker 
192*795d594fSAndroid Build Coastguard Worker     default:
193*795d594fSAndroid Build Coastguard Worker       return false;
194*795d594fSAndroid Build Coastguard Worker   }
195*795d594fSAndroid Build Coastguard Worker   DCHECK_NE(successor, nullptr);
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker   return successor->Dominates(target_block);
198*795d594fSAndroid Build Coastguard Worker }
199*795d594fSAndroid Build Coastguard Worker 
200*795d594fSAndroid Build Coastguard Worker // Check the value used by target_user is non-negative.
IsNonNegativeUse(HInstruction * target_user,HInstruction * value)201*795d594fSAndroid Build Coastguard Worker bool UnsignedUseAnalyzer::IsNonNegativeUse(HInstruction* target_user, HInstruction* value) {
202*795d594fSAndroid Build Coastguard Worker   DCHECK(target_user->HasInput(value));
203*795d594fSAndroid Build Coastguard Worker 
204*795d594fSAndroid Build Coastguard Worker   // Prevent infinitive recursion which can happen when the value is an induction variable.
205*795d594fSAndroid Build Coastguard Worker   if (!seen_values_.insert(value).second) {
206*795d594fSAndroid Build Coastguard Worker     return false;
207*795d594fSAndroid Build Coastguard Worker   }
208*795d594fSAndroid Build Coastguard Worker 
209*795d594fSAndroid Build Coastguard Worker   // Check if the value is always non-negative.
210*795d594fSAndroid Build Coastguard Worker   if (IsGEZero(value)) {
211*795d594fSAndroid Build Coastguard Worker     return true;
212*795d594fSAndroid Build Coastguard Worker   }
213*795d594fSAndroid Build Coastguard Worker 
214*795d594fSAndroid Build Coastguard Worker   for (const HUseListNode<HInstruction*>& use : value->GetUses()) {
215*795d594fSAndroid Build Coastguard Worker     HInstruction* user = use.GetUser();
216*795d594fSAndroid Build Coastguard Worker     if (user == target_user) {
217*795d594fSAndroid Build Coastguard Worker       continue;
218*795d594fSAndroid Build Coastguard Worker     }
219*795d594fSAndroid Build Coastguard Worker 
220*795d594fSAndroid Build Coastguard Worker     // If the value is compared with some non-negative value, this can guarantee the value to be
221*795d594fSAndroid Build Coastguard Worker     // non-negative at its use.
222*795d594fSAndroid Build Coastguard Worker     // JFYI: We're not using HTypeConversion to bind the new information because it would
223*795d594fSAndroid Build Coastguard Worker     // increase the complexity of optimizations: HTypeConversion can create a dependency
224*795d594fSAndroid Build Coastguard Worker     // which does not exist in the input program, for example:
225*795d594fSAndroid Build Coastguard Worker     // between two uses, 1st - cmp, 2nd - target_user.
226*795d594fSAndroid Build Coastguard Worker     if (user->IsCondition()) {
227*795d594fSAndroid Build Coastguard Worker       // The condition must dominate target_user to guarantee that the value is always checked
228*795d594fSAndroid Build Coastguard Worker       // before it is used by target_user.
229*795d594fSAndroid Build Coastguard Worker       if (user->GetBlock()->Dominates(target_user->GetBlock()) &&
230*795d594fSAndroid Build Coastguard Worker           IsComparedValueNonNegativeInBlock(value, user->AsCondition(), target_user->GetBlock())) {
231*795d594fSAndroid Build Coastguard Worker         return true;
232*795d594fSAndroid Build Coastguard Worker       }
233*795d594fSAndroid Build Coastguard Worker     }
234*795d594fSAndroid Build Coastguard Worker 
235*795d594fSAndroid Build Coastguard Worker     // TODO The value is non-negative if it is used as an array index before.
236*795d594fSAndroid Build Coastguard Worker     // TODO The value is non-negative if it is initialized by a positive number and all of its
237*795d594fSAndroid Build Coastguard Worker     //      modifications keep the value non-negative, for example the division operation.
238*795d594fSAndroid Build Coastguard Worker   }
239*795d594fSAndroid Build Coastguard Worker 
240*795d594fSAndroid Build Coastguard Worker   return false;
241*795d594fSAndroid Build Coastguard Worker }
242*795d594fSAndroid Build Coastguard Worker 
HasNonNegativeInputAt(HInstruction * instr,size_t i)243*795d594fSAndroid Build Coastguard Worker bool HasNonNegativeInputAt(HInstruction* instr, size_t i) {
244*795d594fSAndroid Build Coastguard Worker   UnsignedUseAnalyzer analyzer(instr->GetBlock()->GetGraph()->GetAllocator());
245*795d594fSAndroid Build Coastguard Worker   return analyzer.IsNonNegativeUse(instr, instr->InputAt(i));
246*795d594fSAndroid Build Coastguard Worker }
247*795d594fSAndroid Build Coastguard Worker 
HasNonNegativeOrMinIntInputAt(HInstruction * instr,size_t i)248*795d594fSAndroid Build Coastguard Worker bool HasNonNegativeOrMinIntInputAt(HInstruction* instr, size_t i) {
249*795d594fSAndroid Build Coastguard Worker   HInstruction* input = instr->InputAt(i);
250*795d594fSAndroid Build Coastguard Worker   return input->IsAbs() ||
251*795d594fSAndroid Build Coastguard Worker          IsInt64Value(input, DataType::MinValueOfIntegralType(input->GetType())) ||
252*795d594fSAndroid Build Coastguard Worker          HasNonNegativeInputAt(instr, i);
253*795d594fSAndroid Build Coastguard Worker }
254*795d594fSAndroid Build Coastguard Worker 
255*795d594fSAndroid Build Coastguard Worker }  // namespace art
256