|
13 | 13 | #define DEBUG_TYPE "constant-propagation"
|
14 | 14 | #include "swift/SILOptimizer/PassManager/Passes.h"
|
15 | 15 | #include "swift/AST/DiagnosticsSIL.h"
|
| 16 | +#include "swift/SIL/PatternMatch.h" |
16 | 17 | #include "swift/SIL/SILBuilder.h"
|
17 | 18 | #include "swift/SIL/SILInstruction.h"
|
18 | 19 | #include "swift/SILOptimizer/Utils/Local.h"
|
|
23 | 24 | #include "llvm/Support/Debug.h"
|
24 | 25 | #include "llvm/Support/CommandLine.h"
|
25 | 26 | using namespace swift;
|
| 27 | +using namespace swift::PatternMatch; |
26 | 28 |
|
27 | 29 | STATISTIC(NumInstFolded, "Number of constant folded instructions");
|
28 | 30 |
|
@@ -229,50 +231,22 @@ static SILInstruction *constantFoldCompare(BuiltinInst *BI,
|
229 | 231 | return B.createIntegerLiteral(BI->getLoc(), BI->getType(), Res);
|
230 | 232 | }
|
231 | 233 |
|
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()); |
252 | 241 | }
|
253 | 242 |
|
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)); |
273 | 249 | }
|
274 |
| - |
275 |
| - |
276 | 250 | return nullptr;
|
277 | 251 | }
|
278 | 252 |
|
|
0 commit comments