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 DefRelocModel = getRelocationModel();
109 unsigned ARMTargetMachine::getJITMatchQuality() {
116 unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
117 std::string TT = M.getTargetTriple();
118 // Match arm-foo-bar, as well as things like armv5blah-*
119 if (TT.size() >= 4 &&
120 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
122 // If the target triple is something non-arm, we don't match.
123 if (!TT.empty()) return 0;
125 if (M.getEndianness() == Module::LittleEndian &&
126 M.getPointerSize() == Module::Pointer32)
127 return 10; // Weak match
128 else if (M.getEndianness() != Module::AnyEndianness ||
129 M.getPointerSize() != Module::AnyPointerSize)
130 return 0; // Match for some other target
132 return getJITMatchQuality()/2;
136 const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
137 switch (Subtarget.TargetType) {
138 case ARMSubtarget::isDarwin:
139 return new ARMDarwinTargetAsmInfo(*this);
140 case ARMSubtarget::isELF:
141 return new ARMELFTargetAsmInfo(*this);
143 return new ARMGenericTargetAsmInfo(*this);
148 // Pass Pipeline Configuration
149 bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
150 CodeGenOpt::Level OptLevel) {
151 PM.add(createARMISelDag(*this));
155 bool ARMTargetMachine::addPreRegAlloc(PassManagerBase &PM,
156 CodeGenOpt::Level OptLevel) {
157 if (!EnablePreLdStOpti)
159 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
160 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
161 PM.add(createARMLoadStoreOptimizationPass(true));
165 bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
166 CodeGenOpt::Level OptLevel) {
167 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
168 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
169 PM.add(createARMLoadStoreOptimizationPass());
171 if (OptLevel != CodeGenOpt::None &&
172 !DisableIfConversion && !Subtarget.isThumb())
173 PM.add(createIfConverterPass());
175 PM.add(createARMConstantIslandPass());
179 bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
180 CodeGenOpt::Level OptLevel,
183 // Output assembly language.
184 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
186 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
192 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
193 CodeGenOpt::Level OptLevel,
195 MachineCodeEmitter &MCE) {
196 // FIXME: Move this to TargetJITInfo!
197 if (DefRelocModel == Reloc::Default)
198 setRelocationModel(Reloc::Static);
200 // Machine code emitter pass for ARM.
201 PM.add(createARMCodeEmitterPass(*this, MCE));
203 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
205 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
211 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
212 CodeGenOpt::Level OptLevel,
214 JITCodeEmitter &JCE) {
215 // FIXME: Move this to TargetJITInfo!
216 if (DefRelocModel == Reloc::Default)
217 setRelocationModel(Reloc::Static);
219 // Machine code emitter pass for ARM.
220 PM.add(createARMJITCodeEmitterPass(*this, JCE));
222 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
224 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
230 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
231 CodeGenOpt::Level OptLevel,
233 MachineCodeEmitter &MCE) {
234 // Machine code emitter pass for ARM.
235 PM.add(createARMCodeEmitterPass(*this, MCE));
237 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
239 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
245 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
246 CodeGenOpt::Level OptLevel,
248 JITCodeEmitter &JCE) {
249 // Machine code emitter pass for ARM.
250 PM.add(createARMJITCodeEmitterPass(*this, JCE));
252 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
254 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));