diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -42,6 +42,22 @@ STATISTIC(NumTailCalls, "Number of tail calls"); +static cl::opt ExperimentalPrefLoopAlignment( + "riscv-experimental-pref-loop-alignment", cl::init(0), + cl::desc( + "Sets the preferable loop alignment for experiments (as log2 bytes)" + "(the last riscv-experimental-pref-loop-alignment bits" + " of the loop header PC will be 0)."), + cl::Hidden); + +static cl::opt ExperimentalPrefFunctionAlignment( + "riscv-experimental-pref-function-alignment", cl::init(0), + cl::desc( + "Sets the preferable function alignment for experiments (as log2 bytes)" + "(the last riscv-experimental-pref-loop-alignment bits" + " of the loop header PC will be 0)."), + cl::Hidden); + RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM, const RISCVSubtarget &STI) : TargetLowering(TM), Subtarget(STI) { @@ -835,7 +851,9 @@ // Function alignments. const Align FunctionAlignment(Subtarget.hasStdExtC() ? 2 : 4); setMinFunctionAlignment(FunctionAlignment); - setPrefFunctionAlignment(FunctionAlignment); + setPrefFunctionAlignment(std::max( + FunctionAlignment, Align(1 << ExperimentalPrefFunctionAlignment))); + setPrefLoopAlignment(Align(1 << ExperimentalPrefLoopAlignment)); setMinimumJumpTableEntries(5); diff --git a/llvm/test/CodeGen/RISCV/align.ll b/llvm/test/CodeGen/RISCV/align.ll --- a/llvm/test/CodeGen/RISCV/align.ll +++ b/llvm/test/CodeGen/RISCV/align.ll @@ -2,12 +2,41 @@ ; RUN: | FileCheck %s -check-prefix=RV32I ; RUN: llc -mtriple=riscv32 -mattr=+c -verify-machineinstrs < %s \ ; RUN: | FileCheck %s -check-prefix=RV32C +; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \ +; RUN: -riscv-experimental-pref-function-alignment=3 | FileCheck %s \ +; RUN: -check-prefix=ALIGN3 +; RUN: llc -mtriple=riscv32 -mattr=+c -verify-machineinstrs < %s \ +; RUN: -riscv-experimental-pref-function-alignment=3 | FileCheck %s \ +; RUN: -check-prefix=ALIGN3 +; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \ +; RUN: -riscv-experimental-pref-loop-alignment=3 | FileCheck %s \ +; RUN: -check-prefix=LOOPALIGN define void @foo() { ;RV32I: .p2align 2 ;RV32I: foo: ;RV32C: .p2align 1 ;RV32C: foo: +;ALIGN3: .p2align 3 +;ALIGN3: foo: entry: ret void } + +define void @bar(i32 %n) { +;LOOPALIGN-LABEL: bar: +;LOOPALIGN: .p2align 3 +;LOOPALIGN-NEXT: .LBB1_1: +entry: + br label %loop + +loop: + %loop.iv = phi i32 [0, %entry], [%loop.iv.next, %loop] + call void @foo() + %loop.iv.next = add i32 %loop.iv, 1 + %loop.cond = icmp ne i32 %loop.iv.next, %n + br i1 %loop.cond, label %loop, label %exit + +exit: + ret void +}