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