[llvm] [CodeGenPrepare] Preserve flags (such as nsw/nuw) in SinkCast (PR #89904)
Alex Bradbury via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 24 02:57:57 PDT 2024
https://github.com/asb created https://github.com/llvm/llvm-project/pull/89904
As demonstrated in the test change, when deciding to sink a trunc we were losing its flags. This patch moves to cloning the original instruction instead.
CC @elhewaty
>From e518c530b67344f0dd2d0285a43ef678c4a7b529 Mon Sep 17 00:00:00 2001
From: Alex Bradbury <asb at igalia.com>
Date: Wed, 24 Apr 2024 10:46:41 +0100
Subject: [PATCH 1/2] [CodeGenPrepare][test] Add test for sinking of truncs
demonstrating nsw/nuw are dropped
SinkCast creates a new cast with the same type and inputs, which drops
the nsw/nuw flags.
---
.../CodeGenPrepare/RISCV/noop-copy-sink.ll | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
create mode 100644 llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll
diff --git a/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll b/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll
new file mode 100644
index 00000000000000..1b94bc798314ca
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll
@@ -0,0 +1,31 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -mtriple=riscv64 %s \
+; RUN: | FileCheck --check-prefixes=CHECK %s
+
+define i16 @sink_trunc1(i64 %a) {
+; CHECK-LABEL: @sink_trunc1(
+; CHECK-NEXT: fnend:
+; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[A:%.*]] to i16
+; CHECK-NEXT: ret i16 [[TMP0]]
+;
+ %trunc = trunc i64 %a to i16
+ br label %fnend
+
+fnend:
+ ret i16 %trunc
+}
+
+; The flags on the original trunc should be preserved.
+; FIXME: Flags are currently dropped.
+define i16 @sink_trunc2(i64 %a) {
+; CHECK-LABEL: @sink_trunc2(
+; CHECK-NEXT: fnend:
+; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[A:%.*]] to i16
+; CHECK-NEXT: ret i16 [[TMP0]]
+;
+ %trunc = trunc nuw nsw i64 %a to i16
+ br label %fnend
+
+fnend:
+ ret i16 %trunc
+}
>From 589d586279f60d14a3365e21f2b44952d056d134 Mon Sep 17 00:00:00 2001
From: Alex Bradbury <asb at igalia.com>
Date: Wed, 24 Apr 2024 10:51:44 +0100
Subject: [PATCH 2/2] [CodeGenPrepare] Preserve flags (such as nsw/nuw) in
SinkCast
As demonstrated in the test change, when deciding to sink a trunc we
were losing its flags. This patch moves to cloning the original
instruction instead.
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 4 +---
llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll | 3 +--
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 22a766f8d62524..be13db41ce5e66 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -1431,10 +1431,8 @@ static bool SinkCast(CastInst *CI) {
if (!InsertedCast) {
BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
assert(InsertPt != UserBB->end());
- InsertedCast = CastInst::Create(CI->getOpcode(), CI->getOperand(0),
- CI->getType(), "");
+ InsertedCast = static_cast<CastInst *>(CI->clone());
InsertedCast->insertBefore(*UserBB, InsertPt);
- InsertedCast->setDebugLoc(CI->getDebugLoc());
}
// Replace a use of the cast with a use of the new cast.
diff --git a/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll b/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll
index 1b94bc798314ca..55cde6c1431fe3 100644
--- a/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll
@@ -16,11 +16,10 @@ fnend:
}
; The flags on the original trunc should be preserved.
-; FIXME: Flags are currently dropped.
define i16 @sink_trunc2(i64 %a) {
; CHECK-LABEL: @sink_trunc2(
; CHECK-NEXT: fnend:
-; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[A:%.*]] to i16
+; CHECK-NEXT: [[TMP0:%.*]] = trunc nuw nsw i64 [[A:%.*]] to i16
; CHECK-NEXT: ret i16 [[TMP0]]
;
%trunc = trunc nuw nsw i64 %a to i16
More information about the llvm-commits
mailing list