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 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Defines aspects of the compilation that persist across multiple |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 12 | /// functions. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 13 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 16 | #include "IceGlobalContext.h" |
Jim Stichnoth | 639c921 | 2014-12-11 10:04:32 -0800 | [diff] [blame] | 17 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 18 | #include "IceCfg.h" |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 19 | #include "IceCfgNode.h" |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 20 | #include "IceClFlags.h" |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 21 | #include "IceClFlagsExtra.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 22 | #include "IceDefs.h" |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 23 | #include "IceELFObjectWriter.h" |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 24 | #include "IceGlobalInits.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 25 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 26 | #include "IceTargetLowering.h" |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 27 | #include "IceTimerTree.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 28 | #include "IceTypes.h" |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 29 | |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 30 | #ifdef __clang__ |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 31 | #pragma clang diagnostic push |
| 32 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 33 | #endif // __clang__ |
| 34 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 36 | |
| 37 | #ifdef __clang__ |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 38 | #pragma clang diagnostic pop |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 39 | #endif // __clang__ |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 40 | |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 41 | #include <algorithm> // max() |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 42 | |
Jim Stichnoth | dddaf9c | 2014-12-04 14:09:21 -0800 | [diff] [blame] | 43 | namespace std { |
| 44 | template <> struct hash<Ice::RelocatableTuple> { |
| 45 | size_t operator()(const Ice::RelocatableTuple &Key) const { |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 46 | if (!Key.EmitString.empty()) { |
| 47 | return hash<Ice::IceString>()(Key.EmitString); |
| 48 | } |
| 49 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 50 | // If there's no emit string, then we use the relocatable's name, plus the |
| 51 | // hash of a combination of the number of OffsetExprs and the known, fixed |
| 52 | // offset for the reloc. We left shift the known relocatable by 5 trying to |
| 53 | // minimize the interaction between the bits in OffsetExpr.size() and |
| 54 | // Key.Offset. |
Jim Stichnoth | dddaf9c | 2014-12-04 14:09:21 -0800 | [diff] [blame] | 55 | return hash<Ice::IceString>()(Key.Name) + |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 56 | hash<std::size_t>()(Key.OffsetExpr.size() + (Key.Offset << 5)); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 57 | } |
| 58 | }; |
Jim Stichnoth | dddaf9c | 2014-12-04 14:09:21 -0800 | [diff] [blame] | 59 | } // end of namespace std |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 60 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 61 | namespace Ice { |
| 62 | |
Jim Stichnoth | 5bfe215 | 2015-03-19 13:51:56 -0700 | [diff] [blame] | 63 | namespace { |
| 64 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 65 | // Define the key comparison function for the constant pool's unordered_map, |
| 66 | // but only for key types of interest: integer types, floating point types, and |
| 67 | // the special RelocatableTuple. |
Jim Stichnoth | 5bfe215 | 2015-03-19 13:51:56 -0700 | [diff] [blame] | 68 | template <typename KeyType, class Enable = void> struct KeyCompare {}; |
| 69 | |
| 70 | template <typename KeyType> |
| 71 | struct KeyCompare<KeyType, |
| 72 | typename std::enable_if< |
| 73 | std::is_integral<KeyType>::value || |
| 74 | std::is_same<KeyType, RelocatableTuple>::value>::type> { |
| 75 | bool operator()(const KeyType &Value1, const KeyType &Value2) const { |
| 76 | return Value1 == Value2; |
| 77 | } |
| 78 | }; |
| 79 | template <typename KeyType> |
| 80 | struct KeyCompare<KeyType, typename std::enable_if< |
| 81 | std::is_floating_point<KeyType>::value>::type> { |
| 82 | bool operator()(const KeyType &Value1, const KeyType &Value2) const { |
| 83 | return !memcmp(&Value1, &Value2, sizeof(KeyType)); |
| 84 | } |
| 85 | }; |
| 86 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 87 | // Define a key comparison function for sorting the constant pool's values |
| 88 | // after they are dumped to a vector. This covers integer types, floating point |
| 89 | // types, and ConstantRelocatable values. |
Jim Stichnoth | 6e293c8 | 2015-04-09 09:11:18 -0700 | [diff] [blame] | 90 | template <typename ValueType, class Enable = void> struct KeyCompareLess {}; |
| 91 | |
| 92 | template <typename ValueType> |
| 93 | struct KeyCompareLess<ValueType, |
| 94 | typename std::enable_if<std::is_floating_point< |
| 95 | typename ValueType::PrimType>::value>::type> { |
| 96 | bool operator()(const Constant *Const1, const Constant *Const2) const { |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 97 | using CompareType = uint64_t; |
Jim Stichnoth | 6e293c8 | 2015-04-09 09:11:18 -0700 | [diff] [blame] | 98 | static_assert(sizeof(typename ValueType::PrimType) <= sizeof(CompareType), |
| 99 | "Expected floating-point type of width 64-bit or less"); |
| 100 | typename ValueType::PrimType V1 = llvm::cast<ValueType>(Const1)->getValue(); |
| 101 | typename ValueType::PrimType V2 = llvm::cast<ValueType>(Const2)->getValue(); |
| 102 | // We avoid "V1<V2" because of NaN. |
| 103 | // We avoid "memcmp(&V1,&V2,sizeof(V1))<0" which depends on the |
| 104 | // endian-ness of the host system running Subzero. |
| 105 | // Instead, compare the result of bit_cast to uint64_t. |
| 106 | uint64_t I1 = 0, I2 = 0; |
| 107 | memcpy(&I1, &V1, sizeof(V1)); |
| 108 | memcpy(&I2, &V2, sizeof(V2)); |
| 109 | return I1 < I2; |
| 110 | } |
| 111 | }; |
| 112 | template <typename ValueType> |
| 113 | struct KeyCompareLess<ValueType, |
| 114 | typename std::enable_if<std::is_integral< |
| 115 | typename ValueType::PrimType>::value>::type> { |
| 116 | bool operator()(const Constant *Const1, const Constant *Const2) const { |
| 117 | typename ValueType::PrimType V1 = llvm::cast<ValueType>(Const1)->getValue(); |
| 118 | typename ValueType::PrimType V2 = llvm::cast<ValueType>(Const2)->getValue(); |
| 119 | return V1 < V2; |
| 120 | } |
| 121 | }; |
| 122 | template <typename ValueType> |
| 123 | struct KeyCompareLess< |
| 124 | ValueType, typename std::enable_if< |
| 125 | std::is_same<ValueType, ConstantRelocatable>::value>::type> { |
| 126 | bool operator()(const Constant *Const1, const Constant *Const2) const { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 127 | auto *V1 = llvm::cast<ValueType>(Const1); |
| 128 | auto *V2 = llvm::cast<ValueType>(Const2); |
Jim Stichnoth | 6e293c8 | 2015-04-09 09:11:18 -0700 | [diff] [blame] | 129 | if (V1->getName() == V2->getName()) |
| 130 | return V1->getOffset() < V2->getOffset(); |
| 131 | return V1->getName() < V2->getName(); |
| 132 | } |
| 133 | }; |
| 134 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 135 | // TypePool maps constants of type KeyType (e.g. float) to pointers to |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 136 | // type ValueType (e.g. ConstantFloat). |
| 137 | template <Type Ty, typename KeyType, typename ValueType> class TypePool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 138 | TypePool(const TypePool &) = delete; |
| 139 | TypePool &operator=(const TypePool &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 140 | |
| 141 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 142 | TypePool() = default; |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 143 | ValueType *getOrAdd(GlobalContext *Ctx, KeyType Key) { |
| 144 | auto Iter = Pool.find(Key); |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 145 | if (Iter != Pool.end()) { |
| 146 | Iter->second->updateLookupCount(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 147 | return Iter->second; |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 148 | } |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 149 | auto *Result = ValueType::create(Ctx, Ty, Key); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 150 | Pool[Key] = Result; |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 151 | Result->updateLookupCount(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 152 | return Result; |
| 153 | } |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 154 | ConstantList getConstantPool() const { |
| 155 | ConstantList Constants; |
| 156 | Constants.reserve(Pool.size()); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 157 | for (auto &I : Pool) |
| 158 | Constants.push_back(I.second); |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 159 | // The sort (and its KeyCompareLess machinery) is not strictly necessary, |
| 160 | // but is desirable for producing output that is deterministic across |
| 161 | // unordered_map::iterator implementations. |
Jim Stichnoth | 6e293c8 | 2015-04-09 09:11:18 -0700 | [diff] [blame] | 162 | std::sort(Constants.begin(), Constants.end(), KeyCompareLess<ValueType>()); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 163 | return Constants; |
| 164 | } |
Jim Stichnoth | 3d5e08d | 2016-03-01 12:22:29 -0800 | [diff] [blame] | 165 | size_t size() const { return Pool.size(); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 166 | |
| 167 | private: |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 168 | // Use the default hash function, and a custom key comparison function. The |
| 169 | // key comparison function for floating point variables can't use the default |
| 170 | // == based implementation because of special C++ semantics regarding +0.0, |
| 171 | // -0.0, and NaN comparison. However, it's OK to use the default hash for |
| 172 | // floating point values because KeyCompare is the final source of truth - in |
| 173 | // the worst case a "false" collision must be resolved. |
| 174 | using ContainerType = |
| 175 | std::unordered_map<KeyType, ValueType *, std::hash<KeyType>, |
| 176 | KeyCompare<KeyType>>; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 177 | ContainerType Pool; |
| 178 | }; |
| 179 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 180 | // UndefPool maps ICE types to the corresponding ConstantUndef values. |
| 181 | class UndefPool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 182 | UndefPool(const UndefPool &) = delete; |
| 183 | UndefPool &operator=(const UndefPool &) = delete; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 184 | |
| 185 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 186 | UndefPool() : Pool(IceType_NUM) {} |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 187 | |
| 188 | ConstantUndef *getOrAdd(GlobalContext *Ctx, Type Ty) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 189 | if (Pool[Ty] == nullptr) |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 190 | Pool[Ty] = ConstantUndef::create(Ctx, Ty); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 191 | return Pool[Ty]; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 195 | std::vector<ConstantUndef *> Pool; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Jim Stichnoth | 5bfe215 | 2015-03-19 13:51:56 -0700 | [diff] [blame] | 198 | } // end of anonymous namespace |
| 199 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 200 | // The global constant pool bundles individual pools of each type of |
| 201 | // interest. |
| 202 | class ConstantPool { |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 203 | ConstantPool(const ConstantPool &) = delete; |
| 204 | ConstantPool &operator=(const ConstantPool &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 205 | |
| 206 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 207 | ConstantPool() = default; |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 208 | TypePool<IceType_f32, float, ConstantFloat> Floats; |
| 209 | TypePool<IceType_f64, double, ConstantDouble> Doubles; |
| 210 | TypePool<IceType_i1, int8_t, ConstantInteger32> Integers1; |
| 211 | TypePool<IceType_i8, int8_t, ConstantInteger32> Integers8; |
| 212 | TypePool<IceType_i16, int16_t, ConstantInteger32> Integers16; |
| 213 | TypePool<IceType_i32, int32_t, ConstantInteger32> Integers32; |
| 214 | TypePool<IceType_i64, int64_t, ConstantInteger64> Integers64; |
| 215 | TypePool<IceType_i32, RelocatableTuple, ConstantRelocatable> Relocatables; |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 216 | TypePool<IceType_i32, RelocatableTuple, ConstantRelocatable> |
| 217 | ExternRelocatables; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 218 | UndefPool Undefs; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
Jim Stichnoth | 3d5e08d | 2016-03-01 12:22:29 -0800 | [diff] [blame] | 221 | void GlobalContext::CodeStats::dump(const IceString &Name, GlobalContext *Ctx) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 222 | if (!BuildDefs::dump()) |
Jim Stichnoth | 639c921 | 2014-12-11 10:04:32 -0800 | [diff] [blame] | 223 | return; |
Jim Stichnoth | 3d5e08d | 2016-03-01 12:22:29 -0800 | [diff] [blame] | 224 | OstreamLocker _(Ctx); |
| 225 | Ostream &Str = Ctx->getStrDump(); |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 226 | #define X(str, tag) \ |
| 227 | Str << "|" << Name << "|" str "|" << Stats[CS_##tag] << "\n"; |
| 228 | CODESTATS_TABLE |
| 229 | #undef X |
| 230 | Str << "|" << Name << "|Spills+Fills|" |
| 231 | << Stats[CS_NumSpills] + Stats[CS_NumFills] << "\n"; |
Jim Stichnoth | 639c921 | 2014-12-11 10:04:32 -0800 | [diff] [blame] | 232 | Str << "|" << Name << "|Memory Usage|"; |
| 233 | if (ssize_t MemUsed = llvm::TimeRecord::getCurrentTime(false).getMemUsed()) |
| 234 | Str << MemUsed; |
| 235 | else |
| 236 | Str << "(requires '-track-memory')"; |
| 237 | Str << "\n"; |
Jim Stichnoth | 3d5e08d | 2016-03-01 12:22:29 -0800 | [diff] [blame] | 238 | Str << "|" << Name << "|CPool Sizes "; |
| 239 | { |
| 240 | auto Pool = Ctx->getConstPool(); |
| 241 | Str << "|f32=" << Pool->Floats.size(); |
| 242 | Str << "|f64=" << Pool->Doubles.size(); |
| 243 | Str << "|i1=" << Pool->Integers1.size(); |
| 244 | Str << "|i8=" << Pool->Integers8.size(); |
| 245 | Str << "|i16=" << Pool->Integers16.size(); |
| 246 | Str << "|i32=" << Pool->Integers32.size(); |
| 247 | Str << "|i64=" << Pool->Integers64.size(); |
| 248 | Str << "|Rel=" << Pool->Relocatables.size(); |
| 249 | Str << "|ExtRel=" << Pool->ExternRelocatables.size(); |
| 250 | } |
| 251 | Str << "\n"; |
Jim Stichnoth | 639c921 | 2014-12-11 10:04:32 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 254 | GlobalContext::GlobalContext(Ostream *OsDump, Ostream *OsEmit, Ostream *OsError, |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 255 | ELFStreamer *ELFStr) |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 256 | : ConstPool(new ConstantPool()), ErrorStatus(), StrDump(OsDump), |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 257 | StrEmit(OsEmit), StrError(OsError), ObjectWriter(), |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 258 | OptQ(/*Sequential=*/Flags.isSequential(), |
| 259 | /*MaxSize=*/Flags.getNumTranslationThreads()), |
| 260 | // EmitQ is allowed unlimited size. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 261 | EmitQ(/*Sequential=*/Flags.isSequential()), |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 262 | DataLowering(TargetDataLowering::createLowering(this)) { |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 263 | assert(OsDump && "OsDump is not defined for GlobalContext"); |
| 264 | assert(OsEmit && "OsEmit is not defined for GlobalContext"); |
| 265 | assert(OsError && "OsError is not defined for GlobalContext"); |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 266 | // Make sure thread_local fields are properly initialized before any |
| 267 | // accesses are made. Do this here instead of at the start of |
| 268 | // main() so that all clients (e.g. unit tests) can benefit for |
| 269 | // free. |
| 270 | GlobalContext::TlsInit(); |
| 271 | Cfg::TlsInit(); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 272 | // Create a new ThreadContext for the current thread. No need to |
| 273 | // lock AllThreadContexts at this point since no other threads have |
| 274 | // access yet to this GlobalContext object. |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 275 | ThreadContext *MyTLS = new ThreadContext(); |
| 276 | AllThreadContexts.push_back(MyTLS); |
| 277 | ICE_TLS_SET_FIELD(TLS, MyTLS); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 278 | // Pre-register built-in stack names. |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 279 | if (BuildDefs::dump()) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 280 | // TODO(stichnot): There needs to be a strong relationship between |
| 281 | // the newTimerStackID() return values and TSK_Default/TSK_Funcs. |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 282 | newTimerStackID("Total across all functions"); |
| 283 | newTimerStackID("Per-function summary"); |
| 284 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 285 | Timers.initInto(MyTLS->Timers); |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 286 | switch (Flags.getOutFileType()) { |
| 287 | case FT_Elf: |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 288 | ObjectWriter.reset(new ELFObjectWriter(*this, *ELFStr)); |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 289 | break; |
| 290 | case FT_Asm: |
| 291 | case FT_Iasm: |
| 292 | break; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 293 | } |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 294 | |
| 295 | // ProfileBlockInfoVarDecl is initialized here because it takes this as a |
| 296 | // parameter -- we want to |
| 297 | // ensure that at least this' member variables are initialized. |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 298 | ProfileBlockInfoVarDecl = VariableDeclaration::createExternal(this); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 299 | ProfileBlockInfoVarDecl->setAlignment(typeWidthInBytes(IceType_i64)); |
| 300 | ProfileBlockInfoVarDecl->setIsConstant(true); |
| 301 | |
| 302 | // Note: if you change this symbol, make sure to update |
| 303 | // runtime/szrt_profiler.c as well. |
| 304 | ProfileBlockInfoVarDecl->setName("__Sz_block_profile_info"); |
Jim Stichnoth | 94844f1 | 2015-11-04 16:06:16 -0800 | [diff] [blame] | 305 | |
Karl Schimpf | 5403f5d | 2016-01-15 11:07:46 -0800 | [diff] [blame] | 306 | TargetLowering::staticInit(this); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 307 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 308 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 309 | void GlobalContext::translateFunctions() { |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 310 | while (std::unique_ptr<Cfg> Func = optQueueBlockingPop()) { |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 311 | // Install Func in TLS for Cfg-specific container allocators. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 312 | CfgLocalAllocatorScope _(Func.get()); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 313 | // Reset per-function stats being accumulated in TLS. |
| 314 | resetStats(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 315 | // Set verbose level to none if the current function does NOT |
| 316 | // match the -verbose-focus command-line option. |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 317 | if (!matchSymbolName(Func->getFunctionName(), |
| 318 | getFlags().getVerboseFocusOn())) |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 319 | Func->setVerbose(IceV_None); |
| 320 | // Disable translation if -notranslate is specified, or if the |
| 321 | // current function matches the -translate-only option. If |
| 322 | // translation is disabled, just dump the high-level IR and |
| 323 | // continue. |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 324 | if (getFlags().getDisableTranslation() || |
| 325 | !matchSymbolName(Func->getFunctionName(), |
| 326 | getFlags().getTranslateOnly())) { |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 327 | Func->dump(); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 328 | continue; // Func goes out of scope and gets deleted |
| 329 | } |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 330 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 331 | Func->translate(); |
| 332 | EmitterWorkItem *Item = nullptr; |
| 333 | if (Func->hasError()) { |
| 334 | getErrorStatus()->assign(EC_Translation); |
| 335 | OstreamLocker L(this); |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 336 | getStrError() << "ICE translation error: " << Func->getFunctionName() |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 337 | << ": " << Func->getError() << ": " |
| 338 | << Func->getFunctionNameAndSize() << "\n"; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 339 | Item = new EmitterWorkItem(Func->getSequenceNumber()); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 340 | } else { |
Jim Stichnoth | 24824e7 | 2015-02-12 21:35:34 -0800 | [diff] [blame] | 341 | Func->getAssembler<>()->setInternal(Func->getInternal()); |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 342 | switch (getFlags().getOutFileType()) { |
| 343 | case FT_Elf: |
| 344 | case FT_Iasm: { |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 345 | Func->emitIAS(); |
| 346 | // The Cfg has already emitted into the assembly buffer, so |
| 347 | // stats have been fully collected into this thread's TLS. |
| 348 | // Dump them before TLS is reset for the next Cfg. |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 349 | dumpStats(Func->getFunctionNameAndSize()); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 350 | Assembler *Asm = Func->releaseAssembler(); |
| 351 | // Copy relevant fields into Asm before Func is deleted. |
| 352 | Asm->setFunctionName(Func->getFunctionName()); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 353 | Item = new EmitterWorkItem(Func->getSequenceNumber(), Asm); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 354 | Item->setGlobalInits(Func->getGlobalInits()); |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 355 | } break; |
| 356 | case FT_Asm: |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 357 | // The Cfg has not been emitted yet, so stats are not ready |
| 358 | // to be dumped. |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 359 | std::unique_ptr<VariableDeclarationList> GlobalInits = |
| 360 | Func->getGlobalInits(); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 361 | Item = new EmitterWorkItem(Func->getSequenceNumber(), Func.release()); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 362 | Item->setGlobalInits(std::move(GlobalInits)); |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 363 | break; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 364 | } |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 365 | } |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 366 | assert(Item); |
| 367 | emitQueueBlockingPush(Item); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 368 | // The Cfg now gets deleted as Func goes out of scope. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 372 | namespace { |
| 373 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 374 | // Ensure Pending is large enough that Pending[Index] is valid. |
| 375 | void resizePending(std::vector<EmitterWorkItem *> &Pending, uint32_t Index) { |
| 376 | if (Index >= Pending.size()) |
| 377 | Pending.resize(Index + 1); |
| 378 | } |
| 379 | |
| 380 | } // end of anonymous namespace |
| 381 | |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 382 | void GlobalContext::emitFileHeader() { |
| 383 | TimerMarker T1(Ice::TimerStack::TT_emit, this); |
| 384 | if (getFlags().getOutFileType() == FT_Elf) { |
| 385 | getObjectWriter()->writeInitialELFHeader(); |
| 386 | } else { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 387 | if (!BuildDefs::dump()) { |
Jim Stichnoth | c879968 | 2015-06-22 13:04:10 -0700 | [diff] [blame] | 388 | getStrError() << "emitFileHeader for non-ELF"; |
| 389 | getErrorStatus()->assign(EC_Translation); |
| 390 | } |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 391 | TargetHeaderLowering::createLowering(this)->lower(); |
| 392 | } |
| 393 | } |
| 394 | |
Jim Stichnoth | cac003e | 2015-06-18 12:48:58 -0700 | [diff] [blame] | 395 | void GlobalContext::lowerConstants() { DataLowering->lowerConstants(); } |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 396 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 397 | void GlobalContext::lowerJumpTables() { DataLowering->lowerJumpTables(); } |
| 398 | |
John Porto | 844211e | 2016-02-04 08:42:48 -0800 | [diff] [blame] | 399 | void GlobalContext::addBlockInfoPtrs(VariableDeclaration *ProfileBlockInfo) { |
| 400 | for (const VariableDeclaration *Global : Globals) { |
| 401 | if (Cfg::isProfileGlobal(*Global)) { |
| 402 | constexpr RelocOffsetT BlockExecutionCounterOffset = 0; |
| 403 | ProfileBlockInfo->addInitializer( |
| 404 | VariableDeclaration::RelocInitializer::create( |
| 405 | Global, |
| 406 | {RelocOffset::create(this, BlockExecutionCounterOffset)})); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 411 | void GlobalContext::lowerGlobals(const IceString &SectionSuffix) { |
| 412 | TimerMarker T(TimerStack::TT_emitGlobalInitializers, this); |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 413 | const bool DumpGlobalVariables = BuildDefs::dump() && |
| 414 | (Flags.getVerbose() & IceV_GlobalInit) && |
| 415 | Flags.getVerboseFocusOn().empty(); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 416 | if (DumpGlobalVariables) { |
| 417 | OstreamLocker L(this); |
| 418 | Ostream &Stream = getStrDump(); |
| 419 | for (const Ice::VariableDeclaration *Global : Globals) { |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 420 | Global->dump(Stream); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | if (Flags.getDisableTranslation()) |
| 424 | return; |
| 425 | |
John Porto | 844211e | 2016-02-04 08:42:48 -0800 | [diff] [blame] | 426 | addBlockInfoPtrs(ProfileBlockInfoVarDecl); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 427 | // If we need to shuffle the layout of global variables, shuffle them now. |
| 428 | if (getFlags().shouldReorderGlobalVariables()) { |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 429 | // Create a random number generator for global variable reordering. |
| 430 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), |
| 431 | RPE_GlobalVariableReordering); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 432 | RandomShuffle(Globals.begin(), Globals.end(), |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 433 | [&RNG](int N) { return (uint32_t)RNG.next(N); }); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 434 | } |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 435 | DataLowering->lowerGlobals(Globals, SectionSuffix); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 436 | for (VariableDeclaration *Var : Globals) { |
| 437 | Var->discardInitializers(); |
| 438 | } |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 439 | Globals.clear(); |
| 440 | } |
| 441 | |
| 442 | void GlobalContext::lowerProfileData() { |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 443 | // ProfileBlockInfoVarDecl is initialized in the constructor, and will only |
| 444 | // ever be nullptr after this method completes. This assertion is a convoluted |
| 445 | // way of ensuring lowerProfileData is invoked a single time. |
| 446 | assert(ProfileBlockInfoVarDecl != nullptr); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 447 | // This adds a 64-bit sentinel entry to the end of our array. For 32-bit |
| 448 | // architectures this will waste 4 bytes. |
| 449 | const SizeT Sizeof64BitNullPtr = typeWidthInBytes(IceType_i64); |
| 450 | ProfileBlockInfoVarDecl->addInitializer( |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 451 | VariableDeclaration::ZeroInitializer::create(Sizeof64BitNullPtr)); |
| 452 | Globals.push_back(ProfileBlockInfoVarDecl); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 453 | constexpr char ProfileDataSection[] = "$sz_profiler$"; |
| 454 | lowerGlobals(ProfileDataSection); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 455 | ProfileBlockInfoVarDecl = nullptr; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 458 | void GlobalContext::emitItems() { |
| 459 | const bool Threaded = !getFlags().isSequential(); |
| 460 | // Pending is a vector containing the reassembled, ordered list of |
| 461 | // work items. When we're ready for the next item, we first check |
| 462 | // whether it's in the Pending list. If not, we take an item from |
| 463 | // the work queue, and if it's not the item we're waiting for, we |
| 464 | // insert it into Pending and repeat. The work item is deleted |
| 465 | // after it is processed. |
| 466 | std::vector<EmitterWorkItem *> Pending; |
| 467 | uint32_t DesiredSequenceNumber = getFirstSequenceNumber(); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 468 | uint32_t ShuffleStartIndex = DesiredSequenceNumber; |
| 469 | uint32_t ShuffleEndIndex = DesiredSequenceNumber; |
| 470 | bool EmitQueueEmpty = false; |
| 471 | const uint32_t ShuffleWindowSize = |
| 472 | std::max(1u, getFlags().getReorderFunctionsWindowSize()); |
| 473 | bool Shuffle = Threaded && getFlags().shouldReorderFunctions(); |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 474 | // Create a random number generator for function reordering. |
| 475 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), RPE_FunctionReordering); |
| 476 | |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 477 | while (!EmitQueueEmpty) { |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 478 | resizePending(Pending, DesiredSequenceNumber); |
| 479 | // See if Pending contains DesiredSequenceNumber. |
| 480 | EmitterWorkItem *RawItem = Pending[DesiredSequenceNumber]; |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 481 | if (RawItem == nullptr) { |
| 482 | // We need to fetch an EmitterWorkItem from the queue. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 483 | RawItem = emitQueueBlockingPop(); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 484 | if (RawItem == nullptr) { |
| 485 | // This is the notifier for an empty queue. |
| 486 | EmitQueueEmpty = true; |
| 487 | } else { |
| 488 | // We get an EmitterWorkItem, we need to add it to Pending. |
| 489 | uint32_t ItemSeq = RawItem->getSequenceNumber(); |
| 490 | if (Threaded && ItemSeq != DesiredSequenceNumber) { |
| 491 | // Not the desired one, add it to Pending but do not increase |
| 492 | // DesiredSequenceNumber. Continue the loop, do not emit the item. |
| 493 | resizePending(Pending, ItemSeq); |
| 494 | Pending[ItemSeq] = RawItem; |
| 495 | continue; |
| 496 | } |
| 497 | // ItemSeq == DesiredSequenceNumber, we need to check if we should |
| 498 | // emit it or not. If !Threaded, we're OK with ItemSeq != |
| 499 | // DesiredSequenceNumber. |
| 500 | Pending[DesiredSequenceNumber] = RawItem; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 501 | } |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 502 | } |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 503 | // We have the desired EmitterWorkItem or nullptr as the end notifier. |
| 504 | // If the emitter queue is not empty, increase DesiredSequenceNumber and |
| 505 | // ShuffleEndIndex. |
| 506 | if (!EmitQueueEmpty) { |
| 507 | DesiredSequenceNumber++; |
| 508 | ShuffleEndIndex++; |
| 509 | } |
| 510 | |
| 511 | if (Shuffle) { |
| 512 | // Continue fetching EmitterWorkItem if function reordering is turned on, |
| 513 | // and emit queue is not empty, and the number of consecutive pending |
| 514 | // items is smaller than the window size, and RawItem is not a |
| 515 | // WI_GlobalInits kind. Emit WI_GlobalInits kind block first to avoid |
| 516 | // holding an arbitrarily large GlobalDeclarationList. |
| 517 | if (!EmitQueueEmpty && |
| 518 | ShuffleEndIndex - ShuffleStartIndex < ShuffleWindowSize && |
| 519 | RawItem->getKind() != EmitterWorkItem::WI_GlobalInits) |
| 520 | continue; |
| 521 | |
| 522 | // Emit the EmitterWorkItem between Pending[ShuffleStartIndex] to |
| 523 | // Pending[ShuffleEndIndex]. If function reordering turned on, shuffle the |
| 524 | // pending items from Pending[ShuffleStartIndex] to |
| 525 | // Pending[ShuffleEndIndex]. |
| 526 | RandomShuffle(Pending.begin() + ShuffleStartIndex, |
| 527 | Pending.begin() + ShuffleEndIndex, |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 528 | [&RNG](uint64_t N) { return (uint32_t)RNG.next(N); }); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // Emit the item from ShuffleStartIndex to ShuffleEndIndex. |
| 532 | for (uint32_t I = ShuffleStartIndex; I < ShuffleEndIndex; I++) { |
| 533 | std::unique_ptr<EmitterWorkItem> Item(Pending[I]); |
| 534 | |
| 535 | switch (Item->getKind()) { |
| 536 | case EmitterWorkItem::WI_Nop: |
| 537 | break; |
| 538 | case EmitterWorkItem::WI_GlobalInits: { |
| 539 | accumulateGlobals(Item->getGlobalInits()); |
| 540 | } break; |
| 541 | case EmitterWorkItem::WI_Asm: { |
| 542 | lowerGlobalsIfNoCodeHasBeenSeen(); |
| 543 | accumulateGlobals(Item->getGlobalInits()); |
| 544 | |
| 545 | std::unique_ptr<Assembler> Asm = Item->getAsm(); |
| 546 | Asm->alignFunction(); |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 547 | const IceString &Name = Asm->getFunctionName(); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 548 | switch (getFlags().getOutFileType()) { |
| 549 | case FT_Elf: |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 550 | getObjectWriter()->writeFunctionCode(Name, Asm->getInternal(), |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 551 | Asm.get()); |
| 552 | break; |
| 553 | case FT_Iasm: { |
| 554 | OstreamLocker L(this); |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 555 | Cfg::emitTextHeader(Name, this, Asm.get()); |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 556 | Asm->emitIASBytes(this); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 557 | } break; |
| 558 | case FT_Asm: |
| 559 | llvm::report_fatal_error("Unexpected FT_Asm"); |
| 560 | break; |
| 561 | } |
| 562 | } break; |
| 563 | case EmitterWorkItem::WI_Cfg: { |
| 564 | if (!BuildDefs::dump()) |
| 565 | llvm::report_fatal_error("WI_Cfg work item created inappropriately"); |
| 566 | lowerGlobalsIfNoCodeHasBeenSeen(); |
| 567 | accumulateGlobals(Item->getGlobalInits()); |
| 568 | |
| 569 | assert(getFlags().getOutFileType() == FT_Asm); |
| 570 | std::unique_ptr<Cfg> Func = Item->getCfg(); |
| 571 | // Unfortunately, we have to temporarily install the Cfg in TLS |
| 572 | // because Variable::asType() uses the allocator to create the |
| 573 | // differently-typed copy. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 574 | CfgLocalAllocatorScope _(Func.get()); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 575 | Func->emit(); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 576 | dumpStats(Func->getFunctionNameAndSize()); |
Qining Lu | 7cd5351 | 2015-06-26 09:36:00 -0700 | [diff] [blame] | 577 | } break; |
| 578 | } |
| 579 | } |
| 580 | // Update the start index for next shuffling queue |
| 581 | ShuffleStartIndex = ShuffleEndIndex; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 582 | } |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 583 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 584 | // In case there are no code to be generated, we invoke the conditional |
| 585 | // lowerGlobals again -- this is a no-op if code has been emitted. |
| 586 | lowerGlobalsIfNoCodeHasBeenSeen(); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 589 | GlobalContext::~GlobalContext() { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 590 | llvm::DeleteContainerPointers(AllThreadContexts); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 591 | LockedPtr<DestructorArray> Dtors = getDestructors(); |
| 592 | // Destructors are invoked in the opposite object construction order. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 593 | for (const auto &Dtor : reverse_range(*Dtors)) |
| 594 | Dtor(); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 595 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 596 | |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 597 | void GlobalContext::dumpConstantLookupCounts() { |
| 598 | if (!BuildDefs::dump()) |
| 599 | return; |
| 600 | const bool DumpCounts = (Flags.getVerbose() & IceV_ConstPoolStats) && |
| 601 | Flags.getVerboseFocusOn().empty(); |
| 602 | if (!DumpCounts) |
| 603 | return; |
| 604 | |
| 605 | OstreamLocker _(this); |
| 606 | Ostream &Str = getStrDump(); |
| 607 | Str << "Constant pool use stats: count+value+type\n"; |
| 608 | #define X(WhichPool) \ |
| 609 | for (auto *C : getConstPool()->WhichPool.getConstantPool()) { \ |
| 610 | Str << C->getLookupCount() << " "; \ |
| 611 | C->dump(Str); \ |
| 612 | Str << " " << C->getType() << "\n"; \ |
| 613 | } |
| 614 | X(Integers1); |
| 615 | X(Integers8); |
| 616 | X(Integers16); |
| 617 | X(Integers32); |
| 618 | X(Integers64); |
| 619 | X(Floats); |
| 620 | X(Doubles); |
| 621 | X(Relocatables); |
| 622 | X(ExternRelocatables); |
| 623 | #undef X |
| 624 | } |
| 625 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 626 | // TODO(stichnot): Consider adding thread-local caches of constant pool entries |
| 627 | // to reduce contention. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 628 | |
| 629 | // All locking is done by the getConstantInt[0-9]+() target function. |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 630 | Constant *GlobalContext::getConstantInt(Type Ty, int64_t Value) { |
| 631 | switch (Ty) { |
| 632 | case IceType_i1: |
| 633 | return getConstantInt1(Value); |
| 634 | case IceType_i8: |
| 635 | return getConstantInt8(Value); |
| 636 | case IceType_i16: |
| 637 | return getConstantInt16(Value); |
| 638 | case IceType_i32: |
| 639 | return getConstantInt32(Value); |
| 640 | case IceType_i64: |
| 641 | return getConstantInt64(Value); |
| 642 | default: |
| 643 | llvm_unreachable("Bad integer type for getConstant"); |
| 644 | } |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 645 | return nullptr; |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 648 | Constant *GlobalContext::getConstantInt1(int8_t ConstantInt1) { |
| 649 | ConstantInt1 &= INT8_C(1); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 650 | return getConstPool()->Integers1.getOrAdd(this, ConstantInt1); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | Constant *GlobalContext::getConstantInt8(int8_t ConstantInt8) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 654 | return getConstPool()->Integers8.getOrAdd(this, ConstantInt8); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | Constant *GlobalContext::getConstantInt16(int16_t ConstantInt16) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 658 | return getConstPool()->Integers16.getOrAdd(this, ConstantInt16); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | Constant *GlobalContext::getConstantInt32(int32_t ConstantInt32) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 662 | return getConstPool()->Integers32.getOrAdd(this, ConstantInt32); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | Constant *GlobalContext::getConstantInt64(int64_t ConstantInt64) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 666 | return getConstPool()->Integers64.getOrAdd(this, ConstantInt64); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | Constant *GlobalContext::getConstantFloat(float ConstantFloat) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 670 | return getConstPool()->Floats.getOrAdd(this, ConstantFloat); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | Constant *GlobalContext::getConstantDouble(double ConstantDouble) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 674 | return getConstPool()->Doubles.getOrAdd(this, ConstantDouble); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 675 | } |
| 676 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 677 | Constant *GlobalContext::getConstantSym(const RelocOffsetT Offset, |
| 678 | const RelocOffsetArray &OffsetExpr, |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 679 | const IceString &Name, |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 680 | const IceString &EmitString) { |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 681 | return getConstPool()->Relocatables.getOrAdd( |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 682 | this, RelocatableTuple(Offset, OffsetExpr, Name, EmitString)); |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 685 | Constant *GlobalContext::getConstantSym(RelocOffsetT Offset, |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 686 | const IceString &Name) { |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 687 | constexpr char EmptyEmitString[] = ""; |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 688 | return getConstantSym(Offset, {}, Name, EmptyEmitString); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 691 | Constant *GlobalContext::getConstantExternSym(const IceString &Name) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 692 | constexpr RelocOffsetT Offset = 0; |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 693 | return getConstPool()->ExternRelocatables.getOrAdd( |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 694 | this, RelocatableTuple(Offset, {}, Name)); |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 697 | Constant *GlobalContext::getConstantUndef(Type Ty) { |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 698 | return getConstPool()->Undefs.getOrAdd(this, Ty); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 701 | // All locking is done by the getConstant*() target function. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 702 | Constant *GlobalContext::getConstantZero(Type Ty) { |
| 703 | switch (Ty) { |
| 704 | case IceType_i1: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 705 | return getConstantInt1(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 706 | case IceType_i8: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 707 | return getConstantInt8(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 708 | case IceType_i16: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 709 | return getConstantInt16(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 710 | case IceType_i32: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 711 | return getConstantInt32(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 712 | case IceType_i64: |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 713 | return getConstantInt64(0); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 714 | case IceType_f32: |
| 715 | return getConstantFloat(0); |
| 716 | case IceType_f64: |
| 717 | return getConstantDouble(0); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 718 | case IceType_v4i1: |
| 719 | case IceType_v8i1: |
| 720 | case IceType_v16i1: |
| 721 | case IceType_v16i8: |
| 722 | case IceType_v8i16: |
| 723 | case IceType_v4i32: |
| 724 | case IceType_v4f32: { |
| 725 | IceString Str; |
| 726 | llvm::raw_string_ostream BaseOS(Str); |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 727 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 728 | llvm_unreachable(BaseOS.str().c_str()); |
| 729 | } break; |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 730 | case IceType_void: |
| 731 | case IceType_NUM: |
| 732 | break; |
| 733 | } |
| 734 | llvm_unreachable("Unknown type"); |
| 735 | } |
| 736 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 737 | ConstantList GlobalContext::getConstantPool(Type Ty) { |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 738 | switch (Ty) { |
| 739 | case IceType_i1: |
| 740 | case IceType_i8: |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 741 | return getConstPool()->Integers8.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 742 | case IceType_i16: |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 743 | return getConstPool()->Integers16.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 744 | case IceType_i32: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 745 | return getConstPool()->Integers32.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 746 | case IceType_i64: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 747 | return getConstPool()->Integers64.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 748 | case IceType_f32: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 749 | return getConstPool()->Floats.getConstantPool(); |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 750 | case IceType_f64: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 751 | return getConstPool()->Doubles.getConstantPool(); |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 752 | case IceType_v4i1: |
| 753 | case IceType_v8i1: |
| 754 | case IceType_v16i1: |
| 755 | case IceType_v16i8: |
| 756 | case IceType_v8i16: |
| 757 | case IceType_v4i32: |
| 758 | case IceType_v4f32: { |
| 759 | IceString Str; |
| 760 | llvm::raw_string_ostream BaseOS(Str); |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 761 | BaseOS << "Unsupported constant type: " << Ty; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 762 | llvm_unreachable(BaseOS.str().c_str()); |
| 763 | } break; |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 764 | case IceType_void: |
| 765 | case IceType_NUM: |
| 766 | break; |
| 767 | } |
| 768 | llvm_unreachable("Unknown type"); |
| 769 | } |
| 770 | |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 771 | ConstantList GlobalContext::getConstantExternSyms() { |
| 772 | return getConstPool()->ExternRelocatables.getConstantPool(); |
| 773 | } |
| 774 | |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 775 | JumpTableDataList GlobalContext::getJumpTables() { |
| 776 | JumpTableDataList JumpTables(*getJumpTableList()); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 777 | // Make order deterministic by sorting into functions and then ID of the jump |
| 778 | // table within that function. |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 779 | std::sort(JumpTables.begin(), JumpTables.end(), |
| 780 | [](const JumpTableData &A, const JumpTableData &B) { |
| 781 | if (A.getFunctionName() != B.getFunctionName()) |
| 782 | return A.getFunctionName() < B.getFunctionName(); |
| 783 | return A.getId() < B.getId(); |
| 784 | }); |
| 785 | |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 786 | if (getFlags().shouldReorderPooledConstants()) { |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 787 | // If reorder-pooled-constants option is set to true, we also shuffle the |
| 788 | // jump tables before emitting them. |
| 789 | |
| 790 | // Create a random number generator for jump tables reordering, considering |
| 791 | // jump tables as pooled constants. |
| 792 | RandomNumberGenerator RNG(getFlags().getRandomSeed(), |
| 793 | RPE_PooledConstantReordering); |
John Porto | e0d9afa | 2015-08-05 10:13:44 -0700 | [diff] [blame] | 794 | RandomShuffle(JumpTables.begin(), JumpTables.end(), |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 795 | [&RNG](uint64_t N) { return (uint32_t)RNG.next(N); }); |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 796 | } |
| 797 | return JumpTables; |
| 798 | } |
| 799 | |
David Sehr | 0fe6b54 | 2015-11-19 21:47:15 -0800 | [diff] [blame] | 800 | JumpTableData & |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 801 | GlobalContext::addJumpTable(const IceString &FuncName, SizeT Id, |
David Sehr | 0fe6b54 | 2015-11-19 21:47:15 -0800 | [diff] [blame] | 802 | const JumpTableData::TargetList &TargetList) { |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 803 | auto JumpTableList = getJumpTableList(); |
David Sehr | 0fe6b54 | 2015-11-19 21:47:15 -0800 | [diff] [blame] | 804 | JumpTableList->emplace_back(FuncName, Id, TargetList); |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 805 | return JumpTableList->back(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 808 | TimerStackIdT GlobalContext::newTimerStackID(const IceString &Name) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 809 | if (!BuildDefs::dump()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 810 | return 0; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 811 | auto Timers = getTimers(); |
| 812 | TimerStackIdT NewID = Timers->size(); |
| 813 | Timers->push_back(TimerStack(Name)); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 814 | return NewID; |
| 815 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 816 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 817 | TimerIdT GlobalContext::getTimerID(TimerStackIdT StackID, |
| 818 | const IceString &Name) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 819 | auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 820 | assert(StackID < Timers->size()); |
| 821 | return Timers->at(StackID).getTimerID(Name); |
| 822 | } |
| 823 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 824 | void GlobalContext::pushTimer(TimerIdT ID, TimerStackIdT StackID) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 825 | auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 826 | assert(StackID < Timers->size()); |
| 827 | Timers->at(StackID).push(ID); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void GlobalContext::popTimer(TimerIdT ID, TimerStackIdT StackID) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 831 | auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 832 | assert(StackID < Timers->size()); |
| 833 | Timers->at(StackID).pop(ID); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 834 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 835 | |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 836 | void GlobalContext::resetTimer(TimerStackIdT StackID) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 837 | auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 838 | assert(StackID < Timers->size()); |
| 839 | Timers->at(StackID).reset(); |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | void GlobalContext::setTimerName(TimerStackIdT StackID, |
| 843 | const IceString &NewName) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 844 | auto Timers = &ICE_TLS_GET_FIELD(TLS)->Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 845 | assert(StackID < Timers->size()); |
| 846 | Timers->at(StackID).setName(NewName); |
Jim Stichnoth | d14b1a0 | 2014-10-08 08:28:36 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 849 | // Note: optQueueBlockingPush and optQueueBlockingPop use unique_ptr at the |
| 850 | // interface to take and transfer ownership, but they internally store the raw |
| 851 | // Cfg pointer in the work queue. This allows e.g. future queue optimizations |
| 852 | // such as the use of atomics to modify queue elements. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 853 | void GlobalContext::optQueueBlockingPush(std::unique_ptr<Cfg> Func) { |
| 854 | assert(Func); |
| 855 | OptQ.blockingPush(Func.release()); |
| 856 | if (getFlags().isSequential()) |
| 857 | translateFunctions(); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 860 | std::unique_ptr<Cfg> GlobalContext::optQueueBlockingPop() { |
| 861 | return std::unique_ptr<Cfg>(OptQ.blockingPop()); |
| 862 | } |
| 863 | |
| 864 | void GlobalContext::emitQueueBlockingPush(EmitterWorkItem *Item) { |
| 865 | assert(Item); |
| 866 | EmitQ.blockingPush(Item); |
| 867 | if (getFlags().isSequential()) |
| 868 | emitItems(); |
| 869 | } |
| 870 | |
| 871 | EmitterWorkItem *GlobalContext::emitQueueBlockingPop() { |
| 872 | return EmitQ.blockingPop(); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 875 | void GlobalContext::dumpStats(const IceString &Name, bool Final) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 876 | if (!getFlags().getDumpStats()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 877 | return; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 878 | if (Final) { |
Jim Stichnoth | 3d5e08d | 2016-03-01 12:22:29 -0800 | [diff] [blame] | 879 | getStatsCumulative()->dump(Name, this); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 880 | } else { |
Jim Stichnoth | 3d5e08d | 2016-03-01 12:22:29 -0800 | [diff] [blame] | 881 | ICE_TLS_GET_FIELD(TLS)->StatsFunction.dump(Name, this); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 885 | void GlobalContext::dumpTimers(TimerStackIdT StackID, bool DumpCumulative) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 886 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 887 | return; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 888 | auto Timers = getTimers(); |
| 889 | assert(Timers->size() > StackID); |
| 890 | OstreamLocker L(this); |
| 891 | Timers->at(StackID).dump(getStrDump(), DumpCumulative); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame^] | 894 | ClFlags GlobalContext::Flags; |
| 895 | ClFlagsExtra GlobalContext::ExtraFlags; |
| 896 | |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 897 | void TimerMarker::push() { |
| 898 | switch (StackID) { |
| 899 | case GlobalContext::TSK_Default: |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 900 | Active = Ctx->getFlags().getSubzeroTimingEnabled(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 901 | break; |
| 902 | case GlobalContext::TSK_Funcs: |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 903 | Active = Ctx->getFlags().getTimeEachFunction(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 904 | break; |
| 905 | default: |
| 906 | break; |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 907 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 908 | if (Active) |
| 909 | Ctx->pushTimer(ID, StackID); |
| 910 | } |
| 911 | |
| 912 | void TimerMarker::pushCfg(const Cfg *Func) { |
| 913 | Ctx = Func->getContext(); |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 914 | Active = |
| 915 | Func->getFocusedTiming() || Ctx->getFlags().getSubzeroTimingEnabled(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 916 | if (Active) |
| 917 | Ctx->pushTimer(ID, StackID); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 918 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 919 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 920 | ICE_TLS_DEFINE_FIELD(GlobalContext::ThreadContext *, GlobalContext, TLS); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 921 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 922 | } // end of namespace Ice |