|
| 1 | +//===--- TerminatorUtils.h ------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// \file |
| 14 | +/// |
| 15 | +/// ADTs for working with various forms of terminators. |
| 16 | +/// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#ifndef SWIFT_SIL_TERMINATORUTILS_H |
| 20 | +#define SWIFT_SIL_TERMINATORUTILS_H |
| 21 | + |
| 22 | +#include "swift/Basic/LLVM.h" |
| 23 | +#include "swift/SIL/SILInstruction.h" |
| 24 | + |
| 25 | +#include "llvm/ADT/PointerUnion.h" |
| 26 | + |
| 27 | +namespace swift { |
| 28 | + |
| 29 | +/// An ADT for writing generic code against SwitchEnumAddrInst and |
| 30 | +/// SwitchEnumInst. |
| 31 | +/// |
| 32 | +/// We use this instead of SwitchEnumInstBase for this purpose in order to avoid |
| 33 | +/// the need for templating SwitchEnumInstBase from causing this ADT type of |
| 34 | +/// usage to require templates. |
| 35 | +class SwitchEnumTermInst { |
| 36 | + PointerUnion<SwitchEnumAddrInst *, SwitchEnumInst *> value; |
| 37 | + |
| 38 | +public: |
| 39 | + SwitchEnumTermInst(SwitchEnumAddrInst *seai) : value(seai) {} |
| 40 | + SwitchEnumTermInst(SwitchEnumInst *seai) : value(seai) {} |
| 41 | + SwitchEnumTermInst(SILInstruction *i) : value(nullptr) { |
| 42 | + if (auto *seai = dyn_cast<SwitchEnumAddrInst>(i)) { |
| 43 | + value = seai; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (auto *sei = dyn_cast<SwitchEnumInst>(i)) { |
| 48 | + value = sei; |
| 49 | + return; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + SwitchEnumTermInst(const SILInstruction *i) |
| 54 | + : SwitchEnumTermInst(const_cast<SILInstruction *>(i)) {} |
| 55 | + |
| 56 | + operator TermInst *() const { |
| 57 | + if (auto *seai = value.dyn_cast<SwitchEnumAddrInst *>()) |
| 58 | + return seai; |
| 59 | + return value.get<SwitchEnumInst *>(); |
| 60 | + } |
| 61 | + |
| 62 | + TermInst *operator*() const { |
| 63 | + if (auto *seai = value.dyn_cast<SwitchEnumAddrInst *>()) |
| 64 | + return seai; |
| 65 | + return value.get<SwitchEnumInst *>(); |
| 66 | + } |
| 67 | + |
| 68 | + TermInst *operator->() const { |
| 69 | + if (auto *seai = value.dyn_cast<SwitchEnumAddrInst *>()) |
| 70 | + return seai; |
| 71 | + return value.get<SwitchEnumInst *>(); |
| 72 | + } |
| 73 | + |
| 74 | + operator bool() const { return bool(value); } |
| 75 | + |
| 76 | + SILValue getOperand() { |
| 77 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 78 | + return sei->getOperand(); |
| 79 | + return value.get<SwitchEnumAddrInst *>()->getOperand(); |
| 80 | + } |
| 81 | + |
| 82 | + unsigned getNumCases() { |
| 83 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 84 | + return sei->getNumCases(); |
| 85 | + return value.get<SwitchEnumAddrInst *>()->getNumCases(); |
| 86 | + } |
| 87 | + |
| 88 | + std::pair<EnumElementDecl *, SILBasicBlock *> getCase(unsigned i) const { |
| 89 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 90 | + return sei->getCase(i); |
| 91 | + return value.get<SwitchEnumAddrInst *>()->getCase(i); |
| 92 | + } |
| 93 | + |
| 94 | + SILBasicBlock *getCaseDestination(EnumElementDecl *decl) const { |
| 95 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 96 | + return sei->getCaseDestination(decl); |
| 97 | + return value.get<SwitchEnumAddrInst *>()->getCaseDestination(decl); |
| 98 | + } |
| 99 | + |
| 100 | + ProfileCounter getCaseCount(unsigned i) const { |
| 101 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 102 | + return sei->getCaseCount(i); |
| 103 | + return value.get<SwitchEnumAddrInst *>()->getCaseCount(i); |
| 104 | + } |
| 105 | + |
| 106 | + ProfileCounter getDefaultCount() const { |
| 107 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 108 | + return sei->getDefaultCount(); |
| 109 | + return value.get<SwitchEnumAddrInst *>()->getDefaultCount(); |
| 110 | + } |
| 111 | + |
| 112 | + bool hasDefault() const { |
| 113 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 114 | + return sei->hasDefault(); |
| 115 | + return value.get<SwitchEnumAddrInst *>()->hasDefault(); |
| 116 | + } |
| 117 | + |
| 118 | + SILBasicBlock *getDefaultBB() const { |
| 119 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 120 | + return sei->getDefaultBB(); |
| 121 | + return value.get<SwitchEnumAddrInst *>()->getDefaultBB(); |
| 122 | + } |
| 123 | + |
| 124 | + NullablePtr<SILBasicBlock> getDefaultBBOrNull() const { |
| 125 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 126 | + return sei->getDefaultBBOrNull(); |
| 127 | + return value.get<SwitchEnumAddrInst *>()->getDefaultBBOrNull(); |
| 128 | + } |
| 129 | + |
| 130 | + /// If the default refers to exactly one case decl, return it. |
| 131 | + NullablePtr<EnumElementDecl> getUniqueCaseForDefault() const { |
| 132 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 133 | + return sei->getUniqueCaseForDefault(); |
| 134 | + return value.get<SwitchEnumAddrInst *>()->getUniqueCaseForDefault(); |
| 135 | + } |
| 136 | + |
| 137 | + /// If the given block only has one enum element decl matched to it, |
| 138 | + /// return it. |
| 139 | + NullablePtr<EnumElementDecl> |
| 140 | + getUniqueCaseForDestination(SILBasicBlock *BB) const { |
| 141 | + if (auto *sei = value.dyn_cast<SwitchEnumInst *>()) |
| 142 | + return sei->getUniqueCaseForDestination(BB); |
| 143 | + return value.get<SwitchEnumAddrInst *>()->getUniqueCaseForDestination(BB); |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +} // namespace swift |
| 148 | + |
| 149 | +#endif |
0 commit comments