diff --git a/llvm/include/llvm/MC/MCObjectFileInfo.h b/llvm/include/llvm/MC/MCObjectFileInfo.h --- a/llvm/include/llvm/MC/MCObjectFileInfo.h +++ b/llvm/include/llvm/MC/MCObjectFileInfo.h @@ -237,6 +237,8 @@ std::array Swift5ReflectionSections = {}; + unsigned TextSectionAlignment = 4; + public: void initMCObjectFileInfo(MCContext &MCCtx, bool PIC, bool LargeCodeModel = false); @@ -263,7 +265,11 @@ return CompactUnwindDwarfEHFrameOnly; } - virtual unsigned getTextSectionAlignment() const { return 4; } + void setTextSectionAlignment(unsigned Alignment) { + TextSectionAlignment = Alignment; + } + + unsigned getTextSectionAlignment() const { return TextSectionAlignment; } MCSection *getTextSection() const { return TextSection; } MCSection *getDataSection() const { return DataSection; } MCSection *getBSSSection() const { return BSSSection; } 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,7 +6,6 @@ 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 deleted file mode 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h +++ /dev/null @@ -1,27 +0,0 @@ -//===-- RISCVMCObjectFileInfo.h - RISCV object file 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 RISCVMCObjectFileInfo 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 RISCVMCObjectFileInfo : public MCObjectFileInfo { -public: - unsigned getTextSectionAlignment() const override; -}; - -} // namespace llvm - -#endif diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp deleted file mode 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp +++ /dev/null @@ -1,23 +0,0 @@ -//===-- RISCVMCObjectFileInfo.cpp - RISCV object file 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 RISCVMCObjectFileInfo properties. -// -//===----------------------------------------------------------------------===// - -#include "RISCVMCObjectFileInfo.h" -#include "RISCVMCTargetDesc.h" -#include "llvm/MC/MCContext.h" -#include "llvm/MC/MCSubtargetInfo.h" - -using namespace llvm; - -unsigned RISCVMCObjectFileInfo::getTextSectionAlignment() const { - const MCSubtargetInfo *STI = getContext().getSubtargetInfo(); - return STI->hasFeature(RISCV::FeatureStdExtC) ? 2 : 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,13 +15,13 @@ #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/MCAsmBackend.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCCodeEmitter.h" +#include "llvm/MC/MCContext.h" #include "llvm/MC/MCInstrAnalysis.h" #include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCObjectFileInfo.h" @@ -70,8 +70,10 @@ static MCObjectFileInfo * createRISCVMCObjectFileInfo(MCContext &Ctx, bool PIC, bool LargeCodeModel = false) { - MCObjectFileInfo *MOFI = new RISCVMCObjectFileInfo(); + MCObjectFileInfo *MOFI = new MCObjectFileInfo(); MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel); + const MCSubtargetInfo *STI = MOFI->getContext().getSubtargetInfo(); + MOFI->setTextSectionAlignment(STI->hasFeature(RISCV::FeatureStdExtC) ? 2 : 4); return MOFI; } diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp --- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp +++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp @@ -22,6 +22,8 @@ ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); + setTextSectionAlignment( + Ctx.getSubtargetInfo()->hasFeature(RISCV::FeatureStdExtC) ? 2 : 4); } // A address must be loaded from a small section if its size is less than the diff --git a/llvm/test/CodeGen/RISCV/unnecessary-code-align.ll b/llvm/test/CodeGen/RISCV/unnecessary-code-align.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/RISCV/unnecessary-code-align.ll @@ -0,0 +1,15 @@ +; RUN: llc -mtriple=riscv64 -filetype=obj -mattr=+c,+relax %s -o - \ +; RUN: | llvm-readobj -r - | FileCheck %s + +; CHECK: Relocations [ +; CHECK-NOT: Section (3) .rela.text { +; CHECK-NOT: 0x0 R_RISCV_ALIGN - 0x2 +; CHECK-NOT: } +; CHECK: ] +define i32 @test() #0 { +entry: + ret i32 1 +} + +attributes #0 = { nounwind } +