49
49
#include " llvm/Analysis/Loads.h"
50
50
#include " llvm/Analysis/MemoryLocation.h"
51
51
#include " llvm/Analysis/TargetLibraryInfo.h"
52
+ #include " llvm/Analysis/TargetTransformInfo.h"
52
53
#include " llvm/IR/Argument.h"
53
54
#include " llvm/IR/Attributes.h"
54
55
#include " llvm/IR/BasicBlock.h"
@@ -809,6 +810,21 @@ static bool canPaddingBeAccessed(Argument *arg) {
809
810
return false ;
810
811
}
811
812
813
+ static bool areFunctionArgsABICompatible (
814
+ const Function &F, const TargetTransformInfo &TTI,
815
+ SmallPtrSetImpl<Argument *> &ArgsToPromote,
816
+ SmallPtrSetImpl<Argument *> &ByValArgsToTransform) {
817
+ for (const Use &U : F.uses ()) {
818
+ CallSite CS (U.getUser ());
819
+ const Function *Caller = CS.getCaller ();
820
+ const Function *Callee = CS.getCalledFunction ();
821
+ if (!TTI.areFunctionArgsABICompatible (Caller, Callee, ArgsToPromote) ||
822
+ !TTI.areFunctionArgsABICompatible (Caller, Callee, ByValArgsToTransform))
823
+ return false ;
824
+ }
825
+ return true ;
826
+ }
827
+
812
828
// / PromoteArguments - This method checks the specified function to see if there
813
829
// / are any promotable arguments and if it is safe to promote the function (for
814
830
// / example, all callers are direct). If safe to promote some arguments, it
@@ -817,7 +833,8 @@ static Function *
817
833
promoteArguments (Function *F, function_ref<AAResults &(Function &F)> AARGetter,
818
834
unsigned MaxElements,
819
835
Optional<function_ref<void(CallSite OldCS, CallSite NewCS)>>
820
- ReplaceCallSite) {
836
+ ReplaceCallSite,
837
+ const TargetTransformInfo &TTI) {
821
838
// Don't perform argument promotion for naked functions; otherwise we can end
822
839
// up removing parameters that are seemingly 'not used' as they are referred
823
840
// to in the assembly.
@@ -846,7 +863,7 @@ promoteArguments(Function *F, function_ref<AAResults &(Function &F)> AARGetter,
846
863
847
864
// Second check: make sure that all callers are direct callers. We can't
848
865
// transform functions that have indirect callers. Also see if the function
849
- // is self-recursive.
866
+ // is self-recursive and check that target features are compatible .
850
867
bool isSelfRecursive = false ;
851
868
for (Use &U : F->uses ()) {
852
869
CallSite CS (U.getUser ());
@@ -955,6 +972,10 @@ promoteArguments(Function *F, function_ref<AAResults &(Function &F)> AARGetter,
955
972
if (ArgsToPromote.empty () && ByValArgsToTransform.empty ())
956
973
return nullptr ;
957
974
975
+ if (!areFunctionArgsABICompatible (*F, TTI, ArgsToPromote,
976
+ ByValArgsToTransform))
977
+ return nullptr ;
978
+
958
979
return doPromotion (F, ArgsToPromote, ByValArgsToTransform, ReplaceCallSite);
959
980
}
960
981
@@ -980,7 +1001,9 @@ PreservedAnalyses ArgumentPromotionPass::run(LazyCallGraph::SCC &C,
980
1001
return FAM.getResult <AAManager>(F);
981
1002
};
982
1003
983
- Function *NewF = promoteArguments (&OldF, AARGetter, MaxElements, None);
1004
+ const TargetTransformInfo &TTI = FAM.getResult <TargetIRAnalysis>(OldF);
1005
+ Function *NewF =
1006
+ promoteArguments (&OldF, AARGetter, MaxElements, None, TTI);
984
1007
if (!NewF)
985
1008
continue ;
986
1009
LocalChange = true ;
@@ -1018,6 +1041,7 @@ struct ArgPromotion : public CallGraphSCCPass {
1018
1041
void getAnalysisUsage (AnalysisUsage &AU) const override {
1019
1042
AU.addRequired <AssumptionCacheTracker>();
1020
1043
AU.addRequired <TargetLibraryInfoWrapperPass>();
1044
+ AU.addRequired <TargetTransformInfoWrapperPass>();
1021
1045
getAAResultsAnalysisUsage (AU);
1022
1046
CallGraphSCCPass::getAnalysisUsage (AU);
1023
1047
}
@@ -1043,6 +1067,7 @@ INITIALIZE_PASS_BEGIN(ArgPromotion, "argpromotion",
1043
1067
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1044
1068
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1045
1069
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1070
+ INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
1046
1071
INITIALIZE_PASS_END(ArgPromotion, " argpromotion" ,
1047
1072
" Promote 'by reference' arguments to scalars" , false , false )
1048
1073
@@ -1079,8 +1104,10 @@ bool ArgPromotion::runOnSCC(CallGraphSCC &SCC) {
1079
1104
CallerNode->replaceCallEdge (OldCS, NewCS, NewCalleeNode);
1080
1105
};
1081
1106
1107
+ const TargetTransformInfo &TTI =
1108
+ getAnalysis<TargetTransformInfoWrapperPass>().getTTI (*OldF);
1082
1109
if (Function *NewF = promoteArguments (OldF, AARGetter, MaxElements,
1083
- {ReplaceCallSite})) {
1110
+ {ReplaceCallSite}, TTI )) {
1084
1111
LocalChange = true ;
1085
1112
1086
1113
// Update the call graph for the newly promoted function.
0 commit comments