1; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-linux-gnu < %s | FileCheck %s
2
3; The instructions ADDIStocHA8/LDtocL are used to calculate the address of
4; globals. The ones that are in bb.3.if.end could not be hoisted by Machine
5; LICM due to BCTRL_LDinto_toc in bb2.if.then.  This call causes the compiler
6; to insert a save TOC to stack before the call and load into X2 to restore TOC
7; after. By communicating to Machine LICM that X2 is guaranteed to have the
8; same value before and after BCTRL_LDinto_toc, these instructions can be
9; hoisted out of bb.3.if.end to outside of the loop.
10
11; Pre Machine LICM MIR
12;
13;body:
14;  bb.0.entry:
15;    successors: %bb.2.if.then(0x40000000), %bb.3.if.end(0x40000000)
16;    liveins: %x3
17;
18;    %4 = COPY %x3
19;    %5 = ADDIStocHA8 %x2, @ga
20;    %6 = LDtocL @ga, killed %5 :: (load 8 from got)
21;    %7 = LWZ 0, %6 :: (volatile dereferenceable load 4 from @ga)
22;    %8 = ADDIStocHA8 %x2, @gb
23;    %9 = LDtocL @gb, killed %8 :: (load 8 from got)
24;    %10 = LWZ 0, killed %9 :: (volatile dereferenceable load 4 from @gb)
25;    %0 = LWZ 0, %6 :: (volatile dereferenceable load 4 from @ga)
26;    %11 = CMPW killed %7, killed %10
27;    BCC 44, killed %11, %bb.2.if.then
28;    B %bb.3.if.end
29;
30;  bb.2.if.then:
31;    %1 = PHI %0, %bb.0.entry, %3, %bb.3.if.end
32;    ADJCALLSTACKDOWN 32, 0, implicit-def dead %r1, implicit %r1
33;    %20 = COPY %x2
34;    STD %20, 24, %x1 :: (store 8 into stack + 24)
35;    %21 = EXTSW_32_64 %1
36;    %x3 = COPY %21
37;    %x12 = COPY %4
38;    MTCTR8 %4, implicit-def %ctr8
39;    BCTRL8_LDinto_toc 24, %x1, csr_ppc64_altivec, implicit-def dead %lr8, implicit-def dead %x2, implicit %ctr8, implicit %rm, implicit %x3, implicit %x12, implicit %x2, implicit-def %r1, implicit-def %x3
40;    ADJCALLSTACKUP 32, 0, implicit-def dead %r1, implicit %r1
41;    %22 = COPY %x3
42;    %x3 = COPY %22
43;    BLR8 implicit %lr8, implicit %rm, implicit %x3
44;
45;  bb.3.if.end:
46;    successors: %bb.2.if.then(0x04000000), %bb.3.if.end(0x7c000000)
47;
48;    %2 = PHI %0, %bb.0.entry, %3, %bb.3.if.end
49;    %12 = ADDI %2, 1
50;    %13 = ADDIStocHA8 %x2, @ga
51;    %14 = LDtocL @ga, killed %13 :: (load 8 from got)
52;    STW killed %12, 0, %14 :: (volatile store 4 into @ga)
53;    %15 = LWZ 0, %14 :: (volatile dereferenceable load 4 from @ga)
54;    %16 = ADDIStocHA8 %x2, @gb
55;    %17 = LDtocL @gb, killed %16 :: (load 8 from got)
56;    %18 = LWZ 0, killed %17 :: (volatile dereferenceable load 4 from @gb)
57;    %3 = LWZ 0, %14 :: (volatile dereferenceable load 4 from @ga)
58;    %19 = CMPW killed %15, killed %18
59;    BCC 44, killed %19, %bb.2.if.then
60;    B %bb.3.if.end
61
62@ga = external global i32, align 4
63@gb = external global i32, align 4
64define signext i32 @test(i32 (i32)* nocapture %FP) local_unnamed_addr #0 {
65; CHECK-LABEL: test:
66; CHECK:       # %bb.0: # %entry
67; CHECK-NEXT:    mflr 0
68; CHECK:         addis 4, 2, .LC0@toc@ha
69; CHECK-NEXT:    addis 5, 2, .LC1@toc@ha
70; CHECK-NEXT:    mr 12, 3
71; CHECK-NEXT:    ld 4, .LC0@toc@l(4)
72; CHECK-NEXT:    ld 5, .LC1@toc@l(5)
73; CHECK-NEXT:    lwz 6, 0(4)
74; CHECK-NEXT:    lwz 7, 0(5)
75; CHECK-NEXT:    cmpw 6, 7
76; CHECK-NEXT:    lwz 6, 0(4)
77; CHECK-NEXT:    bgt 0, .LBB0_2
78; CHECK-NOT:    addis {{[0-9]+}}, 2, .LC0@toc@ha
79; CHECK-NOT:    addis {{[0-9]+}}, 2, .LC1@toc@ha
80; CHECK-NEXT:    .p2align 5
81; CHECK-NEXT:  .LBB0_1: # %if.end
82; CHECK-NOT:    addis {{[0-9]+}}, 2, .LC0@toc@ha
83; CHECK-NOT:    addis {{[0-9]+}}, 2, .LC1@toc@ha
84; CHECK:    blr
85entry:
86  %0 = load volatile i32, i32* @ga, align 4
87  %1 = load volatile i32, i32* @gb, align 4
88  %cmp1 = icmp sgt i32 %0, %1
89  %2 = load volatile i32, i32* @ga, align 4
90  br i1 %cmp1, label %if.then, label %if.end
91
92if.then:                                          ; preds = %if.end, %entry
93  %.lcssa = phi i32 [ %2, %entry ], [ %6, %if.end ]
94  %call = tail call signext i32 %FP(i32 signext %.lcssa) #1
95  ret i32 %call
96
97if.end:                                           ; preds = %entry, %if.end
98  %3 = phi i32 [ %6, %if.end ], [ %2, %entry ]
99  %inc = add nsw i32 %3, 1
100  store volatile i32 %inc, i32* @ga, align 4
101  %4 = load volatile i32, i32* @ga, align 4
102  %5 = load volatile i32, i32* @gb, align 4
103  %cmp = icmp sgt i32 %4, %5
104  %6 = load volatile i32, i32* @ga, align 4
105  br i1 %cmp, label %if.then, label %if.end
106}
107