[PATCH] D87850: [ConstantFold] Fold the comparison of bitcasted global values

Shimin Cui via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 11:36:40 PDT 2020


scui updated this revision to Diff 296246.
scui added a comment.

Remove the added extra cases from the existing test files


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87850/new/

https://reviews.llvm.org/D87850

Files:
  llvm/lib/IR/ConstantFold.cpp
  llvm/test/Transforms/InstCombine/icmp-bitcast-glob.ll
  llvm/test/Transforms/InstCombine/pr32686.ll
  llvm/test/Transforms/SCCP/ip-ranges-select.ll
  llvm/test/Transforms/SCCP/undef-resolve.ll


Index: llvm/test/Transforms/SCCP/undef-resolve.ll
===================================================================
--- llvm/test/Transforms/SCCP/undef-resolve.ll
+++ llvm/test/Transforms/SCCP/undef-resolve.ll
@@ -254,7 +254,7 @@
   ret i64 %e
 }
 
- at GV = external global i32
+ at GV = common global i32 0, align 4
 
 define i32 @test11(i1 %tobool) {
 ; CHECK-LABEL: @test11(
Index: llvm/test/Transforms/SCCP/ip-ranges-select.ll
===================================================================
--- llvm/test/Transforms/SCCP/ip-ranges-select.ll
+++ llvm/test/Transforms/SCCP/ip-ranges-select.ll
@@ -107,7 +107,7 @@
   ret i1 %res
 }
 
- at GV = external global i32
+ at GV = common global i32 0, align 4
 
 define i32 @f3_constantexpr_cond(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i32 @f3_constantexpr_cond(i32 %x, i32 %y)
Index: llvm/test/Transforms/InstCombine/pr32686.ll
===================================================================
--- llvm/test/Transforms/InstCombine/pr32686.ll
+++ llvm/test/Transforms/InstCombine/pr32686.ll
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -instcombine %s | FileCheck %s
 
- at a = external global i8
+ at a = common global i8 0, align 1
 @b = external global i32
 
 define void @tinkywinky() {
Index: llvm/test/Transforms/InstCombine/icmp-bitcast-glob.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/icmp-bitcast-glob.ll
@@ -0,0 +1,30 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+declare i32 @f32(i32**, i32**)
+
+declare i32 @f64(i64**, i64**)
+
+define i1 @icmp_func() {
+; CHECK-LABEL: @icmp_func(
+; CHECK: ret i1 false
+  %cmp = icmp eq i32 (i8*, i8*)* bitcast (i32 (i32**, i32**)* @f32 to i32 (i8*, i8*)*), bitcast (i32 (i64**, i64**)* @f64 to i32 (i8*, i8*)*)
+  ret i1 %cmp
+}
+
+define i1 @icmp_fptr(i32 (i8*, i8*)*) {
+; CHECK-LABEL: @icmp_fptr(
+; CHECK: %cmp = icmp ne i32 (i8*, i8*)* %0, bitcast (i32 (i32**, i32**)* @f32 to i32 (i8*, i8*)*)
+; CHECK: ret i1 %cmp
+  %cmp = icmp ne i32 (i8*, i8*)* bitcast (i32 (i32**, i32**)* @f32 to i32 (i8*, i8*)*), %0
+  ret i1 %cmp
+}
+
+ at b = external global i32
+
+define i32 @icmp_glob(i32 %x, i32 %y) {
+; CHECK-LABEL: define i32 @icmp_glob(i32 %x, i32 %y)
+; CHECK-NEXT:   ret i32 %y
+;
+  %sel = select i1 icmp eq (i32* bitcast (i32 (i32, i32)* @icmp_glob to i32*), i32* @b), i32 %x, i32 %y
+  ret i32 %sel
+}
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -1751,9 +1751,15 @@
     case Instruction::FPToSI:
       break; // We can't evaluate floating point casts or truncations.
 
+    case Instruction::BitCast:
+      // If this is a global value cast, check to see if the RHS is also a
+      // GlobalValue.
+      if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0))
+        if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2))
+          return areGlobalsPotentiallyEqual(GV, GV2);
+      LLVM_FALLTHROUGH;
     case Instruction::UIToFP:
     case Instruction::SIToFP:
-    case Instruction::BitCast:
     case Instruction::ZExt:
     case Instruction::SExt:
       // We can't evaluate floating point casts or truncations.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87850.296246.patch
Type: text/x-patch
Size: 3301 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201005/ab4c36b1/attachment-0001.bin>


More information about the llvm-commits mailing list