Latency information for ARM v6. It's rough and not yet hooked up. Right now we are...
[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     InstrItins(Subtarget.getInstrItineraryData()) {
107   DefRelocModel = getRelocationModel();
108 }
109
110 unsigned ARMTargetMachine::getJITMatchQuality() {
111 #if defined(__arm__)
112   return 10;
113 #endif
114   return 0;
115 }
116
117 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
118   std::string TT = M.getTargetTriple();
119   // Match arm-foo-bar, as well as things like armv5blah-*
120   if (TT.size() >= 4 &&
121       (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
122     return 20;
123   // If the target triple is something non-arm, we don't match.
124   if (!TT.empty()) return 0;
125
126   if (M.getEndianness()  == Module::LittleEndian &&
127       M.getPointerSize() == Module::Pointer32)
128     return 10;                                   // Weak match
129   else if (M.getEndianness() != Module::AnyEndianness ||
130            M.getPointerSize() != Module::AnyPointerSize)
131     return 0;                                    // Match for some other target
132
133   return getJITMatchQuality()/2;
134 }
135
136
137 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
138   switch (Subtarget.TargetType) {
139    case ARMSubtarget::isDarwin:
140     return new ARMDarwinTargetAsmInfo(*this);
141    case ARMSubtarget::isELF:
142     return new ARMELFTargetAsmInfo(*this);
143    default:
144     return new ARMGenericTargetAsmInfo(*this);
145   }
146 }
147
148
149 // Pass Pipeline Configuration
150 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
151                                        CodeGenOpt::Level OptLevel) {
152   PM.add(createARMISelDag(*this));
153   return false;
154 }
155
156 bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
157                                       CodeGenOpt::Level OptLevel) {
158   if (!EnablePreLdStOpti)
159     return false;
160   // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
161   if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
162     PM.add(createARMLoadStoreOptimizationPass(true));
163   return true;
164 }
165
166 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
167                                       CodeGenOpt::Level OptLevel) {
168   // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
169   if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
170     PM.add(createARMLoadStoreOptimizationPass());
171
172   if (OptLevel != CodeGenOpt::None &&
173       !DisableIfConversion && !Subtarget.isThumb())
174     PM.add(createIfConverterPass());
175
176   PM.add(createARMConstantIslandPass());
177   return true;
178 }
179
180 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
181                                           CodeGenOpt::Level OptLevel,
182                                           bool Verbose,
183                                           raw_ostream &Out) {
184   // Output assembly language.
185   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
186   if (AsmPrinterCtor)
187     PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
188
189   return false;
190 }
191
192
193 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
194                                       CodeGenOpt::Level OptLevel,
195                                       bool DumpAsm,
196                                       MachineCodeEmitter &MCE) {
197   // FIXME: Move this to TargetJITInfo!
198   if (DefRelocModel == Reloc::Default)
199     setRelocationModel(Reloc::Static);
200
201   // Machine code emitter pass for ARM.
202   PM.add(createARMCodeEmitterPass(*this, MCE));
203   if (DumpAsm) {
204     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
205     if (AsmPrinterCtor)
206       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
207   }
208
209   return false;
210 }
211
212 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
213                                       CodeGenOpt::Level OptLevel,
214                                       bool DumpAsm,
215                                       JITCodeEmitter &JCE) {
216   // FIXME: Move this to TargetJITInfo!
217   if (DefRelocModel == Reloc::Default)
218     setRelocationModel(Reloc::Static);
219
220   // Machine code emitter pass for ARM.
221   PM.add(createARMJITCodeEmitterPass(*this, JCE));
222   if (DumpAsm) {
223     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
224     if (AsmPrinterCtor)
225       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
226   }
227
228   return false;
229 }
230
231 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
232                                             CodeGenOpt::Level OptLevel,
233                                             bool DumpAsm,
234                                             MachineCodeEmitter &MCE) {
235   // Machine code emitter pass for ARM.
236   PM.add(createARMCodeEmitterPass(*this, MCE));
237   if (DumpAsm) {
238     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
239     if (AsmPrinterCtor)
240       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
241   }
242
243   return false;
244 }
245
246 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
247                                             CodeGenOpt::Level OptLevel,
248                                             bool DumpAsm,
249                                             JITCodeEmitter &JCE) {
250   // Machine code emitter pass for ARM.
251   PM.add(createARMJITCodeEmitterPass(*this, JCE));
252   if (DumpAsm) {
253     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
254     if (AsmPrinterCtor)
255       PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
256   }
257
258   return false;
259 }
260
261