llvm.org GIT mirror llvm / 7358595
Revert r275678, "Revert "Revert r275027 - Let FuncAttrs infer the 'returned' argument attribute"" This reverts also r275029, "Update Clang tests after adding inference for the returned argument attribute" It broke LTO build. Seems miscompilation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275756 91177308-0d34-0410-b5e6-96231b3b80d8 NAKAMURA Takumi 4 years ago
4 changed file(s) with 6 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
4141 STATISTIC(NumReadNone, "Number of functions marked readnone");
4242 STATISTIC(NumReadOnly, "Number of functions marked readonly");
4343 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
44 STATISTIC(NumReturned, "Number of arguments marked returned");
4544 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
4645 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
4746 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
481480 }
482481
483482 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
484 }
485
486 /// Deduce returned attributes for the SCC.
487 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
488 bool Changed = false;
489
490 AttrBuilder B;
491 B.addAttribute(Attribute::Returned);
492
493 // Check each function in turn, determining if an argument is always returned.
494 for (Function *F : SCCNodes) {
495 // We can infer and propagate function attributes only when we know that the
496 // definition we'll get at link time is *exactly* the definition we see now.
497 // For more details, see GlobalValue::mayBeDerefined.
498 if (!F->hasExactDefinition())
499 continue;
500
501 if (F->getReturnType()->isVoidTy())
502 continue;
503
504 auto FindRetArg = [&]() -> Value * {
505 Value *RetArg = nullptr;
506 for (BasicBlock &BB : *F)
507 if (auto *Ret = dyn_cast(BB.getTerminator())) {
508 // Note that stripPointerCasts should look through functions with
509 // returned arguments.
510 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
511 if (RetVal->getType() == F->getReturnType() && isa(RetVal)) {
512 if (!RetArg)
513 RetArg = RetVal;
514 else if (RetArg != RetVal)
515 return nullptr;
516 }
517 }
518
519 return RetArg;
520 };
521
522 if (Value *RetArg = FindRetArg()) {
523 auto *A = cast(RetArg);
524 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
525 ++NumReturned;
526 Changed = true;
527 }
528 }
529
530 return Changed;
531483 }
532484
533485 /// Deduce nocapture attributes for the SCC.
10711023 }
10721024
10731025 bool Changed = false;
1074 Changed |= addArgumentReturnedAttrs(SCCNodes);
10751026 Changed |= addReadAttrs(SCCNodes, AARGetter);
10761027 Changed |= addArgumentAttrs(SCCNodes);
10771028
11371088 SCCNodes.insert(F);
11381089 }
11391090
1140 Changed |= addArgumentReturnedAttrs(SCCNodes);
11411091 Changed |= addReadAttrs(SCCNodes, AARGetter);
11421092 Changed |= addArgumentAttrs(SCCNodes);
11431093
1313 ret i32* %tmp
1414 }
1515
16 ; CHECK: define i32* @c(i32* readnone returned %r)
16 ; CHECK: define i32* @c(i32* readnone %r)
1717 @g = global i32 0
1818 define i32* @c(i32 *%r) {
1919 %a = icmp eq i32* %r, null
0 ; RUN: opt < %s -functionattrs -S | FileCheck %s
11 @g = global i32* null ; [#uses=1]
22
3 ; CHECK: define i32* @c1(i32* readnone returned %q)
3 ; CHECK: define i32* @c1(i32* readnone %q)
44 define i32* @c1(i32* %q) {
55 ret i32* %q
66 }
139139 ret void
140140 }
141141
142 ; CHECK: define i8* @test1_2(i8* nocapture readnone %x1_2, i8* returned %y1_2)
142 ; CHECK: define i8* @test1_2(i8* nocapture readnone %x1_2, i8* %y1_2)
143143 define i8* @test1_2(i8* %x1_2, i8* %y1_2) {
144144 call void @test1_1(i8* %x1_2, i8* %y1_2)
145145 store i32* null, i32** @g
167167 ret void
168168 }
169169
170 ; CHECK: define i8* @test4_2(i8* nocapture readnone %x4_2, i8* readnone returned %y4_2, i8* nocapture readnone %z4_2)
170 ; CHECK: define i8* @test4_2(i8* nocapture readnone %x4_2, i8* readnone %y4_2, i8* nocapture readnone %z4_2)
171171 define i8* @test4_2(i8* %x4_2, i8* %y4_2, i8* %z4_2) {
172172 call void @test4_1(i8* null)
173173 store i32* null, i32** @g
1010 ret void
1111 }
1212
13 ; CHECK: define i8* @test2(i8* readnone returned %p)
13 ; CHECK: define i8* @test2(i8* readnone %p)
1414 define i8* @test2(i8* %p) {
1515 store i32 0, i32* @x
1616 ret i8* %p
5252 ret void
5353 }
5454
55 ; CHECK: define i32* @test8_1(i32* readnone returned %p)
55 ; CHECK: define i32* @test8_1(i32* readnone %p)
5656 define i32* @test8_1(i32* %p) {
5757 entry:
5858 ret i32* %p