Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -178,8 +178,13 @@ PM.add(createAddDiscriminatorsPass()); } -static void addInstructionCombiningPass(const PassManagerBuilder &Builder, - legacy::PassManagerBase &PM) { +static void addInstructionCombiningPassBeforeSampleProfiler( + const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) { + // instcombine is needed before sample profile annotation because it converts + // certain function calls to be inlinable. simplifycfg and sroa is needed + // before instcombine for necessary preparation. + PM.add(createCFGSimplificationPass()); + PM.add(createSROAPass()); PM.add(createInstructionCombiningPass()); } @@ -491,7 +496,7 @@ MPM->add(createPruneEHPass()); MPM->add(createSampleProfileLoaderPass(CodeGenOpts.SampleProfileFile)); PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, - addInstructionCombiningPass); + addInstructionCombiningPassBeforeSampleProfiler); } PMBuilder.populateFunctionPassManager(*FPM); Index: test/CodeGen/pgo-sample.c =================================================================== --- test/CodeGen/pgo-sample.c +++ test/CodeGen/pgo-sample.c @@ -2,5 +2,8 @@ // // Ensure Pass PGOInstrumentationGenPass is invoked. // RUN: %clang_cc1 -O2 -fprofile-sample-use=%S/Inputs/pgo-sample.prof %s -mllvm -debug-pass=Structure -emit-llvm -o - 2>&1 | FileCheck %s +// CHECK: Simplify the CFG +// CHECK: SROA +// CHECK: Combine redundant instructions // CHECK: Remove unused exception handling info // CHECK: Sample profile pass