Remove any 'nest' parameter attributes if the function
authorDuncan Sands <baldrick@free.fr>
Sat, 16 Feb 2008 20:56:04 +0000 (20:56 +0000)
committerDuncan Sands <baldrick@free.fr>
Sat, 16 Feb 2008 20:56:04 +0000 (20:56 +0000)
is not passed as an argument to a trampoline intrinsic.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47220 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/GlobalOpt.cpp
test/Transforms/GlobalOpt/2008-02-16-NestAttr.ll [new file with mode: 0644]

index 7bed9e22d11e83b5e1e1d0b1ee73f8bbbb1d34fb..fd71dfb826148bcd968f8fda23dfdb86ff5f63ac 100644 (file)
@@ -21,6 +21,7 @@
 #include "llvm/Instructions.h"
 #include "llvm/IntrinsicInst.h"
 #include "llvm/Module.h"
+#include "llvm/ParameterAttributes.h"
 #include "llvm/Pass.h"
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Target/TargetData.h"
@@ -46,6 +47,7 @@ STATISTIC(NumLocalized , "Number of globals localized");
 STATISTIC(NumShrunkToBool  , "Number of global vars shrunk to booleans");
 STATISTIC(NumFastCallFns   , "Number of functions converted to fastcc");
 STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
+STATISTIC(NumNestRemoved   , "Number of nest attributes removed");
 
 namespace {
   struct VISIBILITY_HIDDEN GlobalOpt : public ModulePass {
@@ -1590,6 +1592,35 @@ static void ChangeCalleesToFastCall(Function *F) {
   }
 }
 
+static const ParamAttrsList *StripNest(const ParamAttrsList *Attrs) {
+  if (Attrs) {
+    for (unsigned i = 0, e = Attrs->size(); i != e; ++i) {
+      uint16_t A = Attrs->getParamAttrsAtIndex(i);
+      if (A & ParamAttr::Nest) {
+        Attrs = ParamAttrsList::excludeAttrs(Attrs, Attrs->getParamIndex(i),
+                                             ParamAttr::Nest);
+        // There can be only one.
+        break;
+      }
+    }
+  }
+
+  return Attrs;
+}
+
+static void RemoveNestAttribute(Function *F) {
+  F->setParamAttrs(StripNest(F->getParamAttrs()));
+  for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){
+    Instruction *User = cast<Instruction>(*UI);
+    if (CallInst *CI = dyn_cast<CallInst>(User)) {
+      CI->setParamAttrs(StripNest(CI->getParamAttrs()));
+    } else {
+      InvokeInst *II = cast<InvokeInst>(User);
+      II->setParamAttrs(StripNest(II->getParamAttrs()));
+    }
+  }
+}
+
 bool GlobalOpt::OptimizeFunctions(Module &M) {
   bool Changed = false;
   // Optimize functions.
@@ -1601,16 +1632,27 @@ bool GlobalOpt::OptimizeFunctions(Module &M) {
       M.getFunctionList().erase(F);
       Changed = true;
       ++NumFnDeleted;
-    } else if (F->hasInternalLinkage() &&
-               F->getCallingConv() == CallingConv::C &&  !F->isVarArg() &&
-               OnlyCalledDirectly(F)) {
-      // If this function has C calling conventions, is not a varargs
-      // function, and is only called directly, promote it to use the Fast
-      // calling convention.
-      F->setCallingConv(CallingConv::Fast);
-      ChangeCalleesToFastCall(F);
-      ++NumFastCallFns;
-      Changed = true;
+    } else if (F->hasInternalLinkage()) {
+      if (F->getCallingConv() == CallingConv::C && !F->isVarArg() &&
+          OnlyCalledDirectly(F)) {
+        // If this function has C calling conventions, is not a varargs
+        // function, and is only called directly, promote it to use the Fast
+        // calling convention.
+        F->setCallingConv(CallingConv::Fast);
+        ChangeCalleesToFastCall(F);
+        ++NumFastCallFns;
+        Changed = true;
+      }
+
+      if (F->getParamAttrs() &&
+          F->getParamAttrs()->hasAttrSomewhere(ParamAttr::Nest) &&
+          OnlyCalledDirectly(F)) {
+        // The function is not used by a trampoline intrinsic, so it is safe
+        // to remove the 'nest' attribute.
+        RemoveNestAttribute(F);
+        ++NumNestRemoved;
+        Changed = true;
+      }
     }
   }
   return Changed;
diff --git a/test/Transforms/GlobalOpt/2008-02-16-NestAttr.ll b/test/Transforms/GlobalOpt/2008-02-16-NestAttr.ll
new file mode 100644 (file)
index 0000000..59996c5
--- /dev/null
@@ -0,0 +1,57 @@
+; RUN: llvm-as < %s | opt -globalopt | llvm-dis | grep { nest } | count 1
+       %struct.FRAME.nest = type { i32, i32 (i32)* }
+       %struct.__builtin_trampoline = type { [10 x i8] }
+@.str = internal constant [7 x i8] c"%d %d\0A\00"              ; <[7 x i8]*> [#uses=1]
+
+define i32 @process(i32 (i32)* %func) nounwind  {
+entry:
+       %tmp2 = tail call i32 %func( i32 1 ) nounwind           ; <i32> [#uses=1]
+       ret i32 %tmp2
+}
+
+define internal fastcc i32 @g.1478(%struct.FRAME.nest* nest  %CHAIN.1, i32 %m) nounwind  {
+entry:
+       %tmp3 = getelementptr %struct.FRAME.nest* %CHAIN.1, i32 0, i32 0                ; <i32*> [#uses=1]
+       %tmp4 = load i32* %tmp3, align 4                ; <i32> [#uses=1]
+       %tmp7 = icmp eq i32 %tmp4, %m           ; <i1> [#uses=1]
+       %tmp78 = zext i1 %tmp7 to i32           ; <i32> [#uses=1]
+       ret i32 %tmp78
+}
+
+define internal i32 @f.1481(%struct.FRAME.nest* nest  %CHAIN.2, i32 %m) nounwind  {
+entry:
+       %tmp4 = tail call fastcc i32 @g.1478( %struct.FRAME.nest* nest  %CHAIN.2, i32 %m ) nounwind             ; <i32> [#uses=1]
+       %tmp6 = getelementptr %struct.FRAME.nest* %CHAIN.2, i32 0, i32 0                ; <i32*> [#uses=1]
+       %tmp7 = load i32* %tmp6, align 4                ; <i32> [#uses=1]
+       %tmp9 = icmp eq i32 %tmp4, %tmp7                ; <i1> [#uses=1]
+       %tmp910 = zext i1 %tmp9 to i32          ; <i32> [#uses=1]
+       ret i32 %tmp910
+}
+
+define i32 @nest(i32 %n) nounwind  {
+entry:
+       %TRAMP.316 = alloca [10 x i8]           ; <[10 x i8]*> [#uses=1]
+       %FRAME.0 = alloca %struct.FRAME.nest            ; <%struct.FRAME.nest*> [#uses=3]
+       %TRAMP.316.sub = getelementptr [10 x i8]* %TRAMP.316, i32 0, i32 0              ; <i8*> [#uses=1]
+       %tmp3 = getelementptr %struct.FRAME.nest* %FRAME.0, i32 0, i32 0                ; <i32*> [#uses=1]
+       store i32 %n, i32* %tmp3, align 8
+       %FRAME.06 = bitcast %struct.FRAME.nest* %FRAME.0 to i8*         ; <i8*> [#uses=1]
+       %tramp = call i8* @llvm.init.trampoline( i8* %TRAMP.316.sub, i8* bitcast (i32 (%struct.FRAME.nest*, i32)* @f.1481 to i8*), i8* %FRAME.06 )              ; <i8*> [#uses=1]
+       %tmp7 = getelementptr %struct.FRAME.nest* %FRAME.0, i32 0, i32 1                ; <i32 (i32)**> [#uses=1]
+       %tmp89 = bitcast i8* %tramp to i32 (i32)*               ; <i32 (i32)*> [#uses=2]
+       store i32 (i32)* %tmp89, i32 (i32)** %tmp7, align 4
+       %tmp13 = call i32 @process( i32 (i32)* %tmp89 ) nounwind                ; <i32> [#uses=1]
+       ret i32 %tmp13
+}
+
+declare i8* @llvm.init.trampoline(i8*, i8*, i8*) nounwind 
+
+define i32 @main() nounwind  {
+entry:
+       %tmp = tail call i32 @nest( i32 2 ) nounwind            ; <i32> [#uses=1]
+       %tmp1 = tail call i32 @nest( i32 1 ) nounwind           ; <i32> [#uses=1]
+       %tmp3 = tail call i32 (i8*, ...)* @printf( i8* noalias  getelementptr ([7 x i8]* @.str, i32 0, i32 0), i32 %tmp1, i32 %tmp ) nounwind           ; <i32> [#uses=0]
+       ret i32 undef
+}
+
+declare i32 @printf(i8*, ...) nounwind