LLVM API Documentation

X86TargetMachine.cpp
Go to the documentation of this file.
00001 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the X86 specific subclass of TargetMachine.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "X86TargetMachine.h"
00015 #include "X86.h"
00016 #include "llvm/CodeGen/Passes.h"
00017 #include "llvm/PassManager.h"
00018 #include "llvm/Support/CommandLine.h"
00019 #include "llvm/Support/FormattedStream.h"
00020 #include "llvm/Support/TargetRegistry.h"
00021 #include "llvm/Target/TargetOptions.h"
00022 using namespace llvm;
00023 
00024 extern "C" void LLVMInitializeX86Target() {
00025   // Register the target.
00026   RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
00027   RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
00028 }
00029 
00030 void X86TargetMachine::anchor() { }
00031 
00032 /// X86TargetMachine ctor - Create an X86 target.
00033 ///
00034 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
00035                                    StringRef FS, const TargetOptions &Options,
00036                                    Reloc::Model RM, CodeModel::Model CM,
00037                                    CodeGenOpt::Level OL)
00038     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
00039       Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {
00040   // default to hard float ABI
00041   if (Options.FloatABIType == FloatABI::Default)
00042     this->Options.FloatABIType = FloatABI::Hard;
00043 
00044   // Windows stack unwinder gets confused when execution flow "falls through"
00045   // after a call to 'noreturn' function.
00046   // To prevent that, we emit a trap for 'unreachable' IR instructions.
00047   // (which on X86, happens to be the 'ud2' instruction)
00048   if (Subtarget.isTargetWin64())
00049     this->Options.TrapUnreachable = true;
00050 
00051   initAsmInfo();
00052 }
00053 
00054 //===----------------------------------------------------------------------===//
00055 // Command line options for x86
00056 //===----------------------------------------------------------------------===//
00057 static cl::opt<bool>
00058 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
00059   cl::desc("Minimize AVX to SSE transition penalty"),
00060   cl::init(true));
00061 
00062 //===----------------------------------------------------------------------===//
00063 // X86 Analysis Pass Setup
00064 //===----------------------------------------------------------------------===//
00065 
00066 void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
00067   // Add first the target-independent BasicTTI pass, then our X86 pass. This
00068   // allows the X86 pass to delegate to the target independent layer when
00069   // appropriate.
00070   PM.add(createBasicTargetTransformInfoPass(this));
00071   PM.add(createX86TargetTransformInfoPass(this));
00072 }
00073 
00074 
00075 //===----------------------------------------------------------------------===//
00076 // Pass Pipeline Configuration
00077 //===----------------------------------------------------------------------===//
00078 
00079 namespace {
00080 /// X86 Code Generator Pass Configuration Options.
00081 class X86PassConfig : public TargetPassConfig {
00082 public:
00083   X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
00084     : TargetPassConfig(TM, PM) {}
00085 
00086   X86TargetMachine &getX86TargetMachine() const {
00087     return getTM<X86TargetMachine>();
00088   }
00089 
00090   const X86Subtarget &getX86Subtarget() const {
00091     return *getX86TargetMachine().getSubtargetImpl();
00092   }
00093 
00094   void addIRPasses() override;
00095   bool addInstSelector() override;
00096   bool addILPOpts() override;
00097   bool addPreRegAlloc() override;
00098   bool addPostRegAlloc() override;
00099   bool addPreEmitPass() override;
00100 };
00101 } // namespace
00102 
00103 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
00104   return new X86PassConfig(this, PM);
00105 }
00106 
00107 void X86PassConfig::addIRPasses() {
00108   addPass(createAtomicExpandPass(&getX86TargetMachine()));
00109 
00110   TargetPassConfig::addIRPasses();
00111 }
00112 
00113 bool X86PassConfig::addInstSelector() {
00114   // Install an instruction selector.
00115   addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
00116 
00117   // For ELF, cleanup any local-dynamic TLS accesses.
00118   if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
00119     addPass(createCleanupLocalDynamicTLSPass());
00120 
00121   addPass(createX86GlobalBaseRegPass());
00122 
00123   return false;
00124 }
00125 
00126 bool X86PassConfig::addILPOpts() {
00127   addPass(&EarlyIfConverterID);
00128   return true;
00129 }
00130 
00131 bool X86PassConfig::addPreRegAlloc() {
00132   return false;  // -print-machineinstr shouldn't print after this.
00133 }
00134 
00135 bool X86PassConfig::addPostRegAlloc() {
00136   addPass(createX86FloatingPointStackifierPass());
00137   return true;  // -print-machineinstr should print after this.
00138 }
00139 
00140 bool X86PassConfig::addPreEmitPass() {
00141   bool ShouldPrint = false;
00142   if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
00143     addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
00144     ShouldPrint = true;
00145   }
00146 
00147   if (UseVZeroUpper) {
00148     addPass(createX86IssueVZeroUpperPass());
00149     ShouldPrint = true;
00150   }
00151 
00152   if (getOptLevel() != CodeGenOpt::None) {
00153     addPass(createX86PadShortFunctions());
00154     addPass(createX86FixupLEAs());
00155     ShouldPrint = true;
00156   }
00157 
00158   return ShouldPrint;
00159 }