Index: lib/IR/ModuleSummaryIndex.cpp =================================================================== --- lib/IR/ModuleSummaryIndex.cpp +++ lib/IR/ModuleSummaryIndex.cpp @@ -14,11 +14,17 @@ #include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/ADT/SCCIterator.h" +#include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; +#define DEBUG_TYPE "module-summary-index" + +STATISTIC(ReadOnlyLiveGVars, + "Number of live global variables marked read only"); + FunctionSummary FunctionSummary::ExternalNode = FunctionSummary::makeDummyFunctionSummary({}); bool ValueInfo::isDSOLocal() const { @@ -160,6 +166,14 @@ GVS->setReadOnly(false); propagateConstantsToRefs(S.get()); } +#if LLVM_ENABLE_STATS + for (auto &P : *this) + if (P.second.SummaryList.size()) + if (auto *GVS = dyn_cast( + P.second.SummaryList[0]->getBaseObject())) + if (isGlobalValueLive(GVS) && GVS->isReadOnly()) + ReadOnlyLiveGVars++; +#endif } // TODO: write a graphviz dumper for SCCs (see ModuleSummaryIndex::exportToDot) Index: test/ThinLTO/X86/index-const-prop.ll =================================================================== --- test/ThinLTO/X86/index-const-prop.ll +++ test/ThinLTO/X86/index-const-prop.ll @@ -3,13 +3,18 @@ ; 2. Make a local copy of internal definition if all accesses to it are readonly. This allows constant ; folding it during optimziation phase. +; -stats requires asserts +; REQUIRES: asserts + ; RUN: opt -module-summary %s -o %t1.bc ; RUN: opt -module-summary %p/Inputs/index-const-prop.ll -o %t2.bc ; RUN: llvm-lto -thinlto-action=thinlink -o %t3.index.bc %t1.bc %t2.bc -; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t1.bc -thinlto-index=%t3.index.bc -o %t1.imported.bc +; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t1.bc -thinlto-index=%t3.index.bc -o %t1.imported.bc -stats 2>&1 | FileCheck %s --check-prefix=STATS ; RUN: llvm-dis %t1.imported.bc -o - | FileCheck %s --check-prefix=IMPORT ; RUN: llvm-lto -thinlto-action=optimize %t1.imported.bc -o - | llvm-dis - -o - | FileCheck %s --check-prefix=OPTIMIZE +; STATS: 2 module-summary-index - Number of live global variables marked read only + ; Check that we don't internalize gBar when it is exported ; RUN: llvm-lto -thinlto-action=import -exported-symbol main -exported-symbol gBar %t1.bc -thinlto-index=%t3.index.bc -o %t1.imported2.bc ; RUN: llvm-dis %t1.imported2.bc -o - | FileCheck %s --check-prefix=IMPORT2 @@ -28,6 +33,10 @@ @gBar = external global i32 +; Should not be counted in the stats of live read only variables since it is +; dead and will be dropped anyway. +@gDead = internal unnamed_addr global i32 1, align 4 + define i32 @main() local_unnamed_addr { %call = tail call i32 bitcast (i32 (...)* @foo to i32 ()*)() %call1 = tail call i32 bitcast (i32 (...)* @bar to i32 ()*)()