1 ; Tests to make sure that loads and stores in a diamond get merged
2 ; Loads are hoisted into the header. Stores sunks into the footer.
3 ; RUN: opt -passes=gvn-hoist -S < %s | FileCheck %s
4 target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
6 %struct.node = type { i64, ptr, ptr, ptr, i64, ptr, i64, i64, i64 }
7 %struct.arc = type { i64, i64, i64 }
9 define i64 @foo(ptr nocapture readonly %r) nounwind {
11 %node.0.in16 = getelementptr inbounds %struct.node, ptr %r, i64 0, i32 2
12 %node.017 = load ptr, ptr %node.0.in16, align 8
13 %tobool18 = icmp eq ptr %node.017, null
14 br i1 %tobool18, label %while.end, label %while.body.preheader
16 ; CHECK-LABEL: while.body.preheader
17 while.body.preheader: ; preds = %entry
21 while.body: ; preds = %while.body.preheader, %if.end
22 %node.020 = phi ptr [ %node.0, %if.end ], [ %node.017, %while.body.preheader ]
23 %sum.019 = phi i64 [ %inc, %if.end ], [ 0, %while.body.preheader ]
24 %orientation = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 4
25 %0 = load i64, ptr %orientation, align 8
26 %cmp = icmp eq i64 %0, 1
27 br i1 %cmp, label %if.then, label %if.else
29 if.then: ; preds = %while.body
30 %a = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 5
31 ; CHECK-NOT: load %struct.arc
32 %1 = load ptr, ptr %a, align 8
33 ; CHECK-NOT: load i64, ptr
34 %2 = load i64, ptr %1, align 8
35 %pred = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 1
36 ; CHECK-NOT: load ptr, ptr
37 %3 = load ptr, ptr %pred, align 8
38 %p = getelementptr inbounds %struct.node, ptr %3, i64 0, i32 6
39 ; CHECK-NOT: load i64, ptr
40 %4 = load i64, ptr %p, align 8
41 %add = add nsw i64 %4, %2
42 %p1 = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 6
44 store i64 %add, ptr %p1, align 8
48 if.else: ; preds = %while.body
49 %pred2 = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 1
50 ; CHECK-NOT: load ptr, ptr
51 %5 = load ptr, ptr %pred2, align 8
52 %p3 = getelementptr inbounds %struct.node, ptr %5, i64 0, i32 6
53 ; CHECK-NOT: load i64, ptr
54 %6 = load i64, ptr %p3, align 8
55 %a4 = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 5
56 ; CHECK-NOT: load ptr, ptr
57 %7 = load ptr, ptr %a4, align 8
58 ; CHECK-NOT: load i64, ptr
59 %8 = load i64, ptr %7, align 8
60 %sub = sub nsw i64 %6, %8
61 %p6 = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 6
63 store i64 %sub, ptr %p6, align 8
67 if.end: ; preds = %if.else, %if.then
69 %inc = add nsw i64 %sum.019, 1
70 %node.0.in = getelementptr inbounds %struct.node, ptr %node.020, i64 0, i32 2
71 %node.0 = load ptr, ptr %node.0.in, align 8
72 %tobool = icmp eq ptr %node.0, null
73 br i1 %tobool, label %while.end.loopexit, label %while.body
75 while.end.loopexit: ; preds = %if.end
76 %inc.lcssa = phi i64 [ %inc, %if.end ]
79 while.end: ; preds = %while.end.loopexit, %entry
80 %sum.0.lcssa = phi i64 [ 0, %entry ], [ %inc.lcssa, %while.end.loopexit ]