blob: 25304280d00246f32505b66041b11bfbef3a685e [file] [log] [blame]
Dylan McKay6d8078f2016-05-06 10:12:31 +00001//===-- AVRTargetMachine.cpp - Define TargetMachine for AVR ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dylan McKay6d8078f2016-05-06 10:12:31 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the AVR specific subclass of TargetMachine.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AVRTargetMachine.h"
14
15#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000016#include "llvm/CodeGen/TargetPassConfig.h"
Dylan McKay6d8078f2016-05-06 10:12:31 +000017#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/IR/Module.h"
Dylan McKay6d8078f2016-05-06 10:12:31 +000019#include "llvm/Support/TargetRegistry.h"
20
Dylan McKay6d8078f2016-05-06 10:12:31 +000021#include "AVR.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000022#include "AVRTargetObjectFile.h"
Dylan McKay6d8078f2016-05-06 10:12:31 +000023#include "MCTargetDesc/AVRMCTargetDesc.h"
Richard Trieue982b422019-05-14 22:41:58 +000024#include "TargetInfo/AVRTargetInfo.h"
Dylan McKay6d8078f2016-05-06 10:12:31 +000025
26namespace llvm {
27
Dylan McKay9a2a9962018-02-19 10:40:59 +000028static const char *AVRDataLayout = "e-P1-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
Dylan McKay6d8078f2016-05-06 10:12:31 +000043AVRTargetMachine::AVRTargetMachine(const Target &T, const Triple &TT,
44 StringRef CPU, StringRef FS,
45 const TargetOptions &Options,
Dylan McKay05474472017-08-04 05:48:20 +000046 Optional<Reloc::Model> RM,
47 Optional<CodeModel::Model> CM,
Meador Inge70ab7cc2017-08-06 12:02:17 +000048 CodeGenOpt::Level OL, bool JIT)
Matthias Braunbb8507e2017-10-12 22:57:28 +000049 : LLVMTargetMachine(T, AVRDataLayout, TT, getCPU(CPU), FS, Options,
David Greenca29c272018-12-07 12:10:23 +000050 getEffectiveRelocModel(RM),
51 getEffectiveCodeModel(CM, CodeModel::Small), OL),
Dylan McKayf1f1c012016-05-18 11:11:38 +000052 SubTarget(TT, getCPU(CPU), FS, *this) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000053 this->TLOF = std::make_unique<AVRTargetObjectFile>();
Dylan McKay6d8078f2016-05-06 10:12:31 +000054 initAsmInfo();
55}
56
57namespace {
58/// AVR Code Generator Pass Configuration Options.
59class AVRPassConfig : public TargetPassConfig {
60public:
Matthias Braun5e394c32017-05-30 21:36:41 +000061 AVRPassConfig(AVRTargetMachine &TM, PassManagerBase &PM)
Dylan McKay6d8078f2016-05-06 10:12:31 +000062 : TargetPassConfig(TM, PM) {}
63
64 AVRTargetMachine &getAVRTargetMachine() const {
65 return getTM<AVRTargetMachine>();
66 }
67
68 bool addInstSelector() override;
69 void addPreSched2() override;
Dylan McKay9cf1dc12017-07-11 04:17:13 +000070 void addPreEmitPass() override;
Dylan McKay6d8078f2016-05-06 10:12:31 +000071 void addPreRegAlloc() override;
Dylan McKay6d8078f2016-05-06 10:12:31 +000072};
73} // namespace
74
75TargetPassConfig *AVRTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +000076 return new AVRPassConfig(*this, PM);
Dylan McKay6d8078f2016-05-06 10:12:31 +000077}
Dylan McKayc498ba32015-11-12 09:26:44 +000078
Tom Stellard4b0b2612019-06-11 03:21:13 +000079extern "C" void LLVMInitializeAVRTarget() {
Dylan McKay6d8078f2016-05-06 10:12:31 +000080 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000081 RegisterTargetMachine<AVRTargetMachine> X(getTheAVRTarget());
Dylan McKay8cec7eb2016-12-07 11:08:56 +000082
83 auto &PR = *PassRegistry::getPassRegistry();
84 initializeAVRExpandPseudoPass(PR);
Dylan McKay1e57fa42016-12-13 05:53:14 +000085 initializeAVRRelaxMemPass(PR);
Dylan McKayc498ba32015-11-12 09:26:44 +000086}
Dylan McKay6d8078f2016-05-06 10:12:31 +000087
88const AVRSubtarget *AVRTargetMachine::getSubtargetImpl() const {
89 return &SubTarget;
90}
91
92const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const {
93 return &SubTarget;
94}
95
96//===----------------------------------------------------------------------===//
97// Pass Pipeline Configuration
98//===----------------------------------------------------------------------===//
99
100bool AVRPassConfig::addInstSelector() {
Dylan McKayc988b332016-11-07 06:02:55 +0000101 // Install an instruction selector.
102 addPass(createAVRISelDag(getAVRTargetMachine(), getOptLevel()));
103 // Create the frame analyzer pass used by the PEI pass.
104 addPass(createAVRFrameAnalyzerPass());
105
Dylan McKay6d8078f2016-05-06 10:12:31 +0000106 return false;
107}
108
109void AVRPassConfig::addPreRegAlloc() {
Dylan McKayc988b332016-11-07 06:02:55 +0000110 // Create the dynalloc SP save/restore pass to handle variable sized allocas.
111 addPass(createAVRDynAllocaSRPass());
Dylan McKay6d8078f2016-05-06 10:12:31 +0000112}
113
Dylan McKay1e57fa42016-12-13 05:53:14 +0000114void AVRPassConfig::addPreSched2() {
115 addPass(createAVRRelaxMemPass());
116 addPass(createAVRExpandPseudoPass());
117}
Dylan McKay6d8078f2016-05-06 10:12:31 +0000118
Dylan McKay9cf1dc12017-07-11 04:17:13 +0000119void AVRPassConfig::addPreEmitPass() {
120 // Must run branch selection immediately preceding the asm printer.
121 addPass(&BranchRelaxationPassID);
122}
123
Dylan McKay6d8078f2016-05-06 10:12:31 +0000124} // end of namespace llvm