blob: 1fbabe00606e71c090010efd41a144ec7aff1423 [file] [log] [blame]
Dylan McKay6d8078f2016-05-06 10:12:31 +00001//===-- AVRTargetMachine.cpp - Define TargetMachine for AVR ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the AVR specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AVRTargetMachine.h"
15
16#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000017#include "llvm/CodeGen/TargetPassConfig.h"
Dylan McKay6d8078f2016-05-06 10:12:31 +000018#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/IR/Module.h"
Dylan McKay6d8078f2016-05-06 10:12:31 +000020#include "llvm/Support/TargetRegistry.h"
21
Dylan McKay6d8078f2016-05-06 10:12:31 +000022#include "AVR.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "AVRTargetObjectFile.h"
Dylan McKay6d8078f2016-05-06 10:12:31 +000024#include "MCTargetDesc/AVRMCTargetDesc.h"
25
26namespace llvm {
27
Dylan McKay832c4a62017-09-26 00:45:27 +000028static const char *AVRDataLayout = "e-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8";
Dylan McKayb967d162016-09-28 13:29:10 +000029
Dylan McKay6d8078f2016-05-06 10:12:31 +000030/// Processes a CPU name.
Dylan McKayf1f1c012016-05-18 11:11:38 +000031static StringRef getCPU(StringRef CPU) {
Dylan McKay6d8078f2016-05-06 10:12:31 +000032 if (CPU.empty() || CPU == "generic") {
33 return "avr2";
34 }
35
36 return CPU;
37}
38
Dylan McKaybe8e2e02016-05-20 23:39:04 +000039static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
40 return RM.hasValue() ? *RM : Reloc::Static;
41}
42
Meador Inge70ab7cc2017-08-06 12:02:17 +000043static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
44 if (CM)
45 return *CM;
46 return CodeModel::Small;
47}
48
Dylan McKay6d8078f2016-05-06 10:12:31 +000049AVRTargetMachine::AVRTargetMachine(const Target &T, const Triple &TT,
50 StringRef CPU, StringRef FS,
51 const TargetOptions &Options,
Dylan McKay05474472017-08-04 05:48:20 +000052 Optional<Reloc::Model> RM,
53 Optional<CodeModel::Model> CM,
Meador Inge70ab7cc2017-08-06 12:02:17 +000054 CodeGenOpt::Level OL, bool JIT)
Matthias Braun3a9c1142017-10-12 22:28:54 +000055 : TargetMachine(T, AVRDataLayout, TT, getCPU(CPU), FS, Options,
56 getEffectiveRelocModel(RM), getEffectiveCodeModel(CM), OL),
Dylan McKayf1f1c012016-05-18 11:11:38 +000057 SubTarget(TT, getCPU(CPU), FS, *this) {
Dylan McKay6d8078f2016-05-06 10:12:31 +000058 this->TLOF = make_unique<AVRTargetObjectFile>();
59 initAsmInfo();
60}
61
62namespace {
63/// AVR Code Generator Pass Configuration Options.
64class AVRPassConfig : public TargetPassConfig {
65public:
Matthias Braun5e394c32017-05-30 21:36:41 +000066 AVRPassConfig(AVRTargetMachine &TM, PassManagerBase &PM)
Dylan McKay6d8078f2016-05-06 10:12:31 +000067 : TargetPassConfig(TM, PM) {}
68
69 AVRTargetMachine &getAVRTargetMachine() const {
70 return getTM<AVRTargetMachine>();
71 }
72
73 bool addInstSelector() override;
74 void addPreSched2() override;
Dylan McKay9cf1dc12017-07-11 04:17:13 +000075 void addPreEmitPass() override;
Dylan McKay6d8078f2016-05-06 10:12:31 +000076 void addPreRegAlloc() override;
Dylan McKay6d8078f2016-05-06 10:12:31 +000077};
78} // namespace
79
80TargetPassConfig *AVRTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +000081 return new AVRPassConfig(*this, PM);
Dylan McKay6d8078f2016-05-06 10:12:31 +000082}
Dylan McKayc498ba32015-11-12 09:26:44 +000083
84extern "C" void LLVMInitializeAVRTarget() {
Dylan McKay6d8078f2016-05-06 10:12:31 +000085 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000086 RegisterTargetMachine<AVRTargetMachine> X(getTheAVRTarget());
Dylan McKay8cec7eb2016-12-07 11:08:56 +000087
88 auto &PR = *PassRegistry::getPassRegistry();
89 initializeAVRExpandPseudoPass(PR);
Dylan McKay1e57fa42016-12-13 05:53:14 +000090 initializeAVRRelaxMemPass(PR);
Dylan McKayc498ba32015-11-12 09:26:44 +000091}
Dylan McKay6d8078f2016-05-06 10:12:31 +000092
93const AVRSubtarget *AVRTargetMachine::getSubtargetImpl() const {
94 return &SubTarget;
95}
96
97const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const {
98 return &SubTarget;
99}
100
101//===----------------------------------------------------------------------===//
102// Pass Pipeline Configuration
103//===----------------------------------------------------------------------===//
104
105bool AVRPassConfig::addInstSelector() {
Dylan McKayc988b332016-11-07 06:02:55 +0000106 // Install an instruction selector.
107 addPass(createAVRISelDag(getAVRTargetMachine(), getOptLevel()));
108 // Create the frame analyzer pass used by the PEI pass.
109 addPass(createAVRFrameAnalyzerPass());
110
Dylan McKay6d8078f2016-05-06 10:12:31 +0000111 return false;
112}
113
114void AVRPassConfig::addPreRegAlloc() {
Dylan McKayc988b332016-11-07 06:02:55 +0000115 // Create the dynalloc SP save/restore pass to handle variable sized allocas.
116 addPass(createAVRDynAllocaSRPass());
Dylan McKay6d8078f2016-05-06 10:12:31 +0000117}
118
Dylan McKay1e57fa42016-12-13 05:53:14 +0000119void AVRPassConfig::addPreSched2() {
120 addPass(createAVRRelaxMemPass());
121 addPass(createAVRExpandPseudoPass());
122}
Dylan McKay6d8078f2016-05-06 10:12:31 +0000123
Dylan McKay9cf1dc12017-07-11 04:17:13 +0000124void AVRPassConfig::addPreEmitPass() {
125 // Must run branch selection immediately preceding the asm printer.
126 addPass(&BranchRelaxationPassID);
127}
128
Dylan McKay6d8078f2016-05-06 10:12:31 +0000129} // end of namespace llvm