1 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Top-level implementation for the PowerPC target.
12 //===----------------------------------------------------------------------===//
15 #include "PPCTargetAsmInfo.h"
16 #include "PPCTargetMachine.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Target/TargetMachineRegistry.h"
20 #include "llvm/Target/TargetOptions.h"
21 #include "llvm/Support/raw_ostream.h"
24 /// PowerPCTargetMachineModule - Note that this is used on hosts that
25 /// cannot link in a library unless there are references into the
26 /// library. In particular, it seems that it is not possible to get
27 /// things to work on Win32 without this. Though it is unused, do not
29 extern "C" int PowerPCTargetMachineModule;
30 int PowerPCTargetMachineModule = 0;
32 // Register the targets
33 static RegisterTarget<PPC32TargetMachine>
34 X("ppc32", "PowerPC 32");
35 static RegisterTarget<PPC64TargetMachine>
36 Y("ppc64", "PowerPC 64");
38 // No assembler printer by default
39 PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
41 const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
42 if (Subtarget.isDarwin())
43 return new PPCDarwinTargetAsmInfo(*this);
45 return new PPCLinuxTargetAsmInfo(*this);
48 unsigned PPC32TargetMachine::getJITMatchQuality() {
49 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
50 if (sizeof(void*) == 4)
55 unsigned PPC64TargetMachine::getJITMatchQuality() {
56 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
57 if (sizeof(void*) == 8)
63 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
64 // We strongly match "powerpc-*".
65 std::string TT = M.getTargetTriple();
66 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
69 // If the target triple is something non-powerpc, we don't match.
70 if (!TT.empty()) return 0;
72 if (M.getEndianness() == Module::BigEndian &&
73 M.getPointerSize() == Module::Pointer32)
74 return 10; // Weak match
75 else if (M.getEndianness() != Module::AnyEndianness ||
76 M.getPointerSize() != Module::AnyPointerSize)
77 return 0; // Match for some other target
79 return getJITMatchQuality()/2;
82 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
83 // We strongly match "powerpc64-*".
84 std::string TT = M.getTargetTriple();
85 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
88 if (M.getEndianness() == Module::BigEndian &&
89 M.getPointerSize() == Module::Pointer64)
90 return 10; // Weak match
91 else if (M.getEndianness() != Module::AnyEndianness ||
92 M.getPointerSize() != Module::AnyPointerSize)
93 return 0; // Match for some other target
95 return getJITMatchQuality()/2;
99 PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
101 : Subtarget(*this, M, FS, is64Bit),
102 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
103 FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
104 InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
106 if (getRelocationModel() == Reloc::Default) {
107 if (Subtarget.isDarwin())
108 setRelocationModel(Reloc::DynamicNoPIC);
110 setRelocationModel(Reloc::Static);
114 /// Override this for PowerPC. Tail merging happily breaks up instruction issue
115 /// groups, which typically degrades performance.
116 bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
118 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
119 : PPCTargetMachine(M, FS, false) {
123 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
124 : PPCTargetMachine(M, FS, true) {
128 //===----------------------------------------------------------------------===//
129 // Pass Pipeline Configuration
130 //===----------------------------------------------------------------------===//
132 bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
133 // Install an instruction selector.
134 PM.add(createPPCISelDag(*this));
138 bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
140 // Must run branch selection immediately preceding the asm printer.
141 PM.add(createPPCBranchSelectionPass());
145 bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
147 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
149 PM.add(AsmPrinterCtor(Out, *this));
154 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
155 bool DumpAsm, MachineCodeEmitter &MCE) {
156 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
157 // FIXME: This should be moved to TargetJITInfo!!
158 if (Subtarget.isPPC64()) {
159 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
160 // instructions to materialize arbitrary global variable + function +
161 // constant pool addresses.
162 setRelocationModel(Reloc::PIC_);
163 // Temporary workaround for the inability of PPC64 JIT to handle jump
165 DisableJumpTables = true;
167 setRelocationModel(Reloc::Static);
170 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
172 Subtarget.SetJITMode();
174 // Machine code emitter pass for PowerPC.
175 PM.add(createPPCCodeEmitterPass(*this, MCE));
177 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
179 PM.add(AsmPrinterCtor(errs(), *this));
185 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
186 bool DumpAsm, MachineCodeEmitter &MCE) {
187 // Machine code emitter pass for PowerPC.
188 PM.add(createPPCCodeEmitterPass(*this, MCE));
190 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
192 PM.add(AsmPrinterCtor(errs(), *this));