[llvm] 95bda51 - [ConstantFold] Fold the comparison of bitcasted global values

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 20 12:44:32 PDT 2020


Author: Shimin Cui
Date: 2020-10-20T12:41:49-07:00
New Revision: 95bda510fb7b35ac66fe96b0f24f0ec16005802f

URL: https://github.com/llvm/llvm-project/commit/95bda510fb7b35ac66fe96b0f24f0ec16005802f
DIFF: https://github.com/llvm/llvm-project/commit/95bda510fb7b35ac66fe96b0f24f0ec16005802f.diff

LOG: [ConstantFold] Fold the comparison of bitcasted global values

This is to simplify icmp instructions in the form like:

%cmp = icmp eq i32 (i8*, i8*)* bitcast (i32 (i32**, i32**)* @f32 to i32
%(i8*, i8*)), bitcast (i32 (i64**, i64**) @f64 to i32 (i8*, i8*)*)

Here @f32 and @f64 are two functions.

Differential Revision: https://reviews.llvm.org/D87850

Added: 
    llvm/test/Transforms/InstCombine/icmp-bitcast-glob.ll

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 3f00dd057536..968742b599be 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1751,9 +1751,15 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
     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.

diff  --git a/llvm/test/Transforms/InstCombine/icmp-bitcast-glob.ll b/llvm/test/Transforms/InstCombine/icmp-bitcast-glob.ll
new file mode 100644
index 000000000000..c3ffdd472bcc
--- /dev/null
+++ b/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
+}

diff  --git a/llvm/test/Transforms/InstCombine/pr32686.ll b/llvm/test/Transforms/InstCombine/pr32686.ll
index b2d2aff2fde8..e91cff7309ae 100644
--- a/llvm/test/Transforms/InstCombine/pr32686.ll
+++ b/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() {

diff  --git a/llvm/test/Transforms/SCCP/ip-ranges-select.ll b/llvm/test/Transforms/SCCP/ip-ranges-select.ll
index f1f2b032ac46..c2996ffb1121 100644
--- a/llvm/test/Transforms/SCCP/ip-ranges-select.ll
+++ b/llvm/test/Transforms/SCCP/ip-ranges-select.ll
@@ -107,7 +107,7 @@ define i1 @caller2(i32 %y, i1 %cmp) {
   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)

diff  --git a/llvm/test/Transforms/SCCP/undef-resolve.ll b/llvm/test/Transforms/SCCP/undef-resolve.ll
index e2a4268596f4..9e91def34102 100644
--- a/llvm/test/Transforms/SCCP/undef-resolve.ll
+++ b/llvm/test/Transforms/SCCP/undef-resolve.ll
@@ -254,7 +254,7 @@ entry:
   ret i64 %e
 }
 
- at GV = external global i32
+ at GV = common global i32 0, align 4
 
 define i32 @test11(i1 %tobool) {
 ; CHECK-LABEL: @test11(


        


More information about the llvm-commits mailing list