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