Debian Codegraph @

llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp

raw
1 //===-- Target.cpp ----------------------------------------------*- 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 #include "../Target.h"
9 #include "AArch64.h"
10 #include "AArch64RegisterInfo.h"
11
12 #if defined(__aarch64__) && defined(__linux__)
13 #include <sys/prctl.h> // For PR_PAC_* constants
14 #ifndef PR_PAC_APIAKEY
15 #define PR_PAC_APIAKEY (1UL << 0)
16 #endif
17 #ifndef PR_PAC_APIBKEY
18 #define PR_PAC_APIBKEY (1UL << 1)
19 #endif
20 #ifndef PR_PAC_APDAKEY
21 #define PR_PAC_APDAKEY (1UL << 2)
22 #endif
23 #ifndef PR_PAC_APDBKEY
24 #define PR_PAC_APDBKEY (1UL << 3)
25 #endif
26 #endif
27
28 #define GET_AVAILABLE_OPCODE_CHECKER
29 #include "AArch64GenInstrInfo.inc"
30
31 namespace llvm {
32 namespace exegesis {
33
34 static unsigned getLoadImmediateOpcode(unsigned RegBitWidth) {
35 switch (RegBitWidth) {
36 case 32:
37 return AArch64::MOVi32imm;
38 case 64:
39 return AArch64::MOVi64imm;
40 }
41 llvm_unreachable("Invalid Value Width");
42 }
43
44 // Generates instruction to load an immediate value into a register.
45 static MCInst loadImmediate(MCRegister Reg, unsigned RegBitWidth,
46 const APInt &Value) {
47 assert(Value.getBitWidth() <= RegBitWidth &&
48 "Value must fit in the Register");
49 return MCInstBuilder(getLoadImmediateOpcode(RegBitWidth))
50 .addReg(Reg)
51 .addImm(Value.getZExtValue());
52 }
53
54 static MCInst loadZPRImmediate(MCRegister Reg, unsigned RegBitWidth,
55 const APInt &Value) {
56 assert(Value.getZExtValue() < (1 << 7) &&
57 "Value must be in the range of the immediate opcode");
58 return MCInstBuilder(AArch64::DUP_ZI_D)
59 .addReg(Reg)
60 .addImm(Value.getZExtValue())
61 .addImm(0);
62 }
63
64 static MCInst loadPPRImmediate(MCRegister Reg, unsigned RegBitWidth,
65 const APInt &Value) {
66 // For PPR, we typically use PTRUE instruction to set predicate registers
67 return MCInstBuilder(AArch64::PTRUE_B)
68 .addReg(Reg)
69 .addImm(31); // All lanes true for 16 bits
70 }
71
72 // Generates instructions to load an immediate value into an FPCR register.
73 static std::vector<MCInst>
74 loadFPCRImmediate(MCRegister Reg, unsigned RegBitWidth, const APInt &Value) {
75 MCRegister TempReg = AArch64::X8;
76 MCInst LoadImm = MCInstBuilder(AArch64::MOVi64imm).addReg(TempReg).addImm(0);
77 MCInst MoveToFPCR =
78 MCInstBuilder(AArch64::MSR).addImm(AArch64SysReg::FPCR).addReg(TempReg);
79 return {LoadImm, MoveToFPCR};
80 }
81
82 // Fetch base-instruction to load an FP immediate value into a register.
83 static unsigned getLoadFPImmediateOpcode(unsigned RegBitWidth) {
84 switch (RegBitWidth) {
85 case 16:
86 return AArch64::FMOVH0; // FMOVHi;
87 case 32:
88 return AArch64::FMOVS0; // FMOVSi;
89 case 64:
90 return AArch64::MOVID; // FMOVDi;
91 case 128:
92 return AArch64::MOVIv2d_ns;
93 }
94 llvm_unreachable("Invalid Value Width");
95 }
96
97 // Generates instruction to load an FP immediate value into a register.
98 static MCInst loadFPImmediate(MCRegister Reg, unsigned RegBitWidth,
99 const APInt &Value) {
100 assert(Value.getZExtValue() == 0 && "Expected initialisation value 0");
101 MCInst Instructions =
102 MCInstBuilder(getLoadFPImmediateOpcode(RegBitWidth)).addReg(Reg);
103 if (RegBitWidth >= 64)
104 Instructions.addOperand(MCOperand::createImm(Value.getZExtValue()));
105 return Instructions;
106 }
107
108 #include "AArch64GenExegesis.inc"
109
110 namespace {
111
112 class ExegesisAArch64Target : public ExegesisTarget {
113 public:
114 ExegesisAArch64Target()
115 : ExegesisTarget(AArch64CpuPfmCounters, AArch64_MC::isOpcodeAvailable) {}
116
117 private:
118 std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, MCRegister Reg,
119 const APInt &Value) const override {
120 if (AArch64::GPR32RegClass.contains(Reg))
121 return {loadImmediate(Reg, 32, Value)};
122 if (AArch64::GPR64RegClass.contains(Reg))
123 return {loadImmediate(Reg, 64, Value)};
124 if (AArch64::PPRRegClass.contains(Reg))
125 return {loadPPRImmediate(Reg, 16, Value)};
126 if (AArch64::FPR8RegClass.contains(Reg))
127 return {loadFPImmediate(Reg - AArch64::B0 + AArch64::D0, 64, Value)};
128 if (AArch64::FPR16RegClass.contains(Reg))
129 return {loadFPImmediate(Reg, 16, Value)};
130 if (AArch64::FPR32RegClass.contains(Reg))
131 return {loadFPImmediate(Reg, 32, Value)};
132 if (AArch64::FPR64RegClass.contains(Reg))
133 return {loadFPImmediate(Reg, 64, Value)};
134 if (AArch64::FPR128RegClass.contains(Reg))
135 return {loadFPImmediate(Reg, 128, Value)};
136 if (AArch64::ZPRRegClass.contains(Reg))
137 return {loadZPRImmediate(Reg, 128, Value)};
138 if (Reg == AArch64::FPCR)
139 return {loadFPCRImmediate(Reg, 32, Value)};
140
141 errs() << "setRegTo is not implemented, results will be unreliable\n";
142 return {};
143 }
144
145 bool matchesArch(Triple::ArchType Arch) const override {
146 return Arch == Triple::aarch64 || Arch == Triple::aarch64_be;
147 }
148
149 void addTargetSpecificPasses(PassManagerBase &PM) const override {
150 // Function return is a pseudo-instruction that needs to be expanded
151 PM.add(createAArch64ExpandPseudoPass());
152 }
153 };
154
155 } // namespace
156
157 static ExegesisTarget *getTheExegesisAArch64Target() {
158 static ExegesisAArch64Target Target;
159 return &Target;
160 }
161
162 void InitializeAArch64ExegesisTarget() {
163 ExegesisTarget::registerTarget(getTheExegesisAArch64Target());
164 }
165
166 } // namespace exegesis
167 } // namespace llvm