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 "ARMMCAsmInfo.h"
15 #include "ARMFrameLowering.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FormattedStream.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Target/TargetRegistry.h"
25 static MCAsmInfo
*createMCAsmInfo(const Target
&T
, StringRef TT
) {
28 if (TheTriple
.isOSDarwin())
29 return new ARMMCAsmInfoDarwin();
31 return new ARMELFMCAsmInfo();
34 // This is duplicated code. Refactor this.
35 static MCStreamer
*createMCStreamer(const Target
&T
, const std::string
&TT
,
36 MCContext
&Ctx
, TargetAsmBackend
&TAB
,
38 MCCodeEmitter
*Emitter
,
43 if (TheTriple
.isOSDarwin())
44 return createMachOStreamer(Ctx
, TAB
, OS
, Emitter
, RelaxAll
);
46 if (TheTriple
.isOSWindows()) {
47 llvm_unreachable("ARM does not support Windows COFF format");
51 return createELFStreamer(Ctx
, TAB
, OS
, Emitter
, RelaxAll
, NoExecStack
);
54 extern "C" void LLVMInitializeARMTarget() {
55 // Register the target.
56 RegisterTargetMachine
<ARMTargetMachine
> X(TheARMTarget
);
57 RegisterTargetMachine
<ThumbTargetMachine
> Y(TheThumbTarget
);
59 // Register the target asm info.
60 RegisterAsmInfoFn
A(TheARMTarget
, createMCAsmInfo
);
61 RegisterAsmInfoFn
B(TheThumbTarget
, createMCAsmInfo
);
63 // Register the MC Code Emitter
64 TargetRegistry::RegisterCodeEmitter(TheARMTarget
, createARMMCCodeEmitter
);
65 TargetRegistry::RegisterCodeEmitter(TheThumbTarget
, createARMMCCodeEmitter
);
67 // Register the asm backend.
68 TargetRegistry::RegisterAsmBackend(TheARMTarget
, createARMAsmBackend
);
69 TargetRegistry::RegisterAsmBackend(TheThumbTarget
, createARMAsmBackend
);
71 // Register the object streamer.
72 TargetRegistry::RegisterObjectStreamer(TheARMTarget
, createMCStreamer
);
73 TargetRegistry::RegisterObjectStreamer(TheThumbTarget
, createMCStreamer
);
77 /// TargetMachine ctor - Create an ARM architecture model.
79 ARMBaseTargetMachine::ARMBaseTargetMachine(const Target
&T
,
80 const std::string
&TT
,
81 const std::string
&CPU
,
82 const std::string
&FS
)
83 : LLVMTargetMachine(T
, TT
, CPU
, FS
),
84 Subtarget(TT
, CPU
, FS
),
86 InstrItins(Subtarget
.getInstrItineraryData()) {
87 DefRelocModel
= getRelocationModel();
89 // Default to soft float ABI
90 if (FloatABIType
== FloatABI::Default
)
91 FloatABIType
= FloatABI::Soft
;
94 ARMTargetMachine::ARMTargetMachine(const Target
&T
, const std::string
&TT
,
95 const std::string
&CPU
,
96 const std::string
&FS
)
97 : ARMBaseTargetMachine(T
, TT
, CPU
, FS
), InstrInfo(Subtarget
),
98 DataLayout(Subtarget
.isAPCS_ABI() ?
99 std::string("e-p:32:32-f64:32:64-i64:32:64-"
100 "v128:32:128-v64:32:64-n32") :
101 std::string("e-p:32:32-f64:64:64-i64:64:64-"
102 "v128:64:128-v64:64:64-n32")),
103 ELFWriterInfo(*this),
106 FrameLowering(Subtarget
) {
107 if (!Subtarget
.hasARMOps())
108 report_fatal_error("CPU: '" + Subtarget
.getCPUString() + "' does not "
109 "support ARM mode execution!");
112 ThumbTargetMachine::ThumbTargetMachine(const Target
&T
, const std::string
&TT
,
113 const std::string
&CPU
,
114 const std::string
&FS
)
115 : ARMBaseTargetMachine(T
, TT
, CPU
, FS
),
116 InstrInfo(Subtarget
.hasThumb2()
117 ? ((ARMBaseInstrInfo
*)new Thumb2InstrInfo(Subtarget
))
118 : ((ARMBaseInstrInfo
*)new Thumb1InstrInfo(Subtarget
))),
119 DataLayout(Subtarget
.isAPCS_ABI() ?
120 std::string("e-p:32:32-f64:32:64-i64:32:64-"
121 "i16:16:32-i8:8:32-i1:8:32-"
122 "v128:32:128-v64:32:64-a:0:32-n32") :
123 std::string("e-p:32:32-f64:64:64-i64:64:64-"
124 "i16:16:32-i8:8:32-i1:8:32-"
125 "v128:64:128-v64:64:64-a:0:32-n32")),
126 ELFWriterInfo(*this),
129 FrameLowering(Subtarget
.hasThumb2()
130 ? new ARMFrameLowering(Subtarget
)
131 : (ARMFrameLowering
*)new Thumb1FrameLowering(Subtarget
)) {
134 // Pass Pipeline Configuration
135 bool ARMBaseTargetMachine::addPreISel(PassManagerBase
&PM
,
136 CodeGenOpt::Level OptLevel
) {
137 if (OptLevel
!= CodeGenOpt::None
)
138 PM
.add(createARMGlobalMergePass(getTargetLowering()));
143 bool ARMBaseTargetMachine::addInstSelector(PassManagerBase
&PM
,
144 CodeGenOpt::Level OptLevel
) {
145 PM
.add(createARMISelDag(*this, OptLevel
));
149 bool ARMBaseTargetMachine::addPreRegAlloc(PassManagerBase
&PM
,
150 CodeGenOpt::Level OptLevel
) {
151 // FIXME: temporarily disabling load / store optimization pass for Thumb1.
152 if (OptLevel
!= CodeGenOpt::None
&& !Subtarget
.isThumb1Only())
153 PM
.add(createARMLoadStoreOptimizationPass(true));
154 if (OptLevel
!= CodeGenOpt::None
&& Subtarget
.isCortexA9())
155 PM
.add(createMLxExpansionPass());
160 bool ARMBaseTargetMachine::addPreSched2(PassManagerBase
&PM
,
161 CodeGenOpt::Level OptLevel
) {
162 // FIXME: temporarily disabling load / store optimization pass for Thumb1.
163 if (OptLevel
!= CodeGenOpt::None
) {
164 if (!Subtarget
.isThumb1Only())
165 PM
.add(createARMLoadStoreOptimizationPass());
166 if (Subtarget
.hasNEON())
167 PM
.add(createNEONMoveFixPass());
170 // Expand some pseudo instructions into multiple instructions to allow
171 // proper scheduling.
172 PM
.add(createARMExpandPseudoPass());
174 if (OptLevel
!= CodeGenOpt::None
) {
175 if (!Subtarget
.isThumb1Only())
176 PM
.add(createIfConverterPass());
178 if (Subtarget
.isThumb2())
179 PM
.add(createThumb2ITBlockPass());
184 bool ARMBaseTargetMachine::addPreEmitPass(PassManagerBase
&PM
,
185 CodeGenOpt::Level OptLevel
) {
186 if (Subtarget
.isThumb2() && !Subtarget
.prefers32BitThumb())
187 PM
.add(createThumb2SizeReductionPass());
189 PM
.add(createARMConstantIslandPass());
193 bool ARMBaseTargetMachine::addCodeEmitter(PassManagerBase
&PM
,
194 CodeGenOpt::Level OptLevel
,
195 JITCodeEmitter
&JCE
) {
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(createARMJITCodeEmitterPass(*this, JCE
));