Skip to content

Commit 0e6d484

Browse files
author
Mark Seaborn
committed
PNaCl: Add NoAlias attributes in ExpandByVal and ExpandVarArgs passes
This could help prevent these expansion passes from inhibiting optimisations than run after the expansion. e.g. It gives the optimiser more freedom to move around reads from the varargs buffer because they will not alias writes to other locations. BUG=https://code.google.com/p/nativeclient/issues/detail?id=3400 TEST=PNaCl toolchain trybots + GCC torture tests + LLVM test suite + Spec2k Review URL: https://codereview.chromium.org/14060026
1 parent dda2662 commit 0e6d484

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

lib/Transforms/NaCl/ExpandByVal.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ AttributeSet RemoveAttrs(LLVMContext &Context, AttributeSet Attrs) {
8282
Attr->getKindAsEnum() != Attribute::StructRet) {
8383
AB.addAttribute(*Attr);
8484
}
85+
// IR semantics require that ByVal implies NoAlias. However, IR
86+
// semantics do not require StructRet to imply NoAlias. For
87+
// example, a global variable address can be passed as a
88+
// StructRet argument, although Clang does not do so and Clang
89+
// explicitly adds NoAlias to StructRet arguments.
90+
if (Attr->getKindAsEnum() == Attribute::ByVal) {
91+
AB.addAttribute(Attribute::get(Context, Attribute::NoAlias));
92+
}
8593
}
8694
AttrList.push_back(AttributeSet::get(Context, Index, AB));
8795
}

lib/Transforms/NaCl/ExpandVarArgs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ static void ExpandVarArgFunc(Function *Func) {
8787
NewFunc->getBasicBlockList().splice(NewFunc->begin(),
8888
Func->getBasicBlockList());
8989

90+
// Declare the new argument as "noalias".
91+
NewFunc->setAttributes(
92+
Func->getAttributes().addAttribute(
93+
Func->getContext(), FTy->getNumParams() + 1, Attribute::NoAlias));
94+
9095
// Move the arguments across to the new function.
9196
for (Function::arg_iterator Arg = Func->arg_begin(), E = Func->arg_end(),
9297
NewArg = NewFunc->arg_begin();

test/Transforms/NaCl/expand-byval.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ define void @byval_receiver(%MyStruct* byval align 32 %ptr) {
1515
ret void
1616
}
1717
; Strip the "byval" and "align" attributes.
18-
; CHECK: define void @byval_receiver(%MyStruct* %ptr) {
18+
; CHECK: define void @byval_receiver(%MyStruct* noalias %ptr) {
1919
; CHECK-NEXT: call void @ext_func(%MyStruct* %ptr)
2020

2121

2222
declare void @ext_byval_func(%MyStruct* byval)
23-
; CHECK: declare void @ext_byval_func(%MyStruct*)
23+
; CHECK: declare void @ext_byval_func(%MyStruct* noalias)
2424

2525
define void @byval_caller(%MyStruct* %ptr) {
2626
call void @ext_byval_func(%MyStruct* byval %ptr)
@@ -30,15 +30,15 @@ define void @byval_caller(%MyStruct* %ptr) {
3030
; CHECK-NEXT: %ptr.byval_copy = alloca %MyStruct, align 4
3131
; CHECK: call void @llvm.lifetime.start(i64 12, i8* %{{.*}})
3232
; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %{{.*}}, i8* %{{.*}}, i64 12, i32 0, i1 false)
33-
; CHECK-NEXT: call void @ext_byval_func(%MyStruct* %ptr.byval_copy)
33+
; CHECK-NEXT: call void @ext_byval_func(%MyStruct* noalias %ptr.byval_copy)
3434

3535

3636
define void @byval_tail_caller(%MyStruct* %ptr) {
3737
tail call void @ext_byval_func(%MyStruct* byval %ptr)
3838
ret void
3939
}
4040
; CHECK: define void @byval_tail_caller(%MyStruct* %ptr) {
41-
; CHECK: {{^}} call void @ext_byval_func(%MyStruct* %ptr.byval_copy)
41+
; CHECK: {{^}} call void @ext_byval_func(%MyStruct* noalias %ptr.byval_copy)
4242

4343

4444
define void @byval_invoke(%MyStruct* %ptr) {
@@ -53,7 +53,7 @@ lpad:
5353
; CHECK: define void @byval_invoke(%MyStruct* %ptr) {
5454
; CHECK: %ptr.byval_copy = alloca %MyStruct, align 32
5555
; CHECK: call void @llvm.lifetime.start(i64 12, i8* %{{.*}})
56-
; CHECK: invoke void @ext_byval_func(%MyStruct* %ptr.byval_copy)
56+
; CHECK: invoke void @ext_byval_func(%MyStruct* noalias %ptr.byval_copy)
5757
; CHECK: cont:
5858
; CHECK: call void @llvm.lifetime.end(i64 12, i8* %{{.*}})
5959
; CHECK: lpad:
@@ -64,7 +64,7 @@ lpad:
6464

6565
; Check that "align" is stripped for declarations too.
6666
declare void @ext_byval_func_align(%MyStruct* byval align 32)
67-
; CHECK: declare void @ext_byval_func_align(%MyStruct*)
67+
; CHECK: declare void @ext_byval_func_align(%MyStruct* noalias)
6868

6969
define void @byval_caller_align_via_attr(%MyStruct* %ptr) {
7070
call void @ext_byval_func(%MyStruct* byval align 32 %ptr)

test/Transforms/NaCl/expand-varargs-attrs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare i32 @varargs_func(i32 %arg, ...)
1010
define void @func_with_arg_attrs(%MyStruct* byval, ...) {
1111
ret void
1212
}
13-
; CHECK: define void @func_with_arg_attrs(%MyStruct* byval, i8* %varargs) {
13+
; CHECK: define void @func_with_arg_attrs(%MyStruct* byval, i8* noalias %varargs) {
1414

1515

1616
declare void @take_struct_arg(%MyStruct* byval %s, ...)

test/Transforms/NaCl/expand-varargs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ define i32 @varargs_func(i32 %arg, ...) {
1717
call void @llvm.va_end(i8* %arglist)
1818
ret i32 %result
1919
}
20-
; CHECK: define i32 @varargs_func(i32 %arg, i8* %varargs) {
20+
; CHECK: define i32 @varargs_func(i32 %arg, i8* noalias %varargs) {
2121
; CHECK-NEXT: %arglist_alloc = alloca i8*
2222
; CHECK-NEXT: %arglist = bitcast i8** %arglist_alloc to i8*
2323
; CHECK-NEXT: %arglist1 = bitcast i8* %arglist to i8**

0 commit comments

Comments
 (0)