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) {
23 typedef std::vector<std::string> StrVec;
24 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);
52 void FillInArgMap(ArgMap& Args, const char* Keys[],
53 const char* Values[], unsigned NumKeys)
55 for (unsigned i = 0; i < NumKeys; ++i) {
56 // Explicit cast to StringRef here is necessary to pick up the right
58 Args.GetOrCreateValue(llvm::StringRef(Keys[i]), Values[i]);
62 /// ConvertMArchToMAttr - Convert -march from the gcc dialect to
63 /// something llc can understand.
64 std::string ConvertMArchToMAttr(const StrVec& Opts) {
65 static ArgMap MArchMap(MArchMapSize);
66 static ArgMap MArchMCpuMap(MArchMapSize);
67 static bool StaticDataInitialized = false;
69 if (!StaticDataInitialized) {
70 FillInArgMap(MArchMap, MArchKeysARM, MArchValuesARM, MArchNumKeysARM);
71 FillInArgMap(MArchMCpuMap, MArchMCpuKeysARM,
72 MArchMCpuValuesARM, MArchMCpuNumKeysARM);
73 StaticDataInitialized = true;
76 std::string mattr("-mattr=");
77 std::string mcpu("-mcpu=");
78 bool mattrTouched = false;
79 bool mcpuTouched = false;
81 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
82 const std::string& Arg = *B;
84 // Check if the argument should be forwarded to -mcpu instead of -mattr.
86 ArgMap::const_iterator I = MArchMCpuMap.find(Arg);
88 if (I != MArchMCpuMap.end()) {
90 mcpu += I->getValue();
98 // Check if the argument is a special case.
100 ArgMap::const_iterator I = MArchMap.find(Arg);
102 if (I != MArchMap.end()) {
105 mattr += I->getValue();
110 AddPlusOrMinus(Arg, mattr);
117 out += (mattrTouched ? " " : "") + mcpu;
122 // -mcpu values that need to be special-cased.
123 const char* MCpuKeysPPC[] = { "G3", "G4", "G5", "powerpc", "powerpc64"};
124 const char* MCpuValuesPPC[] = { "g3", "g4", "g5", "ppc", "ppc64"};
125 const unsigned MCpuNumKeysPPC = NUM_KEYS(MCpuKeysPPC);
126 const unsigned MCpuMapSize = NextHighestPowerOf2(MCpuNumKeysPPC);
128 /// ConvertMCpu - Convert -mcpu value from the gcc to the llc dialect.
129 std::string ConvertMCpu(const char* Val) {
130 static ArgMap MCpuMap(MCpuMapSize);
131 static bool StaticDataInitialized = false;
133 if (!StaticDataInitialized) {
134 FillInArgMap(MCpuMap, MCpuKeysPPC, MCpuValuesPPC, MCpuNumKeysPPC);
135 StaticDataInitialized = true;
138 std::string ret = "-mcpu=";
139 ArgMap::const_iterator I = MCpuMap.find(Val);
140 if (I != MCpuMap.end()) {
141 return ret + I->getValue();
146 // -mfpu values that need to be special-cased.
147 const char* MFpuKeysARM[] = { "vfp", "vfpv3",
148 "vfpv3-fp16", "vfpv3-d16", "vfpv3-d16-fp16",
149 "neon", "neon-fp16" };
150 const char* MFpuValuesARM[] = { "vfp2", "vfp3",
151 "+vfp3,+fp16", "+vfp3,+d16", "+vfp3,+d16,+fp16",
152 "+neon", "+neon,+neonfp" };
153 const unsigned MFpuNumKeysARM = NUM_KEYS(MFpuKeysARM);
154 const unsigned MFpuMapSize = NextHighestPowerOf2(MFpuNumKeysARM);
156 /// ConvertMFpu - Convert -mfpu value from the gcc to the llc dialect.
157 std::string ConvertMFpu(const char* Val) {
158 static ArgMap MFpuMap(MFpuMapSize);
159 static bool StaticDataInitialized = false;
161 if (!StaticDataInitialized) {
162 FillInArgMap(MFpuMap, MFpuKeysARM, MFpuValuesARM, MFpuNumKeysARM);
163 StaticDataInitialized = true;
166 std::string ret = "-mattr=";
167 ArgMap::const_iterator I = MFpuMap.find(Val);
168 if (I != MFpuMap.end()) {
169 return ret + I->getValue();
171 return ret + '+' + Val;
174 /// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
175 std::string ConvertToMAttr(const StrVec& Opts) {
176 std::string out("-mattr=");
177 bool firstIter = true;
179 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
180 const std::string& Arg = *B;
187 AddPlusOrMinus(Arg, out);