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