Refactor the flag test of BBF_PROF_WEIGHT
authorBrian Sullivan <briansul@microsoft.com>
Thu, 16 Mar 2017 23:45:17 +0000 (16:45 -0700)
committerBrian Sullivan <briansul@microsoft.com>
Fri, 17 Mar 2017 21:38:26 +0000 (14:38 -0700)
to always call the new method hasProfileWeight()

Commit migrated from https://github.com/dotnet/coreclr/commit/8602b75d969b542e98afd92f56a328c4480bb317

src/coreclr/src/jit/block.h
src/coreclr/src/jit/flowgraph.cpp
src/coreclr/src/jit/importer.cpp
src/coreclr/src/jit/morph.cpp
src/coreclr/src/jit/optimizer.cpp

index 2460979..2f84b9b 100644 (file)
@@ -521,16 +521,31 @@ struct BasicBlock : private LIR::Range
     // getBBWeight -- get the normalized weight of this block
     unsigned getBBWeight(Compiler* comp);
 
-    // setBBWeight -- if the block weight is not derived from a profile, then set the weight to the input
-    // weight, but make sure to not overflow BB_MAX_WEIGHT
+    // hasProfileWeight -- Returns true if this block's weight came from profile data
+    bool hasProfileWeight() const
+    {
+        return ((this->bbFlags & BBF_PROF_WEIGHT) != 0);
+    }
+
+    // setBBWeight -- if the block weight is not derived from a profile,
+    // then set the weight to the input weight, making sure to not overflow BB_MAX_WEIGHT
+    // Note to set the weight from profile data, instead use setBBProfileWeight
     void setBBWeight(unsigned weight)
     {
-        if (!(this->bbFlags & BBF_PROF_WEIGHT))
+        if (!hasProfileWeight())
         {
             this->bbWeight = min(weight, BB_MAX_WEIGHT);
         }
     }
 
+    // setBBProfileWeight -- Set the profile-derived weight for a basic block
+    void setBBProfileWeight(unsigned weight)
+    {
+        this->bbFlags |= BBF_PROF_WEIGHT;
+        // Check if the multiplication by BB_UNITY_WEIGHT will overflow.
+        this->bbWeight = (weight <= BB_MAX_WEIGHT / BB_UNITY_WEIGHT) ? weight * BB_UNITY_WEIGHT : BB_MAX_WEIGHT;
+    }
+
     // modifyBBWeight -- same as setBBWeight, but also make sure that if the block is rarely run, it stays that
     // way, and if it's not rarely run then its weight never drops below 1.
     void modifyBBWeight(unsigned weight)
@@ -541,20 +556,12 @@ struct BasicBlock : private LIR::Range
         }
     }
 
-    // setBBProfileWeight -- Set the profile-derived weight for a basic block
-    void setBBProfileWeight(unsigned weight)
-    {
-        this->bbFlags |= BBF_PROF_WEIGHT;
-        // Check if the multiplication by BB_UNITY_WEIGHT will overflow.
-        this->bbWeight = (weight <= BB_MAX_WEIGHT / BB_UNITY_WEIGHT) ? weight * BB_UNITY_WEIGHT : BB_MAX_WEIGHT;
-    }
-
     // this block will inherit the same weight and relevant bbFlags as bSrc
     void inheritWeight(BasicBlock* bSrc)
     {
         this->bbWeight = bSrc->bbWeight;
 
-        if (bSrc->bbFlags & BBF_PROF_WEIGHT)
+        if (bSrc->hasProfileWeight())
         {
             this->bbFlags |= BBF_PROF_WEIGHT;
         }
index 5e18160..dd99411 100644 (file)
@@ -434,7 +434,7 @@ void Compiler::fgEnsureFirstBBisScratch()
     if (fgFirstBB != nullptr)
     {
         // If we have profile data the new block will inherit fgFirstBlock's weight
-        if (fgFirstBB->bbFlags & BBF_PROF_WEIGHT)
+        if (fgFirstBB->hasProfileWeight())
         {
             block->inheritWeight(fgFirstBB);
         }
@@ -8134,7 +8134,7 @@ void Compiler::fgAddInternal()
                 // If all BBJ_RETURN blocks have a valid profiled weights
                 // then allProfWeight will be true, else it is false
                 //
-                if ((block->bbFlags & BBF_PROF_WEIGHT) == 0)
+                if (!block->hasProfileWeight())
                 {
                     allProfWeight = false;
                 }
@@ -9820,8 +9820,7 @@ void Compiler::fgCompactBlocks(BasicBlock* block, BasicBlock* bNext)
     // or if both block and bNext have non-zero weights
     // then we select the highest weight block.
 
-    if ((block->bbFlags & BBF_PROF_WEIGHT) || (bNext->bbFlags & BBF_PROF_WEIGHT) ||
-        (block->bbWeight && bNext->bbWeight))
+    if (block->hasProfileWeight() || bNext->hasProfileWeight() || (block->bbWeight && bNext->bbWeight))
     {
         // We are keeping block so update its fields
         // when bNext has a greater weight
@@ -11126,7 +11125,7 @@ bool Compiler::fgExpandRarelyRunBlocks()
                     NEW_RARELY_RUN:
                         /* If the weight of the block was obtained from a profile run,
                            than it's more accurate than our static analysis */
-                        if (bPrev->bbFlags & BBF_PROF_WEIGHT)
+                        if (bPrev->hasProfileWeight())
                         {
                             continue;
                         }
@@ -11312,10 +11311,10 @@ bool Compiler::fgExpandRarelyRunBlocks()
         // if bPrev->bbWeight is not based upon profile data we can adjust
         // the weights of bPrev and block
         //
-        else if (bPrev->isBBCallAlwaysPair() &&             // we must have a BBJ_CALLFINALLY and BBK_ALWAYS pair
-                 (bPrev->bbWeight != block->bbWeight) &&    // the weights are currently different
-                 ((bPrev->bbFlags & BBF_PROF_WEIGHT) == 0)) // and the BBJ_CALLFINALLY block is not using profiled
-                                                            // weights
+        else if (bPrev->isBBCallAlwaysPair() &&          // we must have a BBJ_CALLFINALLY and BBK_ALWAYS pair
+                 (bPrev->bbWeight != block->bbWeight) && // the weights are currently different
+                 !bPrev->hasProfileWeight())             // and the BBJ_CALLFINALLY block is not using profiled
+                                                         // weights
         {
             if (block->isRunRarely())
             {
@@ -12594,7 +12593,7 @@ void Compiler::fgComputeEdgeWeights()
 
         for (bDst = fgFirstBB; bDst != nullptr; bDst = bDst->bbNext)
         {
-            if (((bDst->bbFlags & BBF_PROF_WEIGHT) == 0) && (bDst->bbPreds != nullptr))
+            if (!bDst->hasProfileWeight() && (bDst->bbPreds != nullptr))
             {
                 BasicBlock* bOnlyNext;
 
@@ -12621,7 +12620,7 @@ void Compiler::fgComputeEdgeWeights()
                         bOnlyNext = nullptr;
                     }
 
-                    if ((bOnlyNext == bDst) && ((bSrc->bbFlags & BBF_PROF_WEIGHT) != 0))
+                    if ((bOnlyNext == bDst) && bSrc->hasProfileWeight())
                     {
                         // We know the exact weight of bDst
                         newWeight = bSrc->bbWeight;
@@ -12673,8 +12672,7 @@ void Compiler::fgComputeEdgeWeights()
             // Sum up the weights of all of the return blocks and throw blocks
             // This is used when we have a back-edge into block 1
             //
-            if (((bDst->bbFlags & BBF_PROF_WEIGHT) != 0) &&
-                ((bDst->bbJumpKind == BBJ_RETURN) || (bDst->bbJumpKind == BBJ_THROW)))
+            if (bDst->hasProfileWeight() && ((bDst->bbJumpKind == BBJ_RETURN) || (bDst->bbJumpKind == BBJ_THROW)))
             {
                 returnWeight += bDst->bbWeight;
             }
@@ -12742,7 +12740,7 @@ void Compiler::fgComputeEdgeWeights()
             // then we must reset any values that they currently have
             //
 
-            if (((bSrc->bbFlags & BBF_PROF_WEIGHT) == 0) || ((bDst->bbFlags & BBF_PROF_WEIGHT) == 0))
+            if (!bSrc->hasProfileWeight() || !bDst->hasProfileWeight())
             {
                 edge->flEdgeWeightMin = BB_ZERO_WEIGHT;
                 edge->flEdgeWeightMax = BB_MAX_WEIGHT;
@@ -13126,7 +13124,7 @@ bool Compiler::fgOptimizeBranchToEmptyUnconditional(BasicBlock* block, BasicBloc
         // When we optimize a branch to branch we need to update the profile weight
         // of bDest by subtracting out the block/edge weight of the path that is being optimized.
         //
-        if (fgHaveValidEdgeWeights && ((bDest->bbFlags & BBF_PROF_WEIGHT) != 0))
+        if (fgHaveValidEdgeWeights && bDest->hasProfileWeight())
         {
             flowList* edge1 = fgGetPredForBlock(bDest, block);
             noway_assert(edge1 != nullptr);
@@ -13459,7 +13457,7 @@ bool Compiler::fgOptimizeSwitchBranches(BasicBlock* block)
             // When we optimize a branch to branch we need to update the profile weight
             // of bDest by subtracting out the block/edge weight of the path that is being optimized.
             //
-            if (fgIsUsingProfileWeights() && ((bDest->bbFlags & BBF_PROF_WEIGHT) != 0))
+            if (fgIsUsingProfileWeights() && bDest->hasProfileWeight())
             {
                 if (fgHaveValidEdgeWeights)
                 {
@@ -14536,8 +14534,7 @@ void Compiler::fgReorderBlocks()
 
         BasicBlock::weight_t profHotWeight = -1;
 
-        if ((bPrev->bbFlags & BBF_PROF_WEIGHT) && (block->bbFlags & BBF_PROF_WEIGHT) &&
-            ((bDest == nullptr) || (bDest->bbFlags & BBF_PROF_WEIGHT)))
+        if (bPrev->hasProfileWeight() && block->hasProfileWeight() && ((bDest == nullptr) || bDest->hasProfileWeight()))
         {
             //
             // All blocks have profile information
@@ -18945,7 +18942,7 @@ FILE* Compiler::fgOpenFlowGraphFile(bool* wbDontClose, Phases phase, LPCWSTR typ
 
     if (wcscmp(filename, W("profiled")) == 0)
     {
-        if ((fgFirstBB->bbFlags & BBF_PROF_WEIGHT) != 0)
+        if (fgFirstBB->hasProfileWeight())
         {
             createDuplicateFgxFiles = true;
             goto ONE_FILE_PER_METHOD;
@@ -19250,7 +19247,7 @@ bool Compiler::fgDumpFlowGraph(Phases phase)
             {
                 fprintf(fgxFile, "\n            inHandler=\"%s\"", "true");
             }
-            if (((fgFirstBB->bbFlags & BBF_PROF_WEIGHT) != 0) && ((block->bbFlags & BBF_COLD) == 0))
+            if ((fgFirstBB->hasProfileWeight()) && ((block->bbFlags & BBF_COLD) == 0))
             {
                 fprintf(fgxFile, "\n            hot=\"true\"");
             }
@@ -19533,7 +19530,7 @@ void Compiler::fgTableDispBasicBlock(BasicBlock* block, int ibcColWidth /* = 0 *
 
     if (ibcColWidth > 0)
     {
-        if (block->bbFlags & BBF_PROF_WEIGHT)
+        if (block->hasProfileWeight())
         {
             printf("%*u", ibcColWidth, block->bbWeight);
         }
@@ -19787,7 +19784,7 @@ void Compiler::fgDispBasicBlocks(BasicBlock* firstBlock, BasicBlock* lastBlock,
     int ibcColWidth = 0;
     for (block = firstBlock; block != nullptr; block = block->bbNext)
     {
-        if (block->bbFlags & BBF_PROF_WEIGHT)
+        if (block->hasProfileWeight())
         {
             int thisIbcWidth = CountDigits(block->bbWeight);
             ibcColWidth      = max(ibcColWidth, thisIbcWidth);
index 9d1daac..f336ee3 100644 (file)
@@ -16930,7 +16930,7 @@ void Compiler::impMakeDiscretionaryInlineObservations(InlineInfo* pInlineInfo, I
     {
         frequency = InlineCallsiteFrequency::LOOP;
     }
-    else if ((pInlineInfo->iciBlock->bbFlags & BBF_PROF_WEIGHT) && (pInlineInfo->iciBlock->bbWeight > BB_ZERO_WEIGHT))
+    else if (pInlineInfo->iciBlock->hasProfileWeight() && (pInlineInfo->iciBlock->bbWeight > BB_ZERO_WEIGHT))
     {
         frequency = InlineCallsiteFrequency::WARM;
     }
index 077478c..bc80f9c 100644 (file)
@@ -15132,13 +15132,13 @@ bool Compiler::fgFoldConditional(BasicBlock* block)
                 // else if bTaken has valid profile weight and block does not we try to adjust block's weight
                 // We can only adjust the block weights when (the edge block -> bTaken) is the only edge into bTaken
                 //
-                if (block->bbFlags & BBF_PROF_WEIGHT)
+                if (block->hasProfileWeight())
                 {
                     // The edge weights for (block -> bTaken) are 100% of block's weight
                     edgeTaken->flEdgeWeightMin = block->bbWeight;
                     edgeTaken->flEdgeWeightMax = block->bbWeight;
 
-                    if ((bTaken->bbFlags & BBF_PROF_WEIGHT) == 0)
+                    if (!bTaken->hasProfileWeight())
                     {
                         if ((bTaken->countOfInEdges() == 1) || (bTaken->bbWeight < block->bbWeight))
                         {
@@ -15148,7 +15148,7 @@ bool Compiler::fgFoldConditional(BasicBlock* block)
                         }
                     }
                 }
-                else if (bTaken->bbFlags & BBF_PROF_WEIGHT)
+                else if (bTaken->hasProfileWeight())
                 {
                     if (bTaken->countOfInEdges() == 1)
                     {
index 64eae4a..c18ebc5 100644 (file)
@@ -227,7 +227,7 @@ void Compiler::optMarkLoopBlocks(BasicBlock* begBlk, BasicBlock* endBlk, bool ex
 
                 unsigned weight;
 
-                if ((curBlk->bbFlags & BBF_PROF_WEIGHT) != 0)
+                if (curBlk->hasProfileWeight())
                 {
                     // We have real profile weights, so we aren't going to change this blocks weight
                     weight = curBlk->bbWeight;
@@ -370,7 +370,7 @@ void Compiler::optUnmarkLoopBlocks(BasicBlock* begBlk, BasicBlock* endBlk)
             // Don't unmark blocks that are set to BB_MAX_WEIGHT
             // Don't unmark blocks when we are using profile weights
             //
-            if (!curBlk->isMaxBBWeight() && ((curBlk->bbFlags & BBF_PROF_WEIGHT) == 0))
+            if (!curBlk->isMaxBBWeight() && !curBlk->hasProfileWeight())
             {
                 if (!fgDominate(curBlk, endBlk))
                 {
@@ -3527,8 +3527,7 @@ void Compiler::fgOptWhileLoop(BasicBlock* block)
     {
         // Only rely upon the profile weight when all three of these blocks
         // have good profile weights
-        if ((block->bbFlags & BBF_PROF_WEIGHT) && (bTest->bbFlags & BBF_PROF_WEIGHT) &&
-            (block->bbNext->bbFlags & BBF_PROF_WEIGHT))
+        if (block->hasProfileWeight() && bTest->hasProfileWeight() && block->bbNext->hasProfileWeight())
         {
             allProfileWeightsAreValid = true;
 
@@ -6553,9 +6552,8 @@ void Compiler::fgCreateLoopPreHeader(unsigned lnum)
         }
         else
         {
-            bool allValidProfileWeights = ((head->bbFlags & BBF_PROF_WEIGHT) != 0) &&
-                                          ((head->bbJumpDest->bbFlags & BBF_PROF_WEIGHT) != 0) &&
-                                          ((head->bbNext->bbFlags & BBF_PROF_WEIGHT) != 0);
+            bool allValidProfileWeights =
+                (head->hasProfileWeight() && head->bbJumpDest->hasProfileWeight() && head->bbNext->hasProfileWeight());
 
             if (allValidProfileWeights)
             {