blob: b3faadafc5a6fdaa9ed01e1ca720eb2ae2cdaef3 [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"
Owen Andersoncb371882008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.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.
Dan Gohmanb8cab922008-10-14 20:25:08 +000039static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
40static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000041
Bob Wilsona96751f2009-06-23 23:59:40 +000042// Force static initialization.
43extern "C" void LLVMInitializeARMTarget() { }
Douglas Gregor1555a232009-06-16 20:12:29 +000044
Anton Korobeynikov0bd89712008-08-17 13:55:10 +000045// No assembler printer by default
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000046ARMBaseTargetMachine::AsmPrinterCtorFn ARMBaseTargetMachine::AsmPrinterCtor = 0;
Anton Korobeynikov0bd89712008-08-17 13:55:10 +000047
Evan Cheng04321f72007-02-23 03:14:31 +000048/// ThumbTargetMachine - Create an Thumb architecture model.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000049///
Evan Cheng148b6a42007-07-05 21:15:40 +000050unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +000051#if defined(__thumb__)
Evan Cheng148b6a42007-07-05 21:15:40 +000052 return 10;
53#endif
54 return 0;
55}
56
Evan Cheng04321f72007-02-23 03:14:31 +000057unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
58 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +000059 // Match thumb-foo-bar, as well as things like thumbv5blah-*
60 if (TT.size() >= 6 &&
61 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
Evan Cheng04321f72007-02-23 03:14:31 +000062 return 20;
63
Chris Lattner87bdba62007-07-09 17:25:29 +000064 // If the target triple is something non-thumb, we don't match.
65 if (!TT.empty()) return 0;
66
Evan Cheng148b6a42007-07-05 21:15:40 +000067 if (M.getEndianness() == Module::LittleEndian &&
68 M.getPointerSize() == Module::Pointer32)
69 return 10; // Weak match
70 else if (M.getEndianness() != Module::AnyEndianness ||
71 M.getPointerSize() != Module::AnyPointerSize)
72 return 0; // Match for some other target
73
74 return getJITMatchQuality()/2;
Evan Cheng04321f72007-02-23 03:14:31 +000075}
76
Evan Cheng04321f72007-02-23 03:14:31 +000077/// TargetMachine ctor - Create an ARM architecture model.
78///
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000079ARMBaseTargetMachine::ARMBaseTargetMachine(const Module &M,
80 const std::string &FS,
81 bool isThumb)
Evan Cheng04321f72007-02-23 03:14:31 +000082 : Subtarget(M, FS, isThumb),
Evan Chenge8308df2007-03-13 01:20:42 +000083 FrameInfo(Subtarget),
Evan Cheng3cc82232008-11-08 07:38:22 +000084 JITInfo(),
Evan Cheng8557c2b2009-06-19 01:51:50 +000085 InstrItins(Subtarget.getInstrItineraryData()) {
Evan Cheng65f24422008-10-30 16:10:54 +000086 DefRelocModel = getRelocationModel();
87}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000088
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000089ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS)
90 : ARMBaseTargetMachine(M, FS, false), InstrInfo(Subtarget),
91 DataLayout(Subtarget.isAPCS_ABI() ?
92 std::string("e-p:32:32-f64:32:32-i64:32:32") :
93 std::string("e-p:32:32-f64:64:64-i64:64:64")),
94 TLInfo(*this) {
95}
96
97ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
David Goodwinb50ea5c2009-07-02 22:18:33 +000098 : ARMBaseTargetMachine(M, FS, true),
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000099 DataLayout(Subtarget.isAPCS_ABI() ?
100 std::string("e-p:32:32-f64:32:32-i64:32:32-"
101 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
102 std::string("e-p:32:32-f64:64:64-i64:64:64-"
103 "i16:16:32-i8:8:32-i1:8:32-a:0:32")),
104 TLInfo(*this) {
David Goodwinb50ea5c2009-07-02 22:18:33 +0000105 // Create the approriate type of Thumb InstrInfo
106 if (Subtarget.hasThumb2())
107 InstrInfo = new Thumb2InstrInfo(Subtarget);
108 else
109 InstrInfo = new Thumb1InstrInfo(Subtarget);
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000110}
111
Evan Cheng148b6a42007-07-05 21:15:40 +0000112unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000113#if defined(__arm__)
Evan Cheng148b6a42007-07-05 21:15:40 +0000114 return 10;
115#endif
116 return 0;
117}
118
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000119unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
120 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +0000121 // Match arm-foo-bar, as well as things like armv5blah-*
122 if (TT.size() >= 4 &&
Chris Lattner3bf6acc2008-05-06 02:29:28 +0000123 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000124 return 20;
Chris Lattner87bdba62007-07-09 17:25:29 +0000125 // If the target triple is something non-arm, we don't match.
126 if (!TT.empty()) return 0;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000127
Evan Cheng148b6a42007-07-05 21:15:40 +0000128 if (M.getEndianness() == Module::LittleEndian &&
129 M.getPointerSize() == Module::Pointer32)
130 return 10; // Weak match
131 else if (M.getEndianness() != Module::AnyEndianness ||
132 M.getPointerSize() != Module::AnyPointerSize)
133 return 0; // Match for some other target
134
135 return getJITMatchQuality()/2;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000136}
137
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000138
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000139const TargetAsmInfo *ARMBaseTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000140 switch (Subtarget.TargetType) {
141 case ARMSubtarget::isDarwin:
142 return new ARMDarwinTargetAsmInfo(*this);
143 case ARMSubtarget::isELF:
144 return new ARMELFTargetAsmInfo(*this);
145 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000146 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000147 }
Evan Chenga8e29892007-01-19 07:51:42 +0000148}
149
150
Chris Lattner1911fd42006-09-04 04:14:57 +0000151// Pass Pipeline Configuration
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000152bool ARMBaseTargetMachine::addInstSelector(PassManagerBase &PM,
153 CodeGenOpt::Level OptLevel) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000154 PM.add(createARMISelDag(*this));
Chris Lattner1911fd42006-09-04 04:14:57 +0000155 return false;
156}
Rafael Espindola71f3b942006-09-19 15:49:25 +0000157
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000158bool ARMBaseTargetMachine::addPreRegAlloc(PassManagerBase &PM,
159 CodeGenOpt::Level OptLevel) {
Evan Chenge7d6df72009-06-13 09:12:55 +0000160 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
161 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
162 PM.add(createARMLoadStoreOptimizationPass(true));
163 return true;
164}
165
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000166bool ARMBaseTargetMachine::addPreEmitPass(PassManagerBase &PM,
167 CodeGenOpt::Level OptLevel) {
Evan Chenga8e29892007-01-19 07:51:42 +0000168 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling98a366d2009-04-29 23:29:43 +0000169 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000170 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000171
Bill Wendling98a366d2009-04-29 23:29:43 +0000172 if (OptLevel != CodeGenOpt::None &&
173 !DisableIfConversion && !Subtarget.isThumb())
Evan Cheng75604f82007-05-16 20:52:46 +0000174 PM.add(createIfConverterPass());
175
Evan Chenga8e29892007-01-19 07:51:42 +0000176 PM.add(createARMConstantIslandPass());
Rafael Espindola71f3b942006-09-19 15:49:25 +0000177 return true;
178}
179
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000180bool ARMBaseTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
181 CodeGenOpt::Level OptLevel,
182 bool Verbose,
183 raw_ostream &Out) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000184 // Output assembly language.
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000185 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
186 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000187 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000188
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000189 return false;
190}
Evan Cheng148b6a42007-07-05 21:15:40 +0000191
192
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000193bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
194 CodeGenOpt::Level OptLevel,
195 bool DumpAsm,
196 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000197 // FIXME: Move this to TargetJITInfo!
Evan Cheng65f24422008-10-30 16:10:54 +0000198 if (DefRelocModel == Reloc::Default)
199 setRelocationModel(Reloc::Static);
Evan Cheng148b6a42007-07-05 21:15:40 +0000200
201 // Machine code emitter pass for ARM.
202 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000203 if (DumpAsm) {
204 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
205 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000206 PM.add(AsmPrinterCtor(errs(), *this, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000207 }
208
Evan Cheng148b6a42007-07-05 21:15:40 +0000209 return false;
210}
211
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000212bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
213 CodeGenOpt::Level OptLevel,
214 bool DumpAsm,
215 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000216 // FIXME: Move this to TargetJITInfo!
217 if (DefRelocModel == Reloc::Default)
218 setRelocationModel(Reloc::Static);
219
220 // Machine code emitter pass for ARM.
221 PM.add(createARMJITCodeEmitterPass(*this, JCE));
222 if (DumpAsm) {
223 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
224 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000225 PM.add(AsmPrinterCtor(errs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000226 }
227
228 return false;
229}
230
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000231bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
232 CodeGenOpt::Level OptLevel,
233 bool DumpAsm,
234 ObjectCodeEmitter &OCE) {
235 // FIXME: Move this to TargetJITInfo!
236 if (DefRelocModel == Reloc::Default)
237 setRelocationModel(Reloc::Static);
238
239 // Machine code emitter pass for ARM.
240 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
241 if (DumpAsm) {
242 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
243 if (AsmPrinterCtor)
244 PM.add(AsmPrinterCtor(errs(), *this, true));
245 }
246
247 return false;
248}
249
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000250bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
251 CodeGenOpt::Level OptLevel,
252 bool DumpAsm,
253 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000254 // Machine code emitter pass for ARM.
255 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000256 if (DumpAsm) {
257 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
258 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000259 PM.add(AsmPrinterCtor(errs(), *this, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000260 }
261
Evan Cheng148b6a42007-07-05 21:15:40 +0000262 return false;
263}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000264
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000265bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
266 CodeGenOpt::Level OptLevel,
267 bool DumpAsm,
268 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000269 // Machine code emitter pass for ARM.
270 PM.add(createARMJITCodeEmitterPass(*this, JCE));
271 if (DumpAsm) {
272 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
273 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000274 PM.add(AsmPrinterCtor(errs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000275 }
276
277 return false;
278}
279
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000280bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
281 CodeGenOpt::Level OptLevel,
282 bool DumpAsm,
283 ObjectCodeEmitter &OCE) {
284 // Machine code emitter pass for ARM.
285 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
286 if (DumpAsm) {
287 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
288 if (AsmPrinterCtor)
289 PM.add(AsmPrinterCtor(errs(), *this, true));
290 }
291
292 return false;
293}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000294