LaneBitmask.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- llvm/MC/LaneBitmask.h ------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// A common definition of LaneBitmask for use in TableGen and CodeGen.
  11. ///
  12. /// A lane mask is a bitmask representing the covering of a register with
  13. /// sub-registers.
  14. ///
  15. /// This is typically used to track liveness at sub-register granularity.
  16. /// Lane masks for sub-register indices are similar to register units for
  17. /// physical registers. The individual bits in a lane mask can't be assigned
  18. /// any specific meaning. They can be used to check if two sub-register
  19. /// indices overlap.
  20. ///
  21. /// Iff the target has a register such that:
  22. ///
  23. /// getSubReg(Reg, A) overlaps getSubReg(Reg, B)
  24. ///
  25. /// then:
  26. ///
  27. /// (getSubRegIndexLaneMask(A) & getSubRegIndexLaneMask(B)) != 0
  28. #ifndef LLVM_MC_LANEBITMASK_H
  29. #define LLVM_MC_LANEBITMASK_H
  30. #include "llvm/Support/Compiler.h"
  31. #include "llvm/Support/Format.h"
  32. #include "llvm/Support/Printable.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. namespace llvm {
  35. struct LaneBitmask {
  36. // When changing the underlying type, change the format string as well.
  37. using Type = uint64_t;
  38. enum : unsigned { BitWidth = 8*sizeof(Type) };
  39. constexpr static const char *const FormatStr = "%016llX";
  40. constexpr LaneBitmask() = default;
  41. explicit constexpr LaneBitmask(Type V) : Mask(V) {}
  42. constexpr bool operator== (LaneBitmask M) const { return Mask == M.Mask; }
  43. constexpr bool operator!= (LaneBitmask M) const { return Mask != M.Mask; }
  44. constexpr bool operator< (LaneBitmask M) const { return Mask < M.Mask; }
  45. constexpr bool none() const { return Mask == 0; }
  46. constexpr bool any() const { return Mask != 0; }
  47. constexpr bool all() const { return ~Mask == 0; }
  48. constexpr LaneBitmask operator~() const {
  49. return LaneBitmask(~Mask);
  50. }
  51. constexpr LaneBitmask operator|(LaneBitmask M) const {
  52. return LaneBitmask(Mask | M.Mask);
  53. }
  54. constexpr LaneBitmask operator&(LaneBitmask M) const {
  55. return LaneBitmask(Mask & M.Mask);
  56. }
  57. LaneBitmask &operator|=(LaneBitmask M) {
  58. Mask |= M.Mask;
  59. return *this;
  60. }
  61. LaneBitmask &operator&=(LaneBitmask M) {
  62. Mask &= M.Mask;
  63. return *this;
  64. }
  65. constexpr Type getAsInteger() const { return Mask; }
  66. unsigned getNumLanes() const {
  67. return countPopulation(Mask);
  68. }
  69. unsigned getHighestLane() const {
  70. return Log2_64(Mask);
  71. }
  72. static constexpr LaneBitmask getNone() { return LaneBitmask(0); }
  73. static constexpr LaneBitmask getAll() { return ~LaneBitmask(0); }
  74. static constexpr LaneBitmask getLane(unsigned Lane) {
  75. return LaneBitmask(Type(1) << Lane);
  76. }
  77. private:
  78. Type Mask = 0;
  79. };
  80. /// Create Printable object to print LaneBitmasks on a \ref raw_ostream.
  81. inline Printable PrintLaneMask(LaneBitmask LaneMask) {
  82. return Printable([LaneMask](raw_ostream &OS) {
  83. OS << format(LaneBitmask::FormatStr, LaneMask.getAsInteger());
  84. });
  85. }
  86. } // end namespace llvm
  87. #endif // LLVM_MC_LANEBITMASK_H