blob: b0861d734566af03ca74568075a5d56e985288c8 [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,
39};
40
41/// Sorted (by key) array of values for CPU subtype.
42static const SubtargetFeatureKV PowerPCSubTypeKV[] = {
Jim Laskey839615a2005-09-02 19:27:43 +000043 { "601" , "Select the PowerPC 601 processor", 0 },
44 { "602" , "Select the PowerPC 602 processor", 0 },
45 { "603" , "Select the PowerPC 603 processor", 0 },
46 { "603e" , "Select the PowerPC 603e processor", 0 },
47 { "603ev" , "Select the PowerPC 603ev processor", 0 },
48 { "604" , "Select the PowerPC 604 processor", 0 },
49 { "604e" , "Select the PowerPC 604e processor", 0 },
50 { "620" , "Select the PowerPC 620 processor", 0 },
51 { "7400" , "Select the PowerPC 7400 (G4) processor",
52 PowerPCFeatureAltivec },
53 { "7450" , "Select the PowerPC 7450 (G4+) processor",
54 PowerPCFeatureAltivec },
55 { "750" , "Select the PowerPC 750 (G3) processor", 0 },
56 { "970" , "Select the PowerPC 970 (G5 - GPUL) processor",
57 PowerPCFeature64Bit | PowerPCFeatureAltivec |
Jim Laskeyb1e11802005-09-01 21:38:21 +000058 PowerPCFeatureFSqrt | PowerPCFeatureGPUL },
Jim Laskey839615a2005-09-02 19:27:43 +000059 { "g3" , "Select the PowerPC G3 (750) processor", 0 },
60 { "g4" , "Select the PowerPC G4 (7400) processor",
61 PowerPCFeatureAltivec },
62 { "g4+" , "Select the PowerPC G4+ (7450) processor",
63 PowerPCFeatureAltivec },
64 { "g5" , "Select the PowerPC g5 (970 - GPUL) processor",
65 PowerPCFeature64Bit | PowerPCFeatureAltivec |
Jim Laskeyb1e11802005-09-01 21:38:21 +000066 PowerPCFeatureFSqrt | PowerPCFeatureGPUL },
Jim Laskey839615a2005-09-02 19:27:43 +000067 { "generic", "Select instructions for a generic PowerPC processor", 0 }
Jim Laskeyb1e11802005-09-01 21:38:21 +000068};
69/// Length of PowerPCSubTypeKV.
70static const unsigned PowerPCSubTypeKVSize = sizeof(PowerPCSubTypeKV)
71 / sizeof(SubtargetFeatureKV);
72
73/// Sorted (by key) array of values for CPU features.
74static SubtargetFeatureKV PowerPCFeatureKV[] = {
Jim Laskey839615a2005-09-02 19:27:43 +000075 { "64bit" , "Should 64 bit instructions be used" , PowerPCFeature64Bit },
76 { "altivec", "Should Altivec instructions be used" , PowerPCFeatureAltivec },
77 { "fsqrt" , "Should the fsqrt instruction be used", PowerPCFeatureFSqrt },
78 { "gpul" , "Should GPUL instructions be used" , PowerPCFeatureGPUL }
Jim Laskeyb1e11802005-09-01 21:38:21 +000079 };
80/// Length of PowerPCFeatureKV.
81static const unsigned PowerPCFeatureKVSize = sizeof(PowerPCFeatureKV)
82 / sizeof(SubtargetFeatureKV);
83
84
Nate Begeman8c00f8c2005-08-04 07:12:09 +000085#if defined(__APPLE__)
86#include <mach/mach.h>
87#include <mach/mach_host.h>
88#include <mach/host_info.h>
89#include <mach/machine.h>
90
Jim Laskeyb1e11802005-09-01 21:38:21 +000091/// GetCurrentPowerPCFeatures - Returns the current CPUs features.
92static const char *GetCurrentPowerPCCPU() {
Nate Begeman8c00f8c2005-08-04 07:12:09 +000093 host_basic_info_data_t hostInfo;
94 mach_msg_type_number_t infoCount;
95
96 infoCount = HOST_BASIC_INFO_COUNT;
97 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
98 &infoCount);
Jim Laskeyb1e11802005-09-01 21:38:21 +000099
100 if (hostInfo.cpu_type != CPU_TYPE_POWERPC) return "generic";
101
102 switch(hostInfo.cpu_subtype) {
103 case CPU_SUBTYPE_POWERPC_601: return "601";
104 case CPU_SUBTYPE_POWERPC_602: return "602";
105 case CPU_SUBTYPE_POWERPC_603: return "603";
106 case CPU_SUBTYPE_POWERPC_603e: return "603e";
107 case CPU_SUBTYPE_POWERPC_603ev: return "603ev";
108 case CPU_SUBTYPE_POWERPC_604: return "604";
109 case CPU_SUBTYPE_POWERPC_604e: return "604e";
110 case CPU_SUBTYPE_POWERPC_620: return "620";
111 case CPU_SUBTYPE_POWERPC_750: return "750";
112 case CPU_SUBTYPE_POWERPC_7400: return "7400";
113 case CPU_SUBTYPE_POWERPC_7450: return "7450";
114 case CPU_SUBTYPE_POWERPC_970: return "970";
115 default: ;
116 }
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000117
Jim Laskeyb1e11802005-09-01 21:38:21 +0000118 return "generic";
119}
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000120#endif
121
Jim Laskeyb1e11802005-09-01 21:38:21 +0000122PPCSubtarget::PPCSubtarget(const Module &M, const std::string &FS)
Chris Lattner3c304a32005-08-05 22:05:03 +0000123 : StackAlignment(16), IsGigaProcessor(false), IsAIX(false), IsDarwin(false) {
124
Jim Laskeyb1e11802005-09-01 21:38:21 +0000125 // Determine default and user specified characteristics
Chris Lattnerc98d8232005-09-07 05:45:33 +0000126 std::string CPU = "generic";
Jim Laskeyb1e11802005-09-01 21:38:21 +0000127#if defined(__APPLE__)
128 CPU = GetCurrentPowerPCCPU();
129#endif
130 uint32_t Bits =
131 SubtargetFeatures::Parse(FS, CPU,
132 PowerPCSubTypeKV, PowerPCSubTypeKVSize,
133 PowerPCFeatureKV, PowerPCFeatureKVSize);
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000134 IsGigaProcessor = (Bits & PowerPCFeatureGPUL ) != 0;
Nate Begemand401dff2005-09-06 15:30:12 +0000135 Is64Bit = (Bits & PowerPCFeature64Bit) != 0;
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000136 HasFSQRT = (Bits & PowerPCFeatureFSqrt) != 0;
Jim Laskeyb1e11802005-09-01 21:38:21 +0000137
138 // Set the boolean corresponding to the current target triple, or the default
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000139 // if one cannot be determined, to true.
140 const std::string& TT = M.getTargetTriple();
141 if (TT.length() > 5) {
Chris Lattner3c304a32005-08-05 22:05:03 +0000142 IsDarwin = TT.find("darwin") != std::string::npos;
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000143 } else if (TT.empty()) {
144#if defined(_POWER)
Chris Lattner3c304a32005-08-05 22:05:03 +0000145 IsAIX = true;
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000146#elif defined(__APPLE__)
Chris Lattner3c304a32005-08-05 22:05:03 +0000147 IsDarwin = true;
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000148#endif
149 }
Nate Begeman8c00f8c2005-08-04 07:12:09 +0000150}