1 //===-- SIDefines.h - SI Helper Functions -----------------------*- C++ -*-===//
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
7 /// \file - utility functions for the SIDefines and its common uses.
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_SIDEFINESUTILS_H
12 #define LLVM_LIB_TARGET_AMDGPU_UTILS_SIDEFINESUTILS_H
14 #include "llvm/MC/MCExpr.h"
21 /// Deduce the least significant bit aligned shift and mask values for a binary
22 /// Complement \p Value (as they're defined in SIDefines.h as C_*) as a returned
23 /// pair<shift, mask>. That is to say \p Value == ~(mask << shift)
25 /// For example, given C_00B848_FWD_PROGRESS (i.e., 0x7FFFFFFF) from
26 /// SIDefines.h, this will return the pair as (31,1).
27 constexpr std::pair
<unsigned, unsigned> getShiftMask(unsigned Value
) {
32 for (; !(Mask
& 1); Shift
++, Mask
>>= 1) {
35 return std::make_pair(Shift
, Mask
);
38 /// Provided with the MCExpr * \p Val, uint32 \p Mask and \p Shift, will return
39 /// the masked and left shifted, in said order of operations, MCExpr * created
40 /// within the MCContext \p Ctx.
42 /// For example, given MCExpr *Val, Mask == 0xf, Shift == 6 the returned MCExpr
43 /// * will be the equivalent of (Val & 0xf) << 6
44 inline const MCExpr
*maskShiftSet(const MCExpr
*Val
, uint32_t Mask
,
45 uint32_t Shift
, MCContext
&Ctx
) {
47 const MCExpr
*MaskExpr
= MCConstantExpr::create(Mask
, Ctx
);
48 Val
= MCBinaryExpr::createAnd(Val
, MaskExpr
, Ctx
);
51 const MCExpr
*ShiftExpr
= MCConstantExpr::create(Shift
, Ctx
);
52 Val
= MCBinaryExpr::createShl(Val
, ShiftExpr
, Ctx
);
57 /// Provided with the MCExpr * \p Val, uint32 \p Mask and \p Shift, will return
58 /// the right shifted and masked, in said order of operations, MCExpr * created
59 /// within the MCContext \p Ctx.
61 /// For example, given MCExpr *Val, Mask == 0xf, Shift == 6 the returned MCExpr
62 /// * will be the equivalent of (Val >> 6) & 0xf
63 inline const MCExpr
*maskShiftGet(const MCExpr
*Val
, uint32_t Mask
,
64 uint32_t Shift
, MCContext
&Ctx
) {
66 const MCExpr
*ShiftExpr
= MCConstantExpr::create(Shift
, Ctx
);
67 Val
= MCBinaryExpr::createLShr(Val
, ShiftExpr
, Ctx
);
70 const MCExpr
*MaskExpr
= MCConstantExpr::create(Mask
, Ctx
);
71 Val
= MCBinaryExpr::createAnd(Val
, MaskExpr
, Ctx
);
76 } // end namespace AMDGPU
77 } // end namespace llvm
79 #endif // LLVM_LIB_TARGET_AMDGPU_UTILS_SIDEFINESUTILS_H