blob: c4790a289937f8deae6cbdc91fb7054e0476e99a [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;
Dale Johannesen3541af72008-04-14 17:54:17 +000043 bool UnwindTablesMandatory;
Evan Cheng4c1aa862006-02-22 20:19:42 +000044 Reloc::Model RelocationModel;
Evan Cheng152ed052006-07-06 01:53:36 +000045 CodeModel::Model CMModel;
Dan Gohman1797ed52010-02-08 20:27:50 +000046 bool GuaranteedTailCallOpt;
Anton Korobeynikov45709ae2008-04-23 18:18:10 +000047 unsigned StackAlignment;
Evan Cheng6547e402008-07-01 23:18:29 +000048 bool RealignStack;
Dale Johannesen72324642008-07-31 18:13:12 +000049 bool DisableJumpTables;
Owen Anderson95dad832008-10-07 20:22:28 +000050 bool StrongPHIElim;
Evan Cheng42bf74b2009-03-25 01:47:28 +000051 bool AsmVerbosityDefault(false);
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000052}
Misha Brukman0fb5a662004-06-21 21:08:45 +000053
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000054static cl::opt<bool, true>
55PrintCode("print-machineinstrs",
Dan Gohman844731a2008-05-13 00:00:25 +000056 cl::desc("Print generated machine code"),
57 cl::location(PrintMachineCode), cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000058static cl::opt<bool, true>
Evan Cheng89df08c2008-05-30 22:39:18 +000059DisableFPElim("disable-fp-elim",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000060 cl::desc("Disable frame pointer elimination optimization"),
61 cl::location(NoFramePointerElim),
62 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000063static cl::opt<bool, true>
Evan Chenge5667632010-04-21 03:18:23 +000064DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
65 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
66 cl::location(NoFramePointerElimNonLeaf),
67 cl::init(false));
68static cl::opt<bool, true>
Dan Gohman844731a2008-05-13 00:00:25 +000069DisableExcessPrecision("disable-excess-fp-precision",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000070 cl::desc("Disable optimizations that may increase FP precision"),
71 cl::location(NoExcessFPPrecision),
72 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000073static cl::opt<bool, true>
Mon P Wangbc65ca82009-03-20 05:06:58 +000074EnableFPMAD("enable-fp-mad",
75 cl::desc("Enable less precise MAD instructions to be generated"),
76 cl::location(LessPreciseFPMADOption),
77 cl::init(false));
78static cl::opt<bool, true>
Dan Gohman844731a2008-05-13 00:00:25 +000079EnableUnsafeFPMath("enable-unsafe-fp-math",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000080 cl::desc("Enable optimizations that may decrease FP precision"),
81 cl::location(UnsafeFPMath),
82 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000083static cl::opt<bool, true>
Evan Cheng60108e92010-07-15 22:07:12 +000084EnableNoInfsFPMath("enable-no-infs-fp-math",
85 cl::desc("Enable FP math optimizations that assume no +-Infs"),
86 cl::location(NoInfsFPMath),
87 cl::init(false));
88static cl::opt<bool, true>
89EnableNoNaNsFPMath("enable-no-nans-fp-math",
90 cl::desc("Enable FP math optimizations that assume no NaNs"),
91 cl::location(NoNaNsFPMath),
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000092 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000093static cl::opt<bool, true>
Bill Wendlingf9abd7e2009-03-11 22:30:01 +000094EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
95 cl::Hidden,
96 cl::desc("Force codegen to assume rounding mode can change dynamically"),
97 cl::location(HonorSignDependentRoundingFPMathOption),
98 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +000099static cl::opt<bool, true>
100GenerateSoftFloatCalls("soft-float",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000101 cl::desc("Generate software floating point library calls"),
102 cl::location(UseSoftFloat),
103 cl::init(false));
Anton Korobeynikov0eebf652009-06-08 22:53:56 +0000104static cl::opt<llvm::FloatABI::ABIType, true>
105FloatABIForCalls("float-abi",
106 cl::desc("Choose float ABI type"),
107 cl::location(FloatABIType),
108 cl::init(FloatABI::Default),
109 cl::values(
110 clEnumValN(FloatABI::Default, "default",
111 "Target default float ABI type"),
112 clEnumValN(FloatABI::Soft, "soft",
113 "Soft float ABI (implied by -soft-float)"),
114 clEnumValN(FloatABI::Hard, "hard",
115 "Hard float ABI (uses FP registers)"),
116 clEnumValEnd));
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000117static cl::opt<bool, true>
Dan Gohman844731a2008-05-13 00:00:25 +0000118DontPlaceZerosInBSS("nozero-initialized-in-bss",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000119 cl::desc("Don't place zero-initialized symbols into bss section"),
120 cl::location(NoZerosInBSS),
121 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +0000122static cl::opt<bool, true>
Duncan Sands57b6e9e2010-05-02 15:36:26 +0000123EnableJITExceptionHandling("jit-enable-eh",
124 cl::desc("Emit exception handling information"),
125 cl::location(JITExceptionHandling),
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000126 cl::init(false));
Reid Kleckner27632172009-09-20 23:52:43 +0000127// In debug builds, make this default to true.
128#ifdef NDEBUG
129#define EMIT_DEBUG false
130#else
131#define EMIT_DEBUG true
132#endif
133static cl::opt<bool, true>
134EmitJitDebugInfo("jit-emit-debug",
135 cl::desc("Emit debug information to debugger"),
136 cl::location(JITEmitDebugInfo),
137 cl::init(EMIT_DEBUG));
138#undef EMIT_DEBUG
139static cl::opt<bool, true>
140EmitJitDebugInfoToDisk("jit-emit-debug-to-disk",
141 cl::Hidden,
142 cl::desc("Emit debug info objfiles to disk"),
143 cl::location(JITEmitDebugInfoToDisk),
144 cl::init(false));
Dan Gohman844731a2008-05-13 00:00:25 +0000145static cl::opt<bool, true>
146EnableUnwindTables("unwind-tables",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000147 cl::desc("Generate unwinding tables for all functions"),
148 cl::location(UnwindTablesMandatory),
149 cl::init(false));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000150
Dan Gohman844731a2008-05-13 00:00:25 +0000151static cl::opt<llvm::Reloc::Model, true>
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000152DefRelocationModel("relocation-model",
Dan Gohman844731a2008-05-13 00:00:25 +0000153 cl::desc("Choose relocation model"),
154 cl::location(RelocationModel),
155 cl::init(Reloc::Default),
156 cl::values(
157 clEnumValN(Reloc::Default, "default",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000158 "Target default relocation model"),
Dan Gohman844731a2008-05-13 00:00:25 +0000159 clEnumValN(Reloc::Static, "static",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000160 "Non-relocatable code"),
Dan Gohman844731a2008-05-13 00:00:25 +0000161 clEnumValN(Reloc::PIC_, "pic",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000162 "Fully relocatable, position independent code"),
Dan Gohman844731a2008-05-13 00:00:25 +0000163 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000164 "Relocatable external references, non-relocatable code"),
Dan Gohman844731a2008-05-13 00:00:25 +0000165 clEnumValEnd));
166static cl::opt<llvm::CodeModel::Model, true>
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000167DefCodeModel("code-model",
Dan Gohman844731a2008-05-13 00:00:25 +0000168 cl::desc("Choose code model"),
169 cl::location(CMModel),
170 cl::init(CodeModel::Default),
171 cl::values(
172 clEnumValN(CodeModel::Default, "default",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000173 "Target default code model"),
Dan Gohman844731a2008-05-13 00:00:25 +0000174 clEnumValN(CodeModel::Small, "small",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000175 "Small code model"),
Dan Gohman844731a2008-05-13 00:00:25 +0000176 clEnumValN(CodeModel::Kernel, "kernel",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000177 "Kernel code model"),
Dan Gohman844731a2008-05-13 00:00:25 +0000178 clEnumValN(CodeModel::Medium, "medium",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000179 "Medium code model"),
Dan Gohman844731a2008-05-13 00:00:25 +0000180 clEnumValN(CodeModel::Large, "large",
Dan Gohmanb8cab922008-10-14 20:25:08 +0000181 "Large code model"),
Dan Gohman844731a2008-05-13 00:00:25 +0000182 clEnumValEnd));
Dan Gohman844731a2008-05-13 00:00:25 +0000183static cl::opt<bool, true>
Dan Gohman1797ed52010-02-08 20:27:50 +0000184EnableGuaranteedTailCallOpt("tailcallopt",
Evan Cheng8c86db52010-01-27 00:10:09 +0000185 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
Dan Gohman1797ed52010-02-08 20:27:50 +0000186 cl::location(GuaranteedTailCallOpt),
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000187 cl::init(false));
Evan Cheng6547e402008-07-01 23:18:29 +0000188static cl::opt<unsigned, true>
189OverrideStackAlignment("stack-alignment",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000190 cl::desc("Override default stack alignment"),
191 cl::location(StackAlignment),
192 cl::init(0));
Dan Gohman844731a2008-05-13 00:00:25 +0000193static cl::opt<bool, true>
194EnableRealignStack("realign-stack",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000195 cl::desc("Realign stack if needed"),
196 cl::location(RealignStack),
197 cl::init(true));
Evan Cheng6547e402008-07-01 23:18:29 +0000198static cl::opt<bool, true>
Dale Johannesen72324642008-07-31 18:13:12 +0000199DisableSwitchTables(cl::Hidden, "disable-jump-tables",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000200 cl::desc("Do not generate jump tables."),
201 cl::location(DisableJumpTables),
202 cl::init(false));
Owen Anderson95dad832008-10-07 20:22:28 +0000203static cl::opt<bool, true>
204EnableStrongPHIElim(cl::Hidden, "strong-phi-elim",
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000205 cl::desc("Use strong PHI elimination."),
206 cl::location(StrongPHIElim),
207 cl::init(false));
Chris Lattner43ac7212010-04-13 00:36:43 +0000208static cl::opt<bool>
209DataSections("fdata-sections",
210 cl::desc("Emit data into separate sections"),
211 cl::init(false));
212static cl::opt<bool>
213FunctionSections("ffunction-sections",
214 cl::desc("Emit functions into separate sections"),
215 cl::init(false));
Brian Gaeke323819e2004-03-04 19:16:23 +0000216//---------------------------------------------------------------------------
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000217// TargetMachine Class
218//
Misha Brukman167deff2004-08-10 23:10:25 +0000219
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000220TargetMachine::TargetMachine(const Target &T)
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000221 : TheTarget(T), AsmInfo(0),
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000222 MCRelaxAll(false),
223 MCUseLoc(true) {
Anton Korobeynikov0eebf652009-06-08 22:53:56 +0000224 // Typically it will be subtargets that will adjust FloatABIType from Default
225 // to Soft or Hard.
226 if (UseSoftFloat)
227 FloatABIType = FloatABI::Soft;
228}
229
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000230TargetMachine::~TargetMachine() {
Jim Laskeyfde1b3b2006-09-07 23:39:26 +0000231 delete AsmInfo;
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000232}
233
Evan Cheng4c1aa862006-02-22 20:19:42 +0000234/// getRelocationModel - Returns the code generation relocation model. The
235/// choices are static, PIC, and dynamic-no-pic, and target default.
236Reloc::Model TargetMachine::getRelocationModel() {
237 return RelocationModel;
238}
239
240/// setRelocationModel - Sets the code generation relocation model.
241void TargetMachine::setRelocationModel(Reloc::Model Model) {
242 RelocationModel = Model;
243}
Evan Cheng80235d52006-05-23 18:18:46 +0000244
Evan Cheng152ed052006-07-06 01:53:36 +0000245/// getCodeModel - Returns the code model. The choices are small, kernel,
246/// medium, large, and target default.
247CodeModel::Model TargetMachine::getCodeModel() {
248 return CMModel;
249}
250
251/// setCodeModel - Sets the code model.
252void TargetMachine::setCodeModel(CodeModel::Model Model) {
253 CMModel = Model;
254}
255
Evan Cheng42bf74b2009-03-25 01:47:28 +0000256bool TargetMachine::getAsmVerbosityDefault() {
257 return AsmVerbosityDefault;
258}
259
260void TargetMachine::setAsmVerbosityDefault(bool V) {
261 AsmVerbosityDefault = V;
262}
263
Chris Lattner43ac7212010-04-13 00:36:43 +0000264bool TargetMachine::getFunctionSections() {
265 return FunctionSections;
266}
267
268bool TargetMachine::getDataSections() {
269 return DataSections;
270}
271
272void TargetMachine::setFunctionSections(bool V) {
273 FunctionSections = V;
274}
275
276void TargetMachine::setDataSections(bool V) {
277 DataSections = V;
278}
279
Evan Cheng80235d52006-05-23 18:18:46 +0000280namespace llvm {
Evan Chenge5667632010-04-21 03:18:23 +0000281 /// DisableFramePointerElim - This returns true if frame pointer elimination
282 /// optimization should be disabled for the given machine function.
283 bool DisableFramePointerElim(const MachineFunction &MF) {
Bill Wendling53f76022010-05-17 23:09:50 +0000284 // Check to see if we should eliminate non-leaf frame pointers and then
285 // check to see if we should eliminate all frame pointers.
Bill Wendling2abc93d2010-05-18 21:47:08 +0000286 if (NoFramePointerElimNonLeaf && !NoFramePointerElim) {
Evan Chenge5667632010-04-21 03:18:23 +0000287 const MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling0fc546b2010-05-14 21:28:24 +0000288 return MFI->hasCalls();
Evan Chenge5667632010-04-21 03:18:23 +0000289 }
Bill Wendling53f76022010-05-17 23:09:50 +0000290
291 return NoFramePointerElim;
Evan Chenge5667632010-04-21 03:18:23 +0000292 }
293
Mon P Wangbc65ca82009-03-20 05:06:58 +0000294 /// LessPreciseFPMAD - This flag return true when -enable-fp-mad option
295 /// is specified on the command line. When this flag is off(default), the
296 /// code generator is not allowed to generate mad (multiply add) if the
297 /// result is "less precise" than doing those operations individually.
298 bool LessPreciseFPMAD() { return UnsafeFPMath || LessPreciseFPMADOption; }
299
Chris Lattneraa4f1e12007-05-03 00:16:07 +0000300 /// HonorSignDependentRoundingFPMath - Return true if the codegen must assume
301 /// that the rounding mode of the FPU can change from its default.
302 bool HonorSignDependentRoundingFPMath() {
303 return !UnsafeFPMath && HonorSignDependentRoundingFPMathOption;
304 }
Reid Spencer077b3872006-05-24 19:05:21 +0000305}