blob: 6bb0b2e3cca3a97d294d5b1c1c15ce6facf3e397 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#include "ARMTargetMachine.h"
14#include "ARMTargetAsmInfo.h"
15#include "ARMFrameInfo.h"
16#include "ARM.h"
17#include "llvm/Module.h"
18#include "llvm/PassManager.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/Support/CommandLine.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetMachineRegistry.h"
23#include "llvm/Target/TargetOptions.h"
24using namespace llvm;
25
26static cl::opt<bool> DisableLdStOpti("disable-arm-loadstore-opti", cl::Hidden,
27 cl::desc("Disable load store optimization pass"));
Evan Chengcd497f42007-09-20 00:48:22 +000028static cl::opt<bool> DisableIfConversion("disable-arm-if-conversion",cl::Hidden,
29 cl::desc("Disable if-conversion pass"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
Oscar Fuentes4f012352008-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 Gohman089efff2008-05-13 00:00:25 +000038// Register the target.
Dan Gohman669b9bf2008-10-14 20:25:08 +000039static RegisterTarget<ARMTargetMachine> X("arm", "ARM");
40static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Anton Korobeynikov74b114b2008-08-17 13:55:10 +000042// No assembler printer by default
43ARMTargetMachine::AsmPrinterCtorFn ARMTargetMachine::AsmPrinterCtor = 0;
44
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045/// ThumbTargetMachine - Create an Thumb architecture model.
46///
47unsigned ThumbTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +000048#if defined(__thumb__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 return 10;
50#endif
51 return 0;
52}
53
54unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
55 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-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"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 return 20;
60
61 // If the target triple is something non-thumb, we don't match.
62 if (!TT.empty()) return 0;
63
64 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;
72}
73
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +000074ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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),
83 DataLayout(Subtarget.isAPCS_ABI() ?
84 // APCS ABI
85 (isThumb ?
86 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")) :
89 // AAPCS ABI
90 (isThumb ?
91 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"))),
94 InstrInfo(Subtarget),
95 FrameInfo(Subtarget),
Evan Chengba96b1a2008-11-08 07:38:22 +000096 JITInfo(),
Evan Chengb8b40d62008-10-30 16:10:54 +000097 TLInfo(*this) {
98 DefRelocModel = getRelocationModel();
99}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101unsigned ARMTargetMachine::getJITMatchQuality() {
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000102#if defined(__arm__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 return 10;
104#endif
105 return 0;
106}
107
108unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
109 std::string TT = M.getTargetTriple();
Evan Chenga5de2fc2009-03-09 20:25:39 +0000110 // Match arm-foo-bar, as well as things like armv5blah-*
111 if (TT.size() >= 4 &&
Chris Lattneraccc87c2008-05-06 02:29:28 +0000112 (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return 20;
114 // If the target triple is something non-arm, we don't match.
115 if (!TT.empty()) return 0;
116
117 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;
125}
126
127
128const TargetAsmInfo *ARMTargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov3cc6efa2008-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 Korobeynikov3829e8a2008-09-25 21:00:33 +0000135 return new ARMGenericTargetAsmInfo(*this);
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000136 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137}
138
139
140// Pass Pipeline Configuration
Dan Gohmane34aa772008-03-11 22:29:46 +0000141bool ARMTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 PM.add(createARMISelDag(*this));
143 return false;
144}
145
Dan Gohmane34aa772008-03-11 22:29:46 +0000146bool ARMTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 // FIXME: temporarily disabling load / store optimization pass for Thumb mode.
148 if (!Fast && !DisableLdStOpti && !Subtarget.isThumb())
149 PM.add(createARMLoadStoreOptimizationPass());
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000150
Evan Chengcd497f42007-09-20 00:48:22 +0000151 if (!Fast && !DisableIfConversion && !Subtarget.isThumb())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 PM.add(createIfConverterPass());
153
154 PM.add(createARMConstantIslandPass());
155 return true;
156}
157
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000158bool ARMTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
Owen Anderson847b99b2008-08-21 00:14:44 +0000159 raw_ostream &Out) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 // Output assembly language.
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000161 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
162 if (AsmPrinterCtor)
Bill Wendling4f405312009-02-24 08:30:20 +0000163 PM.add(AsmPrinterCtor(Out, *this, Fast));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 return false;
166}
167
168
Dan Gohmane34aa772008-03-11 22:29:46 +0000169bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000170 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 // FIXME: Move this to TargetJITInfo!
Evan Chengb8b40d62008-10-30 16:10:54 +0000172 if (DefRelocModel == Reloc::Default)
173 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174
175 // Machine code emitter pass for ARM.
176 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000177 if (DumpAsm) {
178 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
179 if (AsmPrinterCtor)
Bill Wendling4f405312009-02-24 08:30:20 +0000180 PM.add(AsmPrinterCtor(errs(), *this, Fast));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000181 }
182
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 return false;
184}
185
Dan Gohmane34aa772008-03-11 22:29:46 +0000186bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000187 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 // Machine code emitter pass for ARM.
189 PM.add(createARMCodeEmitterPass(*this, MCE));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000190 if (DumpAsm) {
191 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
192 if (AsmPrinterCtor)
Bill Wendling4f405312009-02-24 08:30:20 +0000193 PM.add(AsmPrinterCtor(errs(), *this, Fast));
Anton Korobeynikov74b114b2008-08-17 13:55:10 +0000194 }
195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 return false;
197}