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