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