[PM] Use a SmallVector instead of std::vector to avoid heap allocations
authorChandler Carruth <chandlerc@gmail.com>
Sat, 24 Jan 2015 10:47:13 +0000 (10:47 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sat, 24 Jan 2015 10:47:13 +0000 (10:47 +0000)
for small switches, and avoid using a complex loop to set up the
weights.

We know what the baseline weights will be so we can just resize the
vector to contain all that value and clobber the one slot that is
likely. This seems much more direct than the previous code that tested
at every iteration, and started off by zeroing the vector.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226995 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LowerExpectIntrinsic.cpp

index ac3c9f4ac6828885e68aab8bf44f5d7f4bfa92ed..aa991faafea48c141b0109b6c27fb4dc7937d911 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Scalar.h"
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Scalar.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/Constants.h"
@@ -24,7 +25,6 @@
 #include "llvm/Pass.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
-#include <vector>
 
 using namespace llvm;
 
 
 using namespace llvm;
 
@@ -73,13 +73,12 @@ static bool handleSwitchExpect(SwitchInst &SI) {
 
   SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue);
   unsigned n = SI.getNumCases(); // +1 for default case.
 
   SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue);
   unsigned n = SI.getNumCases(); // +1 for default case.
-  std::vector<uint32_t> Weights(n + 1);
+  SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
 
 
-  Weights[0] =
-      Case == SI.case_default() ? LikelyBranchWeight : UnlikelyBranchWeight;
-  for (unsigned i = 0; i != n; ++i)
-    Weights[i + 1] =
-        i == Case.getCaseIndex() ? LikelyBranchWeight : UnlikelyBranchWeight;
+  if (Case == SI.case_default())
+    Weights[0] = LikelyBranchWeight;
+  else
+    Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
 
   SI.setMetadata(LLVMContext::MD_prof,
                  MDBuilder(CI->getContext()).createBranchWeights(Weights));
 
   SI.setMetadata(LLVMContext::MD_prof,
                  MDBuilder(CI->getContext()).createBranchWeights(Weights));