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

Juneyoung Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 26 05:12:39 PDT 2020


aqjune created this revision.
aqjune added reviewers: spatel, lebedev.ri, nikic.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84597

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


Index: llvm/test/Analysis/ConstantFolding/freeze.ll
===================================================================
--- llvm/test/Analysis/ConstantFolding/freeze.ll
+++ llvm/test/Analysis/ConstantFolding/freeze.ll
@@ -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 @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
Index: llvm/lib/Analysis/ConstantFolding.cpp
===================================================================
--- llvm/lib/Analysis/ConstantFolding.cpp
+++ llvm/lib/Analysis/ConstantFolding.cpp
@@ -1069,6 +1069,8 @@
 
   switch (Opcode) {
   default: return nullptr;
+  case Instruction::Freeze:
+    return isGuaranteedNotToBeUndefOrPoison(Ops[0]) ? Ops[0] : nullptr;
   case Instruction::ICmp:
   case Instruction::FCmp: llvm_unreachable("Invalid for compares");
   case Instruction::Call:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84597.280718.patch
Type: text/x-patch
Size: 1345 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200726/f4bf6b50/attachment.bin>


More information about the llvm-commits mailing list