Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "loop_optimization.h" |
| 18 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 19 | #include "linear_order.h" |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | // TODO: Generalize to cycles, as found by induction analysis? |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 24 | static bool IsPhiInduction(HPhi* phi, ArenaSet<HInstruction*>* iset) { |
| 25 | DCHECK(iset->empty()); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 26 | HInputsRef inputs = phi->GetInputs(); |
| 27 | if (inputs.size() == 2 && (inputs[1]->IsAdd() || inputs[1]->IsSub())) { |
| 28 | HInstruction* addsub = inputs[1]; |
| 29 | if (addsub->InputAt(0) == phi || addsub->InputAt(1) == phi) { |
| 30 | if (addsub->GetUses().HasExactlyOneElement()) { |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 31 | iset->insert(phi); |
| 32 | iset->insert(addsub); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 33 | return true; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 40 | // Find: phi: Phi(init, addsub) |
| 41 | // s: SuspendCheck |
| 42 | // c: Condition(phi, bound) |
| 43 | // i: If(c) |
| 44 | // TODO: Find a less pattern matching approach? |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 45 | static bool IsEmptyHeader(HBasicBlock* block, ArenaSet<HInstruction*>* iset) { |
| 46 | DCHECK(iset->empty()); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 47 | HInstruction* phi = block->GetFirstPhi(); |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 48 | if (phi != nullptr && phi->GetNext() == nullptr && IsPhiInduction(phi->AsPhi(), iset)) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 49 | HInstruction* s = block->GetFirstInstruction(); |
| 50 | if (s != nullptr && s->IsSuspendCheck()) { |
| 51 | HInstruction* c = s->GetNext(); |
| 52 | if (c != nullptr && c->IsCondition() && c->GetUses().HasExactlyOneElement()) { |
| 53 | HInstruction* i = c->GetNext(); |
| 54 | if (i != nullptr && i->IsIf() && i->InputAt(0) == c) { |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 55 | iset->insert(c); |
| 56 | iset->insert(s); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 65 | static bool IsEmptyBody(HBasicBlock* block, ArenaSet<HInstruction*>* iset) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 66 | HInstruction* phi = block->GetFirstPhi(); |
| 67 | HInstruction* i = block->GetFirstInstruction(); |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 68 | return phi == nullptr && iset->find(i) != iset->end() && |
| 69 | i->GetNext() != nullptr && i->GetNext()->IsGoto(); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 72 | static void RemoveFromCycle(HInstruction* instruction) { |
| 73 | // A bit more elaborate than the usual instruction removal, |
| 74 | // since there may be a cycle in the use structure. |
| 75 | instruction->RemoveAsUserOfAllInputs(); |
| 76 | instruction->RemoveEnvironmentUsers(); |
| 77 | instruction->GetBlock()->RemoveInstructionOrPhi(instruction, /*ensure_safety=*/ false); |
| 78 | } |
| 79 | |
| 80 | // |
| 81 | // Class methods. |
| 82 | // |
| 83 | |
| 84 | HLoopOptimization::HLoopOptimization(HGraph* graph, |
| 85 | HInductionVarAnalysis* induction_analysis) |
| 86 | : HOptimization(graph, kLoopOptimizationPassName), |
| 87 | induction_range_(induction_analysis), |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 88 | loop_allocator_(nullptr), |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 89 | top_loop_(nullptr), |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 90 | last_loop_(nullptr), |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 91 | iset_(nullptr), |
| 92 | induction_simplication_count_(0) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void HLoopOptimization::Run() { |
| 96 | // Well-behaved loops only. |
| 97 | // TODO: make this less of a sledgehammer. |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 98 | if (graph_->HasTryCatch() || graph_->HasIrreducibleLoops()) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 102 | // Phase-local allocator that draws from the global pool. Since the allocator |
| 103 | // itself resides on the stack, it is destructed on exiting Run(), which |
| 104 | // implies its underlying memory is released immediately. |
Nicolas Geoffray | ebe1674 | 2016-10-05 09:55:42 +0100 | [diff] [blame] | 105 | ArenaAllocator allocator(graph_->GetArena()->GetArenaPool()); |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 106 | loop_allocator_ = &allocator; |
Nicolas Geoffray | ebe1674 | 2016-10-05 09:55:42 +0100 | [diff] [blame] | 107 | |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 108 | // Perform loop optimizations. |
| 109 | LocalRun(); |
| 110 | |
| 111 | // Detach. |
| 112 | loop_allocator_ = nullptr; |
| 113 | last_loop_ = top_loop_ = nullptr; |
| 114 | } |
| 115 | |
| 116 | void HLoopOptimization::LocalRun() { |
| 117 | // Build the linear order using the phase-local allocator. This step enables building |
| 118 | // a loop hierarchy that properly reflects the outer-inner and previous-next relation. |
| 119 | ArenaVector<HBasicBlock*> linear_order(loop_allocator_->Adapter(kArenaAllocLinearOrder)); |
| 120 | LinearizeGraph(graph_, loop_allocator_, &linear_order); |
| 121 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 122 | // Build the loop hierarchy. |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 123 | for (HBasicBlock* block : linear_order) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 124 | if (block->IsLoopHeader()) { |
| 125 | AddLoop(block->GetLoopInformation()); |
| 126 | } |
| 127 | } |
Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 128 | |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 129 | // Traverse the loop hierarchy inner-to-outer and optimize. Traversal can use |
| 130 | // a temporary set that stores instructions using the phase-local allocator. |
| 131 | if (top_loop_ != nullptr) { |
| 132 | ArenaSet<HInstruction*> iset(loop_allocator_->Adapter(kArenaAllocLoopOptimization)); |
| 133 | iset_ = &iset; |
| 134 | TraverseLoopsInnerToOuter(top_loop_); |
| 135 | iset_ = nullptr; // detach |
| 136 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void HLoopOptimization::AddLoop(HLoopInformation* loop_info) { |
| 140 | DCHECK(loop_info != nullptr); |
Nicolas Geoffray | ebe1674 | 2016-10-05 09:55:42 +0100 | [diff] [blame] | 141 | LoopNode* node = new (loop_allocator_) LoopNode(loop_info); // phase-local allocator |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 142 | if (last_loop_ == nullptr) { |
| 143 | // First loop. |
| 144 | DCHECK(top_loop_ == nullptr); |
| 145 | last_loop_ = top_loop_ = node; |
| 146 | } else if (loop_info->IsIn(*last_loop_->loop_info)) { |
| 147 | // Inner loop. |
| 148 | node->outer = last_loop_; |
| 149 | DCHECK(last_loop_->inner == nullptr); |
| 150 | last_loop_ = last_loop_->inner = node; |
| 151 | } else { |
| 152 | // Subsequent loop. |
| 153 | while (last_loop_->outer != nullptr && !loop_info->IsIn(*last_loop_->outer->loop_info)) { |
| 154 | last_loop_ = last_loop_->outer; |
| 155 | } |
| 156 | node->outer = last_loop_->outer; |
| 157 | node->previous = last_loop_; |
| 158 | DCHECK(last_loop_->next == nullptr); |
| 159 | last_loop_ = last_loop_->next = node; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void HLoopOptimization::RemoveLoop(LoopNode* node) { |
| 164 | DCHECK(node != nullptr); |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 165 | DCHECK(node->inner == nullptr); |
| 166 | if (node->previous != nullptr) { |
| 167 | // Within sequence. |
| 168 | node->previous->next = node->next; |
| 169 | if (node->next != nullptr) { |
| 170 | node->next->previous = node->previous; |
| 171 | } |
| 172 | } else { |
| 173 | // First of sequence. |
| 174 | if (node->outer != nullptr) { |
| 175 | node->outer->inner = node->next; |
| 176 | } else { |
| 177 | top_loop_ = node->next; |
| 178 | } |
| 179 | if (node->next != nullptr) { |
| 180 | node->next->outer = node->outer; |
| 181 | node->next->previous = nullptr; |
| 182 | } |
| 183 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void HLoopOptimization::TraverseLoopsInnerToOuter(LoopNode* node) { |
| 187 | for ( ; node != nullptr; node = node->next) { |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 188 | int current_induction_simplification_count = induction_simplication_count_; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 189 | if (node->inner != nullptr) { |
| 190 | TraverseLoopsInnerToOuter(node->inner); |
| 191 | } |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 192 | // Visit loop after its inner loops have been visited. If the induction of any inner |
| 193 | // loop has been simplified, recompute the induction information of this loop first. |
| 194 | if (current_induction_simplification_count != induction_simplication_count_) { |
| 195 | induction_range_.ReVisit(node->loop_info); |
| 196 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 197 | SimplifyInduction(node); |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 198 | SimplifyBlocks(node); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 199 | RemoveIfEmptyLoop(node); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void HLoopOptimization::SimplifyInduction(LoopNode* node) { |
| 204 | HBasicBlock* header = node->loop_info->GetHeader(); |
| 205 | HBasicBlock* preheader = node->loop_info->GetPreHeader(); |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 206 | // Scan the phis in the header to find opportunities to simplify an induction |
| 207 | // cycle that is only used outside the loop. Replace these uses, if any, with |
| 208 | // the last value and remove the induction cycle. |
| 209 | // Examples: for (int i = 0; x != null; i++) { .... no i .... } |
| 210 | // for (int i = 0; i < 10; i++, k++) { .... no k .... } return k; |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 211 | for (HInstructionIterator it(header->GetPhis()); !it.Done(); it.Advance()) { |
| 212 | HPhi* phi = it.Current()->AsPhi(); |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 213 | iset_->clear(); |
| 214 | int32_t use_count = 0; |
| 215 | if (IsPhiInduction(phi, iset_) && |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 216 | IsOnlyUsedAfterLoop(node->loop_info, phi, &use_count) && |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 217 | TryReplaceWithLastValue(phi, use_count, preheader)) { |
| 218 | for (HInstruction* i : *iset_) { |
| 219 | RemoveFromCycle(i); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 220 | } |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 221 | induction_simplication_count_++; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void HLoopOptimization::SimplifyBlocks(LoopNode* node) { |
| 227 | for (HBlocksInLoopIterator it(*node->loop_info); !it.Done(); it.Advance()) { |
| 228 | HBasicBlock* block = it.Current(); |
| 229 | // Remove instructions that are dead, usually resulting from eliminating induction cycles. |
| 230 | for (HBackwardInstructionIterator i(block->GetInstructions()); !i.Done(); i.Advance()) { |
| 231 | HInstruction* instruction = i.Current(); |
| 232 | if (instruction->IsDeadAndRemovable()) { |
| 233 | block->RemoveInstruction(instruction); |
| 234 | } |
| 235 | } |
| 236 | // Remove trivial control flow blocks from the loop body, again usually resulting |
| 237 | // from eliminating induction cycles. |
| 238 | if (block->GetPredecessors().size() == 1 && |
| 239 | block->GetSuccessors().size() == 1 && |
| 240 | block->GetFirstInstruction()->IsGoto()) { |
| 241 | HBasicBlock* pred = block->GetSinglePredecessor(); |
| 242 | HBasicBlock* succ = block->GetSingleSuccessor(); |
| 243 | if (succ->GetPredecessors().size() == 1) { |
| 244 | pred->ReplaceSuccessor(block, succ); |
| 245 | block->ClearDominanceInformation(); |
| 246 | block->SetDominator(pred); // needed by next disconnect. |
| 247 | block->DisconnectAndDelete(); |
| 248 | pred->AddDominatedBlock(succ); |
| 249 | succ->SetDominator(pred); |
| 250 | } |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void HLoopOptimization::RemoveIfEmptyLoop(LoopNode* node) { |
| 256 | HBasicBlock* header = node->loop_info->GetHeader(); |
| 257 | HBasicBlock* preheader = node->loop_info->GetPreHeader(); |
| 258 | // Ensure there is only a single loop-body (besides the header). |
| 259 | HBasicBlock* body = nullptr; |
| 260 | for (HBlocksInLoopIterator it(*node->loop_info); !it.Done(); it.Advance()) { |
| 261 | if (it.Current() != header) { |
| 262 | if (body != nullptr) { |
| 263 | return; |
| 264 | } |
| 265 | body = it.Current(); |
| 266 | } |
| 267 | } |
| 268 | // Ensure there is only a single exit point. |
| 269 | if (header->GetSuccessors().size() != 2) { |
| 270 | return; |
| 271 | } |
| 272 | HBasicBlock* exit = (header->GetSuccessors()[0] == body) |
| 273 | ? header->GetSuccessors()[1] |
| 274 | : header->GetSuccessors()[0]; |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 275 | // Ensure exit can only be reached by exiting loop. |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 276 | if (exit->GetPredecessors().size() != 1) { |
| 277 | return; |
| 278 | } |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 279 | // Detect an empty loop: no side effects other than plain iteration. Replace |
| 280 | // subsequent index uses, if any, with the last value and remove the loop. |
| 281 | iset_->clear(); |
| 282 | int32_t use_count = 0; |
| 283 | if (IsEmptyHeader(header, iset_) && |
| 284 | IsEmptyBody(body, iset_) && |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 285 | IsOnlyUsedAfterLoop(node->loop_info, header->GetFirstPhi(), &use_count) && |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 286 | TryReplaceWithLastValue(header->GetFirstPhi(), use_count, preheader)) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 287 | body->DisconnectAndDelete(); |
| 288 | exit->RemovePredecessor(header); |
| 289 | header->RemoveSuccessor(exit); |
| 290 | header->ClearDominanceInformation(); |
| 291 | header->SetDominator(preheader); // needed by next disconnect. |
| 292 | header->DisconnectAndDelete(); |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 293 | preheader->AddSuccessor(exit); |
| 294 | preheader->AddInstruction(new (graph_->GetArena()) HGoto()); // global allocator |
| 295 | preheader->AddDominatedBlock(exit); |
| 296 | exit->SetDominator(preheader); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 297 | // Update hierarchy. |
| 298 | RemoveLoop(node); |
| 299 | } |
| 300 | } |
| 301 | |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 302 | bool HLoopOptimization::IsOnlyUsedAfterLoop(HLoopInformation* loop_info, |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 303 | HInstruction* instruction, |
| 304 | /*out*/ int32_t* use_count) { |
| 305 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 306 | HInstruction* user = use.GetUser(); |
| 307 | if (iset_->find(user) == iset_->end()) { // not excluded? |
| 308 | HLoopInformation* other_loop_info = user->GetBlock()->GetLoopInformation(); |
Aart Bik | 482095d | 2016-10-10 15:39:10 -0700 | [diff] [blame^] | 309 | if (other_loop_info != nullptr && other_loop_info->IsIn(*loop_info)) { |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 310 | return false; |
| 311 | } |
| 312 | ++*use_count; |
| 313 | } |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | void HLoopOptimization::ReplaceAllUses(HInstruction* instruction, HInstruction* replacement) { |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 319 | const HUseList<HInstruction*>& uses = instruction->GetUses(); |
| 320 | for (auto it = uses.begin(), end = uses.end(); it != end;) { |
| 321 | HInstruction* user = it->GetUser(); |
| 322 | size_t index = it->GetIndex(); |
| 323 | ++it; // increment before replacing |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 324 | if (iset_->find(user) == iset_->end()) { // not excluded? |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 325 | user->ReplaceInput(replacement, index); |
| 326 | induction_range_.Replace(user, instruction, replacement); // update induction |
| 327 | } |
| 328 | } |
| 329 | const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses(); |
| 330 | for (auto it = env_uses.begin(), end = env_uses.end(); it != end;) { |
| 331 | HEnvironment* user = it->GetUser(); |
| 332 | size_t index = it->GetIndex(); |
| 333 | ++it; // increment before replacing |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 334 | if (iset_->find(user->GetHolder()) == iset_->end()) { // not excluded? |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 335 | user->RemoveAsUserOfInput(index); |
| 336 | user->SetRawEnvAt(index, replacement); |
| 337 | replacement->AddEnvUseAt(user, index); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Aart Bik | 8c4a854 | 2016-10-06 11:36:57 -0700 | [diff] [blame] | 342 | bool HLoopOptimization::TryReplaceWithLastValue(HInstruction* instruction, |
| 343 | int32_t use_count, |
| 344 | HBasicBlock* block) { |
| 345 | // If true uses appear after the loop, replace these uses with the last value. Environment |
| 346 | // uses can consume this value too, since any first true use is outside the loop (although |
| 347 | // this may imply that de-opting may look "ahead" a bit on the phi value). If there are only |
| 348 | // environment uses, the value is dropped altogether, since the computations have no effect. |
| 349 | if (use_count > 0) { |
| 350 | if (!induction_range_.CanGenerateLastValue(instruction)) { |
| 351 | return false; |
| 352 | } |
| 353 | ReplaceAllUses(instruction, induction_range_.GenerateLastValue(instruction, graph_, block)); |
| 354 | } |
| 355 | return true; |
| 356 | } |
| 357 | |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 358 | } // namespace art |