Introduce new headers whose inclusion forces linking and
[oota-llvm.git] / lib / Target / ARM / ARMTargetMachine.cpp
1 //===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetMachineRegistry.h"
23 #include "llvm/Target/TargetOptions.h"
24 using namespace llvm;
25
26 static cl::opt<bool>
27 EnablePreLdStOpti("arm-pre-alloc-loadstore-opti", cl::Hidden,
28                   cl::desc("Enable pre-regalloc load store optimization pass"));
29 static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
30                               cl::desc("Disable load store optimization pass"));
31 static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
32                               cl::desc("Disable if-conversion pass"));
33
34 /// 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.
38 extern "C" int ARMTargetMachineModule;
39 int ARMTargetMachineModule = 0;
40
41 // Register the target.
42 static RegisterTarget<ARMTargetMachine>   X("arm",   "ARM");
43 static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
44
45 // Force static initialization when called from llvm/InitializeAllTargets.h
46 namespace llvm {
47   void InitializeARMTarget() { }
48 }
49
50 // No assembler printer by default
51 ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
52
53 /// ThumbTargetMachine - Create an Thumb architecture model.
54 ///
55 unsigned ThumbTargetMachine::getJITMatchQuality() {
56 #if defined(__thumb__)
57   return 10;
58 #endif
59   return 0;
60 }
61
62 unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
63   std::string TT = M.getTargetTriple();
64   // 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"))
67     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
82 ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
83   : ARMTargetMachine(M, FS, true) {
84 }
85
86 /// TargetMachine ctor - Create an ARM architecture model.
87 ///
88 ARMTargetMachine::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),
104     JITInfo(),
105     TLInfo(*this) {
106   DefRelocModel = getRelocationModel();
107 }
108
109 unsigned ARMTargetMachine::getJITMatchQuality() {
110 #if defined(__arm__)
111   return 10;
112 #endif
113   return 0;
114 }
115
116 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
117   std::string TT = M.getTargetTriple();
118   // Match arm-foo-bar, as well as things like armv5blah-*
119   if (TT.size() >= 4 &&
120       (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
121     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
136 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
137   switch (Subtarget.TargetType) {
138    case ARMSubtarget::isDarwin:
139     return new ARMDarwinTargetAsmInfo(*this);
140    case ARMSubtarget::isELF:
141     return new ARMELFTargetAsmInfo(*this);
142    default:
143     return new ARMGenericTargetAsmInfo(*this);
144   }
145 }
146
147
148 // Pass Pipeline Configuration
149 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
150                                        CodeGenOpt::Level OptLevel) {
151   PM.add(createARMISelDag(*this));
152   return false;
153 }
154
155 bool 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
165 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
166                                       CodeGenOpt::Level OptLevel) {
167   // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
168   if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
169     PM.add(createARMLoadStoreOptimizationPass());
170
171   if (OptLevel != CodeGenOpt::None &&
172       !DisableIfConversion && !Subtarget.isThumb())
173     PM.add(createIfConverterPass());
174
175   PM.add(createARMConstantIslandPass());
176   return true;
177 }
178
179 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
180                                           CodeGenOpt::Level OptLevel,
181                                           bool Verbose,
182                                           raw_ostream &Out) {
183   // Output assembly language.
184   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
185   if (AsmPrinterCtor)
186     PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
187
188   return false;
189 }
190
191
192 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
193                                       CodeGenOpt::Level OptLevel,
194                                       bool DumpAsm,
195                                       MachineCodeEmitter &MCE) {
196   // FIXME: Move this to TargetJITInfo!
197   if (DefRelocModel == Reloc::Default)
198     setRelocationModel(Reloc::Static);
199
200   // Machine code emitter pass for ARM.
201   PM.add(createARMCodeEmitterPass(*this, MCE));
202   if (DumpAsm) {
203     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
204     if (AsmPrinterCtor)
205       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
206   }
207
208   return false;
209 }
210
211 bool 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
230 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
231                                             CodeGenOpt::Level OptLevel,
232                                             bool DumpAsm,
233                                             MachineCodeEmitter &MCE) {
234   // Machine code emitter pass for ARM.
235   PM.add(createARMCodeEmitterPass(*this, MCE));
236   if (DumpAsm) {
237     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
238     if (AsmPrinterCtor)
239       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
240   }
241
242   return false;
243 }
244
245 bool 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