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 | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 122 | RNG("") { |
| 123 | // Pre-register built-in stack names. |
| 124 | newTimerStackID("Total across all functions"); |
| 125 | newTimerStackID("Per-function summary"); |
| 126 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 127 | |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 128 | // Scan a string for S[0-9A-Z]*_ patterns and replace them with |
| 129 | // S<num>_ where <num> is the next base-36 value. If a type name |
| 130 | // legitimately contains that pattern, then the substitution will be |
| 131 | // made in error and most likely the link will fail. In this case, |
| 132 | // the test classes can be rewritten not to use that pattern, which is |
| 133 | // much simpler and more reliable than implementing a full demangling |
| 134 | // parser. Another substitution-in-error may occur if a type |
| 135 | // identifier ends with the pattern S[0-9A-Z]*, because an immediately |
| 136 | // following substitution string like "S1_" or "PS1_" may be combined |
| 137 | // with the previous type. |
| 138 | void GlobalContext::incrementSubstitutions(ManglerVector &OldName) const { |
| 139 | const std::locale CLocale("C"); |
| 140 | // Provide extra space in case the length of <num> increases. |
| 141 | ManglerVector NewName(OldName.size() * 2); |
| 142 | size_t OldPos = 0; |
| 143 | size_t NewPos = 0; |
| 144 | size_t OldLen = OldName.size(); |
| 145 | for (; OldPos < OldLen; ++OldPos, ++NewPos) { |
| 146 | if (OldName[OldPos] == '\0') |
| 147 | break; |
| 148 | if (OldName[OldPos] == 'S') { |
| 149 | // Search forward until we find _ or invalid character (including \0). |
| 150 | bool AllZs = true; |
| 151 | bool Found = false; |
| 152 | size_t Last; |
| 153 | for (Last = OldPos + 1; Last < OldLen; ++Last) { |
| 154 | char Ch = OldName[Last]; |
| 155 | if (Ch == '_') { |
| 156 | Found = true; |
| 157 | break; |
| 158 | } else if (std::isdigit(Ch) || std::isupper(Ch, CLocale)) { |
| 159 | if (Ch != 'Z') |
| 160 | AllZs = false; |
| 161 | } else { |
| 162 | // Invalid character, stop searching. |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | if (Found) { |
| 167 | NewName[NewPos++] = OldName[OldPos++]; // 'S' |
| 168 | size_t Length = Last - OldPos; |
| 169 | // NewPos and OldPos point just past the 'S'. |
| 170 | assert(NewName[NewPos - 1] == 'S'); |
| 171 | assert(OldName[OldPos - 1] == 'S'); |
| 172 | assert(OldName[OldPos + Length] == '_'); |
| 173 | if (AllZs) { |
Jim Stichnoth | 78b4c0b | 2014-07-11 15:29:23 -0700 | [diff] [blame] | 174 | // Replace N 'Z' characters with a '0' (if N=0) or '1' (if |
| 175 | // N>0) followed by N '0' characters. |
| 176 | NewName[NewPos++] = (Length ? '1' : '0'); |
| 177 | for (size_t i = 0; i < Length; ++i) { |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 178 | NewName[NewPos++] = '0'; |
| 179 | } |
| 180 | } else { |
| 181 | // Iterate right-to-left and increment the base-36 number. |
| 182 | bool Carry = true; |
| 183 | for (size_t i = 0; i < Length; ++i) { |
| 184 | size_t Offset = Length - 1 - i; |
| 185 | char Ch = OldName[OldPos + Offset]; |
| 186 | if (Carry) { |
| 187 | Carry = false; |
| 188 | switch (Ch) { |
| 189 | case '9': |
| 190 | Ch = 'A'; |
| 191 | break; |
| 192 | case 'Z': |
| 193 | Ch = '0'; |
| 194 | Carry = true; |
| 195 | break; |
| 196 | default: |
| 197 | ++Ch; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | NewName[NewPos + Offset] = Ch; |
| 202 | } |
| 203 | NewPos += Length; |
| 204 | } |
| 205 | OldPos = Last; |
| 206 | // Fall through and let the '_' be copied across. |
| 207 | } |
| 208 | } |
| 209 | NewName[NewPos] = OldName[OldPos]; |
| 210 | } |
| 211 | assert(NewName[NewPos] == '\0'); |
| 212 | OldName = NewName; |
| 213 | } |
| 214 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 215 | // In this context, name mangling means to rewrite a symbol using a |
| 216 | // given prefix. For a C++ symbol, nest the original symbol inside |
| 217 | // the "prefix" namespace. For other symbols, just prepend the |
| 218 | // prefix. |
| 219 | IceString GlobalContext::mangleName(const IceString &Name) const { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 220 | // An already-nested name like foo::bar() gets pushed down one |
| 221 | // level, making it equivalent to Prefix::foo::bar(). |
| 222 | // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz |
| 223 | // A non-nested but mangled name like bar() gets nested, making it |
| 224 | // equivalent to Prefix::bar(). |
| 225 | // _Z3barxyz ==> ZN6Prefix3barExyz |
| 226 | // An unmangled, extern "C" style name, gets a simple prefix: |
| 227 | // bar ==> Prefixbar |
| 228 | if (getTestPrefix().empty()) |
| 229 | return Name; |
| 230 | |
| 231 | unsigned PrefixLength = getTestPrefix().length(); |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 232 | ManglerVector NameBase(1 + Name.length()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 233 | const size_t BufLen = 30 + Name.length() + PrefixLength; |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 234 | ManglerVector NewName(BufLen); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 235 | uint32_t BaseLength = 0; // using uint32_t due to sscanf format string |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 236 | |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 237 | int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase.data()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 238 | if (ItemsParsed == 1) { |
| 239 | // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz |
| 240 | // (splice in "6Prefix") ^^^^^^^ |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 241 | snprintf(NewName.data(), BufLen, "_ZN%u%s%s", PrefixLength, |
| 242 | getTestPrefix().c_str(), NameBase.data()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 243 | // We ignore the snprintf return value (here and below). If we |
| 244 | // somehow miscalculated the output buffer length, the output will |
| 245 | // be truncated, but it will be truncated consistently for all |
| 246 | // mangleName() calls on the same input string. |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 247 | incrementSubstitutions(NewName); |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 248 | return NewName.data(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 251 | // Artificially limit BaseLength to 9 digits (less than 1 billion) |
| 252 | // because sscanf behavior is undefined on integer overflow. If |
| 253 | // there are more than 9 digits (which we test by looking at the |
| 254 | // beginning of NameBase), then we consider this a failure to parse |
| 255 | // a namespace mangling, and fall back to the simple prefixing. |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 256 | ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase.data()); |
| 257 | if (ItemsParsed == 2 && BaseLength <= strlen(NameBase.data()) && |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 258 | !isdigit(NameBase[0])) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 259 | // Transform _Z3barxyz ==> _ZN6Prefix3barExyz |
| 260 | // ^^^^^^^^ ^ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 261 | // (splice in "N6Prefix", and insert "E" after "3bar") |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 262 | // But an "I" after the identifier indicates a template argument |
| 263 | // list terminated with "E"; insert the new "E" before/after the |
| 264 | // old "E". E.g.: |
| 265 | // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz |
| 266 | // ^^^^^^^^ ^ |
| 267 | // (splice in "N6Prefix", and insert "E" after "3barIabcE") |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 268 | ManglerVector OrigName(Name.length()); |
| 269 | ManglerVector OrigSuffix(Name.length()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 270 | uint32_t ActualBaseLength = BaseLength; |
| 271 | if (NameBase[ActualBaseLength] == 'I') { |
| 272 | ++ActualBaseLength; |
| 273 | while (NameBase[ActualBaseLength] != 'E' && |
| 274 | NameBase[ActualBaseLength] != '\0') |
| 275 | ++ActualBaseLength; |
| 276 | } |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 277 | strncpy(OrigName.data(), NameBase.data(), ActualBaseLength); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 278 | OrigName[ActualBaseLength] = '\0'; |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 279 | strcpy(OrigSuffix.data(), NameBase.data() + ActualBaseLength); |
| 280 | snprintf(NewName.data(), BufLen, "_ZN%u%s%u%sE%s", PrefixLength, |
| 281 | getTestPrefix().c_str(), BaseLength, OrigName.data(), |
| 282 | OrigSuffix.data()); |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 283 | incrementSubstitutions(NewName); |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 284 | return NewName.data(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Transform bar ==> Prefixbar |
| 288 | // ^^^^^^ |
| 289 | return getTestPrefix() + Name; |
| 290 | } |
| 291 | |
| 292 | GlobalContext::~GlobalContext() {} |
| 293 | |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 294 | Constant *GlobalContext::getConstantInt64(Type Ty, uint64_t ConstantInt64) { |
| 295 | assert(Ty == IceType_i64); |
| 296 | return ConstPool->Integers64.getOrAdd(this, Ty, ConstantInt64); |
| 297 | } |
| 298 | |
| 299 | Constant *GlobalContext::getConstantInt32(Type Ty, uint32_t ConstantInt32) { |
Jim Stichnoth | 3ef786f | 2014-09-08 11:19:21 -0700 | [diff] [blame] | 300 | if (Ty == IceType_i1) |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 301 | ConstantInt32 &= UINT32_C(1); |
| 302 | return ConstPool->Integers32.getOrAdd(this, Ty, ConstantInt32); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | Constant *GlobalContext::getConstantFloat(float ConstantFloat) { |
| 306 | return ConstPool->Floats.getOrAdd(this, IceType_f32, ConstantFloat); |
| 307 | } |
| 308 | |
| 309 | Constant *GlobalContext::getConstantDouble(double ConstantDouble) { |
| 310 | return ConstPool->Doubles.getOrAdd(this, IceType_f64, ConstantDouble); |
| 311 | } |
| 312 | |
| 313 | Constant *GlobalContext::getConstantSym(Type Ty, int64_t Offset, |
| 314 | const IceString &Name, |
| 315 | bool SuppressMangling) { |
| 316 | return ConstPool->Relocatables.getOrAdd( |
| 317 | this, Ty, RelocatableTuple(Offset, Name, SuppressMangling)); |
| 318 | } |
| 319 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 320 | Constant *GlobalContext::getConstantUndef(Type Ty) { |
| 321 | return ConstPool->Undefs.getOrAdd(this, Ty); |
| 322 | } |
| 323 | |
| 324 | Constant *GlobalContext::getConstantZero(Type Ty) { |
| 325 | switch (Ty) { |
| 326 | case IceType_i1: |
| 327 | case IceType_i8: |
| 328 | case IceType_i16: |
| 329 | case IceType_i32: |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 330 | return getConstantInt32(Ty, 0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 331 | case IceType_i64: |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 332 | return getConstantInt64(Ty, 0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 333 | case IceType_f32: |
| 334 | return getConstantFloat(0); |
| 335 | case IceType_f64: |
| 336 | return getConstantDouble(0); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 337 | case IceType_v4i1: |
| 338 | case IceType_v8i1: |
| 339 | case IceType_v16i1: |
| 340 | case IceType_v16i8: |
| 341 | case IceType_v8i16: |
| 342 | case IceType_v4i32: |
| 343 | case IceType_v4f32: { |
| 344 | IceString Str; |
| 345 | llvm::raw_string_ostream BaseOS(Str); |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 346 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 347 | llvm_unreachable(BaseOS.str().c_str()); |
| 348 | } break; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 349 | case IceType_void: |
| 350 | case IceType_NUM: |
| 351 | break; |
| 352 | } |
| 353 | llvm_unreachable("Unknown type"); |
| 354 | } |
| 355 | |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 356 | ConstantList GlobalContext::getConstantPool(Type Ty) const { |
| 357 | switch (Ty) { |
| 358 | case IceType_i1: |
| 359 | case IceType_i8: |
| 360 | case IceType_i16: |
| 361 | case IceType_i32: |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 362 | return ConstPool->Integers32.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 363 | case IceType_i64: |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 364 | return ConstPool->Integers64.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 365 | case IceType_f32: |
| 366 | return ConstPool->Floats.getConstantPool(); |
| 367 | case IceType_f64: |
| 368 | return ConstPool->Doubles.getConstantPool(); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 369 | case IceType_v4i1: |
| 370 | case IceType_v8i1: |
| 371 | case IceType_v16i1: |
| 372 | case IceType_v16i8: |
| 373 | case IceType_v8i16: |
| 374 | case IceType_v4i32: |
| 375 | case IceType_v4f32: { |
| 376 | IceString Str; |
| 377 | llvm::raw_string_ostream BaseOS(Str); |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 378 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 379 | llvm_unreachable(BaseOS.str().c_str()); |
| 380 | } break; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 381 | case IceType_void: |
| 382 | case IceType_NUM: |
| 383 | break; |
| 384 | } |
| 385 | llvm_unreachable("Unknown type"); |
| 386 | } |
| 387 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 388 | TimerIdT GlobalContext::getTimerID(TimerStackIdT StackID, |
| 389 | const IceString &Name) { |
| 390 | assert(StackID < Timers.size()); |
| 391 | return Timers[StackID].getTimerID(Name); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 394 | TimerStackIdT GlobalContext::newTimerStackID(const IceString &Name) { |
| 395 | TimerStackIdT NewID = Timers.size(); |
| 396 | Timers.push_back(TimerStack(Name)); |
| 397 | return NewID; |
| 398 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 399 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 400 | void GlobalContext::pushTimer(TimerIdT ID, TimerStackIdT StackID) { |
| 401 | assert(StackID < Timers.size()); |
| 402 | Timers[StackID].push(ID); |
| 403 | } |
| 404 | |
| 405 | void GlobalContext::popTimer(TimerIdT ID, TimerStackIdT StackID) { |
| 406 | assert(StackID < Timers.size()); |
| 407 | Timers[StackID].pop(ID); |
| 408 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 409 | |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame^] | 410 | void GlobalContext::resetTimer(TimerStackIdT StackID) { |
| 411 | assert(StackID < Timers.size()); |
| 412 | Timers[StackID].reset(); |
| 413 | } |
| 414 | |
| 415 | void GlobalContext::setTimerName(TimerStackIdT StackID, |
| 416 | const IceString &NewName) { |
| 417 | assert(StackID < Timers.size()); |
| 418 | Timers[StackID].setName(NewName); |
| 419 | } |
| 420 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 421 | void GlobalContext::dumpStats(const IceString &Name, bool Final) { |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 422 | if (Flags.DumpStats) { |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 423 | if (Final) { |
| 424 | StatsCumulative.dump(Name, getStrDump()); |
| 425 | } else { |
| 426 | StatsFunction.dump(Name, getStrDump()); |
| 427 | StatsCumulative.dump("_TOTAL_", getStrDump()); |
| 428 | } |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 432 | void GlobalContext::dumpTimers(TimerStackIdT StackID, bool DumpCumulative) { |
| 433 | assert(Timers.size() > StackID); |
| 434 | Timers[StackID].dump(getStrDump(), DumpCumulative); |
| 435 | } |
| 436 | |
| 437 | TimerMarker::TimerMarker(TimerIdT ID, const Cfg *Func) |
| 438 | : ID(ID), Ctx(Func->getContext()), |
| 439 | Active(Func->getFocusedTiming() || Ctx->getFlags().SubzeroTimingEnabled) { |
| 440 | if (Active) |
| 441 | Ctx->pushTimer(ID); |
| 442 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 443 | |
| 444 | } // end of namespace Ice |