diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -3454,13 +3454,19 @@
     CurrentBlockOrder.push_back(&MBB);
   }
 
+  bool OptForSize = F->getFunction().hasOptSize() ||
+                    llvm::shouldOptimizeForSize(F, PSI, &MBFI->getMBFI());
+
   auto BlockSizes = std::vector<uint64_t>(F->size());
   auto BlockCounts = std::vector<uint64_t>(F->size());
   DenseMap<std::pair<uint64_t, uint64_t>, uint64_t> JumpCounts;
   for (MachineBasicBlock &MBB : *F) {
     // Getting the block frequency.
     BlockFrequency BlockFreq = MBFI->getBlockFreq(&MBB);
-    BlockCounts[BlockIndex[&MBB]] = BlockFreq.getFrequency();
+    if (!OptForSize)
+      BlockCounts[BlockIndex[&MBB]] = BlockFreq.getFrequency();
+    else
+      BlockCounts[BlockIndex[&MBB]] = MBB.pred_size();
     // Getting the block size:
     // - approximate the size of an instruction by 4 bytes, and
     // - ignore debug instructions.
@@ -3476,7 +3482,10 @@
       auto EP = MBPI->getEdgeProbability(&MBB, Succ);
       BlockFrequency EdgeFreq = BlockFreq * EP;
       auto Edge = std::make_pair(BlockIndex[&MBB], BlockIndex[Succ]);
-      JumpCounts[Edge] = EdgeFreq.getFrequency();
+      if (!OptForSize)
+        JumpCounts[Edge] = EdgeFreq.getFrequency();
+      else
+        JumpCounts[Edge] = MBB.succ_size() == 1 ? 100 : 1;
     }
   }