aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2023-09-29 14:00:23 +0200
committerNikita Popov <npopov@redhat.com>2023-10-02 12:40:20 +0200
commit3b25407d977d9ed3dbcaccd4f8850b65e95d70fd (patch)
tree8b4d04fb81e7d290608310d28e7dc1791c113a01 /llvm/lib/Analysis/ConstantFolding.cpp
parent2214026e957397cc6385f778b28d570485a31856 (diff)
downloadllvm-3b25407d977d9ed3dbcaccd4f8850b65e95d70fd.zip
llvm-3b25407d977d9ed3dbcaccd4f8850b65e95d70fd.tar.gz
llvm-3b25407d977d9ed3dbcaccd4f8850b65e95d70fd.tar.bz2
[IR] Mark zext/sext constant expressions as undesirable
Introduce isDesirableCastOp() which determines whether IR builder and constant folding should produce constant expressions for a given cast type. This mirrors what we do for binary operators. Mark zext/sext as undesirable, which prevents most creations of such constant expressions. This is still somewhat incomplete and there are a few more places that can create zext/sext expressions. This is part of the work for https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179. The reason for the odd result in the constantexpr-fneg.c test is that initially the "a[]" global is created with an [0 x i32] type, at which point the icmp expression cannot be folded. Later it is replaced with an [1 x i32] global and the icmp gets folded away. But at that point we no longer fold the zext.
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp11
1 files changed, 7 insertions, 4 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index fab73c2..2f164e8 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1435,7 +1435,7 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
/*IsSigned=*/false);
}
}
- return ConstantExpr::getCast(Opcode, C, DestTy);
+ break;
case Instruction::IntToPtr:
// If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
// the int size is >= the ptr size and the address spaces are the same.
@@ -1454,8 +1454,7 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
}
}
}
-
- return ConstantExpr::getCast(Opcode, C, DestTy);
+ break;
case Instruction::Trunc:
case Instruction::ZExt:
case Instruction::SExt:
@@ -1466,10 +1465,14 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
case Instruction::FPToUI:
case Instruction::FPToSI:
case Instruction::AddrSpaceCast:
- return ConstantExpr::getCast(Opcode, C, DestTy);
+ break;
case Instruction::BitCast:
return FoldBitCast(C, DestTy, DL);
}
+
+ if (ConstantExpr::isDesirableCastOp(Opcode))
+ return ConstantExpr::getCast(Opcode, C, DestTy);
+ return ConstantFoldCastInstruction(Opcode, C, DestTy);
}
//===----------------------------------------------------------------------===//