52 #include <unordered_map> 56 #define DEBUG_TYPE "aarch64-simdinstr-opt" 59 "Number of SIMD instructions modified");
61 #define AARCH64_VECTOR_BY_ELEMENT_OPT_NAME \ 62 "AArch64 SIMD instructions optimization pass" 76 std::map<std::pair<unsigned, std::string>,
bool> SIMDInstrTable;
79 std::unordered_map<std::string, bool> InterlEarlyExit;
89 std::vector<unsigned> ReplOpc;
93 #define RuleST2(OpcOrg, OpcR0, OpcR1, OpcR2, RC) \ 94 {OpcOrg, {OpcR0, OpcR1, OpcR2}, RC} 95 #define RuleST4(OpcOrg, OpcR0, OpcR1, OpcR2, OpcR3, OpcR4, OpcR5, OpcR6, \ 96 OpcR7, OpcR8, OpcR9, RC) \ 98 {OpcR0, OpcR1, OpcR2, OpcR3, OpcR4, OpcR5, OpcR6, OpcR7, OpcR8, OpcR9}, RC} 101 std::vector<InstReplInfo> IRT = {
103 RuleST2(AArch64::ST2Twov2d, AArch64::ZIP1v2i64, AArch64::ZIP2v2i64,
104 AArch64::STPQi, AArch64::FPR128RegClass),
105 RuleST2(AArch64::ST2Twov4s, AArch64::ZIP1v4i32, AArch64::ZIP2v4i32,
106 AArch64::STPQi, AArch64::FPR128RegClass),
107 RuleST2(AArch64::ST2Twov2s, AArch64::ZIP1v2i32, AArch64::ZIP2v2i32,
108 AArch64::STPDi, AArch64::FPR64RegClass),
109 RuleST2(AArch64::ST2Twov8h, AArch64::ZIP1v8i16, AArch64::ZIP2v8i16,
110 AArch64::STPQi, AArch64::FPR128RegClass),
111 RuleST2(AArch64::ST2Twov4h, AArch64::ZIP1v4i16, AArch64::ZIP2v4i16,
112 AArch64::STPDi, AArch64::FPR64RegClass),
113 RuleST2(AArch64::ST2Twov16b, AArch64::ZIP1v16i8, AArch64::ZIP2v16i8,
114 AArch64::STPQi, AArch64::FPR128RegClass),
115 RuleST2(AArch64::ST2Twov8b, AArch64::ZIP1v8i8, AArch64::ZIP2v8i8,
116 AArch64::STPDi, AArch64::FPR64RegClass),
118 RuleST4(AArch64::ST4Fourv2d, AArch64::ZIP1v2i64, AArch64::ZIP2v2i64,
119 AArch64::ZIP1v2i64, AArch64::ZIP2v2i64, AArch64::ZIP1v2i64,
120 AArch64::ZIP2v2i64, AArch64::ZIP1v2i64, AArch64::ZIP2v2i64,
121 AArch64::STPQi, AArch64::STPQi, AArch64::FPR128RegClass),
122 RuleST4(AArch64::ST4Fourv4s, AArch64::ZIP1v4i32, AArch64::ZIP2v4i32,
123 AArch64::ZIP1v4i32, AArch64::ZIP2v4i32, AArch64::ZIP1v4i32,
124 AArch64::ZIP2v4i32, AArch64::ZIP1v4i32, AArch64::ZIP2v4i32,
125 AArch64::STPQi, AArch64::STPQi, AArch64::FPR128RegClass),
126 RuleST4(AArch64::ST4Fourv2s, AArch64::ZIP1v2i32, AArch64::ZIP2v2i32,
127 AArch64::ZIP1v2i32, AArch64::ZIP2v2i32, AArch64::ZIP1v2i32,
128 AArch64::ZIP2v2i32, AArch64::ZIP1v2i32, AArch64::ZIP2v2i32,
129 AArch64::STPDi, AArch64::STPDi, AArch64::FPR64RegClass),
130 RuleST4(AArch64::ST4Fourv8h, AArch64::ZIP1v8i16, AArch64::ZIP2v8i16,
131 AArch64::ZIP1v8i16, AArch64::ZIP2v8i16, AArch64::ZIP1v8i16,
132 AArch64::ZIP2v8i16, AArch64::ZIP1v8i16, AArch64::ZIP2v8i16,
133 AArch64::STPQi, AArch64::STPQi, AArch64::FPR128RegClass),
134 RuleST4(AArch64::ST4Fourv4h, AArch64::ZIP1v4i16, AArch64::ZIP2v4i16,
135 AArch64::ZIP1v4i16, AArch64::ZIP2v4i16, AArch64::ZIP1v4i16,
136 AArch64::ZIP2v4i16, AArch64::ZIP1v4i16, AArch64::ZIP2v4i16,
137 AArch64::STPDi, AArch64::STPDi, AArch64::FPR64RegClass),
138 RuleST4(AArch64::ST4Fourv16b, AArch64::ZIP1v16i8, AArch64::ZIP2v16i8,
139 AArch64::ZIP1v16i8, AArch64::ZIP2v16i8, AArch64::ZIP1v16i8,
140 AArch64::ZIP2v16i8, AArch64::ZIP1v16i8, AArch64::ZIP2v16i8,
141 AArch64::STPQi, AArch64::STPQi, AArch64::FPR128RegClass),
142 RuleST4(AArch64::ST4Fourv8b, AArch64::ZIP1v8i8, AArch64::ZIP2v8i8,
143 AArch64::ZIP1v8i8, AArch64::ZIP2v8i8, AArch64::ZIP1v8i8,
144 AArch64::ZIP2v8i8, AArch64::ZIP1v8i8, AArch64::ZIP2v8i8,
145 AArch64::STPDi, AArch64::STPDi, AArch64::FPR64RegClass)
150 static const unsigned MaxNumRepl = 10;
173 bool reuseDUP(
MachineInstr &
MI,
unsigned DupOpcode,
unsigned SrcReg,
174 unsigned LaneNumber,
unsigned *DestReg)
const;
187 bool processSeqRegInst(
MachineInstr *DefiningMI,
unsigned* StReg,
188 unsigned* StRegKill,
unsigned NumArg)
const;
217 bool AArch64SIMDInstrOpt::
222 std::string Subtarget = SchedModel.getSubtargetInfo()->getCPU();
223 auto InstID = std::make_pair(InstDesc->getOpcode(), Subtarget);
224 if (SIMDInstrTable.find(InstID) != SIMDInstrTable.end())
225 return SIMDInstrTable[InstID];
227 unsigned SCIdx = InstDesc->getSchedClass();
229 SchedModel.getMCSchedModel()->getSchedClassDesc(SCIdx);
236 SIMDInstrTable[InstID] =
false;
239 for (
auto IDesc : InstDescRepl)
241 SCDescRepl = SchedModel.getMCSchedModel()->getSchedClassDesc(
242 IDesc->getSchedClass());
245 SIMDInstrTable[InstID] =
false;
251 unsigned ReplCost = 0;
252 for (
auto IDesc :InstDescRepl)
253 ReplCost += SchedModel.computeInstrLatency(IDesc->getOpcode());
255 if (SchedModel.computeInstrLatency(InstDesc->getOpcode()) > ReplCost)
257 SIMDInstrTable[InstID] =
true;
262 SIMDInstrTable[InstID] =
false;
282 OriginalMCID = &
TII->get(AArch64::FMLAv4i32_indexed);
283 ReplInstrMCID.
push_back(&
TII->get(AArch64::DUPv4i32lane));
285 if (shouldReplaceInst(MF, OriginalMCID, ReplInstrMCID))
291 std::string Subtarget = SchedModel.getSubtargetInfo()->getCPU();
292 if (InterlEarlyExit.find(Subtarget) != InterlEarlyExit.end())
293 return InterlEarlyExit[Subtarget];
295 for (
auto &
I : IRT) {
296 OriginalMCID = &
TII->get(
I.OrigOpc);
297 for (
auto &Repl :
I.ReplOpc)
299 if (shouldReplaceInst(MF, OriginalMCID, ReplInstrMCID)) {
300 InterlEarlyExit[Subtarget] =
false;
303 ReplInstrMCID.
clear();
305 InterlEarlyExit[Subtarget] =
true;
316 bool AArch64SIMDInstrOpt::reuseDUP(
MachineInstr &
MI,
unsigned DupOpcode,
317 unsigned SrcReg,
unsigned LaneNumber,
318 unsigned *DestReg)
const {
324 if (CurrentMI->
getOpcode() == DupOpcode &&
350 bool AArch64SIMDInstrOpt::optimizeVectElement(
MachineInstr &MI) {
359 case AArch64::FMLAv4i32_indexed:
360 DupMCID = &
TII->get(AArch64::DUPv4i32lane);
361 MulMCID = &
TII->get(AArch64::FMLAv4f32);
363 case AArch64::FMLSv4i32_indexed:
364 DupMCID = &
TII->get(AArch64::DUPv4i32lane);
365 MulMCID = &
TII->get(AArch64::FMLSv4f32);
367 case AArch64::FMULXv4i32_indexed:
368 DupMCID = &
TII->get(AArch64::DUPv4i32lane);
369 MulMCID = &
TII->get(AArch64::FMULXv4f32);
371 case AArch64::FMULv4i32_indexed:
372 DupMCID = &
TII->get(AArch64::DUPv4i32lane);
373 MulMCID = &
TII->get(AArch64::FMULv4f32);
377 case AArch64::FMLAv2i64_indexed:
378 DupMCID = &
TII->get(AArch64::DUPv2i64lane);
379 MulMCID = &
TII->get(AArch64::FMLAv2f64);
381 case AArch64::FMLSv2i64_indexed:
382 DupMCID = &
TII->get(AArch64::DUPv2i64lane);
383 MulMCID = &
TII->get(AArch64::FMLSv2f64);
385 case AArch64::FMULXv2i64_indexed:
386 DupMCID = &
TII->get(AArch64::DUPv2i64lane);
387 MulMCID = &
TII->get(AArch64::FMULXv2f64);
389 case AArch64::FMULv2i64_indexed:
390 DupMCID = &
TII->get(AArch64::DUPv2i64lane);
391 MulMCID = &
TII->get(AArch64::FMULv2f64);
395 case AArch64::FMLAv2i32_indexed:
396 RC = &AArch64::FPR64RegClass;
397 DupMCID = &
TII->get(AArch64::DUPv2i32lane);
398 MulMCID = &
TII->get(AArch64::FMLAv2f32);
400 case AArch64::FMLSv2i32_indexed:
401 RC = &AArch64::FPR64RegClass;
402 DupMCID = &
TII->get(AArch64::DUPv2i32lane);
403 MulMCID = &
TII->get(AArch64::FMLSv2f32);
405 case AArch64::FMULXv2i32_indexed:
406 RC = &AArch64::FPR64RegClass;
407 DupMCID = &
TII->get(AArch64::DUPv2i32lane);
408 MulMCID = &
TII->get(AArch64::FMULXv2f32);
410 case AArch64::FMULv2i32_indexed:
411 RC = &AArch64::FPR64RegClass;
412 DupMCID = &
TII->get(AArch64::DUPv2i32lane);
413 MulMCID = &
TII->get(AArch64::FMULv2f32);
444 if (!reuseDUP(MI, DupMCID->
getOpcode(), SrcReg2, LaneNumber, &DupDest)) {
446 BuildMI(MBB, MI, DL, *DupMCID, DupDest)
447 .
addReg(SrcReg2, Src2IsKill)
450 BuildMI(MBB, MI, DL, *MulMCID, MulDest)
451 .
addReg(SrcReg0, Src0IsKill)
452 .
addReg(SrcReg1, Src1IsKill)
453 .
addReg(DupDest, Src2IsKill);
456 if (!reuseDUP(MI, DupMCID->
getOpcode(), SrcReg1, LaneNumber, &DupDest)) {
458 BuildMI(MBB, MI, DL, *DupMCID, DupDest)
459 .
addReg(SrcReg1, Src1IsKill)
462 BuildMI(MBB, MI, DL, *MulMCID, MulDest)
463 .
addReg(SrcReg0, Src0IsKill)
464 .
addReg(DupDest, Src1IsKill);
502 bool AArch64SIMDInstrOpt::optimizeLdStInterleave(
MachineInstr &MI) {
504 unsigned SeqReg, AddrReg;
505 unsigned StReg[4], StRegKill[4];
515 for (
auto &
I : IRT) {
519 DefiningMI =
MRI->getUniqueVRegDef(SeqReg);
520 unsigned NumReg = determineSrcReg(MI);
521 if (!processSeqRegInst(DefiningMI, StReg, StRegKill, NumReg))
524 for (
auto &Repl :
I.ReplOpc) {
527 if (Repl != AArch64::STPQi && Repl != AArch64::STPDi)
554 case AArch64::ST2Twov16b:
555 case AArch64::ST2Twov8b:
556 case AArch64::ST2Twov8h:
557 case AArch64::ST2Twov4h:
558 case AArch64::ST2Twov4s:
559 case AArch64::ST2Twov2s:
560 case AArch64::ST2Twov2d:
562 BuildMI(MBB, MI, DL, *ReplInstrMCID[0], ZipDest[0])
565 BuildMI(MBB, MI, DL, *ReplInstrMCID[1], ZipDest[1])
566 .
addReg(StReg[0], StRegKill[0])
567 .
addReg(StReg[1], StRegKill[1]);
569 BuildMI(MBB, MI, DL, *ReplInstrMCID[2])
576 case AArch64::ST4Fourv16b:
577 case AArch64::ST4Fourv8b:
578 case AArch64::ST4Fourv8h:
579 case AArch64::ST4Fourv4h:
580 case AArch64::ST4Fourv4s:
581 case AArch64::ST4Fourv2s:
582 case AArch64::ST4Fourv2d:
584 BuildMI(MBB, MI, DL, *ReplInstrMCID[0], ZipDest[0])
587 BuildMI(MBB, MI, DL, *ReplInstrMCID[1], ZipDest[1])
588 .
addReg(StReg[0], StRegKill[0])
589 .
addReg(StReg[2], StRegKill[2]);
590 BuildMI(MBB, MI, DL, *ReplInstrMCID[2], ZipDest[2])
593 BuildMI(MBB, MI, DL, *ReplInstrMCID[3], ZipDest[3])
594 .
addReg(StReg[1], StRegKill[1])
595 .
addReg(StReg[3], StRegKill[3]);
596 BuildMI(MBB, MI, DL, *ReplInstrMCID[4], ZipDest[4])
599 BuildMI(MBB, MI, DL, *ReplInstrMCID[5], ZipDest[5])
602 BuildMI(MBB, MI, DL, *ReplInstrMCID[6], ZipDest[6])
605 BuildMI(MBB, MI, DL, *ReplInstrMCID[7], ZipDest[7])
609 BuildMI(MBB, MI, DL, *ReplInstrMCID[8])
614 BuildMI(MBB, MI, DL, *ReplInstrMCID[9])
631 bool AArch64SIMDInstrOpt::processSeqRegInst(
MachineInstr *DefiningMI,
632 unsigned* StReg,
unsigned* StRegKill,
unsigned NumArg)
const {
633 assert (DefiningMI != NULL);
634 if (DefiningMI->
getOpcode() != AArch64::REG_SEQUENCE)
637 for (
unsigned i=0; i<NumArg; i++) {
666 unsigned AArch64SIMDInstrOpt::determineSrcReg(
MachineInstr &MI)
const {
671 case AArch64::ST2Twov16b:
672 case AArch64::ST2Twov8b:
673 case AArch64::ST2Twov8h:
674 case AArch64::ST2Twov4h:
675 case AArch64::ST2Twov4s:
676 case AArch64::ST2Twov2s:
677 case AArch64::ST2Twov2d:
680 case AArch64::ST4Fourv16b:
681 case AArch64::ST4Fourv8b:
682 case AArch64::ST4Fourv8h:
683 case AArch64::ST4Fourv4h:
684 case AArch64::ST4Fourv4s:
685 case AArch64::ST4Fourv2s:
686 case AArch64::ST4Fourv2d:
702 SchedModel.init(&ST);
703 if (!SchedModel.hasInstrSchedModel())
706 bool Changed =
false;
707 for (
auto OptimizationKind : {VectorElem, Interleave}) {
708 if (!shouldExitEarly(&MF, OptimizationKind)) {
715 if (OptimizationKind == VectorElem)
716 InstRewrite = optimizeVectElement(MI) ;
718 InstRewrite = optimizeLdStInterleave(MI);
739 return new AArch64SIMDInstrOpt();
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
STATISTIC(NumFunctions, "Total number of functions")
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
#define AARCH64_VECTOR_BY_ELEMENT_OPT_NAME
INITIALIZE_PASS(AArch64SIMDInstrOpt, "aarch64-simdinstr-opt", AARCH64_VECTOR_BY_ELEMENT_OPT_NAME, false, false) bool AArch64SIMDInstrOpt
Based only on latency of instructions, determine if it is cost efficient to replace the instruction I...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Provide an instruction scheduling machine model to CodeGen passes.
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
virtual const TargetInstrInfo * getInstrInfo() const
unsigned getKillRegState(bool B)
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Summarize the scheduling resources required for an instruction of a particular scheduling class...
FunctionPass class - This class is used to implement most global optimizations.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define RuleST2(OpcOrg, OpcR0, OpcR1, OpcR2, RC)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
TargetSubtargetInfo - Generic base class for all target subtargets.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
#define RuleST4(OpcOrg, OpcR0, OpcR1, OpcR2, OpcR3, OpcR4, OpcR5, OpcR6, OpcR7, OpcR8, OpcR9, RC)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FunctionPass * createAArch64SIMDInstrOptPass()
Returns an instance of the high cost ASIMD instruction replacement optimization pass.
unsigned getOpcode() const
Return the opcode number for this descriptor.
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
void initializeAArch64SIMDInstrOptPass(PassRegistry &)
Wrapper class representing virtual and physical registers.