[llvm] [CodeGenPrepare] Preserve flags (such as nsw/nuw) in SinkCast (PR #89904)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 24 02:58:31 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Alex Bradbury (asb)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/89904.diff
2 Files Affected:
- (modified) llvm/lib/CodeGen/CodeGenPrepare.cpp (+1-3)
- (added) llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll (+30)
``````````diff
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
new file mode 100644
index 00000000000000..55cde6c1431fe3
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll
@@ -0,0 +1,30 @@
+; 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.
+define i16 @sink_trunc2(i64 %a) {
+; CHECK-LABEL: @sink_trunc2(
+; CHECK-NEXT: fnend:
+; CHECK-NEXT: [[TMP0:%.*]] = trunc nuw nsw i64 [[A:%.*]] to i16
+; CHECK-NEXT: ret i16 [[TMP0]]
+;
+ %trunc = trunc nuw nsw i64 %a to i16
+ br label %fnend
+
+fnend:
+ ret i16 %trunc
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/89904
More information about the llvm-commits
mailing list