llvm.org GIT mirror llvm / db1e513
Revert r219432 - "Revert "[BasicAA] Revert "Revert r218714 - Make better use of zext and sign information.""" Let's try this again... This reverts r219432, plus a bug fix. Description of the bug in r219432 (by Nick): The bug was using AllPositive to break out of the loop; if the loop break condition i != e is changed to i != e && AllPositive then the test_modulo_analysis_with_global test I've added will fail as the Modulo will be calculated incorrectly (as the last loop iteration is skipped, so Modulo isn't updated with its Scale). Nick also adds this comment: ComputeSignBit is safe to use in loops as it takes into account phi nodes, and the == EK_ZeroEx check is safe in loops as, no matter how the variable changes between iterations, zero-extensions will always guarantee a zero sign bit. The isValueEqualInPotentialCycles check is therefore definitely not needed as all the variable analysis holds no matter how the variables change between loop iterations. And this patch also adds another enhancement to GetLinearExpression - basically to convert ConstantInts to Offsets (see test_const_eval and test_const_eval_scaled for the situations this improves). Original commit message: This reverts r218944, which reverted r218714, plus a bug fix. Description of the bug in r218714 (by Nick): The original patch forgot to check if the Scale in VariableGEPIndex flipped the sign of the variable. The BasicAA pass iterates over the instructions in the order they appear in the function, and so BasicAliasAnalysis::aliasGEP is called with the variable it first comes across as parameter GEP1. Adding a %reorder label puts the definition of %a after %b so aliasGEP is called with %b as the first parameter and %a as the second. aliasGEP later calculates that %a == %b + 1 - %idxprom where %idxprom >= 0 (if %a was passed as the first parameter it would calculate %b == %a - 1 + %idxprom where %idxprom >= 0) - ignoring that %idxprom is scaled by -1 here lead the patch to incorrectly conclude that %a > %b. Revised patch by Nick White, thanks! Thanks to Lang to isolating the bug. Slightly modified by me to add an early exit from the loop and avoid unnecessary, but expensive, function calls. Original commit message: Two related things: 1. Fixes a bug when calculating the offset in GetLinearExpression. The code previously used zext to extend the offset, so negative offsets were converted to large positive ones. 2. Enhance aliasGEP to deduce that, if the difference between two GEP allocations is positive and all the variables that govern the offset are also positive (i.e. the offset is strictly after the higher base pointer), then locations that fit in the gap between the two base pointers are NoAlias. Patch by Nick White! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221876 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 5 years ago
3 changed file(s) with 265 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
206206 return V;
207207 }
208208
209 if (ConstantInt *Const = dyn_cast(V)) {
210 // if it's a constant, just convert it to an offset
211 // and remove the variable.
212 Offset += Const->getValue();
213 assert(Scale == 0 && "Constant values don't have a scale");
214 return V;
215 }
216
209217 if (BinaryOperator *BOp = dyn_cast(V)) {
210218 if (ConstantInt *RHSC = dyn_cast(BOp->getOperand(1))) {
211219 switch (BOp->getOpcode()) {
253261 Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension,
254262 DL, Depth+1, AT, DT);
255263 Scale = Scale.zext(OldWidth);
256 Offset = Offset.zext(OldWidth);
264
265 // We have to sign-extend even if Extension == EK_ZeroExt as we can't
266 // decompose a sign extension (i.e. zext(x - 1) != zext(x) - zext(-1)).
267 Offset = Offset.sext(OldWidth);
257268
258269 return Result;
259270 }
10501061 }
10511062 }
10521063
1053 // Try to distinguish something like &A[i][1] against &A[42][0].
1054 // Grab the least significant bit set in any of the scales.
10551064 if (!GEP1VariableIndices.empty()) {
10561065 uint64_t Modulo = 0;
1057 for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i)
1058 Modulo |= (uint64_t)GEP1VariableIndices[i].Scale;
1066 bool AllPositive = true;
1067 for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i) {
1068
1069 // Try to distinguish something like &A[i][1] against &A[42][0].
1070 // Grab the least significant bit set in any of the scales. We
1071 // don't need std::abs here (even if the scale's negative) as we'll
1072 // be ^'ing Modulo with itself later.
1073 Modulo |= (uint64_t) GEP1VariableIndices[i].Scale;
1074
1075 if (AllPositive) {
1076 // If the Value could change between cycles, then any reasoning about
1077 // the Value this cycle may not hold in the next cycle. We'll just
1078 // give up if we can't determine conditions that hold for every cycle:
1079 const Value *V = GEP1VariableIndices[i].V;
1080
1081 bool SignKnownZero, SignKnownOne;
1082 ComputeSignBit(
1083 const_cast(V),
1084 SignKnownZero, SignKnownOne,
1085 DL, 0, AT, nullptr, DT);
1086
1087 // Zero-extension widens the variable, and so forces the sign
1088 // bit to zero.
1089 bool IsZExt = GEP1VariableIndices[i].Extension == EK_ZeroExt;
1090 SignKnownZero |= IsZExt;
1091 SignKnownOne &= !IsZExt;
1092
1093 // If the variable begins with a zero then we know it's
1094 // positive, regardless of whether the value is signed or
1095 // unsigned.
1096 int64_t Scale = GEP1VariableIndices[i].Scale;
1097 AllPositive =
1098 (SignKnownZero && Scale >= 0) ||
1099 (SignKnownOne && Scale < 0);
1100 }
1101 }
1102
10591103 Modulo = Modulo ^ (Modulo & (Modulo - 1));
10601104
10611105 // We can compute the difference between the two addresses
10641108 uint64_t ModOffset = (uint64_t)GEP1BaseOffset & (Modulo - 1);
10651109 if (V1Size != UnknownSize && V2Size != UnknownSize &&
10661110 ModOffset >= V2Size && V1Size <= Modulo - ModOffset)
1111 return NoAlias;
1112
1113 // If we know all the variables are positive, then GEP1 >= GEP1BasePtr.
1114 // If GEP1BasePtr > V2 (GEP1BaseOffset > 0) then we know the pointers
1115 // don't alias if V2Size can fit in the gap between V2 and GEP1BasePtr.
1116 if (AllPositive && GEP1BaseOffset > 0 && V2Size <= (uint64_t) GEP1BaseOffset)
10671117 return NoAlias;
10681118 }
10691119
3838
3939 ; CHECK-LABEL: pr18068
4040 ; CHECK: MayAlias: i32* %0, i32* %arrayidx5
41 ; CHECK: NoAlias: i32* %arrayidx13, i32* %arrayidx5
4142
4243 define i32 @pr18068(i32* %jj7, i32* %j) {
4344 entry:
0 ; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
1 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-S128"
2 target triple = "x86_64-unknown-linux-gnu"
3
4 ; CHECK-LABEL: test_with_zext
5 ; CHECK: NoAlias: i8* %a, i8* %b
6
7 define void @test_with_zext() {
8 %1 = tail call i8* @malloc(i64 120)
9 %a = getelementptr inbounds i8* %1, i64 8
10 %2 = getelementptr inbounds i8* %1, i64 16
11 %3 = zext i32 3 to i64
12 %b = getelementptr inbounds i8* %2, i64 %3
13 ret void
14 }
15
16 ; CHECK-LABEL: test_with_lshr
17 ; CHECK: NoAlias: i8* %a, i8* %b
18
19 define void @test_with_lshr(i64 %i) {
20 %1 = tail call i8* @malloc(i64 120)
21 %a = getelementptr inbounds i8* %1, i64 8
22 %2 = getelementptr inbounds i8* %1, i64 16
23 %3 = lshr i64 %i, 2
24 %b = getelementptr inbounds i8* %2, i64 %3
25 ret void
26 }
27
28 ; CHECK-LABEL: test_with_a_loop
29 ; CHECK: NoAlias: i8* %a, i8* %b
30
31 define void @test_with_a_loop(i8* %mem) {
32 br label %for.loop
33
34 for.loop:
35 %i = phi i32 [ 0, %0 ], [ %i.plus1, %for.loop ]
36 %a = getelementptr inbounds i8* %mem, i64 8
37 %a.plus1 = getelementptr inbounds i8* %mem, i64 16
38 %i.64 = zext i32 %i to i64
39 %b = getelementptr inbounds i8* %a.plus1, i64 %i.64
40 %i.plus1 = add nuw nsw i32 %i, 1
41 %cmp = icmp eq i32 %i.plus1, 10
42 br i1 %cmp, label %for.loop.exit, label %for.loop
43
44 for.loop.exit:
45 ret void
46 }
47
48 ; CHECK-LABEL: test_with_varying_base_pointer_in_loop
49 ; CHECK: NoAlias: i8* %a, i8* %b
50
51 define void @test_with_varying_base_pointer_in_loop(i8* %mem.orig) {
52 br label %for.loop
53
54 for.loop:
55 %mem = phi i8* [ %mem.orig, %0 ], [ %mem.plus1, %for.loop ]
56 %i = phi i32 [ 0, %0 ], [ %i.plus1, %for.loop ]
57 %a = getelementptr inbounds i8* %mem, i64 8
58 %a.plus1 = getelementptr inbounds i8* %mem, i64 16
59 %i.64 = zext i32 %i to i64
60 %b = getelementptr inbounds i8* %a.plus1, i64 %i.64
61 %i.plus1 = add nuw nsw i32 %i, 1
62 %mem.plus1 = getelementptr inbounds i8* %mem, i64 8
63 %cmp = icmp eq i32 %i.plus1, 10
64 br i1 %cmp, label %for.loop.exit, label %for.loop
65
66 for.loop.exit:
67 ret void
68 }
69
70 ; CHECK-LABEL: test_sign_extension
71 ; CHECK: PartialAlias: i64* %b.i64, i8* %a
72
73 define void @test_sign_extension(i32 %p) {
74 %1 = tail call i8* @malloc(i64 120)
75 %p.64 = zext i32 %p to i64
76 %a = getelementptr inbounds i8* %1, i64 %p.64
77 %p.minus1 = add i32 %p, -1
78 %p.minus1.64 = zext i32 %p.minus1 to i64
79 %b.i8 = getelementptr inbounds i8* %1, i64 %p.minus1.64
80 %b.i64 = bitcast i8* %b.i8 to i64*
81 ret void
82 }
83
84 ; CHECK-LABEL: test_fe_tools
85 ; CHECK: PartialAlias: i32* %a, i32* %b
86
87 define void @test_fe_tools([8 x i32]* %values) {
88 br label %reorder
89
90 for.loop:
91 %i = phi i32 [ 0, %reorder ], [ %i.next, %for.loop ]
92 %idxprom = zext i32 %i to i64
93 %b = getelementptr inbounds [8 x i32]* %values, i64 0, i64 %idxprom
94 %i.next = add nuw nsw i32 %i, 1
95 %1 = icmp eq i32 %i.next, 10
96 br i1 %1, label %for.loop.exit, label %for.loop
97
98 reorder:
99 %a = getelementptr inbounds [8 x i32]* %values, i64 0, i64 1
100 br label %for.loop
101
102 for.loop.exit:
103 ret void
104 }
105
106 @b = global i32 0, align 4
107 @d = global i32 0, align 4
108
109 ; CHECK-LABEL: test_spec2006
110 ; CHECK: PartialAlias: i32** %x, i32** %y
111
112 define void @test_spec2006() {
113 %h = alloca [1 x [2 x i32*]], align 16
114 %d.val = load i32* @d, align 4
115 %d.promoted = sext i32 %d.val to i64
116 %1 = icmp slt i32 %d.val, 2
117 br i1 %1, label %.lr.ph, label %3
118
119 .lr.ph: ; preds = %0
120 br label %2
121
122 ;
123 %i = phi i32 [ %d.val, %.lr.ph ], [ %i.plus1, %2 ]
124 %i.promoted = sext i32 %i to i64
125 %x = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 %d.promoted, i64 %i.promoted
126 %i.plus1 = add nsw i32 %i, 1
127 %cmp = icmp slt i32 %i.plus1, 2
128 br i1 %cmp, label %2, label %3
129
130 ;
131 %y = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 0, i64 1
132 ret void
133 }
134
135 ; CHECK-LABEL: test_modulo_analysis_easy_case
136 ; CHECK: NoAlias: i32** %x, i32** %y
137
138 define void @test_modulo_analysis_easy_case(i64 %i) {
139 %h = alloca [1 x [2 x i32*]], align 16
140 %x = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 %i, i64 0
141 %y = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 0, i64 1
142 ret void
143 }
144
145 ; CHECK-LABEL: test_modulo_analysis_in_loop
146 ; CHECK: NoAlias: i32** %x, i32** %y
147
148 define void @test_modulo_analysis_in_loop() {
149 %h = alloca [1 x [2 x i32*]], align 16
150 br label %for.loop
151
152 for.loop:
153 %i = phi i32 [ 0, %0 ], [ %i.plus1, %for.loop ]
154 %i.promoted = sext i32 %i to i64
155 %x = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 %i.promoted, i64 0
156 %y = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 0, i64 1
157 %i.plus1 = add nsw i32 %i, 1
158 %cmp = icmp slt i32 %i.plus1, 2
159 br i1 %cmp, label %for.loop, label %for.loop.exit
160
161 for.loop.exit:
162 ret void
163 }
164
165 ; CHECK-LABEL: test_modulo_analysis_with_global
166 ; CHECK: PartialAlias: i32** %x, i32** %y
167
168 define void @test_modulo_analysis_with_global() {
169 %h = alloca [1 x [2 x i32*]], align 16
170 %b = load i32* @b, align 4
171 %b.promoted = sext i32 %b to i64
172 br label %for.loop
173
174 for.loop:
175 %i = phi i32 [ 0, %0 ], [ %i.plus1, %for.loop ]
176 %i.promoted = sext i32 %i to i64
177 %x = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 %i.promoted, i64 %b.promoted
178 %y = getelementptr inbounds [1 x [2 x i32*]]* %h, i64 0, i64 0, i64 1
179 %i.plus1 = add nsw i32 %i, 1
180 %cmp = icmp slt i32 %i.plus1, 2
181 br i1 %cmp, label %for.loop, label %for.loop.exit
182
183 for.loop.exit:
184 ret void
185 }
186
187 ; CHECK-LABEL: test_const_eval
188 ; CHECK: NoAlias: i8* %a, i8* %b
189 define void @test_const_eval(i8* %ptr, i64 %offset) {
190 %a = getelementptr inbounds i8* %ptr, i64 %offset
191 %a.dup = getelementptr inbounds i8* %ptr, i64 %offset
192 %three = zext i32 3 to i64
193 %b = getelementptr inbounds i8* %a.dup, i64 %three
194 ret void
195 }
196
197 ; CHECK-LABEL: test_const_eval_scaled
198 ; CHECK: MustAlias: i8* %a, i8* %b
199 define void @test_const_eval_scaled(i8* %ptr) {
200 %three = zext i32 3 to i64
201 %six = mul i64 %three, 2
202 %a = getelementptr inbounds i8* %ptr, i64 %six
203 %b = getelementptr inbounds i8* %ptr, i64 6
204 ret void
205 }
206
207 ; Function Attrs: nounwind
208 declare noalias i8* @malloc(i64)