blob: d77a5ddb554c4e1f3ec68232da712a6cc72152d3 [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- subzero/src/IceGlobalContext.cpp - 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//===----------------------------------------------------------------------===//
9//
10// This file defines aspects of the compilation that persist across
11// multiple functions.
12//
13//===----------------------------------------------------------------------===//
14
Jim Stichnothd97c7df2014-06-04 11:57:08 -070015#include <ctype.h> // isdigit()
16
Jim Stichnothf7c9a142014-04-29 10:52:43 -070017#include "IceDefs.h"
18#include "IceTypes.h"
19#include "IceCfg.h"
20#include "IceGlobalContext.h"
21#include "IceOperand.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070022#include "IceTargetLowering.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070023
24namespace Ice {
25
26// TypePool maps constants of type KeyType (e.g. float) to pointers to
27// type ValueType (e.g. ConstantFloat). KeyType values are compared
28// using memcmp() because of potential NaN values in KeyType values.
29// KeyTypeHasFP indicates whether KeyType is a floating-point type
30// whose values need to be compared using memcmp() for NaN
31// correctness. TODO: use std::is_floating_point<KeyType> instead of
32// KeyTypeHasFP with C++11.
33template <typename KeyType, typename ValueType, bool KeyTypeHasFP = false>
34class TypePool {
35 TypePool(const TypePool &) LLVM_DELETED_FUNCTION;
36 TypePool &operator=(const TypePool &) LLVM_DELETED_FUNCTION;
37
38public:
Jim Stichnothf61d5b22014-05-23 13:31:24 -070039 TypePool() : NextPoolID(0) {}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070040 ValueType *getOrAdd(GlobalContext *Ctx, Type Ty, KeyType Key) {
41 TupleType TupleKey = std::make_pair(Ty, Key);
42 typename ContainerType::const_iterator Iter = Pool.find(TupleKey);
43 if (Iter != Pool.end())
44 return Iter->second;
Jim Stichnothf61d5b22014-05-23 13:31:24 -070045 ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++);
Jim Stichnothf7c9a142014-04-29 10:52:43 -070046 Pool[TupleKey] = Result;
47 return Result;
48 }
Jim Stichnothf61d5b22014-05-23 13:31:24 -070049 ConstantList getConstantPool() const {
50 ConstantList Constants;
51 Constants.reserve(Pool.size());
52 // TODO: replace the loop with std::transform + lambdas.
53 for (typename ContainerType::const_iterator I = Pool.begin(),
54 E = Pool.end();
55 I != E; ++I) {
56 Constants.push_back(I->second);
57 }
58 return Constants;
59 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070060
61private:
62 typedef std::pair<Type, KeyType> TupleType;
63 struct TupleCompare {
64 bool operator()(const TupleType &A, const TupleType &B) {
65 if (A.first != B.first)
66 return A.first < B.first;
67 if (KeyTypeHasFP)
68 return memcmp(&A.second, &B.second, sizeof(KeyType)) < 0;
69 return A.second < B.second;
70 }
71 };
72 typedef std::map<const TupleType, ValueType *, TupleCompare> ContainerType;
73 ContainerType Pool;
Jim Stichnothf61d5b22014-05-23 13:31:24 -070074 uint32_t NextPoolID;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070075};
76
77// The global constant pool bundles individual pools of each type of
78// interest.
79class ConstantPool {
80 ConstantPool(const ConstantPool &) LLVM_DELETED_FUNCTION;
81 ConstantPool &operator=(const ConstantPool &) LLVM_DELETED_FUNCTION;
82
83public:
84 ConstantPool() {}
85 TypePool<float, ConstantFloat, true> Floats;
86 TypePool<double, ConstantDouble, true> Doubles;
87 TypePool<uint64_t, ConstantInteger> Integers;
88 TypePool<RelocatableTuple, ConstantRelocatable> Relocatables;
89};
90
91GlobalContext::GlobalContext(llvm::raw_ostream *OsDump,
92 llvm::raw_ostream *OsEmit, VerboseMask Mask,
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070093 TargetArch Arch, OptLevel Opt,
Jim Stichnothf7c9a142014-04-29 10:52:43 -070094 IceString TestPrefix)
95 : StrDump(OsDump), StrEmit(OsEmit), VMask(Mask),
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070096 ConstPool(new ConstantPool()), Arch(Arch), Opt(Opt),
97 TestPrefix(TestPrefix), HasEmittedFirstMethod(false) {}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070098
99// In this context, name mangling means to rewrite a symbol using a
100// given prefix. For a C++ symbol, nest the original symbol inside
101// the "prefix" namespace. For other symbols, just prepend the
102// prefix.
103IceString GlobalContext::mangleName(const IceString &Name) const {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700104 // An already-nested name like foo::bar() gets pushed down one
105 // level, making it equivalent to Prefix::foo::bar().
106 // _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
107 // A non-nested but mangled name like bar() gets nested, making it
108 // equivalent to Prefix::bar().
109 // _Z3barxyz ==> ZN6Prefix3barExyz
110 // An unmangled, extern "C" style name, gets a simple prefix:
111 // bar ==> Prefixbar
112 if (getTestPrefix().empty())
113 return Name;
114
115 unsigned PrefixLength = getTestPrefix().length();
116 char NameBase[1 + Name.length()];
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700117 const size_t BufLen = 30 + Name.length() + PrefixLength;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700118 char NewName[BufLen];
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700119 uint32_t BaseLength = 0; // using uint32_t due to sscanf format string
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700120
121 int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase);
122 if (ItemsParsed == 1) {
123 // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
124 // (splice in "6Prefix") ^^^^^^^
125 snprintf(NewName, BufLen, "_ZN%u%s%s", PrefixLength,
126 getTestPrefix().c_str(), NameBase);
127 // We ignore the snprintf return value (here and below). If we
128 // somehow miscalculated the output buffer length, the output will
129 // be truncated, but it will be truncated consistently for all
130 // mangleName() calls on the same input string.
131 return NewName;
132 }
133
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700134 // Artificially limit BaseLength to 9 digits (less than 1 billion)
135 // because sscanf behavior is undefined on integer overflow. If
136 // there are more than 9 digits (which we test by looking at the
137 // beginning of NameBase), then we consider this a failure to parse
138 // a namespace mangling, and fall back to the simple prefixing.
139 ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase);
140 if (ItemsParsed == 2 && BaseLength <= strlen(NameBase) &&
141 !isdigit(NameBase[0])) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700142 // Transform _Z3barxyz ==> _ZN6Prefix3barExyz
143 // ^^^^^^^^ ^
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700144 // (splice in "N6Prefix", and insert "E" after "3bar")
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700145 // But an "I" after the identifier indicates a template argument
146 // list terminated with "E"; insert the new "E" before/after the
147 // old "E". E.g.:
148 // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz
149 // ^^^^^^^^ ^
150 // (splice in "N6Prefix", and insert "E" after "3barIabcE")
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700151 char OrigName[Name.length()];
152 char OrigSuffix[Name.length()];
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700153 uint32_t ActualBaseLength = BaseLength;
154 if (NameBase[ActualBaseLength] == 'I') {
155 ++ActualBaseLength;
156 while (NameBase[ActualBaseLength] != 'E' &&
157 NameBase[ActualBaseLength] != '\0')
158 ++ActualBaseLength;
159 }
160 strncpy(OrigName, NameBase, ActualBaseLength);
161 OrigName[ActualBaseLength] = '\0';
162 strcpy(OrigSuffix, NameBase + ActualBaseLength);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700163 snprintf(NewName, BufLen, "_ZN%u%s%u%sE%s", PrefixLength,
164 getTestPrefix().c_str(), BaseLength, OrigName, OrigSuffix);
165 return NewName;
166 }
167
168 // Transform bar ==> Prefixbar
169 // ^^^^^^
170 return getTestPrefix() + Name;
171}
172
173GlobalContext::~GlobalContext() {}
174
175Constant *GlobalContext::getConstantInt(Type Ty, uint64_t ConstantInt64) {
176 return ConstPool->Integers.getOrAdd(this, Ty, ConstantInt64);
177}
178
179Constant *GlobalContext::getConstantFloat(float ConstantFloat) {
180 return ConstPool->Floats.getOrAdd(this, IceType_f32, ConstantFloat);
181}
182
183Constant *GlobalContext::getConstantDouble(double ConstantDouble) {
184 return ConstPool->Doubles.getOrAdd(this, IceType_f64, ConstantDouble);
185}
186
187Constant *GlobalContext::getConstantSym(Type Ty, int64_t Offset,
188 const IceString &Name,
189 bool SuppressMangling) {
190 return ConstPool->Relocatables.getOrAdd(
191 this, Ty, RelocatableTuple(Offset, Name, SuppressMangling));
192}
193
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700194ConstantList GlobalContext::getConstantPool(Type Ty) const {
195 switch (Ty) {
196 case IceType_i1:
197 case IceType_i8:
198 case IceType_i16:
199 case IceType_i32:
200 case IceType_i64:
201 return ConstPool->Integers.getConstantPool();
202 case IceType_f32:
203 return ConstPool->Floats.getConstantPool();
204 case IceType_f64:
205 return ConstPool->Doubles.getConstantPool();
206 case IceType_void:
207 case IceType_NUM:
208 break;
209 }
210 llvm_unreachable("Unknown type");
211}
212
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700213void Timer::printElapsedUs(GlobalContext *Ctx, const IceString &Tag) const {
214 if (Ctx->isVerbose(IceV_Timing)) {
215 // Prefixing with '#' allows timing strings to be included
216 // without error in textual assembly output.
217 Ctx->getStrDump() << "# " << getElapsedUs() << " usec " << Tag << "\n";
218 }
219}
220
221} // end of namespace Ice