#include "AArch64.h"
#include "AArch64ExpandImm.h"
#include "MCTargetDesc/AArch64AddressingModes.h"
using namespace llvm;
using namespace llvm::AArch64_IMM;
static uint64_t getChunk(uint64_t Imm, unsigned ChunkIdx) {
assert(ChunkIdx < 4 && "Out of range chunk index specified!");
return (Imm >> (ChunkIdx * 16)) & 0xFFFF;
}
static bool canUseOrr(uint64_t Chunk, uint64_t &Encoding) {
Chunk = (Chunk << 48) | (Chunk << 32) | (Chunk << 16) | Chunk;
return AArch64_AM::processLogicalImmediate(Chunk, 64, Encoding);
}
static bool tryToreplicateChunks(uint64_t UImm,
SmallVectorImpl<ImmInsnModel> &Insn) {
using CountMap = DenseMap<uint64_t, unsigned>;
CountMap Counts;
for (unsigned Idx = 0; Idx < 4; ++Idx)
++Counts[getChunk(UImm, Idx)];
for (const auto &Chunk : Counts) {
const uint64_t ChunkVal = Chunk.first;
const unsigned Count = Chunk.second;
uint64_t Encoding = 0;
if ((Count != 2 && Count != 3) || !canUseOrr(ChunkVal, Encoding))
continue;
const bool CountThree = Count == 3;
Insn.push_back({ AArch64::ORRXri, 0, Encoding });
unsigned ShiftAmt = 0;
uint64_t Imm16 = 0;
for (; ShiftAmt < 64; ShiftAmt += 16) {
Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
if (Imm16 != ChunkVal)
break;
}
Insn.push_back({ AArch64::MOVKXi, Imm16,
AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt) });
if (CountThree)
return true;
for (ShiftAmt += 16; ShiftAmt < 64; ShiftAmt += 16) {
Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
if (Imm16 != ChunkVal)
break;
}
Insn.push_back({ AArch64::MOVKXi, Imm16,
AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt) });
return true;
}
return false;
}
static bool isStartChunk(uint64_t Chunk) {
if (Chunk == 0 || Chunk == std::numeric_limits<uint64_t>::max())
return false;
return isMask_64(~Chunk);
}
static bool isEndChunk(uint64_t Chunk) {
if (Chunk == 0 || Chunk == std::numeric_limits<uint64_t>::max())
return false;
return isMask_64(Chunk);
}
static uint64_t updateImm(uint64_t Imm, unsigned Idx, bool Clear) {
const uint64_t Mask = 0xFFFF;
if (Clear)
Imm &= ~(Mask << (Idx * 16));
else
Imm |= Mask << (Idx * 16);
return Imm;
}
static bool trySequenceOfOnes(uint64_t UImm,
SmallVectorImpl<ImmInsnModel> &Insn) {
const int NotSet = -1;
const uint64_t Mask = 0xFFFF;
int StartIdx = NotSet;
int EndIdx = NotSet;
for (int Idx = 0; Idx < 4; ++Idx) {
int64_t Chunk = getChunk(UImm, Idx);
Chunk = (Chunk << 48) >> 48;
if (isStartChunk(Chunk))
StartIdx = Idx;
else if (isEndChunk(Chunk))
EndIdx = Idx;
}
if (StartIdx == NotSet || EndIdx == NotSet)
return false;
uint64_t Outside = 0;
uint64_t Inside = Mask;
if (StartIdx > EndIdx) {
std::swap(StartIdx, EndIdx);
std::swap(Outside, Inside);
}
uint64_t OrrImm = UImm;
int FirstMovkIdx = NotSet;
int SecondMovkIdx = NotSet;
for (int Idx = 0; Idx < 4; ++Idx) {
const uint64_t Chunk = getChunk(UImm, Idx);
if ((Idx < StartIdx || EndIdx < Idx) && Chunk != Outside) {
OrrImm = updateImm(OrrImm, Idx, Outside == 0);
if (FirstMovkIdx == NotSet)
FirstMovkIdx = Idx;
else
SecondMovkIdx = Idx;
} else if (Idx > StartIdx && Idx < EndIdx && Chunk != Inside) {
OrrImm = updateImm(OrrImm, Idx, Inside != Mask);
if (FirstMovkIdx == NotSet)
FirstMovkIdx = Idx;
else
SecondMovkIdx = Idx;
}
}
assert(FirstMovkIdx != NotSet && "Constant materializable with single ORR!");
uint64_t Encoding = 0;
AArch64_AM::processLogicalImmediate(OrrImm, 64, Encoding);
Insn.push_back({ AArch64::ORRXri, 0, Encoding });
const bool SingleMovk = SecondMovkIdx == NotSet;
Insn.push_back({ AArch64::MOVKXi, getChunk(UImm, FirstMovkIdx),
AArch64_AM::getShifterImm(AArch64_AM::LSL,
FirstMovkIdx * 16) });
if (SingleMovk)
return true;
Insn.push_back({ AArch64::MOVKXi, getChunk(UImm, SecondMovkIdx),
AArch64_AM::getShifterImm(AArch64_AM::LSL,
SecondMovkIdx * 16) });
return true;
}
static inline void expandMOVImmSimple(uint64_t Imm, unsigned BitSize,
unsigned OneChunks, unsigned ZeroChunks,
SmallVectorImpl<ImmInsnModel> &Insn) {
const unsigned Mask = 0xFFFF;
bool isNeg = false;
if (OneChunks > ZeroChunks) {
isNeg = true;
Imm = ~Imm;
}
unsigned FirstOpc;
if (BitSize == 32) {
Imm &= (1LL << 32) - 1;
FirstOpc = (isNeg ? AArch64::MOVNWi : AArch64::MOVZWi);
} else {
FirstOpc = (isNeg ? AArch64::MOVNXi : AArch64::MOVZXi);
}
unsigned Shift = 0; unsigned LastShift = 0; if (Imm != 0) {
unsigned LZ = countLeadingZeros(Imm);
unsigned TZ = countTrailingZeros(Imm);
Shift = (TZ / 16) * 16;
LastShift = ((63 - LZ) / 16) * 16;
}
unsigned Imm16 = (Imm >> Shift) & Mask;
Insn.push_back({ FirstOpc, Imm16,
AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift) });
if (Shift == LastShift)
return;
if (isNeg)
Imm = ~Imm;
unsigned Opc = (BitSize == 32 ? AArch64::MOVKWi : AArch64::MOVKXi);
while (Shift < LastShift) {
Shift += 16;
Imm16 = (Imm >> Shift) & Mask;
if (Imm16 == (isNeg ? Mask : 0))
continue;
Insn.push_back({ Opc, Imm16,
AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift) });
}
}
void AArch64_IMM::expandMOVImm(uint64_t Imm, unsigned BitSize,
SmallVectorImpl<ImmInsnModel> &Insn) {
const unsigned Mask = 0xFFFF;
unsigned OneChunks = 0;
unsigned ZeroChunks = 0;
for (unsigned Shift = 0; Shift < BitSize; Shift += 16) {
const unsigned Chunk = (Imm >> Shift) & Mask;
if (Chunk == Mask)
OneChunks++;
else if (Chunk == 0)
ZeroChunks++;
}
if ((BitSize / 16) - OneChunks <= 1 || (BitSize / 16) - ZeroChunks <= 1) {
expandMOVImmSimple(Imm, BitSize, OneChunks, ZeroChunks, Insn);
return;
}
uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
uint64_t Encoding;
if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
unsigned Opc = (BitSize == 32 ? AArch64::ORRWri : AArch64::ORRXri);
Insn.push_back({ Opc, 0, Encoding });
return;
}
if (OneChunks >= (BitSize / 16) - 2 || ZeroChunks >= (BitSize / 16) - 2) {
expandMOVImmSimple(Imm, BitSize, OneChunks, ZeroChunks, Insn);
return;
}
assert(BitSize == 64 && "All 32-bit immediates can be expanded with a"
"MOVZ/MOVK pair");
for (unsigned Shift = 0; Shift < BitSize; Shift += 16) {
uint64_t ShiftedMask = (0xFFFFULL << Shift);
uint64_t ZeroChunk = UImm & ~ShiftedMask;
uint64_t OneChunk = UImm | ShiftedMask;
uint64_t RotatedImm = (UImm << 32) | (UImm >> 32);
uint64_t ReplicateChunk = ZeroChunk | (RotatedImm & ShiftedMask);
if (AArch64_AM::processLogicalImmediate(ZeroChunk, BitSize, Encoding) ||
AArch64_AM::processLogicalImmediate(OneChunk, BitSize, Encoding) ||
AArch64_AM::processLogicalImmediate(ReplicateChunk, BitSize,
Encoding)) {
Insn.push_back({ AArch64::ORRXri, 0, Encoding });
const unsigned Imm16 = getChunk(UImm, Shift / 16);
Insn.push_back({ AArch64::MOVKXi, Imm16,
AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift) });
return;
}
}
if (OneChunks || ZeroChunks) {
expandMOVImmSimple(Imm, BitSize, OneChunks, ZeroChunks, Insn);
return;
}
if (BitSize == 64 && tryToreplicateChunks(UImm, Insn))
return;
if (BitSize == 64 && trySequenceOfOnes(UImm, Insn))
return;
expandMOVImmSimple(Imm, BitSize, OneChunks, ZeroChunks, Insn);
}