llvm.org GIT mirror llvm / 5b85a9b
Merging r181524: ------------------------------------------------------------------------ r181524 | rafael | 2013-05-09 10:22:59 -0700 (Thu, 09 May 2013) | 4 lines Don't replace an alias in llvm.used with its target. When we replace an internal alias with its target, be careful not to replace the entry in llvm.used (and llvm.compiler_used). ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_33@181909 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 7 years ago
2 changed file(s) with 144 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
30403040 return true;
30413041 }
30423042
3043 static Value::use_iterator getFirst(Value *V, SmallPtrSet &Tried) {
3044 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
3045 Use *U = &I.getUse();
3046 if (Tried.count(U))
3047 continue;
3048
3049 User *Usr = *I;
3050 GlobalVariable *GV = dyn_cast(Usr);
3051 if (!GV || !GV->hasName()) {
3052 Tried.insert(U);
3053 return I;
3054 }
3055
3056 StringRef Name = GV->getName();
3057 if (Name != "llvm.used" && Name != "llvm.compiler_used") {
3058 Tried.insert(U);
3059 return I;
3060 }
3061 }
3062 return V->use_end();
3063 }
3064
3065 static bool replaceAllNonLLVMUsedUsesWith(Constant *Old, Constant *New);
3066
3067 static bool replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
3068 Value *ToV, Use *U) {
3069 Constant *To = cast(ToV);
3070
3071 SmallVector NewOps;
3072 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
3073 Constant *Op = CA->getOperand(i);
3074 NewOps.push_back(Op == From ? To : Op);
3075 }
3076
3077 Constant *Replacement = ConstantArray::get(CA->getType(), NewOps);
3078 assert(Replacement != CA && "CA didn't contain From!");
3079
3080 bool Ret = replaceAllNonLLVMUsedUsesWith(CA, Replacement);
3081 if (Replacement->use_empty())
3082 Replacement->destroyConstant();
3083 if (CA->use_empty())
3084 CA->destroyConstant();
3085 return Ret;
3086 }
3087
3088 static bool replaceUsesOfWithOnConstant(ConstantExpr *CE, Value *From,
3089 Value *ToV, Use *U) {
3090 Constant *To = cast(ToV);
3091 SmallVector NewOps;
3092 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
3093 Constant *Op = CE->getOperand(i);
3094 NewOps.push_back(Op == From ? To : Op);
3095 }
3096
3097 Constant *Replacement = CE->getWithOperands(NewOps);
3098 assert(Replacement != CE && "CE didn't contain From!");
3099
3100 bool Ret = replaceAllNonLLVMUsedUsesWith(CE, Replacement);
3101 if (Replacement->use_empty())
3102 Replacement->destroyConstant();
3103 if (CE->use_empty())
3104 CE->destroyConstant();
3105 return Ret;
3106 }
3107
3108 static bool replaceUsesOfWithOnConstant(Constant *C, Value *From, Value *To,
3109 Use *U) {
3110 if (ConstantArray *CA = dyn_cast(C))
3111 return replaceUsesOfWithOnConstant(CA, From, To, U);
3112 if (ConstantExpr *CE = dyn_cast(C))
3113 return replaceUsesOfWithOnConstant(CE, From, To, U);
3114 C->replaceUsesOfWithOnConstant(From, To, U);
3115 return true;
3116 }
3117
3118 static bool replaceAllNonLLVMUsedUsesWith(Constant *Old, Constant *New) {
3119 SmallPtrSet Tried;
3120 bool Ret = false;
3121 for (;;) {
3122 Value::use_iterator I = getFirst(Old, Tried);
3123 if (I == Old->use_end())
3124 break;
3125 Use &U = I.getUse();
3126
3127 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
3128 // constant because they are uniqued.
3129 if (Constant *C = dyn_cast(U.getUser())) {
3130 if (!isa(C)) {
3131 Ret |= replaceUsesOfWithOnConstant(C, Old, New, &U);
3132 continue;
3133 }
3134 }
3135
3136 U.set(New);
3137 Ret = true;
3138 }
3139 return Ret;
3140 }
3141
30433142 bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
30443143 bool Changed = false;
30453144
30593158 bool hasOneUse = Target->hasOneUse() && Aliasee->hasOneUse();
30603159
30613160 // Make all users of the alias use the aliasee instead.
3062 if (!J->use_empty()) {
3063 J->replaceAllUsesWith(Aliasee);
3161 if (replaceAllNonLLVMUsedUsesWith(J, Aliasee)) {
30643162 ++NumAliasesResolved;
30653163 Changed = true;
30663164 }
3165 if (!J->use_empty())
3166 continue;
30673167
30683168 // If the alias is externally visible, we may still be able to simplify it.
30693169 if (!J->hasLocalLinkage()) {
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1
2 @c = global i8 42
3
4 @llvm.used = appending global [3 x i8*] [i8* bitcast (void ()* @fa to i8*), i8* bitcast (void ()* @f to i8*), i8* @ca], section "llvm.metadata"
5 ; CHECK: @llvm.used = appending global [3 x i8*] [i8* bitcast (void ()* @fa to i8*), i8* bitcast (void ()* @f to i8*), i8* @ca], section "llvm.metadata"
6
7 @llvm.compiler_used = appending global [2 x i8*] [i8* bitcast (void ()* @fa to i8*), i8* bitcast (void ()* @fa3 to i8*)], section "llvm.metadata"
8
9 @sameAsUsed = global [3 x i8*] [i8* bitcast (void ()* @fa to i8*), i8* bitcast (void ()* @f to i8*), i8* @ca]
10 ; CHECK: @sameAsUsed = global [3 x i8*] [i8* bitcast (void ()* @f to i8*), i8* bitcast (void ()* @f to i8*), i8* @c]
11
12 @other = global i32* bitcast (void ()* @fa to i32*)
13 ; CHECK: @other = global i32* bitcast (void ()* @f to i32*)
14
15 @fa = alias internal void ()* @f
16 ; CHECK: @fa = alias internal void ()* @f
17
18 @fa2 = alias internal void ()* @f
19 ; CHECK-NOT: @fa2
20
21 @fa3 = alias internal void ()* @f
22 ; CHECK: @fa3
23
24 @ca = alias internal i8* @c
25 ; CHECK: @ca = alias internal i8* @c
26
27 define void @f() {
28 ret void
29 }
30
31 define i8* @g() {
32 ret i8* bitcast (void ()* @fa to i8*);
33 }
34
35 define i8* @g2() {
36 ret i8* bitcast (void ()* @fa2 to i8*);
37 }
38
39 define i8* @h() {
40 ret i8* @ca
41 }