1 //===-- TargetMachine.cpp - General Target Information ---------------------==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file describes the general parts of a Target machine. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineFunction.h" 15 #include "llvm/CodeGen/MachineFrameInfo.h" 16 #include "llvm/MC/MCAsmInfo.h" 17 #include "llvm/Target/TargetMachine.h" 18 #include "llvm/Target/TargetOptions.h" 19 #include "llvm/Support/CommandLine.h" 20 using namespace llvm; 21 22 //--------------------------------------------------------------------------- 23 // Command-line options that tend to be useful on more than one back-end. 24 // 25 26 namespace llvm { 27 bool LessPreciseFPMADOption; 28 bool PrintMachineCode; 29 bool NoFramePointerElim; 30 bool NoFramePointerElimNonLeaf; 31 bool NoExcessFPPrecision; 32 bool UnsafeFPMath; 33 bool FiniteOnlyFPMathOption; 34 bool HonorSignDependentRoundingFPMathOption; 35 bool UseSoftFloat; 36 FloatABI::ABIType FloatABIType; 37 bool NoImplicitFloat; 38 bool NoZerosInBSS; 39 bool JITExceptionHandling; 40 bool JITEmitDebugInfo; 41 bool JITEmitDebugInfoToDisk; 42 bool UnwindTablesMandatory; 43 Reloc::Model RelocationModel; 44 CodeModel::Model CMModel; 45 bool GuaranteedTailCallOpt; 46 unsigned StackAlignment; 47 bool RealignStack; 48 bool DisableJumpTables; 49 bool StrongPHIElim; 50 bool AsmVerbosityDefault(false); 51 } 52 53 static cl::opt<bool, true> 54 PrintCode("print-machineinstrs", 55 cl::desc("Print generated machine code"), 56 cl::location(PrintMachineCode), cl::init(false)); 57 static cl::opt<bool, true> 58 DisableFPElim("disable-fp-elim", 59 cl::desc("Disable frame pointer elimination optimization"), 60 cl::location(NoFramePointerElim), 61 cl::init(false)); 62 static cl::opt<bool, true> 63 DisableFPElimNonLeaf("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)); 67 static cl::opt<bool, true> 68 DisableExcessPrecision("disable-excess-fp-precision", 69 cl::desc("Disable optimizations that may increase FP precision"), 70 cl::location(NoExcessFPPrecision), 71 cl::init(false)); 72 static cl::opt<bool, true> 73 EnableFPMAD("enable-fp-mad", 74 cl::desc("Enable less precise MAD instructions to be generated"), 75 cl::location(LessPreciseFPMADOption), 76 cl::init(false)); 77 static cl::opt<bool, true> 78 EnableUnsafeFPMath("enable-unsafe-fp-math", 79 cl::desc("Enable optimizations that may decrease FP precision"), 80 cl::location(UnsafeFPMath), 81 cl::init(false)); 82 static cl::opt<bool, true> 83 EnableFiniteOnlyFPMath("enable-finite-only-fp-math", 84 cl::desc("Enable optimizations that assumes non- NaNs / +-Infs"), 85 cl::location(FiniteOnlyFPMathOption), 86 cl::init(false)); 87 static cl::opt<bool, true> 88 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math", 89 cl::Hidden, 90 cl::desc("Force codegen to assume rounding mode can change dynamically"), 91 cl::location(HonorSignDependentRoundingFPMathOption), 92 cl::init(false)); 93 static cl::opt<bool, true> 94 GenerateSoftFloatCalls("soft-float", 95 cl::desc("Generate software floating point library calls"), 96 cl::location(UseSoftFloat), 97 cl::init(false)); 98 static cl::opt<llvm::FloatABI::ABIType, true> 99 FloatABIForCalls("float-abi", 100 cl::desc("Choose float ABI type"), 101 cl::location(FloatABIType), 102 cl::init(FloatABI::Default), 103 cl::values( 104 clEnumValN(FloatABI::Default, "default", 105 "Target default float ABI type"), 106 clEnumValN(FloatABI::Soft, "soft", 107 "Soft float ABI (implied by -soft-float)"), 108 clEnumValN(FloatABI::Hard, "hard", 109 "Hard float ABI (uses FP registers)"), 110 clEnumValEnd)); 111 static cl::opt<bool, true> 112 DontPlaceZerosInBSS("nozero-initialized-in-bss", 113 cl::desc("Don't place zero-initialized symbols into bss section"), 114 cl::location(NoZerosInBSS), 115 cl::init(false)); 116 static cl::opt<bool, true> 117 EnableJITExceptionHandling("jit-enable-eh", 118 cl::desc("Emit exception handling information"), 119 cl::location(JITExceptionHandling), 120 cl::init(false)); 121 // In debug builds, make this default to true. 122 #ifdef NDEBUG 123 #define EMIT_DEBUG false 124 #else 125 #define EMIT_DEBUG true 126 #endif 127 static cl::opt<bool, true> 128 EmitJitDebugInfo("jit-emit-debug", 129 cl::desc("Emit debug information to debugger"), 130 cl::location(JITEmitDebugInfo), 131 cl::init(EMIT_DEBUG)); 132 #undef EMIT_DEBUG 133 static cl::opt<bool, true> 134 EmitJitDebugInfoToDisk("jit-emit-debug-to-disk", 135 cl::Hidden, 136 cl::desc("Emit debug info objfiles to disk"), 137 cl::location(JITEmitDebugInfoToDisk), 138 cl::init(false)); 139 static cl::opt<bool, true> 140 EnableUnwindTables("unwind-tables", 141 cl::desc("Generate unwinding tables for all functions"), 142 cl::location(UnwindTablesMandatory), 143 cl::init(false)); 144 145 static cl::opt<llvm::Reloc::Model, true> 146 DefRelocationModel("relocation-model", 147 cl::desc("Choose relocation model"), 148 cl::location(RelocationModel), 149 cl::init(Reloc::Default), 150 cl::values( 151 clEnumValN(Reloc::Default, "default", 152 "Target default relocation model"), 153 clEnumValN(Reloc::Static, "static", 154 "Non-relocatable code"), 155 clEnumValN(Reloc::PIC_, "pic", 156 "Fully relocatable, position independent code"), 157 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", 158 "Relocatable external references, non-relocatable code"), 159 clEnumValEnd)); 160 static cl::opt<llvm::CodeModel::Model, true> 161 DefCodeModel("code-model", 162 cl::desc("Choose code model"), 163 cl::location(CMModel), 164 cl::init(CodeModel::Default), 165 cl::values( 166 clEnumValN(CodeModel::Default, "default", 167 "Target default code model"), 168 clEnumValN(CodeModel::Small, "small", 169 "Small code model"), 170 clEnumValN(CodeModel::Kernel, "kernel", 171 "Kernel code model"), 172 clEnumValN(CodeModel::Medium, "medium", 173 "Medium code model"), 174 clEnumValN(CodeModel::Large, "large", 175 "Large code model"), 176 clEnumValEnd)); 177 static cl::opt<bool, true> 178 EnableGuaranteedTailCallOpt("tailcallopt", 179 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."), 180 cl::location(GuaranteedTailCallOpt), 181 cl::init(false)); 182 static cl::opt<unsigned, true> 183 OverrideStackAlignment("stack-alignment", 184 cl::desc("Override default stack alignment"), 185 cl::location(StackAlignment), 186 cl::init(0)); 187 static cl::opt<bool, true> 188 EnableRealignStack("realign-stack", 189 cl::desc("Realign stack if needed"), 190 cl::location(RealignStack), 191 cl::init(true)); 192 static cl::opt<bool, true> 193 DisableSwitchTables(cl::Hidden, "disable-jump-tables", 194 cl::desc("Do not generate jump tables."), 195 cl::location(DisableJumpTables), 196 cl::init(false)); 197 static cl::opt<bool, true> 198 EnableStrongPHIElim(cl::Hidden, "strong-phi-elim", 199 cl::desc("Use strong PHI elimination."), 200 cl::location(StrongPHIElim), 201 cl::init(false)); 202 static cl::opt<bool> 203 DataSections("fdata-sections", 204 cl::desc("Emit data into separate sections"), 205 cl::init(false)); 206 static cl::opt<bool> 207 FunctionSections("ffunction-sections", 208 cl::desc("Emit functions into separate sections"), 209 cl::init(false)); 210 //--------------------------------------------------------------------------- 211 // TargetMachine Class 212 // 213 214 TargetMachine::TargetMachine(const Target &T) 215 : TheTarget(T), AsmInfo(0), 216 MCRelaxAll(false) { 217 // Typically it will be subtargets that will adjust FloatABIType from Default 218 // to Soft or Hard. 219 if (UseSoftFloat) 220 FloatABIType = FloatABI::Soft; 221 } 222 223 TargetMachine::~TargetMachine() { 224 delete AsmInfo; 225 } 226 227 /// getRelocationModel - Returns the code generation relocation model. The 228 /// choices are static, PIC, and dynamic-no-pic, and target default. 229 Reloc::Model TargetMachine::getRelocationModel() { 230 return RelocationModel; 231 } 232 233 /// setRelocationModel - Sets the code generation relocation model. 234 void TargetMachine::setRelocationModel(Reloc::Model Model) { 235 RelocationModel = Model; 236 } 237 238 /// getCodeModel - Returns the code model. The choices are small, kernel, 239 /// medium, large, and target default. 240 CodeModel::Model TargetMachine::getCodeModel() { 241 return CMModel; 242 } 243 244 /// setCodeModel - Sets the code model. 245 void TargetMachine::setCodeModel(CodeModel::Model Model) { 246 CMModel = Model; 247 } 248 249 bool TargetMachine::getAsmVerbosityDefault() { 250 return AsmVerbosityDefault; 251 } 252 253 void TargetMachine::setAsmVerbosityDefault(bool V) { 254 AsmVerbosityDefault = V; 255 } 256 257 bool TargetMachine::getFunctionSections() { 258 return FunctionSections; 259 } 260 261 bool TargetMachine::getDataSections() { 262 return DataSections; 263 } 264 265 void TargetMachine::setFunctionSections(bool V) { 266 FunctionSections = V; 267 } 268 269 void TargetMachine::setDataSections(bool V) { 270 DataSections = V; 271 } 272 273 namespace llvm { 274 /// DisableFramePointerElim - This returns true if frame pointer elimination 275 /// optimization should be disabled for the given machine function. 276 bool DisableFramePointerElim(const MachineFunction &MF) { 277 // Check to see if we should eliminate non-leaf frame pointers and then 278 // check to see if we should eliminate all frame pointers. 279 if (NoFramePointerElimNonLeaf && !NoFramePointerElim) { 280 const MachineFrameInfo *MFI = MF.getFrameInfo(); 281 return MFI->hasCalls(); 282 } 283 284 return NoFramePointerElim; 285 } 286 287 /// LessPreciseFPMAD - This flag return true when -enable-fp-mad option 288 /// is specified on the command line. When this flag is off(default), the 289 /// code generator is not allowed to generate mad (multiply add) if the 290 /// result is "less precise" than doing those operations individually. 291 bool LessPreciseFPMAD() { return UnsafeFPMath || LessPreciseFPMADOption; } 292 293 /// FiniteOnlyFPMath - This returns true when the -enable-finite-only-fp-math 294 /// option is specified on the command line. If this returns false (default), 295 /// the code generator is not allowed to assume that FP arithmetic arguments 296 /// and results are never NaNs or +-Infs. 297 bool FiniteOnlyFPMath() { return UnsafeFPMath || FiniteOnlyFPMathOption; } 298 299 /// HonorSignDependentRoundingFPMath - Return true if the codegen must assume 300 /// that the rounding mode of the FPU can change from its default. 301 bool HonorSignDependentRoundingFPMath() { 302 return !UnsafeFPMath && HonorSignDependentRoundingFPMathOption; 303 } 304 } 305