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" |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 18 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 19 | #include "induction_var_range.h" |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 20 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 21 | namespace art { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 24 | * Returns true if the from/to types denote a narrowing, integral conversion (precision loss). |
| 25 | */ |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 26 | static bool IsNarrowingIntegralConversion(DataType::Type from, DataType::Type to) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 27 | switch (from) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 28 | case DataType::Type::kInt64: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 29 | return to == DataType::Type::kUint8 || |
| 30 | to == DataType::Type::kInt8 || |
| 31 | to == DataType::Type::kUint16 || |
| 32 | to == DataType::Type::kInt16 || |
| 33 | to == DataType::Type::kInt32; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 34 | case DataType::Type::kInt32: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 35 | return to == DataType::Type::kUint8 || |
| 36 | to == DataType::Type::kInt8 || |
| 37 | to == DataType::Type::kUint16 || |
| 38 | to == DataType::Type::kInt16; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 39 | case DataType::Type::kUint16: |
| 40 | case DataType::Type::kInt16: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 41 | return to == DataType::Type::kUint8 || to == DataType::Type::kInt8; |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 42 | default: |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 48 | * Returns result of implicit widening type conversion done in HIR. |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 49 | */ |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 50 | static DataType::Type ImplicitConversion(DataType::Type type) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 51 | switch (type) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 52 | case DataType::Type::kBool: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 53 | case DataType::Type::kUint8: |
| 54 | case DataType::Type::kInt8: |
| 55 | case DataType::Type::kUint16: |
| 56 | case DataType::Type::kInt16: |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 57 | return DataType::Type::kInt32; |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 58 | default: |
| 59 | return type; |
| 60 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Returns true if loop is guarded by "a cmp b" on entry. |
| 65 | */ |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 66 | static bool IsGuardedBy(const HLoopInformation* loop, |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 67 | IfCondition cmp, |
| 68 | HInstruction* a, |
| 69 | HInstruction* b) { |
| 70 | // Chase back through straightline code to the first potential |
| 71 | // block that has a control dependence. |
| 72 | // guard: if (x) bypass |
| 73 | // | |
| 74 | // entry: straightline code |
| 75 | // | |
| 76 | // preheader |
| 77 | // | |
| 78 | // header |
| 79 | HBasicBlock* guard = loop->GetPreHeader(); |
| 80 | HBasicBlock* entry = loop->GetHeader(); |
| 81 | while (guard->GetPredecessors().size() == 1 && |
| 82 | guard->GetSuccessors().size() == 1) { |
| 83 | entry = guard; |
| 84 | guard = guard->GetSinglePredecessor(); |
| 85 | } |
| 86 | // Find guard. |
| 87 | HInstruction* control = guard->GetLastInstruction(); |
| 88 | if (!control->IsIf()) { |
| 89 | return false; |
| 90 | } |
| 91 | HIf* ifs = control->AsIf(); |
| 92 | HInstruction* if_expr = ifs->InputAt(0); |
| 93 | if (if_expr->IsCondition()) { |
| 94 | IfCondition other_cmp = ifs->IfTrueSuccessor() == entry |
| 95 | ? if_expr->AsCondition()->GetCondition() |
| 96 | : if_expr->AsCondition()->GetOppositeCondition(); |
| 97 | if (if_expr->InputAt(0) == a && if_expr->InputAt(1) == b) { |
| 98 | return cmp == other_cmp; |
| 99 | } else if (if_expr->InputAt(1) == a && if_expr->InputAt(0) == b) { |
| 100 | switch (cmp) { |
| 101 | case kCondLT: return other_cmp == kCondGT; |
| 102 | case kCondLE: return other_cmp == kCondGE; |
| 103 | case kCondGT: return other_cmp == kCondLT; |
| 104 | case kCondGE: return other_cmp == kCondLE; |
| 105 | default: LOG(FATAL) << "unexpected cmp: " << cmp; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | /* Finds first loop header phi use. */ |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 113 | HInstruction* FindFirstLoopHeaderPhiUse(const HLoopInformation* loop, HInstruction* instruction) { |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 114 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 115 | if (use.GetUser()->GetBlock() == loop->GetHeader() && |
| 116 | use.GetUser()->IsPhi() && |
| 117 | use.GetUser()->InputAt(1) == instruction) { |
| 118 | return use.GetUser(); |
| 119 | } |
| 120 | } |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Relinks the Phi structure after break-loop rewriting. |
| 126 | */ |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 127 | static bool FixOutsideUse(const HLoopInformation* loop, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 128 | HInstruction* instruction, |
| 129 | HInstruction* replacement, |
| 130 | bool rewrite) { |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 131 | // Deal with regular uses. |
| 132 | const HUseList<HInstruction*>& uses = instruction->GetUses(); |
| 133 | for (auto it = uses.begin(), end = uses.end(); it != end; ) { |
| 134 | HInstruction* user = it->GetUser(); |
| 135 | size_t index = it->GetIndex(); |
| 136 | ++it; // increment prior to potential removal |
| 137 | if (user->GetBlock()->GetLoopInformation() != loop) { |
| 138 | if (replacement == nullptr) { |
| 139 | return false; |
| 140 | } else if (rewrite) { |
| 141 | user->ReplaceInput(replacement, index); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | // Deal with environment uses. |
| 146 | const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses(); |
| 147 | for (auto it = env_uses.begin(), end = env_uses.end(); it != end;) { |
| 148 | HEnvironment* user = it->GetUser(); |
| 149 | size_t index = it->GetIndex(); |
| 150 | ++it; // increment prior to potential removal |
| 151 | if (user->GetHolder()->GetBlock()->GetLoopInformation() != loop) { |
| 152 | if (replacement == nullptr) { |
| 153 | return false; |
| 154 | } else if (rewrite) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 155 | user->ReplaceInput(replacement, index); |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Test and rewrite the loop body of a break-loop. Returns true on success. |
| 164 | */ |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 165 | static bool RewriteBreakLoopBody(const HLoopInformation* loop, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 166 | HBasicBlock* body, |
| 167 | HInstruction* cond, |
| 168 | HInstruction* index, |
| 169 | HInstruction* upper, |
| 170 | bool rewrite) { |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 171 | // Deal with Phis. Outside use prohibited, except for index (which gets exit value). |
| 172 | for (HInstructionIterator it(loop->GetHeader()->GetPhis()); !it.Done(); it.Advance()) { |
| 173 | HInstruction* exit_value = it.Current() == index ? upper : nullptr; |
| 174 | if (!FixOutsideUse(loop, it.Current(), exit_value, rewrite)) { |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | // Deal with other statements in header. |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 179 | for (HInstruction* m = cond->GetPrevious(); m && !m->IsSuspendCheck();) { |
| 180 | HInstruction* p = m->GetPrevious(); |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 181 | if (rewrite) { |
| 182 | m->MoveBefore(body->GetFirstInstruction(), false); |
| 183 | } |
| 184 | if (!FixOutsideUse(loop, m, FindFirstLoopHeaderPhiUse(loop, m), rewrite)) { |
| 185 | return false; |
| 186 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 187 | m = p; |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 192 | // |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 193 | // Class members. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 194 | // |
| 195 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 196 | struct HInductionVarAnalysis::NodeInfo { |
| 197 | explicit NodeInfo(uint32_t d) : depth(d), done(false) {} |
| 198 | uint32_t depth; |
| 199 | bool done; |
| 200 | }; |
| 201 | |
| 202 | struct HInductionVarAnalysis::StackEntry { |
| 203 | StackEntry(HInstruction* insn, NodeInfo* info, size_t link = std::numeric_limits<size_t>::max()) |
| 204 | : instruction(insn), |
| 205 | node_info(info), |
| 206 | user_link(link), |
| 207 | num_visited_inputs(0u), |
| 208 | low_depth(info->depth) {} |
| 209 | |
| 210 | HInstruction* instruction; |
| 211 | NodeInfo* node_info; |
| 212 | size_t user_link; // Stack index of the user that is visiting this input. |
| 213 | size_t num_visited_inputs; |
| 214 | size_t low_depth; |
| 215 | }; |
| 216 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 217 | HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph, const char* name) |
| 218 | : HOptimization(graph, name), |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 219 | induction_(std::less<const HLoopInformation*>(), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 220 | graph->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)), |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 221 | cycles_(std::less<HPhi*>(), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 222 | graph->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 225 | bool HInductionVarAnalysis::Run() { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 226 | // Detects sequence variables (generalized induction variables) during an outer to inner |
| 227 | // traversal of all loops using Gerlek's algorithm. The order is important to enable |
| 228 | // range analysis on outer loop while visiting inner loops. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 229 | for (HBasicBlock* graph_block : graph_->GetReversePostOrder()) { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 230 | // Don't analyze irreducible loops. |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 231 | if (graph_block->IsLoopHeader() && !graph_block->GetLoopInformation()->IsIrreducible()) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 232 | VisitLoop(graph_block->GetLoopInformation()); |
| 233 | } |
| 234 | } |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 235 | return !induction_.empty(); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 238 | void HInductionVarAnalysis::VisitLoop(const HLoopInformation* loop) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 239 | ScopedArenaAllocator local_allocator(graph_->GetArenaStack()); |
| 240 | ScopedArenaSafeMap<HInstruction*, NodeInfo> visited_instructions( |
| 241 | std::less<HInstruction*>(), local_allocator.Adapter(kArenaAllocInductionVarAnalysis)); |
| 242 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 243 | // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's |
| 244 | // algorithm. Due to the descendant-first nature, classification happens "on-demand". |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 245 | size_t global_depth = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 246 | for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) { |
| 247 | HBasicBlock* loop_block = it_loop.Current(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 248 | DCHECK(loop_block->IsInLoop()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 249 | if (loop_block->GetLoopInformation() != loop) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 250 | continue; // Inner loops visited later. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 251 | } |
| 252 | // Visit phi-operations and instructions. |
| 253 | for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 254 | global_depth = TryVisitNodes(loop, it.Current(), global_depth, &visited_instructions); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 255 | } |
| 256 | for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 257 | global_depth = TryVisitNodes(loop, it.Current(), global_depth, &visited_instructions); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 261 | // Determine the loop's trip-count. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 262 | VisitControl(loop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 265 | size_t HInductionVarAnalysis::TryVisitNodes( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 266 | const HLoopInformation* loop, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 267 | HInstruction* start_instruction, |
| 268 | size_t global_depth, |
| 269 | /*inout*/ ScopedArenaSafeMap<HInstruction*, NodeInfo>* visited_instructions) { |
| 270 | // This is recursion-free version of the SCC search algorithm. We have limited stack space, |
| 271 | // so recursion with the depth dependent on the input is undesirable as such depth is unlimited. |
| 272 | auto [it, inserted] = |
| 273 | visited_instructions->insert(std::make_pair(start_instruction, NodeInfo(global_depth + 1u))); |
| 274 | if (!inserted) { |
| 275 | return global_depth; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 276 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 277 | NodeInfo* start_info = &it->second; |
| 278 | ++global_depth; |
| 279 | DCHECK_EQ(global_depth, start_info->depth); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 280 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 281 | ScopedArenaVector<StackEntry> stack(visited_instructions->get_allocator()); |
| 282 | stack.push_back({start_instruction, start_info}); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 283 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 284 | size_t current_entry = 0u; |
| 285 | while (!stack.empty()) { |
| 286 | StackEntry& entry = stack[current_entry]; |
| 287 | |
| 288 | // Look for unvisited inputs (also known as "descentants"). |
| 289 | bool visit_input = false; |
| 290 | auto inputs = entry.instruction->GetInputs(); |
| 291 | while (entry.num_visited_inputs != inputs.size()) { |
| 292 | HInstruction* input = inputs[entry.num_visited_inputs]; |
| 293 | ++entry.num_visited_inputs; |
| 294 | // If the definition is either outside the loop (loop invariant entry value) |
| 295 | // or assigned in inner loop (inner exit value), the input is not visited. |
| 296 | if (input->GetBlock()->GetLoopInformation() != loop) { |
| 297 | continue; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 298 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 299 | // Try visiting the input. If already visited, update `entry.low_depth`. |
| 300 | auto [input_it, input_inserted] = |
| 301 | visited_instructions->insert(std::make_pair(input, NodeInfo(global_depth + 1u))); |
| 302 | NodeInfo* input_info = &input_it->second; |
| 303 | if (input_inserted) { |
| 304 | // Push the input on the `stack` and visit it now. |
| 305 | ++global_depth; |
| 306 | DCHECK_EQ(global_depth, input_info->depth); |
| 307 | stack.push_back({input, input_info, current_entry}); |
| 308 | current_entry = stack.size() - 1u; |
| 309 | visit_input = true; |
| 310 | break; |
| 311 | } else if (!input_info->done && input_info->depth < entry.low_depth) { |
| 312 | entry.low_depth = input_it->second.depth; |
| 313 | } |
| 314 | continue; |
| 315 | } |
| 316 | if (visit_input) { |
| 317 | continue; // Process the new top of the stack. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 320 | // All inputs of the current node have been visited. |
| 321 | // Check if we have found an input below this entry on the stack. |
| 322 | DCHECK(!entry.node_info->done); |
| 323 | size_t previous_entry = entry.user_link; |
| 324 | if (entry.node_info->depth > entry.low_depth) { |
| 325 | DCHECK_LT(previous_entry, current_entry) << entry.node_info->depth << " " << entry.low_depth; |
| 326 | entry.node_info->depth = entry.low_depth; |
| 327 | if (stack[previous_entry].low_depth > entry.low_depth) { |
| 328 | stack[previous_entry].low_depth = entry.low_depth; |
| 329 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 330 | } else { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 331 | // Classify the SCC we have just found. |
| 332 | ArrayRef<StackEntry> stack_tail = ArrayRef<StackEntry>(stack).SubArray(current_entry); |
| 333 | for (StackEntry& tail_entry : stack_tail) { |
| 334 | tail_entry.node_info->done = true; |
| 335 | } |
| 336 | if (current_entry + 1u == stack.size() && !entry.instruction->IsLoopHeaderPhi()) { |
| 337 | ClassifyTrivial(loop, entry.instruction); |
| 338 | } else { |
| 339 | ClassifyNonTrivial(loop, ArrayRef<const StackEntry>(stack_tail)); |
| 340 | } |
| 341 | stack.erase(stack.begin() + current_entry, stack.end()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 342 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 343 | current_entry = previous_entry; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 344 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 345 | |
| 346 | return global_depth; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 349 | /** |
| 350 | * Since graph traversal may enter a SCC at any position, an initial representation may be rotated, |
| 351 | * along dependences, viz. any of (a, b, c, d), (d, a, b, c) (c, d, a, b), (b, c, d, a) assuming |
| 352 | * a chain of dependences (mutual independent items may occur in arbitrary order). For proper |
| 353 | * classification, the lexicographically first loop-phi is rotated to the front. We do that |
| 354 | * as we extract the SCC instructions. |
| 355 | */ |
| 356 | void HInductionVarAnalysis::ExtractScc(ArrayRef<const StackEntry> stack_tail, |
| 357 | ScopedArenaVector<HInstruction*>* scc) { |
| 358 | // Find very first loop-phi. |
| 359 | HInstruction* phi = nullptr; |
| 360 | size_t split_pos = 0; |
| 361 | const size_t size = stack_tail.size(); |
| 362 | for (size_t i = 0; i != size; ++i) { |
| 363 | const StackEntry& entry = stack_tail[i]; |
| 364 | HInstruction* instruction = entry.instruction; |
| 365 | if (instruction->IsLoopHeaderPhi()) { |
| 366 | // All loop Phis in SCC come from the same loop header. |
| 367 | HBasicBlock* block = instruction->GetBlock(); |
| 368 | DCHECK(block->GetLoopInformation()->GetHeader() == block); |
| 369 | DCHECK(phi == nullptr || phi->GetBlock() == block); |
| 370 | if (phi == nullptr || block->GetPhis().FoundBefore(instruction, phi)) { |
| 371 | phi = instruction; |
| 372 | split_pos = i + 1u; |
| 373 | } |
| 374 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 377 | // Extract SCC in two chunks. |
| 378 | DCHECK(scc->empty()); |
| 379 | scc->reserve(size); |
| 380 | for (const StackEntry& entry : ReverseRange(stack_tail.SubArray(/*pos=*/ 0u, split_pos))) { |
| 381 | scc->push_back(entry.instruction); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 382 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 383 | for (const StackEntry& entry : ReverseRange(stack_tail.SubArray(/*pos=*/ split_pos))) { |
| 384 | scc->push_back(entry.instruction); |
| 385 | } |
| 386 | DCHECK_EQ(scc->size(), stack_tail.size()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 389 | void HInductionVarAnalysis::ClassifyTrivial(const HLoopInformation* loop, |
| 390 | HInstruction* instruction) { |
| 391 | const HBasicBlock* context = instruction->GetBlock(); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 392 | DataType::Type type = instruction->GetType(); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 393 | InductionInfo* info = nullptr; |
| 394 | if (instruction->IsPhi()) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 395 | info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 0); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 396 | } else if (instruction->IsAdd()) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 397 | info = TransferAddSub(context, |
| 398 | loop, |
| 399 | LookupInfo(loop, instruction->InputAt(0)), |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 400 | LookupInfo(loop, instruction->InputAt(1)), |
| 401 | kAdd, |
| 402 | type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 403 | } else if (instruction->IsSub()) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 404 | info = TransferAddSub(context, |
| 405 | loop, |
| 406 | LookupInfo(loop, instruction->InputAt(0)), |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 407 | LookupInfo(loop, instruction->InputAt(1)), |
| 408 | kSub, |
| 409 | type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 410 | } else if (instruction->IsNeg()) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 411 | info = TransferNeg(context, loop, LookupInfo(loop, instruction->InputAt(0)), type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 412 | } else if (instruction->IsMul()) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 413 | info = TransferMul(context, |
| 414 | loop, |
| 415 | LookupInfo(loop, instruction->InputAt(0)), |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 416 | LookupInfo(loop, instruction->InputAt(1)), |
| 417 | type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 418 | } else if (instruction->IsShl()) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 419 | HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 420 | if (mulc != nullptr) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 421 | info = TransferMul(context, |
| 422 | loop, |
| 423 | LookupInfo(loop, instruction->InputAt(0)), |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 424 | LookupInfo(loop, mulc), |
| 425 | type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 426 | } |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 427 | } else if (instruction->IsSelect()) { |
| 428 | info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 1); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 429 | } else if (instruction->IsTypeConversion()) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 430 | info = TransferConversion(LookupInfo(loop, instruction->InputAt(0)), |
| 431 | instruction->AsTypeConversion()->GetInputType(), |
| 432 | instruction->AsTypeConversion()->GetResultType()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 433 | } else if (instruction->IsBoundsCheck()) { |
| 434 | info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // Successfully classified? |
| 438 | if (info != nullptr) { |
| 439 | AssignInfo(loop, instruction, info); |
| 440 | } |
| 441 | } |
| 442 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 443 | void HInductionVarAnalysis::ClassifyNonTrivial(const HLoopInformation* loop, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 444 | ArrayRef<const StackEntry> stack_tail) { |
| 445 | const size_t size = stack_tail.size(); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 446 | DCHECK_GE(size, 1u); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 447 | DataType::Type type = stack_tail.back().instruction->GetType(); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 448 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 449 | ScopedArenaAllocator local_allocator(graph_->GetArenaStack()); |
| 450 | ScopedArenaVector<HInstruction*> scc(local_allocator.Adapter(kArenaAllocInductionVarAnalysis)); |
| 451 | ExtractScc(ArrayRef<const StackEntry>(stack_tail), &scc); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 452 | |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 453 | // Analyze from loop-phi onwards. |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 454 | HInstruction* phi = scc[0]; |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 455 | if (!phi->IsLoopHeaderPhi()) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 456 | return; |
| 457 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 458 | |
| 459 | // External link should be loop invariant. |
| 460 | InductionInfo* initial = LookupInfo(loop, phi->InputAt(0)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 461 | if (initial == nullptr || initial->induction_class != kInvariant) { |
| 462 | return; |
| 463 | } |
| 464 | |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 465 | // Store interesting cycle in each loop phi. |
| 466 | for (size_t i = 0; i < size; i++) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 467 | if (scc[i]->IsLoopHeaderPhi()) { |
| 468 | AssignCycle(scc[i]->AsPhi(), ArrayRef<HInstruction* const>(scc)); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 471 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 472 | // 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] | 473 | if (size == 1) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 474 | InductionInfo* update = TransferPhi(loop, phi, /*input_index*/ 1, /*adjust_input_size*/ 0); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 475 | if (update != nullptr) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 476 | AssignInfo(loop, phi, CreateInduction(kWrapAround, |
| 477 | kNop, |
| 478 | initial, |
| 479 | update, |
| 480 | /*fetch*/ nullptr, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 481 | type)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 482 | } |
| 483 | return; |
| 484 | } |
| 485 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 486 | // 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] | 487 | // temporary meaning to its nodes, seeded from the phi instruction and back. |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 488 | ScopedArenaSafeMap<HInstruction*, InductionInfo*> cycle( |
| 489 | std::less<HInstruction*>(), local_allocator.Adapter(kArenaAllocInductionVarAnalysis)); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 490 | for (size_t i = 1; i < size; i++) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 491 | HInstruction* instruction = scc[i]; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 492 | InductionInfo* update = nullptr; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 493 | if (instruction->IsPhi()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 494 | update = SolvePhiAllInputs(loop, phi, instruction, cycle, type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 495 | } else if (instruction->IsAdd()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 496 | update = SolveAddSub(loop, |
| 497 | phi, |
| 498 | instruction, |
| 499 | instruction->InputAt(0), |
| 500 | instruction->InputAt(1), |
| 501 | kAdd, |
| 502 | cycle, |
| 503 | type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 504 | } else if (instruction->IsSub()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 505 | update = SolveAddSub(loop, |
| 506 | phi, |
| 507 | instruction, |
| 508 | instruction->InputAt(0), |
| 509 | instruction->InputAt(1), |
| 510 | kSub, |
| 511 | cycle, |
| 512 | type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 513 | } else if (instruction->IsMul()) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 514 | update = SolveOp( |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 515 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kMul, type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 516 | } else if (instruction->IsDiv()) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 517 | update = SolveOp( |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 518 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kDiv, type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 519 | } else if (instruction->IsRem()) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 520 | update = SolveOp( |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 521 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kRem, type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 522 | } else if (instruction->IsShl()) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 523 | HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 524 | if (mulc != nullptr) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 525 | update = SolveOp(loop, phi, instruction, instruction->InputAt(0), mulc, kMul, type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 526 | } |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 527 | } else if (instruction->IsShr() || instruction->IsUShr()) { |
| 528 | HInstruction* divc = GetShiftConstant(loop, instruction, initial); |
| 529 | if (divc != nullptr) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 530 | update = SolveOp(loop, phi, instruction, instruction->InputAt(0), divc, kDiv, type); |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 531 | } |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 532 | } else if (instruction->IsXor()) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 533 | update = SolveOp( |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 534 | loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kXor, type); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 535 | } else if (instruction->IsEqual()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 536 | update = SolveTest(loop, phi, instruction, /*opposite_value=*/ 0, type); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 537 | } else if (instruction->IsNotEqual()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 538 | update = SolveTest(loop, phi, instruction, /*opposite_value=*/ 1, type); |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 539 | } else if (instruction->IsSelect()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 540 | // Select acts like Phi. |
| 541 | update = SolvePhi(instruction, /*input_index=*/ 0, /*adjust_input_size=*/ 1, cycle); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 542 | } else if (instruction->IsTypeConversion()) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 543 | update = SolveConversion(loop, phi, instruction->AsTypeConversion(), cycle, &type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 544 | } |
| 545 | if (update == nullptr) { |
| 546 | return; |
| 547 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 548 | cycle.Put(instruction, update); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 551 | // Success if all internal links received the same temporary meaning. |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 552 | InductionInfo* induction = SolvePhi(phi, /*input_index=*/ 1, /*adjust_input_size=*/ 0, cycle); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 553 | if (induction != nullptr) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 554 | switch (induction->induction_class) { |
| 555 | case kInvariant: |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 556 | // Construct combined stride of the linear induction. |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 557 | induction = CreateInduction(kLinear, kNop, induction, initial, /*fetch*/ nullptr, type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 558 | FALLTHROUGH_INTENDED; |
| 559 | case kPolynomial: |
| 560 | case kGeometric: |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 561 | case kWrapAround: |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 562 | // Classify first phi and then the rest of the cycle "on-demand". |
| 563 | // Statements are scanned in order. |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 564 | AssignInfo(loop, phi, induction); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 565 | for (size_t i = 1; i < size; i++) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 566 | ClassifyTrivial(loop, scc[i]); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 567 | } |
| 568 | break; |
| 569 | case kPeriodic: |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 570 | // Classify all elements in the cycle with the found periodic induction while |
| 571 | // rotating each first element to the end. Lastly, phi is classified. |
| 572 | // Statements are scanned in reverse order. |
| 573 | for (size_t i = size - 1; i >= 1; i--) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 574 | AssignInfo(loop, scc[i], induction); |
| 575 | induction = RotatePeriodicInduction(induction->op_b, induction->op_a, type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 576 | } |
| 577 | AssignInfo(loop, phi, induction); |
| 578 | break; |
| 579 | default: |
| 580 | break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 585 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction( |
| 586 | InductionInfo* induction, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 587 | InductionInfo* last, |
| 588 | DataType::Type type) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 589 | // Rotates a periodic induction of the form |
| 590 | // (a, b, c, d, e) |
| 591 | // into |
| 592 | // (b, c, d, e, a) |
| 593 | // in preparation of assigning this to the previous variable in the sequence. |
| 594 | if (induction->induction_class == kInvariant) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 595 | return CreateInduction(kPeriodic, |
| 596 | kNop, |
| 597 | induction, |
| 598 | last, |
| 599 | /*fetch*/ nullptr, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 600 | type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 601 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 602 | return CreateInduction(kPeriodic, |
| 603 | kNop, |
| 604 | induction->op_a, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 605 | RotatePeriodicInduction(induction->op_b, last, type), |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 606 | /*fetch*/ nullptr, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 607 | type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 610 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi( |
| 611 | const HLoopInformation* loop, |
| 612 | HInstruction* phi, |
| 613 | size_t input_index, |
| 614 | size_t adjust_input_size) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 615 | // Match all phi inputs from input_index onwards exactly. |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 616 | HInputsRef inputs = phi->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 617 | DCHECK_LT(input_index, inputs.size()); |
| 618 | InductionInfo* a = LookupInfo(loop, inputs[input_index]); |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 619 | 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] | 620 | InductionInfo* b = LookupInfo(loop, inputs[i]); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 621 | if (!InductionEqual(a, b)) { |
| 622 | return nullptr; |
| 623 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 624 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 625 | return a; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 628 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub( |
| 629 | const HBasicBlock* context, |
| 630 | const HLoopInformation* loop, |
| 631 | InductionInfo* a, |
| 632 | InductionInfo* b, |
| 633 | InductionOp op, |
| 634 | DataType::Type type) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 635 | // Transfer over an addition or subtraction: any invariant, linear, polynomial, geometric, |
| 636 | // 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] | 637 | // 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] | 638 | if (a != nullptr && b != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 639 | if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) { |
| 640 | return nullptr; // no transfer |
| 641 | } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 642 | return CreateInvariantOp(context, loop, op, a, b); // direct invariant |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 643 | } else if ((a->induction_class == kLinear && b->induction_class == kLinear) || |
| 644 | (a->induction_class == kPolynomial && b->induction_class == kPolynomial)) { |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 645 | // Rule induc(a, b) + induc(a', b') -> induc(a + a', b + b'). |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 646 | InductionInfo* new_a = TransferAddSub(context, loop, a->op_a, b->op_a, op, type); |
| 647 | InductionInfo* new_b = TransferAddSub(context, loop, a->op_b, b->op_b, op, type); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 648 | if (new_a != nullptr && new_b != nullptr) { |
| 649 | return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 650 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 651 | } else if (a->induction_class == kInvariant) { |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 652 | // 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] | 653 | InductionInfo* new_a = b->op_a; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 654 | InductionInfo* new_b = TransferAddSub(context, loop, a, b->op_b, op, type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 655 | if (b->induction_class == kWrapAround || b->induction_class == kPeriodic) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 656 | new_a = TransferAddSub(context, loop, a, new_a, op, type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 657 | } else if (op == kSub) { // Negation required. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 658 | new_a = TransferNeg(context, loop, new_a, type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 659 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 660 | if (new_a != nullptr && new_b != nullptr) { |
| 661 | return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 662 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 663 | } else if (b->induction_class == kInvariant) { |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 664 | // 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] | 665 | InductionInfo* new_a = a->op_a; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 666 | InductionInfo* new_b = TransferAddSub(context, loop, a->op_b, b, op, type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 667 | if (a->induction_class == kWrapAround || a->induction_class == kPeriodic) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 668 | new_a = TransferAddSub(context, loop, new_a, b, op, type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 669 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 670 | if (new_a != nullptr && new_b != nullptr) { |
| 671 | return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 672 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | return nullptr; |
| 676 | } |
| 677 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 678 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg( |
| 679 | const HBasicBlock* context, |
| 680 | const HLoopInformation* loop, |
| 681 | InductionInfo* a, |
| 682 | DataType::Type type) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 683 | // Transfer over a unary negation: an invariant, linear, polynomial, geometric (mul), |
| 684 | // wrap-around, or periodic input yields a similar but negated induction as result. |
| 685 | if (a != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 686 | if (IsNarrowingLinear(a)) { |
| 687 | return nullptr; // no transfer |
| 688 | } else if (a->induction_class == kInvariant) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 689 | return CreateInvariantOp(context, loop, kNeg, nullptr, a); // direct invariant |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 690 | } else if (a->induction_class != kGeometric || a->operation == kMul) { |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 691 | // Rule - induc(a, b) -> induc(-a, -b). |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 692 | InductionInfo* new_a = TransferNeg(context, loop, a->op_a, type); |
| 693 | InductionInfo* new_b = TransferNeg(context, loop, a->op_b, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 694 | if (new_a != nullptr && new_b != nullptr) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 695 | return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 696 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | return nullptr; |
| 700 | } |
| 701 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 702 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul( |
| 703 | const HBasicBlock* context, |
| 704 | const HLoopInformation* loop, |
| 705 | InductionInfo* a, |
| 706 | InductionInfo* b, |
| 707 | DataType::Type type) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 708 | // Transfer over a multiplication: any invariant, linear, polynomial, geometric (mul), |
| 709 | // wrap-around, or periodic can be multiplied with an invariant to yield a similar |
| 710 | // but multiplied result. Two non-invariant inputs cannot be multiplied, however. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 711 | if (a != nullptr && b != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 712 | if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) { |
| 713 | return nullptr; // no transfer |
| 714 | } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 715 | return CreateInvariantOp(context, loop, kMul, a, b); // direct invariant |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 716 | } else if (a->induction_class == kInvariant && (b->induction_class != kGeometric || |
| 717 | b->operation == kMul)) { |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 718 | // Rule a * induc(a', b') -> induc(a * a', b * b'). |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 719 | InductionInfo* new_a = TransferMul(context, loop, a, b->op_a, type); |
| 720 | InductionInfo* new_b = TransferMul(context, loop, a, b->op_b, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 721 | if (new_a != nullptr && new_b != nullptr) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 722 | return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 723 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 724 | } else if (b->induction_class == kInvariant && (a->induction_class != kGeometric || |
| 725 | a->operation == kMul)) { |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 726 | // Rule induc(a, b) * b' -> induc(a * b', b * b'). |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 727 | InductionInfo* new_a = TransferMul(context, loop, a->op_a, b, type); |
| 728 | InductionInfo* new_b = TransferMul(context, loop, a->op_b, b, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 729 | if (new_a != nullptr && new_b != nullptr) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 730 | return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 731 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | return nullptr; |
| 735 | } |
| 736 | |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 737 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferConversion( |
| 738 | InductionInfo* a, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 739 | DataType::Type from, |
| 740 | DataType::Type to) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 741 | if (a != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 742 | // Allow narrowing conversion on linear induction in certain cases: |
| 743 | // induction is already at narrow type, or can be made narrower. |
| 744 | if (IsNarrowingIntegralConversion(from, to) && |
| 745 | a->induction_class == kLinear && |
| 746 | (a->type == to || IsNarrowingIntegralConversion(a->type, to))) { |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 747 | 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] | 748 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 749 | } |
| 750 | return nullptr; |
| 751 | } |
| 752 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 753 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi( |
| 754 | HInstruction* phi, |
| 755 | size_t input_index, |
| 756 | size_t adjust_input_size, |
| 757 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 758 | // Match all phi inputs from input_index onwards exactly. |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 759 | HInputsRef inputs = phi->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 760 | DCHECK_LT(input_index, inputs.size()); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 761 | auto ita = cycle.find(inputs[input_index]); |
| 762 | if (ita != cycle.end()) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 763 | for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 764 | auto itb = cycle.find(inputs[i]); |
| 765 | if (itb == cycle.end() || |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 766 | !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 767 | return nullptr; |
| 768 | } |
| 769 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 770 | return ita->second; |
| 771 | } |
| 772 | return nullptr; |
| 773 | } |
| 774 | |
| 775 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 776 | const HLoopInformation* loop, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 777 | HInstruction* entry_phi, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 778 | HInstruction* phi, |
| 779 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle, |
| 780 | DataType::Type type) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 781 | // Match all phi inputs. |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 782 | InductionInfo* match = SolvePhi(phi, /*input_index=*/ 0, /*adjust_input_size=*/ 0, cycle); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 783 | if (match != nullptr) { |
| 784 | return match; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 785 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 786 | |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 787 | // Otherwise, try to solve for a periodic seeded from phi onward. |
| 788 | // Only tight multi-statement cycles are considered in order to |
| 789 | // simplify rotating the periodic during the final classification. |
| 790 | if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) { |
| 791 | InductionInfo* a = LookupInfo(loop, phi->InputAt(0)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 792 | if (a != nullptr && a->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 793 | if (phi->InputAt(1) == entry_phi) { |
| 794 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 795 | return CreateInduction(kPeriodic, kNop, a, initial, /*fetch*/ nullptr, type); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 796 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 797 | InductionInfo* b = SolvePhi(phi, /*input_index=*/ 1, /*adjust_input_size=*/ 0, cycle); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 798 | if (b != nullptr && b->induction_class == kPeriodic) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 799 | return CreateInduction(kPeriodic, kNop, a, b, /*fetch*/ nullptr, type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 803 | return nullptr; |
| 804 | } |
| 805 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 806 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 807 | const HLoopInformation* loop, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 808 | HInstruction* entry_phi, |
| 809 | HInstruction* instruction, |
| 810 | HInstruction* x, |
| 811 | HInstruction* y, |
| 812 | InductionOp op, |
| 813 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle, |
| 814 | DataType::Type type) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 815 | const HBasicBlock* context = instruction->GetBlock(); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 816 | auto main_solve_add_sub = [&]() -> HInductionVarAnalysis::InductionInfo* { |
| 817 | // Solve within a cycle over an addition or subtraction. |
| 818 | InductionInfo* b = LookupInfo(loop, y); |
| 819 | if (b != nullptr) { |
| 820 | if (b->induction_class == kInvariant) { |
| 821 | // Adding or subtracting an invariant value, seeded from phi, |
| 822 | // keeps adding to the stride of the linear induction. |
| 823 | if (x == entry_phi) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 824 | return (op == kAdd) ? b : CreateInvariantOp(context, loop, kNeg, nullptr, b); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 825 | } |
| 826 | auto it = cycle.find(x); |
| 827 | if (it != cycle.end()) { |
| 828 | InductionInfo* a = it->second; |
| 829 | if (a->induction_class == kInvariant) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 830 | return CreateInvariantOp(context, loop, op, a, b); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | } else if (b->induction_class == kLinear && b->type == type) { |
| 834 | // Solve within a tight cycle that adds a term that is already classified as a linear |
| 835 | // induction for a polynomial induction k = k + i (represented as sum over linear terms). |
| 836 | if (x == entry_phi && |
| 837 | entry_phi->InputCount() == 2 && |
| 838 | instruction == entry_phi->InputAt(1)) { |
| 839 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 840 | InductionInfo* new_a = op == kAdd ? b : TransferNeg(context, loop, b, type); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 841 | if (new_a != nullptr) { |
| 842 | return CreateInduction(kPolynomial, kNop, new_a, initial, /*fetch*/ nullptr, type); |
| 843 | } |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 844 | } |
| 845 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 846 | } |
| 847 | return nullptr; |
| 848 | }; |
| 849 | HInductionVarAnalysis::InductionInfo* result = main_solve_add_sub(); |
| 850 | if (result == nullptr) { |
| 851 | // Try some alternatives before failing. |
| 852 | if (op == kAdd) { |
| 853 | // Try the other way around for an addition. |
| 854 | std::swap(x, y); |
| 855 | result = main_solve_add_sub(); |
| 856 | } else if (op == kSub) { |
| 857 | // Solve within a tight cycle that is formed by exactly two instructions, |
| 858 | // one phi and one update, for a periodic idiom of the form k = c - k. |
| 859 | if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) { |
| 860 | InductionInfo* a = LookupInfo(loop, x); |
| 861 | if (a != nullptr && a->induction_class == kInvariant) { |
| 862 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
| 863 | result = CreateInduction(kPeriodic, |
| 864 | kNop, |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 865 | CreateInvariantOp(context, loop, kSub, a, initial), |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 866 | initial, |
| 867 | /*fetch*/ nullptr, |
| 868 | type); |
Aart Bik | 74da529 | 2016-12-20 11:13:03 -0800 | [diff] [blame] | 869 | } |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 870 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 871 | } |
| 872 | } |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 873 | return result; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 876 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveOp(const HLoopInformation* loop, |
| 877 | HInstruction* entry_phi, |
| 878 | HInstruction* instruction, |
| 879 | HInstruction* x, |
| 880 | HInstruction* y, |
| 881 | InductionOp op, |
| 882 | DataType::Type type) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 883 | // 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] | 884 | if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 885 | InductionInfo* c = nullptr; |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 886 | InductionInfo* b = LookupInfo(loop, y); |
| 887 | if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 888 | c = b; |
| 889 | } else if (op != kDiv && op != kRem) { |
| 890 | InductionInfo* a = LookupInfo(loop, x); |
| 891 | if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) { |
| 892 | c = a; |
| 893 | } |
| 894 | } |
| 895 | // Found suitable operand left or right? |
| 896 | if (c != nullptr) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 897 | const HBasicBlock* context = instruction->GetBlock(); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 898 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
| 899 | switch (op) { |
| 900 | case kMul: |
| 901 | case kDiv: |
| 902 | // Restrict base of geometric induction to direct fetch. |
| 903 | if (c->operation == kFetch) { |
| 904 | return CreateInduction(kGeometric, |
| 905 | op, |
| 906 | initial, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 907 | CreateConstant(0, type), |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 908 | c->fetch, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 909 | type); |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 910 | } |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 911 | break; |
| 912 | case kRem: |
| 913 | // Idiomatic MOD wrap-around induction. |
| 914 | return CreateInduction(kWrapAround, |
| 915 | kNop, |
| 916 | initial, |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 917 | CreateInvariantOp(context, loop, kRem, initial, c), |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 918 | /*fetch*/ nullptr, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 919 | type); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 920 | case kXor: |
| 921 | // Idiomatic XOR periodic induction. |
| 922 | return CreateInduction(kPeriodic, |
| 923 | kNop, |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 924 | CreateInvariantOp(context, loop, kXor, initial, c), |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 925 | initial, |
| 926 | /*fetch*/ nullptr, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 927 | type); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 928 | default: |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 929 | LOG(FATAL) << op; |
| 930 | UNREACHABLE(); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 931 | } |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 932 | } |
| 933 | } |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 934 | return nullptr; |
| 935 | } |
| 936 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 937 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(const HLoopInformation* loop, |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 938 | HInstruction* entry_phi, |
| 939 | HInstruction* instruction, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 940 | int64_t opposite_value, |
| 941 | DataType::Type type) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 942 | // Detect hidden XOR construction in x = (x == false) or x = (x != true). |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 943 | const HBasicBlock* context = instruction->GetBlock(); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 944 | HInstruction* x = instruction->InputAt(0); |
| 945 | HInstruction* y = instruction->InputAt(1); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 946 | int64_t value = -1; |
| 947 | if (IsExact(context, loop, LookupInfo(loop, x), &value) && value == opposite_value) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 948 | return SolveOp(loop, entry_phi, instruction, graph_->GetIntConstant(1), y, kXor, type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 949 | } else if (IsExact(context, loop, LookupInfo(loop, y), &value) && value == opposite_value) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 950 | return SolveOp(loop, entry_phi, instruction, x, graph_->GetIntConstant(1), kXor, type); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 951 | } |
| 952 | return nullptr; |
| 953 | } |
| 954 | |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 955 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveConversion( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 956 | const HLoopInformation* loop, |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 957 | HInstruction* entry_phi, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 958 | HTypeConversion* conversion, |
| 959 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle, |
| 960 | /*inout*/ DataType::Type* type) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 961 | DataType::Type from = conversion->GetInputType(); |
| 962 | DataType::Type to = conversion->GetResultType(); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 963 | // A narrowing conversion is allowed as *last* operation of the cycle of a linear induction |
| 964 | // with an initial value that fits the type, provided that the narrowest encountered type is |
| 965 | // recorded with the induction to account for the precision loss. The narrower induction does |
| 966 | // *not* transfer to any wider operations, however, since these may yield out-of-type values |
| 967 | if (entry_phi->InputCount() == 2 && conversion == entry_phi->InputAt(1)) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 968 | int64_t min = DataType::MinValueOfIntegralType(to); |
| 969 | int64_t max = DataType::MaxValueOfIntegralType(to); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 970 | int64_t value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 971 | const HBasicBlock* context = conversion->GetBlock(); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 972 | InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0)); |
| 973 | if (IsNarrowingIntegralConversion(from, to) && |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 974 | IsAtLeast(context, loop, initial, &value) && value >= min && |
| 975 | IsAtMost(context, loop, initial, &value) && value <= max) { |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 976 | auto it = cycle.find(conversion->GetInput()); |
| 977 | if (it != cycle.end() && it->second->induction_class == kInvariant) { |
| 978 | *type = to; |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 979 | return it->second; |
| 980 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | return nullptr; |
| 984 | } |
| 985 | |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 986 | // |
| 987 | // Loop trip count analysis methods. |
| 988 | // |
| 989 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 990 | void HInductionVarAnalysis::VisitControl(const HLoopInformation* loop) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 991 | HInstruction* control = loop->GetHeader()->GetLastInstruction(); |
| 992 | if (control->IsIf()) { |
| 993 | HIf* ifs = control->AsIf(); |
| 994 | HBasicBlock* if_true = ifs->IfTrueSuccessor(); |
| 995 | HBasicBlock* if_false = ifs->IfFalseSuccessor(); |
| 996 | HInstruction* if_expr = ifs->InputAt(0); |
| 997 | // Determine if loop has following structure in header. |
| 998 | // loop-header: .... |
| 999 | // if (condition) goto X |
| 1000 | if (if_expr->IsCondition()) { |
| 1001 | HCondition* condition = if_expr->AsCondition(); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1002 | const HBasicBlock* context = condition->GetBlock(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1003 | InductionInfo* a = LookupInfo(loop, condition->InputAt(0)); |
| 1004 | InductionInfo* b = LookupInfo(loop, condition->InputAt(1)); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1005 | DataType::Type type = ImplicitConversion(condition->InputAt(0)->GetType()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1006 | // Determine if the loop control uses a known sequence on an if-exit (X outside) or on |
| 1007 | // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition(). |
| 1008 | if (a == nullptr || b == nullptr) { |
| 1009 | return; // Loop control is not a sequence. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1010 | } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1011 | VisitCondition(context, loop, if_false, a, b, type, condition->GetOppositeCondition()); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1012 | } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1013 | VisitCondition(context, loop, if_true, a, b, type, condition->GetCondition()); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1019 | void HInductionVarAnalysis::VisitCondition(const HBasicBlock* context, |
| 1020 | const HLoopInformation* loop, |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1021 | HBasicBlock* body, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1022 | InductionInfo* a, |
| 1023 | InductionInfo* b, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1024 | DataType::Type type, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1025 | IfCondition cmp) { |
| 1026 | if (a->induction_class == kInvariant && b->induction_class == kLinear) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1027 | // 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] | 1028 | switch (cmp) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1029 | case kCondLT: VisitCondition(context, loop, body, b, a, type, kCondGT); break; |
| 1030 | case kCondLE: VisitCondition(context, loop, body, b, a, type, kCondGE); break; |
| 1031 | case kCondGT: VisitCondition(context, loop, body, b, a, type, kCondLT); break; |
| 1032 | case kCondGE: VisitCondition(context, loop, body, b, a, type, kCondLE); break; |
| 1033 | case kCondNE: VisitCondition(context, loop, body, b, a, type, kCondNE); break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1034 | default: break; |
| 1035 | } |
| 1036 | } else if (a->induction_class == kLinear && b->induction_class == kInvariant) { |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1037 | // Analyze condition with induction at left-hand-side (e.g. i < U). |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1038 | InductionInfo* lower_expr = a->op_b; |
| 1039 | InductionInfo* upper_expr = b; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1040 | InductionInfo* stride_expr = a->op_a; |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1041 | // Test for constant stride and integral condition. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1042 | int64_t stride_value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1043 | if (!IsExact(context, loop, stride_expr, &stride_value)) { |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1044 | return; // unknown stride |
| 1045 | } else if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) { |
| 1046 | return; // not integral |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1047 | } |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1048 | // Since loops with a i != U condition will not be normalized by the method below, first |
| 1049 | // try to rewrite a break-loop with terminating condition i != U into an equivalent loop |
| 1050 | // with non-strict end condition i <= U or i >= U if such a rewriting is possible and safe. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1051 | if (cmp == kCondNE && RewriteBreakLoop(context, loop, body, stride_value, type)) { |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1052 | cmp = stride_value > 0 ? kCondLE : kCondGE; |
| 1053 | } |
| 1054 | // If this rewriting failed, try to rewrite condition i != U into strict end condition i < U |
| 1055 | // or i > U if this end condition is reached exactly (tested by verifying if the loop has a |
| 1056 | // unit stride and the non-strict condition would be always taken). |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1057 | if (cmp == kCondNE && |
| 1058 | ((stride_value == +1 && IsTaken(context, loop, lower_expr, upper_expr, kCondLE)) || |
| 1059 | (stride_value == -1 && IsTaken(context, loop, lower_expr, upper_expr, kCondGE)))) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1060 | cmp = stride_value > 0 ? kCondLT : kCondGT; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1061 | } |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1062 | // A mismatch between the type of condition and the induction is only allowed if the, |
| 1063 | // necessarily narrower, induction range fits the narrower control. |
| 1064 | if (type != a->type && |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1065 | !FitsNarrowerControl(context, loop, lower_expr, upper_expr, stride_value, a->type, cmp)) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1066 | return; // mismatched type |
| 1067 | } |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1068 | // Normalize a linear loop control with a nonzero stride: |
| 1069 | // stride > 0, either i < U or i <= U |
| 1070 | // stride < 0, either i > U or i >= U |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1071 | if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) || |
| 1072 | (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1073 | VisitTripCount(context, loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1074 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1078 | void HInductionVarAnalysis::VisitTripCount(const HBasicBlock* context, |
| 1079 | const HLoopInformation* loop, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1080 | InductionInfo* lower_expr, |
| 1081 | InductionInfo* upper_expr, |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1082 | InductionInfo* stride_expr, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1083 | int64_t stride_value, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1084 | DataType::Type type, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1085 | IfCondition cmp) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1086 | // Any loop of the general form: |
| 1087 | // |
| 1088 | // for (i = L; i <= U; i += S) // S > 0 |
| 1089 | // or for (i = L; i >= U; i += S) // S < 0 |
| 1090 | // .. i .. |
| 1091 | // |
| 1092 | // can be normalized into: |
| 1093 | // |
| 1094 | // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S |
| 1095 | // .. L + S * n .. |
| 1096 | // |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1097 | // taking the following into consideration: |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1098 | // |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1099 | // (1) Using the same precision, the TC (trip-count) expression should be interpreted as |
| 1100 | // an unsigned entity, for example, as in the following loop that uses the full range: |
| 1101 | // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX |
| 1102 | // (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] | 1103 | // for (int i = 12; i < U; i++) // TC = 0 when U <= 12 |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1104 | // 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] | 1105 | // 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] | 1106 | // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in: |
| 1107 | // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX |
| 1108 | // 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] | 1109 | // with an explicit finite-test. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1110 | // (4) For loops which early-exits, the TC forms an upper bound, as in: |
| 1111 | // for (int i = 0; i < 10 && ....; i++) // TC <= 10 |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1112 | InductionInfo* trip_count = upper_expr; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1113 | const bool is_taken = IsTaken(context, loop, lower_expr, upper_expr, cmp); |
| 1114 | const bool is_finite = IsFinite(context, loop, upper_expr, stride_value, type, cmp); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1115 | const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1116 | if (!cancels) { |
| 1117 | // Convert exclusive integral inequality into inclusive integral inequality, |
| 1118 | // 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] | 1119 | if (cmp == kCondLT) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1120 | trip_count = CreateInvariantOp(context, loop, kSub, trip_count, CreateConstant(1, type)); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 1121 | } else if (cmp == kCondGT) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1122 | trip_count = CreateInvariantOp(context, loop, kAdd, trip_count, CreateConstant(1, type)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1123 | } |
| 1124 | // Compensate for stride. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1125 | trip_count = CreateInvariantOp(context, loop, kAdd, trip_count, stride_expr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1126 | } |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1127 | trip_count = CreateInvariantOp(context, loop, kSub, trip_count, lower_expr); |
| 1128 | trip_count = CreateInvariantOp(context, loop, kDiv, trip_count, stride_expr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1129 | // 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] | 1130 | // 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] | 1131 | InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1132 | if (is_taken && is_finite) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1133 | tcKind = kTripCountInLoop; // needs neither test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1134 | } else if (is_finite) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1135 | tcKind = kTripCountInBody; // needs taken-test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1136 | } else if (is_taken) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1137 | tcKind = kTripCountInLoopUnsafe; // needs finite-test |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1138 | } |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1139 | InductionOp op = kNop; |
| 1140 | switch (cmp) { |
| 1141 | case kCondLT: op = kLT; break; |
| 1142 | case kCondLE: op = kLE; break; |
| 1143 | case kCondGT: op = kGT; break; |
| 1144 | case kCondGE: op = kGE; break; |
| 1145 | default: LOG(FATAL) << "CONDITION UNREACHABLE"; |
| 1146 | } |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1147 | // Associate trip count with control instruction, rather than the condition (even |
| 1148 | // though it's its use) since former provides a convenient use-free placeholder. |
| 1149 | HInstruction* control = loop->GetHeader()->GetLastInstruction(); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1150 | InductionInfo* taken_test = CreateInvariantOp(context, loop, op, lower_expr, upper_expr); |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1151 | DCHECK(control->IsIf()); |
| 1152 | AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1155 | bool HInductionVarAnalysis::IsTaken(const HBasicBlock* context, |
| 1156 | const HLoopInformation* loop, |
| 1157 | InductionInfo* lower_expr, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1158 | InductionInfo* upper_expr, |
| 1159 | IfCondition cmp) { |
| 1160 | int64_t lower_value; |
| 1161 | int64_t upper_value; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1162 | switch (cmp) { |
| 1163 | case kCondLT: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1164 | return IsAtMost(context, loop, lower_expr, &lower_value) |
| 1165 | && IsAtLeast(context, loop, upper_expr, &upper_value) |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1166 | && lower_value < upper_value; |
| 1167 | case kCondLE: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1168 | return IsAtMost(context, loop, lower_expr, &lower_value) |
| 1169 | && IsAtLeast(context, loop, upper_expr, &upper_value) |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1170 | && lower_value <= upper_value; |
| 1171 | case kCondGT: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1172 | return IsAtLeast(context, loop, lower_expr, &lower_value) |
| 1173 | && IsAtMost(context, loop, upper_expr, &upper_value) |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1174 | && lower_value > upper_value; |
| 1175 | case kCondGE: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1176 | return IsAtLeast(context, loop, lower_expr, &lower_value) |
| 1177 | && IsAtMost(context, loop, upper_expr, &upper_value) |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1178 | && lower_value >= upper_value; |
| 1179 | default: |
| 1180 | LOG(FATAL) << "CONDITION UNREACHABLE"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1181 | UNREACHABLE(); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1182 | } |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1185 | bool HInductionVarAnalysis::IsFinite(const HBasicBlock* context, |
| 1186 | const HLoopInformation* loop, |
| 1187 | InductionInfo* upper_expr, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1188 | int64_t stride_value, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1189 | DataType::Type type, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1190 | IfCondition cmp) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1191 | int64_t min = DataType::MinValueOfIntegralType(type); |
| 1192 | int64_t max = DataType::MaxValueOfIntegralType(type); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1193 | // Some rules under which it is certain at compile-time that the loop is finite. |
| 1194 | int64_t value; |
| 1195 | switch (cmp) { |
| 1196 | case kCondLT: |
| 1197 | return stride_value == 1 || |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1198 | (IsAtMost(context, loop, upper_expr, &value) && value <= (max - stride_value + 1)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1199 | case kCondLE: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1200 | return (IsAtMost(context, loop, upper_expr, &value) && value <= (max - stride_value)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1201 | case kCondGT: |
| 1202 | return stride_value == -1 || |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1203 | (IsAtLeast(context, loop, upper_expr, &value) && value >= (min - stride_value - 1)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1204 | case kCondGE: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1205 | return (IsAtLeast(context, loop, upper_expr, &value) && value >= (min - stride_value)); |
Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 1206 | default: |
| 1207 | LOG(FATAL) << "CONDITION UNREACHABLE"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1208 | UNREACHABLE(); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 1209 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1212 | bool HInductionVarAnalysis::FitsNarrowerControl(const HBasicBlock* context, |
| 1213 | const HLoopInformation* loop, |
| 1214 | InductionInfo* lower_expr, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1215 | InductionInfo* upper_expr, |
| 1216 | int64_t stride_value, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1217 | DataType::Type type, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1218 | IfCondition cmp) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1219 | int64_t min = DataType::MinValueOfIntegralType(type); |
| 1220 | int64_t max = DataType::MaxValueOfIntegralType(type); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1221 | // Inclusive test need one extra. |
| 1222 | if (stride_value != 1 && stride_value != -1) { |
| 1223 | return false; // non-unit stride |
| 1224 | } else if (cmp == kCondLE) { |
| 1225 | max--; |
| 1226 | } else if (cmp == kCondGE) { |
| 1227 | min++; |
| 1228 | } |
| 1229 | // Do both bounds fit the range? |
Vladimir Marko | 0e2f2ff | 2016-03-22 12:31:54 +0000 | [diff] [blame] | 1230 | int64_t value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1231 | return IsAtLeast(context, loop, lower_expr, &value) && value >= min && |
| 1232 | IsAtMost(context, loop, lower_expr, &value) && value <= max && |
| 1233 | IsAtLeast(context, loop, upper_expr, &value) && value >= min && |
| 1234 | IsAtMost(context, loop, upper_expr, &value) && value <= max; |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1237 | bool HInductionVarAnalysis::RewriteBreakLoop(const HBasicBlock* context, |
| 1238 | const HLoopInformation* loop, |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1239 | HBasicBlock* body, |
| 1240 | int64_t stride_value, |
| 1241 | DataType::Type type) { |
| 1242 | // Only accept unit stride. |
| 1243 | if (std::abs(stride_value) != 1) { |
| 1244 | return false; |
| 1245 | } |
| 1246 | // Simple terminating i != U condition, used nowhere else. |
| 1247 | HIf* ifs = loop->GetHeader()->GetLastInstruction()->AsIf(); |
| 1248 | HInstruction* cond = ifs->InputAt(0); |
| 1249 | if (ifs->GetPrevious() != cond || !cond->HasOnlyOneNonEnvironmentUse()) { |
| 1250 | return false; |
| 1251 | } |
| 1252 | int c = LookupInfo(loop, cond->InputAt(0))->induction_class == kLinear ? 0 : 1; |
| 1253 | HInstruction* index = cond->InputAt(c); |
| 1254 | HInstruction* upper = cond->InputAt(1 - c); |
| 1255 | // Safe to rewrite into i <= U? |
| 1256 | IfCondition cmp = stride_value > 0 ? kCondLE : kCondGE; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1257 | if (!index->IsPhi() || |
| 1258 | !IsFinite(context, loop, LookupInfo(loop, upper), stride_value, type, cmp)) { |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
| 1261 | // Body consists of update to index i only, used nowhere else. |
| 1262 | if (body->GetSuccessors().size() != 1 || |
| 1263 | body->GetSingleSuccessor() != loop->GetHeader() || |
| 1264 | !body->GetPhis().IsEmpty() || |
| 1265 | body->GetInstructions().IsEmpty() || |
| 1266 | body->GetFirstInstruction() != index->InputAt(1) || |
| 1267 | !body->GetFirstInstruction()->HasOnlyOneNonEnvironmentUse() || |
| 1268 | !body->GetFirstInstruction()->GetNext()->IsGoto()) { |
| 1269 | return false; |
| 1270 | } |
| 1271 | // Always taken or guarded by enclosing condition. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1272 | if (!IsTaken(context, loop, LookupInfo(loop, index)->op_b, LookupInfo(loop, upper), cmp) && |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 1273 | !IsGuardedBy(loop, cmp, index->InputAt(0), upper)) { |
| 1274 | return false; |
| 1275 | } |
| 1276 | // Test if break-loop body can be written, and do so on success. |
| 1277 | if (RewriteBreakLoopBody(loop, body, cond, index, upper, /*rewrite*/ false)) { |
| 1278 | RewriteBreakLoopBody(loop, body, cond, index, upper, /*rewrite*/ true); |
| 1279 | } else { |
| 1280 | return false; |
| 1281 | } |
| 1282 | // Rewrite condition in HIR. |
| 1283 | if (ifs->IfTrueSuccessor() != body) { |
| 1284 | cmp = (cmp == kCondLE) ? kCondGT : kCondLT; |
| 1285 | } |
| 1286 | HInstruction* rep = nullptr; |
| 1287 | switch (cmp) { |
| 1288 | case kCondLT: rep = new (graph_->GetAllocator()) HLessThan(index, upper); break; |
| 1289 | case kCondGT: rep = new (graph_->GetAllocator()) HGreaterThan(index, upper); break; |
| 1290 | case kCondLE: rep = new (graph_->GetAllocator()) HLessThanOrEqual(index, upper); break; |
| 1291 | case kCondGE: rep = new (graph_->GetAllocator()) HGreaterThanOrEqual(index, upper); break; |
| 1292 | default: LOG(FATAL) << cmp; UNREACHABLE(); |
| 1293 | } |
| 1294 | loop->GetHeader()->ReplaceAndRemoveInstructionWith(cond, rep); |
| 1295 | return true; |
| 1296 | } |
| 1297 | |
| 1298 | // |
| 1299 | // Helper methods. |
| 1300 | // |
| 1301 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1302 | void HInductionVarAnalysis::AssignInfo(const HLoopInformation* loop, |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1303 | HInstruction* instruction, |
| 1304 | InductionInfo* info) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1305 | auto it = induction_.find(loop); |
| 1306 | if (it == induction_.end()) { |
| 1307 | it = induction_.Put(loop, |
| 1308 | ArenaSafeMap<HInstruction*, InductionInfo*>( |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 1309 | std::less<HInstruction*>(), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1310 | graph_->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis))); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1311 | } |
| 1312 | it->second.Put(instruction, info); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1315 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo( |
| 1316 | const HLoopInformation* loop, |
| 1317 | HInstruction* instruction) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1318 | auto it = induction_.find(loop); |
| 1319 | if (it != induction_.end()) { |
| 1320 | auto loop_it = it->second.find(instruction); |
| 1321 | if (loop_it != it->second.end()) { |
| 1322 | return loop_it->second; |
| 1323 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1324 | } |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 1325 | if (loop->IsDefinedOutOfTheLoop(instruction)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1326 | InductionInfo* info = CreateInvariantFetch(instruction); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1327 | AssignInfo(loop, instruction, info); |
| 1328 | return info; |
| 1329 | } |
| 1330 | return nullptr; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1333 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1334 | DataType::Type type) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1335 | HInstruction* constant; |
| 1336 | switch (type) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1337 | case DataType::Type::kFloat64: constant = graph_->GetDoubleConstant(value); break; |
| 1338 | case DataType::Type::kFloat32: constant = graph_->GetFloatConstant(value); break; |
| 1339 | case DataType::Type::kInt64: constant = graph_->GetLongConstant(value); break; |
| 1340 | default: constant = graph_->GetIntConstant(value); break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1341 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1342 | return CreateInvariantFetch(constant); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1345 | HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1346 | const HBasicBlock* context, |
| 1347 | const HLoopInformation* loop, |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1348 | InductionOp op, |
| 1349 | InductionInfo* a, |
| 1350 | InductionInfo* b) { |
| 1351 | // Perform some light-weight simplifications during construction of a new invariant. |
| 1352 | // This often safes memory and yields a more concise representation of the induction. |
| 1353 | // More exhaustive simplifications are done by later phases once induction nodes are |
| 1354 | // translated back into HIR code (e.g. by loop optimizations or BCE). |
| 1355 | int64_t value = -1; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1356 | if (IsExact(context, loop, a, &value)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1357 | if (value == 0) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 1358 | // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0. |
| 1359 | if (op == kAdd || op == kXor) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1360 | return b; |
| 1361 | } else if (op == kMul) { |
| 1362 | return a; |
| 1363 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1364 | } else if (op == kMul) { |
| 1365 | // Simplify 1 * b = b, -1 * b = -b |
| 1366 | if (value == 1) { |
| 1367 | return b; |
| 1368 | } else if (value == -1) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1369 | return CreateSimplifiedInvariant(context, loop, kNeg, nullptr, b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1370 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1371 | } |
| 1372 | } |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1373 | if (IsExact(context, loop, b, &value)) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1374 | if (value == 0) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 1375 | // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0. |
| 1376 | if (op == kAdd || op == kSub || op == kXor) { |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1377 | return a; |
| 1378 | } else if (op == kMul || op == kNeg) { |
| 1379 | return b; |
| 1380 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1381 | } else if (op == kMul || op == kDiv) { |
| 1382 | // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a |
| 1383 | if (value == 1) { |
| 1384 | return a; |
| 1385 | } else if (value == -1) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1386 | return CreateSimplifiedInvariant(context, loop, kNeg, nullptr, a); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1387 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1388 | } |
| 1389 | } else if (b->operation == kNeg) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1390 | // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b. |
| 1391 | if (op == kAdd) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1392 | return CreateSimplifiedInvariant(context, loop, kSub, a, b->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1393 | } else if (op == kSub) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1394 | return CreateSimplifiedInvariant(context, loop, kAdd, a, b->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1395 | } else if (op == kNeg) { |
| 1396 | return b->op_b; |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1397 | } |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1398 | } else if (b->operation == kSub) { |
| 1399 | // Simplify - (a - b) = b - a. |
| 1400 | if (op == kNeg) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1401 | return CreateSimplifiedInvariant(context, loop, kSub, b->op_b, b->op_a); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1402 | } |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1403 | } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1404 | return new (graph_->GetAllocator()) InductionInfo( |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1405 | kInvariant, op, a, b, nullptr, ImplicitConversion(b->type)); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1408 | HInstruction* HInductionVarAnalysis::GetShiftConstant(const HLoopInformation* loop, |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 1409 | HInstruction* instruction, |
| 1410 | InductionInfo* initial) { |
| 1411 | DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1412 | const HBasicBlock* context = instruction->GetBlock(); |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 1413 | // Shift-rights are only the same as division for non-negative initial inputs. |
| 1414 | // Otherwise we would round incorrectly. |
| 1415 | if (initial != nullptr) { |
| 1416 | int64_t value = -1; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1417 | if (!IsAtLeast(context, loop, initial, &value) || value < 0) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 1418 | return nullptr; |
| 1419 | } |
| 1420 | } |
| 1421 | // Obtain the constant needed to treat shift as equivalent multiplication or division. |
| 1422 | // This yields an existing instruction if the constant is already there. Otherwise, this |
| 1423 | // has a side effect on the HIR. The restriction on the shift factor avoids generating a |
| 1424 | // negative constant (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that |
| 1425 | // 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] | 1426 | InductionInfo* b = LookupInfo(loop, instruction->InputAt(1)); |
| 1427 | int64_t value = -1; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1428 | if (IsExact(context, loop, b, &value)) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1429 | DataType::Type type = instruction->InputAt(0)->GetType(); |
| 1430 | if (type == DataType::Type::kInt32 && 0 <= value && value < 31) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1431 | return graph_->GetIntConstant(1 << value); |
| 1432 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1433 | if (type == DataType::Type::kInt64 && 0 <= value && value < 63) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1434 | return graph_->GetLongConstant(1L << value); |
| 1435 | } |
| 1436 | } |
| 1437 | return nullptr; |
| 1438 | } |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1439 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 1440 | void HInductionVarAnalysis::AssignCycle(HPhi* phi, ArrayRef<HInstruction* const> scc) { |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1441 | ArenaSet<HInstruction*>* set = &cycles_.Put(phi, ArenaSet<HInstruction*>( |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1442 | graph_->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)))->second; |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 1443 | for (HInstruction* i : scc) { |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1444 | set->insert(i); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | ArenaSet<HInstruction*>* HInductionVarAnalysis::LookupCycle(HPhi* phi) { |
| 1449 | auto it = cycles_.find(phi); |
| 1450 | if (it != cycles_.end()) { |
| 1451 | return &it->second; |
| 1452 | } |
| 1453 | return nullptr; |
| 1454 | } |
| 1455 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1456 | bool HInductionVarAnalysis::IsExact(const HBasicBlock* context, |
| 1457 | const HLoopInformation* loop, |
| 1458 | InductionInfo* info, |
| 1459 | /*out*/int64_t* value) { |
| 1460 | InductionVarRange range(this); |
| 1461 | return range.IsConstant(context, loop, info, InductionVarRange::kExact, value); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1464 | bool HInductionVarAnalysis::IsAtMost(const HBasicBlock* context, |
| 1465 | const HLoopInformation* loop, |
| 1466 | InductionInfo* info, |
| 1467 | /*out*/int64_t* value) { |
| 1468 | InductionVarRange range(this); |
| 1469 | return range.IsConstant(context, loop, info, InductionVarRange::kAtMost, value); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1472 | bool HInductionVarAnalysis::IsAtLeast(const HBasicBlock* context, |
| 1473 | const HLoopInformation* loop, |
| 1474 | InductionInfo* info, |
| 1475 | /*out*/int64_t* value) { |
| 1476 | InductionVarRange range(this); |
| 1477 | return range.IsConstant(context, loop, info, InductionVarRange::kAtLeast, value); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1480 | bool HInductionVarAnalysis::IsNarrowingLinear(InductionInfo* info) { |
| 1481 | return info != nullptr && |
| 1482 | info->induction_class == kLinear && |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 1483 | (info->type == DataType::Type::kUint8 || |
| 1484 | info->type == DataType::Type::kInt8 || |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1485 | info->type == DataType::Type::kUint16 || |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 1486 | info->type == DataType::Type::kInt16 || |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1487 | (info->type == DataType::Type::kInt32 && (info->op_a->type == DataType::Type::kInt64 || |
| 1488 | info->op_b->type == DataType::Type::kInt64))); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1491 | bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1, |
| 1492 | InductionInfo* info2) { |
| 1493 | // Test structural equality only, without accounting for simplifications. |
| 1494 | if (info1 != nullptr && info2 != nullptr) { |
| 1495 | return |
| 1496 | info1->induction_class == info2->induction_class && |
| 1497 | info1->operation == info2->operation && |
| 1498 | info1->fetch == info2->fetch && |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 1499 | info1->type == info2->type && |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1500 | InductionEqual(info1->op_a, info2->op_a) && |
| 1501 | InductionEqual(info1->op_b, info2->op_b); |
| 1502 | } |
| 1503 | // Otherwise only two nullptrs are considered equal. |
| 1504 | return info1 == info2; |
| 1505 | } |
| 1506 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1507 | std::string HInductionVarAnalysis::FetchToString(HInstruction* fetch) { |
| 1508 | DCHECK(fetch != nullptr); |
| 1509 | if (fetch->IsIntConstant()) { |
| 1510 | return std::to_string(fetch->AsIntConstant()->GetValue()); |
| 1511 | } else if (fetch->IsLongConstant()) { |
| 1512 | return std::to_string(fetch->AsLongConstant()->GetValue()); |
| 1513 | } |
| 1514 | return std::to_string(fetch->GetId()) + ":" + fetch->DebugName(); |
| 1515 | } |
| 1516 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1517 | std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) { |
| 1518 | if (info != nullptr) { |
| 1519 | if (info->induction_class == kInvariant) { |
| 1520 | std::string inv = "("; |
| 1521 | inv += InductionToString(info->op_a); |
| 1522 | switch (info->operation) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1523 | case kNop: inv += " @ "; break; |
| 1524 | case kAdd: inv += " + "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1525 | case kSub: |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1526 | case kNeg: inv += " - "; break; |
| 1527 | case kMul: inv += " * "; break; |
| 1528 | case kDiv: inv += " / "; break; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1529 | case kRem: inv += " % "; break; |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 1530 | case kXor: inv += " ^ "; break; |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1531 | case kLT: inv += " < "; break; |
| 1532 | case kLE: inv += " <= "; break; |
| 1533 | case kGT: inv += " > "; break; |
| 1534 | case kGE: inv += " >= "; break; |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1535 | case kFetch: inv += FetchToString(info->fetch); break; |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1536 | case kTripCountInLoop: inv += " (TC-loop) "; break; |
| 1537 | case kTripCountInBody: inv += " (TC-body) "; break; |
| 1538 | case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break; |
| 1539 | case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1540 | } |
| 1541 | inv += InductionToString(info->op_b); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1542 | inv += ")"; |
| 1543 | return inv; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1544 | } else { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1545 | if (info->induction_class == kLinear) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1546 | DCHECK(info->operation == kNop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1547 | return "(" + InductionToString(info->op_a) + " * i + " + |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1548 | InductionToString(info->op_b) + "):" + |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1549 | DataType::PrettyDescriptor(info->type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1550 | } else if (info->induction_class == kPolynomial) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1551 | DCHECK(info->operation == kNop); |
| 1552 | return "poly(sum_lt(" + InductionToString(info->op_a) + ") + " + |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1553 | InductionToString(info->op_b) + "):" + |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1554 | DataType::PrettyDescriptor(info->type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1555 | } else if (info->induction_class == kGeometric) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1556 | DCHECK(info->operation == kMul || info->operation == kDiv); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1557 | DCHECK(info->fetch != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1558 | return "geo(" + InductionToString(info->op_a) + " * " + |
| 1559 | FetchToString(info->fetch) + |
| 1560 | (info->operation == kMul ? " ^ i + " : " ^ -i + ") + |
| 1561 | InductionToString(info->op_b) + "):" + |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1562 | DataType::PrettyDescriptor(info->type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1563 | } else if (info->induction_class == kWrapAround) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1564 | DCHECK(info->operation == kNop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1565 | return "wrap(" + InductionToString(info->op_a) + ", " + |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1566 | InductionToString(info->op_b) + "):" + |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1567 | DataType::PrettyDescriptor(info->type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1568 | } else if (info->induction_class == kPeriodic) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1569 | DCHECK(info->operation == kNop); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1570 | return "periodic(" + InductionToString(info->op_a) + ", " + |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1571 | InductionToString(info->op_b) + "):" + |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1572 | DataType::PrettyDescriptor(info->type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | return ""; |
| 1577 | } |
| 1578 | |
| 1579 | } // namespace art |