Home | History | Annotate | Download | only in SimplifyCFG
      1 ; RUN: opt < %s -simplifycfg -S | FileCheck %s
      2 
      3 ; In PR17073 ( http://llvm.org/pr17073 ), we illegally hoisted an operation that can trap.
      4 ; The first test confirms that we don't do that when the trapping op is reached by the current BB (block1).
      5 ; The second test confirms that we don't do that when the trapping op is reached by the previous BB (entry).
      6 ; The third test confirms that we can still do this optimization for an operation (add) that doesn't trap.
      7 ; The tests must be complicated enough to prevent previous SimplifyCFG actions from optimizing away
      8 ; the instructions that we're checking for.
      9 
     10 target datalayout = "e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128"
     11 target triple = "i386-apple-macosx10.9.0"
     12 
     13 @a = common global i32 0, align 4
     14 @b = common global i8 0, align 1
     15 
     16 ; CHECK-LABEL: can_trap1 
     17 ; CHECK-NOT: or i1 %tobool, icmp eq (i32* bitcast (i8* @b to i32*), i32* @a)
     18 ; CHECK-NOT: select i1 %tobool, i32* null, i32* select (i1 icmp eq (i64 urem (i64 2, i64 zext (i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a) to i64)), i64 0), i32* null, i32* @a) 
     19 define i32* @can_trap1() {
     20 entry:
     21   %0 = load i32* @a, align 4
     22   %tobool = icmp eq i32 %0, 0
     23   br i1 %tobool, label %exit, label %block1
     24 
     25 block1:
     26   br i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a), label %exit, label %block2
     27 
     28 block2:
     29   br label %exit
     30 
     31 exit:
     32   %storemerge = phi i32* [ null, %entry ],[ null, %block2 ], [ select (i1 icmp eq (i64 urem (i64 2, i64 zext (i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a) to i64)), i64 0), i32* null, i32* @a), %block1 ]
     33   ret i32* %storemerge
     34 }
     35 
     36 ; CHECK-LABEL: can_trap2 
     37 ; CHECK-NOT: or i1 %tobool, icmp eq (i32* bitcast (i8* @b to i32*), i32* @a)
     38 ; CHECK-NOT: select i1 %tobool, i32* select (i1 icmp eq (i64 urem (i64 2, i64 zext (i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a) to i64)), i64 0), i32* null, i32* @a), i32* null
     39 define i32* @can_trap2() {
     40 entry:
     41   %0 = load i32* @a, align 4
     42   %tobool = icmp eq i32 %0, 0
     43   br i1 %tobool, label %exit, label %block1
     44 
     45 block1:
     46   br i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a), label %exit, label %block2
     47 
     48 block2:
     49   br label %exit
     50 
     51 exit:
     52   %storemerge = phi i32* [ select (i1 icmp eq (i64 urem (i64 2, i64 zext (i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a) to i64)), i64 0), i32* null, i32* @a), %entry ],[ null, %block2 ], [ null, %block1 ]
     53   ret i32* %storemerge
     54 }
     55 
     56 ; CHECK-LABEL: cannot_trap 
     57 ; CHECK: select i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a), i32* select (i1 icmp eq (i64 add (i64 zext (i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a) to i64), i64 2), i64 0), i32* null, i32* @a), i32* null
     58 define i32* @cannot_trap() {
     59 entry:
     60   %0 = load i32* @a, align 4
     61   %tobool = icmp eq i32 %0, 0
     62   br i1 %tobool, label %exit, label %block1
     63 
     64 block1:
     65   br i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a), label %exit, label %block2
     66 
     67 block2:
     68   br label %exit
     69 
     70 exit:
     71   %storemerge = phi i32* [ null, %entry ],[ null, %block2 ], [ select (i1 icmp eq (i64 add (i64 2, i64 zext (i1 icmp eq (i32* bitcast (i8* @b to i32*), i32* @a) to i64)), i64 0), i32* null, i32* @a), %block1 ]
     72   ret i32* %storemerge
     73 }
     74