From 5a12f236c655b266966cb97727739d493df3d6c4 Mon Sep 17 00:00:00 2001 From: Easwaran Raman Date: Tue, 14 Feb 2017 22:49:28 +0000 Subject: Fix a bug in caller's BFI update code after inlining. Multiple blocks in the callee can be mapped to a single cloned block since we prune the callee as we clone it. The existing code iterates over the value map and clones the block frequency (and eventually scales the frequencies of the cloned blocks). Value map's iteration is not deterministic and so the cloned block might get the frequency of any of the original blocks. The fix is to set the max of the original frequencies to the cloned block. The first block in the sequence must have this max frequency and, in the call context, subsequent blocks must have its frequency. Differential Revision: https://reviews.llvm.org/D29696 llvm-svn: 295115 --- llvm/lib/Transforms/Utils/InlineFunction.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'llvm/lib/Transforms/Utils/InlineFunction.cpp') diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index 86f40c32dc0..0cf5145035f 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -1411,9 +1411,16 @@ static void updateCallerBFI(BasicBlock *CallSiteBlock, continue; auto *OrigBB = cast(Entry.first); auto *ClonedBB = cast(Entry.second); - ClonedBBs.insert(ClonedBB); - CallerBFI->setBlockFreq(ClonedBB, - CalleeBFI->getBlockFreq(OrigBB).getFrequency()); + uint64_t Freq = CalleeBFI->getBlockFreq(OrigBB).getFrequency(); + if (!ClonedBBs.insert(ClonedBB).second) { + // Multiple blocks in the callee might get mapped to one cloned block in + // the caller since we prune the callee as we clone it. When that happens, + // we want to use the maximum among the original blocks' frequencies. + uint64_t NewFreq = CallerBFI->getBlockFreq(ClonedBB).getFrequency(); + if (NewFreq > Freq) + Freq = NewFreq; + } + CallerBFI->setBlockFreq(ClonedBB, Freq); } BasicBlock *EntryClone = cast(VMap.lookup(&CalleeEntryBlock)); CallerBFI->setBlockFreqAndScale( -- cgit v1.2.3