| Chris Lattner | 158e1f5 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 1 | //===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Chris Lattner | 158e1f5 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // | 
|  | 11 | //===----------------------------------------------------------------------===// | 
|  | 12 |  | 
|  | 13 | #include "SparcTargetMachine.h" | 
| Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 14 | #include "LeonPasses.h" | 
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "Sparc.h" | 
|  | 16 | #include "SparcTargetObjectFile.h" | 
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/Passes.h" | 
| Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/TargetPassConfig.h" | 
| Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" | 
| Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TargetRegistry.h" | 
| Chris Lattner | 158e1f5 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 21 | using namespace llvm; | 
|  | 22 |  | 
| Daniel Dunbar | 5680b4f | 2009-07-25 06:49:55 +0000 | [diff] [blame] | 23 | extern "C" void LLVMInitializeSparcTarget() { | 
|  | 24 | // Register the target. | 
| Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 25 | RegisterTargetMachine<SparcV8TargetMachine> X(getTheSparcTarget()); | 
|  | 26 | RegisterTargetMachine<SparcV9TargetMachine> Y(getTheSparcV9Target()); | 
|  | 27 | RegisterTargetMachine<SparcelTargetMachine> Z(getTheSparcelTarget()); | 
| Jim Laskey | ae92ce8 | 2006-09-07 23:39:26 +0000 | [diff] [blame] | 28 | } | 
|  | 29 |  | 
| Douglas Katzman | 9160e78 | 2015-04-29 20:30:57 +0000 | [diff] [blame] | 30 | static std::string computeDataLayout(const Triple &T, bool is64Bit) { | 
|  | 31 | // Sparc is typically big endian, but some are little. | 
|  | 32 | std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E"; | 
|  | 33 | Ret += "-m:e"; | 
| Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | // Some ABIs have 32bit pointers. | 
|  | 36 | if (!is64Bit) | 
|  | 37 | Ret += "-p:32:32"; | 
|  | 38 |  | 
|  | 39 | // Alignments for 64 bit integers. | 
|  | 40 | Ret += "-i64:64"; | 
|  | 41 |  | 
|  | 42 | // On SparcV9 128 floats are aligned to 128 bits, on others only to 64. | 
|  | 43 | // On SparcV9 registers can hold 64 or 32 bits, on others only 32. | 
|  | 44 | if (is64Bit) | 
|  | 45 | Ret += "-n32:64"; | 
|  | 46 | else | 
|  | 47 | Ret += "-f128:64-n32"; | 
|  | 48 |  | 
|  | 49 | if (is64Bit) | 
|  | 50 | Ret += "-S128"; | 
|  | 51 | else | 
|  | 52 | Ret += "-S64"; | 
|  | 53 |  | 
|  | 54 | return Ret; | 
|  | 55 | } | 
|  | 56 |  | 
| Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 57 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { | 
|  | 58 | if (!RM.hasValue()) | 
|  | 59 | return Reloc::Static; | 
|  | 60 | return *RM; | 
|  | 61 | } | 
|  | 62 |  | 
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 63 | // Code models. Some only make sense for 64-bit code. | 
|  | 64 | // | 
|  | 65 | // SunCC  Reloc   CodeModel  Constraints | 
|  | 66 | // abs32  Static  Small      text+data+bss linked below 2^32 bytes | 
|  | 67 | // abs44  Static  Medium     text+data+bss linked below 2^44 bytes | 
|  | 68 | // abs64  Static  Large      text smaller than 2^31 bytes | 
|  | 69 | // pic13  PIC_    Small      GOT < 2^13 bytes | 
|  | 70 | // pic32  PIC_    Medium     GOT < 2^32 bytes | 
|  | 71 | // | 
|  | 72 | // All code models require that the text segment is smaller than 2GB. | 
|  | 73 | static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM, | 
|  | 74 | Reloc::Model RM, bool Is64Bit, | 
|  | 75 | bool JIT) { | 
|  | 76 | if (CM) | 
|  | 77 | return *CM; | 
|  | 78 | if (Is64Bit) { | 
|  | 79 | if (JIT) | 
|  | 80 | return CodeModel::Large; | 
|  | 81 | return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium; | 
|  | 82 | } | 
|  | 83 | return CodeModel::Small; | 
|  | 84 | } | 
|  | 85 |  | 
| Rafael Espindola | 38af4d6 | 2016-05-18 16:00:24 +0000 | [diff] [blame] | 86 | /// Create an ILP32 architecture model | 
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 87 | SparcTargetMachine::SparcTargetMachine( | 
|  | 88 | const Target &T, const Triple &TT, StringRef CPU, StringRef FS, | 
|  | 89 | const TargetOptions &Options, Optional<Reloc::Model> RM, | 
|  | 90 | Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT, bool is64bit) | 
| Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 91 | : LLVMTargetMachine( | 
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 92 | T, computeDataLayout(TT, is64bit), TT, CPU, FS, Options, | 
|  | 93 | getEffectiveRelocModel(RM), | 
|  | 94 | getEffectiveCodeModel(CM, getEffectiveRelocModel(RM), is64bit, JIT), | 
|  | 95 | OL), | 
| Chris Dewhurst | 4f7cac3 | 2016-05-23 10:56:36 +0000 | [diff] [blame] | 96 | TLOF(make_unique<SparcELFTargetObjectFile>()), | 
|  | 97 | Subtarget(TT, CPU, FS, *this, is64bit), is64Bit(is64bit) { | 
| Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 98 | initAsmInfo(); | 
| Chris Lattner | 158e1f5 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Reid Kleckner | 357600e | 2014-11-20 23:37:18 +0000 | [diff] [blame] | 101 | SparcTargetMachine::~SparcTargetMachine() {} | 
|  | 102 |  | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 103 | const SparcSubtarget * | 
| Chris Dewhurst | 68388a0 | 2016-05-18 09:14:13 +0000 | [diff] [blame] | 104 | SparcTargetMachine::getSubtargetImpl(const Function &F) const { | 
|  | 105 | Attribute CPUAttr = F.getFnAttribute("target-cpu"); | 
|  | 106 | Attribute FSAttr = F.getFnAttribute("target-features"); | 
|  | 107 |  | 
|  | 108 | std::string CPU = !CPUAttr.hasAttribute(Attribute::None) | 
|  | 109 | ? CPUAttr.getValueAsString().str() | 
|  | 110 | : TargetCPU; | 
|  | 111 | std::string FS = !FSAttr.hasAttribute(Attribute::None) | 
|  | 112 | ? FSAttr.getValueAsString().str() | 
|  | 113 | : TargetFS; | 
|  | 114 |  | 
|  | 115 | // FIXME: This is related to the code below to reset the target options, | 
|  | 116 | // we need to know whether or not the soft float flag is set on the | 
|  | 117 | // function, so we can enable it as a subtarget feature. | 
|  | 118 | bool softFloat = | 
|  | 119 | F.hasFnAttribute("use-soft-float") && | 
|  | 120 | F.getFnAttribute("use-soft-float").getValueAsString() == "true"; | 
|  | 121 |  | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 122 | if (softFloat) | 
| Chris Dewhurst | 68388a0 | 2016-05-18 09:14:13 +0000 | [diff] [blame] | 123 | FS += FS.empty() ? "+soft-float" : ",+soft-float"; | 
|  | 124 |  | 
|  | 125 | auto &I = SubtargetMap[CPU + FS]; | 
|  | 126 | if (!I) { | 
|  | 127 | // This needs to be done before we create a new subtarget since any | 
|  | 128 | // creation will depend on the TM and the code generation flags on the | 
|  | 129 | // function that reside in TargetOptions. | 
|  | 130 | resetTargetOptions(F); | 
|  | 131 | I = llvm::make_unique<SparcSubtarget>(TargetTriple, CPU, FS, *this, | 
|  | 132 | this->is64Bit); | 
|  | 133 | } | 
|  | 134 | return I.get(); | 
|  | 135 | } | 
|  | 136 |  | 
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 137 | namespace { | 
|  | 138 | /// Sparc Code Generator Pass Configuration Options. | 
|  | 139 | class SparcPassConfig : public TargetPassConfig { | 
|  | 140 | public: | 
| Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 141 | SparcPassConfig(SparcTargetMachine &TM, PassManagerBase &PM) | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 142 | : TargetPassConfig(TM, PM) {} | 
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 143 |  | 
|  | 144 | SparcTargetMachine &getSparcTargetMachine() const { | 
|  | 145 | return getTM<SparcTargetMachine>(); | 
|  | 146 | } | 
|  | 147 |  | 
| Robin Morisset | e2de06b | 2014-10-16 20:34:57 +0000 | [diff] [blame] | 148 | void addIRPasses() override; | 
| Craig Topper | b0c941b | 2014-04-29 07:57:13 +0000 | [diff] [blame] | 149 | bool addInstSelector() override; | 
| Matthias Braun | 7e37a5f | 2014-12-11 21:26:47 +0000 | [diff] [blame] | 150 | void addPreEmitPass() override; | 
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 151 | }; | 
|  | 152 | } // namespace | 
|  | 153 |  | 
| Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 154 | TargetPassConfig *SparcTargetMachine::createPassConfig(PassManagerBase &PM) { | 
| Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 155 | return new SparcPassConfig(*this, PM); | 
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Robin Morisset | e2de06b | 2014-10-16 20:34:57 +0000 | [diff] [blame] | 158 | void SparcPassConfig::addIRPasses() { | 
| Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 159 | addPass(createAtomicExpandPass()); | 
| Robin Morisset | e2de06b | 2014-10-16 20:34:57 +0000 | [diff] [blame] | 160 |  | 
|  | 161 | TargetPassConfig::addIRPasses(); | 
|  | 162 | } | 
|  | 163 |  | 
| Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 164 | bool SparcPassConfig::addInstSelector() { | 
| Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 165 | addPass(createSparcISelDag(getSparcTargetMachine())); | 
| Chris Lattner | 158e1f5 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 166 | return false; | 
|  | 167 | } | 
|  | 168 |  | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 169 | void SparcPassConfig::addPreEmitPass(){ | 
| Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 170 | addPass(createSparcDelaySlotFillerPass()); | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 171 |  | 
|  | 172 | if (this->getSparcTargetMachine().getSubtargetImpl()->insertNOPLoad()) | 
|  | 173 | { | 
| Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 174 | addPass(new InsertNOPLoad()); | 
| Chris Dewhurst | 3202f06 | 2016-07-08 15:33:56 +0000 | [diff] [blame] | 175 | } | 
| Chris Dewhurst | 2c3cdd6 | 2016-10-19 14:01:06 +0000 | [diff] [blame] | 176 | if (this->getSparcTargetMachine().getSubtargetImpl()->detectRoundChange()) { | 
| Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 177 | addPass(new DetectRoundChange()); | 
| Chris Dewhurst | 2c3cdd6 | 2016-10-19 14:01:06 +0000 | [diff] [blame] | 178 | } | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 179 | if (this->getSparcTargetMachine().getSubtargetImpl()->fixAllFDIVSQRT()) | 
|  | 180 | { | 
| Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 181 | addPass(new FixAllFDIVSQRT()); | 
| Chris Dewhurst | 3202f06 | 2016-07-08 15:33:56 +0000 | [diff] [blame] | 182 | } | 
| Chris Lattner | 12e9730 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 183 | } | 
| Chris Lattner | 8228b11 | 2010-02-04 06:34:01 +0000 | [diff] [blame] | 184 |  | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 185 | void SparcV8TargetMachine::anchor() { } | 
| David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 186 |  | 
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 187 | SparcV8TargetMachine::SparcV8TargetMachine(const Target &T, const Triple &TT, | 
|  | 188 | StringRef CPU, StringRef FS, | 
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 189 | const TargetOptions &Options, | 
| Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 190 | Optional<Reloc::Model> RM, | 
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 191 | Optional<CodeModel::Model> CM, | 
|  | 192 | CodeGenOpt::Level OL, bool JIT) | 
|  | 193 | : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {} | 
| Chris Lattner | 8228b11 | 2010-02-04 06:34:01 +0000 | [diff] [blame] | 194 |  | 
| James Y Knight | 2cc9da9 | 2016-08-12 14:48:09 +0000 | [diff] [blame] | 195 | void SparcV9TargetMachine::anchor() { } | 
| David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 196 |  | 
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 197 | SparcV9TargetMachine::SparcV9TargetMachine(const Target &T, const Triple &TT, | 
| Douglas Katzman | 9160e78 | 2015-04-29 20:30:57 +0000 | [diff] [blame] | 198 | StringRef CPU, StringRef FS, | 
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 199 | const TargetOptions &Options, | 
| Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 200 | Optional<Reloc::Model> RM, | 
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 201 | Optional<CodeModel::Model> CM, | 
|  | 202 | CodeGenOpt::Level OL, bool JIT) | 
|  | 203 | : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {} | 
| Douglas Katzman | 9160e78 | 2015-04-29 20:30:57 +0000 | [diff] [blame] | 204 |  | 
|  | 205 | void SparcelTargetMachine::anchor() {} | 
|  | 206 |  | 
| Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 207 | SparcelTargetMachine::SparcelTargetMachine(const Target &T, const Triple &TT, | 
| Douglas Katzman | 9160e78 | 2015-04-29 20:30:57 +0000 | [diff] [blame] | 208 | StringRef CPU, StringRef FS, | 
|  | 209 | const TargetOptions &Options, | 
| Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 210 | Optional<Reloc::Model> RM, | 
| Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 211 | Optional<CodeModel::Model> CM, | 
|  | 212 | CodeGenOpt::Level OL, bool JIT) | 
|  | 213 | : SparcTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {} |