llvm.org GIT mirror llvm / a1759fc
[SimplifyIndVars] Ignore dead users IndVarSimplify sometimes makes transforms basing on users that are trivially dead. In particular, if DCE wasn't run before it, there may be a dead `sext/zext` in loop that will trigger widening transforms, however it makes no sense to do it. This patch teaches IndVarsSimplify ignore the mist trivial cases of that. Differential Revision: https://reviews.llvm.org/D47974 Reviewed By: sanjoy git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334567 91177308-0d34-0410-b5e6-96231b3b80d8 Max Kazantsev 1 year, 11 months ago
9 changed file(s) with 105 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
2525 #include "llvm/IR/PatternMatch.h"
2626 #include "llvm/Support/Debug.h"
2727 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Transforms/Utils/Local.h"
2829
2930 using namespace llvm;
3031
771772 SimpleIVUsers.pop_back_val();
772773 Instruction *UseInst = UseOper.first;
773774
775 // If a user of the IndVar is trivially dead, we prefer just to mark it dead
776 // rather than try to do some complex analysis or transformation (such as
777 // widening) basing on it.
778 // TODO: Propagate TLI and pass it here to handle more cases.
779 if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) {
780 DeadInsts.emplace_back(UseInst);
781 continue;
782 }
783
774784 // Bypass back edges to avoid extra work.
775785 if (UseInst == CurrIV) continue;
776786
66 ; the recurrence to be zero, reducing the whole thing to a constant expression.
77 ;
88 ; PR12929: cast() argument of incompatible type
9
10 declare void @use(i8 %x)
911
1012 ; CHECK: @func
1113 ; CHECK: for.cond:
3234 %indvars.iv = phi i8 [ %indvars.iv.next, %for.cond.loopexit ], [ 10, %entry ]
3335 %mul3 = phi i8 [ undef, %entry ], [ %mul.lcssa, %for.cond.loopexit ]
3436 %inc1 = phi i8 [ 0, %entry ], [ %0, %for.cond.loopexit ]
37 call void @use(i8 %inc1)
3538 br label %for.body
3639
3740 for.body: ; preds = %for.body, %for.cond
3942 %mul45 = phi i8 [ %mul3, %for.cond ], [ %mul, %for.body ]
4043 %inc = add i8 %inc26, 1
4144 %mul = mul i8 %inc26, %mul45
45 call void @use(i8 %inc)
4246 %exitcond = icmp ne i8 %inc, %indvars.iv
4347 br i1 %exitcond, label %for.body, label %for.cond.loopexit
4448 }
88 target datalayout = "n8:16:32:64"
99
1010 declare void @llvm.experimental.guard(i1, ...)
11
12 declare void @use(i64 %x)
1113
1214 define void @test_1(i1* %cond_buf, i32* %len_buf) {
1315 ; CHECK-LABEL: @test_1(
5961 %iv.inc = add i32 %iv, 1
6062
6163 %iv.sext = sext i32 %iv to i64
64 call void @use(i64 %iv.sext)
6265
6366 %iv.inc.cmp = icmp slt i32 %iv.inc, %len
6467 call void(i1, ...) @llvm.experimental.guard(i1 %iv.inc.cmp) [ "deopt"() ]
33 ; sext cannot be hoisted outside the loop.
44
55 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
6
7 declare void @use(i64 %x)
68
79 define void @test() nounwind {
810 entry:
2527
2628 %add = add nsw i32 %i2.19, 1
2729 %add.idxprom = sext i32 %add to i64
30 call void @use(i64 %add.idxprom)
2831
2932 %sub = sub nsw i32 %i2.19, 2
3033 %sub.idxprom = sext i32 %sub to i64
34 call void @use(i64 %sub.idxprom)
3135
3236 %sub.neg = sub nsw i32 4, %i2.19
3337 %sub.neg.idxprom = sext i32 %sub.neg to i64
38 call void @use(i64 %sub.neg.idxprom)
3439
3540 %mul = mul nsw i32 %i2.19, 8
3641 %mul.idxprom = sext i32 %mul to i64
42 call void @use(i64 %mul.idxprom)
3743
3844 %add249 = add nsw i32 %i2.19, %shl132
3945 br label %for.body170
66
77 target triple = "x86_64-apple-darwin"
88
9 declare void @use(i64 %x)
10
911 ; CHECK-LABEL: @loop_0
1012 ; CHECK-LABEL: B18:
1113 ; Only one phi now.
12 ; CHECK: phi
14 ; CHECK: phi i64
1315 ; CHECK-NOT: phi
1416 ; One trunc for the gep.
1517 ; CHECK: trunc i64 %indvars.iv to i32
1719 ; CHECK-LABEL: exit24:
1820 ; CHECK: trunc i64 {{.*}}lcssa.wide to i32
1921 define void @loop_0(i32* %a) {
22 Prologue:
23 br i1 undef, label %B18, label %B6
24
25 B18: ; preds = %B24, %Prologue
26 %.02 = phi i32 [ 0, %Prologue ], [ %tmp33, %B24 ]
27 %tmp23 = zext i32 %.02 to i64
28 call void @use(i64 %tmp23)
29 %tmp33 = add i32 %.02, 1
30 %o = getelementptr i32, i32* %a, i32 %.02
31 %v = load i32, i32* %o
32 %t = icmp eq i32 %v, 0
33 br i1 %t, label %exit24, label %B24
34
35 B24: ; preds = %B18
36 %t2 = icmp eq i32 %tmp33, 20
37 br i1 %t2, label %B6, label %B18
38
39 B6: ; preds = %Prologue
40 ret void
41
42 exit24: ; preds = %B18
43 call void @dummy(i32 %.02)
44 unreachable
45 }
46
47 ; Make sure that dead zext is removed and no widening happens.
48 ; CHECK-LABEL: @loop_0.dead
49 ; CHECK: phi i32
50 ; CHECK-NOT: zext
51 ; CHECK-NOT: trunc
52 define void @loop_0.dead(i32* %a) {
2053 Prologue:
2154 br i1 undef, label %B18, label %B6
2255
44 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
55
66 ; Perform LFTR using the original pointer-type IV.
7
8 declare void @use(double %x)
79
810 ; for(char* p = base; p < base + n; ++p) {
911 ; *p = p-base;
9496 %cmp = icmp slt i32 1, %irow
9597 br i1 %cmp, label %loop, label %return
9698
99 loop:
100 %rowidx = phi i32 [ 0, %entry ], [ %row.inc, %loop ]
101 %i = phi i32 [ 0, %entry ], [ %i.inc, %loop ]
102 %diagidx = add nsw i32 %rowidx, %i
103 %diagidxw = sext i32 %diagidx to i64
104 %matrixp = getelementptr inbounds [0 x double], [0 x double]* %matrix, i32 0, i64 %diagidxw
105 %v1 = load double, double* %matrixp
106 call void @use(double %v1)
107 %iw = sext i32 %i to i64
108 %vectorp = getelementptr inbounds [0 x double], [0 x double]* %vector, i32 0, i64 %iw
109 %v2 = load double, double* %vectorp
110 call void @use(double %v2)
111 %row.inc = add nsw i32 %rowidx, %ilead
112 %i.inc = add nsw i32 %i, 1
113 %cmp196 = icmp slt i32 %i.inc, %irow
114 br i1 %cmp196, label %loop, label %return
115
116 return:
117 ret void
118 }
119
120 ; Avoid generating extra code to materialize a trip count. Skip LFTR.
121 define void @unguardedloop([0 x double]* %matrix, [0 x double]* %vector,
122 i32 %irow, i32 %ilead) nounwind {
123 entry:
124 br label %loop
125
126 ; CHECK: entry:
127 ; CHECK-NOT: zext
128 ; CHECK-NOT: add
129 ; CHECK: loop:
130 ; CHECK: phi i64
131 ; CHECK-NOT: phi
132 ; CHECK: icmp slt
133 ; CHECK: br i1
97134 loop:
98135 %rowidx = phi i32 [ 0, %entry ], [ %row.inc, %loop ]
99136 %i = phi i32 [ 0, %entry ], [ %i.inc, %loop ]
113150 ret void
114151 }
115152
116 ; Avoid generating extra code to materialize a trip count. Skip LFTR.
117 define void @unguardedloop([0 x double]* %matrix, [0 x double]* %vector,
118 i32 %irow, i32 %ilead) nounwind {
119 entry:
120 br label %loop
121
122 ; CHECK: entry:
123 ; CHECK-NOT: zext
124 ; CHECK-NOT: add
125 ; CHECK: loop:
126 ; CHECK: phi i64
127 ; CHECK: phi i64
128 ; CHECK-NOT: phi
129 ; CHECK: icmp slt
130 ; CHECK: br i1
131 loop:
132 %rowidx = phi i32 [ 0, %entry ], [ %row.inc, %loop ]
133 %i = phi i32 [ 0, %entry ], [ %i.inc, %loop ]
134 %diagidx = add nsw i32 %rowidx, %i
135 %diagidxw = sext i32 %diagidx to i64
136 %matrixp = getelementptr inbounds [0 x double], [0 x double]* %matrix, i32 0, i64 %diagidxw
137 %v1 = load double, double* %matrixp
138 %iw = sext i32 %i to i64
139 %vectorp = getelementptr inbounds [0 x double], [0 x double]* %vector, i32 0, i64 %iw
140 %v2 = load double, double* %vectorp
141 %row.inc = add nsw i32 %rowidx, %ilead
142 %i.inc = add nsw i32 %i, 1
143 %cmp196 = icmp slt i32 %i.inc, %irow
144 br i1 %cmp196, label %loop, label %return
145
146 return:
147 ret void
148 }
149
150153 ; Remove %i which is only used by the exit test.
151154 ; Verify that SCEV can still compute a backedge count from the sign
152155 ; extended %n, used for pointer comparison by LFTR.
291291 for.end: ; preds = %if.end, %entry
292292 ret void
293293 }
294
295 declare void @use(i1 %x)
294296
295297 ; check that we handle conditions with loop invariant operands which
296298 ; *aren't* in the header - this is a very rare and fragile case where
319321 br i1 true, label %exit, label %loop
320322
321323 exit:
324 call void @use(i1 %cmp)
322325 ret void
323326 }
324327
356356 ret void
357357 }
358358
359 declare void @use32(i32 %x)
360 declare void @use64(i64 %x)
361
359362 ; Test a widened IV that is used by a phi on different paths within the loop.
360363 ;
361364 ; CHECK: for.body:
384387
385388 if.then97:
386389 %idxprom100 = sext i32 %iv to i64
390 call void @use64(i64 %idxprom100)
387391 br label %for.inc
388392
389393 for.inc:
390394 %kmin.1 = phi i32 [ %iv, %if.then33 ], [ 0, %if.then ], [ %iv, %if.then97 ], [ 0, %if.else ]
395 call void @use32(i32 %kmin.1)
391396 %inc = add nsw i32 %iv, 1
392397 br i1 undef, label %for.body, label %for.end
393398
0 ; RUN: opt < %s -indvars -S | FileCheck %s
11 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
2
3 declare void @use(i64 %x)
24
35 ; CHECK-LABEL: @foo
46 define void @foo() {
1416 L2_header:
1517 %i = phi i32 [ 0, %L1_header ], [ %i_next, %L2_latch ]
1618 %i_prom = sext i32 %i to i64
19 call void @use(i64 %i_prom)
1720 br label %L3_header
1821
1922 L3_header: