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;
25 /// ConvertToMAttrImpl - Common implementation of ConvertMArchToMAttr and
26 /// ConvertToMAttr. The optional Args parameter contains information about how
27 /// to transform special-cased values (for example, '-march=armv6' must be
28 /// forwarded as '-mattr=+v6').
29 std::string ConvertToMAttrImpl(const StrVec& Opts, const ArgMap* Args = 0) {
30 std::string out("-mattr=");
31 bool firstIter = true;
33 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
34 const std::string& Arg = *B;
41 // Check if the argument is a special case.
43 ArgMap::const_iterator I = Args->find(Arg);
45 if (I != Args->end()) {
52 // Convert 'no-foo' to '-foo'.
53 if (Arg.find("no-") == 0 && Arg[3] != 0) {
55 out += Arg.c_str() + 3;
57 // Convert 'foo' to '+foo'.
67 // -march values that need to be special-cased.
68 const char* MArchKeysARM[] = { "armv4t", "armv5t", "armv5te", "armv6",
69 "armv6-m", "armv6t2", "armv7-a", "armv7-m" };
70 const char* MArchValuesARM[] = { "v4t", "v5t", "v5te", "v6", "v6m", "v6t2",
72 const unsigned MArchNumKeysARM = NUM_KEYS(MArchKeysARM);
73 const unsigned MArchMapSize = NextHighestPowerOf2(MArchNumKeysARM);
75 void FillInArgMap(ArgMap& Args, const char* Keys[],
76 const char* Values[], unsigned NumKeys)
78 for (unsigned i = 0; i < NumKeys; ++i) {
79 // Explicit cast to StringRef here is necessary to pick up the right
81 Args.GetOrCreateValue(llvm::StringRef(Keys[i]), Values[i]);
85 /// ConvertMArchToMAttr - Convert -march from the gcc dialect to
86 /// something llc can understand.
87 std::string ConvertMArchToMAttr(const StrVec& Opts) {
88 static ArgMap MArchMap(MArchMapSize);
89 static bool StaticDataInitialized = false;
91 if (!StaticDataInitialized) {
92 FillInArgMap(MArchMap, MArchKeysARM, MArchValuesARM, MArchNumKeysARM);
93 StaticDataInitialized = true;
96 return ConvertToMAttrImpl(Opts, &MArchMap);
99 // -mcpu values that need to be special-cased.
100 const char* MCpuKeysPPC[] = { "G3", "G4", "G5", "powerpc", "powerpc64"};
101 const char* MCpuValuesPPC[] = { "g3", "g4", "g5", "ppc", "ppc64"};
102 const unsigned MCpuNumKeysPPC = NUM_KEYS(MCpuKeysPPC);
103 const unsigned MCpuMapSize = NextHighestPowerOf2(MCpuNumKeysPPC);
105 /// ConvertMCpu - Convert -mcpu value from the gcc to the llc dialect.
106 std::string ConvertMCpu(const char* Val) {
107 static ArgMap MCpuMap(MCpuMapSize);
108 static bool StaticDataInitialized = false;
110 if (!StaticDataInitialized) {
111 FillInArgMap(MCpuMap, MCpuKeysPPC, MCpuValuesPPC, MCpuNumKeysPPC);
112 StaticDataInitialized = true;
115 std::string ret = "-mcpu=";
116 ArgMap::const_iterator I = MCpuMap.find(Val);
117 if (I != MCpuMap.end()) {
118 return ret + I->getValue();
123 // -mfpu values that need to be special-cased.
124 const char* MFpuKeysARM[] = { "vfp", "vfpv3",
125 "vfpv3-fp16", "vfpv3-d16", "vfpv3-d16-fp16",
126 "neon", "neon-fp16" };
127 const char* MFpuValuesARM[] = { "vfp2", "vfp3",
128 "+vfp3,+fp16", "+vfp3,+d16", "+vfp3,+d16,+fp16",
129 "+neon", "+neon,+neonfp" };
130 const unsigned MFpuNumKeysARM = NUM_KEYS(MFpuKeysARM);
131 const unsigned MFpuMapSize = NextHighestPowerOf2(MFpuNumKeysARM);
133 /// ConvertMFpu - Convert -mfpu value from the gcc to the llc dialect.
134 std::string ConvertMFpu(const char* Val) {
135 static ArgMap MFpuMap(MFpuMapSize);
136 static bool StaticDataInitialized = false;
138 if (!StaticDataInitialized) {
139 FillInArgMap(MFpuMap, MFpuKeysARM, MFpuValuesARM, MFpuNumKeysARM);
140 StaticDataInitialized = true;
143 std::string ret = "-mattr=";
144 ArgMap::const_iterator I = MFpuMap.find(Val);
145 if (I != MFpuMap.end()) {
146 return ret + I->getValue();
148 return ret + '+' + Val;
151 /// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
152 std::string ConvertToMAttr(const StrVec& Opts) {
153 return ConvertToMAttrImpl(Opts);