| OLD | NEW |
| 1 //===- subzero/src/IceSwitchLowering.cpp - Switch lowering ----------------===// | 1 //===- subzero/src/IceSwitchLowering.cpp - Switch lowering ----------------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| 11 /// \brief Implements platform independent analysis of switch cases to improve | 11 /// \brief Implements platform independent analysis of switch cases to improve |
| 12 /// the generated code. | 12 /// the generated code. |
| 13 /// | 13 /// |
| 14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
| 15 #include "IceSwitchLowering.h" | 15 #include "IceSwitchLowering.h" |
| 16 | 16 |
| 17 #include "IceCfgNode.h" | 17 #include "IceCfgNode.h" |
| 18 #include "IceTargetLowering.h" | 18 #include "IceTargetLowering.h" |
| 19 | 19 |
| 20 #include <algorithm> | 20 #include <algorithm> |
| 21 | 21 |
| 22 namespace Ice { | 22 namespace Ice { |
| 23 | 23 |
| 24 CaseClusterArray CaseCluster::clusterizeSwitch(Cfg *Func, | 24 CaseClusterArray CaseCluster::clusterizeSwitch(Cfg *Func, |
| 25 const InstSwitch *Instr) { | 25 const InstSwitch *Instr) { |
| 26 const SizeT NumCases = Instr->getNumCases(); |
| 26 CaseClusterArray CaseClusters; | 27 CaseClusterArray CaseClusters; |
| 28 CaseClusters.reserve(NumCases); |
| 27 | 29 |
| 28 // Load the cases | 30 // Load the cases |
| 29 SizeT NumCases = Instr->getNumCases(); | |
| 30 CaseClusters.reserve(NumCases); | 31 CaseClusters.reserve(NumCases); |
| 31 for (SizeT I = 0; I < NumCases; ++I) | 32 for (SizeT I = 0; I < NumCases; ++I) |
| 32 CaseClusters.emplace_back(Instr->getValue(I), Instr->getLabel(I)); | 33 CaseClusters.emplace_back(Instr->getValue(I), Instr->getLabel(I)); |
| 33 | 34 |
| 34 // Sort the cases | 35 // Sort the cases |
| 35 std::sort(CaseClusters.begin(), CaseClusters.end(), | 36 std::sort(CaseClusters.begin(), CaseClusters.end(), |
| 36 [](const CaseCluster &x, const CaseCluster &y) { | 37 [](const CaseCluster &x, const CaseCluster &y) { |
| 37 return x.High < y.Low; | 38 return x.High < y.Low; |
| 38 }); | 39 }); |
| 39 | 40 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 53 if (CaseClusters.size() < Func->getTarget()->getMinJumpTableSize()) | 54 if (CaseClusters.size() < Func->getTarget()->getMinJumpTableSize()) |
| 54 return CaseClusters; | 55 return CaseClusters; |
| 55 | 56 |
| 56 // Test for a single jump table. This can be done in constant time whereas | 57 // Test for a single jump table. This can be done in constant time whereas |
| 57 // finding the best set of jump table would be quadratic, too slow(?). If | 58 // finding the best set of jump table would be quadratic, too slow(?). If |
| 58 // jump tables were included in the search tree we'd first have to traverse | 59 // jump tables were included in the search tree we'd first have to traverse |
| 59 // to them. Ideally we would have an unbalanced tree which is biased towards | 60 // to them. Ideally we would have an unbalanced tree which is biased towards |
| 60 // frequently executed code but we can't do this well without profiling data. | 61 // frequently executed code but we can't do this well without profiling data. |
| 61 // So, this single jump table is a good starting point where you can get to | 62 // So, this single jump table is a good starting point where you can get to |
| 62 // the jump table quickly without figuring out how to unbalance the tree. | 63 // the jump table quickly without figuring out how to unbalance the tree. |
| 63 uint64_t MaxValue = CaseClusters.back().High; | 64 const uint64_t MaxValue = CaseClusters.back().High; |
| 64 uint64_t MinValue = CaseClusters.front().Low; | 65 const uint64_t MinValue = CaseClusters.front().Low; |
| 65 // Don't +1 yet to avoid (INT64_MAX-0)+1 overflow | 66 // Don't +1 yet to avoid (INT64_MAX-0)+1 overflow |
| 66 uint64_t TotalRange = MaxValue - MinValue; | 67 const uint64_t Range = MaxValue - MinValue; |
| 67 | 68 |
| 68 // Might be too sparse for the jump table | 69 // Might be too sparse for the jump table |
| 69 if (NumCases * 2 <= TotalRange) | 70 if (NumCases * 2 <= Range) |
| 70 return CaseClusters; | 71 return CaseClusters; |
| 71 // Unlikely. Would mean can't store size of jump table. | 72 // Unlikely. Would mean can't store size of jump table. |
| 72 if (TotalRange == UINT64_MAX) | 73 if (Range == UINT64_MAX) |
| 73 return CaseClusters; | 74 return CaseClusters; |
| 74 ++TotalRange; | 75 const uint64_t TotalRange = Range + 1; |
| 75 | 76 |
| 76 // Replace everything with a jump table | 77 // Replace everything with a jump table |
| 77 InstJumpTable *JumpTable = | 78 auto *JumpTable = |
| 78 InstJumpTable::create(Func, TotalRange, Instr->getLabelDefault()); | 79 InstJumpTable::create(Func, TotalRange, Instr->getLabelDefault()); |
| 79 for (const CaseCluster &Case : CaseClusters) { | 80 for (const CaseCluster &Case : CaseClusters) { |
| 80 // Case.High could be UINT64_MAX which makes the loop awkward. Unwrap the | 81 // Case.High could be UINT64_MAX which makes the loop awkward. Unwrap the |
| 81 // last iteration to avoid wrap around problems. | 82 // last iteration to avoid wrap around problems. |
| 82 for (uint64_t I = Case.Low; I < Case.High; ++I) | 83 for (uint64_t I = Case.Low; I < Case.High; ++I) |
| 83 JumpTable->addTarget(I - MinValue, Case.Target); | 84 JumpTable->addTarget(I - MinValue, Case.Target); |
| 84 JumpTable->addTarget(Case.High - MinValue, Case.Target); | 85 JumpTable->addTarget(Case.High - MinValue, Case.Target); |
| 85 Case.Target->setNeedsAlignment(); | 86 Case.Target->setNeedsAlignment(); |
| 86 } | 87 } |
| 87 Func->addJumpTable(JumpTable); | 88 Func->addJumpTable(JumpTable); |
| 88 | 89 |
| 89 CaseClusters.clear(); | 90 CaseClusters.clear(); |
| 90 CaseClusters.emplace_back(MinValue, MaxValue, JumpTable); | 91 CaseClusters.emplace_back(MinValue, MaxValue, JumpTable); |
| 91 | 92 |
| 92 return CaseClusters; | 93 return CaseClusters; |
| 93 } | 94 } |
| 94 | 95 |
| 95 bool CaseCluster::tryAppend(const CaseCluster &New) { | 96 bool CaseCluster::tryAppend(const CaseCluster &New) { |
| 96 // Can only append ranges with the same target and are adjacent | 97 // Can only append ranges with the same target and are adjacent |
| 97 bool CanAppend = this->Target == New.Target && this->High + 1 == New.Low; | 98 const bool CanAppend = |
| 99 this->Target == New.Target && this->High + 1 == New.Low; |
| 98 if (CanAppend) | 100 if (CanAppend) |
| 99 this->High = New.High; | 101 this->High = New.High; |
| 100 return CanAppend; | 102 return CanAppend; |
| 101 } | 103 } |
| 102 | 104 |
| 103 } // end of namespace Ice | 105 } // end of namespace Ice |
| OLD | NEW |