blob: f9a738b2182ccff7138a18b78be2d39f9c4a572e [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 Braunbb8507e2017-10-12 22:57:28 +000055 : LLVMTargetMachine(T, AVRDataLayout, TT, getCPU(CPU), FS, Options,
56 getEffectiveRelocModel(RM), getEffectiveCodeModel(CM),
57 OL),
Dylan McKayf1f1c012016-05-18 11:11:38 +000058 SubTarget(TT, getCPU(CPU), FS, *this) {
Dylan McKay6d8078f2016-05-06 10:12:31 +000059 this->TLOF = make_unique<AVRTargetObjectFile>();
60 initAsmInfo();
61}
62
63namespace {
64/// AVR Code Generator Pass Configuration Options.
65class AVRPassConfig : public TargetPassConfig {
66public:
Matthias Braun5e394c32017-05-30 21:36:41 +000067 AVRPassConfig(AVRTargetMachine &TM, PassManagerBase &PM)
Dylan McKay6d8078f2016-05-06 10:12:31 +000068 : TargetPassConfig(TM, PM) {}
69
70 AVRTargetMachine &getAVRTargetMachine() const {
71 return getTM<AVRTargetMachine>();
72 }
73
74 bool addInstSelector() override;
75 void addPreSched2() override;
Dylan McKay9cf1dc12017-07-11 04:17:13 +000076 void addPreEmitPass() override;
Dylan McKay6d8078f2016-05-06 10:12:31 +000077 void addPreRegAlloc() override;
Dylan McKay6d8078f2016-05-06 10:12:31 +000078};
79} // namespace
80
81TargetPassConfig *AVRTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +000082 return new AVRPassConfig(*this, PM);
Dylan McKay6d8078f2016-05-06 10:12:31 +000083}
Dylan McKayc498ba32015-11-12 09:26:44 +000084
85extern "C" void LLVMInitializeAVRTarget() {
Dylan McKay6d8078f2016-05-06 10:12:31 +000086 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000087 RegisterTargetMachine<AVRTargetMachine> X(getTheAVRTarget());
Dylan McKay8cec7eb2016-12-07 11:08:56 +000088
89 auto &PR = *PassRegistry::getPassRegistry();
90 initializeAVRExpandPseudoPass(PR);
Dylan McKay1e57fa42016-12-13 05:53:14 +000091 initializeAVRRelaxMemPass(PR);
Dylan McKayc498ba32015-11-12 09:26:44 +000092}
Dylan McKay6d8078f2016-05-06 10:12:31 +000093
94const AVRSubtarget *AVRTargetMachine::getSubtargetImpl() const {
95 return &SubTarget;
96}
97
98const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const {
99 return &SubTarget;
100}
101
102//===----------------------------------------------------------------------===//
103// Pass Pipeline Configuration
104//===----------------------------------------------------------------------===//
105
106bool AVRPassConfig::addInstSelector() {
Dylan McKayc988b332016-11-07 06:02:55 +0000107 // Install an instruction selector.
108 addPass(createAVRISelDag(getAVRTargetMachine(), getOptLevel()));
109 // Create the frame analyzer pass used by the PEI pass.
110 addPass(createAVRFrameAnalyzerPass());
111
Dylan McKay6d8078f2016-05-06 10:12:31 +0000112 return false;
113}
114
115void AVRPassConfig::addPreRegAlloc() {
Dylan McKayc988b332016-11-07 06:02:55 +0000116 // Create the dynalloc SP save/restore pass to handle variable sized allocas.
117 addPass(createAVRDynAllocaSRPass());
Dylan McKay6d8078f2016-05-06 10:12:31 +0000118}
119
Dylan McKay1e57fa42016-12-13 05:53:14 +0000120void AVRPassConfig::addPreSched2() {
121 addPass(createAVRRelaxMemPass());
122 addPass(createAVRExpandPseudoPass());
123}
Dylan McKay6d8078f2016-05-06 10:12:31 +0000124
Dylan McKay9cf1dc12017-07-11 04:17:13 +0000125void AVRPassConfig::addPreEmitPass() {
126 // Must run branch selection immediately preceding the asm printer.
127 addPass(&BranchRelaxationPassID);
128}
129
Dylan McKay6d8078f2016-05-06 10:12:31 +0000130} // end of namespace llvm