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 // The PowerPC ExegesisTarget.
8 //===----------------------------------------------------------------------===//
9 #include "../Target.h"
10 #include "PPC.h"
11 #include "PPCRegisterInfo.h"
12 
13 namespace llvm {
14 namespace exegesis {
15 
16 #include "PPCGenExegesis.inc"
17 
18 namespace {
19 class ExegesisPowerPCTarget : public ExegesisTarget {
20 public:
21   ExegesisPowerPCTarget() : ExegesisTarget(PPCCpuPfmCounters) {}
22 
23 private:
24   std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg,
25                                const APInt &Value) const override;
26   bool matchesArch(Triple::ArchType Arch) const override {
27     return Arch == Triple::ppc64le;
28   }
29 };
30 } // end anonymous namespace
31 
32 static unsigned getLoadImmediateOpcode(unsigned RegBitWidth) {
33   switch (RegBitWidth) {
34   case 32:
35     return PPC::LI;
36   case 64:
37     return PPC::LI8;
38   }
39   llvm_unreachable("Invalid Value Width");
40 }
41 
42 // Generates instruction to load an immediate value into a register.
43 static MCInst loadImmediate(unsigned Reg, unsigned RegBitWidth,
44                             const APInt &Value) {
45   if (Value.getBitWidth() > RegBitWidth)
46     llvm_unreachable("Value must fit in the Register");
47   return MCInstBuilder(getLoadImmediateOpcode(RegBitWidth))
48       .addReg(Reg)
49       .addImm(Value.getZExtValue());
50 }
51 
52 std::vector<MCInst> ExegesisPowerPCTarget::setRegTo(const MCSubtargetInfo &STI,
53                                                     unsigned Reg,
54                                                     const APInt &Value) const {
55   if (PPC::GPRCRegClass.contains(Reg))
56     return {loadImmediate(Reg, 32, Value)};
57   if (PPC::G8RCRegClass.contains(Reg))
58     return {loadImmediate(Reg, 64, Value)};
59   errs() << "setRegTo is not implemented, results will be unreliable\n";
60   return {};
61 }
62 
63 static ExegesisTarget *getTheExegesisPowerPCTarget() {
64   static ExegesisPowerPCTarget Target;
65   return &Target;
66 }
67 
68 void InitializePowerPCExegesisTarget() {
69   ExegesisTarget::registerTarget(getTheExegesisPowerPCTarget());
70 }
71 
72 } // namespace exegesis
73 } // namespace llvm
74