blob: 4f58ede4efa8fe2f43b966d2adf0604f5a4294ab [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- subzero/src/IceDefs.h - Common Subzero declaraions -------*- 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//===----------------------------------------------------------------------===//
9//
10// This file declares various useful types and classes that have
11// widespread use across Subzero. Every Subzero source file is
12// expected to include IceDefs.h.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef SUBZERO_SRC_ICEDEFS_H
17#define SUBZERO_SRC_ICEDEFS_H
18
Jim Stichnothf7c9a142014-04-29 10:52:43 -070019#include <cassert>
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070020#include <cstdint>
Jim Stichnothf7c9a142014-04-29 10:52:43 -070021#include <cstdio> // snprintf
22#include <functional> // std::less
Jan Voungbc004632014-09-16 15:09:10 -070023#include <limits>
Jim Stichnothf7c9a142014-04-29 10:52:43 -070024#include <list>
25#include <map>
Jim Stichnothf7c9a142014-04-29 10:52:43 -070026#include <string>
27#include <vector>
Jan Voungb17f61d2014-08-28 16:00:53 -070028#include "llvm/ADT/ArrayRef.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070029#include "llvm/ADT/BitVector.h"
Jim Stichnoth607e9f02014-11-06 13:32:05 -080030#include "llvm/ADT/ilist.h"
31#include "llvm/ADT/ilist_node.h"
Jim Stichnoth7e571362015-01-09 11:43:26 -080032#include "llvm/ADT/iterator_range.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070033#include "llvm/ADT/SmallBitVector.h"
Jim Stichnoth586d4c22014-12-05 16:43:08 -080034#include "llvm/ADT/SmallVector.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070035#include "llvm/ADT/STLExtras.h"
Jim Stichnoth31c95592014-12-19 12:51:35 -080036#include "llvm/Support/Allocator.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070037#include "llvm/Support/Casting.h"
Jan Voung08c3bcd2014-12-01 17:55:16 -080038#include "llvm/Support/ELF.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070039#include "llvm/Support/raw_ostream.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070040
41namespace Ice {
42
Jan Voungec270732015-01-12 17:00:22 -080043class Assembler;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070044class Cfg;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070045class CfgNode;
46class Constant;
Jan Voungec270732015-01-12 17:00:22 -080047class ELFObjectWriter;
48class ELFStreamer;
Karl Schimpf9d98d792014-10-13 15:01:08 -070049class FunctionDeclaration;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070050class GlobalContext;
Karl Schimpf9d98d792014-10-13 15:01:08 -070051class GlobalDeclaration;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070052class Inst;
Jim Stichnoth336f6c42014-10-30 15:01:31 -070053class InstAssign;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070054class InstPhi;
55class InstTarget;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070056class LiveRange;
57class Liveness;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070058class Operand;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070059class TargetLowering;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070060class Variable;
Karl Schimpf9d98d792014-10-13 15:01:08 -070061class VariableDeclaration;
Jim Stichnoth144cdce2014-09-22 16:02:59 -070062class VariablesMetadata;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070063
Jan Voung1d62cf02015-01-09 14:57:32 -080064template <size_t SlabSize = 1024 * 1024>
65using ArenaAllocator =
66 llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, SlabSize>;
Jim Stichnoth31c95592014-12-19 12:51:35 -080067
Jan Voung1d62cf02015-01-09 14:57:32 -080068ArenaAllocator<> *getCurrentCfgAllocator();
Jim Stichnoth31c95592014-12-19 12:51:35 -080069
70template <typename T> struct CfgLocalAllocator {
71 using value_type = T;
72 CfgLocalAllocator() = default;
73 template <class U> CfgLocalAllocator(const CfgLocalAllocator<U> &) {}
74 T *allocate(std::size_t Num) {
75 return getCurrentCfgAllocator()->Allocate<T>(Num);
76 }
77 void deallocate(T *, std::size_t) {}
78};
79template <typename T, typename U>
80inline bool operator==(const CfgLocalAllocator<T> &,
81 const CfgLocalAllocator<U> &) {
82 return true;
83}
84template <typename T, typename U>
85inline bool operator!=(const CfgLocalAllocator<T> &,
86 const CfgLocalAllocator<U> &) {
87 return false;
88}
89
Jim Stichnothf7c9a142014-04-29 10:52:43 -070090typedef std::string IceString;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080091typedef llvm::ilist<Inst> InstList;
Jim Stichnoth1502e592014-12-11 09:22:45 -080092// Ideally PhiList would be llvm::ilist<InstPhi>, and similar for
93// AssignList, but this runs into issues with SFINAE.
94typedef InstList PhiList;
95typedef InstList AssignList;
Jim Stichnoth31c95592014-12-19 12:51:35 -080096// VarList and NodeList are arena-allocated from the Cfg's allocator.
97typedef std::vector<Variable *, CfgLocalAllocator<Variable *>> VarList;
98typedef std::vector<CfgNode *, CfgLocalAllocator<CfgNode *>> NodeList;
Jim Stichnothf61d5b22014-05-23 13:31:24 -070099typedef std::vector<Constant *> ConstantList;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700100
101// SizeT is for holding small-ish limits like number of source
102// operands in an instruction. It is used instead of size_t (which
103// may be 64-bits wide) when we want to save space.
104typedef uint32_t SizeT;
105
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700106// InstNumberT is for holding an instruction number. Instruction
107// numbers are used for representing Variable live ranges.
108typedef int32_t InstNumberT;
109
Jim Stichnoth47752552014-10-13 17:15:08 -0700110// A LiveBeginEndMapEntry maps a Variable::Number value to an
111// Inst::Number value, giving the instruction number that begins or
112// ends a variable's live range.
113typedef std::pair<SizeT, InstNumberT> LiveBeginEndMapEntry;
Jim Stichnothc599e462015-01-08 16:56:49 -0800114typedef std::vector<LiveBeginEndMapEntry,
115 CfgLocalAllocator<LiveBeginEndMapEntry> > LiveBeginEndMap;
Jim Stichnoth47752552014-10-13 17:15:08 -0700116typedef llvm::BitVector LivenessBV;
117
Jim Stichnoth8363a062014-10-07 10:02:38 -0700118typedef uint32_t TimerStackIdT;
Jim Stichnothc4554d72014-09-30 16:49:38 -0700119typedef uint32_t TimerIdT;
120
Jan Voungfe14fb82014-10-13 15:56:32 -0700121// PNaCl is ILP32, so theoretically we should only need 32-bit offsets.
122typedef int32_t RelocOffsetT;
Jan Voungc0d965f2014-11-04 16:55:01 -0800123enum { RelocAddrSize = 4 };
Jan Voungfe14fb82014-10-13 15:56:32 -0700124
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700125enum LivenessMode {
126 // Basic version of live-range-end calculation. Marks the last uses
127 // of variables based on dataflow analysis. Records the set of
128 // live-in and live-out variables for each block. Identifies and
129 // deletes dead instructions (primarily stores).
130 Liveness_Basic,
131
132 // In addition to Liveness_Basic, also calculate the complete
133 // live range for each variable in a form suitable for interference
134 // calculation and register allocation.
135 Liveness_Intervals
136};
137
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800138enum RegAllocKind {
139 RAK_Global, // full, global register allocation
140 RAK_InfOnly // allocation only for infinite-weight Variables
141};
142
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700143enum VerboseItem {
144 IceV_None = 0,
145 IceV_Instructions = 1 << 0,
146 IceV_Deleted = 1 << 1,
147 IceV_InstNumbers = 1 << 2,
148 IceV_Preds = 1 << 3,
149 IceV_Succs = 1 << 4,
150 IceV_Liveness = 1 << 5,
151 IceV_RegManager = 1 << 6,
152 IceV_RegOrigins = 1 << 7,
153 IceV_LinearScan = 1 << 8,
154 IceV_Frame = 1 << 9,
Jim Stichnothc4554d72014-09-30 16:49:38 -0700155 IceV_AddrOpt = 1 << 10,
Jim Stichnothe6d24782014-12-19 05:42:24 -0800156 IceV_Random = 1 << 11,
Jim Stichnothad403532014-09-25 12:44:17 -0700157 IceV_All = ~IceV_None,
Jim Stichnothc4554d72014-09-30 16:49:38 -0700158 IceV_Most = IceV_All & ~IceV_LinearScan
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700159};
160typedef uint32_t VerboseMask;
161
Jim Stichnoth78282f62014-07-27 23:14:00 -0700162typedef llvm::raw_ostream Ostream;
Jan Voung08c3bcd2014-12-01 17:55:16 -0800163typedef llvm::raw_fd_ostream Fdstream;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700164
Jim Stichnoth7e571362015-01-09 11:43:26 -0800165// Reverse range adaptors written in terms of llvm::make_range().
166template <typename T>
167llvm::iterator_range<typename T::const_reverse_iterator>
168reverse_range(const T &Container) {
169 return llvm::make_range(Container.rbegin(), Container.rend());
170}
171template <typename T>
172llvm::iterator_range<typename T::reverse_iterator> reverse_range(T &Container) {
173 return llvm::make_range(Container.rbegin(), Container.rend());
174}
175
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700176} // end of namespace Ice
177
178#endif // SUBZERO_SRC_ICEDEFS_H