Skip to content

Commit 2b6b161

Browse files
author
Marc Rasi
committed
factor WellKnownFunction handler out into a function
1 parent 0eff06e commit 2b6b161

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

lib/SILOptimizer/Utils/ConstExpr.cpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@ evaluateAndCacheCall(SILFunction &fn, SubstitutionMap substitutionMap,
3838
// general framework.
3939

4040
enum class WellKnownFunction {
41-
Unknown,
4241
// String.init()
4342
StringInitEmpty,
4443
// String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
4544
StringMakeUTF8
4645
};
4746

48-
static WellKnownFunction classifyFunction(SILFunction *fn) {
47+
static llvm::Optional<WellKnownFunction> classifyFunction(SILFunction *fn) {
4948
if (fn->hasSemanticsAttr("string.init_empty"))
5049
return WellKnownFunction::StringInitEmpty;
5150
if (fn->hasSemanticsAttr("string.makeUTF8"))
5251
return WellKnownFunction::StringMakeUTF8;
53-
return WellKnownFunction::Unknown;
52+
return None;
5453
}
5554

5655
//===----------------------------------------------------------------------===//
@@ -131,6 +130,9 @@ class ConstExprFunctionState {
131130
llvm::Optional<SymbolicValue> computeOpaqueCallResult(ApplyInst *apply,
132131
SILFunction *callee);
133132

133+
llvm::Optional<SymbolicValue>
134+
computeWellKnownCallResult(ApplyInst *apply, WellKnownFunction callee);
135+
134136
SymbolicValue getSingleWriterAddressValue(SILValue addr);
135137
SymbolicValue getConstAddrAndLoadResult(SILValue addr);
136138
SymbolicValue loadAddrValue(SILValue addr, SymbolicValue addrVal);
@@ -541,26 +543,15 @@ ConstExprFunctionState::computeOpaqueCallResult(ApplyInst *apply,
541543
return evaluator.getUnknown((SILInstruction *)apply, UnknownReason::Default);
542544
}
543545

544-
/// Given a call to a function, determine whether it is a call to a constexpr
545-
/// function. If so, collect its arguments as constants, fold it and return
546-
/// None. If not, mark the results as Unknown, and return an Unknown with
547-
/// information about the error.
546+
/// Given a call to a well known function, collect its arguments as constants,
547+
/// fold it, and return None. If any of the arguments are not constants, marks
548+
/// the call's results as Unknown, and return an Unknown with information about
549+
/// the error.
548550
llvm::Optional<SymbolicValue>
549-
ConstExprFunctionState::computeCallResult(ApplyInst *apply) {
551+
ConstExprFunctionState::computeWellKnownCallResult(ApplyInst *apply,
552+
WellKnownFunction callee) {
550553
auto conventions = apply->getSubstCalleeConv();
551-
552-
// Determine the callee.
553-
auto calleeFn = getConstantValue(apply->getOperand(0));
554-
if (calleeFn.getKind() != SymbolicValue::Function)
555-
return evaluator.getUnknown((SILInstruction *)apply,
556-
UnknownReason::Default);
557-
558-
SILFunction *callee = calleeFn.getFunctionValue();
559-
560-
// If this is a well-known function, do not step into it.
561-
switch (classifyFunction(callee)) {
562-
default:
563-
break;
554+
switch (callee) {
564555
case WellKnownFunction::StringInitEmpty: { // String.init()
565556
assert(conventions.getNumDirectSILResults() == 1 &&
566557
conventions.getNumIndirectSILResults() == 0 &&
@@ -589,6 +580,28 @@ ConstExprFunctionState::computeCallResult(ApplyInst *apply) {
589580
return None;
590581
}
591582
}
583+
llvm_unreachable("unhandled WellKnownFunction");
584+
}
585+
586+
/// Given a call to a function, determine whether it is a call to a constexpr
587+
/// function. If so, collect its arguments as constants, fold it and return
588+
/// None. If not, mark the results as Unknown, and return an Unknown with
589+
/// information about the error.
590+
llvm::Optional<SymbolicValue>
591+
ConstExprFunctionState::computeCallResult(ApplyInst *apply) {
592+
auto conventions = apply->getSubstCalleeConv();
593+
594+
// Determine the callee.
595+
auto calleeFn = getConstantValue(apply->getOperand(0));
596+
if (calleeFn.getKind() != SymbolicValue::Function)
597+
return evaluator.getUnknown((SILInstruction *)apply,
598+
UnknownReason::Default);
599+
600+
SILFunction *callee = calleeFn.getFunctionValue();
601+
602+
// If this is a well-known function, do not step into it.
603+
if (auto wellKnownFunction = classifyFunction(callee))
604+
return computeWellKnownCallResult(apply, *wellKnownFunction);
592605

593606
// Verify that we can fold all of the arguments to the call.
594607
SmallVector<SymbolicValue, 4> paramConstants;

0 commit comments

Comments
 (0)