[llvm] [llvm][CodeGen] Added a check in CodeGenPrepare::optimizeSwitchType (PR #83322)
Michael Maitland via llvm-commits
llvm-commits at lists.llvm.org
Mon May 20 12:37:03 PDT 2024
================
@@ -7387,17 +7387,38 @@ bool CodeGenPrepare::optimizeSwitchType(SwitchInst *SI) {
// attribute. In that case, we can avoid an unnecessary mask/extension by
// matching the argument extension instead.
Instruction::CastOps ExtType = Instruction::ZExt;
- // Some targets prefer SExt over ZExt.
- if (TLI->isSExtCheaperThanZExt(OldVT, RegType))
- ExtType = Instruction::SExt;
+ unsigned SExtUsers = 0;
+ unsigned ZExtUsers = 0;
if (auto *Arg = dyn_cast<Argument>(Cond)) {
if (Arg->hasSExtAttr())
- ExtType = Instruction::SExt;
+ SExtUsers++;
if (Arg->hasZExtAttr())
- ExtType = Instruction::ZExt;
+ ZExtUsers++;
+ }
+ for (auto U : Cond->users()) {
+ if (isa<SExtInst>(U)) {
+ SExtUsers++;
+ } else if (isa<ZExtInst>(U)) {
+ ZExtUsers++;
+ } else if (auto *IC = dyn_cast<ICmpInst>(U)) {
+ if (IC->isSigned()) {
+ SExtUsers++;
+ } else if (IC->isUnsigned()) {
+ ZExtUsers++;
+ }
+ }
+ }
+ if (SExtUsers > ZExtUsers) {
+ ExtType = Instruction::SExt;
+ } else {
+ ExtType = Instruction::ZExt;
}
+ // Some targets prefer SExt over ZExt.
+ if (TLI->isSExtCheaperThanZExt(OldVT, RegType))
----------------
michaelmaitland wrote:
My reading of @arsenm's comment is that maybe it would be better to avoid the user scan if we dont need it as such:
```
if (TLI->isSExtCheaperThanZExt(OldVT, RegType))
ExtType = Instruction::SExt;
else {
// User scan above
}
https://github.com/llvm/llvm-project/pull/83322
More information about the llvm-commits
mailing list