blob: 04dcd93a34499610df4822b6589b50ccd0706a59 [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
Evan Cheng54353c92009-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"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
30 cl::desc("Disable load store optimization pass"));
Evan Chengcd497f42007-09-20 00:48:22 +000031static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
32 cl::desc("Disable if-conversion pass"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033
Oscar Fuentes4f012352008-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 Gohman089efff2008-05-13 00:00:25 +000041// Register the target.
Dan Gohman669b9bf2008-10-14 20:25:08 +000042static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
43static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000045// Force static initialization when called from llvm/InitializeAllTargets.h
46namespace llvm {
47 void InitializeARMTarget() { }
48}
49
Anton Korobeynikov74b114b2008-08-17 13:55:10 +000050// No assembler printer by default
51ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
52
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053/// ThumbTargetMachine - Create an Thumb architecture model.
54///
55unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +000056#if defined(__thumb__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 return 10;
58#endif
59 return 0;
60}
61
62unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
63 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-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"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 return 20;
68
69 // If the target triple is something non-thumb, we don't match.
70 if (!TT.empty()) return 0;
71
72 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;
80}
81
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +000082ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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),
91 DataLayout(Subtarget.isAPCS_ABI() ?
92 // APCS ABI
93 (isThumb ?
94 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")) :
97 // AAPCS ABI
98 (isThumb ?
99 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"))),
102 InstrInfo(Subtarget),
103 FrameInfo(Subtarget),
Evan Chengba96b1a2008-11-08 07:38:22 +0000104 JITInfo(),
Evan Chengb8b40d62008-10-30 16:10:54 +0000105 TLInfo(*this) {
106 DefRelocModel = getRelocationModel();
107}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
109unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000110#if defined(__arm__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return 10;
112#endif
113 return 0;
114}
115
116unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
117 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-03-09 20:25:39 +0000118 // Match arm-foo-bar, as well as things like armv5blah-*
119 if (TT.size() >= 4 &&
Chris Lattneraccc87c2008-05-06 02:29:28 +0000120 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 return 20;
122 // If the target triple is something non-arm, we don't match.
123 if (!TT.empty()) return 0;
124
125 if (M.getEndianness() == Module::LittleEndian &&
126 M.getPointerSize() == Module::Pointer32)
127 return 10; // Weak match
128 else if (M.getEndianness() != Module::AnyEndianness ||
129 M.getPointerSize() != Module::AnyPointerSize)
130 return 0; // Match for some other target
131
132 return getJITMatchQuality()/2;
133}
134
135
136const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000137 switch (Subtarget.TargetType) {
138 case ARMSubtarget::isDarwin:
139 return new ARMDarwinTargetAsmInfo(*this);
140 case ARMSubtarget::isELF:
141 return new ARMELFTargetAsmInfo(*this);
142 default:
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000143 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000144 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145}
146
147
148// Pass Pipeline Configuration
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000149bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
150 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 PM.add(createARMISelDag(*this));
152 return false;
153}
154
Evan Cheng54353c92009-06-13 09:12:55 +0000155bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
156 CodeGenOpt::Level OptLevel) {
157 if (!EnablePreLdStOpti)
158 return false;
159 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
160 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
161 PM.add(createARMLoadStoreOptimizationPass(true));
162 return true;
163}
164
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000165bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
166 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000168 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000170
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000171 if (OptLevel != CodeGenOpt::None &&
172 !DisableIfConversion && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 PM.add(createIfConverterPass());
174
175 PM.add(createARMConstantIslandPass());
176 return true;
177}
178
Bill Wendling58ed5d22009-04-29 00:15:41 +0000179bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000180 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000181 bool Verbose,
182 raw_ostream &Out) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 // Output assembly language.
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000184 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
185 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000186 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 return false;
189}
190
191
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000192bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
193 CodeGenOpt::Level OptLevel,
194 bool DumpAsm,
195 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 // FIXME: Move this to TargetJITInfo!
Evan Chengb8b40d62008-10-30 16:10:54 +0000197 if (DefRelocModel == Reloc::Default)
198 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199
200 // Machine code emitter pass for ARM.
201 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000202 if (DumpAsm) {
203 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
204 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000205 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000206 }
207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 return false;
209}
210
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000211bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
212 CodeGenOpt::Level OptLevel,
213 bool DumpAsm,
214 JITCodeEmitter &JCE) {
215 // FIXME: Move this to TargetJITInfo!
216 if (DefRelocModel == Reloc::Default)
217 setRelocationModel(Reloc::Static);
218
219 // Machine code emitter pass for ARM.
220 PM.add(createARMJITCodeEmitterPass(*this, JCE));
221 if (DumpAsm) {
222 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
223 if (AsmPrinterCtor)
224 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
225 }
226
227 return false;
228}
229
Bill Wendling58ed5d22009-04-29 00:15:41 +0000230bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000231 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000232 bool DumpAsm,
233 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 // Machine code emitter pass for ARM.
235 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000236 if (DumpAsm) {
237 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
238 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000239 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000240 }
241
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 return false;
243}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000244
245bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
246 CodeGenOpt::Level OptLevel,
247 bool DumpAsm,
248 JITCodeEmitter &JCE) {
249 // Machine code emitter pass for ARM.
250 PM.add(createARMJITCodeEmitterPass(*this, JCE));
251 if (DumpAsm) {
252 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
253 if (AsmPrinterCtor)
254 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
255 }
256
257 return false;
258}
259
260