SIL: let TypeValueInst.value return an optional Int

and don't require the client to check if it's an integer type.
Also, implement `var value` natively and without bridging.
This commit is contained in:
Erik Eckstein
2025-05-15 13:43:28 +02:00
parent dbd0af063c
commit 140b883b9a
4 changed files with 7 additions and 17 deletions

View File

@@ -15,7 +15,7 @@ import SIL
extension TypeValueInst: OnoneSimplifiable, SILCombineSimplifiable {
func simplify(_ context: SimplifyContext) {
// If our parameter is not known statically, then bail.
guard paramType.isInteger else {
guard let value = value else {
return
}

View File

@@ -862,8 +862,12 @@ final public class TypeValueInst: SingleValueInstruction, UnaryInstruction {
CanonicalType(bridged: bridged.TypeValueInst_getParamType())
}
public var value: Int {
bridged.TypeValueInst_getValue()
/// Returns the value of the Integer type is known and fits into an `Int`.
public var value: Int? {
if paramType.isInteger {
return paramType.valueOfInteger
}
return nil
}
}

View File

@@ -857,7 +857,6 @@ struct BridgedInstruction {
BRIDGED_INLINE SwiftInt FullApplySite_numIndirectResultArguments() const;
BRIDGED_INLINE bool ConvertFunctionInst_withoutActuallyEscaping() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType TypeValueInst_getParamType() const;
BRIDGED_INLINE SwiftInt TypeValueInst_getValue() const;
// =========================================================================//
// VarDeclInst and DebugVariableInst

View File

@@ -1659,19 +1659,6 @@ BridgedCanType BridgedInstruction::TypeValueInst_getParamType() const {
return getAs<swift::TypeValueInst>()->getParamType();
}
SwiftInt BridgedInstruction::TypeValueInst_getValue() const {
auto tvi = getAs<swift::TypeValueInst>();
// Assume we've already checked that the parameter type is an IntegerType.
auto integer = tvi->getParamType()->castTo<swift::IntegerType>();
if (integer->isNegative()) {
return integer->getValue().getSExtValue();
} else {
return integer->getValue().getZExtValue();
}
}
//===----------------------------------------------------------------------===//
// VarDeclInst and DebugVariableInst
//===----------------------------------------------------------------------===//