1//===-- LoongArchSubtarget.cpp - LoongArch Subtarget Information -*- C++ -*--=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the LoongArch specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#include "LoongArchSubtarget.h"
14#include "LoongArchFrameLowering.h"
15#include "MCTargetDesc/LoongArchBaseInfo.h"
16
17using namespace llvm;
18
19#define DEBUG_TYPE "loongarch-subtarget"
20
21#define GET_SUBTARGETINFO_TARGET_DESC
22#define GET_SUBTARGETINFO_CTOR
23#include "LoongArchGenSubtargetInfo.inc"
24
25void LoongArchSubtarget::anchor() {}
26
27LoongArchSubtarget &LoongArchSubtarget::initializeSubtargetDependencies(
28 const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS,
29 StringRef ABIName) {
30 bool Is64Bit = TT.isArch64Bit();
31 if (CPU.empty() || CPU == "generic")
32 CPU = Is64Bit ? "generic-la64" : "generic-la32";
33
34 if (TuneCPU.empty())
35 TuneCPU = CPU;
36
37 ParseSubtargetFeatures(CPU, TuneCPU, FS);
38 initializeProperties(TuneCPU);
39 if (Is64Bit) {
40 GRLenVT = MVT::i64;
41 GRLen = 64;
42 }
43
44 if (HasLA32 == HasLA64)
45 report_fatal_error(reason: "Please use one feature of 32bit and 64bit.");
46
47 if (Is64Bit && HasLA32)
48 report_fatal_error(reason: "Feature 32bit should be used for loongarch32 target.");
49
50 if (!Is64Bit && HasLA64)
51 report_fatal_error(reason: "Feature 64bit should be used for loongarch64 target.");
52
53 TargetABI = LoongArchABI::computeTargetABI(TT, FeatureBits: getFeatureBits(), ABIName);
54
55 return *this;
56}
57
58void LoongArchSubtarget::initializeProperties(StringRef TuneCPU) {
59 // Initialize CPU specific properties. We should add a tablegen feature for
60 // this in the future so we can specify it together with the subtarget
61 // features.
62
63 // TODO: Check TuneCPU and override defaults (that are for LA464) once we
64 // support optimizing for more uarchs.
65
66 // Default to the alignment settings empirically confirmed to perform best
67 // on LA464, with 4-wide instruction fetch and decode stages. These settings
68 // can also be overridden in initializeProperties.
69 //
70 // We default to such higher-than-minimum alignments because we assume that:
71 //
72 // * these settings should benefit most existing uarchs/users,
73 // * future general-purpose LoongArch cores are likely to have issue widths
74 // equal to or wider than 4,
75 // * instruction sequences best for LA464 should not pessimize other future
76 // uarchs, and
77 // * narrower cores would not suffer much (aside from slightly increased
78 // ICache footprint maybe), compared to the gains everywhere else.
79 PrefFunctionAlignment = Align(32);
80 PrefLoopAlignment = Align(16);
81 MaxBytesForAlignment = 16;
82}
83
84LoongArchSubtarget::LoongArchSubtarget(const Triple &TT, StringRef CPU,
85 StringRef TuneCPU, StringRef FS,
86 StringRef ABIName,
87 const TargetMachine &TM)
88 : LoongArchGenSubtargetInfo(TT, CPU, TuneCPU, FS),
89 FrameLowering(
90 initializeSubtargetDependencies(TT, CPU, TuneCPU, FS, ABIName)),
91 InstrInfo(*this), RegInfo(getHwMode()), TLInfo(TM, *this) {}
92