Add missing dependencies to the CMake build system.
[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> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
27                               cl::desc("Disable load store optimization pass"));
28 static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
29                               cl::desc("Disable if-conversion pass"));
30
31 /// 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.
35 extern "C" int ARMTargetMachineModule;
36 int ARMTargetMachineModule = 0;
37
38 // Register the target.
39 static RegisterTarget<ARMTargetMachine>   X("arm",   "ARM");
40 static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
41
42 // Force static initialization.
43 extern "C" void LLVMInitializeARMTarget() { }
44
45 // No assembler printer by default
46 ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
47
48 /// ThumbTargetMachine - Create an Thumb architecture model.
49 ///
50 unsigned ThumbTargetMachine::getJITMatchQuality() {
51 #if defined(__thumb__)
52   return 10;
53 #endif
54   return 0;
55 }
56
57 unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
58   std::string TT = M.getTargetTriple();
59   // 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"))
62     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
77 ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
78   : ARMTargetMachine(M, FS, true) {
79 }
80
81 /// TargetMachine ctor - Create an ARM architecture model.
82 ///
83 ARMTargetMachine::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),
99     JITInfo(),
100     TLInfo(*this),
101     InstrItins(Subtarget.getInstrItineraryData()) {
102   DefRelocModel = getRelocationModel();
103 }
104
105 unsigned ARMTargetMachine::getJITMatchQuality() {
106 #if defined(__arm__)
107   return 10;
108 #endif
109   return 0;
110 }
111
112 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
113   std::string TT = M.getTargetTriple();
114   // Match arm-foo-bar, as well as things like armv5blah-*
115   if (TT.size() >= 4 &&
116       (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
117     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
132 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
133   switch (Subtarget.TargetType) {
134    case ARMSubtarget::isDarwin:
135     return new ARMDarwinTargetAsmInfo(*this);
136    case ARMSubtarget::isELF:
137     return new ARMELFTargetAsmInfo(*this);
138    default:
139     return new ARMGenericTargetAsmInfo(*this);
140   }
141 }
142
143
144 // Pass Pipeline Configuration
145 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
146                                        CodeGenOpt::Level OptLevel) {
147   PM.add(createARMISelDag(*this));
148   return false;
149 }
150
151 bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
152                                       CodeGenOpt::Level OptLevel) {
153   // 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
159 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
160                                       CodeGenOpt::Level OptLevel) {
161   // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
162   if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
163     PM.add(createARMLoadStoreOptimizationPass());
164
165   if (OptLevel != CodeGenOpt::None &&
166       !DisableIfConversion && !Subtarget.isThumb())
167     PM.add(createIfConverterPass());
168
169   PM.add(createARMConstantIslandPass());
170   return true;
171 }
172
173 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
174                                           CodeGenOpt::Level OptLevel,
175                                           bool Verbose,
176                                           raw_ostream &Out) {
177   // Output assembly language.
178   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
179   if (AsmPrinterCtor)
180     PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
181
182   return false;
183 }
184
185
186 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
187                                       CodeGenOpt::Level OptLevel,
188                                       bool DumpAsm,
189                                       MachineCodeEmitter &MCE) {
190   // FIXME: Move this to TargetJITInfo!
191   if (DefRelocModel == Reloc::Default)
192     setRelocationModel(Reloc::Static);
193
194   // Machine code emitter pass for ARM.
195   PM.add(createARMCodeEmitterPass(*this, MCE));
196   if (DumpAsm) {
197     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
198     if (AsmPrinterCtor)
199       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
200   }
201
202   return false;
203 }
204
205 bool 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
224 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
225                                             CodeGenOpt::Level OptLevel,
226                                             bool DumpAsm,
227                                             MachineCodeEmitter &MCE) {
228   // Machine code emitter pass for ARM.
229   PM.add(createARMCodeEmitterPass(*this, MCE));
230   if (DumpAsm) {
231     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
232     if (AsmPrinterCtor)
233       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
234   }
235
236   return false;
237 }
238
239 bool 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