Adds extern "C" ints to the .cpp files that use RegisterTarget, as
[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 // No assembler printer by default
43 ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
44
45 /// ThumbTargetMachine - Create an Thumb architecture model.
46 ///
47 unsigned ThumbTargetMachine::getJITMatchQuality() {
48 #if defined(__thumb__)
49   return 10;
50 #endif
51   return 0;
52 }
53
54 unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
55   std::string TT = M.getTargetTriple();
56   if (TT.size() >= 6 && std::string(TT.begin(), TT.begin()+6) == "thumb-")
57     return 20;
58
59   // If the target triple is something non-thumb, we don't match.
60   if (!TT.empty()) return 0;
61
62   if (M.getEndianness()  == Module::LittleEndian &&
63       M.getPointerSize() == Module::Pointer32)
64     return 10;                                   // Weak match
65   else if (M.getEndianness() != Module::AnyEndianness ||
66            M.getPointerSize() != Module::AnyPointerSize)
67     return 0;                                    // Match for some other target
68
69   return getJITMatchQuality()/2;
70 }
71
72 ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
73   : ARMTargetMachine(M, FS, true) {
74 }
75
76 /// TargetMachine ctor - Create an ARM architecture model.
77 ///
78 ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
79                                    bool isThumb)
80   : Subtarget(M, FS, isThumb),
81     DataLayout(Subtarget.isAPCS_ABI() ?
82                // APCS ABI
83           (isThumb ?
84            std::string("e-p:32:32-f64:32:32-i64:32:32-"
85                        "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
86            std::string("e-p:32:32-f64:32:32-i64:32:32")) :
87                // AAPCS ABI
88           (isThumb ?
89            std::string("e-p:32:32-f64:64:64-i64:64:64-"
90                        "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
91            std::string("e-p:32:32-f64:64:64-i64:64:64"))),
92     InstrInfo(Subtarget),
93     FrameInfo(Subtarget),
94     JITInfo(),
95     TLInfo(*this) {
96   DefRelocModel = getRelocationModel();
97 }
98
99 unsigned ARMTargetMachine::getJITMatchQuality() {
100 #if defined(__arm__)
101   return 10;
102 #endif
103   return 0;
104 }
105
106 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
107   std::string TT = M.getTargetTriple();
108   if (TT.size() >= 4 && // Match arm-foo-bar, as well as things like armv5blah-*
109       (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
110     return 20;
111   // If the target triple is something non-arm, we don't match.
112   if (!TT.empty()) return 0;
113
114   if (M.getEndianness()  == Module::LittleEndian &&
115       M.getPointerSize() == Module::Pointer32)
116     return 10;                                   // Weak match
117   else if (M.getEndianness() != Module::AnyEndianness ||
118            M.getPointerSize() != Module::AnyPointerSize)
119     return 0;                                    // Match for some other target
120
121   return getJITMatchQuality()/2;
122 }
123
124
125 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
126   switch (Subtarget.TargetType) {
127    case ARMSubtarget::isDarwin:
128     return new ARMDarwinTargetAsmInfo(*this);
129    case ARMSubtarget::isELF:
130     return new ARMELFTargetAsmInfo(*this);
131    default:
132     return new ARMGenericTargetAsmInfo(*this);
133   }
134 }
135
136
137 // Pass Pipeline Configuration
138 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
139   PM.add(createARMISelDag(*this));
140   return false;
141 }
142
143 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
144   // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
145   if (!Fast && !DisableLdStOpti && !Subtarget.isThumb())
146     PM.add(createARMLoadStoreOptimizationPass());
147
148   if (!Fast && !DisableIfConversion && !Subtarget.isThumb())
149     PM.add(createIfConverterPass());
150
151   PM.add(createARMConstantIslandPass());
152   return true;
153 }
154
155 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
156                                           raw_ostream &Out) {
157   // Output assembly language.
158   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
159   if (AsmPrinterCtor)
160     PM.add(AsmPrinterCtor(Out, *this));
161
162   return false;
163 }
164
165
166 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
167                                       bool DumpAsm, MachineCodeEmitter &MCE) {
168   // FIXME: Move this to TargetJITInfo!
169   if (DefRelocModel == Reloc::Default)
170     setRelocationModel(Reloc::Static);
171
172   // Machine code emitter pass for ARM.
173   PM.add(createARMCodeEmitterPass(*this, MCE));
174   if (DumpAsm) {
175     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
176     if (AsmPrinterCtor)
177       PM.add(AsmPrinterCtor(errs(), *this));
178   }
179
180   return false;
181 }
182
183 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
184                                         bool DumpAsm, MachineCodeEmitter &MCE) {
185   // Machine code emitter pass for ARM.
186   PM.add(createARMCodeEmitterPass(*this, MCE));
187   if (DumpAsm) {
188     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
189     if (AsmPrinterCtor)
190       PM.add(AsmPrinterCtor(errs(), *this));
191   }
192
193   return false;
194 }