| 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 |
|
| 10 |
#include "../Error.h" |
| 11 |
#include "../ParallelSnippetGenerator.h" |
| 12 |
#include "../SerialSnippetGenerator.h" |
| 13 |
#include "../SnippetGenerator.h" |
| 14 |
#include "MCTargetDesc/X86BaseInfo.h" |
| 15 |
#include "MCTargetDesc/X86MCTargetDesc.h" |
| 16 |
#include "X86.h" |
| 17 |
#include "X86Counter.h" |
| 18 |
#include "X86RegisterInfo.h" |
| 19 |
#include "X86Subtarget.h" |
| 20 |
#include "llvm/ADT/Sequence.h" |
| 21 |
#include "llvm/MC/MCInstBuilder.h" |
| 22 |
#include "llvm/Support/Errc.h" |
| 23 |
#include "llvm/Support/Error.h" |
| 24 |
#include "llvm/Support/FormatVariadic.h" |
| 25 |
#include "llvm/Support/Host.h" |
| 26 |
|
| 27 |
#include <memory> |
| 28 |
#include <string> |
| 29 |
#include <vector> |
| 30 |
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) |
| 31 |
#include <immintrin.h> |
| 32 |
#include <intrin.h> |
| 33 |
#endif |
| 34 |
#if defined(__x86_64__) && defined(_MSC_VER) |
| 35 |
#include <float.h> // For _clearfp in ~X86SavedState(). |
| 36 |
#endif |
| 37 |
|
| 38 |
namespace llvm { |
| 39 |
namespace exegesis { |
| 40 |
|
| 41 |
// If a positive value is specified, we are going to use the LBR in |
| 42 |
// latency-mode. |
| 43 |
// |
| 44 |
// Note: |
| 45 |
// - A small value is preferred, but too low a value could result in |
| 46 |
// throttling. |
| 47 |
// - A prime number is preferred to avoid always skipping certain blocks. |
| 48 |
// |
| 49 |
static cl::opt<unsigned> LbrSamplingPeriod( |
| 50 |
"x86-lbr-sample-period", |
| 51 |
cl::desc("The sample period (nbranches/sample), used for LBR sampling"), |
| 52 |
cl::cat(BenchmarkOptions), cl::init(0)); |
| 53 |
|
| 54 |
static cl::opt<bool> |
| 55 |
DisableUpperSSERegisters("x86-disable-upper-sse-registers", |
| 56 |
cl::desc("Disable XMM8-XMM15 register usage"), |
| 57 |
cl::cat(BenchmarkOptions), cl::init(false)); |
| 58 |
|
| 59 |
// FIXME: Validates that repetition-mode is loop if LBR is requested. |
| 60 |
|
| 61 |
// Returns a non-null reason if we cannot handle the memory references in this |
| 62 |
// instruction. |
| 63 |
static const char *isInvalidMemoryInstr(const Instruction &Instr) { |
| 64 |
switch (Instr.Description.TSFlags & X86II::FormMask) { |
| 65 |
default: |
| 66 |
return "Unknown FormMask value"; |
| 67 |
// These have no memory access. |
| 68 |
case X86II::Pseudo: |
| 69 |
case X86II::RawFrm: |
| 70 |
case X86II::AddCCFrm: |
| 71 |
case X86II::PrefixByte: |
| 72 |
case X86II::MRMDestReg: |
| 73 |
case X86II::MRMSrcReg: |
| 74 |
case X86II::MRMSrcReg4VOp3: |
| 75 |
case X86II::MRMSrcRegOp4: |
| 76 |
case X86II::MRMSrcRegCC: |
| 77 |
case X86II::MRMXrCC: |
| 78 |
case X86II::MRMr0: |
| 79 |
case X86II::MRMXr: |
| 80 |
case X86II::MRM0r: |
| 81 |
case X86II::MRM1r: |
| 82 |
case X86II::MRM2r: |
| 83 |
case X86II::MRM3r: |
| 84 |
case X86II::MRM4r: |
| 85 |
case X86II::MRM5r: |
| 86 |
case X86II::MRM6r: |
| 87 |
case X86II::MRM7r: |
| 88 |
case X86II::MRM0X: |
| 89 |
case X86II::MRM1X: |
| 90 |
case X86II::MRM2X: |
| 91 |
case X86II::MRM3X: |
| 92 |
case X86II::MRM4X: |
| 93 |
case X86II::MRM5X: |
| 94 |
case X86II::MRM6X: |
| 95 |
case X86II::MRM7X: |
| 96 |
case X86II::MRM_C0: |
| 97 |
case X86II::MRM_C1: |
| 98 |
case X86II::MRM_C2: |
| 99 |
case X86II::MRM_C3: |
| 100 |
case X86II::MRM_C4: |
| 101 |
case X86II::MRM_C5: |
| 102 |
case X86II::MRM_C6: |
| 103 |
case X86II::MRM_C7: |
| 104 |
case X86II::MRM_C8: |
| 105 |
case X86II::MRM_C9: |
| 106 |
case X86II::MRM_CA: |
| 107 |
case X86II::MRM_CB: |
| 108 |
case X86II::MRM_CC: |
| 109 |
case X86II::MRM_CD: |
| 110 |
case X86II::MRM_CE: |
| 111 |
case X86II::MRM_CF: |
| 112 |
case X86II::MRM_D0: |
| 113 |
case X86II::MRM_D1: |
| 114 |
case X86II::MRM_D2: |
| 115 |
case X86II::MRM_D3: |
| 116 |
case X86II::MRM_D4: |
| 117 |
case X86II::MRM_D5: |
| 118 |
case X86II::MRM_D6: |
| 119 |
case X86II::MRM_D7: |
| 120 |
case X86II::MRM_D8: |
| 121 |
case X86II::MRM_D9: |
| 122 |
case X86II::MRM_DA: |
| 123 |
case X86II::MRM_DB: |
| 124 |
case X86II::MRM_DC: |
| 125 |
case X86II::MRM_DD: |
| 126 |
case X86II::MRM_DE: |
| 127 |
case X86II::MRM_DF: |
| 128 |
case X86II::MRM_E0: |
| 129 |
case X86II::MRM_E1: |
| 130 |
case X86II::MRM_E2: |
| 131 |
case X86II::MRM_E3: |
| 132 |
case X86II::MRM_E4: |
| 133 |
case X86II::MRM_E5: |
| 134 |
case X86II::MRM_E6: |
| 135 |
case X86II::MRM_E7: |
| 136 |
case X86II::MRM_E8: |
| 137 |
case X86II::MRM_E9: |
| 138 |
case X86II::MRM_EA: |
| 139 |
case X86II::MRM_EB: |
| 140 |
case X86II::MRM_EC: |
| 141 |
case X86II::MRM_ED: |
| 142 |
case X86II::MRM_EE: |
| 143 |
case X86II::MRM_EF: |
| 144 |
case X86II::MRM_F0: |
| 145 |
case X86II::MRM_F1: |
| 146 |
case X86II::MRM_F2: |
| 147 |
case X86II::MRM_F3: |
| 148 |
case X86II::MRM_F4: |
| 149 |
case X86II::MRM_F5: |
| 150 |
case X86II::MRM_F6: |
| 151 |
case X86II::MRM_F7: |
| 152 |
case X86II::MRM_F8: |
| 153 |
case X86II::MRM_F9: |
| 154 |
case X86II::MRM_FA: |
| 155 |
case X86II::MRM_FB: |
| 156 |
case X86II::MRM_FC: |
| 157 |
case X86II::MRM_FD: |
| 158 |
case X86II::MRM_FE: |
| 159 |
case X86II::MRM_FF: |
| 160 |
case X86II::RawFrmImm8: |
| 161 |
return nullptr; |
| 162 |
case X86II::AddRegFrm: |
| 163 |
return (Instr.Description.Opcode == X86::POP16r || |
| 164 |
Instr.Description.Opcode == X86::POP32r || |
| 165 |
Instr.Description.Opcode == X86::PUSH16r || |
| 166 |
Instr.Description.Opcode == X86::PUSH32r) |
| 167 |
? "unsupported opcode: unsupported memory access" |
| 168 |
: nullptr; |
| 169 |
// These access memory and are handled. |
| 170 |
case X86II::MRMDestMem: |
| 171 |
case X86II::MRMSrcMem: |
| 172 |
case X86II::MRMSrcMem4VOp3: |
| 173 |
case X86II::MRMSrcMemOp4: |
| 174 |
case X86II::MRMSrcMemCC: |
| 175 |
case X86II::MRMXmCC: |
| 176 |
case X86II::MRMXm: |
| 177 |
case X86II::MRM0m: |
| 178 |
case X86II::MRM1m: |
| 179 |
case X86II::MRM2m: |
| 180 |
case X86II::MRM3m: |
| 181 |
case X86II::MRM4m: |
| 182 |
case X86II::MRM5m: |
| 183 |
case X86II::MRM6m: |
| 184 |
case X86II::MRM7m: |
| 185 |
return nullptr; |
| 186 |
// These access memory and are not handled yet. |
| 187 |
case X86II::RawFrmImm16: |
| 188 |
case X86II::RawFrmMemOffs: |
| 189 |
case X86II::RawFrmSrc: |
| 190 |
case X86II::RawFrmDst: |
| 191 |
case X86II::RawFrmDstSrc: |
| 192 |
return "unsupported opcode: non uniform memory access"; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// If the opcode is invalid, returns a pointer to a character literal indicating |
| 197 |
// the reason. nullptr indicates a valid opcode. |
| 198 |
static const char *isInvalidOpcode(const Instruction &Instr) { |
| 199 |
const auto OpcodeName = Instr.Name; |
| 200 |
if ((Instr.Description.TSFlags & X86II::FormMask) == X86II::Pseudo) |
| 201 |
return "unsupported opcode: pseudo instruction"; |
| 202 |
if ((OpcodeName.startswith("POP") && !OpcodeName.startswith("POPCNT")) || |
| 203 |
OpcodeName.startswith("PUSH") || OpcodeName.startswith("ADJCALLSTACK") || |
| 204 |
OpcodeName.startswith("LEAVE")) |
| 205 |
return "unsupported opcode: Push/Pop/AdjCallStack/Leave"; |
| 206 |
switch (Instr.Description.Opcode) { |
| 207 |
case X86::LFS16rm: |
| 208 |
case X86::LFS32rm: |
| 209 |
case X86::LFS64rm: |
| 210 |
case X86::LGS16rm: |
| 211 |
case X86::LGS32rm: |
| 212 |
case X86::LGS64rm: |
| 213 |
case X86::LSS16rm: |
| 214 |
case X86::LSS32rm: |
| 215 |
case X86::LSS64rm: |
| 216 |
case X86::SYSENTER: |
| 217 |
case X86::WRFSBASE: |
| 218 |
case X86::WRFSBASE64: |
| 219 |
return "unsupported opcode"; |
| 220 |
default: |
| 221 |
break; |
| 222 |
} |
| 223 |
if (const auto reason = isInvalidMemoryInstr(Instr)) |
| 224 |
return reason; |
| 225 |
// We do not handle instructions with OPERAND_PCREL. |
| 226 |
for (const Operand &Op : Instr.Operands) |
| 227 |
if (Op.isExplicit() && |
| 228 |
Op.getExplicitOperandInfo().OperandType == MCOI::OPERAND_PCREL) |
| 229 |
return "unsupported opcode: PC relative operand"; |
| 230 |
// We do not handle second-form X87 instructions. We only handle first-form |
| 231 |
// ones (_Fp), see comment in X86InstrFPStack.td. |
| 232 |
for (const Operand &Op : Instr.Operands) |
| 233 |
if (Op.isReg() && Op.isExplicit() && |
| 234 |
Op.getExplicitOperandInfo().RegClass == X86::RSTRegClassID) |
| 235 |
return "unsupported second-form X87 instruction"; |
| 236 |
return nullptr; |
| 237 |
} |
| 238 |
|
| 239 |
static unsigned getX86FPFlags(const Instruction &Instr) { |
| 240 |
return Instr.Description.TSFlags & X86II::FPTypeMask; |
| 241 |
} |
| 242 |
|
| 243 |
// Helper to fill a memory operand with a value. |
| 244 |
static void setMemOp(InstructionTemplate &IT, int OpIdx, |
| 245 |
const MCOperand &OpVal) { |
| 246 |
const auto Op = IT.getInstr().Operands[OpIdx]; |
| 247 |
assert(Op.isExplicit() && "invalid memory pattern"); |
| 248 |
IT.getValueFor(Op) = OpVal; |
| 249 |
} |
| 250 |
|
| 251 |
// Common (latency, uops) code for LEA templates. `GetDestReg` takes the |
| 252 |
// addressing base and index registers and returns the LEA destination register. |
| 253 |
static Expected<std::vector<CodeTemplate>> generateLEATemplatesCommon( |
| 254 |
const Instruction &Instr, const BitVector &ForbiddenRegisters, |
| 255 |
const LLVMState &State, const SnippetGenerator::Options &Opts, |
| 256 |
std::function<void(unsigned, unsigned, BitVector &CandidateDestRegs)> |
| 257 |
RestrictDestRegs) { |
| 258 |
assert(Instr.Operands.size() == 6 && "invalid LEA"); |
| 259 |
assert(X86II::getMemoryOperandNo(Instr.Description.TSFlags) == 1 && |
| 260 |
"invalid LEA"); |
| 261 |
|
| 262 |
constexpr const int kDestOp = 0; |
| 263 |
constexpr const int kBaseOp = 1; |
| 264 |
constexpr const int kIndexOp = 3; |
| 265 |
auto PossibleDestRegs = |
| 266 |
Instr.Operands[kDestOp].getRegisterAliasing().sourceBits(); |
| 267 |
remove(PossibleDestRegs, ForbiddenRegisters); |
| 268 |
auto PossibleBaseRegs = |
| 269 |
Instr.Operands[kBaseOp].getRegisterAliasing().sourceBits(); |
| 270 |
remove(PossibleBaseRegs, ForbiddenRegisters); |
| 271 |
auto PossibleIndexRegs = |
| 272 |
Instr.Operands[kIndexOp].getRegisterAliasing().sourceBits(); |
| 273 |
remove(PossibleIndexRegs, ForbiddenRegisters); |
| 274 |
|
| 275 |
const auto &RegInfo = State.getRegInfo(); |
| 276 |
std::vector<CodeTemplate> Result; |
| 277 |
for (const unsigned BaseReg : PossibleBaseRegs.set_bits()) { |
| 278 |
for (const unsigned IndexReg : PossibleIndexRegs.set_bits()) { |
| 279 |
for (int LogScale = 0; LogScale <= 3; ++LogScale) { |
| 280 |
// FIXME: Add an option for controlling how we explore immediates. |
| 281 |
for (const int Disp : {0, 42}) { |
| 282 |
InstructionTemplate IT(&Instr); |
| 283 |
const int64_t Scale = 1ull << LogScale; |
| 284 |
setMemOp(IT, 1, MCOperand::createReg(BaseReg)); |
| 285 |
setMemOp(IT, 2, MCOperand::createImm(Scale)); |
| 286 |
setMemOp(IT, 3, MCOperand::createReg(IndexReg)); |
| 287 |
setMemOp(IT, 4, MCOperand::createImm(Disp)); |
| 288 |
// SegmentReg must be 0 for LEA. |
| 289 |
setMemOp(IT, 5, MCOperand::createReg(0)); |
| 290 |
|
| 291 |
// Output reg candidates are selected by the caller. |
| 292 |
auto PossibleDestRegsNow = PossibleDestRegs; |
| 293 |
RestrictDestRegs(BaseReg, IndexReg, PossibleDestRegsNow); |
| 294 |
assert(PossibleDestRegsNow.set_bits().begin() != |
| 295 |
PossibleDestRegsNow.set_bits().end() && |
| 296 |
"no remaining registers"); |
| 297 |
setMemOp( |
| 298 |
IT, 0, |
| 299 |
MCOperand::createReg(*PossibleDestRegsNow.set_bits().begin())); |
| 300 |
|
| 301 |
CodeTemplate CT; |
| 302 |
CT.Instructions.push_back(std::move(IT)); |
| 303 |
CT.Config = formatv("{3}(%{0}, %{1}, {2})", RegInfo.getName(BaseReg), |
| 304 |
RegInfo.getName(IndexReg), Scale, Disp) |
| 305 |
.str(); |
| 306 |
Result.push_back(std::move(CT)); |
| 307 |
if (Result.size() >= Opts.MaxConfigsPerOpcode) |
| 308 |
return std::move(Result); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
return std::move(Result); |
| 315 |
} |
| 316 |
|
| 317 |
namespace { |
| 318 |
class X86SerialSnippetGenerator : public SerialSnippetGenerator { |
| 319 |
public: |
| 320 |
using SerialSnippetGenerator::SerialSnippetGenerator; |
| 321 |
|
| 322 |
Expected<std::vector<CodeTemplate>> |
| 323 |
generateCodeTemplates(InstructionTemplate Variant, |
| 324 |
const BitVector &ForbiddenRegisters) const override; |
| 325 |
}; |
| 326 |
} // namespace |
| 327 |
|
| 328 |
Expected<std::vector<CodeTemplate>> |
| 329 |
X86SerialSnippetGenerator::generateCodeTemplates( |
| 330 |
InstructionTemplate Variant, const BitVector &ForbiddenRegisters) const { |
| 331 |
const Instruction &Instr = Variant.getInstr(); |
| 332 |
|
| 333 |
if (const auto reason = isInvalidOpcode(Instr)) |
| 334 |
return make_error<Failure>(reason); |
| 335 |
|
| 336 |
// LEA gets special attention. |
| 337 |
const auto Opcode = Instr.Description.getOpcode(); |
| 338 |
if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r) { |
| 339 |
return generateLEATemplatesCommon( |
| 340 |
Instr, ForbiddenRegisters, State, Opts, |
| 341 |
[this](unsigned BaseReg, unsigned IndexReg, |
| 342 |
BitVector &CandidateDestRegs) { |
| 343 |
// We just select a destination register that aliases the base |
| 344 |
// register. |
| 345 |
CandidateDestRegs &= |
| 346 |
State.getRATC().getRegister(BaseReg).aliasedBits(); |
| 347 |
}); |
| 348 |
} |
| 349 |
|
| 350 |
if (Instr.hasMemoryOperands()) |
| 351 |
return make_error<Failure>( |
| 352 |
"unsupported memory operand in latency measurements"); |
| 353 |
|
| 354 |
switch (getX86FPFlags(Instr)) { |
| 355 |
case X86II::NotFP: |
| 356 |
return SerialSnippetGenerator::generateCodeTemplates(Variant, |
| 357 |
ForbiddenRegisters); |
| 358 |
case X86II::ZeroArgFP: |
| 359 |
case X86II::OneArgFP: |
| 360 |
case X86II::SpecialFP: |
| 361 |
case X86II::CompareFP: |
| 362 |
case X86II::CondMovFP: |
| 363 |
return make_error<Failure>("Unsupported x87 Instruction"); |
| 364 |
case X86II::OneArgFPRW: |
| 365 |
case X86II::TwoArgFP: |
| 366 |
// These are instructions like |
| 367 |
// - `ST(0) = fsqrt(ST(0))` (OneArgFPRW) |
| 368 |
// - `ST(0) = ST(0) + ST(i)` (TwoArgFP) |
| 369 |
// They are intrinsically serial and do not modify the state of the stack. |
| 370 |
return generateSelfAliasingCodeTemplates(Variant, ForbiddenRegisters); |
| 371 |
default: |
| 372 |
llvm_unreachable("Unknown FP Type!"); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
namespace { |
| 377 |
class X86ParallelSnippetGenerator : public ParallelSnippetGenerator { |
| 378 |
public: |
| 379 |
using ParallelSnippetGenerator::ParallelSnippetGenerator; |
| 380 |
|
| 381 |
Expected<std::vector<CodeTemplate>> |
| 382 |
generateCodeTemplates(InstructionTemplate Variant, |
| 383 |
const BitVector &ForbiddenRegisters) const override; |
| 384 |
}; |
| 385 |
|
| 386 |
} // namespace |
| 387 |
|
| 388 |
Expected<std::vector<CodeTemplate>> |
| 389 |
X86ParallelSnippetGenerator::generateCodeTemplates( |
| 390 |
InstructionTemplate Variant, const BitVector &ForbiddenRegisters) const { |
| 391 |
const Instruction &Instr = Variant.getInstr(); |
| 392 |
|
| 393 |
if (const auto reason = isInvalidOpcode(Instr)) |
| 394 |
return make_error<Failure>(reason); |
| 395 |
|
| 396 |
// LEA gets special attention. |
| 397 |
const auto Opcode = Instr.Description.getOpcode(); |
| 398 |
if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r) { |
| 399 |
return generateLEATemplatesCommon( |
| 400 |
Instr, ForbiddenRegisters, State, Opts, |
| 401 |
[this](unsigned BaseReg, unsigned IndexReg, |
| 402 |
BitVector &CandidateDestRegs) { |
| 403 |
// Any destination register that is not used for addressing is fine. |
| 404 |
remove(CandidateDestRegs, |
| 405 |
State.getRATC().getRegister(BaseReg).aliasedBits()); |
| 406 |
remove(CandidateDestRegs, |
| 407 |
State.getRATC().getRegister(IndexReg).aliasedBits()); |
| 408 |
}); |
| 409 |
} |
| 410 |
|
| 411 |
switch (getX86FPFlags(Instr)) { |
| 412 |
case X86II::NotFP: |
| 413 |
return ParallelSnippetGenerator::generateCodeTemplates(Variant, |
| 414 |
ForbiddenRegisters); |
| 415 |
case X86II::ZeroArgFP: |
| 416 |
case X86II::OneArgFP: |
| 417 |
case X86II::SpecialFP: |
| 418 |
return make_error<Failure>("Unsupported x87 Instruction"); |
| 419 |
case X86II::OneArgFPRW: |
| 420 |
case X86II::TwoArgFP: |
| 421 |
// These are instructions like |
| 422 |
// - `ST(0) = fsqrt(ST(0))` (OneArgFPRW) |
| 423 |
// - `ST(0) = ST(0) + ST(i)` (TwoArgFP) |
| 424 |
// They are intrinsically serial and do not modify the state of the stack. |
| 425 |
// We generate the same code for latency and uops. |
| 426 |
return generateSelfAliasingCodeTemplates(Variant, ForbiddenRegisters); |
| 427 |
case X86II::CompareFP: |
| 428 |
case X86II::CondMovFP: |
| 429 |
// We can compute uops for any FP instruction that does not grow or shrink |
| 430 |
// the stack (either do not touch the stack or push as much as they pop). |
| 431 |
return generateUnconstrainedCodeTemplates( |
| 432 |
Variant, "instruction does not grow/shrink the FP stack"); |
| 433 |
default: |
| 434 |
llvm_unreachable("Unknown FP Type!"); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
static unsigned getLoadImmediateOpcode(unsigned RegBitWidth) { |
| 439 |
switch (RegBitWidth) { |
| 440 |
case 8: |
| 441 |
return X86::MOV8ri; |
| 442 |
case 16: |
| 443 |
return X86::MOV16ri; |
| 444 |
case 32: |
| 445 |
return X86::MOV32ri; |
| 446 |
case 64: |
| 447 |
return X86::MOV64ri; |
| 448 |
} |
| 449 |
llvm_unreachable("Invalid Value Width"); |
| 450 |
} |
| 451 |
|
| 452 |
// Generates instruction to load an immediate value into a register. |
| 453 |
static MCInst loadImmediate(unsigned Reg, unsigned RegBitWidth, |
| 454 |
const APInt &Value) { |
| 455 |
if (Value.getBitWidth() > RegBitWidth) |
| 456 |
llvm_unreachable("Value must fit in the Register"); |
| 457 |
return MCInstBuilder(getLoadImmediateOpcode(RegBitWidth)) |
| 458 |
.addReg(Reg) |
| 459 |
.addImm(Value.getZExtValue()); |
| 460 |
} |
| 461 |
|
| 462 |
// Allocates scratch memory on the stack. |
| 463 |
static MCInst allocateStackSpace(unsigned Bytes) { |
| 464 |
return MCInstBuilder(X86::SUB64ri8) |
| 465 |
.addReg(X86::RSP) |
| 466 |
.addReg(X86::RSP) |
| 467 |
.addImm(Bytes); |
| 468 |
} |
| 469 |
|
| 470 |
// Fills scratch memory at offset `OffsetBytes` with value `Imm`. |
| 471 |
static MCInst fillStackSpace(unsigned MovOpcode, unsigned OffsetBytes, |
| 472 |
uint64_t Imm) { |
| 473 |
return MCInstBuilder(MovOpcode) |
| 474 |
// Address = ESP |
| 475 |
.addReg(X86::RSP) // BaseReg |
| 476 |
.addImm(1) // ScaleAmt |
| 477 |
.addReg(0) // IndexReg |
| 478 |
.addImm(OffsetBytes) // Disp |
| 479 |
.addReg(0) // Segment |
| 480 |
// Immediate. |
| 481 |
.addImm(Imm); |
| 482 |
} |
| 483 |
|
| 484 |
// Loads scratch memory into register `Reg` using opcode `RMOpcode`. |
| 485 |
static MCInst loadToReg(unsigned Reg, unsigned RMOpcode) { |
| 486 |
return MCInstBuilder(RMOpcode) |
| 487 |
.addReg(Reg) |
| 488 |
// Address = ESP |
| 489 |
.addReg(X86::RSP) // BaseReg |
| 490 |
.addImm(1) // ScaleAmt |
| 491 |
.addReg(0) // IndexReg |
| 492 |
.addImm(0) // Disp |
| 493 |
.addReg(0); // Segment |
| 494 |
} |
| 495 |
|
| 496 |
// Releases scratch memory. |
| 497 |
static MCInst releaseStackSpace(unsigned Bytes) { |
| 498 |
return MCInstBuilder(X86::ADD64ri8) |
| 499 |
.addReg(X86::RSP) |
| 500 |
.addReg(X86::RSP) |
| 501 |
.addImm(Bytes); |
| 502 |
} |
| 503 |
|
| 504 |
// Reserves some space on the stack, fills it with the content of the provided |
| 505 |
// constant and provide methods to load the stack value into a register. |
| 506 |
namespace { |
| 507 |
struct ConstantInliner { |
| 508 |
explicit ConstantInliner(const APInt &Constant) : Constant_(Constant) {} |
| 509 |
|
| 510 |
std::vector<MCInst> loadAndFinalize(unsigned Reg, unsigned RegBitWidth, |
| 511 |
unsigned Opcode); |
| 512 |
|
| 513 |
std::vector<MCInst> loadX87STAndFinalize(unsigned Reg); |
| 514 |
|
| 515 |
std::vector<MCInst> loadX87FPAndFinalize(unsigned Reg); |
| 516 |
|
| 517 |
std::vector<MCInst> popFlagAndFinalize(); |
| 518 |
|
| 519 |
std::vector<MCInst> loadImplicitRegAndFinalize(unsigned Opcode, |
| 520 |
unsigned Value); |
| 521 |
|
| 522 |
private: |
| 523 |
ConstantInliner &add(const MCInst &Inst) { |
| 524 |
Instructions.push_back(Inst); |
| 525 |
return *this; |
| 526 |
} |
| 527 |
|
| 528 |
void initStack(unsigned Bytes); |
| 529 |
|
| 530 |
static constexpr const unsigned kF80Bytes = 10; // 80 bits. |
| 531 |
|
| 532 |
APInt Constant_; |
| 533 |
std::vector<MCInst> Instructions; |
| 534 |
}; |
| 535 |
} // namespace |
| 536 |
|
| 537 |
std::vector<MCInst> ConstantInliner::loadAndFinalize(unsigned Reg, |
| 538 |
unsigned RegBitWidth, |
| 539 |
unsigned Opcode) { |
| 540 |
assert((RegBitWidth & 7) == 0 && "RegBitWidth must be a multiple of 8 bits"); |
| 541 |
initStack(RegBitWidth / 8); |
| 542 |
add(loadToReg(Reg, Opcode)); |
| 543 |
add(releaseStackSpace(RegBitWidth / 8)); |
| 544 |
return std::move(Instructions); |
| 545 |
} |
| 546 |
|
| 547 |
std::vector<MCInst> ConstantInliner::loadX87STAndFinalize(unsigned Reg) { |
| 548 |
initStack(kF80Bytes); |
| 549 |
add(MCInstBuilder(X86::LD_F80m) |
| 550 |
// Address = ESP |
| 551 |
.addReg(X86::RSP) // BaseReg |
| 552 |
.addImm(1) // ScaleAmt |
| 553 |
.addReg(0) // IndexReg |
| 554 |
.addImm(0) // Disp |
| 555 |
.addReg(0)); // Segment |
| 556 |
if (Reg != X86::ST0) |
| 557 |
add(MCInstBuilder(X86::ST_Frr).addReg(Reg)); |
| 558 |
add(releaseStackSpace(kF80Bytes)); |
| 559 |
return std::move(Instructions); |
| 560 |
} |
| 561 |
|
| 562 |
std::vector<MCInst> ConstantInliner::loadX87FPAndFinalize(unsigned Reg) { |
| 563 |
initStack(kF80Bytes); |
| 564 |
add(MCInstBuilder(X86::LD_Fp80m) |
| 565 |
.addReg(Reg) |
| 566 |
// Address = ESP |
| 567 |
.addReg(X86::RSP) // BaseReg |
| 568 |
.addImm(1) // ScaleAmt |
| 569 |
.addReg(0) // IndexReg |
| 570 |
.addImm(0) // Disp |
| 571 |
.addReg(0)); // Segment |
| 572 |
add(releaseStackSpace(kF80Bytes)); |
| 573 |
return std::move(Instructions); |
| 574 |
} |
| 575 |
|
| 576 |
std::vector<MCInst> ConstantInliner::popFlagAndFinalize() { |
| 577 |
initStack(8); |
| 578 |
add(MCInstBuilder(X86::POPF64)); |
| 579 |
return std::move(Instructions); |
| 580 |
} |
| 581 |
|
| 582 |
std::vector<MCInst> |
| 583 |
ConstantInliner::loadImplicitRegAndFinalize(unsigned Opcode, unsigned Value) { |
| 584 |
add(allocateStackSpace(4)); |
| 585 |
add(fillStackSpace(X86::MOV32mi, 0, Value)); // Mask all FP exceptions |
| 586 |
add(MCInstBuilder(Opcode) |
| 587 |
// Address = ESP |
| 588 |
.addReg(X86::RSP) // BaseReg |
| 589 |
.addImm(1) // ScaleAmt |
| 590 |
.addReg(0) // IndexReg |
| 591 |
.addImm(0) // Disp |
| 592 |
.addReg(0)); // Segment |
| 593 |
add(releaseStackSpace(4)); |
| 594 |
return std::move(Instructions); |
| 595 |
} |
| 596 |
|
| 597 |
void ConstantInliner::initStack(unsigned Bytes) { |
| 598 |
assert(Constant_.getBitWidth() <= Bytes * 8 && |
| 599 |
"Value does not have the correct size"); |
| 600 |
const APInt WideConstant = Constant_.getBitWidth() < Bytes * 8 |
| 601 |
? Constant_.sext(Bytes * 8) |
| 602 |
: Constant_; |
| 603 |
add(allocateStackSpace(Bytes)); |
| 604 |
size_t ByteOffset = 0; |
| 605 |
for (; Bytes - ByteOffset >= 4; ByteOffset += 4) |
| 606 |
add(fillStackSpace( |
| 607 |
X86::MOV32mi, ByteOffset, |
| 608 |
WideConstant.extractBits(32, ByteOffset * 8).getZExtValue())); |
| 609 |
if (Bytes - ByteOffset >= 2) { |
| 610 |
add(fillStackSpace( |
| 611 |
X86::MOV16mi, ByteOffset, |
| 612 |
WideConstant.extractBits(16, ByteOffset * 8).getZExtValue())); |
| 613 |
ByteOffset += 2; |
| 614 |
} |
| 615 |
if (Bytes - ByteOffset >= 1) |
| 616 |
add(fillStackSpace( |
| 617 |
X86::MOV8mi, ByteOffset, |
| 618 |
WideConstant.extractBits(8, ByteOffset * 8).getZExtValue())); |
| 619 |
} |
| 620 |
|
| 621 |
#include "X86GenExegesis.inc" |
| 622 |
|
| 623 |
namespace { |
| 624 |
|
| 625 |
class X86SavedState : public ExegesisTarget::SavedState { |
| 626 |
public: |
| 627 |
X86SavedState() { |
| 628 |
#ifdef __x86_64__ |
| 629 |
# if defined(_MSC_VER) |
| 630 |
_fxsave64(FPState); |
| 631 |
Eflags = __readeflags(); |
| 632 |
# elif defined(__GNUC__) |
| 633 |
__builtin_ia32_fxsave64(FPState); |
| 634 |
Eflags = __builtin_ia32_readeflags_u64(); |
| 635 |
# endif |
| 636 |
#else |
| 637 |
llvm_unreachable("X86 exegesis running on non-X86 target"); |
| 638 |
#endif |
| 639 |
} |
| 640 |
|
| 641 |
~X86SavedState() { |
| 642 |
// Restoring the X87 state does not flush pending exceptions, make sure |
| 643 |
// these exceptions are flushed now. |
| 644 |
#ifdef __x86_64__ |
| 645 |
# if defined(_MSC_VER) |
| 646 |
_clearfp(); |
| 647 |
_fxrstor64(FPState); |
| 648 |
__writeeflags(Eflags); |
| 649 |
# elif defined(__GNUC__) |
| 650 |
asm volatile("fwait"); |
| 651 |
__builtin_ia32_fxrstor64(FPState); |
| 652 |
__builtin_ia32_writeeflags_u64(Eflags); |
| 653 |
# endif |
| 654 |
#else |
| 655 |
llvm_unreachable("X86 exegesis running on non-X86 target"); |
| 656 |
#endif |
| 657 |
} |
| 658 |
|
| 659 |
private: |
| 660 |
#ifdef __x86_64__ |
| 661 |
alignas(16) char FPState[512]; |
| 662 |
uint64_t Eflags; |
| 663 |
#endif |
| 664 |
}; |
| 665 |
|
| 666 |
class ExegesisX86Target : public ExegesisTarget { |
| 667 |
public: |
| 668 |
ExegesisX86Target() : ExegesisTarget(X86CpuPfmCounters) {} |
| 669 |
|
| 670 |
Expected<std::unique_ptr<pfm::Counter>> |
| 671 |
createCounter(StringRef CounterName, const LLVMState &State) const override { |
| 672 |
// If LbrSamplingPeriod was provided, then ignore the |
| 673 |
// CounterName because we only have one for LBR. |
| 674 |
if (LbrSamplingPeriod > 0) { |
| 675 |
// Can't use LBR without HAVE_LIBPFM, LIBPFM_HAS_FIELD_CYCLES, or without |
| 676 |
// __linux__ (for now) |
| 677 |
#if defined(HAVE_LIBPFM) && defined(LIBPFM_HAS_FIELD_CYCLES) && \ |
| 678 |
defined(__linux__) |
| 679 |
return std::make_unique<X86LbrCounter>( |
| 680 |
X86LbrPerfEvent(LbrSamplingPeriod)); |
| 681 |
#else |
| 682 |
return llvm::make_error<llvm::StringError>( |
| 683 |
"LBR counter requested without HAVE_LIBPFM, LIBPFM_HAS_FIELD_CYCLES, " |
| 684 |
"or running on Linux.", |
| 685 |
llvm::errc::invalid_argument); |
| 686 |
#endif |
| 687 |
} |
| 688 |
return ExegesisTarget::createCounter(CounterName, State); |
| 689 |
} |
| 690 |
|
| 691 |
private: |
| 692 |
void addTargetSpecificPasses(PassManagerBase &PM) const override; |
| 693 |
|
| 694 |
unsigned getScratchMemoryRegister(const Triple &TT) const override; |
| 695 |
|
| 696 |
unsigned getLoopCounterRegister(const Triple &) const override; |
| 697 |
|
| 698 |
unsigned getMaxMemoryAccessSize() const override { return 64; } |
| 699 |
|
| 700 |
Error randomizeTargetMCOperand(const Instruction &Instr, const Variable &Var, |
| 701 |
MCOperand &AssignedValue, |
| 702 |
const BitVector &ForbiddenRegs) const override; |
| 703 |
|
| 704 |
void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg, |
| 705 |
unsigned Offset) const override; |
| 706 |
|
| 707 |
void decrementLoopCounterAndJump(MachineBasicBlock &MBB, |
| 708 |
MachineBasicBlock &TargetMBB, |
| 709 |
const MCInstrInfo &MII) const override; |
| 710 |
|
| 711 |
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg, |
| 712 |
const APInt &Value) const override; |
| 713 |
|
| 714 |
ArrayRef<unsigned> getUnavailableRegisters() const override { |
| 715 |
if (DisableUpperSSERegisters) |
| 716 |
return ArrayRef(kUnavailableRegistersSSE, |
| 717 |
sizeof(kUnavailableRegistersSSE) / |
| 718 |
sizeof(kUnavailableRegistersSSE[0])); |
| 719 |
|
| 720 |
return ArrayRef(kUnavailableRegisters, std::size(kUnavailableRegisters)); |
| 721 |
} |
| 722 |
|
| 723 |
bool allowAsBackToBack(const Instruction &Instr) const override { |
| 724 |
const unsigned Opcode = Instr.Description.Opcode; |
| 725 |
return !isInvalidOpcode(Instr) && Opcode != X86::LEA64r && |
| 726 |
Opcode != X86::LEA64_32r && Opcode != X86::LEA16r; |
| 727 |
} |
| 728 |
|
| 729 |
std::vector<InstructionTemplate> |
| 730 |
generateInstructionVariants(const Instruction &Instr, |
| 731 |
unsigned MaxConfigsPerOpcode) const override; |
| 732 |
|
| 733 |
std::unique_ptr<SnippetGenerator> createSerialSnippetGenerator( |
| 734 |
const LLVMState &State, |
| 735 |
const SnippetGenerator::Options &Opts) const override { |
| 736 |
return std::make_unique<X86SerialSnippetGenerator>(State, Opts); |
| 737 |
} |
| 738 |
|
| 739 |
std::unique_ptr<SnippetGenerator> createParallelSnippetGenerator( |
| 740 |
const LLVMState &State, |
| 741 |
const SnippetGenerator::Options &Opts) const override { |
| 742 |
return std::make_unique<X86ParallelSnippetGenerator>(State, Opts); |
| 743 |
} |
| 744 |
|
| 745 |
bool matchesArch(Triple::ArchType Arch) const override { |
| 746 |
return Arch == Triple::x86_64 || Arch == Triple::x86; |
| 747 |
} |
| 748 |
|
| 749 |
Error checkFeatureSupport() const override { |
| 750 |
// LBR is the only feature we conditionally support now. |
| 751 |
// So if LBR is not requested, then we should be able to run the benchmarks. |
| 752 |
if (LbrSamplingPeriod == 0) |
| 753 |
return Error::success(); |
| 754 |
|
| 755 |
#if defined(__linux__) && defined(HAVE_LIBPFM) && \ |
| 756 |
defined(LIBPFM_HAS_FIELD_CYCLES) |
| 757 |
// FIXME: Fix this. |
| 758 |
// https://bugs.llvm.org/show_bug.cgi?id=48918 |
| 759 |
// For now, only do the check if we see an Intel machine because |
| 760 |
// the counter uses some intel-specific magic and it could |
| 761 |
// be confuse and think an AMD machine actually has LBR support. |
| 762 |
#if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || \ |
| 763 |
defined(_M_X64) |
| 764 |
using namespace sys::detail::x86; |
| 765 |
|
| 766 |
if (getVendorSignature() == VendorSignatures::GENUINE_INTEL) |
| 767 |
// If the kernel supports it, the hardware still may not have it. |
| 768 |
return X86LbrCounter::checkLbrSupport(); |
| 769 |
#else |
| 770 |
llvm_unreachable("Running X86 exegesis on non-X86 target"); |
| 771 |
#endif |
| 772 |
#endif |
| 773 |
return llvm::make_error<llvm::StringError>( |
| 774 |
"LBR not supported on this kernel and/or platform", |
| 775 |
llvm::errc::not_supported); |
| 776 |
} |
| 777 |
|
| 778 |
std::unique_ptr<SavedState> withSavedState() const override { |
| 779 |
return std::make_unique<X86SavedState>(); |
| 780 |
} |
| 781 |
|
| 782 |
static const unsigned kUnavailableRegisters[4]; |
| 783 |
static const unsigned kUnavailableRegistersSSE[12]; |
| 784 |
}; |
| 785 |
|
| 786 |
// We disable a few registers that cannot be encoded on instructions with a REX |
| 787 |
// prefix. |
| 788 |
const unsigned ExegesisX86Target::kUnavailableRegisters[4] = {X86::AH, X86::BH, |
| 789 |
X86::CH, X86::DH}; |
| 790 |
|
| 791 |
// Optionally, also disable the upper (x86_64) SSE registers to reduce frontend |
| 792 |
// decoder load. |
| 793 |
const unsigned ExegesisX86Target::kUnavailableRegistersSSE[12] = { |
| 794 |
X86::AH, X86::BH, X86::CH, X86::DH, X86::XMM8, X86::XMM9, |
| 795 |
X86::XMM10, X86::XMM11, X86::XMM12, X86::XMM13, X86::XMM14, X86::XMM15}; |
| 796 |
|
| 797 |
// We're using one of R8-R15 because these registers are never hardcoded in |
| 798 |
// instructions (e.g. MOVS writes to EDI, ESI, EDX), so they have less |
| 799 |
// conflicts. |
| 800 |
constexpr const unsigned kLoopCounterReg = X86::R8; |
| 801 |
|
| 802 |
} // namespace |
| 803 |
|
| 804 |
void ExegesisX86Target::addTargetSpecificPasses(PassManagerBase &PM) const { |
| 805 |
// Lowers FP pseudo-instructions, e.g. ABS_Fp32 -> ABS_F. |
| 806 |
PM.add(createX86FloatingPointStackifierPass()); |
| 807 |
} |
| 808 |
|
| 809 |
unsigned ExegesisX86Target::getScratchMemoryRegister(const Triple &TT) const { |
| 810 |
if (!TT.isArch64Bit()) { |
| 811 |
// FIXME: This would require popping from the stack, so we would have to |
| 812 |
// add some additional setup code. |
| 813 |
return 0; |
| 814 |
} |
| 815 |
return TT.isOSWindows() ? X86::RCX : X86::RDI; |
| 816 |
} |
| 817 |
|
| 818 |
unsigned ExegesisX86Target::getLoopCounterRegister(const Triple &TT) const { |
| 819 |
if (!TT.isArch64Bit()) { |
| 820 |
return 0; |
| 821 |
} |
| 822 |
return kLoopCounterReg; |
| 823 |
} |
| 824 |
|
| 825 |
Error ExegesisX86Target::randomizeTargetMCOperand( |
| 826 |
const Instruction &Instr, const Variable &Var, MCOperand &AssignedValue, |
| 827 |
const BitVector &ForbiddenRegs) const { |
| 828 |
const Operand &Op = Instr.getPrimaryOperand(Var); |
| 829 |
switch (Op.getExplicitOperandInfo().OperandType) { |
| 830 |
case X86::OperandType::OPERAND_ROUNDING_CONTROL: |
| 831 |
AssignedValue = |
| 832 |
MCOperand::createImm(randomIndex(X86::STATIC_ROUNDING::TO_ZERO)); |
| 833 |
return Error::success(); |
| 834 |
default: |
| 835 |
break; |
| 836 |
} |
| 837 |
return make_error<Failure>( |
| 838 |
Twine("unimplemented operand type ") |
| 839 |
.concat(Twine(Op.getExplicitOperandInfo().OperandType))); |
| 840 |
} |
| 841 |
|
| 842 |
void ExegesisX86Target::fillMemoryOperands(InstructionTemplate &IT, |
| 843 |
unsigned Reg, |
| 844 |
unsigned Offset) const { |
| 845 |
assert(!isInvalidMemoryInstr(IT.getInstr()) && |
| 846 |
"fillMemoryOperands requires a valid memory instruction"); |
| 847 |
int MemOpIdx = X86II::getMemoryOperandNo(IT.getInstr().Description.TSFlags); |
| 848 |
assert(MemOpIdx >= 0 && "invalid memory operand index"); |
| 849 |
// getMemoryOperandNo() ignores tied operands, so we have to add them back. |
| 850 |
MemOpIdx += X86II::getOperandBias(IT.getInstr().Description); |
| 851 |
setMemOp(IT, MemOpIdx + 0, MCOperand::createReg(Reg)); // BaseReg |
| 852 |
setMemOp(IT, MemOpIdx + 1, MCOperand::createImm(1)); // ScaleAmt |
| 853 |
setMemOp(IT, MemOpIdx + 2, MCOperand::createReg(0)); // IndexReg |
| 854 |
setMemOp(IT, MemOpIdx + 3, MCOperand::createImm(Offset)); // Disp |
| 855 |
setMemOp(IT, MemOpIdx + 4, MCOperand::createReg(0)); // Segment |
| 856 |
} |
| 857 |
|
| 858 |
void ExegesisX86Target::decrementLoopCounterAndJump( |
| 859 |
MachineBasicBlock &MBB, MachineBasicBlock &TargetMBB, |
| 860 |
const MCInstrInfo &MII) const { |
| 861 |
BuildMI(&MBB, DebugLoc(), MII.get(X86::ADD64ri8)) |
| 862 |
.addDef(kLoopCounterReg) |
| 863 |
.addUse(kLoopCounterReg) |
| 864 |
.addImm(-1); |
| 865 |
BuildMI(&MBB, DebugLoc(), MII.get(X86::JCC_1)) |
| 866 |
.addMBB(&TargetMBB) |
| 867 |
.addImm(X86::COND_NE); |
| 868 |
} |
| 869 |
|
| 870 |
std::vector<MCInst> ExegesisX86Target::setRegTo(const MCSubtargetInfo &STI, |
| 871 |
unsigned Reg, |
| 872 |
const APInt &Value) const { |
| 873 |
if (X86::GR8RegClass.contains(Reg)) |
| 874 |
return {loadImmediate(Reg, 8, Value)}; |
| 875 |
if (X86::GR16RegClass.contains(Reg)) |
| 876 |
return {loadImmediate(Reg, 16, Value)}; |
| 877 |
if (X86::GR32RegClass.contains(Reg)) |
| 878 |
return {loadImmediate(Reg, 32, Value)}; |
| 879 |
if (X86::GR64RegClass.contains(Reg)) |
| 880 |
return {loadImmediate(Reg, 64, Value)}; |
| 881 |
if (X86::VK8RegClass.contains(Reg) || X86::VK16RegClass.contains(Reg) || |
| 882 |
X86::VK32RegClass.contains(Reg) || X86::VK64RegClass.contains(Reg)) { |
| 883 |
switch (Value.getBitWidth()) { |
| 884 |
case 8: |
| 885 |
if (STI.getFeatureBits()[X86::FeatureDQI]) { |
| 886 |
ConstantInliner CI(Value); |
| 887 |
return CI.loadAndFinalize(Reg, Value.getBitWidth(), X86::KMOVBkm); |
| 888 |
} |
| 889 |
[[fallthrough]]; |
| 890 |
case 16: |
| 891 |
if (STI.getFeatureBits()[X86::FeatureAVX512]) { |
| 892 |
ConstantInliner CI(Value.zextOrTrunc(16)); |
| 893 |
return CI.loadAndFinalize(Reg, 16, X86::KMOVWkm); |
| 894 |
} |
| 895 |
break; |
| 896 |
case 32: |
| 897 |
if (STI.getFeatureBits()[X86::FeatureBWI]) { |
| 898 |
ConstantInliner CI(Value); |
| 899 |
return CI.loadAndFinalize(Reg, Value.getBitWidth(), X86::KMOVDkm); |
| 900 |
} |
| 901 |
break; |
| 902 |
case 64: |
| 903 |
if (STI.getFeatureBits()[X86::FeatureBWI]) { |
| 904 |
ConstantInliner CI(Value); |
| 905 |
return CI.loadAndFinalize(Reg, Value.getBitWidth(), X86::KMOVQkm); |
| 906 |
} |
| 907 |
break; |
| 908 |
} |
| 909 |
} |
| 910 |
ConstantInliner CI(Value); |
| 911 |
if (X86::VR64RegClass.contains(Reg)) |
| 912 |
return CI.loadAndFinalize(Reg, 64, X86::MMX_MOVQ64rm); |
| 913 |
if (X86::VR128XRegClass.contains(Reg)) { |
| 914 |
if (STI.getFeatureBits()[X86::FeatureAVX512]) |
| 915 |
return CI.loadAndFinalize(Reg, 128, X86::VMOVDQU32Z128rm); |
| 916 |
if (STI.getFeatureBits()[X86::FeatureAVX]) |
| 917 |
return CI.loadAndFinalize(Reg, 128, X86::VMOVDQUrm); |
| 918 |
return CI.loadAndFinalize(Reg, 128, X86::MOVDQUrm); |
| 919 |
} |
| 920 |
if (X86::VR256XRegClass.contains(Reg)) { |
| 921 |
if (STI.getFeatureBits()[X86::FeatureAVX512]) |
| 922 |
return CI.loadAndFinalize(Reg, 256, X86::VMOVDQU32Z256rm); |
| 923 |
if (STI.getFeatureBits()[X86::FeatureAVX]) |
| 924 |
return CI.loadAndFinalize(Reg, 256, X86::VMOVDQUYrm); |
| 925 |
} |
| 926 |
if (X86::VR512RegClass.contains(Reg)) |
| 927 |
if (STI.getFeatureBits()[X86::FeatureAVX512]) |
| 928 |
return CI.loadAndFinalize(Reg, 512, X86::VMOVDQU32Zrm); |
| 929 |
if (X86::RSTRegClass.contains(Reg)) { |
| 930 |
return CI.loadX87STAndFinalize(Reg); |
| 931 |
} |
| 932 |
if (X86::RFP32RegClass.contains(Reg) || X86::RFP64RegClass.contains(Reg) || |
| 933 |
X86::RFP80RegClass.contains(Reg)) { |
| 934 |
return CI.loadX87FPAndFinalize(Reg); |
| 935 |
} |
| 936 |
if (Reg == X86::EFLAGS) |
| 937 |
return CI.popFlagAndFinalize(); |
| 938 |
if (Reg == X86::MXCSR) |
| 939 |
return CI.loadImplicitRegAndFinalize( |
| 940 |
STI.getFeatureBits()[X86::FeatureAVX] ? X86::VLDMXCSR : X86::LDMXCSR, |
| 941 |
0x1f80); |
| 942 |
if (Reg == X86::FPCW) |
| 943 |
return CI.loadImplicitRegAndFinalize(X86::FLDCW16m, 0x37f); |
| 944 |
return {}; // Not yet implemented. |
| 945 |
} |
| 946 |
|
| 947 |
// Instruction can have some variable operands, and we may want to see how |
| 948 |
// different operands affect performance. So for each operand position, |
| 949 |
// precompute all the possible choices we might care about, |
| 950 |
// and greedily generate all the possible combinations of choices. |
| 951 |
std::vector<InstructionTemplate> ExegesisX86Target::generateInstructionVariants( |
| 952 |
const Instruction &Instr, unsigned MaxConfigsPerOpcode) const { |
| 953 |
bool Exploration = false; |
| 954 |
SmallVector<SmallVector<MCOperand, 1>, 4> VariableChoices; |
| 955 |
VariableChoices.resize(Instr.Variables.size()); |
| 956 |
for (auto I : llvm::zip(Instr.Variables, VariableChoices)) { |
| 957 |
const Variable &Var = std::get<0>(I); |
| 958 |
SmallVectorImpl<MCOperand> &Choices = std::get<1>(I); |
| 959 |
|
| 960 |
switch (Instr.getPrimaryOperand(Var).getExplicitOperandInfo().OperandType) { |
| 961 |
default: |
| 962 |
// We don't wish to explicitly explore this variable. |
| 963 |
Choices.emplace_back(); // But add invalid MCOperand to simplify logic. |
| 964 |
continue; |
| 965 |
case X86::OperandType::OPERAND_COND_CODE: { |
| 966 |
Exploration = true; |
| 967 |
auto CondCodes = enum_seq_inclusive(X86::CondCode::COND_O, |
| 968 |
X86::CondCode::LAST_VALID_COND, |
| 969 |
force_iteration_on_noniterable_enum); |
| 970 |
Choices.reserve(CondCodes.size()); |
| 971 |
for (int CondCode : CondCodes) |
| 972 |
Choices.emplace_back(MCOperand::createImm(CondCode)); |
| 973 |
break; |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
// If we don't wish to explore any variables, defer to the baseline method. |
| 979 |
if (!Exploration) |
| 980 |
return ExegesisTarget::generateInstructionVariants(Instr, |
| 981 |
MaxConfigsPerOpcode); |
| 982 |
|
| 983 |
std::vector<InstructionTemplate> Variants; |
| 984 |
size_t NumVariants; |
| 985 |
CombinationGenerator<MCOperand, decltype(VariableChoices)::value_type, 4> G( |
| 986 |
VariableChoices); |
| 987 |
|
| 988 |
// How many operand combinations can we produce, within the limit? |
| 989 |
NumVariants = std::min(G.numCombinations(), (size_t)MaxConfigsPerOpcode); |
| 990 |
// And actually produce all the wanted operand combinations. |
| 991 |
Variants.reserve(NumVariants); |
| 992 |
G.generate([&](ArrayRef<MCOperand> State) -> bool { |
| 993 |
Variants.emplace_back(&Instr); |
| 994 |
Variants.back().setVariableValues(State); |
| 995 |
// Did we run out of space for variants? |
| 996 |
return Variants.size() >= NumVariants; |
| 997 |
}); |
| 998 |
|
| 999 |
assert(Variants.size() == NumVariants && |
| 1000 |
Variants.size() <= MaxConfigsPerOpcode && |
| 1001 |
"Should not produce too many variants"); |
| 1002 |
return Variants; |
| 1003 |
} |
| 1004 |
|
| 1005 |
static ExegesisTarget *getTheExegesisX86Target() { |
| 1006 |
static ExegesisX86Target Target; |
| 1007 |
return &Target; |
| 1008 |
} |
| 1009 |
|
| 1010 |
void InitializeX86ExegesisTarget() { |
| 1011 |
ExegesisTarget::registerTarget(getTheExegesisX86Target()); |
| 1012 |
} |
| 1013 |
|
| 1014 |
} // namespace exegesis |
| 1015 |
} // namespace llvm |