blob: 7ea7e5dd02d10eaa64310926fb54b3cc9d3d4efc [file] [log] [blame]
Jim Stichnothc4554d72014-09-30 16:49:38 -07001//===- subzero/src/IceGlobalContext.cpp - Global context defs -------------===//
Jim Stichnothf7c9a142014-04-29 10:52:43 -07002//
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 "IceCfg.h"
Jim Stichnoth989a7032014-08-08 10:13:44 -070019#include "IceClFlags.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070020#include "IceDefs.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070021#include "IceGlobalContext.h"
22#include "IceOperand.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070023#include "IceTargetLowering.h"
Jim Stichnothc4554d72014-09-30 16:49:38 -070024#include "IceTimerTree.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070025#include "IceTypes.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070026
27namespace Ice {
28
29// TypePool maps constants of type KeyType (e.g. float) to pointers to
30// type ValueType (e.g. ConstantFloat). KeyType values are compared
31// using memcmp() because of potential NaN values in KeyType values.
32// KeyTypeHasFP indicates whether KeyType is a floating-point type
33// whose values need to be compared using memcmp() for NaN
34// correctness. TODO: use std::is_floating_point<KeyType> instead of
35// KeyTypeHasFP with C++11.
36template <typename KeyType, typename ValueType, bool KeyTypeHasFP = false>
37class TypePool {
Jim Stichnoth0795ba02014-10-01 14:23:01 -070038 TypePool(const TypePool &) = delete;
39 TypePool &operator=(const TypePool &) = delete;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070040
41public:
Jim Stichnothf61d5b22014-05-23 13:31:24 -070042 TypePool() : NextPoolID(0) {}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070043 ValueType *getOrAdd(GlobalContext *Ctx, Type Ty, KeyType Key) {
44 TupleType TupleKey = std::make_pair(Ty, Key);
Jim Stichnothf44f3712014-10-01 14:05:51 -070045 auto Iter = Pool.find(TupleKey);
Jim Stichnothf7c9a142014-04-29 10:52:43 -070046 if (Iter != Pool.end())
47 return Iter->second;
Jim Stichnothf61d5b22014-05-23 13:31:24 -070048 ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++);
Jim Stichnothf7c9a142014-04-29 10:52:43 -070049 Pool[TupleKey] = Result;
50 return Result;
51 }
Jim Stichnothf61d5b22014-05-23 13:31:24 -070052 ConstantList getConstantPool() const {
53 ConstantList Constants;
54 Constants.reserve(Pool.size());
Jim Stichnothf44f3712014-10-01 14:05:51 -070055 for (auto &I : Pool)
56 Constants.push_back(I.second);
Jim Stichnothf61d5b22014-05-23 13:31:24 -070057 return Constants;
58 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070059
60private:
61 typedef std::pair<Type, KeyType> TupleType;
62 struct TupleCompare {
Jan Voung839c4ce2014-07-28 15:19:43 -070063 bool operator()(const TupleType &A, const TupleType &B) const {
Jim Stichnothf7c9a142014-04-29 10:52:43 -070064 if (A.first != B.first)
65 return A.first < B.first;
66 if (KeyTypeHasFP)
67 return memcmp(&A.second, &B.second, sizeof(KeyType)) < 0;
68 return A.second < B.second;
69 }
70 };
71 typedef std::map<const TupleType, ValueType *, TupleCompare> ContainerType;
72 ContainerType Pool;
Jim Stichnothf61d5b22014-05-23 13:31:24 -070073 uint32_t NextPoolID;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070074};
75
Matt Walad8f4a7d2014-06-18 09:55:03 -070076// UndefPool maps ICE types to the corresponding ConstantUndef values.
77class UndefPool {
Jim Stichnoth0795ba02014-10-01 14:23:01 -070078 UndefPool(const UndefPool &) = delete;
79 UndefPool &operator=(const UndefPool &) = delete;
Matt Walad8f4a7d2014-06-18 09:55:03 -070080
81public:
82 UndefPool() : NextPoolID(0) {}
83
84 ConstantUndef *getOrAdd(GlobalContext *Ctx, Type Ty) {
Jim Stichnothf44f3712014-10-01 14:05:51 -070085 auto I = Pool.find(Ty);
Matt Walad8f4a7d2014-06-18 09:55:03 -070086 if (I != Pool.end())
87 return I->second;
88 ConstantUndef *Undef = ConstantUndef::create(Ctx, Ty, NextPoolID++);
89 Pool[Ty] = Undef;
90 return Undef;
91 }
92
93private:
94 uint32_t NextPoolID;
95 typedef std::map<Type, ConstantUndef *> ContainerType;
96 ContainerType Pool;
97};
98
Jim Stichnothf7c9a142014-04-29 10:52:43 -070099// The global constant pool bundles individual pools of each type of
100// interest.
101class ConstantPool {
Jim Stichnoth0795ba02014-10-01 14:23:01 -0700102 ConstantPool(const ConstantPool &) = delete;
103 ConstantPool &operator=(const ConstantPool &) = delete;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700104
105public:
106 ConstantPool() {}
107 TypePool<float, ConstantFloat, true> Floats;
108 TypePool<double, ConstantDouble, true> Doubles;
Jan Voungbc004632014-09-16 15:09:10 -0700109 TypePool<uint32_t, ConstantInteger32> Integers32;
110 TypePool<uint64_t, ConstantInteger64> Integers64;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700111 TypePool<RelocatableTuple, ConstantRelocatable> Relocatables;
Matt Walad8f4a7d2014-06-18 09:55:03 -0700112 UndefPool Undefs;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700113};
114
115GlobalContext::GlobalContext(llvm::raw_ostream *OsDump,
116 llvm::raw_ostream *OsEmit, VerboseMask Mask,
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700117 TargetArch Arch, OptLevel Opt,
Jim Stichnoth989a7032014-08-08 10:13:44 -0700118 IceString TestPrefix, const ClFlags &Flags)
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700119 : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask),
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700120 ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt),
Matt Wala1bd2fce2014-08-08 14:02:09 -0700121 TestPrefix(TestPrefix), Flags(Flags), HasEmittedFirstMethod(false),
Jim Stichnothc4554d72014-09-30 16:49:38 -0700122 RNG(""), Timers(new TimerStack("main")) {}
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700123
Jim Stichnoth217dc082014-07-11 14:06:55 -0700124// Scan a string for S[0-9A-Z]*_ patterns and replace them with
125// S<num>_ where <num> is the next base-36 value. If a type name
126// legitimately contains that pattern, then the substitution will be
127// made in error and most likely the link will fail. In this case,
128// the test classes can be rewritten not to use that pattern, which is
129// much simpler and more reliable than implementing a full demangling
130// parser. Another substitution-in-error may occur if a type
131// identifier ends with the pattern S[0-9A-Z]*, because an immediately
132// following substitution string like "S1_" or "PS1_" may be combined
133// with the previous type.
134void GlobalContext::incrementSubstitutions(ManglerVector &OldName) const {
135 const std::locale CLocale("C");
136 // Provide extra space in case the length of <num> increases.
137 ManglerVector NewName(OldName.size() * 2);
138 size_t OldPos = 0;
139 size_t NewPos = 0;
140 size_t OldLen = OldName.size();
141 for (; OldPos < OldLen; ++OldPos, ++NewPos) {
142 if (OldName[OldPos] == '\0')
143 break;
144 if (OldName[OldPos] == 'S') {
145 // Search forward until we find _ or invalid character (including \0).
146 bool AllZs = true;
147 bool Found = false;
148 size_t Last;
149 for (Last = OldPos + 1; Last < OldLen; ++Last) {
150 char Ch = OldName[Last];
151 if (Ch == '_') {
152 Found = true;
153 break;
154 } else if (std::isdigit(Ch) || std::isupper(Ch, CLocale)) {
155 if (Ch != 'Z')
156 AllZs = false;
157 } else {
158 // Invalid character, stop searching.
159 break;
160 }
161 }
162 if (Found) {
163 NewName[NewPos++] = OldName[OldPos++]; // 'S'
164 size_t Length = Last - OldPos;
165 // NewPos and OldPos point just past the 'S'.
166 assert(NewName[NewPos - 1] == 'S');
167 assert(OldName[OldPos - 1] == 'S');
168 assert(OldName[OldPos + Length] == '_');
169 if (AllZs) {
Jim Stichnoth78b4c0b2014-07-11 15:29:23 -0700170 // Replace N 'Z' characters with a '0' (if N=0) or '1' (if
171 // N>0) followed by N '0' characters.
172 NewName[NewPos++] = (Length ? '1' : '0');
173 for (size_t i = 0; i < Length; ++i) {
Jim Stichnoth217dc082014-07-11 14:06:55 -0700174 NewName[NewPos++] = '0';
175 }
176 } else {
177 // Iterate right-to-left and increment the base-36 number.
178 bool Carry = true;
179 for (size_t i = 0; i < Length; ++i) {
180 size_t Offset = Length - 1 - i;
181 char Ch = OldName[OldPos + Offset];
182 if (Carry) {
183 Carry = false;
184 switch (Ch) {
185 case '9':
186 Ch = 'A';
187 break;
188 case 'Z':
189 Ch = '0';
190 Carry = true;
191 break;
192 default:
193 ++Ch;
194 break;
195 }
196 }
197 NewName[NewPos + Offset] = Ch;
198 }
199 NewPos += Length;
200 }
201 OldPos = Last;
202 // Fall through and let the '_' be copied across.
203 }
204 }
205 NewName[NewPos] = OldName[OldPos];
206 }
207 assert(NewName[NewPos] == '\0');
208 OldName = NewName;
209}
210
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700211// In this context, name mangling means to rewrite a symbol using a
212// given prefix. For a C++ symbol, nest the original symbol inside
213// the "prefix" namespace. For other symbols, just prepend the
214// prefix.
215IceString GlobalContext::mangleName(const IceString &Name) const {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700216 // An already-nested name like foo::bar() gets pushed down one
217 // level, making it equivalent to Prefix::foo::bar().
218 // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
219 // A non-nested but mangled name like bar() gets nested, making it
220 // equivalent to Prefix::bar().
221 // _Z3barxyz ==> ZN6Prefix3barExyz
222 // An unmangled, extern "C" style name, gets a simple prefix:
223 // bar ==> Prefixbar
224 if (getTestPrefix().empty())
225 return Name;
226
227 unsigned PrefixLength = getTestPrefix().length();
Jim Stichnoth217dc082014-07-11 14:06:55 -0700228 ManglerVector NameBase(1 + Name.length());
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700229 const size_t BufLen = 30 + Name.length() + PrefixLength;
Jim Stichnoth217dc082014-07-11 14:06:55 -0700230 ManglerVector NewName(BufLen);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700231 uint32_t BaseLength = 0; // using uint32_t due to sscanf format string
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700232
Derek Schuff44712d12014-06-17 14:34:34 -0700233 int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase.data());
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700234 if (ItemsParsed == 1) {
235 // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
236 // (splice in "6Prefix") ^^^^^^^
Derek Schuff44712d12014-06-17 14:34:34 -0700237 snprintf(NewName.data(), BufLen, "_ZN%u%s%s", PrefixLength,
238 getTestPrefix().c_str(), NameBase.data());
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700239 // We ignore the snprintf return value (here and below). If we
240 // somehow miscalculated the output buffer length, the output will
241 // be truncated, but it will be truncated consistently for all
242 // mangleName() calls on the same input string.
Jim Stichnoth217dc082014-07-11 14:06:55 -0700243 incrementSubstitutions(NewName);
Derek Schuff44712d12014-06-17 14:34:34 -0700244 return NewName.data();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700245 }
246
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700247 // Artificially limit BaseLength to 9 digits (less than 1 billion)
248 // because sscanf behavior is undefined on integer overflow. If
249 // there are more than 9 digits (which we test by looking at the
250 // beginning of NameBase), then we consider this a failure to parse
251 // a namespace mangling, and fall back to the simple prefixing.
Derek Schuff44712d12014-06-17 14:34:34 -0700252 ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase.data());
253 if (ItemsParsed == 2 && BaseLength <= strlen(NameBase.data()) &&
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700254 !isdigit(NameBase[0])) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700255 // Transform _Z3barxyz ==> _ZN6Prefix3barExyz
256 // ^^^^^^^^ ^
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700257 // (splice in "N6Prefix", and insert "E" after "3bar")
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700258 // But an "I" after the identifier indicates a template argument
259 // list terminated with "E"; insert the new "E" before/after the
260 // old "E". E.g.:
261 // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz
262 // ^^^^^^^^ ^
263 // (splice in "N6Prefix", and insert "E" after "3barIabcE")
Jim Stichnoth217dc082014-07-11 14:06:55 -0700264 ManglerVector OrigName(Name.length());
265 ManglerVector OrigSuffix(Name.length());
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700266 uint32_t ActualBaseLength = BaseLength;
267 if (NameBase[ActualBaseLength] == 'I') {
268 ++ActualBaseLength;
269 while (NameBase[ActualBaseLength] != 'E' &&
270 NameBase[ActualBaseLength] != '\0')
271 ++ActualBaseLength;
272 }
Derek Schuff44712d12014-06-17 14:34:34 -0700273 strncpy(OrigName.data(), NameBase.data(), ActualBaseLength);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700274 OrigName[ActualBaseLength] = '\0';
Derek Schuff44712d12014-06-17 14:34:34 -0700275 strcpy(OrigSuffix.data(), NameBase.data() + ActualBaseLength);
276 snprintf(NewName.data(), BufLen, "_ZN%u%s%u%sE%s", PrefixLength,
277 getTestPrefix().c_str(), BaseLength, OrigName.data(),
278 OrigSuffix.data());
Jim Stichnoth217dc082014-07-11 14:06:55 -0700279 incrementSubstitutions(NewName);
Derek Schuff44712d12014-06-17 14:34:34 -0700280 return NewName.data();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700281 }
282
283 // Transform bar ==> Prefixbar
284 // ^^^^^^
285 return getTestPrefix() + Name;
286}
287
288GlobalContext::~GlobalContext() {}
289
Jan Voungbc004632014-09-16 15:09:10 -0700290Constant *GlobalContext::getConstantInt64(Type Ty, uint64_t ConstantInt64) {
291 assert(Ty == IceType_i64);
292 return ConstPool->Integers64.getOrAdd(this, Ty, ConstantInt64);
293}
294
295Constant *GlobalContext::getConstantInt32(Type Ty, uint32_t ConstantInt32) {
Jim Stichnoth3ef786f2014-09-08 11:19:21 -0700296 if (Ty == IceType_i1)
Jan Voungbc004632014-09-16 15:09:10 -0700297 ConstantInt32 &= UINT32_C(1);
298 return ConstPool->Integers32.getOrAdd(this, Ty, ConstantInt32);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700299}
300
301Constant *GlobalContext::getConstantFloat(float ConstantFloat) {
302 return ConstPool->Floats.getOrAdd(this, IceType_f32, ConstantFloat);
303}
304
305Constant *GlobalContext::getConstantDouble(double ConstantDouble) {
306 return ConstPool->Doubles.getOrAdd(this, IceType_f64, ConstantDouble);
307}
308
309Constant *GlobalContext::getConstantSym(Type Ty, int64_t Offset,
310 const IceString &Name,
311 bool SuppressMangling) {
312 return ConstPool->Relocatables.getOrAdd(
313 this, Ty, RelocatableTuple(Offset, Name, SuppressMangling));
314}
315
Matt Walad8f4a7d2014-06-18 09:55:03 -0700316Constant *GlobalContext::getConstantUndef(Type Ty) {
317 return ConstPool->Undefs.getOrAdd(this, Ty);
318}
319
320Constant *GlobalContext::getConstantZero(Type Ty) {
321 switch (Ty) {
322 case IceType_i1:
323 case IceType_i8:
324 case IceType_i16:
325 case IceType_i32:
Jan Voungbc004632014-09-16 15:09:10 -0700326 return getConstantInt32(Ty, 0);
Matt Walad8f4a7d2014-06-18 09:55:03 -0700327 case IceType_i64:
Jan Voungbc004632014-09-16 15:09:10 -0700328 return getConstantInt64(Ty, 0);
Matt Walad8f4a7d2014-06-18 09:55:03 -0700329 case IceType_f32:
330 return getConstantFloat(0);
331 case IceType_f64:
332 return getConstantDouble(0);
Matt Wala928f1292014-07-07 16:50:46 -0700333 case IceType_v4i1:
334 case IceType_v8i1:
335 case IceType_v16i1:
336 case IceType_v16i8:
337 case IceType_v8i16:
338 case IceType_v4i32:
339 case IceType_v4f32: {
340 IceString Str;
341 llvm::raw_string_ostream BaseOS(Str);
Jim Stichnoth78282f62014-07-27 23:14:00 -0700342 BaseOS << "Unsupported constant type: " << Ty;
Matt Wala928f1292014-07-07 16:50:46 -0700343 llvm_unreachable(BaseOS.str().c_str());
344 } break;
Matt Walad8f4a7d2014-06-18 09:55:03 -0700345 case IceType_void:
346 case IceType_NUM:
347 break;
348 }
349 llvm_unreachable("Unknown type");
350}
351
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700352ConstantList GlobalContext::getConstantPool(Type Ty) const {
353 switch (Ty) {
354 case IceType_i1:
355 case IceType_i8:
356 case IceType_i16:
357 case IceType_i32:
Jan Voungbc004632014-09-16 15:09:10 -0700358 return ConstPool->Integers32.getConstantPool();
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700359 case IceType_i64:
Jan Voungbc004632014-09-16 15:09:10 -0700360 return ConstPool->Integers64.getConstantPool();
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700361 case IceType_f32:
362 return ConstPool->Floats.getConstantPool();
363 case IceType_f64:
364 return ConstPool->Doubles.getConstantPool();
Matt Wala928f1292014-07-07 16:50:46 -0700365 case IceType_v4i1:
366 case IceType_v8i1:
367 case IceType_v16i1:
368 case IceType_v16i8:
369 case IceType_v8i16:
370 case IceType_v4i32:
371 case IceType_v4f32: {
372 IceString Str;
373 llvm::raw_string_ostream BaseOS(Str);
Jim Stichnoth78282f62014-07-27 23:14:00 -0700374 BaseOS << "Unsupported constant type: " << Ty;
Matt Wala928f1292014-07-07 16:50:46 -0700375 llvm_unreachable(BaseOS.str().c_str());
376 } break;
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700377 case IceType_void:
378 case IceType_NUM:
379 break;
380 }
381 llvm_unreachable("Unknown type");
382}
383
Jim Stichnothc4554d72014-09-30 16:49:38 -0700384TimerIdT GlobalContext::getTimerID(const IceString &Name) {
385 return TimerStack::getTimerID(Name);
386}
387
388void GlobalContext::pushTimer(TimerIdT ID) { Timers->push(ID); }
389
390void GlobalContext::popTimer(TimerIdT ID) { Timers->pop(ID); }
391
Jim Stichnothff9c7062014-09-18 04:50:49 -0700392void GlobalContext::dumpStats(const IceString &Name, bool Final) {
Jim Stichnoth18735602014-09-16 19:59:35 -0700393 if (Flags.DumpStats) {
Jim Stichnothff9c7062014-09-18 04:50:49 -0700394 if (Final) {
395 StatsCumulative.dump(Name, getStrDump());
396 } else {
397 StatsFunction.dump(Name, getStrDump());
398 StatsCumulative.dump("_TOTAL_", getStrDump());
399 }
Jim Stichnoth18735602014-09-16 19:59:35 -0700400 }
401}
402
Jim Stichnothc4554d72014-09-30 16:49:38 -0700403void GlobalContext::dumpTimers() { Timers->dump(getStrDump()); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700404
405} // end of namespace Ice