forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SILGlobalVariable.cpp
151 lines (133 loc) · 4.98 KB
/
SILGlobalVariable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//===--- SILGlobalVariable.cpp - Defines SILGlobalVariable structure ------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILGlobalVariable.h"
#include "swift/SIL/SILModule.h"
using namespace swift;
SILGlobalVariable *SILGlobalVariable::create(SILModule &M, SILLinkage linkage,
bool IsFragile,
StringRef name,
SILType loweredType,
Optional<SILLocation> loc,
VarDecl *Decl) {
// Get a StringMapEntry for the variable. As a sop to error cases,
// allow the name to have an empty string.
llvm::StringMapEntry<SILGlobalVariable*> *entry = nullptr;
if (!name.empty()) {
entry = &*M.GlobalVariableTable.insert(std::make_pair(name, nullptr)).first;
assert(!entry->getValue() && "global variable already exists");
name = entry->getKey();
}
auto var = new (M) SILGlobalVariable(M, linkage, IsFragile, name,
loweredType, loc, Decl);
if (entry) entry->setValue(var);
return var;
}
SILGlobalVariable::SILGlobalVariable(SILModule &Module, SILLinkage Linkage,
bool IsFragile,
StringRef Name, SILType LoweredType,
Optional<SILLocation> Loc, VarDecl *Decl)
: Module(Module),
Name(Name),
LoweredType(LoweredType),
Location(Loc),
Linkage(unsigned(Linkage)),
Fragile(IsFragile),
VDecl(Decl) {
IsDeclaration = isAvailableExternally(Linkage);
setLet(Decl ? Decl->isLet() : false);
InitializerF = nullptr;
Module.silGlobals.push_back(this);
}
void SILGlobalVariable::setInitializer(SILFunction *InitF) {
if (InitializerF)
InitializerF->decrementRefCount();
// Increment the ref count to make sure it will not be eliminated.
InitF->incrementRefCount();
InitializerF = InitF;
}
SILGlobalVariable::~SILGlobalVariable() {
getModule().GlobalVariableTable.erase(Name);
}
static bool analyzeStaticInitializer(SILFunction *F, SILInstruction *&Val,
SILGlobalVariable *&GVar) {
Val = nullptr;
GVar = nullptr;
// We only handle a single SILBasicBlock for now.
if (F->size() != 1)
return false;
SILBasicBlock *BB = &F->front();
GlobalAddrInst *SGA = nullptr;
bool HasStore = false;
for (auto &I : *BB) {
// Make sure we have a single GlobalAddrInst and a single StoreInst.
// And the StoreInst writes to the GlobalAddrInst.
if (auto *sga = dyn_cast<GlobalAddrInst>(&I)) {
if (SGA)
return false;
SGA = sga;
GVar = SGA->getReferencedGlobal();
} else if (auto *SI = dyn_cast<StoreInst>(&I)) {
if (HasStore || SI->getDest().getDef() != SGA)
return false;
HasStore = true;
Val = dyn_cast<SILInstruction>(SI->getSrc().getDef());
// We only handle StructInst and TupleInst being stored to a
// global variable for now.
if (!isa<StructInst>(Val) && !isa<TupleInst>(Val))
return false;
} else {
if (auto *bi = dyn_cast<BuiltinInst>(&I)) {
switch (bi->getBuiltinInfo().ID) {
case BuiltinValueKind::FPTrunc:
if (isa<LiteralInst>(bi->getArguments()[0]))
continue;
break;
default:
return false;
}
}
if (I.getKind() != ValueKind::ReturnInst &&
I.getKind() != ValueKind::StructInst &&
I.getKind() != ValueKind::TupleInst &&
I.getKind() != ValueKind::IntegerLiteralInst &&
I.getKind() != ValueKind::FloatLiteralInst &&
I.getKind() != ValueKind::StringLiteralInst)
return false;
}
}
return true;
}
bool SILGlobalVariable::canBeStaticInitializer(SILFunction *F) {
SILInstruction *dummySI;
SILGlobalVariable *dummyGV;
return analyzeStaticInitializer(F, dummySI, dummyGV);
}
/// Check if a given SILFunction can be a static initializer. If yes, return
/// the SILGlobalVariable that it writes to.
SILGlobalVariable *SILGlobalVariable::getVariableOfStaticInitializer(
SILFunction *F) {
SILInstruction *dummySI;
SILGlobalVariable *GV;
if(analyzeStaticInitializer(F, dummySI, GV))
return GV;
return nullptr;
}
/// Return the value that is written into the global variable.
SILInstruction *SILGlobalVariable::getValueOfStaticInitializer() {
if (!InitializerF)
return nullptr;
SILInstruction *SI;
SILGlobalVariable *dummyGV;
if(analyzeStaticInitializer(InitializerF, SI, dummyGV))
return SI;
return nullptr;
}