Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 1 | //===- GlobalOpt.cpp - Optimize Global Variables --------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 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. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "globalopt" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 18 | #include "llvm/CallingConv.h" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7d90a27 | 2004-02-27 18:09:25 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 22 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Jay Foad | 562b84b | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 24 | #include "llvm/Operator.h" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ConstantFolding.h" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/MemoryBuiltins.h" |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 28 | #include "llvm/DataLayout.h" |
Chad Rosier | 00737bd | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetLibraryInfo.h" |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 33 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 44 | STATISTIC(NumMarked , "Number of globals marked constant"); |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 45 | STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr"); |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 46 | STATISTIC(NumSRA , "Number of aggregate globals broken into scalars"); |
| 47 | STATISTIC(NumHeapSRA , "Number of heap objects SRA'd"); |
| 48 | STATISTIC(NumSubstitute,"Number of globals with initializers stored into them"); |
| 49 | STATISTIC(NumDeleted , "Number of globals deleted"); |
| 50 | STATISTIC(NumFnDeleted , "Number of functions deleted"); |
| 51 | STATISTIC(NumGlobUses , "Number of global uses devirtualized"); |
| 52 | STATISTIC(NumLocalized , "Number of globals localized"); |
| 53 | STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans"); |
| 54 | STATISTIC(NumFastCallFns , "Number of functions converted to fastcc"); |
| 55 | STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated"); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 56 | STATISTIC(NumNestRemoved , "Number of nest attributes removed"); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 57 | STATISTIC(NumAliasesResolved, "Number of global aliases resolved"); |
| 58 | STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated"); |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 59 | STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed"); |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 61 | namespace { |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 62 | struct GlobalStatus; |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 63 | struct GlobalOpt : public ModulePass { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 64 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chad Rosier | 00737bd | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 65 | AU.addRequired<TargetLibraryInfo>(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 66 | } |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 67 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 68 | GlobalOpt() : ModulePass(ID) { |
| 69 | initializeGlobalOptPass(*PassRegistry::getPassRegistry()); |
| 70 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 71 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 72 | bool runOnModule(Module &M); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 73 | |
| 74 | private: |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 75 | GlobalVariable *FindGlobalCtors(Module &M); |
| 76 | bool OptimizeFunctions(Module &M); |
| 77 | bool OptimizeGlobalVars(Module &M); |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 78 | bool OptimizeGlobalAliases(Module &M); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 79 | bool OptimizeGlobalCtorsList(GlobalVariable *&GCL); |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 80 | bool ProcessGlobal(GlobalVariable *GV,Module::global_iterator &GVI); |
| 81 | bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI, |
| 82 | const SmallPtrSet<const PHINode*, 16> &PHIUsers, |
| 83 | const GlobalStatus &GS); |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 84 | bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn); |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 85 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 86 | DataLayout *TD; |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 87 | TargetLibraryInfo *TLI; |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 88 | }; |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 91 | char GlobalOpt::ID = 0; |
Chad Rosier | 00737bd | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 92 | INITIALIZE_PASS_BEGIN(GlobalOpt, "globalopt", |
| 93 | "Global Variable Optimizer", false, false) |
| 94 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
| 95 | INITIALIZE_PASS_END(GlobalOpt, "globalopt", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 96 | "Global Variable Optimizer", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 98 | ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); } |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 99 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 100 | namespace { |
| 101 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 102 | /// GlobalStatus - As we analyze each global, keep track of some information |
| 103 | /// about it. If we find out that the address of the global is taken, none of |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 104 | /// this info will be accurate. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 105 | struct GlobalStatus { |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 106 | /// isCompared - True if the global's address is used in a comparison. |
| 107 | bool isCompared; |
| 108 | |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 109 | /// isLoaded - True if the global is ever loaded. If the global isn't ever |
| 110 | /// loaded it can be deleted. |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 111 | bool isLoaded; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 112 | |
| 113 | /// StoredType - Keep track of what stores to the global look like. |
| 114 | /// |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 115 | enum StoredType { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 116 | /// NotStored - There is no store to this global. It can thus be marked |
| 117 | /// constant. |
| 118 | NotStored, |
| 119 | |
| 120 | /// isInitializerStored - This global is stored to, but the only thing |
| 121 | /// stored is the constant it was initialized with. This is only tracked |
| 122 | /// for scalar globals. |
| 123 | isInitializerStored, |
| 124 | |
| 125 | /// isStoredOnce - This global is stored to, but only its initializer and |
| 126 | /// one other value is ever stored to it. If this global isStoredOnce, we |
| 127 | /// track the value stored to it in StoredOnceValue below. This is only |
| 128 | /// tracked for scalar globals. |
| 129 | isStoredOnce, |
| 130 | |
| 131 | /// isStored - This global is stored to by multiple values or something else |
| 132 | /// that we cannot track. |
| 133 | isStored |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 134 | } StoredType; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 135 | |
| 136 | /// StoredOnceValue - If only one value (besides the initializer constant) is |
| 137 | /// ever stored to this global, keep track of what value it is. |
| 138 | Value *StoredOnceValue; |
| 139 | |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 140 | /// AccessingFunction/HasMultipleAccessingFunctions - These start out |
| 141 | /// null/false. When the first accessing function is noticed, it is recorded. |
| 142 | /// When a second different accessing function is noticed, |
| 143 | /// HasMultipleAccessingFunctions is set to true. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 144 | const Function *AccessingFunction; |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 145 | bool HasMultipleAccessingFunctions; |
| 146 | |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 147 | /// HasNonInstructionUser - Set to true if this global has a user that is not |
| 148 | /// an instruction (e.g. a constant expr or GV initializer). |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 149 | bool HasNonInstructionUser; |
| 150 | |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 151 | /// HasPHIUser - Set to true if this global has a user that is a PHI node. |
| 152 | bool HasPHIUser; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 153 | |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 154 | /// AtomicOrdering - Set to the strongest atomic ordering requirement. |
| 155 | AtomicOrdering Ordering; |
| 156 | |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 157 | GlobalStatus() : isCompared(false), isLoaded(false), StoredType(NotStored), |
| 158 | StoredOnceValue(0), AccessingFunction(0), |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 159 | HasMultipleAccessingFunctions(false), |
| 160 | HasNonInstructionUser(false), HasPHIUser(false), |
| 161 | Ordering(NotAtomic) {} |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 162 | }; |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 163 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 165 | |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 166 | /// StrongerOrdering - Return the stronger of the two ordering. If the two |
| 167 | /// orderings are acquire and release, then return AcquireRelease. |
| 168 | /// |
| 169 | static AtomicOrdering StrongerOrdering(AtomicOrdering X, AtomicOrdering Y) { |
| 170 | if (X == Acquire && Y == Release) return AcquireRelease; |
| 171 | if (Y == Acquire && X == Release) return AcquireRelease; |
| 172 | return (AtomicOrdering)std::max(X, Y); |
| 173 | } |
| 174 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 175 | /// SafeToDestroyConstant - It is safe to destroy a constant iff it is only used |
Nick Lewycky | bc384a1 | 2012-02-05 19:48:37 +0000 | [diff] [blame] | 176 | /// by constants itself. Note that constants cannot be cyclic, so this test is |
| 177 | /// pretty easy to implement recursively. |
| 178 | /// |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 179 | static bool SafeToDestroyConstant(const Constant *C) { |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 180 | if (isa<GlobalValue>(C)) return false; |
| 181 | |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 182 | for (Value::const_use_iterator UI = C->use_begin(), E = C->use_end(); UI != E; |
| 183 | ++UI) |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 184 | if (const Constant *CU = dyn_cast<Constant>(*UI)) { |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 185 | if (!SafeToDestroyConstant(CU)) return false; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 186 | } else |
| 187 | return false; |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 192 | /// AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus |
| 193 | /// structure. If the global has its address taken, return true to indicate we |
| 194 | /// can't do anything with it. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 195 | /// |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 196 | static bool AnalyzeGlobal(const Value *V, GlobalStatus &GS, |
| 197 | SmallPtrSet<const PHINode*, 16> &PHIUsers) { |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 198 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; |
Gabor Greif | e664267 | 2010-07-09 16:51:20 +0000 | [diff] [blame] | 199 | ++UI) { |
| 200 | const User *U = *UI; |
| 201 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 202 | GS.HasNonInstructionUser = true; |
Chris Lattner | d91ed10 | 2011-01-01 22:31:46 +0000 | [diff] [blame] | 203 | |
| 204 | // If the result of the constantexpr isn't pointer type, then we won't |
| 205 | // know to expect it in various places. Just reject early. |
| 206 | if (!isa<PointerType>(CE->getType())) return true; |
| 207 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 208 | if (AnalyzeGlobal(CE, GS, PHIUsers)) return true; |
Gabor Greif | e664267 | 2010-07-09 16:51:20 +0000 | [diff] [blame] | 209 | } else if (const Instruction *I = dyn_cast<Instruction>(U)) { |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 210 | if (!GS.HasMultipleAccessingFunctions) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 211 | const Function *F = I->getParent()->getParent(); |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 212 | if (GS.AccessingFunction == 0) |
| 213 | GS.AccessingFunction = F; |
| 214 | else if (GS.AccessingFunction != F) |
| 215 | GS.HasMultipleAccessingFunctions = true; |
| 216 | } |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 217 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 218 | GS.isLoaded = true; |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 219 | // Don't hack on volatile loads. |
| 220 | if (LI->isVolatile()) return true; |
| 221 | GS.Ordering = StrongerOrdering(GS.Ordering, LI->getOrdering()); |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 222 | } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | 3602549 | 2004-10-07 06:01:25 +0000 | [diff] [blame] | 223 | // Don't allow a store OF the address, only stores TO the address. |
| 224 | if (SI->getOperand(0) == V) return true; |
| 225 | |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 226 | // Don't hack on volatile stores. |
| 227 | if (SI->isVolatile()) return true; |
Hans Wennborg | 1839858 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 228 | |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 229 | GS.Ordering = StrongerOrdering(GS.Ordering, SI->getOrdering()); |
Chris Lattner | c69d3c9 | 2008-01-29 19:01:37 +0000 | [diff] [blame] | 230 | |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 231 | // If this is a direct store to the global (i.e., the global is a scalar |
| 232 | // value, not an aggregate), keep more specific information about |
| 233 | // stores. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 234 | if (GS.StoredType != GlobalStatus::isStored) { |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 235 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>( |
| 236 | SI->getOperand(1))) { |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 237 | Value *StoredVal = SI->getOperand(0); |
Hans Wennborg | 1839858 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 238 | |
| 239 | if (Constant *C = dyn_cast<Constant>(StoredVal)) { |
| 240 | if (C->isThreadDependent()) { |
| 241 | // The stored value changes between threads; don't track it. |
| 242 | return true; |
| 243 | } |
| 244 | } |
| 245 | |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 246 | if (StoredVal == GV->getInitializer()) { |
| 247 | if (GS.StoredType < GlobalStatus::isInitializerStored) |
| 248 | GS.StoredType = GlobalStatus::isInitializerStored; |
| 249 | } else if (isa<LoadInst>(StoredVal) && |
| 250 | cast<LoadInst>(StoredVal)->getOperand(0) == GV) { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 251 | if (GS.StoredType < GlobalStatus::isInitializerStored) |
| 252 | GS.StoredType = GlobalStatus::isInitializerStored; |
| 253 | } else if (GS.StoredType < GlobalStatus::isStoredOnce) { |
| 254 | GS.StoredType = GlobalStatus::isStoredOnce; |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 255 | GS.StoredOnceValue = StoredVal; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 256 | } else if (GS.StoredType == GlobalStatus::isStoredOnce && |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 257 | GS.StoredOnceValue == StoredVal) { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 258 | // noop. |
| 259 | } else { |
| 260 | GS.StoredType = GlobalStatus::isStored; |
| 261 | } |
| 262 | } else { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 263 | GS.StoredType = GlobalStatus::isStored; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 264 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 265 | } |
Duncan Sands | b2fe7f1 | 2012-07-02 18:55:39 +0000 | [diff] [blame] | 266 | } else if (isa<BitCastInst>(I)) { |
| 267 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 268 | } else if (isa<GetElementPtrInst>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 269 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 270 | } else if (isa<SelectInst>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 271 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 272 | } else if (const PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 273 | // PHI nodes we can check just like select or GEP instructions, but we |
| 274 | // have to be careful about infinite recursion. |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 275 | if (PHIUsers.insert(PN)) // Not already visited. |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 276 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 277 | GS.HasPHIUser = true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 278 | } else if (isa<CmpInst>(I)) { |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 279 | GS.isCompared = true; |
Nick Lewycky | 1f237b0 | 2011-05-29 18:41:56 +0000 | [diff] [blame] | 280 | } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) { |
| 281 | if (MTI->isVolatile()) return true; |
Gabor Greif | 9e4f243 | 2010-06-24 14:42:01 +0000 | [diff] [blame] | 282 | if (MTI->getArgOperand(0) == V) |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 283 | GS.StoredType = GlobalStatus::isStored; |
Gabor Greif | 9e4f243 | 2010-06-24 14:42:01 +0000 | [diff] [blame] | 284 | if (MTI->getArgOperand(1) == V) |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 285 | GS.isLoaded = true; |
Nick Lewycky | 1f237b0 | 2011-05-29 18:41:56 +0000 | [diff] [blame] | 286 | } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) { |
| 287 | assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!"); |
| 288 | if (MSI->isVolatile()) return true; |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 289 | GS.StoredType = GlobalStatus::isStored; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 290 | } else { |
| 291 | return true; // Any other non-load instruction might take address! |
Chris Lattner | 9ce3000 | 2004-07-20 03:58:07 +0000 | [diff] [blame] | 292 | } |
Gabor Greif | e664267 | 2010-07-09 16:51:20 +0000 | [diff] [blame] | 293 | } else if (const Constant *C = dyn_cast<Constant>(U)) { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 294 | GS.HasNonInstructionUser = true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 295 | // We might have a dead and dangling constant hanging off of here. |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 296 | if (!SafeToDestroyConstant(C)) |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 297 | return true; |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 298 | } else { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 299 | GS.HasNonInstructionUser = true; |
| 300 | // Otherwise must be some other user. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 301 | return true; |
| 302 | } |
Gabor Greif | e664267 | 2010-07-09 16:51:20 +0000 | [diff] [blame] | 303 | } |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 308 | /// isLeakCheckerRoot - Is this global variable possibly used by a leak checker |
| 309 | /// as a root? If so, we might not really want to eliminate the stores to it. |
| 310 | static bool isLeakCheckerRoot(GlobalVariable *GV) { |
| 311 | // A global variable is a root if it is a pointer, or could plausibly contain |
| 312 | // a pointer. There are two challenges; one is that we could have a struct |
| 313 | // the has an inner member which is a pointer. We recurse through the type to |
| 314 | // detect these (up to a point). The other is that we may actually be a union |
| 315 | // of a pointer and another type, and so our LLVM type is an integer which |
| 316 | // gets converted into a pointer, or our type is an [i8 x #] with a pointer |
| 317 | // potentially contained here. |
| 318 | |
| 319 | if (GV->hasPrivateLinkage()) |
| 320 | return false; |
| 321 | |
| 322 | SmallVector<Type *, 4> Types; |
| 323 | Types.push_back(cast<PointerType>(GV->getType())->getElementType()); |
| 324 | |
| 325 | unsigned Limit = 20; |
| 326 | do { |
| 327 | Type *Ty = Types.pop_back_val(); |
| 328 | switch (Ty->getTypeID()) { |
| 329 | default: break; |
| 330 | case Type::PointerTyID: return true; |
| 331 | case Type::ArrayTyID: |
| 332 | case Type::VectorTyID: { |
| 333 | SequentialType *STy = cast<SequentialType>(Ty); |
| 334 | Types.push_back(STy->getElementType()); |
| 335 | break; |
| 336 | } |
| 337 | case Type::StructTyID: { |
| 338 | StructType *STy = cast<StructType>(Ty); |
| 339 | if (STy->isOpaque()) return true; |
| 340 | for (StructType::element_iterator I = STy->element_begin(), |
| 341 | E = STy->element_end(); I != E; ++I) { |
| 342 | Type *InnerTy = *I; |
| 343 | if (isa<PointerType>(InnerTy)) return true; |
| 344 | if (isa<CompositeType>(InnerTy)) |
| 345 | Types.push_back(InnerTy); |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | if (--Limit == 0) return true; |
| 351 | } while (!Types.empty()); |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | /// Given a value that is stored to a global but never read, determine whether |
| 356 | /// it's safe to remove the store and the chain of computation that feeds the |
| 357 | /// store. |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 358 | static bool IsSafeComputationToRemove(Value *V, const TargetLibraryInfo *TLI) { |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 359 | do { |
| 360 | if (isa<Constant>(V)) |
| 361 | return true; |
| 362 | if (!V->hasOneUse()) |
| 363 | return false; |
Nick Lewycky | b8cd66b | 2012-07-25 21:19:40 +0000 | [diff] [blame] | 364 | if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) || |
| 365 | isa<GlobalValue>(V)) |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 366 | return false; |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 367 | if (isAllocationFn(V, TLI)) |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 368 | return true; |
| 369 | |
| 370 | Instruction *I = cast<Instruction>(V); |
| 371 | if (I->mayHaveSideEffects()) |
| 372 | return false; |
| 373 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 374 | if (!GEP->hasAllConstantIndices()) |
| 375 | return false; |
| 376 | } else if (I->getNumOperands() != 1) { |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | V = I->getOperand(0); |
| 381 | } while (1); |
| 382 | } |
| 383 | |
| 384 | /// CleanupPointerRootUsers - This GV is a pointer root. Loop over all users |
| 385 | /// of the global and clean up any that obviously don't assign the global a |
| 386 | /// value that isn't dynamically allocated. |
| 387 | /// |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 388 | static bool CleanupPointerRootUsers(GlobalVariable *GV, |
| 389 | const TargetLibraryInfo *TLI) { |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 390 | // A brief explanation of leak checkers. The goal is to find bugs where |
| 391 | // pointers are forgotten, causing an accumulating growth in memory |
| 392 | // usage over time. The common strategy for leak checkers is to whitelist the |
| 393 | // memory pointed to by globals at exit. This is popular because it also |
| 394 | // solves another problem where the main thread of a C++ program may shut down |
| 395 | // before other threads that are still expecting to use those globals. To |
| 396 | // handle that case, we expect the program may create a singleton and never |
| 397 | // destroy it. |
| 398 | |
| 399 | bool Changed = false; |
| 400 | |
| 401 | // If Dead[n].first is the only use of a malloc result, we can delete its |
| 402 | // chain of computation and the store to the global in Dead[n].second. |
| 403 | SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead; |
| 404 | |
| 405 | // Constants can't be pointers to dynamically allocated memory. |
| 406 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); |
| 407 | UI != E;) { |
| 408 | User *U = *UI++; |
| 409 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 410 | Value *V = SI->getValueOperand(); |
| 411 | if (isa<Constant>(V)) { |
| 412 | Changed = true; |
| 413 | SI->eraseFromParent(); |
| 414 | } else if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 415 | if (I->hasOneUse()) |
| 416 | Dead.push_back(std::make_pair(I, SI)); |
| 417 | } |
| 418 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) { |
| 419 | if (isa<Constant>(MSI->getValue())) { |
| 420 | Changed = true; |
| 421 | MSI->eraseFromParent(); |
| 422 | } else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) { |
| 423 | if (I->hasOneUse()) |
| 424 | Dead.push_back(std::make_pair(I, MSI)); |
| 425 | } |
| 426 | } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) { |
| 427 | GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource()); |
| 428 | if (MemSrc && MemSrc->isConstant()) { |
| 429 | Changed = true; |
| 430 | MTI->eraseFromParent(); |
| 431 | } else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) { |
| 432 | if (I->hasOneUse()) |
| 433 | Dead.push_back(std::make_pair(I, MTI)); |
| 434 | } |
| 435 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
| 436 | if (CE->use_empty()) { |
| 437 | CE->destroyConstant(); |
| 438 | Changed = true; |
| 439 | } |
| 440 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
| 441 | if (SafeToDestroyConstant(C)) { |
| 442 | C->destroyConstant(); |
| 443 | // This could have invalidated UI, start over from scratch. |
| 444 | Dead.clear(); |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 445 | CleanupPointerRootUsers(GV, TLI); |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 446 | return true; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | for (int i = 0, e = Dead.size(); i != e; ++i) { |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 452 | if (IsSafeComputationToRemove(Dead[i].first, TLI)) { |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 453 | Dead[i].second->eraseFromParent(); |
| 454 | Instruction *I = Dead[i].first; |
| 455 | do { |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 456 | if (isAllocationFn(I, TLI)) |
Nick Lewycky | 952f5d5 | 2012-07-24 21:33:00 +0000 | [diff] [blame] | 457 | break; |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 458 | Instruction *J = dyn_cast<Instruction>(I->getOperand(0)); |
| 459 | if (!J) |
| 460 | break; |
| 461 | I->eraseFromParent(); |
| 462 | I = J; |
Nick Lewycky | 952f5d5 | 2012-07-24 21:33:00 +0000 | [diff] [blame] | 463 | } while (1); |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 464 | I->eraseFromParent(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return Changed; |
| 469 | } |
| 470 | |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 471 | /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all |
| 472 | /// users of the global, cleaning up the obvious ones. This is largely just a |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 473 | /// quick scan over the use list to clean up the easy and obvious cruft. This |
| 474 | /// returns true if it made a change. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 475 | static bool CleanupConstantGlobalUsers(Value *V, Constant *Init, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 476 | DataLayout *TD, TargetLibraryInfo *TLI) { |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 477 | bool Changed = false; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 478 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) { |
| 479 | User *U = *UI++; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 481 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 482 | if (Init) { |
| 483 | // Replace the load with the initializer. |
| 484 | LI->replaceAllUsesWith(Init); |
| 485 | LI->eraseFromParent(); |
| 486 | Changed = true; |
| 487 | } |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 488 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 489 | // Store must be unreachable or storing Init into the global. |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 490 | SI->eraseFromParent(); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 491 | Changed = true; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 492 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
| 493 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | aae4a1c | 2005-09-26 07:34:35 +0000 | [diff] [blame] | 494 | Constant *SubInit = 0; |
| 495 | if (Init) |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 496 | SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 497 | Changed |= CleanupConstantGlobalUsers(CE, SubInit, TD, TLI); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 498 | } else if (CE->getOpcode() == Instruction::BitCast && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 499 | CE->getType()->isPointerTy()) { |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 500 | // Pointer cast, delete any stores and memsets to the global. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 501 | Changed |= CleanupConstantGlobalUsers(CE, 0, TD, TLI); |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if (CE->use_empty()) { |
| 505 | CE->destroyConstant(); |
| 506 | Changed = true; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 507 | } |
| 508 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 509 | // Do not transform "gepinst (gep constexpr (GV))" here, because forming |
| 510 | // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold |
| 511 | // and will invalidate our notion of what Init is. |
Chris Lattner | 1945024 | 2007-11-13 21:46:23 +0000 | [diff] [blame] | 512 | Constant *SubInit = 0; |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 513 | if (!isa<ConstantExpr>(GEP->getOperand(0))) { |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 514 | ConstantExpr *CE = |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 515 | dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, TD, TLI)); |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 516 | if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr) |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 517 | SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); |
Benjamin Kramer | c1ea16e | 2012-03-28 14:50:09 +0000 | [diff] [blame] | 518 | |
| 519 | // If the initializer is an all-null value and we have an inbounds GEP, |
| 520 | // we already know what the result of any load from that GEP is. |
| 521 | // TODO: Handle splats. |
| 522 | if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds()) |
| 523 | SubInit = Constant::getNullValue(GEP->getType()->getElementType()); |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 524 | } |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 525 | Changed |= CleanupConstantGlobalUsers(GEP, SubInit, TD, TLI); |
Chris Lattner | c4d81b0 | 2004-10-10 16:47:33 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 527 | if (GEP->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 528 | GEP->eraseFromParent(); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 529 | Changed = true; |
| 530 | } |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 531 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv |
| 532 | if (MI->getRawDest() == V) { |
| 533 | MI->eraseFromParent(); |
| 534 | Changed = true; |
| 535 | } |
| 536 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 537 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
| 538 | // If we have a chain of dead constantexprs or other things dangling from |
| 539 | // us, and if they are all dead, nuke them without remorse. |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 540 | if (SafeToDestroyConstant(C)) { |
Devang Patel | 743cdf8 | 2009-03-06 01:37:41 +0000 | [diff] [blame] | 541 | C->destroyConstant(); |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 542 | // This could have invalidated UI, start over from scratch. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 543 | CleanupConstantGlobalUsers(V, Init, TD, TLI); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 544 | return true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 545 | } |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 548 | return Changed; |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 551 | /// isSafeSROAElementUse - Return true if the specified instruction is a safe |
| 552 | /// user of a derived expression from a global that we want to SROA. |
| 553 | static bool isSafeSROAElementUse(Value *V) { |
| 554 | // We might have a dead and dangling constant hanging off of here. |
| 555 | if (Constant *C = dyn_cast<Constant>(V)) |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 556 | return SafeToDestroyConstant(C); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 558 | Instruction *I = dyn_cast<Instruction>(V); |
| 559 | if (!I) return false; |
| 560 | |
| 561 | // Loads are ok. |
| 562 | if (isa<LoadInst>(I)) return true; |
| 563 | |
| 564 | // Stores *to* the pointer are ok. |
| 565 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 566 | return SI->getOperand(0) != V; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 568 | // Otherwise, it must be a GEP. |
| 569 | GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I); |
| 570 | if (GEPI == 0) return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 572 | if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) || |
| 573 | !cast<Constant>(GEPI->getOperand(1))->isNullValue()) |
| 574 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 576 | for (Value::use_iterator I = GEPI->use_begin(), E = GEPI->use_end(); |
| 577 | I != E; ++I) |
| 578 | if (!isSafeSROAElementUse(*I)) |
| 579 | return false; |
Chris Lattner | 727c210 | 2008-01-14 01:31:05 +0000 | [diff] [blame] | 580 | return true; |
| 581 | } |
| 582 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 583 | |
| 584 | /// IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value. |
| 585 | /// Look at it and its uses and decide whether it is safe to SROA this global. |
| 586 | /// |
| 587 | static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) { |
| 588 | // The user of the global must be a GEP Inst or a ConstantExpr GEP. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 589 | if (!isa<GetElementPtrInst>(U) && |
| 590 | (!isa<ConstantExpr>(U) || |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 591 | cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr)) |
| 592 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 594 | // Check to see if this ConstantExpr GEP is SRA'able. In particular, we |
| 595 | // don't like < 3 operand CE's, and we don't like non-constant integer |
| 596 | // indices. This enforces that all uses are 'gep GV, 0, C, ...' for some |
| 597 | // value of C. |
| 598 | if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) || |
| 599 | !cast<Constant>(U->getOperand(1))->isNullValue() || |
| 600 | !isa<ConstantInt>(U->getOperand(2))) |
| 601 | return false; |
| 602 | |
| 603 | gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U); |
| 604 | ++GEPI; // Skip over the pointer index. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 606 | // If this is a use of an array allocation, do a bit more checking for sanity. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 607 | if (ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) { |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 608 | uint64_t NumElements = AT->getNumElements(); |
| 609 | ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2)); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 611 | // Check to make sure that index falls within the array. If not, |
| 612 | // something funny is going on, so we won't do the optimization. |
| 613 | // |
| 614 | if (Idx->getZExtValue() >= NumElements) |
| 615 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 617 | // We cannot scalar repl this level of the array unless any array |
| 618 | // sub-indices are in-range constants. In particular, consider: |
| 619 | // A[0][i]. We cannot know that the user isn't doing invalid things like |
| 620 | // allowing i to index an out-of-range subscript that accesses A[1]. |
| 621 | // |
| 622 | // Scalar replacing *just* the outer index of the array is probably not |
| 623 | // going to be a win anyway, so just give up. |
| 624 | for (++GEPI; // Skip array index. |
Dan Gohman | 6874a2a | 2009-08-18 14:58:19 +0000 | [diff] [blame] | 625 | GEPI != E; |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 626 | ++GEPI) { |
| 627 | uint64_t NumElements; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 628 | if (ArrayType *SubArrayTy = dyn_cast<ArrayType>(*GEPI)) |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 629 | NumElements = SubArrayTy->getNumElements(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 630 | else if (VectorType *SubVectorTy = dyn_cast<VectorType>(*GEPI)) |
Dan Gohman | 6874a2a | 2009-08-18 14:58:19 +0000 | [diff] [blame] | 631 | NumElements = SubVectorTy->getNumElements(); |
| 632 | else { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 633 | assert((*GEPI)->isStructTy() && |
Dan Gohman | 6874a2a | 2009-08-18 14:58:19 +0000 | [diff] [blame] | 634 | "Indexed GEP type is not array, vector, or struct!"); |
| 635 | continue; |
| 636 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 638 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand()); |
| 639 | if (!IdxVal || IdxVal->getZExtValue() >= NumElements) |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | for (Value::use_iterator I = U->use_begin(), E = U->use_end(); I != E; ++I) |
| 645 | if (!isSafeSROAElementUse(*I)) |
| 646 | return false; |
| 647 | return true; |
| 648 | } |
| 649 | |
| 650 | /// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it |
| 651 | /// is safe for us to perform this transformation. |
| 652 | /// |
| 653 | static bool GlobalUsersSafeToSRA(GlobalValue *GV) { |
| 654 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); |
| 655 | UI != E; ++UI) { |
| 656 | if (!IsUserOfGlobalSafeForSRA(*UI, GV)) |
| 657 | return false; |
| 658 | } |
| 659 | return true; |
| 660 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 663 | /// SRAGlobal - Perform scalar replacement of aggregates on the specified global |
| 664 | /// variable. This opens the door for other optimizations by exposing the |
| 665 | /// behavior of the program in a more fine-grained way. We have determined that |
| 666 | /// this transformation is safe already. We return the first global variable we |
| 667 | /// insert so that the caller can reprocess it. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 668 | static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &TD) { |
Chris Lattner | 727c210 | 2008-01-14 01:31:05 +0000 | [diff] [blame] | 669 | // Make sure this global only has simple uses that we can SRA. |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 670 | if (!GlobalUsersSafeToSRA(GV)) |
Chris Lattner | 727c210 | 2008-01-14 01:31:05 +0000 | [diff] [blame] | 671 | return 0; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 672 | |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 673 | assert(GV->hasLocalLinkage() && !GV->isConstant()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 674 | Constant *Init = GV->getInitializer(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 675 | Type *Ty = Init->getType(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 677 | std::vector<GlobalVariable*> NewGlobals; |
| 678 | Module::GlobalListType &Globals = GV->getParent()->getGlobalList(); |
| 679 | |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 680 | // Get the alignment of the global, either explicit or target-specific. |
| 681 | unsigned StartAlignment = GV->getAlignment(); |
| 682 | if (StartAlignment == 0) |
| 683 | StartAlignment = TD.getABITypeAlignment(GV->getType()); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 684 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 685 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 686 | NewGlobals.reserve(STy->getNumElements()); |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 687 | const StructLayout &Layout = *TD.getStructLayout(STy); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 688 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
Chris Lattner | a1f00f4 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 689 | Constant *In = Init->getAggregateElement(i); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 690 | assert(In && "Couldn't get element of initializer?"); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 691 | GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false, |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 692 | GlobalVariable::InternalLinkage, |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 693 | In, GV->getName()+"."+Twine(i), |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 694 | GV->getThreadLocalMode(), |
Owen Anderson | 3d29df3 | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 695 | GV->getType()->getAddressSpace()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 696 | Globals.insert(GV, NGV); |
| 697 | NewGlobals.push_back(NGV); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 699 | // Calculate the known alignment of the field. If the original aggregate |
| 700 | // had 256 byte alignment for example, something might depend on that: |
| 701 | // propagate info to each field. |
| 702 | uint64_t FieldOffset = Layout.getElementOffset(i); |
| 703 | unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset); |
| 704 | if (NewAlign > TD.getABITypeAlignment(STy->getElementType(i))) |
| 705 | NGV->setAlignment(NewAlign); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 706 | } |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 707 | } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) { |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 708 | unsigned NumElements = 0; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 709 | if (ArrayType *ATy = dyn_cast<ArrayType>(STy)) |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 710 | NumElements = ATy->getNumElements(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 711 | else |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 712 | NumElements = cast<VectorType>(STy)->getNumElements(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 1f21ef1 | 2005-02-23 16:53:04 +0000 | [diff] [blame] | 714 | if (NumElements > 16 && GV->hasNUsesOrMore(16)) |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 715 | return 0; // It's not worth it. |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 716 | NewGlobals.reserve(NumElements); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 717 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 718 | uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType()); |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 719 | unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 720 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
Chris Lattner | a1f00f4 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 721 | Constant *In = Init->getAggregateElement(i); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 722 | assert(In && "Couldn't get element of initializer?"); |
| 723 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 724 | GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false, |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 725 | GlobalVariable::InternalLinkage, |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 726 | In, GV->getName()+"."+Twine(i), |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 727 | GV->getThreadLocalMode(), |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 728 | GV->getType()->getAddressSpace()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 729 | Globals.insert(GV, NGV); |
| 730 | NewGlobals.push_back(NGV); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 732 | // Calculate the known alignment of the field. If the original aggregate |
| 733 | // had 256 byte alignment for example, something might depend on that: |
| 734 | // propagate info to each field. |
| 735 | unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i); |
| 736 | if (NewAlign > EltAlign) |
| 737 | NGV->setAlignment(NewAlign); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | if (NewGlobals.empty()) |
| 742 | return 0; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 743 | |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 744 | DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 746 | Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext())); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 747 | |
| 748 | // Loop over all of the uses of the global, replacing the constantexpr geps, |
| 749 | // with smaller constantexpr geps or direct references. |
| 750 | while (!GV->use_empty()) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 751 | User *GEP = GV->use_back(); |
| 752 | assert(((isa<ConstantExpr>(GEP) && |
| 753 | cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)|| |
| 754 | isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 756 | // Ignore the 1th operand, which has to be zero or else the program is quite |
| 757 | // broken (undefined). Get the 2nd operand, which is the structure or array |
| 758 | // index. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 759 | unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 760 | if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access. |
| 761 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 762 | Value *NewPtr = NewGlobals[Val]; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 763 | |
| 764 | // Form a shorter GEP if needed. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 765 | if (GEP->getNumOperands() > 3) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 766 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 767 | SmallVector<Constant*, 8> Idxs; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 768 | Idxs.push_back(NullInt); |
| 769 | for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i) |
| 770 | Idxs.push_back(CE->getOperand(i)); |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 771 | NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 772 | } else { |
| 773 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP); |
Chris Lattner | 699d144 | 2007-01-31 19:59:55 +0000 | [diff] [blame] | 774 | SmallVector<Value*, 8> Idxs; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 775 | Idxs.push_back(NullInt); |
| 776 | for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) |
| 777 | Idxs.push_back(GEPI->getOperand(i)); |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 778 | NewPtr = GetElementPtrInst::Create(NewPtr, Idxs, |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 779 | GEPI->getName()+"."+Twine(Val),GEPI); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 780 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 781 | } |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 782 | GEP->replaceAllUsesWith(NewPtr); |
| 783 | |
| 784 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP)) |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 785 | GEPI->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 786 | else |
| 787 | cast<ConstantExpr>(GEP)->destroyConstant(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Chris Lattner | e40e2d1 | 2004-10-08 20:25:55 +0000 | [diff] [blame] | 790 | // Delete the old global, now that it is dead. |
| 791 | Globals.erase(GV); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 792 | ++NumSRA; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 793 | |
| 794 | // Loop over the new globals array deleting any globals that are obviously |
| 795 | // dead. This can arise due to scalarization of a structure or an array that |
| 796 | // has elements that are dead. |
| 797 | unsigned FirstGlobal = 0; |
| 798 | for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i) |
| 799 | if (NewGlobals[i]->use_empty()) { |
| 800 | Globals.erase(NewGlobals[i]); |
| 801 | if (FirstGlobal == i) ++FirstGlobal; |
| 802 | } |
| 803 | |
| 804 | return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 807 | /// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 808 | /// value will trap if the value is dynamically null. PHIs keeps track of any |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 809 | /// phi nodes we've seen to avoid reprocessing them. |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 810 | static bool AllUsesOfValueWillTrapIfNull(const Value *V, |
| 811 | SmallPtrSet<const PHINode*, 8> &PHIs) { |
| 812 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 813 | ++UI) { |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 814 | const User *U = *UI; |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 815 | |
| 816 | if (isa<LoadInst>(U)) { |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 817 | // Will trap. |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 818 | } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 819 | if (SI->getOperand(0) == V) { |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 820 | //cerr << "NONTRAPPING USE: " << *U; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 821 | return false; // Storing the value. |
| 822 | } |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 823 | } else if (const CallInst *CI = dyn_cast<CallInst>(U)) { |
Gabor Greif | 654c06f | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 824 | if (CI->getCalledValue() != V) { |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 825 | //cerr << "NONTRAPPING USE: " << *U; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 826 | return false; // Not calling the ptr |
| 827 | } |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 828 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) { |
Gabor Greif | 654c06f | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 829 | if (II->getCalledValue() != V) { |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 830 | //cerr << "NONTRAPPING USE: " << *U; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 831 | return false; // Not calling the ptr |
| 832 | } |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 833 | } else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) { |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 834 | if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false; |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 835 | } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) { |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 836 | if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false; |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 837 | } else if (const PHINode *PN = dyn_cast<PHINode>(U)) { |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 838 | // If we've already seen this phi node, ignore it, it has already been |
| 839 | // checked. |
Jakob Stoklund Olesen | b489d0f | 2010-01-29 23:54:14 +0000 | [diff] [blame] | 840 | if (PHIs.insert(PN) && !AllUsesOfValueWillTrapIfNull(PN, PHIs)) |
| 841 | return false; |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 842 | } else if (isa<ICmpInst>(U) && |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 843 | isa<ConstantPointerNull>(UI->getOperand(1))) { |
Nick Lewycky | e7ee59b | 2010-02-25 06:39:10 +0000 | [diff] [blame] | 844 | // Ignore icmp X, null |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 845 | } else { |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 846 | //cerr << "NONTRAPPING USE: " << *U; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 847 | return false; |
| 848 | } |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 849 | } |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 850 | return true; |
| 851 | } |
| 852 | |
| 853 | /// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 854 | /// from GV will trap if the loaded value is null. Note that this also permits |
| 855 | /// comparisons of the loaded value against null, as a special case. |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 856 | static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) { |
| 857 | for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end(); |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 858 | UI != E; ++UI) { |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 859 | const User *U = *UI; |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 860 | |
Gabor Greif | 6ce02b5 | 2010-04-06 19:24:18 +0000 | [diff] [blame] | 861 | if (const LoadInst *LI = dyn_cast<LoadInst>(U)) { |
| 862 | SmallPtrSet<const PHINode*, 8> PHIs; |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 863 | if (!AllUsesOfValueWillTrapIfNull(LI, PHIs)) |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 864 | return false; |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 865 | } else if (isa<StoreInst>(U)) { |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 866 | // Ignore stores to the global. |
| 867 | } else { |
| 868 | // We don't know or understand this user, bail out. |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 869 | //cerr << "UNKNOWN USER OF GLOBAL!: " << *U; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 870 | return false; |
| 871 | } |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 872 | } |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 873 | return true; |
| 874 | } |
| 875 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 876 | static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 877 | bool Changed = false; |
| 878 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) { |
| 879 | Instruction *I = cast<Instruction>(*UI++); |
| 880 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 881 | LI->setOperand(0, NewV); |
| 882 | Changed = true; |
| 883 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 884 | if (SI->getOperand(1) == V) { |
| 885 | SI->setOperand(1, NewV); |
| 886 | Changed = true; |
| 887 | } |
| 888 | } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
Gabor Greif | fa1f5c2 | 2010-04-06 18:45:08 +0000 | [diff] [blame] | 889 | CallSite CS(I); |
| 890 | if (CS.getCalledValue() == V) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 891 | // Calling through the pointer! Turn into a direct call, but be careful |
| 892 | // that the pointer is not also being passed as an argument. |
Gabor Greif | fa1f5c2 | 2010-04-06 18:45:08 +0000 | [diff] [blame] | 893 | CS.setCalledFunction(NewV); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 894 | Changed = true; |
| 895 | bool PassedAsArg = false; |
Gabor Greif | fa1f5c2 | 2010-04-06 18:45:08 +0000 | [diff] [blame] | 896 | for (unsigned i = 0, e = CS.arg_size(); i != e; ++i) |
| 897 | if (CS.getArgument(i) == V) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 898 | PassedAsArg = true; |
Gabor Greif | fa1f5c2 | 2010-04-06 18:45:08 +0000 | [diff] [blame] | 899 | CS.setArgument(i, NewV); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | if (PassedAsArg) { |
| 903 | // Being passed as an argument also. Be careful to not invalidate UI! |
| 904 | UI = V->use_begin(); |
| 905 | } |
| 906 | } |
| 907 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 908 | Changed |= OptimizeAwayTrappingUsesOfValue(CI, |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 909 | ConstantExpr::getCast(CI->getOpcode(), |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 910 | NewV, CI->getType())); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 911 | if (CI->use_empty()) { |
| 912 | Changed = true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 913 | CI->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 914 | } |
| 915 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
| 916 | // Should handle GEP here. |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 917 | SmallVector<Constant*, 8> Idxs; |
| 918 | Idxs.reserve(GEPI->getNumOperands()-1); |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 919 | for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end(); |
| 920 | i != e; ++i) |
| 921 | if (Constant *C = dyn_cast<Constant>(*i)) |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 922 | Idxs.push_back(C); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 923 | else |
| 924 | break; |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 925 | if (Idxs.size() == GEPI->getNumOperands()-1) |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 926 | Changed |= OptimizeAwayTrappingUsesOfValue(GEPI, |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 927 | ConstantExpr::getGetElementPtr(NewV, Idxs)); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 928 | if (GEPI->use_empty()) { |
| 929 | Changed = true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 930 | GEPI->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return Changed; |
| 936 | } |
| 937 | |
| 938 | |
| 939 | /// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null |
| 940 | /// value stored into it. If there are uses of the loaded value that would trap |
| 941 | /// if the loaded value is dynamically null, then we know that they cannot be |
| 942 | /// reachable with a null optimize away the load. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 943 | static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 944 | DataLayout *TD, |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 945 | TargetLibraryInfo *TLI) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 946 | bool Changed = false; |
| 947 | |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 948 | // Keep track of whether we are able to remove all the uses of the global |
| 949 | // other than the store that defines it. |
| 950 | bool AllNonStoreUsesGone = true; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 952 | // Replace all uses of loads with uses of uses of the stored value. |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 953 | for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){ |
| 954 | User *GlobalUser = *GUI++; |
| 955 | if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) { |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 956 | Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV); |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 957 | // If we were able to delete all uses of the loads |
| 958 | if (LI->use_empty()) { |
| 959 | LI->eraseFromParent(); |
| 960 | Changed = true; |
| 961 | } else { |
| 962 | AllNonStoreUsesGone = false; |
| 963 | } |
| 964 | } else if (isa<StoreInst>(GlobalUser)) { |
| 965 | // Ignore the store that stores "LV" to the global. |
| 966 | assert(GlobalUser->getOperand(1) == GV && |
| 967 | "Must be storing *to* the global"); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 968 | } else { |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 969 | AllNonStoreUsesGone = false; |
| 970 | |
| 971 | // If we get here we could have other crazy uses that are transitively |
| 972 | // loaded. |
| 973 | assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) || |
Benjamin Kramer | ab16423 | 2012-09-28 10:01:27 +0000 | [diff] [blame] | 974 | isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) || |
| 975 | isa<BitCastInst>(GlobalUser) || |
| 976 | isa<GetElementPtrInst>(GlobalUser)) && |
Chris Lattner | 98a42b2 | 2011-05-22 07:15:13 +0000 | [diff] [blame] | 977 | "Only expect load and stores!"); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 978 | } |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 979 | } |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 980 | |
| 981 | if (Changed) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 982 | DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 983 | ++NumGlobUses; |
| 984 | } |
| 985 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 986 | // If we nuked all of the loads, then none of the stores are needed either, |
| 987 | // nor is the global. |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 988 | if (AllNonStoreUsesGone) { |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 989 | if (isLeakCheckerRoot(GV)) { |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 990 | Changed |= CleanupPointerRootUsers(GV, TLI); |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 991 | } else { |
| 992 | Changed = true; |
| 993 | CleanupConstantGlobalUsers(GV, 0, TD, TLI); |
| 994 | } |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 995 | if (GV->use_empty()) { |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 996 | DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n"); |
| 997 | Changed = true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 998 | GV->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 999 | ++NumDeleted; |
| 1000 | } |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1001 | } |
| 1002 | return Changed; |
| 1003 | } |
| 1004 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1005 | /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the |
| 1006 | /// instructions that are foldable. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1007 | static void ConstantPropUsersOf(Value *V, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1008 | DataLayout *TD, TargetLibraryInfo *TLI) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1009 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) |
| 1010 | if (Instruction *I = dyn_cast<Instruction>(*UI++)) |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1011 | if (Constant *NewC = ConstantFoldInstruction(I, TD, TLI)) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1012 | I->replaceAllUsesWith(NewC); |
| 1013 | |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 1014 | // Advance UI to the next non-I use to avoid invalidating it! |
| 1015 | // Instructions could multiply use V. |
| 1016 | while (UI != E && *UI == I) |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1017 | ++UI; |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 1018 | I->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /// OptimizeGlobalAddressOfMalloc - This function takes the specified global |
| 1023 | /// variable, and transforms the program as if it always contained the result of |
| 1024 | /// the specified malloc. Because it is always the result of the specified |
| 1025 | /// malloc, there is no reason to actually DO the malloc. Instead, turn the |
Chris Lattner | 6e8fbad | 2006-11-30 17:32:29 +0000 | [diff] [blame] | 1026 | /// malloc into a global, and any loads of GV as uses of the new global. |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1027 | static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1028 | CallInst *CI, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1029 | Type *AllocTy, |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1030 | ConstantInt *NElements, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1031 | DataLayout *TD, |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1032 | TargetLibraryInfo *TLI) { |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1033 | DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n'); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1035 | Type *GlobalType; |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1036 | if (NElements->getZExtValue() == 1) |
| 1037 | GlobalType = AllocTy; |
| 1038 | else |
| 1039 | // If we have an array allocation, the global variable is of an array. |
| 1040 | GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue()); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Create the new global variable. The contents of the malloc'd memory is |
| 1043 | // undefined, so initialize with an undef value. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1044 | GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(), |
Chris Lattner | e9fd444 | 2010-02-26 23:42:13 +0000 | [diff] [blame] | 1045 | GlobalType, false, |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1046 | GlobalValue::InternalLinkage, |
Chris Lattner | e9fd444 | 2010-02-26 23:42:13 +0000 | [diff] [blame] | 1047 | UndefValue::get(GlobalType), |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1048 | GV->getName()+".body", |
| 1049 | GV, |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1050 | GV->getThreadLocalMode()); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1052 | // If there are bitcast users of the malloc (which is typical, usually we have |
| 1053 | // a malloc + bitcast) then replace them with uses of the new global. Update |
| 1054 | // other users to use the global as well. |
| 1055 | BitCastInst *TheBC = 0; |
| 1056 | while (!CI->use_empty()) { |
| 1057 | Instruction *User = cast<Instruction>(CI->use_back()); |
| 1058 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { |
| 1059 | if (BCI->getType() == NewGV->getType()) { |
| 1060 | BCI->replaceAllUsesWith(NewGV); |
| 1061 | BCI->eraseFromParent(); |
| 1062 | } else { |
| 1063 | BCI->setOperand(0, NewGV); |
| 1064 | } |
| 1065 | } else { |
| 1066 | if (TheBC == 0) |
| 1067 | TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI); |
| 1068 | User->replaceUsesOfWith(CI, TheBC); |
| 1069 | } |
| 1070 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1071 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1072 | Constant *RepValue = NewGV; |
| 1073 | if (NewGV->getType() != GV->getType()->getElementType()) |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1074 | RepValue = ConstantExpr::getBitCast(RepValue, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1075 | GV->getType()->getElementType()); |
| 1076 | |
| 1077 | // If there is a comparison against null, we will insert a global bool to |
| 1078 | // keep track of whether the global was initialized yet or not. |
| 1079 | GlobalVariable *InitBool = |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1080 | new GlobalVariable(Type::getInt1Ty(GV->getContext()), false, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1081 | GlobalValue::InternalLinkage, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1082 | ConstantInt::getFalse(GV->getContext()), |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1083 | GV->getName()+".init", GV->getThreadLocalMode()); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1084 | bool InitBoolUsed = false; |
| 1085 | |
| 1086 | // Loop over all uses of GV, processing them in turn. |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1087 | while (!GV->use_empty()) { |
| 1088 | if (StoreInst *SI = dyn_cast<StoreInst>(GV->use_back())) { |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1089 | // The global is initialized when the store to it occurs. |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1090 | new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0, |
| 1091 | SI->getOrdering(), SI->getSynchScope(), SI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1092 | SI->eraseFromParent(); |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1093 | continue; |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1094 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1095 | |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1096 | LoadInst *LI = cast<LoadInst>(GV->use_back()); |
| 1097 | while (!LI->use_empty()) { |
| 1098 | Use &LoadUse = LI->use_begin().getUse(); |
| 1099 | if (!isa<ICmpInst>(LoadUse.getUser())) { |
| 1100 | LoadUse = RepValue; |
| 1101 | continue; |
| 1102 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1104 | ICmpInst *ICI = cast<ICmpInst>(LoadUse.getUser()); |
| 1105 | // Replace the cmp X, 0 with a use of the bool value. |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1106 | // Sink the load to where the compare was, if atomic rules allow us to. |
| 1107 | Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0, |
| 1108 | LI->getOrdering(), LI->getSynchScope(), |
| 1109 | LI->isUnordered() ? (Instruction*)ICI : LI); |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1110 | InitBoolUsed = true; |
| 1111 | switch (ICI->getPredicate()) { |
| 1112 | default: llvm_unreachable("Unknown ICmp Predicate!"); |
| 1113 | case ICmpInst::ICMP_ULT: |
| 1114 | case ICmpInst::ICMP_SLT: // X < null -> always false |
| 1115 | LV = ConstantInt::getFalse(GV->getContext()); |
| 1116 | break; |
| 1117 | case ICmpInst::ICMP_ULE: |
| 1118 | case ICmpInst::ICMP_SLE: |
| 1119 | case ICmpInst::ICMP_EQ: |
| 1120 | LV = BinaryOperator::CreateNot(LV, "notinit", ICI); |
| 1121 | break; |
| 1122 | case ICmpInst::ICMP_NE: |
| 1123 | case ICmpInst::ICMP_UGE: |
| 1124 | case ICmpInst::ICMP_SGE: |
| 1125 | case ICmpInst::ICMP_UGT: |
| 1126 | case ICmpInst::ICMP_SGT: |
| 1127 | break; // no change. |
| 1128 | } |
| 1129 | ICI->replaceAllUsesWith(LV); |
| 1130 | ICI->eraseFromParent(); |
| 1131 | } |
| 1132 | LI->eraseFromParent(); |
| 1133 | } |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1134 | |
| 1135 | // If the initialization boolean was used, insert it, otherwise delete it. |
| 1136 | if (!InitBoolUsed) { |
| 1137 | while (!InitBool->use_empty()) // Delete initializations |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1138 | cast<StoreInst>(InitBool->use_back())->eraseFromParent(); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1139 | delete InitBool; |
| 1140 | } else |
| 1141 | GV->getParent()->getGlobalList().insert(GV, InitBool); |
| 1142 | |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1143 | // Now the GV is dead, nuke it and the malloc.. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1144 | GV->eraseFromParent(); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1145 | CI->eraseFromParent(); |
| 1146 | |
| 1147 | // To further other optimizations, loop over all users of NewGV and try to |
| 1148 | // constant prop them. This will promote GEP instructions with constant |
| 1149 | // indices into GEP constant-exprs, which will allow global-opt to hack on it. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1150 | ConstantPropUsersOf(NewGV, TD, TLI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1151 | if (RepValue != NewGV) |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1152 | ConstantPropUsersOf(RepValue, TD, TLI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1153 | |
| 1154 | return NewGV; |
| 1155 | } |
| 1156 | |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 1157 | /// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking |
| 1158 | /// to make sure that there are no complex uses of V. We permit simple things |
| 1159 | /// like dereferencing the pointer, but not storing through the address, unless |
| 1160 | /// it is to the specified global. |
Gabor Greif | 0b520db | 2010-04-06 18:58:22 +0000 | [diff] [blame] | 1161 | static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V, |
| 1162 | const GlobalVariable *GV, |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 1163 | SmallPtrSet<const PHINode*, 8> &PHIs) { |
Gabor Greif | 0b520db | 2010-04-06 18:58:22 +0000 | [diff] [blame] | 1164 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 1165 | UI != E; ++UI) { |
Gabor Greif | 0b520db | 2010-04-06 18:58:22 +0000 | [diff] [blame] | 1166 | const Instruction *Inst = cast<Instruction>(*UI); |
Gabor Greif | a01d6db | 2010-04-06 19:14:05 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 1168 | if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) { |
| 1169 | continue; // Fine, ignore. |
| 1170 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1171 | |
Gabor Greif | 0b520db | 2010-04-06 18:58:22 +0000 | [diff] [blame] | 1172 | if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 1173 | if (SI->getOperand(0) == V && SI->getOperand(1) != GV) |
| 1174 | return false; // Storing the pointer itself... bad. |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 1175 | continue; // Otherwise, storing through it, or storing into GV... fine. |
| 1176 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | a2fb234 | 2010-04-10 18:19:22 +0000 | [diff] [blame] | 1178 | // Must index into the array and into the struct. |
| 1179 | if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) { |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 1180 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs)) |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 1181 | return false; |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 1182 | continue; |
| 1183 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1184 | |
Gabor Greif | 0b520db | 2010-04-06 18:58:22 +0000 | [diff] [blame] | 1185 | if (const PHINode *PN = dyn_cast<PHINode>(Inst)) { |
Chris Lattner | c451f9c | 2007-09-13 16:37:20 +0000 | [diff] [blame] | 1186 | // PHIs are ok if all uses are ok. Don't infinitely recurse through PHI |
| 1187 | // cycles. |
| 1188 | if (PHIs.insert(PN)) |
Chris Lattner | 5e6e494 | 2007-09-14 03:41:21 +0000 | [diff] [blame] | 1189 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs)) |
| 1190 | return false; |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 1191 | continue; |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 1192 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1193 | |
Gabor Greif | 0b520db | 2010-04-06 18:58:22 +0000 | [diff] [blame] | 1194 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) { |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 1195 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs)) |
| 1196 | return false; |
| 1197 | continue; |
| 1198 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 1200 | return false; |
| 1201 | } |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 1202 | return true; |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1205 | /// ReplaceUsesOfMallocWithGlobal - The Alloc pointer is stored into GV |
| 1206 | /// somewhere. Transform all uses of the allocation into loads from the |
| 1207 | /// global and uses of the resultant pointer. Further, delete the store into |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1208 | /// GV. This assumes that these value pass the |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1209 | /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1210 | static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc, |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1211 | GlobalVariable *GV) { |
| 1212 | while (!Alloc->use_empty()) { |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1213 | Instruction *U = cast<Instruction>(*Alloc->use_begin()); |
| 1214 | Instruction *InsertPt = U; |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1215 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 1216 | // If this is the store of the allocation into the global, remove it. |
| 1217 | if (SI->getOperand(1) == GV) { |
| 1218 | SI->eraseFromParent(); |
| 1219 | continue; |
| 1220 | } |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1221 | } else if (PHINode *PN = dyn_cast<PHINode>(U)) { |
| 1222 | // Insert the load in the corresponding predecessor, not right before the |
| 1223 | // PHI. |
Gabor Greif | a36791d | 2009-01-23 19:40:15 +0000 | [diff] [blame] | 1224 | InsertPt = PN->getIncomingBlock(Alloc->use_begin())->getTerminator(); |
Chris Lattner | 101f44e | 2008-12-15 21:44:34 +0000 | [diff] [blame] | 1225 | } else if (isa<BitCastInst>(U)) { |
| 1226 | // Must be bitcast between the malloc and store to initialize the global. |
| 1227 | ReplaceUsesOfMallocWithGlobal(U, GV); |
| 1228 | U->eraseFromParent(); |
| 1229 | continue; |
| 1230 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) { |
| 1231 | // If this is a "GEP bitcast" and the user is a store to the global, then |
| 1232 | // just process it as a bitcast. |
| 1233 | if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse()) |
| 1234 | if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->use_back())) |
| 1235 | if (SI->getOperand(1) == GV) { |
| 1236 | // Must be bitcast GEP between the malloc and store to initialize |
| 1237 | // the global. |
| 1238 | ReplaceUsesOfMallocWithGlobal(GEPI, GV); |
| 1239 | GEPI->eraseFromParent(); |
| 1240 | continue; |
| 1241 | } |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1242 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1244 | // Insert a load from the global, and use it instead of the malloc. |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1245 | Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt); |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1246 | U->replaceUsesOfWith(Alloc, NL); |
| 1247 | } |
| 1248 | } |
| 1249 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1250 | /// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi |
| 1251 | /// of a load) are simple enough to perform heap SRA on. This permits GEP's |
| 1252 | /// that index through the array and struct field, icmps of null, and PHIs. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1253 | static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V, |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 1254 | SmallPtrSet<const PHINode*, 32> &LoadUsingPHIs, |
| 1255 | SmallPtrSet<const PHINode*, 32> &LoadUsingPHIsPerLoad) { |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1256 | // We permit two users of the load: setcc comparing against the null |
| 1257 | // pointer, and a getelementptr of a specific form. |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 1258 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; |
| 1259 | ++UI) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1260 | const Instruction *User = cast<Instruction>(*UI); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1262 | // Comparison against null is ok. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1263 | if (const ICmpInst *ICI = dyn_cast<ICmpInst>(User)) { |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1264 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
| 1265 | return false; |
| 1266 | continue; |
| 1267 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1269 | // getelementptr is also ok, but only a simple form. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1270 | if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1271 | // Must index into the array and into the struct. |
| 1272 | if (GEPI->getNumOperands() < 3) |
| 1273 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1275 | // Otherwise the GEP is ok. |
| 1276 | continue; |
| 1277 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1278 | |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1279 | if (const PHINode *PN = dyn_cast<PHINode>(User)) { |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1280 | if (!LoadUsingPHIsPerLoad.insert(PN)) |
| 1281 | // This means some phi nodes are dependent on each other. |
| 1282 | // Avoid infinite looping! |
| 1283 | return false; |
| 1284 | if (!LoadUsingPHIs.insert(PN)) |
| 1285 | // If we have already analyzed this PHI, then it is safe. |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1286 | continue; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1288 | // Make sure all uses of the PHI are simple enough to transform. |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1289 | if (!LoadUsesSimpleEnoughForHeapSRA(PN, |
| 1290 | LoadUsingPHIs, LoadUsingPHIsPerLoad)) |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1291 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1292 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1293 | continue; |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1294 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1296 | // Otherwise we don't know what this is, not ok. |
| 1297 | return false; |
| 1298 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1299 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1300 | return true; |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | /// AllGlobalLoadUsesSimpleEnoughForHeapSRA - If all users of values loaded from |
| 1305 | /// GV are simple enough to perform HeapSRA, return true. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1306 | static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1307 | Instruction *StoredVal) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1308 | SmallPtrSet<const PHINode*, 32> LoadUsingPHIs; |
| 1309 | SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad; |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 1310 | for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end(); |
| 1311 | UI != E; ++UI) |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1312 | if (const LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1313 | if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs, |
| 1314 | LoadUsingPHIsPerLoad)) |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1315 | return false; |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1316 | LoadUsingPHIsPerLoad.clear(); |
| 1317 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1318 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1319 | // If we reach here, we know that all uses of the loads and transitive uses |
| 1320 | // (through PHI nodes) are simple enough to transform. However, we don't know |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1321 | // that all inputs the to the PHI nodes are in the same equivalence sets. |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1322 | // Check to verify that all operands of the PHIs are either PHIS that can be |
| 1323 | // transformed, loads from GV, or MI itself. |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 1324 | for (SmallPtrSet<const PHINode*, 32>::const_iterator I = LoadUsingPHIs.begin() |
| 1325 | , E = LoadUsingPHIs.end(); I != E; ++I) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1326 | const PHINode *PN = *I; |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1327 | for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) { |
| 1328 | Value *InVal = PN->getIncomingValue(op); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1329 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1330 | // PHI of the stored value itself is ok. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1331 | if (InVal == StoredVal) continue; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1332 | |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1333 | if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) { |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1334 | // One of the PHIs in our set is (optimistically) ok. |
| 1335 | if (LoadUsingPHIs.count(InPN)) |
| 1336 | continue; |
| 1337 | return false; |
| 1338 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1340 | // Load from GV is ok. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1341 | if (const LoadInst *LI = dyn_cast<LoadInst>(InVal)) |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1342 | if (LI->getOperand(0) == GV) |
| 1343 | continue; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1345 | // UNDEF? NULL? |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1347 | // Anything else is rejected. |
| 1348 | return false; |
| 1349 | } |
| 1350 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1352 | return true; |
| 1353 | } |
| 1354 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1355 | static Value *GetHeapSROAValue(Value *V, unsigned FieldNo, |
| 1356 | DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1357 | std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) { |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1358 | std::vector<Value*> &FieldVals = InsertedScalarizedValues[V]; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1359 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1360 | if (FieldNo >= FieldVals.size()) |
| 1361 | FieldVals.resize(FieldNo+1); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1363 | // If we already have this value, just reuse the previously scalarized |
| 1364 | // version. |
| 1365 | if (Value *FieldVal = FieldVals[FieldNo]) |
| 1366 | return FieldVal; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1368 | // Depending on what instruction this is, we have several cases. |
| 1369 | Value *Result; |
| 1370 | if (LoadInst *LI = dyn_cast<LoadInst>(V)) { |
| 1371 | // This is a scalarized version of the load from the global. Just create |
| 1372 | // a new Load of the scalarized global. |
| 1373 | Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo, |
| 1374 | InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1375 | PHIsToRewrite), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1376 | LI->getName()+".f"+Twine(FieldNo), LI); |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1377 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 1378 | // PN's type is pointer to struct. Make a new PHI of pointer to struct |
| 1379 | // field. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1380 | StructType *ST = |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1381 | cast<StructType>(cast<PointerType>(PN->getType())->getElementType()); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1382 | |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1383 | PHINode *NewPN = |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1384 | PHINode::Create(PointerType::getUnqual(ST->getElementType(FieldNo)), |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1385 | PN->getNumIncomingValues(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1386 | PN->getName()+".f"+Twine(FieldNo), PN); |
Jay Foad | d8b4fb4 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 1387 | Result = NewPN; |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1388 | PHIsToRewrite.push_back(std::make_pair(PN, FieldNo)); |
| 1389 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1390 | llvm_unreachable("Unknown usable value"); |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1391 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1393 | return FieldVals[FieldNo] = Result; |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1396 | /// RewriteHeapSROALoadUser - Given a load instruction and a value derived from |
| 1397 | /// the load, rewrite the derived value to use the HeapSRoA'd load. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1398 | static void RewriteHeapSROALoadUser(Instruction *LoadUser, |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1399 | DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1400 | std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) { |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1401 | // If this is a comparison against null, handle it. |
| 1402 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) { |
| 1403 | assert(isa<ConstantPointerNull>(SCI->getOperand(1))); |
| 1404 | // If we have a setcc of the loaded pointer, we can use a setcc of any |
| 1405 | // field. |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1406 | Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1407 | InsertedScalarizedValues, PHIsToRewrite); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1408 | |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1409 | Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr, |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1410 | Constant::getNullValue(NPtr->getType()), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1411 | SCI->getName()); |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1412 | SCI->replaceAllUsesWith(New); |
| 1413 | SCI->eraseFromParent(); |
| 1414 | return; |
| 1415 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1417 | // Handle 'getelementptr Ptr, Idx, i32 FieldNo ...' |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1418 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) { |
| 1419 | assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2)) |
| 1420 | && "Unexpected GEPI!"); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1422 | // Load the pointer for this field. |
| 1423 | unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1424 | Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1425 | InsertedScalarizedValues, PHIsToRewrite); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1426 | |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1427 | // Create the new GEP idx vector. |
| 1428 | SmallVector<Value*, 8> GEPIdx; |
| 1429 | GEPIdx.push_back(GEPI->getOperand(1)); |
| 1430 | GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end()); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1431 | |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1432 | Value *NGEPI = GetElementPtrInst::Create(NewPtr, GEPIdx, |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1433 | GEPI->getName(), GEPI); |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1434 | GEPI->replaceAllUsesWith(NGEPI); |
| 1435 | GEPI->eraseFromParent(); |
| 1436 | return; |
| 1437 | } |
Chris Lattner | 309f20f | 2007-09-13 21:31:36 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1439 | // Recursively transform the users of PHI nodes. This will lazily create the |
| 1440 | // PHIs that are needed for individual elements. Keep track of what PHIs we |
| 1441 | // see in InsertedScalarizedValues so that we don't get infinite loops (very |
| 1442 | // antisocial). If the PHI is already in InsertedScalarizedValues, it has |
| 1443 | // already been seen first by another load, so its uses have already been |
| 1444 | // processed. |
| 1445 | PHINode *PN = cast<PHINode>(LoadUser); |
Chris Lattner | c30a38f | 2011-07-21 06:21:31 +0000 | [diff] [blame] | 1446 | if (!InsertedScalarizedValues.insert(std::make_pair(PN, |
| 1447 | std::vector<Value*>())).second) |
| 1448 | return; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1449 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1450 | // If this is the first time we've seen this PHI, recursively process all |
| 1451 | // users. |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1452 | for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) { |
| 1453 | Instruction *User = cast<Instruction>(*UI++); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1454 | RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1455 | } |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1458 | /// RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global. Ptr |
| 1459 | /// is a value loaded from the global. Eliminate all uses of Ptr, making them |
| 1460 | /// use FieldGlobals instead. All uses of loaded values satisfy |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1461 | /// AllGlobalLoadUsesSimpleEnoughForHeapSRA. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1462 | static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load, |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1463 | DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1464 | std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) { |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1465 | for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end(); |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1466 | UI != E; ) { |
| 1467 | Instruction *User = cast<Instruction>(*UI++); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1468 | RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1469 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1471 | if (Load->use_empty()) { |
| 1472 | Load->eraseFromParent(); |
| 1473 | InsertedScalarizedValues.erase(Load); |
| 1474 | } |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1477 | /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break |
| 1478 | /// it up into multiple allocations of arrays of the fields. |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1479 | static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1480 | Value *NElems, DataLayout *TD, |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1481 | const TargetLibraryInfo *TLI) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1482 | DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n'); |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1483 | Type *MAT = getMallocAllocatedType(CI, TLI); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1484 | StructType *STy = cast<StructType>(MAT); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1485 | |
| 1486 | // There is guaranteed to be at least one use of the malloc (storing |
| 1487 | // it into GV). If there are other uses, change them to be uses of |
| 1488 | // the global to simplify later code. This also deletes the store |
| 1489 | // into GV. |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1490 | ReplaceUsesOfMallocWithGlobal(CI, GV); |
| 1491 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1492 | // Okay, at this point, there are no users of the malloc. Insert N |
| 1493 | // new mallocs at the same place as CI, and N globals. |
| 1494 | std::vector<Value*> FieldGlobals; |
| 1495 | std::vector<Value*> FieldMallocs; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1496 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1497 | for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){ |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1498 | Type *FieldTy = STy->getElementType(FieldNo); |
| 1499 | PointerType *PFieldTy = PointerType::getUnqual(FieldTy); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1500 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1501 | GlobalVariable *NGV = |
| 1502 | new GlobalVariable(*GV->getParent(), |
| 1503 | PFieldTy, false, GlobalValue::InternalLinkage, |
| 1504 | Constant::getNullValue(PFieldTy), |
| 1505 | GV->getName() + ".f" + Twine(FieldNo), GV, |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1506 | GV->getThreadLocalMode()); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1507 | FieldGlobals.push_back(NGV); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1508 | |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1509 | unsigned TypeSize = TD->getTypeAllocSize(FieldTy); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1510 | if (StructType *ST = dyn_cast<StructType>(FieldTy)) |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1511 | TypeSize = TD->getStructLayout(ST)->getSizeInBytes(); |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 1512 | Type *IntPtrTy = TD->getIntPtrType(CI->getContext()); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1513 | Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy, |
| 1514 | ConstantInt::get(IntPtrTy, TypeSize), |
Chris Lattner | 5a30a85 | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 1515 | NElems, 0, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1516 | CI->getName() + ".f" + Twine(FieldNo)); |
Chris Lattner | 3f5e0b8 | 2010-02-26 18:23:13 +0000 | [diff] [blame] | 1517 | FieldMallocs.push_back(NMI); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1518 | new StoreInst(NMI, NGV, CI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1519 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1520 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1521 | // The tricky aspect of this transformation is handling the case when malloc |
| 1522 | // fails. In the original code, malloc failing would set the result pointer |
| 1523 | // of malloc to null. In this case, some mallocs could succeed and others |
| 1524 | // could fail. As such, we emit code that looks like this: |
| 1525 | // F0 = malloc(field0) |
| 1526 | // F1 = malloc(field1) |
| 1527 | // F2 = malloc(field2) |
| 1528 | // if (F0 == 0 || F1 == 0 || F2 == 0) { |
| 1529 | // if (F0) { free(F0); F0 = 0; } |
| 1530 | // if (F1) { free(F1); F1 = 0; } |
| 1531 | // if (F2) { free(F2); F2 = 0; } |
| 1532 | // } |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 1533 | // The malloc can also fail if its argument is too large. |
Gabor Greif | 9e4f243 | 2010-06-24 14:42:01 +0000 | [diff] [blame] | 1534 | Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0); |
| 1535 | Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0), |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 1536 | ConstantZero, "isneg"); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1537 | for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) { |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1538 | Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i], |
| 1539 | Constant::getNullValue(FieldMallocs[i]->getType()), |
| 1540 | "isnull"); |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 1541 | RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | // Split the basic block at the old malloc. |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1545 | BasicBlock *OrigBB = CI->getParent(); |
| 1546 | BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont"); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1547 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1548 | // Create the block to check the first condition. Put all these blocks at the |
| 1549 | // end of the function as they are unlikely to be executed. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1550 | BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(), |
| 1551 | "malloc_ret_null", |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1552 | OrigBB->getParent()); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1553 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1554 | // Remove the uncond branch from OrigBB to ContBB, turning it into a cond |
| 1555 | // branch on RunningOr. |
| 1556 | OrigBB->getTerminator()->eraseFromParent(); |
| 1557 | BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1558 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1559 | // Within the NullPtrBlock, we need to emit a comparison and branch for each |
| 1560 | // pointer, because some may be null while others are not. |
| 1561 | for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { |
| 1562 | Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1563 | Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal, |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1564 | Constant::getNullValue(GVVal->getType())); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1565 | BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it", |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1566 | OrigBB->getParent()); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1567 | BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next", |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1568 | OrigBB->getParent()); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 1569 | Instruction *BI = BranchInst::Create(FreeBlock, NextBlock, |
| 1570 | Cmp, NullPtrBlock); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1571 | |
| 1572 | // Fill in FreeBlock. |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 1573 | CallInst::CreateFree(GVVal, BI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1574 | new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i], |
| 1575 | FreeBlock); |
| 1576 | BranchInst::Create(NextBlock, FreeBlock); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1577 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1578 | NullPtrBlock = NextBlock; |
| 1579 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1580 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1581 | BranchInst::Create(ContBB, NullPtrBlock); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1582 | |
| 1583 | // CI is no longer needed, remove it. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1584 | CI->eraseFromParent(); |
| 1585 | |
| 1586 | /// InsertedScalarizedLoads - As we process loads, if we can't immediately |
| 1587 | /// update all uses of the load, keep track of what scalarized loads are |
| 1588 | /// inserted for a given load. |
| 1589 | DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues; |
| 1590 | InsertedScalarizedValues[GV] = FieldGlobals; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1591 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1592 | std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1593 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1594 | // Okay, the malloc site is completely handled. All of the uses of GV are now |
| 1595 | // loads, and all uses of those loads are simple. Rewrite them to use loads |
| 1596 | // of the per-field globals instead. |
| 1597 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;) { |
| 1598 | Instruction *User = cast<Instruction>(*UI++); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1599 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1600 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1601 | RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1602 | continue; |
| 1603 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1604 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1605 | // Must be a store of null. |
| 1606 | StoreInst *SI = cast<StoreInst>(User); |
| 1607 | assert(isa<ConstantPointerNull>(SI->getOperand(0)) && |
| 1608 | "Unexpected heap-sra user!"); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1609 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1610 | // Insert a store of null into each global. |
| 1611 | for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1612 | PointerType *PT = cast<PointerType>(FieldGlobals[i]->getType()); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1613 | Constant *Null = Constant::getNullValue(PT->getElementType()); |
| 1614 | new StoreInst(Null, FieldGlobals[i], SI); |
| 1615 | } |
| 1616 | // Erase the original store. |
| 1617 | SI->eraseFromParent(); |
| 1618 | } |
| 1619 | |
| 1620 | // While we have PHIs that are interesting to rewrite, do it. |
| 1621 | while (!PHIsToRewrite.empty()) { |
| 1622 | PHINode *PN = PHIsToRewrite.back().first; |
| 1623 | unsigned FieldNo = PHIsToRewrite.back().second; |
| 1624 | PHIsToRewrite.pop_back(); |
| 1625 | PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]); |
| 1626 | assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi"); |
| 1627 | |
| 1628 | // Add all the incoming values. This can materialize more phis. |
| 1629 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1630 | Value *InVal = PN->getIncomingValue(i); |
| 1631 | InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1632 | PHIsToRewrite); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1633 | FieldPN->addIncoming(InVal, PN->getIncomingBlock(i)); |
| 1634 | } |
| 1635 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1636 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1637 | // Drop all inter-phi links and any loads that made it this far. |
| 1638 | for (DenseMap<Value*, std::vector<Value*> >::iterator |
| 1639 | I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end(); |
| 1640 | I != E; ++I) { |
| 1641 | if (PHINode *PN = dyn_cast<PHINode>(I->first)) |
| 1642 | PN->dropAllReferences(); |
| 1643 | else if (LoadInst *LI = dyn_cast<LoadInst>(I->first)) |
| 1644 | LI->dropAllReferences(); |
| 1645 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1646 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1647 | // Delete all the phis and loads now that inter-references are dead. |
| 1648 | for (DenseMap<Value*, std::vector<Value*> >::iterator |
| 1649 | I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end(); |
| 1650 | I != E; ++I) { |
| 1651 | if (PHINode *PN = dyn_cast<PHINode>(I->first)) |
| 1652 | PN->eraseFromParent(); |
| 1653 | else if (LoadInst *LI = dyn_cast<LoadInst>(I->first)) |
| 1654 | LI->eraseFromParent(); |
| 1655 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1656 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1657 | // The old global is now dead, remove it. |
| 1658 | GV->eraseFromParent(); |
| 1659 | |
| 1660 | ++NumHeapSRA; |
| 1661 | return cast<GlobalVariable>(FieldGlobals[0]); |
| 1662 | } |
| 1663 | |
Chris Lattner | e61d0a6 | 2008-12-15 21:02:25 +0000 | [diff] [blame] | 1664 | /// TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a |
| 1665 | /// pointer global variable with a single value stored it that is a malloc or |
| 1666 | /// cast of malloc. |
| 1667 | static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1668 | CallInst *CI, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1669 | Type *AllocTy, |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1670 | AtomicOrdering Ordering, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1671 | Module::global_iterator &GVI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1672 | DataLayout *TD, |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1673 | TargetLibraryInfo *TLI) { |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1674 | if (!TD) |
| 1675 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1676 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1677 | // If this is a malloc of an abstract type, don't touch it. |
| 1678 | if (!AllocTy->isSized()) |
| 1679 | return false; |
| 1680 | |
| 1681 | // We can't optimize this global unless all uses of it are *known* to be |
| 1682 | // of the malloc value, not of the null initializer value (consider a use |
| 1683 | // that compares the global's value against zero to see if the malloc has |
| 1684 | // been reached). To do this, we check to see if all uses of the global |
| 1685 | // would trap if the global were null: this proves that they must all |
| 1686 | // happen after the malloc. |
| 1687 | if (!AllUsesOfLoadedValueWillTrapIfNull(GV)) |
| 1688 | return false; |
| 1689 | |
| 1690 | // We can't optimize this if the malloc itself is used in a complex way, |
| 1691 | // for example, being stored into multiple globals. This allows the |
Nick Lewycky | bc384a1 | 2012-02-05 19:48:37 +0000 | [diff] [blame] | 1692 | // malloc to be stored into the specified global, loaded icmp'd, and |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1693 | // GEP'd. These are all things we could transform to using the global |
| 1694 | // for. |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1695 | SmallPtrSet<const PHINode*, 8> PHIs; |
| 1696 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs)) |
| 1697 | return false; |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1698 | |
| 1699 | // If we have a global that is only initialized with a fixed size malloc, |
| 1700 | // transform the program to use global memory instead of malloc'd memory. |
| 1701 | // This eliminates dynamic allocation, avoids an indirection accessing the |
| 1702 | // data, and exposes the resultant global to further GlobalOpt. |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1703 | // We cannot optimize the malloc if we cannot determine malloc array size. |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1704 | Value *NElems = getMallocArraySize(CI, TD, TLI, true); |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1705 | if (!NElems) |
| 1706 | return false; |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1707 | |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1708 | if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems)) |
| 1709 | // Restrict this transformation to only working on small allocations |
| 1710 | // (2048 bytes currently), as we don't want to introduce a 16M global or |
| 1711 | // something. |
| 1712 | if (NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) { |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1713 | GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, TD, TLI); |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1714 | return true; |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1715 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1716 | |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1717 | // If the allocation is an array of structures, consider transforming this |
| 1718 | // into multiple malloc'd arrays, one for each field. This is basically |
| 1719 | // SRoA for malloc'd memory. |
| 1720 | |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1721 | if (Ordering != NotAtomic) |
| 1722 | return false; |
| 1723 | |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1724 | // If this is an allocation of a fixed size array of structs, analyze as a |
| 1725 | // variable size array. malloc [100 x struct],1 -> malloc struct, 100 |
Gabor Greif | 9e4f243 | 2010-06-24 14:42:01 +0000 | [diff] [blame] | 1726 | if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1)) |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1727 | if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy)) |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1728 | AllocTy = AT->getElementType(); |
Gabor Greif | 9e4f243 | 2010-06-24 14:42:01 +0000 | [diff] [blame] | 1729 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1730 | StructType *AllocSTy = dyn_cast<StructType>(AllocTy); |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1731 | if (!AllocSTy) |
| 1732 | return false; |
| 1733 | |
| 1734 | // This the structure has an unreasonable number of fields, leave it |
| 1735 | // alone. |
| 1736 | if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 && |
| 1737 | AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) { |
| 1738 | |
| 1739 | // If this is a fixed size array, transform the Malloc to be an alloc of |
| 1740 | // structs. malloc [100 x struct],1 -> malloc struct, 100 |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1741 | if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) { |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 1742 | Type *IntPtrTy = TD->getIntPtrType(CI->getContext()); |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1743 | unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes(); |
| 1744 | Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize); |
| 1745 | Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements()); |
| 1746 | Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy, |
| 1747 | AllocSize, NumElements, |
Chris Lattner | 5a30a85 | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 1748 | 0, CI->getName()); |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1749 | Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI); |
| 1750 | CI->replaceAllUsesWith(Cast); |
| 1751 | CI->eraseFromParent(); |
Nuno Lopes | eb7c686 | 2012-06-22 00:25:01 +0000 | [diff] [blame] | 1752 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc)) |
| 1753 | CI = cast<CallInst>(BCI->getOperand(0)); |
| 1754 | else |
Nuno Lopes | cd88efe | 2012-06-22 00:29:58 +0000 | [diff] [blame] | 1755 | CI = cast<CallInst>(Malloc); |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1756 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1757 | |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1758 | GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, TD, TLI, true), |
| 1759 | TD, TLI); |
Evan Cheng | 86cd445 | 2010-04-14 20:52:55 +0000 | [diff] [blame] | 1760 | return true; |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1761 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1762 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1763 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1764 | } |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1766 | // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge |
| 1767 | // that only one value (besides its initializer) is ever stored to the global. |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1768 | static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1769 | AtomicOrdering Ordering, |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 1770 | Module::global_iterator &GVI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1771 | DataLayout *TD, TargetLibraryInfo *TLI) { |
Chris Lattner | 344b41c | 2008-12-15 21:20:32 +0000 | [diff] [blame] | 1772 | // Ignore no-op GEPs and bitcasts. |
| 1773 | StoredOnceVal = StoredOnceVal->stripPointerCasts(); |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1774 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1775 | // If we are dealing with a pointer global that is initialized to null and |
| 1776 | // only has one (non-null) value stored into it, then we can optimize any |
| 1777 | // users of the loaded value (often calls and loads) that would trap if the |
| 1778 | // value was null. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1779 | if (GV->getInitializer()->getType()->isPointerTy() && |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1780 | GV->getInitializer()->isNullValue()) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1781 | if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) { |
| 1782 | if (GV->getInitializer()->getType() != SOVC->getType()) |
Chris Lattner | 98a42b2 | 2011-05-22 07:15:13 +0000 | [diff] [blame] | 1783 | SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1785 | // Optimize away any trapping uses of the loaded value. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1786 | if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, TD, TLI)) |
Chris Lattner | 8be8012 | 2004-10-10 17:07:12 +0000 | [diff] [blame] | 1787 | return true; |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1788 | } else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) { |
| 1789 | Type *MallocType = getMallocAllocatedType(CI, TLI); |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 1790 | if (MallocType && |
| 1791 | TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, Ordering, GVI, |
| 1792 | TD, TLI)) |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1793 | return true; |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1794 | } |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1795 | } |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1796 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1797 | return false; |
| 1798 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1799 | |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1800 | /// TryToShrinkGlobalToBoolean - At this point, we have learned that the only |
| 1801 | /// two values ever stored into GV are its initializer and OtherVal. See if we |
| 1802 | /// can shrink the global into a boolean and select between the two values |
| 1803 | /// whenever it is used. This exposes the values to other scalar optimizations. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1804 | static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1805 | Type *GVElType = GV->getType()->getElementType(); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1806 | |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1807 | // If GVElType is already i1, it is already shrunk. If the type of the GV is |
Chris Lattner | 6f6923f | 2009-03-07 23:32:02 +0000 | [diff] [blame] | 1808 | // an FP value, pointer or vector, don't do this optimization because a select |
| 1809 | // between them is very expensive and unlikely to lead to later |
| 1810 | // simplification. In these cases, we typically end up with "cond ? v1 : v2" |
| 1811 | // where v1 and v2 both require constant pool loads, a big loss. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1812 | if (GVElType == Type::getInt1Ty(GV->getContext()) || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1813 | GVElType->isFloatingPointTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1814 | GVElType->isPointerTy() || GVElType->isVectorTy()) |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1815 | return false; |
Gabor Greif | aaaaa02 | 2010-07-12 14:13:15 +0000 | [diff] [blame] | 1816 | |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1817 | // Walk the use list of the global seeing if all the uses are load or store. |
| 1818 | // If there is anything else, bail out. |
Gabor Greif | aaaaa02 | 2010-07-12 14:13:15 +0000 | [diff] [blame] | 1819 | for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){ |
| 1820 | User *U = *I; |
| 1821 | if (!isa<LoadInst>(U) && !isa<StoreInst>(U)) |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1822 | return false; |
Gabor Greif | aaaaa02 | 2010-07-12 14:13:15 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1825 | DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1827 | // Create the new global, initializing it to false. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1828 | GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()), |
| 1829 | false, |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1830 | GlobalValue::InternalLinkage, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1831 | ConstantInt::getFalse(GV->getContext()), |
Nick Lewycky | 0e670df | 2009-05-03 03:49:08 +0000 | [diff] [blame] | 1832 | GV->getName()+".b", |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1833 | GV->getThreadLocalMode()); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1834 | GV->getParent()->getGlobalList().insert(GV, NewGV); |
| 1835 | |
| 1836 | Constant *InitVal = GV->getInitializer(); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1837 | assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1838 | "No reason to shrink to bool!"); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1839 | |
| 1840 | // If initialized to zero and storing one into the global, we can use a cast |
| 1841 | // instead of a select to synthesize the desired value. |
| 1842 | bool IsOneZero = false; |
| 1843 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1844 | IsOneZero = InitVal->isNullValue() && CI->isOne(); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1845 | |
| 1846 | while (!GV->use_empty()) { |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1847 | Instruction *UI = cast<Instruction>(GV->use_back()); |
| 1848 | if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1849 | // Change the store into a boolean store. |
| 1850 | bool StoringOther = SI->getOperand(0) == OtherVal; |
| 1851 | // Only do this if we weren't storing a loaded value. |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 1852 | Value *StoreVal; |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1853 | if (StoringOther || SI->getOperand(0) == InitVal) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1854 | StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()), |
| 1855 | StoringOther); |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 1856 | else { |
| 1857 | // Otherwise, we are storing a previously loaded copy. To do this, |
| 1858 | // change the copy from copying the original value to just copying the |
| 1859 | // bool. |
| 1860 | Instruction *StoredVal = cast<Instruction>(SI->getOperand(0)); |
| 1861 | |
Gabor Greif | 9e4f243 | 2010-06-24 14:42:01 +0000 | [diff] [blame] | 1862 | // If we've already replaced the input, StoredVal will be a cast or |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 1863 | // select instruction. If not, it will be a load of the original |
| 1864 | // global. |
| 1865 | if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { |
| 1866 | assert(LI->getOperand(0) == GV && "Not a copy!"); |
| 1867 | // Insert a new load, to preserve the saved value. |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1868 | StoreVal = new LoadInst(NewGV, LI->getName()+".b", false, 0, |
| 1869 | LI->getOrdering(), LI->getSynchScope(), LI); |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 1870 | } else { |
| 1871 | assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) && |
| 1872 | "This is not a form that we understand!"); |
| 1873 | StoreVal = StoredVal->getOperand(0); |
| 1874 | assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!"); |
| 1875 | } |
| 1876 | } |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1877 | new StoreInst(StoreVal, NewGV, false, 0, |
| 1878 | SI->getOrdering(), SI->getSynchScope(), SI); |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1879 | } else { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1880 | // Change the load into a load of bool then a select. |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1881 | LoadInst *LI = cast<LoadInst>(UI); |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 1882 | LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", false, 0, |
| 1883 | LI->getOrdering(), LI->getSynchScope(), LI); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1884 | Value *NSI; |
| 1885 | if (IsOneZero) |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 1886 | NSI = new ZExtInst(NLI, LI->getType(), "", LI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1887 | else |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1888 | NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 1889 | NSI->takeName(LI); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1890 | LI->replaceAllUsesWith(NSI); |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1891 | } |
| 1892 | UI->eraseFromParent(); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | GV->eraseFromParent(); |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1896 | return true; |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | |
Nick Lewycky | db292a6 | 2012-02-12 00:52:26 +0000 | [diff] [blame] | 1900 | /// ProcessGlobal - Analyze the specified global variable and optimize it if |
| 1901 | /// possible. If we make a change, return true. |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 1902 | bool GlobalOpt::ProcessGlobal(GlobalVariable *GV, |
| 1903 | Module::global_iterator &GVI) { |
Rafael Espindola | 0397729 | 2012-06-14 22:48:13 +0000 | [diff] [blame] | 1904 | if (!GV->isDiscardableIfUnused()) |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 1905 | return false; |
| 1906 | |
| 1907 | // Do more involved optimizations if the global is internal. |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1908 | GV->removeDeadConstantUsers(); |
| 1909 | |
| 1910 | if (GV->use_empty()) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1911 | DEBUG(dbgs() << "GLOBAL DEAD: " << *GV); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1912 | GV->eraseFromParent(); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1913 | ++NumDeleted; |
| 1914 | return true; |
| 1915 | } |
| 1916 | |
Rafael Espindola | 2f135d4 | 2012-06-15 18:00:24 +0000 | [diff] [blame] | 1917 | if (!GV->hasLocalLinkage()) |
| 1918 | return false; |
| 1919 | |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 1920 | SmallPtrSet<const PHINode*, 16> PHIUsers; |
| 1921 | GlobalStatus GS; |
| 1922 | |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1923 | if (AnalyzeGlobal(GV, GS, PHIUsers)) |
| 1924 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 1925 | |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 1926 | if (!GS.isCompared && !GV->hasUnnamedAddr()) { |
| 1927 | GV->setUnnamedAddr(true); |
| 1928 | NumUnnamed++; |
| 1929 | } |
| 1930 | |
| 1931 | if (GV->isConstant() || !GV->hasInitializer()) |
| 1932 | return false; |
| 1933 | |
| 1934 | return ProcessInternalGlobal(GV, GVI, PHIUsers, GS); |
| 1935 | } |
| 1936 | |
| 1937 | /// ProcessInternalGlobal - Analyze the specified global variable and optimize |
| 1938 | /// it if possible. If we make a change, return true. |
| 1939 | bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV, |
| 1940 | Module::global_iterator &GVI, |
Nick Lewycky | db292a6 | 2012-02-12 00:52:26 +0000 | [diff] [blame] | 1941 | const SmallPtrSet<const PHINode*, 16> &PHIUsers, |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 1942 | const GlobalStatus &GS) { |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1943 | // If this is a first class global and has only one accessing function |
| 1944 | // and this function is main (which we know is not recursive we can make |
| 1945 | // this global a local variable) we replace the global with a local alloca |
| 1946 | // in this function. |
| 1947 | // |
| 1948 | // NOTE: It doesn't make sense to promote non single-value types since we |
| 1949 | // are just replacing static memory to stack memory. |
| 1950 | // |
| 1951 | // If the global is in different address space, don't bring it to stack. |
| 1952 | if (!GS.HasMultipleAccessingFunctions && |
| 1953 | GS.AccessingFunction && !GS.HasNonInstructionUser && |
| 1954 | GV->getType()->getElementType()->isSingleValueType() && |
| 1955 | GS.AccessingFunction->getName() == "main" && |
| 1956 | GS.AccessingFunction->hasExternalLinkage() && |
| 1957 | GV->getType()->getAddressSpace() == 0) { |
| 1958 | DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV); |
Nick Lewycky | bc384a1 | 2012-02-05 19:48:37 +0000 | [diff] [blame] | 1959 | Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1960 | ->getEntryBlock().begin()); |
Nick Lewycky | bc384a1 | 2012-02-05 19:48:37 +0000 | [diff] [blame] | 1961 | Type *ElemTy = GV->getType()->getElementType(); |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1962 | // FIXME: Pass Global's alignment when globals have alignment |
Nick Lewycky | bc384a1 | 2012-02-05 19:48:37 +0000 | [diff] [blame] | 1963 | AllocaInst *Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), &FirstI); |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1964 | if (!isa<UndefValue>(GV->getInitializer())) |
| 1965 | new StoreInst(GV->getInitializer(), Alloca, &FirstI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1966 | |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1967 | GV->replaceAllUsesWith(Alloca); |
| 1968 | GV->eraseFromParent(); |
| 1969 | ++NumLocalized; |
| 1970 | return true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1971 | } |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1972 | |
| 1973 | // If the global is never loaded (but may be stored to), it is dead. |
| 1974 | // Delete it now. |
| 1975 | if (!GS.isLoaded) { |
| 1976 | DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV); |
| 1977 | |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 1978 | bool Changed; |
| 1979 | if (isLeakCheckerRoot(GV)) { |
| 1980 | // Delete any constant stores to the global. |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1981 | Changed = CleanupPointerRootUsers(GV, TLI); |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 1982 | } else { |
| 1983 | // Delete any stores we can find to the global. We may not be able to |
| 1984 | // make it completely dead though. |
| 1985 | Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI); |
| 1986 | } |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 1987 | |
| 1988 | // If the global is dead now, delete it. |
| 1989 | if (GV->use_empty()) { |
| 1990 | GV->eraseFromParent(); |
| 1991 | ++NumDeleted; |
| 1992 | Changed = true; |
| 1993 | } |
| 1994 | return Changed; |
| 1995 | |
| 1996 | } else if (GS.StoredType <= GlobalStatus::isInitializerStored) { |
| 1997 | DEBUG(dbgs() << "MARKING CONSTANT: " << *GV); |
| 1998 | GV->setConstant(true); |
| 1999 | |
| 2000 | // Clean up any obviously simplifiable users now. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 2001 | CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI); |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 2002 | |
| 2003 | // If the global is dead now, just nuke it. |
| 2004 | if (GV->use_empty()) { |
| 2005 | DEBUG(dbgs() << " *** Marking constant allowed us to simplify " |
| 2006 | << "all users and delete global!\n"); |
| 2007 | GV->eraseFromParent(); |
| 2008 | ++NumDeleted; |
| 2009 | } |
| 2010 | |
| 2011 | ++NumMarked; |
| 2012 | return true; |
| 2013 | } else if (!GV->getInitializer()->getType()->isSingleValueType()) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2014 | if (DataLayout *TD = getAnalysisIfAvailable<DataLayout>()) |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 2015 | if (GlobalVariable *FirstNewGV = SRAGlobal(GV, *TD)) { |
| 2016 | GVI = FirstNewGV; // Don't skip the newly produced globals! |
| 2017 | return true; |
| 2018 | } |
| 2019 | } else if (GS.StoredType == GlobalStatus::isStoredOnce) { |
| 2020 | // If the initial value for the global was an undef value, and if only |
| 2021 | // one other value was stored into it, we can just change the |
| 2022 | // initializer to be the stored value, then delete all stores to the |
| 2023 | // global. This allows us to mark it constant. |
| 2024 | if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) |
| 2025 | if (isa<UndefValue>(GV->getInitializer())) { |
| 2026 | // Change the initial value here. |
| 2027 | GV->setInitializer(SOVConstant); |
| 2028 | |
| 2029 | // Clean up any obviously simplifiable users now. |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 2030 | CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI); |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 2031 | |
| 2032 | if (GV->use_empty()) { |
| 2033 | DEBUG(dbgs() << " *** Substituting initializer allowed us to " |
Nick Lewycky | 8899d5c | 2012-07-24 07:21:08 +0000 | [diff] [blame] | 2034 | << "simplify all users and delete global!\n"); |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 2035 | GV->eraseFromParent(); |
| 2036 | ++NumDeleted; |
| 2037 | } else { |
| 2038 | GVI = GV; |
| 2039 | } |
| 2040 | ++NumSubstitute; |
| 2041 | return true; |
| 2042 | } |
| 2043 | |
| 2044 | // Try to optimize globals based on the knowledge that only one value |
| 2045 | // (besides its initializer) is ever stored to the global. |
Nick Lewycky | fad4d40 | 2012-02-05 19:56:38 +0000 | [diff] [blame] | 2046 | if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, GVI, |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 2047 | TD, TLI)) |
Rafael Espindola | daad56a | 2011-01-18 04:36:06 +0000 | [diff] [blame] | 2048 | return true; |
| 2049 | |
| 2050 | // Otherwise, if the global was not a boolean, we can shrink it to be a |
| 2051 | // boolean. |
| 2052 | if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) |
| 2053 | if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) { |
| 2054 | ++NumShrunkToBool; |
| 2055 | return true; |
| 2056 | } |
| 2057 | } |
| 2058 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 2059 | return false; |
| 2060 | } |
| 2061 | |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 2062 | /// ChangeCalleesToFastCall - Walk all of the direct calls of the specified |
| 2063 | /// function, changing them to FastCC. |
| 2064 | static void ChangeCalleesToFastCall(Function *F) { |
| 2065 | for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ |
Jay Foad | b7454fd | 2012-05-12 08:30:16 +0000 | [diff] [blame] | 2066 | if (isa<BlockAddress>(*UI)) |
| 2067 | continue; |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 2068 | CallSite User(cast<Instruction>(*UI)); |
| 2069 | User.setCallingConv(CallingConv::Fast); |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 2070 | } |
| 2071 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 2072 | |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 2073 | static AttrListPtr StripNest(LLVMContext &C, const AttrListPtr &Attrs) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 2074 | for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) { |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 2075 | if (!Attrs.getSlot(i).Attrs.hasAttribute(Attributes::Nest)) |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 2076 | continue; |
| 2077 | |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 2078 | // There can be only one. |
Bill Wendling | 46d5dd9 | 2012-10-16 05:23:31 +0000 | [diff] [blame] | 2079 | return Attrs.removeAttr(C, Attrs.getSlot(i).Index, |
| 2080 | Attributes::get(C, Attributes::Nest)); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | return Attrs; |
| 2084 | } |
| 2085 | |
| 2086 | static void RemoveNestAttribute(Function *F) { |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 2087 | F->setAttributes(StripNest(F->getContext(), F->getAttributes())); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 2088 | for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ |
Jay Foad | b7454fd | 2012-05-12 08:30:16 +0000 | [diff] [blame] | 2089 | if (isa<BlockAddress>(*UI)) |
| 2090 | continue; |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 2091 | CallSite User(cast<Instruction>(*UI)); |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 2092 | User.setAttributes(StripNest(F->getContext(), User.getAttributes())); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2096 | bool GlobalOpt::OptimizeFunctions(Module &M) { |
| 2097 | bool Changed = false; |
| 2098 | // Optimize functions. |
| 2099 | for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) { |
| 2100 | Function *F = FI++; |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 2101 | // Functions without names cannot be referenced outside this module. |
| 2102 | if (!F->hasName() && !F->isDeclaration()) |
| 2103 | F->setLinkage(GlobalValue::InternalLinkage); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2104 | F->removeDeadConstantUsers(); |
Eli Friedman | c663305 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 2105 | if (F->isDefTriviallyDead()) { |
Chris Lattner | ec4c7b9 | 2009-11-01 19:03:42 +0000 | [diff] [blame] | 2106 | F->eraseFromParent(); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2107 | Changed = true; |
| 2108 | ++NumFnDeleted; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 2109 | } else if (F->hasLocalLinkage()) { |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 2110 | if (F->getCallingConv() == CallingConv::C && !F->isVarArg() && |
Jay Foad | 757068f | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 2111 | !F->hasAddressTaken()) { |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 2112 | // If this function has C calling conventions, is not a varargs |
| 2113 | // function, and is only called directly, promote it to use the Fast |
| 2114 | // calling convention. |
| 2115 | F->setCallingConv(CallingConv::Fast); |
| 2116 | ChangeCalleesToFastCall(F); |
| 2117 | ++NumFastCallFns; |
| 2118 | Changed = true; |
| 2119 | } |
| 2120 | |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 2121 | if (F->getAttributes().hasAttrSomewhere(Attributes::Nest) && |
Jay Foad | 757068f | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 2122 | !F->hasAddressTaken()) { |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 2123 | // The function is not used by a trampoline intrinsic, so it is safe |
| 2124 | // to remove the 'nest' attribute. |
| 2125 | RemoveNestAttribute(F); |
| 2126 | ++NumNestRemoved; |
| 2127 | Changed = true; |
| 2128 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | return Changed; |
| 2132 | } |
| 2133 | |
| 2134 | bool GlobalOpt::OptimizeGlobalVars(Module &M) { |
| 2135 | bool Changed = false; |
| 2136 | for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); |
| 2137 | GVI != E; ) { |
| 2138 | GlobalVariable *GV = GVI++; |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 2139 | // Global variables without names cannot be referenced outside this module. |
| 2140 | if (!GV->hasName() && !GV->isDeclaration()) |
| 2141 | GV->setLinkage(GlobalValue::InternalLinkage); |
Dan Gohman | 01b97dd | 2009-11-23 16:22:21 +0000 | [diff] [blame] | 2142 | // Simplify the initializer. |
| 2143 | if (GV->hasInitializer()) |
| 2144 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) { |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 2145 | Constant *New = ConstantFoldConstantExpression(CE, TD, TLI); |
Dan Gohman | 01b97dd | 2009-11-23 16:22:21 +0000 | [diff] [blame] | 2146 | if (New && New != CE) |
| 2147 | GV->setInitializer(New); |
| 2148 | } |
Rafael Espindola | c4440e3 | 2011-01-19 16:32:21 +0000 | [diff] [blame] | 2149 | |
| 2150 | Changed |= ProcessGlobal(GV, GVI); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2151 | } |
| 2152 | return Changed; |
| 2153 | } |
| 2154 | |
Nick Lewycky | 2c44a80 | 2011-04-08 07:30:21 +0000 | [diff] [blame] | 2155 | /// FindGlobalCtors - Find the llvm.global_ctors list, verifying that all |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2156 | /// initializers have an init priority of 65535. |
| 2157 | GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) { |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2158 | GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors"); |
| 2159 | if (GV == 0) return 0; |
| 2160 | |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2161 | // Verify that the initializer is simple enough for us to handle. We are |
| 2162 | // only allowed to optimize the initializer if it is unique. |
| 2163 | if (!GV->hasUniqueInitializer()) return 0; |
Nick Lewycky | 5ea5c61 | 2011-04-11 22:11:20 +0000 | [diff] [blame] | 2164 | |
| 2165 | if (isa<ConstantAggregateZero>(GV->getInitializer())) |
| 2166 | return GV; |
| 2167 | ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); |
Eli Friedman | 18a2e50 | 2011-04-09 09:11:09 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2169 | for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) { |
Nick Lewycky | 5ea5c61 | 2011-04-11 22:11:20 +0000 | [diff] [blame] | 2170 | if (isa<ConstantAggregateZero>(*i)) |
| 2171 | continue; |
| 2172 | ConstantStruct *CS = cast<ConstantStruct>(*i); |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2173 | if (isa<ConstantPointerNull>(CS->getOperand(1))) |
| 2174 | continue; |
Chris Lattner | 7d8e58f | 2005-09-26 02:19:27 +0000 | [diff] [blame] | 2175 | |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2176 | // Must have a function or null ptr. |
| 2177 | if (!isa<Function>(CS->getOperand(1))) |
| 2178 | return 0; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2179 | |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2180 | // Init priority must be standard. |
Nick Lewycky | 2c44a80 | 2011-04-08 07:30:21 +0000 | [diff] [blame] | 2181 | ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0)); |
| 2182 | if (CI->getZExtValue() != 65535) |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2183 | return 0; |
| 2184 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2185 | |
Chris Lattner | f51a6cc | 2010-12-06 21:53:07 +0000 | [diff] [blame] | 2186 | return GV; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2189 | /// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand, |
| 2190 | /// return a list of the functions and null terminator as a vector. |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2191 | static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) { |
Nick Lewycky | 5ea5c61 | 2011-04-11 22:11:20 +0000 | [diff] [blame] | 2192 | if (GV->getInitializer()->isNullValue()) |
| 2193 | return std::vector<Function*>(); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2194 | ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); |
| 2195 | std::vector<Function*> Result; |
| 2196 | Result.reserve(CA->getNumOperands()); |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 2197 | for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) { |
| 2198 | ConstantStruct *CS = cast<ConstantStruct>(*i); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2199 | Result.push_back(dyn_cast<Function>(CS->getOperand(1))); |
| 2200 | } |
| 2201 | return Result; |
| 2202 | } |
| 2203 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2204 | /// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the |
| 2205 | /// specified array, returning the new global to use. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2206 | static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2207 | const std::vector<Function*> &Ctors) { |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2208 | // If we made a change, reassemble the initializer list. |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2209 | Constant *CSVals[2]; |
| 2210 | CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()), 65535); |
| 2211 | CSVals[1] = 0; |
| 2212 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2213 | StructType *StructTy = |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2214 | cast <StructType>( |
| 2215 | cast<ArrayType>(GCL->getType()->getElementType())->getElementType()); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2216 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2217 | // Create the new init list. |
| 2218 | std::vector<Constant*> CAList; |
| 2219 | for (unsigned i = 0, e = Ctors.size(); i != e; ++i) { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2220 | if (Ctors[i]) { |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2221 | CSVals[1] = Ctors[i]; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2222 | } else { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2223 | Type *FTy = FunctionType::get(Type::getVoidTy(GCL->getContext()), |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2224 | false); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2225 | PointerType *PFTy = PointerType::getUnqual(FTy); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2226 | CSVals[1] = Constant::getNullValue(PFTy); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2227 | CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()), |
Nick Lewycky | 5ea5c61 | 2011-04-11 22:11:20 +0000 | [diff] [blame] | 2228 | 0x7fffffff); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2229 | } |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2230 | CAList.push_back(ConstantStruct::get(StructTy, CSVals)); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2231 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2232 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2233 | // Create the array initializer. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2234 | Constant *CA = ConstantArray::get(ArrayType::get(StructTy, |
Nick Lewycky | c332fba | 2009-09-19 20:30:26 +0000 | [diff] [blame] | 2235 | CAList.size()), CAList); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2237 | // If we didn't change the number of elements, don't create a new GV. |
| 2238 | if (CA->getType() == GCL->getInitializer()->getType()) { |
| 2239 | GCL->setInitializer(CA); |
| 2240 | return GCL; |
| 2241 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2242 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2243 | // Create the new global and insert it next to the existing list. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2244 | GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(), |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2245 | GCL->getLinkage(), CA, "", |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 2246 | GCL->getThreadLocalMode()); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2247 | GCL->getParent()->getGlobalList().insert(GCL, NGV); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 2248 | NGV->takeName(GCL); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2249 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2250 | // Nuke the old list, replacing any uses with the new one. |
| 2251 | if (!GCL->use_empty()) { |
| 2252 | Constant *V = NGV; |
| 2253 | if (V->getType() != GCL->getType()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2254 | V = ConstantExpr::getBitCast(V, GCL->getType()); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2255 | GCL->replaceAllUsesWith(V); |
| 2256 | } |
| 2257 | GCL->eraseFromParent(); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2259 | if (Ctors.size()) |
| 2260 | return NGV; |
| 2261 | else |
| 2262 | return 0; |
| 2263 | } |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2264 | |
| 2265 | |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2266 | static inline bool |
| 2267 | isSimpleEnoughValueToCommit(Constant *C, |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2268 | SmallPtrSet<Constant*, 8> &SimpleConstants, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2269 | const DataLayout *TD); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2270 | |
| 2271 | |
| 2272 | /// isSimpleEnoughValueToCommit - Return true if the specified constant can be |
| 2273 | /// handled by the code generator. We don't want to generate something like: |
| 2274 | /// void *X = &X/42; |
| 2275 | /// because the code generator doesn't have a relocation that can handle that. |
| 2276 | /// |
| 2277 | /// This function should be called if C was not found (but just got inserted) |
| 2278 | /// in SimpleConstants to avoid having to rescan the same constants all the |
| 2279 | /// time. |
| 2280 | static bool isSimpleEnoughValueToCommitHelper(Constant *C, |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2281 | SmallPtrSet<Constant*, 8> &SimpleConstants, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2282 | const DataLayout *TD) { |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2283 | // Simple integer, undef, constant aggregate zero, global addresses, etc are |
| 2284 | // all supported. |
| 2285 | if (C->getNumOperands() == 0 || isa<BlockAddress>(C) || |
| 2286 | isa<GlobalValue>(C)) |
| 2287 | return true; |
| 2288 | |
| 2289 | // Aggregate values are safe if all their elements are. |
| 2290 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || |
| 2291 | isa<ConstantVector>(C)) { |
| 2292 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { |
| 2293 | Constant *Op = cast<Constant>(C->getOperand(i)); |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2294 | if (!isSimpleEnoughValueToCommit(Op, SimpleConstants, TD)) |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2295 | return false; |
| 2296 | } |
| 2297 | return true; |
| 2298 | } |
| 2299 | |
| 2300 | // We don't know exactly what relocations are allowed in constant expressions, |
| 2301 | // so we allow &global+constantoffset, which is safe and uniformly supported |
| 2302 | // across targets. |
| 2303 | ConstantExpr *CE = cast<ConstantExpr>(C); |
| 2304 | switch (CE->getOpcode()) { |
| 2305 | case Instruction::BitCast: |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2306 | // Bitcast is fine if the casted value is fine. |
| 2307 | return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); |
| 2308 | |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2309 | case Instruction::IntToPtr: |
| 2310 | case Instruction::PtrToInt: |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2311 | // int <=> ptr is fine if the int type is the same size as the |
| 2312 | // pointer type. |
| 2313 | if (!TD || TD->getTypeSizeInBits(CE->getType()) != |
| 2314 | TD->getTypeSizeInBits(CE->getOperand(0)->getType())) |
| 2315 | return false; |
| 2316 | return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2317 | |
| 2318 | // GEP is fine if it is simple + constant offset. |
| 2319 | case Instruction::GetElementPtr: |
| 2320 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 2321 | if (!isa<ConstantInt>(CE->getOperand(i))) |
| 2322 | return false; |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2323 | return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2324 | |
| 2325 | case Instruction::Add: |
| 2326 | // We allow simple+cst. |
| 2327 | if (!isa<ConstantInt>(CE->getOperand(1))) |
| 2328 | return false; |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2329 | return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2330 | } |
| 2331 | return false; |
| 2332 | } |
| 2333 | |
| 2334 | static inline bool |
| 2335 | isSimpleEnoughValueToCommit(Constant *C, |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2336 | SmallPtrSet<Constant*, 8> &SimpleConstants, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2337 | const DataLayout *TD) { |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2338 | // If we already checked this constant, we win. |
| 2339 | if (!SimpleConstants.insert(C)) return true; |
| 2340 | // Check the constant. |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2341 | return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, TD); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2345 | /// isSimpleEnoughPointerToCommit - Return true if this constant is simple |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2346 | /// enough for us to understand. In particular, if it is a cast to anything |
| 2347 | /// other than from one pointer type to another pointer type, we punt. |
| 2348 | /// We basically just support direct accesses to globals and GEP's of |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2349 | /// globals. This should be kept up to date with CommitValueTo. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2350 | static bool isSimpleEnoughPointerToCommit(Constant *C) { |
Dan Gohman | ce5de5b | 2009-09-07 22:42:05 +0000 | [diff] [blame] | 2351 | // Conservatively, avoid aggregate types. This is because we don't |
| 2352 | // want to worry about them partially overlapping other stores. |
| 2353 | if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType()) |
| 2354 | return false; |
| 2355 | |
Dan Gohman | fd54a89 | 2009-09-07 22:31:26 +0000 | [diff] [blame] | 2356 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
Mikhail Glushenkov | 99fca5d | 2010-10-19 16:47:23 +0000 | [diff] [blame] | 2357 | // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or |
Dan Gohman | fd54a89 | 2009-09-07 22:31:26 +0000 | [diff] [blame] | 2358 | // external globals. |
Mikhail Glushenkov | 99fca5d | 2010-10-19 16:47:23 +0000 | [diff] [blame] | 2359 | return GV->hasUniqueInitializer(); |
Dan Gohman | fd54a89 | 2009-09-07 22:31:26 +0000 | [diff] [blame] | 2360 | |
Owen Anderson | e95a32c | 2011-01-14 22:31:13 +0000 | [diff] [blame] | 2361 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2362 | // Handle a constantexpr gep. |
| 2363 | if (CE->getOpcode() == Instruction::GetElementPtr && |
Dan Gohman | c62482d | 2009-09-07 22:40:13 +0000 | [diff] [blame] | 2364 | isa<GlobalVariable>(CE->getOperand(0)) && |
| 2365 | cast<GEPOperator>(CE)->isInBounds()) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2366 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
Mikhail Glushenkov | 99fca5d | 2010-10-19 16:47:23 +0000 | [diff] [blame] | 2367 | // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or |
Dan Gohman | fd54a89 | 2009-09-07 22:31:26 +0000 | [diff] [blame] | 2368 | // external globals. |
Mikhail Glushenkov | 99fca5d | 2010-10-19 16:47:23 +0000 | [diff] [blame] | 2369 | if (!GV->hasUniqueInitializer()) |
Dan Gohman | fd54a89 | 2009-09-07 22:31:26 +0000 | [diff] [blame] | 2370 | return false; |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2371 | |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2372 | // The first index must be zero. |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 2373 | ConstantInt *CI = dyn_cast<ConstantInt>(*llvm::next(CE->op_begin())); |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2374 | if (!CI || !CI->isZero()) return false; |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2375 | |
| 2376 | // The remaining indices must be compile-time known integers within the |
Dan Gohman | e6992f7 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 2377 | // notional bounds of the corresponding static array types. |
| 2378 | if (!CE->isGEPWithNoNotionalOverIndexing()) |
| 2379 | return false; |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2380 | |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 2381 | return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE); |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2382 | |
| 2383 | // A constantexpr bitcast from a pointer to another pointer is a no-op, |
| 2384 | // and we know how to evaluate it by moving the bitcast from the pointer |
| 2385 | // operand to the value operand. |
| 2386 | } else if (CE->getOpcode() == Instruction::BitCast && |
Chris Lattner | d5f656f | 2011-01-16 02:05:10 +0000 | [diff] [blame] | 2387 | isa<GlobalVariable>(CE->getOperand(0))) { |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2388 | // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or |
| 2389 | // external globals. |
Chris Lattner | d5f656f | 2011-01-16 02:05:10 +0000 | [diff] [blame] | 2390 | return cast<GlobalVariable>(CE->getOperand(0))->hasUniqueInitializer(); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2391 | } |
Owen Anderson | e95a32c | 2011-01-14 22:31:13 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2394 | return false; |
| 2395 | } |
| 2396 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2397 | /// EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global |
| 2398 | /// initializer. This returns 'Init' modified to reflect 'Val' stored into it. |
| 2399 | /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into. |
| 2400 | static Constant *EvaluateStoreInto(Constant *Init, Constant *Val, |
Zhou Sheng | efcdb29 | 2012-12-01 10:54:28 +0000 | [diff] [blame] | 2401 | ConstantExpr *Addr, unsigned OpNo) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2402 | // Base case of the recursion. |
| 2403 | if (OpNo == Addr->getNumOperands()) { |
| 2404 | assert(Val->getType() == Init->getType() && "Type mismatch!"); |
| 2405 | return Val; |
| 2406 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2407 | |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2408 | SmallVector<Constant*, 32> Elts; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2409 | if (StructType *STy = dyn_cast<StructType>(Init->getType())) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2410 | // Break up the constant into its elements. |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 2411 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
| 2412 | Elts.push_back(Init->getAggregateElement(i)); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2414 | // Replace the element that we are supposed to. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2415 | ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo)); |
| 2416 | unsigned Idx = CU->getZExtValue(); |
| 2417 | assert(Idx < STy->getNumElements() && "Struct index out of range!"); |
Zhou Sheng | efcdb29 | 2012-12-01 10:54:28 +0000 | [diff] [blame] | 2418 | Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2420 | // Return the modified struct. |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2421 | return ConstantStruct::get(STy, Elts); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2422 | } |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2423 | |
| 2424 | ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2425 | SequentialType *InitTy = cast<SequentialType>(Init->getType()); |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2426 | |
| 2427 | uint64_t NumElts; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2428 | if (ArrayType *ATy = dyn_cast<ArrayType>(InitTy)) |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2429 | NumElts = ATy->getNumElements(); |
| 2430 | else |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 2431 | NumElts = InitTy->getVectorNumElements(); |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2432 | |
| 2433 | // Break up the array into elements. |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 2434 | for (uint64_t i = 0, e = NumElts; i != e; ++i) |
| 2435 | Elts.push_back(Init->getAggregateElement(i)); |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2436 | |
| 2437 | assert(CI->getZExtValue() < NumElts); |
| 2438 | Elts[CI->getZExtValue()] = |
Zhou Sheng | efcdb29 | 2012-12-01 10:54:28 +0000 | [diff] [blame] | 2439 | EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1); |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 2440 | |
| 2441 | if (Init->getType()->isArrayTy()) |
| 2442 | return ConstantArray::get(cast<ArrayType>(InitTy), Elts); |
| 2443 | return ConstantVector::get(Elts); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2446 | /// CommitValueTo - We have decided that Addr (which satisfies the predicate |
| 2447 | /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2448 | static void CommitValueTo(Constant *Val, Constant *Addr) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2449 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) { |
| 2450 | assert(GV->hasInitializer()); |
| 2451 | GV->setInitializer(Val); |
| 2452 | return; |
| 2453 | } |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2455 | ConstantExpr *CE = cast<ConstantExpr>(Addr); |
| 2456 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
Zhou Sheng | efcdb29 | 2012-12-01 10:54:28 +0000 | [diff] [blame] | 2457 | GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2)); |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Nick Lewycky | 7fa7677 | 2012-02-20 03:25:59 +0000 | [diff] [blame] | 2460 | namespace { |
| 2461 | |
| 2462 | /// Evaluator - This class evaluates LLVM IR, producing the Constant |
| 2463 | /// representing each SSA instruction. Changes to global variables are stored |
| 2464 | /// in a mapping that can be iterated over after the evaluation is complete. |
| 2465 | /// Once an evaluation call fails, the evaluation object should not be reused. |
| 2466 | class Evaluator { |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2467 | public: |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2468 | Evaluator(const DataLayout *TD, const TargetLibraryInfo *TLI) |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2469 | : TD(TD), TLI(TLI) { |
| 2470 | ValueStack.push_back(new DenseMap<Value*, Constant*>); |
| 2471 | } |
| 2472 | |
Nick Lewycky | 7fa7677 | 2012-02-20 03:25:59 +0000 | [diff] [blame] | 2473 | ~Evaluator() { |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2474 | DeleteContainerPointers(ValueStack); |
| 2475 | while (!AllocaTmps.empty()) { |
| 2476 | GlobalVariable *Tmp = AllocaTmps.back(); |
| 2477 | AllocaTmps.pop_back(); |
| 2478 | |
| 2479 | // If there are still users of the alloca, the program is doing something |
| 2480 | // silly, e.g. storing the address of the alloca somewhere and using it |
| 2481 | // later. Since this is undefined, we'll just make it be null. |
| 2482 | if (!Tmp->use_empty()) |
| 2483 | Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType())); |
| 2484 | delete Tmp; |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | /// EvaluateFunction - Evaluate a call to function F, returning true if |
| 2489 | /// successful, false if we can't evaluate it. ActualArgs contains the formal |
| 2490 | /// arguments for the function. |
| 2491 | bool EvaluateFunction(Function *F, Constant *&RetVal, |
| 2492 | const SmallVectorImpl<Constant*> &ActualArgs); |
| 2493 | |
| 2494 | /// EvaluateBlock - Evaluate all instructions in block BB, returning true if |
| 2495 | /// successful, false if we can't evaluate it. NewBB returns the next BB that |
| 2496 | /// control flows into, or null upon return. |
| 2497 | bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB); |
| 2498 | |
| 2499 | Constant *getVal(Value *V) { |
| 2500 | if (Constant *CV = dyn_cast<Constant>(V)) return CV; |
| 2501 | Constant *R = ValueStack.back()->lookup(V); |
| 2502 | assert(R && "Reference to an uncomputed value!"); |
| 2503 | return R; |
| 2504 | } |
| 2505 | |
| 2506 | void setVal(Value *V, Constant *C) { |
| 2507 | ValueStack.back()->operator[](V) = C; |
| 2508 | } |
| 2509 | |
| 2510 | const DenseMap<Constant*, Constant*> &getMutatedMemory() const { |
| 2511 | return MutatedMemory; |
| 2512 | } |
| 2513 | |
| 2514 | const SmallPtrSet<GlobalVariable*, 8> &getInvariants() const { |
| 2515 | return Invariants; |
| 2516 | } |
| 2517 | |
| 2518 | private: |
| 2519 | Constant *ComputeLoadResult(Constant *P); |
| 2520 | |
| 2521 | /// ValueStack - As we compute SSA register values, we store their contents |
| 2522 | /// here. The back of the vector contains the current function and the stack |
| 2523 | /// contains the values in the calling frames. |
| 2524 | SmallVector<DenseMap<Value*, Constant*>*, 4> ValueStack; |
| 2525 | |
| 2526 | /// CallStack - This is used to detect recursion. In pathological situations |
| 2527 | /// we could hit exponential behavior, but at least there is nothing |
| 2528 | /// unbounded. |
| 2529 | SmallVector<Function*, 4> CallStack; |
| 2530 | |
| 2531 | /// MutatedMemory - For each store we execute, we update this map. Loads |
| 2532 | /// check this to get the most up-to-date value. If evaluation is successful, |
| 2533 | /// this state is committed to the process. |
| 2534 | DenseMap<Constant*, Constant*> MutatedMemory; |
| 2535 | |
| 2536 | /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable |
| 2537 | /// to represent its body. This vector is needed so we can delete the |
| 2538 | /// temporary globals when we are done. |
| 2539 | SmallVector<GlobalVariable*, 32> AllocaTmps; |
| 2540 | |
| 2541 | /// Invariants - These global variables have been marked invariant by the |
| 2542 | /// static constructor. |
| 2543 | SmallPtrSet<GlobalVariable*, 8> Invariants; |
| 2544 | |
| 2545 | /// SimpleConstants - These are constants we have checked and know to be |
| 2546 | /// simple enough to live in a static initializer of a global. |
| 2547 | SmallPtrSet<Constant*, 8> SimpleConstants; |
| 2548 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2549 | const DataLayout *TD; |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2550 | const TargetLibraryInfo *TLI; |
| 2551 | }; |
| 2552 | |
Nick Lewycky | 7fa7677 | 2012-02-20 03:25:59 +0000 | [diff] [blame] | 2553 | } // anonymous namespace |
| 2554 | |
Chris Lattner | 562a055 | 2005-09-26 05:16:34 +0000 | [diff] [blame] | 2555 | /// ComputeLoadResult - Return the value that would be computed by a load from |
| 2556 | /// P after the stores reflected by 'memory' have been performed. If we can't |
| 2557 | /// decide, return null. |
Nick Lewycky | 7fa7677 | 2012-02-20 03:25:59 +0000 | [diff] [blame] | 2558 | Constant *Evaluator::ComputeLoadResult(Constant *P) { |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2559 | // If this memory location has been recently stored, use the stored value: it |
| 2560 | // is the most up-to-date. |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2561 | DenseMap<Constant*, Constant*>::const_iterator I = MutatedMemory.find(P); |
| 2562 | if (I != MutatedMemory.end()) return I->second; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2563 | |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2564 | // Access it. |
| 2565 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 2566 | if (GV->hasDefinitiveInitializer()) |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2567 | return GV->getInitializer(); |
| 2568 | return 0; |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2569 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2570 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2571 | // Handle a constantexpr getelementptr. |
| 2572 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P)) |
| 2573 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 2574 | isa<GlobalVariable>(CE->getOperand(0))) { |
| 2575 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 2576 | if (GV->hasDefinitiveInitializer()) |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 2577 | return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | return 0; // don't know how to evaluate. |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2583 | /// EvaluateBlock - Evaluate all instructions in block BB, returning true if |
| 2584 | /// successful, false if we can't evaluate it. NewBB returns the next BB that |
| 2585 | /// control flows into, or null upon return. |
Nick Lewycky | 7fa7677 | 2012-02-20 03:25:59 +0000 | [diff] [blame] | 2586 | bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, |
| 2587 | BasicBlock *&NextBB) { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2588 | // This is the main evaluation loop. |
| 2589 | while (1) { |
| 2590 | Constant *InstResult = 0; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2591 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2592 | if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) { |
Eli Friedman | 33cb445 | 2011-08-16 00:20:11 +0000 | [diff] [blame] | 2593 | if (!SI->isSimple()) return false; // no volatile/atomic accesses. |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2594 | Constant *Ptr = getVal(SI->getOperand(1)); |
Nick Lewycky | a641c07 | 2012-02-21 22:08:06 +0000 | [diff] [blame] | 2595 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 2596 | Ptr = ConstantFoldConstantExpression(CE, TD, TLI); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2597 | if (!isSimpleEnoughPointerToCommit(Ptr)) |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2598 | // If this is too complex for us to commit, reject it. |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2599 | return false; |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2600 | |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2601 | Constant *Val = getVal(SI->getOperand(0)); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2602 | |
| 2603 | // If this might be too difficult for the backend to handle (e.g. the addr |
| 2604 | // of one global variable divided by another) then we can't commit it. |
Eli Friedman | fb54ad1 | 2012-01-05 23:03:32 +0000 | [diff] [blame] | 2605 | if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, TD)) |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2606 | return false; |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2607 | |
| 2608 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 2609 | if (CE->getOpcode() == Instruction::BitCast) { |
| 2610 | // If we're evaluating a store through a bitcast, then we need |
| 2611 | // to pull the bitcast off the pointer type and push it onto the |
| 2612 | // stored value. |
Chris Lattner | d5f656f | 2011-01-16 02:05:10 +0000 | [diff] [blame] | 2613 | Ptr = CE->getOperand(0); |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2614 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2615 | Type *NewTy = cast<PointerType>(Ptr->getType())->getElementType(); |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2616 | |
Owen Anderson | 66f708f | 2011-01-16 04:33:33 +0000 | [diff] [blame] | 2617 | // In order to push the bitcast onto the stored value, a bitcast |
| 2618 | // from NewTy to Val's type must be legal. If it's not, we can try |
| 2619 | // introspecting NewTy to find a legal conversion. |
| 2620 | while (!Val->getType()->canLosslesslyBitCastTo(NewTy)) { |
| 2621 | // If NewTy is a struct, we can convert the pointer to the struct |
| 2622 | // into a pointer to its first member. |
| 2623 | // FIXME: This could be extended to support arrays as well. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2624 | if (StructType *STy = dyn_cast<StructType>(NewTy)) { |
Owen Anderson | 66f708f | 2011-01-16 04:33:33 +0000 | [diff] [blame] | 2625 | NewTy = STy->getTypeAtIndex(0U); |
| 2626 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2627 | IntegerType *IdxTy = IntegerType::get(NewTy->getContext(), 32); |
Owen Anderson | 66f708f | 2011-01-16 04:33:33 +0000 | [diff] [blame] | 2628 | Constant *IdxZero = ConstantInt::get(IdxTy, 0, false); |
| 2629 | Constant * const IdxList[] = {IdxZero, IdxZero}; |
| 2630 | |
Jay Foad | b4263a6 | 2011-07-22 08:52:50 +0000 | [diff] [blame] | 2631 | Ptr = ConstantExpr::getGetElementPtr(Ptr, IdxList); |
Nick Lewycky | a641c07 | 2012-02-21 22:08:06 +0000 | [diff] [blame] | 2632 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 2633 | Ptr = ConstantFoldConstantExpression(CE, TD, TLI); |
| 2634 | |
Owen Anderson | 66f708f | 2011-01-16 04:33:33 +0000 | [diff] [blame] | 2635 | // If we can't improve the situation by introspecting NewTy, |
| 2636 | // we have to give up. |
| 2637 | } else { |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2638 | return false; |
Owen Anderson | 66f708f | 2011-01-16 04:33:33 +0000 | [diff] [blame] | 2639 | } |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Owen Anderson | 66f708f | 2011-01-16 04:33:33 +0000 | [diff] [blame] | 2642 | // If we found compatible types, go ahead and push the bitcast |
| 2643 | // onto the stored value. |
Owen Anderson | cff6b37 | 2011-01-14 22:19:20 +0000 | [diff] [blame] | 2644 | Val = ConstantExpr::getBitCast(Val, NewTy); |
| 2645 | } |
| 2646 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2647 | MutatedMemory[Ptr] = Val; |
| 2648 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2649 | InstResult = ConstantExpr::get(BO->getOpcode(), |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2650 | getVal(BO->getOperand(0)), |
| 2651 | getVal(BO->getOperand(1))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2652 | } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2653 | InstResult = ConstantExpr::getCompare(CI->getPredicate(), |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2654 | getVal(CI->getOperand(0)), |
| 2655 | getVal(CI->getOperand(1))); |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2656 | } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2657 | InstResult = ConstantExpr::getCast(CI->getOpcode(), |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2658 | getVal(CI->getOperand(0)), |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2659 | CI->getType()); |
| 2660 | } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) { |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2661 | InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)), |
| 2662 | getVal(SI->getOperand(1)), |
| 2663 | getVal(SI->getOperand(2))); |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2664 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) { |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2665 | Constant *P = getVal(GEP->getOperand(0)); |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 2666 | SmallVector<Constant*, 8> GEPOps; |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 2667 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); |
| 2668 | i != e; ++i) |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2669 | GEPOps.push_back(getVal(*i)); |
Jay Foad | 4b5e207 | 2011-07-21 15:15:37 +0000 | [diff] [blame] | 2670 | InstResult = |
| 2671 | ConstantExpr::getGetElementPtr(P, GEPOps, |
| 2672 | cast<GEPOperator>(GEP)->isInBounds()); |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2673 | } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) { |
Eli Friedman | 33cb445 | 2011-08-16 00:20:11 +0000 | [diff] [blame] | 2674 | if (!LI->isSimple()) return false; // no volatile/atomic accesses. |
Nick Lewycky | a641c07 | 2012-02-21 22:08:06 +0000 | [diff] [blame] | 2675 | Constant *Ptr = getVal(LI->getOperand(0)); |
| 2676 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 2677 | Ptr = ConstantFoldConstantExpression(CE, TD, TLI); |
| 2678 | InstResult = ComputeLoadResult(Ptr); |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2679 | if (InstResult == 0) return false; // Could not evaluate load. |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2680 | } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) { |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2681 | if (AI->isArrayAllocation()) return false; // Cannot handle array allocs. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2682 | Type *Ty = AI->getType()->getElementType(); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2683 | AllocaTmps.push_back(new GlobalVariable(Ty, false, |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2684 | GlobalValue::InternalLinkage, |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2685 | UndefValue::get(Ty), |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2686 | AI->getName())); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2687 | InstResult = AllocaTmps.back(); |
Nick Lewycky | 132bd9c | 2012-02-12 05:09:35 +0000 | [diff] [blame] | 2688 | } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) { |
| 2689 | CallSite CS(CurInst); |
Devang Patel | 412a446 | 2009-03-09 23:04:12 +0000 | [diff] [blame] | 2690 | |
| 2691 | // Debug info can safely be ignored here. |
Nick Lewycky | 132bd9c | 2012-02-12 05:09:35 +0000 | [diff] [blame] | 2692 | if (isa<DbgInfoIntrinsic>(CS.getInstruction())) { |
Devang Patel | 412a446 | 2009-03-09 23:04:12 +0000 | [diff] [blame] | 2693 | ++CurInst; |
| 2694 | continue; |
| 2695 | } |
| 2696 | |
Chris Lattner | 7cd580f | 2006-07-07 21:37:01 +0000 | [diff] [blame] | 2697 | // Cannot handle inline asm. |
Nick Lewycky | 132bd9c | 2012-02-12 05:09:35 +0000 | [diff] [blame] | 2698 | if (isa<InlineAsm>(CS.getCalledValue())) return false; |
Chris Lattner | 7cd580f | 2006-07-07 21:37:01 +0000 | [diff] [blame] | 2699 | |
Nick Lewycky | 81266c5 | 2012-02-17 06:59:21 +0000 | [diff] [blame] | 2700 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) { |
| 2701 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) { |
| 2702 | if (MSI->isVolatile()) return false; |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2703 | Constant *Ptr = getVal(MSI->getDest()); |
| 2704 | Constant *Val = getVal(MSI->getValue()); |
| 2705 | Constant *DestVal = ComputeLoadResult(getVal(Ptr)); |
Nick Lewycky | 81266c5 | 2012-02-17 06:59:21 +0000 | [diff] [blame] | 2706 | if (Val->isNullValue() && DestVal && DestVal->isNullValue()) { |
| 2707 | // This memset is a no-op. |
| 2708 | ++CurInst; |
| 2709 | continue; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 2714 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 2715 | ++CurInst; |
| 2716 | continue; |
| 2717 | } |
| 2718 | |
| 2719 | if (II->getIntrinsicID() == Intrinsic::invariant_start) { |
| 2720 | // We don't insert an entry into Values, as it doesn't have a |
| 2721 | // meaningful return value. |
| 2722 | if (!II->use_empty()) |
| 2723 | return false; |
| 2724 | ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0)); |
Nick Lewycky | 0ef0557 | 2012-02-20 23:32:26 +0000 | [diff] [blame] | 2725 | Value *PtrArg = getVal(II->getArgOperand(1)); |
| 2726 | Value *Ptr = PtrArg->stripPointerCasts(); |
| 2727 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) { |
| 2728 | Type *ElemTy = cast<PointerType>(GV->getType())->getElementType(); |
| 2729 | if (!Size->isAllOnesValue() && |
| 2730 | Size->getValue().getLimitedValue() >= |
| 2731 | TD->getTypeStoreSize(ElemTy)) |
Nick Lewycky | 81266c5 | 2012-02-17 06:59:21 +0000 | [diff] [blame] | 2732 | Invariants.insert(GV); |
| 2733 | } |
| 2734 | // Continue even if we do nothing. |
Nick Lewycky | 1f237b0 | 2011-05-29 18:41:56 +0000 | [diff] [blame] | 2735 | ++CurInst; |
| 2736 | continue; |
| 2737 | } |
| 2738 | return false; |
| 2739 | } |
| 2740 | |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2741 | // Resolve function pointers. |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2742 | Function *Callee = dyn_cast<Function>(getVal(CS.getCalledValue())); |
Nick Lewycky | 132bd9c | 2012-02-12 05:09:35 +0000 | [diff] [blame] | 2743 | if (!Callee || Callee->mayBeOverridden()) |
| 2744 | return false; // Cannot resolve. |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2745 | |
Duncan Sands | fa6a1cf | 2009-08-17 14:33:27 +0000 | [diff] [blame] | 2746 | SmallVector<Constant*, 8> Formals; |
Nick Lewycky | 132bd9c | 2012-02-12 05:09:35 +0000 | [diff] [blame] | 2747 | for (User::op_iterator i = CS.arg_begin(), e = CS.arg_end(); i != e; ++i) |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2748 | Formals.push_back(getVal(*i)); |
Duncan Sands | fa6a1cf | 2009-08-17 14:33:27 +0000 | [diff] [blame] | 2749 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 2750 | if (Callee->isDeclaration()) { |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2751 | // If this is a function we can constant fold, do it. |
Chad Rosier | 00737bd | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 2752 | if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) { |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2753 | InstResult = C; |
| 2754 | } else { |
| 2755 | return false; |
| 2756 | } |
| 2757 | } else { |
| 2758 | if (Callee->getFunctionType()->isVarArg()) |
| 2759 | return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2760 | |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2761 | Constant *RetVal; |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2762 | // Execute the call, if successful, use the return value. |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2763 | ValueStack.push_back(new DenseMap<Value*, Constant*>); |
| 2764 | if (!EvaluateFunction(Callee, RetVal, Formals)) |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2765 | return false; |
Benjamin Kramer | 3bbf2b6 | 2012-02-27 12:48:24 +0000 | [diff] [blame] | 2766 | delete ValueStack.pop_back_val(); |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2767 | InstResult = RetVal; |
| 2768 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 2769 | } else if (isa<TerminatorInst>(CurInst)) { |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2770 | if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) { |
| 2771 | if (BI->isUnconditional()) { |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2772 | NextBB = BI->getSuccessor(0); |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2773 | } else { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2774 | ConstantInt *Cond = |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2775 | dyn_cast<ConstantInt>(getVal(BI->getCondition())); |
Chris Lattner | 97d1fad | 2007-01-12 18:30:11 +0000 | [diff] [blame] | 2776 | if (!Cond) return false; // Cannot determine. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2777 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2778 | NextBB = BI->getSuccessor(!Cond->getZExtValue()); |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2779 | } |
| 2780 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) { |
| 2781 | ConstantInt *Val = |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2782 | dyn_cast<ConstantInt>(getVal(SI->getCondition())); |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2783 | if (!Val) return false; // Cannot determine. |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 2784 | NextBB = SI->findCaseValue(Val).getCaseSuccessor(); |
Chris Lattner | b3d5a65 | 2009-10-29 05:51:50 +0000 | [diff] [blame] | 2785 | } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) { |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2786 | Value *Val = getVal(IBI->getAddress())->stripPointerCasts(); |
Chris Lattner | b3d5a65 | 2009-10-29 05:51:50 +0000 | [diff] [blame] | 2787 | if (BlockAddress *BA = dyn_cast<BlockAddress>(Val)) |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2788 | NextBB = BA->getBasicBlock(); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 2789 | else |
| 2790 | return false; // Cannot determine. |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2791 | } else if (isa<ReturnInst>(CurInst)) { |
| 2792 | NextBB = 0; |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2793 | } else { |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 2794 | // invoke, unwind, resume, unreachable. |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2795 | return false; // Cannot handle this terminator. |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2796 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2797 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2798 | // We succeeded at evaluating this block! |
| 2799 | return true; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2800 | } else { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2801 | // Did not know how to evaluate this! |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2802 | return false; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2803 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2804 | |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2805 | if (!CurInst->use_empty()) { |
| 2806 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult)) |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 2807 | InstResult = ConstantFoldConstantExpression(CE, TD, TLI); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2808 | |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2809 | setVal(CurInst, InstResult); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2810 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2811 | |
Dan Gohman | f1ce79f | 2012-03-13 18:01:37 +0000 | [diff] [blame] | 2812 | // If we just processed an invoke, we finished evaluating the block. |
| 2813 | if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) { |
| 2814 | NextBB = II->getNormalDest(); |
| 2815 | return true; |
| 2816 | } |
| 2817 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2818 | // Advance program counter. |
| 2819 | ++CurInst; |
| 2820 | } |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2823 | /// EvaluateFunction - Evaluate a call to function F, returning true if |
| 2824 | /// successful, false if we can't evaluate it. ActualArgs contains the formal |
| 2825 | /// arguments for the function. |
Nick Lewycky | 7fa7677 | 2012-02-20 03:25:59 +0000 | [diff] [blame] | 2826 | bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal, |
| 2827 | const SmallVectorImpl<Constant*> &ActualArgs) { |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2828 | // Check to see if this function is already executing (recursion). If so, |
| 2829 | // bail out. TODO: we might want to accept limited recursion. |
| 2830 | if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end()) |
| 2831 | return false; |
| 2832 | |
| 2833 | CallStack.push_back(F); |
| 2834 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2835 | // Initialize arguments to the incoming values specified. |
| 2836 | unsigned ArgNo = 0; |
| 2837 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; |
| 2838 | ++AI, ++ArgNo) |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2839 | setVal(AI, ActualArgs[ArgNo]); |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2840 | |
| 2841 | // ExecutedBlocks - We only handle non-looping, non-recursive code. As such, |
| 2842 | // we can only evaluate any one basic block at most once. This set keeps |
| 2843 | // track of what we have executed so we can detect recursive cases etc. |
| 2844 | SmallPtrSet<BasicBlock*, 32> ExecutedBlocks; |
| 2845 | |
| 2846 | // CurBB - The current basic block we're evaluating. |
| 2847 | BasicBlock *CurBB = F->begin(); |
| 2848 | |
Nick Lewycky | 8e4ba6b | 2012-02-12 00:47:24 +0000 | [diff] [blame] | 2849 | BasicBlock::iterator CurInst = CurBB->begin(); |
| 2850 | |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2851 | while (1) { |
Duncan Sands | 4b794f8 | 2012-02-23 08:23:06 +0000 | [diff] [blame] | 2852 | BasicBlock *NextBB = 0; // Initialized to avoid compiler warnings. |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2853 | if (!EvaluateBlock(CurInst, NextBB)) |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2854 | return false; |
| 2855 | |
| 2856 | if (NextBB == 0) { |
| 2857 | // Successfully running until there's no next block means that we found |
| 2858 | // the return. Fill it the return value and pop the call stack. |
| 2859 | ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator()); |
| 2860 | if (RI->getNumOperands()) |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2861 | RetVal = getVal(RI->getOperand(0)); |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2862 | CallStack.pop_back(); |
| 2863 | return true; |
| 2864 | } |
| 2865 | |
| 2866 | // Okay, we succeeded in evaluating this control flow. See if we have |
| 2867 | // executed the new block before. If so, we have a looping function, |
| 2868 | // which we cannot evaluate in reasonable time. |
| 2869 | if (!ExecutedBlocks.insert(NextBB)) |
| 2870 | return false; // looped! |
| 2871 | |
| 2872 | // Okay, we have never been in this block before. Check to see if there |
| 2873 | // are any PHI nodes. If so, evaluate them with information about where |
| 2874 | // we came from. |
| 2875 | PHINode *PN = 0; |
Nick Lewycky | 8e4ba6b | 2012-02-12 00:47:24 +0000 | [diff] [blame] | 2876 | for (CurInst = NextBB->begin(); |
| 2877 | (PN = dyn_cast<PHINode>(CurInst)); ++CurInst) |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2878 | setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB))); |
Nick Lewycky | da82fd4 | 2012-02-06 08:24:44 +0000 | [diff] [blame] | 2879 | |
| 2880 | // Advance to the next block. |
| 2881 | CurBB = NextBB; |
| 2882 | } |
| 2883 | } |
| 2884 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2885 | /// EvaluateStaticConstructor - Evaluate static constructors in the function, if |
| 2886 | /// we can. Return true if we can, false otherwise. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 2887 | static bool EvaluateStaticConstructor(Function *F, const DataLayout *TD, |
Chad Rosier | 00737bd | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 2888 | const TargetLibraryInfo *TLI) { |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2889 | // Call the function. |
Nick Lewycky | 7fa7677 | 2012-02-20 03:25:59 +0000 | [diff] [blame] | 2890 | Evaluator Eval(TD, TLI); |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2891 | Constant *RetValDummy; |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2892 | bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy, |
| 2893 | SmallVector<Constant*, 0>()); |
Chris Lattner | 1945d58 | 2010-12-07 04:33:29 +0000 | [diff] [blame] | 2894 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2895 | if (EvalSuccess) { |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2896 | // We succeeded at evaluation: commit the result. |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 2897 | DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '" |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2898 | << F->getName() << "' to " << Eval.getMutatedMemory().size() |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 2899 | << " stores.\n"); |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2900 | for (DenseMap<Constant*, Constant*>::const_iterator I = |
| 2901 | Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end(); |
Nick Lewycky | 3eab3c4 | 2012-06-24 04:07:14 +0000 | [diff] [blame] | 2902 | I != E; ++I) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2903 | CommitValueTo(I->second, I->first); |
Nick Lewycky | 23ec5d7 | 2012-02-19 23:26:27 +0000 | [diff] [blame] | 2904 | for (SmallPtrSet<GlobalVariable*, 8>::const_iterator I = |
| 2905 | Eval.getInvariants().begin(), E = Eval.getInvariants().end(); |
| 2906 | I != E; ++I) |
Nick Lewycky | 81266c5 | 2012-02-17 06:59:21 +0000 | [diff] [blame] | 2907 | (*I)->setConstant(true); |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2908 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2909 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2910 | return EvalSuccess; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2913 | /// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible. |
| 2914 | /// Return true if anything changed. |
| 2915 | bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { |
| 2916 | std::vector<Function*> Ctors = ParseGlobalCtors(GCL); |
| 2917 | bool MadeChange = false; |
| 2918 | if (Ctors.empty()) return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2919 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2920 | // Loop over global ctors, optimizing them when we can. |
| 2921 | for (unsigned i = 0; i != Ctors.size(); ++i) { |
| 2922 | Function *F = Ctors[i]; |
| 2923 | // Found a null terminator in the middle of the list, prune off the rest of |
| 2924 | // the list. |
Chris Lattner | 7d8e58f | 2005-09-26 02:19:27 +0000 | [diff] [blame] | 2925 | if (F == 0) { |
| 2926 | if (i != Ctors.size()-1) { |
| 2927 | Ctors.resize(i+1); |
| 2928 | MadeChange = true; |
| 2929 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2930 | break; |
| 2931 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2932 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2933 | // We cannot simplify external ctor functions. |
| 2934 | if (F->empty()) continue; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2935 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2936 | // If we can evaluate the ctor at compile time, do. |
Chad Rosier | 00737bd | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 2937 | if (EvaluateStaticConstructor(F, TD, TLI)) { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2938 | Ctors.erase(Ctors.begin()+i); |
| 2939 | MadeChange = true; |
| 2940 | --i; |
| 2941 | ++NumCtorsEvaluated; |
| 2942 | continue; |
| 2943 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2944 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2945 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2946 | if (!MadeChange) return false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 2947 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2948 | GCL = InstallGlobalCtors(GCL, Ctors); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2949 | return true; |
| 2950 | } |
| 2951 | |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 2952 | bool GlobalOpt::OptimizeGlobalAliases(Module &M) { |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 2953 | bool Changed = false; |
| 2954 | |
Duncan Sands | 177d84e | 2009-01-07 20:01:06 +0000 | [diff] [blame] | 2955 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2956 | I != E;) { |
| 2957 | Module::alias_iterator J = I++; |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 2958 | // Aliases without names cannot be referenced outside this module. |
| 2959 | if (!J->hasName() && !J->isDeclaration()) |
| 2960 | J->setLinkage(GlobalValue::InternalLinkage); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2961 | // If the aliasee may change at link time, nothing can be done - bail out. |
| 2962 | if (J->mayBeOverridden()) |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 2963 | continue; |
| 2964 | |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2965 | Constant *Aliasee = J->getAliasee(); |
| 2966 | GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts()); |
Duncan Sands | 95c5d0f | 2009-02-18 17:55:38 +0000 | [diff] [blame] | 2967 | Target->removeDeadConstantUsers(); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2968 | bool hasOneUse = Target->hasOneUse() && Aliasee->hasOneUse(); |
| 2969 | |
| 2970 | // Make all users of the alias use the aliasee instead. |
| 2971 | if (!J->use_empty()) { |
| 2972 | J->replaceAllUsesWith(Aliasee); |
| 2973 | ++NumAliasesResolved; |
| 2974 | Changed = true; |
| 2975 | } |
| 2976 | |
Duncan Sands | 7a154cf | 2009-12-08 10:10:20 +0000 | [diff] [blame] | 2977 | // If the alias is externally visible, we may still be able to simplify it. |
| 2978 | if (!J->hasLocalLinkage()) { |
| 2979 | // If the aliasee has internal linkage, give it the name and linkage |
| 2980 | // of the alias, and delete the alias. This turns: |
| 2981 | // define internal ... @f(...) |
| 2982 | // @a = alias ... @f |
| 2983 | // into: |
| 2984 | // define ... @a(...) |
| 2985 | if (!Target->hasLocalLinkage()) |
| 2986 | continue; |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2987 | |
Duncan Sands | 7a154cf | 2009-12-08 10:10:20 +0000 | [diff] [blame] | 2988 | // Do not perform the transform if multiple aliases potentially target the |
Gabor Greif | 2723691 | 2010-04-07 18:59:26 +0000 | [diff] [blame] | 2989 | // aliasee. This check also ensures that it is safe to replace the section |
Duncan Sands | 7a154cf | 2009-12-08 10:10:20 +0000 | [diff] [blame] | 2990 | // and other attributes of the aliasee with those of the alias. |
| 2991 | if (!hasOneUse) |
| 2992 | continue; |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2993 | |
Duncan Sands | 7a154cf | 2009-12-08 10:10:20 +0000 | [diff] [blame] | 2994 | // Give the aliasee the name, linkage and other attributes of the alias. |
| 2995 | Target->takeName(J); |
| 2996 | Target->setLinkage(J->getLinkage()); |
| 2997 | Target->GlobalValue::copyAttributesFrom(J); |
| 2998 | } |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2999 | |
| 3000 | // Delete the alias. |
| 3001 | M.getAliasList().erase(J); |
| 3002 | ++NumAliasesRemoved; |
| 3003 | Changed = true; |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | return Changed; |
| 3007 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 3008 | |
Nick Lewycky | 6a7df9a | 2012-02-12 02:15:20 +0000 | [diff] [blame] | 3009 | static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) { |
| 3010 | if (!TLI->has(LibFunc::cxa_atexit)) |
Nick Lewycky | 6f160d3 | 2012-02-12 02:17:18 +0000 | [diff] [blame] | 3011 | return 0; |
Nick Lewycky | 6a7df9a | 2012-02-12 02:15:20 +0000 | [diff] [blame] | 3012 | |
| 3013 | Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit)); |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3014 | |
| 3015 | if (!Fn) |
| 3016 | return 0; |
Nick Lewycky | 6a7df9a | 2012-02-12 02:15:20 +0000 | [diff] [blame] | 3017 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3018 | FunctionType *FTy = Fn->getFunctionType(); |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3019 | |
Anders Carlsson | 1f7c7ba | 2011-03-20 19:51:13 +0000 | [diff] [blame] | 3020 | // Checking that the function has the right return type, the right number of |
| 3021 | // parameters and that they all have pointer types should be enough. |
| 3022 | if (!FTy->getReturnType()->isIntegerTy() || |
| 3023 | FTy->getNumParams() != 3 || |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3024 | !FTy->getParamType(0)->isPointerTy() || |
| 3025 | !FTy->getParamType(1)->isPointerTy() || |
| 3026 | !FTy->getParamType(2)->isPointerTy()) |
| 3027 | return 0; |
| 3028 | |
| 3029 | return Fn; |
| 3030 | } |
| 3031 | |
| 3032 | /// cxxDtorIsEmpty - Returns whether the given function is an empty C++ |
| 3033 | /// destructor and can therefore be eliminated. |
| 3034 | /// Note that we assume that other optimization passes have already simplified |
| 3035 | /// the code so we only look for a function with a single basic block, where |
Benjamin Kramer | c1322a1 | 2012-02-09 16:28:15 +0000 | [diff] [blame] | 3036 | /// the only allowed instructions are 'ret', 'call' to an empty C++ dtor and |
| 3037 | /// other side-effect free instructions. |
Anders Carlsson | 372ec6a | 2011-03-20 20:16:43 +0000 | [diff] [blame] | 3038 | static bool cxxDtorIsEmpty(const Function &Fn, |
| 3039 | SmallPtrSet<const Function *, 8> &CalledFunctions) { |
Anders Carlsson | 1f7c7ba | 2011-03-20 19:51:13 +0000 | [diff] [blame] | 3040 | // FIXME: We could eliminate C++ destructors if they're readonly/readnone and |
Nick Lewycky | 35ee1c9 | 2011-03-21 02:26:01 +0000 | [diff] [blame] | 3041 | // nounwind, but that doesn't seem worth doing. |
Anders Carlsson | 1f7c7ba | 2011-03-20 19:51:13 +0000 | [diff] [blame] | 3042 | if (Fn.isDeclaration()) |
| 3043 | return false; |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3044 | |
| 3045 | if (++Fn.begin() != Fn.end()) |
| 3046 | return false; |
| 3047 | |
| 3048 | const BasicBlock &EntryBlock = Fn.getEntryBlock(); |
| 3049 | for (BasicBlock::const_iterator I = EntryBlock.begin(), E = EntryBlock.end(); |
| 3050 | I != E; ++I) { |
| 3051 | if (const CallInst *CI = dyn_cast<CallInst>(I)) { |
Anders Carlsson | b12caf3 | 2011-03-21 14:54:40 +0000 | [diff] [blame] | 3052 | // Ignore debug intrinsics. |
| 3053 | if (isa<DbgInfoIntrinsic>(CI)) |
| 3054 | continue; |
| 3055 | |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3056 | const Function *CalledFn = CI->getCalledFunction(); |
| 3057 | |
| 3058 | if (!CalledFn) |
| 3059 | return false; |
| 3060 | |
Anders Carlsson | 807bc2a | 2011-03-22 03:21:01 +0000 | [diff] [blame] | 3061 | SmallPtrSet<const Function *, 8> NewCalledFunctions(CalledFunctions); |
| 3062 | |
Anders Carlsson | 1f7c7ba | 2011-03-20 19:51:13 +0000 | [diff] [blame] | 3063 | // Don't treat recursive functions as empty. |
Anders Carlsson | 807bc2a | 2011-03-22 03:21:01 +0000 | [diff] [blame] | 3064 | if (!NewCalledFunctions.insert(CalledFn)) |
Anders Carlsson | 1f7c7ba | 2011-03-20 19:51:13 +0000 | [diff] [blame] | 3065 | return false; |
| 3066 | |
Anders Carlsson | 807bc2a | 2011-03-22 03:21:01 +0000 | [diff] [blame] | 3067 | if (!cxxDtorIsEmpty(*CalledFn, NewCalledFunctions)) |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3068 | return false; |
| 3069 | } else if (isa<ReturnInst>(*I)) |
Benjamin Kramer | d469274 | 2012-02-09 14:26:06 +0000 | [diff] [blame] | 3070 | return true; // We're done. |
| 3071 | else if (I->mayHaveSideEffects()) |
| 3072 | return false; // Destructor with side effects, bail. |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | return false; |
| 3076 | } |
| 3077 | |
| 3078 | bool GlobalOpt::OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) { |
| 3079 | /// Itanium C++ ABI p3.3.5: |
| 3080 | /// |
| 3081 | /// After constructing a global (or local static) object, that will require |
| 3082 | /// destruction on exit, a termination function is registered as follows: |
| 3083 | /// |
| 3084 | /// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 3085 | /// |
| 3086 | /// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the |
| 3087 | /// call f(p) when DSO d is unloaded, before all such termination calls |
| 3088 | /// registered before this one. It returns zero if registration is |
Nick Lewycky | 35ee1c9 | 2011-03-21 02:26:01 +0000 | [diff] [blame] | 3089 | /// successful, nonzero on failure. |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3090 | |
| 3091 | // This pass will look for calls to __cxa_atexit where the function is trivial |
| 3092 | // and remove them. |
| 3093 | bool Changed = false; |
| 3094 | |
| 3095 | for (Function::use_iterator I = CXAAtExitFn->use_begin(), |
| 3096 | E = CXAAtExitFn->use_end(); I != E;) { |
Anders Carlsson | 4f735ca | 2011-03-20 20:21:33 +0000 | [diff] [blame] | 3097 | // We're only interested in calls. Theoretically, we could handle invoke |
| 3098 | // instructions as well, but neither llvm-gcc nor clang generate invokes |
| 3099 | // to __cxa_atexit. |
Anders Carlsson | b12caf3 | 2011-03-21 14:54:40 +0000 | [diff] [blame] | 3100 | CallInst *CI = dyn_cast<CallInst>(*I++); |
| 3101 | if (!CI) |
Anders Carlsson | 4f735ca | 2011-03-20 20:21:33 +0000 | [diff] [blame] | 3102 | continue; |
| 3103 | |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3104 | Function *DtorFn = |
Anders Carlsson | b12caf3 | 2011-03-21 14:54:40 +0000 | [diff] [blame] | 3105 | dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts()); |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3106 | if (!DtorFn) |
| 3107 | continue; |
| 3108 | |
Anders Carlsson | 372ec6a | 2011-03-20 20:16:43 +0000 | [diff] [blame] | 3109 | SmallPtrSet<const Function *, 8> CalledFunctions; |
| 3110 | if (!cxxDtorIsEmpty(*DtorFn, CalledFunctions)) |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3111 | continue; |
| 3112 | |
| 3113 | // Just remove the call. |
Anders Carlsson | b12caf3 | 2011-03-21 14:54:40 +0000 | [diff] [blame] | 3114 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 3115 | CI->eraseFromParent(); |
Anders Carlsson | 1f7c7ba | 2011-03-20 19:51:13 +0000 | [diff] [blame] | 3116 | |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3117 | ++NumCXXDtorsRemoved; |
| 3118 | |
| 3119 | Changed |= true; |
| 3120 | } |
| 3121 | |
| 3122 | return Changed; |
| 3123 | } |
| 3124 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 3125 | bool GlobalOpt::runOnModule(Module &M) { |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 3126 | bool Changed = false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 3127 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3128 | TD = getAnalysisIfAvailable<DataLayout>(); |
Nick Lewycky | 6a7df9a | 2012-02-12 02:15:20 +0000 | [diff] [blame] | 3129 | TLI = &getAnalysis<TargetLibraryInfo>(); |
Nick Lewycky | 6a577f8 | 2012-02-12 01:13:18 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 3131 | // Try to find the llvm.globalctors list. |
| 3132 | GlobalVariable *GlobalCtors = FindGlobalCtors(M); |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 3133 | |
Nick Lewycky | 6a7df9a | 2012-02-12 02:15:20 +0000 | [diff] [blame] | 3134 | Function *CXAAtExitFn = FindCXAAtExit(M, TLI); |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3135 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 3136 | bool LocalChange = true; |
| 3137 | while (LocalChange) { |
| 3138 | LocalChange = false; |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 3139 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 3140 | // Delete functions that are trivially dead, ccc -> fastcc |
| 3141 | LocalChange |= OptimizeFunctions(M); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 3142 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 3143 | // Optimize global_ctors list. |
| 3144 | if (GlobalCtors) |
| 3145 | LocalChange |= OptimizeGlobalCtorsList(GlobalCtors); |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 3146 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 3147 | // Optimize non-address-taken globals. |
| 3148 | LocalChange |= OptimizeGlobalVars(M); |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 3149 | |
| 3150 | // Resolve aliases, when possible. |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 3151 | LocalChange |= OptimizeGlobalAliases(M); |
Anders Carlsson | a201c4c | 2011-03-20 17:59:11 +0000 | [diff] [blame] | 3152 | |
| 3153 | // Try to remove trivial global destructors. |
| 3154 | if (CXAAtExitFn) |
| 3155 | LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn); |
| 3156 | |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 3157 | Changed |= LocalChange; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 3158 | } |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 3159 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 3160 | // TODO: Move all global ctors functions to the end of the module for code |
| 3161 | // layout. |
Mikhail Glushenkov | 9d28fdd | 2010-10-18 21:16:00 +0000 | [diff] [blame] | 3162 | |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 3163 | return Changed; |
| 3164 | } |