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" |
Thomas Lively | aab7099 | 2016-06-07 13:54:59 -0700 | [diff] [blame] | 21 | #include "IceInstrumentation.h" |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 22 | #include "IceIntrinsics.h" |
Matt Wala | 1bd2fce | 2014-08-08 14:02:09 -0700 | [diff] [blame] | 23 | #include "IceRNG.h" |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 24 | #include "IceStringPool.h" |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 25 | #include "IceSwitchLowering.h" |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 26 | #include "IceTargetLowering.def" |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 27 | #include "IceThreading.h" |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 28 | #include "IceTimerTree.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 29 | #include "IceTypes.h" |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 30 | #include "IceUtils.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 31 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 32 | #include <array> |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 33 | #include <atomic> |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 34 | #include <cassert> |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 35 | #include <functional> |
John Porto | bd2e231 | 2016-03-15 11:06:25 -0700 | [diff] [blame] | 36 | #include <memory> |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 37 | #include <mutex> |
| 38 | #include <thread> |
| 39 | #include <type_traits> |
John Porto | bd2e231 | 2016-03-15 11:06:25 -0700 | [diff] [blame] | 40 | #include <utility> |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 41 | #include <vector> |
| 42 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 43 | namespace Ice { |
| 44 | |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 45 | class ConstantPool; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 46 | class EmitterWorkItem; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 47 | class FuncSigType; |
Thomas Lively | aab7099 | 2016-06-07 13:54:59 -0700 | [diff] [blame] | 48 | class Instrumentation; |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 49 | |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 50 | // Runtime helper function IDs |
| 51 | |
| 52 | enum class RuntimeHelper { |
| 53 | #define X(Tag, Name) H_##Tag, |
| 54 | RUNTIME_HELPER_FUNCTIONS_TABLE |
| 55 | #undef X |
| 56 | H_Num |
| 57 | }; |
| 58 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 59 | /// OptWorkItem is a simple wrapper used to pass parse information on a function |
| 60 | /// block, to a translator thread. |
| 61 | class OptWorkItem { |
| 62 | OptWorkItem(const OptWorkItem &) = delete; |
| 63 | OptWorkItem &operator=(const OptWorkItem &) = delete; |
| 64 | |
| 65 | public: |
| 66 | // Get the Cfg for the funtion to translate. |
| 67 | virtual std::unique_ptr<Cfg> getParsedCfg() = 0; |
| 68 | virtual ~OptWorkItem() = default; |
| 69 | |
| 70 | protected: |
| 71 | OptWorkItem() = default; |
| 72 | }; |
| 73 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 74 | class GlobalContext { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 75 | GlobalContext() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 76 | GlobalContext(const GlobalContext &) = delete; |
| 77 | GlobalContext &operator=(const GlobalContext &) = delete; |
| 78 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 79 | /// CodeStats collects rudimentary statistics during translation. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 80 | class CodeStats { |
| 81 | CodeStats(const CodeStats &) = delete; |
| 82 | CodeStats &operator=(const CodeStats &) = default; |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 83 | #define CODESTATS_TABLE \ |
| 84 | /* dump string, enum value */ \ |
| 85 | X("Inst Count ", InstCount) \ |
| 86 | X("Regs Saved ", RegsSaved) \ |
| 87 | X("Frame Bytes ", FrameByte) \ |
| 88 | X("Spills ", NumSpills) \ |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 89 | X("Fills ", NumFills) \ |
| 90 | X("R/P Imms ", NumRPImms) |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 91 | //#define X(str, tag) |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 92 | |
| 93 | public: |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 94 | enum CSTag { |
| 95 | #define X(str, tag) CS_##tag, |
| 96 | CODESTATS_TABLE |
| 97 | #undef X |
| 98 | CS_NUM |
| 99 | }; |
| 100 | CodeStats() { reset(); } |
| 101 | void reset() { Stats.fill(0); } |
| 102 | void update(CSTag Tag, uint32_t Count = 1) { |
| 103 | assert(Tag < Stats.size()); |
| 104 | Stats[Tag] += Count; |
| 105 | } |
| 106 | void add(const CodeStats &Other) { |
| 107 | for (uint32_t i = 0; i < Stats.size(); ++i) |
| 108 | Stats[i] += Other.Stats[i]; |
| 109 | } |
Jim Stichnoth | b5eee3d | 2016-03-31 11:05:39 -0700 | [diff] [blame] | 110 | /// Dumps the stats for the given Cfg. If Func==nullptr, it identifies it |
| 111 | /// as the "final" cumulative stats instead as a specific function's name. |
| 112 | void dump(const Cfg *Func, GlobalContext *Ctx); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 113 | |
| 114 | private: |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 115 | std::array<uint32_t, CS_NUM> Stats; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 118 | /// TimerList is a vector of TimerStack objects, with extra methods |
| 119 | /// to initialize and merge these vectors. |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 120 | class TimerList : public std::vector<TimerStack> { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 121 | TimerList(const TimerList &) = delete; |
| 122 | TimerList &operator=(const TimerList &) = delete; |
| 123 | |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 124 | public: |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 125 | TimerList() = default; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 126 | /// initInto() initializes a target list of timers based on the |
| 127 | /// current list. In particular, it creates the same number of |
| 128 | /// timers, in the same order, with the same names, but initially |
| 129 | /// empty of timing data. |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 130 | void initInto(TimerList &Dest) const { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 131 | if (!BuildDefs::timers()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 132 | return; |
| 133 | Dest.clear(); |
| 134 | for (const TimerStack &Stack : *this) { |
| 135 | Dest.push_back(TimerStack(Stack.getName())); |
| 136 | } |
| 137 | } |
| 138 | void mergeFrom(TimerList &Src) { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 139 | if (!BuildDefs::timers()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 140 | return; |
| 141 | assert(size() == Src.size()); |
| 142 | size_type i = 0; |
| 143 | for (TimerStack &Stack : *this) { |
| 144 | assert(Stack.getName() == Src[i].getName()); |
| 145 | Stack.mergeFrom(Src[i]); |
| 146 | ++i; |
| 147 | } |
| 148 | } |
| 149 | }; |
| 150 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 151 | /// ThreadContext contains thread-local data. This data can be |
| 152 | /// combined/reduced as needed after all threads complete. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 153 | class ThreadContext { |
| 154 | ThreadContext(const ThreadContext &) = delete; |
| 155 | ThreadContext &operator=(const ThreadContext &) = delete; |
| 156 | |
| 157 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 158 | ThreadContext() = default; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 159 | CodeStats StatsFunction; |
Jim Stichnoth | a1dd3cc | 2015-01-31 10:48:11 -0800 | [diff] [blame] | 160 | CodeStats StatsCumulative; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 161 | TimerList Timers; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 164 | public: |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 165 | /// The dump stream is a log stream while emit is the stream code |
| 166 | /// is emitted to. The error stream is strictly for logging errors. |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 167 | GlobalContext(Ostream *OsDump, Ostream *OsEmit, Ostream *OsError, |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 168 | ELFStreamer *ELFStreamer); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 169 | ~GlobalContext(); |
| 170 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 171 | void dumpStrings(); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 172 | /// |
| 173 | /// The dump, error, and emit streams need to be used by only one |
| 174 | /// thread at a time. This is done by exclusively reserving the |
| 175 | /// streams via lockStr() and unlockStr(). The OstreamLocker class |
| 176 | /// can be used to conveniently manage this. |
| 177 | /// |
| 178 | /// The model is that a thread grabs the stream lock, then does an |
| 179 | /// arbitrary amount of work during which far-away callees may grab |
| 180 | /// the stream and do something with it, and finally the thread |
| 181 | /// releases the stream lock. This allows large chunks of output to |
| 182 | /// be dumped or emitted without risking interleaving from multiple |
| 183 | /// threads. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 184 | void lockStr() { StrLock.lock(); } |
| 185 | void unlockStr() { StrLock.unlock(); } |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 186 | Ostream &getStrDump() { return *StrDump; } |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 187 | Ostream &getStrError() { return *StrError; } |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 188 | Ostream &getStrEmit() { return *StrEmit; } |
Karl Schimpf | 2fee2a2 | 2015-10-22 08:19:26 -0700 | [diff] [blame] | 189 | void setStrEmit(Ostream &NewStrEmit) { StrEmit = &NewStrEmit; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 190 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 191 | LockedPtr<ErrorCode> getErrorStatus() { |
| 192 | return LockedPtr<ErrorCode>(&ErrorStatus, &ErrorStatusLock); |
| 193 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 194 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 195 | /// \name Manage Constants. |
| 196 | /// @{ |
| 197 | // getConstant*() functions are not const because they might add something to |
| 198 | // the constant pool. |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 199 | Constant *getConstantInt(Type Ty, int64_t Value); |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 200 | Constant *getConstantInt1(int8_t ConstantInt1) { |
| 201 | ConstantInt1 &= INT8_C(1); |
| 202 | switch (ConstantInt1) { |
| 203 | case 0: |
| 204 | return getConstantZero(IceType_i1); |
| 205 | case 1: |
| 206 | return ConstantTrue; |
| 207 | default: |
| 208 | assert(false && "getConstantInt1 not on true/false"); |
| 209 | return getConstantInt1Internal(ConstantInt1); |
| 210 | } |
| 211 | } |
| 212 | Constant *getConstantInt8(int8_t ConstantInt8) { |
| 213 | switch (ConstantInt8) { |
| 214 | case 0: |
| 215 | return getConstantZero(IceType_i8); |
| 216 | default: |
| 217 | return getConstantInt8Internal(ConstantInt8); |
| 218 | } |
| 219 | } |
| 220 | Constant *getConstantInt16(int16_t ConstantInt16) { |
| 221 | switch (ConstantInt16) { |
| 222 | case 0: |
| 223 | return getConstantZero(IceType_i16); |
| 224 | default: |
| 225 | return getConstantInt16Internal(ConstantInt16); |
| 226 | } |
| 227 | } |
| 228 | Constant *getConstantInt32(int32_t ConstantInt32) { |
| 229 | switch (ConstantInt32) { |
| 230 | case 0: |
| 231 | return getConstantZero(IceType_i32); |
| 232 | default: |
| 233 | return getConstantInt32Internal(ConstantInt32); |
| 234 | } |
| 235 | } |
| 236 | Constant *getConstantInt64(int64_t ConstantInt64) { |
| 237 | switch (ConstantInt64) { |
| 238 | case 0: |
| 239 | return getConstantZero(IceType_i64); |
| 240 | default: |
| 241 | return getConstantInt64Internal(ConstantInt64); |
| 242 | } |
| 243 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 244 | Constant *getConstantFloat(float Value); |
| 245 | Constant *getConstantDouble(double Value); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 246 | /// Returns a symbolic constant. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 247 | Constant *getConstantSymWithEmitString(const RelocOffsetT Offset, |
| 248 | const RelocOffsetArray &OffsetExpr, |
| 249 | GlobalString Name, |
| 250 | const std::string &EmitString); |
| 251 | Constant *getConstantSym(RelocOffsetT Offset, GlobalString Name); |
| 252 | Constant *getConstantExternSym(GlobalString Name); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 253 | /// Returns an undef. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 254 | Constant *getConstantUndef(Type Ty); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 255 | /// Returns a zero value. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 256 | Constant *getConstantZero(Type Ty); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 257 | /// getConstantPool() returns a copy of the constant pool for constants of a |
| 258 | /// given type. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 259 | ConstantList getConstantPool(Type Ty); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 260 | /// Returns a copy of the list of external symbols. |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 261 | ConstantList getConstantExternSyms(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 262 | /// @} |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 263 | Constant *getRuntimeHelperFunc(RuntimeHelper FuncID) const { |
| 264 | assert(FuncID < RuntimeHelper::H_Num); |
| 265 | Constant *Result = RuntimeHelperFunc[static_cast<size_t>(FuncID)]; |
| 266 | assert(Result != nullptr && "No such runtime helper function"); |
| 267 | return Result; |
| 268 | } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 269 | GlobalString getGlobalString(const std::string &Name); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 270 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 271 | /// Return a locked pointer to the registered jump tables. |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 272 | JumpTableDataList getJumpTables(); |
John Porto | 0307721 | 2016-04-05 06:30:21 -0700 | [diff] [blame] | 273 | /// Adds JumpTable to the list of know jump tables, for a posteriori emission. |
| 274 | void addJumpTableData(JumpTableData JumpTable); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 275 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 276 | /// Allocate data of type T using the global allocator. We allow entities |
| 277 | /// allocated from this global allocator to be either trivially or |
| 278 | /// non-trivially destructible. We optimize the case when T is trivially |
| 279 | /// destructible by not registering a destructor. Destructors will be invoked |
| 280 | /// during GlobalContext destruction in the reverse object creation order. |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 281 | template <typename T> |
| 282 | typename std::enable_if<std::is_trivially_destructible<T>::value, T>::type * |
| 283 | allocate() { |
| 284 | return getAllocator()->Allocate<T>(); |
| 285 | } |
| 286 | |
| 287 | template <typename T> |
| 288 | typename std::enable_if<!std::is_trivially_destructible<T>::value, T>::type * |
| 289 | allocate() { |
| 290 | T *Ret = getAllocator()->Allocate<T>(); |
| 291 | getDestructors()->emplace_back([Ret]() { Ret->~T(); }); |
| 292 | return Ret; |
| 293 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 294 | |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 295 | const Intrinsics &getIntrinsicsInfo() const { return IntrinsicsInfo; } |
| 296 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 297 | ELFObjectWriter *getObjectWriter() const { return ObjectWriter.get(); } |
| 298 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 299 | /// Reset stats at the beginning of a function. |
Lei Zhang | 04e9b1c | 2017-05-23 15:08:59 -0700 | [diff] [blame] | 300 | void resetStats(); |
Jim Stichnoth | b5eee3d | 2016-03-31 11:05:39 -0700 | [diff] [blame] | 301 | void dumpStats(const Cfg *Func = nullptr); |
Lei Zhang | 04e9b1c | 2017-05-23 15:08:59 -0700 | [diff] [blame] | 302 | void statsUpdateEmitted(uint32_t InstCount); |
| 303 | void statsUpdateRegistersSaved(uint32_t Num); |
| 304 | void statsUpdateFrameBytes(uint32_t Bytes); |
| 305 | void statsUpdateSpills(); |
| 306 | void statsUpdateFills(); |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 307 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 308 | /// Number of Randomized or Pooled Immediates |
Lei Zhang | 04e9b1c | 2017-05-23 15:08:59 -0700 | [diff] [blame] | 309 | void statsUpdateRPImms(); |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 310 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 311 | /// These are predefined TimerStackIdT values. |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 312 | enum TimerStackKind { TSK_Default = 0, TSK_Funcs, TSK_Num }; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 313 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 314 | /// newTimerStackID() creates a new TimerStack in the global space. It does |
| 315 | /// not affect any TimerStack objects in TLS. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 316 | TimerStackIdT newTimerStackID(const std::string &Name); |
Jim Stichnoth | 318c01b | 2016-04-03 21:58:03 -0700 | [diff] [blame] | 317 | /// dumpTimers() dumps the global timer data. This assumes all the |
| 318 | /// thread-local copies of timer data have been merged into the global timer |
| 319 | /// data. |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 320 | void dumpTimers(TimerStackIdT StackID = TSK_Default, |
| 321 | bool DumpCumulative = true); |
Jim Stichnoth | 2b000fd | 2016-04-06 06:37:15 -0700 | [diff] [blame] | 322 | void dumpLocalTimers(const std::string &TimerNameOverride, |
| 323 | TimerStackIdT StackID = TSK_Default, |
| 324 | bool DumpCumulative = true); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 325 | /// The following methods affect only the calling thread's TLS timer data. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 326 | TimerIdT getTimerID(TimerStackIdT StackID, const std::string &Name); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 327 | void pushTimer(TimerIdT ID, TimerStackIdT StackID); |
| 328 | void popTimer(TimerIdT ID, TimerStackIdT StackID); |
| 329 | void resetTimer(TimerStackIdT StackID); |
Jim Stichnoth | 2b000fd | 2016-04-06 06:37:15 -0700 | [diff] [blame] | 330 | std::string getTimerName(TimerStackIdT StackID); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 331 | void setTimerName(TimerStackIdT StackID, const std::string &NewName); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 332 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 333 | /// This is the first work item sequence number that the parser produces, and |
| 334 | /// correspondingly the first sequence number that the emitter thread will |
| 335 | /// wait for. Start numbering at 1 to leave room for a sentinel, in case e.g. |
| 336 | /// we wish to inject items with a special sequence number that may be |
| 337 | /// executed out of order. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 338 | static constexpr uint32_t getFirstSequenceNumber() { return 1; } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 339 | /// Adds a newly parsed and constructed function to the Cfg work queue. |
| 340 | /// Notifies any idle workers that a new function is available for |
| 341 | /// translating. May block if the work queue is too large, in order to control |
| 342 | /// memory footprint. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 343 | void optQueueBlockingPush(std::unique_ptr<OptWorkItem> Item); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 344 | /// Takes a Cfg from the work queue for translating. May block if the work |
| 345 | /// queue is currently empty. Returns nullptr if there is no more work - the |
| 346 | /// queue is empty and either end() has been called or the Sequential flag was |
| 347 | /// set. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 348 | std::unique_ptr<OptWorkItem> optQueueBlockingPop(); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 349 | /// Notifies that no more work will be added to the work queue. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 350 | void optQueueNotifyEnd() { OptQ.notifyEnd(); } |
| 351 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 352 | /// Emit file header for output file. |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 353 | void emitFileHeader(); |
| 354 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 355 | void lowerConstants(); |
| 356 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 357 | void lowerJumpTables(); |
| 358 | |
Jaydeep Patil | 3da9f65 | 2016-11-03 22:54:06 -0700 | [diff] [blame] | 359 | /// Emit target specific read-only data sections if any. E.g., for MIPS this |
| 360 | /// generates a .MIPS.abiflags section. |
| 361 | void emitTargetRODataSections(); |
| 362 | |
John Porto | bd2e231 | 2016-03-15 11:06:25 -0700 | [diff] [blame] | 363 | void emitQueueBlockingPush(std::unique_ptr<EmitterWorkItem> Item); |
| 364 | std::unique_ptr<EmitterWorkItem> emitQueueBlockingPop(); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 365 | void emitQueueNotifyEnd() { EmitQ.notifyEnd(); } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 366 | |
Lei Zhang | 04e9b1c | 2017-05-23 15:08:59 -0700 | [diff] [blame] | 367 | void initParserThread(); |
| 368 | void startWorkerThreads(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 369 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 370 | void waitForWorkerThreads(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 371 | |
Thomas Lively | aab7099 | 2016-06-07 13:54:59 -0700 | [diff] [blame] | 372 | /// sets the instrumentation object to use. |
| 373 | void setInstrumentation(std::unique_ptr<Instrumentation> Instr) { |
| 374 | if (!BuildDefs::minimal()) |
| 375 | Instrumentor = std::move(Instr); |
| 376 | } |
| 377 | |
| 378 | void instrumentFunc(Cfg *Func) { |
| 379 | if (!BuildDefs::minimal() && Instrumentor) |
| 380 | Instrumentor->instrumentFunc(Func); |
| 381 | } |
| 382 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 383 | /// Translation thread startup routine. |
Lei Zhang | 04e9b1c | 2017-05-23 15:08:59 -0700 | [diff] [blame] | 384 | void translateFunctionsWrapper(ThreadContext *MyTLS); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 385 | /// Translate functions from the Cfg queue until the queue is empty. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 386 | void translateFunctions(); |
| 387 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 388 | /// Emitter thread startup routine. |
Lei Zhang | 04e9b1c | 2017-05-23 15:08:59 -0700 | [diff] [blame] | 389 | void emitterWrapper(ThreadContext *MyTLS); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 390 | /// Emit functions and global initializers from the emitter queue until the |
| 391 | /// queue is empty. |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 392 | void emitItems(); |
| 393 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 394 | /// Uses DataLowering to lower Globals. Side effects: |
| 395 | /// - discards the initializer list for the global variable in Globals. |
| 396 | /// - clears the Globals array. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 397 | void lowerGlobals(const std::string &SectionSuffix); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 398 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 399 | /// Lowers the profile information. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 400 | void lowerProfileData(); |
| 401 | |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 402 | void dumpConstantLookupCounts(); |
| 403 | |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 404 | /// DisposeGlobalVariablesAfterLowering controls whether the memory used by |
| 405 | /// GlobaleVariables can be reclaimed right after they have been lowered. |
| 406 | /// @{ |
| 407 | bool getDisposeGlobalVariablesAfterLowering() const { |
| 408 | return DisposeGlobalVariablesAfterLowering; |
| 409 | } |
| 410 | |
| 411 | void setDisposeGlobalVariablesAfterLowering(bool Value) { |
| 412 | DisposeGlobalVariablesAfterLowering = Value; |
| 413 | } |
| 414 | /// @} |
| 415 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 416 | LockedPtr<StringPool> getStrings() const { |
| 417 | return LockedPtr<StringPool>(Strings.get(), &StringsLock); |
| 418 | } |
| 419 | |
Thomas Lively | 2c9992a | 2016-07-20 11:19:17 -0700 | [diff] [blame] | 420 | LockedPtr<VariableDeclarationList> getGlobals() { |
| 421 | return LockedPtr<VariableDeclarationList>(&Globals, &InitAllocLock); |
| 422 | } |
| 423 | |
Karl Schimpf | 3018cf2 | 2016-04-11 14:49:01 -0700 | [diff] [blame] | 424 | /// Number of function blocks that can be queued before waiting for |
| 425 | /// translation |
| 426 | /// threads to consume. |
| 427 | static constexpr size_t MaxOptQSize = 1 << 16; |
| 428 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 429 | private: |
| 430 | // Try to ensure mutexes are allocated on separate cache lines. |
| 431 | |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 432 | // Destructors collaborate with Allocator |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 433 | ICE_CACHELINE_BOUNDARY; |
| 434 | // Managed by getAllocator() |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 435 | mutable GlobalLockType AllocLock; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 436 | ArenaAllocator Allocator; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 437 | |
| 438 | ICE_CACHELINE_BOUNDARY; |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 439 | // Managed by getInitializerAllocator() |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 440 | mutable GlobalLockType InitAllocLock; |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 441 | VariableDeclarationList Globals; |
| 442 | |
| 443 | ICE_CACHELINE_BOUNDARY; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 444 | // Managed by getDestructors() |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 445 | using DestructorArray = std::vector<std::function<void()>>; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 446 | mutable GlobalLockType DestructorsLock; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 447 | DestructorArray Destructors; |
| 448 | |
| 449 | ICE_CACHELINE_BOUNDARY; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 450 | // Managed by getStrings() |
| 451 | mutable GlobalLockType StringsLock; |
| 452 | std::unique_ptr<StringPool> Strings; |
| 453 | |
| 454 | ICE_CACHELINE_BOUNDARY; |
| 455 | // Managed by getConstPool() |
| 456 | mutable GlobalLockType ConstPoolLock; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 457 | std::unique_ptr<ConstantPool> ConstPool; |
| 458 | |
| 459 | ICE_CACHELINE_BOUNDARY; |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 460 | // Managed by getJumpTableList() |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 461 | mutable GlobalLockType JumpTablesLock; |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 462 | JumpTableDataList JumpTableList; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 463 | |
| 464 | ICE_CACHELINE_BOUNDARY; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 465 | // Managed by getErrorStatus() |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 466 | mutable GlobalLockType ErrorStatusLock; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 467 | ErrorCode ErrorStatus; |
| 468 | |
| 469 | ICE_CACHELINE_BOUNDARY; |
| 470 | // Managed by getStatsCumulative() |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 471 | mutable GlobalLockType StatsLock; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 472 | CodeStats StatsCumulative; |
| 473 | |
| 474 | ICE_CACHELINE_BOUNDARY; |
| 475 | // Managed by getTimers() |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 476 | mutable GlobalLockType TimerLock; |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 477 | TimerList Timers; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 478 | |
| 479 | ICE_CACHELINE_BOUNDARY; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 480 | /// StrLock is a global lock on the dump and emit output streams. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 481 | using StrLockType = std::mutex; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 482 | StrLockType StrLock; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 483 | Ostream *StrDump; /// Stream for dumping / diagnostics |
| 484 | Ostream *StrEmit; /// Stream for code emission |
| 485 | Ostream *StrError; /// Stream for logging errors. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 486 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 487 | // True if waitForWorkerThreads() has been called. |
| 488 | std::atomic_bool WaitForWorkerThreadsCalled; |
| 489 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 490 | ICE_CACHELINE_BOUNDARY; |
| 491 | |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 492 | Intrinsics IntrinsicsInfo; |
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; |
Karl Schimpf | 3018cf2 | 2016-04-11 14:49:01 -0700 | [diff] [blame] | 495 | // Value defining when to wake up the main parse thread. |
| 496 | const size_t OptQWakeupSize; |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 497 | BoundedProducerConsumerQueue<OptWorkItem, MaxOptQSize> OptQ; |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 498 | BoundedProducerConsumerQueue<EmitterWorkItem> EmitQ; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 499 | // DataLowering is only ever used by a single thread at a time (either in |
| 500 | // emitItems(), or in IceCompiler::run before the compilation is over.) |
| 501 | // TODO(jpp): move to EmitterContext. |
| 502 | std::unique_ptr<TargetDataLowering> DataLowering; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 503 | /// If !HasEmittedCode, SubZero will accumulate all Globals (which are "true" |
| 504 | /// program global variables) until the first code WorkItem is seen. |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 505 | // TODO(jpp): move to EmitterContext. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 506 | bool HasSeenCode = false; |
Thomas Lively | aab7099 | 2016-06-07 13:54:59 -0700 | [diff] [blame] | 507 | // If Instrumentor is not empty then it will be used to instrument globals and |
| 508 | // CFGs. |
| 509 | std::unique_ptr<Instrumentation> Instrumentor = nullptr; |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 510 | // TODO(jpp): move to EmitterContext. |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 511 | VariableDeclaration *ProfileBlockInfoVarDecl = nullptr; |
| 512 | std::vector<VariableDeclaration *> ProfileBlockInfos; |
| 513 | /// Indicates if global variable declarations can be disposed of right after |
| 514 | /// lowering. |
| 515 | bool DisposeGlobalVariablesAfterLowering = true; |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 516 | Constant *ConstZeroForType[IceType_NUM]; |
| 517 | Constant *ConstantTrue; |
| 518 | // Holds the constants representing each runtime helper function. |
| 519 | Constant *RuntimeHelperFunc[static_cast<size_t>(RuntimeHelper::H_Num)]; |
Jim Stichnoth | 217dc08 | 2014-07-11 14:06:55 -0700 | [diff] [blame] | 520 | |
Karl Schimpf | 20070e8 | 2016-03-17 13:30:13 -0700 | [diff] [blame] | 521 | Constant *getConstantZeroInternal(Type Ty); |
| 522 | Constant *getConstantIntInternal(Type Ty, int64_t Value); |
| 523 | Constant *getConstantInt1Internal(int8_t ConstantInt1); |
| 524 | Constant *getConstantInt8Internal(int8_t ConstantInt8); |
| 525 | Constant *getConstantInt16Internal(int16_t ConstantInt16); |
| 526 | Constant *getConstantInt32Internal(int32_t ConstantInt32); |
| 527 | Constant *getConstantInt64Internal(int64_t ConstantInt64); |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 528 | LockedPtr<ArenaAllocator> getAllocator() { |
| 529 | return LockedPtr<ArenaAllocator>(&Allocator, &AllocLock); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 530 | } |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 531 | LockedPtr<VariableDeclarationList> getInitializerAllocator() { |
| 532 | return LockedPtr<VariableDeclarationList>(&Globals, &InitAllocLock); |
| 533 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 534 | LockedPtr<ConstantPool> getConstPool() { |
| 535 | return LockedPtr<ConstantPool>(ConstPool.get(), &ConstPoolLock); |
| 536 | } |
Andrew Scull | 1eda90a | 2015-08-04 17:03:19 -0700 | [diff] [blame] | 537 | LockedPtr<JumpTableDataList> getJumpTableList() { |
| 538 | return LockedPtr<JumpTableDataList>(&JumpTableList, &JumpTablesLock); |
| 539 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 540 | LockedPtr<CodeStats> getStatsCumulative() { |
| 541 | return LockedPtr<CodeStats>(&StatsCumulative, &StatsLock); |
| 542 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 543 | LockedPtr<TimerList> getTimers() { |
| 544 | return LockedPtr<TimerList>(&Timers, &TimerLock); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 545 | } |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 546 | LockedPtr<DestructorArray> getDestructors() { |
| 547 | return LockedPtr<DestructorArray>(&Destructors, &DestructorsLock); |
| 548 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 549 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 550 | void accumulateGlobals(std::unique_ptr<VariableDeclarationList> Globls) { |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 551 | LockedPtr<VariableDeclarationList> _(&Globals, &InitAllocLock); |
| 552 | if (Globls != nullptr) { |
| 553 | Globals.merge(Globls.get()); |
Thomas Lively | 2c9992a | 2016-07-20 11:19:17 -0700 | [diff] [blame] | 554 | if (!BuildDefs::minimal() && Instrumentor != nullptr) |
| 555 | Instrumentor->setHasSeenGlobals(); |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 556 | } |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void lowerGlobalsIfNoCodeHasBeenSeen() { |
| 560 | if (HasSeenCode) |
| 561 | return; |
| 562 | constexpr char NoSuffix[] = ""; |
| 563 | lowerGlobals(NoSuffix); |
| 564 | HasSeenCode = true; |
| 565 | } |
| 566 | |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 567 | void saveBlockInfoPtrs(); |
John Porto | 844211e | 2016-02-04 08:42:48 -0800 | [diff] [blame] | 568 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 569 | llvm::SmallVector<ThreadContext *, 128> AllThreadContexts; |
| 570 | llvm::SmallVector<std::thread, 128> TranslationThreads; |
| 571 | llvm::SmallVector<std::thread, 128> EmitterThreads; |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 572 | // Each thread has its own TLS pointer which is also held in |
| 573 | // AllThreadContexts. |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 574 | ICE_TLS_DECLARE_FIELD(ThreadContext *, TLS); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 575 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 576 | public: |
Lei Zhang | 04e9b1c | 2017-05-23 15:08:59 -0700 | [diff] [blame] | 577 | static void TlsInit(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 578 | }; |
| 579 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 580 | /// Helper class to push and pop a timer marker. The constructor pushes a |
| 581 | /// marker, and the destructor pops it. This is for convenient timing of regions |
| 582 | /// of code. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 583 | class TimerMarker { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 584 | TimerMarker() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 585 | TimerMarker(const TimerMarker &) = delete; |
| 586 | TimerMarker &operator=(const TimerMarker &) = delete; |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 587 | |
| 588 | public: |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 589 | TimerMarker(TimerIdT ID, GlobalContext *Ctx, |
| 590 | TimerStackIdT StackID = GlobalContext::TSK_Default) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 591 | : ID(ID), Ctx(Ctx), StackID(StackID) { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 592 | if (BuildDefs::timers()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 593 | push(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 594 | } |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 595 | TimerMarker(TimerIdT ID, const Cfg *Func, |
| 596 | TimerStackIdT StackID = GlobalContext::TSK_Default) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 597 | : ID(ID), Ctx(nullptr), StackID(StackID) { |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 598 | // Ctx gets set at the beginning of pushCfg(). |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 599 | if (BuildDefs::timers()) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 600 | pushCfg(Func); |
| 601 | } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 602 | TimerMarker(GlobalContext *Ctx, const std::string &FuncName) |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 603 | : ID(getTimerIdFromFuncName(Ctx, FuncName)), Ctx(Ctx), |
| 604 | StackID(GlobalContext::TSK_Funcs) { |
| 605 | if (BuildDefs::timers()) |
| 606 | push(); |
| 607 | } |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 608 | |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 609 | ~TimerMarker() { |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 610 | if (BuildDefs::timers() && Active) |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 611 | Ctx->popTimer(ID, StackID); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | private: |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 615 | void push(); |
| 616 | void pushCfg(const Cfg *Func); |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 617 | static TimerIdT getTimerIdFromFuncName(GlobalContext *Ctx, |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 618 | const std::string &FuncName); |
Jim Stichnoth | 380d7b9 | 2015-01-30 13:10:39 -0800 | [diff] [blame] | 619 | const TimerIdT ID; |
| 620 | GlobalContext *Ctx; |
| 621 | const TimerStackIdT StackID; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 622 | bool Active = false; |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 625 | /// Helper class for locking the streams and then automatically unlocking them. |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 626 | class OstreamLocker { |
| 627 | private: |
| 628 | OstreamLocker() = delete; |
| 629 | OstreamLocker(const OstreamLocker &) = delete; |
| 630 | OstreamLocker &operator=(const OstreamLocker &) = delete; |
| 631 | |
| 632 | public: |
| 633 | explicit OstreamLocker(GlobalContext *Ctx) : Ctx(Ctx) { Ctx->lockStr(); } |
| 634 | ~OstreamLocker() { Ctx->unlockStr(); } |
| 635 | |
| 636 | private: |
| 637 | GlobalContext *const Ctx; |
| 638 | }; |
| 639 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 640 | } // end of namespace Ice |
| 641 | |
| 642 | #endif // SUBZERO_SRC_ICEGLOBALCONTEXT_H |