Skip to content

Commit 5e9ce79

Browse files
authored
Merge pull request #5529 from swiftix/integer-arithmetic-perf-imrovements
[sil-constant-propagation] Code clean-up. Use SIL pattern matching and fix warnings.
2 parents ffcecfb + 53ab04b commit 5e9ce79

File tree

1 file changed

+15
-41
lines changed

1 file changed

+15
-41
lines changed

lib/SILOptimizer/Mandatory/ConstantPropagation.cpp

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define DEBUG_TYPE "constant-propagation"
1414
#include "swift/SILOptimizer/PassManager/Passes.h"
1515
#include "swift/AST/DiagnosticsSIL.h"
16+
#include "swift/SIL/PatternMatch.h"
1617
#include "swift/SIL/SILBuilder.h"
1718
#include "swift/SIL/SILInstruction.h"
1819
#include "swift/SILOptimizer/Utils/Local.h"
@@ -23,6 +24,7 @@
2324
#include "llvm/Support/Debug.h"
2425
#include "llvm/Support/CommandLine.h"
2526
using namespace swift;
27+
using namespace swift::PatternMatch;
2628

2729
STATISTIC(NumInstFolded, "Number of constant folded instructions");
2830

@@ -229,50 +231,22 @@ static SILInstruction *constantFoldCompare(BuiltinInst *BI,
229231
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), Res);
230232
}
231233

232-
if (RHS) {
233-
int Result = 1;
234-
switch (ID) {
235-
default: break;
236-
case BuiltinValueKind::ICMP_ULT:
237-
// Unsigned is never < 0.
238-
if (!RHS->getValue())
239-
Result = 0;
240-
break;
241-
case BuiltinValueKind::ICMP_UGE:
242-
// Unsigned is always >= 0.
243-
if (!RHS->getValue())
244-
Result = -1;
245-
break;
246-
}
247-
if (Result < 1) {
248-
APInt Res(1, Result);
249-
SILBuilderWithScope B(BI);
250-
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), Res);
251-
}
234+
SILValue Other;
235+
if (match(BI, m_CombineOr(m_BuiltinInst(BuiltinValueKind::ICMP_ULT,
236+
m_SILValue(Other), m_Zero()),
237+
m_BuiltinInst(BuiltinValueKind::ICMP_UGT,
238+
m_Zero(), m_SILValue(Other))))) {
239+
SILBuilderWithScope B(BI);
240+
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), APInt());
252241
}
253242

254-
if (LHS) {
255-
int Result = 1;
256-
switch (ID) {
257-
case BuiltinValueKind::ICMP_ULE:
258-
// 0 is always <= an unsigned value.
259-
if (!LHS->getValue())
260-
Result = -1;
261-
break;
262-
case BuiltinValueKind::ICMP_UGT:
263-
// 0 is never > an unsigned value.
264-
if (!LHS->getValue())
265-
Result = 0;
266-
break;
267-
}
268-
if (Result < 1) {
269-
APInt Res(1, Result);
270-
SILBuilderWithScope B(BI);
271-
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), Res);
272-
}
243+
if (match(BI, m_CombineOr(m_BuiltinInst(BuiltinValueKind::ICMP_UGE,
244+
m_SILValue(Other), m_Zero()),
245+
m_BuiltinInst(BuiltinValueKind::ICMP_ULE,
246+
m_Zero(), m_SILValue(Other))))) {
247+
SILBuilderWithScope B(BI);
248+
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), APInt(1, 1));
273249
}
274-
275-
276250
return nullptr;
277251
}
278252

0 commit comments

Comments
 (0)