Initialize.
[oota-llvm.git] / lib / Transforms / IPO / StructRetPromotion.cpp
index 3ea9a04adb59430cbf6a69e9b9edaa010fb10012..686ffdad6be39208a8780c76d0d7bd9649c96144 100644 (file)
@@ -48,6 +48,7 @@ namespace {
     bool isSafeToUpdateAllCallers(Function *F);
     Function *cloneFunctionBody(Function *F, const StructType *STy);
     void updateCallSites(Function *F, Function *NF);
+    bool nestedStructType(const StructType *STy);
   };
 
   char SRETPromotion::ID = 0;
@@ -73,8 +74,7 @@ bool SRETPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
 bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
   Function *F = CGN->getFunction();
 
-  // Make sure that it is local to this module.
-  if (!F || !F->hasInternalLinkage())
+  if (!F || F->isDeclaration())
     return false;
 
   // Make sure that function returns struct.
@@ -89,12 +89,16 @@ bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
     dyn_cast<StructType>(FArgType->getElementType());
   assert (STy && "Invalid sret parameter element type");
 
+  if (nestedStructType(STy))
+    return false;
+
   // Check if it is ok to perform this promotion.
   if (isSafeToUpdateAllCallers(F) == false) {
     NumRejectedSRETUses++;
     return false;
   }
 
+  NumSRET++;
   // [1] Replace use of sret parameter 
   AllocaInst *TheAlloca = new AllocaInst (STy, NULL, "mrv", 
                                           F->getEntryBlock().begin());
@@ -167,6 +171,10 @@ bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
       // If FirstArg user is a GEP whose all users are not LoadInst then
       // this function F is not suitable for sret promotion.
       else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(ArgI)) {
+        // TODO : Use dom info and insert PHINodes to collect get results
+        // from multiple call sites for this GEP.
+        if (GEP->getParent() != Call->getParent())
+          return false;
         for (Value::use_iterator GEPI = GEP->use_begin(), GEPE = GEP->use_end();
              GEPI != GEPE; ++GEPI) 
           if (!isa<LoadInst>(GEPI))
@@ -205,9 +213,14 @@ Function *SRETPromotion::cloneFunctionBody(Function *F,
   unsigned ParamIndex = 1; // 0th parameter attribute is reserved for return type.
   while (I != E) {
     Params.push_back(I->getType());
-    if (ParameterAttributes attrs = PAL ? PAL->getParamAttrs(ParamIndex) : 
-        ParamAttr::None)
-      ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Params.size(), attrs));
+    ParameterAttributes Attrs = ParamAttr::None;
+    if (PAL) {
+      Attrs = PAL->getParamAttrs(ParamIndex);
+      if (ParamIndex == 1) // Skip sret attribute
+        Attrs = Attrs ^ ParamAttr::StructRet;
+    }
+    if (Attrs != ParamAttr::None)
+      ParamAttrsVec.push_back(ParamAttrsWithIndex::get(ParamIndex, Attrs));
     ++I;
     ++ParamIndex;
   }
@@ -240,7 +253,7 @@ void SRETPromotion::updateCallSites(Function *F, Function *NF) {
   SmallVector<Value*, 16> Args;
 
   // ParamAttrs - Keep track of the parameter attributes for the arguments.
-  ParamAttrsVector ParamAttrsVec;
+  ParamAttrsVector ArgAttrsVec;
 
   for (Value::use_iterator FUI = F->use_begin(), FUE = F->use_end(); FUI != FUE;) {
     CallSite CS = CallSite::get(*FUI);
@@ -250,7 +263,7 @@ void SRETPromotion::updateCallSites(Function *F, Function *NF) {
     const ParamAttrsList *PAL = F->getParamAttrs();
     // Add any return attributes.
     if (ParameterAttributes attrs = PAL ? PAL->getParamAttrs(0) : ParamAttr::None)
-      ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
+      ArgAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
 
     // Copy arguments, however skip first one.
     CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
@@ -259,9 +272,14 @@ void SRETPromotion::updateCallSites(Function *F, Function *NF) {
     unsigned ParamIndex = 1; // 0th parameter attribute is reserved for return type.
     while (AI != AE) {
       Args.push_back(*AI); 
-      if (ParameterAttributes Attrs = PAL ? PAL->getParamAttrs(ParamIndex) :
-          ParamAttr::None)
-        ParamAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
+      ParameterAttributes Attrs = ParamAttr::None;
+      if (PAL) {
+        Attrs = PAL->getParamAttrs(ParamIndex);
+        if (ParamIndex == 1) // Skip sret attribute
+          Attrs = Attrs ^ ParamAttr::StructRet;
+      }
+      if (Attrs != ParamAttr::None)
+        ArgAttrsVec.push_back(ParamAttrsWithIndex::get(Args.size(), Attrs));
       ++ParamIndex;
       ++AI;
     }
@@ -272,16 +290,16 @@ void SRETPromotion::updateCallSites(Function *F, Function *NF) {
       New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
                            Args.begin(), Args.end(), "", Call);
       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
-      cast<InvokeInst>(New)->setParamAttrs(ParamAttrsList::get(ParamAttrsVec));
+      cast<InvokeInst>(New)->setParamAttrs(ParamAttrsList::get(ArgAttrsVec));
     } else {
       New = new CallInst(NF, Args.begin(), Args.end(), "", Call);
       cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
-      cast<CallInst>(New)->setParamAttrs(ParamAttrsList::get(ParamAttrsVec));
+      cast<CallInst>(New)->setParamAttrs(ParamAttrsList::get(ArgAttrsVec));
       if (cast<CallInst>(Call)->isTailCall())
         cast<CallInst>(New)->setTailCall();
     }
     Args.clear();
-    ParamAttrsVec.clear();
+    ArgAttrsVec.clear();
     New->takeName(Call);
 
     // Update all users of sret parameter to extract value using getresult.
@@ -292,7 +310,9 @@ void SRETPromotion::updateCallSites(Function *F, Function *NF) {
       if (C2 && (C2 == Call))
         continue;
       else if (GetElementPtrInst *UGEP = dyn_cast<GetElementPtrInst>(U2)) {
-        Value *GR = new GetResultInst(New, 5, "xxx", UGEP);
+        ConstantInt *Idx = dyn_cast<ConstantInt>(UGEP->getOperand(2));
+        assert (Idx && "Unexpected getelementptr index!");
+        Value *GR = new GetResultInst(New, Idx->getZExtValue(), "gr", UGEP);
         for (Value::use_iterator GI = UGEP->use_begin(),
                GE = UGEP->use_end(); GI != GE; ++GI) {
           if (LoadInst *L = dyn_cast<LoadInst>(*GI)) {
@@ -307,3 +327,15 @@ void SRETPromotion::updateCallSites(Function *F, Function *NF) {
     Call->eraseFromParent();
   }
 }
+
+/// nestedStructType - Return true if STy includes any
+/// other aggregate types
+bool SRETPromotion::nestedStructType(const StructType *STy) {
+  unsigned Num = STy->getNumElements();
+  for (unsigned i = 0; i < Num; i++) {
+    const Type *Ty = STy->getElementType(i);
+    if (!Ty->isFirstClassType() && Ty != Type::VoidTy)
+      return true;
+  }
+  return false;
+}