19 namespace AArch64_IMM {
23 static uint64_t
getChunk(uint64_t Imm,
unsigned ChunkIdx) {
24 assert(ChunkIdx < 4 &&
"Out of range chunk index specified!");
26 return (Imm >> (ChunkIdx * 16)) & 0xFFFF;
31 static bool canUseOrr(uint64_t Chunk, uint64_t &Encoding) {
32 Chunk = (Chunk << 48) | (Chunk << 32) | (Chunk << 16) | Chunk;
51 for (
unsigned Idx = 0; Idx < 4; ++Idx)
55 for (CountMap::const_iterator Chunk = Counts.begin(), End = Counts.end();
56 Chunk != End; ++Chunk) {
57 const uint64_t ChunkVal = Chunk->first;
58 const unsigned Count = Chunk->second;
60 uint64_t Encoding = 0;
64 if ((Count != 2 && Count != 3) || !
canUseOrr(ChunkVal, Encoding))
67 const bool CountThree = Count == 3;
69 Insn.
push_back({ AArch64::ORRXri, 0, Encoding });
71 unsigned ShiftAmt = 0;
74 for (; ShiftAmt < 64; ShiftAmt += 16) {
75 Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
77 if (Imm16 != ChunkVal)
91 for (ShiftAmt += 16; ShiftAmt < 64; ShiftAmt += 16) {
92 Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
94 if (Imm16 != ChunkVal)
126 static uint64_t
updateImm(uint64_t Imm,
unsigned Idx,
bool Clear) {
127 const uint64_t
Mask = 0xFFFF;
131 Imm &= ~(Mask << (Idx * 16));
134 Imm |= Mask << (Idx * 16);
154 const int NotSet = -1;
155 const uint64_t
Mask = 0xFFFF;
157 int StartIdx = NotSet;
160 for (
int Idx = 0; Idx < 4; ++Idx) {
161 int64_t Chunk =
getChunk(UImm, Idx);
163 Chunk = (Chunk << 48) >> 48;
172 if (StartIdx == NotSet || EndIdx == NotSet)
176 uint64_t Outside = 0;
178 uint64_t Inside =
Mask;
183 if (StartIdx > EndIdx) {
188 uint64_t OrrImm = UImm;
189 int FirstMovkIdx = NotSet;
190 int SecondMovkIdx = NotSet;
194 for (
int Idx = 0; Idx < 4; ++Idx) {
195 const uint64_t Chunk =
getChunk(UImm, Idx);
199 if ((Idx < StartIdx || EndIdx < Idx) && Chunk != Outside) {
200 OrrImm =
updateImm(OrrImm, Idx, Outside == 0);
203 if (FirstMovkIdx == NotSet)
210 }
else if (Idx > StartIdx && Idx < EndIdx && Chunk != Inside) {
211 OrrImm =
updateImm(OrrImm, Idx, Inside != Mask);
214 if (FirstMovkIdx == NotSet)
220 assert(FirstMovkIdx != NotSet &&
"Constant materializable with single ORR!");
223 uint64_t Encoding = 0;
225 Insn.
push_back({ AArch64::ORRXri, 0, Encoding });
227 const bool SingleMovk = SecondMovkIdx == NotSet;
230 FirstMovkIdx * 16) });
239 SecondMovkIdx * 16) });
247 unsigned OneChunks,
unsigned ZeroChunks,
249 const unsigned Mask = 0xFFFF;
258 if (OneChunks > ZeroChunks) {
265 Imm &= (1LL << 32) - 1;
266 FirstOpc = (isNeg ? AArch64::MOVNWi : AArch64::MOVZWi);
268 FirstOpc = (isNeg ? AArch64::MOVNXi : AArch64::MOVZXi);
271 unsigned LastShift = 0;
275 Shift = (TZ / 16) * 16;
276 LastShift = ((63 - LZ) / 16) * 16;
278 unsigned Imm16 = (Imm >> Shift) & Mask;
283 if (Shift == LastShift)
291 unsigned Opc = (BitSize == 32 ? AArch64::MOVKWi : AArch64::MOVKXi);
292 while (Shift < LastShift) {
294 Imm16 = (Imm >> Shift) & Mask;
295 if (Imm16 == (isNeg ? Mask : 0))
307 const unsigned Mask = 0xFFFF;
311 unsigned OneChunks = 0;
312 unsigned ZeroChunks = 0;
313 for (
unsigned Shift = 0; Shift < BitSize; Shift += 16) {
314 const unsigned Chunk = (Imm >> Shift) & Mask;
322 if ((BitSize / 16) - OneChunks <= 1 || (BitSize / 16) - ZeroChunks <= 1) {
328 uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
331 unsigned Opc = (BitSize == 32 ? AArch64::ORRWri : AArch64::ORRXri);
340 if (OneChunks >= (BitSize / 16) - 2 || ZeroChunks >= (BitSize / 16) - 2) {
345 assert(BitSize == 64 &&
"All 32-bit immediates can be expanded with a" 356 for (
unsigned Shift = 0; Shift < BitSize; Shift += 16) {
357 uint64_t ShiftedMask = (0xFFFFULL << Shift);
358 uint64_t ZeroChunk = UImm & ~ShiftedMask;
359 uint64_t OneChunk = UImm | ShiftedMask;
360 uint64_t RotatedImm = (UImm << 32) | (UImm >> 32);
361 uint64_t ReplicateChunk = ZeroChunk | (RotatedImm & ShiftedMask);
367 Insn.
push_back({ AArch64::ORRXri, 0, Encoding });
385 if (OneChunks || ZeroChunks) {
static bool isStartChunk(uint64_t Chunk)
Check whether this chunk matches the pattern '1...0...'.
This class represents lattice values for constants.
void push_back(const T &Elt)
static bool processLogicalImmediate(uint64_t Imm, unsigned RegSize, uint64_t &Encoding)
processLogicalImmediate - Determine if an immediate value can be encoded as the immediate operand of ...
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
static uint64_t updateImm(uint64_t Imm, unsigned Idx, bool Clear)
Clear or set all bits in the chunk at the given index.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
static bool tryToreplicateChunks(uint64_t UImm, SmallVectorImpl< ImmInsnModel > &Insn)
Check for identical 16-bit chunks within the constant and if so materialize them with a single ORR in...
static bool canUseOrr(uint64_t Chunk, uint64_t &Encoding)
Check whether the given 16-bit chunk replicated to full 64-bit width can be materialized with an ORR ...
static unsigned getShifterImm(AArch64_AM::ShiftExtendType ST, unsigned Imm)
getShifterImm - Encode the shift type and amount: imm: 6-bit shift amount shifter: 000 ==> lsl 001 ==...
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
static bool trySequenceOfOnes(uint64_t UImm, SmallVectorImpl< ImmInsnModel > &Insn)
Check whether the constant contains a sequence of contiguous ones, which might be interrupted by one ...
static uint64_t getChunk(uint64_t Imm, unsigned ChunkIdx)
Helper function which extracts the specified 16-bit chunk from a 64-bit value.
Align max(MaybeAlign Lhs, Align Rhs)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
void expandMOVImm(uint64_t Imm, unsigned BitSize, SmallVectorImpl< ImmInsnModel > &Insn)
Expand a MOVi32imm or MOVi64imm pseudo instruction to one or more real move-immediate instructions to...
static bool isEndChunk(uint64_t Chunk)
Check whether this chunk matches the pattern '0...1...' This pattern ends a contiguous sequence of on...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
static void expandMOVImmSimple(uint64_t Imm, unsigned BitSize, unsigned OneChunks, unsigned ZeroChunks, SmallVectorImpl< ImmInsnModel > &Insn)
Expand a MOVi32imm or MOVi64imm pseudo instruction to a MOVZ or MOVN of width BitSize followed by up ...