blob: a5ce86e9df9ec6a477a8bd45b32087a3e2f1b9a5 [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#include "ARMTargetMachine.h"
Evan Chenga8e29892007-01-19 07:51:42 +000014#include "ARMTargetAsmInfo.h"
Rafael Espindolaec46ea32006-08-16 14:43:33 +000015#include "ARMFrameInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000016#include "ARM.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "llvm/Module.h"
18#include "llvm/PassManager.h"
Evan Cheng93072922007-05-16 02:01:49 +000019#include "llvm/CodeGen/Passes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#include "llvm/Support/CommandLine.h"
Owen Andersoncb371882008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000022#include "llvm/Target/TargetMachineRegistry.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "llvm/Target/TargetOptions.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000024using namespace llvm;
25
Evan Chenga8e29892007-01-19 07:51:42 +000026static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
27 cl::desc("Disable load store optimization pass"));
Evan Cheng17207dd2007-09-20 00:48:22 +000028static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
29 cl::desc("Disable if-conversion pass"));
Evan Chenga8e29892007-01-19 07:51:42 +000030
Oscar Fuentes92adc192008-11-15 21:36:30 +000031/// 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.
35extern "C" int ARMTargetMachineModule;
36int ARMTargetMachineModule = 0;
37
Dan Gohman844731a2008-05-13 00:00:25 +000038// Register the target.
Dan Gohmanb8cab922008-10-14 20:25:08 +000039static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
40static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000041
Anton Korobeynikov0bd89712008-08-17 13:55:10 +000042// No assembler printer by default
43ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
44
Evan Cheng04321f72007-02-23 03:14:31 +000045/// ThumbTargetMachine - Create an Thumb architecture model.
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000046///
Evan Cheng148b6a42007-07-05 21:15:40 +000047unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +000048#if defined(__thumb__)
Evan Cheng148b6a42007-07-05 21:15:40 +000049 return 10;
50#endif
51 return 0;
52}
53
Evan Cheng04321f72007-02-23 03:14:31 +000054unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
55 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +000056 // Match thumb-foo-bar, as well as things like thumbv5blah-*
57 if (TT.size() >= 6 &&
58 (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
Evan Cheng04321f72007-02-23 03:14:31 +000059 return 20;
60
Chris Lattner87bdba62007-07-09 17:25:29 +000061 // If the target triple is something non-thumb, we don't match.
62 if (!TT.empty()) return 0;
63
Evan Cheng148b6a42007-07-05 21:15:40 +000064 if (M.getEndianness() == Module::LittleEndian &&
65 M.getPointerSize() == Module::Pointer32)
66 return 10; // Weak match
67 else if (M.getEndianness() != Module::AnyEndianness ||
68 M.getPointerSize() != Module::AnyPointerSize)
69 return 0; // Match for some other target
70
71 return getJITMatchQuality()/2;
Evan Cheng04321f72007-02-23 03:14:31 +000072}
73
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000074ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
Evan Cheng04321f72007-02-23 03:14:31 +000075 : ARMTargetMachine(M, FS, true) {
76}
77
78/// TargetMachine ctor - Create an ARM architecture model.
79///
80ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS,
81 bool isThumb)
82 : Subtarget(M, FS, isThumb),
Lauro Ramos Venancio3630e782007-02-13 19:52:28 +000083 DataLayout(Subtarget.isAPCS_ABI() ?
Lauro Ramos Venancio75016052007-02-13 20:06:15 +000084 // APCS ABI
Evan Cheng04321f72007-02-23 03:14:31 +000085 (isThumb ?
Chris Lattnerd2b7cec2007-02-14 05:52:17 +000086 std::string("e-p:32:32-f64:32:32-i64:32:32-"
87 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
88 std::string("e-p:32:32-f64:32:32-i64:32:32")) :
Lauro Ramos Venancio75016052007-02-13 20:06:15 +000089 // AAPCS ABI
Evan Cheng04321f72007-02-23 03:14:31 +000090 (isThumb ?
Chris Lattnerd2b7cec2007-02-14 05:52:17 +000091 std::string("e-p:32:32-f64:64:64-i64:64:64-"
92 "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
93 std::string("e-p:32:32-f64:64:64-i64:64:64"))),
Evan Chengd44ecd82007-01-22 21:24:13 +000094 InstrInfo(Subtarget),
Evan Chenge8308df2007-03-13 01:20:42 +000095 FrameInfo(Subtarget),
Evan Cheng3cc82232008-11-08 07:38:22 +000096 JITInfo(),
Evan Cheng65f24422008-10-30 16:10:54 +000097 TLInfo(*this) {
98 DefRelocModel = getRelocationModel();
99}
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000100
Evan Cheng148b6a42007-07-05 21:15:40 +0000101unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000102#if defined(__arm__)
Evan Cheng148b6a42007-07-05 21:15:40 +0000103 return 10;
104#endif
105 return 0;
106}
107
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000108unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
109 std::string TT = M.getTargetTriple();
Evan Cheng8c6b9912009-03-09 20:25:39 +0000110 // Match arm-foo-bar, as well as things like armv5blah-*
111 if (TT.size() >= 4 &&
Chris Lattner3bf6acc2008-05-06 02:29:28 +0000112 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000113 return 20;
Chris Lattner87bdba62007-07-09 17:25:29 +0000114 // If the target triple is something non-arm, we don't match.
115 if (!TT.empty()) return 0;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000116
Evan Cheng148b6a42007-07-05 21:15:40 +0000117 if (M.getEndianness() == Module::LittleEndian &&
118 M.getPointerSize() == Module::Pointer32)
119 return 10; // Weak match
120 else if (M.getEndianness() != Module::AnyEndianness ||
121 M.getPointerSize() != Module::AnyPointerSize)
122 return 0; // Match for some other target
123
124 return getJITMatchQuality()/2;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000125}
126
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000127
Evan Chenga8e29892007-01-19 07:51:42 +0000128const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000129 switch (Subtarget.TargetType) {
130 case ARMSubtarget::isDarwin:
131 return new ARMDarwinTargetAsmInfo(*this);
132 case ARMSubtarget::isELF:
133 return new ARMELFTargetAsmInfo(*this);
134 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000135 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000136 }
Evan Chenga8e29892007-01-19 07:51:42 +0000137}
138
139
Chris Lattner1911fd42006-09-04 04:14:57 +0000140// Pass Pipeline Configuration
Bill Wendling98a366d2009-04-29 23:29:43 +0000141bool ARMTargetMachine::addInstSelector(PassManagerBase &PM,
142 CodeGenOpt::Level OptLevel) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000143 PM.add(createARMISelDag(*this));
Chris Lattner1911fd42006-09-04 04:14:57 +0000144 return false;
145}
Rafael Espindola71f3b942006-09-19 15:49:25 +0000146
Bill Wendling98a366d2009-04-29 23:29:43 +0000147bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM,
148 CodeGenOpt::Level OptLevel) {
Evan Chenga8e29892007-01-19 07:51:42 +0000149 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
Bill Wendling98a366d2009-04-29 23:29:43 +0000150 if (OptLevel != CodeGenOpt::None && !DisableLdStOpti && !Subtarget.isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000151 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000152
Bill Wendling98a366d2009-04-29 23:29:43 +0000153 if (OptLevel != CodeGenOpt::None &&
154 !DisableIfConversion && !Subtarget.isThumb())
Evan Cheng75604f82007-05-16 20:52:46 +0000155 PM.add(createIfConverterPass());
156
Evan Chenga8e29892007-01-19 07:51:42 +0000157 PM.add(createARMConstantIslandPass());
Rafael Espindola71f3b942006-09-19 15:49:25 +0000158 return true;
159}
160
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000161bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000162 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000163 bool Verbose,
164 raw_ostream &Out) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000165 // Output assembly language.
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000166 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
167 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000168 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000169
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000170 return false;
171}
Evan Cheng148b6a42007-07-05 21:15:40 +0000172
173
Bill Wendling98a366d2009-04-29 23:29:43 +0000174bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
175 CodeGenOpt::Level OptLevel,
176 bool DumpAsm,
177 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000178 // FIXME: Move this to TargetJITInfo!
Evan Cheng65f24422008-10-30 16:10:54 +0000179 if (DefRelocModel == Reloc::Default)
180 setRelocationModel(Reloc::Static);
Evan Cheng148b6a42007-07-05 21:15:40 +0000181
182 // Machine code emitter pass for ARM.
183 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000184 if (DumpAsm) {
185 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
186 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000187 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000188 }
189
Evan Cheng148b6a42007-07-05 21:15:40 +0000190 return false;
191}
192
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000193bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000194 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000195 bool DumpAsm,
196 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000197 // Machine code emitter pass for ARM.
198 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000199 if (DumpAsm) {
200 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
201 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000202 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000203 }
204
Evan Cheng148b6a42007-07-05 21:15:40 +0000205 return false;
206}