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