Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 1 | //===-- AVRTargetMachine.cpp - Define TargetMachine for AVR ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 6 | // |
| 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 Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/TargetPassConfig.h" |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 17 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Module.h" |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 19 | #include "llvm/Support/TargetRegistry.h" |
| 20 | |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 21 | #include "AVR.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 22 | #include "AVRTargetObjectFile.h" |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 23 | #include "MCTargetDesc/AVRMCTargetDesc.h" |
Richard Trieu | e982b42 | 2019-05-14 22:41:58 +0000 | [diff] [blame] | 24 | #include "TargetInfo/AVRTargetInfo.h" |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
| 27 | |
Dylan McKay | 9a2a996 | 2018-02-19 10:40:59 +0000 | [diff] [blame] | 28 | static const char *AVRDataLayout = "e-P1-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 | |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 43 | AVRTargetMachine::AVRTargetMachine(const Target &T, const Triple &TT, |
| 44 | StringRef CPU, StringRef FS, |
| 45 | const TargetOptions &Options, |
Dylan McKay | 0547447 | 2017-08-04 05:48:20 +0000 | [diff] [blame] | 46 | Optional<Reloc::Model> RM, |
| 47 | Optional<CodeModel::Model> CM, |
Meador Inge | 70ab7cc | 2017-08-06 12:02:17 +0000 | [diff] [blame] | 48 | CodeGenOpt::Level OL, bool JIT) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 49 | : LLVMTargetMachine(T, AVRDataLayout, TT, getCPU(CPU), FS, Options, |
David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 50 | getEffectiveRelocModel(RM), |
| 51 | getEffectiveCodeModel(CM, CodeModel::Small), OL), |
Dylan McKay | f1f1c01 | 2016-05-18 11:11:38 +0000 | [diff] [blame] | 52 | SubTarget(TT, getCPU(CPU), FS, *this) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 53 | this->TLOF = std::make_unique<AVRTargetObjectFile>(); |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 54 | initAsmInfo(); |
| 55 | } |
| 56 | |
| 57 | namespace { |
| 58 | /// AVR Code Generator Pass Configuration Options. |
| 59 | class AVRPassConfig : public TargetPassConfig { |
| 60 | public: |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 61 | AVRPassConfig(AVRTargetMachine &TM, PassManagerBase &PM) |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 62 | : TargetPassConfig(TM, PM) {} |
| 63 | |
| 64 | AVRTargetMachine &getAVRTargetMachine() const { |
| 65 | return getTM<AVRTargetMachine>(); |
| 66 | } |
| 67 | |
| 68 | bool addInstSelector() override; |
| 69 | void addPreSched2() override; |
Dylan McKay | 9cf1dc1 | 2017-07-11 04:17:13 +0000 | [diff] [blame] | 70 | void addPreEmitPass() override; |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 71 | void addPreRegAlloc() override; |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 72 | }; |
| 73 | } // namespace |
| 74 | |
| 75 | TargetPassConfig *AVRTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 76 | return new AVRPassConfig(*this, PM); |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 77 | } |
Dylan McKay | c498ba3 | 2015-11-12 09:26:44 +0000 | [diff] [blame] | 78 | |
Tom Stellard | 4b0b261 | 2019-06-11 03:21:13 +0000 | [diff] [blame] | 79 | extern "C" void LLVMInitializeAVRTarget() { |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 80 | // Register the target. |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 81 | RegisterTargetMachine<AVRTargetMachine> X(getTheAVRTarget()); |
Dylan McKay | 8cec7eb | 2016-12-07 11:08:56 +0000 | [diff] [blame] | 82 | |
| 83 | auto &PR = *PassRegistry::getPassRegistry(); |
| 84 | initializeAVRExpandPseudoPass(PR); |
Dylan McKay | 1e57fa4 | 2016-12-13 05:53:14 +0000 | [diff] [blame] | 85 | initializeAVRRelaxMemPass(PR); |
Dylan McKay | c498ba3 | 2015-11-12 09:26:44 +0000 | [diff] [blame] | 86 | } |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 87 | |
| 88 | const AVRSubtarget *AVRTargetMachine::getSubtargetImpl() const { |
| 89 | return &SubTarget; |
| 90 | } |
| 91 | |
| 92 | const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const { |
| 93 | return &SubTarget; |
| 94 | } |
| 95 | |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | // Pass Pipeline Configuration |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
| 100 | bool AVRPassConfig::addInstSelector() { |
Dylan McKay | c988b33 | 2016-11-07 06:02:55 +0000 | [diff] [blame] | 101 | // 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 McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
| 109 | void AVRPassConfig::addPreRegAlloc() { |
Dylan McKay | c988b33 | 2016-11-07 06:02:55 +0000 | [diff] [blame] | 110 | // Create the dynalloc SP save/restore pass to handle variable sized allocas. |
| 111 | addPass(createAVRDynAllocaSRPass()); |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Dylan McKay | 1e57fa4 | 2016-12-13 05:53:14 +0000 | [diff] [blame] | 114 | void AVRPassConfig::addPreSched2() { |
| 115 | addPass(createAVRRelaxMemPass()); |
| 116 | addPass(createAVRExpandPseudoPass()); |
| 117 | } |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 118 | |
Dylan McKay | 9cf1dc1 | 2017-07-11 04:17:13 +0000 | [diff] [blame] | 119 | void AVRPassConfig::addPreEmitPass() { |
| 120 | // Must run branch selection immediately preceding the asm printer. |
| 121 | addPass(&BranchRelaxationPassID); |
| 122 | } |
| 123 | |
Dylan McKay | 6d8078f | 2016-05-06 10:12:31 +0000 | [diff] [blame] | 124 | } // end of namespace llvm |