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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 10 | // This pass transforms simple global variables that never have their address |
| 11 | // taken. If obviously true, it marks read/write globals as constant, deletes |
| 12 | // variables only stored to, etc. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "globalopt" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 18 | #include "llvm/CallingConv.h" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7d90a27 | 2004-02-27 18:09:25 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 22 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 77a2a9d | 2004-08-14 20:57:17 +0000 | [diff] [blame] | 30 | #include <set> |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 35 | Statistic<> NumMarked ("globalopt", "Number of globals marked constant"); |
| 36 | Statistic<> NumSRA ("globalopt", "Number of aggregate globals broken " |
| 37 | "into scalars"); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 38 | Statistic<> NumSubstitute("globalopt", |
| 39 | "Number of globals with initializers stored into them"); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 40 | Statistic<> NumDeleted ("globalopt", "Number of globals deleted"); |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 41 | Statistic<> NumFnDeleted("globalopt", "Number of functions deleted"); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 42 | Statistic<> NumGlobUses ("globalopt", "Number of global uses devirtualized"); |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 43 | Statistic<> NumLocalized("globalopt", "Number of globals localized"); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 44 | Statistic<> NumShrunkToBool("globalopt", |
| 45 | "Number of global vars shrunk to booleans"); |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 46 | Statistic<> NumFastCallFns("globalopt", |
| 47 | "Number of functions converted to fastcc"); |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 48 | Statistic<> NumCtorsEvaluated("globalopt","Number of static ctors evaluated"); |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 50 | struct GlobalOpt : public ModulePass { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 51 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 52 | AU.addRequired<TargetData>(); |
| 53 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 54 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 55 | bool runOnModule(Module &M); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 56 | |
| 57 | private: |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 58 | GlobalVariable *FindGlobalCtors(Module &M); |
| 59 | bool OptimizeFunctions(Module &M); |
| 60 | bool OptimizeGlobalVars(Module &M); |
| 61 | bool OptimizeGlobalCtorsList(GlobalVariable *&GCL); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 62 | bool ProcessInternalGlobal(GlobalVariable *GV, Module::global_iterator &GVI); |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 65 | RegisterOpt<GlobalOpt> X("globalopt", "Global Variable Optimizer"); |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 68 | ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); } |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 70 | /// GlobalStatus - As we analyze each global, keep track of some information |
| 71 | /// 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] | 72 | /// this info will be accurate. |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 73 | struct GlobalStatus { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 74 | /// isLoaded - True if the global is ever loaded. If the global isn't ever |
| 75 | /// loaded it can be deleted. |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 76 | bool isLoaded; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 77 | |
| 78 | /// StoredType - Keep track of what stores to the global look like. |
| 79 | /// |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 80 | enum StoredType { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 81 | /// NotStored - There is no store to this global. It can thus be marked |
| 82 | /// constant. |
| 83 | NotStored, |
| 84 | |
| 85 | /// isInitializerStored - This global is stored to, but the only thing |
| 86 | /// stored is the constant it was initialized with. This is only tracked |
| 87 | /// for scalar globals. |
| 88 | isInitializerStored, |
| 89 | |
| 90 | /// isStoredOnce - This global is stored to, but only its initializer and |
| 91 | /// one other value is ever stored to it. If this global isStoredOnce, we |
| 92 | /// track the value stored to it in StoredOnceValue below. This is only |
| 93 | /// tracked for scalar globals. |
| 94 | isStoredOnce, |
| 95 | |
| 96 | /// isStored - This global is stored to by multiple values or something else |
| 97 | /// that we cannot track. |
| 98 | isStored |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 99 | } StoredType; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 100 | |
| 101 | /// StoredOnceValue - If only one value (besides the initializer constant) is |
| 102 | /// ever stored to this global, keep track of what value it is. |
| 103 | Value *StoredOnceValue; |
| 104 | |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 105 | // AccessingFunction/HasMultipleAccessingFunctions - These start out |
| 106 | // null/false. When the first accessing function is noticed, it is recorded. |
| 107 | // When a second different accessing function is noticed, |
| 108 | // HasMultipleAccessingFunctions is set to true. |
| 109 | Function *AccessingFunction; |
| 110 | bool HasMultipleAccessingFunctions; |
| 111 | |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 112 | // HasNonInstructionUser - Set to true if this global has a user that is not |
| 113 | // an instruction (e.g. a constant expr or GV initializer). |
| 114 | bool HasNonInstructionUser; |
| 115 | |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 116 | /// isNotSuitableForSRA - Keep track of whether any SRA preventing users of |
| 117 | /// the global exist. Such users include GEP instruction with variable |
| 118 | /// indexes, and non-gep/load/store users like constant expr casts. |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 119 | bool isNotSuitableForSRA; |
Chris Lattner | 9ce3000 | 2004-07-20 03:58:07 +0000 | [diff] [blame] | 120 | |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 121 | GlobalStatus() : isLoaded(false), StoredType(NotStored), StoredOnceValue(0), |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 122 | AccessingFunction(0), HasMultipleAccessingFunctions(false), |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 123 | HasNonInstructionUser(false), isNotSuitableForSRA(false) {} |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 124 | }; |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 125 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | /// ConstantIsDead - Return true if the specified constant is (transitively) |
| 129 | /// dead. The constant may be used by other constants (e.g. constant arrays and |
| 130 | /// constant exprs) as long as they are dead, but it cannot be used by anything |
| 131 | /// else. |
| 132 | static bool ConstantIsDead(Constant *C) { |
| 133 | if (isa<GlobalValue>(C)) return false; |
| 134 | |
| 135 | for (Value::use_iterator UI = C->use_begin(), E = C->use_end(); UI != E; ++UI) |
| 136 | if (Constant *CU = dyn_cast<Constant>(*UI)) { |
| 137 | if (!ConstantIsDead(CU)) return false; |
| 138 | } else |
| 139 | return false; |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 144 | /// AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus |
| 145 | /// structure. If the global has its address taken, return true to indicate we |
| 146 | /// can't do anything with it. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 147 | /// |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 148 | static bool AnalyzeGlobal(Value *V, GlobalStatus &GS, |
| 149 | std::set<PHINode*> &PHIUsers) { |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 150 | 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] | 151 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 152 | GS.HasNonInstructionUser = true; |
| 153 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 154 | if (AnalyzeGlobal(CE, GS, PHIUsers)) return true; |
| 155 | if (CE->getOpcode() != Instruction::GetElementPtr) |
| 156 | GS.isNotSuitableForSRA = true; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 157 | else if (!GS.isNotSuitableForSRA) { |
| 158 | // Check to see if this ConstantExpr GEP is SRA'able. In particular, we |
| 159 | // don't like < 3 operand CE's, and we don't like non-constant integer |
| 160 | // indices. |
| 161 | if (CE->getNumOperands() < 3 || !CE->getOperand(1)->isNullValue()) |
| 162 | GS.isNotSuitableForSRA = true; |
| 163 | else { |
| 164 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 165 | if (!isa<ConstantInt>(CE->getOperand(i))) { |
| 166 | GS.isNotSuitableForSRA = true; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 172 | } else if (Instruction *I = dyn_cast<Instruction>(*UI)) { |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 173 | if (!GS.HasMultipleAccessingFunctions) { |
| 174 | Function *F = I->getParent()->getParent(); |
| 175 | if (GS.AccessingFunction == 0) |
| 176 | GS.AccessingFunction = F; |
| 177 | else if (GS.AccessingFunction != F) |
| 178 | GS.HasMultipleAccessingFunctions = true; |
| 179 | } |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 180 | if (isa<LoadInst>(I)) { |
| 181 | GS.isLoaded = true; |
| 182 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | 3602549 | 2004-10-07 06:01:25 +0000 | [diff] [blame] | 183 | // Don't allow a store OF the address, only stores TO the address. |
| 184 | if (SI->getOperand(0) == V) return true; |
| 185 | |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 186 | // If this is a direct store to the global (i.e., the global is a scalar |
| 187 | // value, not an aggregate), keep more specific information about |
| 188 | // stores. |
| 189 | if (GS.StoredType != GlobalStatus::isStored) |
| 190 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(SI->getOperand(1))){ |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 191 | Value *StoredVal = SI->getOperand(0); |
| 192 | if (StoredVal == GV->getInitializer()) { |
| 193 | if (GS.StoredType < GlobalStatus::isInitializerStored) |
| 194 | GS.StoredType = GlobalStatus::isInitializerStored; |
| 195 | } else if (isa<LoadInst>(StoredVal) && |
| 196 | cast<LoadInst>(StoredVal)->getOperand(0) == GV) { |
| 197 | // G = G |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 198 | if (GS.StoredType < GlobalStatus::isInitializerStored) |
| 199 | GS.StoredType = GlobalStatus::isInitializerStored; |
| 200 | } else if (GS.StoredType < GlobalStatus::isStoredOnce) { |
| 201 | GS.StoredType = GlobalStatus::isStoredOnce; |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 202 | GS.StoredOnceValue = StoredVal; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 203 | } else if (GS.StoredType == GlobalStatus::isStoredOnce && |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 204 | GS.StoredOnceValue == StoredVal) { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 205 | // noop. |
| 206 | } else { |
| 207 | GS.StoredType = GlobalStatus::isStored; |
| 208 | } |
| 209 | } else { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 210 | GS.StoredType = GlobalStatus::isStored; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +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 | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 214 | |
| 215 | // If the first two indices are constants, this can be SRA'd. |
| 216 | if (isa<GlobalVariable>(I->getOperand(0))) { |
| 217 | if (I->getNumOperands() < 3 || !isa<Constant>(I->getOperand(1)) || |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 218 | !cast<Constant>(I->getOperand(1))->isNullValue() || |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 219 | !isa<ConstantInt>(I->getOperand(2))) |
| 220 | GS.isNotSuitableForSRA = true; |
| 221 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I->getOperand(0))){ |
| 222 | if (CE->getOpcode() != Instruction::GetElementPtr || |
| 223 | CE->getNumOperands() < 3 || I->getNumOperands() < 2 || |
| 224 | !isa<Constant>(I->getOperand(0)) || |
| 225 | !cast<Constant>(I->getOperand(0))->isNullValue()) |
| 226 | GS.isNotSuitableForSRA = true; |
| 227 | } else { |
| 228 | GS.isNotSuitableForSRA = true; |
| 229 | } |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 230 | } else if (isa<SelectInst>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 231 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
| 232 | GS.isNotSuitableForSRA = true; |
| 233 | } else if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 234 | // PHI nodes we can check just like select or GEP instructions, but we |
| 235 | // have to be careful about infinite recursion. |
| 236 | if (PHIUsers.insert(PN).second) // Not already visited. |
| 237 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
| 238 | GS.isNotSuitableForSRA = true; |
| 239 | } else if (isa<SetCondInst>(I)) { |
| 240 | GS.isNotSuitableForSRA = true; |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 241 | } else if (isa<MemCpyInst>(I) || isa<MemMoveInst>(I)) { |
| 242 | if (I->getOperand(1) == V) |
| 243 | GS.StoredType = GlobalStatus::isStored; |
| 244 | if (I->getOperand(2) == V) |
| 245 | GS.isLoaded = true; |
| 246 | GS.isNotSuitableForSRA = true; |
| 247 | } else if (isa<MemSetInst>(I)) { |
| 248 | assert(I->getOperand(1) == V && "Memset only takes one pointer!"); |
| 249 | GS.StoredType = GlobalStatus::isStored; |
| 250 | GS.isNotSuitableForSRA = true; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 251 | } else { |
| 252 | return true; // Any other non-load instruction might take address! |
Chris Lattner | 9ce3000 | 2004-07-20 03:58:07 +0000 | [diff] [blame] | 253 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 254 | } else if (Constant *C = dyn_cast<Constant>(*UI)) { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 255 | GS.HasNonInstructionUser = true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 256 | // We might have a dead and dangling constant hanging off of here. |
| 257 | if (!ConstantIsDead(C)) |
| 258 | return true; |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 259 | } else { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 260 | GS.HasNonInstructionUser = true; |
| 261 | // Otherwise must be some other user. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 262 | return true; |
| 263 | } |
| 264 | |
| 265 | return false; |
| 266 | } |
| 267 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 268 | static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) { |
| 269 | ConstantInt *CI = dyn_cast<ConstantInt>(Idx); |
| 270 | if (!CI) return 0; |
Chris Lattner | 4d0801b | 2005-01-08 19:45:31 +0000 | [diff] [blame] | 271 | unsigned IdxV = (unsigned)CI->getRawValue(); |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 273 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg)) { |
| 274 | if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV); |
| 275 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) { |
| 276 | if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV); |
| 277 | } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(Agg)) { |
| 278 | if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 279 | } else if (isa<ConstantAggregateZero>(Agg)) { |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 280 | if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) { |
| 281 | if (IdxV < STy->getNumElements()) |
| 282 | return Constant::getNullValue(STy->getElementType(IdxV)); |
| 283 | } else if (const SequentialType *STy = |
| 284 | dyn_cast<SequentialType>(Agg->getType())) { |
| 285 | return Constant::getNullValue(STy->getElementType()); |
| 286 | } |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 287 | } else if (isa<UndefValue>(Agg)) { |
| 288 | if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) { |
| 289 | if (IdxV < STy->getNumElements()) |
| 290 | return UndefValue::get(STy->getElementType(IdxV)); |
| 291 | } else if (const SequentialType *STy = |
| 292 | dyn_cast<SequentialType>(Agg->getType())) { |
| 293 | return UndefValue::get(STy->getElementType()); |
| 294 | } |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 295 | } |
| 296 | return 0; |
| 297 | } |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 299 | |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 300 | /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all |
| 301 | /// 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] | 302 | /// quick scan over the use list to clean up the easy and obvious cruft. This |
| 303 | /// returns true if it made a change. |
| 304 | static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) { |
| 305 | bool Changed = false; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 306 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) { |
| 307 | User *U = *UI++; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 309 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 310 | if (Init) { |
| 311 | // Replace the load with the initializer. |
| 312 | LI->replaceAllUsesWith(Init); |
| 313 | LI->eraseFromParent(); |
| 314 | Changed = true; |
| 315 | } |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 316 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 317 | // Store must be unreachable or storing Init into the global. |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 318 | SI->eraseFromParent(); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 319 | Changed = true; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 320 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
| 321 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | aae4a1c | 2005-09-26 07:34:35 +0000 | [diff] [blame] | 322 | Constant *SubInit = 0; |
| 323 | if (Init) |
| 324 | SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 325 | Changed |= CleanupConstantGlobalUsers(CE, SubInit); |
| 326 | } else if (CE->getOpcode() == Instruction::Cast && |
| 327 | isa<PointerType>(CE->getType())) { |
| 328 | // Pointer cast, delete any stores and memsets to the global. |
| 329 | Changed |= CleanupConstantGlobalUsers(CE, 0); |
| 330 | } |
| 331 | |
| 332 | if (CE->use_empty()) { |
| 333 | CE->destroyConstant(); |
| 334 | Changed = true; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 335 | } |
| 336 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
Chris Lattner | 0b142e3 | 2005-09-26 05:34:07 +0000 | [diff] [blame] | 337 | Constant *SubInit = 0; |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 338 | ConstantExpr *CE = |
| 339 | dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP)); |
Chris Lattner | 0b142e3 | 2005-09-26 05:34:07 +0000 | [diff] [blame] | 340 | if (CE && CE->getOpcode() == Instruction::GetElementPtr) |
| 341 | SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 342 | Changed |= CleanupConstantGlobalUsers(GEP, SubInit); |
Chris Lattner | c4d81b0 | 2004-10-10 16:47:33 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 344 | if (GEP->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 345 | GEP->eraseFromParent(); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 346 | Changed = true; |
| 347 | } |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 348 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv |
| 349 | if (MI->getRawDest() == V) { |
| 350 | MI->eraseFromParent(); |
| 351 | Changed = true; |
| 352 | } |
| 353 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 354 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
| 355 | // If we have a chain of dead constantexprs or other things dangling from |
| 356 | // us, and if they are all dead, nuke them without remorse. |
| 357 | if (ConstantIsDead(C)) { |
| 358 | C->destroyConstant(); |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 359 | // This could have invalidated UI, start over from scratch. |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 360 | CleanupConstantGlobalUsers(V, Init); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 361 | return true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 365 | return Changed; |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 368 | /// SRAGlobal - Perform scalar replacement of aggregates on the specified global |
| 369 | /// variable. This opens the door for other optimizations by exposing the |
| 370 | /// behavior of the program in a more fine-grained way. We have determined that |
| 371 | /// this transformation is safe already. We return the first global variable we |
| 372 | /// insert so that the caller can reprocess it. |
| 373 | static GlobalVariable *SRAGlobal(GlobalVariable *GV) { |
| 374 | assert(GV->hasInternalLinkage() && !GV->isConstant()); |
| 375 | Constant *Init = GV->getInitializer(); |
| 376 | const Type *Ty = Init->getType(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 378 | std::vector<GlobalVariable*> NewGlobals; |
| 379 | Module::GlobalListType &Globals = GV->getParent()->getGlobalList(); |
| 380 | |
| 381 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 382 | NewGlobals.reserve(STy->getNumElements()); |
| 383 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 384 | Constant *In = getAggregateConstantElement(Init, |
| 385 | ConstantUInt::get(Type::UIntTy, i)); |
| 386 | assert(In && "Couldn't get element of initializer?"); |
| 387 | GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false, |
| 388 | GlobalVariable::InternalLinkage, |
| 389 | In, GV->getName()+"."+utostr(i)); |
| 390 | Globals.insert(GV, NGV); |
| 391 | NewGlobals.push_back(NGV); |
| 392 | } |
| 393 | } else if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) { |
| 394 | unsigned NumElements = 0; |
| 395 | if (const ArrayType *ATy = dyn_cast<ArrayType>(STy)) |
| 396 | NumElements = ATy->getNumElements(); |
| 397 | else if (const PackedType *PTy = dyn_cast<PackedType>(STy)) |
| 398 | NumElements = PTy->getNumElements(); |
| 399 | else |
| 400 | assert(0 && "Unknown aggregate sequential type!"); |
| 401 | |
Chris Lattner | 1f21ef1 | 2005-02-23 16:53:04 +0000 | [diff] [blame] | 402 | if (NumElements > 16 && GV->hasNUsesOrMore(16)) |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 403 | return 0; // It's not worth it. |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 404 | NewGlobals.reserve(NumElements); |
| 405 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
| 406 | Constant *In = getAggregateConstantElement(Init, |
| 407 | ConstantUInt::get(Type::UIntTy, i)); |
| 408 | assert(In && "Couldn't get element of initializer?"); |
| 409 | |
| 410 | GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false, |
| 411 | GlobalVariable::InternalLinkage, |
| 412 | In, GV->getName()+"."+utostr(i)); |
| 413 | Globals.insert(GV, NGV); |
| 414 | NewGlobals.push_back(NGV); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (NewGlobals.empty()) |
| 419 | return 0; |
| 420 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 421 | DEBUG(std::cerr << "PERFORMING GLOBAL SRA ON: " << *GV); |
| 422 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 423 | Constant *NullInt = Constant::getNullValue(Type::IntTy); |
| 424 | |
| 425 | // Loop over all of the uses of the global, replacing the constantexpr geps, |
| 426 | // with smaller constantexpr geps or direct references. |
| 427 | while (!GV->use_empty()) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 428 | User *GEP = GV->use_back(); |
| 429 | assert(((isa<ConstantExpr>(GEP) && |
| 430 | cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)|| |
| 431 | isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 433 | // Ignore the 1th operand, which has to be zero or else the program is quite |
| 434 | // broken (undefined). Get the 2nd operand, which is the structure or array |
| 435 | // index. |
Chris Lattner | 4d0801b | 2005-01-08 19:45:31 +0000 | [diff] [blame] | 436 | unsigned Val = |
| 437 | (unsigned)cast<ConstantInt>(GEP->getOperand(2))->getRawValue(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 438 | if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access. |
| 439 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 440 | Value *NewPtr = NewGlobals[Val]; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 441 | |
| 442 | // Form a shorter GEP if needed. |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 443 | if (GEP->getNumOperands() > 3) |
| 444 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) { |
| 445 | std::vector<Constant*> Idxs; |
| 446 | Idxs.push_back(NullInt); |
| 447 | for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i) |
| 448 | Idxs.push_back(CE->getOperand(i)); |
| 449 | NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs); |
| 450 | } else { |
| 451 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP); |
| 452 | std::vector<Value*> Idxs; |
| 453 | Idxs.push_back(NullInt); |
| 454 | for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) |
| 455 | Idxs.push_back(GEPI->getOperand(i)); |
| 456 | NewPtr = new GetElementPtrInst(NewPtr, Idxs, |
| 457 | GEPI->getName()+"."+utostr(Val), GEPI); |
| 458 | } |
| 459 | GEP->replaceAllUsesWith(NewPtr); |
| 460 | |
| 461 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP)) |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 462 | GEPI->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 463 | else |
| 464 | cast<ConstantExpr>(GEP)->destroyConstant(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Chris Lattner | e40e2d1 | 2004-10-08 20:25:55 +0000 | [diff] [blame] | 467 | // Delete the old global, now that it is dead. |
| 468 | Globals.erase(GV); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 469 | ++NumSRA; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 470 | |
| 471 | // Loop over the new globals array deleting any globals that are obviously |
| 472 | // dead. This can arise due to scalarization of a structure or an array that |
| 473 | // has elements that are dead. |
| 474 | unsigned FirstGlobal = 0; |
| 475 | for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i) |
| 476 | if (NewGlobals[i]->use_empty()) { |
| 477 | Globals.erase(NewGlobals[i]); |
| 478 | if (FirstGlobal == i) ++FirstGlobal; |
| 479 | } |
| 480 | |
| 481 | return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 484 | /// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified |
| 485 | /// value will trap if the value is dynamically null. |
| 486 | static bool AllUsesOfValueWillTrapIfNull(Value *V) { |
| 487 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 488 | if (isa<LoadInst>(*UI)) { |
| 489 | // Will trap. |
| 490 | } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 491 | if (SI->getOperand(0) == V) { |
| 492 | //std::cerr << "NONTRAPPING USE: " << **UI; |
| 493 | return false; // Storing the value. |
| 494 | } |
| 495 | } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 496 | if (CI->getOperand(0) != V) { |
| 497 | //std::cerr << "NONTRAPPING USE: " << **UI; |
| 498 | return false; // Not calling the ptr |
| 499 | } |
| 500 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
| 501 | if (II->getOperand(0) != V) { |
| 502 | //std::cerr << "NONTRAPPING USE: " << **UI; |
| 503 | return false; // Not calling the ptr |
| 504 | } |
| 505 | } else if (CastInst *CI = dyn_cast<CastInst>(*UI)) { |
| 506 | if (!AllUsesOfValueWillTrapIfNull(CI)) return false; |
| 507 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) { |
| 508 | if (!AllUsesOfValueWillTrapIfNull(GEPI)) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 509 | } else if (isa<SetCondInst>(*UI) && |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 510 | isa<ConstantPointerNull>(UI->getOperand(1))) { |
| 511 | // Ignore setcc X, null |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 512 | } else { |
| 513 | //std::cerr << "NONTRAPPING USE: " << **UI; |
| 514 | return false; |
| 515 | } |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | /// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 520 | /// from GV will trap if the loaded value is null. Note that this also permits |
| 521 | /// comparisons of the loaded value against null, as a special case. |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 522 | static bool AllUsesOfLoadedValueWillTrapIfNull(GlobalVariable *GV) { |
| 523 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI!=E; ++UI) |
| 524 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
| 525 | if (!AllUsesOfValueWillTrapIfNull(LI)) |
| 526 | return false; |
| 527 | } else if (isa<StoreInst>(*UI)) { |
| 528 | // Ignore stores to the global. |
| 529 | } else { |
| 530 | // We don't know or understand this user, bail out. |
| 531 | //std::cerr << "UNKNOWN USER OF GLOBAL!: " << **UI; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | return true; |
| 536 | } |
| 537 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 538 | static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) { |
| 539 | bool Changed = false; |
| 540 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) { |
| 541 | Instruction *I = cast<Instruction>(*UI++); |
| 542 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 543 | LI->setOperand(0, NewV); |
| 544 | Changed = true; |
| 545 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 546 | if (SI->getOperand(1) == V) { |
| 547 | SI->setOperand(1, NewV); |
| 548 | Changed = true; |
| 549 | } |
| 550 | } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
| 551 | if (I->getOperand(0) == V) { |
| 552 | // Calling through the pointer! Turn into a direct call, but be careful |
| 553 | // that the pointer is not also being passed as an argument. |
| 554 | I->setOperand(0, NewV); |
| 555 | Changed = true; |
| 556 | bool PassedAsArg = false; |
| 557 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 558 | if (I->getOperand(i) == V) { |
| 559 | PassedAsArg = true; |
| 560 | I->setOperand(i, NewV); |
| 561 | } |
| 562 | |
| 563 | if (PassedAsArg) { |
| 564 | // Being passed as an argument also. Be careful to not invalidate UI! |
| 565 | UI = V->use_begin(); |
| 566 | } |
| 567 | } |
| 568 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 569 | Changed |= OptimizeAwayTrappingUsesOfValue(CI, |
| 570 | ConstantExpr::getCast(NewV, CI->getType())); |
| 571 | if (CI->use_empty()) { |
| 572 | Changed = true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 573 | CI->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 574 | } |
| 575 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
| 576 | // Should handle GEP here. |
| 577 | std::vector<Constant*> Indices; |
| 578 | Indices.reserve(GEPI->getNumOperands()-1); |
| 579 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 580 | if (Constant *C = dyn_cast<Constant>(GEPI->getOperand(i))) |
| 581 | Indices.push_back(C); |
| 582 | else |
| 583 | break; |
| 584 | if (Indices.size() == GEPI->getNumOperands()-1) |
| 585 | Changed |= OptimizeAwayTrappingUsesOfValue(GEPI, |
| 586 | ConstantExpr::getGetElementPtr(NewV, Indices)); |
| 587 | if (GEPI->use_empty()) { |
| 588 | Changed = true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 589 | GEPI->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | return Changed; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | /// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null |
| 599 | /// value stored into it. If there are uses of the loaded value that would trap |
| 600 | /// if the loaded value is dynamically null, then we know that they cannot be |
| 601 | /// reachable with a null optimize away the load. |
| 602 | static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV) { |
| 603 | std::vector<LoadInst*> Loads; |
| 604 | bool Changed = false; |
| 605 | |
| 606 | // Replace all uses of loads with uses of uses of the stored value. |
| 607 | for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); |
| 608 | GUI != E; ++GUI) |
| 609 | if (LoadInst *LI = dyn_cast<LoadInst>(*GUI)) { |
| 610 | Loads.push_back(LI); |
| 611 | Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV); |
| 612 | } else { |
| 613 | assert(isa<StoreInst>(*GUI) && "Only expect load and stores!"); |
| 614 | } |
| 615 | |
| 616 | if (Changed) { |
| 617 | DEBUG(std::cerr << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV); |
| 618 | ++NumGlobUses; |
| 619 | } |
| 620 | |
| 621 | // Delete all of the loads we can, keeping track of whether we nuked them all! |
| 622 | bool AllLoadsGone = true; |
| 623 | while (!Loads.empty()) { |
| 624 | LoadInst *L = Loads.back(); |
| 625 | if (L->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 626 | L->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 627 | Changed = true; |
| 628 | } else { |
| 629 | AllLoadsGone = false; |
| 630 | } |
| 631 | Loads.pop_back(); |
| 632 | } |
| 633 | |
| 634 | // If we nuked all of the loads, then none of the stores are needed either, |
| 635 | // nor is the global. |
| 636 | if (AllLoadsGone) { |
| 637 | DEBUG(std::cerr << " *** GLOBAL NOW DEAD!\n"); |
| 638 | CleanupConstantGlobalUsers(GV, 0); |
| 639 | if (GV->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 640 | GV->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 641 | ++NumDeleted; |
| 642 | } |
| 643 | Changed = true; |
| 644 | } |
| 645 | return Changed; |
| 646 | } |
| 647 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 648 | /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the |
| 649 | /// instructions that are foldable. |
| 650 | static void ConstantPropUsersOf(Value *V) { |
| 651 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) |
| 652 | if (Instruction *I = dyn_cast<Instruction>(*UI++)) |
| 653 | if (Constant *NewC = ConstantFoldInstruction(I)) { |
| 654 | I->replaceAllUsesWith(NewC); |
| 655 | |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 656 | // Advance UI to the next non-I use to avoid invalidating it! |
| 657 | // Instructions could multiply use V. |
| 658 | while (UI != E && *UI == I) |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 659 | ++UI; |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 660 | I->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | /// OptimizeGlobalAddressOfMalloc - This function takes the specified global |
| 665 | /// variable, and transforms the program as if it always contained the result of |
| 666 | /// the specified malloc. Because it is always the result of the specified |
| 667 | /// malloc, there is no reason to actually DO the malloc. Instead, turn the |
| 668 | /// malloc into a global, and any laods of GV as uses of the new global. |
| 669 | static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, |
| 670 | MallocInst *MI) { |
| 671 | DEBUG(std::cerr << "PROMOTING MALLOC GLOBAL: " << *GV << " MALLOC = " <<*MI); |
| 672 | ConstantInt *NElements = cast<ConstantInt>(MI->getArraySize()); |
| 673 | |
| 674 | if (NElements->getRawValue() != 1) { |
| 675 | // If we have an array allocation, transform it to a single element |
| 676 | // allocation to make the code below simpler. |
| 677 | Type *NewTy = ArrayType::get(MI->getAllocatedType(), |
Chris Lattner | 4d0801b | 2005-01-08 19:45:31 +0000 | [diff] [blame] | 678 | (unsigned)NElements->getRawValue()); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 679 | MallocInst *NewMI = |
| 680 | new MallocInst(NewTy, Constant::getNullValue(Type::UIntTy), |
| 681 | MI->getName(), MI); |
| 682 | std::vector<Value*> Indices; |
| 683 | Indices.push_back(Constant::getNullValue(Type::IntTy)); |
| 684 | Indices.push_back(Indices[0]); |
| 685 | Value *NewGEP = new GetElementPtrInst(NewMI, Indices, |
| 686 | NewMI->getName()+".el0", MI); |
| 687 | MI->replaceAllUsesWith(NewGEP); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 688 | MI->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 689 | MI = NewMI; |
| 690 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 692 | // Create the new global variable. The contents of the malloc'd memory is |
| 693 | // undefined, so initialize with an undef value. |
| 694 | Constant *Init = UndefValue::get(MI->getAllocatedType()); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 695 | GlobalVariable *NewGV = new GlobalVariable(MI->getAllocatedType(), false, |
| 696 | GlobalValue::InternalLinkage, Init, |
| 697 | GV->getName()+".body"); |
| 698 | GV->getParent()->getGlobalList().insert(GV, NewGV); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 700 | // Anything that used the malloc now uses the global directly. |
| 701 | MI->replaceAllUsesWith(NewGV); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 702 | |
| 703 | Constant *RepValue = NewGV; |
| 704 | if (NewGV->getType() != GV->getType()->getElementType()) |
| 705 | RepValue = ConstantExpr::getCast(RepValue, GV->getType()->getElementType()); |
| 706 | |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 707 | // If there is a comparison against null, we will insert a global bool to |
| 708 | // keep track of whether the global was initialized yet or not. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 709 | GlobalVariable *InitBool = |
| 710 | new GlobalVariable(Type::BoolTy, false, GlobalValue::InternalLinkage, |
Chris Lattner | bc965b9 | 2004-12-02 06:25:58 +0000 | [diff] [blame] | 711 | ConstantBool::False, GV->getName()+".init"); |
| 712 | bool InitBoolUsed = false; |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 714 | // Loop over all uses of GV, processing them in turn. |
Chris Lattner | bc965b9 | 2004-12-02 06:25:58 +0000 | [diff] [blame] | 715 | std::vector<StoreInst*> Stores; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 716 | while (!GV->use_empty()) |
| 717 | if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) { |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 718 | while (!LI->use_empty()) { |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 719 | Use &LoadUse = LI->use_begin().getUse(); |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 720 | if (!isa<SetCondInst>(LoadUse.getUser())) |
| 721 | LoadUse = RepValue; |
| 722 | else { |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 723 | // Replace the setcc X, 0 with a use of the bool value. |
| 724 | SetCondInst *SCI = cast<SetCondInst>(LoadUse.getUser()); |
| 725 | Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", SCI); |
Chris Lattner | bc965b9 | 2004-12-02 06:25:58 +0000 | [diff] [blame] | 726 | InitBoolUsed = true; |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 727 | switch (SCI->getOpcode()) { |
| 728 | default: assert(0 && "Unknown opcode!"); |
| 729 | case Instruction::SetLT: |
| 730 | LV = ConstantBool::False; // X < null -> always false |
| 731 | break; |
| 732 | case Instruction::SetEQ: |
| 733 | case Instruction::SetLE: |
| 734 | LV = BinaryOperator::createNot(LV, "notinit", SCI); |
| 735 | break; |
| 736 | case Instruction::SetNE: |
| 737 | case Instruction::SetGE: |
| 738 | case Instruction::SetGT: |
| 739 | break; // no change. |
| 740 | } |
| 741 | SCI->replaceAllUsesWith(LV); |
| 742 | SCI->eraseFromParent(); |
| 743 | } |
| 744 | } |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 745 | LI->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 746 | } else { |
| 747 | StoreInst *SI = cast<StoreInst>(GV->use_back()); |
Chris Lattner | bc965b9 | 2004-12-02 06:25:58 +0000 | [diff] [blame] | 748 | // The global is initialized when the store to it occurs. |
| 749 | new StoreInst(ConstantBool::True, InitBool, SI); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 750 | SI->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Chris Lattner | bc965b9 | 2004-12-02 06:25:58 +0000 | [diff] [blame] | 753 | // If the initialization boolean was used, insert it, otherwise delete it. |
| 754 | if (!InitBoolUsed) { |
| 755 | while (!InitBool->use_empty()) // Delete initializations |
| 756 | cast<Instruction>(InitBool->use_back())->eraseFromParent(); |
| 757 | delete InitBool; |
| 758 | } else |
| 759 | GV->getParent()->getGlobalList().insert(GV, InitBool); |
| 760 | |
| 761 | |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 762 | // Now the GV is dead, nuke it and the malloc. |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 763 | GV->eraseFromParent(); |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 764 | MI->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 765 | |
| 766 | // To further other optimizations, loop over all users of NewGV and try to |
| 767 | // constant prop them. This will promote GEP instructions with constant |
| 768 | // indices into GEP constant-exprs, which will allow global-opt to hack on it. |
| 769 | ConstantPropUsersOf(NewGV); |
| 770 | if (RepValue != NewGV) |
| 771 | ConstantPropUsersOf(RepValue); |
| 772 | |
| 773 | return NewGV; |
| 774 | } |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 775 | |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 776 | /// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking |
| 777 | /// to make sure that there are no complex uses of V. We permit simple things |
| 778 | /// like dereferencing the pointer, but not storing through the address, unless |
| 779 | /// it is to the specified global. |
| 780 | static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V, |
| 781 | GlobalVariable *GV) { |
| 782 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI) |
| 783 | if (isa<LoadInst>(*UI) || isa<SetCondInst>(*UI)) { |
| 784 | // Fine, ignore. |
| 785 | } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 786 | if (SI->getOperand(0) == V && SI->getOperand(1) != GV) |
| 787 | return false; // Storing the pointer itself... bad. |
| 788 | // Otherwise, storing through it, or storing into GV... fine. |
| 789 | } else if (isa<GetElementPtrInst>(*UI) || isa<SelectInst>(*UI)) { |
| 790 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(cast<Instruction>(*UI),GV)) |
| 791 | return false; |
| 792 | } else { |
| 793 | return false; |
| 794 | } |
| 795 | return true; |
| 796 | |
| 797 | } |
| 798 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 799 | // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge |
| 800 | // 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] | 801 | static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 802 | Module::global_iterator &GVI, TargetData &TD) { |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 803 | if (CastInst *CI = dyn_cast<CastInst>(StoredOnceVal)) |
| 804 | StoredOnceVal = CI->getOperand(0); |
| 805 | else if (GetElementPtrInst *GEPI =dyn_cast<GetElementPtrInst>(StoredOnceVal)){ |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 806 | // "getelementptr Ptr, 0, 0, 0" is really just a cast. |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 807 | bool IsJustACast = true; |
| 808 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 809 | if (!isa<Constant>(GEPI->getOperand(i)) || |
| 810 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { |
| 811 | IsJustACast = false; |
| 812 | break; |
| 813 | } |
| 814 | if (IsJustACast) |
| 815 | StoredOnceVal = GEPI->getOperand(0); |
| 816 | } |
| 817 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 818 | // If we are dealing with a pointer global that is initialized to null and |
| 819 | // only has one (non-null) value stored into it, then we can optimize any |
| 820 | // users of the loaded value (often calls and loads) that would trap if the |
| 821 | // value was null. |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 822 | if (isa<PointerType>(GV->getInitializer()->getType()) && |
| 823 | GV->getInitializer()->isNullValue()) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 824 | if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) { |
| 825 | if (GV->getInitializer()->getType() != SOVC->getType()) |
| 826 | SOVC = ConstantExpr::getCast(SOVC, GV->getInitializer()->getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 828 | // Optimize away any trapping uses of the loaded value. |
| 829 | if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC)) |
Chris Lattner | 8be8012 | 2004-10-10 17:07:12 +0000 | [diff] [blame] | 830 | return true; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 831 | } else if (MallocInst *MI = dyn_cast<MallocInst>(StoredOnceVal)) { |
| 832 | // If we have a global that is only initialized with a fixed size malloc, |
| 833 | // and if all users of the malloc trap, and if the malloc'd address is not |
| 834 | // put anywhere else, transform the program to use global memory instead |
| 835 | // of malloc'd memory. This eliminates dynamic allocation (good) and |
| 836 | // exposes the resultant global to further GlobalOpt (even better). Note |
| 837 | // that we restrict this transformation to only working on small |
| 838 | // allocations (2048 bytes currently), as we don't want to introduce a 16M |
| 839 | // global or something. |
| 840 | if (ConstantInt *NElements = dyn_cast<ConstantInt>(MI->getArraySize())) |
| 841 | if (MI->getAllocatedType()->isSized() && |
| 842 | NElements->getRawValue()* |
| 843 | TD.getTypeSize(MI->getAllocatedType()) < 2048 && |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 844 | AllUsesOfLoadedValueWillTrapIfNull(GV) && |
| 845 | ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV)) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 846 | GVI = OptimizeGlobalAddressOfMalloc(GV, MI); |
| 847 | return true; |
| 848 | } |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 849 | } |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 850 | } |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 851 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 852 | return false; |
| 853 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 855 | /// ShrinkGlobalToBoolean - At this point, we have learned that the only two |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 856 | /// values ever stored into GV are its initializer and OtherVal. |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 857 | static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) { |
| 858 | // Create the new global, initializing it to false. |
| 859 | GlobalVariable *NewGV = new GlobalVariable(Type::BoolTy, false, |
| 860 | GlobalValue::InternalLinkage, ConstantBool::False, GV->getName()+".b"); |
| 861 | GV->getParent()->getGlobalList().insert(GV, NewGV); |
| 862 | |
| 863 | Constant *InitVal = GV->getInitializer(); |
| 864 | assert(InitVal->getType() != Type::BoolTy && "No reason to shrink to bool!"); |
| 865 | |
| 866 | // If initialized to zero and storing one into the global, we can use a cast |
| 867 | // instead of a select to synthesize the desired value. |
| 868 | bool IsOneZero = false; |
| 869 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) |
| 870 | IsOneZero = InitVal->isNullValue() && CI->equalsInt(1); |
| 871 | |
| 872 | while (!GV->use_empty()) { |
| 873 | Instruction *UI = cast<Instruction>(GV->use_back()); |
| 874 | if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { |
| 875 | // Change the store into a boolean store. |
| 876 | bool StoringOther = SI->getOperand(0) == OtherVal; |
| 877 | // Only do this if we weren't storing a loaded value. |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 878 | Value *StoreVal; |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 879 | if (StoringOther || SI->getOperand(0) == InitVal) |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 880 | StoreVal = ConstantBool::get(StoringOther); |
| 881 | else { |
| 882 | // Otherwise, we are storing a previously loaded copy. To do this, |
| 883 | // change the copy from copying the original value to just copying the |
| 884 | // bool. |
| 885 | Instruction *StoredVal = cast<Instruction>(SI->getOperand(0)); |
| 886 | |
| 887 | // If we're already replaced the input, StoredVal will be a cast or |
| 888 | // select instruction. If not, it will be a load of the original |
| 889 | // global. |
| 890 | if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { |
| 891 | assert(LI->getOperand(0) == GV && "Not a copy!"); |
| 892 | // Insert a new load, to preserve the saved value. |
| 893 | StoreVal = new LoadInst(NewGV, LI->getName()+".b", LI); |
| 894 | } else { |
| 895 | assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) && |
| 896 | "This is not a form that we understand!"); |
| 897 | StoreVal = StoredVal->getOperand(0); |
| 898 | assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!"); |
| 899 | } |
| 900 | } |
| 901 | new StoreInst(StoreVal, NewGV, SI); |
| 902 | } else if (!UI->use_empty()) { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 903 | // Change the load into a load of bool then a select. |
| 904 | LoadInst *LI = cast<LoadInst>(UI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 906 | std::string Name = LI->getName(); LI->setName(""); |
| 907 | LoadInst *NLI = new LoadInst(NewGV, Name+".b", LI); |
| 908 | Value *NSI; |
| 909 | if (IsOneZero) |
| 910 | NSI = new CastInst(NLI, LI->getType(), Name, LI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 911 | else |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 912 | NSI = new SelectInst(NLI, OtherVal, InitVal, Name, LI); |
| 913 | LI->replaceAllUsesWith(NSI); |
| 914 | } |
| 915 | UI->eraseFromParent(); |
| 916 | } |
| 917 | |
| 918 | GV->eraseFromParent(); |
| 919 | } |
| 920 | |
| 921 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 922 | /// ProcessInternalGlobal - Analyze the specified global variable and optimize |
| 923 | /// it if possible. If we make a change, return true. |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 924 | bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV, |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 925 | Module::global_iterator &GVI) { |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 926 | std::set<PHINode*> PHIUsers; |
| 927 | GlobalStatus GS; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 928 | GV->removeDeadConstantUsers(); |
| 929 | |
| 930 | if (GV->use_empty()) { |
| 931 | DEBUG(std::cerr << "GLOBAL DEAD: " << *GV); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 932 | GV->eraseFromParent(); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 933 | ++NumDeleted; |
| 934 | return true; |
| 935 | } |
| 936 | |
| 937 | if (!AnalyzeGlobal(GV, GS, PHIUsers)) { |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 938 | // If this is a first class global and has only one accessing function |
| 939 | // and this function is main (which we know is not recursive we can make |
| 940 | // this global a local variable) we replace the global with a local alloca |
| 941 | // in this function. |
| 942 | // |
| 943 | // NOTE: It doesn't make sense to promote non first class types since we |
| 944 | // are just replacing static memory to stack memory. |
| 945 | if (!GS.HasMultipleAccessingFunctions && |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 946 | GS.AccessingFunction && !GS.HasNonInstructionUser && |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 947 | GV->getType()->getElementType()->isFirstClassType() && |
| 948 | GS.AccessingFunction->getName() == "main" && |
| 949 | GS.AccessingFunction->hasExternalLinkage()) { |
| 950 | DEBUG(std::cerr << "LOCALIZING GLOBAL: " << *GV); |
| 951 | Instruction* FirstI = GS.AccessingFunction->getEntryBlock().begin(); |
| 952 | const Type* ElemTy = GV->getType()->getElementType(); |
| 953 | AllocaInst* Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), FirstI); |
| 954 | if (!isa<UndefValue>(GV->getInitializer())) |
| 955 | new StoreInst(GV->getInitializer(), Alloca, FirstI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 956 | |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 957 | GV->replaceAllUsesWith(Alloca); |
| 958 | GV->eraseFromParent(); |
| 959 | ++NumLocalized; |
| 960 | return true; |
| 961 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 962 | // If the global is never loaded (but may be stored to), it is dead. |
| 963 | // Delete it now. |
| 964 | if (!GS.isLoaded) { |
| 965 | DEBUG(std::cerr << "GLOBAL NEVER LOADED: " << *GV); |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 966 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 967 | // Delete any stores we can find to the global. We may not be able to |
| 968 | // make it completely dead though. |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 969 | bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer()); |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 970 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 971 | // If the global is dead now, delete it. |
| 972 | if (GV->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 973 | GV->eraseFromParent(); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 974 | ++NumDeleted; |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 975 | Changed = true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 976 | } |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 977 | return Changed; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 978 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 979 | } else if (GS.StoredType <= GlobalStatus::isInitializerStored) { |
| 980 | DEBUG(std::cerr << "MARKING CONSTANT: " << *GV); |
| 981 | GV->setConstant(true); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 982 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 983 | // Clean up any obviously simplifiable users now. |
| 984 | CleanupConstantGlobalUsers(GV, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 985 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 986 | // If the global is dead now, just nuke it. |
| 987 | if (GV->use_empty()) { |
| 988 | DEBUG(std::cerr << " *** Marking constant allowed us to simplify " |
| 989 | "all users and delete global!\n"); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 990 | GV->eraseFromParent(); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 991 | ++NumDeleted; |
| 992 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 993 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 994 | ++NumMarked; |
| 995 | return true; |
| 996 | } else if (!GS.isNotSuitableForSRA && |
| 997 | !GV->getInitializer()->getType()->isFirstClassType()) { |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 998 | if (GlobalVariable *FirstNewGV = SRAGlobal(GV)) { |
| 999 | GVI = FirstNewGV; // Don't skip the newly produced globals! |
| 1000 | return true; |
| 1001 | } |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1002 | } else if (GS.StoredType == GlobalStatus::isStoredOnce) { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1003 | // If the initial value for the global was an undef value, and if only |
| 1004 | // one other value was stored into it, we can just change the |
| 1005 | // initializer to be an undef value, then delete all stores to the |
| 1006 | // global. This allows us to mark it constant. |
| 1007 | if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) |
| 1008 | if (isa<UndefValue>(GV->getInitializer())) { |
| 1009 | // Change the initial value here. |
| 1010 | GV->setInitializer(SOVConstant); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1011 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1012 | // Clean up any obviously simplifiable users now. |
| 1013 | CleanupConstantGlobalUsers(GV, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1015 | if (GV->use_empty()) { |
| 1016 | DEBUG(std::cerr << " *** Substituting initializer allowed us to " |
| 1017 | "simplify all users and delete global!\n"); |
| 1018 | GV->eraseFromParent(); |
| 1019 | ++NumDeleted; |
| 1020 | } else { |
| 1021 | GVI = GV; |
| 1022 | } |
| 1023 | ++NumSubstitute; |
| 1024 | return true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1025 | } |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1027 | // Try to optimize globals based on the knowledge that only one value |
| 1028 | // (besides its initializer) is ever stored to the global. |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1029 | if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI, |
| 1030 | getAnalysis<TargetData>())) |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1031 | return true; |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Otherwise, if the global was not a boolean, we can shrink it to be a |
| 1034 | // boolean. |
| 1035 | if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) |
Chris Lattner | 077f1a8 | 2004-12-12 06:03:06 +0000 | [diff] [blame] | 1036 | if (GV->getType()->getElementType() != Type::BoolTy && |
| 1037 | !GV->getType()->getElementType()->isFloatingPoint()) { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1038 | DEBUG(std::cerr << " *** SHRINKING TO BOOL: " << *GV); |
| 1039 | ShrinkGlobalToBoolean(GV, SOVConstant); |
| 1040 | ++NumShrunkToBool; |
| 1041 | return true; |
| 1042 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 1048 | /// OnlyCalledDirectly - Return true if the specified function is only called |
| 1049 | /// directly. In other words, its address is never taken. |
| 1050 | static bool OnlyCalledDirectly(Function *F) { |
| 1051 | for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ |
| 1052 | Instruction *User = dyn_cast<Instruction>(*UI); |
| 1053 | if (!User) return false; |
| 1054 | if (!isa<CallInst>(User) && !isa<InvokeInst>(User)) return false; |
| 1055 | |
| 1056 | // See if the function address is passed as an argument. |
| 1057 | for (unsigned i = 1, e = User->getNumOperands(); i != e; ++i) |
| 1058 | if (User->getOperand(i) == F) return false; |
| 1059 | } |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | /// ChangeCalleesToFastCall - Walk all of the direct calls of the specified |
| 1064 | /// function, changing them to FastCC. |
| 1065 | static void ChangeCalleesToFastCall(Function *F) { |
| 1066 | for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ |
| 1067 | Instruction *User = cast<Instruction>(*UI); |
| 1068 | if (CallInst *CI = dyn_cast<CallInst>(User)) |
| 1069 | CI->setCallingConv(CallingConv::Fast); |
| 1070 | else |
| 1071 | cast<InvokeInst>(User)->setCallingConv(CallingConv::Fast); |
| 1072 | } |
| 1073 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1075 | bool GlobalOpt::OptimizeFunctions(Module &M) { |
| 1076 | bool Changed = false; |
| 1077 | // Optimize functions. |
| 1078 | for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) { |
| 1079 | Function *F = FI++; |
| 1080 | F->removeDeadConstantUsers(); |
| 1081 | if (F->use_empty() && (F->hasInternalLinkage() || |
| 1082 | F->hasLinkOnceLinkage())) { |
| 1083 | M.getFunctionList().erase(F); |
| 1084 | Changed = true; |
| 1085 | ++NumFnDeleted; |
| 1086 | } else if (F->hasInternalLinkage() && |
| 1087 | F->getCallingConv() == CallingConv::C && !F->isVarArg() && |
| 1088 | OnlyCalledDirectly(F)) { |
| 1089 | // If this function has C calling conventions, is not a varargs |
| 1090 | // function, and is only called directly, promote it to use the Fast |
| 1091 | // calling convention. |
| 1092 | F->setCallingConv(CallingConv::Fast); |
| 1093 | ChangeCalleesToFastCall(F); |
| 1094 | ++NumFastCallFns; |
| 1095 | Changed = true; |
| 1096 | } |
| 1097 | } |
| 1098 | return Changed; |
| 1099 | } |
| 1100 | |
| 1101 | bool GlobalOpt::OptimizeGlobalVars(Module &M) { |
| 1102 | bool Changed = false; |
| 1103 | for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); |
| 1104 | GVI != E; ) { |
| 1105 | GlobalVariable *GV = GVI++; |
| 1106 | if (!GV->isConstant() && GV->hasInternalLinkage() && |
| 1107 | GV->hasInitializer()) |
| 1108 | Changed |= ProcessInternalGlobal(GV, GVI); |
| 1109 | } |
| 1110 | return Changed; |
| 1111 | } |
| 1112 | |
| 1113 | /// FindGlobalCtors - Find the llvm.globalctors list, verifying that all |
| 1114 | /// initializers have an init priority of 65535. |
| 1115 | GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) { |
| 1116 | for (Module::giterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
| 1117 | if (I->getName() == "llvm.global_ctors") { |
| 1118 | // Found it, verify it's an array of { int, void()* }. |
| 1119 | const ArrayType *ATy =dyn_cast<ArrayType>(I->getType()->getElementType()); |
| 1120 | if (!ATy) return 0; |
| 1121 | const StructType *STy = dyn_cast<StructType>(ATy->getElementType()); |
| 1122 | if (!STy || STy->getNumElements() != 2 || |
| 1123 | STy->getElementType(0) != Type::IntTy) return 0; |
| 1124 | const PointerType *PFTy = dyn_cast<PointerType>(STy->getElementType(1)); |
| 1125 | if (!PFTy) return 0; |
| 1126 | const FunctionType *FTy = dyn_cast<FunctionType>(PFTy->getElementType()); |
| 1127 | if (!FTy || FTy->getReturnType() != Type::VoidTy || FTy->isVarArg() || |
| 1128 | FTy->getNumParams() != 0) |
| 1129 | return 0; |
| 1130 | |
| 1131 | // Verify that the initializer is simple enough for us to handle. |
| 1132 | if (!I->hasInitializer()) return 0; |
| 1133 | ConstantArray *CA = dyn_cast<ConstantArray>(I->getInitializer()); |
| 1134 | if (!CA) return 0; |
| 1135 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1136 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(CA->getOperand(i))) { |
Chris Lattner | 7d8e58f | 2005-09-26 02:19:27 +0000 | [diff] [blame] | 1137 | if (isa<ConstantPointerNull>(CS->getOperand(1))) |
| 1138 | continue; |
| 1139 | |
| 1140 | // Must have a function or null ptr. |
| 1141 | if (!isa<Function>(CS->getOperand(1))) |
| 1142 | return 0; |
| 1143 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1144 | // Init priority must be standard. |
| 1145 | ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); |
| 1146 | if (!CI || CI->getRawValue() != 65535) |
| 1147 | return 0; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1148 | } else { |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | return I; |
| 1153 | } |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1157 | /// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand, |
| 1158 | /// return a list of the functions and null terminator as a vector. |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1159 | static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) { |
| 1160 | ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); |
| 1161 | std::vector<Function*> Result; |
| 1162 | Result.reserve(CA->getNumOperands()); |
| 1163 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { |
| 1164 | ConstantStruct *CS = cast<ConstantStruct>(CA->getOperand(i)); |
| 1165 | Result.push_back(dyn_cast<Function>(CS->getOperand(1))); |
| 1166 | } |
| 1167 | return Result; |
| 1168 | } |
| 1169 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1170 | /// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the |
| 1171 | /// specified array, returning the new global to use. |
| 1172 | static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, |
| 1173 | const std::vector<Function*> &Ctors) { |
| 1174 | // If we made a change, reassemble the initializer list. |
| 1175 | std::vector<Constant*> CSVals; |
| 1176 | CSVals.push_back(ConstantSInt::get(Type::IntTy, 65535)); |
| 1177 | CSVals.push_back(0); |
| 1178 | |
| 1179 | // Create the new init list. |
| 1180 | std::vector<Constant*> CAList; |
| 1181 | for (unsigned i = 0, e = Ctors.size(); i != e; ++i) { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1182 | if (Ctors[i]) { |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1183 | CSVals[1] = Ctors[i]; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1184 | } else { |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1185 | const Type *FTy = FunctionType::get(Type::VoidTy, |
| 1186 | std::vector<const Type*>(), false); |
| 1187 | const PointerType *PFTy = PointerType::get(FTy); |
| 1188 | CSVals[1] = Constant::getNullValue(PFTy); |
| 1189 | CSVals[0] = ConstantSInt::get(Type::IntTy, 2147483647); |
| 1190 | } |
| 1191 | CAList.push_back(ConstantStruct::get(CSVals)); |
| 1192 | } |
| 1193 | |
| 1194 | // Create the array initializer. |
| 1195 | const Type *StructTy = |
| 1196 | cast<ArrayType>(GCL->getType()->getElementType())->getElementType(); |
| 1197 | Constant *CA = ConstantArray::get(ArrayType::get(StructTy, CAList.size()), |
| 1198 | CAList); |
| 1199 | |
| 1200 | // If we didn't change the number of elements, don't create a new GV. |
| 1201 | if (CA->getType() == GCL->getInitializer()->getType()) { |
| 1202 | GCL->setInitializer(CA); |
| 1203 | return GCL; |
| 1204 | } |
| 1205 | |
| 1206 | // Create the new global and insert it next to the existing list. |
| 1207 | GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(), |
| 1208 | GCL->getLinkage(), CA, |
| 1209 | GCL->getName()); |
| 1210 | GCL->setName(""); |
| 1211 | GCL->getParent()->getGlobalList().insert(GCL, NGV); |
| 1212 | |
| 1213 | // Nuke the old list, replacing any uses with the new one. |
| 1214 | if (!GCL->use_empty()) { |
| 1215 | Constant *V = NGV; |
| 1216 | if (V->getType() != GCL->getType()) |
| 1217 | V = ConstantExpr::getCast(V, GCL->getType()); |
| 1218 | GCL->replaceAllUsesWith(V); |
| 1219 | } |
| 1220 | GCL->eraseFromParent(); |
| 1221 | |
| 1222 | if (Ctors.size()) |
| 1223 | return NGV; |
| 1224 | else |
| 1225 | return 0; |
| 1226 | } |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1227 | |
| 1228 | |
| 1229 | static Constant *getVal(std::map<Value*, Constant*> &ComputedValues, |
| 1230 | Value *V) { |
| 1231 | if (Constant *CV = dyn_cast<Constant>(V)) return CV; |
| 1232 | Constant *R = ComputedValues[V]; |
| 1233 | assert(R && "Reference to an uncomputed value!"); |
| 1234 | return R; |
| 1235 | } |
| 1236 | |
| 1237 | /// isSimpleEnoughPointerToCommit - Return true if this constant is simple |
| 1238 | /// enough for us to understand. In particular, if it is a cast of something, |
| 1239 | /// we punt. We basically just support direct accesses to globals and GEP's of |
| 1240 | /// globals. This should be kept up to date with CommitValueTo. |
| 1241 | static bool isSimpleEnoughPointerToCommit(Constant *C) { |
| 1242 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
| 1243 | return !GV->isExternal(); // reject external globals. |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 1244 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 1245 | // Handle a constantexpr gep. |
| 1246 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 1247 | isa<GlobalVariable>(CE->getOperand(0))) { |
| 1248 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
| 1249 | return GV->hasInitializer() && |
| 1250 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE); |
| 1251 | } |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1252 | return false; |
| 1253 | } |
| 1254 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 1255 | /// EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global |
| 1256 | /// initializer. This returns 'Init' modified to reflect 'Val' stored into it. |
| 1257 | /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into. |
| 1258 | static Constant *EvaluateStoreInto(Constant *Init, Constant *Val, |
| 1259 | ConstantExpr *Addr, unsigned OpNo) { |
| 1260 | // Base case of the recursion. |
| 1261 | if (OpNo == Addr->getNumOperands()) { |
| 1262 | assert(Val->getType() == Init->getType() && "Type mismatch!"); |
| 1263 | return Val; |
| 1264 | } |
| 1265 | |
| 1266 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 1267 | std::vector<Constant*> Elts; |
| 1268 | |
| 1269 | // Break up the constant into its elements. |
| 1270 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 1271 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 1272 | Elts.push_back(CS->getOperand(i)); |
| 1273 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 1274 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
| 1275 | Elts.push_back(Constant::getNullValue(STy->getElementType(i))); |
| 1276 | } else if (isa<UndefValue>(Init)) { |
| 1277 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
| 1278 | Elts.push_back(UndefValue::get(STy->getElementType(i))); |
| 1279 | } else { |
| 1280 | assert(0 && "This code is out of sync with " |
| 1281 | " ConstantFoldLoadThroughGEPConstantExpr"); |
| 1282 | } |
| 1283 | |
| 1284 | // Replace the element that we are supposed to. |
| 1285 | ConstantUInt *CU = cast<ConstantUInt>(Addr->getOperand(OpNo)); |
| 1286 | assert(CU->getValue() < STy->getNumElements() && |
| 1287 | "Struct index out of range!"); |
| 1288 | unsigned Idx = (unsigned)CU->getValue(); |
| 1289 | Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1); |
| 1290 | |
| 1291 | // Return the modified struct. |
| 1292 | return ConstantStruct::get(Elts); |
| 1293 | } else { |
| 1294 | ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo)); |
| 1295 | const ArrayType *ATy = cast<ArrayType>(Init->getType()); |
| 1296 | |
| 1297 | // Break up the array into elements. |
| 1298 | std::vector<Constant*> Elts; |
| 1299 | if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 1300 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1301 | Elts.push_back(CA->getOperand(i)); |
| 1302 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 1303 | Constant *Elt = Constant::getNullValue(ATy->getElementType()); |
| 1304 | Elts.assign(ATy->getNumElements(), Elt); |
| 1305 | } else if (isa<UndefValue>(Init)) { |
| 1306 | Constant *Elt = UndefValue::get(ATy->getElementType()); |
| 1307 | Elts.assign(ATy->getNumElements(), Elt); |
| 1308 | } else { |
| 1309 | assert(0 && "This code is out of sync with " |
| 1310 | " ConstantFoldLoadThroughGEPConstantExpr"); |
| 1311 | } |
| 1312 | |
| 1313 | assert((uint64_t)CI->getRawValue() < ATy->getNumElements()); |
| 1314 | Elts[(uint64_t)CI->getRawValue()] = |
| 1315 | EvaluateStoreInto(Elts[(uint64_t)CI->getRawValue()], Val, Addr, OpNo+1); |
| 1316 | return ConstantArray::get(ATy, Elts); |
| 1317 | } |
| 1318 | } |
| 1319 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1320 | /// CommitValueTo - We have decided that Addr (which satisfies the predicate |
| 1321 | /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen. |
| 1322 | static void CommitValueTo(Constant *Val, Constant *Addr) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 1323 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) { |
| 1324 | assert(GV->hasInitializer()); |
| 1325 | GV->setInitializer(Val); |
| 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | ConstantExpr *CE = cast<ConstantExpr>(Addr); |
| 1330 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
| 1331 | |
| 1332 | Constant *Init = GV->getInitializer(); |
| 1333 | Init = EvaluateStoreInto(Init, Val, CE, 2); |
| 1334 | GV->setInitializer(Init); |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Chris Lattner | 562a055 | 2005-09-26 05:16:34 +0000 | [diff] [blame] | 1337 | /// ComputeLoadResult - Return the value that would be computed by a load from |
| 1338 | /// P after the stores reflected by 'memory' have been performed. If we can't |
| 1339 | /// decide, return null. |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 1340 | static Constant *ComputeLoadResult(Constant *P, |
| 1341 | const std::map<Constant*, Constant*> &Memory) { |
| 1342 | // If this memory location has been recently stored, use the stored value: it |
| 1343 | // is the most up-to-date. |
| 1344 | std::map<Constant*, Constant*>::const_iterator I = Memory.find(P); |
| 1345 | if (I != Memory.end()) return I->second; |
| 1346 | |
| 1347 | // Access it. |
| 1348 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { |
| 1349 | if (GV->hasInitializer()) |
| 1350 | return GV->getInitializer(); |
| 1351 | return 0; |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 1352 | } |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 1353 | |
| 1354 | // Handle a constantexpr getelementptr. |
| 1355 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P)) |
| 1356 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 1357 | isa<GlobalVariable>(CE->getOperand(0))) { |
| 1358 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
| 1359 | if (GV->hasInitializer()) |
| 1360 | return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE); |
| 1361 | } |
| 1362 | |
| 1363 | return 0; // don't know how to evaluate. |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1366 | /// EvaluateFunction - Evaluate a call to function F, returning true if |
| 1367 | /// successful, false if we can't evaluate it. ActualArgs contains the formal |
| 1368 | /// arguments for the function. |
| 1369 | static bool EvaluateFunction(Function *F, |
| 1370 | const std::vector<Constant*> &ActualArgs, |
| 1371 | std::vector<Function*> &CallStack, |
| 1372 | std::map<Constant*, Constant*> &MutatedMemory, |
| 1373 | std::vector<GlobalVariable*> &AllocaTmps) { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1374 | /// Values - As we compute SSA register values, we store their contents here. |
| 1375 | std::map<Value*, Constant*> Values; |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1376 | |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1377 | /// ExecutedBlocks - We only handle non-looping, non-recursive code. As such, |
| 1378 | /// we can only evaluate any one basic block at most once. This set keeps |
| 1379 | /// track of what we have executed so we can detect recursive cases etc. |
| 1380 | std::set<BasicBlock*> ExecutedBlocks; |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1381 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1382 | // CurInst - The current instruction we're evaluating. |
| 1383 | BasicBlock::iterator CurInst = F->begin()->begin(); |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1384 | ExecutedBlocks.insert(F->begin()); |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1385 | |
| 1386 | // This is the main evaluation loop. |
| 1387 | while (1) { |
| 1388 | Constant *InstResult = 0; |
| 1389 | |
| 1390 | if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) { |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1391 | if (SI->isVolatile()) break; // no volatile accesses. |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1392 | Constant *Ptr = getVal(Values, SI->getOperand(1)); |
| 1393 | if (!isSimpleEnoughPointerToCommit(Ptr)) |
| 1394 | // If this is too complex for us to commit, reject it. |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1395 | break; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1396 | Constant *Val = getVal(Values, SI->getOperand(0)); |
| 1397 | MutatedMemory[Ptr] = Val; |
| 1398 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) { |
| 1399 | InstResult = ConstantExpr::get(BO->getOpcode(), |
| 1400 | getVal(Values, BO->getOperand(0)), |
| 1401 | getVal(Values, BO->getOperand(1))); |
| 1402 | } else if (ShiftInst *SI = dyn_cast<ShiftInst>(CurInst)) { |
| 1403 | InstResult = ConstantExpr::get(SI->getOpcode(), |
| 1404 | getVal(Values, SI->getOperand(0)), |
| 1405 | getVal(Values, SI->getOperand(1))); |
| 1406 | } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) { |
| 1407 | InstResult = ConstantExpr::getCast(getVal(Values, CI->getOperand(0)), |
| 1408 | CI->getType()); |
| 1409 | } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) { |
| 1410 | InstResult = ConstantExpr::getSelect(getVal(Values, SI->getOperand(0)), |
| 1411 | getVal(Values, SI->getOperand(1)), |
| 1412 | getVal(Values, SI->getOperand(2))); |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 1413 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) { |
| 1414 | Constant *P = getVal(Values, GEP->getOperand(0)); |
| 1415 | std::vector<Constant*> GEPOps; |
| 1416 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 1417 | GEPOps.push_back(getVal(Values, GEP->getOperand(i))); |
| 1418 | InstResult = ConstantExpr::getGetElementPtr(P, GEPOps); |
| 1419 | } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) { |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1420 | if (LI->isVolatile()) break; // no volatile accesses. |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 1421 | InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)), |
| 1422 | MutatedMemory); |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1423 | if (InstResult == 0) break; // Could not evaluate load. |
| 1424 | } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) { |
| 1425 | if (AI->isArrayAllocation()) break; // Cannot handle array allocs. |
| 1426 | const Type *Ty = AI->getType()->getElementType(); |
| 1427 | AllocaTmps.push_back(new GlobalVariable(Ty, false, |
| 1428 | GlobalValue::InternalLinkage, |
| 1429 | UndefValue::get(Ty), |
| 1430 | AI->getName())); |
| 1431 | InstResult = AllocaTmps.back(); |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1432 | } else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(CurInst)) { |
| 1433 | BasicBlock *NewBB = 0; |
| 1434 | if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) { |
| 1435 | if (BI->isUnconditional()) { |
| 1436 | NewBB = BI->getSuccessor(0); |
| 1437 | } else { |
| 1438 | ConstantBool *Cond = |
| 1439 | dyn_cast<ConstantBool>(getVal(Values, BI->getCondition())); |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1440 | if (!Cond) break; // Cannot determine. |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1441 | NewBB = BI->getSuccessor(!Cond->getValue()); |
| 1442 | } |
| 1443 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) { |
| 1444 | ConstantInt *Val = |
| 1445 | dyn_cast<ConstantInt>(getVal(Values, SI->getCondition())); |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1446 | if (!Val) break; // Cannot determine. |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1447 | NewBB = SI->getSuccessor(SI->findCaseValue(Val)); |
| 1448 | } else if (ReturnInst *RI = dyn_cast<ReturnInst>(CurInst)) { |
| 1449 | assert(RI->getNumOperands() == 0); |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1450 | return true; // We succeeded at evaluating this ctor! |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1451 | } else { |
| 1452 | // unwind, unreachable. |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1453 | break; // Cannot handle this terminator. |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | // Okay, we succeeded in evaluating this control flow. See if we have |
| 1457 | // executed the new block before. If so, we have a looping or recursive |
| 1458 | // function, which we cannot evaluate in reasonable time. |
| 1459 | if (!ExecutedBlocks.insert(NewBB).second) |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1460 | break; // Recursed/looped! |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Okay, we have never been in this block before. Check to see if there |
| 1463 | // are any PHI nodes. If so, evaluate them with information about where |
| 1464 | // we came from. |
| 1465 | BasicBlock *OldBB = CurInst->getParent(); |
| 1466 | CurInst = NewBB->begin(); |
| 1467 | PHINode *PN; |
| 1468 | for (; (PN = dyn_cast<PHINode>(CurInst)); ++CurInst) |
| 1469 | Values[PN] = getVal(Values, PN->getIncomingValueForBlock(OldBB)); |
| 1470 | |
| 1471 | // Do NOT increment CurInst. We know that the terminator had no value. |
| 1472 | continue; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1473 | } else { |
| 1474 | // TODO: use ConstantFoldCall for function calls. |
| 1475 | |
| 1476 | // Did not know how to evaluate this! |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1477 | break; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | if (!CurInst->use_empty()) |
| 1481 | Values[CurInst] = InstResult; |
| 1482 | |
| 1483 | // Advance program counter. |
| 1484 | ++CurInst; |
| 1485 | } |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1487 | return false; |
| 1488 | } |
| 1489 | |
| 1490 | /// EvaluateStaticConstructor - Evaluate static constructors in the function, if |
| 1491 | /// we can. Return true if we can, false otherwise. |
| 1492 | static bool EvaluateStaticConstructor(Function *F) { |
| 1493 | /// MutatedMemory - For each store we execute, we update this map. Loads |
| 1494 | /// check this to get the most up-to-date value. If evaluation is successful, |
| 1495 | /// this state is committed to the process. |
| 1496 | std::map<Constant*, Constant*> MutatedMemory; |
| 1497 | |
| 1498 | /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable |
| 1499 | /// to represent its body. This vector is needed so we can delete the |
| 1500 | /// temporary globals when we are done. |
| 1501 | std::vector<GlobalVariable*> AllocaTmps; |
| 1502 | |
| 1503 | /// CallStack - This is used to detect recursion. In pathological situations |
| 1504 | /// we could hit exponential behavior, but at least there is nothing |
| 1505 | /// unbounded. |
| 1506 | std::vector<Function*> CallStack; |
| 1507 | |
| 1508 | // Call the function. |
| 1509 | bool EvalSuccess = EvaluateFunction(F, std::vector<Constant*>(), CallStack, |
| 1510 | MutatedMemory, AllocaTmps); |
| 1511 | if (EvalSuccess) { |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1512 | // We succeeded at evaluation: commit the result. |
| 1513 | DEBUG(std::cerr << "FULLY EVALUATED GLOBAL CTOR FUNCTION '" << |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1514 | F->getName() << "' to " << MutatedMemory.size() << " stores.\n"); |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1515 | for (std::map<Constant*, Constant*>::iterator I = MutatedMemory.begin(), |
| 1516 | E = MutatedMemory.end(); I != E; ++I) |
| 1517 | CommitValueTo(I->second, I->first); |
| 1518 | } |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1520 | // At this point, we are done interpreting. If we created any 'alloca' |
| 1521 | // temporaries, release them now. |
| 1522 | while (!AllocaTmps.empty()) { |
| 1523 | GlobalVariable *Tmp = AllocaTmps.back(); |
| 1524 | AllocaTmps.pop_back(); |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1525 | |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 1526 | // If there are still users of the alloca, the program is doing something |
| 1527 | // silly, e.g. storing the address of the alloca somewhere and using it |
| 1528 | // later. Since this is undefined, we'll just make it be null. |
| 1529 | if (!Tmp->use_empty()) |
| 1530 | Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType())); |
| 1531 | delete Tmp; |
| 1532 | } |
Chris Lattner | aae4a1c | 2005-09-26 07:34:35 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1534 | return EvalSuccess; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame^] | 1538 | |
| 1539 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1540 | /// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible. |
| 1541 | /// Return true if anything changed. |
| 1542 | bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { |
| 1543 | std::vector<Function*> Ctors = ParseGlobalCtors(GCL); |
| 1544 | bool MadeChange = false; |
| 1545 | if (Ctors.empty()) return false; |
| 1546 | |
| 1547 | // Loop over global ctors, optimizing them when we can. |
| 1548 | for (unsigned i = 0; i != Ctors.size(); ++i) { |
| 1549 | Function *F = Ctors[i]; |
| 1550 | // Found a null terminator in the middle of the list, prune off the rest of |
| 1551 | // the list. |
Chris Lattner | 7d8e58f | 2005-09-26 02:19:27 +0000 | [diff] [blame] | 1552 | if (F == 0) { |
| 1553 | if (i != Ctors.size()-1) { |
| 1554 | Ctors.resize(i+1); |
| 1555 | MadeChange = true; |
| 1556 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1557 | break; |
| 1558 | } |
| 1559 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1560 | // We cannot simplify external ctor functions. |
| 1561 | if (F->empty()) continue; |
| 1562 | |
| 1563 | // If we can evaluate the ctor at compile time, do. |
| 1564 | if (EvaluateStaticConstructor(F)) { |
| 1565 | Ctors.erase(Ctors.begin()+i); |
| 1566 | MadeChange = true; |
| 1567 | --i; |
| 1568 | ++NumCtorsEvaluated; |
| 1569 | continue; |
| 1570 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | if (!MadeChange) return false; |
| 1574 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1575 | GCL = InstallGlobalCtors(GCL, Ctors); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1576 | return true; |
| 1577 | } |
| 1578 | |
| 1579 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 1580 | bool GlobalOpt::runOnModule(Module &M) { |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 1581 | bool Changed = false; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1582 | |
| 1583 | // Try to find the llvm.globalctors list. |
| 1584 | GlobalVariable *GlobalCtors = FindGlobalCtors(M); |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 1586 | bool LocalChange = true; |
| 1587 | while (LocalChange) { |
| 1588 | LocalChange = false; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1589 | |
| 1590 | // Delete functions that are trivially dead, ccc -> fastcc |
| 1591 | LocalChange |= OptimizeFunctions(M); |
| 1592 | |
| 1593 | // Optimize global_ctors list. |
| 1594 | if (GlobalCtors) |
| 1595 | LocalChange |= OptimizeGlobalCtorsList(GlobalCtors); |
| 1596 | |
| 1597 | // Optimize non-address-taken globals. |
| 1598 | LocalChange |= OptimizeGlobalVars(M); |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 1599 | Changed |= LocalChange; |
| 1600 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1601 | |
| 1602 | // TODO: Move all global ctors functions to the end of the module for code |
| 1603 | // layout. |
| 1604 | |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 1605 | return Changed; |
| 1606 | } |