aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/Constants.cpp
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2019-10-29 07:41:41 -0400
committerSanjay Patel <spatel@rotateright.com>2019-10-29 08:52:10 -0400
commita1e8ad4f2fa79eabd484856a47a56c5c01259051 (patch)
tree7c46a5322912724cd51f1c7573a593b5e51407fc /llvm/lib/IR/Constants.cpp
parent4394b5bee615a8c0d1703261204a5bd53d0d54ce (diff)
downloadllvm-a1e8ad4f2fa79eabd484856a47a56c5c01259051.zip
llvm-a1e8ad4f2fa79eabd484856a47a56c5c01259051.tar.gz
llvm-a1e8ad4f2fa79eabd484856a47a56c5c01259051.tar.bz2
[IR] move helper function to replace undef constant (elements) with fixed constants
This is the NFC part of D69519. We had this functionality locally in instcombine, but it can be used elsewhere, so hoisting it to Constant class.
Diffstat (limited to 'llvm/lib/IR/Constants.cpp')
-rw-r--r--llvm/lib/IR/Constants.cpp29
1 files changed, 25 insertions, 4 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index f792f01..ab4e478 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -31,6 +31,7 @@
#include <algorithm>
using namespace llvm;
+using namespace PatternMatch;
//===----------------------------------------------------------------------===//
// Constant Class
@@ -259,10 +260,9 @@ bool Constant::isElementWiseEqual(Value *Y) const {
auto *Cy = dyn_cast<Constant>(Y);
if (!Cy)
return false;
- return PatternMatch::match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ,
- const_cast<Constant *>(this),
- Cy),
- PatternMatch::m_One());
+ return match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ,
+ const_cast<Constant *>(this), Cy),
+ m_One());
}
bool Constant::containsUndefElement() const {
@@ -595,6 +595,27 @@ void Constant::removeDeadConstantUsers() const {
}
}
+Constant *Constant::replaceUndefsWith(Constant *C, Constant *Replacement) {
+ Type *Ty = C->getType();
+ if (C && match(C, m_Undef())) {
+ assert(Ty == Replacement->getType() && "Expected matching types");
+ return Replacement;
+ }
+
+ // Don't know how to deal with this constant.
+ if (!Ty->isVectorTy())
+ return C;
+
+ unsigned NumElts = Ty->getVectorNumElements();
+ SmallVector<Constant *, 32> NewC(NumElts);
+ for (unsigned i = 0; i != NumElts; ++i) {
+ Constant *EltC = C->getAggregateElement(i);
+ assert(EltC->getType() == Replacement->getType() &&
+ "Expected matching types");
+ NewC[i] = EltC && match(EltC, m_Undef()) ? Replacement : EltC;
+ }
+ return ConstantVector::get(NewC);
+}
//===----------------------------------------------------------------------===//