1414// ===----------------------------------------------------------------------===//
1515
1616#include " llvm/CGData/StableFunctionMap.h"
17- #include " llvm/ADT/SmallSet.h"
1817#include " llvm/Support/CommandLine.h"
1918#include " llvm/Support/Debug.h"
2019
@@ -36,30 +35,21 @@ static cl::opt<unsigned> GlobalMergingMaxParams(
3635 cl::desc (
3736 " The maximum number of parameters allowed when merging functions." ),
3837 cl::init(std::numeric_limits<unsigned >::max()), cl::Hidden);
39- static cl::opt<bool > GlobalMergingSkipNoParams (
40- " global-merging-skip-no-params" ,
41- cl::desc (" Skip merging functions with no parameters." ), cl::init(true ),
42- cl::Hidden);
43- static cl::opt<double > GlobalMergingInstOverhead (
44- " global-merging-inst-overhead" ,
45- cl::desc (" The overhead cost associated with each instruction when lowering "
46- " to machine instruction." ),
47- cl::init(1.2 ), cl::Hidden);
48- static cl::opt<double > GlobalMergingParamOverhead (
38+ static cl::opt<unsigned > GlobalMergingParamOverhead (
4939 " global-merging-param-overhead" ,
5040 cl::desc (" The overhead cost associated with each parameter when merging "
5141 " functions." ),
52- cl::init(2.0 ), cl::Hidden);
53- static cl::opt<double >
42+ cl::init(2 ), cl::Hidden);
43+ static cl::opt<unsigned >
5444 GlobalMergingCallOverhead (" global-merging-call-overhead" ,
5545 cl::desc (" The overhead cost associated with each "
5646 " function call when merging functions." ),
57- cl::init(1.0 ), cl::Hidden);
58- static cl::opt<double > GlobalMergingExtraThreshold (
47+ cl::init(1 ), cl::Hidden);
48+ static cl::opt<unsigned > GlobalMergingExtraThreshold (
5949 " global-merging-extra-threshold" ,
6050 cl::desc (" An additional cost threshold that must be exceeded for merging "
6151 " to be considered beneficial." ),
62- cl::init(0.0 ), cl::Hidden);
52+ cl::init(0 ), cl::Hidden);
6353
6454unsigned StableFunctionMap::getIdOrCreateForName (StringRef Name) {
6555 auto It = NameToId.find (Name);
@@ -170,32 +160,21 @@ static bool isProfitable(
170160 if (InstCount < GlobalMergingMinInstrs)
171161 return false ;
172162
173- double Cost = 0.0 ;
174- SmallSet<stable_hash, 8 > UniqueHashVals;
175- for (auto &SF : SFS) {
176- UniqueHashVals.clear ();
177- for (auto &[IndexPair, Hash] : *SF->IndexOperandHashMap )
178- UniqueHashVals.insert (Hash);
179- unsigned ParamCount = UniqueHashVals.size ();
180- if (ParamCount > GlobalMergingMaxParams)
181- return false ;
182- // Theoretically, if ParamCount is 0, it results in identical code folding
183- // (ICF), which we can skip merging here since the linker already handles
184- // ICF. This pass would otherwise introduce unnecessary thunks that are
185- // merely direct jumps. However, enabling this could be beneficial depending
186- // on downstream passes, so we provide an option for it.
187- if (GlobalMergingSkipNoParams && ParamCount == 0 )
188- return false ;
189- Cost += ParamCount * GlobalMergingParamOverhead + GlobalMergingCallOverhead;
190- }
191- Cost += GlobalMergingExtraThreshold;
163+ unsigned ParamCount = SFS[0 ]->IndexOperandHashMap ->size ();
164+ if (ParamCount > GlobalMergingMaxParams)
165+ return false ;
166+
167+ unsigned Benefit = InstCount * (StableFunctionCount - 1 );
168+ unsigned Cost =
169+ (GlobalMergingParamOverhead * ParamCount + GlobalMergingCallOverhead) *
170+ StableFunctionCount +
171+ GlobalMergingExtraThreshold;
192172
193- double Benefit =
194- InstCount * (StableFunctionCount - 1 ) * GlobalMergingInstOverhead;
195173 bool Result = Benefit > Cost;
196174 LLVM_DEBUG (dbgs () << " isProfitable: Hash = " << SFS[0 ]->Hash << " , "
197175 << " StableFunctionCount = " << StableFunctionCount
198176 << " , InstCount = " << InstCount
177+ << " , ParamCount = " << ParamCount
199178 << " , Benefit = " << Benefit << " , Cost = " << Cost
200179 << " , Result = " << (Result ? " true" : " false" ) << " \n " );
201180 return Result;
0 commit comments