blob: 805a342dfb74ecd4c5c33ef34c7437eda35de5b4 [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- subzero/src/IceGlobalContext.cpp - Global context defs ---*- C++ -*-===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines aspects of the compilation that persist across
11// multiple functions.
12//
13//===----------------------------------------------------------------------===//
14
Jim Stichnoth217dc082014-07-11 14:06:55 -070015#include <ctype.h> // isdigit(), isupper()
Jan Voung839c4ce2014-07-28 15:19:43 -070016#include <locale> // locale
Jim Stichnothd97c7df2014-06-04 11:57:08 -070017
Jim Stichnothf7c9a142014-04-29 10:52:43 -070018#include "IceDefs.h"
19#include "IceTypes.h"
20#include "IceCfg.h"
Jim Stichnoth989a7032014-08-08 10:13:44 -070021#include "IceClFlags.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070022#include "IceGlobalContext.h"
23#include "IceOperand.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070024#include "IceTargetLowering.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070025
26namespace Ice {
27
28// TypePool maps constants of type KeyType (e.g. float) to pointers to
29// type ValueType (e.g. ConstantFloat). KeyType values are compared
30// using memcmp() because of potential NaN values in KeyType values.
31// KeyTypeHasFP indicates whether KeyType is a floating-point type
32// whose values need to be compared using memcmp() for NaN
33// correctness. TODO: use std::is_floating_point<KeyType> instead of
34// KeyTypeHasFP with C++11.
35template <typename KeyType, typename ValueType, bool KeyTypeHasFP = false>
36class TypePool {
37 TypePool(const TypePool &) LLVM_DELETED_FUNCTION;
38 TypePool &operator=(const TypePool &) LLVM_DELETED_FUNCTION;
39
40public:
Jim Stichnothf61d5b22014-05-23 13:31:24 -070041 TypePool() : NextPoolID(0) {}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070042 ValueType *getOrAdd(GlobalContext *Ctx, Type Ty, KeyType Key) {
43 TupleType TupleKey = std::make_pair(Ty, Key);
44 typename ContainerType::const_iterator Iter = Pool.find(TupleKey);
45 if (Iter != Pool.end())
46 return Iter->second;
Jim Stichnothf61d5b22014-05-23 13:31:24 -070047 ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++);
Jim Stichnothf7c9a142014-04-29 10:52:43 -070048 Pool[TupleKey] = Result;
49 return Result;
50 }
Jim Stichnothf61d5b22014-05-23 13:31:24 -070051 ConstantList getConstantPool() const {
52 ConstantList Constants;
53 Constants.reserve(Pool.size());
54 // TODO: replace the loop with std::transform + lambdas.
55 for (typename ContainerType::const_iterator I = Pool.begin(),
56 E = Pool.end();
57 I != E; ++I) {
58 Constants.push_back(I->second);
59 }
60 return Constants;
61 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070062
63private:
64 typedef std::pair<Type, KeyType> TupleType;
65 struct TupleCompare {
Jan Voung839c4ce2014-07-28 15:19:43 -070066 bool operator()(const TupleType &A, const TupleType &B) const {
Jim Stichnothf7c9a142014-04-29 10:52:43 -070067 if (A.first != B.first)
68 return A.first < B.first;
69 if (KeyTypeHasFP)
70 return memcmp(&A.second, &B.second, sizeof(KeyType)) < 0;
71 return A.second < B.second;
72 }
73 };
74 typedef std::map<const TupleType, ValueType *, TupleCompare> ContainerType;
75 ContainerType Pool;
Jim Stichnothf61d5b22014-05-23 13:31:24 -070076 uint32_t NextPoolID;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070077};
78
Matt Walad8f4a7d2014-06-18 09:55:03 -070079// UndefPool maps ICE types to the corresponding ConstantUndef values.
80class UndefPool {
81 UndefPool(const UndefPool &) LLVM_DELETED_FUNCTION;
82 UndefPool &operator=(const UndefPool &) LLVM_DELETED_FUNCTION;
83
84public:
85 UndefPool() : NextPoolID(0) {}
86
87 ConstantUndef *getOrAdd(GlobalContext *Ctx, Type Ty) {
88 ContainerType::iterator I = Pool.find(Ty);
89 if (I != Pool.end())
90 return I->second;
91 ConstantUndef *Undef = ConstantUndef::create(Ctx, Ty, NextPoolID++);
92 Pool[Ty] = Undef;
93 return Undef;
94 }
95
96private:
97 uint32_t NextPoolID;
98 typedef std::map<Type, ConstantUndef *> ContainerType;
99 ContainerType Pool;
100};
101
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700102// The global constant pool bundles individual pools of each type of
103// interest.
104class ConstantPool {
105 ConstantPool(const ConstantPool &) LLVM_DELETED_FUNCTION;
106 ConstantPool &operator=(const ConstantPool &) LLVM_DELETED_FUNCTION;
107
108public:
109 ConstantPool() {}
110 TypePool<float, ConstantFloat, true> Floats;
111 TypePool<double, ConstantDouble, true> Doubles;
Jan Voungbc004632014-09-16 15:09:10 -0700112 TypePool<uint32_t, ConstantInteger32> Integers32;
113 TypePool<uint64_t, ConstantInteger64> Integers64;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700114 TypePool<RelocatableTuple, ConstantRelocatable> Relocatables;
Matt Walad8f4a7d2014-06-18 09:55:03 -0700115 UndefPool Undefs;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700116};
117
118GlobalContext::GlobalContext(llvm::raw_ostream *OsDump,
119 llvm::raw_ostream *OsEmit, VerboseMask Mask,
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700120 TargetArch Arch, OptLevel Opt,
Jim Stichnoth989a7032014-08-08 10:13:44 -0700121 IceString TestPrefix, const ClFlags &Flags)
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700122 : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask),
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700123 ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt),
Matt Wala1bd2fce2014-08-08 14:02:09 -0700124 TestPrefix(TestPrefix), Flags(Flags), HasEmittedFirstMethod(false),
125 RNG("") {}
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700126
Jim Stichnoth217dc082014-07-11 14:06:55 -0700127// Scan a string for S[0-9A-Z]*_ patterns and replace them with
128// S<num>_ where <num> is the next base-36 value. If a type name
129// legitimately contains that pattern, then the substitution will be
130// made in error and most likely the link will fail. In this case,
131// the test classes can be rewritten not to use that pattern, which is
132// much simpler and more reliable than implementing a full demangling
133// parser. Another substitution-in-error may occur if a type
134// identifier ends with the pattern S[0-9A-Z]*, because an immediately
135// following substitution string like "S1_" or "PS1_" may be combined
136// with the previous type.
137void GlobalContext::incrementSubstitutions(ManglerVector &OldName) const {
138 const std::locale CLocale("C");
139 // Provide extra space in case the length of <num> increases.
140 ManglerVector NewName(OldName.size() * 2);
141 size_t OldPos = 0;
142 size_t NewPos = 0;
143 size_t OldLen = OldName.size();
144 for (; OldPos < OldLen; ++OldPos, ++NewPos) {
145 if (OldName[OldPos] == '\0')
146 break;
147 if (OldName[OldPos] == 'S') {
148 // Search forward until we find _ or invalid character (including \0).
149 bool AllZs = true;
150 bool Found = false;
151 size_t Last;
152 for (Last = OldPos + 1; Last < OldLen; ++Last) {
153 char Ch = OldName[Last];
154 if (Ch == '_') {
155 Found = true;
156 break;
157 } else if (std::isdigit(Ch) || std::isupper(Ch, CLocale)) {
158 if (Ch != 'Z')
159 AllZs = false;
160 } else {
161 // Invalid character, stop searching.
162 break;
163 }
164 }
165 if (Found) {
166 NewName[NewPos++] = OldName[OldPos++]; // 'S'
167 size_t Length = Last - OldPos;
168 // NewPos and OldPos point just past the 'S'.
169 assert(NewName[NewPos - 1] == 'S');
170 assert(OldName[OldPos - 1] == 'S');
171 assert(OldName[OldPos + Length] == '_');
172 if (AllZs) {
Jim Stichnoth78b4c0b2014-07-11 15:29:23 -0700173 // Replace N 'Z' characters with a '0' (if N=0) or '1' (if
174 // N>0) followed by N '0' characters.
175 NewName[NewPos++] = (Length ? '1' : '0');
176 for (size_t i = 0; i < Length; ++i) {
Jim Stichnoth217dc082014-07-11 14:06:55 -0700177 NewName[NewPos++] = '0';
178 }
179 } else {
180 // Iterate right-to-left and increment the base-36 number.
181 bool Carry = true;
182 for (size_t i = 0; i < Length; ++i) {
183 size_t Offset = Length - 1 - i;
184 char Ch = OldName[OldPos + Offset];
185 if (Carry) {
186 Carry = false;
187 switch (Ch) {
188 case '9':
189 Ch = 'A';
190 break;
191 case 'Z':
192 Ch = '0';
193 Carry = true;
194 break;
195 default:
196 ++Ch;
197 break;
198 }
199 }
200 NewName[NewPos + Offset] = Ch;
201 }
202 NewPos += Length;
203 }
204 OldPos = Last;
205 // Fall through and let the '_' be copied across.
206 }
207 }
208 NewName[NewPos] = OldName[OldPos];
209 }
210 assert(NewName[NewPos] == '\0');
211 OldName = NewName;
212}
213
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700214// In this context, name mangling means to rewrite a symbol using a
215// given prefix. For a C++ symbol, nest the original symbol inside
216// the "prefix" namespace. For other symbols, just prepend the
217// prefix.
218IceString GlobalContext::mangleName(const IceString &Name) const {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700219 // An already-nested name like foo::bar() gets pushed down one
220 // level, making it equivalent to Prefix::foo::bar().
221 // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
222 // A non-nested but mangled name like bar() gets nested, making it
223 // equivalent to Prefix::bar().
224 // _Z3barxyz ==> ZN6Prefix3barExyz
225 // An unmangled, extern "C" style name, gets a simple prefix:
226 // bar ==> Prefixbar
227 if (getTestPrefix().empty())
228 return Name;
229
230 unsigned PrefixLength = getTestPrefix().length();
Jim Stichnoth217dc082014-07-11 14:06:55 -0700231 ManglerVector NameBase(1 + Name.length());
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700232 const size_t BufLen = 30 + Name.length() + PrefixLength;
Jim Stichnoth217dc082014-07-11 14:06:55 -0700233 ManglerVector NewName(BufLen);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700234 uint32_t BaseLength = 0; // using uint32_t due to sscanf format string
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700235
Derek Schuff44712d12014-06-17 14:34:34 -0700236 int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase.data());
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700237 if (ItemsParsed == 1) {
238 // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
239 // (splice in "6Prefix") ^^^^^^^
Derek Schuff44712d12014-06-17 14:34:34 -0700240 snprintf(NewName.data(), BufLen, "_ZN%u%s%s", PrefixLength,
241 getTestPrefix().c_str(), NameBase.data());
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700242 // We ignore the snprintf return value (here and below). If we
243 // somehow miscalculated the output buffer length, the output will
244 // be truncated, but it will be truncated consistently for all
245 // mangleName() calls on the same input string.
Jim Stichnoth217dc082014-07-11 14:06:55 -0700246 incrementSubstitutions(NewName);
Derek Schuff44712d12014-06-17 14:34:34 -0700247 return NewName.data();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700248 }
249
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700250 // Artificially limit BaseLength to 9 digits (less than 1 billion)
251 // because sscanf behavior is undefined on integer overflow. If
252 // there are more than 9 digits (which we test by looking at the
253 // beginning of NameBase), then we consider this a failure to parse
254 // a namespace mangling, and fall back to the simple prefixing.
Derek Schuff44712d12014-06-17 14:34:34 -0700255 ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase.data());
256 if (ItemsParsed == 2 && BaseLength <= strlen(NameBase.data()) &&
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700257 !isdigit(NameBase[0])) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700258 // Transform _Z3barxyz ==> _ZN6Prefix3barExyz
259 // ^^^^^^^^ ^
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700260 // (splice in "N6Prefix", and insert "E" after "3bar")
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700261 // But an "I" after the identifier indicates a template argument
262 // list terminated with "E"; insert the new "E" before/after the
263 // old "E". E.g.:
264 // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz
265 // ^^^^^^^^ ^
266 // (splice in "N6Prefix", and insert "E" after "3barIabcE")
Jim Stichnoth217dc082014-07-11 14:06:55 -0700267 ManglerVector OrigName(Name.length());
268 ManglerVector OrigSuffix(Name.length());
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700269 uint32_t ActualBaseLength = BaseLength;
270 if (NameBase[ActualBaseLength] == 'I') {
271 ++ActualBaseLength;
272 while (NameBase[ActualBaseLength] != 'E' &&
273 NameBase[ActualBaseLength] != '\0')
274 ++ActualBaseLength;
275 }
Derek Schuff44712d12014-06-17 14:34:34 -0700276 strncpy(OrigName.data(), NameBase.data(), ActualBaseLength);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700277 OrigName[ActualBaseLength] = '\0';
Derek Schuff44712d12014-06-17 14:34:34 -0700278 strcpy(OrigSuffix.data(), NameBase.data() + ActualBaseLength);
279 snprintf(NewName.data(), BufLen, "_ZN%u%s%u%sE%s", PrefixLength,
280 getTestPrefix().c_str(), BaseLength, OrigName.data(),
281 OrigSuffix.data());
Jim Stichnoth217dc082014-07-11 14:06:55 -0700282 incrementSubstitutions(NewName);
Derek Schuff44712d12014-06-17 14:34:34 -0700283 return NewName.data();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700284 }
285
286 // Transform bar ==> Prefixbar
287 // ^^^^^^
288 return getTestPrefix() + Name;
289}
290
291GlobalContext::~GlobalContext() {}
292
Jan Voungbc004632014-09-16 15:09:10 -0700293Constant *GlobalContext::getConstantInt64(Type Ty, uint64_t ConstantInt64) {
294 assert(Ty == IceType_i64);
295 return ConstPool->Integers64.getOrAdd(this, Ty, ConstantInt64);
296}
297
298Constant *GlobalContext::getConstantInt32(Type Ty, uint32_t ConstantInt32) {
Jim Stichnoth3ef786f2014-09-08 11:19:21 -0700299 if (Ty == IceType_i1)
Jan Voungbc004632014-09-16 15:09:10 -0700300 ConstantInt32 &= UINT32_C(1);
301 return ConstPool->Integers32.getOrAdd(this, Ty, ConstantInt32);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700302}
303
304Constant *GlobalContext::getConstantFloat(float ConstantFloat) {
305 return ConstPool->Floats.getOrAdd(this, IceType_f32, ConstantFloat);
306}
307
308Constant *GlobalContext::getConstantDouble(double ConstantDouble) {
309 return ConstPool->Doubles.getOrAdd(this, IceType_f64, ConstantDouble);
310}
311
312Constant *GlobalContext::getConstantSym(Type Ty, int64_t Offset,
313 const IceString &Name,
314 bool SuppressMangling) {
315 return ConstPool->Relocatables.getOrAdd(
316 this, Ty, RelocatableTuple(Offset, Name, SuppressMangling));
317}
318
Matt Walad8f4a7d2014-06-18 09:55:03 -0700319Constant *GlobalContext::getConstantUndef(Type Ty) {
320 return ConstPool->Undefs.getOrAdd(this, Ty);
321}
322
323Constant *GlobalContext::getConstantZero(Type Ty) {
324 switch (Ty) {
325 case IceType_i1:
326 case IceType_i8:
327 case IceType_i16:
328 case IceType_i32:
Jan Voungbc004632014-09-16 15:09:10 -0700329 return getConstantInt32(Ty, 0);
Matt Walad8f4a7d2014-06-18 09:55:03 -0700330 case IceType_i64:
Jan Voungbc004632014-09-16 15:09:10 -0700331 return getConstantInt64(Ty, 0);
Matt Walad8f4a7d2014-06-18 09:55:03 -0700332 case IceType_f32:
333 return getConstantFloat(0);
334 case IceType_f64:
335 return getConstantDouble(0);
Matt Wala928f1292014-07-07 16:50:46 -0700336 case IceType_v4i1:
337 case IceType_v8i1:
338 case IceType_v16i1:
339 case IceType_v16i8:
340 case IceType_v8i16:
341 case IceType_v4i32:
342 case IceType_v4f32: {
343 IceString Str;
344 llvm::raw_string_ostream BaseOS(Str);
Jim Stichnoth78282f62014-07-27 23:14:00 -0700345 BaseOS << "Unsupported constant type: " << Ty;
Matt Wala928f1292014-07-07 16:50:46 -0700346 llvm_unreachable(BaseOS.str().c_str());
347 } break;
Matt Walad8f4a7d2014-06-18 09:55:03 -0700348 case IceType_void:
349 case IceType_NUM:
350 break;
351 }
352 llvm_unreachable("Unknown type");
353}
354
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700355ConstantList GlobalContext::getConstantPool(Type Ty) const {
356 switch (Ty) {
357 case IceType_i1:
358 case IceType_i8:
359 case IceType_i16:
360 case IceType_i32:
Jan Voungbc004632014-09-16 15:09:10 -0700361 return ConstPool->Integers32.getConstantPool();
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700362 case IceType_i64:
Jan Voungbc004632014-09-16 15:09:10 -0700363 return ConstPool->Integers64.getConstantPool();
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700364 case IceType_f32:
365 return ConstPool->Floats.getConstantPool();
366 case IceType_f64:
367 return ConstPool->Doubles.getConstantPool();
Matt Wala928f1292014-07-07 16:50:46 -0700368 case IceType_v4i1:
369 case IceType_v8i1:
370 case IceType_v16i1:
371 case IceType_v16i8:
372 case IceType_v8i16:
373 case IceType_v4i32:
374 case IceType_v4f32: {
375 IceString Str;
376 llvm::raw_string_ostream BaseOS(Str);
Jim Stichnoth78282f62014-07-27 23:14:00 -0700377 BaseOS << "Unsupported constant type: " << Ty;
Matt Wala928f1292014-07-07 16:50:46 -0700378 llvm_unreachable(BaseOS.str().c_str());
379 } break;
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700380 case IceType_void:
381 case IceType_NUM:
382 break;
383 }
384 llvm_unreachable("Unknown type");
385}
386
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700387void Timer::printElapsedUs(GlobalContext *Ctx, const IceString &Tag) const {
388 if (Ctx->isVerbose(IceV_Timing)) {
389 // Prefixing with '#' allows timing strings to be included
390 // without error in textual assembly output.
391 Ctx->getStrDump() << "# " << getElapsedUs() << " usec " << Tag << "\n";
392 }
393}
394
395} // end of namespace Ice