@@ -40,6 +40,9 @@ namespace {
4040//  We maintain some constants here to ensure that we access the branch weights
4141//  correctly, and can change the behavior in the future if the layout changes
4242
43+ //  The index at which the weights vector starts
44+ constexpr  unsigned  WeightsIdx = 1 ;
45+ 
4346//  the minimum number of operands for MD_prof nodes with branch weights
4447constexpr  unsigned  MinBWOps = 3 ;
4548
@@ -72,16 +75,15 @@ static void extractFromBranchWeightMD(const MDNode *ProfileData,
7275  assert (isBranchWeightMD (ProfileData) && " wrong metadata" 
7376
7477  unsigned  NOps = ProfileData->getNumOperands ();
75-   unsigned  WeightsIdx = getBranchWeightOffset (ProfileData);
7678  assert (WeightsIdx < NOps && " Weights Index must be less than NOps." 
7779  Weights.resize (NOps - WeightsIdx);
7880
7981  for  (unsigned  Idx = WeightsIdx, E = NOps; Idx != E; ++Idx) {
8082    ConstantInt *Weight =
8183        mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand (Idx));
8284    assert (Weight && " Malformed branch_weight in MD_prof node" 
83-     assert (Weight->getValue ().getActiveBits () <= ( sizeof (T) *  8 )  &&
84-            " Too many bits for MD_prof branch_weight " 
85+     assert (Weight->getValue ().getActiveBits () <= 32  &&
86+            " Too many bits for uint32_t " 
8587    Weights[Idx - WeightsIdx] = Weight->getZExtValue ();
8688  }
8789}
@@ -121,26 +123,6 @@ bool hasValidBranchWeightMD(const Instruction &I) {
121123  return  getValidBranchWeightMDNode (I);
122124}
123125
124- bool  hasBranchWeightOrigin (const  Instruction &I) {
125-   auto  *ProfileData = I.getMetadata (LLVMContext::MD_prof);
126-   return  hasBranchWeightOrigin (ProfileData);
127- }
128- 
129- bool  hasBranchWeightOrigin (const  MDNode *ProfileData) {
130-   if  (!isBranchWeightMD (ProfileData))
131-     return  false ;
132-   auto  *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand (1 ));
133-   //  NOTE: if we ever have more types of branch weight provenance,
134-   //  we need to check the string value is "expected". For now, we
135-   //  supply a more generic API, and avoid the spurious comparisons.
136-   assert (ProfDataName == nullptr  || ProfDataName->getString () == " expected" 
137-   return  ProfDataName != nullptr ;
138- }
139- 
140- unsigned  getBranchWeightOffset (const  MDNode *ProfileData) {
141-   return  hasBranchWeightOrigin (ProfileData) ? 2  : 1 ;
142- }
143- 
144126MDNode *getBranchWeightMDNode (const  Instruction &I) {
145127  auto  *ProfileData = I.getMetadata (LLVMContext::MD_prof);
146128  if  (!isBranchWeightMD (ProfileData))
@@ -150,9 +132,7 @@ MDNode *getBranchWeightMDNode(const Instruction &I) {
150132
151133MDNode *getValidBranchWeightMDNode (const  Instruction &I) {
152134  auto  *ProfileData = getBranchWeightMDNode (I);
153-   auto  Offset = getBranchWeightOffset (ProfileData);
154-   if  (ProfileData &&
155-       ProfileData->getNumOperands () == Offset + I.getNumSuccessors ())
135+   if  (ProfileData && ProfileData->getNumOperands () == 1  + I.getNumSuccessors ())
156136    return  ProfileData;
157137  return  nullptr ;
158138}
@@ -211,8 +191,7 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
211191    return  false ;
212192
213193  if  (ProfDataName->getString () == " branch_weights" 
214-     unsigned  Offset = getBranchWeightOffset (ProfileData);
215-     for  (unsigned  Idx = Offset; Idx < ProfileData->getNumOperands (); ++Idx) {
194+     for  (unsigned  Idx = 1 ; Idx < ProfileData->getNumOperands (); Idx++) {
216195      auto  *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand (Idx));
217196      assert (V && " Malformed branch_weight in MD_prof node" 
218197      TotalVal += V->getValue ().getZExtValue ();
@@ -233,10 +212,9 @@ bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalVal) {
233212  return  extractProfTotalWeight (I.getMetadata (LLVMContext::MD_prof), TotalVal);
234213}
235214
236- void  setBranchWeights (Instruction &I, ArrayRef<uint32_t > Weights,
237-                       bool  IsExpected) {
215+ void  setBranchWeights (Instruction &I, ArrayRef<uint32_t > Weights) {
238216  MDBuilder MDB (I.getContext ());
239-   MDNode *BranchWeights = MDB.createBranchWeights (Weights, IsExpected );
217+   MDNode *BranchWeights = MDB.createBranchWeights (Weights);
240218  I.setMetadata (LLVMContext::MD_prof, BranchWeights);
241219}
242220
0 commit comments