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

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 25 07:05:11 PDT 2024


Author: Alex Bradbury
Date: 2024-04-25T15:05:07+01:00
New Revision: 1c8410a67d9d7bbadc2e5f2c8698531b1060c8ac

URL: https://github.com/llvm/llvm-project/commit/1c8410a67d9d7bbadc2e5f2c8698531b1060c8ac
DIFF: https://github.com/llvm/llvm-project/commit/1c8410a67d9d7bbadc2e5f2c8698531b1060c8ac.diff

LOG: [CodeGenPrepare] Preserve flags (such as nsw/nuw) in SinkCast (#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.

Added: 
    

Modified: 
    llvm/lib/CodeGen/CodeGenPrepare.cpp
    llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 22a766f8d62524..8eaf78157550ee 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 = 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