54 #define DEBUG_TYPE "x86-avoid-SFB" 61 "x86-sfb-inspection-limit",
62 cl::desc(
"X86: Number of instructions backward to " 63 "inspect for store forwarding blocks."),
68 using DisplacementSizeMap = std::map<int64_t, unsigned>;
76 return "X86 Avoid Store Forwarding Blocks";
91 BlockedLoadsStoresPairs;
102 const DisplacementSizeMap &BlockingStoresDispSizeMap);
106 int64_t LMMOffset, int64_t SMMOffset);
110 int64_t StoreDisp,
unsigned Size, int64_t LMMOffset,
129 return new X86AvoidSFBPass();
133 return Opcode == X86::MOVUPSrm || Opcode == X86::MOVAPSrm ||
134 Opcode == X86::VMOVUPSrm || Opcode == X86::VMOVAPSrm ||
135 Opcode == X86::VMOVUPDrm || Opcode == X86::VMOVAPDrm ||
136 Opcode == X86::VMOVDQUrm || Opcode == X86::VMOVDQArm ||
137 Opcode == X86::VMOVUPSZ128rm || Opcode == X86::VMOVAPSZ128rm ||
138 Opcode == X86::VMOVUPDZ128rm || Opcode == X86::VMOVAPDZ128rm ||
139 Opcode == X86::VMOVDQU64Z128rm || Opcode == X86::VMOVDQA64Z128rm ||
140 Opcode == X86::VMOVDQU32Z128rm || Opcode == X86::VMOVDQA32Z128rm;
143 return Opcode == X86::VMOVUPSYrm || Opcode == X86::VMOVAPSYrm ||
144 Opcode == X86::VMOVUPDYrm || Opcode == X86::VMOVAPDYrm ||
145 Opcode == X86::VMOVDQUYrm || Opcode == X86::VMOVDQAYrm ||
146 Opcode == X86::VMOVUPSZ256rm || Opcode == X86::VMOVAPSZ256rm ||
147 Opcode == X86::VMOVUPDZ256rm || Opcode == X86::VMOVAPDZ256rm ||
148 Opcode == X86::VMOVDQU64Z256rm || Opcode == X86::VMOVDQA64Z256rm ||
149 Opcode == X86::VMOVDQU32Z256rm || Opcode == X86::VMOVDQA32Z256rm;
160 return StOpcode == X86::MOVUPSmr || StOpcode == X86::MOVAPSmr;
163 return StOpcode == X86::VMOVUPSmr || StOpcode == X86::VMOVAPSmr;
166 return StOpcode == X86::VMOVUPDmr || StOpcode == X86::VMOVAPDmr;
169 return StOpcode == X86::VMOVDQUmr || StOpcode == X86::VMOVDQAmr;
170 case X86::VMOVUPSZ128rm:
171 case X86::VMOVAPSZ128rm:
172 return StOpcode == X86::VMOVUPSZ128mr || StOpcode == X86::VMOVAPSZ128mr;
173 case X86::VMOVUPDZ128rm:
174 case X86::VMOVAPDZ128rm:
175 return StOpcode == X86::VMOVUPDZ128mr || StOpcode == X86::VMOVAPDZ128mr;
176 case X86::VMOVUPSYrm:
177 case X86::VMOVAPSYrm:
178 return StOpcode == X86::VMOVUPSYmr || StOpcode == X86::VMOVAPSYmr;
179 case X86::VMOVUPDYrm:
180 case X86::VMOVAPDYrm:
181 return StOpcode == X86::VMOVUPDYmr || StOpcode == X86::VMOVAPDYmr;
182 case X86::VMOVDQUYrm:
183 case X86::VMOVDQAYrm:
184 return StOpcode == X86::VMOVDQUYmr || StOpcode == X86::VMOVDQAYmr;
185 case X86::VMOVUPSZ256rm:
186 case X86::VMOVAPSZ256rm:
187 return StOpcode == X86::VMOVUPSZ256mr || StOpcode == X86::VMOVAPSZ256mr;
188 case X86::VMOVUPDZ256rm:
189 case X86::VMOVAPDZ256rm:
190 return StOpcode == X86::VMOVUPDZ256mr || StOpcode == X86::VMOVAPDZ256mr;
191 case X86::VMOVDQU64Z128rm:
192 case X86::VMOVDQA64Z128rm:
193 return StOpcode == X86::VMOVDQU64Z128mr || StOpcode == X86::VMOVDQA64Z128mr;
194 case X86::VMOVDQU32Z128rm:
195 case X86::VMOVDQA32Z128rm:
196 return StOpcode == X86::VMOVDQU32Z128mr || StOpcode == X86::VMOVDQA32Z128mr;
197 case X86::VMOVDQU64Z256rm:
198 case X86::VMOVDQA64Z256rm:
199 return StOpcode == X86::VMOVDQU64Z256mr || StOpcode == X86::VMOVDQA64Z256mr;
200 case X86::VMOVDQU32Z256rm:
201 case X86::VMOVDQA32Z256rm:
202 return StOpcode == X86::VMOVDQU32Z256mr || StOpcode == X86::VMOVDQA32Z256mr;
210 PBlock |= Opcode == X86::MOV64mr || Opcode == X86::MOV64mi32 ||
211 Opcode == X86::MOV32mr || Opcode == X86::MOV32mi ||
212 Opcode == X86::MOV16mr || Opcode == X86::MOV16mi ||
213 Opcode == X86::MOV8mr || Opcode == X86::MOV8mi;
215 PBlock |= Opcode == X86::VMOVUPSmr || Opcode == X86::VMOVAPSmr ||
216 Opcode == X86::VMOVUPDmr || Opcode == X86::VMOVAPDmr ||
217 Opcode == X86::VMOVDQUmr || Opcode == X86::VMOVDQAmr ||
218 Opcode == X86::VMOVUPSZ128mr || Opcode == X86::VMOVAPSZ128mr ||
219 Opcode == X86::VMOVUPDZ128mr || Opcode == X86::VMOVAPDZ128mr ||
220 Opcode == X86::VMOVDQU64Z128mr ||
221 Opcode == X86::VMOVDQA64Z128mr ||
222 Opcode == X86::VMOVDQU32Z128mr || Opcode == X86::VMOVDQA32Z128mr;
233 switch (LoadOpcode) {
234 case X86::VMOVUPSYrm:
235 case X86::VMOVAPSYrm:
236 return X86::VMOVUPSrm;
237 case X86::VMOVUPDYrm:
238 case X86::VMOVAPDYrm:
239 return X86::VMOVUPDrm;
240 case X86::VMOVDQUYrm:
241 case X86::VMOVDQAYrm:
242 return X86::VMOVDQUrm;
243 case X86::VMOVUPSZ256rm:
244 case X86::VMOVAPSZ256rm:
245 return X86::VMOVUPSZ128rm;
246 case X86::VMOVUPDZ256rm:
247 case X86::VMOVAPDZ256rm:
248 return X86::VMOVUPDZ128rm;
249 case X86::VMOVDQU64Z256rm:
250 case X86::VMOVDQA64Z256rm:
251 return X86::VMOVDQU64Z128rm;
252 case X86::VMOVDQU32Z256rm:
253 case X86::VMOVDQA32Z256rm:
254 return X86::VMOVDQU32Z128rm;
262 switch (StoreOpcode) {
263 case X86::VMOVUPSYmr:
264 case X86::VMOVAPSYmr:
265 return X86::VMOVUPSmr;
266 case X86::VMOVUPDYmr:
267 case X86::VMOVAPDYmr:
268 return X86::VMOVUPDmr;
269 case X86::VMOVDQUYmr:
270 case X86::VMOVDQAYmr:
271 return X86::VMOVDQUmr;
272 case X86::VMOVUPSZ256mr:
273 case X86::VMOVAPSZ256mr:
274 return X86::VMOVUPSZ128mr;
275 case X86::VMOVUPDZ256mr:
276 case X86::VMOVAPDZ256mr:
277 return X86::VMOVUPDZ128mr;
278 case X86::VMOVDQU64Z256mr:
279 case X86::VMOVDQA64Z256mr:
280 return X86::VMOVDQU64Z128mr;
281 case X86::VMOVDQU32Z256mr:
282 case X86::VMOVDQA32Z256mr:
283 return X86::VMOVDQU32Z128mr;
293 assert(AddrOffset != -1 &&
"Expected Memory Operand");
319 if (!((Base.
isReg() && Base.
getReg() != X86::NoRegister) || Base.
isFI()))
325 if (!(Index.
isReg() && Index.
getReg() == X86::NoRegister))
327 if (!(Segment.
isReg() && Segment.
getReg() == X86::NoRegister))
340 unsigned BlockCount = 0;
344 PBInst !=
E; ++PBInst) {
345 if (PBInst->isMetaInstruction())
348 if (BlockCount >= InspectionLimit)
352 return PotentialBlockers;
359 if (BlockCount < InspectionLimit) {
361 int LimitLeft = InspectionLimit - BlockCount;
369 PBInst != PME; ++PBInst) {
370 if (PBInst->isMetaInstruction())
373 if (PredCount >= LimitLeft)
375 if (PBInst->getDesc().isCall())
381 return PotentialBlockers;
386 unsigned NStoreOpcode, int64_t StoreDisp,
387 unsigned Size, int64_t LMMOffset,
402 .addReg(X86::NoRegister)
404 .addReg(X86::NoRegister)
407 if (LoadBase.
isReg())
416 if (PrevInstrIt.getNodePtr() == LoadInst)
419 BuildMI(*MBB, StInst, StInst->getDebugLoc(),
TII->get(NStoreOpcode))
422 .addReg(X86::NoRegister)
424 .addReg(X86::NoRegister)
428 if (StoreBase.
isReg())
431 assert(StoreSrcVReg.
isReg() &&
"Expected virtual register");
436 void X86AvoidSFBPass::buildCopies(
int Size,
MachineInstr *LoadInst,
438 int64_t StDispImm, int64_t LMMOffset,
440 int LdDisp = LdDispImm;
441 int StDisp = StDispImm;
447 StDisp, MOV128SZ, LMMOffset, SMMOffset);
454 if (Size - MOV64SZ >= 0) {
456 buildCopy(LoadInst, X86::MOV64rm, LdDisp, StoreInst, X86::MOV64mr, StDisp,
457 MOV64SZ, LMMOffset, SMMOffset);
464 if (Size - MOV32SZ >= 0) {
466 buildCopy(LoadInst, X86::MOV32rm, LdDisp, StoreInst, X86::MOV32mr, StDisp,
467 MOV32SZ, LMMOffset, SMMOffset);
474 if (Size - MOV16SZ >= 0) {
476 buildCopy(LoadInst, X86::MOV16rm, LdDisp, StoreInst, X86::MOV16mr, StDisp,
477 MOV16SZ, LMMOffset, SMMOffset);
484 if (Size - MOV8SZ >= 0) {
486 buildCopy(LoadInst, X86::MOV8rm, LdDisp, StoreInst, X86::MOV8mr, StDisp,
487 MOV8SZ, LMMOffset, SMMOffset);
495 assert(Size == 0 &&
"Wrong size division");
504 if (LoadBase.
isReg()) {
510 if (StorePrevNonDbgInstr == LoadInst)
514 if (StoreBase.
isReg()) {
516 if (StorePrevNonDbgInstr == LoadInst)
537 void X86AvoidSFBPass::findPotentiallylBlockedCopies(
MachineFunction &MF) {
539 for (
auto &
MI : MBB) {
542 int DefVR =
MI.getOperand(0).getReg();
543 if (!
MRI->hasOneNonDBGUse(DefVR))
545 for (
auto UI =
MRI->use_nodbg_begin(DefVR), UE =
MRI->use_nodbg_end();
555 "Expected one memory operand for load instruction");
557 "Expected one memory operand for store instruction");
559 BlockedLoadsStoresPairs.push_back(std::make_pair(&
MI, &StoreMI));
565 unsigned X86AvoidSFBPass::getRegSizeInBytes(
MachineInstr *LoadInst) {
568 return TRI->getRegSizeInBits(*TRC) / 8;
571 void X86AvoidSFBPass::breakBlockedCopies(
573 const DisplacementSizeMap &BlockingStoresDispSizeMap) {
576 int64_t LMMOffset = 0;
577 int64_t SMMOffset = 0;
579 int64_t LdDisp1 = LdDispImm;
581 int64_t StDisp1 = StDispImm;
585 int64_t LdStDelta = StDispImm - LdDispImm;
587 for (
auto DispSizePair : BlockingStoresDispSizeMap) {
588 LdDisp2 = DispSizePair.first;
589 StDisp2 = DispSizePair.first + LdStDelta;
590 Size2 = DispSizePair.second;
592 if (LdDisp2 < LdDisp1) {
593 int OverlapDelta = LdDisp1 - LdDisp2;
594 LdDisp2 += OverlapDelta;
595 StDisp2 += OverlapDelta;
596 Size2 -= OverlapDelta;
598 Size1 = LdDisp2 - LdDisp1;
602 buildCopies(Size1, LoadInst, LdDisp1, StoreInst, StDisp1, LMMOffset,
605 buildCopies(Size2, LoadInst, LdDisp2, StoreInst, StDisp2, LMMOffset + Size1,
607 LdDisp1 = LdDisp2 + Size2;
608 StDisp1 = StDisp2 + Size2;
609 LMMOffset += Size1 + Size2;
610 SMMOffset += Size1 + Size2;
612 unsigned Size3 = (LdDispImm + getRegSizeInBytes(LoadInst)) - LdDisp1;
613 buildCopies(Size3, LoadInst, LdDisp1, StoreInst, StDisp1, LMMOffset,
623 if (LoadBase.
isReg())
629 int64_t StoreDispImm,
unsigned StoreSize) {
630 return ((StoreDispImm >= LoadDispImm) &&
631 (StoreDispImm <= LoadDispImm + (LoadSize - StoreSize)));
637 int64_t DispImm,
unsigned Size) {
638 if (BlockingStoresDispSizeMap.count(DispImm)) {
640 if (BlockingStoresDispSizeMap[DispImm] > Size)
641 BlockingStoresDispSizeMap[DispImm] =
Size;
644 BlockingStoresDispSizeMap[DispImm] =
Size;
650 if (BlockingStoresDispSizeMap.size() <= 1)
654 for (
auto DispSizePair : BlockingStoresDispSizeMap) {
655 int64_t CurrDisp = DispSizePair.first;
656 unsigned CurrSize = DispSizePair.second;
657 while (DispSizeStack.
size()) {
658 int64_t PrevDisp = DispSizeStack.
back().first;
659 unsigned PrevSize = DispSizeStack.
back().second;
660 if (CurrDisp + CurrSize > PrevDisp + PrevSize)
666 BlockingStoresDispSizeMap.clear();
667 for (
auto Disp : DispSizeStack)
668 BlockingStoresDispSizeMap.insert(Disp);
672 bool Changed =
false;
679 assert(
MRI->isSSA() &&
"Expected MIR to be in SSA form");
682 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
685 findPotentiallylBlockedCopies(MF);
687 for (
auto LoadStoreInstPair : BlockedLoadsStoresPairs) {
690 DisplacementSizeMap BlockingStoresDispSizeMap;
694 for (
auto PBInst : PotentialBlockers) {
700 assert(PBInst->hasOneMemOperand() &&
"Expected One Memory Operand");
701 unsigned PBstSize = (*PBInst->memoperands_begin())->getSize();
713 if (BlockingStoresDispSizeMap.empty())
725 breakBlockedCopies(LoadInst, StoreInst, BlockingStoresDispSizeMap);
727 ForRemoval.push_back(LoadInst);
728 ForRemoval.push_back(StoreInst);
730 for (
auto RemovedInst : ForRemoval) {
731 RemovedInst->eraseFromParent();
734 BlockedLoadsStoresPairs.clear();
bool is64Bit() const
Is this x86_64? (disregarding specific ABI / programming model)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static bool isYMMLoadOpcode(unsigned Opcode)
instr_iterator instr_begin()
static void updateKillStatus(MachineInstr *LoadInst, MachineInstr *StoreInst)
This class represents lattice values for constants.
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.
The two locations do not alias at all.
AddrNumOperands - Total number of operands in a memory reference.
uint64_t getSize() const
Return the size in bytes of the memory reference.
unsigned const TargetRegisterInfo * TRI
An instruction for reading from memory.
static void removeRedundantBlockingStores(DisplacementSizeMap &BlockingStoresDispSizeMap)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static cl::opt< bool > DisableX86AvoidStoreForwardBlocks("x86-disable-avoid-SFB", cl::Hidden, cl::desc("X86: Disable Store Forwarding Blocks fixup."), cl::init(false))
static void updateBlockingStoresDispSizeMap(DisplacementSizeMap &BlockingStoresDispSizeMap, int64_t DispImm, unsigned Size)
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
A description of a memory reference used in the backend.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static bool hasSameBaseOpValue(MachineInstr *LoadInst, MachineInstr *StoreInst)
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, unsigned base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
static bool isBlockingStore(int64_t LoadDispImm, unsigned LoadSize, int64_t StoreDispImm, unsigned StoreSize)
static bool isPotentialBlockingStoreInst(int Opcode, int LoadOpcode)
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
An instruction for storing to memory.
COFF::MachineTypes Machine
reverse_iterator rbegin()
AliasResult
The possible results of an alias query.
const Value * getValue() const
Return the base address of the memory access.
static cl::opt< unsigned > X86AvoidSFBInspectionLimit("x86-sfb-inspection-limit", cl::desc("X86: Number of instructions backward to " "inspect for store forwarding blocks."), cl::init(20), cl::Hidden)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
static int getAddrOffset(MachineInstr *MI)
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static unsigned getYMMtoXMMStoreOpcode(unsigned StoreOpcode)
Represent the analysis usage information of a pass.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
static bool isPotentialBlockedMemCpyLd(unsigned Opcode)
FunctionPass class - This class is used to implement most global optimizations.
unsigned getOperandBias(const MCInstrDesc &Desc)
Compute whether all of the def operands are repeated in the uses and therefore should be skipped...
std::vector< MachineBasicBlock * >::iterator pred_iterator
pred_iterator pred_begin()
static const int MOV128SZ
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setIsKill(bool Val=true)
Representation for a specific memory location.
Iterator for intrusive lists based on ilist_node.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
static SmallVector< MachineInstr *, 2 > findPotentialBlockers(MachineInstr *LoadInst)
MachineOperand class - Representation of each machine instruction operand.
AddrSegmentReg - The operand # of the segment in the memory operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static MachineOperand & getBaseOperand(MachineInstr *MI)
IterT skipDebugInstructionsBackward(IterT It, IterT Begin)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static MachineOperand & getDispOperand(MachineInstr *MI)
const MachineBasicBlock * getParent() const
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.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
static bool isRelevantAddressingMode(MachineInstr *MI)
INITIALIZE_PASS_BEGIN(X86AvoidSFBPass, DEBUG_TYPE, "Machine code sinking", false, false) INITIALIZE_PASS_END(X86AvoidSFBPass
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
bool isCall() const
Return true if the instruction is a call.
static bool isXMMLoadOpcode(unsigned Opcode)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
FunctionPass * createX86AvoidStoreForwardingBlocks()
Return a pass that avoids creating store forward block issues in the hardware.
static unsigned getYMMtoXMMLoadOpcode(unsigned LoadOpcode)
StringRef - Represent a constant reference to a string, i.e.
static bool isPotentialBlockedMemCpyPair(int LdOpcode, int StOpcode)
Register getReg() const
getReg - Returns the register number.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const MachineOperand & getOperand(unsigned i) const
Wrapper class representing virtual and physical registers.
int getMemoryOperandNo(uint64_t TSFlags)
The function returns the MCInst operand # for the first field of the memory operand.