xref: /llvm-project/llvm/lib/Target/LoongArch/LoongArchSubtarget.cpp (revision cab606c30661a746b2513a8330e0c8eca771913e)
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 
17 using 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 
25 static cl::opt<bool> UseAA("loongarch-use-aa", cl::init(true),
26                            cl::desc("Enable the use of AA during codegen."));
27 
28 void LoongArchSubtarget::anchor() {}
29 
30 // Enable use of alias analysis during code generation (during MI scheduling,
31 // DAGCombine, etc.).
32 bool LoongArchSubtarget::useAA() const { return UseAA; }
33 
34 LoongArchSubtarget &LoongArchSubtarget::initializeSubtargetDependencies(
35     const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS,
36     StringRef ABIName) {
37   bool Is64Bit = TT.isArch64Bit();
38   if (CPU.empty() || CPU == "generic")
39     CPU = Is64Bit ? "generic-la64" : "generic-la32";
40 
41   if (TuneCPU.empty())
42     TuneCPU = CPU;
43 
44   ParseSubtargetFeatures(CPU, TuneCPU, FS);
45   initializeProperties(TuneCPU);
46   if (Is64Bit) {
47     GRLenVT = MVT::i64;
48     GRLen = 64;
49   }
50 
51   if (HasLA32 == HasLA64)
52     report_fatal_error("Please use one feature of 32bit and 64bit.");
53 
54   if (Is64Bit && HasLA32)
55     report_fatal_error("Feature 32bit should be used for loongarch32 target.");
56 
57   if (!Is64Bit && HasLA64)
58     report_fatal_error("Feature 64bit should be used for loongarch64 target.");
59 
60   TargetABI = LoongArchABI::computeTargetABI(TT, getFeatureBits(), ABIName);
61 
62   return *this;
63 }
64 
65 void LoongArchSubtarget::initializeProperties(StringRef TuneCPU) {
66   // Initialize CPU specific properties. We should add a tablegen feature for
67   // this in the future so we can specify it together with the subtarget
68   // features.
69 
70   // TODO: Check TuneCPU and override defaults (that are for LA464) once we
71   // support optimizing for more uarchs.
72 
73   // Default to the alignment settings empirically confirmed to perform best
74   // on LA464, with 4-wide instruction fetch and decode stages. These settings
75   // can also be overridden in initializeProperties.
76   //
77   // We default to such higher-than-minimum alignments because we assume that:
78   //
79   // * these settings should benefit most existing uarchs/users,
80   // * future general-purpose LoongArch cores are likely to have issue widths
81   //   equal to or wider than 4,
82   // * instruction sequences best for LA464 should not pessimize other future
83   //   uarchs, and
84   // * narrower cores would not suffer much (aside from slightly increased
85   //   ICache footprint maybe), compared to the gains everywhere else.
86   PrefFunctionAlignment = Align(32);
87   PrefLoopAlignment = Align(16);
88   MaxBytesForAlignment = 16;
89 }
90 
91 LoongArchSubtarget::LoongArchSubtarget(const Triple &TT, StringRef CPU,
92                                        StringRef TuneCPU, StringRef FS,
93                                        StringRef ABIName,
94                                        const TargetMachine &TM)
95     : LoongArchGenSubtargetInfo(TT, CPU, TuneCPU, FS),
96       FrameLowering(
97           initializeSubtargetDependencies(TT, CPU, TuneCPU, FS, ABIName)),
98       InstrInfo(*this), RegInfo(getHwMode()), TLInfo(TM, *this) {}
99