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" |
| 18 | |
| 19 | namespace art { |
| 20 | |
| 21 | /** |
| 22 | * Returns true if instruction is invariant within the given loop. |
| 23 | */ |
| 24 | static bool IsLoopInvariant(HLoopInformation* loop, HInstruction* instruction) { |
| 25 | HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation(); |
| 26 | if (other_loop != loop) { |
| 27 | // If instruction does not occur in same loop, it is invariant |
| 28 | // if it appears in an outer loop (including no loop at all). |
| 29 | return other_loop == nullptr || loop->IsIn(*other_loop); |
| 30 | } |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Returns true if instruction is proper entry-phi-operation for given loop |
| 36 | * (referred to as mu-operation in Gerlek's paper). |
| 37 | */ |
| 38 | static bool IsEntryPhi(HLoopInformation* loop, HInstruction* instruction) { |
| 39 | return |
| 40 | instruction->IsPhi() && |
| 41 | instruction->InputCount() == 2 && |
| 42 | instruction->GetBlock() == loop->GetHeader(); |
| 43 | } |
| 44 | |
| 45 | // |
| 46 | // Class methods. |
| 47 | // |
| 48 | |
| 49 | HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph) |
| 50 | : HOptimization(graph, kInductionPassName), |
| 51 | global_depth_(0), |
| 52 | stack_(graph->GetArena()->Adapter()), |
| 53 | scc_(graph->GetArena()->Adapter()), |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 54 | map_(std::less<HInstruction*>(), graph->GetArena()->Adapter()), |
| 55 | cycle_(std::less<HInstruction*>(), graph->GetArena()->Adapter()), |
| 56 | induction_(std::less<HLoopInformation*>(), graph->GetArena()->Adapter()) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void HInductionVarAnalysis::Run() { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 60 | // Detects sequence variables (generalized induction variables) during an inner-loop-first |
| 61 | // traversal of all loops using Gerlek's algorithm. The order is only relevant if outer |
| 62 | // loops would use induction information of inner loops (not currently done). |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 63 | for (HPostOrderIterator it_graph(*graph_); !it_graph.Done(); it_graph.Advance()) { |
| 64 | HBasicBlock* graph_block = it_graph.Current(); |
| 65 | if (graph_block->IsLoopHeader()) { |
| 66 | VisitLoop(graph_block->GetLoopInformation()); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) { |
| 72 | // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's |
| 73 | // algorithm. Due to the descendant-first nature, classification happens "on-demand". |
| 74 | global_depth_ = 0; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 75 | DCHECK(stack_.empty()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 76 | map_.clear(); |
| 77 | |
| 78 | for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) { |
| 79 | HBasicBlock* loop_block = it_loop.Current(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 80 | DCHECK(loop_block->IsInLoop()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 81 | if (loop_block->GetLoopInformation() != loop) { |
| 82 | continue; // Inner loops already visited. |
| 83 | } |
| 84 | // Visit phi-operations and instructions. |
| 85 | for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) { |
| 86 | HInstruction* instruction = it.Current(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 87 | if (!IsVisitedNode(instruction)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 88 | VisitNode(loop, instruction); |
| 89 | } |
| 90 | } |
| 91 | for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) { |
| 92 | HInstruction* instruction = it.Current(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 93 | if (!IsVisitedNode(instruction)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 94 | VisitNode(loop, instruction); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 99 | DCHECK(stack_.empty()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 100 | map_.clear(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 101 | |
| 102 | // Determine the loop's trip count. |
| 103 | VisitControl(loop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 107 | const uint32_t d1 = ++global_depth_; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 108 | map_.Put(instruction, NodeInfo(d1)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 109 | stack_.push_back(instruction); |
| 110 | |
| 111 | // Visit all descendants. |
| 112 | uint32_t low = d1; |
| 113 | for (size_t i = 0, count = instruction->InputCount(); i < count; ++i) { |
| 114 | low = std::min(low, VisitDescendant(loop, instruction->InputAt(i))); |
| 115 | } |
| 116 | |
| 117 | // Lower or found SCC? |
| 118 | if (low < d1) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 119 | map_.find(instruction)->second.depth = low; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 120 | } else { |
| 121 | scc_.clear(); |
| 122 | cycle_.clear(); |
| 123 | |
| 124 | // Pop the stack to build the SCC for classification. |
| 125 | while (!stack_.empty()) { |
| 126 | HInstruction* x = stack_.back(); |
| 127 | scc_.push_back(x); |
| 128 | stack_.pop_back(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 129 | map_.find(x)->second.done = true; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 130 | if (x == instruction) { |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Classify the SCC. |
| 136 | if (scc_.size() == 1 && !IsEntryPhi(loop, scc_[0])) { |
| 137 | ClassifyTrivial(loop, scc_[0]); |
| 138 | } else { |
| 139 | ClassifyNonTrivial(loop); |
| 140 | } |
| 141 | |
| 142 | scc_.clear(); |
| 143 | cycle_.clear(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) { |
| 148 | // If the definition is either outside the loop (loop invariant entry value) |
| 149 | // or assigned in inner loop (inner exit value), the traversal stops. |
| 150 | HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation(); |
| 151 | if (otherLoop != loop) { |
| 152 | return global_depth_; |
| 153 | } |
| 154 | |
| 155 | // Inspect descendant node. |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 156 | if (!IsVisitedNode(instruction)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 157 | VisitNode(loop, instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 158 | return map_.find(instruction)->second.depth; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 159 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 160 | auto it = map_.find(instruction); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 161 | return it->second.done ? global_depth_ : it->second.depth; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) { |
| 166 | InductionInfo* info = nullptr; |
| 167 | if (instruction->IsPhi()) { |
| 168 | for (size_t i = 1, count = instruction->InputCount(); i < count; i++) { |
| 169 | info = TransferPhi(LookupInfo(loop, instruction->InputAt(0)), |
| 170 | LookupInfo(loop, instruction->InputAt(i))); |
| 171 | } |
| 172 | } else if (instruction->IsAdd()) { |
| 173 | info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)), |
| 174 | LookupInfo(loop, instruction->InputAt(1)), kAdd); |
| 175 | } else if (instruction->IsSub()) { |
| 176 | info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)), |
| 177 | LookupInfo(loop, instruction->InputAt(1)), kSub); |
| 178 | } else if (instruction->IsMul()) { |
| 179 | info = TransferMul(LookupInfo(loop, instruction->InputAt(0)), |
| 180 | LookupInfo(loop, instruction->InputAt(1))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 181 | } else if (instruction->IsShl()) { |
| 182 | info = TransferShl(LookupInfo(loop, instruction->InputAt(0)), |
| 183 | LookupInfo(loop, instruction->InputAt(1)), |
| 184 | instruction->InputAt(0)->GetType()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 185 | } else if (instruction->IsNeg()) { |
| 186 | info = TransferNeg(LookupInfo(loop, instruction->InputAt(0))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 187 | } else if (instruction->IsBoundsCheck()) { |
| 188 | info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through. |
| 189 | } else if (instruction->IsTypeConversion()) { |
| 190 | HTypeConversion* conversion = instruction->AsTypeConversion(); |
| 191 | // TODO: accept different conversion scenarios. |
| 192 | if (conversion->GetResultType() == conversion->GetInputType()) { |
| 193 | info = LookupInfo(loop, conversion->GetInput()); |
| 194 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Successfully classified? |
| 198 | if (info != nullptr) { |
| 199 | AssignInfo(loop, instruction, info); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) { |
| 204 | const size_t size = scc_.size(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 205 | DCHECK_GE(size, 1u); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 206 | HInstruction* phi = scc_[size - 1]; |
| 207 | if (!IsEntryPhi(loop, phi)) { |
| 208 | return; |
| 209 | } |
| 210 | HInstruction* external = phi->InputAt(0); |
| 211 | HInstruction* internal = phi->InputAt(1); |
| 212 | InductionInfo* initial = LookupInfo(loop, external); |
| 213 | if (initial == nullptr || initial->induction_class != kInvariant) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | // Singleton entry-phi-operation may be a wrap-around induction. |
| 218 | if (size == 1) { |
| 219 | InductionInfo* update = LookupInfo(loop, internal); |
| 220 | if (update != nullptr) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 221 | AssignInfo(loop, phi, CreateInduction(kWrapAround, initial, update)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 222 | } |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // 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] | 227 | // temporary meaning to its nodes, seeded from the phi instruction and back. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 228 | for (size_t i = 0; i < size - 1; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 229 | HInstruction* instruction = scc_[i]; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 230 | InductionInfo* update = nullptr; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 231 | if (instruction->IsPhi()) { |
| 232 | update = SolvePhi(loop, phi, instruction); |
| 233 | } else if (instruction->IsAdd()) { |
| 234 | update = SolveAddSub( |
| 235 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true); |
| 236 | } else if (instruction->IsSub()) { |
| 237 | update = SolveAddSub( |
| 238 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 239 | } |
| 240 | if (update == nullptr) { |
| 241 | return; |
| 242 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 243 | cycle_.Put(instruction, update); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 246 | // Success if the internal link received a meaning. |
| 247 | auto it = cycle_.find(internal); |
| 248 | if (it != cycle_.end()) { |
| 249 | InductionInfo* induction = it->second; |
| 250 | switch (induction->induction_class) { |
| 251 | case kInvariant: |
| 252 | // Classify phi (last element in scc_) and then the rest of the cycle "on-demand". |
| 253 | // Statements are scanned in the Tarjan SCC order, with phi first. |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 254 | AssignInfo(loop, phi, CreateInduction(kLinear, induction, initial)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 255 | for (size_t i = 0; i < size - 1; i++) { |
| 256 | ClassifyTrivial(loop, scc_[i]); |
| 257 | } |
| 258 | break; |
| 259 | case kPeriodic: |
| 260 | // Classify all elements in the cycle with the found periodic induction while rotating |
| 261 | // each first element to the end. Lastly, phi (last element in scc_) is classified. |
| 262 | // Statements are scanned in the reverse Tarjan SCC order, with phi last. |
| 263 | for (size_t i = 2; i <= size; i++) { |
| 264 | AssignInfo(loop, scc_[size - i], induction); |
| 265 | induction = RotatePeriodicInduction(induction->op_b, induction->op_a); |
| 266 | } |
| 267 | AssignInfo(loop, phi, induction); |
| 268 | break; |
| 269 | default: |
| 270 | break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 275 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction( |
| 276 | InductionInfo* induction, |
| 277 | InductionInfo* last) { |
| 278 | // Rotates a periodic induction of the form |
| 279 | // (a, b, c, d, e) |
| 280 | // into |
| 281 | // (b, c, d, e, a) |
| 282 | // in preparation of assigning this to the previous variable in the sequence. |
| 283 | if (induction->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 284 | return CreateInduction(kPeriodic, induction, last); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 285 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 286 | return CreateInduction(kPeriodic, induction->op_a, RotatePeriodicInduction(induction->op_b, last)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 289 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(InductionInfo* a, |
| 290 | InductionInfo* b) { |
| 291 | // Transfer over a phi: if both inputs are identical, result is input. |
| 292 | if (InductionEqual(a, b)) { |
| 293 | return a; |
| 294 | } |
| 295 | return nullptr; |
| 296 | } |
| 297 | |
| 298 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a, |
| 299 | InductionInfo* b, |
| 300 | InductionOp op) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 301 | // Transfer over an addition or subtraction: any invariant, linear, wrap-around, or periodic |
| 302 | // can be combined with an invariant to yield a similar result. Even two linear inputs can |
| 303 | // be combined. All other combinations fail, however. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 304 | if (a != nullptr && b != nullptr) { |
| 305 | if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 306 | return CreateInvariantOp(op, a, b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 307 | } else if (a->induction_class == kLinear && b->induction_class == kLinear) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 308 | return CreateInduction( |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 309 | kLinear, TransferAddSub(a->op_a, b->op_a, op), TransferAddSub(a->op_b, b->op_b, op)); |
| 310 | } else if (a->induction_class == kInvariant) { |
| 311 | InductionInfo* new_a = b->op_a; |
| 312 | InductionInfo* new_b = TransferAddSub(a, b->op_b, op); |
| 313 | if (b->induction_class != kLinear) { |
| 314 | DCHECK(b->induction_class == kWrapAround || b->induction_class == kPeriodic); |
| 315 | new_a = TransferAddSub(a, new_a, op); |
| 316 | } else if (op == kSub) { // Negation required. |
| 317 | new_a = TransferNeg(new_a); |
| 318 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 319 | return CreateInduction(b->induction_class, new_a, new_b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 320 | } else if (b->induction_class == kInvariant) { |
| 321 | InductionInfo* new_a = a->op_a; |
| 322 | InductionInfo* new_b = TransferAddSub(a->op_b, b, op); |
| 323 | if (a->induction_class != kLinear) { |
| 324 | DCHECK(a->induction_class == kWrapAround || a->induction_class == kPeriodic); |
| 325 | new_a = TransferAddSub(new_a, b, op); |
| 326 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 327 | return CreateInduction(a->induction_class, new_a, new_b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | return nullptr; |
| 331 | } |
| 332 | |
| 333 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a, |
| 334 | InductionInfo* b) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 335 | // Transfer over a multiplication: any invariant, linear, wrap-around, or periodic |
| 336 | // can be multiplied with an invariant to yield a similar but multiplied result. |
| 337 | // Two non-invariant inputs cannot be multiplied, however. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 338 | if (a != nullptr && b != nullptr) { |
| 339 | if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 340 | return CreateInvariantOp(kMul, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 341 | } else if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 342 | 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] | 343 | } else if (b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 344 | 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] | 345 | } |
| 346 | } |
| 347 | return nullptr; |
| 348 | } |
| 349 | |
| 350 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferShl(InductionInfo* a, |
| 351 | InductionInfo* b, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 352 | Primitive::Type type) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 353 | // 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] | 354 | int64_t value = -1; |
| 355 | if (a != nullptr && IsIntAndGet(b, &value)) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 356 | // Obtain the constant needed for the multiplication. This yields an existing instruction |
| 357 | // if the constants is already there. Otherwise, this has a side effect on the HIR. |
| 358 | // The restriction on the shift factor avoids generating a negative constant |
| 359 | // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization |
| 360 | // 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] | 361 | if ((type == Primitive::kPrimInt && 0 <= value && value < 31) || |
| 362 | (type == Primitive::kPrimLong && 0 <= value && value < 63)) { |
| 363 | return TransferMul(a, CreateConstant(1 << value, type)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | return nullptr; |
| 367 | } |
| 368 | |
| 369 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 370 | // Transfer over a unary negation: an invariant, linear, wrap-around, or periodic input |
| 371 | // yields a similar but negated induction as result. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 372 | if (a != nullptr) { |
| 373 | if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 374 | return CreateInvariantOp(kNeg, nullptr, a); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 375 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 376 | 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] | 377 | } |
| 378 | return nullptr; |
| 379 | } |
| 380 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 381 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HLoopInformation* loop, |
| 382 | HInstruction* phi, |
| 383 | HInstruction* instruction) { |
| 384 | // Solve within a cycle over a phi: identical inputs are combined into that input as result. |
| 385 | const size_t count = instruction->InputCount(); |
| 386 | DCHECK_GT(count, 0u); |
| 387 | auto ita = cycle_.find(instruction->InputAt(0)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 388 | if (ita != cycle_.end()) { |
| 389 | InductionInfo* a = ita->second; |
| 390 | for (size_t i = 1; i < count; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 391 | auto itb = cycle_.find(instruction->InputAt(i)); |
| 392 | if (itb == cycle_.end() || !HInductionVarAnalysis::InductionEqual(a, itb->second)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 393 | return nullptr; |
| 394 | } |
| 395 | } |
| 396 | return a; |
| 397 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 398 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 399 | // Solve within a cycle over another entry-phi: add invariants into a periodic. |
| 400 | if (IsEntryPhi(loop, instruction)) { |
| 401 | InductionInfo* a = LookupInfo(loop, instruction->InputAt(0)); |
| 402 | if (a != nullptr && a->induction_class == kInvariant) { |
| 403 | if (instruction->InputAt(1) == phi) { |
| 404 | InductionInfo* initial = LookupInfo(loop, phi->InputAt(0)); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 405 | return CreateInduction(kPeriodic, a, initial); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 406 | } |
| 407 | auto it = cycle_.find(instruction->InputAt(1)); |
| 408 | if (it != cycle_.end()) { |
| 409 | InductionInfo* b = it->second; |
| 410 | if (b->induction_class == kPeriodic) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 411 | return CreateInduction(kPeriodic, a, b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 412 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 416 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 417 | return nullptr; |
| 418 | } |
| 419 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 420 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop, |
| 421 | HInstruction* phi, |
| 422 | HInstruction* instruction, |
| 423 | HInstruction* x, |
| 424 | HInstruction* y, |
| 425 | InductionOp op, |
| 426 | bool is_first_call) { |
| 427 | // Solve within a cycle over an addition or subtraction: adding or subtracting an |
| 428 | // invariant value, seeded from phi, keeps adding to the stride of the induction. |
| 429 | InductionInfo* b = LookupInfo(loop, y); |
| 430 | if (b != nullptr && b->induction_class == kInvariant) { |
| 431 | if (x == phi) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 432 | return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 433 | } |
| 434 | auto it = cycle_.find(x); |
| 435 | if (it != cycle_.end()) { |
| 436 | InductionInfo* a = it->second; |
| 437 | if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 438 | return CreateInvariantOp(op, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 439 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 442 | |
| 443 | // Try some alternatives before failing. |
| 444 | if (op == kAdd) { |
| 445 | // Try the other way around for an addition if considered for first time. |
| 446 | if (is_first_call) { |
| 447 | return SolveAddSub(loop, phi, instruction, y, x, op, false); |
| 448 | } |
| 449 | } else if (op == kSub) { |
| 450 | // Solve within a tight cycle for a periodic idiom k = c - k; |
| 451 | if (y == phi && instruction == phi->InputAt(1)) { |
| 452 | InductionInfo* a = LookupInfo(loop, x); |
| 453 | if (a != nullptr && a->induction_class == kInvariant) { |
| 454 | InductionInfo* initial = LookupInfo(loop, phi->InputAt(0)); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 455 | return CreateInduction(kPeriodic, CreateInvariantOp(kSub, a, initial), initial); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 460 | return nullptr; |
| 461 | } |
| 462 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 463 | void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) { |
| 464 | HInstruction* control = loop->GetHeader()->GetLastInstruction(); |
| 465 | if (control->IsIf()) { |
| 466 | HIf* ifs = control->AsIf(); |
| 467 | HBasicBlock* if_true = ifs->IfTrueSuccessor(); |
| 468 | HBasicBlock* if_false = ifs->IfFalseSuccessor(); |
| 469 | HInstruction* if_expr = ifs->InputAt(0); |
| 470 | // Determine if loop has following structure in header. |
| 471 | // loop-header: .... |
| 472 | // if (condition) goto X |
| 473 | if (if_expr->IsCondition()) { |
| 474 | HCondition* condition = if_expr->AsCondition(); |
| 475 | InductionInfo* a = LookupInfo(loop, condition->InputAt(0)); |
| 476 | InductionInfo* b = LookupInfo(loop, condition->InputAt(1)); |
| 477 | Primitive::Type type = condition->InputAt(0)->GetType(); |
| 478 | // Determine if the loop control uses integral arithmetic and an if-exit (X outside) or an |
| 479 | // if-iterate (X inside), always expressed as if-iterate when passing into VisitCondition(). |
| 480 | if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) { |
| 481 | // Loop control is not 32/64-bit integral. |
| 482 | } else if (a == nullptr || b == nullptr) { |
| 483 | // Loop control is not a sequence. |
| 484 | } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) { |
| 485 | VisitCondition(loop, a, b, type, condition->GetOppositeCondition()); |
| 486 | } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) { |
| 487 | VisitCondition(loop, a, b, type, condition->GetCondition()); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop, |
| 494 | InductionInfo* a, |
| 495 | InductionInfo* b, |
| 496 | Primitive::Type type, |
| 497 | IfCondition cmp) { |
| 498 | if (a->induction_class == kInvariant && b->induction_class == kLinear) { |
| 499 | // Swap conditions (e.g. U > i is same as i < U). |
| 500 | switch (cmp) { |
| 501 | case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break; |
| 502 | case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break; |
| 503 | case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break; |
| 504 | case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break; |
| 505 | default: break; |
| 506 | } |
| 507 | } else if (a->induction_class == kLinear && b->induction_class == kInvariant) { |
| 508 | // Normalize a linear loop control with a constant, nonzero stride: |
| 509 | // stride > 0, either i < U or i <= U |
| 510 | // stride < 0, either i > U or i >= U |
| 511 | InductionInfo* stride = a->op_a; |
| 512 | InductionInfo* lo_val = a->op_b; |
| 513 | InductionInfo* hi_val = b; |
| 514 | int64_t value = -1; |
| 515 | if (IsIntAndGet(stride, &value)) { |
| 516 | if ((value > 0 && (cmp == kCondLT || cmp == kCondLE)) || |
| 517 | (value < 0 && (cmp == kCondGT || cmp == kCondGE))) { |
| 518 | bool is_strict = cmp == kCondLT || cmp == kCondGT; |
| 519 | VisitTripCount(loop, lo_val, hi_val, stride, value, type, is_strict); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop, |
| 526 | InductionInfo* lo_val, |
| 527 | InductionInfo* hi_val, |
| 528 | InductionInfo* stride, |
| 529 | int32_t stride_value, |
| 530 | Primitive::Type type, |
| 531 | bool is_strict) { |
| 532 | // Any loop of the general form: |
| 533 | // |
| 534 | // for (i = L; i <= U; i += S) // S > 0 |
| 535 | // or for (i = L; i >= U; i += S) // S < 0 |
| 536 | // .. i .. |
| 537 | // |
| 538 | // can be normalized into: |
| 539 | // |
| 540 | // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S |
| 541 | // .. L + S * n .. |
| 542 | // |
| 543 | // NOTE: The TC (trip-count) expression is only valid if the top-test path is taken at |
| 544 | // least once. Otherwise TC is 0. Also, the expression assumes the loop does not |
| 545 | // have any early-exits. Otherwise, TC is an upper bound. |
| 546 | // |
| 547 | bool cancels = is_strict && abs(stride_value) == 1; // compensation cancels conversion? |
| 548 | if (!cancels) { |
| 549 | // Convert exclusive integral inequality into inclusive integral inequality, |
| 550 | // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1. |
| 551 | if (is_strict) { |
| 552 | const InductionOp op = stride_value > 0 ? kSub : kAdd; |
| 553 | hi_val = CreateInvariantOp(op, hi_val, CreateConstant(1, type)); |
| 554 | } |
| 555 | // Compensate for stride. |
| 556 | hi_val = CreateInvariantOp(kAdd, hi_val, stride); |
| 557 | } |
| 558 | |
| 559 | // Assign the trip-count expression to the loop control. Clients that use the information |
| 560 | // should be aware that due to the L <= U assumption, the expression is only valid in the |
| 561 | // loop-body proper, and not yet in the loop-header. If the loop has any early exits, the |
| 562 | // trip-count forms a conservative upper bound on the number of loop iterations. |
| 563 | InductionInfo* trip_count = |
| 564 | CreateInvariantOp(kDiv, CreateInvariantOp(kSub, hi_val, lo_val), stride); |
| 565 | AssignInfo(loop, loop->GetHeader()->GetLastInstruction(), trip_count); |
| 566 | } |
| 567 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 568 | void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop, |
| 569 | HInstruction* instruction, |
| 570 | InductionInfo* info) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 571 | auto it = induction_.find(loop); |
| 572 | if (it == induction_.end()) { |
| 573 | it = induction_.Put(loop, |
| 574 | ArenaSafeMap<HInstruction*, InductionInfo*>( |
| 575 | std::less<HInstruction*>(), graph_->GetArena()->Adapter())); |
| 576 | } |
| 577 | it->second.Put(instruction, info); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 580 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop, |
| 581 | HInstruction* instruction) { |
| 582 | auto it = induction_.find(loop); |
| 583 | if (it != induction_.end()) { |
| 584 | auto loop_it = it->second.find(instruction); |
| 585 | if (loop_it != it->second.end()) { |
| 586 | return loop_it->second; |
| 587 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 588 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 589 | if (IsLoopInvariant(loop, instruction)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 590 | InductionInfo* info = CreateInvariantFetch(instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 591 | AssignInfo(loop, instruction, info); |
| 592 | return info; |
| 593 | } |
| 594 | return nullptr; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 597 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value, |
| 598 | Primitive::Type type) { |
| 599 | if (type == Primitive::kPrimInt) { |
| 600 | return CreateInvariantFetch(graph_->GetIntConstant(value)); |
| 601 | } |
| 602 | DCHECK_EQ(type, Primitive::kPrimLong); |
| 603 | return CreateInvariantFetch(graph_->GetLongConstant(value)); |
| 604 | } |
| 605 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 606 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant( |
| 607 | InductionOp op, |
| 608 | InductionInfo* a, |
| 609 | InductionInfo* b) { |
| 610 | // Perform some light-weight simplifications during construction of a new invariant. |
| 611 | // This often safes memory and yields a more concise representation of the induction. |
| 612 | // More exhaustive simplifications are done by later phases once induction nodes are |
| 613 | // translated back into HIR code (e.g. by loop optimizations or BCE). |
| 614 | int64_t value = -1; |
| 615 | if (IsIntAndGet(a, &value)) { |
| 616 | if (value == 0) { |
| 617 | // Simplify 0 + b = b, 0 * b = 0. |
| 618 | if (op == kAdd) { |
| 619 | return b; |
| 620 | } else if (op == kMul) { |
| 621 | return a; |
| 622 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 623 | } else if (op == kMul) { |
| 624 | // Simplify 1 * b = b, -1 * b = -b |
| 625 | if (value == 1) { |
| 626 | return b; |
| 627 | } else if (value == -1) { |
| 628 | op = kNeg; |
| 629 | a = nullptr; |
| 630 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | if (IsIntAndGet(b, &value)) { |
| 634 | if (value == 0) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 635 | // Simplify a + 0 = a, a - 0 = a, a * 0 = 0, -0 = 0. |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 636 | if (op == kAdd || op == kSub) { |
| 637 | return a; |
| 638 | } else if (op == kMul || op == kNeg) { |
| 639 | return b; |
| 640 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 641 | } else if (op == kMul || op == kDiv) { |
| 642 | // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a |
| 643 | if (value == 1) { |
| 644 | return a; |
| 645 | } else if (value == -1) { |
| 646 | op = kNeg; |
| 647 | b = a; |
| 648 | a = nullptr; |
| 649 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 650 | } |
| 651 | } else if (b->operation == kNeg) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 652 | // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b. |
| 653 | if (op == kAdd) { |
| 654 | op = kSub; |
| 655 | b = b->op_b; |
| 656 | } else if (op == kSub) { |
| 657 | op = kAdd; |
| 658 | b = b->op_b; |
| 659 | } else if (op == kNeg) { |
| 660 | return b->op_b; |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr); |
| 664 | } |
| 665 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 666 | bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1, |
| 667 | InductionInfo* info2) { |
| 668 | // Test structural equality only, without accounting for simplifications. |
| 669 | if (info1 != nullptr && info2 != nullptr) { |
| 670 | return |
| 671 | info1->induction_class == info2->induction_class && |
| 672 | info1->operation == info2->operation && |
| 673 | info1->fetch == info2->fetch && |
| 674 | InductionEqual(info1->op_a, info2->op_a) && |
| 675 | InductionEqual(info1->op_b, info2->op_b); |
| 676 | } |
| 677 | // Otherwise only two nullptrs are considered equal. |
| 678 | return info1 == info2; |
| 679 | } |
| 680 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 681 | bool HInductionVarAnalysis::IsIntAndGet(InductionInfo* info, int64_t* value) { |
| 682 | if (info != nullptr && info->induction_class == kInvariant && info->operation == kFetch) { |
| 683 | DCHECK(info->fetch); |
| 684 | if (info->fetch->IsIntConstant()) { |
| 685 | *value = info->fetch->AsIntConstant()->GetValue(); |
| 686 | return true; |
| 687 | } else if (info->fetch->IsLongConstant()) { |
| 688 | *value = info->fetch->AsLongConstant()->GetValue(); |
| 689 | return true; |
| 690 | } |
| 691 | } |
| 692 | return false; |
| 693 | } |
| 694 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 695 | std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) { |
| 696 | if (info != nullptr) { |
| 697 | if (info->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 698 | int64_t value = -1; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 699 | std::string inv = "("; |
| 700 | inv += InductionToString(info->op_a); |
| 701 | switch (info->operation) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 702 | case kNop: inv += " @ "; break; |
| 703 | case kAdd: inv += " + "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 704 | case kSub: |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 705 | case kNeg: inv += " - "; break; |
| 706 | case kMul: inv += " * "; break; |
| 707 | case kDiv: inv += " / "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 708 | case kFetch: |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 709 | DCHECK(info->fetch); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 710 | if (IsIntAndGet(info, &value)) { |
| 711 | inv += std::to_string(value); |
| 712 | } else { |
| 713 | inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName(); |
| 714 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 715 | break; |
| 716 | } |
| 717 | inv += InductionToString(info->op_b); |
| 718 | return inv + ")"; |
| 719 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 720 | DCHECK(info->operation == kNop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 721 | if (info->induction_class == kLinear) { |
| 722 | return "(" + InductionToString(info->op_a) + " * i + " + |
| 723 | InductionToString(info->op_b) + ")"; |
| 724 | } else if (info->induction_class == kWrapAround) { |
| 725 | return "wrap(" + InductionToString(info->op_a) + ", " + |
| 726 | InductionToString(info->op_b) + ")"; |
| 727 | } else if (info->induction_class == kPeriodic) { |
| 728 | return "periodic(" + InductionToString(info->op_a) + ", " + |
| 729 | InductionToString(info->op_b) + ")"; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | return ""; |
| 734 | } |
| 735 | |
| 736 | } // namespace art |