6e4c907e2e249b6b02a1dbea668805af59143b8b
[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 "PPCTargetObjectFile.h"
16 #include "PPC.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/FormattedStream.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Target/TargetOptions.h"
25 using namespace llvm;
26
27 static cl::
28 opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
29                         cl::desc("Disable CTR loops for PPC"));
30
31 static cl::opt<bool>
32 VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
33   cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
34
35 extern "C" void LLVMInitializePowerPCTarget() {
36   // Register the targets
37   RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
38   RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
39   RegisterTargetMachine<PPC64TargetMachine> C(ThePPC64LETarget);
40 }
41
42 static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL, StringRef TT) {
43   std::string FullFS = FS;
44   Triple TargetTriple(TT);
45
46   // Make sure 64-bit features are available when CPUname is generic
47   if (TargetTriple.getArch() == Triple::ppc64 ||
48       TargetTriple.getArch() == Triple::ppc64le) {
49     if (!FullFS.empty())
50       FullFS = "+64bit," + FullFS;
51     else
52       FullFS = "+64bit";
53   }
54
55   if (OL >= CodeGenOpt::Default) {
56     if (!FullFS.empty())
57       FullFS = "+crbits," + FullFS;
58     else
59       FullFS = "+crbits";
60   }
61   return FullFS;
62 }
63
64 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
65   // If it isn't a Mach-O file then it's going to be a linux ELF
66   // object file.
67   if (TT.isOSDarwin())
68     return make_unique<TargetLoweringObjectFileMachO>();
69
70   return make_unique<PPC64LinuxTargetObjectFile>();
71 }
72
73 // The FeatureString here is a little subtle. We are modifying the feature string
74 // with what are (currently) non-function specific overrides as it goes into the
75 // LLVMTargetMachine constructor and then using the stored value in the
76 // Subtarget constructor below it.
77 PPCTargetMachine::PPCTargetMachine(const Target &T, StringRef TT, StringRef CPU,
78                                    StringRef FS, const TargetOptions &Options,
79                                    Reloc::Model RM, CodeModel::Model CM,
80                                    CodeGenOpt::Level OL)
81     : LLVMTargetMachine(T, TT, CPU, computeFSAdditions(FS, OL, TT), Options, RM,
82                         CM, OL),
83       TLOF(createTLOF(Triple(getTargetTriple()))),
84       Subtarget(TT, CPU, TargetFS, *this) {
85   initAsmInfo();
86 }
87
88 void PPC32TargetMachine::anchor() { }
89
90 PPC32TargetMachine::PPC32TargetMachine(const Target &T, StringRef TT,
91                                        StringRef CPU, StringRef FS,
92                                        const TargetOptions &Options,
93                                        Reloc::Model RM, CodeModel::Model CM,
94                                        CodeGenOpt::Level OL)
95   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
96 }
97
98 void PPC64TargetMachine::anchor() { }
99
100 PPC64TargetMachine::PPC64TargetMachine(const Target &T, StringRef TT,
101                                        StringRef CPU,  StringRef FS,
102                                        const TargetOptions &Options,
103                                        Reloc::Model RM, CodeModel::Model CM,
104                                        CodeGenOpt::Level OL)
105   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
106 }
107
108 const PPCSubtarget *
109 PPCTargetMachine::getSubtargetImpl(const Function &F) const {
110   AttributeSet FnAttrs = F.getAttributes();
111   Attribute CPUAttr =
112       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
113   Attribute FSAttr =
114       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
115
116   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
117                         ? CPUAttr.getValueAsString().str()
118                         : TargetCPU;
119   std::string FS = !FSAttr.hasAttribute(Attribute::None)
120                        ? FSAttr.getValueAsString().str()
121                        : TargetFS;
122
123   auto &I = SubtargetMap[CPU + FS];
124   if (!I) {
125     // This needs to be done before we create a new subtarget since any
126     // creation will depend on the TM and the code generation flags on the
127     // function that reside in TargetOptions.
128     resetTargetOptions(F);
129     I = llvm::make_unique<PPCSubtarget>(TargetTriple, CPU, FS, *this);
130   }
131   return I.get();
132 }
133
134 //===----------------------------------------------------------------------===//
135 // Pass Pipeline Configuration
136 //===----------------------------------------------------------------------===//
137
138 namespace {
139 /// PPC Code Generator Pass Configuration Options.
140 class PPCPassConfig : public TargetPassConfig {
141 public:
142   PPCPassConfig(PPCTargetMachine *TM, PassManagerBase &PM)
143     : TargetPassConfig(TM, PM) {}
144
145   PPCTargetMachine &getPPCTargetMachine() const {
146     return getTM<PPCTargetMachine>();
147   }
148
149   const PPCSubtarget &getPPCSubtarget() const {
150     return *getPPCTargetMachine().getSubtargetImpl();
151   }
152
153   void addIRPasses() override;
154   bool addPreISel() override;
155   bool addILPOpts() override;
156   bool addInstSelector() override;
157   bool addPreRegAlloc() override;
158   bool addPreSched2() override;
159   bool addPreEmitPass() override;
160 };
161 } // namespace
162
163 TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
164   return new PPCPassConfig(this, PM);
165 }
166
167 void PPCPassConfig::addIRPasses() {
168   addPass(createAtomicExpandPass(&getPPCTargetMachine()));
169   TargetPassConfig::addIRPasses();
170 }
171
172 bool PPCPassConfig::addPreISel() {
173   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
174     addPass(createPPCCTRLoops(getPPCTargetMachine()));
175
176   return false;
177 }
178
179 bool PPCPassConfig::addILPOpts() {
180   addPass(&EarlyIfConverterID);
181   return true;
182 }
183
184 bool PPCPassConfig::addInstSelector() {
185   // Install an instruction selector.
186   addPass(createPPCISelDag(getPPCTargetMachine()));
187
188 #ifndef NDEBUG
189   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
190     addPass(createPPCCTRLoopsVerify());
191 #endif
192
193   addPass(createPPCVSXCopyPass());
194   return false;
195 }
196
197 bool PPCPassConfig::addPreRegAlloc() {
198   initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
199   insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
200              &PPCVSXFMAMutateID);
201   return false;
202 }
203
204 bool PPCPassConfig::addPreSched2() {
205   addPass(createPPCVSXCopyCleanupPass());
206
207   if (getOptLevel() != CodeGenOpt::None)
208     addPass(&IfConverterID);
209
210   return true;
211 }
212
213 bool PPCPassConfig::addPreEmitPass() {
214   if (getOptLevel() != CodeGenOpt::None)
215     addPass(createPPCEarlyReturnPass());
216   // Must run branch selection immediately preceding the asm printer.
217   addPass(createPPCBranchSelectionPass());
218   return false;
219 }
220
221 void PPCTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
222   // Add first the target-independent BasicTTI pass, then our PPC pass. This
223   // allows the PPC pass to delegate to the target independent layer when
224   // appropriate.
225   PM.add(createBasicTargetTransformInfoPass(this));
226   PM.add(createPPCTargetTransformInfoPass(this));
227 }
228