Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceGlobalContext.cpp - Global context defs ---*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines aspects of the compilation that persist across |
| 11 | // multiple functions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame^] | 15 | #include <ctype.h> // isdigit() |
| 16 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 17 | #include "IceDefs.h" |
| 18 | #include "IceTypes.h" |
| 19 | #include "IceCfg.h" |
| 20 | #include "IceGlobalContext.h" |
| 21 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 22 | #include "IceTargetLowering.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 23 | |
| 24 | namespace Ice { |
| 25 | |
| 26 | // TypePool maps constants of type KeyType (e.g. float) to pointers to |
| 27 | // type ValueType (e.g. ConstantFloat). KeyType values are compared |
| 28 | // using memcmp() because of potential NaN values in KeyType values. |
| 29 | // KeyTypeHasFP indicates whether KeyType is a floating-point type |
| 30 | // whose values need to be compared using memcmp() for NaN |
| 31 | // correctness. TODO: use std::is_floating_point<KeyType> instead of |
| 32 | // KeyTypeHasFP with C++11. |
| 33 | template <typename KeyType, typename ValueType, bool KeyTypeHasFP = false> |
| 34 | class TypePool { |
| 35 | TypePool(const TypePool &) LLVM_DELETED_FUNCTION; |
| 36 | TypePool &operator=(const TypePool &) LLVM_DELETED_FUNCTION; |
| 37 | |
| 38 | public: |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 39 | TypePool() : NextPoolID(0) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 40 | ValueType *getOrAdd(GlobalContext *Ctx, Type Ty, KeyType Key) { |
| 41 | TupleType TupleKey = std::make_pair(Ty, Key); |
| 42 | typename ContainerType::const_iterator Iter = Pool.find(TupleKey); |
| 43 | if (Iter != Pool.end()) |
| 44 | return Iter->second; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 45 | ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 46 | Pool[TupleKey] = Result; |
| 47 | return Result; |
| 48 | } |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 49 | ConstantList getConstantPool() const { |
| 50 | ConstantList Constants; |
| 51 | Constants.reserve(Pool.size()); |
| 52 | // TODO: replace the loop with std::transform + lambdas. |
| 53 | for (typename ContainerType::const_iterator I = Pool.begin(), |
| 54 | E = Pool.end(); |
| 55 | I != E; ++I) { |
| 56 | Constants.push_back(I->second); |
| 57 | } |
| 58 | return Constants; |
| 59 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | typedef std::pair<Type, KeyType> TupleType; |
| 63 | struct TupleCompare { |
| 64 | bool operator()(const TupleType &A, const TupleType &B) { |
| 65 | if (A.first != B.first) |
| 66 | return A.first < B.first; |
| 67 | if (KeyTypeHasFP) |
| 68 | return memcmp(&A.second, &B.second, sizeof(KeyType)) < 0; |
| 69 | return A.second < B.second; |
| 70 | } |
| 71 | }; |
| 72 | typedef std::map<const TupleType, ValueType *, TupleCompare> ContainerType; |
| 73 | ContainerType Pool; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 74 | uint32_t NextPoolID; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | // The global constant pool bundles individual pools of each type of |
| 78 | // interest. |
| 79 | class ConstantPool { |
| 80 | ConstantPool(const ConstantPool &) LLVM_DELETED_FUNCTION; |
| 81 | ConstantPool &operator=(const ConstantPool &) LLVM_DELETED_FUNCTION; |
| 82 | |
| 83 | public: |
| 84 | ConstantPool() {} |
| 85 | TypePool<float, ConstantFloat, true> Floats; |
| 86 | TypePool<double, ConstantDouble, true> Doubles; |
| 87 | TypePool<uint64_t, ConstantInteger> Integers; |
| 88 | TypePool<RelocatableTuple, ConstantRelocatable> Relocatables; |
| 89 | }; |
| 90 | |
| 91 | GlobalContext::GlobalContext(llvm::raw_ostream *OsDump, |
| 92 | llvm::raw_ostream *OsEmit, VerboseMask Mask, |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 93 | TargetArch Arch, OptLevel Opt, |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 94 | IceString TestPrefix) |
| 95 | : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask), |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 96 | ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt), |
| 97 | TestPrefix(TestPrefix), HasEmittedFirstMethod(false) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 98 | |
| 99 | // In this context, name mangling means to rewrite a symbol using a |
| 100 | // given prefix. For a C++ symbol, nest the original symbol inside |
| 101 | // the "prefix" namespace. For other symbols, just prepend the |
| 102 | // prefix. |
| 103 | IceString GlobalContext::mangleName(const IceString &Name) const { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 104 | // An already-nested name like foo::bar() gets pushed down one |
| 105 | // level, making it equivalent to Prefix::foo::bar(). |
| 106 | // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz |
| 107 | // A non-nested but mangled name like bar() gets nested, making it |
| 108 | // equivalent to Prefix::bar(). |
| 109 | // _Z3barxyz ==> ZN6Prefix3barExyz |
| 110 | // An unmangled, extern "C" style name, gets a simple prefix: |
| 111 | // bar ==> Prefixbar |
| 112 | if (getTestPrefix().empty()) |
| 113 | return Name; |
| 114 | |
| 115 | unsigned PrefixLength = getTestPrefix().length(); |
| 116 | char NameBase[1 + Name.length()]; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 117 | const size_t BufLen = 30 + Name.length() + PrefixLength; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 118 | char NewName[BufLen]; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 119 | uint32_t BaseLength = 0; // using uint32_t due to sscanf format string |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 120 | |
| 121 | int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase); |
| 122 | if (ItemsParsed == 1) { |
| 123 | // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz |
| 124 | // (splice in "6Prefix") ^^^^^^^ |
| 125 | snprintf(NewName, BufLen, "_ZN%u%s%s", PrefixLength, |
| 126 | getTestPrefix().c_str(), NameBase); |
| 127 | // We ignore the snprintf return value (here and below). If we |
| 128 | // somehow miscalculated the output buffer length, the output will |
| 129 | // be truncated, but it will be truncated consistently for all |
| 130 | // mangleName() calls on the same input string. |
| 131 | return NewName; |
| 132 | } |
| 133 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame^] | 134 | // Artificially limit BaseLength to 9 digits (less than 1 billion) |
| 135 | // because sscanf behavior is undefined on integer overflow. If |
| 136 | // there are more than 9 digits (which we test by looking at the |
| 137 | // beginning of NameBase), then we consider this a failure to parse |
| 138 | // a namespace mangling, and fall back to the simple prefixing. |
| 139 | ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase); |
| 140 | if (ItemsParsed == 2 && BaseLength <= strlen(NameBase) && |
| 141 | !isdigit(NameBase[0])) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 142 | // Transform _Z3barxyz ==> _ZN6Prefix3barExyz |
| 143 | // ^^^^^^^^ ^ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 144 | // (splice in "N6Prefix", and insert "E" after "3bar") |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 145 | // But an "I" after the identifier indicates a template argument |
| 146 | // list terminated with "E"; insert the new "E" before/after the |
| 147 | // old "E". E.g.: |
| 148 | // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz |
| 149 | // ^^^^^^^^ ^ |
| 150 | // (splice in "N6Prefix", and insert "E" after "3barIabcE") |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 151 | char OrigName[Name.length()]; |
| 152 | char OrigSuffix[Name.length()]; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 153 | uint32_t ActualBaseLength = BaseLength; |
| 154 | if (NameBase[ActualBaseLength] == 'I') { |
| 155 | ++ActualBaseLength; |
| 156 | while (NameBase[ActualBaseLength] != 'E' && |
| 157 | NameBase[ActualBaseLength] != '\0') |
| 158 | ++ActualBaseLength; |
| 159 | } |
| 160 | strncpy(OrigName, NameBase, ActualBaseLength); |
| 161 | OrigName[ActualBaseLength] = '\0'; |
| 162 | strcpy(OrigSuffix, NameBase + ActualBaseLength); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 163 | snprintf(NewName, BufLen, "_ZN%u%s%u%sE%s", PrefixLength, |
| 164 | getTestPrefix().c_str(), BaseLength, OrigName, OrigSuffix); |
| 165 | return NewName; |
| 166 | } |
| 167 | |
| 168 | // Transform bar ==> Prefixbar |
| 169 | // ^^^^^^ |
| 170 | return getTestPrefix() + Name; |
| 171 | } |
| 172 | |
| 173 | GlobalContext::~GlobalContext() {} |
| 174 | |
| 175 | Constant *GlobalContext::getConstantInt(Type Ty, uint64_t ConstantInt64) { |
| 176 | return ConstPool->Integers.getOrAdd(this, Ty, ConstantInt64); |
| 177 | } |
| 178 | |
| 179 | Constant *GlobalContext::getConstantFloat(float ConstantFloat) { |
| 180 | return ConstPool->Floats.getOrAdd(this, IceType_f32, ConstantFloat); |
| 181 | } |
| 182 | |
| 183 | Constant *GlobalContext::getConstantDouble(double ConstantDouble) { |
| 184 | return ConstPool->Doubles.getOrAdd(this, IceType_f64, ConstantDouble); |
| 185 | } |
| 186 | |
| 187 | Constant *GlobalContext::getConstantSym(Type Ty, int64_t Offset, |
| 188 | const IceString &Name, |
| 189 | bool SuppressMangling) { |
| 190 | return ConstPool->Relocatables.getOrAdd( |
| 191 | this, Ty, RelocatableTuple(Offset, Name, SuppressMangling)); |
| 192 | } |
| 193 | |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 194 | ConstantList GlobalContext::getConstantPool(Type Ty) const { |
| 195 | switch (Ty) { |
| 196 | case IceType_i1: |
| 197 | case IceType_i8: |
| 198 | case IceType_i16: |
| 199 | case IceType_i32: |
| 200 | case IceType_i64: |
| 201 | return ConstPool->Integers.getConstantPool(); |
| 202 | case IceType_f32: |
| 203 | return ConstPool->Floats.getConstantPool(); |
| 204 | case IceType_f64: |
| 205 | return ConstPool->Doubles.getConstantPool(); |
| 206 | case IceType_void: |
| 207 | case IceType_NUM: |
| 208 | break; |
| 209 | } |
| 210 | llvm_unreachable("Unknown type"); |
| 211 | } |
| 212 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 213 | void Timer::printElapsedUs(GlobalContext *Ctx, const IceString &Tag) const { |
| 214 | if (Ctx->isVerbose(IceV_Timing)) { |
| 215 | // Prefixing with '#' allows timing strings to be included |
| 216 | // without error in textual assembly output. |
| 217 | Ctx->getStrDump() << "# " << getElapsedUs() << " usec " << Tag << "\n"; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | } // end of namespace Ice |