diff --git a/ir/type.cpp b/ir/type.cpp index f6d7841ee..8ecb160eb 100644 --- a/ir/type.cpp +++ b/ir/type.cpp @@ -824,6 +824,18 @@ unsigned AggregateType::countPaddings(unsigned to_idx) const { return count; } +unsigned AggregateType::paddedIndex(unsigned idx) const { + unsigned seen = 0; + for (unsigned i = 0; i < elements; ++i) { + if (is_padding[i]) + continue; + if (seen == idx) + return i; + ++seen; + } + UNREACHABLE(); +} + expr AggregateType::numElementsExcludingPadding() const { auto elems = numElements(); return numElements() - expr::mkInt(numPaddingsConst(), elems); diff --git a/ir/type.h b/ir/type.h index 46eb9994f..cab94a357 100644 --- a/ir/type.h +++ b/ir/type.h @@ -292,6 +292,8 @@ class AggregateType : public Type { Type& getChild(unsigned index) const { return *children[index]; } bool isPadding(unsigned i) const { return is_padding[i]; } unsigned countPaddings(unsigned to_idx) const; + // Maps an index that ignores paddings to the index in children. + unsigned paddedIndex(unsigned idx) const; unsigned bits() const override; unsigned np_bits(bool fromInt) const override; diff --git a/llvm_util/llvm2alive.cpp b/llvm_util/llvm2alive.cpp index 8743df05c..fd3c44f1e 100644 --- a/llvm_util/llvm2alive.cpp +++ b/llvm_util/llvm2alive.cpp @@ -566,7 +566,7 @@ class llvm2alive_ : public llvm::InstVisitor> { auto *valty = &val->getType(); for (auto idx : i.indices()) { auto *aty = valty->getAsAggregateType(); - unsigned idx_with_paddings = aty->countPaddings(idx) + idx; + unsigned idx_with_paddings = aty->paddedIndex(idx); inst->addIdx(idx_with_paddings); valty = &aty->getChild(idx_with_paddings); } @@ -585,7 +585,7 @@ class llvm2alive_ : public llvm::InstVisitor> { for (auto idx : i.indices()) { auto *aty = ty->getAsAggregateType(); - unsigned idx_with_paddings = aty->countPaddings(idx) + idx; + unsigned idx_with_paddings = aty->paddedIndex(idx); inst->addIdx(idx_with_paddings); ty = &aty->getChild(idx_with_paddings); } diff --git a/tests/alive-tv/extractvalue-padding2.srctgt.ll b/tests/alive-tv/extractvalue-padding2.srctgt.ll new file mode 100644 index 000000000..b41660028 --- /dev/null +++ b/tests/alive-tv/extractvalue-padding2.srctgt.ll @@ -0,0 +1,9 @@ +define i32 @src({ i8, i16, i8, i32 } %x) { + %a = insertvalue { i8, i16, i8, i32 } %x, i32 7, 3 + %b = extractvalue { i8, i16, i8, i32 } %a, 3 + ret i32 %b +} + +define i32 @tgt({ i8, i16, i8, i32 } %x) { + ret i32 7 +} diff --git a/tests/alive-tv/extractvalue-padding3.srctgt.ll b/tests/alive-tv/extractvalue-padding3.srctgt.ll new file mode 100644 index 000000000..d8df8a84e --- /dev/null +++ b/tests/alive-tv/extractvalue-padding3.srctgt.ll @@ -0,0 +1,8 @@ +define i24 @src() { + %a = extractvalue [5 x i24] [i24 0, i24 1, i24 2, i24 3, i24 4], 4 + ret i24 %a +} + +define i24 @tgt() { + ret i24 4 +} diff --git a/tests/alive-tv/insertvalue-padding2.ident.ll b/tests/alive-tv/insertvalue-padding2.ident.ll new file mode 100644 index 000000000..42a7afe17 --- /dev/null +++ b/tests/alive-tv/insertvalue-padding2.ident.ll @@ -0,0 +1,4 @@ +define { i8, i16, i8, i32 } @f() { + %a = insertvalue { i8, i16, i8, i32 } poison, i32 1, 3 + ret { i8, i16, i8, i32 } %a +}