1 #include "llvm/ADT/StringMap.h"
8 /// NUM_KEYS - Calculate the size of a const char* array.
9 #define NUM_KEYS(Keys) sizeof(Keys) / sizeof(const char*)
11 // See http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
12 inline unsigned NextHighestPowerOf2 (unsigned i) {
22 typedef std::vector<std::string> StrVec;
23 typedef llvm::StringMap<const char*> ArgMap;
26 /// AddPlusOrMinus - Convert 'no-foo' to '-foo' and 'foo' to '+foo'.
27 void AddPlusOrMinus (const std::string& Arg, std::string& out) {
28 if (Arg.find("no-") == 0 && Arg[3] != 0) {
30 out += Arg.c_str() + 3;
38 // -march values that need to be special-cased.
39 const char* MArchKeysARM[] = { "armv4t", "armv5t", "armv5te", "armv6",
40 "armv6-m", "armv6t2", "armv7-a", "armv7-m" };
41 const char* MArchValuesARM[] = { "v4t", "v5t", "v5te", "v6", "v6m", "v6t2",
43 const unsigned MArchNumKeysARM = NUM_KEYS(MArchKeysARM);
44 const unsigned MArchMapSize = NextHighestPowerOf2(MArchNumKeysARM);
46 // -march values that should be forwarded as -mcpu
47 const char* MArchMCpuKeysARM[] = { "iwmmxt", "ep9312" };
48 const char* MArchMCpuValuesARM[] = { "iwmmxt", "ep9312"};
49 const unsigned MArchMCpuNumKeysARM = NUM_KEYS(MArchMCpuKeysARM);
50 const unsigned MArchMCpuMapSize = NextHighestPowerOf2(MArchMCpuNumKeysARM);
53 void FillInArgMap(ArgMap& Args, const char* Keys[],
54 const char* Values[], unsigned NumKeys)
56 for (unsigned i = 0; i < NumKeys; ++i) {
57 // Explicit cast to StringRef here is necessary to pick up the right
59 Args.GetOrCreateValue(llvm::StringRef(Keys[i]), Values[i]);
63 /// ConvertMArchToMAttr - Convert -march from the gcc dialect to
64 /// something llc can understand.
65 std::string ConvertMArchToMAttr(const StrVec& Opts) {
66 static ArgMap MArchMap(MArchMapSize);
67 static ArgMap MArchMCpuMap(MArchMapSize);
68 static bool StaticDataInitialized = false;
70 if (!StaticDataInitialized) {
71 FillInArgMap(MArchMap, MArchKeysARM, MArchValuesARM, MArchNumKeysARM);
72 FillInArgMap(MArchMCpuMap, MArchMCpuKeysARM,
73 MArchMCpuValuesARM, MArchMCpuNumKeysARM);
74 StaticDataInitialized = true;
77 std::string mattr("-mattr=");
78 std::string mcpu("-mcpu=");
79 bool mattrTouched = false;
80 bool mcpuTouched = false;
81 bool firstIter = true;
83 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
84 const std::string& Arg = *B;
91 // Check if the argument is a special case.
93 ArgMap::const_iterator I = MArchMap.find(Arg);
95 if (I != MArchMap.end()) {
97 mattr += I->getValue();
102 // Check if the argument should be forwarded to -mcpu instead of -mattr.
104 ArgMap::const_iterator I = MArchMCpuMap.find(Arg);
106 if (I != MArchMCpuMap.end()) {
108 mcpu += I->getValue();
113 AddPlusOrMinus(Arg, mattr);
120 out += (mattrTouched ? " " : "") + mcpu;
125 // -mcpu values that need to be special-cased.
126 const char* MCpuKeysPPC[] = { "G3", "G4", "G5", "powerpc", "powerpc64"};
127 const char* MCpuValuesPPC[] = { "g3", "g4", "g5", "ppc", "ppc64"};
128 const unsigned MCpuNumKeysPPC = NUM_KEYS(MCpuKeysPPC);
129 const unsigned MCpuMapSize = NextHighestPowerOf2(MCpuNumKeysPPC);
131 /// ConvertMCpu - Convert -mcpu value from the gcc to the llc dialect.
132 std::string ConvertMCpu(const char* Val) {
133 static ArgMap MCpuMap(MCpuMapSize);
134 static bool StaticDataInitialized = false;
136 if (!StaticDataInitialized) {
137 FillInArgMap(MCpuMap, MCpuKeysPPC, MCpuValuesPPC, MCpuNumKeysPPC);
138 StaticDataInitialized = true;
141 std::string ret = "-mcpu=";
142 ArgMap::const_iterator I = MCpuMap.find(Val);
143 if (I != MCpuMap.end()) {
144 return ret + I->getValue();
149 // -mfpu values that need to be special-cased.
150 const char* MFpuKeysARM[] = { "vfp", "vfpv3",
151 "vfpv3-fp16", "vfpv3-d16", "vfpv3-d16-fp16",
152 "neon", "neon-fp16" };
153 const char* MFpuValuesARM[] = { "vfp2", "vfp3",
154 "+vfp3,+fp16", "+vfp3,+d16", "+vfp3,+d16,+fp16",
155 "+neon", "+neon,+neonfp" };
156 const unsigned MFpuNumKeysARM = NUM_KEYS(MFpuKeysARM);
157 const unsigned MFpuMapSize = NextHighestPowerOf2(MFpuNumKeysARM);
159 /// ConvertMFpu - Convert -mfpu value from the gcc to the llc dialect.
160 std::string ConvertMFpu(const char* Val) {
161 static ArgMap MFpuMap(MFpuMapSize);
162 static bool StaticDataInitialized = false;
164 if (!StaticDataInitialized) {
165 FillInArgMap(MFpuMap, MFpuKeysARM, MFpuValuesARM, MFpuNumKeysARM);
166 StaticDataInitialized = true;
169 std::string ret = "-mattr=";
170 ArgMap::const_iterator I = MFpuMap.find(Val);
171 if (I != MFpuMap.end()) {
172 return ret + I->getValue();
174 return ret + '+' + Val;
177 /// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
178 std::string ConvertToMAttr(const StrVec& Opts) {
179 std::string out("-mattr=");
180 bool firstIter = true;
182 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
183 const std::string& Arg = *B;
190 AddPlusOrMinus(Arg, out);