1 //===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
13 #include "ARMTargetMachine.h"
14 #include "ARMTargetAsmInfo.h"
15 #include "ARMFrameInfo.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"
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"));
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;
38 // Register the target.
39 static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
40 static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
42 // Force static initialization when called from llvm/InitializeAllTargets.h
44 void InitializeARMTarget() { }
47 // No assembler printer by default
48 ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
50 /// ThumbTargetMachine - Create an Thumb architecture model.
52 unsigned ThumbTargetMachine::getJITMatchQuality() {
53 #if defined(__thumb__)
59 unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
60 std::string TT = M.getTargetTriple();
61 // Match thumb-foo-bar, as well as things like thumbv5blah-*
63 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
66 // If the target triple is something non-thumb, we don't match.
67 if (!TT.empty()) return 0;
69 if (M.getEndianness() == Module::LittleEndian &&
70 M.getPointerSize() == Module::Pointer32)
71 return 10; // Weak match
72 else if (M.getEndianness() != Module::AnyEndianness ||
73 M.getPointerSize() != Module::AnyPointerSize)
74 return 0; // Match for some other target
76 return getJITMatchQuality()/2;
79 ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
80 : ARMTargetMachine(M, FS, true) {
83 /// TargetMachine ctor - Create an ARM architecture model.
85 ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
87 : Subtarget(M, FS, isThumb),
88 DataLayout(Subtarget.isAPCS_ABI() ?
91 std::string("e-p:32:32-f64:32:32-i64:32:32-"
92 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
93 std::string("e-p:32:32-f64:32:32-i64:32:32")) :
96 std::string("e-p:32:32-f64:64:64-i64:64:64-"
97 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
98 std::string("e-p:32:32-f64:64:64-i64:64:64"))),
100 FrameInfo(Subtarget),
103 InstrItins(Subtarget.getInstrItineraryData()) {
104 DefRelocModel = getRelocationModel();
107 unsigned ARMTargetMachine::getJITMatchQuality() {
114 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
115 std::string TT = M.getTargetTriple();
116 // Match arm-foo-bar, as well as things like armv5blah-*
117 if (TT.size() >= 4 &&
118 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
120 // If the target triple is something non-arm, we don't match.
121 if (!TT.empty()) return 0;
123 if (M.getEndianness() == Module::LittleEndian &&
124 M.getPointerSize() == Module::Pointer32)
125 return 10; // Weak match
126 else if (M.getEndianness() != Module::AnyEndianness ||
127 M.getPointerSize() != Module::AnyPointerSize)
128 return 0; // Match for some other target
130 return getJITMatchQuality()/2;
134 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
135 switch (Subtarget.TargetType) {
136 case ARMSubtarget::isDarwin:
137 return new ARMDarwinTargetAsmInfo(*this);
138 case ARMSubtarget::isELF:
139 return new ARMELFTargetAsmInfo(*this);
141 return new ARMGenericTargetAsmInfo(*this);
146 // Pass Pipeline Configuration
147 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
148 CodeGenOpt::Level OptLevel) {
149 PM.add(createARMISelDag(*this));
153 bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
154 CodeGenOpt::Level OptLevel) {
155 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
156 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
157 PM.add(createARMLoadStoreOptimizationPass(true));
161 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
163 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
164 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
165 PM.add(createARMLoadStoreOptimizationPass());
167 if (OptLevel != CodeGenOpt::None &&
168 !DisableIfConversion && !Subtarget.isThumb())
169 PM.add(createIfConverterPass());
171 PM.add(createARMConstantIslandPass());
175 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
176 CodeGenOpt::Level OptLevel,
179 // Output assembly language.
180 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
182 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
188 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
189 CodeGenOpt::Level OptLevel,
191 MachineCodeEmitter &MCE) {
192 // FIXME: Move this to TargetJITInfo!
193 if (DefRelocModel == Reloc::Default)
194 setRelocationModel(Reloc::Static);
196 // Machine code emitter pass for ARM.
197 PM.add(createARMCodeEmitterPass(*this, MCE));
199 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
201 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
207 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
208 CodeGenOpt::Level OptLevel,
210 JITCodeEmitter &JCE) {
211 // FIXME: Move this to TargetJITInfo!
212 if (DefRelocModel == Reloc::Default)
213 setRelocationModel(Reloc::Static);
215 // Machine code emitter pass for ARM.
216 PM.add(createARMJITCodeEmitterPass(*this, JCE));
218 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
220 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
226 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
227 CodeGenOpt::Level OptLevel,
229 MachineCodeEmitter &MCE) {
230 // Machine code emitter pass for ARM.
231 PM.add(createARMCodeEmitterPass(*this, MCE));
233 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
235 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
241 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
244 JITCodeEmitter &JCE) {
245 // Machine code emitter pass for ARM.
246 PM.add(createARMJITCodeEmitterPass(*this, JCE));
248 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
250 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));