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