Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceDefs.h - Common Subzero declarations ------*- C++ -*-===// |
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 Declares various useful types and classes that have widespread use |
| 12 | /// across Subzero. |
| 13 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef SUBZERO_SRC_ICEDEFS_H |
| 17 | #define SUBZERO_SRC_ICEDEFS_H |
| 18 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 19 | #include "IceBuildDefs.h" // TODO(stichnot): move into individual files |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 20 | #include "IceMemory.h" |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 21 | #include "IceTLS.h" |
| 22 | |
Jan Voung | b17f61d | 2014-08-28 16:00:53 -0700 | [diff] [blame] | 23 | #include "llvm/ADT/ArrayRef.h" |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 24 | #include "llvm/ADT/ilist.h" |
| 25 | #include "llvm/ADT/ilist_node.h" |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 26 | #include "llvm/ADT/iterator_range.h" |
Jim Stichnoth | 586d4c2 | 2014-12-05 16:43:08 -0800 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
| 29 | #include "llvm/Support/Casting.h" |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 30 | #include "llvm/Support/ELF.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 32 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 33 | #include <cassert> |
| 34 | #include <cstdint> |
| 35 | #include <cstdio> // snprintf |
| 36 | #include <functional> // std::less |
| 37 | #include <limits> |
| 38 | #include <list> |
| 39 | #include <map> |
| 40 | #include <memory> |
| 41 | #include <mutex> |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 42 | #include <set> |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 43 | #include <string> |
| 44 | #include <system_error> |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 45 | #include <unordered_map> |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 46 | #include <unordered_set> |
| 47 | #include <utility> |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 48 | #include <vector> |
Jim Stichnoth | 7d53825 | 2015-01-23 10:22:56 -0800 | [diff] [blame] | 49 | |
Jim Stichnoth | 84ea9a7 | 2016-09-09 09:33:07 -0700 | [diff] [blame] | 50 | #define XSTRINGIFY(x) STRINGIFY(x) |
| 51 | #define STRINGIFY(x) #x |
| 52 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 53 | namespace Ice { |
| 54 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 55 | class Assembler; |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 56 | template <template <typename> class> class BitVectorTmpl; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 57 | class Cfg; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 58 | class CfgNode; |
| 59 | class Constant; |
Nicolas Capens | 3e37647 | 2016-09-13 11:35:57 -0400 | [diff] [blame] | 60 | class ELFFileStreamer; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 61 | class ELFObjectWriter; |
| 62 | class ELFStreamer; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 63 | class FunctionDeclaration; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 64 | class GlobalContext; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 65 | class GlobalDeclaration; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 66 | class Inst; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 67 | class InstAssign; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 68 | class InstJumpTable; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 69 | class InstPhi; |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 70 | class InstSwitch; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 71 | class InstTarget; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 72 | class LiveRange; |
| 73 | class Liveness; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 74 | class Operand; |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 75 | class TargetDataLowering; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 76 | class TargetLowering; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 77 | class Variable; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 78 | class VariableDeclaration; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 79 | class VariablesMetadata; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 80 | |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 81 | /// SizeT is for holding small-ish limits like number of source operands in an |
| 82 | /// instruction. It is used instead of size_t (which may be 64-bits wide) when |
| 83 | /// we want to save space. |
| 84 | using SizeT = uint32_t; |
| 85 | |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 86 | constexpr char GlobalOffsetTable[] = "_GLOBAL_OFFSET_TABLE_"; |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 87 | // makeUnique should be used when memory is expected to be allocated from the |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 88 | // heap (as opposed to allocated from some Allocator.) It is intended to be |
| 89 | // used instead of new. |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 90 | // |
| 91 | // The expected usage is as follows |
| 92 | // |
| 93 | // class MyClass { |
| 94 | // public: |
| 95 | // static std::unique_ptr<MyClass> create(<ctor_args>) { |
| 96 | // return makeUnique<MyClass>(<ctor_args>); |
| 97 | // } |
| 98 | // |
| 99 | // private: |
| 100 | // ENABLE_MAKE_UNIQUE; |
| 101 | // |
| 102 | // MyClass(<ctor_args>) ... |
| 103 | // } |
| 104 | // |
| 105 | // ENABLE_MAKE_UNIQUE is a trick that is necessary if MyClass' ctor is private. |
| 106 | // Private ctors are highly encouraged when you're writing a class that you'd |
| 107 | // like to have allocated with makeUnique as it would prevent users from |
| 108 | // declaring stack allocated variables. |
| 109 | namespace Internal { |
| 110 | struct MakeUniqueEnabler { |
| 111 | template <class T, class... Args> |
| 112 | static std::unique_ptr<T> create(Args &&... TheArgs) { |
| 113 | std::unique_ptr<T> Unique(new T(std::forward<Args>(TheArgs)...)); |
| 114 | return Unique; |
| 115 | } |
| 116 | }; |
| 117 | } // end of namespace Internal |
| 118 | |
| 119 | template <class T, class... Args> |
| 120 | static std::unique_ptr<T> makeUnique(Args &&... TheArgs) { |
| 121 | return ::Ice::Internal::MakeUniqueEnabler::create<T>( |
| 122 | std::forward<Args>(TheArgs)...); |
| 123 | } |
| 124 | |
| 125 | #define ENABLE_MAKE_UNIQUE friend struct ::Ice::Internal::MakeUniqueEnabler |
| 126 | |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 127 | using InstList = llvm::ilist<Inst>; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 128 | // Ideally PhiList would be llvm::ilist<InstPhi>, and similar for AssignList, |
| 129 | // but this runs into issues with SFINAE. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 130 | using PhiList = InstList; |
| 131 | using AssignList = InstList; |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 132 | |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 133 | // Standard library containers with CfgLocalAllocator. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 134 | template <typename T> using CfgList = std::list<T, CfgLocalAllocator<T>>; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 135 | template <typename T, typename H = std::hash<T>, typename Eq = std::equal_to<T>> |
| 136 | using CfgUnorderedSet = std::unordered_set<T, H, Eq, CfgLocalAllocator<T>>; |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 137 | template <typename T, typename Cmp = std::less<T>> |
| 138 | using CfgSet = std::set<T, Cmp, CfgLocalAllocator<T>>; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 139 | template <typename T, typename U, typename H = std::hash<T>, |
| 140 | typename Eq = std::equal_to<T>> |
| 141 | using CfgUnorderedMap = |
| 142 | std::unordered_map<T, U, H, Eq, CfgLocalAllocator<std::pair<const T, U>>>; |
| 143 | template <typename T> using CfgVector = std::vector<T, CfgLocalAllocator<T>>; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 144 | |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 145 | // Containers that are arena-allocated from the Cfg's allocator. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 146 | using OperandList = CfgVector<Operand *>; |
| 147 | using VarList = CfgVector<Variable *>; |
| 148 | using NodeList = CfgVector<CfgNode *>; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 149 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 150 | // Containers that use the default (global) allocator. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 151 | using ConstantList = std::vector<Constant *>; |
| 152 | using FunctionDeclarationList = std::vector<FunctionDeclaration *>; |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 153 | |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 154 | /// VariableDeclarationList is a container for holding VariableDeclarations -- |
| 155 | /// i.e., Global Variables. It is also used to create said variables, and their |
| 156 | /// initializers in an arena. |
| 157 | class VariableDeclarationList { |
| 158 | VariableDeclarationList(const VariableDeclarationList &) = delete; |
| 159 | VariableDeclarationList &operator=(const VariableDeclarationList &) = delete; |
| 160 | VariableDeclarationList(VariableDeclarationList &&) = delete; |
| 161 | VariableDeclarationList &operator=(VariableDeclarationList &&) = delete; |
| 162 | |
| 163 | public: |
| 164 | using VariableDeclarationArray = std::vector<VariableDeclaration *>; |
| 165 | |
| 166 | VariableDeclarationList() : Arena(new ArenaAllocator()) {} |
| 167 | |
| 168 | ~VariableDeclarationList() { clearAndPurge(); } |
| 169 | |
| 170 | template <typename T> T *allocate_initializer(SizeT Count = 1) { |
| 171 | static_assert( |
| 172 | std::is_trivially_destructible<T>::value, |
| 173 | "allocate_initializer can only allocate trivially destructible types."); |
| 174 | return Arena->Allocate<T>(Count); |
| 175 | } |
| 176 | |
| 177 | template <typename T> T *allocate_variable_declaration() { |
| 178 | static_assert(!std::is_trivially_destructible<T>::value, |
| 179 | "allocate_variable_declaration expects non-trivially " |
| 180 | "destructible types."); |
| 181 | T *Ret = Arena->Allocate<T>(); |
| 182 | Dtors.emplace_back([Ret]() { Ret->~T(); }); |
| 183 | return Ret; |
| 184 | } |
| 185 | |
| 186 | // This do nothing method is invoked when a global variable is created, but it |
Thomas Lively | 3f5cb6f | 2016-06-13 11:23:29 -0700 | [diff] [blame] | 187 | // will not be emitted. If we ever need to track the created variable, having |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 188 | // this hook is handy. |
| 189 | void willNotBeEmitted(VariableDeclaration *) {} |
| 190 | |
| 191 | /// Merges Other with this, effectively resetting Other to an empty state. |
| 192 | void merge(VariableDeclarationList *Other) { |
| 193 | assert(Other != nullptr); |
| 194 | addArena(std::move(Other->Arena)); |
| 195 | for (std::size_t i = 0; i < Other->MergedArenas.size(); ++i) { |
| 196 | addArena(std::move(Other->MergedArenas[i])); |
| 197 | } |
| 198 | Other->MergedArenas.clear(); |
| 199 | |
| 200 | Dtors.insert(Dtors.end(), Other->Dtors.begin(), Other->Dtors.end()); |
| 201 | Other->Dtors.clear(); |
| 202 | |
| 203 | Globals.insert(Globals.end(), Other->Globals.begin(), Other->Globals.end()); |
| 204 | Other->Globals.clear(); |
| 205 | } |
| 206 | |
| 207 | /// Destroys all GlobalVariables and initializers that this knows about |
| 208 | /// (including those merged with it), and releases memory. |
| 209 | void clearAndPurge() { |
| 210 | if (Arena == nullptr) { |
| 211 | // Arena is only null if this was merged, so we ensure there's no state |
| 212 | // being held by this. |
| 213 | assert(Dtors.empty()); |
| 214 | assert(Globals.empty()); |
| 215 | assert(MergedArenas.empty()); |
| 216 | return; |
| 217 | } |
| 218 | // Invokes destructors in reverse creation order. |
| 219 | for (auto Dtor = Dtors.rbegin(); Dtor != Dtors.rend(); ++Dtor) { |
| 220 | (*Dtor)(); |
| 221 | } |
| 222 | Dtors.clear(); |
| 223 | Globals.clear(); |
| 224 | MergedArenas.clear(); |
| 225 | Arena->Reset(); |
| 226 | } |
| 227 | |
| 228 | /// Adapt the relevant parts of the std::vector<VariableDeclaration *> |
| 229 | /// interface. |
| 230 | /// @{ |
| 231 | VariableDeclarationArray::iterator begin() { return Globals.begin(); } |
| 232 | |
| 233 | VariableDeclarationArray::iterator end() { return Globals.end(); } |
| 234 | |
| 235 | VariableDeclarationArray::const_iterator begin() const { |
| 236 | return Globals.begin(); |
| 237 | } |
| 238 | |
| 239 | VariableDeclarationArray::const_iterator end() const { return Globals.end(); } |
| 240 | |
| 241 | bool empty() const { return Globals.empty(); } |
| 242 | |
| 243 | VariableDeclarationArray::size_type size() const { return Globals.size(); } |
| 244 | |
| 245 | VariableDeclarationArray::reference |
| 246 | at(VariableDeclarationArray::size_type Pos) { |
| 247 | return Globals.at(Pos); |
| 248 | } |
| 249 | |
| 250 | void push_back(VariableDeclaration *Global) { Globals.push_back(Global); } |
| 251 | |
| 252 | void reserve(VariableDeclarationArray::size_type Capacity) { |
| 253 | Globals.reserve(Capacity); |
| 254 | } |
| 255 | |
| 256 | void clear() { Globals.clear(); } |
| 257 | |
| 258 | VariableDeclarationArray::reference back() { return Globals.back(); } |
| 259 | /// @} |
| 260 | |
| 261 | private: |
| 262 | using ArenaPtr = std::unique_ptr<ArenaAllocator>; |
| 263 | using DestructorsArray = std::vector<std::function<void()>>; |
| 264 | |
| 265 | void addArena(ArenaPtr NewArena) { |
| 266 | MergedArenas.emplace_back(std::move(NewArena)); |
| 267 | } |
| 268 | |
| 269 | ArenaPtr Arena; |
| 270 | VariableDeclarationArray Globals; |
| 271 | DestructorsArray Dtors; |
| 272 | std::vector<ArenaPtr> MergedArenas; |
| 273 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 274 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 275 | /// InstNumberT is for holding an instruction number. Instruction numbers are |
| 276 | /// used for representing Variable live ranges. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 277 | using InstNumberT = int32_t; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 278 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 279 | /// A LiveBeginEndMapEntry maps a Variable::Number value to an Inst::Number |
| 280 | /// value, giving the instruction number that begins or ends a variable's live |
| 281 | /// range. |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 282 | template <typename T> |
| 283 | using LivenessVector = std::vector<T, LivenessAllocator<T>>; |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 284 | using LiveBeginEndMapEntry = std::pair<SizeT, InstNumberT>; |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 285 | using LiveBeginEndMap = LivenessVector<LiveBeginEndMapEntry>; |
| 286 | using LivenessBV = BitVectorTmpl<LivenessAllocator>; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 287 | |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 288 | using TimerStackIdT = uint32_t; |
| 289 | using TimerIdT = uint32_t; |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 290 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 291 | /// Use alignas(MaxCacheLineSize) to isolate variables/fields that might be |
| 292 | /// contended while multithreading. Assumes the maximum cache line size is 64. |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 293 | enum { MaxCacheLineSize = 64 }; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 294 | // Use ICE_CACHELINE_BOUNDARY to force the next field in a declaration |
| 295 | // list to be aligned to the next cache line. |
Nicolas Capens | d5e7caf | 2016-09-01 11:40:49 -0400 | [diff] [blame] | 296 | #if defined(_MSC_VER) |
| 297 | #define ICE_CACHELINE_BOUNDARY __declspec(align(MaxCacheLineSize)) int : 0; |
| 298 | #else // !defined(_MSC_VER) |
JF Bastien | 867684e | 2015-01-28 15:07:38 -0800 | [diff] [blame] | 299 | // Note: zero is added to work around the following GCC 4.8 bug (fixed in 4.9): |
| 300 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55382 |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 301 | #define ICE_CACHELINE_BOUNDARY \ |
JF Bastien | 867684e | 2015-01-28 15:07:38 -0800 | [diff] [blame] | 302 | __attribute__((aligned(MaxCacheLineSize + 0))) int : 0 |
Nicolas Capens | d5e7caf | 2016-09-01 11:40:49 -0400 | [diff] [blame] | 303 | #endif // !defined(_MSC_VER) |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 304 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 305 | /// PNaCl is ILP32, so theoretically we should only need 32-bit offsets. |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 306 | using RelocOffsetT = int32_t; |
Jan Voung | c0d965f | 2014-11-04 16:55:01 -0800 | [diff] [blame] | 307 | enum { RelocAddrSize = 4 }; |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 308 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 309 | enum LivenessMode { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 310 | /// Basic version of live-range-end calculation. Marks the last uses of |
| 311 | /// variables based on dataflow analysis. Records the set of live-in and |
| 312 | /// live-out variables for each block. Identifies and deletes dead |
| 313 | /// instructions (primarily stores). |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 314 | Liveness_Basic, |
| 315 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 316 | /// In addition to Liveness_Basic, also calculate the complete live range for |
| 317 | /// each variable in a form suitable for interference calculation and register |
| 318 | /// allocation. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 319 | Liveness_Intervals |
| 320 | }; |
| 321 | |
Manasij Mukherjee | 53c8fbd | 2016-08-01 15:40:42 -0700 | [diff] [blame] | 322 | enum LCSEOptions { |
| 323 | LCSE_Disabled, |
| 324 | LCSE_EnabledSSA, // Default Mode, assumes SSA. |
| 325 | LCSE_EnabledNoSSA // Does not assume SSA, to be enabled if CSE is done later. |
| 326 | }; |
| 327 | |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 328 | enum RegAllocKind { |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 329 | RAK_Unknown, |
Jim Stichnoth | 4001c93 | 2015-10-09 14:33:26 -0700 | [diff] [blame] | 330 | RAK_Global, /// full, global register allocation |
| 331 | RAK_SecondChance, /// second-chance bin-packing after full regalloc attempt |
| 332 | RAK_Phi, /// infinite-weight Variables with active spilling/filling |
| 333 | RAK_InfOnly /// allocation only for infinite-weight Variables |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 334 | }; |
| 335 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 336 | enum VerboseItem { |
| 337 | IceV_None = 0, |
| 338 | IceV_Instructions = 1 << 0, |
| 339 | IceV_Deleted = 1 << 1, |
| 340 | IceV_InstNumbers = 1 << 2, |
| 341 | IceV_Preds = 1 << 3, |
| 342 | IceV_Succs = 1 << 4, |
| 343 | IceV_Liveness = 1 << 5, |
Jim Stichnoth | 769be68 | 2015-01-15 09:05:08 -0800 | [diff] [blame] | 344 | IceV_RegOrigins = 1 << 6, |
| 345 | IceV_LinearScan = 1 << 7, |
| 346 | IceV_Frame = 1 << 8, |
| 347 | IceV_AddrOpt = 1 << 9, |
| 348 | IceV_Random = 1 << 10, |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 349 | IceV_Folding = 1 << 11, |
Jim Stichnoth | e4f65d8 | 2015-06-17 22:16:02 -0700 | [diff] [blame] | 350 | IceV_RMW = 1 << 12, |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 351 | IceV_Loop = 1 << 13, |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 352 | IceV_Mem = 1 << 14, |
| 353 | // Leave some extra space to make it easier to add new per-pass items. |
| 354 | IceV_NO_PER_PASS_DUMP_BEYOND = 1 << 19, |
| 355 | // Items greater than IceV_NO_PER_PASS_DUMP_BEYOND don't by themselves trigger |
| 356 | // per-pass Cfg dump output. |
| 357 | IceV_Status = 1 << 20, |
| 358 | IceV_AvailableRegs = 1 << 21, |
| 359 | IceV_GlobalInit = 1 << 22, |
| 360 | IceV_ConstPoolStats = 1 << 23, |
Eric Holk | 16f8061 | 2016-04-04 17:07:42 -0700 | [diff] [blame] | 361 | IceV_Wasm = 1 << 24, |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 362 | IceV_ShufMat = 1 << 25, |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 363 | IceV_All = ~IceV_None, |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 364 | IceV_Most = |
Jim Stichnoth | 76719b4 | 2016-03-14 08:37:52 -0700 | [diff] [blame] | 365 | IceV_All & ~IceV_LinearScan & ~IceV_GlobalInit & ~IceV_ConstPoolStats |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 366 | }; |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 367 | using VerboseMask = uint32_t; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 368 | |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 369 | enum FileType { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 370 | FT_Elf, /// ELF .o file |
| 371 | FT_Asm, /// Assembly .s file |
| 372 | FT_Iasm /// "Integrated assembler" .byte-style .s file |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 373 | }; |
| 374 | |
Nicolas Capens | 41ce47c | 2016-09-16 17:00:12 -0400 | [diff] [blame] | 375 | enum ABI { |
| 376 | ABI_PNaCl, /// x32 for unsandboxed 64-bit x86 |
| 377 | ABI_Platform /// Native executable ABI |
| 378 | }; |
| 379 | |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 380 | using Ostream = llvm::raw_ostream; |
| 381 | using Fdstream = llvm::raw_fd_ostream; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 382 | |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 383 | using GlobalLockType = std::mutex; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 384 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 385 | /// LockedPtr is an RAII wrapper that allows automatically locked access to a |
| 386 | /// given pointer, automatically unlocking it when when the LockedPtr goes out |
| 387 | /// of scope. |
| 388 | template <typename T> class LockedPtr { |
| 389 | LockedPtr() = delete; |
| 390 | LockedPtr(const LockedPtr &) = delete; |
| 391 | LockedPtr &operator=(const LockedPtr &) = delete; |
| 392 | |
| 393 | public: |
| 394 | LockedPtr(T *Value, GlobalLockType *Lock) : Value(Value), Lock(Lock) { |
| 395 | Lock->lock(); |
| 396 | } |
| 397 | LockedPtr(LockedPtr &&Other) : Value(Other.Value), Lock(Other.Lock) { |
| 398 | Other.Value = nullptr; |
| 399 | Other.Lock = nullptr; |
| 400 | } |
Nicolas Capens | 6c629dc | 2017-04-25 16:41:00 -0400 | [diff] [blame] | 401 | ~LockedPtr() { |
| 402 | if (Lock != nullptr) |
| 403 | Lock->unlock(); |
| 404 | } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 405 | T *operator->() const { return Value; } |
| 406 | T &operator*() const { return *Value; } |
| 407 | T *get() { return Value; } |
| 408 | |
| 409 | private: |
| 410 | T *Value; |
| 411 | GlobalLockType *Lock; |
| 412 | }; |
| 413 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 414 | enum ErrorCodes { EC_None = 0, EC_Args, EC_Bitcode, EC_Translation }; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 415 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 416 | /// Wrapper around std::error_code for allowing multiple errors to be folded |
| 417 | /// into one. The current implementation keeps track of the first error, which |
| 418 | /// is likely to be the most useful one, and this could be extended to e.g. |
| 419 | /// collect a vector of errors. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 420 | class ErrorCode : public std::error_code { |
| 421 | ErrorCode(const ErrorCode &) = delete; |
| 422 | ErrorCode &operator=(const ErrorCode &) = delete; |
| 423 | |
| 424 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 425 | ErrorCode() = default; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 426 | void assign(ErrorCodes Code) { |
| 427 | if (!HasError) { |
| 428 | HasError = true; |
| 429 | std::error_code::assign(Code, std::generic_category()); |
| 430 | } |
| 431 | } |
| 432 | void assign(int Code) { assign(static_cast<ErrorCodes>(Code)); } |
| 433 | |
| 434 | private: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 435 | bool HasError = false; |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 436 | }; |
| 437 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 438 | /// Reverse range adaptors written in terms of llvm::make_range(). |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 439 | template <typename T> |
| 440 | llvm::iterator_range<typename T::const_reverse_iterator> |
| 441 | reverse_range(const T &Container) { |
| 442 | return llvm::make_range(Container.rbegin(), Container.rend()); |
| 443 | } |
| 444 | template <typename T> |
| 445 | llvm::iterator_range<typename T::reverse_iterator> reverse_range(T &Container) { |
| 446 | return llvm::make_range(Container.rbegin(), Container.rend()); |
| 447 | } |
| 448 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 449 | /// Options for pooling and randomization of immediates. |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 450 | enum RandomizeAndPoolImmediatesEnum { RPI_None, RPI_Randomize, RPI_Pool }; |
| 451 | |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 452 | /// Salts for Random number generator for different randomization passes. |
| 453 | enum RandomizationPassesEnum { |
| 454 | RPE_BasicBlockReordering, |
| 455 | RPE_ConstantBlinding, |
| 456 | RPE_FunctionReordering, |
| 457 | RPE_GlobalVariableReordering, |
| 458 | RPE_NopInsertion, |
| 459 | RPE_PooledConstantReordering, |
| 460 | RPE_RegAllocRandomization, |
| 461 | RPE_num |
| 462 | }; |
| 463 | |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 464 | using RelocOffsetArray = llvm::SmallVector<class RelocOffset *, 4>; |
| 465 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 466 | } // end of namespace Ice |
| 467 | |
| 468 | #endif // SUBZERO_SRC_ICEDEFS_H |