Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 9dd9f4f

Browse files
committed
[ConstantFold] Fix defect in constant folding computation for GEP
When the array indexes are all determined by GVN to be constants, a call is made to constant-folding to optimize/simplify the address computation. The constant-folding, however, makes a mistake in that it sometimes reads back stale Idxs instead of NewIdxs, that it re-computed in previous iteration. This leads to incorrect addresses coming out of constant-folding to GEP. A test case is included. The error is only triggered when indexes have particular patterns that the stale/new index updates interplay matters. Reviewers: Daniel Berlin Differential Revision: https://reviews.llvm.org/D30642 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297317 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c036eeb commit 9dd9f4f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/IR/ConstantFold.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,8 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
22312231
ConstantInt *Factor = ConstantInt::get(CI->getType(), NumElements);
22322232
NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
22332233

2234-
Constant *PrevIdx = cast<Constant>(Idxs[i - 1]);
2234+
Constant *PrevIdx = NewIdxs[i-1] ? NewIdxs[i-1] :
2235+
cast<Constant>(Idxs[i - 1]);
22352236
Constant *Div = ConstantExpr::getSDiv(CI, Factor);
22362237

22372238
unsigned CommonExtendedWidth =
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; RUN: opt -gvn -S -o - %s | FileCheck %s
2+
; RUN: opt -newgvn -S -o - %s | FileCheck %s
3+
; Test that the constantfolding getelementptr computation results in
4+
; j[5][4][1] (j+239)
5+
; and not [1][4][4][1] (#449) which is an incorrect out-of-range error
6+
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
7+
target triple = "armv7-none-eabi"
8+
9+
@f = local_unnamed_addr global i32 2, align 4
10+
@t6 = local_unnamed_addr global i32 1, align 4
11+
@j = local_unnamed_addr global [6 x [6 x [7 x i8]]] [[6 x [7 x i8]] [[7 x i8] c"\06\00\00\00\00\00\00", [7 x i8] zeroinitializer, [7 x i8] zeroinitializer, [7 x i8] zeroinitializer, [7 x i8] zeroinitializer, [7 x i8] zeroinitializer], [6 x [7 x i8]] zeroinitializer, [6 x [7 x i8]] zeroinitializer, [6 x [7 x i8]] zeroinitializer, [6 x [7 x i8]] zeroinitializer, [6 x [7 x i8]] zeroinitializer], align 1
12+
@p = internal global i64 0, align 8
13+
@y = local_unnamed_addr global i64* @p, align 4
14+
@b = internal unnamed_addr global i32 0, align 4
15+
@h = common local_unnamed_addr global i16 0, align 2
16+
@a = common local_unnamed_addr global i32 0, align 4
17+
@k = common local_unnamed_addr global i32 0, align 4
18+
@t11 = common local_unnamed_addr global i32 0, align 4
19+
20+
; Function Attrs: nounwind
21+
define i32 @main() local_unnamed_addr {
22+
entry:
23+
%0 = load i32, i32* @t6, align 4
24+
%inc = add nsw i32 %0, 1
25+
store i32 %inc, i32* @t6, align 4
26+
store i16 4, i16* @h, align 2
27+
%1 = load i32, i32* @a, align 4
28+
%conv = trunc i32 %1 to i8
29+
store i32 1, i32* @f, align 4
30+
%2 = load i64, i64* @p, align 8
31+
%cmp4 = icmp slt i64 %2, 2
32+
%conv6 = zext i1 %cmp4 to i8
33+
%3 = load i16, i16* @h, align 2
34+
%conv7 = sext i16 %3 to i32
35+
%add = add nsw i32 %conv7, 1
36+
%f.promoted = load i32, i32* @f, align 4
37+
%4 = mul i32 %conv7, 7
38+
%5 = add i32 %4, 5
39+
%6 = sub i32 -1, %f.promoted
40+
%7 = icmp sgt i32 %6, -2
41+
%smax = select i1 %7, i32 %6, i32 -2
42+
%8 = sub i32 6, %smax
43+
%scevgep = getelementptr [6 x [6 x [7 x i8]]], [6 x [6 x [7 x i8]]]* @j, i32 0, i32 0, i32 %5, i32 %8
44+
%9 = add i32 %f.promoted, %smax
45+
%10 = add i32 %9, 2
46+
call void @llvm.memset.p0i8.i32(i8* %scevgep, i8 %conv6, i32 %10, i32 1, i1 false)
47+
; CHECK: call void @llvm.memset.p0i8.i32(i8* getelementptr inbounds ([6 x [6 x [7 x i8]]], [6 x [6 x [7 x i8]]]* @j, i32 0, i64 5, i64 4, i32 1), i8 %conv6, i32 1, i32 1, i1 false)
48+
; CHECK-NOT: call void @llvm.memset.p0i8.i32(i8* getelementptr ([6 x [6 x [7 x i8]]], [6 x [6 x [7 x i8]]]* @j, i64 1, i64 4, i64 4, i32 1)
49+
ret i32 0
50+
}
51+
; Function Attrs: argmemonly nounwind
52+
declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i32, i1)

0 commit comments

Comments
 (0)