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 //===----------------------------------------------------------------------===//
10 // Top-level implementation for the PowerPC target.
12 //===----------------------------------------------------------------------===//
15 #include "PowerPCTargetMachine.h"
16 #include "PowerPCFrameInfo.h"
17 #include "PPC32TargetMachine.h"
18 #include "PPC32JITInfo.h"
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Analysis/Verifier.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"
33 const char *PPC32ID = "PowerPC/32bit";
35 static cl::opt<bool> EnablePPCDAGDAG("enable-ppc-dag-isel", cl::Hidden,
36 cl::desc("Enable DAG-to-DAG isel for PPC (beta)"));
38 // Register the targets
39 RegisterTarget<PPC32TargetMachine>
40 X("ppc32", " PowerPC 32-bit");
43 PowerPCTargetMachine::PowerPCTargetMachine(const std::string &name,
44 IntrinsicLowering *IL,
47 const PowerPCFrameInfo &TFI)
48 : TargetMachine(name, IL, TD), FrameInfo(TFI), Subtarget(M) {
49 if (TargetDefault == PPCTarget) {
50 if (Subtarget.isAIX()) PPCTarget = TargetAIX;
51 if (Subtarget.isDarwin()) PPCTarget = TargetDarwin;
55 unsigned PPC32TargetMachine::getJITMatchQuality() {
56 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
63 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
64 /// a static compiler for this target.
66 bool PowerPCTargetMachine::addPassesToEmitFile(PassManager &PM,
68 CodeGenFileType FileType) {
69 if (FileType != TargetMachine::AssemblyFile) return true;
71 // Run loop strength reduction before anything else.
72 PM.add(createLoopStrengthReducePass());
73 PM.add(createCFGSimplificationPass());
75 // FIXME: Implement efficient support for garbage collection intrinsics.
76 PM.add(createLowerGCPass());
78 // FIXME: Implement the invoke/unwind instructions!
79 PM.add(createLowerInvokePass());
81 // FIXME: Implement the switch instruction in the instruction selector!
82 PM.add(createLowerSwitchPass());
84 // Make sure that no unreachable blocks are instruction selected.
85 PM.add(createUnreachableBlockEliminationPass());
87 // Install an instruction selector.
89 PM.add(createPPC32ISelDag(*this));
91 PM.add(createPPC32ISelPattern(*this));
94 PM.add(createMachineFunctionPrinterPass(&std::cerr));
96 PM.add(createRegisterAllocator());
99 PM.add(createMachineFunctionPrinterPass(&std::cerr));
101 PM.add(createPrologEpilogCodeInserter());
103 // Must run branch selection immediately preceding the asm printer
104 PM.add(createPPCBranchSelectionPass());
106 // Decide which asm printer to use. If the user has not specified one on
107 // the command line, choose whichever one matches the default (current host).
110 PM.add(createAIXAsmPrinter(Out, *this));
114 PM.add(createDarwinAsmPrinter(Out, *this));
118 PM.add(createMachineCodeDeleter());
122 void PowerPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
123 // The JIT does not support or need PIC.
126 // Run loop strength reduction before anything else.
127 PM.add(createLoopStrengthReducePass());
128 PM.add(createCFGSimplificationPass());
130 // FIXME: Implement efficient support for garbage collection intrinsics.
131 PM.add(createLowerGCPass());
133 // FIXME: Implement the invoke/unwind instructions!
134 PM.add(createLowerInvokePass());
136 // FIXME: Implement the switch instruction in the instruction selector!
137 PM.add(createLowerSwitchPass());
139 // Make sure that no unreachable blocks are instruction selected.
140 PM.add(createUnreachableBlockEliminationPass());
142 // Install an instruction selector.
143 PM.add(createPPC32ISelPattern(TM));
145 PM.add(createRegisterAllocator());
146 PM.add(createPrologEpilogCodeInserter());
148 // Must run branch selection immediately preceding the asm printer
149 PM.add(createPPCBranchSelectionPass());
151 if (PrintMachineCode)
152 PM.add(createMachineFunctionPrinterPass(&std::cerr));
155 /// PowerPCTargetMachine ctor - Create an ILP32 architecture model
157 PPC32TargetMachine::PPC32TargetMachine(const Module &M, IntrinsicLowering *IL)
158 : PowerPCTargetMachine(PPC32ID, IL, M,
159 TargetData(PPC32ID,false,4,4,4,4,4,4,2,1,1),
160 PowerPCFrameInfo(*this, false)), JITInfo(*this) {}
162 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
163 // We strongly match "powerpc-*".
164 std::string TT = M.getTargetTriple();
165 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
168 if (M.getEndianness() == Module::BigEndian &&
169 M.getPointerSize() == Module::Pointer32)
170 return 10; // Weak match
171 else if (M.getEndianness() != Module::AnyEndianness ||
172 M.getPointerSize() != Module::AnyPointerSize)
173 return 0; // Match for some other target
175 return getJITMatchQuality()/2;