Skip to content

Commit 3fe6d27

Browse files
committed
X86AsmBackend: Simplify isRightAfterData for the auto-pad feature
1 parent ffc5385 commit 3fe6d27

File tree

2 files changed

+7
-35
lines changed

2 files changed

+7
-35
lines changed

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class LLVM_ABI MCStreamer {
438438
assert(!CurFrag || CurFrag->getKind() == MCFragment::FT_Data);
439439
return CurFrag;
440440
}
441-
size_t getCurFragOffset() const { return getCurrentFragment()->Offset; }
441+
size_t getCurFragSize() const { return getCurrentFragment()->getFixedSize(); }
442442
/// Save the current and previous section on the section stack.
443443
void pushSection() {
444444
SectionStack.push_back(

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -388,36 +388,6 @@ static bool mayHaveInterruptDelaySlot(unsigned InstOpcode) {
388388
return false;
389389
}
390390

391-
/// Check if the instruction to be emitted is right after any data.
392-
static bool
393-
isRightAfterData(MCFragment *CurrentFragment,
394-
const std::pair<MCFragment *, size_t> &PrevInstPosition) {
395-
MCFragment *F = CurrentFragment;
396-
// Since data is always emitted into a DataFragment, our check strategy is
397-
// simple here.
398-
// - If the fragment is a DataFragment
399-
// - If it's empty (section start or data after align), return false.
400-
// - If it's not the fragment where the previous instruction is,
401-
// returns true.
402-
// - If it's the fragment holding the previous instruction but its
403-
// size changed since the previous instruction was emitted into
404-
// it, returns true.
405-
// - Otherwise returns false.
406-
// - If the fragment is not a DataFragment, returns false.
407-
if (F->getKind() == MCFragment::FT_Data)
408-
return F->getFixedSize() && (F != PrevInstPosition.first ||
409-
F->getFixedSize() != PrevInstPosition.second);
410-
411-
return false;
412-
}
413-
414-
/// \returns the fragment size if it has instructions, otherwise returns 0.
415-
static size_t getSizeForInstFragment(const MCFragment *F) {
416-
if (!F || !F->hasInstructions())
417-
return 0;
418-
return F->getSize();
419-
}
420-
421391
/// Return true if we can insert NOP or prefixes automatically before the
422392
/// the instruction to be emitted.
423393
bool X86AsmBackend::canPadInst(const MCInst &Inst, MCObjectStreamer &OS) const {
@@ -441,9 +411,11 @@ bool X86AsmBackend::canPadInst(const MCInst &Inst, MCObjectStreamer &OS) const {
441411
// semantic.
442412
return false;
443413

444-
if (isRightAfterData(OS.getCurrentFragment(), PrevInstPosition))
445-
// If this instruction follows any data, there is no clear
446-
// instruction boundary, inserting a nop/prefix would change semantic.
414+
// If this instruction follows any data, there is no clear instruction
415+
// boundary, inserting a nop/prefix would change semantic.
416+
auto Offset = OS.getCurFragSize();
417+
if (Offset && (OS.getCurrentFragment() != PrevInstPosition.first ||
418+
Offset != PrevInstPosition.second))
447419
return false;
448420

449421
return true;
@@ -552,7 +524,7 @@ void X86AsmBackend::emitInstructionEnd(MCObjectStreamer &OS,
552524
// Update PrevInstOpcode here, canPadInst() reads that.
553525
MCFragment *CF = OS.getCurrentFragment();
554526
PrevInstOpcode = Inst.getOpcode();
555-
PrevInstPosition = std::make_pair(CF, getSizeForInstFragment(CF));
527+
PrevInstPosition = std::make_pair(CF, OS.getCurFragSize());
556528

557529
if (!canPadBranches(OS))
558530
return;

0 commit comments

Comments
 (0)