53 #define DEBUG_TYPE "x86-avoid-SFB" 64 "x86-sfb-inspection-limit",
65 cl::desc(
"X86: Number of instructions backward to " 66 "inspect for store forwarding blocks."),
71 using DisplacementSizeMap = std::map<int64_t, unsigned>;
81 return "X86 Avoid Store Forwarding Blocks";
96 BlockedLoadsStoresPairs;
107 const DisplacementSizeMap &BlockingStoresDispSizeMap);
111 int64_t LMMOffset, int64_t SMMOffset);
113 void buildCopy(
MachineInstr *LoadInst,
unsigned NLoadOpcode, int64_t LoadDisp,
115 int64_t StoreDisp,
unsigned Size, int64_t LMMOffset,
134 return new X86AvoidSFBPass();
138 return Opcode == X86::MOVUPSrm || Opcode == X86::MOVAPSrm ||
139 Opcode == X86::VMOVUPSrm || Opcode == X86::VMOVAPSrm ||
140 Opcode == X86::VMOVUPDrm || Opcode == X86::VMOVAPDrm ||
141 Opcode == X86::VMOVDQUrm || Opcode == X86::VMOVDQArm ||
142 Opcode == X86::VMOVUPSZ128rm || Opcode == X86::VMOVAPSZ128rm ||
143 Opcode == X86::VMOVUPDZ128rm || Opcode == X86::VMOVAPDZ128rm ||
144 Opcode == X86::VMOVDQU64Z128rm || Opcode == X86::VMOVDQA64Z128rm ||
145 Opcode == X86::VMOVDQU32Z128rm || Opcode == X86::VMOVDQA32Z128rm;
148 return Opcode == X86::VMOVUPSYrm || Opcode == X86::VMOVAPSYrm ||
149 Opcode == X86::VMOVUPDYrm || Opcode == X86::VMOVAPDYrm ||
150 Opcode == X86::VMOVDQUYrm || Opcode == X86::VMOVDQAYrm ||
151 Opcode == X86::VMOVUPSZ256rm || Opcode == X86::VMOVAPSZ256rm ||
152 Opcode == X86::VMOVUPDZ256rm || Opcode == X86::VMOVAPDZ256rm ||
153 Opcode == X86::VMOVDQU64Z256rm || Opcode == X86::VMOVDQA64Z256rm ||
154 Opcode == X86::VMOVDQU32Z256rm || Opcode == X86::VMOVDQA32Z256rm;
165 return StOpcode == X86::MOVUPSmr || StOpcode == X86::MOVAPSmr;
168 return StOpcode == X86::VMOVUPSmr || StOpcode == X86::VMOVAPSmr;
171 return StOpcode == X86::VMOVUPDmr || StOpcode == X86::VMOVAPDmr;
174 return StOpcode == X86::VMOVDQUmr || StOpcode == X86::VMOVDQAmr;
175 case X86::VMOVUPSZ128rm:
176 case X86::VMOVAPSZ128rm:
177 return StOpcode == X86::VMOVUPSZ128mr || StOpcode == X86::VMOVAPSZ128mr;
178 case X86::VMOVUPDZ128rm:
179 case X86::VMOVAPDZ128rm:
180 return StOpcode == X86::VMOVUPDZ128mr || StOpcode == X86::VMOVAPDZ128mr;
181 case X86::VMOVUPSYrm:
182 case X86::VMOVAPSYrm:
183 return StOpcode == X86::VMOVUPSYmr || StOpcode == X86::VMOVAPSYmr;
184 case X86::VMOVUPDYrm:
185 case X86::VMOVAPDYrm:
186 return StOpcode == X86::VMOVUPDYmr || StOpcode == X86::VMOVAPDYmr;
187 case X86::VMOVDQUYrm:
188 case X86::VMOVDQAYrm:
189 return StOpcode == X86::VMOVDQUYmr || StOpcode == X86::VMOVDQAYmr;
190 case X86::VMOVUPSZ256rm:
191 case X86::VMOVAPSZ256rm:
192 return StOpcode == X86::VMOVUPSZ256mr || StOpcode == X86::VMOVAPSZ256mr;
193 case X86::VMOVUPDZ256rm:
194 case X86::VMOVAPDZ256rm:
195 return StOpcode == X86::VMOVUPDZ256mr || StOpcode == X86::VMOVAPDZ256mr;
196 case X86::VMOVDQU64Z128rm:
197 case X86::VMOVDQA64Z128rm:
198 return StOpcode == X86::VMOVDQU64Z128mr || StOpcode == X86::VMOVDQA64Z128mr;
199 case X86::VMOVDQU32Z128rm:
200 case X86::VMOVDQA32Z128rm:
201 return StOpcode == X86::VMOVDQU32Z128mr || StOpcode == X86::VMOVDQA32Z128mr;
202 case X86::VMOVDQU64Z256rm:
203 case X86::VMOVDQA64Z256rm:
204 return StOpcode == X86::VMOVDQU64Z256mr || StOpcode == X86::VMOVDQA64Z256mr;
205 case X86::VMOVDQU32Z256rm:
206 case X86::VMOVDQA32Z256rm:
207 return StOpcode == X86::VMOVDQU32Z256mr || StOpcode == X86::VMOVDQA32Z256mr;
215 PBlock |= Opcode == X86::MOV64mr || Opcode == X86::MOV64mi32 ||
216 Opcode == X86::MOV32mr || Opcode == X86::MOV32mi ||
217 Opcode == X86::MOV16mr || Opcode == X86::MOV16mi ||
218 Opcode == X86::MOV8mr || Opcode == X86::MOV8mi;
220 PBlock |= Opcode == X86::VMOVUPSmr || Opcode == X86::VMOVAPSmr ||
221 Opcode == X86::VMOVUPDmr || Opcode == X86::VMOVAPDmr ||
222 Opcode == X86::VMOVDQUmr || Opcode == X86::VMOVDQAmr ||
223 Opcode == X86::VMOVUPSZ128mr || Opcode == X86::VMOVAPSZ128mr ||
224 Opcode == X86::VMOVUPDZ128mr || Opcode == X86::VMOVAPDZ128mr ||
225 Opcode == X86::VMOVDQU64Z128mr ||
226 Opcode == X86::VMOVDQA64Z128mr ||
227 Opcode == X86::VMOVDQU32Z128mr || Opcode == X86::VMOVDQA32Z128mr;
238 switch (LoadOpcode) {
239 case X86::VMOVUPSYrm:
240 case X86::VMOVAPSYrm:
241 return X86::VMOVUPSrm;
242 case X86::VMOVUPDYrm:
243 case X86::VMOVAPDYrm:
244 return X86::VMOVUPDrm;
245 case X86::VMOVDQUYrm:
246 case X86::VMOVDQAYrm:
247 return X86::VMOVDQUrm;
248 case X86::VMOVUPSZ256rm:
249 case X86::VMOVAPSZ256rm:
250 return X86::VMOVUPSZ128rm;
251 case X86::VMOVUPDZ256rm:
252 case X86::VMOVAPDZ256rm:
253 return X86::VMOVUPDZ128rm;
254 case X86::VMOVDQU64Z256rm:
255 case X86::VMOVDQA64Z256rm:
256 return X86::VMOVDQU64Z128rm;
257 case X86::VMOVDQU32Z256rm:
258 case X86::VMOVDQA32Z256rm:
259 return X86::VMOVDQU32Z128rm;
267 switch (StoreOpcode) {
268 case X86::VMOVUPSYmr:
269 case X86::VMOVAPSYmr:
270 return X86::VMOVUPSmr;
271 case X86::VMOVUPDYmr:
272 case X86::VMOVAPDYmr:
273 return X86::VMOVUPDmr;
274 case X86::VMOVDQUYmr:
275 case X86::VMOVDQAYmr:
276 return X86::VMOVDQUmr;
277 case X86::VMOVUPSZ256mr:
278 case X86::VMOVAPSZ256mr:
279 return X86::VMOVUPSZ128mr;
280 case X86::VMOVUPDZ256mr:
281 case X86::VMOVAPDZ256mr:
282 return X86::VMOVUPDZ128mr;
283 case X86::VMOVDQU64Z256mr:
284 case X86::VMOVDQA64Z256mr:
285 return X86::VMOVDQU64Z128mr;
286 case X86::VMOVDQU32Z256mr:
287 case X86::VMOVDQA32Z256mr:
288 return X86::VMOVDQU32Z128mr;
298 assert(AddrOffset != -1 &&
"Expected Memory Operand");
324 if (!((Base.
isReg() && Base.
getReg() != X86::NoRegister) || Base.
isFI()))
330 if (!(Index.
isReg() && Index.
getReg() == X86::NoRegister))
332 if (!(Segment.
isReg() && Segment.
getReg() == X86::NoRegister))
345 unsigned BlockCount = 0;
349 PBInst !=
E; ++PBInst) {
351 if (BlockCount >= InspectionLimit)
355 return PotentialBlockers;
362 if (BlockCount < InspectionLimit) {
364 int LimitLeft = InspectionLimit - BlockCount;
372 PBInst != PME; ++PBInst) {
374 if (PredCount >= LimitLeft)
376 if (PBInst->getDesc().isCall())
382 return PotentialBlockers;
387 unsigned NStoreOpcode, int64_t StoreDisp,
388 unsigned Size, int64_t LMMOffset,
396 unsigned Reg1 =
MRI->createVirtualRegister(
403 .addReg(X86::NoRegister)
405 .addReg(X86::NoRegister)
408 if (LoadBase.
isReg())
417 BuildMI(*MBB, StInst, StInst->getDebugLoc(),
TII->get(NStoreOpcode))
420 .addReg(X86::NoRegister)
422 .addReg(X86::NoRegister)
426 if (StoreBase.
isReg())
429 assert(StoreSrcVReg.
isReg() &&
"Expected virtual register");
434 void X86AvoidSFBPass::buildCopies(
int Size,
MachineInstr *LoadInst,
436 int64_t StDispImm, int64_t LMMOffset,
438 int LdDisp = LdDispImm;
439 int StDisp = StDispImm;
445 StDisp, MOV128SZ, LMMOffset, SMMOffset);
454 buildCopy(LoadInst, X86::MOV64rm, LdDisp, StoreInst, X86::MOV64mr, StDisp,
455 MOV64SZ, LMMOffset, SMMOffset);
464 buildCopy(LoadInst, X86::MOV32rm, LdDisp, StoreInst, X86::MOV32mr, StDisp,
465 MOV32SZ, LMMOffset, SMMOffset);
474 buildCopy(LoadInst, X86::MOV16rm, LdDisp, StoreInst, X86::MOV16mr, StDisp,
475 MOV16SZ, LMMOffset, SMMOffset);
484 buildCopy(LoadInst, X86::MOV8rm, LdDisp, StoreInst, X86::MOV8mr, StDisp,
485 MOV8SZ, LMMOffset, SMMOffset);
493 assert(Size == 0 &&
"Wrong size division");
499 if (LoadBase.
isReg()) {
509 if (StoreBase.
isReg()) {
532 void X86AvoidSFBPass::findPotentiallylBlockedCopies(
MachineFunction &MF) {
534 for (
auto &
MI : MBB) {
537 int DefVR =
MI.getOperand(0).getReg();
538 if (!
MRI->hasOneUse(DefVR))
540 for (
auto UI =
MRI->use_nodbg_begin(DefVR), UE =
MRI->use_nodbg_end();
550 "Expected one memory operand for load instruction");
552 "Expected one memory operand for store instruction");
554 BlockedLoadsStoresPairs.push_back(std::make_pair(&
MI, &StoreMI));
560 unsigned X86AvoidSFBPass::getRegSizeInBytes(
MachineInstr *LoadInst) {
563 return TRI->getRegSizeInBits(*TRC) / 8;
566 void X86AvoidSFBPass::breakBlockedCopies(
568 const DisplacementSizeMap &BlockingStoresDispSizeMap) {
571 int64_t LMMOffset = 0;
572 int64_t SMMOffset = 0;
574 int64_t LdDisp1 = LdDispImm;
576 int64_t StDisp1 = StDispImm;
580 int64_t LdStDelta = StDispImm - LdDispImm;
582 for (
auto DispSizePair : BlockingStoresDispSizeMap) {
583 LdDisp2 = DispSizePair.first;
584 StDisp2 = DispSizePair.first + LdStDelta;
585 Size2 = DispSizePair.second;
587 if (LdDisp2 < LdDisp1) {
588 int OverlapDelta = LdDisp1 - LdDisp2;
589 LdDisp2 += OverlapDelta;
590 StDisp2 += OverlapDelta;
591 Size2 -= OverlapDelta;
597 buildCopies(Size1, LoadInst, LdDisp1, StoreInst, StDisp1, LMMOffset,
600 buildCopies(Size2, LoadInst, LdDisp2, StoreInst, StDisp2, LMMOffset + Size1,
602 LdDisp1 = LdDisp2 + Size2;
603 StDisp1 = StDisp2 + Size2;
604 LMMOffset += Size1 + Size2;
605 SMMOffset += Size1 + Size2;
607 unsigned Size3 = (LdDispImm + getRegSizeInBytes(LoadInst)) - LdDisp1;
608 buildCopies(Size3, LoadInst, LdDisp1, StoreInst, StDisp1, LMMOffset,
618 if (LoadBase.
isReg())
624 int64_t StoreDispImm,
unsigned StoreSize) {
625 return ((StoreDispImm >= LoadDispImm) &&
626 (StoreDispImm <= LoadDispImm + (LoadSize - StoreSize)));
632 int64_t DispImm,
unsigned Size) {
633 if (BlockingStoresDispSizeMap.count(DispImm)) {
635 if (BlockingStoresDispSizeMap[DispImm] > Size)
636 BlockingStoresDispSizeMap[DispImm] =
Size;
639 BlockingStoresDispSizeMap[DispImm] =
Size;
645 if (BlockingStoresDispSizeMap.size() <= 1)
648 int64_t PrevDisp = BlockingStoresDispSizeMap.begin()->first;
649 unsigned PrevSize = BlockingStoresDispSizeMap.begin()->second;
651 for (
auto DispSizePair = std::next(BlockingStoresDispSizeMap.begin());
652 DispSizePair != BlockingStoresDispSizeMap.end(); ++DispSizePair) {
653 int64_t CurrDisp = DispSizePair->first;
654 unsigned CurrSize = DispSizePair->second;
655 if (CurrDisp + CurrSize <= PrevDisp + PrevSize) {
661 for (
auto Disp : ForRemoval)
662 BlockingStoresDispSizeMap.erase(Disp);
666 bool Changed =
false;
668 if (DisableX86AvoidStoreForwardBlocks || skipFunction(MF.
getFunction()) ||
673 assert(
MRI->isSSA() &&
"Expected MIR to be in SSA form");
676 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
679 findPotentiallylBlockedCopies(MF);
681 for (
auto LoadStoreInstPair : BlockedLoadsStoresPairs) {
684 DisplacementSizeMap BlockingStoresDispSizeMap;
688 for (
auto PBInst : PotentialBlockers) {
694 assert(PBInst->hasOneMemOperand() &&
"Expected One Memory Operand");
695 unsigned PBstSize = (*PBInst->memoperands_begin())->getSize();
707 if (BlockingStoresDispSizeMap.empty())
719 breakBlockedCopies(LoadInst, StoreInst, BlockingStoresDispSizeMap);
721 ForRemoval.push_back(LoadInst);
722 ForRemoval.push_back(StoreInst);
724 for (
auto RemovedInst : ForRemoval) {
725 RemovedInst->eraseFromParent();
728 BlockedLoadsStoresPairs.clear();
bool is64Bit() const
Is this x86_64? (disregarding specific ABI / programming model)
void push_back(const T &Elt)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static bool isYMMLoadOpcode(unsigned Opcode)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static void updateKillStatus(MachineInstr *LoadInst, MachineInstr *StoreInst)
AddrNumOperands - Total number of operands in a memory reference.
Compute iterated dominance frontiers using a linear time algorithm.
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.
unsigned getReg() const
getReg - Returns the register number.
The two locations do not alias at all.
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)
void initializeX86AvoidSFBPassPass(PassRegistry &)
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)
getOperandBias - compute whether all of the def operands are repeated in the uses and therefore shoul...
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.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
static SmallVector< MachineInstr *, 2 > findPotentialBlockers(MachineInstr *LoadInst)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static MachineOperand & getBaseOperand(MachineInstr *MI)
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
APFloat abs(APFloat X)
Returns the absolute value of the argument.
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.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static unsigned getYMMtoXMMLoadOpcode(unsigned LoadOpcode)
AddrSegmentReg - The operand # of the segment in the memory operand.
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static bool isPotentialBlockedMemCpyPair(int LdOpcode, int StOpcode)
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const MachineOperand & getOperand(unsigned i) const
int getMemoryOperandNo(uint64_t TSFlags)
getMemoryOperandNo - The function returns the MCInst operand # for the first field of the memory oper...