ArgumentPromotion.cpp revision debcb01b0f0a15f568ca69e8f288fade4bfc7297
19e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner//===-- ArgumentPromotion.cpp - Promote by-reference arguments ------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//                     The LLVM Compiler Infrastructure
4ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//===----------------------------------------------------------------------===//
9ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//
10ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// This pass promotes "by reference" arguments to be "by value" arguments.  In
11ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// practice, this means looking for internal functions that have pointer
1255cbec317d9c30c8ae1d35eaa008ca63d1f2fce9Gordon Henriksen// arguments.  If it can prove, through the use of alias analysis, that an
1355cbec317d9c30c8ae1d35eaa008ca63d1f2fce9Gordon Henriksen// argument is *only* loaded, then it can pass the value into the function
14ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// instead of the address of the value.  This can cause recursive simplification
159e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner// of code and lead to the elimination of allocas (especially in C++ template
169e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner// code like the STL).
17ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//
189440db886627161a8413e823797569fc7b10beafChris Lattner// This pass also handles aggregate arguments that are passed into a function,
199440db886627161a8413e823797569fc7b10beafChris Lattner// scalarizing them if the elements of the aggregate are only loaded.  Note that
20477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman// by default it refuses to scalarize aggregates which would require passing in
21477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman// more than three operands to the function, because passing thousands of
224cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands// operands for a large array or structure is unprofitable! This limit can be
23477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman// configured or disabled, however.
249440db886627161a8413e823797569fc7b10beafChris Lattner//
25ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner// Note that this transformation could also be done for arguments that are only
2655cbec317d9c30c8ae1d35eaa008ca63d1f2fce9Gordon Henriksen// stored to (returning the value instead), but does not currently.  This case
2755cbec317d9c30c8ae1d35eaa008ca63d1f2fce9Gordon Henriksen// would be best handled when and if LLVM begins supporting multiple return
2855cbec317d9c30c8ae1d35eaa008ca63d1f2fce9Gordon Henriksen// values from functions.
29ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//
30ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner//===----------------------------------------------------------------------===//
31ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
329e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner#define DEBUG_TYPE "argpromotion"
33ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Transforms/IPO.h"
34ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Constants.h"
35ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/DerivedTypes.h"
36ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Module.h"
375eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner#include "llvm/CallGraphSCCPass.h"
38ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Instructions.h"
3914ce9ef2e9013ba56e1daafebd91fe3ee1e8647eOwen Anderson#include "llvm/LLVMContext.h"
40ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Analysis/AliasAnalysis.h"
415eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner#include "llvm/Analysis/CallGraph.h"
42ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Target/TargetData.h"
43ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Support/CallSite.h"
44ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/Compiler.h"
45ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include "llvm/Support/CFG.h"
46551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
47ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/raw_ostream.h"
48551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/DepthFirstIterator.h"
49551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
50551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/StringExtras.h"
51ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner#include <set>
52ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattnerusing namespace llvm;
53ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
5486453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumArgumentsPromoted , "Number of pointer arguments promoted");
5586453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumAggregatesPromoted, "Number of aggregate arguments promoted");
5610603e0c84d15f61443e8b17bc35f98cc46606d9Chris LattnerSTATISTIC(NumByValArgsPromoted , "Number of byval arguments promoted");
5786453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumArgumentsDead     , "Number of dead pointer args eliminated");
58ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
5986453c52ba02e743d29c08456e51006500041456Chris Lattnernamespace {
60ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass.
61ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  ///
629133fe28954d498fc4de13064c7d65bd811de02cReid Spencer  struct VISIBILITY_HIDDEN ArgPromotion : public CallGraphSCCPass {
63ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      AU.addRequired<AliasAnalysis>();
65ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      AU.addRequired<TargetData>();
665eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner      CallGraphSCCPass::getAnalysisUsage(AU);
67ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    }
68ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
695eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner    virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
70ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky    static char ID; // Pass identification, replacement for typeid
7138deef9ce58b33dba34515f23fb7dbde02164c77Dan Gohman    explicit ArgPromotion(unsigned maxElements = 3)
7238deef9ce58b33dba34515f23fb7dbde02164c77Dan Gohman      : CallGraphSCCPass(&ID), maxElements(maxElements) {}
734cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
74477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    /// A vector used to hold the indices of a single GEP instruction
75477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    typedef std::vector<uint64_t> IndicesVector;
76794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
77ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  private:
785eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner    bool PromoteArguments(CallGraphNode *CGN);
7940c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner    bool isSafeToPromoteArgument(Argument *Arg, bool isByVal) const;
804cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands    Function *DoPromotion(Function *F,
8110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner                          SmallPtrSet<Argument*, 8> &ArgsToPromote,
8210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner                          SmallPtrSet<Argument*, 8> &ByValArgsToTransform);
83992e97eed344a141ff581838cfdacb76a1e9559aMatthijs Kooijman    /// The maximum number of elements to expand, or 0 for unlimited.
84992e97eed344a141ff581838cfdacb76a1e9559aMatthijs Kooijman    unsigned maxElements;
85ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  };
86ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner}
87ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
88844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar ArgPromotion::ID = 0;
89844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic RegisterPass<ArgPromotion>
90844731a7f1909f55935e3514c9e713a62d67662eDan GohmanX("argpromotion", "Promote 'by reference' arguments to scalars");
91844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
92bcd203cf860269987f32b14737b200b84fc2b63eChris LattnerPass *llvm::createArgumentPromotionPass(unsigned maxElements) {
93bcd203cf860269987f32b14737b200b84fc2b63eChris Lattner  return new ArgPromotion(maxElements);
94ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner}
95ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
965eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattnerbool ArgPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
975eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner  bool Changed = false, LocalChange;
98ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
99f5afcabff887c2e9023f0c69c44f1de15b5c4347Chris Lattner  do {  // Iterate until we stop promoting from this SCC.
1005eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner    LocalChange = false;
1015eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner    // Attempt to promote arguments from all functions in this SCC.
1025eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner    for (unsigned i = 0, e = SCC.size(); i != e; ++i)
1035eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner      LocalChange |= PromoteArguments(SCC[i]);
1045eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner    Changed |= LocalChange;               // Remember that we changed something.
1055eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner  } while (LocalChange);
106fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
107ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  return Changed;
108ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner}
109ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
1109e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// PromoteArguments - This method checks the specified function to see if there
1119e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// are any promotable arguments and if it is safe to promote the function (for
1129e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// example, all callers are direct).  If safe to promote some arguments, it
1139e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// calls the DoPromotion method.
1149e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner///
1155eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattnerbool ArgPromotion::PromoteArguments(CallGraphNode *CGN) {
1165eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner  Function *F = CGN->getFunction();
1175eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner
1185eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner  // Make sure that it is local to this module.
119bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola  if (!F || !F->hasLocalLinkage()) return false;
120ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
121ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // First check: see if there are any pointer arguments!  If not, quick exit.
12240c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  SmallVector<std::pair<Argument*, unsigned>, 16> PointerArgs;
12340c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  unsigned ArgNo = 0;
12440c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
12540c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner       I != E; ++I, ++ArgNo)
126ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    if (isa<PointerType>(I->getType()))
12740c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner      PointerArgs.push_back(std::pair<Argument*, unsigned>(I, ArgNo));
128ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  if (PointerArgs.empty()) return false;
129ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
130ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Second check: make sure that all callers are direct callers.  We can't
131ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // transform functions that have indirect callers.
132757068f3bad425fb126fe16ab7b8a82a636e6bbdJay Foad  if (F->hasAddressTaken())
133757068f3bad425fb126fe16ab7b8a82a636e6bbdJay Foad    return false;
134ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
13540c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  // Check to see which arguments are promotable.  If an argument is promotable,
13640c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  // add it to ArgsToPromote.
13740c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  SmallPtrSet<Argument*, 8> ArgsToPromote;
13810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner  SmallPtrSet<Argument*, 8> ByValArgsToTransform;
13940c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  for (unsigned i = 0; i != PointerArgs.size(); ++i) {
1400598866c052147c31b808391f58434ce3dbfb838Devang Patel    bool isByVal = F->paramHasAttr(PointerArgs[i].second+1, Attribute::ByVal);
1414cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
14210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    // If this is a byval argument, and if the aggregate type is small, just
14310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    // pass the elements, which is always safe.
14410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    Argument *PtrArg = PointerArgs[i].first;
14510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    if (isByVal) {
14610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      const Type *AgTy = cast<PointerType>(PtrArg->getType())->getElementType();
14743e2a035309f4e353a8bd5547d10125414597e74Duncan Sands      if (const StructType *STy = dyn_cast<StructType>(AgTy)) {
148bcd203cf860269987f32b14737b200b84fc2b63eChris Lattner        if (maxElements > 0 && STy->getNumElements() > maxElements) {
149ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar          DEBUG(errs() << "argpromotion disable promoting argument '"
150ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar                << PtrArg->getName() << "' because it would require adding more"
151ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar                << " than " << maxElements << " arguments to the function.\n");
152bcd203cf860269987f32b14737b200b84fc2b63eChris Lattner        } else {
153399101a5990621b0357009ab1852cc00f410a6c6Dan Gohman          // If all the elements are single-value types, we can promote it.
15410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          bool AllSimple = true;
15510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
156399101a5990621b0357009ab1852cc00f410a6c6Dan Gohman            if (!STy->getElementType(i)->isSingleValueType()) {
15710603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner              AllSimple = false;
15810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner              break;
15910603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner            }
1604cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
16110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          // Safe to transform, don't even bother trying to "promote" it.
16210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          // Passing the elements as a scalar will allow scalarrepl to hack on
16310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          // the new alloca we introduce.
16410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          if (AllSimple) {
16510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner            ByValArgsToTransform.insert(PtrArg);
16610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner            continue;
16710603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          }
16810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        }
16943e2a035309f4e353a8bd5547d10125414597e74Duncan Sands      }
17010603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    }
1714cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
17210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    // Otherwise, see if we can promote the pointer to its value.
17310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    if (isSafeToPromoteArgument(PtrArg, isByVal))
17410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      ArgsToPromote.insert(PtrArg);
17540c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  }
1764cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
177ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // No promotable pointer arguments.
17810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner  if (ArgsToPromote.empty() && ByValArgsToTransform.empty()) return false;
179ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
18010603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner  Function *NewF = DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
1815eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner
182dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands  // Update the call graph to know that the function has been transformed.
1835eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner  getAnalysis<CallGraph>().changeFunction(F, NewF);
184ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  return true;
185ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner}
186ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
18711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// IsAlwaysValidPointer - Return true if the specified pointer is always legal
18811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// to load.
18911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattnerstatic bool IsAlwaysValidPointer(Value *V) {
19011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true;
19111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V))
19211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner    return IsAlwaysValidPointer(GEP->getOperand(0));
19311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
19411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner    if (CE->getOpcode() == Instruction::GetElementPtr)
19511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner      return IsAlwaysValidPointer(CE->getOperand(0));
19611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner
19711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  return false;
19811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner}
19911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner
20011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// AllCalleesPassInValidPointerForArgument - Return true if we can prove that
20111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner/// all callees pass in a valid pointer for the specified function argument.
20211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattnerstatic bool AllCalleesPassInValidPointerForArgument(Argument *Arg) {
20311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  Function *Callee = Arg->getParent();
20411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner
20593e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner  unsigned ArgNo = std::distance(Callee->arg_begin(),
20693e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner                                 Function::arg_iterator(Arg));
20711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner
20811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  // Look at all call sites of the function.  At this pointer we know we only
20911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  // have direct callees.
21011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  for (Value::use_iterator UI = Callee->use_begin(), E = Callee->use_end();
21111a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner       UI != E; ++UI) {
21211a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner    CallSite CS = CallSite::get(*UI);
21311a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner    assert(CS.getInstruction() && "Should only have direct calls!");
21411a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner
21511a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner    if (!IsAlwaysValidPointer(CS.getArgument(ArgNo)))
21611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner      return false;
21711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  }
21811a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  return true;
21911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner}
22011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner
221477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// Returns true if Prefix is a prefix of longer. That means, Longer has a size
222477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// that is greater than or equal to the size of prefix, and each of the
223477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// elements in Prefix is the same as the corresponding elements in Longer.
224477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman///
225477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// This means it also returns true when Prefix and Longer are equal!
226477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijmanstatic bool IsPrefix(const ArgPromotion::IndicesVector &Prefix,
227477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman                     const ArgPromotion::IndicesVector &Longer) {
228477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  if (Prefix.size() > Longer.size())
229477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    return false;
230477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  for (unsigned i = 0, e = Prefix.size(); i != e; ++i)
231477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    if (Prefix[i] != Longer[i])
232477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      return false;
233477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  return true;
234477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman}
235477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
236477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
237477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// Checks if Indices, or a prefix of Indices, is in Set.
238477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijmanstatic bool PrefixIn(const ArgPromotion::IndicesVector &Indices,
239477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman                     std::set<ArgPromotion::IndicesVector> &Set) {
240477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    std::set<ArgPromotion::IndicesVector>::iterator Low;
241477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    Low = Set.upper_bound(Indices);
242477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    if (Low != Set.begin())
243477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      Low--;
244477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // Low is now the last element smaller than or equal to Indices. This means
245477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // it points to a prefix of Indices (possibly Indices itself), if such
246477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // prefix exists.
247477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    //
248477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // This load is safe if any prefix of its operands is safe to load.
249477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    return Low != Set.end() && IsPrefix(*Low, Indices);
250477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman}
251477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
252477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// Mark the given indices (ToMark) as safe in the the given set of indices
253477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// (Safe). Marking safe usually means adding ToMark to Safe. However, if there
254477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// is already a prefix of Indices in Safe, Indices are implicitely marked safe
255477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// already. Furthermore, any indices that Indices is itself a prefix of, are
256477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman/// removed from Safe (since they are implicitely safe because of Indices now).
257477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijmanstatic void MarkIndicesSafe(const ArgPromotion::IndicesVector &ToMark,
258477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman                            std::set<ArgPromotion::IndicesVector> &Safe) {
259477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  std::set<ArgPromotion::IndicesVector>::iterator Low;
260477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  Low = Safe.upper_bound(ToMark);
261477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // Guard against the case where Safe is empty
262477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  if (Low != Safe.begin())
263477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    Low--;
264477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // Low is now the last element smaller than or equal to Indices. This
265477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // means it points to a prefix of Indices (possibly Indices itself), if
266477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // such prefix exists.
267477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  if (Low != Safe.end()) {
268477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    if (IsPrefix(*Low, ToMark))
269477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // If there is already a prefix of these indices (or exactly these
270477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // indices) marked a safe, don't bother adding these indices
271477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      return;
272477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
273477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // Increment Low, so we can use it as a "insert before" hint
2744cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands    ++Low;
275477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  }
2764cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands  // Insert
277477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  Low = Safe.insert(Low, ToMark);
278477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  ++Low;
279477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // If there we're a prefix of longer index list(s), remove those
280477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  std::set<ArgPromotion::IndicesVector>::iterator End = Safe.end();
281477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  while (Low != End && IsPrefix(ToMark, *Low)) {
282477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    std::set<ArgPromotion::IndicesVector>::iterator Remove = Low;
283477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    ++Low;
284477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    Safe.erase(Remove);
285477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  }
286477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman}
2879e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
2889e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// isSafeToPromoteArgument - As you might guess from the name of this method,
2899e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// it checks to see if it is both safe and useful to promote the argument.
2909e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// This method limits promotion of aggregates to only promote up to three
2919e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// elements of the aggregate in order to avoid exploding the number of
2929e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// arguments passed in.
29340c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattnerbool ArgPromotion::isSafeToPromoteArgument(Argument *Arg, bool isByVal) const {
294477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  typedef std::set<IndicesVector> GEPIndicesSet;
295477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
296477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // Quick exit for unused arguments
297477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  if (Arg->use_empty())
298477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    return true;
299477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
3009440db886627161a8413e823797569fc7b10beafChris Lattner  // We can only promote this argument if all of the uses are loads, or are GEP
3019440db886627161a8413e823797569fc7b10beafChris Lattner  // instructions (with constant indices) that are subsequently loaded.
302477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  //
303477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // Promoting the argument causes it to be loaded in the caller
304477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // unconditionally. This is only safe if we can prove that either the load
305477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // would have happened in the callee anyway (ie, there is a load in the entry
306477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // block) or the pointer passed in at every call site is guaranteed to be
307477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // valid.
308477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // In the former case, invalid loads can happen, but would have happened
309477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // anyway, in the latter case, invalid loads won't happen. This prevents us
310477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // from introducing an invalid load that wouldn't have happened in the
311477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // original code.
312477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  //
313477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // This set will contain all sets of indices that are loaded in the entry
314477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // block, and thus are safe to unconditionally load in the caller.
315477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  GEPIndicesSet SafeToUnconditionallyLoad;
316170b1816b2cd6f02a8d0d5f1e13d9bda2f247012Chris Lattner
317477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // This set contains all the sets of indices that we are planning to promote.
318477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // This makes it possible to limit the number of arguments added.
319477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  GEPIndicesSet ToPromote;
3204cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
321477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // If the pointer is always valid, any load with first index 0 is valid.
322477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  if(isByVal || AllCalleesPassInValidPointerForArgument(Arg))
323477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    SafeToUnconditionallyLoad.insert(IndicesVector(1, 0));
324477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
325477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // First, iterate the entry block and mark loads of (geps of) arguments as
326477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // safe.
32711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  BasicBlock *EntryBlock = Arg->getParent()->begin();
328477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // Declare this here so we can reuse it
329477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  IndicesVector Indices;
330477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  for (BasicBlock::iterator I = EntryBlock->begin(), E = EntryBlock->end();
331477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman       I != E; ++I)
332477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
333477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      Value *V = LI->getPointerOperand();
334477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
335477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        V = GEP->getPointerOperand();
336477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        if (V == Arg) {
337477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          // This load actually loads (part of) Arg? Check the indices then.
338477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          Indices.reserve(GEP->getNumIndices());
339477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          for (User::op_iterator II = GEP->idx_begin(), IE = GEP->idx_end();
340477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman               II != IE; ++II)
341477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            if (ConstantInt *CI = dyn_cast<ConstantInt>(*II))
342477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              Indices.push_back(CI->getSExtValue());
343477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            else
344477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              // We found a non-constant GEP index for this argument? Bail out
345477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              // right away, can't promote this argument at all.
346477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              return false;
347477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
348477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          // Indices checked out, mark them as safe
349477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          MarkIndicesSafe(Indices, SafeToUnconditionallyLoad);
350477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          Indices.clear();
351477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        }
352477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      } else if (V == Arg) {
353477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // Direct loads are equivalent to a GEP with a single 0 index.
354477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        MarkIndicesSafe(IndicesVector(1, 0), SafeToUnconditionallyLoad);
355477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      }
356477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    }
357477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
358477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // Now, iterate all uses of the argument to see if there are any uses that are
359477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // not (GEP+)loads, or any (GEP+)loads that are not safe to promote.
360a10145fdf6325c5c528689e8952ad7e4e2a1a6b5Chris Lattner  SmallVector<LoadInst*, 16> Loads;
361477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  IndicesVector Operands;
362ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  for (Value::use_iterator UI = Arg->use_begin(), E = Arg->use_end();
363477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman       UI != E; ++UI) {
364477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    Operands.clear();
365ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
366ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      if (LI->isVolatile()) return false;  // Don't hack volatile loads
367ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      Loads.push_back(LI);
368477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // Direct loads are equivalent to a GEP with a zero index and then a load.
369477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      Operands.push_back(0);
3709440db886627161a8413e823797569fc7b10beafChris Lattner    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
3719440db886627161a8413e823797569fc7b10beafChris Lattner      if (GEP->use_empty()) {
3729440db886627161a8413e823797569fc7b10beafChris Lattner        // Dead GEP's cause trouble later.  Just remove them if we run into
3739440db886627161a8413e823797569fc7b10beafChris Lattner        // them.
3749e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner        getAnalysis<AliasAnalysis>().deleteValue(GEP);
37540c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner        GEP->eraseFromParent();
376477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // TODO: This runs the above loop over and over again for dead GEPS
377477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // Couldn't we just do increment the UI iterator earlier and erase the
378477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // use?
37940c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner        return isSafeToPromoteArgument(Arg, isByVal);
3809440db886627161a8413e823797569fc7b10beafChris Lattner      }
381477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
3829440db886627161a8413e823797569fc7b10beafChris Lattner      // Ensure that all of the indices are constants.
383477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      for (User::op_iterator i = GEP->idx_begin(), e = GEP->idx_end();
384477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        i != e; ++i)
3855e46321d665d6b1f445aff70d8eabb4870a6cf0eGabor Greif        if (ConstantInt *C = dyn_cast<ConstantInt>(*i))
386477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          Operands.push_back(C->getSExtValue());
3879440db886627161a8413e823797569fc7b10beafChris Lattner        else
3889440db886627161a8413e823797569fc7b10beafChris Lattner          return false;  // Not a constant operand GEP!
3899440db886627161a8413e823797569fc7b10beafChris Lattner
3909440db886627161a8413e823797569fc7b10beafChris Lattner      // Ensure that the only users of the GEP are load instructions.
3919440db886627161a8413e823797569fc7b10beafChris Lattner      for (Value::use_iterator UI = GEP->use_begin(), E = GEP->use_end();
3929440db886627161a8413e823797569fc7b10beafChris Lattner           UI != E; ++UI)
3939440db886627161a8413e823797569fc7b10beafChris Lattner        if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
3949440db886627161a8413e823797569fc7b10beafChris Lattner          if (LI->isVolatile()) return false;  // Don't hack volatile loads
3959440db886627161a8413e823797569fc7b10beafChris Lattner          Loads.push_back(LI);
3969440db886627161a8413e823797569fc7b10beafChris Lattner        } else {
397477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          // Other uses than load?
3989440db886627161a8413e823797569fc7b10beafChris Lattner          return false;
3999440db886627161a8413e823797569fc7b10beafChris Lattner        }
4009440db886627161a8413e823797569fc7b10beafChris Lattner    } else {
4019440db886627161a8413e823797569fc7b10beafChris Lattner      return false;  // Not a load or a GEP.
4029440db886627161a8413e823797569fc7b10beafChris Lattner    }
4034cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
404477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // Now, see if it is safe to promote this load / loads of this GEP. Loading
405477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // is safe if Operands, or a prefix of Operands, is marked as safe.
406477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    if (!PrefixIn(Operands, SafeToUnconditionallyLoad))
407477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      return false;
408ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
409477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // See if we are already promoting a load with these indices. If not, check
410477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // to make sure that we aren't promoting too many elements.  If so, nothing
411477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // to do.
412477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    if (ToPromote.find(Operands) == ToPromote.end()) {
413477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      if (maxElements > 0 && ToPromote.size() == maxElements) {
414ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar        DEBUG(errs() << "argpromotion not promoting argument '"
415ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar              << Arg->getName() << "' because it would require adding more "
416ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar              << "than " << maxElements << " arguments to the function.\n");
417477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // We limit aggregate promotion to only promoting up to a fixed number
418477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // of elements of the aggregate.
419477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        return false;
420477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      }
421477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      ToPromote.insert(Operands);
422477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    }
423477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  }
424ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
425477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  if (Loads.empty()) return true;  // No users, this is a dead argument.
42611a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner
42711a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  // Okay, now we know that the argument is only used by load instructions and
428477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // it is safe to unconditionally perform all of them. Use alias analysis to
42911a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  // check to see if the pointer is guaranteed to not be modified from entry of
43011a3d7b7ddd10659b72ed248d878fa0d90ddcb45Chris Lattner  // the function to each of the load instructions.
431ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
432ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Because there could be several/many load instructions, remember which
433ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // blocks we know to be transparent to the load.
434e027efa21816dbd2350d137fdfa181c24cbe8c49Chris Lattner  SmallPtrSet<BasicBlock*, 16> TranspBlocks;
43546f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson
43646f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson  AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
4379440db886627161a8413e823797569fc7b10beafChris Lattner  TargetData &TD = getAnalysis<TargetData>();
438ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
439ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  for (unsigned i = 0, e = Loads.size(); i != e; ++i) {
440ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // Check to see if the load is invalidated from the start of the block to
441ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // the load itself.
442ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    LoadInst *Load = Loads[i];
443ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    BasicBlock *BB = Load->getParent();
4449440db886627161a8413e823797569fc7b10beafChris Lattner
4459440db886627161a8413e823797569fc7b10beafChris Lattner    const PointerType *LoadTy =
446477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      cast<PointerType>(Load->getPointerOperand()->getType());
447514ab348fddcdffa8367685dc608b2f8d5de986dDuncan Sands    unsigned LoadSize = (unsigned)TD.getTypeStoreSize(LoadTy->getElementType());
4489440db886627161a8413e823797569fc7b10beafChris Lattner
449ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    if (AA.canInstructionRangeModify(BB->front(), *Load, Arg, LoadSize))
450ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      return false;  // Pointer is invalidated!
451ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
452ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // Now check every path from the entry block to the load for transparency.
453ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // To do this, we perform a depth first search on the inverse CFG from the
454ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // loading block.
455ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
456e027efa21816dbd2350d137fdfa181c24cbe8c49Chris Lattner      for (idf_ext_iterator<BasicBlock*, SmallPtrSet<BasicBlock*, 16> >
457e027efa21816dbd2350d137fdfa181c24cbe8c49Chris Lattner             I = idf_ext_begin(*PI, TranspBlocks),
458ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner             E = idf_ext_end(*PI, TranspBlocks); I != E; ++I)
459ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner        if (AA.canBasicBlockModify(**I, Arg, LoadSize))
460ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner          return false;
461ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  }
462ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
463ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // If the path from the entry of the function to each load is free of
464ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // instructions that potentially invalidate the load, we can make the
465ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // transformation!
466ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  return true;
467ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner}
468ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
4699e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner/// DoPromotion - This method actually performs the promotion of the specified
4705eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner/// arguments, and returns the new function.  At this point, we know that it's
4715eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner/// safe to do so.
4725eb6f6c829ddfe353f94623aa1009c72be930497Chris LattnerFunction *ArgPromotion::DoPromotion(Function *F,
47310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner                                    SmallPtrSet<Argument*, 8> &ArgsToPromote,
47410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner                              SmallPtrSet<Argument*, 8> &ByValArgsToTransform) {
475fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
476ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Start by computing a new prototype for the function, which is the same as
477ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // the old function, but has modified arguments.
478ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  const FunctionType *FTy = F->getFunctionType();
479ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  std::vector<const Type*> Params;
480ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
481477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  typedef std::set<IndicesVector> ScalarizeTable;
482beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner
4839440db886627161a8413e823797569fc7b10beafChris Lattner  // ScalarizedElements - If we are promoting a pointer that has elements
4849440db886627161a8413e823797569fc7b10beafChris Lattner  // accessed out of it, keep track of which elements are accessed so that we
4859440db886627161a8413e823797569fc7b10beafChris Lattner  // can add one argument for each.
4869440db886627161a8413e823797569fc7b10beafChris Lattner  //
4879440db886627161a8413e823797569fc7b10beafChris Lattner  // Arguments that are directly loaded will have a zero element value here, to
4889440db886627161a8413e823797569fc7b10beafChris Lattner  // handle cases where there are both a direct load and GEP accesses.
4899440db886627161a8413e823797569fc7b10beafChris Lattner  //
490beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner  std::map<Argument*, ScalarizeTable> ScalarizedElements;
4919440db886627161a8413e823797569fc7b10beafChris Lattner
4929e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  // OriginalLoads - Keep track of a representative load instruction from the
4939e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  // original function so that we can tell the alias analysis implementation
4949e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  // what the new GEP/Load instructions we are inserting look like.
495477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  std::map<IndicesVector, LoadInst*> OriginalLoads;
4969e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
4970598866c052147c31b808391f58434ce3dbfb838Devang Patel  // Attributes - Keep track of the parameter attributes for the arguments
498dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands  // that we are *not* promoting. For the ones that we do promote, the parameter
499dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands  // attributes are lost
5000598866c052147c31b808391f58434ce3dbfb838Devang Patel  SmallVector<AttributeWithIndex, 8> AttributesVec;
5010598866c052147c31b808391f58434ce3dbfb838Devang Patel  const AttrListPtr &PAL = F->getAttributes();
502dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands
503532d022794beabceae09c7fcc222a6e4e929c748Duncan Sands  // Add any return attributes.
50419c874638d9478a5d5028854817a5ee72293bb2bDevang Patel  if (Attributes attrs = PAL.getRetAttributes())
5050598866c052147c31b808391f58434ce3dbfb838Devang Patel    AttributesVec.push_back(AttributeWithIndex::get(0, attrs));
5064cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
507477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman  // First, determine the new argument list
508ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner  unsigned ArgIndex = 1;
509dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
510ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner       ++I, ++ArgIndex) {
51110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    if (ByValArgsToTransform.count(I)) {
512477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // Simple byval argument? Just add all the struct element types.
51310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
51410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      const StructType *STy = cast<StructType>(AgTy);
51510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
51610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        Params.push_back(STy->getElementType(i));
51710603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      ++NumByValArgsPromoted;
51810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    } else if (!ArgsToPromote.count(I)) {
519477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // Unchanged argument
520ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      Params.push_back(I->getType());
52119c874638d9478a5d5028854817a5ee72293bb2bDevang Patel      if (Attributes attrs = PAL.getParamAttributes(ArgIndex))
5220598866c052147c31b808391f58434ce3dbfb838Devang Patel        AttributesVec.push_back(AttributeWithIndex::get(Params.size(), attrs));
5239e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner    } else if (I->use_empty()) {
524477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // Dead argument (which are always marked as promotable)
5259e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner      ++NumArgumentsDead;
5269e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner    } else {
527477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // Okay, this is being promoted. This means that the only uses are loads
528477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // or GEPs which are only used by loads
529477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
530477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // In this table, we will track which indices are loaded from the argument
531477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      // (where direct loads are tracked as no indices).
532beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner      ScalarizeTable &ArgIndices = ScalarizedElements[I];
5339440db886627161a8413e823797569fc7b10beafChris Lattner      for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
5349440db886627161a8413e823797569fc7b10beafChris Lattner           ++UI) {
5359440db886627161a8413e823797569fc7b10beafChris Lattner        Instruction *User = cast<Instruction>(*UI);
53646f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson        assert(isa<LoadInst>(User) || isa<GetElementPtrInst>(User));
537477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        IndicesVector Indices;
538477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        Indices.reserve(User->getNumOperands() - 1);
539477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // Since loads will only have a single operand, and GEPs only a single
540477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // non-index operand, this will record direct loads without any indices,
541477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // and gep+loads with the GEP indices.
542477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        for (User::op_iterator II = User->op_begin() + 1, IE = User->op_end();
543477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman             II != IE; ++II)
544477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          Indices.push_back(cast<ConstantInt>(*II)->getSExtValue());
545477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // GEPs with a single 0 index can be merged with direct loads
546477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        if (Indices.size() == 1 && Indices.front() == 0)
547477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          Indices.clear();
54846f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson        ArgIndices.insert(Indices);
54946f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson        LoadInst *OrigLoad;
55046f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson        if (LoadInst *L = dyn_cast<LoadInst>(User))
55146f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson          OrigLoad = L;
55246f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson        else
553477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          // Take any load, we will use it only to update Alias Analysis
55446f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson          OrigLoad = cast<LoadInst>(User->use_back());
55546f022a7f105211d5ea2c394e406d1943b80908cOwen Anderson        OriginalLoads[Indices] = OrigLoad;
5569440db886627161a8413e823797569fc7b10beafChris Lattner      }
5579440db886627161a8413e823797569fc7b10beafChris Lattner
5589440db886627161a8413e823797569fc7b10beafChris Lattner      // Add a parameter to the function for each element passed in.
559beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner      for (ScalarizeTable::iterator SI = ArgIndices.begin(),
560477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman             E = ArgIndices.end(); SI != E; ++SI) {
561b079a391c8b85d088dabce715d99be5917af88faTorok Edwin        // not allowed to dereference ->begin() if size() is 0
5621ccd185cb49d81465a2901622e58ceae046d1d83Chris Lattner        Params.push_back(GetElementPtrInst::getIndexedType(I->getType(),
5635b7dfbd89a0a0b7b59603bf9eb0f3f58534b4dd5David Greene                                                           SI->begin(),
5645b7dfbd89a0a0b7b59603bf9eb0f3f58534b4dd5David Greene                                                           SI->end()));
565477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        assert(Params.back());
566477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman      }
5679440db886627161a8413e823797569fc7b10beafChris Lattner
5689440db886627161a8413e823797569fc7b10beafChris Lattner      if (ArgIndices.size() == 1 && ArgIndices.begin()->empty())
5699440db886627161a8413e823797569fc7b10beafChris Lattner        ++NumArgumentsPromoted;
5709440db886627161a8413e823797569fc7b10beafChris Lattner      else
5719440db886627161a8413e823797569fc7b10beafChris Lattner        ++NumAggregatesPromoted;
572ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    }
57310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner  }
574ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
57519c874638d9478a5d5028854817a5ee72293bb2bDevang Patel  // Add any function attributes.
57619c874638d9478a5d5028854817a5ee72293bb2bDevang Patel  if (Attributes attrs = PAL.getFnAttributes())
57719c874638d9478a5d5028854817a5ee72293bb2bDevang Patel    AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
57819c874638d9478a5d5028854817a5ee72293bb2bDevang Patel
579ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  const Type *RetTy = FTy->getReturnType();
580e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson  LLVMContext &Context = RetTy->getContext();
581ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
582ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
583ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // have zero fixed arguments.
584ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  bool ExtraArgHack = false;
585ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  if (Params.empty() && FTy->isVarArg()) {
586ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    ExtraArgHack = true;
587c5b206b6be61d0d933b98b6af5e22f42edd48ad1Reid Spencer    Params.push_back(Type::Int32Ty);
588ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  }
589dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands
590dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands  // Construct the new function type using the new arguments.
591debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson  FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
592fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
593dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands  // Create the new function body and insert it into the module...
594051a950000e21935165db56695e35bade668193bGabor Greif  Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
59528c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands  NF->copyAttributesFrom(F);
596ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner
597ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner  // Recompute the parameter attributes list based on the new arguments for
598ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner  // the function.
5990598866c052147c31b808391f58434ce3dbfb838Devang Patel  NF->setAttributes(AttrListPtr::get(AttributesVec.begin(), AttributesVec.end()));
6000598866c052147c31b808391f58434ce3dbfb838Devang Patel  AttributesVec.clear();
60128c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands
602ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  F->getParent()->getFunctionList().insert(F, NF);
6032b3407f5b3d8626b924c532bcfea15daaf1bd3c0Zhou Sheng  NF->takeName(F);
6049e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
6059e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  // Get the alias analysis information that we need to update to reflect our
6069e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  // changes.
6079e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
6089e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
60934c8847b2d27433ec7b81c824b66771e7665873aDuncan Sands  // Get the callgraph information that we need to update to reflect our
61034c8847b2d27433ec7b81c824b66771e7665873aDuncan Sands  // changes.
61134c8847b2d27433ec7b81c824b66771e7665873aDuncan Sands  CallGraph &CG = getAnalysis<CallGraph>();
61234c8847b2d27433ec7b81c824b66771e7665873aDuncan Sands
613ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Loop over all of the callers of the function, transforming the call sites
614ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // to pass in the loaded pointers.
615ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  //
616ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner  SmallVector<Value*, 16> Args;
617ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  while (!F->use_empty()) {
618ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    CallSite CS = CallSite::get(F->use_back());
619ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    Instruction *Call = CS.getInstruction();
6200598866c052147c31b808391f58434ce3dbfb838Devang Patel    const AttrListPtr &CallPAL = CS.getAttributes();
6214cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
622532d022794beabceae09c7fcc222a6e4e929c748Duncan Sands    // Add any return attributes.
62319c874638d9478a5d5028854817a5ee72293bb2bDevang Patel    if (Attributes attrs = CallPAL.getRetAttributes())
6240598866c052147c31b808391f58434ce3dbfb838Devang Patel      AttributesVec.push_back(AttributeWithIndex::get(0, attrs));
625532d022794beabceae09c7fcc222a6e4e929c748Duncan Sands
6269e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner    // Loop over the operands, inserting GEP and loads in the caller as
6279e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner    // appropriate.
628ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    CallSite::arg_iterator AI = CS.arg_begin();
629ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner    ArgIndex = 1;
630f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner    for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
631ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner         I != E; ++I, ++AI, ++ArgIndex)
63210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
633ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner        Args.push_back(*AI);          // Unmodified argument
6344cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
63519c874638d9478a5d5028854817a5ee72293bb2bDevang Patel        if (Attributes Attrs = CallPAL.getParamAttributes(ArgIndex))
6360598866c052147c31b808391f58434ce3dbfb838Devang Patel          AttributesVec.push_back(AttributeWithIndex::get(Args.size(), Attrs));
6374cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
63810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      } else if (ByValArgsToTransform.count(I)) {
63910603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        // Emit a GEP and load for each element of the struct.
64010603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
64110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        const StructType *STy = cast<StructType>(AgTy);
642eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson        Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
64310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
644eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson          Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
645051a950000e21935165db56695e35bade668193bGabor Greif          Value *Idx = GetElementPtrInst::Create(*AI, Idxs, Idxs+2,
646051a950000e21935165db56695e35bade668193bGabor Greif                                                 (*AI)->getName()+"."+utostr(i),
647051a950000e21935165db56695e35bade668193bGabor Greif                                                 Call);
64810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          // TODO: Tell AA about the new values?
64910603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          Args.push_back(new LoadInst(Idx, Idx->getName()+".val", Call));
6504cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands        }
65110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      } else if (!I->use_empty()) {
6529e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner        // Non-dead argument: insert GEPs and loads as appropriate.
653beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner        ScalarizeTable &ArgIndices = ScalarizedElements[I];
654477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // Store the Value* version of the indices in here, but declare it now
655477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // for reuse
656477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        std::vector<Value*> Ops;
657beabf45a6923b1748edae40d53e2b2c4362cc32fChris Lattner        for (ScalarizeTable::iterator SI = ArgIndices.begin(),
6589440db886627161a8413e823797569fc7b10beafChris Lattner               E = ArgIndices.end(); SI != E; ++SI) {
6599440db886627161a8413e823797569fc7b10beafChris Lattner          Value *V = *AI;
6609e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner          LoadInst *OrigLoad = OriginalLoads[*SI];
6619e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner          if (!SI->empty()) {
662477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            Ops.reserve(SI->size());
663477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            const Type *ElTy = V->getType();
6644cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands            for (IndicesVector::const_iterator II = SI->begin(),
665477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman                 IE = SI->end(); II != IE; ++II) {
666477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              // Use i32 to index structs, and i64 for others (pointers/arrays).
667477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              // This satisfies GEP constraints.
668477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              const Type *IdxTy = (isa<StructType>(ElTy) ? Type::Int32Ty : Type::Int64Ty);
669eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson              Ops.push_back(ConstantInt::get(IdxTy, *II));
670477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              // Keep track of the type we're currently indexing
671477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman              ElTy = cast<CompositeType>(ElTy)->getTypeAtIndex(*II);
672477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            }
673477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            // And create a GEP to extract those indices
674477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            V = GetElementPtrInst::Create(V, Ops.begin(), Ops.end(),
675051a950000e21935165db56695e35bade668193bGabor Greif                                          V->getName()+".idx", Call);
676477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            Ops.clear();
6779e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner            AA.copyValue(OrigLoad->getOperand(0), V);
6789e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner          }
6799440db886627161a8413e823797569fc7b10beafChris Lattner          Args.push_back(new LoadInst(V, V->getName()+".val", Call));
6809e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner          AA.copyValue(OrigLoad, Args.back());
6819440db886627161a8413e823797569fc7b10beafChris Lattner        }
682ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      }
683ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
684ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    if (ExtraArgHack)
685e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson      Args.push_back(Context.getNullValue(Type::Int32Ty));
686ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
687ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // Push any varargs arguments on the list
688ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner    for (; AI != CS.arg_end(); ++AI, ++ArgIndex) {
689ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      Args.push_back(*AI);
69019c874638d9478a5d5028854817a5ee72293bb2bDevang Patel      if (Attributes Attrs = CallPAL.getParamAttributes(ArgIndex))
6910598866c052147c31b808391f58434ce3dbfb838Devang Patel        AttributesVec.push_back(AttributeWithIndex::get(Args.size(), Attrs));
692ab04e13a1f017c2b0a82344b4c083d92139ee2ccChris Lattner    }
693ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
69419c874638d9478a5d5028854817a5ee72293bb2bDevang Patel    // Add any function attributes.
69519c874638d9478a5d5028854817a5ee72293bb2bDevang Patel    if (Attributes attrs = CallPAL.getFnAttributes())
69619c874638d9478a5d5028854817a5ee72293bb2bDevang Patel      AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
69719c874638d9478a5d5028854817a5ee72293bb2bDevang Patel
698ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    Instruction *New;
699ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
700051a950000e21935165db56695e35bade668193bGabor Greif      New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
701051a950000e21935165db56695e35bade668193bGabor Greif                               Args.begin(), Args.end(), "", Call);
702f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner      cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
7030598866c052147c31b808391f58434ce3dbfb838Devang Patel      cast<InvokeInst>(New)->setAttributes(AttrListPtr::get(AttributesVec.begin(),
7040598866c052147c31b808391f58434ce3dbfb838Devang Patel                                                          AttributesVec.end()));
705ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    } else {
706051a950000e21935165db56695e35bade668193bGabor Greif      New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
707f201dbc1a4a638659ec3916785196e2c204c7755Chris Lattner      cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
7080598866c052147c31b808391f58434ce3dbfb838Devang Patel      cast<CallInst>(New)->setAttributes(AttrListPtr::get(AttributesVec.begin(),
7090598866c052147c31b808391f58434ce3dbfb838Devang Patel                                                        AttributesVec.end()));
7101430ef134d36888b99d2e6fedd2b025882593538Chris Lattner      if (cast<CallInst>(Call)->isTailCall())
7111430ef134d36888b99d2e6fedd2b025882593538Chris Lattner        cast<CallInst>(New)->setTailCall();
712ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    }
713ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    Args.clear();
7140598866c052147c31b808391f58434ce3dbfb838Devang Patel    AttributesVec.clear();
715ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
7169e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner    // Update the alias analysis implementation to know that we are replacing
7179e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner    // the old call with a new one.
7189e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner    AA.replaceWithNewValue(Call, New);
7199e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
72034c8847b2d27433ec7b81c824b66771e7665873aDuncan Sands    // Update the callgraph to know that the callsite has been transformed.
72134c8847b2d27433ec7b81c824b66771e7665873aDuncan Sands    CG[Call->getParent()->getParent()]->replaceCallSite(Call, New);
72234c8847b2d27433ec7b81c824b66771e7665873aDuncan Sands
723ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    if (!Call->use_empty()) {
724ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      Call->replaceAllUsesWith(New);
725046800a7125cd497613efc0e1ea15cb595666585Chris Lattner      New->takeName(Call);
726ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    }
727fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
728ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // Finally, remove the old call from the program, reducing the use-count of
729ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    // F.
73040c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner    Call->eraseFromParent();
731ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  }
732ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
733ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Since we have now created the new function, splice the body of the old
734ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // function right into the new function, leaving the old rotting hulk of the
735ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // function empty.
736ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
737ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
738ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Loop over the argument list, transfering uses of the old arguments over to
739ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // the new arguments, also transfering over the names as well.
740ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  //
74193e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
74210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner       I2 = NF->arg_begin(); I != E; ++I) {
74310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    if (!ArgsToPromote.count(I) && !ByValArgsToTransform.count(I)) {
744ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      // If this is an unmodified argument, move the name and users over to the
745ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      // new version.
746ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      I->replaceAllUsesWith(I2);
747046800a7125cd497613efc0e1ea15cb595666585Chris Lattner      I2->takeName(I);
7489e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner      AA.replaceWithNewValue(I, I2);
749ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      ++I2;
75010603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      continue;
75110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    }
7524cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
75310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    if (ByValArgsToTransform.count(I)) {
75410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      // In the callee, we create an alloca, and store each of the new incoming
75510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      // arguments into the alloca.
75610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      Instruction *InsertPt = NF->begin()->begin();
7574cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
75810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      // Just add all the struct element types.
75910603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      const Type *AgTy = cast<PointerType>(I->getType())->getElementType();
76050dead06ffc107edb7e84857baaeeb09039c631cOwen Anderson      Value *TheAlloca = new AllocaInst(AgTy, 0, "", InsertPt);
76110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      const StructType *STy = cast<StructType>(AgTy);
762eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson      Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
7634cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
76410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
765eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson        Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
766dfd3b647839d3f944b5d8bffd6e9a2ec022d3507Daniel Dunbar        Value *Idx =
767dfd3b647839d3f944b5d8bffd6e9a2ec022d3507Daniel Dunbar          GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2,
768dfd3b647839d3f944b5d8bffd6e9a2ec022d3507Daniel Dunbar                                    TheAlloca->getName()+"."+utostr(i),
769dfd3b647839d3f944b5d8bffd6e9a2ec022d3507Daniel Dunbar                                    InsertPt);
77010603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        I2->setName(I->getName()+"."+utostr(i));
77110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        new StoreInst(I2++, Idx, InsertPt);
77210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      }
7734cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
77410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      // Anything that used the arg should now use the alloca.
77510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      I->replaceAllUsesWith(TheAlloca);
77610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      TheAlloca->takeName(I);
77710603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      AA.replaceWithNewValue(I, TheAlloca);
77810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      continue;
7794cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands    }
7804cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
78110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    if (I->use_empty()) {
7829e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner      AA.deleteValue(I);
78310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      continue;
78410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    }
7854cddaf77c4f2482e7d3e7cc1c80895523dcfb68eDuncan Sands
78610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    // Otherwise, if we promoted this argument, then all users are load
787477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // instructions (or GEPs with only load users), and all loads should be
788477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman    // using the new argument that we added.
78910603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    ScalarizeTable &ArgIndices = ScalarizedElements[I];
79010603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner
79110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    while (!I->use_empty()) {
79210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      if (LoadInst *LI = dyn_cast<LoadInst>(I->use_back())) {
79310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        assert(ArgIndices.begin()->empty() &&
79410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner               "Load element should sort to front!");
79510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        I2->setName(I->getName()+".val");
79610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        LI->replaceAllUsesWith(I2);
79710603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        AA.replaceWithNewValue(LI, I2);
79810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        LI->eraseFromParent();
799ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar        DEBUG(errs() << "*** Promoted load of argument '" << I->getName()
800ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar              << "' in function '" << F->getName() << "'\n");
80110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      } else {
80210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back());
803477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        IndicesVector Operands;
804477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        Operands.reserve(GEP->getNumIndices());
805477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        for (User::op_iterator II = GEP->idx_begin(), IE = GEP->idx_end();
806477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman             II != IE; ++II)
807477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          Operands.push_back(cast<ConstantInt>(*II)->getSExtValue());
808477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman
809477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        // GEPs with a single 0 index can be merged with direct loads
810477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        if (Operands.size() == 1 && Operands.front() == 0)
811477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman          Operands.clear();
81210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner
81310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        Function::arg_iterator TheArg = I2;
81410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        for (ScalarizeTable::iterator It = ArgIndices.begin();
81510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner             *It != Operands; ++It, ++TheArg) {
81610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          assert(It != ArgIndices.end() && "GEP not handled??");
81710603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        }
8189440db886627161a8413e823797569fc7b10beafChris Lattner
81910603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        std::string NewName = I->getName();
820477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
821477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman            NewName += "." + utostr(Operands[i]);
822477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        }
823477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        NewName += ".val";
824477f5a2f11a0383b4ecfcb0db2913027ed38ee39Matthijs Kooijman        TheArg->setName(NewName);
82510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner
826ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar        DEBUG(errs() << "*** Promoted agg argument '" << TheArg->getName()
827ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar              << "' of function '" << NF->getName() << "'\n");
82810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner
82910603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        // All of the uses must be load instructions.  Replace them all with
83010603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        // the argument specified by ArgNo.
83110603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        while (!GEP->use_empty()) {
83210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          LoadInst *L = cast<LoadInst>(GEP->use_back());
83310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          L->replaceAllUsesWith(TheArg);
83410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          AA.replaceWithNewValue(L, TheArg);
83510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner          L->eraseFromParent();
8369440db886627161a8413e823797569fc7b10beafChris Lattner        }
83710603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        AA.deleteValue(GEP);
83810603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner        GEP->eraseFromParent();
839ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner      }
840ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner    }
841ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner
84210603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    // Increment I2 past all of the arguments added for this promoted pointer.
84310603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner    for (unsigned i = 0, e = ArgIndices.size(); i != e; ++i)
84410603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner      ++I2;
84510603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner  }
84610603e0c84d15f61443e8b17bc35f98cc46606d9Chris Lattner
8479e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  // Notify the alias analysis implementation that we inserted a new argument.
8489e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  if (ExtraArgHack)
849e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson    AA.copyValue(Context.getNullValue(Type::Int32Ty), NF->arg_begin());
8509e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
8519e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
8529e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  // Tell the alias analysis that the old function is about to disappear.
8539e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner  AA.replaceWithNewValue(F, NF);
8549e7cc2f0d42aa4127e3b8406e25907a96ce9ada0Chris Lattner
855ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner  // Now that the old function is dead, delete it.
85640c14be5bc9ba900f01c408b65aca57e053788e1Chris Lattner  F->eraseFromParent();
8575eb6f6c829ddfe353f94623aa1009c72be930497Chris Lattner  return NF;
858ed570a7dca45e001a6223e2a25d034b838934f88Chris Lattner}
859