LLVM API Documentation

PPCTargetMachine.cpp
Go to the documentation of this file.
00001 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
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 // Top-level implementation for the PowerPC target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "PPCTargetMachine.h"
00015 #include "PPC.h"
00016 #include "PPCTargetObjectFile.h"
00017 #include "PPCTargetTransformInfo.h"
00018 #include "llvm/CodeGen/Passes.h"
00019 #include "llvm/IR/Function.h"
00020 #include "llvm/IR/LegacyPassManager.h"
00021 #include "llvm/MC/MCStreamer.h"
00022 #include "llvm/Support/CommandLine.h"
00023 #include "llvm/Support/FormattedStream.h"
00024 #include "llvm/Support/TargetRegistry.h"
00025 #include "llvm/Target/TargetOptions.h"
00026 #include "llvm/Transforms/Scalar.h"
00027 using namespace llvm;
00028 
00029 static cl::
00030 opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
00031                         cl::desc("Disable CTR loops for PPC"));
00032 
00033 static cl::
00034 opt<bool> DisablePreIncPrep("disable-ppc-preinc-prep", cl::Hidden,
00035                             cl::desc("Disable PPC loop preinc prep"));
00036 
00037 static cl::opt<bool>
00038 VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
00039   cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
00040 
00041 static cl::opt<bool>
00042 EnableGEPOpt("ppc-gep-opt", cl::Hidden,
00043              cl::desc("Enable optimizations on complex GEPs"),
00044              cl::init(true));
00045 
00046 static cl::opt<bool>
00047 EnablePrefetch("enable-ppc-prefetching",
00048                   cl::desc("disable software prefetching on PPC"),
00049                   cl::init(false), cl::Hidden);
00050 
00051 extern "C" void LLVMInitializePowerPCTarget() {
00052   // Register the targets
00053   RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
00054   RegisterTargetMachine<PPC64TargetMachine> B(ThePPC64Target);
00055   RegisterTargetMachine<PPC64TargetMachine> C(ThePPC64LETarget);
00056 }
00057 
00058 /// Return the datalayout string of a subtarget.
00059 static std::string getDataLayoutString(const Triple &T) {
00060   bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
00061   std::string Ret;
00062 
00063   // Most PPC* platforms are big endian, PPC64LE is little endian.
00064   if (T.getArch() == Triple::ppc64le)
00065     Ret = "e";
00066   else
00067     Ret = "E";
00068 
00069   Ret += DataLayout::getManglingComponent(T);
00070 
00071   // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
00072   // pointers.
00073   if (!is64Bit || T.getOS() == Triple::Lv2)
00074     Ret += "-p:32:32";
00075 
00076   // Note, the alignment values for f64 and i64 on ppc64 in Darwin
00077   // documentation are wrong; these are correct (i.e. "what gcc does").
00078   if (is64Bit || !T.isOSDarwin())
00079     Ret += "-i64:64";
00080   else
00081     Ret += "-f64:32:64";
00082 
00083   // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
00084   if (is64Bit)
00085     Ret += "-n32:64";
00086   else
00087     Ret += "-n32";
00088 
00089   return Ret;
00090 }
00091 
00092 static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL, StringRef TT) {
00093   std::string FullFS = FS;
00094   Triple TargetTriple(TT);
00095 
00096   // Make sure 64-bit features are available when CPUname is generic
00097   if (TargetTriple.getArch() == Triple::ppc64 ||
00098       TargetTriple.getArch() == Triple::ppc64le) {
00099     if (!FullFS.empty())
00100       FullFS = "+64bit," + FullFS;
00101     else
00102       FullFS = "+64bit";
00103   }
00104 
00105   if (OL >= CodeGenOpt::Default) {
00106     if (!FullFS.empty())
00107       FullFS = "+crbits," + FullFS;
00108     else
00109       FullFS = "+crbits";
00110   }
00111 
00112   if (OL != CodeGenOpt::None) {
00113      if (!FullFS.empty())
00114       FullFS = "+invariant-function-descriptors," + FullFS;
00115     else
00116       FullFS = "+invariant-function-descriptors";
00117   }
00118 
00119   return FullFS;
00120 }
00121 
00122 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
00123   // If it isn't a Mach-O file then it's going to be a linux ELF
00124   // object file.
00125   if (TT.isOSDarwin())
00126     return make_unique<TargetLoweringObjectFileMachO>();
00127 
00128   return make_unique<PPC64LinuxTargetObjectFile>();
00129 }
00130 
00131 static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
00132                                                  const TargetOptions &Options) {
00133   if (Options.MCOptions.getABIName().startswith("elfv1"))
00134     return PPCTargetMachine::PPC_ABI_ELFv1;
00135   else if (Options.MCOptions.getABIName().startswith("elfv2"))
00136     return PPCTargetMachine::PPC_ABI_ELFv2;
00137 
00138   assert(Options.MCOptions.getABIName().empty() &&
00139    "Unknown target-abi option!");
00140 
00141   if (!TT.isMacOSX()) {
00142     switch (TT.getArch()) {
00143     case Triple::ppc64le:
00144       return PPCTargetMachine::PPC_ABI_ELFv2;
00145     case Triple::ppc64:
00146       return PPCTargetMachine::PPC_ABI_ELFv1;
00147     default:
00148       // Fallthrough.
00149       ;
00150     }
00151   }
00152   return PPCTargetMachine::PPC_ABI_UNKNOWN;
00153 }
00154 
00155 // The FeatureString here is a little subtle. We are modifying the feature string
00156 // with what are (currently) non-function specific overrides as it goes into the
00157 // LLVMTargetMachine constructor and then using the stored value in the
00158 // Subtarget constructor below it.
00159 PPCTargetMachine::PPCTargetMachine(const Target &T, StringRef TT, StringRef CPU,
00160                                    StringRef FS, const TargetOptions &Options,
00161                                    Reloc::Model RM, CodeModel::Model CM,
00162                                    CodeGenOpt::Level OL)
00163     : LLVMTargetMachine(T, TT, CPU, computeFSAdditions(FS, OL, TT), Options, RM,
00164                         CM, OL),
00165       TLOF(createTLOF(Triple(getTargetTriple()))),
00166       TargetABI(computeTargetABI(Triple(TT), Options)),
00167       DL(getDataLayoutString(Triple(TT))), Subtarget(TT, CPU, TargetFS, *this) {
00168   initAsmInfo();
00169 }
00170 
00171 PPCTargetMachine::~PPCTargetMachine() {}
00172 
00173 void PPC32TargetMachine::anchor() { }
00174 
00175 PPC32TargetMachine::PPC32TargetMachine(const Target &T, StringRef TT,
00176                                        StringRef CPU, StringRef FS,
00177                                        const TargetOptions &Options,
00178                                        Reloc::Model RM, CodeModel::Model CM,
00179                                        CodeGenOpt::Level OL)
00180   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
00181 }
00182 
00183 void PPC64TargetMachine::anchor() { }
00184 
00185 PPC64TargetMachine::PPC64TargetMachine(const Target &T, StringRef TT,
00186                                        StringRef CPU,  StringRef FS,
00187                                        const TargetOptions &Options,
00188                                        Reloc::Model RM, CodeModel::Model CM,
00189                                        CodeGenOpt::Level OL)
00190   : PPCTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {
00191 }
00192 
00193 const PPCSubtarget *
00194 PPCTargetMachine::getSubtargetImpl(const Function &F) const {
00195   Attribute CPUAttr = F.getFnAttribute("target-cpu");
00196   Attribute FSAttr = F.getFnAttribute("target-features");
00197 
00198   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
00199                         ? CPUAttr.getValueAsString().str()
00200                         : TargetCPU;
00201   std::string FS = !FSAttr.hasAttribute(Attribute::None)
00202                        ? FSAttr.getValueAsString().str()
00203                        : TargetFS;
00204 
00205   auto &I = SubtargetMap[CPU + FS];
00206   if (!I) {
00207     // This needs to be done before we create a new subtarget since any
00208     // creation will depend on the TM and the code generation flags on the
00209     // function that reside in TargetOptions.
00210     resetTargetOptions(F);
00211     I = llvm::make_unique<PPCSubtarget>(TargetTriple, CPU, FS, *this);
00212   }
00213   return I.get();
00214 }
00215 
00216 //===----------------------------------------------------------------------===//
00217 // Pass Pipeline Configuration
00218 //===----------------------------------------------------------------------===//
00219 
00220 namespace {
00221 /// PPC Code Generator Pass Configuration Options.
00222 class PPCPassConfig : public TargetPassConfig {
00223 public:
00224   PPCPassConfig(PPCTargetMachine *TM, PassManagerBase &PM)
00225     : TargetPassConfig(TM, PM) {}
00226 
00227   PPCTargetMachine &getPPCTargetMachine() const {
00228     return getTM<PPCTargetMachine>();
00229   }
00230 
00231   void addIRPasses() override;
00232   bool addPreISel() override;
00233   bool addILPOpts() override;
00234   bool addInstSelector() override;
00235   void addPreRegAlloc() override;
00236   void addPreSched2() override;
00237   void addPreEmitPass() override;
00238 };
00239 } // namespace
00240 
00241 TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
00242   return new PPCPassConfig(this, PM);
00243 }
00244 
00245 void PPCPassConfig::addIRPasses() {
00246   addPass(createAtomicExpandPass(&getPPCTargetMachine()));
00247 
00248   // For the BG/Q (or if explicitly requested), add explicit data prefetch
00249   // intrinsics.
00250   bool UsePrefetching =
00251     Triple(TM->getTargetTriple()).getVendor() == Triple::BGQ &&           
00252     getOptLevel() != CodeGenOpt::None;
00253   if (EnablePrefetch.getNumOccurrences() > 0)
00254     UsePrefetching = EnablePrefetch;
00255   if (UsePrefetching)
00256     addPass(createPPCLoopDataPrefetchPass());
00257 
00258   if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
00259     // Call SeparateConstOffsetFromGEP pass to extract constants within indices
00260     // and lower a GEP with multiple indices to either arithmetic operations or
00261     // multiple GEPs with single index.
00262     addPass(createSeparateConstOffsetFromGEPPass(TM, true));
00263     // Call EarlyCSE pass to find and remove subexpressions in the lowered
00264     // result.
00265     addPass(createEarlyCSEPass());
00266     // Do loop invariant code motion in case part of the lowered result is
00267     // invariant.
00268     addPass(createLICMPass());
00269   }
00270 
00271   TargetPassConfig::addIRPasses();
00272 }
00273 
00274 bool PPCPassConfig::addPreISel() {
00275   if (!DisablePreIncPrep && getOptLevel() != CodeGenOpt::None)
00276     addPass(createPPCLoopPreIncPrepPass(getPPCTargetMachine()));
00277 
00278   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
00279     addPass(createPPCCTRLoops(getPPCTargetMachine()));
00280 
00281   return false;
00282 }
00283 
00284 bool PPCPassConfig::addILPOpts() {
00285   addPass(&EarlyIfConverterID);
00286   return true;
00287 }
00288 
00289 bool PPCPassConfig::addInstSelector() {
00290   // Install an instruction selector.
00291   addPass(createPPCISelDag(getPPCTargetMachine()));
00292 
00293 #ifndef NDEBUG
00294   if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
00295     addPass(createPPCCTRLoopsVerify());
00296 #endif
00297 
00298   addPass(createPPCVSXCopyPass());
00299   return false;
00300 }
00301 
00302 void PPCPassConfig::addPreRegAlloc() {
00303   initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
00304   insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
00305              &PPCVSXFMAMutateID);
00306   if (getPPCTargetMachine().getRelocationModel() == Reloc::PIC_)
00307     addPass(createPPCTLSDynamicCallPass());
00308 }
00309 
00310 void PPCPassConfig::addPreSched2() {
00311   if (getOptLevel() != CodeGenOpt::None)
00312     addPass(&IfConverterID);
00313 }
00314 
00315 void PPCPassConfig::addPreEmitPass() {
00316   if (getOptLevel() != CodeGenOpt::None)
00317     addPass(createPPCEarlyReturnPass(), false);
00318   // Must run branch selection immediately preceding the asm printer.
00319   addPass(createPPCBranchSelectionPass(), false);
00320 }
00321 
00322 TargetIRAnalysis PPCTargetMachine::getTargetIRAnalysis() {
00323   return TargetIRAnalysis(
00324       [this](Function &F) { return TargetTransformInfo(PPCTTIImpl(this, F)); });
00325 }