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)), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 90 | map_(std::less<HInstruction*>(), |
| 91 | graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)), |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 92 | scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 93 | cycle_(std::less<HInstruction*>(), |
| 94 | graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)), |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 95 | type_(Primitive::kPrimVoid), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 96 | induction_(std::less<HLoopInformation*>(), |
| 97 | graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void HInductionVarAnalysis::Run() { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 101 | // Detects sequence variables (generalized induction variables) during an outer to inner |
| 102 | // traversal of all loops using Gerlek's algorithm. The order is important to enable |
| 103 | // range analysis on outer loop while visiting inner loops. |
| 104 | for (HReversePostOrderIterator it_graph(*graph_); !it_graph.Done(); it_graph.Advance()) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 105 | HBasicBlock* graph_block = it_graph.Current(); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 106 | // Don't analyze irreducible loops. |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 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) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 124 | continue; // Inner loops visited later. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 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 | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 288 | } else if (instruction->IsXor()) { |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame^] | 289 | update = SolveXor(loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1)); |
| 290 | } else if (instruction->IsEqual()) { |
| 291 | update = SolveTest(loop, phi, instruction, 0); |
| 292 | } else if (instruction->IsNotEqual()) { |
| 293 | update = SolveTest(loop, phi, instruction, 1); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 294 | } else if (instruction->IsTypeConversion()) { |
| 295 | update = SolveCnv(instruction->AsTypeConversion()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 296 | } |
| 297 | if (update == nullptr) { |
| 298 | return; |
| 299 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 300 | cycle_.Put(instruction, update); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 303 | // Success if all internal links received the same temporary meaning. |
| 304 | InductionInfo* induction = SolvePhi(phi, /* input_index */ 1); |
| 305 | if (induction != nullptr) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 306 | switch (induction->induction_class) { |
| 307 | case kInvariant: |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 308 | // Classify first phi and then the rest of the cycle "on-demand". |
| 309 | // Statements are scanned in order. |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 310 | AssignInfo(loop, phi, CreateInduction(kLinear, induction, initial, type_)); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 311 | for (size_t i = 1; i < size; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 312 | ClassifyTrivial(loop, scc_[i]); |
| 313 | } |
| 314 | break; |
| 315 | case kPeriodic: |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 316 | // Classify all elements in the cycle with the found periodic induction while |
| 317 | // rotating each first element to the end. Lastly, phi is classified. |
| 318 | // Statements are scanned in reverse order. |
| 319 | for (size_t i = size - 1; i >= 1; i--) { |
| 320 | AssignInfo(loop, scc_[i], induction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 321 | induction = RotatePeriodicInduction(induction->op_b, induction->op_a); |
| 322 | } |
| 323 | AssignInfo(loop, phi, induction); |
| 324 | break; |
| 325 | default: |
| 326 | break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 331 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction( |
| 332 | InductionInfo* induction, |
| 333 | InductionInfo* last) { |
| 334 | // Rotates a periodic induction of the form |
| 335 | // (a, b, c, d, e) |
| 336 | // into |
| 337 | // (b, c, d, e, a) |
| 338 | // in preparation of assigning this to the previous variable in the sequence. |
| 339 | if (induction->induction_class == kInvariant) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 340 | return CreateInduction(kPeriodic, induction, last, type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 341 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 342 | return CreateInduction( |
| 343 | kPeriodic, induction->op_a, RotatePeriodicInduction(induction->op_b, last), type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 346 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop, |
| 347 | HInstruction* phi, |
| 348 | size_t input_index) { |
| 349 | // Match all phi inputs from input_index onwards exactly. |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 350 | HInputsRef inputs = phi->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 351 | DCHECK_LT(input_index, inputs.size()); |
| 352 | InductionInfo* a = LookupInfo(loop, inputs[input_index]); |
| 353 | for (size_t i = input_index + 1; i < inputs.size(); i++) { |
| 354 | InductionInfo* b = LookupInfo(loop, inputs[i]); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 355 | if (!InductionEqual(a, b)) { |
| 356 | return nullptr; |
| 357 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 358 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 359 | return a; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a, |
| 363 | InductionInfo* b, |
| 364 | InductionOp op) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 365 | // Transfer over an addition or subtraction: any invariant, linear, wrap-around, or periodic |
| 366 | // can be combined with an invariant to yield a similar result. Even two linear inputs can |
| 367 | // be combined. All other combinations fail, however. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 368 | if (a != nullptr && b != nullptr) { |
| 369 | if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 370 | return CreateInvariantOp(op, a, b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 371 | } else if (a->induction_class == kLinear && b->induction_class == kLinear) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 372 | return CreateInduction(kLinear, |
| 373 | TransferAddSub(a->op_a, b->op_a, op), |
| 374 | TransferAddSub(a->op_b, b->op_b, op), |
| 375 | type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 376 | } else if (a->induction_class == kInvariant) { |
| 377 | InductionInfo* new_a = b->op_a; |
| 378 | InductionInfo* new_b = TransferAddSub(a, b->op_b, op); |
| 379 | if (b->induction_class != kLinear) { |
| 380 | DCHECK(b->induction_class == kWrapAround || b->induction_class == kPeriodic); |
| 381 | new_a = TransferAddSub(a, new_a, op); |
| 382 | } else if (op == kSub) { // Negation required. |
| 383 | new_a = TransferNeg(new_a); |
| 384 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 385 | return CreateInduction(b->induction_class, new_a, new_b, type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 386 | } else if (b->induction_class == kInvariant) { |
| 387 | InductionInfo* new_a = a->op_a; |
| 388 | InductionInfo* new_b = TransferAddSub(a->op_b, b, op); |
| 389 | if (a->induction_class != kLinear) { |
| 390 | DCHECK(a->induction_class == kWrapAround || a->induction_class == kPeriodic); |
| 391 | new_a = TransferAddSub(new_a, b, op); |
| 392 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 393 | return CreateInduction(a->induction_class, new_a, new_b, type_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | return nullptr; |
| 397 | } |
| 398 | |
| 399 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a, |
| 400 | InductionInfo* b) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 401 | // Transfer over a multiplication: any invariant, linear, wrap-around, or periodic |
| 402 | // can be multiplied with an invariant to yield a similar but multiplied result. |
| 403 | // Two non-invariant inputs cannot be multiplied, however. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 404 | if (a != nullptr && b != nullptr) { |
| 405 | if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 406 | return CreateInvariantOp(kMul, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 407 | } else if (a->induction_class == kInvariant) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 408 | return CreateInduction(b->induction_class, |
| 409 | TransferMul(a, b->op_a), |
| 410 | TransferMul(a, b->op_b), |
| 411 | type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 412 | } else if (b->induction_class == kInvariant) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 413 | return CreateInduction(a->induction_class, |
| 414 | TransferMul(a->op_a, b), |
| 415 | TransferMul(a->op_b, b), |
| 416 | type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | return nullptr; |
| 420 | } |
| 421 | |
| 422 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferShl(InductionInfo* a, |
| 423 | InductionInfo* b, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 424 | Primitive::Type type) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 425 | // 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] | 426 | int64_t value = -1; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 427 | if (a != nullptr && IsExact(b, &value)) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 428 | // Obtain the constant needed for the multiplication. This yields an existing instruction |
| 429 | // if the constants is already there. Otherwise, this has a side effect on the HIR. |
| 430 | // The restriction on the shift factor avoids generating a negative constant |
| 431 | // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization |
| 432 | // 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] | 433 | if ((type == Primitive::kPrimInt && 0 <= value && value < 31) || |
| 434 | (type == Primitive::kPrimLong && 0 <= value && value < 63)) { |
| 435 | return TransferMul(a, CreateConstant(1 << value, type)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | return nullptr; |
| 439 | } |
| 440 | |
| 441 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 442 | // Transfer over a unary negation: an invariant, linear, wrap-around, or periodic input |
| 443 | // yields a similar but negated induction as result. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 444 | if (a != nullptr) { |
| 445 | if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 446 | return CreateInvariantOp(kNeg, nullptr, a); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 447 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 448 | return CreateInduction(a->induction_class, TransferNeg(a->op_a), TransferNeg(a->op_b), type_); |
| 449 | } |
| 450 | return nullptr; |
| 451 | } |
| 452 | |
| 453 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferCnv(InductionInfo* a, |
| 454 | Primitive::Type from, |
| 455 | Primitive::Type to) { |
| 456 | if (a != nullptr) { |
| 457 | // Allow narrowing conversion in certain cases. |
| 458 | if (IsNarrowingIntegralConversion(from, to)) { |
| 459 | if (a->induction_class == kLinear) { |
| 460 | if (a->type == to || (a->type == from && IsNarrowingIntegralConversion(from, to))) { |
| 461 | return CreateInduction(kLinear, a->op_a, a->op_b, to); |
| 462 | } |
| 463 | } |
| 464 | // TODO: other cases useful too? |
| 465 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 466 | } |
| 467 | return nullptr; |
| 468 | } |
| 469 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 470 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi, |
| 471 | size_t input_index) { |
| 472 | // Match all phi inputs from input_index onwards exactly. |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 473 | HInputsRef inputs = phi->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 474 | DCHECK_LT(input_index, inputs.size()); |
| 475 | auto ita = cycle_.find(inputs[input_index]); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 476 | if (ita != cycle_.end()) { |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 477 | for (size_t i = input_index + 1; i < inputs.size(); i++) { |
| 478 | auto itb = cycle_.find(inputs[i]); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 479 | if (itb == cycle_.end() || |
| 480 | !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 481 | return nullptr; |
| 482 | } |
| 483 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 484 | return ita->second; |
| 485 | } |
| 486 | return nullptr; |
| 487 | } |
| 488 | |
| 489 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs( |
| 490 | HLoopInformation* loop, |
| 491 | HInstruction* entry_phi, |
| 492 | HInstruction* phi) { |
| 493 | // Match all phi inputs. |
| 494 | InductionInfo* match = SolvePhi(phi, /* input_index */ 0); |
| 495 | if (match != nullptr) { |
| 496 | return match; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 497 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 498 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 499 | // Otherwise, try to solve for a periodic seeded from phi onward. |
| 500 | // Only tight multi-statement cycles are considered in order to |
| 501 | // simplify rotating the periodic during the final classification. |
| 502 | if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) { |
| 503 | InductionInfo* a = LookupInfo(loop, phi->InputAt(0)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 504 | if (a != nullptr && a->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 505 | if (phi->InputAt(1) == entry_phi) { |
| 506 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 507 | return CreateInduction(kPeriodic, a, initial, type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 508 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 509 | InductionInfo* b = SolvePhi(phi, /* input_index */ 1); |
| 510 | if (b != nullptr && b->induction_class == kPeriodic) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 511 | return CreateInduction(kPeriodic, a, b, type_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 515 | return nullptr; |
| 516 | } |
| 517 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 518 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 519 | HInstruction* entry_phi, |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 520 | HInstruction* instruction, |
| 521 | HInstruction* x, |
| 522 | HInstruction* y, |
| 523 | InductionOp op, |
| 524 | bool is_first_call) { |
| 525 | // Solve within a cycle over an addition or subtraction: adding or subtracting an |
| 526 | // invariant value, seeded from phi, keeps adding to the stride of the induction. |
| 527 | InductionInfo* b = LookupInfo(loop, y); |
| 528 | if (b != nullptr && b->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 529 | if (x == entry_phi) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 530 | return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 531 | } |
| 532 | auto it = cycle_.find(x); |
| 533 | if (it != cycle_.end()) { |
| 534 | InductionInfo* a = it->second; |
| 535 | if (a->induction_class == kInvariant) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 536 | return CreateInvariantOp(op, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 537 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 540 | |
| 541 | // Try some alternatives before failing. |
| 542 | if (op == kAdd) { |
| 543 | // Try the other way around for an addition if considered for first time. |
| 544 | if (is_first_call) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 545 | return SolveAddSub(loop, entry_phi, instruction, y, x, op, false); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 546 | } |
| 547 | } else if (op == kSub) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 548 | // Solve within a tight cycle that is formed by exactly two instructions, |
| 549 | // one phi and one update, for a periodic idiom of the form k = c - k; |
| 550 | 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] | 551 | InductionInfo* a = LookupInfo(loop, x); |
| 552 | if (a != nullptr && a->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 553 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 554 | return CreateInduction(kPeriodic, CreateInvariantOp(kSub, a, initial), initial, type_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 559 | return nullptr; |
| 560 | } |
| 561 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 562 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveXor(HLoopInformation* loop, |
| 563 | HInstruction* entry_phi, |
| 564 | HInstruction* instruction, |
| 565 | HInstruction* x, |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame^] | 566 | HInstruction* y) { |
| 567 | // Solve within a tight cycle on x = c ^ x or x = x ^ c. |
| 568 | if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) { |
| 569 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
| 570 | InductionInfo* a = LookupInfo(loop, x); |
| 571 | if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) { |
| 572 | return CreateInduction(kPeriodic, CreateInvariantOp(kXor, a, initial), initial, type_); |
| 573 | } |
| 574 | InductionInfo* b = LookupInfo(loop, y); |
| 575 | if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 576 | return CreateInduction(kPeriodic, CreateInvariantOp(kXor, initial, b), initial, type_); |
| 577 | } |
| 578 | } |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame^] | 579 | return nullptr; |
| 580 | } |
| 581 | |
| 582 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(HLoopInformation* loop, |
| 583 | HInstruction* entry_phi, |
| 584 | HInstruction* instruction, |
| 585 | int64_t opposite_value) { |
| 586 | // Detect hidden XOR construction in tight cycles on x = (x == 0) or x = (x != 1). |
| 587 | int64_t value = -1; |
| 588 | HInstruction* x = instruction->InputAt(0); |
| 589 | HInstruction* y = instruction->InputAt(1); |
| 590 | if (IsExact(LookupInfo(loop, x), &value) && value == opposite_value) { |
| 591 | return SolveXor(loop, entry_phi, instruction, graph_->GetIntConstant(1), y); |
| 592 | } else if (IsExact(LookupInfo(loop, y), &value) && value == opposite_value) { |
| 593 | return SolveXor(loop, entry_phi, instruction, x, graph_->GetIntConstant(1)); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 594 | } |
| 595 | return nullptr; |
| 596 | } |
| 597 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 598 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveCnv(HTypeConversion* conversion) { |
| 599 | Primitive::Type from = conversion->GetInputType(); |
| 600 | Primitive::Type to = conversion->GetResultType(); |
| 601 | // A narrowing conversion is allowed within the cycle of a linear induction, provided that the |
| 602 | // narrowest encountered type is recorded with the induction to account for the precision loss. |
| 603 | if (IsNarrowingIntegralConversion(from, to)) { |
| 604 | auto it = cycle_.find(conversion->GetInput()); |
| 605 | if (it != cycle_.end() && it->second->induction_class == kInvariant) { |
| 606 | type_ = Narrowest(type_, to); |
| 607 | return it->second; |
| 608 | } |
| 609 | } |
| 610 | return nullptr; |
| 611 | } |
| 612 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 613 | void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) { |
| 614 | HInstruction* control = loop->GetHeader()->GetLastInstruction(); |
| 615 | if (control->IsIf()) { |
| 616 | HIf* ifs = control->AsIf(); |
| 617 | HBasicBlock* if_true = ifs->IfTrueSuccessor(); |
| 618 | HBasicBlock* if_false = ifs->IfFalseSuccessor(); |
| 619 | HInstruction* if_expr = ifs->InputAt(0); |
| 620 | // Determine if loop has following structure in header. |
| 621 | // loop-header: .... |
| 622 | // if (condition) goto X |
| 623 | if (if_expr->IsCondition()) { |
| 624 | HCondition* condition = if_expr->AsCondition(); |
| 625 | InductionInfo* a = LookupInfo(loop, condition->InputAt(0)); |
| 626 | InductionInfo* b = LookupInfo(loop, condition->InputAt(1)); |
| 627 | Primitive::Type type = condition->InputAt(0)->GetType(); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 628 | // Determine if the loop control uses a known sequence on an if-exit (X outside) or on |
| 629 | // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition(). |
| 630 | if (a == nullptr || b == nullptr) { |
| 631 | return; // Loop control is not a sequence. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 632 | } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) { |
| 633 | VisitCondition(loop, a, b, type, condition->GetOppositeCondition()); |
| 634 | } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) { |
| 635 | VisitCondition(loop, a, b, type, condition->GetCondition()); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop, |
| 642 | InductionInfo* a, |
| 643 | InductionInfo* b, |
| 644 | Primitive::Type type, |
| 645 | IfCondition cmp) { |
| 646 | if (a->induction_class == kInvariant && b->induction_class == kLinear) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 647 | // 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] | 648 | switch (cmp) { |
| 649 | case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break; |
| 650 | case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break; |
| 651 | case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break; |
| 652 | case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break; |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 653 | case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 654 | default: break; |
| 655 | } |
| 656 | } else if (a->induction_class == kLinear && b->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 657 | // Analyze condition with induction at left-hand-side (e.g. i < U). |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 658 | InductionInfo* lower_expr = a->op_b; |
| 659 | InductionInfo* upper_expr = b; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 660 | InductionInfo* stride_expr = a->op_a; |
| 661 | // Constant stride? |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 662 | int64_t stride_value = 0; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 663 | if (!IsExact(stride_expr, &stride_value)) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 664 | return; |
| 665 | } |
Aart Bik | 358af83 | 2016-02-24 14:17:53 -0800 | [diff] [blame] | 666 | // Rewrite condition i != U into strict end condition i < U or i > U if this end condition |
| 667 | // is reached exactly (tested by verifying if the loop has a unit stride and the non-strict |
| 668 | // condition would be always taken). |
| 669 | if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLE)) || |
| 670 | (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGE)))) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 671 | cmp = stride_value > 0 ? kCondLT : kCondGT; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 672 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 673 | // Only accept integral condition. A mismatch between the type of condition and the induction |
| 674 | // is only allowed if the, necessarily narrower, induction range fits the narrower control. |
| 675 | if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) { |
| 676 | return; // not integral |
| 677 | } else if (type != a->type && |
| 678 | !FitsNarrowerControl(lower_expr, upper_expr, stride_value, a->type, cmp)) { |
| 679 | return; // mismatched type |
| 680 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 681 | // Normalize a linear loop control with a nonzero stride: |
| 682 | // stride > 0, either i < U or i <= U |
| 683 | // stride < 0, either i > U or i >= U |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 684 | if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) || |
| 685 | (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 686 | VisitTripCount(loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 687 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 692 | InductionInfo* lower_expr, |
| 693 | InductionInfo* upper_expr, |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 694 | InductionInfo* stride_expr, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 695 | int64_t stride_value, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 696 | Primitive::Type type, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 697 | IfCondition cmp) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 698 | // Any loop of the general form: |
| 699 | // |
| 700 | // for (i = L; i <= U; i += S) // S > 0 |
| 701 | // or for (i = L; i >= U; i += S) // S < 0 |
| 702 | // .. i .. |
| 703 | // |
| 704 | // can be normalized into: |
| 705 | // |
| 706 | // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S |
| 707 | // .. L + S * n .. |
| 708 | // |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 709 | // taking the following into consideration: |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 710 | // |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 711 | // (1) Using the same precision, the TC (trip-count) expression should be interpreted as |
| 712 | // an unsigned entity, for example, as in the following loop that uses the full range: |
| 713 | // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX |
| 714 | // (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] | 715 | // for (int i = 12; i < U; i++) // TC = 0 when U <= 12 |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 716 | // 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] | 717 | // 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] | 718 | // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in: |
| 719 | // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX |
| 720 | // 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] | 721 | // with an explicit finite-test. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 722 | // (4) For loops which early-exits, the TC forms an upper bound, as in: |
| 723 | // for (int i = 0; i < 10 && ....; i++) // TC <= 10 |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 724 | InductionInfo* trip_count = upper_expr; |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 725 | const bool is_taken = IsTaken(lower_expr, upper_expr, cmp); |
| 726 | const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp); |
| 727 | const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 728 | if (!cancels) { |
| 729 | // Convert exclusive integral inequality into inclusive integral inequality, |
| 730 | // 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] | 731 | if (cmp == kCondLT) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 732 | trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type)); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 733 | } else if (cmp == kCondGT) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 734 | trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 735 | } |
| 736 | // Compensate for stride. |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 737 | trip_count = CreateInvariantOp(kAdd, trip_count, stride_expr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 738 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 739 | trip_count = CreateInvariantOp( |
| 740 | kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride_expr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 741 | // 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] | 742 | // 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] | 743 | InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 744 | if (is_taken && is_finite) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 745 | tcKind = kTripCountInLoop; // needs neither test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 746 | } else if (is_finite) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 747 | tcKind = kTripCountInBody; // needs taken-test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 748 | } else if (is_taken) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 749 | tcKind = kTripCountInLoopUnsafe; // needs finite-test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 750 | } |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 751 | InductionOp op = kNop; |
| 752 | switch (cmp) { |
| 753 | case kCondLT: op = kLT; break; |
| 754 | case kCondLE: op = kLE; break; |
| 755 | case kCondGT: op = kGT; break; |
| 756 | case kCondGE: op = kGE; break; |
| 757 | default: LOG(FATAL) << "CONDITION UNREACHABLE"; |
| 758 | } |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 759 | // Associate trip count with control instruction, rather than the condition (even |
| 760 | // though it's its use) since former provides a convenient use-free placeholder. |
| 761 | HInstruction* control = loop->GetHeader()->GetLastInstruction(); |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 762 | InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr); |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 763 | DCHECK(control->IsIf()); |
| 764 | AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr, |
| 768 | InductionInfo* upper_expr, |
| 769 | IfCondition cmp) { |
| 770 | int64_t lower_value; |
| 771 | int64_t upper_value; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 772 | switch (cmp) { |
| 773 | case kCondLT: |
| 774 | return IsAtMost(lower_expr, &lower_value) |
| 775 | && IsAtLeast(upper_expr, &upper_value) |
| 776 | && lower_value < upper_value; |
| 777 | case kCondLE: |
| 778 | return IsAtMost(lower_expr, &lower_value) |
| 779 | && IsAtLeast(upper_expr, &upper_value) |
| 780 | && lower_value <= upper_value; |
| 781 | case kCondGT: |
| 782 | return IsAtLeast(lower_expr, &lower_value) |
| 783 | && IsAtMost(upper_expr, &upper_value) |
| 784 | && lower_value > upper_value; |
| 785 | case kCondGE: |
| 786 | return IsAtLeast(lower_expr, &lower_value) |
| 787 | && IsAtMost(upper_expr, &upper_value) |
| 788 | && lower_value >= upper_value; |
| 789 | default: |
| 790 | LOG(FATAL) << "CONDITION UNREACHABLE"; |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 791 | } |
| 792 | return false; // not certain, may be untaken |
| 793 | } |
| 794 | |
| 795 | bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr, |
| 796 | int64_t stride_value, |
| 797 | Primitive::Type type, |
| 798 | IfCondition cmp) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 799 | const int64_t min = Primitive::MinValueOfIntegralType(type); |
| 800 | const int64_t max = Primitive::MaxValueOfIntegralType(type); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 801 | // Some rules under which it is certain at compile-time that the loop is finite. |
| 802 | int64_t value; |
| 803 | switch (cmp) { |
| 804 | case kCondLT: |
| 805 | return stride_value == 1 || |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 806 | (IsAtMost(upper_expr, &value) && value <= (max - stride_value + 1)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 807 | case kCondLE: |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 808 | return (IsAtMost(upper_expr, &value) && value <= (max - stride_value)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 809 | case kCondGT: |
| 810 | return stride_value == -1 || |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 811 | (IsAtLeast(upper_expr, &value) && value >= (min - stride_value - 1)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 812 | case kCondGE: |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 813 | return (IsAtLeast(upper_expr, &value) && value >= (min - stride_value)); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 814 | default: |
| 815 | LOG(FATAL) << "CONDITION UNREACHABLE"; |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 816 | } |
| 817 | return false; // not certain, may be infinite |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 820 | bool HInductionVarAnalysis::FitsNarrowerControl(InductionInfo* lower_expr, |
| 821 | InductionInfo* upper_expr, |
| 822 | int64_t stride_value, |
| 823 | Primitive::Type type, |
| 824 | IfCondition cmp) { |
| 825 | int64_t min = Primitive::MinValueOfIntegralType(type); |
| 826 | int64_t max = Primitive::MaxValueOfIntegralType(type); |
| 827 | // Inclusive test need one extra. |
| 828 | if (stride_value != 1 && stride_value != -1) { |
| 829 | return false; // non-unit stride |
| 830 | } else if (cmp == kCondLE) { |
| 831 | max--; |
| 832 | } else if (cmp == kCondGE) { |
| 833 | min++; |
| 834 | } |
| 835 | // Do both bounds fit the range? |
Vladimir Marko | 0e2f2ff | 2016-03-22 12:31:54 +0000 | [diff] [blame] | 836 | // Note: The `value` is initialized to please valgrind - the compiler can reorder |
| 837 | // the return value check with the `value` check, b/27651442 . |
| 838 | int64_t value = 0; |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 839 | return IsAtLeast(lower_expr, &value) && value >= min && |
| 840 | IsAtMost(lower_expr, &value) && value <= max && |
| 841 | IsAtLeast(upper_expr, &value) && value >= min && |
| 842 | IsAtMost(upper_expr, &value) && value <= max; |
| 843 | } |
| 844 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 845 | void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop, |
| 846 | HInstruction* instruction, |
| 847 | InductionInfo* info) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 848 | auto it = induction_.find(loop); |
| 849 | if (it == induction_.end()) { |
| 850 | it = induction_.Put(loop, |
| 851 | ArenaSafeMap<HInstruction*, InductionInfo*>( |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 852 | std::less<HInstruction*>(), |
| 853 | graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 854 | } |
| 855 | it->second.Put(instruction, info); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 858 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop, |
| 859 | HInstruction* instruction) { |
| 860 | auto it = induction_.find(loop); |
| 861 | if (it != induction_.end()) { |
| 862 | auto loop_it = it->second.find(instruction); |
| 863 | if (loop_it != it->second.end()) { |
| 864 | return loop_it->second; |
| 865 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 866 | } |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 867 | if (loop->IsDefinedOutOfTheLoop(instruction)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 868 | InductionInfo* info = CreateInvariantFetch(instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 869 | AssignInfo(loop, instruction, info); |
| 870 | return info; |
| 871 | } |
| 872 | return nullptr; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 875 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value, |
| 876 | Primitive::Type type) { |
| 877 | if (type == Primitive::kPrimInt) { |
| 878 | return CreateInvariantFetch(graph_->GetIntConstant(value)); |
| 879 | } |
| 880 | DCHECK_EQ(type, Primitive::kPrimLong); |
| 881 | return CreateInvariantFetch(graph_->GetLongConstant(value)); |
| 882 | } |
| 883 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 884 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant( |
| 885 | InductionOp op, |
| 886 | InductionInfo* a, |
| 887 | InductionInfo* b) { |
| 888 | // Perform some light-weight simplifications during construction of a new invariant. |
| 889 | // This often safes memory and yields a more concise representation of the induction. |
| 890 | // More exhaustive simplifications are done by later phases once induction nodes are |
| 891 | // translated back into HIR code (e.g. by loop optimizations or BCE). |
| 892 | int64_t value = -1; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 893 | if (IsExact(a, &value)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 894 | if (value == 0) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 895 | // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0. |
| 896 | if (op == kAdd || op == kXor) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 897 | return b; |
| 898 | } else if (op == kMul) { |
| 899 | return a; |
| 900 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 901 | } else if (op == kMul) { |
| 902 | // Simplify 1 * b = b, -1 * b = -b |
| 903 | if (value == 1) { |
| 904 | return b; |
| 905 | } else if (value == -1) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 906 | return CreateSimplifiedInvariant(kNeg, nullptr, b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 907 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 910 | if (IsExact(b, &value)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 911 | if (value == 0) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 912 | // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0. |
| 913 | if (op == kAdd || op == kSub || op == kXor) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 914 | return a; |
| 915 | } else if (op == kMul || op == kNeg) { |
| 916 | return b; |
| 917 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 918 | } else if (op == kMul || op == kDiv) { |
| 919 | // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a |
| 920 | if (value == 1) { |
| 921 | return a; |
| 922 | } else if (value == -1) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 923 | return CreateSimplifiedInvariant(kNeg, nullptr, a); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 924 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 925 | } |
| 926 | } else if (b->operation == kNeg) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 927 | // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b. |
| 928 | if (op == kAdd) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 929 | return CreateSimplifiedInvariant(kSub, a, b->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 930 | } else if (op == kSub) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 931 | return CreateSimplifiedInvariant(kAdd, a, b->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 932 | } else if (op == kNeg) { |
| 933 | return b->op_b; |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 934 | } |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 935 | } else if (b->operation == kSub) { |
| 936 | // Simplify - (a - b) = b - a. |
| 937 | if (op == kNeg) { |
| 938 | return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a); |
| 939 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 940 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 941 | return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr, b->type); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 944 | bool HInductionVarAnalysis::IsExact(InductionInfo* info, int64_t* value) { |
| 945 | return InductionVarRange(this).IsConstant(info, InductionVarRange::kExact, value); |
| 946 | } |
| 947 | |
| 948 | bool HInductionVarAnalysis::IsAtMost(InductionInfo* info, int64_t* value) { |
| 949 | return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtMost, value); |
| 950 | } |
| 951 | |
| 952 | bool HInductionVarAnalysis::IsAtLeast(InductionInfo* info, int64_t* value) { |
| 953 | return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtLeast, value); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 956 | bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1, |
| 957 | InductionInfo* info2) { |
| 958 | // Test structural equality only, without accounting for simplifications. |
| 959 | if (info1 != nullptr && info2 != nullptr) { |
| 960 | return |
| 961 | info1->induction_class == info2->induction_class && |
| 962 | info1->operation == info2->operation && |
| 963 | info1->fetch == info2->fetch && |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 964 | info1->type == info2->type && |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 965 | InductionEqual(info1->op_a, info2->op_a) && |
| 966 | InductionEqual(info1->op_b, info2->op_b); |
| 967 | } |
| 968 | // Otherwise only two nullptrs are considered equal. |
| 969 | return info1 == info2; |
| 970 | } |
| 971 | |
| 972 | std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) { |
| 973 | if (info != nullptr) { |
| 974 | if (info->induction_class == kInvariant) { |
| 975 | std::string inv = "("; |
| 976 | inv += InductionToString(info->op_a); |
| 977 | switch (info->operation) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 978 | case kNop: inv += " @ "; break; |
| 979 | case kAdd: inv += " + "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 980 | case kSub: |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 981 | case kNeg: inv += " - "; break; |
| 982 | case kMul: inv += " * "; break; |
| 983 | case kDiv: inv += " / "; break; |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 984 | case kXor: inv += " ^ "; break; |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 985 | case kLT: inv += " < "; break; |
| 986 | case kLE: inv += " <= "; break; |
| 987 | case kGT: inv += " > "; break; |
| 988 | case kGE: inv += " >= "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 989 | case kFetch: |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 990 | DCHECK(info->fetch); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 991 | if (info->fetch->IsIntConstant()) { |
| 992 | inv += std::to_string(info->fetch->AsIntConstant()->GetValue()); |
| 993 | } else if (info->fetch->IsLongConstant()) { |
| 994 | inv += std::to_string(info->fetch->AsLongConstant()->GetValue()); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 995 | } else { |
| 996 | inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName(); |
| 997 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 998 | break; |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 999 | case kTripCountInLoop: inv += " (TC-loop) "; break; |
| 1000 | case kTripCountInBody: inv += " (TC-body) "; break; |
| 1001 | case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break; |
| 1002 | case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1003 | } |
| 1004 | inv += InductionToString(info->op_b); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1005 | inv += ")"; |
| 1006 | return inv; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1007 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1008 | DCHECK(info->operation == kNop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1009 | if (info->induction_class == kLinear) { |
| 1010 | return "(" + InductionToString(info->op_a) + " * i + " + |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1011 | InductionToString(info->op_b) + "):" + |
| 1012 | Primitive::PrettyDescriptor(info->type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1013 | } else if (info->induction_class == kWrapAround) { |
| 1014 | return "wrap(" + InductionToString(info->op_a) + ", " + |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1015 | InductionToString(info->op_b) + "):" + |
| 1016 | Primitive::PrettyDescriptor(info->type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1017 | } else if (info->induction_class == kPeriodic) { |
| 1018 | return "periodic(" + InductionToString(info->op_a) + ", " + |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1019 | InductionToString(info->op_b) + "):" + |
| 1020 | Primitive::PrettyDescriptor(info->type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | return ""; |
| 1025 | } |
| 1026 | |
| 1027 | } // namespace art |