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)
45 unsigned PPC64TargetMachine::getJITMatchQuality() {
46 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
47 if (sizeof(void*) == 8)
53 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
54 // We strongly match "powerpc-*".
55 std::string TT = M.getTargetTriple();
56 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
59 if (M.getEndianness() == Module::BigEndian &&
60 M.getPointerSize() == Module::Pointer32)
61 return 10; // Weak match
62 else if (M.getEndianness() != Module::AnyEndianness ||
63 M.getPointerSize() != Module::AnyPointerSize)
64 return 0; // Match for some other target
66 return getJITMatchQuality()/2;
69 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
70 // We strongly match "powerpc64-*".
71 std::string TT = M.getTargetTriple();
72 if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
75 if (M.getEndianness() == Module::BigEndian &&
76 M.getPointerSize() == Module::Pointer64)
77 return 10; // Weak match
78 else if (M.getEndianness() != Module::AnyEndianness ||
79 M.getPointerSize() != Module::AnyPointerSize)
80 return 0; // Match for some other target
82 return getJITMatchQuality()/2;
86 PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
88 : TargetMachine("PowerPC"), Subtarget(M, FS, is64Bit),
89 DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
90 FrameInfo(*this, false), JITInfo(*this), TLInfo(*this),
91 InstrItins(Subtarget.getInstrItineraryData()) {
93 if (getRelocationModel() == Reloc::Default)
94 if (Subtarget.isDarwin())
95 setRelocationModel(Reloc::DynamicNoPIC);
97 setRelocationModel(Reloc::PIC);
100 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
101 : PPCTargetMachine(M, FS, false) {
105 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
106 : PPCTargetMachine(M, FS, true) {
109 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
110 /// a static compiler for this target.
112 bool PPCTargetMachine::addPassesToEmitFile(PassManager &PM,
114 CodeGenFileType FileType,
116 if (FileType != TargetMachine::AssemblyFile) return true;
118 // Run loop strength reduction before anything else.
119 if (!Fast) PM.add(createLoopStrengthReducePass(&TLInfo));
121 // FIXME: Implement efficient support for garbage collection intrinsics.
122 PM.add(createLowerGCPass());
124 // FIXME: Implement the invoke/unwind instructions!
125 PM.add(createLowerInvokePass());
127 // Clean up after other passes, e.g. merging critical edges.
128 if (!Fast) PM.add(createCFGSimplificationPass());
130 // Make sure that no unreachable blocks are instruction selected.
131 PM.add(createUnreachableBlockEliminationPass());
133 // Install an instruction selector.
134 PM.add(createPPCISelDag(*this));
136 if (PrintMachineCode)
137 PM.add(createMachineFunctionPrinterPass(&std::cerr));
139 PM.add(createRegisterAllocator());
141 if (PrintMachineCode)
142 PM.add(createMachineFunctionPrinterPass(&std::cerr));
144 PM.add(createPrologEpilogCodeInserter());
146 // Must run branch selection immediately preceding the asm printer
147 PM.add(createPPCBranchSelectionPass());
149 // Decide which asm printer to use. If the user has not specified one on
150 // the command line, choose whichever one matches the default (current host).
151 if (Subtarget.isAIX())
152 PM.add(createAIXAsmPrinter(Out, *this));
154 PM.add(createDarwinAsmPrinter(Out, *this));
156 PM.add(createMachineCodeDeleter());
160 void PPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
161 // The JIT should use the static relocation model.
162 TM.setRelocationModel(Reloc::Static);
164 // Run loop strength reduction before anything else.
165 PM.add(createLoopStrengthReducePass(TM.getTargetLowering()));
167 // FIXME: Implement efficient support for garbage collection intrinsics.
168 PM.add(createLowerGCPass());
170 // FIXME: Implement the invoke/unwind instructions!
171 PM.add(createLowerInvokePass());
173 // Clean up after other passes, e.g. merging critical edges.
174 PM.add(createCFGSimplificationPass());
176 // Make sure that no unreachable blocks are instruction selected.
177 PM.add(createUnreachableBlockEliminationPass());
179 // Install an instruction selector.
180 PM.add(createPPCISelDag(TM));
182 PM.add(createRegisterAllocator());
183 PM.add(createPrologEpilogCodeInserter());
185 // Must run branch selection immediately preceding the asm printer
186 PM.add(createPPCBranchSelectionPass());
188 if (PrintMachineCode)
189 PM.add(createMachineFunctionPrinterPass(&std::cerr));