llvm.org GIT mirror llvm / 96c454b
[globalisel][combines] Don't sink G_TRUNC down to use if that use is a G_PHI This fixes a problem where the register allocator fails to eliminate a PHI because there's a non-PHI in the middle of the PHI instructions at the start of a BB. This G_TRUNC can be better placed but this at least fixes the correctness issue quickly. I'll follow up with a patch to the verifier to catch this kind of bug in future. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@343693 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Sanders 1 year, 9 months ago
2 changed file(s) with 119 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
153153 assert(Preferred.Ty != LoadValueTy && "Extending to same type?");
154154
155155 // Rewrite the load and schedule the canonical use for erasure.
156 const auto TruncateUse = [](MachineIRBuilder &Builder, MachineOperand &UseMO,
157 unsigned DstReg, unsigned SrcReg) {
156 const auto TruncateUse = [&MI](MachineIRBuilder &Builder,
157 MachineOperand &UseMO, unsigned DstReg,
158 unsigned SrcReg) {
158159 MachineInstr &UseMI = *UseMO.getParent();
159160 MachineBasicBlock &UseMBB = *UseMI.getParent();
160161
161162 Builder.setInsertPt(UseMBB, MachineBasicBlock::iterator(UseMI));
163
164 if (UseMI.isPHI())
165 Builder.setInsertPt(*MI.getParent(),
166 std::next(MachineBasicBlock::iterator(MI)));
167
162168 Builder.buildTrunc(DstReg, SrcReg);
163169 };
164170
0 # RUN: llc -O0 -run-pass=aarch64-prelegalizer-combiner -global-isel %s -o - | FileCheck %s
1
2 --- |
3 target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
4 target triple = "aarch64--"
5 define void @multiple_copies(i8* %addr) {
6 entry:
7 br i1 0, label %if, label %else
8 if:
9 br label %exit
10 else:
11 br label %exit
12 exit:
13 ret void
14 }
15
16 define void @sink_to_phi(i8* %addr) {
17 entry:
18 br i1 0, label %if, label %exit
19 if:
20 br label %exit
21 exit:
22 ret void
23 }
24 ...
25
26 ---
27 name: multiple_copies
28 # CHECK-LABEL: name: multiple_copies
29 tracksRegLiveness: true
30 body: |
31 bb.0.entry:
32 liveins: $x0, $w1
33 successors: %bb.1(0x40000000), %bb.2(0x40000000); %bb.1(50.00%), %bb.2(50.00%)
34 ; CHECK: [[T0:%[0-9]+]]:_(s32) = G_SEXTLOAD
35 %0:_(p0) = COPY $x0
36 %1:_(s32) = COPY $w1
37 %2:_(s8) = G_LOAD %0 :: (load 1 from %ir.addr)
38 %3:_(s32) = G_SEXT %2
39 %4:_(s32) = G_CONSTANT i32 1
40 %5:_(s1) = G_ICMP intpred(ne), %1:_(s32), %4:_
41 G_BRCOND %5:_(s1), %bb.1
42 G_BR %bb.2.else
43 bb.1.if:
44 ; CHECK: bb.1.if:
45 successors: %bb.3(0x80000000)
46 %10:_(s8) = G_CONSTANT i8 1
47 ; CHECK: [[T1:%[0-9]+]]:_(s8) = G_TRUNC [[T0]](s32)
48 %6:_(s8) = G_ADD %2, %10
49 ; CHECK: [[T2:%[0-9]+]]:_(s8) = G_ADD [[T1]], {{.*}}
50 G_BR %bb.3.exit
51 bb.2.else:
52 ; CHECK: bb.2.else:
53 successors: %bb.3(0x80000000)
54 %11:_(s8) = G_CONSTANT i8 1
55 ; CHECK: [[T3:%[0-9]+]]:_(s8) = G_TRUNC [[T0]](s32)
56 %7:_(s8) = G_SUB %2, %11
57 ; CHECK: [[T4:%[0-9]+]]:_(s8) = G_SUB [[T3]], {{.*}}
58 G_BR %bb.3.exit
59 bb.3.exit:
60 ; CHECK: bb.3.exit:
61 %8:_(s8) = G_PHI %6:_(s8), %bb.1, %7:_(s8), %bb.2
62 ; CHECK: [[T5:%[0-9]+]]:_(s8) = G_PHI [[T2]](s8), %bb.1, [[T4]](s8)
63 %9:_(s32) = G_ZEXT %8
64 ; CHECK: [[T6:%[0-9]+]]:_(s32) = G_ZEXT [[T5]](s8)
65 ; CHECK: $w0 = COPY [[T0]](s32)
66 ; CHECK: $w1 = COPY [[T6]](s32)
67 $w0 = COPY %3
68 $w1 = COPY %9
69 ...
70
71 ---
72 name: sink_to_phi
73 # CHECK-LABEL: name: sink_to_phi
74 # This test currently tests that we don't sink if we would sink to a phi. This
75 # is needed to avoid inserting into the middle of the leading G_PHI instructions
76 # of a BB
77 tracksRegLiveness: true
78 body: |
79 bb.0.entry:
80 liveins: $x0, $w1
81 successors: %bb.1(0x40000000), %bb.2(0x40000000); %bb.1(50.00%), %bb.2(50.00%)
82 ; CHECK: [[T0:%[0-9]+]]:_(s32) = G_SEXTLOAD
83 %0:_(p0) = COPY $x0
84 %1:_(s32) = COPY $w1
85 %2:_(s8) = G_LOAD %0 :: (load 1 from %ir.addr)
86 ; CHECK: [[T4:%[0-9]+]]:_(s8) = G_TRUNC [[T0]](s32)
87 %3:_(s32) = G_SEXT %2
88 %4:_(s32) = G_CONSTANT i32 1
89 %5:_(s1) = G_ICMP intpred(ne), %1:_(s32), %4:_
90 G_BRCOND %5:_(s1), %bb.1
91 G_BR %bb.2.exit
92 bb.1.if:
93 ; CHECK: bb.1.if:
94 successors: %bb.2(0x80000000)
95 %10:_(s8) = G_CONSTANT i8 1
96 ; CHECK: [[T1:%[0-9]+]]:_(s8) = G_TRUNC [[T0]](s32)
97 %6:_(s8) = G_ADD %2, %10
98 ; CHECK: [[T2:%[0-9]+]]:_(s8) = G_ADD [[T1]], {{.*}}
99 G_BR %bb.2.exit
100 bb.2.exit:
101 ; CHECK: bb.2.exit:
102 %8:_(s8) = G_PHI %6:_(s8), %bb.1, %2:_(s8), %bb.0
103 ; CHECK: [[T5:%[0-9]+]]:_(s8) = G_PHI [[T2]](s8), %bb.1, [[T4]](s8)
104 %9:_(s32) = G_ZEXT %8
105 ; CHECK: [[T6:%[0-9]+]]:_(s32) = G_ZEXT [[T5]](s8)
106 ; CHECK: $w0 = COPY [[T0]](s32)
107 ; CHECK: $w1 = COPY [[T6]](s32)
108 $w0 = COPY %3
109 $w1 = COPY %9
110 ...