60 #define DEBUG_TYPE "ppc-vsx-swaps"
70 struct PPCVSXSwapEntry {
78 unsigned int IsLoad : 1;
79 unsigned int IsStore : 1;
80 unsigned int IsSwap : 1;
81 unsigned int MentionsPhysVR : 1;
82 unsigned int IsSwappable : 1;
83 unsigned int MentionsPartialVR : 1;
84 unsigned int SpecialHandling : 3;
85 unsigned int WebRejected : 1;
86 unsigned int WillRemove : 1;
108 std::vector<PPCVSXSwapEntry> SwapVector;
128 bool gatherVectorInstructions();
137 unsigned lookThruCopyLike(
unsigned SrcReg,
unsigned VecIdx);
143 void recordUnoptimizableWebs();
146 void markSwapsForRemoval();
155 unsigned DstReg,
unsigned SrcReg);
158 void handleSpecialSwappables(
int EntryIdx);
161 void dumpSwapVector();
172 return (isRegInClass(Reg, &PPC::VSRCRegClass) ||
173 isRegInClass(Reg, &PPC::VRRCRegClass));
177 bool isScalarVecReg(
unsigned Reg) {
178 return (isRegInClass(Reg, &PPC::VSFRCRegClass) ||
179 isRegInClass(Reg, &PPC::VSSRCRegClass));
185 bool isAnyVecReg(
unsigned Reg,
bool &Partial) {
186 if (isScalarVecReg(Reg))
188 return isScalarVecReg(Reg) ||
isVecReg(Reg);
204 bool Changed =
false;
207 if (gatherVectorInstructions()) {
209 recordUnoptimizableWebs();
210 markSwapsForRemoval();
211 Changed = removeSwaps();
232 const int InitialVectorSize(256);
234 SwapVector.reserve(InitialVectorSize);
245 bool PPCVSXSwapRemoval::gatherVectorInstructions() {
246 bool RelevantFunction =
false;
251 if (
MI.isDebugValue())
254 bool RelevantInstr =
false;
255 bool Partial =
false;
260 unsigned Reg = MO.getReg();
261 if (isAnyVecReg(Reg, Partial)) {
262 RelevantInstr =
true;
270 RelevantFunction =
true;
275 PPCVSXSwapEntry SwapEntry{};
276 int VecIdx = addSwapEntry(&
MI, SwapEntry);
278 switch(
MI.getOpcode()) {
287 SwapVector[VecIdx].MentionsPartialVR = 1;
289 SwapVector[VecIdx].IsSwappable = 1;
299 int immed =
MI.getOperand(3).getImm();
301 unsigned trueReg1 = lookThruCopyLike(
MI.getOperand(1).getReg(),
303 unsigned trueReg2 = lookThruCopyLike(
MI.getOperand(2).getReg(),
305 if (trueReg1 == trueReg2)
306 SwapVector[VecIdx].IsSwap = 1;
310 SwapVector[VecIdx].IsSwappable = 1;
311 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
321 }
else if (immed == 0 || immed == 3) {
323 SwapVector[VecIdx].IsSwappable = 1;
324 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
326 unsigned trueReg1 = lookThruCopyLike(
MI.getOperand(1).getReg(),
328 unsigned trueReg2 = lookThruCopyLike(
MI.getOperand(2).getReg(),
330 if (trueReg1 == trueReg2)
331 SwapVector[VecIdx].MentionsPhysVR = 0;
335 SwapVector[VecIdx].IsSwappable = 1;
336 SwapVector[VecIdx].SpecialHandling = SHValues::SH_XXPERMDI;
345 SwapVector[VecIdx].IsLoad = 1;
351 SwapVector[VecIdx].IsLoad = 1;
352 SwapVector[VecIdx].IsSwap = 1;
360 SwapVector[VecIdx].IsLoad = 1;
361 SwapVector[VecIdx].IsSwappable = 1;
368 SwapVector[VecIdx].IsStore = 1;
374 SwapVector[VecIdx].IsStore = 1;
375 SwapVector[VecIdx].IsSwap = 1;
382 SwapVector[VecIdx].IsSwappable = 1;
387 else if (isScalarVecReg(
MI.getOperand(0).getReg()) &&
388 isScalarVecReg(
MI.getOperand(1).getReg()))
389 SwapVector[VecIdx].IsSwappable = 1;
391 case PPC::SUBREG_TO_REG: {
401 SwapVector[VecIdx].IsSwappable = 1;
402 else if (
isVecReg(
MI.getOperand(0).getReg()) &&
403 isScalarVecReg(
MI.getOperand(2).getReg())) {
404 SwapVector[VecIdx].IsSwappable = 1;
405 SwapVector[VecIdx].SpecialHandling = SHValues::SH_COPYWIDEN;
415 SwapVector[VecIdx].IsSwappable = 1;
416 SwapVector[VecIdx].SpecialHandling = SHValues::SH_SPLAT;
427 case PPC::EXTRACT_SUBREG:
428 case PPC::INSERT_SUBREG:
429 case PPC::COPY_TO_REGCLASS:
449 case PPC::VCIPHERLAST:
469 case PPC::VNCIPHERLAST:
494 case PPC::VSHASIGMAD:
495 case PPC::VSHASIGMAW:
525 if (RelevantFunction) {
526 DEBUG(
dbgs() <<
"Swap vector when first built\n\n");
527 DEBUG(dumpSwapVector());
530 return RelevantFunction;
536 PPCVSXSwapEntry& SwapEntry) {
537 SwapEntry.VSEMI =
MI;
538 SwapEntry.VSEId = SwapVector.size();
539 SwapVector.push_back(SwapEntry);
540 EC->insert(SwapEntry.VSEId);
541 SwapMap[
MI] = SwapEntry.VSEId;
542 return SwapEntry.VSEId;
554 unsigned PPCVSXSwapRemoval::lookThruCopyLike(
unsigned SrcReg,
569 if (!isScalarVecReg(CopySrcReg))
570 SwapVector[VecIdx].MentionsPhysVR = 1;
574 return lookThruCopyLike(CopySrcReg, VecIdx);
584 void PPCVSXSwapRemoval::formWebs() {
586 DEBUG(
dbgs() <<
"\n*** Forming webs for swap removal ***\n\n");
588 for (
unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
592 DEBUG(
dbgs() <<
"\n" << SwapVector[EntryIdx].VSEId <<
" ");
603 unsigned Reg = MO.getReg();
604 if (!
isVecReg(Reg) && !isScalarVecReg(Reg))
608 if (!(MI->
isCopy() && isScalarVecReg(Reg)))
609 SwapVector[EntryIdx].MentionsPhysVR = 1;
617 assert(SwapMap.find(DefMI) != SwapMap.end() &&
618 "Inconsistency: def of vector reg not found in swap map!");
619 int DefIdx = SwapMap[DefMI];
620 (void)
EC->unionSets(SwapVector[DefIdx].VSEId,
621 SwapVector[EntryIdx].VSEId);
623 DEBUG(
dbgs() <<
format(
"Unioning %d with %d\n", SwapVector[DefIdx].VSEId,
624 SwapVector[EntryIdx].VSEId));
635 void PPCVSXSwapRemoval::recordUnoptimizableWebs() {
637 DEBUG(
dbgs() <<
"\n*** Rejecting webs for swap removal ***\n\n");
639 for (
unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
640 int Repr =
EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
643 if (SwapVector[Repr].WebRejected)
649 if (SwapVector[EntryIdx].MentionsPhysVR ||
650 SwapVector[EntryIdx].MentionsPartialVR ||
651 !(SwapVector[EntryIdx].IsSwappable || SwapVector[EntryIdx].IsSwap)) {
653 SwapVector[Repr].WebRejected = 1;
656 format(
"Web %d rejected for physreg, partial reg, or not "
657 "swap[pable]\n", Repr));
658 DEBUG(
dbgs() <<
" in " << EntryIdx <<
": ");
659 DEBUG(SwapVector[EntryIdx].VSEMI->dump());
665 else if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
674 int UseIdx = SwapMap[&
UseMI];
676 if (!SwapVector[UseIdx].IsSwap || SwapVector[UseIdx].IsLoad ||
677 SwapVector[UseIdx].IsStore) {
679 SwapVector[Repr].WebRejected = 1;
682 format(
"Web %d rejected for load not feeding swap\n", Repr));
683 DEBUG(
dbgs() <<
" def " << EntryIdx <<
": ");
685 DEBUG(
dbgs() <<
" use " << UseIdx <<
": ");
693 }
else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
698 int DefIdx = SwapMap[DefMI];
700 if (!SwapVector[DefIdx].IsSwap || SwapVector[DefIdx].IsLoad ||
701 SwapVector[DefIdx].IsStore) {
703 SwapVector[Repr].WebRejected = 1;
706 format(
"Web %d rejected for store not fed by swap\n", Repr));
707 DEBUG(
dbgs() <<
" def " << DefIdx <<
": ");
709 DEBUG(
dbgs() <<
" use " << EntryIdx <<
": ");
717 int UseIdx = SwapMap[&
UseMI];
719 if (SwapVector[UseIdx].VSEMI->getOpcode() != MI->
getOpcode()) {
720 SwapVector[Repr].WebRejected = 1;
723 format(
"Web %d rejected for swap not feeding only stores\n",
727 DEBUG(
dbgs() <<
" use " << UseIdx <<
": ");
728 DEBUG(SwapVector[UseIdx].VSEMI->dump());
735 DEBUG(
dbgs() <<
"Swap vector after web analysis:\n\n");
736 DEBUG(dumpSwapVector());
744 void PPCVSXSwapRemoval::markSwapsForRemoval() {
746 DEBUG(
dbgs() <<
"\n*** Marking swaps for removal ***\n\n");
748 for (
unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
750 if (SwapVector[EntryIdx].IsLoad && SwapVector[EntryIdx].IsSwap) {
751 int Repr =
EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
753 if (!SwapVector[Repr].WebRejected) {
758 int UseIdx = SwapMap[&
UseMI];
759 SwapVector[UseIdx].WillRemove = 1;
761 DEBUG(
dbgs() <<
"Marking swap fed by load for removal: ");
766 }
else if (SwapVector[EntryIdx].IsStore && SwapVector[EntryIdx].IsSwap) {
767 int Repr =
EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
769 if (!SwapVector[Repr].WebRejected) {
773 int DefIdx = SwapMap[DefMI];
774 SwapVector[DefIdx].WillRemove = 1;
776 DEBUG(
dbgs() <<
"Marking swap feeding store for removal: ");
780 }
else if (SwapVector[EntryIdx].IsSwappable &&
781 SwapVector[EntryIdx].SpecialHandling != 0) {
782 int Repr =
EC->getLeaderValue(SwapVector[EntryIdx].VSEId);
784 if (!SwapVector[Repr].WebRejected)
785 handleSpecialSwappables(EntryIdx);
797 unsigned DstReg,
unsigned SrcReg) {
810 void PPCVSXSwapRemoval::handleSpecialSwappables(
int EntryIdx) {
811 switch (SwapVector[EntryIdx].SpecialHandling) {
818 case SHValues::SH_SPLAT: {
828 case PPC::VSPLTB: NElts = 16;
break;
829 case PPC::VSPLTH: NElts = 8;
break;
831 case PPC::XXSPLTW: NElts = 4;
break;
840 EltNo = (EltNo + NElts / 2) % NElts;
857 case SHValues::SH_XXPERMDI: {
864 if (Selector == 0 || Selector == 3)
865 Selector = 3 - Selector;
881 case SHValues::SH_COPYWIDEN: {
884 DEBUG(
dbgs() <<
"Changing SUBREG_TO_REG: ");
889 unsigned NewVReg =
MRI->createVirtualRegister(DstRC);
902 if (DstRC == &PPC::VRRCRegClass) {
903 unsigned VSRCTmp1 =
MRI->createVirtualRegister(&PPC::VSRCRegClass);
904 unsigned VSRCTmp2 =
MRI->createVirtualRegister(&PPC::VSRCRegClass);
907 TII->get(PPC::COPY), VSRCTmp1)
911 insertSwap(MI, InsertPoint, VSRCTmp2, VSRCTmp1);
915 TII->get(PPC::COPY), DstReg)
920 insertSwap(MI, InsertPoint, DstReg, NewVReg);
930 bool PPCVSXSwapRemoval::removeSwaps() {
932 DEBUG(
dbgs() <<
"\n*** Removing swaps ***\n\n");
934 bool Changed =
false;
936 for (
unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
937 if (SwapVector[EntryIdx].WillRemove) {
946 SwapVector[EntryIdx].VSEId));
956 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
960 for (
unsigned EntryIdx = 0; EntryIdx < SwapVector.size(); ++EntryIdx) {
963 int ID = SwapVector[EntryIdx].VSEId;
970 if (SwapVector[EntryIdx].IsLoad)
972 if (SwapVector[EntryIdx].IsStore)
974 if (SwapVector[EntryIdx].IsSwap)
976 if (SwapVector[EntryIdx].MentionsPhysVR)
977 dbgs() <<
"physreg ";
978 if (SwapVector[EntryIdx].MentionsPartialVR)
979 dbgs() <<
"partialreg ";
981 if (SwapVector[EntryIdx].IsSwappable) {
982 dbgs() <<
"swappable ";
983 switch(SwapVector[EntryIdx].SpecialHandling) {
985 dbgs() <<
"special:**unknown**";
990 dbgs() <<
"special:extract ";
993 dbgs() <<
"special:insert ";
996 dbgs() <<
"special:load ";
999 dbgs() <<
"special:store ";
1002 dbgs() <<
"special:splat ";
1005 dbgs() <<
"special:xxpermdi ";
1008 dbgs() <<
"special:copywiden ";
1013 if (SwapVector[EntryIdx].WebRejected)
1014 dbgs() <<
"rejected ";
1015 if (SwapVector[EntryIdx].WillRemove)
1016 dbgs() <<
"remove ";
1032 "PowerPC VSX Swap Removal",
false,
false)
1036 char PPCVSXSwapRemoval::ID = 0;
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
FunctionPass * createPPCVSXSwapRemovalPass()
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
bool hasSubClassEq(const TargetRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
iterator_range< mop_iterator > operands()
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
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Reg
All possible values of the reg field in the ModR/M byte.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
INLINEASM - Represents an inline asm block.
bool isCopyLike() const
Return true if the instruction behaves like a copy.
CHAIN = STXVD2X CHAIN, VSRC, Ptr - Occurs only for little endian.
void initializePPCVSXSwapRemovalPass(PassRegistry &)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
VSRC, CHAIN = LXVD2X_LE CHAIN, Ptr - Occurs only for little endian.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineInstrBundleIterator< MachineInstr > iterator
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
const MachineOperand & getOperand(unsigned i) const
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
static bool isVecReg(unsigned Reg)
bool needsSwapsForVSXMemOps() const
MachineOperand class - Representation of each machine instruction operand.
VPERM - The PPC VPERM Instruction.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringRef > StandardNames)
initialize - Initialize the set of available library functions based on the specified target triple...
bool isSubregToReg() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
static unsigned UseReg(const MachineOperand &MO)
void setReg(unsigned Reg)
Change the register this operand corresponds to.
unsigned getReg() const
getReg - Returns the register number.
INITIALIZE_PASS_BEGIN(PPCVSXSwapRemoval, DEBUG_TYPE,"PowerPC VSX Swap Removal", false, false) INITIALIZE_PASS_END(PPCVSXSwapRemoval
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
XXPERMDI - The PPC XXPERMDI instruction.
bool contains(unsigned Reg) const
Return true if the specified register is included in this register class.