Use raw_ostream throughout the AsmPrinter.
[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
90 unsigned ARMTargetMachine::getJITMatchQuality() {
91 #if defined(__arm__)
92   return 10;
93 #endif
94   return 0;
95 }
96
97 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
98   std::string TT = M.getTargetTriple();
99   if (TT.size() >= 4 && // Match arm-foo-bar, as well as things like armv5blah-*
100       (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
101     return 20;
102   // If the target triple is something non-arm, we don't match.
103   if (!TT.empty()) return 0;
104
105   if (M.getEndianness()  == Module::LittleEndian &&
106       M.getPointerSize() == Module::Pointer32)
107     return 10;                                   // Weak match
108   else if (M.getEndianness() != Module::AnyEndianness ||
109            M.getPointerSize() != Module::AnyPointerSize)
110     return 0;                                    // Match for some other target
111
112   return getJITMatchQuality()/2;
113 }
114
115
116 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
117   switch (Subtarget.TargetType) {
118    case ARMSubtarget::isDarwin:
119     return new ARMDarwinTargetAsmInfo(*this);
120    case ARMSubtarget::isELF:
121     return new ARMELFTargetAsmInfo(*this);
122    default:
123     return new ARMTargetAsmInfo(*this);
124   }
125 }
126
127
128 // Pass Pipeline Configuration
129 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
130   PM.add(createARMISelDag(*this));
131   return false;
132 }
133
134 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
135   // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
136   if (!Fast && !DisableLdStOpti && !Subtarget.isThumb())
137     PM.add(createARMLoadStoreOptimizationPass());
138
139   if (!Fast && !DisableIfConversion && !Subtarget.isThumb())
140     PM.add(createIfConverterPass());
141
142   PM.add(createARMConstantIslandPass());
143   return true;
144 }
145
146 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
147                                           raw_ostream &Out) {
148   // Output assembly language.
149   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
150   if (AsmPrinterCtor)
151     PM.add(AsmPrinterCtor(Out, *this));
152
153   return false;
154 }
155
156
157 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
158                                       bool DumpAsm, MachineCodeEmitter &MCE) {
159   // FIXME: Move this to TargetJITInfo!
160   setRelocationModel(Reloc::Static);
161
162   // Machine code emitter pass for ARM.
163   PM.add(createARMCodeEmitterPass(*this, MCE));
164   if (DumpAsm) {
165     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
166     if (AsmPrinterCtor)
167       PM.add(AsmPrinterCtor(errs(), *this));
168   }
169
170   return false;
171 }
172
173 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
174                                         bool DumpAsm, MachineCodeEmitter &MCE) {
175   // Machine code emitter pass for ARM.
176   PM.add(createARMCodeEmitterPass(*this, MCE));
177   if (DumpAsm) {
178     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
179     if (AsmPrinterCtor)
180       PM.add(AsmPrinterCtor(errs(), *this));
181   }
182
183   return false;
184 }