@@ -270,19 +270,11 @@ class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
270
270
271
271
bool needsUnsigned = needsUnsignedCmp (op.getPredicate ());
272
272
emitc::CmpPredicate pred = toEmitCPred (op.getPredicate ());
273
- Type arithmeticType = type;
274
- if (type.isUnsignedInteger () != needsUnsigned) {
275
- arithmeticType = rewriter.getIntegerType (type.getIntOrFloatBitWidth (),
276
- /* isSigned=*/ !needsUnsigned);
277
- }
278
- Value lhs = adaptor.getLhs ();
279
- Value rhs = adaptor.getRhs ();
280
- if (arithmeticType != type) {
281
- lhs = rewriter.template create <emitc::CastOp>(op.getLoc (), arithmeticType,
282
- lhs);
283
- rhs = rewriter.template create <emitc::CastOp>(op.getLoc (), arithmeticType,
284
- rhs);
285
- }
273
+
274
+ Type arithmeticType = adaptIntegralTypeSignedness (type, needsUnsigned);
275
+ Value lhs = adaptValueType (adaptor.getLhs (), rewriter, arithmeticType);
276
+ Value rhs = adaptValueType (adaptor.getRhs (), rewriter, arithmeticType);
277
+
286
278
rewriter.replaceOpWithNewOp <emitc::CmpOp>(op, op.getType (), pred, lhs, rhs);
287
279
return success ();
288
280
}
@@ -356,37 +348,26 @@ class CastConversion : public OpConversionPattern<ArithOp> {
356
348
return success ();
357
349
}
358
350
359
- bool isTruncation = operandType.getIntOrFloatBitWidth () >
360
- opReturnType.getIntOrFloatBitWidth ();
351
+ bool isTruncation =
352
+ (isa<IntegerType>(operandType) && isa<IntegerType>(opReturnType) &&
353
+ operandType.getIntOrFloatBitWidth () >
354
+ opReturnType.getIntOrFloatBitWidth ());
361
355
bool doUnsigned = castToUnsigned || isTruncation;
362
356
363
- Type castType = opReturnType;
364
- // If the op is a ui variant and the type wanted as
365
- // return type isn't unsigned, we need to issue an unsigned type to do
366
- // the conversion.
367
- if (castType.isUnsignedInteger () != doUnsigned) {
368
- castType = rewriter.getIntegerType (opReturnType.getIntOrFloatBitWidth (),
369
- /* isSigned=*/ !doUnsigned);
370
- }
357
+ // Adapt the signedness of the result (bitwidth-preserving cast)
358
+ // This is needed e.g., if the return type is signless.
359
+ Type castDestType = adaptIntegralTypeSignedness (opReturnType, doUnsigned);
371
360
372
- Value actualOp = adaptor.getIn ();
373
- // Adapt the signedness of the operand if necessary
374
- if (operandType.isUnsignedInteger () != doUnsigned) {
375
- Type correctSignednessType =
376
- rewriter.getIntegerType (operandType.getIntOrFloatBitWidth (),
377
- /* isSigned=*/ !doUnsigned);
378
- actualOp = rewriter.template create <emitc::CastOp>(
379
- op.getLoc (), correctSignednessType, actualOp);
380
- }
361
+ // Adapt the signedness of the operand (bitwidth-preserving cast)
362
+ Type castSrcType = adaptIntegralTypeSignedness (operandType, doUnsigned);
363
+ Value actualOp = adaptValueType (adaptor.getIn (), rewriter, castSrcType);
381
364
382
- auto result = rewriter.template create <emitc::CastOp>(op.getLoc (), castType,
383
- actualOp);
365
+ // Actual cast (may change bitwidth)
366
+ auto cast = rewriter.template create <emitc::CastOp>(op.getLoc (),
367
+ castDestType, actualOp);
384
368
385
369
// Cast to the expected output type
386
- if (castType != opReturnType) {
387
- result = rewriter.template create <emitc::CastOp>(op.getLoc (),
388
- opReturnType, result);
389
- }
370
+ auto result = adaptValueType (cast, rewriter, opReturnType);
390
371
391
372
rewriter.replaceOp (op, result);
392
373
return success ();
@@ -438,8 +419,6 @@ class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
438
419
return rewriter.notifyMatchFailure (op, " i1 type is not implemented" );
439
420
}
440
421
441
- Value lhs = adaptor.getLhs ();
442
- Value rhs = adaptor.getRhs ();
443
422
Type arithmeticType = type;
444
423
if ((type.isSignlessInteger () || type.isSignedInteger ()) &&
445
424
!bitEnumContainsAll (op.getOverflowFlags (),
@@ -449,20 +428,15 @@ class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
449
428
arithmeticType = rewriter.getIntegerType (type.getIntOrFloatBitWidth (),
450
429
/* isSigned=*/ false );
451
430
}
452
- if (arithmeticType != type) {
453
- lhs = rewriter.template create <emitc::CastOp>(op.getLoc (), arithmeticType,
454
- lhs);
455
- rhs = rewriter.template create <emitc::CastOp>(op.getLoc (), arithmeticType,
456
- rhs);
457
- }
458
431
459
- Value result = rewriter.template create <EmitCOp>(op.getLoc (),
460
- arithmeticType, lhs, rhs);
432
+ Value lhs = adaptValueType (adaptor.getLhs (), rewriter, arithmeticType);
433
+ Value rhs = adaptValueType (adaptor.getRhs (), rewriter, arithmeticType);
434
+
435
+ Value arithmeticResult = rewriter.template create <EmitCOp>(
436
+ op.getLoc (), arithmeticType, lhs, rhs);
437
+
438
+ Value result = adaptValueType (arithmeticResult, rewriter, type);
461
439
462
- if (arithmeticType != type) {
463
- result =
464
- rewriter.template create <emitc::CastOp>(op.getLoc (), type, result);
465
- }
466
440
rewriter.replaceOp (op, result);
467
441
return success ();
468
442
}
0 commit comments