More PPC32 -> PPC changes, as well as merging some classes that were
[oota-llvm.git] / lib / Target / PowerPC / PPCTargetMachine.cpp
1 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Top-level implementation for the PowerPC target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPC.h"
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/IntrinsicLowering.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Target/TargetMachineRegistry.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Support/CommandLine.h"
28 #include <iostream>
29 using namespace llvm;
30
31 namespace {
32   static cl::opt<bool> DisablePPCDAGDAG("disable-ppc-dag-isel", cl::Hidden,
33                              cl::desc("Disable DAG-to-DAG isel for PPC"));
34   
35   // Register the targets
36   RegisterTarget<PPCTargetMachine>
37   X("ppc32", "  PowerPC");
38 }
39
40 unsigned PPCTargetMachine::getJITMatchQuality() {
41 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
42   return 10;
43 #else
44   return 0;
45 #endif
46 }
47
48 unsigned PPCTargetMachine::getModuleMatchQuality(const Module &M) {
49   // We strongly match "powerpc-*".
50   std::string TT = M.getTargetTriple();
51   if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
52     return 20;
53   
54   if (M.getEndianness()  == Module::BigEndian &&
55       M.getPointerSize() == Module::Pointer32)
56     return 10;                                   // Weak match
57   else if (M.getEndianness() != Module::AnyEndianness ||
58            M.getPointerSize() != Module::AnyPointerSize)
59     return 0;                                    // Match for some other target
60   
61   return getJITMatchQuality()/2;
62 }
63
64 PPCTargetMachine::PPCTargetMachine(const Module &M, IntrinsicLowering *IL,
65                                    const std::string &FS)
66 : TargetMachine("PowerPC", IL, false, 4, 4, 4, 4, 4, 4, 2, 1, 1),
67   Subtarget(M, FS), FrameInfo(*this, false), JITInfo(*this) {
68   if (TargetDefault == PPCTarget) {
69     if (Subtarget.isAIX()) PPCTarget = TargetAIX;
70     if (Subtarget.isDarwin()) PPCTarget = TargetDarwin;
71   }
72 }
73
74 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
75 /// a static compiler for this target.
76 ///
77 bool PPCTargetMachine::addPassesToEmitFile(PassManager &PM,
78                                            std::ostream &Out,
79                                            CodeGenFileType FileType) {
80   if (FileType != TargetMachine::AssemblyFile) return true;
81
82   // Run loop strength reduction before anything else.
83   PM.add(createLoopStrengthReducePass());
84
85   // FIXME: Implement efficient support for garbage collection intrinsics.
86   PM.add(createLowerGCPass());
87
88   // FIXME: Implement the invoke/unwind instructions!
89   PM.add(createLowerInvokePass());
90   
91   // Clean up after other passes, e.g. merging critical edges.
92   PM.add(createCFGSimplificationPass());
93
94   // FIXME: Implement the switch instruction in the instruction selector!
95   PM.add(createLowerSwitchPass());
96
97   // Make sure that no unreachable blocks are instruction selected.
98   PM.add(createUnreachableBlockEliminationPass());
99
100   // Install an instruction selector.
101   if (!DisablePPCDAGDAG)
102     PM.add(createPPC32ISelDag(*this));
103   else
104     PM.add(createPPC32ISelPattern(*this));
105
106   if (PrintMachineCode)
107     PM.add(createMachineFunctionPrinterPass(&std::cerr));
108
109   PM.add(createRegisterAllocator());
110
111   if (PrintMachineCode)
112     PM.add(createMachineFunctionPrinterPass(&std::cerr));
113
114   PM.add(createPrologEpilogCodeInserter());
115
116   // Must run branch selection immediately preceding the asm printer
117   PM.add(createPPCBranchSelectionPass());
118
119   // Decide which asm printer to use.  If the user has not specified one on
120   // the command line, choose whichever one matches the default (current host).
121   switch (PPCTarget) {
122   case TargetAIX:
123     PM.add(createAIXAsmPrinter(Out, *this));
124     break;
125   case TargetDefault:
126   case TargetDarwin:
127     PM.add(createDarwinAsmPrinter(Out, *this));
128     break;
129   }
130
131   PM.add(createMachineCodeDeleter());
132   return false;
133 }
134
135 void PPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
136   // The JIT does not support or need PIC.
137   PICEnabled = false;
138
139   // Run loop strength reduction before anything else.
140   PM.add(createLoopStrengthReducePass());
141
142   // FIXME: Implement efficient support for garbage collection intrinsics.
143   PM.add(createLowerGCPass());
144
145   // FIXME: Implement the invoke/unwind instructions!
146   PM.add(createLowerInvokePass());
147
148   // Clean up after other passes, e.g. merging critical edges.
149   PM.add(createCFGSimplificationPass());
150
151   // FIXME: Implement the switch instruction in the instruction selector!
152   PM.add(createLowerSwitchPass());
153
154   // Make sure that no unreachable blocks are instruction selected.
155   PM.add(createUnreachableBlockEliminationPass());
156
157   // Install an instruction selector.
158   if (!DisablePPCDAGDAG)
159     PM.add(createPPC32ISelDag(TM));
160   else
161     PM.add(createPPC32ISelPattern(TM));
162
163   PM.add(createRegisterAllocator());
164   PM.add(createPrologEpilogCodeInserter());
165
166   // Must run branch selection immediately preceding the asm printer
167   PM.add(createPPCBranchSelectionPass());
168
169   if (PrintMachineCode)
170     PM.add(createMachineFunctionPrinterPass(&std::cerr));
171 }
172