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