blob: 865e41aa1dd4a19083e6ff09e999be7e18db22aa [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;
Evan Cheng04417462006-07-06 01:53:36 +000043 CodeModel::Model CMModel;
Dan Gohman3464a5b2010-02-08 20:27:50 +000044 bool GuaranteedTailCallOpt;
Evan Cheng8b2a2a12011-06-23 18:15:47 +000045 unsigned StackAlignmentOverride;
Evan Chengc963f6c2008-07-01 23:18:29 +000046 bool RealignStack;
Dale Johannesenc31eb202008-07-31 18:13:12 +000047 bool DisableJumpTables;
Owen Anderson1d338fc2008-10-07 20:22:28 +000048 bool StrongPHIElim;
Evan Chenga7c7b542011-04-07 00:58:44 +000049 bool HasDivModLibcall;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000050 bool AsmVerbosityDefault(false);
Chris Lattneraa2372562006-05-24 17:04:05 +000051}
Misha Brukman069ca062004-06-21 21:08:45 +000052
Bill Wendling42adc732009-03-11 22:30:01 +000053static cl::opt<bool, true>
54PrintCode("print-machineinstrs",
Dan Gohmand78c4002008-05-13 00:00:25 +000055 cl::desc("Print generated machine code"),
56 cl::location(PrintMachineCode), cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000057static cl::opt<bool, true>
Evan Cheng864541a2008-05-30 22:39:18 +000058DisableFPElim("disable-fp-elim",
Bill Wendling42adc732009-03-11 22:30:01 +000059 cl::desc("Disable frame pointer elimination optimization"),
60 cl::location(NoFramePointerElim),
61 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000062static cl::opt<bool, true>
Evan Cheng4158a0f2010-04-21 03:18:23 +000063DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
64 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
65 cl::location(NoFramePointerElimNonLeaf),
66 cl::init(false));
67static cl::opt<bool, true>
Dan Gohmand78c4002008-05-13 00:00:25 +000068DisableExcessPrecision("disable-excess-fp-precision",
Bill Wendling42adc732009-03-11 22:30:01 +000069 cl::desc("Disable optimizations that may increase FP precision"),
70 cl::location(NoExcessFPPrecision),
71 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000072static cl::opt<bool, true>
Mon P Wangf67448a2009-03-20 05:06:58 +000073EnableFPMAD("enable-fp-mad",
74 cl::desc("Enable less precise MAD instructions to be generated"),
75 cl::location(LessPreciseFPMADOption),
76 cl::init(false));
77static cl::opt<bool, true>
Dan Gohmand78c4002008-05-13 00:00:25 +000078EnableUnsafeFPMath("enable-unsafe-fp-math",
Bill Wendling42adc732009-03-11 22:30:01 +000079 cl::desc("Enable optimizations that may decrease FP precision"),
80 cl::location(UnsafeFPMath),
81 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000082static cl::opt<bool, true>
Evan Cheng55f0c6b2010-07-15 22:07:12 +000083EnableNoInfsFPMath("enable-no-infs-fp-math",
84 cl::desc("Enable FP math optimizations that assume no +-Infs"),
85 cl::location(NoInfsFPMath),
86 cl::init(false));
87static cl::opt<bool, true>
88EnableNoNaNsFPMath("enable-no-nans-fp-math",
89 cl::desc("Enable FP math optimizations that assume no NaNs"),
90 cl::location(NoNaNsFPMath),
Bill Wendling42adc732009-03-11 22:30:01 +000091 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000092static cl::opt<bool, true>
Bill Wendling42adc732009-03-11 22:30:01 +000093EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
94 cl::Hidden,
95 cl::desc("Force codegen to assume rounding mode can change dynamically"),
96 cl::location(HonorSignDependentRoundingFPMathOption),
97 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +000098static cl::opt<bool, true>
99GenerateSoftFloatCalls("soft-float",
Bill Wendling42adc732009-03-11 22:30:01 +0000100 cl::desc("Generate software floating point library calls"),
101 cl::location(UseSoftFloat),
102 cl::init(false));
Anton Korobeynikov77d19432009-06-08 22:53:56 +0000103static cl::opt<llvm::FloatABI::ABIType, true>
104FloatABIForCalls("float-abi",
105 cl::desc("Choose float ABI type"),
106 cl::location(FloatABIType),
107 cl::init(FloatABI::Default),
108 cl::values(
109 clEnumValN(FloatABI::Default, "default",
110 "Target default float ABI type"),
111 clEnumValN(FloatABI::Soft, "soft",
112 "Soft float ABI (implied by -soft-float)"),
113 clEnumValN(FloatABI::Hard, "hard",
114 "Hard float ABI (uses FP registers)"),
115 clEnumValEnd));
Bill Wendling42adc732009-03-11 22:30:01 +0000116static cl::opt<bool, true>
Dan Gohmand78c4002008-05-13 00:00:25 +0000117DontPlaceZerosInBSS("nozero-initialized-in-bss",
Bill Wendling42adc732009-03-11 22:30:01 +0000118 cl::desc("Don't place zero-initialized symbols into bss section"),
119 cl::location(NoZerosInBSS),
120 cl::init(false));
Dan Gohmand78c4002008-05-13 00:00:25 +0000121static cl::opt<bool, true>
Duncan Sands211427b2010-05-02 15:36:26 +0000122EnableJITExceptionHandling("jit-enable-eh",
123 cl::desc("Emit exception handling information"),
124 cl::location(JITExceptionHandling),
Bill Wendling42adc732009-03-11 22:30:01 +0000125 cl::init(false));
Reid Kleckner9a10db82009-09-20 23:52:43 +0000126// In debug builds, make this default to true.
127#ifdef NDEBUG
128#define EMIT_DEBUG false
129#else
130#define EMIT_DEBUG true
131#endif
132static cl::opt<bool, true>
133EmitJitDebugInfo("jit-emit-debug",
134 cl::desc("Emit debug information to debugger"),
135 cl::location(JITEmitDebugInfo),
136 cl::init(EMIT_DEBUG));
137#undef EMIT_DEBUG
138static cl::opt<bool, true>
139EmitJitDebugInfoToDisk("jit-emit-debug-to-disk",
140 cl::Hidden,
141 cl::desc("Emit debug info objfiles to disk"),
142 cl::location(JITEmitDebugInfoToDisk),
143 cl::init(false));
Arnold Schwaighofer9ccea992007-10-11 19:40:01 +0000144
Dan Gohmand78c4002008-05-13 00:00:25 +0000145static cl::opt<llvm::CodeModel::Model, true>
Bill Wendling42adc732009-03-11 22:30:01 +0000146DefCodeModel("code-model",
Dan Gohmand78c4002008-05-13 00:00:25 +0000147 cl::desc("Choose code model"),
148 cl::location(CMModel),
149 cl::init(CodeModel::Default),
150 cl::values(
151 clEnumValN(CodeModel::Default, "default",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000152 "Target default code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000153 clEnumValN(CodeModel::Small, "small",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000154 "Small code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000155 clEnumValN(CodeModel::Kernel, "kernel",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000156 "Kernel code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000157 clEnumValN(CodeModel::Medium, "medium",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000158 "Medium code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000159 clEnumValN(CodeModel::Large, "large",
Dan Gohman9c4b7d52008-10-14 20:25:08 +0000160 "Large code model"),
Dan Gohmand78c4002008-05-13 00:00:25 +0000161 clEnumValEnd));
Dan Gohmand78c4002008-05-13 00:00:25 +0000162static cl::opt<bool, true>
Dan Gohman3464a5b2010-02-08 20:27:50 +0000163EnableGuaranteedTailCallOpt("tailcallopt",
Evan Cheng640b58b2010-01-27 00:10:09 +0000164 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
Dan Gohman3464a5b2010-02-08 20:27:50 +0000165 cl::location(GuaranteedTailCallOpt),
Bill Wendling42adc732009-03-11 22:30:01 +0000166 cl::init(false));
Evan Chengc963f6c2008-07-01 23:18:29 +0000167static cl::opt<unsigned, true>
168OverrideStackAlignment("stack-alignment",
Bill Wendling42adc732009-03-11 22:30:01 +0000169 cl::desc("Override default stack alignment"),
Evan Cheng8b2a2a12011-06-23 18:15:47 +0000170 cl::location(StackAlignmentOverride),
Bill Wendling42adc732009-03-11 22:30:01 +0000171 cl::init(0));
Dan Gohmand78c4002008-05-13 00:00:25 +0000172static cl::opt<bool, true>
173EnableRealignStack("realign-stack",
Bill Wendling42adc732009-03-11 22:30:01 +0000174 cl::desc("Realign stack if needed"),
175 cl::location(RealignStack),
176 cl::init(true));
Evan Chengc963f6c2008-07-01 23:18:29 +0000177static cl::opt<bool, true>
Dale Johannesenc31eb202008-07-31 18:13:12 +0000178DisableSwitchTables(cl::Hidden, "disable-jump-tables",
Bill Wendling42adc732009-03-11 22:30:01 +0000179 cl::desc("Do not generate jump tables."),
180 cl::location(DisableJumpTables),
181 cl::init(false));
Owen Anderson1d338fc2008-10-07 20:22:28 +0000182static cl::opt<bool, true>
183EnableStrongPHIElim(cl::Hidden, "strong-phi-elim",
Bill Wendling42adc732009-03-11 22:30:01 +0000184 cl::desc("Use strong PHI elimination."),
185 cl::location(StrongPHIElim),
186 cl::init(false));
Evan Cheng74d92c12011-04-08 21:37:21 +0000187static cl::opt<std::string>
188TrapFuncName("trap-func", cl::Hidden,
189 cl::desc("Emit a call to trap function rather than a trap instruction"),
190 cl::init(""));
Chris Lattner5b212a32010-04-13 00:36:43 +0000191static cl::opt<bool>
192DataSections("fdata-sections",
193 cl::desc("Emit data into separate sections"),
194 cl::init(false));
195static cl::opt<bool>
196FunctionSections("ffunction-sections",
197 cl::desc("Emit functions into separate sections"),
198 cl::init(false));
Brian Gaeke8351d8c2004-03-04 19:16:23 +0000199//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +0000200// TargetMachine Class
201//
Misha Brukman3decf862004-08-10 23:10:25 +0000202
Evan Cheng4d1ca962011-07-08 01:53:10 +0000203TargetMachine::TargetMachine(const Target &T,
204 StringRef TT, StringRef CPU, StringRef FS)
205 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS), AsmInfo(0),
Rafael Espindolab58867c2010-11-19 02:26:16 +0000206 MCRelaxAll(false),
Rafael Espindolac5efca42011-01-23 18:50:12 +0000207 MCNoExecStack(false),
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000208 MCSaveTempLabels(false),
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000209 MCUseLoc(true),
210 MCUseCFI(true) {
Anton Korobeynikov77d19432009-06-08 22:53:56 +0000211 // Typically it will be subtargets that will adjust FloatABIType from Default
212 // to Soft or Hard.
213 if (UseSoftFloat)
214 FloatABIType = FloatABI::Soft;
215}
216
Chris Lattner5d236002003-12-28 21:23:38 +0000217TargetMachine::~TargetMachine() {
Jim Laskeyae92ce82006-09-07 23:39:26 +0000218 delete AsmInfo;
Chris Lattner5d236002003-12-28 21:23:38 +0000219}
220
Evan Cheng73136df2006-02-22 20:19:42 +0000221/// getRelocationModel - Returns the code generation relocation model. The
222/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng2129f592011-07-19 06:37:02 +0000223Reloc::Model TargetMachine::getRelocationModel() const {
224 if (!CodeGenInfo)
225 return Reloc::Default;
226 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +0000227}
Evan Chengac4f66f2006-05-23 18:18:46 +0000228
Evan Cheng04417462006-07-06 01:53:36 +0000229/// getCodeModel - Returns the code model. The choices are small, kernel,
230/// medium, large, and target default.
231CodeModel::Model TargetMachine::getCodeModel() {
232 return CMModel;
233}
234
235/// setCodeModel - Sets the code model.
236void TargetMachine::setCodeModel(CodeModel::Model Model) {
237 CMModel = Model;
238}
239
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000240bool TargetMachine::getAsmVerbosityDefault() {
241 return AsmVerbosityDefault;
242}
243
244void TargetMachine::setAsmVerbosityDefault(bool V) {
245 AsmVerbosityDefault = V;
246}
247
Chris Lattner5b212a32010-04-13 00:36:43 +0000248bool TargetMachine::getFunctionSections() {
249 return FunctionSections;
250}
251
252bool TargetMachine::getDataSections() {
253 return DataSections;
254}
255
256void TargetMachine::setFunctionSections(bool V) {
257 FunctionSections = V;
258}
259
260void TargetMachine::setDataSections(bool V) {
261 DataSections = V;
262}
263
Evan Chengac4f66f2006-05-23 18:18:46 +0000264namespace llvm {
Evan Cheng4158a0f2010-04-21 03:18:23 +0000265 /// DisableFramePointerElim - This returns true if frame pointer elimination
266 /// optimization should be disabled for the given machine function.
267 bool DisableFramePointerElim(const MachineFunction &MF) {
Bill Wendling02d33682010-05-17 23:09:50 +0000268 // Check to see if we should eliminate non-leaf frame pointers and then
269 // check to see if we should eliminate all frame pointers.
Bill Wendling4ed63f82010-05-18 21:47:08 +0000270 if (NoFramePointerElimNonLeaf && !NoFramePointerElim) {
Evan Cheng4158a0f2010-04-21 03:18:23 +0000271 const MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingdb4bc542010-05-14 21:28:24 +0000272 return MFI->hasCalls();
Evan Cheng4158a0f2010-04-21 03:18:23 +0000273 }
Bill Wendling02d33682010-05-17 23:09:50 +0000274
275 return NoFramePointerElim;
Evan Cheng4158a0f2010-04-21 03:18:23 +0000276 }
277
Mon P Wangf67448a2009-03-20 05:06:58 +0000278 /// LessPreciseFPMAD - This flag return true when -enable-fp-mad option
279 /// is specified on the command line. When this flag is off(default), the
280 /// code generator is not allowed to generate mad (multiply add) if the
281 /// result is "less precise" than doing those operations individually.
282 bool LessPreciseFPMAD() { return UnsafeFPMath || LessPreciseFPMADOption; }
283
Chris Lattner6ac3a372007-05-03 00:16:07 +0000284 /// HonorSignDependentRoundingFPMath - Return true if the codegen must assume
285 /// that the rounding mode of the FPU can change from its default.
286 bool HonorSignDependentRoundingFPMath() {
287 return !UnsafeFPMath && HonorSignDependentRoundingFPMathOption;
288 }
Evan Cheng74d92c12011-04-08 21:37:21 +0000289
290 /// getTrapFunctionName - If this returns a non-empty string, this means isel
291 /// should lower Intrinsic::trap to a call to the specified function name
292 /// instead of an ISD::TRAP node.
293 StringRef getTrapFunctionName() {
294 return TrapFuncName;
295 }
Reid Spencer94531bf2006-05-24 19:05:21 +0000296}