fix shufflevector to match latest LLVM changes
MODIFY
ir/instr.cpp
ir/instr.cpp
@@ -2426,53 +2426,35 @@ unique_ptr<Instr> InsertElement::dup(const string &suffix) const {
vector<Value*> ShuffleVector::operands() const {
- return { v1, v2, mask };
+ return { v1, v2 };
}
void ShuffleVector::rauw(const Value &what, Value &with) {
RAUW(v1);
RAUW(v2);
- RAUW(mask);
}
void ShuffleVector::print(ostream &os) const {
- os << getName() << " = shufflevector " << *v1 << ", " << *v2 << ", " << *mask;
+ os << getName() << " = shufflevector " << *v1 << ", " << *v2;
+ for (auto m : mask)
+ os << ", " << m;
}
StateValue ShuffleVector::toSMT(State &s) const {
- auto vty = static_cast<const VectorType*>(v1->getType().getAsAggregateType());
- expr sz = expr::mkUInt(vty->numElementsConst(), 32);
- auto mty = mask->getType().getAsAggregateType();
+ auto vty = v1->getType().getAsAggregateType();
+ auto sz = vty->numElementsConst();
vector<StateValue> vals;
- if (dynamic_cast<UndefValue*>(mask))
- return UndefValue(getType()).toSMT(s);
-
auto &vect1 = s[*v1];
auto &vect2 = s[*v2];
- auto &mask_vector = static_cast<AggregateValue*>(mask)->getVals();
- for (unsigned i = 0, e = mty->numElementsConst(); i != e; ++i) {
- auto mask = mask_vector[i];
- // mask must be either a constant or undef
- // special case undef to yield undef
- if (dynamic_cast<UndefValue*>(mask)) {
- vals.emplace_back(UndefValue(vty->getChild(0)).toSMT(s));
- continue;
+ for (auto m : mask) {
+ if (m >= 2 * sz) {
+ vals.emplace_back(UndefValue(vty->getChild(0)).toSMT(s).value, true);
+ } else {
+ vals.emplace_back(m < sz ? vty->extract(vect1, m)
+ : vty->extract(vect2, m - sz));
}
-
- auto &[iv, ip] = s[*mask];
- assert(ip.isTrue());
- auto [lv, lp] = vty->extract(vect1, iv);
- auto [rv, rp] = vty->extract(vect2, iv - sz);
-
- expr val = expr::mkIf(iv.uge(sz), rv, lv);
- expr inbounds = iv.ult(sz + sz);
- if (!inbounds.isTrue())
- val = expr::mkIf(inbounds, val,
- UndefValue(vty->getChild(0)).toSMT(s).value);
-
- vals.emplace_back(move(val), !inbounds || expr::mkIf(iv.uge(sz), rp, lp));
}
return getType().getAsAggregateType()->aggregateVals(vals);
@@ -2481,17 +2463,14 @@ StateValue ShuffleVector::toSMT(State &s) const {
expr ShuffleVector::getTypeConstraints(const Function &f) const {
return Value::getTypeConstraints() &&
getType().enforceVectorTypeSameChildTy(v1->getType()) &&
- getType().enforceVectorTypeEquiv(mask->getType()) &&
+ getType().getAsAggregateType()->numElements() == mask.size() &&
v1->getType().enforceVectorType() &&
- v1->getType() == v2->getType() &&
- // mask is a vector of i32
- mask->getType().enforceVectorType([](auto &ty)
- { return ty.enforceIntType(32); });
+ v1->getType() == v2->getType();
}
unique_ptr<Instr> ShuffleVector::dup(const string &suffix) const {
return make_unique<ShuffleVector>(getType(), getName() + suffix,
- *v1, *v2, *mask);
+ *v1, *v2, mask);
}
}

MODIFY
ir/instr.h
ir/instr.h
@@ -616,10 +616,12 @@ public:
class ShuffleVector final : public Instr {
- Value *v1, *v2, *mask;
+ Value *v1, *v2;
+ std::vector<unsigned> mask;
public:
- ShuffleVector(Type &type, std::string &&name, Value &v1, Value &v2, Value &m)
- : Instr(type, move(name)), v1(&v1), v2(&v2), mask(&m) {}
+ ShuffleVector(Type &type, std::string &&name, Value &v1, Value &v2,
+ std::vector<unsigned> mask)
+ : Instr(type, move(name)), v1(&v1), v2(&v2), mask(std::move(mask)) {}
std::vector<Value*> operands() const override;
void rauw(const Value &what, Value &with) override;
void print(std::ostream &os) const override;

MODIFY
llvm_util/llvm2alive.cpp
llvm_util/llvm2alive.cpp
@@ -670,9 +670,12 @@ public:
}
RetTy visitShuffleVectorInst(llvm::ShuffleVectorInst &i) {
- PARSE_TRIOP();
+ PARSE_BINOP();
+ vector<unsigned> mask;
+ for (auto m : i.getShuffleMask())
+ mask.push_back(m);
RETURN_IDENTIFIER(make_unique<ShuffleVector>(*ty, value_name(i), *a, *b,
- *c));
+ move(mask)));
}
RetTy visitDbgInfoIntrinsic(llvm::DbgInfoIntrinsic&) { return {}; }

MODIFY
tests/unit/vector/shufflevector1.opt
tests/unit/vector/shufflevector1.opt
Name: constant-folding-1
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <1 x i32> <i32 0>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 0
=>
%r = <1 x i4> <i4 3>
Name: constant-folding-2
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <1 x i32> <i32 1>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 1
=>
%r = <1 x i4> <i4 5>
Name: constant-folding-3
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <1 x i32> <i32 2>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 2
=>
%r = <1 x i4> <i4 4>
Name: constant-folding-4
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <1 x i32> <i32 3>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 3
=>
%r = <1 x i4> <i4 6>
Name: constant-folding-5
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <2 x i32> <i32 3, i32 2>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 3, 2
=>
%r = <2 x i4> <i4 6, i4 4>
Name: constant-folding-6
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <3 x i32> <i32 3, i32 2, i32 1>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 3, 2, 1
=>
%r = <3 x i4> <i4 6, i4 4, i4 5>
Name: constant-folding-7
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 3, 2, 1, 0
=>
%r = <4 x i4> <i4 6, i4 4, i4 5, i4 3>
Name: constant-folding-8
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, 3, 2, 1, 0
=>
%r = <4 x i4> <i4 6, i4 4, i4 5, i4 3>
Name: constant-folding-9
-%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, <3 x i32> <i32 undef, i32 1, i32 4>
+%r = shufflevector <2 x i4> <i4 3, i4 5>, <2 x i4> <i4 4, i4 6>, -1, 1, 4
=>
%r = <3 x i4> <i4 undef, i4 5, i4 undef>
Name: constant-folding-10
-%r = shufflevector <2 x float> <float 3.0, float 5.0>, <2 x float> <float 4.0, float 6.0>, <3 x i32> <i32 undef, i32 1, i32 4>
+%r = shufflevector <2 x float> <float 3.0, float 5.0>, <2 x float> <float 4.0, float 6.0>, -1, 1, 4
=>
%r = <3 x float> <float undef, float 5.0, float undef>

MODIFY
tools/alive_parser.cpp
tools/alive_parser.cpp
@@ -967,10 +967,13 @@ static unique_ptr<Instr> parse_shufflevector(string_view name) {
parse_comma();
auto &ty_b = parse_type();
auto &b = parse_operand(ty_b);
- parse_comma();
- auto &ty_m = parse_vector_type();
- auto &m = parse_operand(ty_m);
- return make_unique<ShuffleVector>(get_sym_type(), string(name), a, b, m);
+
+ vector<unsigned> mask;
+ while (tokenizer.consumeIf(COMMA)) {
+ mask.push_back((unsigned)parse_number());
+ }
+ return make_unique<ShuffleVector>(get_sym_type(), string(name), a, b,
+ move(mask));
}
static unique_ptr<Instr> parse_copyop(string_view name, token t) {

fix shufflevector to match latest LLVM changes