blob: 32e8c85698afcb73c728a767832749b5e5beae71 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#include "ARMTargetMachine.h"
14#include "ARMTargetAsmInfo.h"
15#include "ARMFrameInfo.h"
16#include "ARM.h"
17#include "llvm/Module.h"
18#include "llvm/PassManager.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/Support/CommandLine.h"
David Greene302008d2009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetMachineRegistry.h"
23#include "llvm/Target/TargetOptions.h"
24using namespace llvm;
25
26static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
27 cl::desc("Disable load store optimization pass"));
Evan Chengcd497f42007-09-20 00:48:22 +000028static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
29 cl::desc("Disable if-conversion pass"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
Oscar Fuentes4f012352008-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 Gohman089efff2008-05-13 00:00:25 +000038// Register the target.
Daniel Dunbar6fa4f572009-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");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
Bob Wilsonebbc1c42009-06-23 23:59:40 +000045// Force static initialization.
46extern "C" void LLVMInitializeARMTarget() { }
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000047
Anton Korobeynikov74b114b2008-08-17 13:55:10 +000048// No assembler printer by default
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +000049ARMBaseTargetMachine::AsmPrinterCtorFn ARMBaseTargetMachine::AsmPrinterCtor = 0;
Anton Korobeynikov74b114b2008-08-17 13:55:10 +000050
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051/// ThumbTargetMachine - Create an Thumb architecture model.
52///
53unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +000054#if defined(__thumb__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 return 10;
56#endif
57 return 0;
58}
59
60unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
61 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-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"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 return 20;
66
67 // If the target triple is something non-thumb, we don't match.
68 if (!TT.empty()) return 0;
69
70 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;
78}
79
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080/// TargetMachine ctor - Create an ARM architecture model.
81///
Daniel Dunbar8ea70212009-07-15 12:11:05 +000082ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T,
83 const Module &M,
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +000084 const std::string &FS,
85 bool isThumb)
Daniel Dunbar8ea70212009-07-15 12:11:05 +000086 : LLVMTargetMachine(T),
87 Subtarget(M, FS, isThumb),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 FrameInfo(Subtarget),
Evan Chengba96b1a2008-11-08 07:38:22 +000089 JITInfo(),
Evan Cheng88e78d22009-06-19 01:51:50 +000090 InstrItins(Subtarget.getInstrItineraryData()) {
Evan Chengb8b40d62008-10-30 16:10:54 +000091 DefRelocModel = getRelocationModel();
92}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
Daniel Dunbar8ea70212009-07-15 12:11:05 +000094ARMTargetMachine::ARMTargetMachine(const Target &T, const Module &M,
95 const std::string &FS)
96 : ARMBaseTargetMachine(T, M, FS, false), InstrInfo(Subtarget),
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +000097 DataLayout(Subtarget.isAPCS_ABI() ?
98 std::string("e-p:32:32-f64:32:32-i64:32:32") :
99 std::string("e-p:32:32-f64:64:64-i64:64:64")),
100 TLInfo(*this) {
101}
102
Daniel Dunbar8ea70212009-07-15 12:11:05 +0000103ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Module &M,
104 const std::string &FS)
105 : ARMBaseTargetMachine(T, M, FS, true),
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000106 DataLayout(Subtarget.isAPCS_ABI() ?
107 std::string("e-p:32:32-f64:32:32-i64:32:32-"
108 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
109 std::string("e-p:32:32-f64:64:64-i64:64:64-"
110 "i16:16:32-i8:8:32-i1:8:32-a:0:32")),
111 TLInfo(*this) {
David Goodwinaca520d2009-07-02 22:18:33 +0000112 // Create the approriate type of Thumb InstrInfo
113 if (Subtarget.hasThumb2())
114 InstrInfo = new Thumb2InstrInfo(Subtarget);
115 else
116 InstrInfo = new Thumb1InstrInfo(Subtarget);
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000117}
118
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000120#if defined(__arm__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 return 10;
122#endif
123 return 0;
124}
125
126unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
127 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-03-09 20:25:39 +0000128 // Match arm-foo-bar, as well as things like armv5blah-*
129 if (TT.size() >= 4 &&
Chris Lattneraccc87c2008-05-06 02:29:28 +0000130 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 return 20;
132 // If the target triple is something non-arm, we don't match.
133 if (!TT.empty()) return 0;
134
135 if (M.getEndianness() == Module::LittleEndian &&
136 M.getPointerSize() == Module::Pointer32)
137 return 10; // Weak match
138 else if (M.getEndianness() != Module::AnyEndianness ||
139 M.getPointerSize() != Module::AnyPointerSize)
140 return 0; // Match for some other target
141
142 return getJITMatchQuality()/2;
143}
144
145
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000146const TargetAsmInfo *ARMBaseTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000147 switch (Subtarget.TargetType) {
148 case ARMSubtarget::isDarwin:
149 return new ARMDarwinTargetAsmInfo(*this);
150 case ARMSubtarget::isELF:
151 return new ARMELFTargetAsmInfo(*this);
152 default:
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000153 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000154 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155}
156
157
158// Pass Pipeline Configuration
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000159bool ARMBaseTargetMachine::addInstSelector(PassManagerBase &PM,
160 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 PM.add(createARMISelDag(*this));
162 return false;
163}
164
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000165bool ARMBaseTargetMachine::addPreRegAlloc(PassManagerBase &PM,
166 CodeGenOpt::Level OptLevel) {
Evan Cheng54353c92009-06-13 09:12:55 +0000167 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
168 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
169 PM.add(createARMLoadStoreOptimizationPass(true));
170 return true;
171}
172
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000173bool ARMBaseTargetMachine::addPreEmitPass(PassManagerBase &PM,
174 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000176 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000178
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000179 if (OptLevel != CodeGenOpt::None &&
180 !DisableIfConversion && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 PM.add(createIfConverterPass());
182
Evan Chengd5b67fa2009-07-10 01:54:42 +0000183 if (Subtarget.isThumb2())
184 PM.add(createThumb2ITBlockPass());
185
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 PM.add(createARMConstantIslandPass());
187 return true;
188}
189
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000190bool ARMBaseTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
191 CodeGenOpt::Level OptLevel,
192 bool Verbose,
David Greene302008d2009-07-14 20:18:05 +0000193 formatted_raw_ostream &Out) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 // Output assembly language.
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000195 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
196 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000197 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000198
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 return false;
200}
201
202
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000203bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
204 CodeGenOpt::Level OptLevel,
205 bool DumpAsm,
206 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 // FIXME: Move this to TargetJITInfo!
Evan Chengb8b40d62008-10-30 16:10:54 +0000208 if (DefRelocModel == Reloc::Default)
209 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
211 // Machine code emitter pass for ARM.
212 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000213 if (DumpAsm) {
214 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
215 if (AsmPrinterCtor)
David Greene302008d2009-07-14 20:18:05 +0000216 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000217 }
218
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 return false;
220}
221
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000222bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
223 CodeGenOpt::Level OptLevel,
224 bool DumpAsm,
225 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000226 // FIXME: Move this to TargetJITInfo!
227 if (DefRelocModel == Reloc::Default)
228 setRelocationModel(Reloc::Static);
229
230 // Machine code emitter pass for ARM.
231 PM.add(createARMJITCodeEmitterPass(*this, JCE));
232 if (DumpAsm) {
233 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
234 if (AsmPrinterCtor)
David Greene302008d2009-07-14 20:18:05 +0000235 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000236 }
237
238 return false;
239}
240
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000241bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
243 bool DumpAsm,
244 ObjectCodeEmitter &OCE) {
245 // FIXME: Move this to TargetJITInfo!
246 if (DefRelocModel == Reloc::Default)
247 setRelocationModel(Reloc::Static);
248
249 // Machine code emitter pass for ARM.
250 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
251 if (DumpAsm) {
252 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
253 if (AsmPrinterCtor)
David Greene302008d2009-07-14 20:18:05 +0000254 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000255 }
256
257 return false;
258}
259
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000260bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
261 CodeGenOpt::Level OptLevel,
262 bool DumpAsm,
263 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 // Machine code emitter pass for ARM.
265 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000266 if (DumpAsm) {
267 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
268 if (AsmPrinterCtor)
David Greene302008d2009-07-14 20:18:05 +0000269 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000270 }
271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 return false;
273}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000274
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000275bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
276 CodeGenOpt::Level OptLevel,
277 bool DumpAsm,
278 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000279 // Machine code emitter pass for ARM.
280 PM.add(createARMJITCodeEmitterPass(*this, JCE));
281 if (DumpAsm) {
282 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
283 if (AsmPrinterCtor)
David Greene302008d2009-07-14 20:18:05 +0000284 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000285 }
286
287 return false;
288}
289
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000290bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
291 CodeGenOpt::Level OptLevel,
292 bool DumpAsm,
293 ObjectCodeEmitter &OCE) {
294 // Machine code emitter pass for ARM.
295 PM.add(createARMObjectCodeEmitterPass(*this, OCE));
296 if (DumpAsm) {
297 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
298 if (AsmPrinterCtor)
David Greene302008d2009-07-14 20:18:05 +0000299 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000300 }
301
302 return false;
303}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000304