Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "induction_var_analysis.h" |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 18 | #include "induction_var_range.h" |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | /** |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 23 | * Since graph traversal may enter a SCC at any position, an initial representation may be rotated, |
| 24 | * along dependences, viz. any of (a, b, c, d), (d, a, b, c) (c, d, a, b), (b, c, d, a) assuming |
| 25 | * a chain of dependences (mutual independent items may occur in arbitrary order). For proper |
| 26 | * classification, the lexicographically first entry-phi is rotated to the front. |
| 27 | */ |
| 28 | static void RotateEntryPhiFirst(HLoopInformation* loop, |
| 29 | ArenaVector<HInstruction*>* scc, |
| 30 | ArenaVector<HInstruction*>* new_scc) { |
| 31 | // Find very first entry-phi. |
| 32 | const HInstructionList& phis = loop->GetHeader()->GetPhis(); |
| 33 | HInstruction* phi = nullptr; |
| 34 | size_t phi_pos = -1; |
| 35 | const size_t size = scc->size(); |
| 36 | for (size_t i = 0; i < size; i++) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 37 | HInstruction* other = (*scc)[i]; |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 38 | if (other->IsLoopHeaderPhi() && (phi == nullptr || phis.FoundBefore(other, phi))) { |
| 39 | phi = other; |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 40 | phi_pos = i; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // If found, bring that entry-phi to front. |
| 45 | if (phi != nullptr) { |
| 46 | new_scc->clear(); |
| 47 | for (size_t i = 0; i < size; i++) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 48 | new_scc->push_back((*scc)[phi_pos]); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 49 | if (++phi_pos >= size) phi_pos = 0; |
| 50 | } |
| 51 | DCHECK_EQ(size, new_scc->size()); |
| 52 | scc->swap(*new_scc); |
| 53 | } |
| 54 | } |
| 55 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 56 | // |
| 57 | // Class methods. |
| 58 | // |
| 59 | |
| 60 | HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph) |
| 61 | : HOptimization(graph, kInductionPassName), |
| 62 | global_depth_(0), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 63 | stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)), |
| 64 | scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)), |
| 65 | map_(std::less<HInstruction*>(), |
| 66 | graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)), |
| 67 | cycle_(std::less<HInstruction*>(), |
| 68 | graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)), |
| 69 | induction_(std::less<HLoopInformation*>(), |
| 70 | graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void HInductionVarAnalysis::Run() { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 74 | // Detects sequence variables (generalized induction variables) during an outer to inner |
| 75 | // traversal of all loops using Gerlek's algorithm. The order is important to enable |
| 76 | // range analysis on outer loop while visiting inner loops. |
| 77 | for (HReversePostOrderIterator it_graph(*graph_); !it_graph.Done(); it_graph.Advance()) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 78 | HBasicBlock* graph_block = it_graph.Current(); |
| 79 | if (graph_block->IsLoopHeader()) { |
| 80 | VisitLoop(graph_block->GetLoopInformation()); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) { |
| 86 | // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's |
| 87 | // algorithm. Due to the descendant-first nature, classification happens "on-demand". |
| 88 | global_depth_ = 0; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 89 | DCHECK(stack_.empty()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 90 | map_.clear(); |
| 91 | |
| 92 | for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) { |
| 93 | HBasicBlock* loop_block = it_loop.Current(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 94 | DCHECK(loop_block->IsInLoop()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 95 | if (loop_block->GetLoopInformation() != loop) { |
| 96 | continue; // Inner loops already visited. |
| 97 | } |
| 98 | // Visit phi-operations and instructions. |
| 99 | for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) { |
| 100 | HInstruction* instruction = it.Current(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 101 | if (!IsVisitedNode(instruction)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 102 | VisitNode(loop, instruction); |
| 103 | } |
| 104 | } |
| 105 | for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) { |
| 106 | HInstruction* instruction = it.Current(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 107 | if (!IsVisitedNode(instruction)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 108 | VisitNode(loop, instruction); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 113 | DCHECK(stack_.empty()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 114 | map_.clear(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 115 | |
| 116 | // Determine the loop's trip count. |
| 117 | VisitControl(loop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 121 | const uint32_t d1 = ++global_depth_; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 122 | map_.Put(instruction, NodeInfo(d1)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 123 | stack_.push_back(instruction); |
| 124 | |
| 125 | // Visit all descendants. |
| 126 | uint32_t low = d1; |
| 127 | for (size_t i = 0, count = instruction->InputCount(); i < count; ++i) { |
| 128 | low = std::min(low, VisitDescendant(loop, instruction->InputAt(i))); |
| 129 | } |
| 130 | |
| 131 | // Lower or found SCC? |
| 132 | if (low < d1) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 133 | map_.find(instruction)->second.depth = low; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 134 | } else { |
| 135 | scc_.clear(); |
| 136 | cycle_.clear(); |
| 137 | |
| 138 | // Pop the stack to build the SCC for classification. |
| 139 | while (!stack_.empty()) { |
| 140 | HInstruction* x = stack_.back(); |
| 141 | scc_.push_back(x); |
| 142 | stack_.pop_back(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 143 | map_.find(x)->second.done = true; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 144 | if (x == instruction) { |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Classify the SCC. |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 150 | if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 151 | ClassifyTrivial(loop, scc_[0]); |
| 152 | } else { |
| 153 | ClassifyNonTrivial(loop); |
| 154 | } |
| 155 | |
| 156 | scc_.clear(); |
| 157 | cycle_.clear(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) { |
| 162 | // If the definition is either outside the loop (loop invariant entry value) |
| 163 | // or assigned in inner loop (inner exit value), the traversal stops. |
| 164 | HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation(); |
| 165 | if (otherLoop != loop) { |
| 166 | return global_depth_; |
| 167 | } |
| 168 | |
| 169 | // Inspect descendant node. |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 170 | if (!IsVisitedNode(instruction)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 171 | VisitNode(loop, instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 172 | return map_.find(instruction)->second.depth; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 173 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 174 | auto it = map_.find(instruction); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 175 | return it->second.done ? global_depth_ : it->second.depth; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) { |
| 180 | InductionInfo* info = nullptr; |
| 181 | if (instruction->IsPhi()) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 182 | info = TransferPhi(loop, instruction, /* input_index */ 0); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 183 | } else if (instruction->IsAdd()) { |
| 184 | info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)), |
| 185 | LookupInfo(loop, instruction->InputAt(1)), kAdd); |
| 186 | } else if (instruction->IsSub()) { |
| 187 | info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)), |
| 188 | LookupInfo(loop, instruction->InputAt(1)), kSub); |
| 189 | } else if (instruction->IsMul()) { |
| 190 | info = TransferMul(LookupInfo(loop, instruction->InputAt(0)), |
| 191 | LookupInfo(loop, instruction->InputAt(1))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 192 | } else if (instruction->IsShl()) { |
| 193 | info = TransferShl(LookupInfo(loop, instruction->InputAt(0)), |
| 194 | LookupInfo(loop, instruction->InputAt(1)), |
| 195 | instruction->InputAt(0)->GetType()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 196 | } else if (instruction->IsNeg()) { |
| 197 | info = TransferNeg(LookupInfo(loop, instruction->InputAt(0))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 198 | } else if (instruction->IsBoundsCheck()) { |
| 199 | info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through. |
| 200 | } else if (instruction->IsTypeConversion()) { |
| 201 | HTypeConversion* conversion = instruction->AsTypeConversion(); |
| 202 | // TODO: accept different conversion scenarios. |
| 203 | if (conversion->GetResultType() == conversion->GetInputType()) { |
| 204 | info = LookupInfo(loop, conversion->GetInput()); |
| 205 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Successfully classified? |
| 209 | if (info != nullptr) { |
| 210 | AssignInfo(loop, instruction, info); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) { |
| 215 | const size_t size = scc_.size(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 216 | DCHECK_GE(size, 1u); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 217 | |
| 218 | // Rotate proper entry-phi to front. |
| 219 | if (size > 1) { |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 220 | ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 221 | RotateEntryPhiFirst(loop, &scc_, &other); |
| 222 | } |
| 223 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 224 | // Analyze from entry-phi onwards. |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 225 | HInstruction* phi = scc_[0]; |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 226 | if (!phi->IsLoopHeaderPhi()) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 227 | return; |
| 228 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 229 | |
| 230 | // External link should be loop invariant. |
| 231 | InductionInfo* initial = LookupInfo(loop, phi->InputAt(0)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 232 | if (initial == nullptr || initial->induction_class != kInvariant) { |
| 233 | return; |
| 234 | } |
| 235 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 236 | // Singleton is wrap-around induction if all internal links have the same meaning. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 237 | if (size == 1) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 238 | InductionInfo* update = TransferPhi(loop, phi, /* input_index */ 1); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 239 | if (update != nullptr) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 240 | AssignInfo(loop, phi, CreateInduction(kWrapAround, initial, update)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 241 | } |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 246 | // temporary meaning to its nodes, seeded from the phi instruction and back. |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 247 | for (size_t i = 1; i < size; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 248 | HInstruction* instruction = scc_[i]; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 249 | InductionInfo* update = nullptr; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 250 | if (instruction->IsPhi()) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 251 | update = SolvePhiAllInputs(loop, phi, instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 252 | } else if (instruction->IsAdd()) { |
| 253 | update = SolveAddSub( |
| 254 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true); |
| 255 | } else if (instruction->IsSub()) { |
| 256 | update = SolveAddSub( |
| 257 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 258 | } |
| 259 | if (update == nullptr) { |
| 260 | return; |
| 261 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 262 | cycle_.Put(instruction, update); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 265 | // Success if all internal links received the same temporary meaning. |
| 266 | InductionInfo* induction = SolvePhi(phi, /* input_index */ 1); |
| 267 | if (induction != nullptr) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 268 | switch (induction->induction_class) { |
| 269 | case kInvariant: |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 270 | // Classify first phi and then the rest of the cycle "on-demand". |
| 271 | // Statements are scanned in order. |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 272 | AssignInfo(loop, phi, CreateInduction(kLinear, induction, initial)); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 273 | for (size_t i = 1; i < size; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 274 | ClassifyTrivial(loop, scc_[i]); |
| 275 | } |
| 276 | break; |
| 277 | case kPeriodic: |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 278 | // Classify all elements in the cycle with the found periodic induction while |
| 279 | // rotating each first element to the end. Lastly, phi is classified. |
| 280 | // Statements are scanned in reverse order. |
| 281 | for (size_t i = size - 1; i >= 1; i--) { |
| 282 | AssignInfo(loop, scc_[i], induction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 283 | induction = RotatePeriodicInduction(induction->op_b, induction->op_a); |
| 284 | } |
| 285 | AssignInfo(loop, phi, induction); |
| 286 | break; |
| 287 | default: |
| 288 | break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 293 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction( |
| 294 | InductionInfo* induction, |
| 295 | InductionInfo* last) { |
| 296 | // Rotates a periodic induction of the form |
| 297 | // (a, b, c, d, e) |
| 298 | // into |
| 299 | // (b, c, d, e, a) |
| 300 | // in preparation of assigning this to the previous variable in the sequence. |
| 301 | if (induction->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 302 | return CreateInduction(kPeriodic, induction, last); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 303 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 304 | return CreateInduction(kPeriodic, induction->op_a, RotatePeriodicInduction(induction->op_b, last)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 307 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop, |
| 308 | HInstruction* phi, |
| 309 | size_t input_index) { |
| 310 | // Match all phi inputs from input_index onwards exactly. |
| 311 | const size_t count = phi->InputCount(); |
| 312 | DCHECK_LT(input_index, count); |
| 313 | InductionInfo* a = LookupInfo(loop, phi->InputAt(input_index)); |
| 314 | for (size_t i = input_index + 1; i < count; i++) { |
| 315 | InductionInfo* b = LookupInfo(loop, phi->InputAt(i)); |
| 316 | if (!InductionEqual(a, b)) { |
| 317 | return nullptr; |
| 318 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 319 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 320 | return a; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a, |
| 324 | InductionInfo* b, |
| 325 | InductionOp op) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 326 | // Transfer over an addition or subtraction: any invariant, linear, wrap-around, or periodic |
| 327 | // can be combined with an invariant to yield a similar result. Even two linear inputs can |
| 328 | // be combined. All other combinations fail, however. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 329 | if (a != nullptr && b != nullptr) { |
| 330 | if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 331 | return CreateInvariantOp(op, a, b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 332 | } else if (a->induction_class == kLinear && b->induction_class == kLinear) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 333 | return CreateInduction( |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 334 | kLinear, TransferAddSub(a->op_a, b->op_a, op), TransferAddSub(a->op_b, b->op_b, op)); |
| 335 | } else if (a->induction_class == kInvariant) { |
| 336 | InductionInfo* new_a = b->op_a; |
| 337 | InductionInfo* new_b = TransferAddSub(a, b->op_b, op); |
| 338 | if (b->induction_class != kLinear) { |
| 339 | DCHECK(b->induction_class == kWrapAround || b->induction_class == kPeriodic); |
| 340 | new_a = TransferAddSub(a, new_a, op); |
| 341 | } else if (op == kSub) { // Negation required. |
| 342 | new_a = TransferNeg(new_a); |
| 343 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 344 | return CreateInduction(b->induction_class, new_a, new_b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 345 | } else if (b->induction_class == kInvariant) { |
| 346 | InductionInfo* new_a = a->op_a; |
| 347 | InductionInfo* new_b = TransferAddSub(a->op_b, b, op); |
| 348 | if (a->induction_class != kLinear) { |
| 349 | DCHECK(a->induction_class == kWrapAround || a->induction_class == kPeriodic); |
| 350 | new_a = TransferAddSub(new_a, b, op); |
| 351 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 352 | return CreateInduction(a->induction_class, new_a, new_b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | return nullptr; |
| 356 | } |
| 357 | |
| 358 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a, |
| 359 | InductionInfo* b) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 360 | // Transfer over a multiplication: any invariant, linear, wrap-around, or periodic |
| 361 | // can be multiplied with an invariant to yield a similar but multiplied result. |
| 362 | // Two non-invariant inputs cannot be multiplied, however. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 363 | if (a != nullptr && b != nullptr) { |
| 364 | if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 365 | return CreateInvariantOp(kMul, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 366 | } else if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 367 | return CreateInduction(b->induction_class, TransferMul(a, b->op_a), TransferMul(a, b->op_b)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 368 | } else if (b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 369 | return CreateInduction(a->induction_class, TransferMul(a->op_a, b), TransferMul(a->op_b, b)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | return nullptr; |
| 373 | } |
| 374 | |
| 375 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferShl(InductionInfo* a, |
| 376 | InductionInfo* b, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 377 | Primitive::Type type) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 378 | // Transfer over a shift left: treat shift by restricted constant as equivalent multiplication. |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 379 | int64_t value = -1; |
| 380 | if (a != nullptr && IsIntAndGet(b, &value)) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 381 | // Obtain the constant needed for the multiplication. This yields an existing instruction |
| 382 | // if the constants is already there. Otherwise, this has a side effect on the HIR. |
| 383 | // The restriction on the shift factor avoids generating a negative constant |
| 384 | // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization |
| 385 | // for shift factors outside [0,32) and [0,64) ranges is done by earlier simplification. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 386 | if ((type == Primitive::kPrimInt && 0 <= value && value < 31) || |
| 387 | (type == Primitive::kPrimLong && 0 <= value && value < 63)) { |
| 388 | return TransferMul(a, CreateConstant(1 << value, type)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
| 394 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 395 | // Transfer over a unary negation: an invariant, linear, wrap-around, or periodic input |
| 396 | // yields a similar but negated induction as result. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 397 | if (a != nullptr) { |
| 398 | if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 399 | return CreateInvariantOp(kNeg, nullptr, a); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 400 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 401 | return CreateInduction(a->induction_class, TransferNeg(a->op_a), TransferNeg(a->op_b)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 402 | } |
| 403 | return nullptr; |
| 404 | } |
| 405 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 406 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi, |
| 407 | size_t input_index) { |
| 408 | // Match all phi inputs from input_index onwards exactly. |
| 409 | const size_t count = phi->InputCount(); |
| 410 | DCHECK_LT(input_index, count); |
| 411 | auto ita = cycle_.find(phi->InputAt(input_index)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 412 | if (ita != cycle_.end()) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 413 | for (size_t i = input_index + 1; i < count; i++) { |
| 414 | auto itb = cycle_.find(phi->InputAt(i)); |
| 415 | if (itb == cycle_.end() || |
| 416 | !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 417 | return nullptr; |
| 418 | } |
| 419 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 420 | return ita->second; |
| 421 | } |
| 422 | return nullptr; |
| 423 | } |
| 424 | |
| 425 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs( |
| 426 | HLoopInformation* loop, |
| 427 | HInstruction* entry_phi, |
| 428 | HInstruction* phi) { |
| 429 | // Match all phi inputs. |
| 430 | InductionInfo* match = SolvePhi(phi, /* input_index */ 0); |
| 431 | if (match != nullptr) { |
| 432 | return match; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 433 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 434 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 435 | // Otherwise, try to solve for a periodic seeded from phi onward. |
| 436 | // Only tight multi-statement cycles are considered in order to |
| 437 | // simplify rotating the periodic during the final classification. |
| 438 | if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) { |
| 439 | InductionInfo* a = LookupInfo(loop, phi->InputAt(0)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 440 | if (a != nullptr && a->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 441 | if (phi->InputAt(1) == entry_phi) { |
| 442 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 443 | return CreateInduction(kPeriodic, a, initial); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 444 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 445 | InductionInfo* b = SolvePhi(phi, /* input_index */ 1); |
| 446 | if (b != nullptr && b->induction_class == kPeriodic) { |
| 447 | return CreateInduction(kPeriodic, a, b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 451 | return nullptr; |
| 452 | } |
| 453 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 454 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 455 | HInstruction* entry_phi, |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 456 | HInstruction* instruction, |
| 457 | HInstruction* x, |
| 458 | HInstruction* y, |
| 459 | InductionOp op, |
| 460 | bool is_first_call) { |
| 461 | // Solve within a cycle over an addition or subtraction: adding or subtracting an |
| 462 | // invariant value, seeded from phi, keeps adding to the stride of the induction. |
| 463 | InductionInfo* b = LookupInfo(loop, y); |
| 464 | if (b != nullptr && b->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 465 | if (x == entry_phi) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 466 | return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 467 | } |
| 468 | auto it = cycle_.find(x); |
| 469 | if (it != cycle_.end()) { |
| 470 | InductionInfo* a = it->second; |
| 471 | if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 472 | return CreateInvariantOp(op, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 473 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 476 | |
| 477 | // Try some alternatives before failing. |
| 478 | if (op == kAdd) { |
| 479 | // Try the other way around for an addition if considered for first time. |
| 480 | if (is_first_call) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 481 | return SolveAddSub(loop, entry_phi, instruction, y, x, op, false); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 482 | } |
| 483 | } else if (op == kSub) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 484 | // Solve within a tight cycle that is formed by exactly two instructions, |
| 485 | // one phi and one update, for a periodic idiom of the form k = c - k; |
| 486 | if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 487 | InductionInfo* a = LookupInfo(loop, x); |
| 488 | if (a != nullptr && a->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 489 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 490 | return CreateInduction(kPeriodic, CreateInvariantOp(kSub, a, initial), initial); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 495 | return nullptr; |
| 496 | } |
| 497 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 498 | void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) { |
| 499 | HInstruction* control = loop->GetHeader()->GetLastInstruction(); |
| 500 | if (control->IsIf()) { |
| 501 | HIf* ifs = control->AsIf(); |
| 502 | HBasicBlock* if_true = ifs->IfTrueSuccessor(); |
| 503 | HBasicBlock* if_false = ifs->IfFalseSuccessor(); |
| 504 | HInstruction* if_expr = ifs->InputAt(0); |
| 505 | // Determine if loop has following structure in header. |
| 506 | // loop-header: .... |
| 507 | // if (condition) goto X |
| 508 | if (if_expr->IsCondition()) { |
| 509 | HCondition* condition = if_expr->AsCondition(); |
| 510 | InductionInfo* a = LookupInfo(loop, condition->InputAt(0)); |
| 511 | InductionInfo* b = LookupInfo(loop, condition->InputAt(1)); |
| 512 | Primitive::Type type = condition->InputAt(0)->GetType(); |
| 513 | // Determine if the loop control uses integral arithmetic and an if-exit (X outside) or an |
| 514 | // if-iterate (X inside), always expressed as if-iterate when passing into VisitCondition(). |
| 515 | if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) { |
| 516 | // Loop control is not 32/64-bit integral. |
| 517 | } else if (a == nullptr || b == nullptr) { |
| 518 | // Loop control is not a sequence. |
| 519 | } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) { |
| 520 | VisitCondition(loop, a, b, type, condition->GetOppositeCondition()); |
| 521 | } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) { |
| 522 | VisitCondition(loop, a, b, type, condition->GetCondition()); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop, |
| 529 | InductionInfo* a, |
| 530 | InductionInfo* b, |
| 531 | Primitive::Type type, |
| 532 | IfCondition cmp) { |
| 533 | if (a->induction_class == kInvariant && b->induction_class == kLinear) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 534 | // Swap condition if induction is at right-hand-side (e.g. U > i is same as i < U). |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 535 | switch (cmp) { |
| 536 | case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break; |
| 537 | case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break; |
| 538 | case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break; |
| 539 | case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break; |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 540 | case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 541 | default: break; |
| 542 | } |
| 543 | } else if (a->induction_class == kLinear && b->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 544 | // Analyze condition with induction at left-hand-side (e.g. i < U). |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 545 | InductionInfo* lower_expr = a->op_b; |
| 546 | InductionInfo* upper_expr = b; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 547 | InductionInfo* stride = a->op_a; |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 548 | int64_t stride_value = 0; |
| 549 | if (!IsIntAndGet(stride, &stride_value)) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 550 | return; |
| 551 | } |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 552 | // Rewrite condition i != U into i < U or i > U if end condition is reached exactly. |
| 553 | if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLT)) || |
| 554 | (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGT)))) { |
| 555 | cmp = stride_value > 0 ? kCondLT : kCondGT; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 556 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 557 | // Normalize a linear loop control with a nonzero stride: |
| 558 | // stride > 0, either i < U or i <= U |
| 559 | // stride < 0, either i > U or i >= U |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 560 | if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) || |
| 561 | (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 562 | VisitTripCount(loop, lower_expr, upper_expr, stride, stride_value, type, cmp); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 563 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 568 | InductionInfo* lower_expr, |
| 569 | InductionInfo* upper_expr, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 570 | InductionInfo* stride, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 571 | int64_t stride_value, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 572 | Primitive::Type type, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 573 | IfCondition cmp) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 574 | // Any loop of the general form: |
| 575 | // |
| 576 | // for (i = L; i <= U; i += S) // S > 0 |
| 577 | // or for (i = L; i >= U; i += S) // S < 0 |
| 578 | // .. i .. |
| 579 | // |
| 580 | // can be normalized into: |
| 581 | // |
| 582 | // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S |
| 583 | // .. L + S * n .. |
| 584 | // |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 585 | // taking the following into consideration: |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 586 | // |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 587 | // (1) Using the same precision, the TC (trip-count) expression should be interpreted as |
| 588 | // an unsigned entity, for example, as in the following loop that uses the full range: |
| 589 | // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX |
| 590 | // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in: |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 591 | // for (int i = 12; i < U; i++) // TC = 0 when U < 12 |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 592 | // If this cannot be determined at compile-time, the TC is only valid within the |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 593 | // loop-body proper, not the loop-header unless enforced with an explicit taken-test. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 594 | // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in: |
| 595 | // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX |
| 596 | // If this cannot be determined at compile-time, the TC is only valid when enforced |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 597 | // with an explicit finite-test. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 598 | // (4) For loops which early-exits, the TC forms an upper bound, as in: |
| 599 | // for (int i = 0; i < 10 && ....; i++) // TC <= 10 |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 600 | InductionInfo* trip_count = upper_expr; |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 601 | const bool is_taken = IsTaken(lower_expr, upper_expr, cmp); |
| 602 | const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp); |
| 603 | const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 604 | if (!cancels) { |
| 605 | // Convert exclusive integral inequality into inclusive integral inequality, |
| 606 | // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1. |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 607 | if (cmp == kCondLT) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 608 | trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type)); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 609 | } else if (cmp == kCondGT) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 610 | trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 611 | } |
| 612 | // Compensate for stride. |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 613 | trip_count = CreateInvariantOp(kAdd, trip_count, stride); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 614 | } |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 615 | trip_count = CreateInvariantOp(kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 616 | // Assign the trip-count expression to the loop control. Clients that use the information |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 617 | // should be aware that the expression is only valid under the conditions listed above. |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 618 | InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 619 | if (is_taken && is_finite) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 620 | tcKind = kTripCountInLoop; // needs neither test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 621 | } else if (is_finite) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 622 | tcKind = kTripCountInBody; // needs taken-test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 623 | } else if (is_taken) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 624 | tcKind = kTripCountInLoopUnsafe; // needs finite-test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 625 | } |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 626 | InductionOp op = kNop; |
| 627 | switch (cmp) { |
| 628 | case kCondLT: op = kLT; break; |
| 629 | case kCondLE: op = kLE; break; |
| 630 | case kCondGT: op = kGT; break; |
| 631 | case kCondGE: op = kGE; break; |
| 632 | default: LOG(FATAL) << "CONDITION UNREACHABLE"; |
| 633 | } |
| 634 | InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr); |
| 635 | AssignInfo(loop, |
| 636 | loop->GetHeader()->GetLastInstruction(), |
| 637 | CreateTripCount(tcKind, trip_count, taken_test)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr, |
| 641 | InductionInfo* upper_expr, |
| 642 | IfCondition cmp) { |
| 643 | int64_t lower_value; |
| 644 | int64_t upper_value; |
| 645 | if (IsIntAndGet(lower_expr, &lower_value) && IsIntAndGet(upper_expr, &upper_value)) { |
| 646 | switch (cmp) { |
| 647 | case kCondLT: return lower_value < upper_value; |
| 648 | case kCondLE: return lower_value <= upper_value; |
| 649 | case kCondGT: return lower_value > upper_value; |
| 650 | case kCondGE: return lower_value >= upper_value; |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 651 | default: LOG(FATAL) << "CONDITION UNREACHABLE"; |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | return false; // not certain, may be untaken |
| 655 | } |
| 656 | |
| 657 | bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr, |
| 658 | int64_t stride_value, |
| 659 | Primitive::Type type, |
| 660 | IfCondition cmp) { |
| 661 | const int64_t min = type == Primitive::kPrimInt |
| 662 | ? std::numeric_limits<int32_t>::min() |
| 663 | : std::numeric_limits<int64_t>::min(); |
| 664 | const int64_t max = type == Primitive::kPrimInt |
| 665 | ? std::numeric_limits<int32_t>::max() |
| 666 | : std::numeric_limits<int64_t>::max(); |
| 667 | // Some rules under which it is certain at compile-time that the loop is finite. |
| 668 | int64_t value; |
| 669 | switch (cmp) { |
| 670 | case kCondLT: |
| 671 | return stride_value == 1 || |
| 672 | (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value + 1)); |
| 673 | case kCondLE: |
| 674 | return (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value)); |
| 675 | case kCondGT: |
| 676 | return stride_value == -1 || |
| 677 | (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value - 1)); |
| 678 | case kCondGE: |
| 679 | return (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value)); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 680 | default: |
| 681 | LOG(FATAL) << "CONDITION UNREACHABLE"; |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 682 | } |
| 683 | return false; // not certain, may be infinite |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 686 | void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop, |
| 687 | HInstruction* instruction, |
| 688 | InductionInfo* info) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 689 | auto it = induction_.find(loop); |
| 690 | if (it == induction_.end()) { |
| 691 | it = induction_.Put(loop, |
| 692 | ArenaSafeMap<HInstruction*, InductionInfo*>( |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 693 | std::less<HInstruction*>(), |
| 694 | graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 695 | } |
| 696 | it->second.Put(instruction, info); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 699 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop, |
| 700 | HInstruction* instruction) { |
| 701 | auto it = induction_.find(loop); |
| 702 | if (it != induction_.end()) { |
| 703 | auto loop_it = it->second.find(instruction); |
| 704 | if (loop_it != it->second.end()) { |
| 705 | return loop_it->second; |
| 706 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 707 | } |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 708 | if (loop->IsDefinedOutOfTheLoop(instruction)) { |
| 709 | DCHECK(instruction->GetBlock()->Dominates(loop->GetPreHeader())); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 710 | InductionInfo* info = CreateInvariantFetch(instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 711 | AssignInfo(loop, instruction, info); |
| 712 | return info; |
| 713 | } |
| 714 | return nullptr; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 717 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value, |
| 718 | Primitive::Type type) { |
| 719 | if (type == Primitive::kPrimInt) { |
| 720 | return CreateInvariantFetch(graph_->GetIntConstant(value)); |
| 721 | } |
| 722 | DCHECK_EQ(type, Primitive::kPrimLong); |
| 723 | return CreateInvariantFetch(graph_->GetLongConstant(value)); |
| 724 | } |
| 725 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 726 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant( |
| 727 | InductionOp op, |
| 728 | InductionInfo* a, |
| 729 | InductionInfo* b) { |
| 730 | // Perform some light-weight simplifications during construction of a new invariant. |
| 731 | // This often safes memory and yields a more concise representation of the induction. |
| 732 | // More exhaustive simplifications are done by later phases once induction nodes are |
| 733 | // translated back into HIR code (e.g. by loop optimizations or BCE). |
| 734 | int64_t value = -1; |
| 735 | if (IsIntAndGet(a, &value)) { |
| 736 | if (value == 0) { |
| 737 | // Simplify 0 + b = b, 0 * b = 0. |
| 738 | if (op == kAdd) { |
| 739 | return b; |
| 740 | } else if (op == kMul) { |
| 741 | return a; |
| 742 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 743 | } else if (op == kMul) { |
| 744 | // Simplify 1 * b = b, -1 * b = -b |
| 745 | if (value == 1) { |
| 746 | return b; |
| 747 | } else if (value == -1) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 748 | return CreateSimplifiedInvariant(kNeg, nullptr, b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 749 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | if (IsIntAndGet(b, &value)) { |
| 753 | if (value == 0) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 754 | // Simplify a + 0 = a, a - 0 = a, a * 0 = 0, -0 = 0. |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 755 | if (op == kAdd || op == kSub) { |
| 756 | return a; |
| 757 | } else if (op == kMul || op == kNeg) { |
| 758 | return b; |
| 759 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 760 | } else if (op == kMul || op == kDiv) { |
| 761 | // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a |
| 762 | if (value == 1) { |
| 763 | return a; |
| 764 | } else if (value == -1) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 765 | return CreateSimplifiedInvariant(kNeg, nullptr, a); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 766 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 767 | } |
| 768 | } else if (b->operation == kNeg) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 769 | // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b. |
| 770 | if (op == kAdd) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 771 | return CreateSimplifiedInvariant(kSub, a, b->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 772 | } else if (op == kSub) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 773 | return CreateSimplifiedInvariant(kAdd, a, b->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 774 | } else if (op == kNeg) { |
| 775 | return b->op_b; |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 776 | } |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 777 | } else if (b->operation == kSub) { |
| 778 | // Simplify - (a - b) = b - a. |
| 779 | if (op == kNeg) { |
| 780 | return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a); |
| 781 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 782 | } |
| 783 | return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr); |
| 784 | } |
| 785 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 786 | bool HInductionVarAnalysis::IsIntAndGet(InductionInfo* info, int64_t* value) { |
| 787 | if (info != nullptr && info->induction_class == kInvariant) { |
| 788 | // A direct constant fetch. |
| 789 | if (info->operation == kFetch) { |
| 790 | DCHECK(info->fetch); |
| 791 | if (info->fetch->IsIntConstant()) { |
| 792 | *value = info->fetch->AsIntConstant()->GetValue(); |
| 793 | return true; |
| 794 | } else if (info->fetch->IsLongConstant()) { |
| 795 | *value = info->fetch->AsLongConstant()->GetValue(); |
| 796 | return true; |
| 797 | } |
| 798 | } |
| 799 | // Use range analysis to resolve compound values. |
| 800 | InductionVarRange range(this); |
| 801 | int32_t min_val = 0; |
| 802 | int32_t max_val = 0; |
| 803 | if (range.IsConstantRange(info, &min_val, &max_val) && min_val == max_val) { |
| 804 | *value = min_val; |
| 805 | return true; |
| 806 | } |
| 807 | } |
| 808 | return false; |
| 809 | } |
| 810 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 811 | bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1, |
| 812 | InductionInfo* info2) { |
| 813 | // Test structural equality only, without accounting for simplifications. |
| 814 | if (info1 != nullptr && info2 != nullptr) { |
| 815 | return |
| 816 | info1->induction_class == info2->induction_class && |
| 817 | info1->operation == info2->operation && |
| 818 | info1->fetch == info2->fetch && |
| 819 | InductionEqual(info1->op_a, info2->op_a) && |
| 820 | InductionEqual(info1->op_b, info2->op_b); |
| 821 | } |
| 822 | // Otherwise only two nullptrs are considered equal. |
| 823 | return info1 == info2; |
| 824 | } |
| 825 | |
| 826 | std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) { |
| 827 | if (info != nullptr) { |
| 828 | if (info->induction_class == kInvariant) { |
| 829 | std::string inv = "("; |
| 830 | inv += InductionToString(info->op_a); |
| 831 | switch (info->operation) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 832 | case kNop: inv += " @ "; break; |
| 833 | case kAdd: inv += " + "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 834 | case kSub: |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 835 | case kNeg: inv += " - "; break; |
| 836 | case kMul: inv += " * "; break; |
| 837 | case kDiv: inv += " / "; break; |
| 838 | case kLT: inv += " < "; break; |
| 839 | case kLE: inv += " <= "; break; |
| 840 | case kGT: inv += " > "; break; |
| 841 | case kGE: inv += " >= "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 842 | case kFetch: |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 843 | DCHECK(info->fetch); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame^] | 844 | if (info->fetch->IsIntConstant()) { |
| 845 | inv += std::to_string(info->fetch->AsIntConstant()->GetValue()); |
| 846 | } else if (info->fetch->IsLongConstant()) { |
| 847 | inv += std::to_string(info->fetch->AsLongConstant()->GetValue()); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 848 | } else { |
| 849 | inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName(); |
| 850 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 851 | break; |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 852 | case kTripCountInLoop: inv += " (TC-loop) "; break; |
| 853 | case kTripCountInBody: inv += " (TC-body) "; break; |
| 854 | case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break; |
| 855 | case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 856 | } |
| 857 | inv += InductionToString(info->op_b); |
| 858 | return inv + ")"; |
| 859 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 860 | DCHECK(info->operation == kNop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 861 | if (info->induction_class == kLinear) { |
| 862 | return "(" + InductionToString(info->op_a) + " * i + " + |
| 863 | InductionToString(info->op_b) + ")"; |
| 864 | } else if (info->induction_class == kWrapAround) { |
| 865 | return "wrap(" + InductionToString(info->op_a) + ", " + |
| 866 | InductionToString(info->op_b) + ")"; |
| 867 | } else if (info->induction_class == kPeriodic) { |
| 868 | return "periodic(" + InductionToString(info->op_a) + ", " + |
| 869 | InductionToString(info->op_b) + ")"; |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | return ""; |
| 874 | } |
| 875 | |
| 876 | } // namespace art |