Skip to content

Commit 87bc0f7

Browse files
authored
[VectorCombine] Preserve cast flags in foldBitOpOfCastConstant (llvm#161237)
Follow-up of llvm#157822.
1 parent f2c9abc commit 87bc0f7

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,16 @@ bool VectorCombine::foldBitOpOfCastConstant(Instruction &I) {
10311031
// Create the cast operation directly to ensure we get a new instruction
10321032
Instruction *NewCast = CastInst::Create(CastOpcode, NewOp, I.getType());
10331033

1034+
// Preserve cast instruction flags
1035+
if (RHSFlags.NNeg)
1036+
NewCast->setNonNeg();
1037+
if (RHSFlags.NUW)
1038+
NewCast->setHasNoUnsignedWrap();
1039+
if (RHSFlags.NSW)
1040+
NewCast->setHasNoSignedWrap();
1041+
1042+
NewCast->andIRFlags(LHSCast);
1043+
10341044
// Insert the new instruction
10351045
Value *Result = Builder.Insert(NewCast);
10361046

llvm/test/Transforms/VectorCombine/X86/bitop-of-castops.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ define <4 x i32> @or_sext_v4i8_to_v4i32_constant_with_loss(<4 x i8> %a) {
357357
define <4 x i16> @and_trunc_nuw_nsw_constant(<4 x i32> %a) {
358358
; CHECK-LABEL: @and_trunc_nuw_nsw_constant(
359359
; CHECK-NEXT: [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], <i32 1, i32 2, i32 3, i32 4>
360-
; CHECK-NEXT: [[AND:%.*]] = trunc <4 x i32> [[AND_INNER]] to <4 x i16>
360+
; CHECK-NEXT: [[AND:%.*]] = trunc nuw nsw <4 x i32> [[AND_INNER]] to <4 x i16>
361361
; CHECK-NEXT: ret <4 x i16> [[AND]]
362362
;
363363
%t1 = trunc nuw nsw <4 x i32> %a to <4 x i16>
@@ -368,7 +368,7 @@ define <4 x i16> @and_trunc_nuw_nsw_constant(<4 x i32> %a) {
368368
define <4 x i8> @and_trunc_nuw_nsw_minus_constant(<4 x i32> %a) {
369369
; CHECK-LABEL: @and_trunc_nuw_nsw_minus_constant(
370370
; CHECK-NEXT: [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], <i32 240, i32 241, i32 242, i32 243>
371-
; CHECK-NEXT: [[AND:%.*]] = trunc <4 x i32> [[AND_INNER]] to <4 x i8>
371+
; CHECK-NEXT: [[AND:%.*]] = trunc nuw <4 x i32> [[AND_INNER]] to <4 x i8>
372372
; CHECK-NEXT: ret <4 x i8> [[AND]]
373373
;
374374
%t1 = trunc nuw nsw <4 x i32> %a to <4 x i8>
@@ -379,7 +379,7 @@ define <4 x i8> @and_trunc_nuw_nsw_minus_constant(<4 x i32> %a) {
379379
define <4 x i8> @and_trunc_nuw_nsw_multiconstant(<4 x i32> %a) {
380380
; CHECK-LABEL: @and_trunc_nuw_nsw_multiconstant(
381381
; CHECK-NEXT: [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], <i32 240, i32 1, i32 242, i32 3>
382-
; CHECK-NEXT: [[AND:%.*]] = trunc <4 x i32> [[AND_INNER]] to <4 x i8>
382+
; CHECK-NEXT: [[AND:%.*]] = trunc nuw <4 x i32> [[AND_INNER]] to <4 x i8>
383383
; CHECK-NEXT: ret <4 x i8> [[AND]]
384384
;
385385
%t1 = trunc nuw nsw <4 x i32> %a to <4 x i8>
@@ -391,7 +391,7 @@ define <4 x i8> @and_trunc_nuw_nsw_multiconstant(<4 x i32> %a) {
391391
define <4 x i32> @or_zext_nneg_constant(<4 x i16> %a) {
392392
; CHECK-LABEL: @or_zext_nneg_constant(
393393
; CHECK-NEXT: [[OR_INNER:%.*]] = or <4 x i16> [[A:%.*]], <i16 1, i16 2, i16 3, i16 4>
394-
; CHECK-NEXT: [[OR:%.*]] = zext <4 x i16> [[OR_INNER]] to <4 x i32>
394+
; CHECK-NEXT: [[OR:%.*]] = zext nneg <4 x i16> [[OR_INNER]] to <4 x i32>
395395
; CHECK-NEXT: ret <4 x i32> [[OR]]
396396
;
397397
%z1 = zext nneg <4 x i16> %a to <4 x i32>

0 commit comments

Comments
 (0)