diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -332,9 +332,6 @@ /// Returns false if no metadata was found. bool extractProfTotalWeight(uint64_t &TotalVal) const; - /// Sets the branch_weights metadata to \p W for CallInst. - void setProfWeight(uint64_t W); - /// Set the debug location information for this instruction. void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); } diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp --- a/llvm/lib/IR/Instruction.cpp +++ b/llvm/lib/IR/Instruction.cpp @@ -774,12 +774,3 @@ New->copyMetadata(*this); return New; } - -void Instruction::setProfWeight(uint64_t W) { - assert(isa(this) && - "Can only set weights for call like instructions"); - SmallVector Weights; - Weights.push_back(W); - MDBuilder MDB(getContext()); - setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); -} diff --git a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp --- a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp +++ b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp @@ -336,10 +336,7 @@ NewCS->setAttributes( AttributeList::get(F->getContext(), CallPAL.getFnAttributes(), CallPAL.getRetAttributes(), ArgAttrVec)); - NewCS->setDebugLoc(CB.getDebugLoc()); - uint64_t W; - if (CB.extractProfTotalWeight(W)) - NewCS->setProfWeight(W); + NewCS->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); Args.clear(); ArgAttrVec.clear(); diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -1568,11 +1568,8 @@ } // Copy over various properties and the new attributes. - uint64_t W; - if (OldCB->extractProfTotalWeight(W)) - NewCB->setProfWeight(W); + NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); NewCB->setCallingConv(OldCB->getCallingConv()); - NewCB->setDebugLoc(OldCB->getDebugLoc()); NewCB->takeName(OldCB); NewCB->setAttributes(AttributeList::get( Ctx, OldCallAttributeList.getFnAttributes(), diff --git a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp --- a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp +++ b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp @@ -205,10 +205,7 @@ } NewCB->setCallingConv(CB->getCallingConv()); NewCB->setAttributes(PAL); - NewCB->setDebugLoc(CB->getDebugLoc()); - uint64_t W; - if (CB->extractProfTotalWeight(W)) - NewCB->setProfWeight(W); + NewCB->copyMetadata(*CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); Args.clear(); @@ -936,10 +933,7 @@ } NewCB->setCallingConv(CB.getCallingConv()); NewCB->setAttributes(NewCallPAL); - NewCB->setDebugLoc(CB.getDebugLoc()); - uint64_t W; - if (CB.extractProfTotalWeight(W)) - NewCB->setProfWeight(W); + NewCB->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg}); Args.clear(); ArgAttrVec.clear(); diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -4955,11 +4955,8 @@ NewCall->setCallingConv(Call.getCallingConv()); NewCall->setAttributes(NewCallerPAL); - // Preserve the weight metadata for the new call instruction. The metadata - // is used by SamplePGO to check callsite's hotness. - uint64_t W; - if (Caller->extractProfTotalWeight(W)) - NewCall->setProfWeight(W); + // Preserve prof metadata if any. + NewCall->copyMetadata(*Caller, {LLVMContext::MD_prof}); // Insert a cast of the return type as necessary. Instruction *NC = NewCall; diff --git a/llvm/test/Transforms/InstCombine/cast-call-combine-prof.ll b/llvm/test/Transforms/InstCombine/cast-call-combine-prof.ll --- a/llvm/test/Transforms/InstCombine/cast-call-combine-prof.ll +++ b/llvm/test/Transforms/InstCombine/cast-call-combine-prof.ll @@ -32,7 +32,7 @@ unreachable } -; CHECK: ![[$PROF]] = !{!"branch_weights", i32 2000} +; CHECK: ![[$PROF]] = !{!"VP", i32 0, i64 2000, i64 -3913987384944532146, i64 2000} !0 = !{!"VP", i32 0, i64 2000, i64 -3913987384944532146, i64 2000} !llvm.module.flags = !{!1}