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"
27 EnablePreLdStOpti("arm-pre-alloc-loadstore-opti", cl::Hidden,
28 cl::desc("Enable pre-regalloc load store optimization pass"));
29 static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
30 cl::desc("Disable load store optimization pass"));
31 static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
32 cl::desc("Disable if-conversion pass"));
34 /// ARMTargetMachineModule - Note that this is used on hosts that cannot link
35 /// in a library unless there are references into the library. In particular,
36 /// it seems that it is not possible to get things to work on Win32 without
37 /// this. Though it is unused, do not remove it.
38 extern "C" int ARMTargetMachineModule;
39 int ARMTargetMachineModule = 0;
41 // Register the target.
42 static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
43 static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
45 // Force static initialization when called from llvm/InitializeAllTargets.h
47 void InitializeARMTarget() { }
50 // No assembler printer by default
51 ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
53 /// ThumbTargetMachine - Create an Thumb architecture model.
55 unsigned ThumbTargetMachine::getJITMatchQuality() {
56 #if defined(__thumb__)
62 unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
63 std::string TT = M.getTargetTriple();
64 // Match thumb-foo-bar, as well as things like thumbv5blah-*
66 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
69 // If the target triple is something non-thumb, we don't match.
70 if (!TT.empty()) return 0;
72 if (M.getEndianness() == Module::LittleEndian &&
73 M.getPointerSize() == Module::Pointer32)
74 return 10; // Weak match
75 else if (M.getEndianness() != Module::AnyEndianness ||
76 M.getPointerSize() != Module::AnyPointerSize)
77 return 0; // Match for some other target
79 return getJITMatchQuality()/2;
82 ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
83 : ARMTargetMachine(M, FS, true) {
86 /// TargetMachine ctor - Create an ARM architecture model.
88 ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
90 : Subtarget(M, FS, isThumb),
91 DataLayout(Subtarget.isAPCS_ABI() ?
94 std::string("e-p:32:32-f64:32:32-i64:32:32-"
95 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
96 std::string("e-p:32:32-f64:32:32-i64:32:32")) :
99 std::string("e-p:32:32-f64:64:64-i64:64:64-"
100 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
101 std::string("e-p:32:32-f64:64:64-i64:64:64"))),
102 InstrInfo(Subtarget),
103 FrameInfo(Subtarget),
106 InstrItins(Subtarget.getInstrItineraryData()) {
107 DefRelocModel = getRelocationModel();
110 unsigned ARMTargetMachine::getJITMatchQuality() {
117 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
118 std::string TT = M.getTargetTriple();
119 // Match arm-foo-bar, as well as things like armv5blah-*
120 if (TT.size() >= 4 &&
121 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
123 // If the target triple is something non-arm, we don't match.
124 if (!TT.empty()) return 0;
126 if (M.getEndianness() == Module::LittleEndian &&
127 M.getPointerSize() == Module::Pointer32)
128 return 10; // Weak match
129 else if (M.getEndianness() != Module::AnyEndianness ||
130 M.getPointerSize() != Module::AnyPointerSize)
131 return 0; // Match for some other target
133 return getJITMatchQuality()/2;
137 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
138 switch (Subtarget.TargetType) {
139 case ARMSubtarget::isDarwin:
140 return new ARMDarwinTargetAsmInfo(*this);
141 case ARMSubtarget::isELF:
142 return new ARMELFTargetAsmInfo(*this);
144 return new ARMGenericTargetAsmInfo(*this);
149 // Pass Pipeline Configuration
150 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
151 CodeGenOpt::Level OptLevel) {
152 PM.add(createARMISelDag(*this));
156 bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
157 CodeGenOpt::Level OptLevel) {
158 if (!EnablePreLdStOpti)
160 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
161 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
162 PM.add(createARMLoadStoreOptimizationPass(true));
166 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
167 CodeGenOpt::Level OptLevel) {
168 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
169 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
170 PM.add(createARMLoadStoreOptimizationPass());
172 if (OptLevel != CodeGenOpt::None &&
173 !DisableIfConversion && !Subtarget.isThumb())
174 PM.add(createIfConverterPass());
176 PM.add(createARMConstantIslandPass());
180 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
181 CodeGenOpt::Level OptLevel,
184 // Output assembly language.
185 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
187 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
193 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
194 CodeGenOpt::Level OptLevel,
196 MachineCodeEmitter &MCE) {
197 // FIXME: Move this to TargetJITInfo!
198 if (DefRelocModel == Reloc::Default)
199 setRelocationModel(Reloc::Static);
201 // Machine code emitter pass for ARM.
202 PM.add(createARMCodeEmitterPass(*this, MCE));
204 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
206 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
212 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
213 CodeGenOpt::Level OptLevel,
215 JITCodeEmitter &JCE) {
216 // FIXME: Move this to TargetJITInfo!
217 if (DefRelocModel == Reloc::Default)
218 setRelocationModel(Reloc::Static);
220 // Machine code emitter pass for ARM.
221 PM.add(createARMJITCodeEmitterPass(*this, JCE));
223 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
225 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
231 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
232 CodeGenOpt::Level OptLevel,
234 MachineCodeEmitter &MCE) {
235 // Machine code emitter pass for ARM.
236 PM.add(createARMCodeEmitterPass(*this, MCE));
238 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
240 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
246 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
247 CodeGenOpt::Level OptLevel,
249 JITCodeEmitter &JCE) {
250 // Machine code emitter pass for ARM.
251 PM.add(createARMJITCodeEmitterPass(*this, JCE));
253 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
255 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));