blob: 8006b9be32ebe925c5f1ab75427319ffe0685043 [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
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000042// Force static initialization when called from llvm/InitializeAllTargets.h
43namespace llvm {
44 void InitializeARMTarget() { }
45}
46
Anton Korobeynikov74b114b2008-08-17 13:55:10 +000047// No assembler printer by default
48ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
49
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050/// ThumbTargetMachine - Create an Thumb architecture model.
51///
52unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +000053#if defined(__thumb__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 return 10;
55#endif
56 return 0;
57}
58
59unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
60 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-03-09 20:25:39 +000061 // Match thumb-foo-bar, as well as things like thumbv5blah-*
62 if (TT.size() >= 6 &&
63 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 return 20;
65
66 // If the target triple is something non-thumb, we don't match.
67 if (!TT.empty()) return 0;
68
69 if (M.getEndianness() == Module::LittleEndian &&
70 M.getPointerSize() == Module::Pointer32)
71 return 10; // Weak match
72 else if (M.getEndianness() != Module::AnyEndianness ||
73 M.getPointerSize() != Module::AnyPointerSize)
74 return 0; // Match for some other target
75
76 return getJITMatchQuality()/2;
77}
78
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +000079ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 : ARMTargetMachine(M, FS, true) {
81}
82
83/// TargetMachine ctor - Create an ARM architecture model.
84///
85ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
86 bool isThumb)
87 : Subtarget(M, FS, isThumb),
88 DataLayout(Subtarget.isAPCS_ABI() ?
89 // APCS ABI
90 (isThumb ?
91 std::string("e-p:32:32-f64:32:32-i64:32:32-"
92 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
93 std::string("e-p:32:32-f64:32:32-i64:32:32")) :
94 // AAPCS ABI
95 (isThumb ?
96 std::string("e-p:32:32-f64:64:64-i64:64:64-"
97 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
98 std::string("e-p:32:32-f64:64:64-i64:64:64"))),
99 InstrInfo(Subtarget),
100 FrameInfo(Subtarget),
Evan Chengba96b1a2008-11-08 07:38:22 +0000101 JITInfo(),
Evan Cheng88e78d22009-06-19 01:51:50 +0000102 TLInfo(*this),
103 InstrItins(Subtarget.getInstrItineraryData()) {
Evan Chengb8b40d62008-10-30 16:10:54 +0000104 DefRelocModel = getRelocationModel();
105}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000108#if defined(__arm__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return 10;
110#endif
111 return 0;
112}
113
114unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
115 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-03-09 20:25:39 +0000116 // Match arm-foo-bar, as well as things like armv5blah-*
117 if (TT.size() >= 4 &&
Chris Lattneraccc87c2008-05-06 02:29:28 +0000118 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 return 20;
120 // If the target triple is something non-arm, we don't match.
121 if (!TT.empty()) return 0;
122
123 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;
131}
132
133
134const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov3cc6efa2008-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 Korobeynikov3829e8a2008-09-25 21:00:33 +0000141 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000142 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143}
144
145
146// Pass Pipeline Configuration
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000147bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
148 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 PM.add(createARMISelDag(*this));
150 return false;
151}
152
Evan Cheng54353c92009-06-13 09:12:55 +0000153bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
154 CodeGenOpt::Level OptLevel) {
Evan Cheng54353c92009-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
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000161bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000164 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000166
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000167 if (OptLevel != CodeGenOpt::None &&
168 !DisableIfConversion && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 PM.add(createIfConverterPass());
170
171 PM.add(createARMConstantIslandPass());
172 return true;
173}
174
Bill Wendling58ed5d22009-04-29 00:15:41 +0000175bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000176 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000177 bool Verbose,
178 raw_ostream &Out) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 // Output assembly language.
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000180 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
181 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000182 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000183
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 return false;
185}
186
187
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000188bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
189 CodeGenOpt::Level OptLevel,
190 bool DumpAsm,
191 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 // FIXME: Move this to TargetJITInfo!
Evan Chengb8b40d62008-10-30 16:10:54 +0000193 if (DefRelocModel == Reloc::Default)
194 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
196 // Machine code emitter pass for ARM.
197 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000198 if (DumpAsm) {
199 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
200 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000201 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000202 }
203
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 return false;
205}
206
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000207bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
208 CodeGenOpt::Level OptLevel,
209 bool DumpAsm,
210 JITCodeEmitter &JCE) {
211 // 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
Bill Wendling58ed5d22009-04-29 00:15:41 +0000226bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000227 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000228 bool DumpAsm,
229 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Machine code emitter pass for ARM.
231 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000232 if (DumpAsm) {
233 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
234 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000235 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000236 }
237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 return false;
239}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000240
241bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
243 bool DumpAsm,
244 JITCodeEmitter &JCE) {
245 // 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