Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 15 #include "IceDefs.h" | |
| 16 #include "IceTypes.h" | |
| 17 #include "IceCfg.h" | |
| 18 #include "IceGlobalContext.h" | |
| 19 #include "IceOperand.h" | |
| 20 | |
| 21 namespace Ice { | |
| 22 | |
| 23 // TypePool maps constants of type KeyType (e.g. float) to pointers to | |
| 24 // type ValueType (e.g. ConstantFloat). KeyType values are compared | |
| 25 // using memcmp() because of potential NaN values in KeyType values. | |
| 26 // KeyTypeHasFP indicates whether KeyType is a floating-point type | |
| 27 // whose values need to be compared using memcmp() for NaN | |
| 28 // correctness. TODO: use std::is_floating_point<KeyType> instead of | |
| 29 // KeyTypeHasFP with C++11. | |
| 30 template <typename KeyType, typename ValueType, bool KeyTypeHasFP = false> | |
| 31 class TypePool { | |
| 32 TypePool(const TypePool &) LLVM_DELETED_FUNCTION; | |
| 33 TypePool &operator=(const TypePool &) LLVM_DELETED_FUNCTION; | |
| 34 | |
| 35 public: | |
| 36 TypePool() {} | |
| 37 ValueType *getOrAdd(GlobalContext *Ctx, Type Ty, KeyType Key) { | |
| 38 TupleType TupleKey = std::make_pair(Ty, Key); | |
| 39 typename ContainerType::const_iterator Iter = Pool.find(TupleKey); | |
| 40 if (Iter != Pool.end()) | |
| 41 return Iter->second; | |
| 42 ValueType *Result = ValueType::create(Ctx, Ty, Key); | |
| 43 Pool[TupleKey] = Result; | |
| 44 return Result; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 typedef std::pair<Type, KeyType> TupleType; | |
| 49 struct TupleCompare { | |
| 50 bool operator()(const TupleType &A, const TupleType &B) { | |
| 51 if (A.first != B.first) | |
|
JF
2014/04/26 20:20:56
.first could also potentially be FP, so you should
Jim Stichnoth
2014/04/27 15:04:57
No - A.first and B.first are of type "enum Type" (
| |
| 52 return A.first < B.first; | |
| 53 if (KeyTypeHasFP) | |
| 54 return memcmp(&A.second, &B.second, sizeof(KeyType)) < 0; | |
| 55 return A.second < B.second; | |
| 56 } | |
| 57 }; | |
| 58 typedef std::map<const TupleType, ValueType *, TupleCompare> ContainerType; | |
| 59 ContainerType Pool; | |
| 60 }; | |
| 61 | |
| 62 // The global constant pool bundles individual pools of each type of | |
| 63 // interest. | |
| 64 class ConstantPool { | |
| 65 ConstantPool(const ConstantPool &) LLVM_DELETED_FUNCTION; | |
| 66 ConstantPool &operator=(const ConstantPool &) LLVM_DELETED_FUNCTION; | |
| 67 | |
| 68 public: | |
| 69 ConstantPool() {} | |
| 70 TypePool<float, ConstantFloat, true> Floats; | |
| 71 TypePool<double, ConstantDouble, true> Doubles; | |
| 72 TypePool<uint64_t, ConstantInteger> Integers; | |
| 73 TypePool<RelocatableTuple, ConstantRelocatable> Relocatables; | |
| 74 }; | |
| 75 | |
| 76 GlobalContext::GlobalContext(llvm::raw_ostream *OsDump, | |
| 77 llvm::raw_ostream *OsEmit, VerboseMask Mask, | |
| 78 IceString TestPrefix) | |
| 79 : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask), | |
| 80 ConstPool(new ConstantPool()), TestPrefix(TestPrefix) {} | |
| 81 | |
| 82 // In this context, name mangling means to rewrite a symbol using a | |
| 83 // given prefix. For a C++ symbol, nest the original symbol inside | |
| 84 // the "prefix" namespace. For other symbols, just prepend the | |
| 85 // prefix. | |
| 86 IceString GlobalContext::mangleName(const IceString &Name) const { | |
| 87 // TODO: Add explicit tests (beyond the implicit tests in the linker | |
| 88 // that come from the cross tests). | |
| 89 // | |
| 90 // An already-nested name like foo::bar() gets pushed down one | |
| 91 // level, making it equivalent to Prefix::foo::bar(). | |
| 92 // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz | |
| 93 // A non-nested but mangled name like bar() gets nested, making it | |
| 94 // equivalent to Prefix::bar(). | |
| 95 // _Z3barxyz ==> ZN6Prefix3barExyz | |
| 96 // An unmangled, extern "C" style name, gets a simple prefix: | |
| 97 // bar ==> Prefixbar | |
| 98 if (getTestPrefix().empty()) | |
| 99 return Name; | |
| 100 | |
| 101 unsigned PrefixLength = getTestPrefix().length(); | |
| 102 char NameBase[1 + Name.length()]; | |
| 103 const size_t BufLen = 30 + Name.length() + getTestPrefix().length(); | |
| 104 char NewName[BufLen]; | |
| 105 uint32_t BaseLength = 0; | |
| 106 | |
| 107 int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase); | |
| 108 if (ItemsParsed == 1) { | |
| 109 // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz | |
| 110 // (splice in "6Prefix") ^^^^^^^ | |
| 111 snprintf(NewName, BufLen, "_ZN%u%s%s", PrefixLength, | |
| 112 getTestPrefix().c_str(), NameBase); | |
| 113 // We ignore the snprintf return value (here and below). If we | |
| 114 // somehow miscalculated the output buffer length, the output will | |
| 115 // be truncated, but it will be truncated consistently for all | |
| 116 // mangleName() calls on the same input string. | |
| 117 return NewName; | |
| 118 } | |
| 119 | |
| 120 ItemsParsed = sscanf(Name.c_str(), "_Z%u%s", &BaseLength, NameBase); | |
| 121 if (ItemsParsed == 2) { | |
| 122 // Transform _Z3barxyz ==> ZN6Prefix3barExyz | |
| 123 // ^^^^^^^^ ^ | |
| 124 // (splice in "N6Prefix", and insert "E" after "3bar") | |
| 125 char OrigName[Name.length()]; | |
| 126 char OrigSuffix[Name.length()]; | |
| 127 strncpy(OrigName, NameBase, BaseLength); | |
| 128 OrigName[BaseLength] = '\0'; | |
| 129 strcpy(OrigSuffix, NameBase + BaseLength); | |
| 130 snprintf(NewName, BufLen, "_ZN%u%s%u%sE%s", PrefixLength, | |
| 131 getTestPrefix().c_str(), BaseLength, OrigName, OrigSuffix); | |
| 132 return NewName; | |
| 133 } | |
| 134 | |
| 135 // Transform bar ==> Prefixbar | |
| 136 // ^^^^^^ | |
| 137 return getTestPrefix() + Name; | |
| 138 } | |
| 139 | |
| 140 GlobalContext::~GlobalContext() {} | |
| 141 | |
| 142 Constant *GlobalContext::getConstantInt(Type Ty, uint64_t ConstantInt64) { | |
| 143 return ConstPool->Integers.getOrAdd(this, Ty, ConstantInt64); | |
| 144 } | |
| 145 | |
| 146 Constant *GlobalContext::getConstantFloat(float ConstantFloat) { | |
| 147 return ConstPool->Floats.getOrAdd(this, IceType_f32, ConstantFloat); | |
| 148 } | |
| 149 | |
| 150 Constant *GlobalContext::getConstantDouble(double ConstantDouble) { | |
| 151 return ConstPool->Doubles.getOrAdd(this, IceType_f64, ConstantDouble); | |
| 152 } | |
| 153 | |
| 154 Constant *GlobalContext::getConstantSym(Type Ty, int64_t Offset, | |
| 155 const IceString &Name, | |
| 156 bool SuppressMangling) { | |
| 157 return ConstPool->Relocatables.getOrAdd( | |
| 158 this, Ty, RelocatableTuple(Offset, Name, SuppressMangling)); | |
| 159 } | |
| 160 | |
| 161 void Timer::printElapsedUs(GlobalContext *Ctx, const IceString &Tag) const { | |
| 162 if (Ctx->isVerbose(IceV_Timing)) { | |
| 163 // Prefixing with '#' allows timing strings to be included | |
| 164 // without error in textual assembly output. | |
| 165 Ctx->getStrDump() << "# " << getElapsedUs() << " usec " << Tag << "\n"; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 } // end of namespace Ice | |
| OLD | NEW |