diff options
author | Devang Patel <dpatel@apple.com> | 2009-03-05 18:12:02 +0000 |
---|---|---|
committer | Devang Patel <dpatel@apple.com> | 2009-03-05 18:12:02 +0000 |
commit | 504960067233f271884c23869e81272918ad2883 (patch) | |
tree | 7c72cecfbc4604ab73f4ba15489530102a4078d5 /lib/Transforms/IPO | |
parent | 30a31ebf7ffa369e4b5636730a3ecfaa51e7e1f8 (diff) |
GlobalOpt only process non constant local GVs while optimizing global vars.
If non constant local GV named A is used by a constant local GV named B (e.g. llvm.dbg.variable) and B is not used by anyone else then eliminate A as well as B.
In other words, debug info should not interfere in removal of unused GV.
--This life, and those below, will be ignored--
M test/Transforms/GlobalOpt/2009-03-03-dbg.ll
M lib/Transforms/IPO/GlobalOpt.cpp
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66167 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/IPO')
-rw-r--r-- | lib/Transforms/IPO/GlobalOpt.cpp | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 0a35fa93e4..9284500a86 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -137,17 +137,28 @@ struct VISIBILITY_HIDDEN GlobalStatus { } /// ConstantIsDead - Return true if the specified constant is (transitively) -/// dead. The constant may be used by other constants (e.g. constant arrays and -/// constant exprs) as long as they are dead, but it cannot be used by anything -/// else. -static bool ConstantIsDead(Constant *C) { +/// dead. The constant may be used by other constants (e.g. constant arrays, +/// constant exprs, constant global variables) as long as they are dead, +/// but it cannot be used by anything else. If DeadGVs is not null then +/// record dead constant GV users. +static bool ConstantIsDead(Constant *C, + SmallPtrSet<GlobalVariable *, 4> *DeadGVs = false) { if (isa<GlobalValue>(C)) return false; - for (Value::use_iterator UI = C->use_begin(), E = C->use_end(); UI != E; ++UI) - if (Constant *CU = dyn_cast<Constant>(*UI)) { - if (!ConstantIsDead(CU)) return false; + for (Value::use_iterator UI = C->use_begin(), E = C->use_end(); UI != E; ++UI) { + if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*UI)) { + if (!GV->isConstant() || !GV->hasLocalLinkage() || !GV->use_empty()) + return false; + else { + if (DeadGVs) + DeadGVs->insert(GV); + } + } + else if (Constant *CU = dyn_cast<Constant>(*UI)) { + if (!ConstantIsDead(CU, DeadGVs)) return false; } else return false; + } return true; } @@ -338,8 +349,19 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) { } else if (Constant *C = dyn_cast<Constant>(U)) { // If we have a chain of dead constantexprs or other things dangling from // us, and if they are all dead, nuke them without remorse. - if (ConstantIsDead(C)) { - C->destroyConstant(); + SmallPtrSet<GlobalVariable *, 4> DeadGVs; + if (ConstantIsDead(C, &DeadGVs)) { + for (SmallPtrSet<GlobalVariable *, 4>::iterator TI = DeadGVs.begin(), + TE = DeadGVs.end(); TI != TE; ) { + GlobalVariable *TGV = *TI; ++TI; + if (TGV == C) + C = NULL; + TGV->eraseFromParent(); + } + if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) + GV->eraseFromParent(); + else if (C) + C->destroyConstant(); // This could have invalidated UI, start over from scratch. CleanupConstantGlobalUsers(V, Init); return true; |