66 #define DEBUG_TYPE "si-load-store-opt"
89 static bool offsetsCanBeCombined(CombineInfo &CI);
91 bool findMatchingDSInst(CombineInfo &CI);
108 StringRef getPassName()
const override {
return "SI Load / Store Optimizer"; }
121 "SI Load / Store Optimizer",
false,
false)
126 char SILoadStoreOptimizer::
ID = 0;
131 return new SILoadStoreOptimizer();
139 MI->removeFromParent();
159 !(A->mayStore() || B->mayStore()));
191 if (!InstToMove->mayLoadOrStore())
199 bool SILoadStoreOptimizer::offsetsCanBeCombined(CombineInfo &CI) {
202 if (CI.Offset0 == CI.Offset1)
206 if ((CI.Offset0 % CI.EltSize != 0) || (CI.Offset1 % CI.EltSize != 0))
209 unsigned EltOffset0 = CI.Offset0 / CI.EltSize;
210 unsigned EltOffset1 = CI.Offset1 / CI.EltSize;
216 if ((EltOffset0 % 64 == 0) && (EltOffset1 % 64) == 0 &&
218 CI.Offset0 = EltOffset0 / 64;
219 CI.Offset1 = EltOffset1 / 64;
226 CI.Offset0 = EltOffset0;
227 CI.Offset1 = EltOffset1;
232 unsigned OffsetDiff =
std::abs((
int)EltOffset1 - (
int)EltOffset0);
233 CI.BaseOff =
std::min(CI.Offset0, CI.Offset1);
235 if ((OffsetDiff % 64 == 0) &&
isUInt<8>(OffsetDiff / 64)) {
236 CI.Offset0 = (EltOffset0 - CI.BaseOff / CI.EltSize) / 64;
237 CI.Offset1 = (EltOffset1 - CI.BaseOff / CI.EltSize) / 64;
243 CI.Offset0 = EltOffset0 - CI.BaseOff / CI.EltSize;
244 CI.Offset1 = EltOffset1 - CI.BaseOff / CI.EltSize;
251 bool SILoadStoreOptimizer::findMatchingDSInst(CombineInfo &CI) {
259 for ( ; MBBI !=
E; ++MBBI) {
260 if (MBBI->getOpcode() != CI.I->getOpcode()) {
268 if (MBBI->hasUnmodeledSideEffects())
273 if (MBBI->mayLoadOrStore() &&
278 CI.InstsToMove.push_back(&*MBBI);
291 if (MBBI->hasOrderedMemoryRef())
304 AMDGPU::OpName::addr);
310 if (AddrReg0.getReg() == AddrReg1.
getReg() &&
311 AddrReg0.getSubReg() == AddrReg1.
getSubReg()) {
313 AMDGPU::OpName::offset);
314 CI.Offset0 = CI.I->getOperand(OffsetIdx).getImm() & 0xffff;
315 CI.Offset1 = MBBI->getOperand(OffsetIdx).getImm() & 0xffff;
322 if (offsetsCanBeCombined(CI))
345 const auto *AddrReg =
TII->getNamedOperand(*CI.I, AMDGPU::OpName::addr);
347 const auto *Dest0 =
TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdst);
348 const auto *Dest1 =
TII->getNamedOperand(*CI.Paired, AMDGPU::OpName::vdst);
350 unsigned NewOffset0 = CI.Offset0;
351 unsigned NewOffset1 = CI.Offset1;
352 unsigned Opc = (CI.EltSize == 4) ? AMDGPU::DS_READ2_B32
353 : AMDGPU::DS_READ2_B64;
356 Opc = (CI.EltSize == 4) ? AMDGPU::DS_READ2ST64_B32
357 : AMDGPU::DS_READ2ST64_B64;
359 unsigned SubRegIdx0 = (CI.EltSize == 4) ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
360 unsigned SubRegIdx1 = (CI.EltSize == 4) ? AMDGPU::sub1 : AMDGPU::sub2_sub3;
362 if (NewOffset0 > NewOffset1) {
369 (NewOffset0 != NewOffset1) &&
370 "Computed offset doesn't fit");
375 = (CI.EltSize == 4) ? &AMDGPU::VReg_64RegClass : &AMDGPU::VReg_128RegClass;
376 unsigned DestReg =
MRI->createVirtualRegister(SuperRC);
380 unsigned BaseReg = AddrReg->getReg();
381 unsigned BaseRegFlags = 0;
383 BaseReg =
MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
385 BuildMI(*MBB, CI.Paired, DL,
TII->get(AMDGPU::V_ADD_I32_e32), BaseReg)
387 .
addReg(AddrReg->getReg());
391 BuildMI(*MBB, CI.Paired, DL, Read2Desc, DestReg)
392 .
addReg(BaseReg, BaseRegFlags)
396 .
setMemRefs(CI.I->mergeMemRefsWith(*CI.Paired));
403 BuildMI(*MBB, CI.Paired, DL, CopyDesc)
405 .
addReg(DestReg, 0, SubRegIdx0);
413 CI.I->eraseFromParent();
414 CI.Paired->eraseFromParent();
416 DEBUG(
dbgs() <<
"Inserted read2: " << *Read2 <<
'\n');
427 const MachineOperand *Data0 =
TII->getNamedOperand(*CI.I, AMDGPU::OpName::data0);
429 =
TII->getNamedOperand(*CI.Paired, AMDGPU::OpName::data0);
431 unsigned NewOffset0 = CI.Offset0;
432 unsigned NewOffset1 = CI.Offset1;
433 unsigned Opc = (CI.EltSize == 4) ? AMDGPU::DS_WRITE2_B32
434 : AMDGPU::DS_WRITE2_B64;
437 Opc = (CI.EltSize == 4) ? AMDGPU::DS_WRITE2ST64_B32
438 : AMDGPU::DS_WRITE2ST64_B64;
440 if (NewOffset0 > NewOffset1) {
447 (NewOffset0 != NewOffset1) &&
448 "Computed offset doesn't fit");
453 unsigned BaseReg = Addr->
getReg();
454 unsigned BaseRegFlags = 0;
456 BaseReg =
MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
458 BuildMI(*MBB, CI.Paired, DL,
TII->get(AMDGPU::V_ADD_I32_e32), BaseReg)
464 BuildMI(*MBB, CI.Paired, DL, Write2Desc)
465 .
addReg(BaseReg, BaseRegFlags)
471 .
setMemRefs(CI.I->mergeMemRefsWith(*CI.Paired));
476 CI.I->eraseFromParent();
477 CI.Paired->eraseFromParent();
479 DEBUG(
dbgs() <<
"Inserted write2 inst: " << *Write2 <<
'\n');
487 bool Modified =
false;
501 if (Opc == AMDGPU::DS_READ_B32 || Opc == AMDGPU::DS_READ_B64) {
502 CI.EltSize = (Opc == AMDGPU::DS_READ_B64) ? 8 : 4;
503 if (findMatchingDSInst(CI)) {
505 I = mergeRead2Pair(CI);
511 }
else if (Opc == AMDGPU::DS_WRITE_B32 || Opc == AMDGPU::DS_WRITE_B64) {
512 CI.EltSize = (Opc == AMDGPU::DS_WRITE_B64) ? 8 : 4;
513 if (findMatchingDSInst(CI)) {
515 I = mergeWrite2Pair(CI);
541 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
543 DEBUG(
dbgs() <<
"Running SILoadStoreOptimizer\n");
545 bool Modified =
false;
548 Modified |= optimizeBlock(MBB);
void push_back(const T &Elt)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Interface definition for SIRegisterInfo.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AMDGPU specific subclass of TargetSubtarget.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
char & SILoadStoreOptimizerID
Describe properties that are true of each instruction in the target description file.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const SIInstrInfo * getInstrInfo() const override
bool readsVirtualRegister(unsigned Reg) const
Return true if the MachineInstr reads the specified virtual register.
bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
bool loadStoreOptEnabled() const
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static bool memAccessesCanBeReordered(MachineBasicBlock::iterator A, MachineBasicBlock::iterator B, const SIInstrInfo *TII, AliasAnalysis *AA)
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
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const HexagonRegisterInfo & getRegisterInfo() const
HexagonInstrInfo specifics.
static bool canMoveInstsAcrossMemOp(MachineInstr &MemOp, ArrayRef< MachineInstr * > InstsToMove, const SIInstrInfo *TII, AliasAnalysis *AA)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const MachineInstrBuilder & setMemRefs(MachineInstr::mmo_iterator b, MachineInstr::mmo_iterator e) const
constexpr bool isUInt< 8 >(uint64_t x)
unsigned const MachineRegisterInfo * MRI
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
INITIALIZE_PASS_BEGIN(SILoadStoreOptimizer, DEBUG_TYPE,"SI Load / Store Optimizer", false, false) INITIALIZE_PASS_END(SILoadStoreOptimizer
Represent the analysis usage information of a pass.
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
unsigned getSubReg() const
static void moveInstsAfter(MachineBasicBlock::iterator I, ArrayRef< MachineInstr * > InstsToMove)
static void addDefsToList(const MachineInstr &MI, SmallVectorImpl< const MachineOperand * > &Defs)
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...
bool areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA=nullptr) const override
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
void initializeSILoadStoreOptimizerPass(PassRegistry &)
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.
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
unsigned getReg() const
getReg - Returns the register number.
FunctionPass * createSILoadStoreOptimizerPass()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
static bool addToListsIfDependent(MachineInstr &MI, SmallVectorImpl< const MachineOperand * > &Defs, SmallVectorImpl< MachineInstr * > &Insts)
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.