@@ -283,9 +283,10 @@ class MergeFunctions {
283
283
// Replace G with an alias to F (deleting function G)
284
284
void writeAlias (Function *F, Function *G);
285
285
286
- // Replace G with an alias to F if possible, or a thunk to F if possible.
287
- // Returns false if neither is the case.
288
- bool writeThunkOrAlias (Function *F, Function *G);
286
+ // If needed, replace G with an alias to F if possible, or a thunk to F if
287
+ // profitable. Returns false if neither is the case. If \p G is not needed
288
+ // (e.g. it is discardable and linkonce_odr), \p G is removed directly.
289
+ bool writeThunkOrAliasIfNeeded (Function *F, Function *G);
289
290
290
291
// / Replace function F with function G in the function tree.
291
292
void replaceFunctionInTree (const FunctionNode &FN, Function *G);
@@ -875,9 +876,15 @@ void MergeFunctions::writeAlias(Function *F, Function *G) {
875
876
++NumAliasesWritten;
876
877
}
877
878
878
- // Replace G with an alias to F if possible, or a thunk to F if
879
- // profitable. Returns false if neither is the case.
880
- bool MergeFunctions::writeThunkOrAlias (Function *F, Function *G) {
879
+ // If needed, replace G with an alias to F if possible, or a thunk to F if
880
+ // profitable. Returns false if neither is the case. If \p G is not needed (e.g.
881
+ // it is discardable and linkonce_odr), \p G is removed directly.
882
+ bool MergeFunctions::writeThunkOrAliasIfNeeded (Function *F, Function *G) {
883
+ if (G->isDiscardableIfUnused () &&
884
+ G->use_empty () && !MergeFunctionsPDI) {
885
+ G->eraseFromParent ();
886
+ return true ;
887
+ }
881
888
if (canCreateAliasFor (G)) {
882
889
writeAlias (F, G);
883
890
return true ;
@@ -904,9 +911,10 @@ void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
904
911
assert ((!isODR (G) || isODR (F)) &&
905
912
" if G is ODR, F must also be ODR due to ordering" );
906
913
907
- // Both writeThunkOrAlias() calls below must succeed, either because we can
908
- // create aliases for G and NewF, or because a thunk for F is profitable.
909
- // F here has the same signature as NewF below, so that's what we check.
914
+ // Both writeThunkOrAliasIfNeeded() calls below must succeed, either because
915
+ // we can create aliases for G and NewF, or because a thunk for F is
916
+ // profitable. F here has the same signature as NewF below, so that's what
917
+ // we check.
910
918
if (!canCreateThunkFor (F) &&
911
919
(!canCreateAliasFor (F) || !canCreateAliasFor (G)))
912
920
return ;
@@ -930,13 +938,13 @@ void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
930
938
if (isODR (F))
931
939
replaceDirectCallers (NewF, F);
932
940
933
- // We collect alignment before writeThunkOrAlias that overwrites NewF and
934
- // G's content.
941
+ // We collect alignment before writeThunkOrAliasIfNeeded that overwrites
942
+ // NewF and G's content.
935
943
const MaybeAlign NewFAlign = NewF->getAlign ();
936
944
const MaybeAlign GAlign = G->getAlign ();
937
945
938
- writeThunkOrAlias (F, G);
939
- writeThunkOrAlias (F, NewF);
946
+ writeThunkOrAliasIfNeeded (F, G);
947
+ writeThunkOrAliasIfNeeded (F, NewF);
940
948
941
949
if (NewFAlign || GAlign)
942
950
F->setAlignment (std::max (NewFAlign.valueOrOne (), GAlign.valueOrOne ()));
@@ -975,7 +983,7 @@ void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
975
983
return ;
976
984
}
977
985
978
- if (writeThunkOrAlias (F, G)) {
986
+ if (writeThunkOrAliasIfNeeded (F, G)) {
979
987
++NumFunctionsMerged;
980
988
}
981
989
}
0 commit comments