[optnone] Make the optnone attribute effective at suppressing function

attribute and function argument attribute synthesizing and propagating.

As with the other uses of this attribute, the goal remains a best-effort
(no guarantees) attempt to not optimize the function or assume things
about the function when optimizing. This is particularly useful for
compiler testing, bisecting miscompiles, triaging things, etc. I was
hitting specific issues using optnone to isolate test code from a test
driver for my fuzz testing, and this is one step of fixing that.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215538 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chandler Carruth 2014-08-13 10:49:33 +00:00
parent 8b77c00bbf
commit 701073e58e
2 changed files with 37 additions and 7 deletions

View File

@ -161,8 +161,9 @@ bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
Function *F = (*I)->getFunction();
if (!F)
// External node - may write memory. Just give up.
if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
// External node or node we don't want to optimize - assume it may write
// memory and give up.
return false;
AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
@ -527,7 +528,8 @@ bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
// looking up whether a given CallGraphNode is in this SCC.
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
Function *F = (*I)->getFunction();
if (F && !F->isDeclaration() && !F->mayBeOverridden())
if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
!F->hasFnAttribute(Attribute::OptimizeNone))
SCCNodes.insert(F);
}
@ -541,8 +543,9 @@ bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
Function *F = (*I)->getFunction();
if (!F)
// External node - only a problem for arguments that we pass to it.
if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
// External node or function we're trying not to optimize - only a problem
// for arguments that we pass to it.
continue;
// Definitions with weak linkage may be overridden at linktime with
@ -794,8 +797,8 @@ bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
Function *F = (*I)->getFunction();
if (!F)
// External node - skip it;
if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
// External node or node we don't want to optimize - skip it;
return false;
// Already noalias.
@ -834,6 +837,9 @@ bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
/// given function and set any applicable attributes. Returns true
/// if any attributes were set and false otherwise.
bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
if (F.hasFnAttribute(Attribute::OptimizeNone))
return false;
FunctionType *FTy = F.getFunctionType();
LibFunc::Func TheLibFunc;
if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))

View File

@ -0,0 +1,24 @@
; RUN: opt < %s -functionattrs -S | FileCheck %s
@x = global i32 0
define void @test_opt(i8* %p) {
; CHECK-LABEL: @test_opt
; CHECK: (i8* nocapture readnone %p) #0 {
ret void
}
define void @test_optnone(i8* %p) noinline optnone {
; CHECK-LABEL: @test_optnone
; CHECK: (i8* %p) #1 {
ret void
}
declare i8 @strlen(i8*) noinline optnone
; CHECK-LABEL: @strlen
; CHECK: (i8*) #1
; CHECK-LABEL: attributes #0
; CHECK: = { readnone }
; CHECK-LABEL: attributes #1
; CHECK: = { noinline optnone }