Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceGlobalContext.cpp - Global context defs -------------===// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 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 Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 15 | #include <ctype.h> // isdigit(), isupper() |
Jan Voung | 839c4ce | 2014-07-28 15:19:43 -0700 | [diff] [blame] | 16 | #include <locale> // locale |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 17 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 18 | #include "IceCfg.h" |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 19 | #include "IceClFlags.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 20 | #include "IceDefs.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 21 | #include "IceGlobalContext.h" |
| 22 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 23 | #include "IceTargetLowering.h" |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 24 | #include "IceTimerTree.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 25 | #include "IceTypes.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 26 | |
| 27 | namespace 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. |
| 36 | template <typename KeyType, typename ValueType, bool KeyTypeHasFP = false> |
| 37 | class TypePool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame^] | 38 | TypePool(const TypePool &) = delete; |
| 39 | TypePool &operator=(const TypePool &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 40 | |
| 41 | public: |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 42 | TypePool() : NextPoolID(0) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 43 | ValueType *getOrAdd(GlobalContext *Ctx, Type Ty, KeyType Key) { |
| 44 | TupleType TupleKey = std::make_pair(Ty, Key); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 45 | auto Iter = Pool.find(TupleKey); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 46 | if (Iter != Pool.end()) |
| 47 | return Iter->second; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 48 | ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 49 | Pool[TupleKey] = Result; |
| 50 | return Result; |
| 51 | } |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 52 | ConstantList getConstantPool() const { |
| 53 | ConstantList Constants; |
| 54 | Constants.reserve(Pool.size()); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 55 | for (auto &I : Pool) |
| 56 | Constants.push_back(I.second); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 57 | return Constants; |
| 58 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | typedef std::pair<Type, KeyType> TupleType; |
| 62 | struct TupleCompare { |
Jan Voung | 839c4ce | 2014-07-28 15:19:43 -0700 | [diff] [blame] | 63 | bool operator()(const TupleType &A, const TupleType &B) const { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 64 | 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 Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 73 | uint32_t NextPoolID; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 76 | // UndefPool maps ICE types to the corresponding ConstantUndef values. |
| 77 | class UndefPool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame^] | 78 | UndefPool(const UndefPool &) = delete; |
| 79 | UndefPool &operator=(const UndefPool &) = delete; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 80 | |
| 81 | public: |
| 82 | UndefPool() : NextPoolID(0) {} |
| 83 | |
| 84 | ConstantUndef *getOrAdd(GlobalContext *Ctx, Type Ty) { |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 85 | auto I = Pool.find(Ty); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 86 | 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 | |
| 93 | private: |
| 94 | uint32_t NextPoolID; |
| 95 | typedef std::map<Type, ConstantUndef *> ContainerType; |
| 96 | ContainerType Pool; |
| 97 | }; |
| 98 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 99 | // The global constant pool bundles individual pools of each type of |
| 100 | // interest. |
| 101 | class ConstantPool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame^] | 102 | ConstantPool(const ConstantPool &) = delete; |
| 103 | ConstantPool &operator=(const ConstantPool &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 104 | |
| 105 | public: |
| 106 | ConstantPool() {} |
| 107 | TypePool<float, ConstantFloat, true> Floats; |
| 108 | TypePool<double, ConstantDouble, true> Doubles; |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 109 | TypePool<uint32_t, ConstantInteger32> Integers32; |
| 110 | TypePool<uint64_t, ConstantInteger64> Integers64; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 111 | TypePool<RelocatableTuple, ConstantRelocatable> Relocatables; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 112 | UndefPool Undefs; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | GlobalContext::GlobalContext(llvm::raw_ostream *OsDump, |
| 116 | llvm::raw_ostream *OsEmit, VerboseMask Mask, |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 117 | TargetArch Arch, OptLevel Opt, |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 118 | IceString TestPrefix, const ClFlags &Flags) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 119 | : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask), |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 120 | ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt), |
Matt Wala | 1bd2fce | 2014-08-08 14:02:09 -0700 | [diff] [blame] | 121 | TestPrefix(TestPrefix), Flags(Flags), HasEmittedFirstMethod(false), |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 122 | RNG(""), Timers(new TimerStack("main")) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 123 | |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 124 | // 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. |
| 134 | void 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 Stichnoth | 78b4c0b | 2014-07-11 15:29:23 -0700 | [diff] [blame] | 170 | // 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 Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 174 | 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 Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 211 | // 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. |
| 215 | IceString GlobalContext::mangleName(const IceString &Name) const { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 216 | // 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 Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 228 | ManglerVector NameBase(1 + Name.length()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 229 | const size_t BufLen = 30 + Name.length() + PrefixLength; |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 230 | ManglerVector NewName(BufLen); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 231 | uint32_t BaseLength = 0; // using uint32_t due to sscanf format string |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 232 | |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 233 | int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase.data()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 234 | if (ItemsParsed == 1) { |
| 235 | // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz |
| 236 | // (splice in "6Prefix") ^^^^^^^ |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 237 | snprintf(NewName.data(), BufLen, "_ZN%u%s%s", PrefixLength, |
| 238 | getTestPrefix().c_str(), NameBase.data()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 239 | // 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 Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 243 | incrementSubstitutions(NewName); |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 244 | return NewName.data(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 247 | // 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 Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 252 | ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase.data()); |
| 253 | if (ItemsParsed == 2 && BaseLength <= strlen(NameBase.data()) && |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 254 | !isdigit(NameBase[0])) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 255 | // Transform _Z3barxyz ==> _ZN6Prefix3barExyz |
| 256 | // ^^^^^^^^ ^ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 257 | // (splice in "N6Prefix", and insert "E" after "3bar") |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 258 | // 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 Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 264 | ManglerVector OrigName(Name.length()); |
| 265 | ManglerVector OrigSuffix(Name.length()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 266 | uint32_t ActualBaseLength = BaseLength; |
| 267 | if (NameBase[ActualBaseLength] == 'I') { |
| 268 | ++ActualBaseLength; |
| 269 | while (NameBase[ActualBaseLength] != 'E' && |
| 270 | NameBase[ActualBaseLength] != '\0') |
| 271 | ++ActualBaseLength; |
| 272 | } |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 273 | strncpy(OrigName.data(), NameBase.data(), ActualBaseLength); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 274 | OrigName[ActualBaseLength] = '\0'; |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 275 | 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 Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 279 | incrementSubstitutions(NewName); |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 280 | return NewName.data(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Transform bar ==> Prefixbar |
| 284 | // ^^^^^^ |
| 285 | return getTestPrefix() + Name; |
| 286 | } |
| 287 | |
| 288 | GlobalContext::~GlobalContext() {} |
| 289 | |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 290 | Constant *GlobalContext::getConstantInt64(Type Ty, uint64_t ConstantInt64) { |
| 291 | assert(Ty == IceType_i64); |
| 292 | return ConstPool->Integers64.getOrAdd(this, Ty, ConstantInt64); |
| 293 | } |
| 294 | |
| 295 | Constant *GlobalContext::getConstantInt32(Type Ty, uint32_t ConstantInt32) { |
Jim Stichnoth | 3ef786f | 2014-09-08 11:19:21 -0700 | [diff] [blame] | 296 | if (Ty == IceType_i1) |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 297 | ConstantInt32 &= UINT32_C(1); |
| 298 | return ConstPool->Integers32.getOrAdd(this, Ty, ConstantInt32); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | Constant *GlobalContext::getConstantFloat(float ConstantFloat) { |
| 302 | return ConstPool->Floats.getOrAdd(this, IceType_f32, ConstantFloat); |
| 303 | } |
| 304 | |
| 305 | Constant *GlobalContext::getConstantDouble(double ConstantDouble) { |
| 306 | return ConstPool->Doubles.getOrAdd(this, IceType_f64, ConstantDouble); |
| 307 | } |
| 308 | |
| 309 | Constant *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 Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 316 | Constant *GlobalContext::getConstantUndef(Type Ty) { |
| 317 | return ConstPool->Undefs.getOrAdd(this, Ty); |
| 318 | } |
| 319 | |
| 320 | Constant *GlobalContext::getConstantZero(Type Ty) { |
| 321 | switch (Ty) { |
| 322 | case IceType_i1: |
| 323 | case IceType_i8: |
| 324 | case IceType_i16: |
| 325 | case IceType_i32: |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 326 | return getConstantInt32(Ty, 0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 327 | case IceType_i64: |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 328 | return getConstantInt64(Ty, 0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 329 | case IceType_f32: |
| 330 | return getConstantFloat(0); |
| 331 | case IceType_f64: |
| 332 | return getConstantDouble(0); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 333 | 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 Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 342 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 343 | llvm_unreachable(BaseOS.str().c_str()); |
| 344 | } break; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 345 | case IceType_void: |
| 346 | case IceType_NUM: |
| 347 | break; |
| 348 | } |
| 349 | llvm_unreachable("Unknown type"); |
| 350 | } |
| 351 | |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 352 | ConstantList 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 Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 358 | return ConstPool->Integers32.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 359 | case IceType_i64: |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 360 | return ConstPool->Integers64.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 361 | case IceType_f32: |
| 362 | return ConstPool->Floats.getConstantPool(); |
| 363 | case IceType_f64: |
| 364 | return ConstPool->Doubles.getConstantPool(); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 365 | 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 Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 374 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 375 | llvm_unreachable(BaseOS.str().c_str()); |
| 376 | } break; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 377 | case IceType_void: |
| 378 | case IceType_NUM: |
| 379 | break; |
| 380 | } |
| 381 | llvm_unreachable("Unknown type"); |
| 382 | } |
| 383 | |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 384 | TimerIdT GlobalContext::getTimerID(const IceString &Name) { |
| 385 | return TimerStack::getTimerID(Name); |
| 386 | } |
| 387 | |
| 388 | void GlobalContext::pushTimer(TimerIdT ID) { Timers->push(ID); } |
| 389 | |
| 390 | void GlobalContext::popTimer(TimerIdT ID) { Timers->pop(ID); } |
| 391 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 392 | void GlobalContext::dumpStats(const IceString &Name, bool Final) { |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 393 | if (Flags.DumpStats) { |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 394 | if (Final) { |
| 395 | StatsCumulative.dump(Name, getStrDump()); |
| 396 | } else { |
| 397 | StatsFunction.dump(Name, getStrDump()); |
| 398 | StatsCumulative.dump("_TOTAL_", getStrDump()); |
| 399 | } |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 403 | void GlobalContext::dumpTimers() { Timers->dump(getStrDump()); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 404 | |
| 405 | } // end of namespace Ice |