[ConstantFolding] Fold freeze if it is never undef or poison
authorJuneyoung Lee <aqjune@gmail.com>
Sun, 26 Jul 2020 12:54:44 +0000 (21:54 +0900)
committerJuneyoung Lee <aqjune@gmail.com>
Sun, 26 Jul 2020 12:54:44 +0000 (21:54 +0900)
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

llvm/lib/Analysis/ConstantFolding.cpp
llvm/test/Analysis/ConstantFolding/freeze.ll

index 794edeb..7e4293d 100644 (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);
index c2da041..4908f71 100644 (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