aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
diff options
context:
space:
mode:
authorYingwei Zheng <dtcxzyw2333@gmail.com>2024-04-11 19:10:53 +0800
committerGitHub <noreply@github.com>2024-04-11 19:10:53 +0800
commitb1094776152b68efa05f69b7b833f9cbc0727efc (patch)
tree1072025da2c771c822d01289c308bd86f567155c /llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
parent9d9bb7b1b6e96dc833133dacf1e2c7d9792e640e (diff)
downloadllvm-b1094776152b68efa05f69b7b833f9cbc0727efc.zip
llvm-b1094776152b68efa05f69b7b833f9cbc0727efc.tar.gz
llvm-b1094776152b68efa05f69b7b833f9cbc0727efc.tar.bz2
[InstCombine] Infer nsw/nuw for trunc (#87910)
This patch adds support for inferring trunc's nsw/nuw flags.
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 0652a8b..437e9b9 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -897,7 +897,20 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
}
}
- return nullptr;
+ bool Changed = false;
+ if (!Trunc.hasNoSignedWrap() &&
+ ComputeMaxSignificantBits(Src, /*Depth=*/0, &Trunc) <= DestWidth) {
+ Trunc.setHasNoSignedWrap(true);
+ Changed = true;
+ }
+ if (!Trunc.hasNoUnsignedWrap() &&
+ MaskedValueIsZero(Src, APInt::getBitsSetFrom(SrcWidth, DestWidth),
+ /*Depth=*/0, &Trunc)) {
+ Trunc.setHasNoUnsignedWrap(true);
+ Changed = true;
+ }
+
+ return Changed ? &Trunc : nullptr;
}
Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp,