blob: 0b4b351d659c66d9f8e17bbd038d0eae8bc9b8fb [file] [log] [blame]
Chris Lattner25db5802004-10-07 04:16:33 +00001//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattner25db5802004-10-07 04:16:33 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner25db5802004-10-07 04:16:33 +00008//===----------------------------------------------------------------------===//
9//
10// This pass transforms simple global variables that never have their address
11// taken. If obviously true, it marks read/write globals as constant, deletes
12// variables only stored to, etc.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "globalopt"
17#include "llvm/Transforms/IPO.h"
Chris Lattnera4c80222005-05-08 22:18:06 +000018#include "llvm/CallingConv.h"
Chris Lattner25db5802004-10-07 04:16:33 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Instructions.h"
Chris Lattner7561ca12005-02-27 18:58:52 +000022#include "llvm/IntrinsicInst.h"
Chris Lattner25db5802004-10-07 04:16:33 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/Debug.h"
Chris Lattner004e2502004-10-11 05:54:41 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Transforms/Utils/Local.h"
Chris Lattner25db5802004-10-07 04:16:33 +000028#include "llvm/ADT/Statistic.h"
Chris Lattnerabab0712004-10-08 17:32:09 +000029#include "llvm/ADT/StringExtras.h"
Chris Lattner25db5802004-10-07 04:16:33 +000030#include <set>
31#include <algorithm>
32using namespace llvm;
33
34namespace {
Chris Lattnerabab0712004-10-08 17:32:09 +000035 Statistic<> NumMarked ("globalopt", "Number of globals marked constant");
36 Statistic<> NumSRA ("globalopt", "Number of aggregate globals broken "
37 "into scalars");
Chris Lattner8e71c6a2004-10-16 18:09:00 +000038 Statistic<> NumSubstitute("globalopt",
39 "Number of globals with initializers stored into them");
Chris Lattnerabab0712004-10-08 17:32:09 +000040 Statistic<> NumDeleted ("globalopt", "Number of globals deleted");
Chris Lattner25db5802004-10-07 04:16:33 +000041 Statistic<> NumFnDeleted("globalopt", "Number of functions deleted");
Chris Lattnere42eb312004-10-10 23:14:11 +000042 Statistic<> NumGlobUses ("globalopt", "Number of global uses devirtualized");
Alkis Evlogimenosc4a44c62005-02-10 18:36:30 +000043 Statistic<> NumLocalized("globalopt", "Number of globals localized");
Chris Lattner40e4cec2004-12-12 05:53:50 +000044 Statistic<> NumShrunkToBool("globalopt",
45 "Number of global vars shrunk to booleans");
Chris Lattnera4c80222005-05-08 22:18:06 +000046 Statistic<> NumFastCallFns("globalopt",
47 "Number of functions converted to fastcc");
Chris Lattner41b6a5a2005-09-26 01:43:45 +000048 Statistic<> NumEmptyCtor ("globalopt", "Number of empty ctors removed");
Chris Lattner25db5802004-10-07 04:16:33 +000049
50 struct GlobalOpt : public ModulePass {
Chris Lattner004e2502004-10-11 05:54:41 +000051 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addRequired<TargetData>();
53 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000054
Chris Lattner25db5802004-10-07 04:16:33 +000055 bool runOnModule(Module &M);
Chris Lattner004e2502004-10-11 05:54:41 +000056
57 private:
Chris Lattner41b6a5a2005-09-26 01:43:45 +000058 GlobalVariable *FindGlobalCtors(Module &M);
59 bool OptimizeFunctions(Module &M);
60 bool OptimizeGlobalVars(Module &M);
61 bool OptimizeGlobalCtorsList(GlobalVariable *&GCL);
Chris Lattner531f9e92005-03-15 04:54:21 +000062 bool ProcessInternalGlobal(GlobalVariable *GV, Module::global_iterator &GVI);
Chris Lattner25db5802004-10-07 04:16:33 +000063 };
64
65 RegisterOpt<GlobalOpt> X("globalopt", "Global Variable Optimizer");
66}
67
68ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
69
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 Lattner617f1a32004-10-07 21:30:30 +000072/// this info will be accurate.
Chris Lattner25db5802004-10-07 04:16:33 +000073struct GlobalStatus {
Chris Lattner617f1a32004-10-07 21:30:30 +000074 /// isLoaded - True if the global is ever loaded. If the global isn't ever
75 /// loaded it can be deleted.
Chris Lattner25db5802004-10-07 04:16:33 +000076 bool isLoaded;
Chris Lattner617f1a32004-10-07 21:30:30 +000077
78 /// StoredType - Keep track of what stores to the global look like.
79 ///
Chris Lattner25db5802004-10-07 04:16:33 +000080 enum StoredType {
Chris Lattner617f1a32004-10-07 21:30:30 +000081 /// 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 Lattner25db5802004-10-07 04:16:33 +000099 } StoredType;
Chris Lattner617f1a32004-10-07 21:30:30 +0000100
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 Evlogimenosc4a44c62005-02-10 18:36:30 +0000105 // 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 Lattner50bdfcb2005-06-15 21:11:48 +0000112 // 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 Lattner617f1a32004-10-07 21:30:30 +0000116 /// 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 Lattner25db5802004-10-07 04:16:33 +0000119 bool isNotSuitableForSRA;
120
Chris Lattner617f1a32004-10-07 21:30:30 +0000121 GlobalStatus() : isLoaded(false), StoredType(NotStored), StoredOnceValue(0),
Alkis Evlogimenosc4a44c62005-02-10 18:36:30 +0000122 AccessingFunction(0), HasMultipleAccessingFunctions(false),
Chris Lattner50bdfcb2005-06-15 21:11:48 +0000123 HasNonInstructionUser(false), isNotSuitableForSRA(false) {}
Chris Lattner25db5802004-10-07 04:16:33 +0000124};
125
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000126
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.
132static 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 Lattner25db5802004-10-07 04:16:33 +0000144/// 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.
147///
148static bool AnalyzeGlobal(Value *V, GlobalStatus &GS,
149 std::set<PHINode*> &PHIUsers) {
150 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
151 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
Chris Lattner50bdfcb2005-06-15 21:11:48 +0000152 GS.HasNonInstructionUser = true;
153
Chris Lattner25db5802004-10-07 04:16:33 +0000154 if (AnalyzeGlobal(CE, GS, PHIUsers)) return true;
155 if (CE->getOpcode() != Instruction::GetElementPtr)
156 GS.isNotSuitableForSRA = true;
Chris Lattnerabab0712004-10-08 17:32:09 +0000157 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 Lattner25db5802004-10-07 04:16:33 +0000172 } else if (Instruction *I = dyn_cast<Instruction>(*UI)) {
Alkis Evlogimenosc4a44c62005-02-10 18:36:30 +0000173 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 Lattner25db5802004-10-07 04:16:33 +0000180 if (isa<LoadInst>(I)) {
181 GS.isLoaded = true;
182 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner02b6c912004-10-07 06:01:25 +0000183 // Don't allow a store OF the address, only stores TO the address.
184 if (SI->getOperand(0) == V) return true;
185
Chris Lattner617f1a32004-10-07 21:30:30 +0000186 // 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 Lattner28eeb732004-11-14 20:50:30 +0000191 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 Lattner617f1a32004-10-07 21:30:30 +0000198 if (GS.StoredType < GlobalStatus::isInitializerStored)
199 GS.StoredType = GlobalStatus::isInitializerStored;
200 } else if (GS.StoredType < GlobalStatus::isStoredOnce) {
201 GS.StoredType = GlobalStatus::isStoredOnce;
Chris Lattner28eeb732004-11-14 20:50:30 +0000202 GS.StoredOnceValue = StoredVal;
Chris Lattner617f1a32004-10-07 21:30:30 +0000203 } else if (GS.StoredType == GlobalStatus::isStoredOnce &&
Chris Lattner28eeb732004-11-14 20:50:30 +0000204 GS.StoredOnceValue == StoredVal) {
Chris Lattner617f1a32004-10-07 21:30:30 +0000205 // noop.
206 } else {
207 GS.StoredType = GlobalStatus::isStored;
208 }
209 } else {
Chris Lattner25db5802004-10-07 04:16:33 +0000210 GS.StoredType = GlobalStatus::isStored;
Chris Lattner617f1a32004-10-07 21:30:30 +0000211 }
Chris Lattner7561ca12005-02-27 18:58:52 +0000212 } else if (isa<GetElementPtrInst>(I)) {
Chris Lattner25db5802004-10-07 04:16:33 +0000213 if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
Chris Lattner004e2502004-10-11 05:54:41 +0000214
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 Brukmanb1c93172005-04-21 23:48:37 +0000218 !cast<Constant>(I->getOperand(1))->isNullValue() ||
Chris Lattner004e2502004-10-11 05:54:41 +0000219 !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 Lattner7561ca12005-02-27 18:58:52 +0000230 } else if (isa<SelectInst>(I)) {
Chris Lattner25db5802004-10-07 04:16:33 +0000231 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 Lattner7561ca12005-02-27 18:58:52 +0000241 } 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 Lattner25db5802004-10-07 04:16:33 +0000251 } else {
252 return true; // Any other non-load instruction might take address!
253 }
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000254 } else if (Constant *C = dyn_cast<Constant>(*UI)) {
Chris Lattner50bdfcb2005-06-15 21:11:48 +0000255 GS.HasNonInstructionUser = true;
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000256 // We might have a dead and dangling constant hanging off of here.
257 if (!ConstantIsDead(C))
258 return true;
Chris Lattner25db5802004-10-07 04:16:33 +0000259 } else {
Chris Lattner50bdfcb2005-06-15 21:11:48 +0000260 GS.HasNonInstructionUser = true;
261 // Otherwise must be some other user.
Chris Lattner25db5802004-10-07 04:16:33 +0000262 return true;
263 }
264
265 return false;
266}
267
Chris Lattnerabab0712004-10-08 17:32:09 +0000268static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) {
269 ConstantInt *CI = dyn_cast<ConstantInt>(Idx);
270 if (!CI) return 0;
Chris Lattner46fa04b2005-01-08 19:45:31 +0000271 unsigned IdxV = (unsigned)CI->getRawValue();
Chris Lattner25db5802004-10-07 04:16:33 +0000272
Chris Lattnerabab0712004-10-08 17:32:09 +0000273 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 Lattner8e71c6a2004-10-16 18:09:00 +0000279 } else if (isa<ConstantAggregateZero>(Agg)) {
Chris Lattnerabab0712004-10-08 17:32:09 +0000280 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 Lattner8e71c6a2004-10-16 18:09:00 +0000287 } 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 Lattnerabab0712004-10-08 17:32:09 +0000295 }
296 return 0;
297}
Chris Lattner25db5802004-10-07 04:16:33 +0000298
299static Constant *TraverseGEPInitializer(User *GEP, Constant *Init) {
Chris Lattner7561ca12005-02-27 18:58:52 +0000300 if (Init == 0) return 0;
Chris Lattner25db5802004-10-07 04:16:33 +0000301 if (GEP->getNumOperands() == 1 ||
302 !isa<Constant>(GEP->getOperand(1)) ||
303 !cast<Constant>(GEP->getOperand(1))->isNullValue())
304 return 0;
305
306 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) {
307 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
308 if (!Idx) return 0;
Chris Lattnerabab0712004-10-08 17:32:09 +0000309 Init = getAggregateConstantElement(Init, Idx);
310 if (Init == 0) return 0;
Chris Lattner25db5802004-10-07 04:16:33 +0000311 }
312 return Init;
313}
314
315/// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all
316/// users of the global, cleaning up the obvious ones. This is largely just a
Chris Lattnercb9f1522004-10-10 16:43:46 +0000317/// quick scan over the use list to clean up the easy and obvious cruft. This
318/// returns true if it made a change.
319static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) {
320 bool Changed = false;
Chris Lattner25db5802004-10-07 04:16:33 +0000321 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) {
322 User *U = *UI++;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000323
Chris Lattner25db5802004-10-07 04:16:33 +0000324 if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
Chris Lattner7561ca12005-02-27 18:58:52 +0000325 if (Init) {
326 // Replace the load with the initializer.
327 LI->replaceAllUsesWith(Init);
328 LI->eraseFromParent();
329 Changed = true;
330 }
Chris Lattner25db5802004-10-07 04:16:33 +0000331 } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
332 // Store must be unreachable or storing Init into the global.
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000333 SI->eraseFromParent();
Chris Lattnercb9f1522004-10-10 16:43:46 +0000334 Changed = true;
Chris Lattner25db5802004-10-07 04:16:33 +0000335 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
336 if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattner7561ca12005-02-27 18:58:52 +0000337 Constant *SubInit = TraverseGEPInitializer(CE, Init);
338 Changed |= CleanupConstantGlobalUsers(CE, SubInit);
339 } else if (CE->getOpcode() == Instruction::Cast &&
340 isa<PointerType>(CE->getType())) {
341 // Pointer cast, delete any stores and memsets to the global.
342 Changed |= CleanupConstantGlobalUsers(CE, 0);
343 }
344
345 if (CE->use_empty()) {
346 CE->destroyConstant();
347 Changed = true;
Chris Lattner25db5802004-10-07 04:16:33 +0000348 }
349 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
Chris Lattner7561ca12005-02-27 18:58:52 +0000350 Constant *SubInit = TraverseGEPInitializer(GEP, Init);
351 Changed |= CleanupConstantGlobalUsers(GEP, SubInit);
Chris Lattnera0e769c2004-10-10 16:47:33 +0000352
Chris Lattnercb9f1522004-10-10 16:43:46 +0000353 if (GEP->use_empty()) {
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000354 GEP->eraseFromParent();
Chris Lattnercb9f1522004-10-10 16:43:46 +0000355 Changed = true;
356 }
Chris Lattner7561ca12005-02-27 18:58:52 +0000357 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
358 if (MI->getRawDest() == V) {
359 MI->eraseFromParent();
360 Changed = true;
361 }
362
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000363 } else if (Constant *C = dyn_cast<Constant>(U)) {
364 // If we have a chain of dead constantexprs or other things dangling from
365 // us, and if they are all dead, nuke them without remorse.
366 if (ConstantIsDead(C)) {
367 C->destroyConstant();
Chris Lattner7561ca12005-02-27 18:58:52 +0000368 // This could have invalidated UI, start over from scratch.
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000369 CleanupConstantGlobalUsers(V, Init);
Chris Lattnercb9f1522004-10-10 16:43:46 +0000370 return true;
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000371 }
Chris Lattner25db5802004-10-07 04:16:33 +0000372 }
373 }
Chris Lattnercb9f1522004-10-10 16:43:46 +0000374 return Changed;
Chris Lattner25db5802004-10-07 04:16:33 +0000375}
376
Chris Lattnerabab0712004-10-08 17:32:09 +0000377/// SRAGlobal - Perform scalar replacement of aggregates on the specified global
378/// variable. This opens the door for other optimizations by exposing the
379/// behavior of the program in a more fine-grained way. We have determined that
380/// this transformation is safe already. We return the first global variable we
381/// insert so that the caller can reprocess it.
382static GlobalVariable *SRAGlobal(GlobalVariable *GV) {
383 assert(GV->hasInternalLinkage() && !GV->isConstant());
384 Constant *Init = GV->getInitializer();
385 const Type *Ty = Init->getType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000386
Chris Lattnerabab0712004-10-08 17:32:09 +0000387 std::vector<GlobalVariable*> NewGlobals;
388 Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
389
390 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
391 NewGlobals.reserve(STy->getNumElements());
392 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
393 Constant *In = getAggregateConstantElement(Init,
394 ConstantUInt::get(Type::UIntTy, i));
395 assert(In && "Couldn't get element of initializer?");
396 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
397 GlobalVariable::InternalLinkage,
398 In, GV->getName()+"."+utostr(i));
399 Globals.insert(GV, NGV);
400 NewGlobals.push_back(NGV);
401 }
402 } else if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
403 unsigned NumElements = 0;
404 if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
405 NumElements = ATy->getNumElements();
406 else if (const PackedType *PTy = dyn_cast<PackedType>(STy))
407 NumElements = PTy->getNumElements();
408 else
409 assert(0 && "Unknown aggregate sequential type!");
410
Chris Lattner25169ca2005-02-23 16:53:04 +0000411 if (NumElements > 16 && GV->hasNUsesOrMore(16))
Chris Lattnerd6a44922005-02-01 01:23:31 +0000412 return 0; // It's not worth it.
Chris Lattnerabab0712004-10-08 17:32:09 +0000413 NewGlobals.reserve(NumElements);
414 for (unsigned i = 0, e = NumElements; i != e; ++i) {
415 Constant *In = getAggregateConstantElement(Init,
416 ConstantUInt::get(Type::UIntTy, i));
417 assert(In && "Couldn't get element of initializer?");
418
419 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
420 GlobalVariable::InternalLinkage,
421 In, GV->getName()+"."+utostr(i));
422 Globals.insert(GV, NGV);
423 NewGlobals.push_back(NGV);
424 }
425 }
426
427 if (NewGlobals.empty())
428 return 0;
429
Chris Lattner004e2502004-10-11 05:54:41 +0000430 DEBUG(std::cerr << "PERFORMING GLOBAL SRA ON: " << *GV);
431
Chris Lattnerabab0712004-10-08 17:32:09 +0000432 Constant *NullInt = Constant::getNullValue(Type::IntTy);
433
434 // Loop over all of the uses of the global, replacing the constantexpr geps,
435 // with smaller constantexpr geps or direct references.
436 while (!GV->use_empty()) {
Chris Lattner004e2502004-10-11 05:54:41 +0000437 User *GEP = GV->use_back();
438 assert(((isa<ConstantExpr>(GEP) &&
439 cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
440 isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000441
Chris Lattnerabab0712004-10-08 17:32:09 +0000442 // Ignore the 1th operand, which has to be zero or else the program is quite
443 // broken (undefined). Get the 2nd operand, which is the structure or array
444 // index.
Chris Lattner46fa04b2005-01-08 19:45:31 +0000445 unsigned Val =
446 (unsigned)cast<ConstantInt>(GEP->getOperand(2))->getRawValue();
Chris Lattnerabab0712004-10-08 17:32:09 +0000447 if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
448
Chris Lattner004e2502004-10-11 05:54:41 +0000449 Value *NewPtr = NewGlobals[Val];
Chris Lattnerabab0712004-10-08 17:32:09 +0000450
451 // Form a shorter GEP if needed.
Chris Lattner004e2502004-10-11 05:54:41 +0000452 if (GEP->getNumOperands() > 3)
453 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
454 std::vector<Constant*> Idxs;
455 Idxs.push_back(NullInt);
456 for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
457 Idxs.push_back(CE->getOperand(i));
458 NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs);
459 } else {
460 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
461 std::vector<Value*> Idxs;
462 Idxs.push_back(NullInt);
463 for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
464 Idxs.push_back(GEPI->getOperand(i));
465 NewPtr = new GetElementPtrInst(NewPtr, Idxs,
466 GEPI->getName()+"."+utostr(Val), GEPI);
467 }
468 GEP->replaceAllUsesWith(NewPtr);
469
470 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000471 GEPI->eraseFromParent();
Chris Lattner004e2502004-10-11 05:54:41 +0000472 else
473 cast<ConstantExpr>(GEP)->destroyConstant();
Chris Lattnerabab0712004-10-08 17:32:09 +0000474 }
475
Chris Lattner73ad73e2004-10-08 20:25:55 +0000476 // Delete the old global, now that it is dead.
477 Globals.erase(GV);
Chris Lattnerabab0712004-10-08 17:32:09 +0000478 ++NumSRA;
Chris Lattner004e2502004-10-11 05:54:41 +0000479
480 // Loop over the new globals array deleting any globals that are obviously
481 // dead. This can arise due to scalarization of a structure or an array that
482 // has elements that are dead.
483 unsigned FirstGlobal = 0;
484 for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
485 if (NewGlobals[i]->use_empty()) {
486 Globals.erase(NewGlobals[i]);
487 if (FirstGlobal == i) ++FirstGlobal;
488 }
489
490 return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0;
Chris Lattnerabab0712004-10-08 17:32:09 +0000491}
492
Chris Lattner09a52722004-10-09 21:48:45 +0000493/// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
494/// value will trap if the value is dynamically null.
495static bool AllUsesOfValueWillTrapIfNull(Value *V) {
496 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
497 if (isa<LoadInst>(*UI)) {
498 // Will trap.
499 } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
500 if (SI->getOperand(0) == V) {
501 //std::cerr << "NONTRAPPING USE: " << **UI;
502 return false; // Storing the value.
503 }
504 } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
505 if (CI->getOperand(0) != V) {
506 //std::cerr << "NONTRAPPING USE: " << **UI;
507 return false; // Not calling the ptr
508 }
509 } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
510 if (II->getOperand(0) != V) {
511 //std::cerr << "NONTRAPPING USE: " << **UI;
512 return false; // Not calling the ptr
513 }
514 } else if (CastInst *CI = dyn_cast<CastInst>(*UI)) {
515 if (!AllUsesOfValueWillTrapIfNull(CI)) return false;
516 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) {
517 if (!AllUsesOfValueWillTrapIfNull(GEPI)) return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000518 } else if (isa<SetCondInst>(*UI) &&
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000519 isa<ConstantPointerNull>(UI->getOperand(1))) {
520 // Ignore setcc X, null
Chris Lattner09a52722004-10-09 21:48:45 +0000521 } else {
522 //std::cerr << "NONTRAPPING USE: " << **UI;
523 return false;
524 }
525 return true;
526}
527
528/// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000529/// from GV will trap if the loaded value is null. Note that this also permits
530/// comparisons of the loaded value against null, as a special case.
Chris Lattner09a52722004-10-09 21:48:45 +0000531static bool AllUsesOfLoadedValueWillTrapIfNull(GlobalVariable *GV) {
532 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI!=E; ++UI)
533 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
534 if (!AllUsesOfValueWillTrapIfNull(LI))
535 return false;
536 } else if (isa<StoreInst>(*UI)) {
537 // Ignore stores to the global.
538 } else {
539 // We don't know or understand this user, bail out.
540 //std::cerr << "UNKNOWN USER OF GLOBAL!: " << **UI;
541 return false;
542 }
543
544 return true;
545}
546
Chris Lattnere42eb312004-10-10 23:14:11 +0000547static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
548 bool Changed = false;
549 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) {
550 Instruction *I = cast<Instruction>(*UI++);
551 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
552 LI->setOperand(0, NewV);
553 Changed = true;
554 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
555 if (SI->getOperand(1) == V) {
556 SI->setOperand(1, NewV);
557 Changed = true;
558 }
559 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
560 if (I->getOperand(0) == V) {
561 // Calling through the pointer! Turn into a direct call, but be careful
562 // that the pointer is not also being passed as an argument.
563 I->setOperand(0, NewV);
564 Changed = true;
565 bool PassedAsArg = false;
566 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i)
567 if (I->getOperand(i) == V) {
568 PassedAsArg = true;
569 I->setOperand(i, NewV);
570 }
571
572 if (PassedAsArg) {
573 // Being passed as an argument also. Be careful to not invalidate UI!
574 UI = V->use_begin();
575 }
576 }
577 } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
578 Changed |= OptimizeAwayTrappingUsesOfValue(CI,
579 ConstantExpr::getCast(NewV, CI->getType()));
580 if (CI->use_empty()) {
581 Changed = true;
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000582 CI->eraseFromParent();
Chris Lattnere42eb312004-10-10 23:14:11 +0000583 }
584 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
585 // Should handle GEP here.
586 std::vector<Constant*> Indices;
587 Indices.reserve(GEPI->getNumOperands()-1);
588 for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
589 if (Constant *C = dyn_cast<Constant>(GEPI->getOperand(i)))
590 Indices.push_back(C);
591 else
592 break;
593 if (Indices.size() == GEPI->getNumOperands()-1)
594 Changed |= OptimizeAwayTrappingUsesOfValue(GEPI,
595 ConstantExpr::getGetElementPtr(NewV, Indices));
596 if (GEPI->use_empty()) {
597 Changed = true;
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000598 GEPI->eraseFromParent();
Chris Lattnere42eb312004-10-10 23:14:11 +0000599 }
600 }
601 }
602
603 return Changed;
604}
605
606
607/// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null
608/// value stored into it. If there are uses of the loaded value that would trap
609/// if the loaded value is dynamically null, then we know that they cannot be
610/// reachable with a null optimize away the load.
611static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV) {
612 std::vector<LoadInst*> Loads;
613 bool Changed = false;
614
615 // Replace all uses of loads with uses of uses of the stored value.
616 for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end();
617 GUI != E; ++GUI)
618 if (LoadInst *LI = dyn_cast<LoadInst>(*GUI)) {
619 Loads.push_back(LI);
620 Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
621 } else {
622 assert(isa<StoreInst>(*GUI) && "Only expect load and stores!");
623 }
624
625 if (Changed) {
626 DEBUG(std::cerr << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV);
627 ++NumGlobUses;
628 }
629
630 // Delete all of the loads we can, keeping track of whether we nuked them all!
631 bool AllLoadsGone = true;
632 while (!Loads.empty()) {
633 LoadInst *L = Loads.back();
634 if (L->use_empty()) {
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000635 L->eraseFromParent();
Chris Lattnere42eb312004-10-10 23:14:11 +0000636 Changed = true;
637 } else {
638 AllLoadsGone = false;
639 }
640 Loads.pop_back();
641 }
642
643 // If we nuked all of the loads, then none of the stores are needed either,
644 // nor is the global.
645 if (AllLoadsGone) {
646 DEBUG(std::cerr << " *** GLOBAL NOW DEAD!\n");
647 CleanupConstantGlobalUsers(GV, 0);
648 if (GV->use_empty()) {
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000649 GV->eraseFromParent();
Chris Lattnere42eb312004-10-10 23:14:11 +0000650 ++NumDeleted;
651 }
652 Changed = true;
653 }
654 return Changed;
655}
656
Chris Lattner004e2502004-10-11 05:54:41 +0000657/// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
658/// instructions that are foldable.
659static void ConstantPropUsersOf(Value *V) {
660 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; )
661 if (Instruction *I = dyn_cast<Instruction>(*UI++))
662 if (Constant *NewC = ConstantFoldInstruction(I)) {
663 I->replaceAllUsesWith(NewC);
664
Chris Lattnerd6a44922005-02-01 01:23:31 +0000665 // Advance UI to the next non-I use to avoid invalidating it!
666 // Instructions could multiply use V.
667 while (UI != E && *UI == I)
Chris Lattner004e2502004-10-11 05:54:41 +0000668 ++UI;
Chris Lattnerd6a44922005-02-01 01:23:31 +0000669 I->eraseFromParent();
Chris Lattner004e2502004-10-11 05:54:41 +0000670 }
671}
672
673/// OptimizeGlobalAddressOfMalloc - This function takes the specified global
674/// variable, and transforms the program as if it always contained the result of
675/// the specified malloc. Because it is always the result of the specified
676/// malloc, there is no reason to actually DO the malloc. Instead, turn the
677/// malloc into a global, and any laods of GV as uses of the new global.
678static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
679 MallocInst *MI) {
680 DEBUG(std::cerr << "PROMOTING MALLOC GLOBAL: " << *GV << " MALLOC = " <<*MI);
681 ConstantInt *NElements = cast<ConstantInt>(MI->getArraySize());
682
683 if (NElements->getRawValue() != 1) {
684 // If we have an array allocation, transform it to a single element
685 // allocation to make the code below simpler.
686 Type *NewTy = ArrayType::get(MI->getAllocatedType(),
Chris Lattner46fa04b2005-01-08 19:45:31 +0000687 (unsigned)NElements->getRawValue());
Chris Lattner004e2502004-10-11 05:54:41 +0000688 MallocInst *NewMI =
689 new MallocInst(NewTy, Constant::getNullValue(Type::UIntTy),
690 MI->getName(), MI);
691 std::vector<Value*> Indices;
692 Indices.push_back(Constant::getNullValue(Type::IntTy));
693 Indices.push_back(Indices[0]);
694 Value *NewGEP = new GetElementPtrInst(NewMI, Indices,
695 NewMI->getName()+".el0", MI);
696 MI->replaceAllUsesWith(NewGEP);
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000697 MI->eraseFromParent();
Chris Lattner004e2502004-10-11 05:54:41 +0000698 MI = NewMI;
699 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000700
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000701 // Create the new global variable. The contents of the malloc'd memory is
702 // undefined, so initialize with an undef value.
703 Constant *Init = UndefValue::get(MI->getAllocatedType());
Chris Lattner004e2502004-10-11 05:54:41 +0000704 GlobalVariable *NewGV = new GlobalVariable(MI->getAllocatedType(), false,
705 GlobalValue::InternalLinkage, Init,
706 GV->getName()+".body");
707 GV->getParent()->getGlobalList().insert(GV, NewGV);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000708
Chris Lattner004e2502004-10-11 05:54:41 +0000709 // Anything that used the malloc now uses the global directly.
710 MI->replaceAllUsesWith(NewGV);
Chris Lattner004e2502004-10-11 05:54:41 +0000711
712 Constant *RepValue = NewGV;
713 if (NewGV->getType() != GV->getType()->getElementType())
714 RepValue = ConstantExpr::getCast(RepValue, GV->getType()->getElementType());
715
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000716 // If there is a comparison against null, we will insert a global bool to
717 // keep track of whether the global was initialized yet or not.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000718 GlobalVariable *InitBool =
719 new GlobalVariable(Type::BoolTy, false, GlobalValue::InternalLinkage,
Chris Lattner3b181392004-12-02 06:25:58 +0000720 ConstantBool::False, GV->getName()+".init");
721 bool InitBoolUsed = false;
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000722
Chris Lattner004e2502004-10-11 05:54:41 +0000723 // Loop over all uses of GV, processing them in turn.
Chris Lattner3b181392004-12-02 06:25:58 +0000724 std::vector<StoreInst*> Stores;
Chris Lattner004e2502004-10-11 05:54:41 +0000725 while (!GV->use_empty())
726 if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) {
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000727 while (!LI->use_empty()) {
Chris Lattnerd6a44922005-02-01 01:23:31 +0000728 Use &LoadUse = LI->use_begin().getUse();
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000729 if (!isa<SetCondInst>(LoadUse.getUser()))
730 LoadUse = RepValue;
731 else {
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000732 // Replace the setcc X, 0 with a use of the bool value.
733 SetCondInst *SCI = cast<SetCondInst>(LoadUse.getUser());
734 Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", SCI);
Chris Lattner3b181392004-12-02 06:25:58 +0000735 InitBoolUsed = true;
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000736 switch (SCI->getOpcode()) {
737 default: assert(0 && "Unknown opcode!");
738 case Instruction::SetLT:
739 LV = ConstantBool::False; // X < null -> always false
740 break;
741 case Instruction::SetEQ:
742 case Instruction::SetLE:
743 LV = BinaryOperator::createNot(LV, "notinit", SCI);
744 break;
745 case Instruction::SetNE:
746 case Instruction::SetGE:
747 case Instruction::SetGT:
748 break; // no change.
749 }
750 SCI->replaceAllUsesWith(LV);
751 SCI->eraseFromParent();
752 }
753 }
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000754 LI->eraseFromParent();
Chris Lattner004e2502004-10-11 05:54:41 +0000755 } else {
756 StoreInst *SI = cast<StoreInst>(GV->use_back());
Chris Lattner3b181392004-12-02 06:25:58 +0000757 // The global is initialized when the store to it occurs.
758 new StoreInst(ConstantBool::True, InitBool, SI);
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000759 SI->eraseFromParent();
Chris Lattner004e2502004-10-11 05:54:41 +0000760 }
761
Chris Lattner3b181392004-12-02 06:25:58 +0000762 // If the initialization boolean was used, insert it, otherwise delete it.
763 if (!InitBoolUsed) {
764 while (!InitBool->use_empty()) // Delete initializations
765 cast<Instruction>(InitBool->use_back())->eraseFromParent();
766 delete InitBool;
767 } else
768 GV->getParent()->getGlobalList().insert(GV, InitBool);
769
770
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000771 // Now the GV is dead, nuke it and the malloc.
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000772 GV->eraseFromParent();
Chris Lattnerfe9abf92004-10-22 06:43:28 +0000773 MI->eraseFromParent();
Chris Lattner004e2502004-10-11 05:54:41 +0000774
775 // To further other optimizations, loop over all users of NewGV and try to
776 // constant prop them. This will promote GEP instructions with constant
777 // indices into GEP constant-exprs, which will allow global-opt to hack on it.
778 ConstantPropUsersOf(NewGV);
779 if (RepValue != NewGV)
780 ConstantPropUsersOf(RepValue);
781
782 return NewGV;
783}
Chris Lattnere42eb312004-10-10 23:14:11 +0000784
Chris Lattnerc0677c02004-12-02 07:11:07 +0000785/// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
786/// to make sure that there are no complex uses of V. We permit simple things
787/// like dereferencing the pointer, but not storing through the address, unless
788/// it is to the specified global.
789static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V,
790 GlobalVariable *GV) {
791 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI)
792 if (isa<LoadInst>(*UI) || isa<SetCondInst>(*UI)) {
793 // Fine, ignore.
794 } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
795 if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
796 return false; // Storing the pointer itself... bad.
797 // Otherwise, storing through it, or storing into GV... fine.
798 } else if (isa<GetElementPtrInst>(*UI) || isa<SelectInst>(*UI)) {
799 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(cast<Instruction>(*UI),GV))
800 return false;
801 } else {
802 return false;
803 }
804 return true;
805
806}
807
Chris Lattner09a52722004-10-09 21:48:45 +0000808// OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
809// that only one value (besides its initializer) is ever stored to the global.
Chris Lattner004e2502004-10-11 05:54:41 +0000810static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
Chris Lattner531f9e92005-03-15 04:54:21 +0000811 Module::global_iterator &GVI, TargetData &TD) {
Chris Lattner09a52722004-10-09 21:48:45 +0000812 if (CastInst *CI = dyn_cast<CastInst>(StoredOnceVal))
813 StoredOnceVal = CI->getOperand(0);
814 else if (GetElementPtrInst *GEPI =dyn_cast<GetElementPtrInst>(StoredOnceVal)){
Chris Lattnere42eb312004-10-10 23:14:11 +0000815 // "getelementptr Ptr, 0, 0, 0" is really just a cast.
Chris Lattner09a52722004-10-09 21:48:45 +0000816 bool IsJustACast = true;
817 for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
818 if (!isa<Constant>(GEPI->getOperand(i)) ||
819 !cast<Constant>(GEPI->getOperand(i))->isNullValue()) {
820 IsJustACast = false;
821 break;
822 }
823 if (IsJustACast)
824 StoredOnceVal = GEPI->getOperand(0);
825 }
826
Chris Lattnere42eb312004-10-10 23:14:11 +0000827 // If we are dealing with a pointer global that is initialized to null and
828 // only has one (non-null) value stored into it, then we can optimize any
829 // users of the loaded value (often calls and loads) that would trap if the
830 // value was null.
Chris Lattner09a52722004-10-09 21:48:45 +0000831 if (isa<PointerType>(GV->getInitializer()->getType()) &&
832 GV->getInitializer()->isNullValue()) {
Chris Lattnere42eb312004-10-10 23:14:11 +0000833 if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
834 if (GV->getInitializer()->getType() != SOVC->getType())
835 SOVC = ConstantExpr::getCast(SOVC, GV->getInitializer()->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000836
Chris Lattnere42eb312004-10-10 23:14:11 +0000837 // Optimize away any trapping uses of the loaded value.
838 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC))
Chris Lattner604ed7a2004-10-10 17:07:12 +0000839 return true;
Chris Lattner004e2502004-10-11 05:54:41 +0000840 } else if (MallocInst *MI = dyn_cast<MallocInst>(StoredOnceVal)) {
841 // If we have a global that is only initialized with a fixed size malloc,
842 // and if all users of the malloc trap, and if the malloc'd address is not
843 // put anywhere else, transform the program to use global memory instead
844 // of malloc'd memory. This eliminates dynamic allocation (good) and
845 // exposes the resultant global to further GlobalOpt (even better). Note
846 // that we restrict this transformation to only working on small
847 // allocations (2048 bytes currently), as we don't want to introduce a 16M
848 // global or something.
849 if (ConstantInt *NElements = dyn_cast<ConstantInt>(MI->getArraySize()))
850 if (MI->getAllocatedType()->isSized() &&
851 NElements->getRawValue()*
852 TD.getTypeSize(MI->getAllocatedType()) < 2048 &&
Chris Lattnerc0677c02004-12-02 07:11:07 +0000853 AllUsesOfLoadedValueWillTrapIfNull(GV) &&
854 ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV)) {
Chris Lattner004e2502004-10-11 05:54:41 +0000855 GVI = OptimizeGlobalAddressOfMalloc(GV, MI);
856 return true;
857 }
Chris Lattnere42eb312004-10-10 23:14:11 +0000858 }
Chris Lattner09a52722004-10-09 21:48:45 +0000859 }
Chris Lattner004e2502004-10-11 05:54:41 +0000860
Chris Lattner09a52722004-10-09 21:48:45 +0000861 return false;
862}
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000863
Chris Lattner40e4cec2004-12-12 05:53:50 +0000864/// ShrinkGlobalToBoolean - At this point, we have learned that the only two
Misha Brukmanb1c93172005-04-21 23:48:37 +0000865/// values ever stored into GV are its initializer and OtherVal.
Chris Lattner40e4cec2004-12-12 05:53:50 +0000866static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
867 // Create the new global, initializing it to false.
868 GlobalVariable *NewGV = new GlobalVariable(Type::BoolTy, false,
869 GlobalValue::InternalLinkage, ConstantBool::False, GV->getName()+".b");
870 GV->getParent()->getGlobalList().insert(GV, NewGV);
871
872 Constant *InitVal = GV->getInitializer();
873 assert(InitVal->getType() != Type::BoolTy && "No reason to shrink to bool!");
874
875 // If initialized to zero and storing one into the global, we can use a cast
876 // instead of a select to synthesize the desired value.
877 bool IsOneZero = false;
878 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
879 IsOneZero = InitVal->isNullValue() && CI->equalsInt(1);
880
881 while (!GV->use_empty()) {
882 Instruction *UI = cast<Instruction>(GV->use_back());
883 if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
884 // Change the store into a boolean store.
885 bool StoringOther = SI->getOperand(0) == OtherVal;
886 // Only do this if we weren't storing a loaded value.
Chris Lattner745196a2004-12-12 19:34:41 +0000887 Value *StoreVal;
Chris Lattner40e4cec2004-12-12 05:53:50 +0000888 if (StoringOther || SI->getOperand(0) == InitVal)
Chris Lattner745196a2004-12-12 19:34:41 +0000889 StoreVal = ConstantBool::get(StoringOther);
890 else {
891 // Otherwise, we are storing a previously loaded copy. To do this,
892 // change the copy from copying the original value to just copying the
893 // bool.
894 Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
895
896 // If we're already replaced the input, StoredVal will be a cast or
897 // select instruction. If not, it will be a load of the original
898 // global.
899 if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
900 assert(LI->getOperand(0) == GV && "Not a copy!");
901 // Insert a new load, to preserve the saved value.
902 StoreVal = new LoadInst(NewGV, LI->getName()+".b", LI);
903 } else {
904 assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
905 "This is not a form that we understand!");
906 StoreVal = StoredVal->getOperand(0);
907 assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
908 }
909 }
910 new StoreInst(StoreVal, NewGV, SI);
911 } else if (!UI->use_empty()) {
Chris Lattner40e4cec2004-12-12 05:53:50 +0000912 // Change the load into a load of bool then a select.
913 LoadInst *LI = cast<LoadInst>(UI);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000914
Chris Lattner40e4cec2004-12-12 05:53:50 +0000915 std::string Name = LI->getName(); LI->setName("");
916 LoadInst *NLI = new LoadInst(NewGV, Name+".b", LI);
917 Value *NSI;
918 if (IsOneZero)
919 NSI = new CastInst(NLI, LI->getType(), Name, LI);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000920 else
Chris Lattner40e4cec2004-12-12 05:53:50 +0000921 NSI = new SelectInst(NLI, OtherVal, InitVal, Name, LI);
922 LI->replaceAllUsesWith(NSI);
923 }
924 UI->eraseFromParent();
925 }
926
927 GV->eraseFromParent();
928}
929
930
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000931/// ProcessInternalGlobal - Analyze the specified global variable and optimize
932/// it if possible. If we make a change, return true.
Chris Lattner004e2502004-10-11 05:54:41 +0000933bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
Chris Lattner531f9e92005-03-15 04:54:21 +0000934 Module::global_iterator &GVI) {
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000935 std::set<PHINode*> PHIUsers;
936 GlobalStatus GS;
937 PHIUsers.clear();
938 GV->removeDeadConstantUsers();
939
940 if (GV->use_empty()) {
941 DEBUG(std::cerr << "GLOBAL DEAD: " << *GV);
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000942 GV->eraseFromParent();
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000943 ++NumDeleted;
944 return true;
945 }
946
947 if (!AnalyzeGlobal(GV, GS, PHIUsers)) {
Alkis Evlogimenosc4a44c62005-02-10 18:36:30 +0000948 // If this is a first class global and has only one accessing function
949 // and this function is main (which we know is not recursive we can make
950 // this global a local variable) we replace the global with a local alloca
951 // in this function.
952 //
953 // NOTE: It doesn't make sense to promote non first class types since we
954 // are just replacing static memory to stack memory.
955 if (!GS.HasMultipleAccessingFunctions &&
Chris Lattner50bdfcb2005-06-15 21:11:48 +0000956 GS.AccessingFunction && !GS.HasNonInstructionUser &&
Alkis Evlogimenosc4a44c62005-02-10 18:36:30 +0000957 GV->getType()->getElementType()->isFirstClassType() &&
958 GS.AccessingFunction->getName() == "main" &&
959 GS.AccessingFunction->hasExternalLinkage()) {
960 DEBUG(std::cerr << "LOCALIZING GLOBAL: " << *GV);
961 Instruction* FirstI = GS.AccessingFunction->getEntryBlock().begin();
962 const Type* ElemTy = GV->getType()->getElementType();
963 AllocaInst* Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), FirstI);
964 if (!isa<UndefValue>(GV->getInitializer()))
965 new StoreInst(GV->getInitializer(), Alloca, FirstI);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000966
Alkis Evlogimenosc4a44c62005-02-10 18:36:30 +0000967 GV->replaceAllUsesWith(Alloca);
968 GV->eraseFromParent();
969 ++NumLocalized;
970 return true;
971 }
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000972 // If the global is never loaded (but may be stored to), it is dead.
973 // Delete it now.
974 if (!GS.isLoaded) {
975 DEBUG(std::cerr << "GLOBAL NEVER LOADED: " << *GV);
Chris Lattnerf369b382004-10-09 03:32:52 +0000976
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000977 // Delete any stores we can find to the global. We may not be able to
978 // make it completely dead though.
Chris Lattnercb9f1522004-10-10 16:43:46 +0000979 bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer());
Chris Lattnerf369b382004-10-09 03:32:52 +0000980
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000981 // If the global is dead now, delete it.
982 if (GV->use_empty()) {
Chris Lattner8e71c6a2004-10-16 18:09:00 +0000983 GV->eraseFromParent();
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000984 ++NumDeleted;
Chris Lattnerf369b382004-10-09 03:32:52 +0000985 Changed = true;
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000986 }
Chris Lattnerf369b382004-10-09 03:32:52 +0000987 return Changed;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000988
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000989 } else if (GS.StoredType <= GlobalStatus::isInitializerStored) {
990 DEBUG(std::cerr << "MARKING CONSTANT: " << *GV);
991 GV->setConstant(true);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000992
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000993 // Clean up any obviously simplifiable users now.
994 CleanupConstantGlobalUsers(GV, GV->getInitializer());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000995
Chris Lattner1c4bddc2004-10-08 20:59:28 +0000996 // If the global is dead now, just nuke it.
997 if (GV->use_empty()) {
998 DEBUG(std::cerr << " *** Marking constant allowed us to simplify "
999 "all users and delete global!\n");
Chris Lattner8e71c6a2004-10-16 18:09:00 +00001000 GV->eraseFromParent();
Chris Lattner1c4bddc2004-10-08 20:59:28 +00001001 ++NumDeleted;
1002 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001003
Chris Lattner1c4bddc2004-10-08 20:59:28 +00001004 ++NumMarked;
1005 return true;
1006 } else if (!GS.isNotSuitableForSRA &&
1007 !GV->getInitializer()->getType()->isFirstClassType()) {
Chris Lattner1c4bddc2004-10-08 20:59:28 +00001008 if (GlobalVariable *FirstNewGV = SRAGlobal(GV)) {
1009 GVI = FirstNewGV; // Don't skip the newly produced globals!
1010 return true;
1011 }
Chris Lattner09a52722004-10-09 21:48:45 +00001012 } else if (GS.StoredType == GlobalStatus::isStoredOnce) {
Chris Lattner40e4cec2004-12-12 05:53:50 +00001013 // If the initial value for the global was an undef value, and if only
1014 // one other value was stored into it, we can just change the
1015 // initializer to be an undef value, then delete all stores to the
1016 // global. This allows us to mark it constant.
1017 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
1018 if (isa<UndefValue>(GV->getInitializer())) {
1019 // Change the initial value here.
1020 GV->setInitializer(SOVConstant);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001021
Chris Lattner40e4cec2004-12-12 05:53:50 +00001022 // Clean up any obviously simplifiable users now.
1023 CleanupConstantGlobalUsers(GV, GV->getInitializer());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001024
Chris Lattner40e4cec2004-12-12 05:53:50 +00001025 if (GV->use_empty()) {
1026 DEBUG(std::cerr << " *** Substituting initializer allowed us to "
1027 "simplify all users and delete global!\n");
1028 GV->eraseFromParent();
1029 ++NumDeleted;
1030 } else {
1031 GVI = GV;
1032 }
1033 ++NumSubstitute;
1034 return true;
Chris Lattner8e71c6a2004-10-16 18:09:00 +00001035 }
Chris Lattner8e71c6a2004-10-16 18:09:00 +00001036
Chris Lattner09a52722004-10-09 21:48:45 +00001037 // Try to optimize globals based on the knowledge that only one value
1038 // (besides its initializer) is ever stored to the global.
Chris Lattner004e2502004-10-11 05:54:41 +00001039 if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI,
1040 getAnalysis<TargetData>()))
Chris Lattner09a52722004-10-09 21:48:45 +00001041 return true;
Chris Lattner40e4cec2004-12-12 05:53:50 +00001042
1043 // Otherwise, if the global was not a boolean, we can shrink it to be a
1044 // boolean.
1045 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
Chris Lattner1cbd5be2004-12-12 06:03:06 +00001046 if (GV->getType()->getElementType() != Type::BoolTy &&
1047 !GV->getType()->getElementType()->isFloatingPoint()) {
Chris Lattner40e4cec2004-12-12 05:53:50 +00001048 DEBUG(std::cerr << " *** SHRINKING TO BOOL: " << *GV);
1049 ShrinkGlobalToBoolean(GV, SOVConstant);
1050 ++NumShrunkToBool;
1051 return true;
1052 }
Chris Lattner1c4bddc2004-10-08 20:59:28 +00001053 }
1054 }
1055 return false;
1056}
1057
Chris Lattnera4c80222005-05-08 22:18:06 +00001058/// OnlyCalledDirectly - Return true if the specified function is only called
1059/// directly. In other words, its address is never taken.
1060static bool OnlyCalledDirectly(Function *F) {
1061 for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){
1062 Instruction *User = dyn_cast<Instruction>(*UI);
1063 if (!User) return false;
1064 if (!isa<CallInst>(User) && !isa<InvokeInst>(User)) return false;
1065
1066 // See if the function address is passed as an argument.
1067 for (unsigned i = 1, e = User->getNumOperands(); i != e; ++i)
1068 if (User->getOperand(i) == F) return false;
1069 }
1070 return true;
1071}
1072
1073/// ChangeCalleesToFastCall - Walk all of the direct calls of the specified
1074/// function, changing them to FastCC.
1075static void ChangeCalleesToFastCall(Function *F) {
1076 for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){
1077 Instruction *User = cast<Instruction>(*UI);
1078 if (CallInst *CI = dyn_cast<CallInst>(User))
1079 CI->setCallingConv(CallingConv::Fast);
1080 else
1081 cast<InvokeInst>(User)->setCallingConv(CallingConv::Fast);
1082 }
1083}
Chris Lattner1c4bddc2004-10-08 20:59:28 +00001084
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001085bool GlobalOpt::OptimizeFunctions(Module &M) {
1086 bool Changed = false;
1087 // Optimize functions.
1088 for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
1089 Function *F = FI++;
1090 F->removeDeadConstantUsers();
1091 if (F->use_empty() && (F->hasInternalLinkage() ||
1092 F->hasLinkOnceLinkage())) {
1093 M.getFunctionList().erase(F);
1094 Changed = true;
1095 ++NumFnDeleted;
1096 } else if (F->hasInternalLinkage() &&
1097 F->getCallingConv() == CallingConv::C && !F->isVarArg() &&
1098 OnlyCalledDirectly(F)) {
1099 // If this function has C calling conventions, is not a varargs
1100 // function, and is only called directly, promote it to use the Fast
1101 // calling convention.
1102 F->setCallingConv(CallingConv::Fast);
1103 ChangeCalleesToFastCall(F);
1104 ++NumFastCallFns;
1105 Changed = true;
1106 }
1107 }
1108 return Changed;
1109}
1110
1111bool GlobalOpt::OptimizeGlobalVars(Module &M) {
1112 bool Changed = false;
1113 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1114 GVI != E; ) {
1115 GlobalVariable *GV = GVI++;
1116 if (!GV->isConstant() && GV->hasInternalLinkage() &&
1117 GV->hasInitializer())
1118 Changed |= ProcessInternalGlobal(GV, GVI);
1119 }
1120 return Changed;
1121}
1122
1123/// FindGlobalCtors - Find the llvm.globalctors list, verifying that all
1124/// initializers have an init priority of 65535.
1125GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) {
1126 for (Module::giterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
1127 if (I->getName() == "llvm.global_ctors") {
1128 // Found it, verify it's an array of { int, void()* }.
1129 const ArrayType *ATy =dyn_cast<ArrayType>(I->getType()->getElementType());
1130 if (!ATy) return 0;
1131 const StructType *STy = dyn_cast<StructType>(ATy->getElementType());
1132 if (!STy || STy->getNumElements() != 2 ||
1133 STy->getElementType(0) != Type::IntTy) return 0;
1134 const PointerType *PFTy = dyn_cast<PointerType>(STy->getElementType(1));
1135 if (!PFTy) return 0;
1136 const FunctionType *FTy = dyn_cast<FunctionType>(PFTy->getElementType());
1137 if (!FTy || FTy->getReturnType() != Type::VoidTy || FTy->isVarArg() ||
1138 FTy->getNumParams() != 0)
1139 return 0;
1140
1141 // Verify that the initializer is simple enough for us to handle.
1142 if (!I->hasInitializer()) return 0;
1143 ConstantArray *CA = dyn_cast<ConstantArray>(I->getInitializer());
1144 if (!CA) return 0;
1145 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1146 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(CA->getOperand(i))) {
Chris Lattner838bdc12005-09-26 02:19:27 +00001147 if (isa<ConstantPointerNull>(CS->getOperand(1)))
1148 continue;
1149
1150 // Must have a function or null ptr.
1151 if (!isa<Function>(CS->getOperand(1)))
1152 return 0;
1153
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001154 // Init priority must be standard.
1155 ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
1156 if (!CI || CI->getRawValue() != 65535)
1157 return 0;
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001158 } else {
1159 return 0;
1160 }
1161
1162 return I;
1163 }
1164 return 0;
1165}
1166
Chris Lattner696beef2005-09-26 02:31:18 +00001167/// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand,
1168/// return a list of the functions and null terminator as a vector.
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001169static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) {
1170 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
1171 std::vector<Function*> Result;
1172 Result.reserve(CA->getNumOperands());
1173 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
1174 ConstantStruct *CS = cast<ConstantStruct>(CA->getOperand(i));
1175 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
1176 }
1177 return Result;
1178}
1179
Chris Lattner696beef2005-09-26 02:31:18 +00001180/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the
1181/// specified array, returning the new global to use.
1182static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
1183 const std::vector<Function*> &Ctors) {
1184 // If we made a change, reassemble the initializer list.
1185 std::vector<Constant*> CSVals;
1186 CSVals.push_back(ConstantSInt::get(Type::IntTy, 65535));
1187 CSVals.push_back(0);
1188
1189 // Create the new init list.
1190 std::vector<Constant*> CAList;
1191 for (unsigned i = 0, e = Ctors.size(); i != e; ++i) {
1192 if (Ctors[i])
1193 CSVals[1] = Ctors[i];
1194 else {
1195 const Type *FTy = FunctionType::get(Type::VoidTy,
1196 std::vector<const Type*>(), false);
1197 const PointerType *PFTy = PointerType::get(FTy);
1198 CSVals[1] = Constant::getNullValue(PFTy);
1199 CSVals[0] = ConstantSInt::get(Type::IntTy, 2147483647);
1200 }
1201 CAList.push_back(ConstantStruct::get(CSVals));
1202 }
1203
1204 // Create the array initializer.
1205 const Type *StructTy =
1206 cast<ArrayType>(GCL->getType()->getElementType())->getElementType();
1207 Constant *CA = ConstantArray::get(ArrayType::get(StructTy, CAList.size()),
1208 CAList);
1209
1210 // If we didn't change the number of elements, don't create a new GV.
1211 if (CA->getType() == GCL->getInitializer()->getType()) {
1212 GCL->setInitializer(CA);
1213 return GCL;
1214 }
1215
1216 // Create the new global and insert it next to the existing list.
1217 GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
1218 GCL->getLinkage(), CA,
1219 GCL->getName());
1220 GCL->setName("");
1221 GCL->getParent()->getGlobalList().insert(GCL, NGV);
1222
1223 // Nuke the old list, replacing any uses with the new one.
1224 if (!GCL->use_empty()) {
1225 Constant *V = NGV;
1226 if (V->getType() != GCL->getType())
1227 V = ConstantExpr::getCast(V, GCL->getType());
1228 GCL->replaceAllUsesWith(V);
1229 }
1230 GCL->eraseFromParent();
1231
1232 if (Ctors.size())
1233 return NGV;
1234 else
1235 return 0;
1236}
1237
1238
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001239/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
1240/// Return true if anything changed.
1241bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
1242 std::vector<Function*> Ctors = ParseGlobalCtors(GCL);
1243 bool MadeChange = false;
1244 if (Ctors.empty()) return false;
1245
1246 // Loop over global ctors, optimizing them when we can.
1247 for (unsigned i = 0; i != Ctors.size(); ++i) {
1248 Function *F = Ctors[i];
1249 // Found a null terminator in the middle of the list, prune off the rest of
1250 // the list.
Chris Lattner838bdc12005-09-26 02:19:27 +00001251 if (F == 0) {
1252 if (i != Ctors.size()-1) {
1253 Ctors.resize(i+1);
1254 MadeChange = true;
1255 }
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001256 break;
1257 }
1258
1259 // If the function is empty, just remove it from the ctor list.
1260 if (!F->empty() && isa<ReturnInst>(F->begin()->getTerminator()) &&
1261 &F->begin()->front() == F->begin()->getTerminator()) {
1262 Ctors.erase(Ctors.begin()+i);
1263 MadeChange = true;
1264 --i;
1265 ++NumEmptyCtor;
1266 }
1267 }
1268
1269 if (!MadeChange) return false;
1270
Chris Lattner696beef2005-09-26 02:31:18 +00001271 GCL = InstallGlobalCtors(GCL, Ctors);
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001272 return true;
1273}
1274
1275
Chris Lattner25db5802004-10-07 04:16:33 +00001276bool GlobalOpt::runOnModule(Module &M) {
1277 bool Changed = false;
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001278
1279 // Try to find the llvm.globalctors list.
1280 GlobalVariable *GlobalCtors = FindGlobalCtors(M);
Chris Lattner25db5802004-10-07 04:16:33 +00001281
Chris Lattner25db5802004-10-07 04:16:33 +00001282 bool LocalChange = true;
1283 while (LocalChange) {
1284 LocalChange = false;
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001285
1286 // Delete functions that are trivially dead, ccc -> fastcc
1287 LocalChange |= OptimizeFunctions(M);
1288
1289 // Optimize global_ctors list.
1290 if (GlobalCtors)
1291 LocalChange |= OptimizeGlobalCtorsList(GlobalCtors);
1292
1293 // Optimize non-address-taken globals.
1294 LocalChange |= OptimizeGlobalVars(M);
Chris Lattner25db5802004-10-07 04:16:33 +00001295 Changed |= LocalChange;
1296 }
Chris Lattner41b6a5a2005-09-26 01:43:45 +00001297
1298 // TODO: Move all global ctors functions to the end of the module for code
1299 // layout.
1300
Chris Lattner25db5802004-10-07 04:16:33 +00001301 return Changed;
1302}