llvm.org GIT mirror llvm / 71f1967
[BasicAA] Revert r221876 because it can produce incorrect aliasing information: see PR24468. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245394 91177308-0d34-0410-b5e6-96231b3b80d8 Quentin Colombet 4 years ago
3 changed file(s) with 4 addition(s) and 261 deletion(s). Raw diff Collapse all Expand all
189189 return V;
190190 }
191191
192 if (ConstantInt *Const = dyn_cast(V)) {
193 // if it's a constant, just convert it to an offset
194 // and remove the variable.
195 Offset += Const->getValue();
196 assert(Scale == 0 && "Constant values don't have a scale");
197 return V;
198 }
199
200192 if (BinaryOperator *BOp = dyn_cast(V)) {
201193 if (ConstantInt *RHSC = dyn_cast(BOp->getOperand(1))) {
202194 switch (BOp->getOpcode()) {
245237 Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension, DL,
246238 Depth + 1, AC, DT);
247239 Scale = Scale.zext(OldWidth);
248
249 // We have to sign-extend even if Extension == EK_ZeroExt as we can't
250 // decompose a sign extension (i.e. zext(x - 1) != zext(x) - zext(-1)).
251 Offset = Offset.sext(OldWidth);
240 Offset = Offset.zext(OldWidth);
252241
253242 return Result;
254243 }
958947 }
959948 }
960949
950 // Try to distinguish something like &A[i][1] against &A[42][0].
951 // Grab the least significant bit set in any of the scales.
961952 if (!GEP1VariableIndices.empty()) {
962953 uint64_t Modulo = 0;
963 bool AllPositive = true;
964 for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i) {
965
966 // Try to distinguish something like &A[i][1] against &A[42][0].
967 // Grab the least significant bit set in any of the scales. We
968 // don't need std::abs here (even if the scale's negative) as we'll
969 // be ^'ing Modulo with itself later.
954 for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i)
970955 Modulo |= (uint64_t)GEP1VariableIndices[i].Scale;
971
972 if (AllPositive) {
973 // If the Value could change between cycles, then any reasoning about
974 // the Value this cycle may not hold in the next cycle. We'll just
975 // give up if we can't determine conditions that hold for every cycle:
976 const Value *V = GEP1VariableIndices[i].V;
977
978 bool SignKnownZero, SignKnownOne;
979 ComputeSignBit(const_cast(V), SignKnownZero, SignKnownOne, *DL,
980 0, AC1, nullptr, DT);
981
982 // Zero-extension widens the variable, and so forces the sign
983 // bit to zero.
984 bool IsZExt = GEP1VariableIndices[i].Extension == EK_ZeroExt;
985 SignKnownZero |= IsZExt;
986 SignKnownOne &= !IsZExt;
987
988 // If the variable begins with a zero then we know it's
989 // positive, regardless of whether the value is signed or
990 // unsigned.
991 int64_t Scale = GEP1VariableIndices[i].Scale;
992 AllPositive =
993 (SignKnownZero && Scale >= 0) || (SignKnownOne && Scale < 0);
994 }
995 }
996
997956 Modulo = Modulo ^ (Modulo & (Modulo - 1));
998957
999958 // We can compute the difference between the two addresses
1003962 if (V1Size != MemoryLocation::UnknownSize &&
1004963 V2Size != MemoryLocation::UnknownSize && ModOffset >= V2Size &&
1005964 V1Size <= Modulo - ModOffset)
1006 return NoAlias;
1007
1008 // If we know all the variables are positive, then GEP1 >= GEP1BasePtr.
1009 // If GEP1BasePtr > V2 (GEP1BaseOffset > 0) then we know the pointers
1010 // don't alias if V2Size can fit in the gap between V2 and GEP1BasePtr.
1011 if (AllPositive && GEP1BaseOffset > 0 && V2Size <= (uint64_t)GEP1BaseOffset)
1012965 return NoAlias;
1013966 }
1014967
3838
3939 ; CHECK-LABEL: pr18068
4040 ; CHECK: MayAlias: i32* %0, i32* %arrayidx5
41 ; CHECK: NoAlias: i32* %arrayidx13, i32* %arrayidx5
4241
4342 define i32 @pr18068(i32* %jj7, i32* %j) {
4443 entry:
+0
-209
test/Analysis/BasicAA/zext.ll less more
None ; 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, i8* %1, i64 8
10 %2 = getelementptr inbounds i8, i8* %1, i64 16
11 %3 = zext i32 3 to i64
12 %b = getelementptr inbounds i8, 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, i8* %1, i64 8
22 %2 = getelementptr inbounds i8, i8* %1, i64 16
23 %3 = lshr i64 %i, 2
24 %b = getelementptr inbounds i8, 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, i8* %mem, i64 8
37 %a.plus1 = getelementptr inbounds i8, i8* %mem, i64 16
38 %i.64 = zext i32 %i to i64
39 %b = getelementptr inbounds i8, 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, i8* %mem, i64 8
58 %a.plus1 = getelementptr inbounds i8, i8* %mem, i64 16
59 %i.64 = zext i32 %i to i64
60 %b = getelementptr inbounds i8, i8* %a.plus1, i64 %i.64
61 %i.plus1 = add nuw nsw i32 %i, 1
62 %mem.plus1 = getelementptr inbounds i8, 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, 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, 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], [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], [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, 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*]], [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*]], [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*]], [1 x [2 x i32*]]* %h, i64 0, i64 %i, i64 0
141 %y = getelementptr inbounds [1 x [2 x i32*]], [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*]], [1 x [2 x i32*]]* %h, i64 0, i64 %i.promoted, i64 0
156 %y = getelementptr inbounds [1 x [2 x i32*]], [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, 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*]], [1 x [2 x i32*]]* %h, i64 0, i64 %i.promoted, i64 %b.promoted
178 %y = getelementptr inbounds [1 x [2 x i32*]], [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, i8* %ptr, i64 %offset
191 %a.dup = getelementptr inbounds i8, i8* %ptr, i64 %offset
192 %three = zext i32 3 to i64
193 %b = getelementptr inbounds i8, 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, i8* %ptr, i64 %six
203 %b = getelementptr inbounds i8, i8* %ptr, i64 6
204 ret void
205 }
206
207 ; Function Attrs: nounwind
208 declare noalias i8* @malloc(i64)