blob: 5aa250e512b9ed557b2d1cbebb88ee27f33273ad [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);
50const unsigned MArchMCpuMapSize = NextHighestPowerOf2(MArchMCpuNumKeysARM);
51
52
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000053void FillInArgMap(ArgMap& Args, const char* Keys[],
54 const char* Values[], unsigned NumKeys)
55{
56 for (unsigned i = 0; i < NumKeys; ++i) {
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000057 // Explicit cast to StringRef here is necessary to pick up the right
58 // overload.
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000059 Args.GetOrCreateValue(llvm::StringRef(Keys[i]), Values[i]);
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000060 }
61}
62
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000063/// ConvertMArchToMAttr - Convert -march from the gcc dialect to
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000064/// something llc can understand.
65std::string ConvertMArchToMAttr(const StrVec& Opts) {
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000066 static ArgMap MArchMap(MArchMapSize);
Mikhail Glushenkov42db9972010-12-15 01:22:25 +000067 static ArgMap MArchMCpuMap(MArchMapSize);
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000068 static bool StaticDataInitialized = false;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000069
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000070 if (!StaticDataInitialized) {
71 FillInArgMap(MArchMap, MArchKeysARM, MArchValuesARM, MArchNumKeysARM);
Mikhail Glushenkov42db9972010-12-15 01:22:25 +000072 FillInArgMap(MArchMCpuMap, MArchMCpuKeysARM,
73 MArchMCpuValuesARM, MArchMCpuNumKeysARM);
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000074 StaticDataInitialized = true;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000075 }
76
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000077 std::string mattr("-mattr=");
78 std::string mcpu("-mcpu=");
79 bool mattrTouched = false;
80 bool mcpuTouched = false;
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000081
82 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
83 const std::string& Arg = *B;
84
Mikhail Glushenkov36604a62010-12-15 01:22:29 +000085 // Check if the argument should be forwarded to -mcpu instead of -mattr.
86 {
87 ArgMap::const_iterator I = MArchMCpuMap.find(Arg);
88
89 if (I != MArchMCpuMap.end()) {
90 mcpuTouched = true;
91 mcpu += I->getValue();
92 continue;
93 }
94 }
95
Mikhail Glushenkov24d7fa22010-12-15 01:22:34 +000096 if (mattrTouched)
97 mattr += ",";
98
99 // Check if the argument is a special case.
100 {
101 ArgMap::const_iterator I = MArchMap.find(Arg);
102
103 if (I != MArchMap.end()) {
104 mattrTouched = true;
105 mattr += '+';
106 mattr += I->getValue();
107 continue;
108 }
109 }
110
Mikhail Glushenkov36604a62010-12-15 01:22:29 +0000111 AddPlusOrMinus(Arg, mattr);
112 }
113
114 std::string out;
115 if (mattrTouched)
116 out += mattr;
117 if (mcpuTouched)
118 out += (mattrTouched ? " " : "") + mcpu;
119
120 return out;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +0000121}
122
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +0000123// -mcpu values that need to be special-cased.
124const char* MCpuKeysPPC[] = { "G3", "G4", "G5", "powerpc", "powerpc64"};
125const char* MCpuValuesPPC[] = { "g3", "g4", "g5", "ppc", "ppc64"};
126const unsigned MCpuNumKeysPPC = NUM_KEYS(MCpuKeysPPC);
127const unsigned MCpuMapSize = NextHighestPowerOf2(MCpuNumKeysPPC);
128
129/// ConvertMCpu - Convert -mcpu value from the gcc to the llc dialect.
130std::string ConvertMCpu(const char* Val) {
131 static ArgMap MCpuMap(MCpuMapSize);
132 static bool StaticDataInitialized = false;
133
134 if (!StaticDataInitialized) {
135 FillInArgMap(MCpuMap, MCpuKeysPPC, MCpuValuesPPC, MCpuNumKeysPPC);
136 StaticDataInitialized = true;
137 }
138
139 std::string ret = "-mcpu=";
140 ArgMap::const_iterator I = MCpuMap.find(Val);
141 if (I != MCpuMap.end()) {
142 return ret + I->getValue();
143 }
144 return ret + Val;
145}
146
147// -mfpu values that need to be special-cased.
148const char* MFpuKeysARM[] = { "vfp", "vfpv3",
149 "vfpv3-fp16", "vfpv3-d16", "vfpv3-d16-fp16",
150 "neon", "neon-fp16" };
151const char* MFpuValuesARM[] = { "vfp2", "vfp3",
152 "+vfp3,+fp16", "+vfp3,+d16", "+vfp3,+d16,+fp16",
153 "+neon", "+neon,+neonfp" };
154const unsigned MFpuNumKeysARM = NUM_KEYS(MFpuKeysARM);
155const unsigned MFpuMapSize = NextHighestPowerOf2(MFpuNumKeysARM);
156
157/// ConvertMFpu - Convert -mfpu value from the gcc to the llc dialect.
158std::string ConvertMFpu(const char* Val) {
159 static ArgMap MFpuMap(MFpuMapSize);
160 static bool StaticDataInitialized = false;
161
162 if (!StaticDataInitialized) {
163 FillInArgMap(MFpuMap, MFpuKeysARM, MFpuValuesARM, MFpuNumKeysARM);
164 StaticDataInitialized = true;
165 }
166
167 std::string ret = "-mattr=";
168 ArgMap::const_iterator I = MFpuMap.find(Val);
169 if (I != MFpuMap.end()) {
170 return ret + I->getValue();
171 }
172 return ret + '+' + Val;
173}
174
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +0000175/// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
176std::string ConvertToMAttr(const StrVec& Opts) {
Mikhail Glushenkov36604a62010-12-15 01:22:29 +0000177 std::string out("-mattr=");
178 bool firstIter = true;
179
180 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
181 const std::string& Arg = *B;
182
183 if (firstIter)
184 firstIter = false;
185 else
186 out += ",";
187
188 AddPlusOrMinus(Arg, out);
189 }
190
191 return out;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +0000192}
193
Mikhail Glushenkov84612022009-12-07 17:03:21 +0000194}