GlobalOpt.cpp revision d514d8294d13e6a695ed324b415d3a9263313355
1//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass transforms simple global variables that never have their address
11// taken.  If obviously true, it marks read/write globals as constant, deletes
12// variables only stored to, etc.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "globalopt"
17#include "llvm/Transforms/IPO.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Pass.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Transforms/Utils/Local.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
28#include <set>
29#include <algorithm>
30using namespace llvm;
31
32namespace {
33  Statistic<> NumMarked   ("globalopt", "Number of globals marked constant");
34  Statistic<> NumSRA      ("globalopt", "Number of aggregate globals broken "
35                           "into scalars");
36  Statistic<> NumSubstitute("globalopt",
37                        "Number of globals with initializers stored into them");
38  Statistic<> NumDeleted  ("globalopt", "Number of globals deleted");
39  Statistic<> NumFnDeleted("globalopt", "Number of functions deleted");
40  Statistic<> NumGlobUses ("globalopt", "Number of global uses devirtualized");
41  Statistic<> NumShrunkToBool("globalopt",
42                              "Number of global vars shrunk to booleans");
43
44  struct GlobalOpt : public ModulePass {
45    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46      AU.addRequired<TargetData>();
47    }
48
49    bool runOnModule(Module &M);
50
51  private:
52    bool ProcessInternalGlobal(GlobalVariable *GV, Module::giterator &GVI);
53  };
54
55  RegisterOpt<GlobalOpt> X("globalopt", "Global Variable Optimizer");
56}
57
58ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
59
60/// GlobalStatus - As we analyze each global, keep track of some information
61/// about it.  If we find out that the address of the global is taken, none of
62/// this info will be accurate.
63struct GlobalStatus {
64  /// isLoaded - True if the global is ever loaded.  If the global isn't ever
65  /// loaded it can be deleted.
66  bool isLoaded;
67
68  /// StoredType - Keep track of what stores to the global look like.
69  ///
70  enum StoredType {
71    /// NotStored - There is no store to this global.  It can thus be marked
72    /// constant.
73    NotStored,
74
75    /// isInitializerStored - This global is stored to, but the only thing
76    /// stored is the constant it was initialized with.  This is only tracked
77    /// for scalar globals.
78    isInitializerStored,
79
80    /// isStoredOnce - This global is stored to, but only its initializer and
81    /// one other value is ever stored to it.  If this global isStoredOnce, we
82    /// track the value stored to it in StoredOnceValue below.  This is only
83    /// tracked for scalar globals.
84    isStoredOnce,
85
86    /// isStored - This global is stored to by multiple values or something else
87    /// that we cannot track.
88    isStored
89  } StoredType;
90
91  /// StoredOnceValue - If only one value (besides the initializer constant) is
92  /// ever stored to this global, keep track of what value it is.
93  Value *StoredOnceValue;
94
95  /// isNotSuitableForSRA - Keep track of whether any SRA preventing users of
96  /// the global exist.  Such users include GEP instruction with variable
97  /// indexes, and non-gep/load/store users like constant expr casts.
98  bool isNotSuitableForSRA;
99
100  GlobalStatus() : isLoaded(false), StoredType(NotStored), StoredOnceValue(0),
101                   isNotSuitableForSRA(false) {}
102};
103
104
105
106/// ConstantIsDead - Return true if the specified constant is (transitively)
107/// dead.  The constant may be used by other constants (e.g. constant arrays and
108/// constant exprs) as long as they are dead, but it cannot be used by anything
109/// else.
110static bool ConstantIsDead(Constant *C) {
111  if (isa<GlobalValue>(C)) return false;
112
113  for (Value::use_iterator UI = C->use_begin(), E = C->use_end(); UI != E; ++UI)
114    if (Constant *CU = dyn_cast<Constant>(*UI)) {
115      if (!ConstantIsDead(CU)) return false;
116    } else
117      return false;
118  return true;
119}
120
121
122/// AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus
123/// structure.  If the global has its address taken, return true to indicate we
124/// can't do anything with it.
125///
126static bool AnalyzeGlobal(Value *V, GlobalStatus &GS,
127                          std::set<PHINode*> &PHIUsers) {
128  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
129    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
130      if (AnalyzeGlobal(CE, GS, PHIUsers)) return true;
131      if (CE->getOpcode() != Instruction::GetElementPtr)
132        GS.isNotSuitableForSRA = true;
133      else if (!GS.isNotSuitableForSRA) {
134        // Check to see if this ConstantExpr GEP is SRA'able.  In particular, we
135        // don't like < 3 operand CE's, and we don't like non-constant integer
136        // indices.
137        if (CE->getNumOperands() < 3 || !CE->getOperand(1)->isNullValue())
138          GS.isNotSuitableForSRA = true;
139        else {
140          for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
141            if (!isa<ConstantInt>(CE->getOperand(i))) {
142              GS.isNotSuitableForSRA = true;
143              break;
144            }
145        }
146      }
147
148    } else if (Instruction *I = dyn_cast<Instruction>(*UI)) {
149      if (isa<LoadInst>(I)) {
150        GS.isLoaded = true;
151      } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
152        // Don't allow a store OF the address, only stores TO the address.
153        if (SI->getOperand(0) == V) return true;
154
155        // If this is a direct store to the global (i.e., the global is a scalar
156        // value, not an aggregate), keep more specific information about
157        // stores.
158        if (GS.StoredType != GlobalStatus::isStored)
159          if (GlobalVariable *GV = dyn_cast<GlobalVariable>(SI->getOperand(1))){
160            Value *StoredVal = SI->getOperand(0);
161            if (StoredVal == GV->getInitializer()) {
162              if (GS.StoredType < GlobalStatus::isInitializerStored)
163                GS.StoredType = GlobalStatus::isInitializerStored;
164            } else if (isa<LoadInst>(StoredVal) &&
165                       cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
166              // G = G
167              if (GS.StoredType < GlobalStatus::isInitializerStored)
168                GS.StoredType = GlobalStatus::isInitializerStored;
169            } else if (GS.StoredType < GlobalStatus::isStoredOnce) {
170              GS.StoredType = GlobalStatus::isStoredOnce;
171              GS.StoredOnceValue = StoredVal;
172            } else if (GS.StoredType == GlobalStatus::isStoredOnce &&
173                       GS.StoredOnceValue == StoredVal) {
174              // noop.
175            } else {
176              GS.StoredType = GlobalStatus::isStored;
177            }
178          } else {
179            GS.StoredType = GlobalStatus::isStored;
180          }
181      } else if (I->getOpcode() == Instruction::GetElementPtr) {
182        if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
183
184        // If the first two indices are constants, this can be SRA'd.
185        if (isa<GlobalVariable>(I->getOperand(0))) {
186          if (I->getNumOperands() < 3 || !isa<Constant>(I->getOperand(1)) ||
187              !cast<Constant>(I->getOperand(1))->isNullValue() ||
188              !isa<ConstantInt>(I->getOperand(2)))
189            GS.isNotSuitableForSRA = true;
190        } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I->getOperand(0))){
191          if (CE->getOpcode() != Instruction::GetElementPtr ||
192              CE->getNumOperands() < 3 || I->getNumOperands() < 2 ||
193              !isa<Constant>(I->getOperand(0)) ||
194              !cast<Constant>(I->getOperand(0))->isNullValue())
195            GS.isNotSuitableForSRA = true;
196        } else {
197          GS.isNotSuitableForSRA = true;
198        }
199      } else if (I->getOpcode() == Instruction::Select) {
200        if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
201        GS.isNotSuitableForSRA = true;
202      } else if (PHINode *PN = dyn_cast<PHINode>(I)) {
203        // PHI nodes we can check just like select or GEP instructions, but we
204        // have to be careful about infinite recursion.
205        if (PHIUsers.insert(PN).second)  // Not already visited.
206          if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
207        GS.isNotSuitableForSRA = true;
208      } else if (isa<SetCondInst>(I)) {
209        GS.isNotSuitableForSRA = true;
210      } else {
211        return true;  // Any other non-load instruction might take address!
212      }
213    } else if (Constant *C = dyn_cast<Constant>(*UI)) {
214      // We might have a dead and dangling constant hanging off of here.
215      if (!ConstantIsDead(C))
216        return true;
217    } else {
218      // Otherwise must be a global or some other user.
219      return true;
220    }
221
222  return false;
223}
224
225static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) {
226  ConstantInt *CI = dyn_cast<ConstantInt>(Idx);
227  if (!CI) return 0;
228  unsigned IdxV = (unsigned)CI->getRawValue();
229
230  if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg)) {
231    if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV);
232  } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) {
233    if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV);
234  } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(Agg)) {
235    if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV);
236  } else if (isa<ConstantAggregateZero>(Agg)) {
237    if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) {
238      if (IdxV < STy->getNumElements())
239        return Constant::getNullValue(STy->getElementType(IdxV));
240    } else if (const SequentialType *STy =
241               dyn_cast<SequentialType>(Agg->getType())) {
242      return Constant::getNullValue(STy->getElementType());
243    }
244  } else if (isa<UndefValue>(Agg)) {
245    if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) {
246      if (IdxV < STy->getNumElements())
247        return UndefValue::get(STy->getElementType(IdxV));
248    } else if (const SequentialType *STy =
249               dyn_cast<SequentialType>(Agg->getType())) {
250      return UndefValue::get(STy->getElementType());
251    }
252  }
253  return 0;
254}
255
256static Constant *TraverseGEPInitializer(User *GEP, Constant *Init) {
257  if (GEP->getNumOperands() == 1 ||
258      !isa<Constant>(GEP->getOperand(1)) ||
259      !cast<Constant>(GEP->getOperand(1))->isNullValue())
260    return 0;
261
262  for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) {
263    ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
264    if (!Idx) return 0;
265    Init = getAggregateConstantElement(Init, Idx);
266    if (Init == 0) return 0;
267  }
268  return Init;
269}
270
271/// CleanupConstantGlobalUsers - We just marked GV constant.  Loop over all
272/// users of the global, cleaning up the obvious ones.  This is largely just a
273/// quick scan over the use list to clean up the easy and obvious cruft.  This
274/// returns true if it made a change.
275static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) {
276  bool Changed = false;
277  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) {
278    User *U = *UI++;
279
280    if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
281      // Replace the load with the initializer.
282      LI->replaceAllUsesWith(Init);
283      LI->eraseFromParent();
284      Changed = true;
285    } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
286      // Store must be unreachable or storing Init into the global.
287      SI->eraseFromParent();
288      Changed = true;
289    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
290      if (CE->getOpcode() == Instruction::GetElementPtr) {
291        if (Constant *SubInit = TraverseGEPInitializer(CE, Init))
292          Changed |= CleanupConstantGlobalUsers(CE, SubInit);
293        if (CE->use_empty()) {
294          CE->destroyConstant();
295          Changed = true;
296        }
297      }
298    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
299      if (Constant *SubInit = TraverseGEPInitializer(GEP, Init))
300        Changed |= CleanupConstantGlobalUsers(GEP, SubInit);
301      else {
302        // If this GEP has variable indexes, we should still be able to delete
303        // any stores through it.
304        for (Value::use_iterator GUI = GEP->use_begin(), E = GEP->use_end();
305             GUI != E;)
306          if (StoreInst *SI = dyn_cast<StoreInst>(*GUI++)) {
307            SI->eraseFromParent();
308            Changed = true;
309          }
310      }
311
312      if (GEP->use_empty()) {
313        GEP->eraseFromParent();
314        Changed = true;
315      }
316    } else if (Constant *C = dyn_cast<Constant>(U)) {
317      // If we have a chain of dead constantexprs or other things dangling from
318      // us, and if they are all dead, nuke them without remorse.
319      if (ConstantIsDead(C)) {
320        C->destroyConstant();
321        // This could have incalidated UI, start over from scratch.x
322        CleanupConstantGlobalUsers(V, Init);
323        return true;
324      }
325    }
326  }
327  return Changed;
328}
329
330/// SRAGlobal - Perform scalar replacement of aggregates on the specified global
331/// variable.  This opens the door for other optimizations by exposing the
332/// behavior of the program in a more fine-grained way.  We have determined that
333/// this transformation is safe already.  We return the first global variable we
334/// insert so that the caller can reprocess it.
335static GlobalVariable *SRAGlobal(GlobalVariable *GV) {
336  assert(GV->hasInternalLinkage() && !GV->isConstant());
337  Constant *Init = GV->getInitializer();
338  const Type *Ty = Init->getType();
339
340  std::vector<GlobalVariable*> NewGlobals;
341  Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
342
343  if (const StructType *STy = dyn_cast<StructType>(Ty)) {
344    NewGlobals.reserve(STy->getNumElements());
345    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
346      Constant *In = getAggregateConstantElement(Init,
347                                            ConstantUInt::get(Type::UIntTy, i));
348      assert(In && "Couldn't get element of initializer?");
349      GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
350                                               GlobalVariable::InternalLinkage,
351                                               In, GV->getName()+"."+utostr(i));
352      Globals.insert(GV, NGV);
353      NewGlobals.push_back(NGV);
354    }
355  } else if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
356    unsigned NumElements = 0;
357    if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
358      NumElements = ATy->getNumElements();
359    else if (const PackedType *PTy = dyn_cast<PackedType>(STy))
360      NumElements = PTy->getNumElements();
361    else
362      assert(0 && "Unknown aggregate sequential type!");
363
364    if (NumElements > 16 && GV->getNumUses() > 16)
365      return 0; // It's not worth it.
366    NewGlobals.reserve(NumElements);
367    for (unsigned i = 0, e = NumElements; i != e; ++i) {
368      Constant *In = getAggregateConstantElement(Init,
369                                            ConstantUInt::get(Type::UIntTy, i));
370      assert(In && "Couldn't get element of initializer?");
371
372      GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
373                                               GlobalVariable::InternalLinkage,
374                                               In, GV->getName()+"."+utostr(i));
375      Globals.insert(GV, NGV);
376      NewGlobals.push_back(NGV);
377    }
378  }
379
380  if (NewGlobals.empty())
381    return 0;
382
383  DEBUG(std::cerr << "PERFORMING GLOBAL SRA ON: " << *GV);
384
385  Constant *NullInt = Constant::getNullValue(Type::IntTy);
386
387  // Loop over all of the uses of the global, replacing the constantexpr geps,
388  // with smaller constantexpr geps or direct references.
389  while (!GV->use_empty()) {
390    User *GEP = GV->use_back();
391    assert(((isa<ConstantExpr>(GEP) &&
392             cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
393            isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
394
395    // Ignore the 1th operand, which has to be zero or else the program is quite
396    // broken (undefined).  Get the 2nd operand, which is the structure or array
397    // index.
398    unsigned Val =
399       (unsigned)cast<ConstantInt>(GEP->getOperand(2))->getRawValue();
400    if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
401
402    Value *NewPtr = NewGlobals[Val];
403
404    // Form a shorter GEP if needed.
405    if (GEP->getNumOperands() > 3)
406      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
407        std::vector<Constant*> Idxs;
408        Idxs.push_back(NullInt);
409        for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
410          Idxs.push_back(CE->getOperand(i));
411        NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs);
412      } else {
413        GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
414        std::vector<Value*> Idxs;
415        Idxs.push_back(NullInt);
416        for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
417          Idxs.push_back(GEPI->getOperand(i));
418        NewPtr = new GetElementPtrInst(NewPtr, Idxs,
419                                       GEPI->getName()+"."+utostr(Val), GEPI);
420      }
421    GEP->replaceAllUsesWith(NewPtr);
422
423    if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
424      GEPI->eraseFromParent();
425    else
426      cast<ConstantExpr>(GEP)->destroyConstant();
427  }
428
429  // Delete the old global, now that it is dead.
430  Globals.erase(GV);
431  ++NumSRA;
432
433  // Loop over the new globals array deleting any globals that are obviously
434  // dead.  This can arise due to scalarization of a structure or an array that
435  // has elements that are dead.
436  unsigned FirstGlobal = 0;
437  for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
438    if (NewGlobals[i]->use_empty()) {
439      Globals.erase(NewGlobals[i]);
440      if (FirstGlobal == i) ++FirstGlobal;
441    }
442
443  return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0;
444}
445
446/// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
447/// value will trap if the value is dynamically null.
448static bool AllUsesOfValueWillTrapIfNull(Value *V) {
449  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
450    if (isa<LoadInst>(*UI)) {
451      // Will trap.
452    } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
453      if (SI->getOperand(0) == V) {
454        //std::cerr << "NONTRAPPING USE: " << **UI;
455        return false;  // Storing the value.
456      }
457    } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
458      if (CI->getOperand(0) != V) {
459        //std::cerr << "NONTRAPPING USE: " << **UI;
460        return false;  // Not calling the ptr
461      }
462    } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
463      if (II->getOperand(0) != V) {
464        //std::cerr << "NONTRAPPING USE: " << **UI;
465        return false;  // Not calling the ptr
466      }
467    } else if (CastInst *CI = dyn_cast<CastInst>(*UI)) {
468      if (!AllUsesOfValueWillTrapIfNull(CI)) return false;
469    } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) {
470      if (!AllUsesOfValueWillTrapIfNull(GEPI)) return false;
471    } else if (isa<SetCondInst>(*UI) &&
472               isa<ConstantPointerNull>(UI->getOperand(1))) {
473      // Ignore setcc X, null
474    } else {
475      //std::cerr << "NONTRAPPING USE: " << **UI;
476      return false;
477    }
478  return true;
479}
480
481/// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads
482/// from GV will trap if the loaded value is null.  Note that this also permits
483/// comparisons of the loaded value against null, as a special case.
484static bool AllUsesOfLoadedValueWillTrapIfNull(GlobalVariable *GV) {
485  for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI!=E; ++UI)
486    if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
487      if (!AllUsesOfValueWillTrapIfNull(LI))
488        return false;
489    } else if (isa<StoreInst>(*UI)) {
490      // Ignore stores to the global.
491    } else {
492      // We don't know or understand this user, bail out.
493      //std::cerr << "UNKNOWN USER OF GLOBAL!: " << **UI;
494      return false;
495    }
496
497  return true;
498}
499
500static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
501  bool Changed = false;
502  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) {
503    Instruction *I = cast<Instruction>(*UI++);
504    if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
505      LI->setOperand(0, NewV);
506      Changed = true;
507    } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
508      if (SI->getOperand(1) == V) {
509        SI->setOperand(1, NewV);
510        Changed = true;
511      }
512    } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
513      if (I->getOperand(0) == V) {
514        // Calling through the pointer!  Turn into a direct call, but be careful
515        // that the pointer is not also being passed as an argument.
516        I->setOperand(0, NewV);
517        Changed = true;
518        bool PassedAsArg = false;
519        for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i)
520          if (I->getOperand(i) == V) {
521            PassedAsArg = true;
522            I->setOperand(i, NewV);
523          }
524
525        if (PassedAsArg) {
526          // Being passed as an argument also.  Be careful to not invalidate UI!
527          UI = V->use_begin();
528        }
529      }
530    } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
531      Changed |= OptimizeAwayTrappingUsesOfValue(CI,
532                                    ConstantExpr::getCast(NewV, CI->getType()));
533      if (CI->use_empty()) {
534        Changed = true;
535        CI->eraseFromParent();
536      }
537    } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
538      // Should handle GEP here.
539      std::vector<Constant*> Indices;
540      Indices.reserve(GEPI->getNumOperands()-1);
541      for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
542        if (Constant *C = dyn_cast<Constant>(GEPI->getOperand(i)))
543          Indices.push_back(C);
544        else
545          break;
546      if (Indices.size() == GEPI->getNumOperands()-1)
547        Changed |= OptimizeAwayTrappingUsesOfValue(GEPI,
548                                ConstantExpr::getGetElementPtr(NewV, Indices));
549      if (GEPI->use_empty()) {
550        Changed = true;
551        GEPI->eraseFromParent();
552      }
553    }
554  }
555
556  return Changed;
557}
558
559
560/// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null
561/// value stored into it.  If there are uses of the loaded value that would trap
562/// if the loaded value is dynamically null, then we know that they cannot be
563/// reachable with a null optimize away the load.
564static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV) {
565  std::vector<LoadInst*> Loads;
566  bool Changed = false;
567
568  // Replace all uses of loads with uses of uses of the stored value.
569  for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end();
570       GUI != E; ++GUI)
571    if (LoadInst *LI = dyn_cast<LoadInst>(*GUI)) {
572      Loads.push_back(LI);
573      Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
574    } else {
575      assert(isa<StoreInst>(*GUI) && "Only expect load and stores!");
576    }
577
578  if (Changed) {
579    DEBUG(std::cerr << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV);
580    ++NumGlobUses;
581  }
582
583  // Delete all of the loads we can, keeping track of whether we nuked them all!
584  bool AllLoadsGone = true;
585  while (!Loads.empty()) {
586    LoadInst *L = Loads.back();
587    if (L->use_empty()) {
588      L->eraseFromParent();
589      Changed = true;
590    } else {
591      AllLoadsGone = false;
592    }
593    Loads.pop_back();
594  }
595
596  // If we nuked all of the loads, then none of the stores are needed either,
597  // nor is the global.
598  if (AllLoadsGone) {
599    DEBUG(std::cerr << "  *** GLOBAL NOW DEAD!\n");
600    CleanupConstantGlobalUsers(GV, 0);
601    if (GV->use_empty()) {
602      GV->eraseFromParent();
603      ++NumDeleted;
604    }
605    Changed = true;
606  }
607  return Changed;
608}
609
610/// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
611/// instructions that are foldable.
612static void ConstantPropUsersOf(Value *V) {
613  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; )
614    if (Instruction *I = dyn_cast<Instruction>(*UI++))
615      if (Constant *NewC = ConstantFoldInstruction(I)) {
616        I->replaceAllUsesWith(NewC);
617
618        // Advance UI to the next non-I use to avoid invalidating it!
619        // Instructions could multiply use V.
620        while (UI != E && *UI == I)
621          ++UI;
622        I->eraseFromParent();
623      }
624}
625
626/// OptimizeGlobalAddressOfMalloc - This function takes the specified global
627/// variable, and transforms the program as if it always contained the result of
628/// the specified malloc.  Because it is always the result of the specified
629/// malloc, there is no reason to actually DO the malloc.  Instead, turn the
630/// malloc into a global, and any laods of GV as uses of the new global.
631static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
632                                                     MallocInst *MI) {
633  DEBUG(std::cerr << "PROMOTING MALLOC GLOBAL: " << *GV << "  MALLOC = " <<*MI);
634  ConstantInt *NElements = cast<ConstantInt>(MI->getArraySize());
635
636  if (NElements->getRawValue() != 1) {
637    // If we have an array allocation, transform it to a single element
638    // allocation to make the code below simpler.
639    Type *NewTy = ArrayType::get(MI->getAllocatedType(),
640                                 (unsigned)NElements->getRawValue());
641    MallocInst *NewMI =
642      new MallocInst(NewTy, Constant::getNullValue(Type::UIntTy),
643                     MI->getName(), MI);
644    std::vector<Value*> Indices;
645    Indices.push_back(Constant::getNullValue(Type::IntTy));
646    Indices.push_back(Indices[0]);
647    Value *NewGEP = new GetElementPtrInst(NewMI, Indices,
648                                          NewMI->getName()+".el0", MI);
649    MI->replaceAllUsesWith(NewGEP);
650    MI->eraseFromParent();
651    MI = NewMI;
652  }
653
654  // Create the new global variable.  The contents of the malloc'd memory is
655  // undefined, so initialize with an undef value.
656  Constant *Init = UndefValue::get(MI->getAllocatedType());
657  GlobalVariable *NewGV = new GlobalVariable(MI->getAllocatedType(), false,
658                                             GlobalValue::InternalLinkage, Init,
659                                             GV->getName()+".body");
660  GV->getParent()->getGlobalList().insert(GV, NewGV);
661
662  // Anything that used the malloc now uses the global directly.
663  MI->replaceAllUsesWith(NewGV);
664
665  Constant *RepValue = NewGV;
666  if (NewGV->getType() != GV->getType()->getElementType())
667    RepValue = ConstantExpr::getCast(RepValue, GV->getType()->getElementType());
668
669  // If there is a comparison against null, we will insert a global bool to
670  // keep track of whether the global was initialized yet or not.
671  GlobalVariable *InitBool =
672    new GlobalVariable(Type::BoolTy, false, GlobalValue::InternalLinkage,
673                       ConstantBool::False, GV->getName()+".init");
674  bool InitBoolUsed = false;
675
676  // Loop over all uses of GV, processing them in turn.
677  std::vector<StoreInst*> Stores;
678  while (!GV->use_empty())
679    if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) {
680      while (!LI->use_empty()) {
681        Use &LoadUse = LI->use_begin().getUse();
682        if (!isa<SetCondInst>(LoadUse.getUser()))
683          LoadUse = RepValue;
684        else {
685          // Replace the setcc X, 0 with a use of the bool value.
686          SetCondInst *SCI = cast<SetCondInst>(LoadUse.getUser());
687          Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", SCI);
688          InitBoolUsed = true;
689          switch (SCI->getOpcode()) {
690          default: assert(0 && "Unknown opcode!");
691          case Instruction::SetLT:
692            LV = ConstantBool::False;   // X < null -> always false
693            break;
694          case Instruction::SetEQ:
695          case Instruction::SetLE:
696            LV = BinaryOperator::createNot(LV, "notinit", SCI);
697            break;
698          case Instruction::SetNE:
699          case Instruction::SetGE:
700          case Instruction::SetGT:
701            break;  // no change.
702          }
703          SCI->replaceAllUsesWith(LV);
704          SCI->eraseFromParent();
705        }
706      }
707      LI->eraseFromParent();
708    } else {
709      StoreInst *SI = cast<StoreInst>(GV->use_back());
710      // The global is initialized when the store to it occurs.
711      new StoreInst(ConstantBool::True, InitBool, SI);
712      SI->eraseFromParent();
713    }
714
715  // If the initialization boolean was used, insert it, otherwise delete it.
716  if (!InitBoolUsed) {
717    while (!InitBool->use_empty())  // Delete initializations
718      cast<Instruction>(InitBool->use_back())->eraseFromParent();
719    delete InitBool;
720  } else
721    GV->getParent()->getGlobalList().insert(GV, InitBool);
722
723
724  // Now the GV is dead, nuke it and the malloc.
725  GV->eraseFromParent();
726  MI->eraseFromParent();
727
728  // To further other optimizations, loop over all users of NewGV and try to
729  // constant prop them.  This will promote GEP instructions with constant
730  // indices into GEP constant-exprs, which will allow global-opt to hack on it.
731  ConstantPropUsersOf(NewGV);
732  if (RepValue != NewGV)
733    ConstantPropUsersOf(RepValue);
734
735  return NewGV;
736}
737
738/// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
739/// to make sure that there are no complex uses of V.  We permit simple things
740/// like dereferencing the pointer, but not storing through the address, unless
741/// it is to the specified global.
742static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V,
743                                                      GlobalVariable *GV) {
744  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI)
745    if (isa<LoadInst>(*UI) || isa<SetCondInst>(*UI)) {
746      // Fine, ignore.
747    } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
748      if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
749        return false;  // Storing the pointer itself... bad.
750      // Otherwise, storing through it, or storing into GV... fine.
751    } else if (isa<GetElementPtrInst>(*UI) || isa<SelectInst>(*UI)) {
752      if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(cast<Instruction>(*UI),GV))
753        return false;
754    } else {
755      return false;
756    }
757  return true;
758
759}
760
761// OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
762// that only one value (besides its initializer) is ever stored to the global.
763static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
764                                     Module::giterator &GVI, TargetData &TD) {
765  if (CastInst *CI = dyn_cast<CastInst>(StoredOnceVal))
766    StoredOnceVal = CI->getOperand(0);
767  else if (GetElementPtrInst *GEPI =dyn_cast<GetElementPtrInst>(StoredOnceVal)){
768    // "getelementptr Ptr, 0, 0, 0" is really just a cast.
769    bool IsJustACast = true;
770    for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
771      if (!isa<Constant>(GEPI->getOperand(i)) ||
772          !cast<Constant>(GEPI->getOperand(i))->isNullValue()) {
773        IsJustACast = false;
774        break;
775      }
776    if (IsJustACast)
777      StoredOnceVal = GEPI->getOperand(0);
778  }
779
780  // If we are dealing with a pointer global that is initialized to null and
781  // only has one (non-null) value stored into it, then we can optimize any
782  // users of the loaded value (often calls and loads) that would trap if the
783  // value was null.
784  if (isa<PointerType>(GV->getInitializer()->getType()) &&
785      GV->getInitializer()->isNullValue()) {
786    if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
787      if (GV->getInitializer()->getType() != SOVC->getType())
788        SOVC = ConstantExpr::getCast(SOVC, GV->getInitializer()->getType());
789
790      // Optimize away any trapping uses of the loaded value.
791      if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC))
792        return true;
793    } else if (MallocInst *MI = dyn_cast<MallocInst>(StoredOnceVal)) {
794      // If we have a global that is only initialized with a fixed size malloc,
795      // and if all users of the malloc trap, and if the malloc'd address is not
796      // put anywhere else, transform the program to use global memory instead
797      // of malloc'd memory.  This eliminates dynamic allocation (good) and
798      // exposes the resultant global to further GlobalOpt (even better).  Note
799      // that we restrict this transformation to only working on small
800      // allocations (2048 bytes currently), as we don't want to introduce a 16M
801      // global or something.
802      if (ConstantInt *NElements = dyn_cast<ConstantInt>(MI->getArraySize()))
803        if (MI->getAllocatedType()->isSized() &&
804            NElements->getRawValue()*
805                     TD.getTypeSize(MI->getAllocatedType()) < 2048 &&
806            AllUsesOfLoadedValueWillTrapIfNull(GV) &&
807            ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV)) {
808          GVI = OptimizeGlobalAddressOfMalloc(GV, MI);
809          return true;
810        }
811    }
812  }
813
814  return false;
815}
816
817/// ShrinkGlobalToBoolean - At this point, we have learned that the only two
818/// values ever stored into GV are its initializer and OtherVal.
819static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
820  // Create the new global, initializing it to false.
821  GlobalVariable *NewGV = new GlobalVariable(Type::BoolTy, false,
822         GlobalValue::InternalLinkage, ConstantBool::False, GV->getName()+".b");
823  GV->getParent()->getGlobalList().insert(GV, NewGV);
824
825  Constant *InitVal = GV->getInitializer();
826  assert(InitVal->getType() != Type::BoolTy && "No reason to shrink to bool!");
827
828  // If initialized to zero and storing one into the global, we can use a cast
829  // instead of a select to synthesize the desired value.
830  bool IsOneZero = false;
831  if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
832    IsOneZero = InitVal->isNullValue() && CI->equalsInt(1);
833
834  while (!GV->use_empty()) {
835    Instruction *UI = cast<Instruction>(GV->use_back());
836    if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
837      // Change the store into a boolean store.
838      bool StoringOther = SI->getOperand(0) == OtherVal;
839      // Only do this if we weren't storing a loaded value.
840      Value *StoreVal;
841      if (StoringOther || SI->getOperand(0) == InitVal)
842        StoreVal = ConstantBool::get(StoringOther);
843      else {
844        // Otherwise, we are storing a previously loaded copy.  To do this,
845        // change the copy from copying the original value to just copying the
846        // bool.
847        Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
848
849        // If we're already replaced the input, StoredVal will be a cast or
850        // select instruction.  If not, it will be a load of the original
851        // global.
852        if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
853          assert(LI->getOperand(0) == GV && "Not a copy!");
854          // Insert a new load, to preserve the saved value.
855          StoreVal = new LoadInst(NewGV, LI->getName()+".b", LI);
856        } else {
857          assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
858                 "This is not a form that we understand!");
859          StoreVal = StoredVal->getOperand(0);
860          assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
861        }
862      }
863      new StoreInst(StoreVal, NewGV, SI);
864    } else if (!UI->use_empty()) {
865      // Change the load into a load of bool then a select.
866      LoadInst *LI = cast<LoadInst>(UI);
867
868      std::string Name = LI->getName(); LI->setName("");
869      LoadInst *NLI = new LoadInst(NewGV, Name+".b", LI);
870      Value *NSI;
871      if (IsOneZero)
872        NSI = new CastInst(NLI, LI->getType(), Name, LI);
873      else
874        NSI = new SelectInst(NLI, OtherVal, InitVal, Name, LI);
875      LI->replaceAllUsesWith(NSI);
876    }
877    UI->eraseFromParent();
878  }
879
880  GV->eraseFromParent();
881}
882
883
884/// ProcessInternalGlobal - Analyze the specified global variable and optimize
885/// it if possible.  If we make a change, return true.
886bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
887                                      Module::giterator &GVI) {
888  std::set<PHINode*> PHIUsers;
889  GlobalStatus GS;
890  PHIUsers.clear();
891  GV->removeDeadConstantUsers();
892
893  if (GV->use_empty()) {
894    DEBUG(std::cerr << "GLOBAL DEAD: " << *GV);
895    GV->eraseFromParent();
896    ++NumDeleted;
897    return true;
898  }
899
900  if (!AnalyzeGlobal(GV, GS, PHIUsers)) {
901    // If the global is never loaded (but may be stored to), it is dead.
902    // Delete it now.
903    if (!GS.isLoaded) {
904      DEBUG(std::cerr << "GLOBAL NEVER LOADED: " << *GV);
905
906      // Delete any stores we can find to the global.  We may not be able to
907      // make it completely dead though.
908      bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer());
909
910      // If the global is dead now, delete it.
911      if (GV->use_empty()) {
912        GV->eraseFromParent();
913        ++NumDeleted;
914        Changed = true;
915      }
916      return Changed;
917
918    } else if (GS.StoredType <= GlobalStatus::isInitializerStored) {
919      DEBUG(std::cerr << "MARKING CONSTANT: " << *GV);
920      GV->setConstant(true);
921
922      // Clean up any obviously simplifiable users now.
923      CleanupConstantGlobalUsers(GV, GV->getInitializer());
924
925      // If the global is dead now, just nuke it.
926      if (GV->use_empty()) {
927        DEBUG(std::cerr << "   *** Marking constant allowed us to simplify "
928              "all users and delete global!\n");
929        GV->eraseFromParent();
930        ++NumDeleted;
931      }
932
933      ++NumMarked;
934      return true;
935    } else if (!GS.isNotSuitableForSRA &&
936               !GV->getInitializer()->getType()->isFirstClassType()) {
937      if (GlobalVariable *FirstNewGV = SRAGlobal(GV)) {
938        GVI = FirstNewGV;  // Don't skip the newly produced globals!
939        return true;
940      }
941    } else if (GS.StoredType == GlobalStatus::isStoredOnce) {
942      // If the initial value for the global was an undef value, and if only
943      // one other value was stored into it, we can just change the
944      // initializer to be an undef value, then delete all stores to the
945      // global.  This allows us to mark it constant.
946      if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
947        if (isa<UndefValue>(GV->getInitializer())) {
948          // Change the initial value here.
949          GV->setInitializer(SOVConstant);
950
951          // Clean up any obviously simplifiable users now.
952          CleanupConstantGlobalUsers(GV, GV->getInitializer());
953
954          if (GV->use_empty()) {
955            DEBUG(std::cerr << "   *** Substituting initializer allowed us to "
956                  "simplify all users and delete global!\n");
957            GV->eraseFromParent();
958            ++NumDeleted;
959          } else {
960            GVI = GV;
961          }
962          ++NumSubstitute;
963          return true;
964        }
965
966      // Try to optimize globals based on the knowledge that only one value
967      // (besides its initializer) is ever stored to the global.
968      if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI,
969                                   getAnalysis<TargetData>()))
970        return true;
971
972      // Otherwise, if the global was not a boolean, we can shrink it to be a
973      // boolean.
974      if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
975        if (GV->getType()->getElementType() != Type::BoolTy &&
976            !GV->getType()->getElementType()->isFloatingPoint()) {
977          DEBUG(std::cerr << "   *** SHRINKING TO BOOL: " << *GV);
978          ShrinkGlobalToBoolean(GV, SOVConstant);
979          ++NumShrunkToBool;
980          return true;
981        }
982    }
983  }
984  return false;
985}
986
987
988bool GlobalOpt::runOnModule(Module &M) {
989  bool Changed = false;
990
991  // As a prepass, delete functions that are trivially dead.
992  bool LocalChange = true;
993  while (LocalChange) {
994    LocalChange = false;
995    for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
996      Function *F = FI++;
997      F->removeDeadConstantUsers();
998      if (F->use_empty() && (F->hasInternalLinkage() ||
999                             F->hasLinkOnceLinkage())) {
1000        M.getFunctionList().erase(F);
1001        LocalChange = true;
1002        ++NumFnDeleted;
1003      }
1004    }
1005    Changed |= LocalChange;
1006  }
1007
1008  LocalChange = true;
1009  while (LocalChange) {
1010    LocalChange = false;
1011    for (Module::giterator GVI = M.gbegin(), E = M.gend(); GVI != E;) {
1012      GlobalVariable *GV = GVI++;
1013      if (!GV->isConstant() && GV->hasInternalLinkage() &&
1014          GV->hasInitializer())
1015        LocalChange |= ProcessInternalGlobal(GV, GVI);
1016    }
1017    Changed |= LocalChange;
1018  }
1019  return Changed;
1020}
1021