80#define DEBUG_TYPE "aarch64-mi-peephole-opt"
96 using OpcodePair = std::pair<unsigned, unsigned>;
98 using SplitAndOpcFunc =
99 std::function<std::optional<OpcodePair>(
T,
unsigned,
T &,
T &)>;
101 std::function<void(
MachineInstr &, OpcodePair,
unsigned,
unsigned,
118 template <
typename T>
120 SplitAndOpcFunc<T> SplitAndOpc, BuildMIFunc BuildInstr);
125 template <
typename T>
126 bool visitADDSUB(
unsigned PosOpc,
unsigned NegOpc,
MachineInstr &
MI);
127 template <
typename T>
128 bool visitADDSSUBS(OpcodePair PosOpcs, OpcodePair NegOpcs,
MachineInstr &
MI);
130 template <
typename T>
143 return "AArch64 MI Peephole Optimization pass";
153char AArch64MIPeepholeOpt::ID = 0;
158 "AArch64 MI Peephole Optimization",
false,
false)
162 T UImm =
static_cast<T>(Imm);
199bool AArch64MIPeepholeOpt::visitAND(
211 return splitTwoPartImm<T>(
214 T &Imm1) -> std::optional<OpcodePair> {
215 if (splitBitmaskImm(
Imm,
RegSize, Imm0, Imm1))
216 return std::make_pair(Opc, Opc);
238 if (
MI.getOperand(3).getImm() != 0)
241 if (
MI.getOperand(1).getReg() != AArch64::WZR)
258 if (SrcMI->
getOpcode() == TargetOpcode::COPY &&
265 if (RC != &AArch64::FPR32RegClass &&
266 ((RC != &AArch64::FPR64RegClass && RC != &AArch64::FPR128RegClass) ||
271 CpySrc =
MRI->createVirtualRegister(&AArch64::FPR32RegClass);
273 TII->get(TargetOpcode::COPY), CpySrc)
281 else if (SrcMI->
getOpcode() <= TargetOpcode::GENERIC_OP_END)
286 MRI->replaceRegWith(DefReg, SrcReg);
287 MRI->clearKillFlags(SrcReg);
289 MI.eraseFromParent();
296 if (
MI.getOperand(1).getReg() !=
MI.getOperand(2).getReg())
300 MI.getOpcode() == AArch64::CSELXr ? AArch64::XZR : AArch64::WZR;
302 MI.getOpcode() == AArch64::CSELXr ? AArch64::ORRXrs : AArch64::ORRWrs;
310 MI.eraseFromParent();
322 if (!
MI.isRegTiedToDefOperand(1))
341 if ((SrcMI->
getOpcode() <= TargetOpcode::GENERIC_OP_END) ||
342 !AArch64::GPR64allRegClass.hasSubClassEq(RC))
348 TII->get(TargetOpcode::SUBREG_TO_REG), DstReg)
350 .
add(
MI.getOperand(2))
351 .
add(
MI.getOperand(3));
354 MI.eraseFromParent();
363 if ((Imm & 0xfff000) == 0 || (Imm & 0xfff) == 0 ||
364 (Imm & ~
static_cast<T>(0xffffff)) != 0)
370 if (
Insn.size() == 1)
374 Imm0 = (Imm >> 12) & 0xfff;
380bool AArch64MIPeepholeOpt::visitADDSUB(
398 if (
MI.getOperand(1).getReg() == AArch64::XZR ||
399 MI.getOperand(1).getReg() == AArch64::WZR)
402 return splitTwoPartImm<T>(
404 [PosOpc, NegOpc](
T Imm,
unsigned RegSize,
T &Imm0,
405 T &Imm1) -> std::optional<OpcodePair> {
407 return std::make_pair(PosOpc, PosOpc);
409 return std::make_pair(NegOpc, NegOpc);
429bool AArch64MIPeepholeOpt::visitADDSSUBS(
434 if (
MI.getOperand(1).getReg() == AArch64::XZR ||
435 MI.getOperand(1).getReg() == AArch64::WZR)
438 return splitTwoPartImm<T>(
442 T &Imm1) -> std::optional<OpcodePair> {
454 if (!NZCVUsed || NZCVUsed->C || NZCVUsed->V)
482 if (L && !
L->isLoopInvariant(
MI))
486 MovMI =
MRI->getUniqueVRegDef(
MI.getOperand(2).getReg());
491 SubregToRegMI =
nullptr;
492 if (MovMI->
getOpcode() == TargetOpcode::SUBREG_TO_REG) {
493 SubregToRegMI = MovMI;
499 if (MovMI->
getOpcode() != AArch64::MOVi32imm &&
500 MovMI->
getOpcode() != AArch64::MOVi64imm)
515bool AArch64MIPeepholeOpt::splitTwoPartImm(
517 SplitAndOpcFunc<T> SplitAndOpc, BuildMIFunc BuildInstr) {
520 "Invalid RegSize for legal immediate peephole optimization");
524 if (!checkMovImmInstr(
MI, MovMI, SubregToRegMI))
536 if (
auto R = SplitAndOpc(Imm,
RegSize, Imm0, Imm1))
549 TII->getRegClass(
TII->get(Opcode.first), 0,
TRI, *MF);
551 TII->getRegClass(
TII->get(Opcode.first), 1,
TRI, *MF);
553 (Opcode.first == Opcode.second)
555 :
TII->getRegClass(
TII->get(Opcode.second), 0,
TRI, *MF);
557 (Opcode.first == Opcode.second)
558 ? FirstInstrOperandRC
559 :
TII->getRegClass(
TII->get(Opcode.second), 1,
TRI, *MF);
564 Register NewTmpReg =
MRI->createVirtualRegister(FirstInstrDstRC);
568 ?
MRI->createVirtualRegister(SecondInstrDstRC)
572 MRI->constrainRegClass(SrcReg, FirstInstrOperandRC);
573 MRI->constrainRegClass(NewTmpReg, SecondInstrOperandRC);
574 if (DstReg != NewDstReg)
575 MRI->constrainRegClass(NewDstReg,
MRI->getRegClass(DstReg));
578 BuildInstr(
MI, Opcode, Imm0, Imm1, SrcReg, NewTmpReg, NewDstReg);
582 if (DstReg != NewDstReg) {
583 MRI->replaceRegWith(DstReg, NewDstReg);
584 MI.getOperand(0).setReg(DstReg);
588 MI.eraseFromParent();
596bool AArch64MIPeepholeOpt::visitINSviGPR(
MachineInstr &
MI,
unsigned Opc) {
613 if (!SrcMI || SrcMI->
getOpcode() != TargetOpcode::COPY)
620 &AArch64::FPR128RegClass) {
630 .
add(
MI.getOperand(1))
631 .
add(
MI.getOperand(2))
637 MI.eraseFromParent();
645 if (!
MI->getOperand(0).isReg() || !
MI->getOperand(0).isDef())
648 if (RC != &AArch64::FPR64RegClass)
650 return MI->getOpcode() > TargetOpcode::GENERIC_OP_END;
662 if (Low64MI->
getOpcode() != AArch64::INSERT_SUBREG)
682 if (!High64MI || High64MI->
getOpcode() != AArch64::INSERT_SUBREG)
685 if (High64MI && High64MI->
getOpcode() == TargetOpcode::COPY)
687 if (!High64MI || (High64MI->
getOpcode() != AArch64::MOVID &&
688 High64MI->
getOpcode() != AArch64::MOVIv2d_ns))
696 MRI->constrainRegClass(NewDef,
MRI->getRegClass(OldDef));
697 MRI->replaceRegWith(OldDef, NewDef);
698 MI.eraseFromParent();
713 MRI->clearKillFlags(OldDef);
714 MRI->clearKillFlags(NewDef);
715 MRI->constrainRegClass(NewDef,
MRI->getRegClass(OldDef));
716 MRI->replaceRegWith(OldDef, NewDef);
717 MI.eraseFromParent();
725 int64_t Immr =
MI.getOperand(2).getImm();
726 int64_t Imms =
MI.getOperand(3).getImm();
728 bool IsLSR = Imms == 31 && Immr <= Imms;
729 bool IsLSL = Immr == Imms + 33;
730 if (!IsLSR && !IsLSL)
738 TII->getRegClass(
TII->get(
MI.getOpcode()), 0,
TRI, *
MI.getMF());
740 TRI->getSubRegisterClass(DstRC64, AArch64::sub_32);
741 assert(DstRC32 &&
"Destination register class of UBFMXri doesn't have a "
742 "sub_32 subregister class");
745 TII->getRegClass(
TII->get(
MI.getOpcode()), 1,
TRI, *
MI.getMF());
747 TRI->getSubRegisterClass(SrcRC64, AArch64::sub_32);
748 assert(SrcRC32 &&
"Source register class of UBFMXri doesn't have a sub_32 "
749 "subregister class");
751 Register DstReg64 =
MI.getOperand(0).getReg();
752 Register DstReg32 =
MRI->createVirtualRegister(DstRC32);
753 Register SrcReg64 =
MI.getOperand(1).getReg();
754 Register SrcReg32 =
MRI->createVirtualRegister(SrcRC32);
758 .
addReg(SrcReg64, 0, AArch64::sub_32);
765 TII->get(AArch64::SUBREG_TO_REG), DstReg64)
769 MI.eraseFromParent();
777 Register InputReg =
MI.getOperand(1).getReg();
778 if (
MI.getOperand(1).getSubReg() != AArch64::sub_32 ||
779 !
MRI->hasOneNonDBGUse(InputReg))
796 if (SrcMI->
getOpcode() != AArch64::SBFMXri ||
799 return AArch64::NoRegister;
804 if (SrcMI->
getOpcode() != AArch64::SUBREG_TO_REG ||
807 return AArch64::NoRegister;
809 if (!Orr || Orr->
getOpcode() != AArch64::ORRWrr ||
812 return AArch64::NoRegister;
814 if (!Cpy || Cpy->
getOpcode() != AArch64::COPY ||
816 return AArch64::NoRegister;
821 Register SrcReg = getSXTWSrcReg(SrcMI);
823 SrcReg = getUXTWSrcReg(SrcMI);
827 MRI->constrainRegClass(SrcReg,
MRI->getRegClass(InputReg));
829 MI.getOperand(1).setReg(SrcReg);
831 for (
auto *DeadMI : DeadInstrs) {
833 DeadMI->eraseFromParent();
845 MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
848 assert(
MRI->isSSA() &&
"Expected to be run on SSA form!");
850 bool Changed =
false;
854 switch (
MI.getOpcode()) {
857 case AArch64::INSERT_SUBREG:
858 Changed |= visitINSERT(
MI);
860 case AArch64::ANDWrr:
861 Changed |= visitAND<uint32_t>(AArch64::ANDWri,
MI);
863 case AArch64::ANDXrr:
864 Changed |= visitAND<uint64_t>(AArch64::ANDXri,
MI);
866 case AArch64::ORRWrs:
867 Changed |= visitORR(
MI);
869 case AArch64::ADDWrr:
870 Changed |= visitADDSUB<uint32_t>(AArch64::ADDWri, AArch64::SUBWri,
MI);
872 case AArch64::SUBWrr:
873 Changed |= visitADDSUB<uint32_t>(AArch64::SUBWri, AArch64::ADDWri,
MI);
875 case AArch64::ADDXrr:
876 Changed |= visitADDSUB<uint64_t>(AArch64::ADDXri, AArch64::SUBXri,
MI);
878 case AArch64::SUBXrr:
879 Changed |= visitADDSUB<uint64_t>(AArch64::SUBXri, AArch64::ADDXri,
MI);
881 case AArch64::ADDSWrr:
883 visitADDSSUBS<uint32_t>({AArch64::ADDWri, AArch64::ADDSWri},
884 {AArch64::SUBWri, AArch64::SUBSWri},
MI);
886 case AArch64::SUBSWrr:
888 visitADDSSUBS<uint32_t>({AArch64::SUBWri, AArch64::SUBSWri},
889 {AArch64::ADDWri, AArch64::ADDSWri},
MI);
891 case AArch64::ADDSXrr:
893 visitADDSSUBS<uint64_t>({AArch64::ADDXri, AArch64::ADDSXri},
894 {AArch64::SUBXri, AArch64::SUBSXri},
MI);
896 case AArch64::SUBSXrr:
898 visitADDSSUBS<uint64_t>({AArch64::SUBXri, AArch64::SUBSXri},
899 {AArch64::ADDXri, AArch64::ADDSXri},
MI);
901 case AArch64::CSELWr:
902 case AArch64::CSELXr:
903 Changed |= visitCSEL(
MI);
905 case AArch64::INSvi64gpr:
906 Changed |= visitINSviGPR(
MI, AArch64::INSvi64lane);
908 case AArch64::INSvi32gpr:
909 Changed |= visitINSviGPR(
MI, AArch64::INSvi32lane);
911 case AArch64::INSvi16gpr:
912 Changed |= visitINSviGPR(
MI, AArch64::INSvi16lane);
914 case AArch64::INSvi8gpr:
915 Changed |= visitINSviGPR(
MI, AArch64::INSvi8lane);
917 case AArch64::INSvi64lane:
918 Changed |= visitINSvi64lane(
MI);
920 case AArch64::FMOVDr:
921 Changed |= visitFMOVDr(
MI);
923 case AArch64::UBFMXri:
924 Changed |= visitUBFMXri(
MI);
927 Changed |= visitCopy(
MI);
937 return new AArch64MIPeepholeOpt();
unsigned const MachineRegisterInfo * MRI
SmallVector< AArch64_IMM::ImmInsnModel, 4 > Insn
static bool is64bitDefwithZeroHigh64bit(MachineInstr *MI, MachineRegisterInfo *MRI)
static bool splitAddSubImm(T Imm, unsigned RegSize, T &Imm0, T &Imm1)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
const HexagonInstrInfo * TII
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
FunctionPass class - This class is used to implement most global optimizations.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
unsigned getSubReg() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
static bool isLogicalImmediate(uint64_t imm, unsigned regSize)
isLogicalImmediate - Return true if the immediate is valid for a logical immediate instruction of the...
static uint64_t encodeLogicalImmediate(uint64_t imm, unsigned regSize)
encodeLogicalImmediate - Return the encoded immediate value for a logical immediate instruction of th...
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...
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ Define
Register definition.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createAArch64MIPeepholeOptPass()
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
std::optional< UsedNZCV > examineCFlagsUse(MachineInstr &MI, MachineInstr &CmpInstr, const TargetRegisterInfo &TRI, SmallVectorImpl< MachineInstr * > *CCUseInstrs=nullptr)
unsigned getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
void initializeAArch64MIPeepholeOptPass(PassRegistry &)