Remove duplicate copy of InstrItineraryData from the TargetMachine,
[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 static cl::opt<bool>
30 VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
31   cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
32
33 extern "C" void LLVMInitializePowerPCTarget() {
34   // Register the targets
35   RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
36   RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
37   RegisterTargetMachine<PPC64TargetMachine> C(ThePPC64LETarget);
38 }
39
40 /// Return the datalayout string of a subtarget.
41 static std::string getDataLayoutString(const PPCSubtarget &ST) {
42   const Triple &T = ST.getTargetTriple();
43
44   std::string Ret;
45
46   // Most PPC* platforms are big endian, PPC64LE is little endian.
47   if (ST.isLittleEndian())
48     Ret = "e";
49   else
50     Ret = "E";
51
52   Ret += DataLayout::getManglingComponent(T);
53
54   // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
55   // pointers.
56   if (!ST.isPPC64() || T.getOS() == Triple::Lv2)
57     Ret += "-p:32:32";
58
59   // Note, the alignment values for f64 and i64 on ppc64 in Darwin
60   // documentation are wrong; these are correct (i.e. "what gcc does").
61   if (ST.isPPC64() || ST.isSVR4ABI())
62     Ret += "-i64:64";
63   else
64     Ret += "-f64:32:64";
65
66   // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
67   if (ST.isPPC64())
68     Ret += "-n32:64";
69   else
70     Ret += "-n32";
71
72   return Ret;
73 }
74
75 PPCTargetMachine::PPCTargetMachine(const Target &T, StringRef TT, StringRef CPU,
76                                    StringRef FS, const TargetOptions &Options,
77                                    Reloc::Model RM, CodeModel::Model CM,
78                                    CodeGenOpt::Level OL, bool is64Bit)
79     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
80       Subtarget(TT, CPU, FS, is64Bit, OL), DL(getDataLayoutString(Subtarget)),
81       InstrInfo(*this), FrameLowering(Subtarget), JITInfo(*this, is64Bit),
82       TLInfo(*this), TSInfo(*this) {
83   initAsmInfo();
84 }
85
86 void PPC32TargetMachine::anchor() { }
87
88 PPC32TargetMachine::PPC32TargetMachine(const Target &T, StringRef TT,
89                                        StringRef CPU, StringRef FS,
90                                        const TargetOptions &Options,
91                                        Reloc::Model RM, CodeModel::Model CM,
92                                        CodeGenOpt::Level OL)
93   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {
94 }
95
96 void PPC64TargetMachine::anchor() { }
97
98 PPC64TargetMachine::PPC64TargetMachine(const Target &T, StringRef TT,
99                                        StringRef CPU,  StringRef FS,
100                                        const TargetOptions &Options,
101                                        Reloc::Model RM, CodeModel::Model CM,
102                                        CodeGenOpt::Level OL)
103   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {
104 }
105
106
107 //===----------------------------------------------------------------------===//
108 // Pass Pipeline Configuration
109 //===----------------------------------------------------------------------===//
110
111 namespace {
112 /// PPC Code Generator Pass Configuration Options.
113 class PPCPassConfig : public TargetPassConfig {
114 public:
115   PPCPassConfig(PPCTargetMachine *TM, PassManagerBase &PM)
116     : TargetPassConfig(TM, PM) {}
117
118   PPCTargetMachine &getPPCTargetMachine() const {
119     return getTM<PPCTargetMachine>();
120   }
121
122   const PPCSubtarget &getPPCSubtarget() const {
123     return *getPPCTargetMachine().getSubtargetImpl();
124   }
125
126   bool addPreISel() override;
127   bool addILPOpts() override;
128   bool addInstSelector() override;
129   bool addPreRegAlloc() override;
130   bool addPreSched2() override;
131   bool addPreEmitPass() override;
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   addPass(&EarlyIfConverterID);
148   return true;
149 }
150
151 bool PPCPassConfig::addInstSelector() {
152   // Install an instruction selector.
153   addPass(createPPCISelDag(getPPCTargetMachine()));
154
155 #ifndef NDEBUG
156   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
157     addPass(createPPCCTRLoopsVerify());
158 #endif
159
160   addPass(createPPCVSXCopyPass());
161   return false;
162 }
163
164 bool PPCPassConfig::addPreRegAlloc() {
165   initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
166   insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
167              &PPCVSXFMAMutateID);
168   return false;
169 }
170
171 bool PPCPassConfig::addPreSched2() {
172   addPass(createPPCVSXCopyCleanupPass());
173
174   if (getOptLevel() != CodeGenOpt::None)
175     addPass(&IfConverterID);
176
177   return true;
178 }
179
180 bool PPCPassConfig::addPreEmitPass() {
181   if (getOptLevel() != CodeGenOpt::None)
182     addPass(createPPCEarlyReturnPass());
183   // Must run branch selection immediately preceding the asm printer.
184   addPass(createPPCBranchSelectionPass());
185   return false;
186 }
187
188 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
189                                       JITCodeEmitter &JCE) {
190   // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
191   // writing?
192   Subtarget.SetJITMode();
193
194   // Machine code emitter pass for PowerPC.
195   PM.add(createPPCJITCodeEmitterPass(*this, JCE));
196
197   return false;
198 }
199
200 void PPCTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
201   // Add first the target-independent BasicTTI pass, then our PPC pass. This
202   // allows the PPC pass to delegate to the target independent layer when
203   // appropriate.
204   PM.add(createBasicTargetTransformInfoPass(this));
205   PM.add(createPPCTargetTransformInfoPass(this));
206 }
207