Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 1 | //===-- 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 Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/TargetPassConfig.h" |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TargetRegistry.h" |
| 21 | |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 22 | #include "AVR.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 23 | #include "AVRTargetObjectFile.h" |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 24 | #include "MCTargetDesc/AVRMCTargetDesc.h" |
| 25 | |
| 26 | namespace llvm { |
| 27 | |
Dylan McKay | 832c4a6 | 2017-09-26 00:45:27 +0000 | [diff] [blame] | 28 | static const char *AVRDataLayout = "e-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8"; |
Dylan McKay | b967d16 | 2016-09-28 13:29:10 +0000 | [diff] [blame] | 29 | |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 30 | /// Processes a CPU name. |
Dylan McKay | f1f1c01 | 2016-05-18 11:11:38 +0000 | [diff] [blame] | 31 | static StringRef getCPU(StringRef CPU) { |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 32 | if (CPU.empty() || CPU == "generic") { |
| 33 | return "avr2"; |
| 34 | } |
| 35 | |
| 36 | return CPU; |
| 37 | } |
| 38 | |
Dylan McKay | be8e2e0 | 2016-05-20 23:39:04 +0000 | [diff] [blame] | 39 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { |
| 40 | return RM.hasValue() ? *RM : Reloc::Static; |
| 41 | } |
| 42 | |
Meador Inge | 70ab7cc | 2017-08-06 12:02:17 +0000 | [diff] [blame] | 43 | static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) { |
| 44 | if (CM) |
| 45 | return *CM; |
| 46 | return CodeModel::Small; |
| 47 | } |
| 48 | |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 49 | AVRTargetMachine::AVRTargetMachine(const Target &T, const Triple &TT, |
| 50 | StringRef CPU, StringRef FS, |
| 51 | const TargetOptions &Options, |
Dylan McKay | 0547447 | 2017-08-04 05:48:20 +0000 | [diff] [blame] | 52 | Optional<Reloc::Model> RM, |
| 53 | Optional<CodeModel::Model> CM, |
Meador Inge | 70ab7cc | 2017-08-06 12:02:17 +0000 | [diff] [blame] | 54 | CodeGenOpt::Level OL, bool JIT) |
Matthias Braun | 3a9c114 | 2017-10-12 22:28:54 +0000 | [diff] [blame] | 55 | : TargetMachine(T, AVRDataLayout, TT, getCPU(CPU), FS, Options, |
| 56 | getEffectiveRelocModel(RM), getEffectiveCodeModel(CM), OL), |
Dylan McKay | f1f1c01 | 2016-05-18 11:11:38 +0000 | [diff] [blame] | 57 | SubTarget(TT, getCPU(CPU), FS, *this) { |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 58 | this->TLOF = make_unique<AVRTargetObjectFile>(); |
| 59 | initAsmInfo(); |
| 60 | } |
| 61 | |
| 62 | namespace { |
| 63 | /// AVR Code Generator Pass Configuration Options. |
| 64 | class AVRPassConfig : public TargetPassConfig { |
| 65 | public: |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 66 | AVRPassConfig(AVRTargetMachine &TM, PassManagerBase &PM) |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 67 | : TargetPassConfig(TM, PM) {} |
| 68 | |
| 69 | AVRTargetMachine &getAVRTargetMachine() const { |
| 70 | return getTM<AVRTargetMachine>(); |
| 71 | } |
| 72 | |
| 73 | bool addInstSelector() override; |
| 74 | void addPreSched2() override; |
Dylan McKay | 9cf1dc1 | 2017-07-11 04:17:13 +0000 | [diff] [blame] | 75 | void addPreEmitPass() override; |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 76 | void addPreRegAlloc() override; |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 77 | }; |
| 78 | } // namespace |
| 79 | |
| 80 | TargetPassConfig *AVRTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 81 | return new AVRPassConfig(*this, PM); |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 82 | } |
Dylan McKay | c498ba3 | 2015-11-12 09:26:44 +0000 | [diff] [blame] | 83 | |
| 84 | extern "C" void LLVMInitializeAVRTarget() { |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 85 | // Register the target. |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 86 | RegisterTargetMachine<AVRTargetMachine> X(getTheAVRTarget()); |
Dylan McKay | 8cec7eb | 2016-12-07 11:08:56 +0000 | [diff] [blame] | 87 | |
| 88 | auto &PR = *PassRegistry::getPassRegistry(); |
| 89 | initializeAVRExpandPseudoPass(PR); |
Dylan McKay | 1e57fa4 | 2016-12-13 05:53:14 +0000 | [diff] [blame] | 90 | initializeAVRRelaxMemPass(PR); |
Dylan McKay | c498ba3 | 2015-11-12 09:26:44 +0000 | [diff] [blame] | 91 | } |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 92 | |
| 93 | const AVRSubtarget *AVRTargetMachine::getSubtargetImpl() const { |
| 94 | return &SubTarget; |
| 95 | } |
| 96 | |
| 97 | const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const { |
| 98 | return &SubTarget; |
| 99 | } |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | // Pass Pipeline Configuration |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
| 105 | bool AVRPassConfig::addInstSelector() { |
Dylan McKay | c988b33 | 2016-11-07 06:02:55 +0000 | [diff] [blame] | 106 | // 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 McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
| 114 | void AVRPassConfig::addPreRegAlloc() { |
Dylan McKay | c988b33 | 2016-11-07 06:02:55 +0000 | [diff] [blame] | 115 | // Create the dynalloc SP save/restore pass to handle variable sized allocas. |
| 116 | addPass(createAVRDynAllocaSRPass()); |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Dylan McKay | 1e57fa4 | 2016-12-13 05:53:14 +0000 | [diff] [blame] | 119 | void AVRPassConfig::addPreSched2() { |
| 120 | addPass(createAVRRelaxMemPass()); |
| 121 | addPass(createAVRExpandPseudoPass()); |
| 122 | } |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 123 | |
Dylan McKay | 9cf1dc1 | 2017-07-11 04:17:13 +0000 | [diff] [blame] | 124 | void AVRPassConfig::addPreEmitPass() { |
| 125 | // Must run branch selection immediately preceding the asm printer. |
| 126 | addPass(&BranchRelaxationPassID); |
| 127 | } |
| 128 | |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 129 | } // end of namespace llvm |