LaneBitmask.h 3.4 KB

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