[PATCH] Constfold insertelement to undef when index is out-of-bounds

David Majnemer david.majnemer at gmail.com
Mon Apr 27 01:08:41 PDT 2015


REPOSITORY
  rL LLVM

================
Comment at: lib/IR/ConstantFold.cpp:812
@@ +811,3 @@
+    return UndefValue::get(Val->getType());
+
+  SmallVector<Constant*, 16> Result{NumElts};
----------------
It might be nicer to do something like:
  uint64_t Idx = CIdx->getZExtValue();

After the above comparison, we know that `getZExtValue` will succeed.  This lets us simplify the loop below a little (i.e. `Idx == i` vs `CIdx->equalsInt(i)`)

================
Comment at: lib/IR/ConstantFold.cpp:813
@@ +812,3 @@
+
+  SmallVector<Constant*, 16> Result{NumElts};
+  auto *Ty = Type::getInt32Ty(Val->getContext());
----------------
No need to initialize the SmallVector.  Please use `Result.reserve(NumElts);` instead.

================
Comment at: lib/IR/ConstantFold.cpp:816-817
@@ -818,1 +815,4 @@
+  for (unsigned i = 0; i != NumElts; ++i) {
+    Result[i] = CIdx->equalsInt(i) ? Elt :
+                ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
   }
----------------
This will need to switch to using `push_back` once you use `reserve`.

http://reviews.llvm.org/D9287

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the llvm-commits mailing list