Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1 | //===-- PowerPCTargetMachine.cpp - Define TargetMachine for PowerPC -------===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 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. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 13 | #include "PowerPC.h" |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 14 | #include "PowerPCTargetMachine.h" |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 15 | #include "PowerPCFrameInfo.h" |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 16 | #include "PPC32TargetMachine.h" |
| 17 | #include "PPC64TargetMachine.h" |
| 18 | #include "PPC32JITInfo.h" |
| 19 | #include "PPC64JITInfo.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/PassManager.h" |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Verifier.h" |
Misha Brukman | 8c9f520 | 2004-06-21 18:30:31 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
| 25 | #include "llvm/CodeGen/Passes.h" |
Chris Lattner | 68905bb | 2004-07-11 04:17:58 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | d36c970 | 2004-07-11 02:48:49 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachineRegistry.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Scalar.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | d36c970 | 2004-07-11 02:48:49 +0000 | [diff] [blame] | 30 | #include <iostream> |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 33 | bool llvm::GPOPT = false; |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 34 | PPCTargetEnum llvm::PPCTarget = TargetDefault; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 35 | |
Misha Brukman | 1d3527e | 2004-08-11 23:47:08 +0000 | [diff] [blame] | 36 | namespace llvm { |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 37 | cl::opt<PPCTargetEnum, true> |
| 38 | PPCTargetArg( |
| 39 | cl::desc("Force generation of code for a specific PPC target:"), |
| 40 | cl::values( |
| 41 | clEnumValN(TargetAIX, "aix", " Enable AIX codegen"), |
| 42 | clEnumValN(TargetDarwin,"darwin"," Enable Darwin codegen"), |
| 43 | clEnumValEnd), |
| 44 | cl::location(PPCTarget), cl::init(TargetDefault)); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 45 | cl::opt<bool> EnablePPCLSR("enable-lsr-for-ppc", |
| 46 | cl::desc("Enable LSR for PPC (beta)"), |
Chris Lattner | 0c74906 | 2005-03-02 06:19:22 +0000 | [diff] [blame] | 47 | cl::Hidden); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 48 | cl::opt<bool, true> EnableGPOPT("enable-gpopt", cl::Hidden, |
| 49 | cl::location(GPOPT), |
| 50 | cl::desc("Enable optimizations for GP cpus")); |
Misha Brukman | 1d3527e | 2004-08-11 23:47:08 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 53 | namespace { |
Misha Brukman | 66aa3e0 | 2004-08-17 05:06:47 +0000 | [diff] [blame] | 54 | const std::string PPC32ID = "PowerPC/32bit"; |
| 55 | const std::string PPC64ID = "PowerPC/64bit"; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 56 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 57 | // Register the targets |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 58 | RegisterTarget<PPC32TargetMachine> |
Chris Lattner | cbb9812 | 2004-10-10 16:26:13 +0000 | [diff] [blame] | 59 | X("ppc32", " PowerPC 32-bit"); |
Chris Lattner | f908888 | 2004-08-20 18:09:18 +0000 | [diff] [blame] | 60 | |
| 61 | #if 0 |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 62 | RegisterTarget<PPC64TargetMachine> |
Misha Brukman | 983e92d | 2004-08-19 21:36:14 +0000 | [diff] [blame] | 63 | Y("ppc64", " PowerPC 64-bit (unimplemented)"); |
Chris Lattner | f908888 | 2004-08-20 18:09:18 +0000 | [diff] [blame] | 64 | #endif |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Misha Brukman | 0145881 | 2004-08-11 00:11:25 +0000 | [diff] [blame] | 67 | PowerPCTargetMachine::PowerPCTargetMachine(const std::string &name, |
| 68 | IntrinsicLowering *IL, |
Nate Begeman | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 69 | const Module &M, |
Misha Brukman | 0145881 | 2004-08-11 00:11:25 +0000 | [diff] [blame] | 70 | const TargetData &TD, |
Chris Lattner | e4fce6f | 2004-11-23 05:56:40 +0000 | [diff] [blame] | 71 | const PowerPCFrameInfo &TFI) |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 72 | : TargetMachine(name, IL, TD), FrameInfo(TFI), Subtarget(M) { |
| 73 | if (TargetDefault == PPCTarget) { |
| 74 | if (Subtarget.IsAIX()) PPCTarget = TargetAIX; |
| 75 | if (Subtarget.IsDarwin()) PPCTarget = TargetDarwin; |
| 76 | } |
| 77 | } |
Chris Lattner | d36c970 | 2004-07-11 02:48:49 +0000 | [diff] [blame] | 78 | |
Chris Lattner | e4fce6f | 2004-11-23 05:56:40 +0000 | [diff] [blame] | 79 | unsigned PPC32TargetMachine::getJITMatchQuality() { |
Misha Brukman | 01eca8d | 2004-07-12 23:36:12 +0000 | [diff] [blame] | 80 | #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) |
| 81 | return 10; |
| 82 | #else |
| 83 | return 0; |
| 84 | #endif |
| 85 | } |
Misha Brukman | 01eca8d | 2004-07-12 23:36:12 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 0431c96 | 2005-06-25 02:48:37 +0000 | [diff] [blame] | 87 | /// addPassesToEmitFile - Add passes to the specified pass manager to implement |
| 88 | /// a static compiler for this target. |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 89 | /// |
Chris Lattner | 0431c96 | 2005-06-25 02:48:37 +0000 | [diff] [blame] | 90 | bool PowerPCTargetMachine::addPassesToEmitFile(PassManager &PM, |
| 91 | std::ostream &Out, |
| 92 | CodeGenFileType FileType) { |
| 93 | if (FileType != TargetMachine::AssemblyFile) return true; |
| 94 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 95 | bool LP64 = (0 != dynamic_cast<PPC64TargetMachine *>(this)); |
Chris Lattner | 0c74906 | 2005-03-02 06:19:22 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 4318a3d | 2005-03-02 21:56:00 +0000 | [diff] [blame] | 97 | if (EnablePPCLSR) { |
Chris Lattner | 0c74906 | 2005-03-02 06:19:22 +0000 | [diff] [blame] | 98 | PM.add(createLoopStrengthReducePass()); |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 99 | PM.add(createVerifierPass()); |
Chris Lattner | 4318a3d | 2005-03-02 21:56:00 +0000 | [diff] [blame] | 100 | PM.add(createCFGSimplificationPass()); |
| 101 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 102 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 103 | // FIXME: Implement efficient support for garbage collection intrinsics. |
| 104 | PM.add(createLowerGCPass()); |
| 105 | |
| 106 | // FIXME: Implement the invoke/unwind instructions! |
| 107 | PM.add(createLowerInvokePass()); |
| 108 | |
| 109 | // FIXME: Implement the switch instruction in the instruction selector! |
| 110 | PM.add(createLowerSwitchPass()); |
| 111 | |
| 112 | PM.add(createLowerConstantExpressionsPass()); |
| 113 | |
| 114 | // Make sure that no unreachable blocks are instruction selected. |
| 115 | PM.add(createUnreachableBlockEliminationPass()); |
| 116 | |
Nate Begeman | f8b0294 | 2005-04-15 22:12:16 +0000 | [diff] [blame] | 117 | // Default to pattern ISel |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 118 | if (LP64) |
Nate Begeman | d3e6b94 | 2005-04-05 08:51:15 +0000 | [diff] [blame] | 119 | PM.add(createPPC64ISelPattern(*this)); |
Nate Begeman | f8b0294 | 2005-04-15 22:12:16 +0000 | [diff] [blame] | 120 | else if (PatternISelTriState == 0) |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 121 | PM.add(createPPC32ISelSimple(*this)); |
Nate Begeman | f8b0294 | 2005-04-15 22:12:16 +0000 | [diff] [blame] | 122 | else |
| 123 | PM.add(createPPC32ISelPattern(*this)); |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 124 | |
| 125 | if (PrintMachineCode) |
| 126 | PM.add(createMachineFunctionPrinterPass(&std::cerr)); |
| 127 | |
| 128 | PM.add(createRegisterAllocator()); |
| 129 | |
| 130 | if (PrintMachineCode) |
| 131 | PM.add(createMachineFunctionPrinterPass(&std::cerr)); |
| 132 | |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 133 | PM.add(createPrologEpilogCodeInserter()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 134 | |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 135 | // Must run branch selection immediately preceding the asm printer |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 136 | PM.add(createPPCBranchSelectionPass()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 137 | |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 138 | // Decide which asm printer to use. If the user has not specified one on |
| 139 | // the command line, choose whichever one matches the default (current host). |
| 140 | switch (PPCTarget) { |
| 141 | case TargetDefault: |
| 142 | assert(0 && "Default host has no asm printer!"); |
| 143 | break; |
| 144 | case TargetAIX: |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 145 | PM.add(createAIXAsmPrinter(Out, *this)); |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 146 | break; |
| 147 | case TargetDarwin: |
Nate Begeman | ed42853 | 2004-09-04 05:00:00 +0000 | [diff] [blame] | 148 | PM.add(createDarwinAsmPrinter(Out, *this)); |
Nate Begeman | 3d72d14 | 2005-08-04 20:49:48 +0000 | [diff] [blame] | 149 | break; |
| 150 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 151 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 152 | PM.add(createMachineCodeDeleter()); |
| 153 | return false; |
| 154 | } |
| 155 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 156 | void PowerPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) { |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 157 | // The JIT does not support or need PIC. |
| 158 | PICEnabled = false; |
Nate Begeman | f8b0294 | 2005-04-15 22:12:16 +0000 | [diff] [blame] | 159 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 160 | bool LP64 = (0 != dynamic_cast<PPC64TargetMachine *>(&TM)); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 4318a3d | 2005-03-02 21:56:00 +0000 | [diff] [blame] | 162 | if (EnablePPCLSR) { |
Chris Lattner | 0c74906 | 2005-03-02 06:19:22 +0000 | [diff] [blame] | 163 | PM.add(createLoopStrengthReducePass()); |
Chris Lattner | 4318a3d | 2005-03-02 21:56:00 +0000 | [diff] [blame] | 164 | PM.add(createCFGSimplificationPass()); |
| 165 | } |
Chris Lattner | 0c74906 | 2005-03-02 06:19:22 +0000 | [diff] [blame] | 166 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 167 | // FIXME: Implement efficient support for garbage collection intrinsics. |
| 168 | PM.add(createLowerGCPass()); |
| 169 | |
| 170 | // FIXME: Implement the invoke/unwind instructions! |
| 171 | PM.add(createLowerInvokePass()); |
| 172 | |
| 173 | // FIXME: Implement the switch instruction in the instruction selector! |
| 174 | PM.add(createLowerSwitchPass()); |
| 175 | |
| 176 | PM.add(createLowerConstantExpressionsPass()); |
| 177 | |
| 178 | // Make sure that no unreachable blocks are instruction selected. |
| 179 | PM.add(createUnreachableBlockEliminationPass()); |
| 180 | |
Nate Begeman | f8b0294 | 2005-04-15 22:12:16 +0000 | [diff] [blame] | 181 | // Default to pattern ISel |
| 182 | if (LP64) |
| 183 | PM.add(createPPC64ISelPattern(TM)); |
| 184 | else if (PatternISelTriState == 0) |
| 185 | PM.add(createPPC32ISelSimple(TM)); |
| 186 | else |
| 187 | PM.add(createPPC32ISelPattern(TM)); |
| 188 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 189 | PM.add(createRegisterAllocator()); |
| 190 | PM.add(createPrologEpilogCodeInserter()); |
Chris Lattner | e4fce6f | 2004-11-23 05:56:40 +0000 | [diff] [blame] | 191 | |
| 192 | // Must run branch selection immediately preceding the asm printer |
| 193 | PM.add(createPPCBranchSelectionPass()); |
| 194 | |
| 195 | if (PrintMachineCode) |
| 196 | PM.add(createMachineFunctionPrinterPass(&std::cerr)); |
Misha Brukman | 0145881 | 2004-08-11 00:11:25 +0000 | [diff] [blame] | 197 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 198 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 199 | /// PowerPCTargetMachine ctor - Create an ILP32 architecture model |
| 200 | /// |
Misha Brukman | 66aa3e0 | 2004-08-17 05:06:47 +0000 | [diff] [blame] | 201 | PPC32TargetMachine::PPC32TargetMachine(const Module &M, IntrinsicLowering *IL) |
Nate Begeman | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 202 | : PowerPCTargetMachine(PPC32ID, IL, M, |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 203 | TargetData(PPC32ID,false,4,4,4,4,4,4,2,1,1), |
Chris Lattner | e4fce6f | 2004-11-23 05:56:40 +0000 | [diff] [blame] | 204 | PowerPCFrameInfo(*this, false)), JITInfo(*this) {} |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 205 | |
| 206 | /// PPC64TargetMachine ctor - Create a LP64 architecture model |
| 207 | /// |
| 208 | PPC64TargetMachine::PPC64TargetMachine(const Module &M, IntrinsicLowering *IL) |
Nate Begeman | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 209 | : PowerPCTargetMachine(PPC64ID, IL, M, |
Chris Lattner | 2130c08 | 2005-07-21 19:17:18 +0000 | [diff] [blame] | 210 | TargetData(PPC64ID,false,8,4,4,4,4,4,2,1,1), |
Chris Lattner | e4fce6f | 2004-11-23 05:56:40 +0000 | [diff] [blame] | 211 | PowerPCFrameInfo(*this, true)) {} |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 212 | |
| 213 | unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) { |
Chris Lattner | 3ea78c4 | 2004-12-12 17:40:28 +0000 | [diff] [blame] | 214 | // We strongly match "powerpc-*". |
| 215 | std::string TT = M.getTargetTriple(); |
| 216 | if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-") |
| 217 | return 20; |
| 218 | |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 219 | if (M.getEndianness() == Module::BigEndian && |
| 220 | M.getPointerSize() == Module::Pointer32) |
Chris Lattner | 3ea78c4 | 2004-12-12 17:40:28 +0000 | [diff] [blame] | 221 | return 10; // Weak match |
Nate Begeman | 7a4fe9b | 2004-08-11 07:40:04 +0000 | [diff] [blame] | 222 | else if (M.getEndianness() != Module::AnyEndianness || |
| 223 | M.getPointerSize() != Module::AnyPointerSize) |
| 224 | return 0; // Match for some other target |
| 225 | |
| 226 | return getJITMatchQuality()/2; |
| 227 | } |
| 228 | |
| 229 | unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) { |
| 230 | if (M.getEndianness() == Module::BigEndian && |
| 231 | M.getPointerSize() == Module::Pointer64) |
| 232 | return 10; // Direct match |
| 233 | else if (M.getEndianness() != Module::AnyEndianness || |
| 234 | M.getPointerSize() != Module::AnyPointerSize) |
| 235 | return 0; // Match for some other target |
| 236 | |
| 237 | return getJITMatchQuality()/2; |
| 238 | } |