diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt --- a/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt +++ b/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt @@ -6,6 +6,7 @@ RISCVMCAsmInfo.cpp RISCVMCCodeEmitter.cpp RISCVMCExpr.cpp + RISCVMCObjectFileInfo.cpp RISCVMCTargetDesc.cpp RISCVMatInt.cpp RISCVTargetStreamer.cpp diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h new file mode 100644 --- /dev/null +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h @@ -0,0 +1,28 @@ +//===-- RISCVMCAsmInfo.h - RISCV Asm Info ----------------------*- C++ -*--===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains the declaration of the RISCVMCAsmInfo class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H +#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H + +#include "llvm/MC/MCObjectFileInfo.h" + +namespace llvm { +class Triple; + +class RISCVMCObjectFileInfo : public MCObjectFileInfo { +public: + unsigned getTextSectionAlignment() const; +}; + +} // namespace llvm + +#endif diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp @@ -0,0 +1,26 @@ +//===-- RISCVMCAsmInfo.cpp - RISCV Asm properties -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains the declarations of the RISCVMCAsmInfo properties. +// +//===----------------------------------------------------------------------===// + +#include "RISCVMCObjectFileInfo.h" +#include "RISCVMCTargetDesc.h" +#include "llvm/MC/MCContext.h" + +using namespace llvm; + +unsigned RISCVMCObjectFileInfo::getTextSectionAlignment() const { + MCContext &Ctx = getContext(); + const MCSubtargetInfo *STI = Ctx.getSubtargetInfo(); + if (STI->hasFeature(RISCV::FeatureStdExtC)) { + return 2; + } + return 4; +} diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp @@ -15,12 +15,14 @@ #include "RISCVELFStreamer.h" #include "RISCVInstPrinter.h" #include "RISCVMCAsmInfo.h" +#include "RISCVMCObjectFileInfo.h" #include "RISCVTargetStreamer.h" #include "TargetInfo/RISCVTargetInfo.h" #include "llvm/ADT/STLExtras.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCInstrAnalysis.h" #include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSubtargetInfo.h" @@ -62,6 +64,14 @@ return MAI; } +static MCObjectFileInfo * +createRISCVMCObjectFileInfo(bool PIC, MCContext &ctx, + bool LargeCodeModel = false) { + MCObjectFileInfo *MOFI = new RISCVMCObjectFileInfo(); + MOFI->InitMCObjectFileInfo(PIC, ctx, LargeCodeModel); + return MOFI; +} + static MCSubtargetInfo *createRISCVMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) { if (CPU.empty()) @@ -141,6 +151,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTargetMC() { for (Target *T : {&getTheRISCV32Target(), &getTheRISCV64Target()}) { TargetRegistry::RegisterMCAsmInfo(*T, createRISCVMCAsmInfo); + TargetRegistry::RegisterMCObjectFileInfo(*T, createRISCVMCObjectFileInfo); TargetRegistry::RegisterMCInstrInfo(*T, createRISCVMCInstrInfo); TargetRegistry::RegisterMCRegInfo(*T, createRISCVMCRegisterInfo); TargetRegistry::RegisterMCAsmBackend(*T, createRISCVAsmBackend); diff --git a/llvm/test/MC/RISCV/align.s b/llvm/test/MC/RISCV/align.s --- a/llvm/test/MC/RISCV/align.s +++ b/llvm/test/MC/RISCV/align.s @@ -33,13 +33,15 @@ # Linker could satisfy alignment by removing NOPs after linker relaxation. # The first R_RISCV_ALIGN come from -# MCELFStreamer::InitSections() emitCodeAlignment(4). +# MCELFStreamer::InitSections() emitCodeAlignment(getTextSectionAligntment()). # C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2 # C-EXT-RELAX-INST: c.nop test: .p2align 2 -# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2 -# C-EXT-RELAX-INST: c.nop +# If the +c extension is enable, the text section will be 2-byte aligned, so +# one c.nop instruction is sufficient. +# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN - 0x2 +# C-EXT-RELAX-INST-NOT: c.nop bne zero, a0, .LBB0_2 mv a0, zero .p2align 3