blob: dec023231e1ae6604aa8053c2a5c260d73d90be6 [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 Chenge7d6df72009-06-13 09:12:55 +000026static cl::opt<bool>
27EnablePreLdStOpti("arm-pre-alloc-loadstore-opti", cl::Hidden,
28 cl::desc("Enable pre-regalloc load store optimization pass"));
Evan Chenga8e29892007-01-19 07:51:42 +000029static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
30 cl::desc("Disable load store optimization pass"));
Evan Cheng17207dd2007-09-20 00:48:22 +000031static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
32 cl::desc("Disable if-conversion pass"));
Evan Chenga8e29892007-01-19 07:51:42 +000033
Oscar Fuentes92adc192008-11-15 21:36:30 +000034/// ARMTargetMachineModule - Note that this is used on hosts that cannot link
35/// in a library unless there are references into the library. In particular,
36/// it seems that it is not possible to get things to work on Win32 without
37/// this. Though it is unused, do not remove it.
38extern "C" int ARMTargetMachineModule;
39int ARMTargetMachineModule = 0;
40
Dan Gohman844731a2008-05-13 00:00:25 +000041// Register the target.
Dan Gohmanb8cab922008-10-14 20:25:08 +000042static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
43static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044
Douglas Gregor1555a232009-06-16 20:12:29 +000045// Force static initialization when called from llvm/InitializeAllTargets.h
46namespace llvm {
47 void InitializeARMTarget() { }
48}
49
Anton Korobeynikov0bd89712008-08-17 13:55:10 +000050// No assembler printer by default
51ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
52
Evan Cheng04321f72007-02-23 03:14:31 +000053/// ThumbTargetMachine - Create an Thumb architecture model.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000054///
Evan Cheng148b6a42007-07-05 21:15:40 +000055unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +000056#if defined(__thumb__)
Evan Cheng148b6a42007-07-05 21:15:40 +000057 return 10;
58#endif
59 return 0;
60}
61
Evan Cheng04321f72007-02-23 03:14:31 +000062unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
63 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +000064 // Match thumb-foo-bar, as well as things like thumbv5blah-*
65 if (TT.size() >= 6 &&
66 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
Evan Cheng04321f72007-02-23 03:14:31 +000067 return 20;
68
Chris Lattner87bdba62007-07-09 17:25:29 +000069 // If the target triple is something non-thumb, we don't match.
70 if (!TT.empty()) return 0;
71
Evan Cheng148b6a42007-07-05 21:15:40 +000072 if (M.getEndianness() == Module::LittleEndian &&
73 M.getPointerSize() == Module::Pointer32)
74 return 10; // Weak match
75 else if (M.getEndianness() != Module::AnyEndianness ||
76 M.getPointerSize() != Module::AnyPointerSize)
77 return 0; // Match for some other target
78
79 return getJITMatchQuality()/2;
Evan Cheng04321f72007-02-23 03:14:31 +000080}
81
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000082ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
Evan Cheng04321f72007-02-23 03:14:31 +000083 : ARMTargetMachine(M, FS, true) {
84}
85
86/// TargetMachine ctor - Create an ARM architecture model.
87///
88ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
89 bool isThumb)
90 : Subtarget(M, FS, isThumb),
Lauro Ramos Venancio3630e782007-02-13 19:52:28 +000091 DataLayout(Subtarget.isAPCS_ABI() ?
Lauro Ramos Venancio75016052007-02-13 20:06:15 +000092 // APCS ABI
Evan Cheng04321f72007-02-23 03:14:31 +000093 (isThumb ?
Chris Lattnerd2b7cec2007-02-14 05:52:17 +000094 std::string("e-p:32:32-f64:32:32-i64:32:32-"
95 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
96 std::string("e-p:32:32-f64:32:32-i64:32:32")) :
Lauro Ramos Venancio75016052007-02-13 20:06:15 +000097 // AAPCS ABI
Evan Cheng04321f72007-02-23 03:14:31 +000098 (isThumb ?
Chris Lattnerd2b7cec2007-02-14 05:52:17 +000099 std::string("e-p:32:32-f64:64:64-i64:64:64-"
100 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
101 std::string("e-p:32:32-f64:64:64-i64:64:64"))),
Evan Chengd44ecd82007-01-22 21:24:13 +0000102 InstrInfo(Subtarget),
Evan Chenge8308df2007-03-13 01:20:42 +0000103 FrameInfo(Subtarget),
Evan Cheng3cc82232008-11-08 07:38:22 +0000104 JITInfo(),
Evan Cheng8557c2b2009-06-19 01:51:50 +0000105 TLInfo(*this),
106 InstrItins(Subtarget.getInstrItineraryData()) {
Evan Cheng65f24422008-10-30 16:10:54 +0000107 DefRelocModel = getRelocationModel();
108}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000109
Evan Cheng148b6a42007-07-05 21:15:40 +0000110unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000111#if defined(__arm__)
Evan Cheng148b6a42007-07-05 21:15:40 +0000112 return 10;
113#endif
114 return 0;
115}
116
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000117unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
118 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +0000119 // Match arm-foo-bar, as well as things like armv5blah-*
120 if (TT.size() >= 4 &&
Chris Lattner3bf6acc2008-05-06 02:29:28 +0000121 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000122 return 20;
Chris Lattner87bdba62007-07-09 17:25:29 +0000123 // If the target triple is something non-arm, we don't match.
124 if (!TT.empty()) return 0;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000125
Evan Cheng148b6a42007-07-05 21:15:40 +0000126 if (M.getEndianness() == Module::LittleEndian &&
127 M.getPointerSize() == Module::Pointer32)
128 return 10; // Weak match
129 else if (M.getEndianness() != Module::AnyEndianness ||
130 M.getPointerSize() != Module::AnyPointerSize)
131 return 0; // Match for some other target
132
133 return getJITMatchQuality()/2;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000134}
135
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000136
Evan Chenga8e29892007-01-19 07:51:42 +0000137const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000138 switch (Subtarget.TargetType) {
139 case ARMSubtarget::isDarwin:
140 return new ARMDarwinTargetAsmInfo(*this);
141 case ARMSubtarget::isELF:
142 return new ARMELFTargetAsmInfo(*this);
143 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000144 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000145 }
Evan Chenga8e29892007-01-19 07:51:42 +0000146}
147
148
Chris Lattner1911fd42006-09-04 04:14:57 +0000149// Pass Pipeline Configuration
Bill Wendling98a366d2009-04-29 23:29:43 +0000150bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
151 CodeGenOpt::Level OptLevel) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000152 PM.add(createARMISelDag(*this));
Chris Lattner1911fd42006-09-04 04:14:57 +0000153 return false;
154}
Rafael Espindola71f3b942006-09-19 15:49:25 +0000155
Evan Chenge7d6df72009-06-13 09:12:55 +0000156bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
157 CodeGenOpt::Level OptLevel) {
158 if (!EnablePreLdStOpti)
159 return false;
160 // 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
Bill Wendling98a366d2009-04-29 23:29:43 +0000166bool ARMTargetMachine::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
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000180bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000181 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000182 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)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000187 PM.add(AsmPrinterCtor(Out, *this, OptLevel, 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
Bill Wendling98a366d2009-04-29 23:29:43 +0000193bool ARMTargetMachine::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)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000206 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000207 }
208
Evan Cheng148b6a42007-07-05 21:15:40 +0000209 return false;
210}
211
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000212bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
213 CodeGenOpt::Level OptLevel,
214 bool DumpAsm,
215 JITCodeEmitter &JCE) {
216 // 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)
225 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
226 }
227
228 return false;
229}
230
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000231bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000232 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000233 bool DumpAsm,
234 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000235 // Machine code emitter pass for ARM.
236 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000237 if (DumpAsm) {
238 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
239 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000240 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000241 }
242
Evan Cheng148b6a42007-07-05 21:15:40 +0000243 return false;
244}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000245
246bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
247 CodeGenOpt::Level OptLevel,
248 bool DumpAsm,
249 JITCodeEmitter &JCE) {
250 // Machine code emitter pass for ARM.
251 PM.add(createARMJITCodeEmitterPass(*this, JCE));
252 if (DumpAsm) {
253 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
254 if (AsmPrinterCtor)
255 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
256 }
257
258 return false;
259}
260
261