blob: c07b33ec8fee366fd1170b60524e02ea7ab53d61 [file] [log] [blame]
Nate Begeman8c00f8c2005-08-04 07:12:09 +00001//===- PowerPCSubtarget.cpp - PPC Subtarget Information ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PPC specific subclass of TargetSubtarget.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner26689592005-10-14 23:51:18 +000014#include "PPCSubtarget.h"
15#include "PPC.h"
Nate Begeman8c00f8c2005-08-04 07:12:09 +000016#include "llvm/Module.h"
Chris Lattner3c304a32005-08-05 22:05:03 +000017#include "llvm/Support/CommandLine.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000018#include "llvm/Target/SubtargetFeature.h"
19
Chris Lattner3c304a32005-08-05 22:05:03 +000020using namespace llvm;
21PPCTargetEnum llvm::PPCTarget = TargetDefault;
22
23namespace llvm {
24 cl::opt<PPCTargetEnum, true>
25 PPCTargetArg(cl::desc("Force generation of code for a specific PPC target:"),
26 cl::values(
27 clEnumValN(TargetAIX, "aix", " Enable AIX codegen"),
Chris Lattner1e9de3e2005-09-02 18:33:05 +000028 clEnumValN(TargetDarwin,"darwin",
29 " Enable Darwin codegen"),
Chris Lattner3c304a32005-08-05 22:05:03 +000030 clEnumValEnd),
31 cl::location(PPCTarget), cl::init(TargetDefault));
Chris Lattner3c304a32005-08-05 22:05:03 +000032}
Nate Begeman8c00f8c2005-08-04 07:12:09 +000033
Jim Laskeyb1e11802005-09-01 21:38:21 +000034enum PowerPCFeature {
35 PowerPCFeature64Bit = 1 << 0,
36 PowerPCFeatureAltivec = 1 << 1,
37 PowerPCFeatureFSqrt = 1 << 2,
38 PowerPCFeatureGPUL = 1 << 3,
Nate Begeman9d2b8172005-10-18 00:56:42 +000039 PowerPCFeature64BRegs = 1 << 4
Jim Laskeyb1e11802005-09-01 21:38:21 +000040};
41
42/// Sorted (by key) array of values for CPU subtype.
43static const SubtargetFeatureKV PowerPCSubTypeKV[] = {
Jim Laskey839615a2005-09-02 19:27:43 +000044 { "601" , "Select the PowerPC 601 processor", 0 },
45 { "602" , "Select the PowerPC 602 processor", 0 },
46 { "603" , "Select the PowerPC 603 processor", 0 },
47 { "603e" , "Select the PowerPC 603e processor", 0 },
48 { "603ev" , "Select the PowerPC 603ev processor", 0 },
49 { "604" , "Select the PowerPC 604 processor", 0 },
50 { "604e" , "Select the PowerPC 604e processor", 0 },
51 { "620" , "Select the PowerPC 620 processor", 0 },
52 { "7400" , "Select the PowerPC 7400 (G4) processor",
53 PowerPCFeatureAltivec },
54 { "7450" , "Select the PowerPC 7450 (G4+) processor",
55 PowerPCFeatureAltivec },
56 { "750" , "Select the PowerPC 750 (G3) processor", 0 },
57 { "970" , "Select the PowerPC 970 (G5 - GPUL) processor",
58 PowerPCFeature64Bit | PowerPCFeatureAltivec |
Jim Laskeyb1e11802005-09-01 21:38:21 +000059 PowerPCFeatureFSqrt | PowerPCFeatureGPUL },
Jim Laskey839615a2005-09-02 19:27:43 +000060 { "g3" , "Select the PowerPC G3 (750) processor", 0 },
61 { "g4" , "Select the PowerPC G4 (7400) processor",
62 PowerPCFeatureAltivec },
63 { "g4+" , "Select the PowerPC G4+ (7450) processor",
64 PowerPCFeatureAltivec },
65 { "g5" , "Select the PowerPC g5 (970 - GPUL) processor",
66 PowerPCFeature64Bit | PowerPCFeatureAltivec |
Jim Laskeyb1e11802005-09-01 21:38:21 +000067 PowerPCFeatureFSqrt | PowerPCFeatureGPUL },
Jim Laskey839615a2005-09-02 19:27:43 +000068 { "generic", "Select instructions for a generic PowerPC processor", 0 }
Jim Laskeyb1e11802005-09-01 21:38:21 +000069};
70/// Length of PowerPCSubTypeKV.
71static const unsigned PowerPCSubTypeKVSize = sizeof(PowerPCSubTypeKV)
72 / sizeof(SubtargetFeatureKV);
73
74/// Sorted (by key) array of values for CPU features.
75static SubtargetFeatureKV PowerPCFeatureKV[] = {
Jim Laskey839615a2005-09-02 19:27:43 +000076 { "64bit" , "Should 64 bit instructions be used" , PowerPCFeature64Bit },
Nate Begeman9d2b8172005-10-18 00:56:42 +000077 { "64bitregs", "Should 64 bit registers be used" , PowerPCFeature64BRegs },
Jim Laskey839615a2005-09-02 19:27:43 +000078 { "altivec", "Should Altivec instructions be used" , PowerPCFeatureAltivec },
79 { "fsqrt" , "Should the fsqrt instruction be used", PowerPCFeatureFSqrt },
80 { "gpul" , "Should GPUL instructions be used" , PowerPCFeatureGPUL }
Jim Laskeyb1e11802005-09-01 21:38:21 +000081 };
82/// Length of PowerPCFeatureKV.
83static const unsigned PowerPCFeatureKVSize = sizeof(PowerPCFeatureKV)
84 / sizeof(SubtargetFeatureKV);
85
86
Nate Begeman8c00f8c2005-08-04 07:12:09 +000087#if defined(__APPLE__)
88#include <mach/mach.h>
89#include <mach/mach_host.h>
90#include <mach/host_info.h>
91#include <mach/machine.h>
92
Jim Laskeyb1e11802005-09-01 21:38:21 +000093/// GetCurrentPowerPCFeatures - Returns the current CPUs features.
94static const char *GetCurrentPowerPCCPU() {
Nate Begeman8c00f8c2005-08-04 07:12:09 +000095 host_basic_info_data_t hostInfo;
96 mach_msg_type_number_t infoCount;
97
98 infoCount = HOST_BASIC_INFO_COUNT;
99 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
100 &infoCount);
Jim Laskeyb1e11802005-09-01 21:38:21 +0000101
102 if (hostInfo.cpu_type != CPU_TYPE_POWERPC) return "generic";
103
104 switch(hostInfo.cpu_subtype) {
105 case CPU_SUBTYPE_POWERPC_601: return "601";
106 case CPU_SUBTYPE_POWERPC_602: return "602";
107 case CPU_SUBTYPE_POWERPC_603: return "603";
108 case CPU_SUBTYPE_POWERPC_603e: return "603e";
109 case CPU_SUBTYPE_POWERPC_603ev: return "603ev";
110 case CPU_SUBTYPE_POWERPC_604: return "604";
111 case CPU_SUBTYPE_POWERPC_604e: return "604e";
112 case CPU_SUBTYPE_POWERPC_620: return "620";
113 case CPU_SUBTYPE_POWERPC_750: return "750";
114 case CPU_SUBTYPE_POWERPC_7400: return "7400";
115 case CPU_SUBTYPE_POWERPC_7450: return "7450";
116 case CPU_SUBTYPE_POWERPC_970: return "970";
117 default: ;
118 }
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000119
Jim Laskeyb1e11802005-09-01 21:38:21 +0000120 return "generic";
121}
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000122#endif
123
Jim Laskeyb1e11802005-09-01 21:38:21 +0000124PPCSubtarget::PPCSubtarget(const Module &M, const std::string &FS)
Chris Lattner3c304a32005-08-05 22:05:03 +0000125 : StackAlignment(16), IsGigaProcessor(false), IsAIX(false), IsDarwin(false) {
126
Jim Laskeyb1e11802005-09-01 21:38:21 +0000127 // Determine default and user specified characteristics
Chris Lattnerc98d8232005-09-07 05:45:33 +0000128 std::string CPU = "generic";
Jim Laskeyb1e11802005-09-01 21:38:21 +0000129#if defined(__APPLE__)
130 CPU = GetCurrentPowerPCCPU();
131#endif
132 uint32_t Bits =
133 SubtargetFeatures::Parse(FS, CPU,
134 PowerPCSubTypeKV, PowerPCSubTypeKVSize,
135 PowerPCFeatureKV, PowerPCFeatureKVSize);
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000136 IsGigaProcessor = (Bits & PowerPCFeatureGPUL ) != 0;
Nate Begemand401dff2005-09-06 15:30:12 +0000137 Is64Bit = (Bits & PowerPCFeature64Bit) != 0;
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000138 HasFSQRT = (Bits & PowerPCFeatureFSqrt) != 0;
Nate Begeman9d2b8172005-10-18 00:56:42 +0000139 Has64BitRegs = (Bits & PowerPCFeature64BRegs) != 0;
Jim Laskeyb1e11802005-09-01 21:38:21 +0000140
141 // Set the boolean corresponding to the current target triple, or the default
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000142 // if one cannot be determined, to true.
143 const std::string& TT = M.getTargetTriple();
144 if (TT.length() > 5) {
Chris Lattner3c304a32005-08-05 22:05:03 +0000145 IsDarwin = TT.find("darwin") != std::string::npos;
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000146 } else if (TT.empty()) {
147#if defined(_POWER)
Chris Lattner3c304a32005-08-05 22:05:03 +0000148 IsAIX = true;
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000149#elif defined(__APPLE__)
Chris Lattner3c304a32005-08-05 22:05:03 +0000150 IsDarwin = true;
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000151#endif
152 }
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000153}