Skip to content

Commit a80d0cb

Browse files
committed
remove now unused utilities
* StaticInitCloner * some APIs in SILGlobalVariable
1 parent 4284dc1 commit a80d0cb

File tree

6 files changed

+35
-307
lines changed

6 files changed

+35
-307
lines changed

include/swift/SIL/SILGlobalVariable.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,6 @@ class SILGlobalVariable
181181
const_iterator begin() const { return StaticInitializerBlock.begin(); }
182182
const_iterator end() const { return StaticInitializerBlock.end(); }
183183

184-
/// Returns true if \p I is a valid instruction to be contained in the
185-
/// static initializer.
186-
static bool isValidStaticInitializerInst(const SILInstruction *I,
187-
SILModule &M);
188-
189-
/// Returns the usub_with_overflow builtin if \p TE extracts the result of
190-
/// such a subtraction, which is required to have an integer_literal as right
191-
/// operand.
192-
static BuiltinInst *getOffsetSubtract(const TupleExtractInst *TE, SILModule &M);
193-
194184
void dropAllReferences() {
195185
StaticInitializerBlock.dropAllReferences();
196186
}
@@ -290,10 +280,8 @@ SILFunction *findInitializer(SILFunction *AddrF, BuiltinInst *&CallToOnce);
290280
///
291281
/// Given a global initializer, InitFunc, return the GlobalVariable that it
292282
/// statically initializes or return nullptr if it isn't an obvious static
293-
/// initializer. If a global variable is returned, InitVal is initialized to the
294-
/// the instruction producing the global's initial value.
295-
SILGlobalVariable *getVariableOfStaticInitializer(
296-
SILFunction *InitFunc, SingleValueInstruction *&InitVal);
283+
/// initializer.
284+
SILGlobalVariable *getVariableOfStaticInitializer(SILFunction *InitFunc);
297285

298286
} // namespace swift
299287

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -344,84 +344,6 @@ class CloneCollector {
344344
}
345345
};
346346

347-
/// Utility class for cloning init values into the static initializer of a
348-
/// SILGlobalVariable.
349-
class StaticInitCloner : public SILCloner<StaticInitCloner> {
350-
friend class SILInstructionVisitor<StaticInitCloner>;
351-
friend class SILCloner<StaticInitCloner>;
352-
353-
/// The number of not yet cloned operands for each instruction.
354-
llvm::DenseMap<SILInstruction *, int> numOpsToClone;
355-
356-
/// List of instructions for which all operands are already cloned (or which
357-
/// don't have any operands).
358-
llvm::SmallVector<SILInstruction *, 8> readyToClone;
359-
360-
SILDebugLocation insertLoc;
361-
362-
SILInstruction *firstClonedInst = nullptr;
363-
364-
public:
365-
StaticInitCloner(SILGlobalVariable *gVar)
366-
: SILCloner<StaticInitCloner>(gVar),
367-
insertLoc(ArtificialUnreachableLocation(), nullptr) {}
368-
369-
StaticInitCloner(SILInstruction *insertionPoint)
370-
: SILCloner<StaticInitCloner>(*insertionPoint->getFunction()),
371-
insertLoc(insertionPoint->getDebugLocation()) {
372-
Builder.setInsertionPoint(insertionPoint);
373-
}
374-
375-
StaticInitCloner(const SILBuilder &builder)
376-
: SILCloner<StaticInitCloner>(builder.getFunction()),
377-
insertLoc(ArtificialUnreachableLocation(), nullptr) {
378-
Builder.setInsertionPoint(builder.getInsertionBB(), builder.getInsertionPoint());
379-
if (builder.getInsertionPoint() != builder.getInsertionBB()->end())
380-
insertLoc = builder.getInsertionPoint()->getDebugLocation();
381-
}
382-
383-
/// Add \p InitVal and all its operands (transitively) for cloning.
384-
///
385-
/// Note: all init values must are added, before calling clone().
386-
/// Returns false if cloning is not possible, e.g. if we would end up cloning
387-
/// a reference to a private function into a function which is serialized.
388-
bool add(SILValue initVal);
389-
390-
/// Clone \p InitVal and all its operands into the initializer of the
391-
/// SILGlobalVariable.
392-
///
393-
/// \return Returns the cloned instruction in the SILGlobalVariable.
394-
SILValue clone(SILValue initVal);
395-
396-
SILInstruction *getFirstClonedInst() const { return firstClonedInst; }
397-
398-
/// Convenience function to clone a single \p InitVal.
399-
static void appendToInitializer(SILGlobalVariable *gVar,
400-
SingleValueInstruction *initVal) {
401-
StaticInitCloner cloner(gVar);
402-
bool success = cloner.add(initVal);
403-
(void)success;
404-
assert(success && "adding initVal cannot fail for a global variable");
405-
cloner.clone(initVal);
406-
}
407-
408-
protected:
409-
410-
void postProcess(SILInstruction *Orig, SILInstruction *Cloned) {
411-
if (!firstClonedInst)
412-
firstClonedInst = Cloned;
413-
SILCloner<StaticInitCloner>::postProcess(Orig, Cloned);
414-
}
415-
416-
SILLocation remapLocation(SILLocation loc) {
417-
return insertLoc.getLocation();
418-
}
419-
420-
const SILDebugScope *remapScope(const SILDebugScope *DS) {
421-
return insertLoc.getScope();
422-
}
423-
};
424-
425347
} // namespace swift
426348

427349
#endif

lib/IRGen/GenConstant.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ llvm::Constant *emitConstantStructOrTuple(IRGenModule &IGM, InstTy inst,
155155
}
156156
} // end anonymous namespace
157157

158+
/// Returns the usub_with_overflow builtin if \p TE extracts the result of
159+
/// such a subtraction, which is required to have an integer_literal as right
160+
/// operand.
161+
static BuiltinInst *getOffsetSubtract(const TupleExtractInst *TE, SILModule &M) {
162+
// Match the pattern:
163+
// tuple_extract(usub_with_overflow(x, integer_literal, integer_literal 0), 0)
164+
165+
if (TE->getFieldIndex() != 0)
166+
return nullptr;
167+
168+
auto *BI = dyn_cast<BuiltinInst>(TE->getOperand());
169+
if (!BI)
170+
return nullptr;
171+
if (M.getBuiltinInfo(BI->getName()).ID != BuiltinValueKind::USubOver)
172+
return nullptr;
173+
174+
if (!isa<IntegerLiteralInst>(BI->getArguments()[1]))
175+
return nullptr;
176+
177+
auto *overflowFlag = dyn_cast<IntegerLiteralInst>(BI->getArguments()[2]);
178+
if (!overflowFlag || !overflowFlag->getValue().isNullValue())
179+
return nullptr;
180+
181+
return BI;
182+
}
183+
158184
llvm::Constant *irgen::emitConstantValue(IRGenModule &IGM, SILValue operand) {
159185
if (auto *SI = dyn_cast<StructInst>(operand)) {
160186
// The only way to get a struct's stored properties (which we need to map to
@@ -200,8 +226,7 @@ llvm::Constant *irgen::emitConstantValue(IRGenModule &IGM, SILValue operand) {
200226
// Handle StringObjectOr(tuple_extract(usub_with_overflow(x, offset)), bits)
201227
// This pattern appears in UTF8 String literal construction.
202228
// Generate the equivalent: add(x, sub(bits - offset)
203-
BuiltinInst *SubtrBI =
204-
SILGlobalVariable::getOffsetSubtract(TE, IGM.getSILModule());
229+
BuiltinInst *SubtrBI = getOffsetSubtract(TE, IGM.getSILModule());
205230
assert(SubtrBI && "unsupported argument of StringObjectOr");
206231
auto *ptr = emitConstantValue(IGM, SubtrBI->getArguments()[0]);
207232
auto *offset = emitConstantValue(IGM, SubtrBI->getArguments()[1]);

lib/SIL/IR/SILGlobalVariable.cpp

Lines changed: 6 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -86,116 +86,6 @@ SILInstruction *SILGlobalVariable::getStaticInitializerValue() {
8686
return &StaticInitializerBlock.back();
8787
}
8888

89-
BuiltinInst *SILGlobalVariable::getOffsetSubtract(const TupleExtractInst *TE,
90-
SILModule &M) {
91-
92-
// Match the pattern:
93-
// tuple_extract(usub_with_overflow(x, integer_literal, integer_literal 0), 0)
94-
95-
if (TE->getFieldIndex() != 0)
96-
return nullptr;
97-
98-
auto *BI = dyn_cast<BuiltinInst>(TE->getOperand());
99-
if (!BI)
100-
return nullptr;
101-
if (M.getBuiltinInfo(BI->getName()).ID != BuiltinValueKind::USubOver)
102-
return nullptr;
103-
104-
if (!isa<IntegerLiteralInst>(BI->getArguments()[1]))
105-
return nullptr;
106-
107-
auto *overflowFlag = dyn_cast<IntegerLiteralInst>(BI->getArguments()[2]);
108-
if (!overflowFlag || !overflowFlag->getValue().isNullValue())
109-
return nullptr;
110-
111-
return BI;
112-
}
113-
114-
bool SILGlobalVariable::isValidStaticInitializerInst(const SILInstruction *I,
115-
SILModule &M) {
116-
for (const Operand &op : I->getAllOperands()) {
117-
// Rule out SILUndef and SILArgument.
118-
if (!isa<SingleValueInstruction>(op.get()))
119-
return false;
120-
}
121-
switch (I->getKind()) {
122-
case SILInstructionKind::BuiltinInst: {
123-
auto *bi = cast<BuiltinInst>(I);
124-
switch (M.getBuiltinInfo(bi->getName()).ID) {
125-
case BuiltinValueKind::ZeroInitializer: {
126-
auto type = bi->getType().getASTType();
127-
if (auto vector = dyn_cast<BuiltinVectorType>(type))
128-
type = vector.getElementType();
129-
return isa<BuiltinIntegerType>(type) || isa<BuiltinFloatType>(type);
130-
}
131-
case BuiltinValueKind::PtrToInt:
132-
if (isa<LiteralInst>(bi->getArguments()[0]))
133-
return true;
134-
break;
135-
case BuiltinValueKind::StringObjectOr:
136-
// The first operand can be a string literal (i.e. a pointer), but the
137-
// second operand must be a constant. This enables creating a
138-
// a pointer+offset relocation.
139-
// Note that StringObjectOr requires the or'd bits in the first
140-
// operand to be 0, so the operation is equivalent to an addition.
141-
if (isa<IntegerLiteralInst>(bi->getArguments()[1]))
142-
return true;
143-
break;
144-
case BuiltinValueKind::ZExtOrBitCast:
145-
return true;
146-
case BuiltinValueKind::USubOver: {
147-
// Handle StringObjectOr(tuple_extract(usub_with_overflow(x, offset)), bits)
148-
// This pattern appears in UTF8 String literal construction.
149-
auto *TE = bi->getSingleUserOfType<TupleExtractInst>();
150-
return TE && getOffsetSubtract(TE, M);
151-
}
152-
case BuiltinValueKind::OnFastPath:
153-
return true;
154-
default:
155-
break;
156-
}
157-
return false;
158-
}
159-
case SILInstructionKind::TupleExtractInst: {
160-
// Handle StringObjectOr(tuple_extract(usub_with_overflow(x, offset)), bits)
161-
// This pattern appears in UTF8 String literal construction.
162-
auto *TE = cast<TupleExtractInst>(I);
163-
if (!getOffsetSubtract(TE, M))
164-
return false;
165-
auto *BI = TE->getSingleUserOfType<BuiltinInst>();
166-
return BI &&
167-
M.getBuiltinInfo(BI->getName()).ID == BuiltinValueKind::StringObjectOr;
168-
}
169-
case SILInstructionKind::StringLiteralInst:
170-
switch (cast<StringLiteralInst>(I)->getEncoding()) {
171-
case StringLiteralInst::Encoding::Bytes:
172-
case StringLiteralInst::Encoding::UTF8:
173-
return true;
174-
case StringLiteralInst::Encoding::ObjCSelector:
175-
// Objective-C selector string literals cannot be used in static
176-
// initializers.
177-
return false;
178-
}
179-
return false;
180-
case SILInstructionKind::FunctionRefInst:
181-
// TODO: support async function pointers in static globals.
182-
if (cast<FunctionRefInst>(I)->getReferencedFunction()->isAsync())
183-
return false;
184-
return true;
185-
case SILInstructionKind::StructInst:
186-
case SILInstructionKind::TupleInst:
187-
case SILInstructionKind::IntegerLiteralInst:
188-
case SILInstructionKind::FloatLiteralInst:
189-
case SILInstructionKind::ObjectInst:
190-
case SILInstructionKind::ValueToBridgeObjectInst:
191-
case SILInstructionKind::ConvertFunctionInst:
192-
case SILInstructionKind::ThinToThickFunctionInst:
193-
return true;
194-
default:
195-
return false;
196-
}
197-
}
198-
19989
/// Return whether this variable corresponds to a Clang node.
20090
bool SILGlobalVariable::hasClangNode() const {
20191
return (VDecl ? VDecl->hasClangNode() : false);
@@ -244,11 +134,7 @@ SILGlobalVariable *swift::getVariableOfGlobalInit(SILFunction *AddrF) {
244134
if (!InitF)
245135
return nullptr;
246136

247-
// If the globalinit_func is trivial, continue; otherwise bail.
248-
SingleValueInstruction *dummyInitVal;
249-
auto *SILG = getVariableOfStaticInitializer(InitF, dummyInitVal);
250-
251-
return SILG;
137+
return getVariableOfStaticInitializer(InitF);
252138
}
253139

254140
SILFunction *swift::getCalleeOfOnceCall(BuiltinInst *BI) {
@@ -297,48 +183,17 @@ SILFunction *swift::findInitializer(SILFunction *AddrF,
297183
return callee;
298184
}
299185

300-
SILGlobalVariable *
301-
swift::getVariableOfStaticInitializer(SILFunction *InitFunc,
302-
SingleValueInstruction *&InitVal) {
303-
InitVal = nullptr;
304-
SILGlobalVariable *GVar = nullptr;
186+
SILGlobalVariable *swift::getVariableOfStaticInitializer(SILFunction *InitFunc) {
305187
// We only handle a single SILBasicBlock for now.
306188
if (InitFunc->size() != 1)
307189
return nullptr;
308190

309-
SILBasicBlock *BB = &InitFunc->front();
310-
GlobalAddrInst *SGA = nullptr;
311-
bool HasStore = false;
312-
for (auto &I : *BB) {
313-
// Make sure we have a single GlobalAddrInst and a single StoreInst.
314-
// And the StoreInst writes to the GlobalAddrInst.
315-
if (isa<AllocGlobalInst>(&I) || isa<ReturnInst>(&I)
316-
|| isa<DebugValueInst>(&I) || isa<DebugStepInst>(&I)) {
317-
continue;
318-
} else if (auto *sga = dyn_cast<GlobalAddrInst>(&I)) {
319-
if (SGA)
320-
return nullptr;
321-
SGA = sga;
322-
GVar = SGA->getReferencedGlobal();
323-
} else if (auto *SI = dyn_cast<StoreInst>(&I)) {
324-
if (HasStore || SI->getDest() != SGA)
325-
return nullptr;
326-
HasStore = true;
327-
InitVal = cast<SingleValueInstruction>(SI->getSrc());
328-
} else if (auto *mt = dyn_cast<MetatypeInst>(&I)) {
329-
// Unused meta_type instructions are sometimes generated by SILGen.
330-
// Handle this case to not require to run DeadCodeElimination before
331-
// MandatoryGlobalOpt.
332-
if (!mt->use_empty())
333-
return nullptr;
334-
} else if (!SILGlobalVariable::isValidStaticInitializerInst(&I,
335-
I.getModule())) {
336-
return nullptr;
191+
for (auto &inst : InitFunc->front()) {
192+
if (auto *agi = dyn_cast<AllocGlobalInst>(&inst)) {
193+
return agi->getReferencedGlobal();
337194
}
338195
}
339-
if (!InitVal)
340-
return nullptr;
341-
return GVar;
196+
return nullptr;
342197
}
343198

344199
SILType

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6870,8 +6870,6 @@ void SILGlobalVariable::verify() const {
68706870

68716871
// Verify the static initializer.
68726872
for (const SILInstruction &I : StaticInitializerBlock) {
6873-
assert(isValidStaticInitializerInst(&I, getModule()) &&
6874-
"illegal static initializer");
68756873
auto init = cast<SingleValueInstruction>(&I);
68766874
if (init == &StaticInitializerBlock.back()) {
68776875
assert(init->use_empty() && "Init value must not have another use");

0 commit comments

Comments
 (0)