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