Fix f64 and f128 for ppc-darwin.
[oota-llvm.git] / lib / Target / PowerPC / PPCTargetMachine.cpp
1 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
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 // Top-level implementation for the PowerPC target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPCTargetMachine.h"
15 #include "PPC.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FormattedStream.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Target/TargetOptions.h"
23 using namespace llvm;
24
25 static cl::
26 opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
27                         cl::desc("Disable CTR loops for PPC"));
28
29 extern "C" void LLVMInitializePowerPCTarget() {
30   // Register the targets
31   RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
32   RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
33   RegisterTargetMachine<PPC64TargetMachine> C(ThePPC64LETarget);
34 }
35
36 /// Return the datalayout string of a subtarget.
37 static std::string getDataLayoutString(const PPCSubtarget &ST) {
38   const Triple &T = ST.getTargetTriple();
39
40   // PPC is big endian.
41   std::string Ret = "E";
42
43   // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
44   // pointers.
45   if (!ST.isPPC64() || T.getOS() == Triple::Lv2)
46     Ret += "-p:32:32";
47
48   // Note, the alignment values for f64 and i64 on ppc64 in Darwin
49   // documentation are wrong; these are correct (i.e. "what gcc does").
50   if (ST.isPPC64() || ST.isSVR4ABI())
51     Ret += "-i64:64";
52   else
53     Ret += "-f64:32:64";
54
55   // Set support for 128 floats depending on the ABI.
56   if (!ST.isPPC64() && ST.isSVR4ABI())
57     Ret += "-f128:64:128";
58
59   // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
60   if (ST.isPPC64())
61     Ret += "-n32:64";
62   else
63     Ret += "-n32";
64
65   return Ret;
66 }
67
68 PPCTargetMachine::PPCTargetMachine(const Target &T, StringRef TT,
69                                    StringRef CPU, StringRef FS,
70                                    const TargetOptions &Options,
71                                    Reloc::Model RM, CodeModel::Model CM,
72                                    CodeGenOpt::Level OL,
73                                    bool is64Bit)
74   : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
75     Subtarget(TT, CPU, FS, is64Bit),
76     DL(getDataLayoutString(Subtarget)), InstrInfo(*this),
77     FrameLowering(Subtarget), JITInfo(*this, is64Bit),
78     TLInfo(*this), TSInfo(*this),
79     InstrItins(Subtarget.getInstrItineraryData()) {
80
81   // The binutils for the BG/P are too old for CFI.
82   if (Subtarget.isBGP())
83     setMCUseCFI(false);
84   initAsmInfo();
85 }
86
87 void PPC32TargetMachine::anchor() { }
88
89 PPC32TargetMachine::PPC32TargetMachine(const Target &T, StringRef TT,
90                                        StringRef CPU, StringRef FS,
91                                        const TargetOptions &Options,
92                                        Reloc::Model RM, CodeModel::Model CM,
93                                        CodeGenOpt::Level OL)
94   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {
95 }
96
97 void PPC64TargetMachine::anchor() { }
98
99 PPC64TargetMachine::PPC64TargetMachine(const Target &T, StringRef TT,
100                                        StringRef CPU,  StringRef FS,
101                                        const TargetOptions &Options,
102                                        Reloc::Model RM, CodeModel::Model CM,
103                                        CodeGenOpt::Level OL)
104   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {
105 }
106
107
108 //===----------------------------------------------------------------------===//
109 // Pass Pipeline Configuration
110 //===----------------------------------------------------------------------===//
111
112 namespace {
113 /// PPC Code Generator Pass Configuration Options.
114 class PPCPassConfig : public TargetPassConfig {
115 public:
116   PPCPassConfig(PPCTargetMachine *TM, PassManagerBase &PM)
117     : TargetPassConfig(TM, PM) {}
118
119   PPCTargetMachine &getPPCTargetMachine() const {
120     return getTM<PPCTargetMachine>();
121   }
122
123   const PPCSubtarget &getPPCSubtarget() const {
124     return *getPPCTargetMachine().getSubtargetImpl();
125   }
126
127   virtual bool addPreISel();
128   virtual bool addILPOpts();
129   virtual bool addInstSelector();
130   virtual bool addPreSched2();
131   virtual bool addPreEmitPass();
132 };
133 } // namespace
134
135 TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
136   return new PPCPassConfig(this, PM);
137 }
138
139 bool PPCPassConfig::addPreISel() {
140   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
141     addPass(createPPCCTRLoops(getPPCTargetMachine()));
142
143   return false;
144 }
145
146 bool PPCPassConfig::addILPOpts() {
147   if (getPPCSubtarget().hasISEL()) {
148     addPass(&EarlyIfConverterID);
149     return true;
150   }
151
152   return false;
153 }
154
155 bool PPCPassConfig::addInstSelector() {
156   // Install an instruction selector.
157   addPass(createPPCISelDag(getPPCTargetMachine()));
158
159 #ifndef NDEBUG
160   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
161     addPass(createPPCCTRLoopsVerify());
162 #endif
163
164   return false;
165 }
166
167 bool PPCPassConfig::addPreSched2() {
168   if (getOptLevel() != CodeGenOpt::None)
169     addPass(&IfConverterID);
170
171   return true;
172 }
173
174 bool PPCPassConfig::addPreEmitPass() {
175   if (getOptLevel() != CodeGenOpt::None)
176     addPass(createPPCEarlyReturnPass());
177   // Must run branch selection immediately preceding the asm printer.
178   addPass(createPPCBranchSelectionPass());
179   return false;
180 }
181
182 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
183                                       JITCodeEmitter &JCE) {
184   // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
185   // writing?
186   Subtarget.SetJITMode();
187
188   // Machine code emitter pass for PowerPC.
189   PM.add(createPPCJITCodeEmitterPass(*this, JCE));
190
191   return false;
192 }
193
194 void PPCTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
195   // Add first the target-independent BasicTTI pass, then our PPC pass. This
196   // allows the PPC pass to delegate to the target independent layer when
197   // appropriate.
198   PM.add(createBasicTargetTransformInfoPass(this));
199   PM.add(createPPCTargetTransformInfoPass(this));
200 }
201