1 //===-- PowerPCTargetMachine.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 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
14 #include "PowerPCTargetMachine.h"
15 #include "PowerPCFrameInfo.h"
16 #include "PPC32TargetMachine.h"
17 #include "PPC64TargetMachine.h"
18 #include "PPC32JITInfo.h"
19 #include "PPC64JITInfo.h"
20 #include "llvm/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/CodeGen/IntrinsicLowering.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Target/TargetMachineRegistry.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Support/CommandLine.h"
32 bool llvm::GPOPT = false;
35 cl::opt<bool> AIX("aix",
36 cl::desc("Generate AIX/xcoff instead of Darwin/MachO"),
38 cl::opt<bool> EnablePPCLSR("enable-lsr-for-ppc",
39 cl::desc("Enable LSR for PPC (beta)"),
41 cl::opt<bool, true> EnableGPOPT("enable-gpopt", cl::Hidden,
43 cl::desc("Enable optimizations for GP cpus"));
47 const std::string PPC32ID = "PowerPC/32bit";
48 const std::string PPC64ID = "PowerPC/64bit";
50 // Register the targets
51 RegisterTarget<PPC32TargetMachine>
52 X("ppc32", " PowerPC 32-bit");
55 RegisterTarget<PPC64TargetMachine>
56 Y("ppc64", " PowerPC 64-bit (unimplemented)");
60 PowerPCTargetMachine::PowerPCTargetMachine(const std::string &name,
61 IntrinsicLowering *IL,
63 const PowerPCFrameInfo &TFI)
64 : TargetMachine(name, IL, TD), FrameInfo(TFI)
67 unsigned PPC32TargetMachine::getJITMatchQuality() {
68 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
75 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
76 /// a static compiler for this target.
78 bool PowerPCTargetMachine::addPassesToEmitFile(PassManager &PM,
80 CodeGenFileType FileType) {
81 if (FileType != TargetMachine::AssemblyFile) return true;
83 bool LP64 = (0 != dynamic_cast<PPC64TargetMachine *>(this));
86 PM.add(createLoopStrengthReducePass());
87 PM.add(createCFGSimplificationPass());
90 // FIXME: Implement efficient support for garbage collection intrinsics.
91 PM.add(createLowerGCPass());
93 // FIXME: Implement the invoke/unwind instructions!
94 PM.add(createLowerInvokePass());
96 // FIXME: Implement the switch instruction in the instruction selector!
97 PM.add(createLowerSwitchPass());
99 PM.add(createLowerConstantExpressionsPass());
101 // Make sure that no unreachable blocks are instruction selected.
102 PM.add(createUnreachableBlockEliminationPass());
104 // Default to pattern ISel
106 PM.add(createPPC64ISelPattern(*this));
107 else if (PatternISelTriState == 0)
108 PM.add(createPPC32ISelSimple(*this));
110 PM.add(createPPC32ISelPattern(*this));
112 if (PrintMachineCode)
113 PM.add(createMachineFunctionPrinterPass(&std::cerr));
115 PM.add(createRegisterAllocator());
117 if (PrintMachineCode)
118 PM.add(createMachineFunctionPrinterPass(&std::cerr));
120 PM.add(createPrologEpilogCodeInserter());
122 // Must run branch selection immediately preceding the asm printer
123 PM.add(createPPCBranchSelectionPass());
126 PM.add(createAIXAsmPrinter(Out, *this));
128 PM.add(createDarwinAsmPrinter(Out, *this));
130 PM.add(createMachineCodeDeleter());
134 void PowerPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
135 // The JIT does not support or need PIC.
138 bool LP64 = (0 != dynamic_cast<PPC64TargetMachine *>(&TM));
141 PM.add(createLoopStrengthReducePass());
142 PM.add(createCFGSimplificationPass());
145 // FIXME: Implement efficient support for garbage collection intrinsics.
146 PM.add(createLowerGCPass());
148 // FIXME: Implement the invoke/unwind instructions!
149 PM.add(createLowerInvokePass());
151 // FIXME: Implement the switch instruction in the instruction selector!
152 PM.add(createLowerSwitchPass());
154 PM.add(createLowerConstantExpressionsPass());
156 // Make sure that no unreachable blocks are instruction selected.
157 PM.add(createUnreachableBlockEliminationPass());
159 // Default to pattern ISel
161 PM.add(createPPC64ISelPattern(TM));
162 else if (PatternISelTriState == 0)
163 PM.add(createPPC32ISelSimple(TM));
165 PM.add(createPPC32ISelPattern(TM));
167 PM.add(createRegisterAllocator());
168 PM.add(createPrologEpilogCodeInserter());
170 // Must run branch selection immediately preceding the asm printer
171 PM.add(createPPCBranchSelectionPass());
173 if (PrintMachineCode)
174 PM.add(createMachineFunctionPrinterPass(&std::cerr));
177 /// PowerPCTargetMachine ctor - Create an ILP32 architecture model
179 PPC32TargetMachine::PPC32TargetMachine(const Module &M, IntrinsicLowering *IL)
180 : PowerPCTargetMachine(PPC32ID, IL,
181 TargetData(PPC32ID,false,4,4,4,4,4,4,2,1,1),
182 PowerPCFrameInfo(*this, false)), JITInfo(*this) {}
184 /// PPC64TargetMachine ctor - Create a LP64 architecture model
186 PPC64TargetMachine::PPC64TargetMachine(const Module &M, IntrinsicLowering *IL)
187 : PowerPCTargetMachine(PPC64ID, IL,
188 TargetData(PPC64ID,false,8,4,4,4,4,4,2,1,1),
189 PowerPCFrameInfo(*this, true)) {}
191 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
192 // We strongly match "powerpc-*".
193 std::string TT = M.getTargetTriple();
194 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
197 if (M.getEndianness() == Module::BigEndian &&
198 M.getPointerSize() == Module::Pointer32)
199 return 10; // Weak match
200 else if (M.getEndianness() != Module::AnyEndianness ||
201 M.getPointerSize() != Module::AnyPointerSize)
202 return 0; // Match for some other target
204 return getJITMatchQuality()/2;
207 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
208 if (M.getEndianness() == Module::BigEndian &&
209 M.getPointerSize() == Module::Pointer64)
210 return 10; // Direct match
211 else if (M.getEndianness() != Module::AnyEndianness ||
212 M.getPointerSize() != Module::AnyPointerSize)
213 return 0; // Match for some other target
215 return getJITMatchQuality()/2;