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 | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 559 | InductionInfo* stride = a->op_a; |
| 560 | InductionInfo* lo_val = a->op_b; |
| 561 | InductionInfo* hi_val = b; |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 562 | // Analyze stride (may be compound). |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 563 | InductionVarRange::Value v1 = InductionVarRange::GetVal(stride, nullptr, /* is_min */ true); |
| 564 | InductionVarRange::Value v2 = InductionVarRange::GetVal(stride, nullptr, /* is_min */ false); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 565 | if (v1.a_constant != 0 || v2.a_constant != 0 || v1.b_constant != v2.b_constant) { |
| 566 | return; |
| 567 | } |
| 568 | // Rewrite safe condition i != U with unit stride into i < U or i > U |
| 569 | // (unit stride guarantees that the end condition is always reached). |
| 570 | const int32_t stride_value = v1.b_constant; |
| 571 | int64_t lo_value = 0; |
| 572 | int64_t hi_value = 0; |
| 573 | if (cmp == kCondNE && IsIntAndGet(lo_val, &lo_value) && IsIntAndGet(hi_val, &hi_value)) { |
| 574 | if ((stride_value == +1 && lo_value < hi_value) || |
| 575 | (stride_value == -1 && lo_value > hi_value)) { |
| 576 | cmp = stride_value > 0 ? kCondLT : kCondGT; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 579 | // Normalize a linear loop control with a nonzero stride: |
| 580 | // stride > 0, either i < U or i <= U |
| 581 | // stride < 0, either i > U or i >= U |
| 582 | // |
| 583 | // TODO: construct conditions for constant/symbolic safety of trip-count |
| 584 | // |
| 585 | if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) || |
| 586 | (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) { |
| 587 | VisitTripCount(loop, lo_val, hi_val, stride, stride_value, type, cmp); |
| 588 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop, |
| 593 | InductionInfo* lo_val, |
| 594 | InductionInfo* hi_val, |
| 595 | InductionInfo* stride, |
| 596 | int32_t stride_value, |
| 597 | Primitive::Type type, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 598 | IfCondition cmp) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 599 | // Any loop of the general form: |
| 600 | // |
| 601 | // for (i = L; i <= U; i += S) // S > 0 |
| 602 | // or for (i = L; i >= U; i += S) // S < 0 |
| 603 | // .. i .. |
| 604 | // |
| 605 | // can be normalized into: |
| 606 | // |
| 607 | // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S |
| 608 | // .. L + S * n .. |
| 609 | // |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 610 | // NOTE: The TC (trip-count) expression is only valid when safe. Otherwise TC is 0 |
| 611 | // (or possibly infinite). Also, the expression assumes the loop does not have |
| 612 | // early-exits. Otherwise, TC is an upper bound. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 613 | // |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 614 | bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 615 | if (!cancels) { |
| 616 | // Convert exclusive integral inequality into inclusive integral inequality, |
| 617 | // 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] | 618 | if (cmp == kCondLT) { |
| 619 | hi_val = CreateInvariantOp(kSub, hi_val, CreateConstant(1, type)); |
| 620 | } else if (cmp == kCondGT) { |
| 621 | hi_val = CreateInvariantOp(kAdd, hi_val, CreateConstant(1, type)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 622 | } |
| 623 | // Compensate for stride. |
| 624 | hi_val = CreateInvariantOp(kAdd, hi_val, stride); |
| 625 | } |
| 626 | |
| 627 | // Assign the trip-count expression to the loop control. Clients that use the information |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 628 | // should be aware that the expression is only valid in the loop-body proper (when symbolically |
| 629 | // safe), and not yet in the loop-header (unless constant safe). If the loop has any early exits, |
| 630 | // the trip-count forms a conservative upper bound on the number of loop iterations. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 631 | InductionInfo* trip_count = |
| 632 | CreateInvariantOp(kDiv, CreateInvariantOp(kSub, hi_val, lo_val), stride); |
| 633 | AssignInfo(loop, loop->GetHeader()->GetLastInstruction(), trip_count); |
| 634 | } |
| 635 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 636 | void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop, |
| 637 | HInstruction* instruction, |
| 638 | InductionInfo* info) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 639 | auto it = induction_.find(loop); |
| 640 | if (it == induction_.end()) { |
| 641 | it = induction_.Put(loop, |
| 642 | ArenaSafeMap<HInstruction*, InductionInfo*>( |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame^] | 643 | std::less<HInstruction*>(), |
| 644 | graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 645 | } |
| 646 | it->second.Put(instruction, info); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 649 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop, |
| 650 | HInstruction* instruction) { |
| 651 | auto it = induction_.find(loop); |
| 652 | if (it != induction_.end()) { |
| 653 | auto loop_it = it->second.find(instruction); |
| 654 | if (loop_it != it->second.end()) { |
| 655 | return loop_it->second; |
| 656 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 657 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 658 | if (IsLoopInvariant(loop, instruction)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 659 | InductionInfo* info = CreateInvariantFetch(instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 660 | AssignInfo(loop, instruction, info); |
| 661 | return info; |
| 662 | } |
| 663 | return nullptr; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 666 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value, |
| 667 | Primitive::Type type) { |
| 668 | if (type == Primitive::kPrimInt) { |
| 669 | return CreateInvariantFetch(graph_->GetIntConstant(value)); |
| 670 | } |
| 671 | DCHECK_EQ(type, Primitive::kPrimLong); |
| 672 | return CreateInvariantFetch(graph_->GetLongConstant(value)); |
| 673 | } |
| 674 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 675 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant( |
| 676 | InductionOp op, |
| 677 | InductionInfo* a, |
| 678 | InductionInfo* b) { |
| 679 | // Perform some light-weight simplifications during construction of a new invariant. |
| 680 | // This often safes memory and yields a more concise representation of the induction. |
| 681 | // More exhaustive simplifications are done by later phases once induction nodes are |
| 682 | // translated back into HIR code (e.g. by loop optimizations or BCE). |
| 683 | int64_t value = -1; |
| 684 | if (IsIntAndGet(a, &value)) { |
| 685 | if (value == 0) { |
| 686 | // Simplify 0 + b = b, 0 * b = 0. |
| 687 | if (op == kAdd) { |
| 688 | return b; |
| 689 | } else if (op == kMul) { |
| 690 | return a; |
| 691 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 692 | } else if (op == kMul) { |
| 693 | // Simplify 1 * b = b, -1 * b = -b |
| 694 | if (value == 1) { |
| 695 | return b; |
| 696 | } else if (value == -1) { |
| 697 | op = kNeg; |
| 698 | a = nullptr; |
| 699 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | if (IsIntAndGet(b, &value)) { |
| 703 | if (value == 0) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 704 | // Simplify a + 0 = a, a - 0 = a, a * 0 = 0, -0 = 0. |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 705 | if (op == kAdd || op == kSub) { |
| 706 | return a; |
| 707 | } else if (op == kMul || op == kNeg) { |
| 708 | return b; |
| 709 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 710 | } else if (op == kMul || op == kDiv) { |
| 711 | // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a |
| 712 | if (value == 1) { |
| 713 | return a; |
| 714 | } else if (value == -1) { |
| 715 | op = kNeg; |
| 716 | b = a; |
| 717 | a = nullptr; |
| 718 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 719 | } |
| 720 | } else if (b->operation == kNeg) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 721 | // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b. |
| 722 | if (op == kAdd) { |
| 723 | op = kSub; |
| 724 | b = b->op_b; |
| 725 | } else if (op == kSub) { |
| 726 | op = kAdd; |
| 727 | b = b->op_b; |
| 728 | } else if (op == kNeg) { |
| 729 | return b->op_b; |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr); |
| 733 | } |
| 734 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 735 | bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1, |
| 736 | InductionInfo* info2) { |
| 737 | // Test structural equality only, without accounting for simplifications. |
| 738 | if (info1 != nullptr && info2 != nullptr) { |
| 739 | return |
| 740 | info1->induction_class == info2->induction_class && |
| 741 | info1->operation == info2->operation && |
| 742 | info1->fetch == info2->fetch && |
| 743 | InductionEqual(info1->op_a, info2->op_a) && |
| 744 | InductionEqual(info1->op_b, info2->op_b); |
| 745 | } |
| 746 | // Otherwise only two nullptrs are considered equal. |
| 747 | return info1 == info2; |
| 748 | } |
| 749 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 750 | bool HInductionVarAnalysis::IsIntAndGet(InductionInfo* info, int64_t* value) { |
| 751 | if (info != nullptr && info->induction_class == kInvariant && info->operation == kFetch) { |
| 752 | DCHECK(info->fetch); |
| 753 | if (info->fetch->IsIntConstant()) { |
| 754 | *value = info->fetch->AsIntConstant()->GetValue(); |
| 755 | return true; |
| 756 | } else if (info->fetch->IsLongConstant()) { |
| 757 | *value = info->fetch->AsLongConstant()->GetValue(); |
| 758 | return true; |
| 759 | } |
| 760 | } |
| 761 | return false; |
| 762 | } |
| 763 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 764 | std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) { |
| 765 | if (info != nullptr) { |
| 766 | if (info->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 767 | int64_t value = -1; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 768 | std::string inv = "("; |
| 769 | inv += InductionToString(info->op_a); |
| 770 | switch (info->operation) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 771 | case kNop: inv += " @ "; break; |
| 772 | case kAdd: inv += " + "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 773 | case kSub: |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 774 | case kNeg: inv += " - "; break; |
| 775 | case kMul: inv += " * "; break; |
| 776 | case kDiv: inv += " / "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 777 | case kFetch: |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 778 | DCHECK(info->fetch); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 779 | if (IsIntAndGet(info, &value)) { |
| 780 | inv += std::to_string(value); |
| 781 | } else { |
| 782 | inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName(); |
| 783 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | inv += InductionToString(info->op_b); |
| 787 | return inv + ")"; |
| 788 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 789 | DCHECK(info->operation == kNop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 790 | if (info->induction_class == kLinear) { |
| 791 | return "(" + InductionToString(info->op_a) + " * i + " + |
| 792 | InductionToString(info->op_b) + ")"; |
| 793 | } else if (info->induction_class == kWrapAround) { |
| 794 | return "wrap(" + InductionToString(info->op_a) + ", " + |
| 795 | InductionToString(info->op_b) + ")"; |
| 796 | } else if (info->induction_class == kPeriodic) { |
| 797 | return "periodic(" + InductionToString(info->op_a) + ", " + |
| 798 | InductionToString(info->op_b) + ")"; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | return ""; |
| 803 | } |
| 804 | |
| 805 | } // namespace art |