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.
43 extern "C" void LLVMInitializeARMTarget() { }
45 // No assembler printer by default
46 ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
48 /// ThumbTargetMachine - Create an Thumb architecture model.
50 unsigned ThumbTargetMachine::getJITMatchQuality() {
51 #if defined(__thumb__)
57 unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
58 std::string TT = M.getTargetTriple();
59 // Match thumb-foo-bar, as well as things like thumbv5blah-*
61 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
64 // If the target triple is something non-thumb, we don't match.
65 if (!TT.empty()) return 0;
67 if (M.getEndianness() == Module::LittleEndian &&
68 M.getPointerSize() == Module::Pointer32)
69 return 10; // Weak match
70 else if (M.getEndianness() != Module::AnyEndianness ||
71 M.getPointerSize() != Module::AnyPointerSize)
72 return 0; // Match for some other target
74 return getJITMatchQuality()/2;
77 ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
78 : ARMTargetMachine(M, FS, true) {
81 /// TargetMachine ctor - Create an ARM architecture model.
83 ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
85 : Subtarget(M, FS, isThumb),
86 DataLayout(Subtarget.isAPCS_ABI() ?
89 std::string("e-p:32:32-f64:32:32-i64:32:32-"
90 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
91 std::string("e-p:32:32-f64:32:32-i64:32:32")) :
94 std::string("e-p:32:32-f64:64:64-i64:64:64-"
95 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
96 std::string("e-p:32:32-f64:64:64-i64:64:64"))),
101 InstrItins(Subtarget.getInstrItineraryData()) {
102 DefRelocModel = getRelocationModel();
105 unsigned ARMTargetMachine::getJITMatchQuality() {
112 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
113 std::string TT = M.getTargetTriple();
114 // Match arm-foo-bar, as well as things like armv5blah-*
115 if (TT.size() >= 4 &&
116 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
118 // If the target triple is something non-arm, we don't match.
119 if (!TT.empty()) return 0;
121 if (M.getEndianness() == Module::LittleEndian &&
122 M.getPointerSize() == Module::Pointer32)
123 return 10; // Weak match
124 else if (M.getEndianness() != Module::AnyEndianness ||
125 M.getPointerSize() != Module::AnyPointerSize)
126 return 0; // Match for some other target
128 return getJITMatchQuality()/2;
132 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
133 switch (Subtarget.TargetType) {
134 case ARMSubtarget::isDarwin:
135 return new ARMDarwinTargetAsmInfo(*this);
136 case ARMSubtarget::isELF:
137 return new ARMELFTargetAsmInfo(*this);
139 return new ARMGenericTargetAsmInfo(*this);
144 // Pass Pipeline Configuration
145 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
146 CodeGenOpt::Level OptLevel) {
147 PM.add(createARMISelDag(*this));
151 bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
152 CodeGenOpt::Level OptLevel) {
153 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
154 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
155 PM.add(createARMLoadStoreOptimizationPass(true));
159 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
160 CodeGenOpt::Level OptLevel) {
161 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
162 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
163 PM.add(createARMLoadStoreOptimizationPass());
165 if (OptLevel != CodeGenOpt::None &&
166 !DisableIfConversion && !Subtarget.isThumb())
167 PM.add(createIfConverterPass());
169 PM.add(createARMConstantIslandPass());
173 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
174 CodeGenOpt::Level OptLevel,
177 // Output assembly language.
178 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
180 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
186 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
187 CodeGenOpt::Level OptLevel,
189 MachineCodeEmitter &MCE) {
190 // FIXME: Move this to TargetJITInfo!
191 if (DefRelocModel == Reloc::Default)
192 setRelocationModel(Reloc::Static);
194 // Machine code emitter pass for ARM.
195 PM.add(createARMCodeEmitterPass(*this, MCE));
197 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
199 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
205 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
206 CodeGenOpt::Level OptLevel,
208 JITCodeEmitter &JCE) {
209 // FIXME: Move this to TargetJITInfo!
210 if (DefRelocModel == Reloc::Default)
211 setRelocationModel(Reloc::Static);
213 // Machine code emitter pass for ARM.
214 PM.add(createARMJITCodeEmitterPass(*this, JCE));
216 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
218 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
224 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
225 CodeGenOpt::Level OptLevel,
227 MachineCodeEmitter &MCE) {
228 // Machine code emitter pass for ARM.
229 PM.add(createARMCodeEmitterPass(*this, MCE));
231 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
233 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
239 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
240 CodeGenOpt::Level OptLevel,
242 JITCodeEmitter &JCE) {
243 // Machine code emitter pass for ARM.
244 PM.add(createARMJITCodeEmitterPass(*this, JCE));
246 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
248 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));