blob: 76ccc09195a095d350eaadc3ed4570db8129ff68 [file] [log] [blame]
Chris Lattner22a6a902001-09-14 05:34:53 +00001//===-- TargetMachine.cpp - General Target Information ---------------------==//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner22a6a902001-09-14 05:34:53 +00009//
10// This file describes the general parts of a Target machine.
11//
12//===----------------------------------------------------------------------===//
Vikram S. Adve3414e782001-07-21 12:42:08 +000013
Evan Cheng4158a0f2010-04-21 03:18:23 +000014#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000016#include "llvm/MC/MCAsmInfo.h"
Misha Brukman0bfea682004-06-21 21:20:23 +000017#include "llvm/Target/TargetMachine.h"
Evan Cheng73136df2006-02-22 20:19:42 +000018#include "llvm/Target/TargetOptions.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/CommandLine.h"
Chris Lattner5d236002003-12-28 21:23:38 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Vikram S. Adve36d3e032002-09-16 15:39:26 +000022//---------------------------------------------------------------------------
Brian Gaeke8351d8c2004-03-04 19:16:23 +000023// Command-line options that tend to be useful on more than one back-end.
24//
25
Misha Brukman96041e52004-06-21 21:44:12 +000026namespace llvm {
Mon P Wangf67448a2009-03-20 05:06:58 +000027 bool LessPreciseFPMADOption;
Misha Brukman96041e52004-06-21 21:44:12 +000028 bool PrintMachineCode;
29 bool NoFramePointerElim;
Evan Cheng4158a0f2010-04-21 03:18:23 +000030 bool NoFramePointerElimNonLeaf;
Chris Lattner9de58902005-01-15 06:00:32 +000031 bool NoExcessFPPrecision;
Chris Lattner4a2cc662005-04-30 04:09:52 +000032 bool UnsafeFPMath;
Evan Cheng55f0c6b2010-07-15 22:07:12 +000033 bool NoInfsFPMath;
34 bool NoNaNsFPMath;
Chris Lattnerf65f18b2007-05-03 00:27:11 +000035 bool HonorSignDependentRoundingFPMathOption;
Evan Chenge107a8b2006-12-09 02:41:30 +000036 bool UseSoftFloat;
Anton Korobeynikov77d19432009-06-08 22:53:56 +000037 FloatABI::ABIType FloatABIType;
Bill Wendling42adc732009-03-11 22:30:01 +000038 bool NoImplicitFloat;
Anton Korobeynikov3f6d5282007-01-17 10:33:08 +000039 bool NoZerosInBSS;
Duncan Sands211427b2010-05-02 15:36:26 +000040 bool JITExceptionHandling;
Reid Kleckner9a10db82009-09-20 23:52:43 +000041 bool JITEmitDebugInfo;
42 bool JITEmitDebugInfoToDisk;
Dale Johannesen876224b2008-04-14 17:54:17 +000043 bool UnwindTablesMandatory;
Evan Cheng73136df2006-02-22 20:19:42 +000044 Reloc::Model RelocationModel;
Evan Cheng04417462006-07-06 01:53:36 +000045 CodeModel::Model CMModel;
Dan Gohman3464a5b2010-02-08 20:27:50 +000046 bool GuaranteedTailCallOpt;
Anton Korobeynikovcb195f52008-04-23 18:18:10 +000047 unsigned StackAlignment;
Evan Chengc963f6c2008-07-01 23:18:29 +000048 bool RealignStack;
Dale Johannesenc31eb202008-07-31 18:13:12 +000049 bool DisableJumpTables;
Owen Anderson1d338fc2008-10-07 20:22:28 +000050 bool StrongPHIElim;
Evan Chenga7c7b542011-04-07 00:58:44 +000051 bool HasDivModLibcall;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000052 bool AsmVerbosityDefault(false);
Chris Lattneraa2372562006-05-24 17:04:05 +000053}
Misha Brukman069ca062004-06-21 21:08:45 +000054
Bill Wendling42adc732009-03-11 22:30:01 +000055static cl::opt<bool, true>
56PrintCode("print-machineinstrs",
Dan Gohmand78c4002008-05-13 00:00:25 +000057 cl::desc("Print generated machine code"),
58 cl::location(PrintMachineCode), cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000059static cl::opt<bool, true>
Evan Cheng864541a2008-05-30 22:39:18 +000060DisableFPElim("disable-fp-elim",
Bill Wendling42adc732009-03-11 22:30:01 +000061 cl::desc("Disable frame pointer elimination optimization"),
62 cl::location(NoFramePointerElim),
63 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000064static cl::opt<bool, true>
Evan Cheng4158a0f2010-04-21 03:18:23 +000065DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
66 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
67 cl::location(NoFramePointerElimNonLeaf),
68 cl::init(false));
69static cl::opt<bool, true>
Dan Gohmand78c4002008-05-13 00:00:25 +000070DisableExcessPrecision("disable-excess-fp-precision",
Bill Wendling42adc732009-03-11 22:30:01 +000071 cl::desc("Disable optimizations that may increase FP precision"),
72 cl::location(NoExcessFPPrecision),
73 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000074static cl::opt<bool, true>
Mon P Wangf67448a2009-03-20 05:06:58 +000075EnableFPMAD("enable-fp-mad",
76 cl::desc("Enable less precise MAD instructions to be generated"),
77 cl::location(LessPreciseFPMADOption),
78 cl::init(false));
79static cl::opt<bool, true>
Dan Gohmand78c4002008-05-13 00:00:25 +000080EnableUnsafeFPMath("enable-unsafe-fp-math",
Bill Wendling42adc732009-03-11 22:30:01 +000081 cl::desc("Enable optimizations that may decrease FP precision"),
82 cl::location(UnsafeFPMath),
83 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000084static cl::opt<bool, true>
Evan Cheng55f0c6b2010-07-15 22:07:12 +000085EnableNoInfsFPMath("enable-no-infs-fp-math",
86 cl::desc("Enable FP math optimizations that assume no +-Infs"),
87 cl::location(NoInfsFPMath),
88 cl::init(false));
89static cl::opt<bool, true>
90EnableNoNaNsFPMath("enable-no-nans-fp-math",
91 cl::desc("Enable FP math optimizations that assume no NaNs"),
92 cl::location(NoNaNsFPMath),
Bill Wendling42adc732009-03-11 22:30:01 +000093 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000094static cl::opt<bool, true>
Bill Wendling42adc732009-03-11 22:30:01 +000095EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
96 cl::Hidden,
97 cl::desc("Force codegen to assume rounding mode can change dynamically"),
98 cl::location(HonorSignDependentRoundingFPMathOption),
99 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +0000100static cl::opt<bool, true>
101GenerateSoftFloatCalls("soft-float",
Bill Wendling42adc732009-03-11 22:30:01 +0000102 cl::desc("Generate software floating point library calls"),
103 cl::location(UseSoftFloat),
104 cl::init(false));
Anton Korobeynikov77d19432009-06-08 22:53:56 +0000105static cl::opt<llvm::FloatABI::ABIType, true>
106FloatABIForCalls("float-abi",
107 cl::desc("Choose float ABI type"),
108 cl::location(FloatABIType),
109 cl::init(FloatABI::Default),
110 cl::values(
111 clEnumValN(FloatABI::Default, "default",
112 "Target default float ABI type"),
113 clEnumValN(FloatABI::Soft, "soft",
114 "Soft float ABI (implied by -soft-float)"),
115 clEnumValN(FloatABI::Hard, "hard",
116 "Hard float ABI (uses FP registers)"),
117 clEnumValEnd));
Bill Wendling42adc732009-03-11 22:30:01 +0000118static cl::opt<bool, true>
Dan Gohmand78c4002008-05-13 00:00:25 +0000119DontPlaceZerosInBSS("nozero-initialized-in-bss",
Bill Wendling42adc732009-03-11 22:30:01 +0000120 cl::desc("Don't place zero-initialized symbols into bss section"),
121 cl::location(NoZerosInBSS),
122 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +0000123static cl::opt<bool, true>
Duncan Sands211427b2010-05-02 15:36:26 +0000124EnableJITExceptionHandling("jit-enable-eh",
125 cl::desc("Emit exception handling information"),
126 cl::location(JITExceptionHandling),
Bill Wendling42adc732009-03-11 22:30:01 +0000127 cl::init(false));
Reid Kleckner9a10db82009-09-20 23:52:43 +0000128// In debug builds, make this default to true.
129#ifdef NDEBUG
130#define EMIT_DEBUG false
131#else
132#define EMIT_DEBUG true
133#endif
134static cl::opt<bool, true>
135EmitJitDebugInfo("jit-emit-debug",
136 cl::desc("Emit debug information to debugger"),
137 cl::location(JITEmitDebugInfo),
138 cl::init(EMIT_DEBUG));
139#undef EMIT_DEBUG
140static cl::opt<bool, true>
141EmitJitDebugInfoToDisk("jit-emit-debug-to-disk",
142 cl::Hidden,
143 cl::desc("Emit debug info objfiles to disk"),
144 cl::location(JITEmitDebugInfoToDisk),
145 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +0000146static cl::opt<bool, true>
147EnableUnwindTables("unwind-tables",
Bill Wendling42adc732009-03-11 22:30:01 +0000148 cl::desc("Generate unwinding tables for all functions"),
149 cl::location(UnwindTablesMandatory),
150 cl::init(false));
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000151
Dan Gohmand78c4002008-05-13 00:00:25 +0000152static cl::opt<llvm::Reloc::Model, true>
Bill Wendling42adc732009-03-11 22:30:01 +0000153DefRelocationModel("relocation-model",
Dan Gohmand78c4002008-05-13 00:00:25 +0000154 cl::desc("Choose relocation model"),
155 cl::location(RelocationModel),
156 cl::init(Reloc::Default),
157 cl::values(
158 clEnumValN(Reloc::Default, "default",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000159 "Target default relocation model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000160 clEnumValN(Reloc::Static, "static",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000161 "Non-relocatable code"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000162 clEnumValN(Reloc::PIC_, "pic",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000163 "Fully relocatable, position independent code"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000164 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000165 "Relocatable external references, non-relocatable code"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000166 clEnumValEnd));
167static cl::opt<llvm::CodeModel::Model, true>
Bill Wendling42adc732009-03-11 22:30:01 +0000168DefCodeModel("code-model",
Dan Gohmand78c4002008-05-13 00:00:25 +0000169 cl::desc("Choose code model"),
170 cl::location(CMModel),
171 cl::init(CodeModel::Default),
172 cl::values(
173 clEnumValN(CodeModel::Default, "default",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000174 "Target default code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000175 clEnumValN(CodeModel::Small, "small",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000176 "Small code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000177 clEnumValN(CodeModel::Kernel, "kernel",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000178 "Kernel code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000179 clEnumValN(CodeModel::Medium, "medium",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000180 "Medium code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000181 clEnumValN(CodeModel::Large, "large",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000182 "Large code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000183 clEnumValEnd));
Dan Gohmand78c4002008-05-13 00:00:25 +0000184static cl::opt<bool, true>
Dan Gohman3464a5b2010-02-08 20:27:50 +0000185EnableGuaranteedTailCallOpt("tailcallopt",
Evan Cheng640b58b2010-01-27 00:10:09 +0000186 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
Dan Gohman3464a5b2010-02-08 20:27:50 +0000187 cl::location(GuaranteedTailCallOpt),
Bill Wendling42adc732009-03-11 22:30:01 +0000188 cl::init(false));
Evan Chengc963f6c2008-07-01 23:18:29 +0000189static cl::opt<unsigned, true>
190OverrideStackAlignment("stack-alignment",
Bill Wendling42adc732009-03-11 22:30:01 +0000191 cl::desc("Override default stack alignment"),
192 cl::location(StackAlignment),
193 cl::init(0));
Dan Gohmand78c4002008-05-13 00:00:25 +0000194static cl::opt<bool, true>
195EnableRealignStack("realign-stack",
Bill Wendling42adc732009-03-11 22:30:01 +0000196 cl::desc("Realign stack if needed"),
197 cl::location(RealignStack),
198 cl::init(true));
Evan Chengc963f6c2008-07-01 23:18:29 +0000199static cl::opt<bool, true>
Dale Johannesenc31eb202008-07-31 18:13:12 +0000200DisableSwitchTables(cl::Hidden, "disable-jump-tables",
Bill Wendling42adc732009-03-11 22:30:01 +0000201 cl::desc("Do not generate jump tables."),
202 cl::location(DisableJumpTables),
203 cl::init(false));
Owen Anderson1d338fc2008-10-07 20:22:28 +0000204static cl::opt<bool, true>
205EnableStrongPHIElim(cl::Hidden, "strong-phi-elim",
Bill Wendling42adc732009-03-11 22:30:01 +0000206 cl::desc("Use strong PHI elimination."),
207 cl::location(StrongPHIElim),
208 cl::init(false));
Evan Cheng74d92c12011-04-08 21:37:21 +0000209static cl::opt<std::string>
210TrapFuncName("trap-func", cl::Hidden,
211 cl::desc("Emit a call to trap function rather than a trap instruction"),
212 cl::init(""));
Chris Lattner5b212a32010-04-13 00:36:43 +0000213static cl::opt<bool>
214DataSections("fdata-sections",
215 cl::desc("Emit data into separate sections"),
216 cl::init(false));
217static cl::opt<bool>
218FunctionSections("ffunction-sections",
219 cl::desc("Emit functions into separate sections"),
220 cl::init(false));
Brian Gaeke8351d8c2004-03-04 19:16:23 +0000221//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +0000222// TargetMachine Class
223//
Misha Brukman3decf862004-08-10 23:10:25 +0000224
Daniel Dunbare8338102009-07-15 20:24:03 +0000225TargetMachine::TargetMachine(const Target &T)
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000226 : TheTarget(T), AsmInfo(0),
Rafael Espindolab58867c2010-11-19 02:26:16 +0000227 MCRelaxAll(false),
Rafael Espindolac5efca42011-01-23 18:50:12 +0000228 MCNoExecStack(false),
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000229 MCSaveTempLabels(false),
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000230 MCUseLoc(true),
231 MCUseCFI(true) {
Anton Korobeynikov77d19432009-06-08 22:53:56 +0000232 // Typically it will be subtargets that will adjust FloatABIType from Default
233 // to Soft or Hard.
234 if (UseSoftFloat)
235 FloatABIType = FloatABI::Soft;
236}
237
Chris Lattner5d236002003-12-28 21:23:38 +0000238TargetMachine::~TargetMachine() {
Jim Laskeyae92ce82006-09-07 23:39:26 +0000239 delete AsmInfo;
Chris Lattner5d236002003-12-28 21:23:38 +0000240}
241
Evan Cheng73136df2006-02-22 20:19:42 +0000242/// getRelocationModel - Returns the code generation relocation model. The
243/// choices are static, PIC, and dynamic-no-pic, and target default.
244Reloc::Model TargetMachine::getRelocationModel() {
245 return RelocationModel;
246}
247
248/// setRelocationModel - Sets the code generation relocation model.
249void TargetMachine::setRelocationModel(Reloc::Model Model) {
250 RelocationModel = Model;
251}
Evan Chengac4f66f2006-05-23 18:18:46 +0000252
Evan Cheng04417462006-07-06 01:53:36 +0000253/// getCodeModel - Returns the code model. The choices are small, kernel,
254/// medium, large, and target default.
255CodeModel::Model TargetMachine::getCodeModel() {
256 return CMModel;
257}
258
259/// setCodeModel - Sets the code model.
260void TargetMachine::setCodeModel(CodeModel::Model Model) {
261 CMModel = Model;
262}
263
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000264bool TargetMachine::getAsmVerbosityDefault() {
265 return AsmVerbosityDefault;
266}
267
268void TargetMachine::setAsmVerbosityDefault(bool V) {
269 AsmVerbosityDefault = V;
270}
271
Chris Lattner5b212a32010-04-13 00:36:43 +0000272bool TargetMachine::getFunctionSections() {
273 return FunctionSections;
274}
275
276bool TargetMachine::getDataSections() {
277 return DataSections;
278}
279
280void TargetMachine::setFunctionSections(bool V) {
281 FunctionSections = V;
282}
283
284void TargetMachine::setDataSections(bool V) {
285 DataSections = V;
286}
287
Evan Chengac4f66f2006-05-23 18:18:46 +0000288namespace llvm {
Evan Cheng4158a0f2010-04-21 03:18:23 +0000289 /// DisableFramePointerElim - This returns true if frame pointer elimination
290 /// optimization should be disabled for the given machine function.
291 bool DisableFramePointerElim(const MachineFunction &MF) {
Bill Wendling02d33682010-05-17 23:09:50 +0000292 // Check to see if we should eliminate non-leaf frame pointers and then
293 // check to see if we should eliminate all frame pointers.
Bill Wendling4ed63f82010-05-18 21:47:08 +0000294 if (NoFramePointerElimNonLeaf && !NoFramePointerElim) {
Evan Cheng4158a0f2010-04-21 03:18:23 +0000295 const MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingdb4bc542010-05-14 21:28:24 +0000296 return MFI->hasCalls();
Evan Cheng4158a0f2010-04-21 03:18:23 +0000297 }
Bill Wendling02d33682010-05-17 23:09:50 +0000298
299 return NoFramePointerElim;
Evan Cheng4158a0f2010-04-21 03:18:23 +0000300 }
301
Mon P Wangf67448a2009-03-20 05:06:58 +0000302 /// LessPreciseFPMAD - This flag return true when -enable-fp-mad option
303 /// is specified on the command line. When this flag is off(default), the
304 /// code generator is not allowed to generate mad (multiply add) if the
305 /// result is "less precise" than doing those operations individually.
306 bool LessPreciseFPMAD() { return UnsafeFPMath || LessPreciseFPMADOption; }
307
Chris Lattner6ac3a372007-05-03 00:16:07 +0000308 /// HonorSignDependentRoundingFPMath - Return true if the codegen must assume
309 /// that the rounding mode of the FPU can change from its default.
310 bool HonorSignDependentRoundingFPMath() {
311 return !UnsafeFPMath && HonorSignDependentRoundingFPMathOption;
312 }
Evan Cheng74d92c12011-04-08 21:37:21 +0000313
314 /// getTrapFunctionName - If this returns a non-empty string, this means isel
315 /// should lower Intrinsic::trap to a call to the specified function name
316 /// instead of an ISD::TRAP node.
317 StringRef getTrapFunctionName() {
318 return TrapFuncName;
319 }
Reid Spencer94531bf2006-05-24 19:05:21 +0000320}