@@ -151,11 +151,10 @@ bool RISCVLoadStoreOpt::tryToPairLdStInst(MachineBasicBlock::iterator &MBBI) {
151151bool RISCVLoadStoreOpt::tryConvertToLdStPair (
152152 MachineBasicBlock::iterator First, MachineBasicBlock::iterator Second) {
153153 unsigned PairOpc;
154- // TODO: Handle the rest from RISCVInstrInfo::isPairableLdStInstOpc.
155154 Align RequiredAlignment;
156155 switch (First->getOpcode ()) {
157156 default :
158- return false ;
157+ llvm_unreachable ( " Unsupported load/store instruction for pairing " ) ;
159158 case RISCV::SW:
160159 PairOpc = RISCV::MIPS_SWP;
161160 RequiredAlignment = Align (8 );
@@ -178,13 +177,11 @@ bool RISCVLoadStoreOpt::tryConvertToLdStPair(
178177 const MachineMemOperand *MMO = *First->memoperands_begin ();
179178 Align MMOAlign = MMO->getAlign ();
180179
181- // Only pair if alignment is exactly RequiredAlignment bytes.
182- if (MMOAlign != RequiredAlignment)
180+ if (MMOAlign < RequiredAlignment)
183181 return false ;
184182
185183 int64_t Offset = First->getOperand (2 ).getImm ();
186- if (!isUInt<7 >(Offset) ||
187- !isAligned (Align (MMO->getSize ().getValue ()), Offset))
184+ if (!isUInt<7 >(Offset))
188185 return false ;
189186
190187 MachineInstrBuilder MIB = BuildMI (
@@ -360,11 +357,20 @@ RISCVLoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
360357 // Kill flags may become invalid when moving stores for pairing.
361358 if (I->getOperand (0 ).isUse ()) {
362359 if (!MergeForward) {
363- // Clear kill flags on store if moving upwards.
364- I->getOperand (0 ).setIsKill (false );
365- Paired->getOperand (0 ).setIsKill (false );
360+ // Check if the Paired store's source register has a kill flag and clear
361+ // it only if there are intermediate uses between I and Paired.
362+ MachineOperand &PairedRegOp = Paired->getOperand (0 );
363+ if (PairedRegOp.isKill ()) {
364+ for (auto It = std::next (I); It != Paired; ++It) {
365+ if (It->readsRegister (PairedRegOp.getReg (), TRI)) {
366+ PairedRegOp.setIsKill (false );
367+ break ;
368+ }
369+ }
370+ }
366371 } else {
367- // Clear kill flags of the first stores register.
372+ // Clear kill flags of the first store's register in the forward
373+ // direction.
368374 Register Reg = I->getOperand (0 ).getReg ();
369375 for (MachineInstr &MI : make_range (std::next (I), std::next (Paired)))
370376 MI.clearRegisterKills (Reg, TRI);
0 commit comments