Skip to content

Commit 36a2d7b

Browse files
authored
[AsmWriter] Combine IsConstant and GetConstant (NFCI) (#129288)
There was an assert in GetConstant checked if Bound is constant. However, GetConstant was only called when IsConstant==true. This refactor attempts to get rid of the assert by combining GetConstant and IsContstant.
1 parent 2b509ec commit 36a2d7b

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
@@ -2124,45 +2124,42 @@ static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N,
21242124
Out << "!DIGenericSubrange(";
21252125
MDFieldPrinter Printer(Out, WriterCtx);
21262126

2127-
auto IsConstant = [&](Metadata *Bound) -> bool {
2128-
if (auto *BE = dyn_cast_or_null<DIExpression>(Bound)) {
2129-
return BE->isConstant() &&
2130-
DIExpression::SignedOrUnsignedConstant::SignedConstant ==
2131-
*BE->isConstant();
2132-
}
2133-
return false;
2134-
};
2135-
2136-
auto GetConstant = [&](Metadata *Bound) -> int64_t {
2137-
assert(IsConstant(Bound) && "Expected constant");
2127+
auto GetConstant = [&](Metadata *Bound) -> std::optional<int64_t> {
21382128
auto *BE = dyn_cast_or_null<DIExpression>(Bound);
2139-
return static_cast<int64_t>(BE->getElement(1));
2129+
if (!BE)
2130+
return std::nullopt;
2131+
if (BE->isConstant() &&
2132+
DIExpression::SignedOrUnsignedConstant::SignedConstant ==
2133+
*BE->isConstant()) {
2134+
return static_cast<int64_t>(BE->getElement(1));
2135+
}
2136+
return std::nullopt;
21402137
};
21412138

21422139
auto *Count = N->getRawCountNode();
2143-
if (IsConstant(Count))
2144-
Printer.printInt("count", GetConstant(Count),
2140+
if (auto ConstantCount = GetConstant(Count))
2141+
Printer.printInt("count", *ConstantCount,
21452142
/* ShouldSkipZero */ false);
21462143
else
21472144
Printer.printMetadata("count", Count, /*ShouldSkipNull */ true);
21482145

21492146
auto *LBound = N->getRawLowerBound();
2150-
if (IsConstant(LBound))
2151-
Printer.printInt("lowerBound", GetConstant(LBound),
2147+
if (auto ConstantLBound = GetConstant(LBound))
2148+
Printer.printInt("lowerBound", *ConstantLBound,
21522149
/* ShouldSkipZero */ false);
21532150
else
21542151
Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true);
21552152

21562153
auto *UBound = N->getRawUpperBound();
2157-
if (IsConstant(UBound))
2158-
Printer.printInt("upperBound", GetConstant(UBound),
2154+
if (auto ConstantUBound = GetConstant(UBound))
2155+
Printer.printInt("upperBound", *ConstantUBound,
21592156
/* ShouldSkipZero */ false);
21602157
else
21612158
Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true);
21622159

21632160
auto *Stride = N->getRawStride();
2164-
if (IsConstant(Stride))
2165-
Printer.printInt("stride", GetConstant(Stride),
2161+
if (auto ConstantStride = GetConstant(Stride))
2162+
Printer.printInt("stride", *ConstantStride,
21662163
/* ShouldSkipZero */ false);
21672164
else
21682165
Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true);

0 commit comments

Comments
 (0)