xref: /aosp_15_r20/art/compiler/optimizing/load_store_analysis.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 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 "load_store_analysis.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "base/scoped_arena_allocator.h"
20*795d594fSAndroid Build Coastguard Worker #include "optimizing/escape.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker // A cap for the number of heap locations to prevent pathological time/space consumption.
25*795d594fSAndroid Build Coastguard Worker // The number of heap locations for most of the methods stays below this threshold.
26*795d594fSAndroid Build Coastguard Worker constexpr size_t kMaxNumberOfHeapLocations = 32;
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker // Test if two integer ranges [l1,h1] and [l2,h2] overlap.
29*795d594fSAndroid Build Coastguard Worker // Note that the ranges are inclusive on both ends.
30*795d594fSAndroid Build Coastguard Worker //       l1|------|h1
31*795d594fSAndroid Build Coastguard Worker //  l2|------|h2
CanIntegerRangesOverlap(int64_t l1,int64_t h1,int64_t l2,int64_t h2)32*795d594fSAndroid Build Coastguard Worker static bool CanIntegerRangesOverlap(int64_t l1, int64_t h1, int64_t l2, int64_t h2) {
33*795d594fSAndroid Build Coastguard Worker   return std::max(l1, l2) <= std::min(h1, h2);
34*795d594fSAndroid Build Coastguard Worker }
35*795d594fSAndroid Build Coastguard Worker 
CanBinaryOpAndIndexAlias(const HBinaryOperation * idx1,const size_t vector_length1,const HInstruction * idx2,const size_t vector_length2)36*795d594fSAndroid Build Coastguard Worker static bool CanBinaryOpAndIndexAlias(const HBinaryOperation* idx1,
37*795d594fSAndroid Build Coastguard Worker                                      const size_t vector_length1,
38*795d594fSAndroid Build Coastguard Worker                                      const HInstruction* idx2,
39*795d594fSAndroid Build Coastguard Worker                                      const size_t vector_length2) {
40*795d594fSAndroid Build Coastguard Worker   if (!IsAddOrSub(idx1)) {
41*795d594fSAndroid Build Coastguard Worker     // We currently only support Add and Sub operations.
42*795d594fSAndroid Build Coastguard Worker     return true;
43*795d594fSAndroid Build Coastguard Worker   }
44*795d594fSAndroid Build Coastguard Worker   if (idx1->GetLeastConstantLeft() != idx2) {
45*795d594fSAndroid Build Coastguard Worker     // Cannot analyze [i+CONST1] and [j].
46*795d594fSAndroid Build Coastguard Worker     return true;
47*795d594fSAndroid Build Coastguard Worker   }
48*795d594fSAndroid Build Coastguard Worker   if (!idx1->GetConstantRight()->IsIntConstant()) {
49*795d594fSAndroid Build Coastguard Worker     return true;
50*795d594fSAndroid Build Coastguard Worker   }
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   // Since 'i' are the same in [i+CONST] and [i],
53*795d594fSAndroid Build Coastguard Worker   // further compare [CONST] and [0].
54*795d594fSAndroid Build Coastguard Worker   int64_t l1 = idx1->IsAdd()
55*795d594fSAndroid Build Coastguard Worker       ? idx1->GetConstantRight()->AsIntConstant()->GetValue()
56*795d594fSAndroid Build Coastguard Worker       : -idx1->GetConstantRight()->AsIntConstant()->GetValue();
57*795d594fSAndroid Build Coastguard Worker   int64_t l2 = 0;
58*795d594fSAndroid Build Coastguard Worker   int64_t h1 = l1 + (vector_length1 - 1);
59*795d594fSAndroid Build Coastguard Worker   int64_t h2 = l2 + (vector_length2 - 1);
60*795d594fSAndroid Build Coastguard Worker   return CanIntegerRangesOverlap(l1, h1, l2, h2);
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker 
CanBinaryOpsAlias(const HBinaryOperation * idx1,const size_t vector_length1,const HBinaryOperation * idx2,const size_t vector_length2)63*795d594fSAndroid Build Coastguard Worker static bool CanBinaryOpsAlias(const HBinaryOperation* idx1,
64*795d594fSAndroid Build Coastguard Worker                               const size_t vector_length1,
65*795d594fSAndroid Build Coastguard Worker                               const HBinaryOperation* idx2,
66*795d594fSAndroid Build Coastguard Worker                               const size_t vector_length2) {
67*795d594fSAndroid Build Coastguard Worker   if (!IsAddOrSub(idx1) || !IsAddOrSub(idx2)) {
68*795d594fSAndroid Build Coastguard Worker     // We currently only support Add and Sub operations.
69*795d594fSAndroid Build Coastguard Worker     return true;
70*795d594fSAndroid Build Coastguard Worker   }
71*795d594fSAndroid Build Coastguard Worker   if (idx1->GetLeastConstantLeft() != idx2->GetLeastConstantLeft()) {
72*795d594fSAndroid Build Coastguard Worker     // Cannot analyze [i+CONST1] and [j+CONST2].
73*795d594fSAndroid Build Coastguard Worker     return true;
74*795d594fSAndroid Build Coastguard Worker   }
75*795d594fSAndroid Build Coastguard Worker   if (!idx1->GetConstantRight()->IsIntConstant() ||
76*795d594fSAndroid Build Coastguard Worker       !idx2->GetConstantRight()->IsIntConstant()) {
77*795d594fSAndroid Build Coastguard Worker     return true;
78*795d594fSAndroid Build Coastguard Worker   }
79*795d594fSAndroid Build Coastguard Worker 
80*795d594fSAndroid Build Coastguard Worker   // Since 'i' are the same in [i+CONST1] and [i+CONST2],
81*795d594fSAndroid Build Coastguard Worker   // further compare [CONST1] and [CONST2].
82*795d594fSAndroid Build Coastguard Worker   int64_t l1 = idx1->IsAdd()
83*795d594fSAndroid Build Coastguard Worker       ? idx1->GetConstantRight()->AsIntConstant()->GetValue()
84*795d594fSAndroid Build Coastguard Worker       : -idx1->GetConstantRight()->AsIntConstant()->GetValue();
85*795d594fSAndroid Build Coastguard Worker   int64_t l2 = idx2->IsAdd()
86*795d594fSAndroid Build Coastguard Worker       ? idx2->GetConstantRight()->AsIntConstant()->GetValue()
87*795d594fSAndroid Build Coastguard Worker       : -idx2->GetConstantRight()->AsIntConstant()->GetValue();
88*795d594fSAndroid Build Coastguard Worker   int64_t h1 = l1 + (vector_length1 - 1);
89*795d594fSAndroid Build Coastguard Worker   int64_t h2 = l2 + (vector_length2 - 1);
90*795d594fSAndroid Build Coastguard Worker   return CanIntegerRangesOverlap(l1, h1, l2, h2);
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker 
InstructionEligibleForLSERemoval(HInstruction * inst) const93*795d594fSAndroid Build Coastguard Worker bool HeapLocationCollector::InstructionEligibleForLSERemoval(HInstruction* inst) const {
94*795d594fSAndroid Build Coastguard Worker   if (inst->IsNewInstance()) {
95*795d594fSAndroid Build Coastguard Worker     return !inst->AsNewInstance()->NeedsChecks();
96*795d594fSAndroid Build Coastguard Worker   } else if (inst->IsNewArray()) {
97*795d594fSAndroid Build Coastguard Worker     HInstruction* array_length = inst->AsNewArray()->GetLength();
98*795d594fSAndroid Build Coastguard Worker     bool known_array_length =
99*795d594fSAndroid Build Coastguard Worker         array_length->IsIntConstant() && array_length->AsIntConstant()->GetValue() >= 0;
100*795d594fSAndroid Build Coastguard Worker     return known_array_length &&
101*795d594fSAndroid Build Coastguard Worker            std::all_of(inst->GetUses().cbegin(),
102*795d594fSAndroid Build Coastguard Worker                        inst->GetUses().cend(),
103*795d594fSAndroid Build Coastguard Worker                        [&](const HUseListNode<HInstruction*>& user) {
104*795d594fSAndroid Build Coastguard Worker                          if (user.GetUser()->IsArrayGet() || user.GetUser()->IsArraySet()) {
105*795d594fSAndroid Build Coastguard Worker                            return user.GetUser()->InputAt(1)->IsIntConstant();
106*795d594fSAndroid Build Coastguard Worker                          }
107*795d594fSAndroid Build Coastguard Worker                          return true;
108*795d594fSAndroid Build Coastguard Worker                        });
109*795d594fSAndroid Build Coastguard Worker   } else {
110*795d594fSAndroid Build Coastguard Worker     return false;
111*795d594fSAndroid Build Coastguard Worker   }
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker 
DumpReferenceStats(OptimizingCompilerStats * stats)114*795d594fSAndroid Build Coastguard Worker void HeapLocationCollector::DumpReferenceStats(OptimizingCompilerStats* stats) {
115*795d594fSAndroid Build Coastguard Worker   if (stats == nullptr) {
116*795d594fSAndroid Build Coastguard Worker     return;
117*795d594fSAndroid Build Coastguard Worker   }
118*795d594fSAndroid Build Coastguard Worker   std::vector<bool> seen_instructions(GetGraph()->GetCurrentInstructionId(), false);
119*795d594fSAndroid Build Coastguard Worker   for (auto hl : heap_locations_) {
120*795d594fSAndroid Build Coastguard Worker     auto ri = hl->GetReferenceInfo();
121*795d594fSAndroid Build Coastguard Worker     if (ri == nullptr || seen_instructions[ri->GetReference()->GetId()]) {
122*795d594fSAndroid Build Coastguard Worker       continue;
123*795d594fSAndroid Build Coastguard Worker     }
124*795d594fSAndroid Build Coastguard Worker     auto instruction = ri->GetReference();
125*795d594fSAndroid Build Coastguard Worker     seen_instructions[instruction->GetId()] = true;
126*795d594fSAndroid Build Coastguard Worker     if (ri->IsSingletonAndRemovable()) {
127*795d594fSAndroid Build Coastguard Worker       if (InstructionEligibleForLSERemoval(instruction)) {
128*795d594fSAndroid Build Coastguard Worker         MaybeRecordStat(stats, MethodCompilationStat::kFullLSEPossible);
129*795d594fSAndroid Build Coastguard Worker       }
130*795d594fSAndroid Build Coastguard Worker     }
131*795d594fSAndroid Build Coastguard Worker   }
132*795d594fSAndroid Build Coastguard Worker }
133*795d594fSAndroid Build Coastguard Worker 
CanArrayElementsAlias(const HInstruction * idx1,const size_t vector_length1,const HInstruction * idx2,const size_t vector_length2) const134*795d594fSAndroid Build Coastguard Worker bool HeapLocationCollector::CanArrayElementsAlias(const HInstruction* idx1,
135*795d594fSAndroid Build Coastguard Worker                                                   const size_t vector_length1,
136*795d594fSAndroid Build Coastguard Worker                                                   const HInstruction* idx2,
137*795d594fSAndroid Build Coastguard Worker                                                   const size_t vector_length2) const {
138*795d594fSAndroid Build Coastguard Worker   DCHECK(idx1 != nullptr);
139*795d594fSAndroid Build Coastguard Worker   DCHECK(idx2 != nullptr);
140*795d594fSAndroid Build Coastguard Worker   DCHECK_GE(vector_length1, HeapLocation::kScalar);
141*795d594fSAndroid Build Coastguard Worker   DCHECK_GE(vector_length2, HeapLocation::kScalar);
142*795d594fSAndroid Build Coastguard Worker 
143*795d594fSAndroid Build Coastguard Worker   // [i] and [i].
144*795d594fSAndroid Build Coastguard Worker   if (idx1 == idx2) {
145*795d594fSAndroid Build Coastguard Worker     return true;
146*795d594fSAndroid Build Coastguard Worker   }
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker   // [CONST1] and [CONST2].
149*795d594fSAndroid Build Coastguard Worker   if (idx1->IsIntConstant() && idx2->IsIntConstant()) {
150*795d594fSAndroid Build Coastguard Worker     int64_t l1 = idx1->AsIntConstant()->GetValue();
151*795d594fSAndroid Build Coastguard Worker     int64_t l2 = idx2->AsIntConstant()->GetValue();
152*795d594fSAndroid Build Coastguard Worker     // To avoid any overflow in following CONST+vector_length calculation,
153*795d594fSAndroid Build Coastguard Worker     // use int64_t instead of int32_t.
154*795d594fSAndroid Build Coastguard Worker     int64_t h1 = l1 + (vector_length1 - 1);
155*795d594fSAndroid Build Coastguard Worker     int64_t h2 = l2 + (vector_length2 - 1);
156*795d594fSAndroid Build Coastguard Worker     return CanIntegerRangesOverlap(l1, h1, l2, h2);
157*795d594fSAndroid Build Coastguard Worker   }
158*795d594fSAndroid Build Coastguard Worker 
159*795d594fSAndroid Build Coastguard Worker   // [i+CONST] and [i].
160*795d594fSAndroid Build Coastguard Worker   if (idx1->IsBinaryOperation() &&
161*795d594fSAndroid Build Coastguard Worker       idx1->AsBinaryOperation()->GetConstantRight() != nullptr &&
162*795d594fSAndroid Build Coastguard Worker       idx1->AsBinaryOperation()->GetLeastConstantLeft() == idx2) {
163*795d594fSAndroid Build Coastguard Worker     return CanBinaryOpAndIndexAlias(idx1->AsBinaryOperation(),
164*795d594fSAndroid Build Coastguard Worker                                     vector_length1,
165*795d594fSAndroid Build Coastguard Worker                                     idx2,
166*795d594fSAndroid Build Coastguard Worker                                     vector_length2);
167*795d594fSAndroid Build Coastguard Worker   }
168*795d594fSAndroid Build Coastguard Worker 
169*795d594fSAndroid Build Coastguard Worker   // [i] and [i+CONST].
170*795d594fSAndroid Build Coastguard Worker   if (idx2->IsBinaryOperation() &&
171*795d594fSAndroid Build Coastguard Worker       idx2->AsBinaryOperation()->GetConstantRight() != nullptr &&
172*795d594fSAndroid Build Coastguard Worker       idx2->AsBinaryOperation()->GetLeastConstantLeft() == idx1) {
173*795d594fSAndroid Build Coastguard Worker     return CanBinaryOpAndIndexAlias(idx2->AsBinaryOperation(),
174*795d594fSAndroid Build Coastguard Worker                                     vector_length2,
175*795d594fSAndroid Build Coastguard Worker                                     idx1,
176*795d594fSAndroid Build Coastguard Worker                                     vector_length1);
177*795d594fSAndroid Build Coastguard Worker   }
178*795d594fSAndroid Build Coastguard Worker 
179*795d594fSAndroid Build Coastguard Worker   // [i+CONST1] and [i+CONST2].
180*795d594fSAndroid Build Coastguard Worker   if (idx1->IsBinaryOperation() &&
181*795d594fSAndroid Build Coastguard Worker       idx1->AsBinaryOperation()->GetConstantRight() != nullptr &&
182*795d594fSAndroid Build Coastguard Worker       idx2->IsBinaryOperation() &&
183*795d594fSAndroid Build Coastguard Worker       idx2->AsBinaryOperation()->GetConstantRight() != nullptr) {
184*795d594fSAndroid Build Coastguard Worker     return CanBinaryOpsAlias(idx1->AsBinaryOperation(),
185*795d594fSAndroid Build Coastguard Worker                              vector_length1,
186*795d594fSAndroid Build Coastguard Worker                              idx2->AsBinaryOperation(),
187*795d594fSAndroid Build Coastguard Worker                              vector_length2);
188*795d594fSAndroid Build Coastguard Worker   }
189*795d594fSAndroid Build Coastguard Worker 
190*795d594fSAndroid Build Coastguard Worker   // By default, MAY alias.
191*795d594fSAndroid Build Coastguard Worker   return true;
192*795d594fSAndroid Build Coastguard Worker }
193*795d594fSAndroid Build Coastguard Worker 
Run()194*795d594fSAndroid Build Coastguard Worker bool LoadStoreAnalysis::Run() {
195*795d594fSAndroid Build Coastguard Worker   // Currently load_store analysis can't handle predicated load/stores; specifically pairs of
196*795d594fSAndroid Build Coastguard Worker   // memory operations with different predicates.
197*795d594fSAndroid Build Coastguard Worker   // TODO: support predicated SIMD.
198*795d594fSAndroid Build Coastguard Worker   if (graph_->HasPredicatedSIMD()) {
199*795d594fSAndroid Build Coastguard Worker     return false;
200*795d594fSAndroid Build Coastguard Worker   }
201*795d594fSAndroid Build Coastguard Worker 
202*795d594fSAndroid Build Coastguard Worker   for (HBasicBlock* block : graph_->GetReversePostOrder()) {
203*795d594fSAndroid Build Coastguard Worker     heap_location_collector_.VisitBasicBlock(block);
204*795d594fSAndroid Build Coastguard Worker   }
205*795d594fSAndroid Build Coastguard Worker 
206*795d594fSAndroid Build Coastguard Worker   if (heap_location_collector_.GetNumberOfHeapLocations() > kMaxNumberOfHeapLocations) {
207*795d594fSAndroid Build Coastguard Worker     // Bail out if there are too many heap locations to deal with.
208*795d594fSAndroid Build Coastguard Worker     heap_location_collector_.CleanUp();
209*795d594fSAndroid Build Coastguard Worker     return false;
210*795d594fSAndroid Build Coastguard Worker   }
211*795d594fSAndroid Build Coastguard Worker   if (!heap_location_collector_.HasHeapStores()) {
212*795d594fSAndroid Build Coastguard Worker     // Without heap stores, this pass would act mostly as GVN on heap accesses.
213*795d594fSAndroid Build Coastguard Worker     heap_location_collector_.CleanUp();
214*795d594fSAndroid Build Coastguard Worker     return false;
215*795d594fSAndroid Build Coastguard Worker   }
216*795d594fSAndroid Build Coastguard Worker   heap_location_collector_.BuildAliasingMatrix();
217*795d594fSAndroid Build Coastguard Worker   heap_location_collector_.DumpReferenceStats(stats_);
218*795d594fSAndroid Build Coastguard Worker   return true;
219*795d594fSAndroid Build Coastguard Worker }
220*795d594fSAndroid Build Coastguard Worker 
221*795d594fSAndroid Build Coastguard Worker }  // namespace art
222