blob: ddad08a1b4ca5be9394efc0dc7b4d1bd00a6fcc2 [file] [log] [blame]
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +00001#include "llvm/ADT/StringMap.h"
2
Mikhail Glushenkov84612022009-12-07 17:03:21 +00003#include <string>
4#include <vector>
5
6namespace hooks {
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +00007
8/// NUM_KEYS - Calculate the size of a const char* array.
9#define NUM_KEYS(Keys) sizeof(Keys) / sizeof(const char*)
10
11// See http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
12inline unsigned NextHighestPowerOf2 (unsigned i) {
Mikhail Glushenkov24d7fa22010-12-15 01:22:34 +000013 --i;
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000014 i |= i >> 1;
15 i |= i >> 2;
16 i |= i >> 4;
17 i |= i >> 8;
18 i |= i >> 16;
Mikhail Glushenkov24d7fa22010-12-15 01:22:34 +000019 ++i;
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000020 return i;
21}
22
Mikhail Glushenkov84612022009-12-07 17:03:21 +000023typedef std::vector<std::string> StrVec;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000024typedef llvm::StringMap<const char*> ArgMap;
Mikhail Glushenkov84612022009-12-07 17:03:21 +000025
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000026/// AddPlusOrMinus - Convert 'no-foo' to '-foo' and 'foo' to '+foo'.
27void AddPlusOrMinus (const std::string& Arg, std::string& out) {
28 if (Arg.find("no-") == 0 && Arg[3] != 0) {
29 out += '-';
30 out += Arg.c_str() + 3;
Mikhail Glushenkov84612022009-12-07 17:03:21 +000031 }
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000032 else {
33 out += '+';
34 out += Arg;
35 }
Mikhail Glushenkov84612022009-12-07 17:03:21 +000036}
37
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000038// -march values that need to be special-cased.
39const char* MArchKeysARM[] = { "armv4t", "armv5t", "armv5te", "armv6",
Mikhail Glushenkov26e10ae2010-12-15 01:22:15 +000040 "armv6-m", "armv6t2", "armv7-a", "armv7-m" };
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000041const char* MArchValuesARM[] = { "v4t", "v5t", "v5te", "v6", "v6m", "v6t2",
Mikhail Glushenkov26e10ae2010-12-15 01:22:15 +000042 "v7a", "v7m" };
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000043const unsigned MArchNumKeysARM = NUM_KEYS(MArchKeysARM);
44const unsigned MArchMapSize = NextHighestPowerOf2(MArchNumKeysARM);
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000045
Mikhail Glushenkov42db9972010-12-15 01:22:25 +000046// -march values that should be forwarded as -mcpu
47const char* MArchMCpuKeysARM[] = { "iwmmxt", "ep9312" };
48const char* MArchMCpuValuesARM[] = { "iwmmxt", "ep9312"};
49const unsigned MArchMCpuNumKeysARM = NUM_KEYS(MArchMCpuKeysARM);
Mikhail Glushenkov42db9972010-12-15 01:22:25 +000050
51
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000052void FillInArgMap(ArgMap& Args, const char* Keys[],
53 const char* Values[], unsigned NumKeys)
54{
55 for (unsigned i = 0; i < NumKeys; ++i) {
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000056 // Explicit cast to StringRef here is necessary to pick up the right
57 // overload.
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000058 Args.GetOrCreateValue(llvm::StringRef(Keys[i]), Values[i]);
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000059 }
60}
61
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000062/// ConvertMArchToMAttr - Convert -march from the gcc dialect to
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000063/// something llc can understand.
64std::string ConvertMArchToMAttr(const StrVec& Opts) {
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000065 static ArgMap MArchMap(MArchMapSize);
Mikhail Glushenkov42db9972010-12-15 01:22:25 +000066 static ArgMap MArchMCpuMap(MArchMapSize);
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000067 static bool StaticDataInitialized = false;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000068
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000069 if (!StaticDataInitialized) {
70 FillInArgMap(MArchMap, MArchKeysARM, MArchValuesARM, MArchNumKeysARM);
Mikhail Glushenkov42db9972010-12-15 01:22:25 +000071 FillInArgMap(MArchMCpuMap, MArchMCpuKeysARM,
72 MArchMCpuValuesARM, MArchMCpuNumKeysARM);
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000073 StaticDataInitialized = true;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000074 }
75
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000076 std::string mattr("-mattr=");
77 std::string mcpu("-mcpu=");
78 bool mattrTouched = false;
79 bool mcpuTouched = false;
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000080
81 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
82 const std::string& Arg = *B;
83
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000084 // Check if the argument should be forwarded to -mcpu instead of -mattr.
85 {
86 ArgMap::const_iterator I = MArchMCpuMap.find(Arg);
87
88 if (I != MArchMCpuMap.end()) {
89 mcpuTouched = true;
90 mcpu += I->getValue();
91 continue;
92 }
93 }
94
Mikhail Glushenkov24d7fa22010-12-15 01:22:34 +000095 if (mattrTouched)
96 mattr += ",";
97
98 // Check if the argument is a special case.
99 {
100 ArgMap::const_iterator I = MArchMap.find(Arg);
101
102 if (I != MArchMap.end()) {
103 mattrTouched = true;
104 mattr += '+';
105 mattr += I->getValue();
106 continue;
107 }
108 }
109
Mikhail Glushenkov36604a62010-12-15 01:22:29 +0000110 AddPlusOrMinus(Arg, mattr);
111 }
112
113 std::string out;
114 if (mattrTouched)
115 out += mattr;
116 if (mcpuTouched)
117 out += (mattrTouched ? " " : "") + mcpu;
118
119 return out;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +0000120}
121
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +0000122// -mcpu values that need to be special-cased.
123const char* MCpuKeysPPC[] = { "G3", "G4", "G5", "powerpc", "powerpc64"};
124const char* MCpuValuesPPC[] = { "g3", "g4", "g5", "ppc", "ppc64"};
125const unsigned MCpuNumKeysPPC = NUM_KEYS(MCpuKeysPPC);
126const unsigned MCpuMapSize = NextHighestPowerOf2(MCpuNumKeysPPC);
127
128/// ConvertMCpu - Convert -mcpu value from the gcc to the llc dialect.
129std::string ConvertMCpu(const char* Val) {
130 static ArgMap MCpuMap(MCpuMapSize);
131 static bool StaticDataInitialized = false;
132
133 if (!StaticDataInitialized) {
134 FillInArgMap(MCpuMap, MCpuKeysPPC, MCpuValuesPPC, MCpuNumKeysPPC);
135 StaticDataInitialized = true;
136 }
137
138 std::string ret = "-mcpu=";
139 ArgMap::const_iterator I = MCpuMap.find(Val);
140 if (I != MCpuMap.end()) {
141 return ret + I->getValue();
142 }
143 return ret + Val;
144}
145
146// -mfpu values that need to be special-cased.
147const char* MFpuKeysARM[] = { "vfp", "vfpv3",
148 "vfpv3-fp16", "vfpv3-d16", "vfpv3-d16-fp16",
149 "neon", "neon-fp16" };
150const char* MFpuValuesARM[] = { "vfp2", "vfp3",
151 "+vfp3,+fp16", "+vfp3,+d16", "+vfp3,+d16,+fp16",
152 "+neon", "+neon,+neonfp" };
153const unsigned MFpuNumKeysARM = NUM_KEYS(MFpuKeysARM);
154const unsigned MFpuMapSize = NextHighestPowerOf2(MFpuNumKeysARM);
155
156/// ConvertMFpu - Convert -mfpu value from the gcc to the llc dialect.
157std::string ConvertMFpu(const char* Val) {
158 static ArgMap MFpuMap(MFpuMapSize);
159 static bool StaticDataInitialized = false;
160
161 if (!StaticDataInitialized) {
162 FillInArgMap(MFpuMap, MFpuKeysARM, MFpuValuesARM, MFpuNumKeysARM);
163 StaticDataInitialized = true;
164 }
165
166 std::string ret = "-mattr=";
167 ArgMap::const_iterator I = MFpuMap.find(Val);
168 if (I != MFpuMap.end()) {
169 return ret + I->getValue();
170 }
171 return ret + '+' + Val;
172}
173
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +0000174/// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
175std::string ConvertToMAttr(const StrVec& Opts) {
Mikhail Glushenkov36604a62010-12-15 01:22:29 +0000176 std::string out("-mattr=");
177 bool firstIter = true;
178
179 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
180 const std::string& Arg = *B;
181
182 if (firstIter)
183 firstIter = false;
184 else
185 out += ",";
186
187 AddPlusOrMinus(Arg, out);
188 }
189
190 return out;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +0000191}
192
Mikhail Glushenkov84612022009-12-07 17:03:21 +0000193}