xref: /llvm-project/llvm/lib/Target/ARC/ARCTargetMachine.cpp (revision bac974278c5e5a3d6dea40d2d22cb36bcc487cee)
1 //===- ARCTargetMachine.cpp - Define TargetMachine for ARC ------*- 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 //
10 //===----------------------------------------------------------------------===//
11 
12 #include "ARCTargetMachine.h"
13 #include "ARC.h"
14 #include "ARCTargetTransformInfo.h"
15 #include "TargetInfo/ARCTargetInfo.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/MC/TargetRegistry.h"
20 #include <optional>
21 
22 using namespace llvm;
23 
24 static Reloc::Model getRelocModel(std::optional<Reloc::Model> RM) {
25   return RM.value_or(Reloc::Static);
26 }
27 
28 /// ARCTargetMachine ctor - Create an ILP32 architecture model
29 ARCTargetMachine::ARCTargetMachine(const Target &T, const Triple &TT,
30                                    StringRef CPU, StringRef FS,
31                                    const TargetOptions &Options,
32                                    std::optional<Reloc::Model> RM,
33                                    std::optional<CodeModel::Model> CM,
34                                    CodeGenOpt::Level OL, bool JIT)
35     : LLVMTargetMachine(T,
36                         "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
37                         "f32:32:32-i64:32-f64:32-a:0:32-n32",
38                         TT, CPU, FS, Options, getRelocModel(RM),
39                         getEffectiveCodeModel(CM, CodeModel::Small), OL),
40       TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
41       Subtarget(TT, std::string(CPU), std::string(FS), *this) {
42   initAsmInfo();
43 }
44 
45 ARCTargetMachine::~ARCTargetMachine() = default;
46 
47 namespace {
48 
49 /// ARC Code Generator Pass Configuration Options.
50 class ARCPassConfig : public TargetPassConfig {
51 public:
52   ARCPassConfig(ARCTargetMachine &TM, PassManagerBase &PM)
53       : TargetPassConfig(TM, PM) {}
54 
55   ARCTargetMachine &getARCTargetMachine() const {
56     return getTM<ARCTargetMachine>();
57   }
58 
59   bool addInstSelector() override;
60   void addPreEmitPass() override;
61   void addPreRegAlloc() override;
62 };
63 
64 } // end anonymous namespace
65 
66 TargetPassConfig *ARCTargetMachine::createPassConfig(PassManagerBase &PM) {
67   return new ARCPassConfig(*this, PM);
68 }
69 
70 bool ARCPassConfig::addInstSelector() {
71   addPass(createARCISelDag(getARCTargetMachine(), getOptLevel()));
72   return false;
73 }
74 
75 void ARCPassConfig::addPreEmitPass() { addPass(createARCBranchFinalizePass()); }
76 
77 void ARCPassConfig::addPreRegAlloc() {
78     addPass(createARCExpandPseudosPass());
79     addPass(createARCOptAddrMode());
80 }
81 
82 // Force static initialization.
83 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARCTarget() {
84   RegisterTargetMachine<ARCTargetMachine> X(getTheARCTarget());
85 }
86 
87 TargetTransformInfo
88 ARCTargetMachine::getTargetTransformInfo(const Function &F) const {
89   return TargetTransformInfo(ARCTTIImpl(this, F));
90 }
91