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