blob: bb760fd9a68eb111923046765f0967d2bc0caac1 [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#include "ARMTargetMachine.h"
Evan Chenga8e29892007-01-19 07:51:42 +000014#include "ARMTargetAsmInfo.h"
Rafael Espindolaec46ea32006-08-16 14:43:33 +000015#include "ARMFrameInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000016#include "ARM.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "llvm/Module.h"
18#include "llvm/PassManager.h"
Evan Cheng93072922007-05-16 02:01:49 +000019#include "llvm/CodeGen/Passes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000022#include "llvm/Target/TargetMachineRegistry.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "llvm/Target/TargetOptions.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000024using namespace llvm;
25
Evan Chenga8e29892007-01-19 07:51:42 +000026static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
27 cl::desc("Disable load store optimization pass"));
Evan Cheng17207dd2007-09-20 00:48:22 +000028static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
29 cl::desc("Disable if-conversion pass"));
Evan Chenga8e29892007-01-19 07:51:42 +000030
Oscar Fuentes92adc192008-11-15 21:36:30 +000031/// ARMTargetMachineModule - Note that this is used on hosts that cannot link
32/// in a library unless there are references into the library. In particular,
33/// it seems that it is not possible to get things to work on Win32 without
34/// this. Though it is unused, do not remove it.
35extern "C" int ARMTargetMachineModule;
36int ARMTargetMachineModule = 0;
37
Dan Gohman844731a2008-05-13 00:00:25 +000038// Register the target.
Daniel Dunbar42467902009-07-15 09:22:31 +000039extern Target TheARMTarget;
40static RegisterTarget<ARMTargetMachine> X(TheARMTarget, "arm", "ARM");
41
42extern Target TheThumbTarget;
43static RegisterTarget<ThumbTargetMachine> Y(TheThumbTarget, "thumb", "Thumb");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044
Bob Wilsona96751f2009-06-23 23:59:40 +000045// Force static initialization.
46extern "C" void LLVMInitializeARMTarget() { }
Douglas Gregor1555a232009-06-16 20:12:29 +000047
Anton Korobeynikov0bd89712008-08-17 13:55:10 +000048// No assembler printer by default
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000049ARMBaseTargetMachine::AsmPrinterCtorFn ARMBaseTargetMachine::AsmPrinterCtor = 0;
Anton Korobeynikov0bd89712008-08-17 13:55:10 +000050
Evan Cheng04321f72007-02-23 03:14:31 +000051/// ThumbTargetMachine - Create an Thumb architecture model.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000052///
Evan Cheng148b6a42007-07-05 21:15:40 +000053unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +000054#if defined(__thumb__)
Evan Cheng148b6a42007-07-05 21:15:40 +000055 return 10;
56#endif
57 return 0;
58}
59
Evan Cheng04321f72007-02-23 03:14:31 +000060unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
61 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +000062 // Match thumb-foo-bar, as well as things like thumbv5blah-*
63 if (TT.size() >= 6 &&
64 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
Evan Cheng04321f72007-02-23 03:14:31 +000065 return 20;
66
Chris Lattner87bdba62007-07-09 17:25:29 +000067 // If the target triple is something non-thumb, we don't match.
68 if (!TT.empty()) return 0;
69
Evan Cheng148b6a42007-07-05 21:15:40 +000070 if (M.getEndianness() == Module::LittleEndian &&
71 M.getPointerSize() == Module::Pointer32)
72 return 10; // Weak match
73 else if (M.getEndianness() != Module::AnyEndianness ||
74 M.getPointerSize() != Module::AnyPointerSize)
75 return 0; // Match for some other target
76
77 return getJITMatchQuality()/2;
Evan Cheng04321f72007-02-23 03:14:31 +000078}
79
Evan Cheng04321f72007-02-23 03:14:31 +000080/// TargetMachine ctor - Create an ARM architecture model.
81///
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000082ARMBaseTargetMachine::ARMBaseTargetMachine(const Module &M,
83 const std::string &FS,
84 bool isThumb)
Evan Cheng04321f72007-02-23 03:14:31 +000085 : Subtarget(M, FS, isThumb),
Evan Chenge8308df2007-03-13 01:20:42 +000086 FrameInfo(Subtarget),
Evan Cheng3cc82232008-11-08 07:38:22 +000087 JITInfo(),
Evan Cheng8557c2b2009-06-19 01:51:50 +000088 InstrItins(Subtarget.getInstrItineraryData()) {
Evan Cheng65f24422008-10-30 16:10:54 +000089 DefRelocModel = getRelocationModel();
90}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000091
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000092ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS)
93 : ARMBaseTargetMachine(M, FS, false), InstrInfo(Subtarget),
94 DataLayout(Subtarget.isAPCS_ABI() ?
95 std::string("e-p:32:32-f64:32:32-i64:32:32") :
96 std::string("e-p:32:32-f64:64:64-i64:64:64")),
97 TLInfo(*this) {
98}
99
100ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
David Goodwinb50ea5c2009-07-02 22:18:33 +0000101 : ARMBaseTargetMachine(M, FS, true),
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000102 DataLayout(Subtarget.isAPCS_ABI() ?
103 std::string("e-p:32:32-f64:32:32-i64:32:32-"
104 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
105 std::string("e-p:32:32-f64:64:64-i64:64:64-"
106 "i16:16:32-i8:8:32-i1:8:32-a:0:32")),
107 TLInfo(*this) {
David Goodwinb50ea5c2009-07-02 22:18:33 +0000108 // Create the approriate type of Thumb InstrInfo
109 if (Subtarget.hasThumb2())
110 InstrInfo = new Thumb2InstrInfo(Subtarget);
111 else
112 InstrInfo = new Thumb1InstrInfo(Subtarget);
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000113}
114
Evan Cheng148b6a42007-07-05 21:15:40 +0000115unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000116#if defined(__arm__)
Evan Cheng148b6a42007-07-05 21:15:40 +0000117 return 10;
118#endif
119 return 0;
120}
121
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000122unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
123 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +0000124 // Match arm-foo-bar, as well as things like armv5blah-*
125 if (TT.size() >= 4 &&
Chris Lattner3bf6acc2008-05-06 02:29:28 +0000126 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000127 return 20;
Chris Lattner87bdba62007-07-09 17:25:29 +0000128 // If the target triple is something non-arm, we don't match.
129 if (!TT.empty()) return 0;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000130
Evan Cheng148b6a42007-07-05 21:15:40 +0000131 if (M.getEndianness() == Module::LittleEndian &&
132 M.getPointerSize() == Module::Pointer32)
133 return 10; // Weak match
134 else if (M.getEndianness() != Module::AnyEndianness ||
135 M.getPointerSize() != Module::AnyPointerSize)
136 return 0; // Match for some other target
137
138 return getJITMatchQuality()/2;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000139}
140
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000141
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000142const TargetAsmInfo *ARMBaseTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000143 switch (Subtarget.TargetType) {
144 case ARMSubtarget::isDarwin:
145 return new ARMDarwinTargetAsmInfo(*this);
146 case ARMSubtarget::isELF:
147 return new ARMELFTargetAsmInfo(*this);
148 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000149 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000150 }
Evan Chenga8e29892007-01-19 07:51:42 +0000151}
152
153
Chris Lattner1911fd42006-09-04 04:14:57 +0000154// Pass Pipeline Configuration
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000155bool ARMBaseTargetMachine::addInstSelector(PassManagerBase &PM,
156 CodeGenOpt::Level OptLevel) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000157 PM.add(createARMISelDag(*this));
Chris Lattner1911fd42006-09-04 04:14:57 +0000158 return false;
159}
Rafael Espindola71f3b942006-09-19 15:49:25 +0000160
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000161bool ARMBaseTargetMachine::addPreRegAlloc(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
Evan Chenge7d6df72009-06-13 09:12:55 +0000163 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
164 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
165 PM.add(createARMLoadStoreOptimizationPass(true));
166 return true;
167}
168
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000169bool ARMBaseTargetMachine::addPreEmitPass(PassManagerBase &PM,
170 CodeGenOpt::Level OptLevel) {
Evan Chenga8e29892007-01-19 07:51:42 +0000171 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling98a366d2009-04-29 23:29:43 +0000172 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000173 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000174
Bill Wendling98a366d2009-04-29 23:29:43 +0000175 if (OptLevel != CodeGenOpt::None &&
176 !DisableIfConversion && !Subtarget.isThumb())
Evan Cheng75604f82007-05-16 20:52:46 +0000177 PM.add(createIfConverterPass());
178
Evan Cheng06e16582009-07-10 01:54:42 +0000179 if (Subtarget.isThumb2())
180 PM.add(createThumb2ITBlockPass());
181
Evan Chenga8e29892007-01-19 07:51:42 +0000182 PM.add(createARMConstantIslandPass());
Rafael Espindola71f3b942006-09-19 15:49:25 +0000183 return true;
184}
185
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000186bool ARMBaseTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
187 CodeGenOpt::Level OptLevel,
188 bool Verbose,
David Greene71847812009-07-14 20:18:05 +0000189 formatted_raw_ostream &Out) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000190 // Output assembly language.
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000191 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
192 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000193 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000194
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000195 return false;
196}
Evan Cheng148b6a42007-07-05 21:15:40 +0000197
198
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000199bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
200 CodeGenOpt::Level OptLevel,
201 bool DumpAsm,
202 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000203 // FIXME: Move this to TargetJITInfo!
Evan Cheng65f24422008-10-30 16:10:54 +0000204 if (DefRelocModel == Reloc::Default)
205 setRelocationModel(Reloc::Static);
Evan Cheng148b6a42007-07-05 21:15:40 +0000206
207 // Machine code emitter pass for ARM.
208 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000209 if (DumpAsm) {
210 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
211 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000212 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000213 }
214
Evan Cheng148b6a42007-07-05 21:15:40 +0000215 return false;
216}
217
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000218bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
219 CodeGenOpt::Level OptLevel,
220 bool DumpAsm,
221 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000222 // FIXME: Move this to TargetJITInfo!
223 if (DefRelocModel == Reloc::Default)
224 setRelocationModel(Reloc::Static);
225
226 // Machine code emitter pass for ARM.
227 PM.add(createARMJITCodeEmitterPass(*this, JCE));
228 if (DumpAsm) {
229 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
230 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000231 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000232 }
233
234 return false;
235}
236
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000237bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
238 CodeGenOpt::Level OptLevel,
239 bool DumpAsm,
240 ObjectCodeEmitter &OCE) {
241 // FIXME: Move this to TargetJITInfo!
242 if (DefRelocModel == Reloc::Default)
243 setRelocationModel(Reloc::Static);
244
245 // Machine code emitter pass for ARM.
246 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
247 if (DumpAsm) {
248 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
249 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000250 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000251 }
252
253 return false;
254}
255
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000256bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
257 CodeGenOpt::Level OptLevel,
258 bool DumpAsm,
259 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000260 // Machine code emitter pass for ARM.
261 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000262 if (DumpAsm) {
263 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
264 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000265 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000266 }
267
Evan Cheng148b6a42007-07-05 21:15:40 +0000268 return false;
269}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000270
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000271bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
272 CodeGenOpt::Level OptLevel,
273 bool DumpAsm,
274 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000275 // Machine code emitter pass for ARM.
276 PM.add(createARMJITCodeEmitterPass(*this, JCE));
277 if (DumpAsm) {
278 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
279 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000280 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000281 }
282
283 return false;
284}
285
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000286bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
287 CodeGenOpt::Level OptLevel,
288 bool DumpAsm,
289 ObjectCodeEmitter &OCE) {
290 // Machine code emitter pass for ARM.
291 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
292 if (DumpAsm) {
293 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
294 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000295 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000296 }
297
298 return false;
299}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000300