1 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Top-level implementation for the PowerPC target.
12 //===----------------------------------------------------------------------===//
15 #include "PPCFrameInfo.h"
16 #include "PPCTargetMachine.h"
17 #include "PPCJITInfo.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Target/TargetMachineRegistry.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Support/CommandLine.h"
31 // Register the targets
32 RegisterTarget<PPC32TargetMachine>
33 X("ppc32", " PowerPC 32");
34 RegisterTarget<PPC64TargetMachine>
35 Y("ppc64", " PowerPC 64");
38 unsigned PPC32TargetMachine::getJITMatchQuality() {
39 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
40 if (sizeof(void*) == 4)
46 unsigned PPC64TargetMachine::getJITMatchQuality() {
47 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
48 if (sizeof(void*) == 8)
49 return 10 * 0/*FIXME: not PPC64-JIT support yet! */;
54 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
55 // We strongly match "powerpc-*".
56 std::string TT = M.getTargetTriple();
57 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
60 if (M.getEndianness() == Module::BigEndian &&
61 M.getPointerSize() == Module::Pointer32)
62 return 10; // Weak match
63 else if (M.getEndianness() != Module::AnyEndianness ||
64 M.getPointerSize() != Module::AnyPointerSize)
65 return 0; // Match for some other target
67 return getJITMatchQuality()/2;
70 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
71 // We strongly match "powerpc64-*".
72 std::string TT = M.getTargetTriple();
73 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
76 if (M.getEndianness() == Module::BigEndian &&
77 M.getPointerSize() == Module::Pointer64)
78 return 10; // Weak match
79 else if (M.getEndianness() != Module::AnyEndianness ||
80 M.getPointerSize() != Module::AnyPointerSize)
81 return 0; // Match for some other target
83 return getJITMatchQuality()/2;
87 PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
89 : TargetMachine("PowerPC"), Subtarget(M, FS, is64Bit),
90 DataLayout(Subtarget.getTargetDataString()),
91 FrameInfo(*this, false), JITInfo(*this), TLInfo(*this),
92 InstrItins(Subtarget.getInstrItineraryData()) {
94 if (TargetDefault == PPCTarget) {
95 if (Subtarget.isAIX()) PPCTarget = TargetAIX;
96 if (Subtarget.isDarwin()) PPCTarget = TargetDarwin;
98 if (getRelocationModel() == Reloc::Default)
99 if (Subtarget.isDarwin())
100 setRelocationModel(Reloc::DynamicNoPIC);
102 setRelocationModel(Reloc::PIC);
105 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
106 : PPCTargetMachine(M, FS, false) {
110 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
111 : PPCTargetMachine(M, FS, true) {
114 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
115 /// a static compiler for this target.
117 bool PPCTargetMachine::addPassesToEmitFile(PassManager &PM,
119 CodeGenFileType FileType,
121 if (FileType != TargetMachine::AssemblyFile) return true;
123 // Run loop strength reduction before anything else.
124 if (!Fast) PM.add(createLoopStrengthReducePass(&TLInfo));
126 // FIXME: Implement efficient support for garbage collection intrinsics.
127 PM.add(createLowerGCPass());
129 // FIXME: Implement the invoke/unwind instructions!
130 PM.add(createLowerInvokePass());
132 // Clean up after other passes, e.g. merging critical edges.
133 if (!Fast) PM.add(createCFGSimplificationPass());
135 // Make sure that no unreachable blocks are instruction selected.
136 PM.add(createUnreachableBlockEliminationPass());
138 // Install an instruction selector.
139 PM.add(createPPCISelDag(*this));
141 if (PrintMachineCode)
142 PM.add(createMachineFunctionPrinterPass(&std::cerr));
144 PM.add(createRegisterAllocator());
146 if (PrintMachineCode)
147 PM.add(createMachineFunctionPrinterPass(&std::cerr));
149 PM.add(createPrologEpilogCodeInserter());
151 // Must run branch selection immediately preceding the asm printer
152 PM.add(createPPCBranchSelectionPass());
154 // Decide which asm printer to use. If the user has not specified one on
155 // the command line, choose whichever one matches the default (current host).
158 PM.add(createAIXAsmPrinter(Out, *this));
162 PM.add(createDarwinAsmPrinter(Out, *this));
166 PM.add(createMachineCodeDeleter());
170 void PPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
171 // The JIT should use the static relocation model.
172 TM.setRelocationModel(Reloc::Static);
174 // Run loop strength reduction before anything else.
175 PM.add(createLoopStrengthReducePass(TM.getTargetLowering()));
177 // FIXME: Implement efficient support for garbage collection intrinsics.
178 PM.add(createLowerGCPass());
180 // FIXME: Implement the invoke/unwind instructions!
181 PM.add(createLowerInvokePass());
183 // Clean up after other passes, e.g. merging critical edges.
184 PM.add(createCFGSimplificationPass());
186 // Make sure that no unreachable blocks are instruction selected.
187 PM.add(createUnreachableBlockEliminationPass());
189 // Install an instruction selector.
190 PM.add(createPPCISelDag(TM));
192 PM.add(createRegisterAllocator());
193 PM.add(createPrologEpilogCodeInserter());
195 // Must run branch selection immediately preceding the asm printer
196 PM.add(createPPCBranchSelectionPass());
198 if (PrintMachineCode)
199 PM.add(createMachineFunctionPrinterPass(&std::cerr));