Skip to content

Commit 2512ce3

Browse files
authored
[IR] Add support for trunc's nuw/nsw flags in copyIRFlags (#89353)
This patch fixes #85592 (comment). I found this while fixing flag propagation in my ["vectorizer"](https://github.com/dtcxzyw/llvm-codegen-benchmark/blob/main/vectorize.cpp).
1 parent 494c637 commit 2512ce3

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

llvm/lib/IR/Instruction.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,13 @@ void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
636636
}
637637
}
638638

639+
if (auto *TI = dyn_cast<TruncInst>(V)) {
640+
if (isa<TruncInst>(this)) {
641+
setHasNoSignedWrap(TI->hasNoSignedWrap());
642+
setHasNoUnsignedWrap(TI->hasNoUnsignedWrap());
643+
}
644+
}
645+
639646
// Copy the exact flag.
640647
if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
641648
if (isa<PossiblyExactOperator>(this))

llvm/test/Transforms/Scalarizer/basic.ll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ end:
283283
define <4 x float> @f6(<4 x float> %x) {
284284
; CHECK-LABEL: @f6(
285285
; CHECK-NEXT: [[X_I0:%.*]] = extractelement <4 x float> [[X:%.*]], i64 0
286-
; CHECK-NEXT: [[RES_I0:%.*]] = fadd float [[X_I0]], 1.000000e+00, !fpmath !9
286+
; CHECK-NEXT: [[RES_I0:%.*]] = fadd float [[X_I0]], 1.000000e+00, !fpmath [[META9:![0-9]+]]
287287
; CHECK-NEXT: [[X_I1:%.*]] = extractelement <4 x float> [[X]], i64 1
288-
; CHECK-NEXT: [[RES_I1:%.*]] = fadd float [[X_I1]], 2.000000e+00, !fpmath !9
288+
; CHECK-NEXT: [[RES_I1:%.*]] = fadd float [[X_I1]], 2.000000e+00, !fpmath [[META9]]
289289
; CHECK-NEXT: [[X_I2:%.*]] = extractelement <4 x float> [[X]], i64 2
290-
; CHECK-NEXT: [[RES_I2:%.*]] = fadd float [[X_I2]], 3.000000e+00, !fpmath !9
290+
; CHECK-NEXT: [[RES_I2:%.*]] = fadd float [[X_I2]], 3.000000e+00, !fpmath [[META9]]
291291
; CHECK-NEXT: [[X_I3:%.*]] = extractelement <4 x float> [[X]], i64 3
292-
; CHECK-NEXT: [[RES_I3:%.*]] = fadd float [[X_I3]], 4.000000e+00, !fpmath !9
292+
; CHECK-NEXT: [[RES_I3:%.*]] = fadd float [[X_I3]], 4.000000e+00, !fpmath [[META9]]
293293
; CHECK-NEXT: [[RES_UPTO0:%.*]] = insertelement <4 x float> poison, float [[RES_I0]], i64 0
294294
; CHECK-NEXT: [[RES_UPTO1:%.*]] = insertelement <4 x float> [[RES_UPTO0]], float [[RES_I1]], i64 1
295295
; CHECK-NEXT: [[RES_UPTO2:%.*]] = insertelement <4 x float> [[RES_UPTO1]], float [[RES_I2]], i64 2
@@ -865,6 +865,20 @@ define <2 x float> @f25(<2 x float> %src) {
865865
ret <2 x float> %mul
866866
}
867867

868+
define <2 x i8> @test_copy_trunc_flags(<2 x i32> %src) {
869+
; CHECK-LABEL: @test_copy_trunc_flags(
870+
; CHECK-NEXT: [[SRC_I0:%.*]] = extractelement <2 x i32> [[SRC:%.*]], i64 0
871+
; CHECK-NEXT: [[TRUNC_I0:%.*]] = trunc nuw nsw i32 [[SRC_I0]] to i8
872+
; CHECK-NEXT: [[SRC_I1:%.*]] = extractelement <2 x i32> [[SRC]], i64 1
873+
; CHECK-NEXT: [[TRUNC_I1:%.*]] = trunc nuw nsw i32 [[SRC_I1]] to i8
874+
; CHECK-NEXT: [[TRUNC_UPTO0:%.*]] = insertelement <2 x i8> poison, i8 [[TRUNC_I0]], i64 0
875+
; CHECK-NEXT: [[TRUNC:%.*]] = insertelement <2 x i8> [[TRUNC_UPTO0]], i8 [[TRUNC_I1]], i64 1
876+
; CHECK-NEXT: ret <2 x i8> [[TRUNC]]
877+
;
878+
%trunc = trunc nuw nsw <2 x i32> %src to <2 x i8>
879+
ret <2 x i8> %trunc
880+
}
881+
868882
!0 = !{ !"root" }
869883
!1 = !{ !"set1", !0 }
870884
!2 = !{ !"set2", !0 }

0 commit comments

Comments
 (0)