@@ -168,6 +168,14 @@ static cl::list<std::string>
168168 cl::desc (" Prevent function(s) from being devirtualized" ),
169169 cl::Hidden, cl::CommaSeparated);
170170
171+ // / If explicitly specified, the devirt module pass will stop transformation
172+ // / once the total number of devirtualizations reach the cutoff value. Setting
173+ // / this option to 0 explicitly will do 0 devirtualization.
174+ static cl::opt<unsigned > WholeProgramDevirtCutoff (
175+ " wholeprogramdevirt-cutoff" ,
176+ cl::desc (" Max number of devirtualizations for devirt module pass" ),
177+ cl::init(0 ));
178+
171179// / Mechanism to add runtime checking of devirtualization decisions, optionally
172180// / trapping or falling back to indirect call on any that are not correct.
173181// / Trapping mode is useful for debugging undefined behavior leading to failures
@@ -316,6 +324,9 @@ VirtualCallTarget::VirtualCallTarget(GlobalValue *Fn, const TypeMemberInfo *TM)
316324
317325namespace {
318326
327+ // Tracks the number of devirted calls in the IR transformation.
328+ static unsigned NumDevirtCalls = 0 ;
329+
319330// A slot in a set of virtual tables. The TypeID identifies the set of virtual
320331// tables, and the ByteOffset is the offset in bytes from the address point to
321332// the virtual function pointer.
@@ -1169,10 +1180,16 @@ void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
11691180 if (!OptimizedCalls.insert (&VCallSite.CB ).second )
11701181 continue ;
11711182
1183+ // Stop when the number of devirted calls reaches the cutoff.
1184+ if (WholeProgramDevirtCutoff.getNumOccurrences () > 0 &&
1185+ NumDevirtCalls >= WholeProgramDevirtCutoff)
1186+ return ;
1187+
11721188 if (RemarksEnabled)
11731189 VCallSite.emitRemark (" single-impl" ,
11741190 TheFn->stripPointerCasts ()->getName (), OREGetter);
11751191 NumSingleImpl++;
1192+ NumDevirtCalls++;
11761193 auto &CB = VCallSite.CB ;
11771194 assert (!CB.getCalledFunction () && " devirtualizing direct call?" );
11781195 IRBuilder<> Builder (&CB);
0 commit comments