Skip to content

Commit a4c0192

Browse files
committed
Refactor out IsConstant() to remove redundant assert.
1 parent dd3c4fb commit a4c0192

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

llvm/lib/IR/AsmWriter.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,45 +2107,42 @@ static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N,
21072107
Out << "!DIGenericSubrange(";
21082108
MDFieldPrinter Printer(Out, WriterCtx);
21092109

2110-
auto IsConstant = [&](Metadata *Bound) -> bool {
2111-
if (auto *BE = dyn_cast_or_null<DIExpression>(Bound)) {
2112-
return BE->isConstant() &&
2113-
DIExpression::SignedOrUnsignedConstant::SignedConstant ==
2114-
*BE->isConstant();
2115-
}
2116-
return false;
2117-
};
2118-
2119-
auto GetConstant = [&](Metadata *Bound) -> int64_t {
2120-
assert(IsConstant(Bound) && "Expected constant");
2110+
auto GetConstant = [&](Metadata *Bound) -> std::optional<int64_t> {
21212111
auto *BE = dyn_cast_or_null<DIExpression>(Bound);
2122-
return static_cast<int64_t>(BE->getElement(1));
2112+
if (!BE)
2113+
return std::nullopt;
2114+
if (BE->isConstant() &&
2115+
DIExpression::SignedOrUnsignedConstant::SignedConstant ==
2116+
*BE->isConstant()) {
2117+
return static_cast<int64_t>(BE->getElement(1));
2118+
}
2119+
return std::nullopt;
21232120
};
21242121

21252122
auto *Count = N->getRawCountNode();
2126-
if (IsConstant(Count))
2127-
Printer.printInt("count", GetConstant(Count),
2123+
if (auto ConstantCount = GetConstant(Count))
2124+
Printer.printInt("count", *ConstantCount,
21282125
/* ShouldSkipZero */ false);
21292126
else
21302127
Printer.printMetadata("count", Count, /*ShouldSkipNull */ true);
21312128

21322129
auto *LBound = N->getRawLowerBound();
2133-
if (IsConstant(LBound))
2134-
Printer.printInt("lowerBound", GetConstant(LBound),
2130+
if (auto ConstantLBound = GetConstant(LBound))
2131+
Printer.printInt("lowerBound", *ConstantLBound,
21352132
/* ShouldSkipZero */ false);
21362133
else
21372134
Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true);
21382135

21392136
auto *UBound = N->getRawUpperBound();
2140-
if (IsConstant(UBound))
2141-
Printer.printInt("upperBound", GetConstant(UBound),
2137+
if (auto ConstantUBound = GetConstant(UBound))
2138+
Printer.printInt("upperBound", *ConstantUBound,
21422139
/* ShouldSkipZero */ false);
21432140
else
21442141
Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true);
21452142

21462143
auto *Stride = N->getRawStride();
2147-
if (IsConstant(Stride))
2148-
Printer.printInt("stride", GetConstant(Stride),
2144+
if (auto ConstantStride = GetConstant(Stride))
2145+
Printer.printInt("stride", *ConstantStride,
21492146
/* ShouldSkipZero */ false);
21502147
else
21512148
Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true);

0 commit comments

Comments
 (0)