blob: 4189a242e9d8ec960821e93fc11d70bcb5b4504d [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/Module.h"
19#include "llvm/IR/LegacyPassManager.h"
20#include "llvm/Support/TargetRegistry.h"
21
22#include "AVRTargetObjectFile.h"
23#include "AVR.h"
24#include "MCTargetDesc/AVRMCTargetDesc.h"
25
26namespace llvm {
27
Dylan McKayb967d162016-09-28 13:29:10 +000028static const char *AVRDataLayout = "e-p:16:16:16-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-n8";
29
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 McKaybe8e2e02016-05-20 23:39:04 +000046 Optional<Reloc::Model> RM, CodeModel::Model CM,
Dylan McKay6d8078f2016-05-06 10:12:31 +000047 CodeGenOpt::Level OL)
48 : LLVMTargetMachine(
Dylan McKayb967d162016-09-28 13:29:10 +000049 T, AVRDataLayout, TT,
Dylan McKaybe8e2e02016-05-20 23:39:04 +000050 getCPU(CPU), FS, Options, getEffectiveRelocModel(RM), CM, OL),
Dylan McKayf1f1c012016-05-18 11:11:38 +000051 SubTarget(TT, getCPU(CPU), FS, *this) {
Dylan McKay6d8078f2016-05-06 10:12:31 +000052 this->TLOF = make_unique<AVRTargetObjectFile>();
53 initAsmInfo();
54}
55
56namespace {
57/// AVR Code Generator Pass Configuration Options.
58class AVRPassConfig : public TargetPassConfig {
59public:
60 AVRPassConfig(AVRTargetMachine *TM, PassManagerBase &PM)
61 : TargetPassConfig(TM, PM) {}
62
63 AVRTargetMachine &getAVRTargetMachine() const {
64 return getTM<AVRTargetMachine>();
65 }
66
67 bool addInstSelector() override;
68 void addPreSched2() override;
69 void addPreRegAlloc() override;
Dylan McKay6d8078f2016-05-06 10:12:31 +000070};
71} // namespace
72
73TargetPassConfig *AVRTargetMachine::createPassConfig(PassManagerBase &PM) {
74 return new AVRPassConfig(this, PM);
75}
Dylan McKayc498ba32015-11-12 09:26:44 +000076
77extern "C" void LLVMInitializeAVRTarget() {
Dylan McKay6d8078f2016-05-06 10:12:31 +000078 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000079 RegisterTargetMachine<AVRTargetMachine> X(getTheAVRTarget());
Dylan McKay8cec7eb2016-12-07 11:08:56 +000080
81 auto &PR = *PassRegistry::getPassRegistry();
82 initializeAVRExpandPseudoPass(PR);
Dylan McKayc498ba32015-11-12 09:26:44 +000083}
Dylan McKay6d8078f2016-05-06 10:12:31 +000084
85const AVRSubtarget *AVRTargetMachine::getSubtargetImpl() const {
86 return &SubTarget;
87}
88
89const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const {
90 return &SubTarget;
91}
92
93//===----------------------------------------------------------------------===//
94// Pass Pipeline Configuration
95//===----------------------------------------------------------------------===//
96
97bool AVRPassConfig::addInstSelector() {
Dylan McKayc988b332016-11-07 06:02:55 +000098 // Install an instruction selector.
99 addPass(createAVRISelDag(getAVRTargetMachine(), getOptLevel()));
100 // Create the frame analyzer pass used by the PEI pass.
101 addPass(createAVRFrameAnalyzerPass());
102
Dylan McKay6d8078f2016-05-06 10:12:31 +0000103 return false;
104}
105
106void AVRPassConfig::addPreRegAlloc() {
Dylan McKayc988b332016-11-07 06:02:55 +0000107 // Create the dynalloc SP save/restore pass to handle variable sized allocas.
108 addPass(createAVRDynAllocaSRPass());
Dylan McKay6d8078f2016-05-06 10:12:31 +0000109}
110
Dylan McKaya789f402016-11-16 21:58:04 +0000111void AVRPassConfig::addPreSched2() { addPass(createAVRExpandPseudoPass()); }
Dylan McKay6d8078f2016-05-06 10:12:31 +0000112
Dylan McKay6d8078f2016-05-06 10:12:31 +0000113} // end of namespace llvm