@@ -278,6 +278,44 @@ constantFoldBinaryWithOverflow(BuiltinInst *BI, BuiltinValueKind ID,
278
278
ResultsInError);
279
279
}
280
280
281
+ static SILValue countZeros (BuiltinInst *BI, llvm::Intrinsic::ID ID) {
282
+ assert (BI->getArguments ().size () == 2 && " Ctlz should have 2 args." );
283
+ OperandValueArrayRef Args = BI->getArguments ();
284
+
285
+ // Fold for integer constant arguments.
286
+ auto *LHS = dyn_cast<IntegerLiteralInst>(Args[0 ]);
287
+ if (!LHS) {
288
+ return nullptr ;
289
+ }
290
+ APInt LHSI = LHS->getValue ();
291
+ unsigned LZ = 0 ;
292
+ // Check corner-case of source == zero
293
+ if (LHSI == 0 ) {
294
+ auto *RHS = dyn_cast<IntegerLiteralInst>(Args[1 ]);
295
+ if (!RHS || RHS->getValue () != 0 ) {
296
+ // Undefined
297
+ return nullptr ;
298
+ }
299
+ LZ = LHSI.getBitWidth ();
300
+ } else {
301
+ switch (ID) {
302
+ default :
303
+ return nullptr ;
304
+ case llvm::Intrinsic::ctlz: {
305
+ LZ = LHSI.countLeadingZeros ();
306
+ break ;
307
+ }
308
+ case llvm::Intrinsic::cttz: {
309
+ LZ = LHSI.countTrailingZeros ();
310
+ break ;
311
+ }
312
+ }
313
+ }
314
+ APInt LZAsAPInt = APInt (LHSI.getBitWidth (), LZ);
315
+ SILBuilderWithScope B (BI);
316
+ return B.createIntegerLiteral (BI->getLoc (), LHS->getType (), LZAsAPInt);
317
+ }
318
+
281
319
static SILValue constantFoldIntrinsic (BuiltinInst *BI, llvm::Intrinsic::ID ID,
282
320
Optional<bool > &ResultsInError) {
283
321
switch (ID) {
@@ -291,31 +329,9 @@ static SILValue constantFoldIntrinsic(BuiltinInst *BI, llvm::Intrinsic::ID ID,
291
329
return Op1;
292
330
}
293
331
294
- case llvm::Intrinsic::ctlz: {
295
- assert (BI->getArguments ().size () == 2 && " Ctlz should have 2 args." );
296
- OperandValueArrayRef Args = BI->getArguments ();
297
-
298
- // Fold for integer constant arguments.
299
- auto *LHS = dyn_cast<IntegerLiteralInst>(Args[0 ]);
300
- if (!LHS) {
301
- return nullptr ;
302
- }
303
- APInt LHSI = LHS->getValue ();
304
- unsigned LZ = 0 ;
305
- // Check corner-case of source == zero
306
- if (LHSI == 0 ) {
307
- auto *RHS = dyn_cast<IntegerLiteralInst>(Args[1 ]);
308
- if (!RHS || RHS->getValue () != 0 ) {
309
- // Undefined
310
- return nullptr ;
311
- }
312
- LZ = LHSI.getBitWidth ();
313
- } else {
314
- LZ = LHSI.countLeadingZeros ();
315
- }
316
- APInt LZAsAPInt = APInt (LHSI.getBitWidth (), LZ);
317
- SILBuilderWithScope B (BI);
318
- return B.createIntegerLiteral (BI->getLoc (), LHS->getType (), LZAsAPInt);
332
+ case llvm::Intrinsic::ctlz:
333
+ case llvm::Intrinsic::cttz: {
334
+ return countZeros (BI, ID);
319
335
}
320
336
321
337
case llvm::Intrinsic::sadd_with_overflow:
0 commit comments