@@ -247,6 +247,17 @@ static bool supportLoadFromLiteral(const MachineInstr &MI) {
247247 }
248248}
249249
250+ // / Returns \p true if there are no non-debug instructions between \p First and
251+ // / \p Second
252+ static bool areInstructionsConsecutive (const MachineInstr *First,
253+ const MachineInstr *Second) {
254+ auto It = First->getIterator ();
255+ auto EndIt = First->getParent ()->instr_end ();
256+ if (It == EndIt)
257+ return false ;
258+ return next_nodbg (It, EndIt) == Second->getIterator ();
259+ }
260+
250261// / Number of GPR registers tracked by mapRegToGPRIndex()
251262static const unsigned N_GPR_REGS = 31 ;
252263// / Map register number to index from 0-30.
@@ -415,7 +426,7 @@ static void handleADRP(const MachineInstr &MI, AArch64FunctionInfo &AFI,
415426 ++NumADRPToLDR;
416427 }
417428 break ;
418- case MCLOH_AdrpAddLdr: {
429+ case MCLOH_AdrpAddLdr:
419430 // There is a possibility that the linker may try to rewrite:
420431 // adrp x0, @sym@PAGE
421432 // add x1, x0, @sym@PAGEOFF
@@ -432,28 +443,24 @@ static void handleADRP(const MachineInstr &MI, AArch64FunctionInfo &AFI,
432443 // FIXME: Implement proper liveness tracking for all registers. For now,
433444 // don't emit the LOH if there are any instructions between the add and
434445 // the ldr.
435- MachineInstr *AddMI = const_cast <MachineInstr *>(Info.MI1 );
436- const MachineInstr *LdrMI = Info.MI0 ;
437- auto AddIt = MachineBasicBlock::iterator (AddMI);
438- auto EndIt = AddMI->getParent ()->end ();
439- if (AddMI->getIterator () == EndIt || LdrMI != &*next_nodbg (AddIt, EndIt))
446+ if (!areInstructionsConsecutive (Info.MI1 , Info.MI0 ))
440447 break ;
441-
442448 LLVM_DEBUG (dbgs () << " Adding MCLOH_AdrpAddLdr:\n "
443449 << ' \t ' << MI << ' \t ' << *Info.MI1 << ' \t '
444450 << *Info.MI0 );
445451 AFI.addLOHDirective (MCLOH_AdrpAddLdr, {&MI, Info.MI1 , Info.MI0 });
446452 ++NumADDToLDR;
447453 break ;
448- }
449454 case MCLOH_AdrpAddStr:
450- if (Info.MI1 != nullptr ) {
451- LLVM_DEBUG (dbgs () << " Adding MCLOH_AdrpAddStr:\n "
452- << ' \t ' << MI << ' \t ' << *Info.MI1 << ' \t '
453- << *Info.MI0 );
454- AFI.addLOHDirective (MCLOH_AdrpAddStr, {&MI, Info.MI1 , Info.MI0 });
455- ++NumADDToSTR;
456- }
455+ if (!Info.MI1 )
456+ break ;
457+ if (!areInstructionsConsecutive (Info.MI1 , Info.MI0 ))
458+ break ;
459+ LLVM_DEBUG (dbgs () << " Adding MCLOH_AdrpAddStr:\n "
460+ << ' \t ' << MI << ' \t ' << *Info.MI1 << ' \t '
461+ << *Info.MI0 );
462+ AFI.addLOHDirective (MCLOH_AdrpAddStr, {&MI, Info.MI1 , Info.MI0 });
463+ ++NumADDToSTR;
457464 break ;
458465 case MCLOH_AdrpLdrGotLdr:
459466 LLVM_DEBUG (dbgs () << " Adding MCLOH_AdrpLdrGotLdr:\n "
0 commit comments