blob: 7033907bbb01d6545713a153446c8eeff9328dd4 [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
Anton Korobeynikov0bd89712008-08-17 13:55:10 +000045// No assembler printer by default
46ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
47
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
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000077ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
Evan Cheng04321f72007-02-23 03:14:31 +000078 : ARMTargetMachine(M, FS, true) {
79}
80
81/// TargetMachine ctor - Create an ARM architecture model.
82///
83ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
84 bool isThumb)
85 : Subtarget(M, FS, isThumb),
Lauro Ramos Venancio3630e782007-02-13 19:52:28 +000086 DataLayout(Subtarget.isAPCS_ABI() ?
Lauro Ramos Venancio75016052007-02-13 20:06:15 +000087 // APCS ABI
Evan Cheng04321f72007-02-23 03:14:31 +000088 (isThumb ?
Chris Lattnerd2b7cec2007-02-14 05:52:17 +000089 std::string("e-p:32:32-f64:32:32-i64:32:32-"
90 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
91 std::string("e-p:32:32-f64:32:32-i64:32:32")) :
Lauro Ramos Venancio75016052007-02-13 20:06:15 +000092 // AAPCS 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:64:64-i64:64:64-"
95 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
96 std::string("e-p:32:32-f64:64:64-i64:64:64"))),
Evan Chengd44ecd82007-01-22 21:24:13 +000097 InstrInfo(Subtarget),
Evan Chenge8308df2007-03-13 01:20:42 +000098 FrameInfo(Subtarget),
Evan Cheng3cc82232008-11-08 07:38:22 +000099 JITInfo(),
Evan Cheng65f24422008-10-30 16:10:54 +0000100 TLInfo(*this) {
101 DefRelocModel = getRelocationModel();
102}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000103
Evan Cheng148b6a42007-07-05 21:15:40 +0000104unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000105#if defined(__arm__)
Evan Cheng148b6a42007-07-05 21:15:40 +0000106 return 10;
107#endif
108 return 0;
109}
110
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000111unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
112 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +0000113 // Match arm-foo-bar, as well as things like armv5blah-*
114 if (TT.size() >= 4 &&
Chris Lattner3bf6acc2008-05-06 02:29:28 +0000115 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000116 return 20;
Chris Lattner87bdba62007-07-09 17:25:29 +0000117 // If the target triple is something non-arm, we don't match.
118 if (!TT.empty()) return 0;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000119
Evan Cheng148b6a42007-07-05 21:15:40 +0000120 if (M.getEndianness() == Module::LittleEndian &&
121 M.getPointerSize() == Module::Pointer32)
122 return 10; // Weak match
123 else if (M.getEndianness() != Module::AnyEndianness ||
124 M.getPointerSize() != Module::AnyPointerSize)
125 return 0; // Match for some other target
126
127 return getJITMatchQuality()/2;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000128}
129
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000130
Evan Chenga8e29892007-01-19 07:51:42 +0000131const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000132 switch (Subtarget.TargetType) {
133 case ARMSubtarget::isDarwin:
134 return new ARMDarwinTargetAsmInfo(*this);
135 case ARMSubtarget::isELF:
136 return new ARMELFTargetAsmInfo(*this);
137 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000138 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000139 }
Evan Chenga8e29892007-01-19 07:51:42 +0000140}
141
142
Chris Lattner1911fd42006-09-04 04:14:57 +0000143// Pass Pipeline Configuration
Bill Wendling98a366d2009-04-29 23:29:43 +0000144bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
145 CodeGenOpt::Level OptLevel) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000146 PM.add(createARMISelDag(*this));
Chris Lattner1911fd42006-09-04 04:14:57 +0000147 return false;
148}
Rafael Espindola71f3b942006-09-19 15:49:25 +0000149
Evan Chenge7d6df72009-06-13 09:12:55 +0000150bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
151 CodeGenOpt::Level OptLevel) {
152 if (!EnablePreLdStOpti)
153 return false;
154 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
155 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
156 PM.add(createARMLoadStoreOptimizationPass(true));
157 return true;
158}
159
Bill Wendling98a366d2009-04-29 23:29:43 +0000160bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
161 CodeGenOpt::Level OptLevel) {
Evan Chenga8e29892007-01-19 07:51:42 +0000162 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling98a366d2009-04-29 23:29:43 +0000163 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000164 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000165
Bill Wendling98a366d2009-04-29 23:29:43 +0000166 if (OptLevel != CodeGenOpt::None &&
167 !DisableIfConversion && !Subtarget.isThumb())
Evan Cheng75604f82007-05-16 20:52:46 +0000168 PM.add(createIfConverterPass());
169
Evan Chenga8e29892007-01-19 07:51:42 +0000170 PM.add(createARMConstantIslandPass());
Rafael Espindola71f3b942006-09-19 15:49:25 +0000171 return true;
172}
173
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000174bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000175 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000176 bool Verbose,
177 raw_ostream &Out) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000178 // Output assembly language.
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000179 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
180 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000181 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000182
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000183 return false;
184}
Evan Cheng148b6a42007-07-05 21:15:40 +0000185
186
Bill Wendling98a366d2009-04-29 23:29:43 +0000187bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
188 CodeGenOpt::Level OptLevel,
189 bool DumpAsm,
190 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000191 // FIXME: Move this to TargetJITInfo!
Evan Cheng65f24422008-10-30 16:10:54 +0000192 if (DefRelocModel == Reloc::Default)
193 setRelocationModel(Reloc::Static);
Evan Cheng148b6a42007-07-05 21:15:40 +0000194
195 // Machine code emitter pass for ARM.
196 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000197 if (DumpAsm) {
198 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
199 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000200 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000201 }
202
Evan Cheng148b6a42007-07-05 21:15:40 +0000203 return false;
204}
205
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000206bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
207 CodeGenOpt::Level OptLevel,
208 bool DumpAsm,
209 JITCodeEmitter &JCE) {
210 // FIXME: Move this to TargetJITInfo!
211 if (DefRelocModel == Reloc::Default)
212 setRelocationModel(Reloc::Static);
213
214 // Machine code emitter pass for ARM.
215 PM.add(createARMJITCodeEmitterPass(*this, JCE));
216 if (DumpAsm) {
217 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
218 if (AsmPrinterCtor)
219 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
220 }
221
222 return false;
223}
224
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000225bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000226 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000227 bool DumpAsm,
228 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000229 // Machine code emitter pass for ARM.
230 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000231 if (DumpAsm) {
232 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
233 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000234 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000235 }
236
Evan Cheng148b6a42007-07-05 21:15:40 +0000237 return false;
238}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000239
240bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
241 CodeGenOpt::Level OptLevel,
242 bool DumpAsm,
243 JITCodeEmitter &JCE) {
244 // Machine code emitter pass for ARM.
245 PM.add(createARMJITCodeEmitterPass(*this, JCE));
246 if (DumpAsm) {
247 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
248 if (AsmPrinterCtor)
249 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
250 }
251
252 return false;
253}
254
255