@@ -127,7 +127,6 @@ class X86AsmBackend : public MCAsmBackend {
127
127
unsigned PrevInstOpcode = 0 ;
128
128
MCBoundaryAlignFragment *PendingBA = nullptr ;
129
129
std::pair<MCFragment *, size_t > PrevInstPosition;
130
- bool IsRightAfterData = false ;
131
130
132
131
uint8_t determinePaddingPrefix (const MCInst &Inst) const ;
133
132
bool isMacroFused (const MCInst &Cmp, const MCInst &Jcc) const ;
@@ -156,10 +155,13 @@ class X86AsmBackend : public MCAsmBackend {
156
155
AlignBranchType = X86AlignBranchKindLoc;
157
156
if (X86PadMaxPrefixSize.getNumOccurrences ())
158
157
TargetPrefixMax = X86PadMaxPrefixSize;
158
+
159
+ AllowAutoPadding =
160
+ AlignBoundary != Align (1 ) && AlignBranchType != X86::AlignBranchNone;
161
+ AllowEnhancedRelaxation =
162
+ AllowAutoPadding && TargetPrefixMax != 0 && X86PadForBranchAlign;
159
163
}
160
164
161
- bool allowAutoPadding () const override ;
162
- bool allowEnhancedRelaxation () const override ;
163
165
void emitInstructionBegin (MCObjectStreamer &OS, const MCInst &Inst,
164
166
const MCSubtargetInfo &STI);
165
167
void emitInstructionEnd (MCObjectStreamer &OS, const MCInst &Inst);
@@ -365,14 +367,6 @@ static bool hasVariantSymbol(const MCInst &MI) {
365
367
return false ;
366
368
}
367
369
368
- bool X86AsmBackend::allowAutoPadding () const {
369
- return (AlignBoundary != Align (1 ) && AlignBranchType != X86::AlignBranchNone);
370
- }
371
-
372
- bool X86AsmBackend::allowEnhancedRelaxation () const {
373
- return allowAutoPadding () && TargetPrefixMax != 0 && X86PadForBranchAlign;
374
- }
375
-
376
370
// / X86 has certain instructions which enable interrupts exactly one
377
371
// / instruction *after* the instruction which stores to SS. Return true if the
378
372
// / given instruction may have such an interrupt delay slot.
@@ -447,7 +441,7 @@ bool X86AsmBackend::canPadInst(const MCInst &Inst, MCObjectStreamer &OS) const {
447
441
// semantic.
448
442
return false ;
449
443
450
- if (IsRightAfterData )
444
+ if (isRightAfterData (OS. getCurrentFragment (), PrevInstPosition) )
451
445
// If this instruction follows any data, there is no clear
452
446
// instruction boundary, inserting a nop/prefix would change semantic.
453
447
return false ;
@@ -456,6 +450,8 @@ bool X86AsmBackend::canPadInst(const MCInst &Inst, MCObjectStreamer &OS) const {
456
450
}
457
451
458
452
bool X86AsmBackend::canPadBranches (MCObjectStreamer &OS) const {
453
+ if (!OS.getAllowAutoPadding ())
454
+ return false ;
459
455
assert (allowAutoPadding () && " incorrect initialization!" );
460
456
461
457
// We only pad in text section.
@@ -482,22 +478,28 @@ bool X86AsmBackend::needAlign(const MCInst &Inst) const {
482
478
(AlignBranchType & X86::AlignBranchIndirect));
483
479
}
484
480
481
+ void X86_MC::emitInstruction (MCObjectStreamer &S, const MCInst &Inst,
482
+ const MCSubtargetInfo &STI) {
483
+ bool AutoPadding = S.getAllowAutoPadding ();
484
+ if (LLVM_LIKELY (!AutoPadding && !X86PadForAlign)) {
485
+ S.MCObjectStreamer ::emitInstruction (Inst, STI);
486
+ return ;
487
+ }
488
+
489
+ auto &Backend = static_cast <X86AsmBackend &>(S.getAssembler ().getBackend ());
490
+ Backend.emitInstructionBegin (S, Inst, STI);
491
+ S.MCObjectStreamer ::emitInstruction (Inst, STI);
492
+ Backend.emitInstructionEnd (S, Inst);
493
+ }
494
+
485
495
// / Insert BoundaryAlignFragment before instructions to align branches.
486
496
void X86AsmBackend::emitInstructionBegin (MCObjectStreamer &OS,
487
497
const MCInst &Inst, const MCSubtargetInfo &STI) {
488
- // Used by canPadInst. Done here, because in emitInstructionEnd, the current
489
- // fragment will have changed.
490
- IsRightAfterData =
491
- isRightAfterData (OS.getCurrentFragment (), PrevInstPosition);
492
- bool CanPadInst = false ;
493
- bool AutoPadding = OS.getAllowAutoPadding ();
494
- if (LLVM_UNLIKELY (AutoPadding || X86PadForAlign)) {
495
- CanPadInst = canPadInst (Inst, OS);
496
- if (CanPadInst)
497
- OS.getCurrentFragment ()->setAllowAutoPadding (true );
498
- }
498
+ bool CanPadInst = canPadInst (Inst, OS);
499
+ if (CanPadInst)
500
+ OS.getCurrentFragment ()->setAllowAutoPadding (true );
499
501
500
- if (!AutoPadding || ! canPadBranches (OS))
502
+ if (!canPadBranches (OS))
501
503
return ;
502
504
503
505
// NB: PrevInst only valid if canPadBranches is true.
@@ -552,7 +554,7 @@ void X86AsmBackend::emitInstructionEnd(MCObjectStreamer &OS,
552
554
PrevInstOpcode = Inst.getOpcode ();
553
555
PrevInstPosition = std::make_pair (CF, getSizeForInstFragment (CF));
554
556
555
- if (!OS. getAllowAutoPadding () || ! canPadBranches (OS))
557
+ if (!canPadBranches (OS))
556
558
return ;
557
559
558
560
// PrevInst is only needed if canPadBranches. Copying an MCInst isn't cheap.
@@ -572,8 +574,7 @@ void X86AsmBackend::emitInstructionEnd(MCObjectStreamer &OS,
572
574
OS.newFragment ();
573
575
574
576
// Update the maximum alignment on the current section if necessary.
575
- MCSection *Sec = OS.getCurrentSectionOnly ();
576
- Sec->ensureMinAlignment (AlignBoundary);
577
+ CF->getParent ()->ensureMinAlignment (AlignBoundary);
577
578
}
578
579
579
580
std::optional<MCFixupKind> X86AsmBackend::getFixupKind (StringRef Name) const {
@@ -1544,14 +1545,6 @@ class X86ELFStreamer : public MCELFStreamer {
1544
1545
};
1545
1546
} // end anonymous namespace
1546
1547
1547
- void X86_MC::emitInstruction (MCObjectStreamer &S, const MCInst &Inst,
1548
- const MCSubtargetInfo &STI) {
1549
- auto &Backend = static_cast <X86AsmBackend &>(S.getAssembler ().getBackend ());
1550
- Backend.emitInstructionBegin (S, Inst, STI);
1551
- S.MCObjectStreamer ::emitInstruction (Inst, STI);
1552
- Backend.emitInstructionEnd (S, Inst);
1553
- }
1554
-
1555
1548
void X86ELFStreamer::emitInstruction (const MCInst &Inst,
1556
1549
const MCSubtargetInfo &STI) {
1557
1550
X86_MC::emitInstruction (*this , Inst, STI);
0 commit comments