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