blob: f7b821539df1c9865fef83de0fc6da55c957aed6 [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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +000046ARMBaseTargetMachine::AsmPrinterCtorFn ARMBaseTargetMachine::AsmPrinterCtor = 0;
Anton Korobeynikov74b114b2008-08-17 13:55:10 +000047
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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077/// TargetMachine ctor - Create an ARM architecture model.
78///
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +000079ARMBaseTargetMachine::ARMBaseTargetMachine(const Module &M,
80 const std::string &FS,
81 bool isThumb)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 : Subtarget(M, FS, isThumb),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 FrameInfo(Subtarget),
Evan Chengba96b1a2008-11-08 07:38:22 +000084 JITInfo(),
Evan Cheng88e78d22009-06-19 01:51:50 +000085 InstrItins(Subtarget.getInstrItineraryData()) {
Evan Chengb8b40d62008-10-30 16:10:54 +000086 DefRelocModel = getRelocationModel();
87}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
Anton Korobeynikov65d16ea2009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107unsigned 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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000134const TargetAsmInfo *ARMBaseTargetMachine::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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000147bool ARMBaseTargetMachine::addInstSelector(PassManagerBase &PM,
148 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 PM.add(createARMISelDag(*this));
150 return false;
151}
152
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000153bool ARMBaseTargetMachine::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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000161bool ARMBaseTargetMachine::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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000175bool ARMBaseTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
176 CodeGenOpt::Level OptLevel,
177 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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000188bool ARMBaseTargetMachine::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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000207bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase &PM,
208 CodeGenOpt::Level OptLevel,
209 bool DumpAsm,
210 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-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 Korobeynikov65d16ea2009-06-26 21:28:53 +0000226bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
227 CodeGenOpt::Level OptLevel,
228 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
Anton Korobeynikov65d16ea2009-06-26 21:28:53 +0000241bool ARMBaseTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
243 bool DumpAsm,
244 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-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