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