Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(726)

Side by Side Diff: src/IceSwitchLowering.cpp

Issue 1234803007: Introduction of improved switch lowering. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //===----- subzero/src/IceSwitchLowering.h - Switch lowering -----*- C++ -*-==//
jvoung (off chromium) 2015/07/15 18:32:01 .cpp and no need for the -*- C++ -*- in the .cpp
Jim Stichnoth 2015/07/15 19:17:42 .cpp , and also don't need the -*- C++ -*- stuff.
ascull 2015/07/16 19:38:45 Done.
2 //
3 // The Subzero Code Generator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements platform independent analysis of switch cases to
12 /// improve the generated code.
13 ///
14 //===----------------------------------------------------------------------===//
15 #include "IceSwitchLowering.h"
16 #include "IceTargetLowering.h"
17
18 #include <algorithm>
19
20 namespace Ice {
21
22 CaseClusterArray CaseCluster::clusterizeSwitch(Cfg *Func,
23 const InstSwitch *Inst) {
24
25 CaseClusterArray CaseClusters;
26
27 // Load the cases
28 SizeT NumCases = Inst->getNumCases();
29 CaseClusters.reserve(NumCases);
30 for (SizeT I = 0; I < NumCases; ++I)
31 CaseClusters.emplace_back(Inst->getValue(I), Inst->getLabel(I));
32
33 // Sort the cases
34 std::sort(CaseClusters.begin(), CaseClusters.end(),
35 [](const CaseCluster& x, const CaseCluster& y) {
36 return x.High < y.Low;
37 });
38
39 // Merge adjacent case ranges
40 auto Active = CaseClusters.begin();
41 std::for_each(Active+1, CaseClusters.end(), [&Active](const CaseCluster& x) {
42 if (!Active->tryAppend(x)) *(++Active) = x;
43 });
44 CaseClusters.erase(Active+1, CaseClusters.end());
45
46 // TODO(ascull): Merge in a cycle i.e. -1(=UINTXX_MAX) to 0. This depends on
47 // the types for correct wrap around behavior.
48
49 // Small number of cases is trivial
jvoung (off chromium) 2015/07/15 18:32:01 By "trivial" you mean that loading from the jump t
ascull 2015/07/16 19:38:45 Done.
50 if (CaseClusters.size() < Func->getTarget()->getMinJumpTableSize())
51 return CaseClusters;
52
53 // Test for a single jump table
jvoung (off chromium) 2015/07/15 18:32:01 I think it would be good to document why a single
ascull 2015/07/16 19:38:45 Done.
54 uint64_t MaxValue = CaseClusters.back().High;
55 uint64_t MinValue = CaseClusters.front().Low;
56 // Don't +1 yet to avoid (INT64_MAX-0)+1 overflow
57 uint64_t TotalRange = MaxValue - MinValue;
58
59 // Might be too sparse for the jump table
60 if (NumCases * 2 <= TotalRange)
61 return CaseClusters;
62 // Unlikely. Would mean can't store size of jump table.
63 if (TotalRange == UINT64_MAX)
64 return CaseClusters;
65 ++TotalRange;
66
67 // Replace everything with a jump table
68 InstJumpTable *JumpTable = InstJumpTable::create(Func,
69 TotalRange,
70 Inst->getLabelDefault());
71 for (const CaseCluster& Case : CaseClusters)
72 for (uint64_t I = Case.Low; I <= Case.High; ++I)
73 JumpTable->addTarget(I-MinValue, Case.Label);
74
75 CaseClusters.clear();
76 CaseClusters.emplace_back(MinValue, MaxValue, JumpTable);
77
78 return CaseClusters;
79 }
80
81 bool CaseCluster::tryAppend(const CaseCluster& x) {
jvoung (off chromium) 2015/07/15 18:32:01 "x" -> "X"
Jim Stichnoth 2015/07/15 19:17:42 More descriptive argument name than "x"? Capitali
ascull 2015/07/16 19:38:45 Done.
82 // Can only append ranges with the same target and are adjacent
83 bool can = this->Label == x.Label && this->High+1 == x.Low;
jvoung (off chromium) 2015/07/15 18:32:01 Start variable name with capital letter ("Can")
ascull 2015/07/16 19:38:45 Done.
84 if (can) this->High = x.High;
85 return can;
86 }
87
88 } // end of namespace Ice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698