Index: llvm/include/llvm/MC/MCFragment.h =================================================================== --- llvm/include/llvm/MC/MCFragment.h +++ llvm/include/llvm/MC/MCFragment.h @@ -333,6 +333,41 @@ } }; +class MCNeverAlignFragment : public MCFragment { + /// The alignment the end of the next fragment should avoid. + unsigned Alignment; + + /// Flag to indicate that (optimal) NOPs should be emitted instead + /// of using the provided value. The exact interpretation of this flag is + /// target dependent. + bool EmitNops : 1; + + /// Value to use for filling padding bytes. + int64_t Value; + + /// The size of the integer (in bytes) of \p Value. + unsigned ValueSize; + +public: + MCNeverAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize, + MCSection *Sec = nullptr) + : MCFragment(FT_NeverAlign, false, Sec), Alignment(Alignment), + EmitNops(false), Value(Value), ValueSize(ValueSize) {} + + unsigned getAlignment() const { return Alignment; } + + int64_t getValue() const { return Value; } + + unsigned getValueSize() const { return ValueSize; } + + bool hasEmitNops() const { return EmitNops; } + void setEmitNops(bool Value) { EmitNops = Value; } + + static bool classof(const MCFragment *F) { + return F->getKind() == MCFragment::FT_NeverAlign; + } +}; + class MCFillFragment : public MCFragment { uint8_t ValueSize; /// Value to use for filling bytes. Index: llvm/lib/MC/MCAssembler.cpp =================================================================== --- llvm/lib/MC/MCAssembler.cpp +++ llvm/lib/MC/MCAssembler.cpp @@ -346,6 +346,28 @@ return Size; } + case MCFragment::FT_NeverAlign: { + const MCNeverAlignFragment &NAF = cast(F); + const MCFragment *NF = F.getNextNode(); + uint64_t Offset = Layout.getFragmentOffset(&NAF); + size_t NextFragSize = 0; + if (const auto *NextFrag = dyn_cast(NF)) { + NextFragSize = NextFrag->getContents().size(); + } else if (const auto *NextFrag = dyn_cast(NF)) { + NextFragSize = NextFrag->getContents().size(); + } else { + // Didn't find the expected fragment after the current one. + return 0; + } + // Check if the next fragment ends at the alignment we want to avoid. + if ((Offset + NextFragSize) % NAF.getAlignment() == 0) { + // Avoid this alignment by introducing minimum nop. + assert(getBackend().getMinimumNopSize() != NAF.getAlignment()); + return getBackend().getMinimumNopSize(); + } + return 0; + } + case MCFragment::FT_Org: { const MCOrgFragment &OF = cast(F); MCValue Value; Index: llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp =================================================================== --- llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -1128,6 +1128,7 @@ bool parseDirectiveArch(); bool parseDirectiveNops(SMLoc L); bool parseDirectiveEven(SMLoc L); + bool parseDirectiveAvoidEndAlign(SMLoc L); bool ParseDirectiveCode(StringRef IDVal, SMLoc L); /// CodeView FPO data directives. @@ -4631,6 +4632,8 @@ return false; } else if (IDVal == ".nops") return parseDirectiveNops(DirectiveID.getLoc()); + else if (IDVal == ".avoid_end_align") + return parseDirectiveAvoidEndAlign(DirectiveID.getLoc()); else if (IDVal == ".even") return parseDirectiveEven(DirectiveID.getLoc()); else if (IDVal == ".cv_fpo_proc") @@ -4726,6 +4729,32 @@ return false; } +/// parseDirectiveAvoidEndAlign +/// ::= .avoid_end_align alignment +bool X86AsmParser::parseDirectiveAvoidEndAlign(SMLoc L) { + int64_t Alignment = 0; + SMLoc AlignmentLoc; + const MCSubtargetInfo STI = getSTI(); + AlignmentLoc = getTok().getLoc(); + if (getParser().checkForValidSection() || + getParser().parseAbsoluteExpression(Alignment)) + return true; + + if (getParser().parseToken(AsmToken::EndOfStatement, + "unexpected token in '.avoid_end_align' directive")) + return true; + + if (Alignment <= 0) { + Error(AlignmentLoc, "'.avoid_end_align' directive with non-positive size"); + return false; + } + + /// Emit NeverAlign + getParser().getStreamer().emitNeverAlignCodeAtEnd(Alignment); + + return false; +} + /// ParseDirectiveCode /// ::= .code16 | .code32 | .code64 bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) { Index: llvm/test/MC/X86/x86_64-directive-avoid_end_align.s =================================================================== --- /dev/null +++ llvm/test/MC/X86/x86_64-directive-avoid_end_align.s @@ -0,0 +1,16 @@ +# RUN: llvm-mc -triple=x86_64 %s -filetype=obj | llvm-objdump -d - | FileCheck %s + +# %bb.0: # %entry +.nops 59 + pushq %rbx + movl %edi, %ebx +.avoid_end_align 64 +# CHECK: 3e: 90 nop + testl %eax, %eax +# CHECK-NEXT: 3f: 85 c0 testl %eax, %eax + je .LBB0_2 +# %bb.1: # %taken + nop +.LBB0_2: # %untaken + popq %rbx + retq