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)
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()), InstrInfo(*this),
91 FrameInfo(*this, false), JITInfo(*this), TLInfo(*this),
92 InstrItins(Subtarget.getInstrItineraryData()) {
94 if (getRelocationModel() == Reloc::Default)
95 if (Subtarget.isDarwin())
96 setRelocationModel(Reloc::DynamicNoPIC);
98 setRelocationModel(Reloc::PIC);
101 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
102 : PPCTargetMachine(M, FS, false) {
106 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
107 : PPCTargetMachine(M, FS, true) {
110 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
111 /// a static compiler for this target.
113 bool PPCTargetMachine::addPassesToEmitFile(PassManager &PM,
115 CodeGenFileType FileType,
117 if (FileType != TargetMachine::AssemblyFile) return true;
119 // Run loop strength reduction before anything else.
120 if (!Fast) PM.add(createLoopStrengthReducePass(&TLInfo));
122 // FIXME: Implement efficient support for garbage collection intrinsics.
123 PM.add(createLowerGCPass());
125 // FIXME: Implement the invoke/unwind instructions!
126 PM.add(createLowerInvokePass());
128 // Clean up after other passes, e.g. merging critical edges.
129 if (!Fast) PM.add(createCFGSimplificationPass());
131 // Make sure that no unreachable blocks are instruction selected.
132 PM.add(createUnreachableBlockEliminationPass());
134 // Install an instruction selector.
135 PM.add(createPPCISelDag(*this));
137 if (PrintMachineCode)
138 PM.add(createMachineFunctionPrinterPass(&std::cerr));
140 PM.add(createRegisterAllocator());
142 if (PrintMachineCode)
143 PM.add(createMachineFunctionPrinterPass(&std::cerr));
145 PM.add(createPrologEpilogCodeInserter());
147 // Must run branch selection immediately preceding the asm printer
148 PM.add(createPPCBranchSelectionPass());
150 // Decide which asm printer to use. If the user has not specified one on
151 // the command line, choose whichever one matches the default (current host).
152 if (Subtarget.isAIX())
153 PM.add(createAIXAsmPrinter(Out, *this));
155 PM.add(createDarwinAsmPrinter(Out, *this));
157 PM.add(createMachineCodeDeleter());
161 void PPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
162 // The JIT should use the static relocation model.
163 TM.setRelocationModel(Reloc::Static);
165 // Run loop strength reduction before anything else.
166 PM.add(createLoopStrengthReducePass(TM.getTargetLowering()));
168 // FIXME: Implement efficient support for garbage collection intrinsics.
169 PM.add(createLowerGCPass());
171 // FIXME: Implement the invoke/unwind instructions!
172 PM.add(createLowerInvokePass());
174 // Clean up after other passes, e.g. merging critical edges.
175 PM.add(createCFGSimplificationPass());
177 // Make sure that no unreachable blocks are instruction selected.
178 PM.add(createUnreachableBlockEliminationPass());
180 // Install an instruction selector.
181 PM.add(createPPCISelDag(TM));
183 PM.add(createRegisterAllocator());
184 PM.add(createPrologEpilogCodeInserter());
186 // Must run branch selection immediately preceding the asm printer
187 PM.add(createPPCBranchSelectionPass());
189 if (PrintMachineCode)
190 PM.add(createMachineFunctionPrinterPass(&std::cerr));