blob: 35737e7800c2bae6c29fdfbda000aa9467beb098 [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"
Owen Anderson847b99b2008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.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.
Dan Gohman669b9bf2008-10-14 20:25:08 +000039static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
40static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Bob Wilsonebbc1c42009-06-23 23:59:40 +000042// Force static initialization.
43extern "C" void LLVMInitializeARMTarget() { }
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000044
Anton Korobeynikov74b114b2008-08-17 13:55:10 +000045// No assembler printer by default
46ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
47
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048/// ThumbTargetMachine - Create an Thumb architecture model.
49///
50unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +000051#if defined(__thumb__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 return 10;
53#endif
54 return 0;
55}
56
57unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
58 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-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"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 return 20;
63
64 // If the target triple is something non-thumb, we don't match.
65 if (!TT.empty()) return 0;
66
67 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;
75}
76
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +000077ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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),
86 DataLayout(Subtarget.isAPCS_ABI() ?
87 // APCS ABI
88 (isThumb ?
89 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")) :
92 // AAPCS ABI
93 (isThumb ?
94 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"))),
97 InstrInfo(Subtarget),
98 FrameInfo(Subtarget),
Evan Chengba96b1a2008-11-08 07:38:22 +000099 JITInfo(),
Evan Cheng88e78d22009-06-19 01:51:50 +0000100 TLInfo(*this),
101 InstrItins(Subtarget.getInstrItineraryData()) {
Evan Chengb8b40d62008-10-30 16:10:54 +0000102 DefRelocModel = getRelocationModel();
103}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
105unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000106#if defined(__arm__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return 10;
108#endif
109 return 0;
110}
111
112unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
113 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-03-09 20:25:39 +0000114 // Match arm-foo-bar, as well as things like armv5blah-*
115 if (TT.size() >= 4 &&
Chris Lattneraccc87c2008-05-06 02:29:28 +0000116 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 return 20;
118 // If the target triple is something non-arm, we don't match.
119 if (!TT.empty()) return 0;
120
121 if (M.getEndianness() == Module::LittleEndian &&
122 M.getPointerSize() == Module::Pointer32)
123 return 10; // Weak match
124 else if (M.getEndianness() != Module::AnyEndianness ||
125 M.getPointerSize() != Module::AnyPointerSize)
126 return 0; // Match for some other target
127
128 return getJITMatchQuality()/2;
129}
130
131
132const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000133 switch (Subtarget.TargetType) {
134 case ARMSubtarget::isDarwin:
135 return new ARMDarwinTargetAsmInfo(*this);
136 case ARMSubtarget::isELF:
137 return new ARMELFTargetAsmInfo(*this);
138 default:
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000139 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000140 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141}
142
143
144// Pass Pipeline Configuration
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000145bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
146 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 PM.add(createARMISelDag(*this));
148 return false;
149}
150
Evan Cheng54353c92009-06-13 09:12:55 +0000151bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
152 CodeGenOpt::Level OptLevel) {
Evan Cheng54353c92009-06-13 09:12:55 +0000153 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
154 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
155 PM.add(createARMLoadStoreOptimizationPass(true));
156 return true;
157}
158
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000159bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
160 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000162 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000164
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000165 if (OptLevel != CodeGenOpt::None &&
166 !DisableIfConversion && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 PM.add(createIfConverterPass());
168
169 PM.add(createARMConstantIslandPass());
170 return true;
171}
172
Bill Wendling58ed5d22009-04-29 00:15:41 +0000173bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000174 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000175 bool Verbose,
176 raw_ostream &Out) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 // Output assembly language.
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000178 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
179 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000180 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 return false;
183}
184
185
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000186bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
187 CodeGenOpt::Level OptLevel,
188 bool DumpAsm,
189 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 // FIXME: Move this to TargetJITInfo!
Evan Chengb8b40d62008-10-30 16:10:54 +0000191 if (DefRelocModel == Reloc::Default)
192 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
194 // Machine code emitter pass for ARM.
195 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000196 if (DumpAsm) {
197 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
198 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000199 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000200 }
201
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 return false;
203}
204
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000205bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
206 CodeGenOpt::Level OptLevel,
207 bool DumpAsm,
208 JITCodeEmitter &JCE) {
209 // FIXME: Move this to TargetJITInfo!
210 if (DefRelocModel == Reloc::Default)
211 setRelocationModel(Reloc::Static);
212
213 // Machine code emitter pass for ARM.
214 PM.add(createARMJITCodeEmitterPass(*this, JCE));
215 if (DumpAsm) {
216 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
217 if (AsmPrinterCtor)
218 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
219 }
220
221 return false;
222}
223
Bill Wendling58ed5d22009-04-29 00:15:41 +0000224bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000225 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000226 bool DumpAsm,
227 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 // Machine code emitter pass for ARM.
229 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000230 if (DumpAsm) {
231 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
232 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000233 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000234 }
235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 return false;
237}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000238
239bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
240 CodeGenOpt::Level OptLevel,
241 bool DumpAsm,
242 JITCodeEmitter &JCE) {
243 // Machine code emitter pass for ARM.
244 PM.add(createARMJITCodeEmitterPass(*this, JCE));
245 if (DumpAsm) {
246 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
247 if (AsmPrinterCtor)
248 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
249 }
250
251 return false;
252}
253
254