blob: f7b821539df1c9865fef83de0fc6da55c957aed6 [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)
98 : ARMBaseTargetMachine(M, FS, true), InstrInfo(Subtarget),
99 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) {
105}
106
Evan Cheng148b6a42007-07-05 21:15:40 +0000107unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000108#if defined(__arm__)
Evan Cheng148b6a42007-07-05 21:15:40 +0000109 return 10;
110#endif
111 return 0;
112}
113
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000114unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
115 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +0000116 // Match arm-foo-bar, as well as things like armv5blah-*
117 if (TT.size() >= 4 &&
Chris Lattner3bf6acc2008-05-06 02:29:28 +0000118 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000119 return 20;
Chris Lattner87bdba62007-07-09 17:25:29 +0000120 // If the target triple is something non-arm, we don't match.
121 if (!TT.empty()) return 0;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000122
Evan Cheng148b6a42007-07-05 21:15:40 +0000123 if (M.getEndianness() == Module::LittleEndian &&
124 M.getPointerSize() == Module::Pointer32)
125 return 10; // Weak match
126 else if (M.getEndianness() != Module::AnyEndianness ||
127 M.getPointerSize() != Module::AnyPointerSize)
128 return 0; // Match for some other target
129
130 return getJITMatchQuality()/2;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000131}
132
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000133
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000134const TargetAsmInfo *ARMBaseTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000135 switch (Subtarget.TargetType) {
136 case ARMSubtarget::isDarwin:
137 return new ARMDarwinTargetAsmInfo(*this);
138 case ARMSubtarget::isELF:
139 return new ARMELFTargetAsmInfo(*this);
140 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000141 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000142 }
Evan Chenga8e29892007-01-19 07:51:42 +0000143}
144
145
Chris Lattner1911fd42006-09-04 04:14:57 +0000146// Pass Pipeline Configuration
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000147bool ARMBaseTargetMachine::addInstSelector(PassManagerBase &PM,
148 CodeGenOpt::Level OptLevel) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000149 PM.add(createARMISelDag(*this));
Chris Lattner1911fd42006-09-04 04:14:57 +0000150 return false;
151}
Rafael Espindola71f3b942006-09-19 15:49:25 +0000152
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000153bool ARMBaseTargetMachine::addPreRegAlloc(PassManagerBase &PM,
154 CodeGenOpt::Level OptLevel) {
Evan Chenge7d6df72009-06-13 09:12:55 +0000155 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
156 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
157 PM.add(createARMLoadStoreOptimizationPass(true));
158 return true;
159}
160
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000161bool ARMBaseTargetMachine::addPreEmitPass(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
Evan Chenga8e29892007-01-19 07:51:42 +0000163 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling98a366d2009-04-29 23:29:43 +0000164 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000165 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000166
Bill Wendling98a366d2009-04-29 23:29:43 +0000167 if (OptLevel != CodeGenOpt::None &&
168 !DisableIfConversion && !Subtarget.isThumb())
Evan Cheng75604f82007-05-16 20:52:46 +0000169 PM.add(createIfConverterPass());
170
Evan Chenga8e29892007-01-19 07:51:42 +0000171 PM.add(createARMConstantIslandPass());
Rafael Espindola71f3b942006-09-19 15:49:25 +0000172 return true;
173}
174
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000175bool ARMBaseTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
176 CodeGenOpt::Level OptLevel,
177 bool Verbose,
178 raw_ostream &Out) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000179 // Output assembly language.
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000180 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
181 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000182 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000183
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000184 return false;
185}
Evan Cheng148b6a42007-07-05 21:15:40 +0000186
187
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000188bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
189 CodeGenOpt::Level OptLevel,
190 bool DumpAsm,
191 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000192 // FIXME: Move this to TargetJITInfo!
Evan Cheng65f24422008-10-30 16:10:54 +0000193 if (DefRelocModel == Reloc::Default)
194 setRelocationModel(Reloc::Static);
Evan Cheng148b6a42007-07-05 21:15:40 +0000195
196 // Machine code emitter pass for ARM.
197 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000198 if (DumpAsm) {
199 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
200 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000201 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000202 }
203
Evan Cheng148b6a42007-07-05 21:15:40 +0000204 return false;
205}
206
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000207bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
208 CodeGenOpt::Level OptLevel,
209 bool DumpAsm,
210 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000211 // FIXME: Move this to TargetJITInfo!
212 if (DefRelocModel == Reloc::Default)
213 setRelocationModel(Reloc::Static);
214
215 // Machine code emitter pass for ARM.
216 PM.add(createARMJITCodeEmitterPass(*this, JCE));
217 if (DumpAsm) {
218 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
219 if (AsmPrinterCtor)
220 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
221 }
222
223 return false;
224}
225
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000226bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
227 CodeGenOpt::Level OptLevel,
228 bool DumpAsm,
229 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000230 // Machine code emitter pass for ARM.
231 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000232 if (DumpAsm) {
233 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
234 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000235 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000236 }
237
Evan Cheng148b6a42007-07-05 21:15:40 +0000238 return false;
239}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000240
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000241bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
243 bool DumpAsm,
244 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000245 // Machine code emitter pass for ARM.
246 PM.add(createARMJITCodeEmitterPass(*this, JCE));
247 if (DumpAsm) {
248 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
249 if (AsmPrinterCtor)
250 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
251 }
252
253 return false;
254}
255
256