Skip to content

Commit bd75b4f

Browse files
yjxxinyangjiaxin
andauthored
add KMOW (#561)
* add KMOW * add KMOVW * bug fix * bug fix Co-authored-by: yangjiaxin <[email protected]>
1 parent dd14a17 commit bd75b4f

File tree

7 files changed

+104
-2
lines changed

7 files changed

+104
-2
lines changed

generated/Arch/X86/SaveState.S

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,14 @@ movdqu [RIP + STATE_PTR + 1872], XMM29
235235
movdqu [RIP + STATE_PTR + 1936], XMM30
236236
movdqu [RIP + STATE_PTR + 2000], XMM31
237237
#endif // HAS_FEATURE_AVX512
238+
#if HAS_FEATURE_AVX512
239+
kmovq [RIP + STATE_PTR + 3512], K0
240+
kmovq [RIP + STATE_PTR + 3528], K1
241+
kmovq [RIP + STATE_PTR + 3544], K2
242+
kmovq [RIP + STATE_PTR + 3560], K3
243+
kmovq [RIP + STATE_PTR + 3576], K4
244+
kmovq [RIP + STATE_PTR + 3592], K5
245+
kmovq [RIP + STATE_PTR + 3608], K6
246+
kmovq [RIP + STATE_PTR + 3624], K7
247+
248+
#endif // HAS_FEATURE_AVX512

include/remill/Arch/X86/Runtime/State.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,13 @@ struct alignas(8) MMX final {
754754
} __attribute__((packed)) elems[8];
755755
};
756756

757+
struct alignas(8) K_REG final {
758+
struct alignas(8) {
759+
uint64_t _0;
760+
uint64_t val;
761+
} __attribute__((packed)) elems[8];
762+
};
763+
757764
static_assert(128 == sizeof(MMX), "Invalid structure packing of `MMX`.");
758765

759766
enum : size_t { kNumVecRegisters = 32 };
@@ -779,9 +786,10 @@ struct alignas(16) X86State : public ArchState {
779786
XCR0 xcr0; // 8 bytes.
780787
FPU x87; // 512 bytes
781788
SegmentCaches seg_caches; // 96 bytes
789+
K_REG k_reg; // 128 bytes.
782790
} __attribute__((packed));
783791

784-
static_assert((96 + 3264 + 16) == sizeof(X86State),
792+
static_assert((96 + 3264 + 16 + 128) == sizeof(X86State),
785793
"Invalid packing of `struct State`");
786794

787795
struct State : public X86State {};

lib/Arch/X86/Arch.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,17 @@ void X86Arch::PopulateRegisterTable(void) const {
16881688
REG(MM6, mmx.elems[6].val.qwords.elems[0], u64);
16891689
REG(MM7, mmx.elems[7].val.qwords.elems[0], u64);
16901690

1691+
if (has_avx512) {
1692+
REG(K0, k_reg.elems[0].val, u64);
1693+
REG(K1, k_reg.elems[1].val, u64);
1694+
REG(K2, k_reg.elems[2].val, u64);
1695+
REG(K3, k_reg.elems[3].val, u64);
1696+
REG(K4, k_reg.elems[4].val, u64);
1697+
REG(K5, k_reg.elems[5].val, u64);
1698+
REG(K6, k_reg.elems[6].val, u64);
1699+
REG(K7, k_reg.elems[7].val, u64);
1700+
}
1701+
16911702
// Arithmetic flags. Data-flow analyses will clear these out ;-)
16921703
REG(AF, aflag.af, u8);
16931704
REG(CF, aflag.cf, u8);

lib/Arch/X86/Semantics/DATAXFER.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,3 +1121,19 @@ IF_64BIT(DEF_ISEL(MOVSXD_GPRv_MEMd_64) = MOVSX<R64W, M32, int64_t>;)
11211121
IF_64BIT(DEF_ISEL(MOVSXD_GPRv_MEMz_64) = MOVSX<R64W, M32, int64_t>;)
11221122
IF_64BIT(DEF_ISEL(MOVSXD_GPRv_GPR32_64) = MOVSX<R64W, R32, int64_t>;)
11231123
IF_64BIT(DEF_ISEL(MOVSXD_GPRv_GPRz_64) = MOVSX<R64W, R32, int64_t>;)
1124+
1125+
namespace {
1126+
template <typename S1, typename S2>
1127+
DEF_SEM(KMOVW, S1 dst, S2 src) {
1128+
auto value = UInt16(Read(src));
1129+
WriteZExt(dst, value);
1130+
return memory;
1131+
}
1132+
1133+
} // namespace
1134+
1135+
DEF_ISEL(KMOVW_MASKmskw_MASKu16_AVX512) = KMOVW<R64W, R64>;
1136+
DEF_ISEL(KMOVW_GPR32u32_MASKmskw_AVX512) = KMOVW<R32W, R64>;
1137+
DEF_ISEL(KMOVW_MASKmskw_GPR32u32_AVX512) = KMOVW<R64W, R32>;
1138+
DEF_ISEL(KMOVW_MASKmskw_MEMu16_AVX512) = KMOVW<R64W, M16>;
1139+
DEF_ISEL(KMOVW_MEMu16_MASKmskw_AVX512) = KMOVW<M16W, R64>;

lib/BC/DeadStoreEliminator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ DEFINE_bool(name_register_variables, false,
6565
namespace remill {
6666
namespace {
6767

68-
static constexpr size_t kMaxNumSlots = 256;
68+
static constexpr size_t kMaxNumSlots = 272;
6969

7070
using ValueToOffset = std::unordered_map<llvm::Value *, uint64_t>;
7171
using InstToOffset = std::unordered_map<llvm::Instruction *, uint64_t>;

tests/X86/DATAXFER/KMOVW.S

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2021 Trail of Bits, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if HAS_FEATURE_AVX512
18+
TEST_BEGIN_64(KMOVW_MASKmskw_GPR32u32_AVX512, 1)
19+
TEST_INPUTS(
20+
0,
21+
1,
22+
0x41,
23+
0xff,
24+
0x80)
25+
26+
kmovw k2, ARG1_32
27+
TEST_END_64
28+
29+
30+
TEST_BEGIN_64(KMOVW_MASKmskw_MASKu16_AVX512, 1)
31+
TEST_INPUTS(
32+
0,
33+
1,
34+
0x41,
35+
0xff,
36+
0x80)
37+
38+
kmovw k2, ARG1_32
39+
kmovw k2, k1
40+
TEST_END_64
41+
42+
TEST_BEGIN_64(KMOVW_GPR32u32_MASKmskw_AVX512, 1)
43+
TEST_INPUTS(
44+
0,
45+
1,
46+
0x41,
47+
0xff,
48+
0x80)
49+
50+
kmovw k2, ARG1_32
51+
kmovw RET_32, k2
52+
TEST_END_64
53+
54+
#endif // HAS_FEATURE_AVX512s

tests/X86/Tests.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ SYMBOL(__x86_test_table_begin):
328328
#include "tests/X86/ABI.S"
329329
#include "tests/X86/MMX/INPUTS.S"
330330

331+
331332
/* Change to `0` and put new `#include`s above when making new tests to speed
332333
* up compile and test times. */
333334
#if 1
@@ -356,6 +357,7 @@ SYMBOL(__x86_test_table_begin):
356357
#include "tests/X86/DATAXFER/MOVUPS.S"
357358
#include "tests/X86/DATAXFER/MOVZX.S"
358359
#include "tests/X86/DATAXFER/XCHG.S"
360+
#include "tests/X86/DATAXFER/KMOVW.S"
359361

360362
/* Bring in the rest of the semantic tests. */
361363

0 commit comments

Comments
 (0)