llvm.org GIT mirror llvm / dbc8b93
[SimplifyIndvar] Replace the sdiv used by IV if we can prove both of its operands are non-negative Since there is no sdiv in SCEV, an 'udiv' is a better canonical form than an 'sdiv' as the user of induction variable Differential Revision: https://reviews.llvm.org/D31488 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299118 91177308-0d34-0410-b5e6-96231b3b80d8 Hongbin Zheng 2 years ago
2 changed file(s) with 168 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
3434 STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
3535 STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
3636 STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
37 STATISTIC(
38 NumSimplifiedSDiv,
39 "Number of IV signed division operations converted to unsigned division");
3740 STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
3841
3942 namespace {
7477 void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
7578 void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand,
7679 bool IsSigned);
80 bool eliminateSDiv(BinaryOperator *SDiv);
7781 bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
7882 };
7983 }
264268 Changed = true;
265269 }
266270
271 bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
272 // Get the SCEVs for the ICmp operands.
273 auto *N = SE->getSCEV(SDiv->getOperand(0));
274 auto *D = SE->getSCEV(SDiv->getOperand(1));
275
276 // Simplify unnecessary loops away.
277 const Loop *L = LI->getLoopFor(SDiv->getParent());
278 N = SE->getSCEVAtScope(N, L);
279 D = SE->getSCEVAtScope(D, L);
280
281 // Replace sdiv by udiv if both of the operands are non-negative
282 if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
283 auto *UDiv = BinaryOperator::Create(
284 BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
285 SDiv->getName() + ".udiv", SDiv);
286 UDiv->setIsExact(SDiv->isExact());
287 SDiv->replaceAllUsesWith(UDiv);
288 DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
289 ++NumSimplifiedSDiv;
290 Changed = true;
291 DeadInsts.push_back(SDiv);
292 return true;
293 }
294
295 return false;
296 }
297
267298 /// SimplifyIVUsers helper for eliminating useless
268299 /// remainder operations operating on an induction variable.
269300 void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem,
425456 eliminateIVComparison(ICmp, IVOperand);
426457 return true;
427458 }
428 if (BinaryOperator *Rem = dyn_cast(UseInst)) {
429 bool IsSigned = Rem->getOpcode() == Instruction::SRem;
430 if (IsSigned || Rem->getOpcode() == Instruction::URem) {
431 eliminateIVRemainder(Rem, IVOperand, IsSigned);
459 if (BinaryOperator *Bin = dyn_cast(UseInst)) {
460 bool IsSRem = Bin->getOpcode() == Instruction::SRem;
461 if (IsSRem || Bin->getOpcode() == Instruction::URem) {
462 eliminateIVRemainder(Bin, IVOperand, IsSRem);
432463 return true;
433464 }
465
466 if (Bin->getOpcode() == Instruction::SDiv)
467 return eliminateSDiv(Bin);
434468 }
435469
436470 if (auto *CI = dyn_cast(UseInst))
0 ; RUN: opt < %s -indvars -S | FileCheck %s
1
2 define void @test0(i32* %a) {
3 ; CHECK-LABEL: @test0(
4 entry:
5 br label %for.body
6
7 for.body: ; preds = %entry, %for.body
8 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
9 %div = sdiv i32 %i.01, 2
10 ; CHECK-NOT: sdiv
11 ; CHECK: udiv
12 %idxprom = sext i32 %div to i64
13 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
14 store i32 %i.01, i32* %arrayidx, align 4
15 %inc = add nsw i32 %i.01, 1
16 %cmp = icmp slt i32 %inc, 64
17 br i1 %cmp, label %for.body, label %for.end
18
19 for.end: ; preds = %for.body
20 ret void
21 }
22
23 define void @test1(i32* %a) {
24 ; CHECK-LABEL: @test1(
25 entry:
26 br label %for.body
27
28 for.body: ; preds = %entry, %for.body
29 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
30 %div = sdiv exact i32 %i.01, 2
31 ; CHECK-NOT: sdiv
32 ; CHECK: udiv exact
33 %idxprom = sext i32 %div to i64
34 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
35 store i32 %i.01, i32* %arrayidx, align 4
36 %inc = add nsw i32 %i.01, 1
37 %cmp = icmp slt i32 %inc, 64
38 br i1 %cmp, label %for.body, label %for.end
39
40 for.end: ; preds = %for.body
41 ret void
42 }
43
44 define void @test2(i32* %a, i32 %d) {
45 ; CHECK-LABEL: @test2(
46 entry:
47 br label %for.body
48
49 for.body: ; preds = %entry, %for.body
50 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
51 %mul = mul nsw i32 %i.01, 64
52 %div = sdiv i32 %mul, %d
53 ; CHECK-NOT: udiv
54 %idxprom = sext i32 %div to i64
55 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
56 store i32 %i.01, i32* %arrayidx, align 4
57 %inc = add nsw i32 %i.01, 1
58 %cmp = icmp slt i32 %inc, 64
59 br i1 %cmp, label %for.body, label %for.end
60
61 for.end: ; preds = %for.body
62 ret void
63 }
64
65 define void @test3(i32* %a) {
66 ; CHECK-LABEL: @test3(
67 entry:
68 br label %for.body
69
70 for.body: ; preds = %entry, %for.body
71 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
72 %div = sdiv i32 2048, %i.01
73 ; CHECK: udiv
74 ; CHECK-NOT: sdiv
75 %idxprom = sext i32 %div to i64
76 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
77 store i32 %i.01, i32* %arrayidx, align 4
78 %inc = add nsw i32 %i.01, 1
79 %cmp = icmp slt i32 %inc, 64
80 br i1 %cmp, label %for.body, label %for.end
81
82 for.end: ; preds = %for.body
83 ret void
84 }
85
86 define void @test4(i32* %a) {
87 ; CHECK-LABEL: @test4(
88 entry:
89 br label %for.body
90
91 for.body: ; preds = %entry, %for.body
92 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
93 %mul = mul nsw i32 %i.01, 64
94 %div = sdiv i32 %mul, 8
95 ; CHECK: udiv
96 ; CHECK-NOT: sdiv
97 %idxprom = sext i32 %div to i64
98 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
99 store i32 %i.01, i32* %arrayidx, align 4
100 %inc = add nsw i32 %i.01, 1
101 %cmp = icmp slt i32 %inc, 64
102 br i1 %cmp, label %for.body, label %for.end
103
104 for.end: ; preds = %for.body
105 ret void
106 }
107
108 define void @test5(i32* %a) {
109 ; CHECK-LABEL: @test5(
110 entry:
111 br label %for.body
112
113 for.body: ; preds = %entry, %for.body
114 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
115 %mul = mul nsw i32 %i.01, 64
116 %div = sdiv i32 %mul, 6
117 ; CHECK: udiv
118 ; CHECK-NOT: sdiv
119 %idxprom = sext i32 %div to i64
120 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
121 store i32 %i.01, i32* %arrayidx, align 4
122 %inc = add nsw i32 %i.01, 1
123 %cmp = icmp slt i32 %inc, 64
124 br i1 %cmp, label %for.body, label %for.end
125
126 for.end: ; preds = %for.body
127 ret void
128 }
129