[llvm] [CodeGenPrepare] Preserve flags (such as nsw/nuw) in SinkCast (PR #89904)

Alex Bradbury via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 25 07:04:32 PDT 2024


https://github.com/asb updated https://github.com/llvm/llvm-project/pull/89904

>From 3be22b6339aa5dd64895b6583c38ae078a7872c3 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 1/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

>From a242885647b29738e1a15d82d8b4fc3522f90147 Mon Sep 17 00:00:00 2001
From: Alex Bradbury <asb at igalia.com>
Date: Wed, 24 Apr 2024 12:40:07 +0100
Subject: [PATCH 2/2] Use cast rather than static_cast

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index be13db41ce5e66..8eaf78157550ee 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -1431,7 +1431,7 @@ static bool SinkCast(CastInst *CI) {
     if (!InsertedCast) {
       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
       assert(InsertPt != UserBB->end());
-      InsertedCast = static_cast<CastInst *>(CI->clone());
+      InsertedCast = cast<CastInst>(CI->clone());
       InsertedCast->insertBefore(*UserBB, InsertPt);
     }
 



More information about the llvm-commits mailing list