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 | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 17 | #include <unordered_map> |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 18 | |
Jim Stichnoth | 639c921 | 2014-12-11 10:04:32 -0800 | [diff] [blame] | 19 | #include "llvm/Support/Timer.h" |
| 20 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 21 | #include "IceCfg.h" |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 22 | #include "IceClFlags.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 23 | #include "IceDefs.h" |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 24 | #include "IceELFObjectWriter.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 25 | #include "IceGlobalContext.h" |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 26 | #include "IceGlobalInits.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 27 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 28 | #include "IceTargetLowering.h" |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 29 | #include "IceTimerTree.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 30 | #include "IceTypes.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 31 | |
Jim Stichnoth | dddaf9c | 2014-12-04 14:09:21 -0800 | [diff] [blame] | 32 | namespace std { |
| 33 | template <> struct hash<Ice::RelocatableTuple> { |
| 34 | size_t operator()(const Ice::RelocatableTuple &Key) const { |
| 35 | return hash<Ice::IceString>()(Key.Name) + |
| 36 | hash<Ice::RelocOffsetT>()(Key.Offset); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 37 | } |
| 38 | }; |
Jim Stichnoth | dddaf9c | 2014-12-04 14:09:21 -0800 | [diff] [blame] | 39 | } // end of namespace std |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 40 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 41 | namespace Ice { |
| 42 | |
| 43 | // TypePool maps constants of type KeyType (e.g. float) to pointers to |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 44 | // type ValueType (e.g. ConstantFloat). |
| 45 | template <Type Ty, typename KeyType, typename ValueType> class TypePool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 46 | TypePool(const TypePool &) = delete; |
| 47 | TypePool &operator=(const TypePool &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 48 | |
| 49 | public: |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 50 | TypePool() : NextPoolID(0) {} |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 51 | ValueType *getOrAdd(GlobalContext *Ctx, KeyType Key) { |
| 52 | auto Iter = Pool.find(Key); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 53 | if (Iter != Pool.end()) |
| 54 | return Iter->second; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 55 | ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 56 | Pool[Key] = Result; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 57 | return Result; |
| 58 | } |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 59 | ConstantList getConstantPool() const { |
| 60 | ConstantList Constants; |
| 61 | Constants.reserve(Pool.size()); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 62 | for (auto &I : Pool) |
| 63 | Constants.push_back(I.second); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 64 | return Constants; |
| 65 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 66 | |
| 67 | private: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 68 | typedef std::unordered_map<KeyType, ValueType *> ContainerType; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 69 | ContainerType Pool; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 70 | uint32_t NextPoolID; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 73 | // UndefPool maps ICE types to the corresponding ConstantUndef values. |
| 74 | class UndefPool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 75 | UndefPool(const UndefPool &) = delete; |
| 76 | UndefPool &operator=(const UndefPool &) = delete; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 77 | |
| 78 | public: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 79 | UndefPool() : NextPoolID(0), Pool(IceType_NUM) {} |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 80 | |
| 81 | ConstantUndef *getOrAdd(GlobalContext *Ctx, Type Ty) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 82 | if (Pool[Ty] == nullptr) |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 83 | Pool[Ty] = ConstantUndef::create(Ctx, Ty, NextPoolID++); |
| 84 | return Pool[Ty]; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
| 88 | uint32_t NextPoolID; |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 89 | std::vector<ConstantUndef *> Pool; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 92 | // The global constant pool bundles individual pools of each type of |
| 93 | // interest. |
| 94 | class ConstantPool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 95 | ConstantPool(const ConstantPool &) = delete; |
| 96 | ConstantPool &operator=(const ConstantPool &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 97 | |
| 98 | public: |
| 99 | ConstantPool() {} |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 100 | TypePool<IceType_f32, float, ConstantFloat> Floats; |
| 101 | TypePool<IceType_f64, double, ConstantDouble> Doubles; |
| 102 | TypePool<IceType_i1, int8_t, ConstantInteger32> Integers1; |
| 103 | TypePool<IceType_i8, int8_t, ConstantInteger32> Integers8; |
| 104 | TypePool<IceType_i16, int16_t, ConstantInteger32> Integers16; |
| 105 | TypePool<IceType_i32, int32_t, ConstantInteger32> Integers32; |
| 106 | TypePool<IceType_i64, int64_t, ConstantInteger64> Integers64; |
| 107 | TypePool<IceType_i32, RelocatableTuple, ConstantRelocatable> Relocatables; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 108 | UndefPool Undefs; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 111 | void GlobalContext::CodeStats::dump(const IceString &Name, Ostream &Str) { |
Jim Stichnoth | 639c921 | 2014-12-11 10:04:32 -0800 | [diff] [blame] | 112 | if (!ALLOW_DUMP) |
| 113 | return; |
| 114 | Str << "|" << Name << "|Inst Count |" << InstructionsEmitted << "\n"; |
| 115 | Str << "|" << Name << "|Regs Saved |" << RegistersSaved << "\n"; |
| 116 | Str << "|" << Name << "|Frame Bytes |" << FrameBytes << "\n"; |
| 117 | Str << "|" << Name << "|Spills |" << Spills << "\n"; |
| 118 | Str << "|" << Name << "|Fills |" << Fills << "\n"; |
| 119 | Str << "|" << Name << "|Spills+Fills|" << Spills + Fills << "\n"; |
| 120 | Str << "|" << Name << "|Memory Usage|"; |
| 121 | if (ssize_t MemUsed = llvm::TimeRecord::getCurrentTime(false).getMemUsed()) |
| 122 | Str << MemUsed; |
| 123 | else |
| 124 | Str << "(requires '-track-memory')"; |
| 125 | Str << "\n"; |
| 126 | } |
| 127 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 128 | GlobalContext::GlobalContext(Ostream *OsDump, Ostream *OsEmit, |
| 129 | ELFStreamer *ELFStr, VerboseMask Mask, |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 130 | TargetArch Arch, OptLevel Opt, |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 131 | IceString TestPrefix, const ClFlags &Flags) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 132 | : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask), |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 133 | ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt), |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 134 | TestPrefix(TestPrefix), Flags(Flags), RNG(""), ObjectWriter() { |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame^] | 135 | // Make sure thread_local fields are properly initialized before any |
| 136 | // accesses are made. Do this here instead of at the start of |
| 137 | // main() so that all clients (e.g. unit tests) can benefit for |
| 138 | // free. |
| 139 | GlobalContext::TlsInit(); |
| 140 | Cfg::TlsInit(); |
| 141 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 142 | // Create a new ThreadContext for the current thread. No need to |
| 143 | // lock AllThreadContexts at this point since no other threads have |
| 144 | // access yet to this GlobalContext object. |
| 145 | AllThreadContexts.push_back(new ThreadContext()); |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame^] | 146 | ICE_TLS_SET_FIELD(TLS, AllThreadContexts.back()); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 147 | // Pre-register built-in stack names. |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 148 | if (ALLOW_DUMP) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 149 | // TODO(stichnot): There needs to be a strong relationship between |
| 150 | // the newTimerStackID() return values and TSK_Default/TSK_Funcs. |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 151 | newTimerStackID("Total across all functions"); |
| 152 | newTimerStackID("Per-function summary"); |
| 153 | } |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 154 | if (Flags.UseELFWriter) { |
| 155 | ObjectWriter.reset(new ELFObjectWriter(*this, *ELFStr)); |
| 156 | } |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 157 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 158 | |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 159 | // Scan a string for S[0-9A-Z]*_ patterns and replace them with |
| 160 | // S<num>_ where <num> is the next base-36 value. If a type name |
| 161 | // legitimately contains that pattern, then the substitution will be |
| 162 | // made in error and most likely the link will fail. In this case, |
| 163 | // the test classes can be rewritten not to use that pattern, which is |
| 164 | // much simpler and more reliable than implementing a full demangling |
| 165 | // parser. Another substitution-in-error may occur if a type |
| 166 | // identifier ends with the pattern S[0-9A-Z]*, because an immediately |
| 167 | // following substitution string like "S1_" or "PS1_" may be combined |
| 168 | // with the previous type. |
| 169 | void GlobalContext::incrementSubstitutions(ManglerVector &OldName) const { |
| 170 | const std::locale CLocale("C"); |
| 171 | // Provide extra space in case the length of <num> increases. |
| 172 | ManglerVector NewName(OldName.size() * 2); |
| 173 | size_t OldPos = 0; |
| 174 | size_t NewPos = 0; |
| 175 | size_t OldLen = OldName.size(); |
| 176 | for (; OldPos < OldLen; ++OldPos, ++NewPos) { |
| 177 | if (OldName[OldPos] == '\0') |
| 178 | break; |
| 179 | if (OldName[OldPos] == 'S') { |
| 180 | // Search forward until we find _ or invalid character (including \0). |
| 181 | bool AllZs = true; |
| 182 | bool Found = false; |
| 183 | size_t Last; |
| 184 | for (Last = OldPos + 1; Last < OldLen; ++Last) { |
| 185 | char Ch = OldName[Last]; |
| 186 | if (Ch == '_') { |
| 187 | Found = true; |
| 188 | break; |
| 189 | } else if (std::isdigit(Ch) || std::isupper(Ch, CLocale)) { |
| 190 | if (Ch != 'Z') |
| 191 | AllZs = false; |
| 192 | } else { |
| 193 | // Invalid character, stop searching. |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | if (Found) { |
| 198 | NewName[NewPos++] = OldName[OldPos++]; // 'S' |
| 199 | size_t Length = Last - OldPos; |
| 200 | // NewPos and OldPos point just past the 'S'. |
| 201 | assert(NewName[NewPos - 1] == 'S'); |
| 202 | assert(OldName[OldPos - 1] == 'S'); |
| 203 | assert(OldName[OldPos + Length] == '_'); |
| 204 | if (AllZs) { |
Jim Stichnoth | 78b4c0b | 2014-07-11 15:29:23 -0700 | [diff] [blame] | 205 | // Replace N 'Z' characters with a '0' (if N=0) or '1' (if |
| 206 | // N>0) followed by N '0' characters. |
| 207 | NewName[NewPos++] = (Length ? '1' : '0'); |
| 208 | for (size_t i = 0; i < Length; ++i) { |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 209 | NewName[NewPos++] = '0'; |
| 210 | } |
| 211 | } else { |
| 212 | // Iterate right-to-left and increment the base-36 number. |
| 213 | bool Carry = true; |
| 214 | for (size_t i = 0; i < Length; ++i) { |
| 215 | size_t Offset = Length - 1 - i; |
| 216 | char Ch = OldName[OldPos + Offset]; |
| 217 | if (Carry) { |
| 218 | Carry = false; |
| 219 | switch (Ch) { |
| 220 | case '9': |
| 221 | Ch = 'A'; |
| 222 | break; |
| 223 | case 'Z': |
| 224 | Ch = '0'; |
| 225 | Carry = true; |
| 226 | break; |
| 227 | default: |
| 228 | ++Ch; |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | NewName[NewPos + Offset] = Ch; |
| 233 | } |
| 234 | NewPos += Length; |
| 235 | } |
| 236 | OldPos = Last; |
| 237 | // Fall through and let the '_' be copied across. |
| 238 | } |
| 239 | } |
| 240 | NewName[NewPos] = OldName[OldPos]; |
| 241 | } |
| 242 | assert(NewName[NewPos] == '\0'); |
| 243 | OldName = NewName; |
| 244 | } |
| 245 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 246 | // In this context, name mangling means to rewrite a symbol using a |
| 247 | // given prefix. For a C++ symbol, nest the original symbol inside |
| 248 | // the "prefix" namespace. For other symbols, just prepend the |
| 249 | // prefix. |
| 250 | IceString GlobalContext::mangleName(const IceString &Name) const { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 251 | // An already-nested name like foo::bar() gets pushed down one |
| 252 | // level, making it equivalent to Prefix::foo::bar(). |
| 253 | // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz |
| 254 | // A non-nested but mangled name like bar() gets nested, making it |
| 255 | // equivalent to Prefix::bar(). |
| 256 | // _Z3barxyz ==> ZN6Prefix3barExyz |
| 257 | // An unmangled, extern "C" style name, gets a simple prefix: |
| 258 | // bar ==> Prefixbar |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 259 | if (!ALLOW_DUMP || getTestPrefix().empty()) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 260 | return Name; |
| 261 | |
| 262 | unsigned PrefixLength = getTestPrefix().length(); |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 263 | ManglerVector NameBase(1 + Name.length()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 264 | const size_t BufLen = 30 + Name.length() + PrefixLength; |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 265 | ManglerVector NewName(BufLen); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 266 | uint32_t BaseLength = 0; // using uint32_t due to sscanf format string |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 267 | |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 268 | int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase.data()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 269 | if (ItemsParsed == 1) { |
| 270 | // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz |
| 271 | // (splice in "6Prefix") ^^^^^^^ |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 272 | snprintf(NewName.data(), BufLen, "_ZN%u%s%s", PrefixLength, |
| 273 | getTestPrefix().c_str(), NameBase.data()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 274 | // We ignore the snprintf return value (here and below). If we |
| 275 | // somehow miscalculated the output buffer length, the output will |
| 276 | // be truncated, but it will be truncated consistently for all |
| 277 | // mangleName() calls on the same input string. |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 278 | incrementSubstitutions(NewName); |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 279 | return NewName.data(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 282 | // Artificially limit BaseLength to 9 digits (less than 1 billion) |
| 283 | // because sscanf behavior is undefined on integer overflow. If |
| 284 | // there are more than 9 digits (which we test by looking at the |
| 285 | // beginning of NameBase), then we consider this a failure to parse |
| 286 | // a namespace mangling, and fall back to the simple prefixing. |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 287 | ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase.data()); |
| 288 | if (ItemsParsed == 2 && BaseLength <= strlen(NameBase.data()) && |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 289 | !isdigit(NameBase[0])) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 290 | // Transform _Z3barxyz ==> _ZN6Prefix3barExyz |
| 291 | // ^^^^^^^^ ^ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 292 | // (splice in "N6Prefix", and insert "E" after "3bar") |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 293 | // But an "I" after the identifier indicates a template argument |
| 294 | // list terminated with "E"; insert the new "E" before/after the |
| 295 | // old "E". E.g.: |
| 296 | // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz |
| 297 | // ^^^^^^^^ ^ |
| 298 | // (splice in "N6Prefix", and insert "E" after "3barIabcE") |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 299 | ManglerVector OrigName(Name.length()); |
| 300 | ManglerVector OrigSuffix(Name.length()); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 301 | uint32_t ActualBaseLength = BaseLength; |
| 302 | if (NameBase[ActualBaseLength] == 'I') { |
| 303 | ++ActualBaseLength; |
| 304 | while (NameBase[ActualBaseLength] != 'E' && |
| 305 | NameBase[ActualBaseLength] != '\0') |
| 306 | ++ActualBaseLength; |
| 307 | } |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 308 | strncpy(OrigName.data(), NameBase.data(), ActualBaseLength); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 309 | OrigName[ActualBaseLength] = '\0'; |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 310 | strcpy(OrigSuffix.data(), NameBase.data() + ActualBaseLength); |
| 311 | snprintf(NewName.data(), BufLen, "_ZN%u%s%u%sE%s", PrefixLength, |
| 312 | getTestPrefix().c_str(), BaseLength, OrigName.data(), |
| 313 | OrigSuffix.data()); |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 314 | incrementSubstitutions(NewName); |
Derek Schuff | 44712d1 | 2014-06-17 14:34:34 -0700 | [diff] [blame] | 315 | return NewName.data(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Transform bar ==> Prefixbar |
| 319 | // ^^^^^^ |
| 320 | return getTestPrefix() + Name; |
| 321 | } |
| 322 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 323 | GlobalContext::~GlobalContext() { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 324 | llvm::DeleteContainerPointers(AllThreadContexts); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 325 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 326 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 327 | // TODO(stichnot): Consider adding thread-local caches of constant |
| 328 | // pool entries to reduce contention. |
| 329 | |
| 330 | // All locking is done by the getConstantInt[0-9]+() target function. |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 331 | Constant *GlobalContext::getConstantInt(Type Ty, int64_t Value) { |
| 332 | switch (Ty) { |
| 333 | case IceType_i1: |
| 334 | return getConstantInt1(Value); |
| 335 | case IceType_i8: |
| 336 | return getConstantInt8(Value); |
| 337 | case IceType_i16: |
| 338 | return getConstantInt16(Value); |
| 339 | case IceType_i32: |
| 340 | return getConstantInt32(Value); |
| 341 | case IceType_i64: |
| 342 | return getConstantInt64(Value); |
| 343 | default: |
| 344 | llvm_unreachable("Bad integer type for getConstant"); |
| 345 | } |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 346 | return nullptr; |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 349 | Constant *GlobalContext::getConstantInt1(int8_t ConstantInt1) { |
| 350 | ConstantInt1 &= INT8_C(1); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 351 | return getConstPool()->Integers1.getOrAdd(this, ConstantInt1); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | Constant *GlobalContext::getConstantInt8(int8_t ConstantInt8) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 355 | return getConstPool()->Integers8.getOrAdd(this, ConstantInt8); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | Constant *GlobalContext::getConstantInt16(int16_t ConstantInt16) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 359 | return getConstPool()->Integers16.getOrAdd(this, ConstantInt16); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | Constant *GlobalContext::getConstantInt32(int32_t ConstantInt32) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 363 | return getConstPool()->Integers32.getOrAdd(this, ConstantInt32); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | Constant *GlobalContext::getConstantInt64(int64_t ConstantInt64) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 367 | return getConstPool()->Integers64.getOrAdd(this, ConstantInt64); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | Constant *GlobalContext::getConstantFloat(float ConstantFloat) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 371 | return getConstPool()->Floats.getOrAdd(this, ConstantFloat); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | Constant *GlobalContext::getConstantDouble(double ConstantDouble) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 375 | return getConstPool()->Doubles.getOrAdd(this, ConstantDouble); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 378 | Constant *GlobalContext::getConstantSym(RelocOffsetT Offset, |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 379 | const IceString &Name, |
| 380 | bool SuppressMangling) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 381 | return getConstPool()->Relocatables.getOrAdd( |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 382 | this, RelocatableTuple(Offset, Name, SuppressMangling)); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 385 | Constant *GlobalContext::getConstantUndef(Type Ty) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 386 | return getConstPool()->Undefs.getOrAdd(this, Ty); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 389 | // All locking is done by the getConstant*() target function. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 390 | Constant *GlobalContext::getConstantZero(Type Ty) { |
| 391 | switch (Ty) { |
| 392 | case IceType_i1: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 393 | return getConstantInt1(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 394 | case IceType_i8: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 395 | return getConstantInt8(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 396 | case IceType_i16: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 397 | return getConstantInt16(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 398 | case IceType_i32: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 399 | return getConstantInt32(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 400 | case IceType_i64: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 401 | return getConstantInt64(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 402 | case IceType_f32: |
| 403 | return getConstantFloat(0); |
| 404 | case IceType_f64: |
| 405 | return getConstantDouble(0); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 406 | case IceType_v4i1: |
| 407 | case IceType_v8i1: |
| 408 | case IceType_v16i1: |
| 409 | case IceType_v16i8: |
| 410 | case IceType_v8i16: |
| 411 | case IceType_v4i32: |
| 412 | case IceType_v4f32: { |
| 413 | IceString Str; |
| 414 | llvm::raw_string_ostream BaseOS(Str); |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 415 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 416 | llvm_unreachable(BaseOS.str().c_str()); |
| 417 | } break; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 418 | case IceType_void: |
| 419 | case IceType_NUM: |
| 420 | break; |
| 421 | } |
| 422 | llvm_unreachable("Unknown type"); |
| 423 | } |
| 424 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 425 | ConstantList GlobalContext::getConstantPool(Type Ty) { |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 426 | switch (Ty) { |
| 427 | case IceType_i1: |
| 428 | case IceType_i8: |
| 429 | case IceType_i16: |
| 430 | case IceType_i32: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 431 | return getConstPool()->Integers32.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 432 | case IceType_i64: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 433 | return getConstPool()->Integers64.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 434 | case IceType_f32: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 435 | return getConstPool()->Floats.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 436 | case IceType_f64: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 437 | return getConstPool()->Doubles.getConstantPool(); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 438 | case IceType_v4i1: |
| 439 | case IceType_v8i1: |
| 440 | case IceType_v16i1: |
| 441 | case IceType_v16i8: |
| 442 | case IceType_v8i16: |
| 443 | case IceType_v4i32: |
| 444 | case IceType_v4f32: { |
| 445 | IceString Str; |
| 446 | llvm::raw_string_ostream BaseOS(Str); |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 447 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 448 | llvm_unreachable(BaseOS.str().c_str()); |
| 449 | } break; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 450 | case IceType_void: |
| 451 | case IceType_NUM: |
| 452 | break; |
| 453 | } |
| 454 | llvm_unreachable("Unknown type"); |
| 455 | } |
| 456 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 457 | TimerStackIdT GlobalContext::newTimerStackID(const IceString &Name) { |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 458 | if (!ALLOW_DUMP) |
| 459 | return 0; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 460 | auto Timers = getTimers(); |
| 461 | TimerStackIdT NewID = Timers->size(); |
| 462 | Timers->push_back(TimerStack(Name)); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 463 | return NewID; |
| 464 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 465 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 466 | TimerIdT GlobalContext::getTimerID(TimerStackIdT StackID, |
| 467 | const IceString &Name) { |
| 468 | auto Timers = getTimers(); |
| 469 | assert(StackID < Timers->size()); |
| 470 | return Timers->at(StackID).getTimerID(Name); |
| 471 | } |
| 472 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 473 | void GlobalContext::pushTimer(TimerIdT ID, TimerStackIdT StackID) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 474 | auto Timers = getTimers(); |
| 475 | assert(StackID < Timers->size()); |
| 476 | Timers->at(StackID).push(ID); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void GlobalContext::popTimer(TimerIdT ID, TimerStackIdT StackID) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 480 | auto Timers = getTimers(); |
| 481 | assert(StackID < Timers->size()); |
| 482 | Timers->at(StackID).pop(ID); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 483 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 484 | |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 485 | void GlobalContext::resetTimer(TimerStackIdT StackID) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 486 | auto Timers = getTimers(); |
| 487 | assert(StackID < Timers->size()); |
| 488 | Timers->at(StackID).reset(); |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void GlobalContext::setTimerName(TimerStackIdT StackID, |
| 492 | const IceString &NewName) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 493 | auto Timers = getTimers(); |
| 494 | assert(StackID < Timers->size()); |
| 495 | Timers->at(StackID).setName(NewName); |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 498 | void GlobalContext::dumpStats(const IceString &Name, bool Final) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 499 | if (!ALLOW_DUMP || !getFlags().DumpStats) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 500 | return; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 501 | OstreamLocker OL(this); |
| 502 | if (Final) { |
| 503 | getStatsCumulative()->dump(Name, getStrDump()); |
| 504 | } else { |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame^] | 505 | ICE_TLS_GET_FIELD(TLS)->StatsFunction.dump(Name, getStrDump()); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 506 | getStatsCumulative()->dump("_TOTAL_", getStrDump()); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 510 | void GlobalContext::dumpTimers(TimerStackIdT StackID, bool DumpCumulative) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 511 | if (!ALLOW_DUMP) |
| 512 | return; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 513 | auto Timers = getTimers(); |
| 514 | assert(Timers->size() > StackID); |
| 515 | OstreamLocker L(this); |
| 516 | Timers->at(StackID).dump(getStrDump(), DumpCumulative); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | TimerMarker::TimerMarker(TimerIdT ID, const Cfg *Func) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 520 | : ID(ID), Ctx(Func->getContext()), Active(false) { |
| 521 | if (ALLOW_DUMP) { |
| 522 | Active = Func->getFocusedTiming() || Ctx->getFlags().SubzeroTimingEnabled; |
| 523 | if (Active) |
| 524 | Ctx->pushTimer(ID); |
| 525 | } |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 526 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 527 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame^] | 528 | ICE_TLS_DEFINE_FIELD(GlobalContext::ThreadContext *, GlobalContext, TLS); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 529 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 530 | } // end of namespace Ice |