Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: lib/Transforms/IPO/GlobalOpt.cpp

Issue 183273009: Prep for merging 3.4: Undo changes from 3.3 branch (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Retry Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/Target/X86/X86Subtarget.cpp ('k') | lib/Transforms/InstCombine/InstCombineMulDivRem.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- GlobalOpt.cpp - Optimize Global Variables --------------------------===// 1 //===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This pass transforms simple global variables that never have their address 10 // This pass transforms simple global variables that never have their address
(...skipping 3044 matching lines...) Expand 10 before | Expand all | Expand 10 after
3055 continue; 3055 continue;
3056 } 3056 }
3057 } 3057 }
3058 3058
3059 if (!MadeChange) return false; 3059 if (!MadeChange) return false;
3060 3060
3061 GCL = InstallGlobalCtors(GCL, Ctors); 3061 GCL = InstallGlobalCtors(GCL, Ctors);
3062 return true; 3062 return true;
3063 } 3063 }
3064 3064
3065 static Value::use_iterator getFirst(Value *V, SmallPtrSet<Use*, 8> &Tried) {
3066 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
3067 Use *U = &I.getUse();
3068 if (Tried.count(U))
3069 continue;
3070
3071 User *Usr = *I;
3072 GlobalVariable *GV = dyn_cast<GlobalVariable>(Usr);
3073 if (!GV || !GV->hasName()) {
3074 Tried.insert(U);
3075 return I;
3076 }
3077
3078 StringRef Name = GV->getName();
3079 if (Name != "llvm.used" && Name != "llvm.compiler_used") {
3080 Tried.insert(U);
3081 return I;
3082 }
3083 }
3084 return V->use_end();
3085 }
3086
3087 static bool replaceAllNonLLVMUsedUsesWith(Constant *Old, Constant *New);
3088
3089 static bool replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
3090 Value *ToV, Use *U) {
3091 Constant *To = cast<Constant>(ToV);
3092
3093 SmallVector<Constant*, 8> NewOps;
3094 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
3095 Constant *Op = CA->getOperand(i);
3096 NewOps.push_back(Op == From ? To : Op);
3097 }
3098
3099 Constant *Replacement = ConstantArray::get(CA->getType(), NewOps);
3100 assert(Replacement != CA && "CA didn't contain From!");
3101
3102 bool Ret = replaceAllNonLLVMUsedUsesWith(CA, Replacement);
3103 if (Replacement->use_empty())
3104 Replacement->destroyConstant();
3105 if (CA->use_empty())
3106 CA->destroyConstant();
3107 return Ret;
3108 }
3109
3110 static bool replaceUsesOfWithOnConstant(ConstantExpr *CE, Value *From,
3111 Value *ToV, Use *U) {
3112 Constant *To = cast<Constant>(ToV);
3113 SmallVector<Constant*, 8> NewOps;
3114 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
3115 Constant *Op = CE->getOperand(i);
3116 NewOps.push_back(Op == From ? To : Op);
3117 }
3118
3119 Constant *Replacement = CE->getWithOperands(NewOps);
3120 assert(Replacement != CE && "CE didn't contain From!");
3121
3122 bool Ret = replaceAllNonLLVMUsedUsesWith(CE, Replacement);
3123 if (Replacement->use_empty())
3124 Replacement->destroyConstant();
3125 if (CE->use_empty())
3126 CE->destroyConstant();
3127 return Ret;
3128 }
3129
3130 static bool replaceUsesOfWithOnConstant(Constant *C, Value *From, Value *To,
3131 Use *U) {
3132 if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
3133 return replaceUsesOfWithOnConstant(CA, From, To, U);
3134 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
3135 return replaceUsesOfWithOnConstant(CE, From, To, U);
3136 C->replaceUsesOfWithOnConstant(From, To, U);
3137 return true;
3138 }
3139
3140 static bool replaceAllNonLLVMUsedUsesWith(Constant *Old, Constant *New) {
3141 SmallPtrSet<Use*, 8> Tried;
3142 bool Ret = false;
3143 for (;;) {
3144 Value::use_iterator I = getFirst(Old, Tried);
3145 if (I == Old->use_end())
3146 break;
3147 Use &U = I.getUse();
3148
3149 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
3150 // constant because they are uniqued.
3151 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
3152 if (!isa<GlobalValue>(C)) {
3153 Ret |= replaceUsesOfWithOnConstant(C, Old, New, &U);
3154 continue;
3155 }
3156 }
3157
3158 U.set(New);
3159 Ret = true;
3160 }
3161 return Ret;
3162 }
3163
3164 bool GlobalOpt::OptimizeGlobalAliases(Module &M) { 3065 bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
3165 bool Changed = false; 3066 bool Changed = false;
3166 3067
3167 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 3068 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
3168 I != E;) { 3069 I != E;) {
3169 Module::alias_iterator J = I++; 3070 Module::alias_iterator J = I++;
3170 // Aliases without names cannot be referenced outside this module. 3071 // Aliases without names cannot be referenced outside this module.
3171 if (!J->hasName() && !J->isDeclaration()) 3072 if (!J->hasName() && !J->isDeclaration())
3172 J->setLinkage(GlobalValue::InternalLinkage); 3073 J->setLinkage(GlobalValue::InternalLinkage);
3173 // If the aliasee may change at link time, nothing can be done - bail out. 3074 // If the aliasee may change at link time, nothing can be done - bail out.
3174 if (J->mayBeOverridden()) 3075 if (J->mayBeOverridden())
3175 continue; 3076 continue;
3176 3077
3177 Constant *Aliasee = J->getAliasee(); 3078 Constant *Aliasee = J->getAliasee();
3178 GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts()); 3079 GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
3179 Target->removeDeadConstantUsers(); 3080 Target->removeDeadConstantUsers();
3180 bool hasOneUse = Target->hasOneUse() && Aliasee->hasOneUse(); 3081 bool hasOneUse = Target->hasOneUse() && Aliasee->hasOneUse();
3181 3082
3182 // Make all users of the alias use the aliasee instead. 3083 // Make all users of the alias use the aliasee instead.
3183 if (replaceAllNonLLVMUsedUsesWith(J, Aliasee)) { 3084 if (!J->use_empty()) {
3085 J->replaceAllUsesWith(Aliasee);
3184 ++NumAliasesResolved; 3086 ++NumAliasesResolved;
3185 Changed = true; 3087 Changed = true;
3186 } 3088 }
3187 if (!J->use_empty())
3188 continue;
3189 3089
3190 // If the alias is externally visible, we may still be able to simplify it. 3090 // If the alias is externally visible, we may still be able to simplify it.
3191 if (!J->hasLocalLinkage()) { 3091 if (!J->hasLocalLinkage()) {
3192 // If the aliasee has internal linkage, give it the name and linkage 3092 // If the aliasee has internal linkage, give it the name and linkage
3193 // of the alias, and delete the alias. This turns: 3093 // of the alias, and delete the alias. This turns:
3194 // define internal ... @f(...) 3094 // define internal ... @f(...)
3195 // @a = alias ... @f 3095 // @a = alias ... @f
3196 // into: 3096 // into:
3197 // define ... @a(...) 3097 // define ... @a(...)
3198 if (!Target->hasLocalLinkage()) 3098 if (!Target->hasLocalLinkage())
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
3368 LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn); 3268 LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
3369 3269
3370 Changed |= LocalChange; 3270 Changed |= LocalChange;
3371 } 3271 }
3372 3272
3373 // TODO: Move all global ctors functions to the end of the module for code 3273 // TODO: Move all global ctors functions to the end of the module for code
3374 // layout. 3274 // layout.
3375 3275
3376 return Changed; 3276 return Changed;
3377 } 3277 }
OLDNEW
« no previous file with comments | « lib/Target/X86/X86Subtarget.cpp ('k') | lib/Transforms/InstCombine/InstCombineMulDivRem.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698