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