[ConstantFolding] Fold freeze if it is never undef or poison

This is a simple patch that adds constant folding for freeze
instruction.

IIUC, it isn't needed to update ConstantFold.cpp because there is no freeze
constexpr.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D84597
This commit is contained in:
Juneyoung Lee 2020-07-26 21:54:44 +09:00
parent 9f074214b7
commit 32088f4f7f
2 changed files with 4 additions and 4 deletions

View File

@ -1071,6 +1071,8 @@ Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
default: return nullptr;
case Instruction::ICmp:
case Instruction::FCmp: llvm_unreachable("Invalid for compares");
case Instruction::Freeze:
return isGuaranteedNotToBeUndefOrPoison(Ops[0]) ? Ops[0] : nullptr;
case Instruction::Call:
if (auto *F = dyn_cast<Function>(Ops.back())) {
const auto *Call = cast<CallBase>(InstOrCE);

View File

@ -6,8 +6,7 @@
define i64 @ptrdiff1() {
; CHECK-LABEL: @ptrdiff1(
; CHECK-NEXT: [[R:%.*]] = freeze i64 sub (i64 ptrtoint (i16* @g to i64), i64 ptrtoint (i16* @g2 to i64))
; CHECK-NEXT: ret i64 [[R]]
; CHECK-NEXT: ret i64 sub (i64 ptrtoint (i16* @g to i64), i64 ptrtoint (i16* @g2 to i64))
;
%i = ptrtoint i16* @g to i64
%i2 = ptrtoint i16* @g2 to i64
@ -18,8 +17,7 @@ define i64 @ptrdiff1() {
define i64 @ptrdiff2() {
; CHECK-LABEL: @ptrdiff2(
; CHECK-NEXT: [[R:%.*]] = freeze i64 -2
; CHECK-NEXT: ret i64 [[R]]
; CHECK-NEXT: ret i64 -2
;
%i = ptrtoint i16* @g to i64
%gep = getelementptr i16, i16* @g, i64 1