Index: lib/Target/AMDGPU/SILoadStoreOptimizer.cpp =================================================================== --- lib/Target/AMDGPU/SILoadStoreOptimizer.cpp +++ lib/Target/AMDGPU/SILoadStoreOptimizer.cpp @@ -141,10 +141,12 @@ } } -static void addDefsToList(const MachineInstr &MI, - SmallVectorImpl &Defs) { +static void addDefsToList(const MachineInstr &MI, DenseSet &Defs) { + // XXX: Should this be looking for implicit defs? for (const MachineOperand &Def : MI.defs()) { - Defs.push_back(&Def); + bool Inserted = Defs.insert(Def.getReg()).second; + (void)Inserted; + assert(Inserted); } } @@ -152,25 +154,25 @@ MachineBasicBlock::iterator B, const SIInstrInfo *TII, AliasAnalysis * AA) { - return (TII->areMemAccessesTriviallyDisjoint(*A, *B, AA) || - // RAW or WAR - cannot reorder - // WAW - cannot reorder - // RAR - safe to reorder - !(A->mayStore() || B->mayStore())); + // RAW or WAR - cannot reorder + // WAW - cannot reorder + // RAR - safe to reorder + return !(A->mayStore() || B->mayStore()) || + TII->areMemAccessesTriviallyDisjoint(*A, *B, AA); } // Add MI and its defs to the lists if MI reads one of the defs that are // already in the list. Returns true in that case. static bool addToListsIfDependent(MachineInstr &MI, - SmallVectorImpl &Defs, + DenseSet &Defs, SmallVectorImpl &Insts) { - for (const MachineOperand *Def : Defs) { - bool ReadDef = MI.readsVirtualRegister(Def->getReg()); - // If ReadDef is true, then there is a use of Def between I - // and the instruction that I will potentially be merged with. We - // will need to move this instruction after the merged instructions. - if (ReadDef) { + for (MachineOperand &Use : MI.operands()) { + // If one of the defs is read, then there is a use of Def between I and the + // instruction that I will potentially be merged with. We will need to move + // this instruction after the merged instructions. + + if (Use.isReg() && Use.readsReg() && Defs.count(Use.getReg())) { Insts.push_back(&MI); addDefsToList(MI, Defs); return true; @@ -248,8 +250,10 @@ return false; } + bool SILoadStoreOptimizer::findMatchingDSInst(CombineInfo &CI) { - MachineBasicBlock::iterator E = CI.I->getParent()->end(); + MachineBasicBlock *MBB = CI.I->getParent(); + MachineBasicBlock::iterator E = MBB->end(); MachineBasicBlock::iterator MBBI = CI.I; int AddrIdx = AMDGPU::getNamedOperandIdx(CI.I->getOpcode(), @@ -258,12 +262,13 @@ // We only ever merge operations with the same base address register, so don't // bother scanning forward if there are no other uses. - if (MRI->hasOneNonDBGUse(AddrReg0.getReg())) + if (TargetRegisterInfo::isPhysicalRegister(AddrReg0.getReg()) || + MRI->hasOneNonDBGUse(AddrReg0.getReg())) return false; ++MBBI; - SmallVector DefsToMove; + DenseSet DefsToMove; addDefsToList(*CI.I, DefsToMove); for ( ; MBBI != E; ++MBBI) {