27 #define AARCH64_EXPAND_PSEUDO_NAME "AArch64 pseudo instruction expansion pass"
51 unsigned LdarOp,
unsigned StlrOp,
unsigned CmpOp,
52 unsigned ExtendImm,
unsigned ZeroReg,
82 static uint64_t
getChunk(uint64_t Imm,
unsigned ChunkIdx) {
83 assert(ChunkIdx < 4 &&
"Out of range chunk index specified!");
85 return (Imm >> (ChunkIdx * 16)) & 0xFFFF;
90 static uint64_t
replicateChunk(uint64_t Imm,
unsigned FromIdx,
unsigned ToIdx) {
91 assert((FromIdx < 4) && (ToIdx < 4) &&
"Out of range chunk index specified!");
92 const unsigned ShiftAmt = ToIdx * 16;
95 const uint64_t Chunk =
getChunk(Imm, FromIdx) << ShiftAmt;
97 Imm &= ~(0xFFFFLL << ShiftAmt);
108 assert(ChunkIdx < 4 &&
"Out of range chunk index specified!");
109 const unsigned ShiftAmt = ChunkIdx * 16;
117 .addReg(AArch64::XZR)
131 transferImpOps(MI, MIB, MIB1);
141 static bool canUseOrr(uint64_t Chunk, uint64_t &Encoding) {
142 Chunk = (Chunk << 48) | (Chunk << 32) | (Chunk << 16) | Chunk;
163 for (
unsigned Idx = 0; Idx < 4; ++Idx)
167 for (CountMap::const_iterator Chunk = Counts.begin(),
End = Counts.end();
168 Chunk !=
End; ++Chunk) {
169 const uint64_t ChunkVal = Chunk->first;
170 const unsigned Count = Chunk->second;
172 uint64_t Encoding = 0;
176 if ((Count != 2 && Count != 3) || !
canUseOrr(ChunkVal, Encoding))
179 const bool CountThree = Count == 3;
184 .addReg(AArch64::XZR)
190 unsigned ShiftAmt = 0;
193 for (; ShiftAmt < 64; ShiftAmt += 16) {
194 Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
196 if (Imm16 != ChunkVal)
212 transferImpOps(MI, MIB, MIB1);
218 for (ShiftAmt += 16; ShiftAmt < 64; ShiftAmt += 16) {
219 Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
221 if (Imm16 != ChunkVal)
233 transferImpOps(MI, MIB, MIB2);
245 if (Chunk == 0 || Chunk == UINT64_MAX)
255 if (Chunk == 0 || Chunk == UINT64_MAX)
262 static uint64_t
updateImm(uint64_t Imm,
unsigned Idx,
bool Clear) {
263 const uint64_t
Mask = 0xFFFF;
267 Imm &= ~(Mask << (Idx * 16));
270 Imm |= Mask << (Idx * 16);
293 const int NotSet = -1;
294 const uint64_t
Mask = 0xFFFF;
296 int StartIdx = NotSet;
299 for (
int Idx = 0; Idx < 4; ++Idx) {
300 int64_t Chunk =
getChunk(UImm, Idx);
302 Chunk = (Chunk << 48) >> 48;
311 if (StartIdx == NotSet || EndIdx == NotSet)
315 uint64_t Outside = 0;
317 uint64_t Inside =
Mask;
322 if (StartIdx > EndIdx) {
327 uint64_t OrrImm = UImm;
328 int FirstMovkIdx = NotSet;
329 int SecondMovkIdx = NotSet;
333 for (
int Idx = 0; Idx < 4; ++Idx) {
334 const uint64_t Chunk =
getChunk(UImm, Idx);
338 if ((Idx < StartIdx || EndIdx < Idx) && Chunk != Outside) {
339 OrrImm =
updateImm(OrrImm, Idx, Outside == 0);
342 if (FirstMovkIdx == NotSet)
349 }
else if (Idx > StartIdx && Idx < EndIdx && Chunk != Inside) {
350 OrrImm =
updateImm(OrrImm, Idx, Inside != Mask);
353 if (FirstMovkIdx == NotSet)
359 assert(FirstMovkIdx != NotSet &&
"Constant materializable with single ORR!");
362 uint64_t Encoding = 0;
367 .addReg(AArch64::XZR)
373 const bool SingleMovk = SecondMovkIdx == NotSet;
386 transferImpOps(MI, MIB, MIB1);
400 transferImpOps(MI, MIB, MIB2);
413 const unsigned Mask = 0xFFFF;
415 if (DstReg == AArch64::XZR || DstReg == AArch64::WZR) {
423 uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
426 unsigned Opc = (BitSize == 32 ? AArch64::ORRWri : AArch64::ORRXri);
430 .addReg(BitSize == 32 ? AArch64::WZR : AArch64::XZR)
432 transferImpOps(MI, MIB, MIB);
439 unsigned OneChunks = 0;
440 unsigned ZeroChunks = 0;
441 for (
unsigned Shift = 0; Shift < BitSize; Shift += 16) {
442 const unsigned Chunk = (Imm >> Shift) & Mask;
472 if (BitSize == 64 && OneChunks < 3 && ZeroChunks < 3) {
526 if (OneChunks > ZeroChunks) {
533 Imm &= (1LL << 32) - 1;
534 FirstOpc = (isNeg ? AArch64::MOVNWi : AArch64::MOVZWi);
536 FirstOpc = (isNeg ? AArch64::MOVNXi : AArch64::MOVZXi);
539 unsigned LastShift = 0;
543 Shift = (TZ / 16) * 16;
544 LastShift = ((63 - LZ) / 16) * 16;
546 unsigned Imm16 = (Imm >> Shift) & Mask;
560 if (Shift == LastShift) {
561 transferImpOps(MI, MIB1, MIB1);
567 unsigned Opc = (BitSize == 32 ? AArch64::MOVKWi : AArch64::MOVKXi);
568 while (Shift < LastShift) {
570 Imm16 = (Imm >> Shift) & Mask;
571 if (Imm16 == (isNeg ? Mask : 0))
582 transferImpOps(MI, MIB1, MIB2);
587 bool AArch64ExpandPseudo::expandCMP_SWAP(
589 unsigned StlrOp,
unsigned CmpOp,
unsigned ExtendImm,
unsigned ZeroReg,
609 MF->
insert(++LoadCmpBB->getIterator(), StoreBB);
610 MF->
insert(++StoreBB->getIterator(), DoneBB);
618 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::MOVZWi), StatusReg)
622 BuildMI(LoadCmpBB, DL,
TII->get(CmpOp), ZeroReg)
626 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::Bcc))
630 LoadCmpBB->addSuccessor(DoneBB);
631 LoadCmpBB->addSuccessor(StoreBB);
636 BuildMI(StoreBB, DL,
TII->get(StlrOp), StatusReg)
639 BuildMI(StoreBB, DL,
TII->get(AArch64::CBNZW))
642 StoreBB->addSuccessor(LoadCmpBB);
643 StoreBB->addSuccessor(DoneBB);
645 DoneBB->splice(DoneBB->end(), &MBB,
MI, MBB.
end());
646 DoneBB->transferSuccessors(&MBB);
650 NextMBBI = MBB.
end();
651 MI.eraseFromParent();
660 StoreBB->clearLiveIns();
662 LoadCmpBB->clearLiveIns();
668 bool AArch64ExpandPseudo::expandCMP_SWAP_128(
693 MF->
insert(++LoadCmpBB->getIterator(), StoreBB);
694 MF->
insert(++StoreBB->getIterator(), DoneBB);
701 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::LDAXPX))
705 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::SUBSXrs), AArch64::XZR)
709 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::CSINCWr), StatusReg)
710 .addUse(AArch64::WZR)
713 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::SUBSXrs), AArch64::XZR)
717 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::CSINCWr), StatusReg)
721 BuildMI(LoadCmpBB, DL,
TII->get(AArch64::CBNZW))
724 LoadCmpBB->addSuccessor(DoneBB);
725 LoadCmpBB->addSuccessor(StoreBB);
730 BuildMI(StoreBB, DL,
TII->get(AArch64::STLXPX), StatusReg)
734 BuildMI(StoreBB, DL,
TII->get(AArch64::CBNZW))
737 StoreBB->addSuccessor(LoadCmpBB);
738 StoreBB->addSuccessor(DoneBB);
740 DoneBB->splice(DoneBB->end(), &MBB,
MI, MBB.
end());
741 DoneBB->transferSuccessors(&MBB);
745 NextMBBI = MBB.
end();
746 MI.eraseFromParent();
755 StoreBB->clearLiveIns();
757 LoadCmpBB->clearLiveIns();
774 case AArch64::ADDWrr:
775 case AArch64::SUBWrr:
776 case AArch64::ADDXrr:
777 case AArch64::SUBXrr:
778 case AArch64::ADDSWrr:
779 case AArch64::SUBSWrr:
780 case AArch64::ADDSXrr:
781 case AArch64::SUBSXrr:
782 case AArch64::ANDWrr:
783 case AArch64::ANDXrr:
784 case AArch64::BICWrr:
785 case AArch64::BICXrr:
786 case AArch64::ANDSWrr:
787 case AArch64::ANDSXrr:
788 case AArch64::BICSWrr:
789 case AArch64::BICSXrr:
790 case AArch64::EONWrr:
791 case AArch64::EONXrr:
792 case AArch64::EORWrr:
793 case AArch64::EORXrr:
794 case AArch64::ORNWrr:
795 case AArch64::ORNXrr:
796 case AArch64::ORRWrr:
797 case AArch64::ORRXrr: {
802 case AArch64::ADDWrr: Opcode = AArch64::ADDWrs;
break;
803 case AArch64::SUBWrr: Opcode = AArch64::SUBWrs;
break;
804 case AArch64::ADDXrr: Opcode = AArch64::ADDXrs;
break;
805 case AArch64::SUBXrr: Opcode = AArch64::SUBXrs;
break;
806 case AArch64::ADDSWrr: Opcode = AArch64::ADDSWrs;
break;
807 case AArch64::SUBSWrr: Opcode = AArch64::SUBSWrs;
break;
808 case AArch64::ADDSXrr: Opcode = AArch64::ADDSXrs;
break;
809 case AArch64::SUBSXrr: Opcode = AArch64::SUBSXrs;
break;
810 case AArch64::ANDWrr: Opcode = AArch64::ANDWrs;
break;
811 case AArch64::ANDXrr: Opcode = AArch64::ANDXrs;
break;
812 case AArch64::BICWrr: Opcode = AArch64::BICWrs;
break;
813 case AArch64::BICXrr: Opcode = AArch64::BICXrs;
break;
814 case AArch64::ANDSWrr: Opcode = AArch64::ANDSWrs;
break;
815 case AArch64::ANDSXrr: Opcode = AArch64::ANDSXrs;
break;
816 case AArch64::BICSWrr: Opcode = AArch64::BICSWrs;
break;
817 case AArch64::BICSXrr: Opcode = AArch64::BICSXrs;
break;
818 case AArch64::EONWrr: Opcode = AArch64::EONWrs;
break;
819 case AArch64::EONXrr: Opcode = AArch64::EONXrs;
break;
820 case AArch64::EORWrr: Opcode = AArch64::EORWrs;
break;
821 case AArch64::EORXrr: Opcode = AArch64::EORXrs;
break;
822 case AArch64::ORNWrr: Opcode = AArch64::ORNWrs;
break;
823 case AArch64::ORNXrr: Opcode = AArch64::ORNXrs;
break;
824 case AArch64::ORRWrr: Opcode = AArch64::ORRWrs;
break;
825 case AArch64::ORRXrr: Opcode = AArch64::ORRXrs;
break;
833 transferImpOps(MI, MIB1, MIB1);
860 "Only expect globals, externalsymbols, or constant pools");
868 transferImpOps(MI, MIB1, MIB2);
873 case AArch64::MOVaddr:
874 case AArch64::MOVaddrJT:
875 case AArch64::MOVaddrCP:
876 case AArch64::MOVaddrBA:
877 case AArch64::MOVaddrTLS:
878 case AArch64::MOVaddrEXT: {
892 transferImpOps(MI, MIB1, MIB2);
896 case AArch64::MOVbaseTLS: {
898 auto SysReg = AArch64SysReg::TPIDR_EL0;
902 SysReg = AArch64SysReg::TPIDR_EL1;
909 case AArch64::MOVi32imm:
910 return expandMOVImm(MBB, MBBI, 32);
911 case AArch64::MOVi64imm:
912 return expandMOVImm(MBB, MBBI, 64);
913 case AArch64::RET_ReallyLR: {
922 transferImpOps(MI, MIB, MIB);
926 case AArch64::CMP_SWAP_8:
927 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRB, AArch64::STLXRB,
930 AArch64::WZR, NextMBBI);
931 case AArch64::CMP_SWAP_16:
932 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRH, AArch64::STLXRH,
935 AArch64::WZR, NextMBBI);
936 case AArch64::CMP_SWAP_32:
937 return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRW, AArch64::STLXRW,
940 AArch64::WZR, NextMBBI);
941 case AArch64::CMP_SWAP_64:
942 return expandCMP_SWAP(MBB, MBBI,
943 AArch64::LDAXRX, AArch64::STLXRX, AArch64::SUBSXrs,
945 AArch64::XZR, NextMBBI);
946 case AArch64::CMP_SWAP_128:
947 return expandCMP_SWAP_128(MBB, MBBI, NextMBBI);
949 case AArch64::AESMCrrTied:
950 case AArch64::AESIMCrrTied: {
953 TII->get(Opcode == AArch64::AESMCrrTied ? AArch64::AESMCrr :
957 transferImpOps(MI, MIB, MIB);
968 bool Modified =
false;
973 Modified |= expandMI(MBB, MBBI, NMBBI);
983 bool Modified =
false;
985 Modified |= expandMBB(MBB);
991 return new AArch64ExpandPseudo();
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const GlobalValue * getGlobal() const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
Describe properties that are true of each instruction in the target description file.
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
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 ...
const char * getSymbolName() const
static bool tryToreplicateChunks(uint64_t UImm, MachineInstr &MI, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const AArch64InstrInfo *TII)
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 ...
std::size_t 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...
const Triple & getTargetTriple() const
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
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 uint64_t replicateChunk(uint64_t Imm, unsigned FromIdx, unsigned ToIdx)
Helper function which replicates a 16-bit chunk within a 64-bit value.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
unsigned getKillRegState(bool B)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
static uint64_t updateImm(uint64_t Imm, unsigned Idx, bool Clear)
Clear or set all bits in the chunk at the given index.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
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 getDeadRegState(bool B)
static bool isStartChunk(uint64_t Chunk)
Check whether this chunk matches the pattern '1...0...'.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned getTargetFlags() const
void computeLiveIns(LivePhysRegs &LiveRegs, const MachineRegisterInfo &MRI, MachineBasicBlock &MBB)
Computes the live-in list for MBB assuming all of its successors live-in lists are up-to-date...
unsigned const MachineRegisterInfo * MRI
std::size_t 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...
CodeModel::Model getCodeModel() const
Returns the code model.
void initializeAArch64ExpandPseudoPass(PassRegistry &)
MachineInstrBuilder & UseMI
const MachineOperand & getOperand(unsigned i) const
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
static const unsigned End
FunctionPass class - This class is used to implement most global optimizations.
INITIALIZE_PASS(AArch64ExpandPseudo,"aarch64-expand-pseudo", AARCH64_EXPAND_PSEUDO_NAME, false, false) static void transferImpOps(MachineInstr &OldMI
Transfer implicit operands on the pseudo instruction to the instructions created from the expansion...
int64_t getOffset() const
Return the offset from the symbol in this operand.
self_iterator getIterator()
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
static uint64_t getChunk(uint64_t Imm, unsigned ChunkIdx)
Helper function which extracts the specified 16-bit chunk from a 64-bit value.
MachineOperand class - Representation of each machine instruction operand.
FunctionPass * createAArch64ExpandPseudoPass()
Returns an instance of the pseudo instruction expansion pass.
static bool isEndChunk(uint64_t Chunk)
Check whether this chunk matches the pattern '0...1...' This pattern ends a contiguous sequence of on...
static bool trySequenceOfOnes(uint64_t UImm, MachineInstr &MI, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const AArch64InstrInfo *TII)
Check whether the constant contains a sequence of contiguous ones, which might be interrupted by one ...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
static unsigned getArithExtendImm(AArch64_AM::ShiftExtendType ET, unsigned Imm)
getArithExtendImm - Encode the extend type and shift amount for an arithmetic instruction: imm: 3-bit...
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned char TargetFlags=0) const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static std::vector< std::string > Flags
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned char TargetFlags=0) const
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
#define AARCH64_EXPAND_PSEUDO_NAME
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addUse(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
virtual const TargetInstrInfo * getInstrInfo() const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
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 bool tryOrrMovk(uint64_t UImm, uint64_t OrrImm, MachineInstr &MI, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const AArch64InstrInfo *TII, unsigned ChunkIdx)
Helper function which tries to materialize a 64-bit value with an ORR + MOVK instruction sequence...
StringRef - Represent a constant reference to a string, i.e.
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow...
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.