@@ -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
}
@@ -328,37 +320,26 @@ class CastConversion : public OpConversionPattern<ArithOp> {
328
320
return success ();
329
321
}
330
322
331
- bool isTruncation = operandType.getIntOrFloatBitWidth () >
332
- opReturnType.getIntOrFloatBitWidth ();
323
+ bool isTruncation =
324
+ (isa<IntegerType>(operandType) && isa<IntegerType>(opReturnType) &&
325
+ operandType.getIntOrFloatBitWidth () >
326
+ opReturnType.getIntOrFloatBitWidth ());
333
327
bool doUnsigned = castToUnsigned || isTruncation;
334
328
335
- Type castType = opReturnType;
336
- // If the op is a ui variant and the type wanted as
337
- // return type isn't unsigned, we need to issue an unsigned type to do
338
- // the conversion.
339
- if (castType.isUnsignedInteger () != doUnsigned) {
340
- castType = rewriter.getIntegerType (opReturnType.getIntOrFloatBitWidth (),
341
- /* isSigned=*/ !doUnsigned);
342
- }
329
+ // Adapt the signedness of the result (bitwidth-preserving cast)
330
+ // This is needed e.g., if the return type is signless.
331
+ Type castDestType = adaptIntegralTypeSignedness (opReturnType, doUnsigned);
343
332
344
- Value actualOp = adaptor.getIn ();
345
- // Adapt the signedness of the operand if necessary
346
- if (operandType.isUnsignedInteger () != doUnsigned) {
347
- Type correctSignednessType =
348
- rewriter.getIntegerType (operandType.getIntOrFloatBitWidth (),
349
- /* isSigned=*/ !doUnsigned);
350
- actualOp = rewriter.template create <emitc::CastOp>(
351
- op.getLoc (), correctSignednessType, actualOp);
352
- }
333
+ // Adapt the signedness of the operand (bitwidth-preserving cast)
334
+ Type castSrcType = adaptIntegralTypeSignedness (operandType, doUnsigned);
335
+ Value actualOp = adaptValueType (adaptor.getIn (), rewriter, castSrcType);
353
336
354
- auto result = rewriter.template create <emitc::CastOp>(op.getLoc (), castType,
355
- actualOp);
337
+ // Actual cast (may change bitwidth)
338
+ auto cast = rewriter.template create <emitc::CastOp>(op.getLoc (),
339
+ castDestType, actualOp);
356
340
357
341
// Cast to the expected output type
358
- if (castType != opReturnType) {
359
- result = rewriter.template create <emitc::CastOp>(op.getLoc (),
360
- opReturnType, result);
361
- }
342
+ auto result = adaptValueType (cast, rewriter, opReturnType);
362
343
363
344
rewriter.replaceOp (op, result);
364
345
return success ();
@@ -410,8 +391,6 @@ class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
410
391
return rewriter.notifyMatchFailure (op, " i1 type is not implemented" );
411
392
}
412
393
413
- Value lhs = adaptor.getLhs ();
414
- Value rhs = adaptor.getRhs ();
415
394
Type arithmeticType = type;
416
395
if ((type.isSignlessInteger () || type.isSignedInteger ()) &&
417
396
!bitEnumContainsAll (op.getOverflowFlags (),
@@ -421,20 +400,15 @@ class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
421
400
arithmeticType = rewriter.getIntegerType (type.getIntOrFloatBitWidth (),
422
401
/* isSigned=*/ false );
423
402
}
424
- if (arithmeticType != type) {
425
- lhs = rewriter.template create <emitc::CastOp>(op.getLoc (), arithmeticType,
426
- lhs);
427
- rhs = rewriter.template create <emitc::CastOp>(op.getLoc (), arithmeticType,
428
- rhs);
429
- }
430
403
431
- Value result = rewriter.template create <EmitCOp>(op.getLoc (),
432
- arithmeticType, lhs, rhs);
404
+ Value lhs = adaptValueType (adaptor.getLhs (), rewriter, arithmeticType);
405
+ Value rhs = adaptValueType (adaptor.getRhs (), rewriter, arithmeticType);
406
+
407
+ Value arithmeticResult = rewriter.template create <EmitCOp>(
408
+ op.getLoc (), arithmeticType, lhs, rhs);
409
+
410
+ Value result = adaptValueType (arithmeticResult, rewriter, type);
433
411
434
- if (arithmeticType != type) {
435
- result =
436
- rewriter.template create <emitc::CastOp>(op.getLoc (), type, result);
437
- }
438
412
rewriter.replaceOp (op, result);
439
413
return success ();
440
414
}
0 commit comments