blob: 3f58e84390149c506102a386c7fdfb2732e2d217 [file] [log] [blame]
Chris Lattnerb26bcc52001-09-14 05:34:53 +00001//===-- TargetMachine.cpp - General Target Information ---------------------==//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb26bcc52001-09-14 05:34:53 +00009//
10// This file describes the general parts of a Target machine.
11//
12//===----------------------------------------------------------------------===//
Vikram S. Advedaae6992001-07-21 12:42:08 +000013
Evan Chenge5667632010-04-21 03:18:23 +000014#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000016#include "llvm/MC/MCAsmInfo.h"
Misha Brukmane67d5fb2004-06-21 21:20:23 +000017#include "llvm/Target/TargetMachine.h"
Evan Cheng4c1aa862006-02-22 20:19:42 +000018#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/CommandLine.h"
Chris Lattnerf70e0c22003-12-28 21:23:38 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Vikram S. Advee1f72802002-09-16 15:39:26 +000022//---------------------------------------------------------------------------
Brian Gaeke323819e2004-03-04 19:16:23 +000023// Command-line options that tend to be useful on more than one back-end.
24//
25
Misha Brukmanf90e4022004-06-21 21:44:12 +000026namespace llvm {
Mon P Wangbc65ca82009-03-20 05:06:58 +000027 bool LessPreciseFPMADOption;
Misha Brukmanf90e4022004-06-21 21:44:12 +000028 bool PrintMachineCode;
29 bool NoFramePointerElim;
Evan Chenge5667632010-04-21 03:18:23 +000030 bool NoFramePointerElimNonLeaf;
Chris Lattner45554a62005-01-15 06:00:32 +000031 bool NoExcessFPPrecision;
Chris Lattner34f74a62005-04-30 04:09:52 +000032 bool UnsafeFPMath;
Evan Cheng60108e92010-07-15 22:07:12 +000033 bool NoInfsFPMath;
34 bool NoNaNsFPMath;
Chris Lattnera7ad3d12007-05-03 00:27:11 +000035 bool HonorSignDependentRoundingFPMathOption;
Evan Chengc9ab2f32006-12-09 02:41:30 +000036 bool UseSoftFloat;
Anton Korobeynikov0eebf652009-06-08 22:53:56 +000037 FloatABI::ABIType FloatABIType;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000038 bool NoImplicitFloat;
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +000039 bool NoZerosInBSS;
Duncan Sands57b6e9e2010-05-02 15:36:26 +000040 bool JITExceptionHandling;
Reid Kleckner27632172009-09-20 23:52:43 +000041 bool JITEmitDebugInfo;
42 bool JITEmitDebugInfoToDisk;
Dan Gohman1797ed52010-02-08 20:27:50 +000043 bool GuaranteedTailCallOpt;
Evan Chengf05589d2011-06-23 18:15:47 +000044 unsigned StackAlignmentOverride;
Evan Cheng6547e402008-07-01 23:18:29 +000045 bool RealignStack;
Dale Johannesen72324642008-07-31 18:13:12 +000046 bool DisableJumpTables;
Owen Anderson95dad832008-10-07 20:22:28 +000047 bool StrongPHIElim;
Evan Cheng2c69f8e2011-04-07 00:58:44 +000048 bool HasDivModLibcall;
Evan Cheng42bf74b2009-03-25 01:47:28 +000049 bool AsmVerbosityDefault(false);
Rafael Espindola0f9827c2011-08-30 19:29:02 +000050 bool EnableSegmentedStacks;
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000051}
Misha Brukman0fb5a662004-06-21 21:08:45 +000052
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000053static cl::opt<bool, true>
54PrintCode("print-machineinstrs",
Dan Gohman844731a2008-05-13 00:00:25 +000055 cl::desc("Print generated machine code"),
56 cl::location(PrintMachineCode), cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000057static cl::opt<bool, true>
Evan Cheng89df08c2008-05-30 22:39:18 +000058DisableFPElim("disable-fp-elim",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000059 cl::desc("Disable frame pointer elimination optimization"),
60 cl::location(NoFramePointerElim),
61 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000062static cl::opt<bool, true>
Evan Chenge5667632010-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 Gohman844731a2008-05-13 00:00:25 +000068DisableExcessPrecision("disable-excess-fp-precision",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000069 cl::desc("Disable optimizations that may increase FP precision"),
70 cl::location(NoExcessFPPrecision),
71 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000072static cl::opt<bool, true>
Mon P Wangbc65ca82009-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 Gohman844731a2008-05-13 00:00:25 +000078EnableUnsafeFPMath("enable-unsafe-fp-math",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000079 cl::desc("Enable optimizations that may decrease FP precision"),
80 cl::location(UnsafeFPMath),
81 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000082static cl::opt<bool, true>
Evan Cheng60108e92010-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 Wendlingf9abd7e2009-03-11 22:30:01 +000091 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000092static cl::opt<bool, true>
Bill Wendlingf9abd7e2009-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 Gohman844731a2008-05-13 00:00:25 +000098static cl::opt<bool, true>
99GenerateSoftFloatCalls("soft-float",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000100 cl::desc("Generate software floating point library calls"),
101 cl::location(UseSoftFloat),
102 cl::init(false));
Anton Korobeynikov0eebf652009-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 Wendlingf9abd7e2009-03-11 22:30:01 +0000116static cl::opt<bool, true>
Dan Gohman844731a2008-05-13 00:00:25 +0000117DontPlaceZerosInBSS("nozero-initialized-in-bss",
Bill Wendlingf9abd7e2009-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 Gohman844731a2008-05-13 00:00:25 +0000121static cl::opt<bool, true>
Duncan Sands57b6e9e2010-05-02 15:36:26 +0000122EnableJITExceptionHandling("jit-enable-eh",
123 cl::desc("Emit exception handling information"),
124 cl::location(JITExceptionHandling),
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000125 cl::init(false));
Reid Kleckner27632172009-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 Schwaighoferc85e1712007-10-11 19:40:01 +0000144
Dan Gohman844731a2008-05-13 00:00:25 +0000145static cl::opt<bool, true>
Dan Gohman1797ed52010-02-08 20:27:50 +0000146EnableGuaranteedTailCallOpt("tailcallopt",
Evan Cheng8c86db52010-01-27 00:10:09 +0000147 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
Dan Gohman1797ed52010-02-08 20:27:50 +0000148 cl::location(GuaranteedTailCallOpt),
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000149 cl::init(false));
Evan Cheng6547e402008-07-01 23:18:29 +0000150static cl::opt<unsigned, true>
151OverrideStackAlignment("stack-alignment",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000152 cl::desc("Override default stack alignment"),
Evan Chengf05589d2011-06-23 18:15:47 +0000153 cl::location(StackAlignmentOverride),
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000154 cl::init(0));
Dan Gohman844731a2008-05-13 00:00:25 +0000155static cl::opt<bool, true>
156EnableRealignStack("realign-stack",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000157 cl::desc("Realign stack if needed"),
158 cl::location(RealignStack),
159 cl::init(true));
Evan Cheng6547e402008-07-01 23:18:29 +0000160static cl::opt<bool, true>
Dale Johannesen72324642008-07-31 18:13:12 +0000161DisableSwitchTables(cl::Hidden, "disable-jump-tables",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000162 cl::desc("Do not generate jump tables."),
163 cl::location(DisableJumpTables),
164 cl::init(false));
Owen Anderson95dad832008-10-07 20:22:28 +0000165static cl::opt<bool, true>
166EnableStrongPHIElim(cl::Hidden, "strong-phi-elim",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000167 cl::desc("Use strong PHI elimination."),
168 cl::location(StrongPHIElim),
169 cl::init(false));
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000170static cl::opt<std::string>
171TrapFuncName("trap-func", cl::Hidden,
172 cl::desc("Emit a call to trap function rather than a trap instruction"),
173 cl::init(""));
Chris Lattner43ac7212010-04-13 00:36:43 +0000174static cl::opt<bool>
175DataSections("fdata-sections",
176 cl::desc("Emit data into separate sections"),
177 cl::init(false));
178static cl::opt<bool>
179FunctionSections("ffunction-sections",
180 cl::desc("Emit functions into separate sections"),
181 cl::init(false));
Rafael Espindola0f9827c2011-08-30 19:29:02 +0000182static cl::opt<bool, true>
183SegmentedStacks("segmented-stacks",
184 cl::desc("Use segmented stacks if possible."),
185 cl::location(EnableSegmentedStacks),
186 cl::init(false));
187
Brian Gaeke323819e2004-03-04 19:16:23 +0000188//---------------------------------------------------------------------------
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000189// TargetMachine Class
190//
Misha Brukman167deff2004-08-10 23:10:25 +0000191
Evan Chengebdeeab2011-07-08 01:53:10 +0000192TargetMachine::TargetMachine(const Target &T,
193 StringRef TT, StringRef CPU, StringRef FS)
Benjamin Kramere236dc62011-07-20 01:27:58 +0000194 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
195 CodeGenInfo(0), AsmInfo(0),
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000196 MCRelaxAll(false),
Rafael Espindoladd0dfdf2011-01-23 18:50:12 +0000197 MCNoExecStack(false),
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000198 MCSaveTempLabels(false),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000199 MCUseLoc(true),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000200 MCUseCFI(true),
201 MCUseDwarfDirectory(true) {
Anton Korobeynikov0eebf652009-06-08 22:53:56 +0000202 // Typically it will be subtargets that will adjust FloatABIType from Default
203 // to Soft or Hard.
204 if (UseSoftFloat)
205 FloatABIType = FloatABI::Soft;
206}
207
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000208TargetMachine::~TargetMachine() {
Benjamin Kramere236dc62011-07-20 01:27:58 +0000209 delete CodeGenInfo;
Jim Laskeyfde1b3b2006-09-07 23:39:26 +0000210 delete AsmInfo;
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000211}
212
Evan Cheng4c1aa862006-02-22 20:19:42 +0000213/// getRelocationModel - Returns the code generation relocation model. The
214/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng43966132011-07-19 06:37:02 +0000215Reloc::Model TargetMachine::getRelocationModel() const {
216 if (!CodeGenInfo)
217 return Reloc::Default;
218 return CodeGenInfo->getRelocationModel();
Evan Cheng4c1aa862006-02-22 20:19:42 +0000219}
Evan Cheng80235d52006-05-23 18:18:46 +0000220
Evan Cheng152ed052006-07-06 01:53:36 +0000221/// getCodeModel - Returns the code model. The choices are small, kernel,
222/// medium, large, and target default.
Evan Cheng34ad6db2011-07-20 07:51:56 +0000223CodeModel::Model TargetMachine::getCodeModel() const {
224 if (!CodeGenInfo)
225 return CodeModel::Default;
226 return CodeGenInfo->getCodeModel();
Evan Cheng152ed052006-07-06 01:53:36 +0000227}
228
Evan Cheng42bf74b2009-03-25 01:47:28 +0000229bool TargetMachine::getAsmVerbosityDefault() {
230 return AsmVerbosityDefault;
231}
232
233void TargetMachine::setAsmVerbosityDefault(bool V) {
234 AsmVerbosityDefault = V;
235}
236
Chris Lattner43ac7212010-04-13 00:36:43 +0000237bool TargetMachine::getFunctionSections() {
238 return FunctionSections;
239}
240
241bool TargetMachine::getDataSections() {
242 return DataSections;
243}
244
245void TargetMachine::setFunctionSections(bool V) {
246 FunctionSections = V;
247}
248
249void TargetMachine::setDataSections(bool V) {
250 DataSections = V;
251}
252
Evan Cheng80235d52006-05-23 18:18:46 +0000253namespace llvm {
Evan Chenge5667632010-04-21 03:18:23 +0000254 /// DisableFramePointerElim - This returns true if frame pointer elimination
255 /// optimization should be disabled for the given machine function.
256 bool DisableFramePointerElim(const MachineFunction &MF) {
Bill Wendling53f76022010-05-17 23:09:50 +0000257 // Check to see if we should eliminate non-leaf frame pointers and then
258 // check to see if we should eliminate all frame pointers.
Bill Wendling2abc93d2010-05-18 21:47:08 +0000259 if (NoFramePointerElimNonLeaf && !NoFramePointerElim) {
Evan Chenge5667632010-04-21 03:18:23 +0000260 const MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling0fc546b2010-05-14 21:28:24 +0000261 return MFI->hasCalls();
Evan Chenge5667632010-04-21 03:18:23 +0000262 }
Bill Wendling53f76022010-05-17 23:09:50 +0000263
264 return NoFramePointerElim;
Evan Chenge5667632010-04-21 03:18:23 +0000265 }
266
Mon P Wangbc65ca82009-03-20 05:06:58 +0000267 /// LessPreciseFPMAD - This flag return true when -enable-fp-mad option
268 /// is specified on the command line. When this flag is off(default), the
269 /// code generator is not allowed to generate mad (multiply add) if the
270 /// result is "less precise" than doing those operations individually.
271 bool LessPreciseFPMAD() { return UnsafeFPMath || LessPreciseFPMADOption; }
272
Chris Lattneraa4f1e12007-05-03 00:16:07 +0000273 /// HonorSignDependentRoundingFPMath - Return true if the codegen must assume
274 /// that the rounding mode of the FPU can change from its default.
275 bool HonorSignDependentRoundingFPMath() {
276 return !UnsafeFPMath && HonorSignDependentRoundingFPMathOption;
277 }
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000278
279 /// getTrapFunctionName - If this returns a non-empty string, this means isel
280 /// should lower Intrinsic::trap to a call to the specified function name
281 /// instead of an ISD::TRAP node.
282 StringRef getTrapFunctionName() {
283 return TrapFuncName;
284 }
Reid Spencer077b3872006-05-24 19:05:21 +0000285}