[llvm] 32088f4 - [ConstantFolding] Fold freeze if it is never undef or poison

Juneyoung Lee via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 26 05:55:19 PDT 2020


Author: Juneyoung Lee
Date: 2020-07-26T21:54:44+09:00
New Revision: 32088f4f7fc734474d1249d288a34894a4e901a9

URL: https://github.com/llvm/llvm-project/commit/32088f4f7fc734474d1249d288a34894a4e901a9
DIFF: https://github.com/llvm/llvm-project/commit/32088f4f7fc734474d1249d288a34894a4e901a9.diff

LOG: [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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 794edeb714fd..7e4293d72a0e 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -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);

diff  --git a/llvm/test/Analysis/ConstantFolding/freeze.ll b/llvm/test/Analysis/ConstantFolding/freeze.ll
index c2da04130845..4908f71b924e 100644
--- a/llvm/test/Analysis/ConstantFolding/freeze.ll
+++ b/llvm/test/Analysis/ConstantFolding/freeze.ll
@@ -6,8 +6,7 @@
 
 define i64 @ptr
diff 1() {
 ; CHECK-LABEL: @ptr
diff 1(
-; 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 @ptr
diff 1() {
 
 define i64 @ptr
diff 2() {
 ; CHECK-LABEL: @ptr
diff 2(
-; 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


        


More information about the llvm-commits mailing list