1. Remove condition on delete.
[oota-llvm.git] / lib / Target / X86 / X86TargetMachine.cpp
1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the X86 specific subclass of TargetMachine.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86TargetAsmInfo.h"
15 #include "X86TargetMachine.h"
16 #include "X86.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Target/TargetMachineRegistry.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include <iostream>
25 using namespace llvm;
26
27 /// X86TargetMachineModule - Note that this is used on hosts that cannot link
28 /// in a library unless there are references into the library.  In particular,
29 /// it seems that it is not possible to get things to work on Win32 without
30 /// this.  Though it is unused, do not remove it.
31 extern "C" int X86TargetMachineModule;
32 int X86TargetMachineModule = 0;
33
34 namespace {
35   // Register the target.
36   RegisterTarget<X86TargetMachine> X("x86", "  IA-32 (Pentium and above)");
37 }
38
39 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
40   return new X86TargetAsmInfo(*this);
41 }
42
43 unsigned X86TargetMachine::getJITMatchQuality() {
44 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
45   return 10;
46 #else
47   return 0;
48 #endif
49 }
50
51 unsigned X86TargetMachine::getModuleMatchQuality(const Module &M) {
52   // We strongly match "i[3-9]86-*".
53   std::string TT = M.getTargetTriple();
54   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
55       TT[4] == '-' && TT[1] - '3' < 6)
56     return 20;
57
58   if (M.getEndianness()  == Module::LittleEndian &&
59       M.getPointerSize() == Module::Pointer32)
60     return 10;                                   // Weak match
61   else if (M.getEndianness() != Module::AnyEndianness ||
62            M.getPointerSize() != Module::AnyPointerSize)
63     return 0;                                    // Match for some other target
64
65   return getJITMatchQuality()/2;
66 }
67
68 /// X86TargetMachine ctor - Create an ILP32 architecture model
69 ///
70 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS)
71   : Subtarget(M, FS), DataLayout("e-p:32:32-d:32-l:32"),
72     FrameInfo(TargetFrameInfo::StackGrowsDown,
73               Subtarget.getStackAlignment(), -4),
74     InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
75   if (getRelocationModel() == Reloc::Default)
76     if (Subtarget.isTargetDarwin())
77       setRelocationModel(Reloc::DynamicNoPIC);
78     else
79       setRelocationModel(Reloc::PIC_);
80 }
81
82 //===----------------------------------------------------------------------===//
83 // Pass Pipeline Configuration
84 //===----------------------------------------------------------------------===//
85
86 bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
87   // Install an instruction selector.
88   PM.add(createX86ISelDag(*this, Fast));
89   return false;
90 }
91
92 bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
93   PM.add(createX86FloatingPointStackifierPass());
94   return true;  // -print-machineinstr should print after this.
95 }
96
97 bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast, 
98                                           std::ostream &Out) {
99   PM.add(createX86CodePrinterPass(Out, *this));
100   return false;
101 }
102
103 bool X86TargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
104                                        std::ostream &Out) {
105   if (Subtarget.isTargetELF()) {
106     addX86ELFObjectWriterPass(PM, Out, *this);
107     return false;
108   }
109   return true;
110 }
111
112 bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
113                                       MachineCodeEmitter &MCE) {
114   // FIXME: Move this to TargetJITInfo!
115   setRelocationModel(Reloc::Static);
116   
117   PM.add(createX86CodeEmitterPass(*this, MCE));
118   return false;
119 }