10b57cec5SDimitry Andric //===- llvm/MC/LaneBitmask.h ------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// A common definition of LaneBitmask for use in TableGen and CodeGen.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric /// A lane mask is a bitmask representing the covering of a register with
130b57cec5SDimitry Andric /// sub-registers.
140b57cec5SDimitry Andric ///
150b57cec5SDimitry Andric /// This is typically used to track liveness at sub-register granularity.
160b57cec5SDimitry Andric /// Lane masks for sub-register indices are similar to register units for
170b57cec5SDimitry Andric /// physical registers. The individual bits in a lane mask can't be assigned
180b57cec5SDimitry Andric /// any specific meaning. They can be used to check if two sub-register
190b57cec5SDimitry Andric /// indices overlap.
200b57cec5SDimitry Andric ///
210b57cec5SDimitry Andric /// Iff the target has a register such that:
220b57cec5SDimitry Andric ///
230b57cec5SDimitry Andric ///   getSubReg(Reg, A) overlaps getSubReg(Reg, B)
240b57cec5SDimitry Andric ///
250b57cec5SDimitry Andric /// then:
260b57cec5SDimitry Andric ///
270b57cec5SDimitry Andric ///   (getSubRegIndexLaneMask(A) & getSubRegIndexLaneMask(B)) != 0
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric #ifndef LLVM_MC_LANEBITMASK_H
300b57cec5SDimitry Andric #define LLVM_MC_LANEBITMASK_H
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
330b57cec5SDimitry Andric #include "llvm/Support/Format.h"
34fe6060f1SDimitry Andric #include "llvm/Support/MathExtras.h"
350b57cec5SDimitry Andric #include "llvm/Support/Printable.h"
360b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric namespace llvm {
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   struct LaneBitmask {
410b57cec5SDimitry Andric     // When changing the underlying type, change the format string as well.
425ffd83dbSDimitry Andric     using Type = uint64_t;
430b57cec5SDimitry Andric     enum : unsigned { BitWidth = 8*sizeof(Type) };
445ffd83dbSDimitry Andric     constexpr static const char *const FormatStr = "%016llX";
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric     constexpr LaneBitmask() = default;
LaneBitmaskLaneBitmask470b57cec5SDimitry Andric     explicit constexpr LaneBitmask(Type V) : Mask(V) {}
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric     constexpr bool operator== (LaneBitmask M) const { return Mask == M.Mask; }
500b57cec5SDimitry Andric     constexpr bool operator!= (LaneBitmask M) const { return Mask != M.Mask; }
510b57cec5SDimitry Andric     constexpr bool operator< (LaneBitmask M)  const { return Mask < M.Mask; }
noneLaneBitmask520b57cec5SDimitry Andric     constexpr bool none() const { return Mask == 0; }
anyLaneBitmask530b57cec5SDimitry Andric     constexpr bool any()  const { return Mask != 0; }
allLaneBitmask540b57cec5SDimitry Andric     constexpr bool all()  const { return ~Mask == 0; }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric     constexpr LaneBitmask operator~() const {
570b57cec5SDimitry Andric       return LaneBitmask(~Mask);
580b57cec5SDimitry Andric     }
590b57cec5SDimitry Andric     constexpr LaneBitmask operator|(LaneBitmask M) const {
600b57cec5SDimitry Andric       return LaneBitmask(Mask | M.Mask);
610b57cec5SDimitry Andric     }
620b57cec5SDimitry Andric     constexpr LaneBitmask operator&(LaneBitmask M) const {
630b57cec5SDimitry Andric       return LaneBitmask(Mask & M.Mask);
640b57cec5SDimitry Andric     }
650b57cec5SDimitry Andric     LaneBitmask &operator|=(LaneBitmask M) {
660b57cec5SDimitry Andric       Mask |= M.Mask;
670b57cec5SDimitry Andric       return *this;
680b57cec5SDimitry Andric     }
690b57cec5SDimitry Andric     LaneBitmask &operator&=(LaneBitmask M) {
700b57cec5SDimitry Andric       Mask &= M.Mask;
710b57cec5SDimitry Andric       return *this;
720b57cec5SDimitry Andric     }
730b57cec5SDimitry Andric 
getAsIntegerLaneBitmask740b57cec5SDimitry Andric     constexpr Type getAsInteger() const { return Mask; }
750b57cec5SDimitry Andric 
getNumLanesLaneBitmask76bdd1243dSDimitry Andric     unsigned getNumLanes() const { return llvm::popcount(Mask); }
getHighestLaneLaneBitmask770b57cec5SDimitry Andric     unsigned getHighestLane() const {
785ffd83dbSDimitry Andric       return Log2_64(Mask);
790b57cec5SDimitry Andric     }
800b57cec5SDimitry Andric 
getNoneLaneBitmask810b57cec5SDimitry Andric     static constexpr LaneBitmask getNone() { return LaneBitmask(0); }
getAllLaneBitmask820b57cec5SDimitry Andric     static constexpr LaneBitmask getAll() { return ~LaneBitmask(0); }
getLaneLaneBitmask830b57cec5SDimitry Andric     static constexpr LaneBitmask getLane(unsigned Lane) {
840b57cec5SDimitry Andric       return LaneBitmask(Type(1) << Lane);
850b57cec5SDimitry Andric     }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   private:
880b57cec5SDimitry Andric     Type Mask = 0;
890b57cec5SDimitry Andric   };
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   /// Create Printable object to print LaneBitmasks on a \ref raw_ostream.
PrintLaneMask(LaneBitmask LaneMask)920b57cec5SDimitry Andric   inline Printable PrintLaneMask(LaneBitmask LaneMask) {
930b57cec5SDimitry Andric     return Printable([LaneMask](raw_ostream &OS) {
940b57cec5SDimitry Andric       OS << format(LaneBitmask::FormatStr, LaneMask.getAsInteger());
950b57cec5SDimitry Andric     });
960b57cec5SDimitry Andric   }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric } // end namespace llvm
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric #endif // LLVM_MC_LANEBITMASK_H
101