Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceGlobalContext.h - Global context defs -----*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
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 Declares 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 | |
| 16 | #ifndef SUBZERO_SRC_ICEGLOBALCONTEXT_H |
| 17 | #define SUBZERO_SRC_ICEGLOBALCONTEXT_H |
| 18 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 19 | #include "IceDefs.h" |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 20 | #include "IceClFlags.h" |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 21 | #include "IceIntrinsics.h" |
Matt Wala | 1bd2fce | 2014-08-08 14:02:09 -0700 | [diff] [blame] | 22 | #include "IceRNG.h" |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 23 | #include "IceSwitchLowering.h" |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 24 | #include "IceThreading.h" |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 25 | #include "IceTimerTree.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 26 | #include "IceTypes.h" |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 27 | #include "IceUtils.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 28 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 29 | #include <array> |
| 30 | #include <functional> |
| 31 | #include <mutex> |
| 32 | #include <thread> |
| 33 | #include <type_traits> |
| 34 | #include <vector> |
| 35 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 36 | namespace Ice { |
| 37 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 38 | class ClFlags; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 39 | class ConstantPool; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 40 | class EmitterWorkItem; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 41 | class FuncSigType; |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 42 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 43 | /// LockedPtr is a way to provide automatically locked access to some object. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 44 | template <typename T> class LockedPtr { |
| 45 | LockedPtr() = delete; |
| 46 | LockedPtr(const LockedPtr &) = delete; |
| 47 | LockedPtr &operator=(const LockedPtr &) = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 48 | |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 49 | public: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 50 | LockedPtr(T *Value, GlobalLockType *Lock) : Value(Value), Lock(Lock) { |
| 51 | Lock->lock(); |
| 52 | } |
| 53 | LockedPtr(LockedPtr &&Other) : Value(Other.Value), Lock(Other.Lock) { |
| 54 | Other.Value = nullptr; |
| 55 | Other.Lock = nullptr; |
| 56 | } |
| 57 | ~LockedPtr() { Lock->unlock(); } |
| 58 | T *operator->() const { return Value; } |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 59 | T &operator*() const { return *Value; } |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 60 | |
| 61 | private: |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 62 | T *Value; |
| 63 | GlobalLockType *Lock; |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 66 | class GlobalContext { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 67 | GlobalContext() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 68 | GlobalContext(const GlobalContext &) = delete; |
| 69 | GlobalContext &operator=(const GlobalContext &) = delete; |
| 70 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 71 | /// CodeStats collects rudimentary statistics during translation. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 72 | class CodeStats { |
| 73 | CodeStats(const CodeStats &) = delete; |
| 74 | CodeStats &operator=(const CodeStats &) = default; |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 75 | #define CODESTATS_TABLE \ |
| 76 | /* dump string, enum value */ \ |
| 77 | X("Inst Count ", InstCount) \ |
| 78 | X("Regs Saved ", RegsSaved) \ |
| 79 | X("Frame Bytes ", FrameByte) \ |
| 80 | X("Spills ", NumSpills) \ |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 81 | X("Fills ", NumFills) \ |
| 82 | X("R/P Imms ", NumRPImms) |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 83 | //#define X(str, tag) |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 84 | |
| 85 | public: |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 86 | enum CSTag { |
| 87 | #define X(str, tag) CS_##tag, |
| 88 | CODESTATS_TABLE |
| 89 | #undef X |
| 90 | CS_NUM |
| 91 | }; |
| 92 | CodeStats() { reset(); } |
| 93 | void reset() { Stats.fill(0); } |
| 94 | void update(CSTag Tag, uint32_t Count = 1) { |
| 95 | assert(Tag < Stats.size()); |
| 96 | Stats[Tag] += Count; |
| 97 | } |
| 98 | void add(const CodeStats &Other) { |
| 99 | for (uint32_t i = 0; i < Stats.size(); ++i) |
| 100 | Stats[i] += Other.Stats[i]; |
| 101 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 102 | void dump(const IceString &Name, Ostream &Str); |
| 103 | |
| 104 | private: |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 105 | std::array<uint32_t, CS_NUM> Stats; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 108 | /// TimerList is a vector of TimerStack objects, with extra methods |
| 109 | /// to initialize and merge these vectors. |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 110 | class TimerList : public std::vector<TimerStack> { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 111 | TimerList(const TimerList &) = delete; |
| 112 | TimerList &operator=(const TimerList &) = delete; |
| 113 | |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 114 | public: |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 115 | TimerList() = default; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 116 | /// initInto() initializes a target list of timers based on the |
| 117 | /// current list. In particular, it creates the same number of |
| 118 | /// timers, in the same order, with the same names, but initially |
| 119 | /// empty of timing data. |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 120 | void initInto(TimerList &Dest) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 121 | if (!BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 122 | return; |
| 123 | Dest.clear(); |
| 124 | for (const TimerStack &Stack : *this) { |
| 125 | Dest.push_back(TimerStack(Stack.getName())); |
| 126 | } |
| 127 | } |
| 128 | void mergeFrom(TimerList &Src) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 129 | if (!BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 130 | return; |
| 131 | assert(size() == Src.size()); |
| 132 | size_type i = 0; |
| 133 | for (TimerStack &Stack : *this) { |
| 134 | assert(Stack.getName() == Src[i].getName()); |
| 135 | Stack.mergeFrom(Src[i]); |
| 136 | ++i; |
| 137 | } |
| 138 | } |
| 139 | }; |
| 140 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 141 | /// ThreadContext contains thread-local data. This data can be |
| 142 | /// combined/reduced as needed after all threads complete. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 143 | class ThreadContext { |
| 144 | ThreadContext(const ThreadContext &) = delete; |
| 145 | ThreadContext &operator=(const ThreadContext &) = delete; |
| 146 | |
| 147 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 148 | ThreadContext() = default; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 149 | CodeStats StatsFunction; |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 150 | CodeStats StatsCumulative; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 151 | TimerList Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 154 | public: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 155 | /// The dump stream is a log stream while emit is the stream code |
| 156 | /// is emitted to. The error stream is strictly for logging errors. |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 157 | GlobalContext(Ostream *OsDump, Ostream *OsEmit, Ostream *OsError, |
| 158 | ELFStreamer *ELFStreamer, const ClFlags &Flags); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 159 | ~GlobalContext(); |
| 160 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 161 | /// |
| 162 | /// The dump, error, and emit streams need to be used by only one |
| 163 | /// thread at a time. This is done by exclusively reserving the |
| 164 | /// streams via lockStr() and unlockStr(). The OstreamLocker class |
| 165 | /// can be used to conveniently manage this. |
| 166 | /// |
| 167 | /// The model is that a thread grabs the stream lock, then does an |
| 168 | /// arbitrary amount of work during which far-away callees may grab |
| 169 | /// the stream and do something with it, and finally the thread |
| 170 | /// releases the stream lock. This allows large chunks of output to |
| 171 | /// be dumped or emitted without risking interleaving from multiple |
| 172 | /// threads. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 173 | void lockStr() { StrLock.lock(); } |
| 174 | void unlockStr() { StrLock.unlock(); } |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 175 | Ostream &getStrDump() { return *StrDump; } |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 176 | Ostream &getStrError() { return *StrError; } |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 177 | Ostream &getStrEmit() { return *StrEmit; } |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 178 | void setStrEmit(Ostream &NewStrEmit) { StrEmit = &NewStrEmit; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 179 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 180 | LockedPtr<ErrorCode> getErrorStatus() { |
| 181 | return LockedPtr<ErrorCode>(&ErrorStatus, &ErrorStatusLock); |
| 182 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 183 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 184 | /// When emitting assembly, we allow a string to be prepended to |
| 185 | /// names of translated functions. This makes it easier to create an |
| 186 | /// execution test against a reference translator like llc, with both |
| 187 | /// translators using the same bitcode as input. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 188 | IceString mangleName(const IceString &Name) const; |
| 189 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 190 | /// \name Manage Constants. |
| 191 | /// @{ |
| 192 | // getConstant*() functions are not const because they might add something to |
| 193 | // the constant pool. |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 194 | Constant *getConstantInt(Type Ty, int64_t Value); |
| 195 | Constant *getConstantInt1(int8_t ConstantInt1); |
| 196 | Constant *getConstantInt8(int8_t ConstantInt8); |
| 197 | Constant *getConstantInt16(int16_t ConstantInt16); |
| 198 | Constant *getConstantInt32(int32_t ConstantInt32); |
| 199 | Constant *getConstantInt64(int64_t ConstantInt64); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 200 | Constant *getConstantFloat(float Value); |
| 201 | Constant *getConstantDouble(double Value); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 202 | /// Returns a symbolic constant. |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame^] | 203 | Constant *getConstantSym(const RelocOffsetT Offset, |
| 204 | const RelocOffsetArray &OffsetExpr, |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 205 | const IceString &Name, const IceString &EmitString, |
| 206 | bool SuppressMangling); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 207 | Constant *getConstantSym(RelocOffsetT Offset, const IceString &Name, |
| 208 | bool SuppressMangling); |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 209 | Constant *getConstantExternSym(const IceString &Name); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 210 | /// Returns an undef. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 211 | Constant *getConstantUndef(Type Ty); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 212 | /// Returns a zero value. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 213 | Constant *getConstantZero(Type Ty); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 214 | /// getConstantPool() returns a copy of the constant pool for constants of a |
| 215 | /// given type. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 216 | ConstantList getConstantPool(Type Ty); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 217 | /// Returns a copy of the list of external symbols. |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 218 | ConstantList getConstantExternSyms(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 219 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 220 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 221 | /// Return a locked pointer to the registered jump tables. |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 222 | JumpTableDataList getJumpTables(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 223 | /// Create a new jump table entry and return a reference to it. |
David Sehr | 0fe6b54 | 2015-11-19 21:47:15 -0800 | [diff] [blame] | 224 | JumpTableData &addJumpTable(IceString FuncName, SizeT Id, |
| 225 | const JumpTableData::TargetList &TargetList); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 226 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 227 | const ClFlags &getFlags() const { return Flags; } |
| 228 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 229 | /// Allocate data of type T using the global allocator. We allow entities |
| 230 | /// allocated from this global allocator to be either trivially or |
| 231 | /// non-trivially destructible. We optimize the case when T is trivially |
| 232 | /// destructible by not registering a destructor. Destructors will be invoked |
| 233 | /// during GlobalContext destruction in the reverse object creation order. |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 234 | template <typename T> |
| 235 | typename std::enable_if<std::is_trivially_destructible<T>::value, T>::type * |
| 236 | allocate() { |
| 237 | return getAllocator()->Allocate<T>(); |
| 238 | } |
| 239 | |
| 240 | template <typename T> |
| 241 | typename std::enable_if<!std::is_trivially_destructible<T>::value, T>::type * |
| 242 | allocate() { |
| 243 | T *Ret = getAllocator()->Allocate<T>(); |
| 244 | getDestructors()->emplace_back([Ret]() { Ret->~T(); }); |
| 245 | return Ret; |
| 246 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 247 | |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 248 | const Intrinsics &getIntrinsicsInfo() const { return IntrinsicsInfo; } |
| 249 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 250 | ELFObjectWriter *getObjectWriter() const { return ObjectWriter.get(); } |
| 251 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 252 | /// Reset stats at the beginning of a function. |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 253 | void resetStats() { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 254 | if (BuildDefs::dump()) |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 255 | ICE_TLS_GET_FIELD(TLS)->StatsFunction.reset(); |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 256 | } |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 257 | void dumpStats(const IceString &Name, bool Final = false); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 258 | void statsUpdateEmitted(uint32_t InstCount) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 259 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 260 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 261 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 262 | Tls->StatsFunction.update(CodeStats::CS_InstCount, InstCount); |
| 263 | Tls->StatsCumulative.update(CodeStats::CS_InstCount, InstCount); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 264 | } |
| 265 | void statsUpdateRegistersSaved(uint32_t Num) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 266 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 267 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 268 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 269 | Tls->StatsFunction.update(CodeStats::CS_RegsSaved, Num); |
| 270 | Tls->StatsCumulative.update(CodeStats::CS_RegsSaved, Num); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 271 | } |
| 272 | void statsUpdateFrameBytes(uint32_t Bytes) { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 273 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 274 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 275 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 276 | Tls->StatsFunction.update(CodeStats::CS_FrameByte, Bytes); |
| 277 | Tls->StatsCumulative.update(CodeStats::CS_FrameByte, Bytes); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 278 | } |
| 279 | void statsUpdateSpills() { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 280 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 281 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 282 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 283 | Tls->StatsFunction.update(CodeStats::CS_NumSpills); |
| 284 | Tls->StatsCumulative.update(CodeStats::CS_NumSpills); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 285 | } |
| 286 | void statsUpdateFills() { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 287 | if (!getFlags().getDumpStats()) |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 288 | return; |
Jan Voung | 3e5009f | 2015-03-24 09:04:13 -0700 | [diff] [blame] | 289 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 290 | Tls->StatsFunction.update(CodeStats::CS_NumFills); |
| 291 | Tls->StatsCumulative.update(CodeStats::CS_NumFills); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 294 | /// Number of Randomized or Pooled Immediates |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 295 | void statsUpdateRPImms() { |
| 296 | if (!getFlags().getDumpStats()) |
| 297 | return; |
| 298 | ThreadContext *Tls = ICE_TLS_GET_FIELD(TLS); |
| 299 | Tls->StatsFunction.update(CodeStats::CS_NumRPImms); |
| 300 | Tls->StatsCumulative.update(CodeStats::CS_NumRPImms); |
| 301 | } |
| 302 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 303 | /// These are predefined TimerStackIdT values. |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 304 | enum TimerStackKind { TSK_Default = 0, TSK_Funcs, TSK_Num }; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 305 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 306 | /// newTimerStackID() creates a new TimerStack in the global space. It does |
| 307 | /// not affect any TimerStack objects in TLS. |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 308 | TimerStackIdT newTimerStackID(const IceString &Name); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 309 | /// dumpTimers() dumps the global timer data. As such, one probably wants to |
| 310 | /// call mergeTimerStacks() as a prerequisite. |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 311 | void dumpTimers(TimerStackIdT StackID = TSK_Default, |
| 312 | bool DumpCumulative = true); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 313 | /// The following methods affect only the calling thread's TLS timer data. |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 314 | TimerIdT getTimerID(TimerStackIdT StackID, const IceString &Name); |
| 315 | void pushTimer(TimerIdT ID, TimerStackIdT StackID); |
| 316 | void popTimer(TimerIdT ID, TimerStackIdT StackID); |
| 317 | void resetTimer(TimerStackIdT StackID); |
| 318 | void setTimerName(TimerStackIdT StackID, const IceString &NewName); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 319 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 320 | /// This is the first work item sequence number that the parser produces, and |
| 321 | /// correspondingly the first sequence number that the emitter thread will |
| 322 | /// wait for. Start numbering at 1 to leave room for a sentinel, in case e.g. |
| 323 | /// we wish to inject items with a special sequence number that may be |
| 324 | /// executed out of order. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 325 | static uint32_t getFirstSequenceNumber() { return 1; } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 326 | /// Adds a newly parsed and constructed function to the Cfg work queue. |
| 327 | /// Notifies any idle workers that a new function is available for |
| 328 | /// translating. May block if the work queue is too large, in order to control |
| 329 | /// memory footprint. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 330 | void optQueueBlockingPush(std::unique_ptr<Cfg> Func); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 331 | /// Takes a Cfg from the work queue for translating. May block if the work |
| 332 | /// queue is currently empty. Returns nullptr if there is no more work - the |
| 333 | /// queue is empty and either end() has been called or the Sequential flag was |
| 334 | /// set. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 335 | std::unique_ptr<Cfg> optQueueBlockingPop(); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 336 | /// Notifies that no more work will be added to the work queue. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 337 | void optQueueNotifyEnd() { OptQ.notifyEnd(); } |
| 338 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 339 | /// Emit file header for output file. |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 340 | void emitFileHeader(); |
| 341 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 342 | void lowerConstants(); |
| 343 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 344 | void lowerJumpTables(); |
| 345 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 346 | void emitQueueBlockingPush(EmitterWorkItem *Item); |
| 347 | EmitterWorkItem *emitQueueBlockingPop(); |
| 348 | void emitQueueNotifyEnd() { EmitQ.notifyEnd(); } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 349 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 350 | void initParserThread() { |
| 351 | ThreadContext *Tls = new ThreadContext(); |
| 352 | auto Timers = getTimers(); |
| 353 | Timers->initInto(Tls->Timers); |
| 354 | AllThreadContexts.push_back(Tls); |
| 355 | ICE_TLS_SET_FIELD(TLS, Tls); |
| 356 | } |
| 357 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 358 | void startWorkerThreads() { |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 359 | size_t NumWorkers = getFlags().getNumTranslationThreads(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 360 | auto Timers = getTimers(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 361 | for (size_t i = 0; i < NumWorkers; ++i) { |
| 362 | ThreadContext *WorkerTLS = new ThreadContext(); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 363 | Timers->initInto(WorkerTLS->Timers); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 364 | AllThreadContexts.push_back(WorkerTLS); |
| 365 | TranslationThreads.push_back(std::thread( |
| 366 | &GlobalContext::translateFunctionsWrapper, this, WorkerTLS)); |
| 367 | } |
| 368 | if (NumWorkers) { |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 369 | ThreadContext *WorkerTLS = new ThreadContext(); |
| 370 | Timers->initInto(WorkerTLS->Timers); |
| 371 | AllThreadContexts.push_back(WorkerTLS); |
| 372 | EmitterThreads.push_back( |
| 373 | std::thread(&GlobalContext::emitterWrapper, this, WorkerTLS)); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | void waitForWorkerThreads() { |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 378 | optQueueNotifyEnd(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 379 | for (std::thread &Worker : TranslationThreads) { |
| 380 | Worker.join(); |
| 381 | } |
| 382 | TranslationThreads.clear(); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 383 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 384 | // Only notify the emit queue to end after all the translation threads have |
| 385 | // ended. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 386 | emitQueueNotifyEnd(); |
| 387 | for (std::thread &Worker : EmitterThreads) { |
| 388 | Worker.join(); |
| 389 | } |
| 390 | EmitterThreads.clear(); |
| 391 | |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 392 | if (BuildDefs::dump()) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 393 | auto Timers = getTimers(); |
| 394 | for (ThreadContext *TLS : AllThreadContexts) |
| 395 | Timers->mergeFrom(TLS->Timers); |
| 396 | } |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 397 | if (BuildDefs::dump()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 398 | // Do a separate loop over AllThreadContexts to avoid holding two locks |
| 399 | // at once. |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 400 | auto Stats = getStatsCumulative(); |
| 401 | for (ThreadContext *TLS : AllThreadContexts) |
| 402 | Stats->add(TLS->StatsCumulative); |
| 403 | } |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 406 | /// Translation thread startup routine. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 407 | void translateFunctionsWrapper(ThreadContext *MyTLS) { |
| 408 | ICE_TLS_SET_FIELD(TLS, MyTLS); |
| 409 | translateFunctions(); |
| 410 | } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 411 | /// Translate functions from the Cfg queue until the queue is empty. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 412 | void translateFunctions(); |
| 413 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 414 | /// Emitter thread startup routine. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 415 | void emitterWrapper(ThreadContext *MyTLS) { |
| 416 | ICE_TLS_SET_FIELD(TLS, MyTLS); |
| 417 | emitItems(); |
| 418 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 419 | /// Emit functions and global initializers from the emitter queue until the |
| 420 | /// queue is empty. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 421 | void emitItems(); |
| 422 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 423 | /// Uses DataLowering to lower Globals. Side effects: |
| 424 | /// - discards the initializer list for the global variable in Globals. |
| 425 | /// - clears the Globals array. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 426 | void lowerGlobals(const IceString &SectionSuffix); |
| 427 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 428 | /// Lowers the profile information. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 429 | void lowerProfileData(); |
| 430 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 431 | /// Utility function to match a symbol name against a match string. This is |
| 432 | /// used in a few cases where we want to take some action on a particular |
| 433 | /// function or symbol based on a command-line argument, such as changing the |
| 434 | /// verbose level for a particular function. An empty Match argument means |
| 435 | /// match everything. Returns true if there is a match. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 436 | static bool matchSymbolName(const IceString &SymbolName, |
| 437 | const IceString &Match) { |
| 438 | return Match.empty() || Match == SymbolName; |
| 439 | } |
| 440 | |
| 441 | private: |
| 442 | // Try to ensure mutexes are allocated on separate cache lines. |
| 443 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 444 | // Destructors collaborate with Allocator |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 445 | ICE_CACHELINE_BOUNDARY; |
| 446 | // Managed by getAllocator() |
| 447 | GlobalLockType AllocLock; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame^] | 448 | ArenaAllocator Allocator; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 449 | |
| 450 | ICE_CACHELINE_BOUNDARY; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 451 | // Managed by getDestructors() |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 452 | using DestructorArray = std::vector<std::function<void()>>; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 453 | GlobalLockType DestructorsLock; |
| 454 | DestructorArray Destructors; |
| 455 | |
| 456 | ICE_CACHELINE_BOUNDARY; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 457 | // Managed by getConstantPool() |
| 458 | GlobalLockType ConstPoolLock; |
| 459 | std::unique_ptr<ConstantPool> ConstPool; |
| 460 | |
| 461 | ICE_CACHELINE_BOUNDARY; |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 462 | // Managed by getJumpTableList() |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 463 | GlobalLockType JumpTablesLock; |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 464 | JumpTableDataList JumpTableList; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 465 | |
| 466 | ICE_CACHELINE_BOUNDARY; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 467 | // Managed by getErrorStatus() |
| 468 | GlobalLockType ErrorStatusLock; |
| 469 | ErrorCode ErrorStatus; |
| 470 | |
| 471 | ICE_CACHELINE_BOUNDARY; |
| 472 | // Managed by getStatsCumulative() |
| 473 | GlobalLockType StatsLock; |
| 474 | CodeStats StatsCumulative; |
| 475 | |
| 476 | ICE_CACHELINE_BOUNDARY; |
| 477 | // Managed by getTimers() |
| 478 | GlobalLockType TimerLock; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 479 | TimerList Timers; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 480 | |
| 481 | ICE_CACHELINE_BOUNDARY; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 482 | /// StrLock is a global lock on the dump and emit output streams. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 483 | using StrLockType = std::mutex; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 484 | StrLockType StrLock; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 485 | Ostream *StrDump; /// Stream for dumping / diagnostics |
| 486 | Ostream *StrEmit; /// Stream for code emission |
| 487 | Ostream *StrError; /// Stream for logging errors. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 488 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 489 | ICE_CACHELINE_BOUNDARY; |
| 490 | |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 491 | Intrinsics IntrinsicsInfo; |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 492 | const ClFlags &Flags; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 493 | // TODO(jpp): move to EmitterContext. |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 494 | std::unique_ptr<ELFObjectWriter> ObjectWriter; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 495 | BoundedProducerConsumerQueue<Cfg> OptQ; |
| 496 | BoundedProducerConsumerQueue<EmitterWorkItem> EmitQ; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 497 | // DataLowering is only ever used by a single thread at a time (either in |
| 498 | // emitItems(), or in IceCompiler::run before the compilation is over.) |
| 499 | // TODO(jpp): move to EmitterContext. |
| 500 | std::unique_ptr<TargetDataLowering> DataLowering; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 501 | /// If !HasEmittedCode, SubZero will accumulate all Globals (which are "true" |
| 502 | /// program global variables) until the first code WorkItem is seen. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 503 | // TODO(jpp): move to EmitterContext. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 504 | bool HasSeenCode = false; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 505 | // TODO(jpp): move to EmitterContext. |
| 506 | VariableDeclarationList Globals; |
| 507 | // TODO(jpp): move to EmitterContext. |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 508 | VariableDeclaration *ProfileBlockInfoVarDecl; |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 509 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame^] | 510 | LockedPtr<ArenaAllocator> getAllocator() { |
| 511 | return LockedPtr<ArenaAllocator>(&Allocator, &AllocLock); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 512 | } |
| 513 | LockedPtr<ConstantPool> getConstPool() { |
| 514 | return LockedPtr<ConstantPool>(ConstPool.get(), &ConstPoolLock); |
| 515 | } |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 516 | LockedPtr<JumpTableDataList> getJumpTableList() { |
| 517 | return LockedPtr<JumpTableDataList>(&JumpTableList, &JumpTablesLock); |
| 518 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 519 | LockedPtr<CodeStats> getStatsCumulative() { |
| 520 | return LockedPtr<CodeStats>(&StatsCumulative, &StatsLock); |
| 521 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 522 | LockedPtr<TimerList> getTimers() { |
| 523 | return LockedPtr<TimerList>(&Timers, &TimerLock); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 524 | } |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 525 | LockedPtr<DestructorArray> getDestructors() { |
| 526 | return LockedPtr<DestructorArray>(&Destructors, &DestructorsLock); |
| 527 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 528 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 529 | void accumulateGlobals(std::unique_ptr<VariableDeclarationList> Globls) { |
| 530 | if (Globls != nullptr) |
| 531 | Globals.insert(Globals.end(), Globls->begin(), Globls->end()); |
| 532 | } |
| 533 | |
| 534 | void lowerGlobalsIfNoCodeHasBeenSeen() { |
| 535 | if (HasSeenCode) |
| 536 | return; |
| 537 | constexpr char NoSuffix[] = ""; |
| 538 | lowerGlobals(NoSuffix); |
| 539 | HasSeenCode = true; |
| 540 | } |
| 541 | |
John Porto | 844211e | 2016-02-04 08:42:48 -0800 | [diff] [blame] | 542 | void addBlockInfoPtrs(VariableDeclaration *ProfileBlockInfo); |
| 543 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 544 | llvm::SmallVector<ThreadContext *, 128> AllThreadContexts; |
| 545 | llvm::SmallVector<std::thread, 128> TranslationThreads; |
| 546 | llvm::SmallVector<std::thread, 128> EmitterThreads; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 547 | // Each thread has its own TLS pointer which is also held in |
| 548 | // AllThreadContexts. |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 549 | ICE_TLS_DECLARE_FIELD(ThreadContext *, TLS); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 550 | |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 551 | // Private helpers for mangleName() |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 552 | using ManglerVector = llvm::SmallVector<char, 32>; |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 553 | void incrementSubstitutions(ManglerVector &OldName) const; |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 554 | |
| 555 | public: |
| 556 | static void TlsInit() { ICE_TLS_INIT_FIELD(TLS); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 557 | }; |
| 558 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 559 | /// Helper class to push and pop a timer marker. The constructor pushes a |
| 560 | /// marker, and the destructor pops it. This is for convenient timing of regions |
| 561 | /// of code. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 562 | class TimerMarker { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 563 | TimerMarker() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 564 | TimerMarker(const TimerMarker &) = delete; |
| 565 | TimerMarker &operator=(const TimerMarker &) = delete; |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 566 | |
| 567 | public: |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 568 | TimerMarker(TimerIdT ID, GlobalContext *Ctx, |
| 569 | TimerStackIdT StackID = GlobalContext::TSK_Default) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 570 | : ID(ID), Ctx(Ctx), StackID(StackID) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 571 | if (BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 572 | push(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 573 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 574 | TimerMarker(TimerIdT ID, const Cfg *Func, |
| 575 | TimerStackIdT StackID = GlobalContext::TSK_Default) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 576 | : ID(ID), Ctx(nullptr), StackID(StackID) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 577 | // Ctx gets set at the beginning of pushCfg(). |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 578 | if (BuildDefs::dump()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 579 | pushCfg(Func); |
| 580 | } |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 581 | |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 582 | ~TimerMarker() { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 583 | if (BuildDefs::dump() && Active) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 584 | Ctx->popTimer(ID, StackID); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | private: |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 588 | void push(); |
| 589 | void pushCfg(const Cfg *Func); |
| 590 | const TimerIdT ID; |
| 591 | GlobalContext *Ctx; |
| 592 | const TimerStackIdT StackID; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 593 | bool Active = false; |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 594 | }; |
| 595 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 596 | /// Helper class for locking the streams and then automatically unlocking them. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 597 | class OstreamLocker { |
| 598 | private: |
| 599 | OstreamLocker() = delete; |
| 600 | OstreamLocker(const OstreamLocker &) = delete; |
| 601 | OstreamLocker &operator=(const OstreamLocker &) = delete; |
| 602 | |
| 603 | public: |
| 604 | explicit OstreamLocker(GlobalContext *Ctx) : Ctx(Ctx) { Ctx->lockStr(); } |
| 605 | ~OstreamLocker() { Ctx->unlockStr(); } |
| 606 | |
| 607 | private: |
| 608 | GlobalContext *const Ctx; |
| 609 | }; |
| 610 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 611 | } // end of namespace Ice |
| 612 | |
| 613 | #endif // SUBZERO_SRC_ICEGLOBALCONTEXT_H |