@@ -391,9 +391,9 @@ MachineInstrBuilder X86FrameLowering::BuildStackAdjustment(
391
391
return MI;
392
392
}
393
393
394
- int X86FrameLowering::mergeSPUpdates (MachineBasicBlock &MBB,
395
- MachineBasicBlock::iterator &MBBI,
396
- bool doMergeWithPrevious) const {
394
+ int64_t X86FrameLowering::mergeSPUpdates (MachineBasicBlock &MBB,
395
+ MachineBasicBlock::iterator &MBBI,
396
+ bool doMergeWithPrevious) const {
397
397
if ((doMergeWithPrevious && MBBI == MBB.begin ()) ||
398
398
(!doMergeWithPrevious && MBBI == MBB.end ()))
399
399
return 0 ;
@@ -415,27 +415,38 @@ int X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB,
415
415
if (doMergeWithPrevious && PI != MBB.begin () && PI->isCFIInstruction ())
416
416
PI = std::prev (PI);
417
417
418
- unsigned Opc = PI->getOpcode ();
419
- int Offset = 0 ;
420
-
421
- if ((Opc == X86::ADD64ri32 || Opc == X86::ADD32ri) &&
422
- PI->getOperand (0 ).getReg () == StackPtr) {
423
- assert (PI->getOperand (1 ).getReg () == StackPtr);
424
- Offset = PI->getOperand (2 ).getImm ();
425
- } else if ((Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
426
- PI->getOperand (0 ).getReg () == StackPtr &&
427
- PI->getOperand (1 ).getReg () == StackPtr &&
428
- PI->getOperand (2 ).getImm () == 1 &&
429
- PI->getOperand (3 ).getReg () == X86::NoRegister &&
430
- PI->getOperand (5 ).getReg () == X86::NoRegister) {
431
- // For LEAs we have: def = lea SP, FI, noreg, Offset, noreg.
432
- Offset = PI->getOperand (4 ).getImm ();
433
- } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB32ri) &&
434
- PI->getOperand (0 ).getReg () == StackPtr) {
435
- assert (PI->getOperand (1 ).getReg () == StackPtr);
436
- Offset = -PI->getOperand (2 ).getImm ();
437
- } else
438
- return 0 ;
418
+ int64_t Offset = 0 ;
419
+ for (;;) {
420
+ unsigned Opc = PI->getOpcode ();
421
+
422
+ if ((Opc == X86::ADD64ri32 || Opc == X86::ADD32ri) &&
423
+ PI->getOperand (0 ).getReg () == StackPtr) {
424
+ assert (PI->getOperand (1 ).getReg () == StackPtr);
425
+ Offset = PI->getOperand (2 ).getImm ();
426
+ } else if ((Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
427
+ PI->getOperand (0 ).getReg () == StackPtr &&
428
+ PI->getOperand (1 ).getReg () == StackPtr &&
429
+ PI->getOperand (2 ).getImm () == 1 &&
430
+ PI->getOperand (3 ).getReg () == X86::NoRegister &&
431
+ PI->getOperand (5 ).getReg () == X86::NoRegister) {
432
+ // For LEAs we have: def = lea SP, FI, noreg, Offset, noreg.
433
+ Offset = PI->getOperand (4 ).getImm ();
434
+ } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB32ri) &&
435
+ PI->getOperand (0 ).getReg () == StackPtr) {
436
+ assert (PI->getOperand (1 ).getReg () == StackPtr);
437
+ Offset = -PI->getOperand (2 ).getImm ();
438
+ } else
439
+ return 0 ;
440
+
441
+ constexpr int64_t Chunk = (1LL << 31 ) - 1 ;
442
+ if (Offset < Chunk)
443
+ break ;
444
+
445
+ if (doMergeWithPrevious ? (PI == MBB.begin ()) : (PI == MBB.end ()))
446
+ return 0 ;
447
+
448
+ PI = doMergeWithPrevious ? std::prev (PI) : std::next (PI);
449
+ }
439
450
440
451
PI = MBB.erase (PI);
441
452
if (PI != MBB.end () && PI->isCFIInstruction ()) {
@@ -2457,7 +2468,7 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
2457
2468
if (HasFP) {
2458
2469
if (X86FI->hasSwiftAsyncContext ()) {
2459
2470
// Discard the context.
2460
- int Offset = 16 + mergeSPUpdates (MBB, MBBI, true );
2471
+ int64_t Offset = 16 + mergeSPUpdates (MBB, MBBI, true );
2461
2472
emitSPUpdate (MBB, MBBI, DL, Offset, /* InEpilogue*/ true );
2462
2473
}
2463
2474
// Pop EBP.
@@ -2618,7 +2629,7 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
2618
2629
2619
2630
if (Terminator == MBB.end () || !isTailCallOpcode (Terminator->getOpcode ())) {
2620
2631
// Add the return addr area delta back since we are not tail calling.
2621
- int Offset = -1 * X86FI->getTCReturnAddrDelta ();
2632
+ int64_t Offset = -1 * X86FI->getTCReturnAddrDelta ();
2622
2633
assert (Offset >= 0 && " TCDelta should never be positive" );
2623
2634
if (Offset) {
2624
2635
// Check for possible merge with preceding ADD instruction.
0 commit comments