Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceSwitchLowering.h - Switch lowering --------*- C++ -*-===// | 1 //===- subzero/src/IceSwitchLowering.h - Switch lowering --------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 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 The file contains helpers for switch lowering. | 11 /// \brief The file contains helpers for switch lowering. |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #ifndef SUBZERO_SRC_ICESWITCHLOWERING_H | 14 #ifndef SUBZERO_SRC_ICESWITCHLOWERING_H |
| 15 #define SUBZERO_SRC_ICESWITCHLOWERING_H | 15 #define SUBZERO_SRC_ICESWITCHLOWERING_H |
| 16 | 16 |
| 17 #include "IceCfgNode.h" | 17 #include "IceDefs.h" |
| 18 #include "IceInst.h" | |
| 19 | 18 |
| 20 namespace Ice { | 19 namespace Ice { |
| 21 | 20 |
| 22 class CaseCluster; | 21 class CaseCluster; |
| 22 class Cfg; | |
|
Jim Stichnoth
2015/07/30 15:20:24
At least Cfg and CfgNode forward declarations shou
ascull
2015/07/30 17:29:59
Done.
| |
| 23 class CfgNode; | |
| 24 class InstJumpTable; | |
| 25 class InstSwitch; | |
| 23 | 26 |
| 24 typedef std::vector<CaseCluster, CfgLocalAllocator<CaseCluster>> | 27 typedef std::vector<CaseCluster, CfgLocalAllocator<CaseCluster>> |
| 25 CaseClusterArray; | 28 CaseClusterArray; |
| 26 | 29 |
| 27 /// A cluster of cases can be tested by a common method during switch lowering. | 30 /// A cluster of cases can be tested by a common method during switch lowering. |
| 28 class CaseCluster { | 31 class CaseCluster { |
| 29 CaseCluster() = delete; | 32 CaseCluster() = delete; |
| 30 | 33 |
| 31 public: | 34 public: |
| 32 enum CaseClusterKind { | 35 enum CaseClusterKind { |
| 33 Range, /// Numerically adjacent case values with same target. | 36 Range, /// Numerically adjacent case values with same target. |
| 34 JumpTable, /// Different targets and possibly sparse. | 37 JumpTable, /// Different targets and possibly sparse. |
| 35 }; | 38 }; |
| 36 | 39 |
| 37 CaseCluster(const CaseCluster &) = default; | 40 CaseCluster(const CaseCluster &) = default; |
| 38 CaseCluster &operator=(const CaseCluster &) = default; | 41 CaseCluster &operator=(const CaseCluster &) = default; |
| 39 | 42 |
| 40 /// Create a cluster of a single case represented by a unitary range. | 43 /// Create a cluster of a single case represented by a unitary range. |
| 41 CaseCluster(uint64_t Value, CfgNode *Label) | 44 CaseCluster(uint64_t Value, CfgNode *Target) |
| 42 : Kind(Range), Low(Value), High(Value), Label(Label) {} | 45 : Kind(Range), Low(Value), High(Value), Target(Target) {} |
| 43 /// Create a case consisting of a jump table. | 46 /// Create a case consisting of a jump table. |
| 44 CaseCluster(uint64_t Low, uint64_t High, InstJumpTable *JT) | 47 CaseCluster(uint64_t Low, uint64_t High, InstJumpTable *JT) |
| 45 : Kind(JumpTable), Low(Low), High(High), JT(JT) {} | 48 : Kind(JumpTable), Low(Low), High(High), JT(JT) {} |
| 46 | 49 |
| 47 CaseClusterKind getKind() const { return Kind; } | 50 CaseClusterKind getKind() const { return Kind; } |
| 48 uint64_t getLow() const { return Low; } | 51 uint64_t getLow() const { return Low; } |
| 49 uint64_t getHigh() const { return High; } | 52 uint64_t getHigh() const { return High; } |
| 50 CfgNode *getLabel() const { | 53 CfgNode *getTarget() const { |
| 51 assert(Kind == Range); | 54 assert(Kind == Range); |
| 52 return Label; | 55 return Target; |
| 53 } | 56 } |
| 54 InstJumpTable *getJumpTable() const { | 57 InstJumpTable *getJumpTable() const { |
| 55 assert(Kind == JumpTable); | 58 assert(Kind == JumpTable); |
| 56 return JT; | 59 return JT; |
| 57 } | 60 } |
| 58 | 61 |
| 62 bool isUnitRange() const { return Low == High; } | |
| 63 bool isPairRange() const { return Low == High - 1; } | |
| 64 | |
| 59 /// Discover cases which can be clustered together and return the clusters | 65 /// Discover cases which can be clustered together and return the clusters |
| 60 /// ordered by case value. | 66 /// ordered by case value. |
| 61 static CaseClusterArray clusterizeSwitch(Cfg *Func, const InstSwitch *Inst); | 67 static CaseClusterArray clusterizeSwitch(Cfg *Func, const InstSwitch *Inst); |
| 62 | 68 |
| 63 private: | 69 private: |
| 64 CaseClusterKind Kind; | 70 CaseClusterKind Kind; |
| 65 uint64_t Low; | 71 uint64_t Low; |
| 66 uint64_t High; | 72 uint64_t High; |
| 67 union { | 73 union { |
| 68 CfgNode *Label; /// Target for a range. | 74 CfgNode *Target; /// Target for a range. |
| 69 InstJumpTable *JT; /// Jump table targets. | 75 InstJumpTable *JT; /// Jump table targets. |
| 70 }; | 76 }; |
| 71 | 77 |
| 72 /// Try and append a cluster returning whether or not it was successful. | 78 /// Try and append a cluster returning whether or not it was successful. |
| 73 bool tryAppend(const CaseCluster &New); | 79 bool tryAppend(const CaseCluster &New); |
| 74 }; | 80 }; |
| 75 | 81 |
| 82 /// Store the jump table data so that it can be emitted later in the correct | |
| 83 /// ELF section once the offsets from the start of the function are known. | |
| 84 class JumpTableData { | |
| 85 JumpTableData() = delete; | |
| 86 JumpTableData(const JumpTableData &) = delete; | |
| 87 JumpTableData &operator=(const JumpTableData &) = delete; | |
| 88 | |
| 89 public: | |
| 90 JumpTableData(IceString FuncName, SizeT Id, SizeT NumTargets) | |
| 91 : FuncName(FuncName), Id(Id) { | |
| 92 TargetOffsets.reserve(NumTargets); | |
| 93 } | |
| 94 JumpTableData(JumpTableData &&) = default; | |
| 95 | |
| 96 void pushTarget(intptr_t Offset) { TargetOffsets.emplace_back(Offset); } | |
| 97 | |
| 98 const IceString &getFunctionName() const { return FuncName; } | |
| 99 SizeT getId() const { return Id; } | |
| 100 const std::vector<intptr_t> &getTargetOffsets() const { | |
| 101 return TargetOffsets; | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 const IceString FuncName; | |
| 106 const SizeT Id; | |
| 107 std::vector<intptr_t> TargetOffsets; | |
| 108 }; | |
| 109 | |
| 76 } // end of namespace Ice | 110 } // end of namespace Ice |
| 77 | 111 |
| 78 #endif // SUBZERO_SRC_ICESWITCHLOWERING_H | 112 #endif // SUBZERO_SRC_ICESWITCHLOWERING_H |
| OLD | NEW |