llvmc: Better -mfpu/-mcpu support for ARM & PPC.
[oota-llvm.git] / tools / llvmc / src / Hooks.cpp
1 #include "llvm/ADT/StringMap.h"
2
3 #include <string>
4 #include <vector>
5
6 namespace hooks {
7
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
12 inline unsigned NextHighestPowerOf2 (unsigned i) {
13   i |= i >> 1;
14   i |= i >> 2;
15   i |= i >> 4;
16   i |= i >> 8;
17   i |= i >> 16;
18   i++;
19   return i;
20 }
21
22 typedef std::vector<std::string> StrVec;
23 typedef llvm::StringMap<const char*> ArgMap;
24
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;
32
33   for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
34     const std::string& Arg = *B;
35
36     if (firstIter)
37       firstIter = false;
38     else
39       out += ",";
40
41     // Check if the argument is a special case.
42     if (Args != 0) {
43       ArgMap::const_iterator I = Args->find(Arg);
44
45       if (I != Args->end()) {
46         out += '+';
47         out += I->getValue();
48         continue;
49       }
50     }
51
52     // Convert 'no-foo' to '-foo'.
53     if (Arg.find("no-") == 0 && Arg[3] != 0) {
54       out += '-';
55       out += Arg.c_str() + 3;
56     }
57     // Convert 'foo' to '+foo'.
58     else {
59       out += '+';
60       out += Arg;
61     }
62   }
63
64   return out;
65 }
66
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",
71                                  "v7a", "v7m" };
72 const unsigned MArchNumKeysARM = NUM_KEYS(MArchKeysARM);
73 const unsigned MArchMapSize = NextHighestPowerOf2(MArchNumKeysARM);
74
75 void FillInArgMap(ArgMap& Args, const char* Keys[],
76                   const char* Values[], unsigned NumKeys)
77 {
78   for (unsigned i = 0; i < NumKeys; ++i) {
79     // Explicit cast to StringRef here is necessary to pick up the right
80     // overload.
81     Args.GetOrCreateValue(llvm::StringRef(Keys[i]), Values[i]);
82   }
83 }
84
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;
90
91   if (!StaticDataInitialized) {
92     FillInArgMap(MArchMap, MArchKeysARM, MArchValuesARM, MArchNumKeysARM);
93     StaticDataInitialized = true;
94   }
95
96   return ConvertToMAttrImpl(Opts, &MArchMap);
97 }
98
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);
104
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;
109
110   if (!StaticDataInitialized) {
111     FillInArgMap(MCpuMap, MCpuKeysPPC, MCpuValuesPPC, MCpuNumKeysPPC);
112     StaticDataInitialized = true;
113   }
114
115   std::string ret = "-mcpu=";
116   ArgMap::const_iterator I = MCpuMap.find(Val);
117   if (I != MCpuMap.end()) {
118     return ret + I->getValue();
119   }
120   return ret + Val;
121 }
122
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);
132
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;
137
138   if (!StaticDataInitialized) {
139     FillInArgMap(MFpuMap, MFpuKeysARM, MFpuValuesARM, MFpuNumKeysARM);
140     StaticDataInitialized = true;
141   }
142
143   std::string ret = "-mattr=";
144   ArgMap::const_iterator I = MFpuMap.find(Val);
145   if (I != MFpuMap.end()) {
146     return ret + I->getValue();
147   }
148   return ret + '+' + Val;
149 }
150
151 /// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
152 std::string ConvertToMAttr(const StrVec& Opts) {
153   return ConvertToMAttrImpl(Opts);
154 }
155
156 }