Skip to content

Commit 1c8410a

Browse files
authored
[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.
1 parent 2554a85 commit 1c8410a

File tree

2 files changed

+2
-5
lines changed

2 files changed

+2
-5
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,10 +1431,8 @@ static bool SinkCast(CastInst *CI) {
14311431
if (!InsertedCast) {
14321432
BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
14331433
assert(InsertPt != UserBB->end());
1434-
InsertedCast = CastInst::Create(CI->getOpcode(), CI->getOperand(0),
1435-
CI->getType(), "");
1434+
InsertedCast = cast<CastInst>(CI->clone());
14361435
InsertedCast->insertBefore(*UserBB, InsertPt);
1437-
InsertedCast->setDebugLoc(CI->getDebugLoc());
14381436
}
14391437

14401438
// Replace a use of the cast with a use of the new cast.

llvm/test/Transforms/CodeGenPrepare/RISCV/noop-copy-sink.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ fnend:
1616
}
1717

1818
; The flags on the original trunc should be preserved.
19-
; FIXME: Flags are currently dropped.
2019
define i16 @sink_trunc2(i64 %a) {
2120
; CHECK-LABEL: @sink_trunc2(
2221
; CHECK-NEXT: fnend:
23-
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[A:%.*]] to i16
22+
; CHECK-NEXT: [[TMP0:%.*]] = trunc nuw nsw i64 [[A:%.*]] to i16
2423
; CHECK-NEXT: ret i16 [[TMP0]]
2524
;
2625
%trunc = trunc nuw nsw i64 %a to i16

0 commit comments

Comments
 (0)