|
| 1 | +//===- FunnelShiftCombineTest.cpp - X86 Funnel Shift Combine Tests --------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Analysis/OptimizationRemarkEmitter.h" |
| 10 | +#include "llvm/AsmParser/Parser.h" |
| 11 | +#include "llvm/CodeGen/MachineModuleInfo.h" |
| 12 | +#include "llvm/CodeGen/SelectionDAG.h" |
| 13 | +#include "llvm/CodeGen/TargetLowering.h" |
| 14 | +#include "llvm/IR/Module.h" |
| 15 | +#include "llvm/MC/TargetRegistry.h" |
| 16 | +#include "llvm/Support/SourceMgr.h" |
| 17 | +#include "llvm/Support/TargetSelect.h" |
| 18 | +#include "llvm/Target/TargetMachine.h" |
| 19 | +#include "llvm/Target/TargetOptions.h" |
| 20 | +#include "gtest/gtest.h" |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +class X86FunnelShiftCombineTest : public testing::Test { |
| 27 | +protected: |
| 28 | + static void SetUpTestCase() { |
| 29 | + LLVMInitializeX86TargetInfo(); |
| 30 | + LLVMInitializeX86Target(); |
| 31 | + LLVMInitializeX86TargetMC(); |
| 32 | + } |
| 33 | + |
| 34 | + void SetUp() override { |
| 35 | + Triple TargetTriple("x86_64-unknown-unknown"); |
| 36 | + std::string Error; |
| 37 | + const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error); |
| 38 | + if (!T) |
| 39 | + GTEST_SKIP(); |
| 40 | + |
| 41 | + TargetOptions Options; |
| 42 | + // Enable VBMI2 to test funnel shift combines |
| 43 | + TM = std::unique_ptr<TargetMachine>(T->createTargetMachine( |
| 44 | + TargetTriple, "", "+avx512f,+avx512vbmi2", Options, std::nullopt, |
| 45 | + std::nullopt, CodeGenOptLevel::Default)); |
| 46 | + if (!TM) |
| 47 | + GTEST_SKIP(); |
| 48 | + |
| 49 | + StringRef Assembly = "define void @test() { ret void }"; |
| 50 | + SMDiagnostic SMError; |
| 51 | + M = parseAssemblyString(Assembly, SMError, Context); |
| 52 | + ASSERT_TRUE(M && "Could not parse module!"); |
| 53 | + M->setDataLayout(TM->createDataLayout()); |
| 54 | + |
| 55 | + F = M->getFunction("test"); |
| 56 | + ASSERT_TRUE(F && "Could not get function test!"); |
| 57 | + |
| 58 | + MachineModuleInfo MMI(TM.get()); |
| 59 | + MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F), |
| 60 | + MMI.getContext(), 0); |
| 61 | + |
| 62 | + DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::Default); |
| 63 | + ASSERT_TRUE(DAG && "Failed to create SelectionDAG!"); |
| 64 | + OptimizationRemarkEmitter ORE(F); |
| 65 | + DAG->init(*MF, ORE, nullptr, nullptr, nullptr, nullptr, nullptr, MMI, |
| 66 | + nullptr); |
| 67 | + } |
| 68 | + |
| 69 | + LLVMContext Context; |
| 70 | + std::unique_ptr<TargetMachine> TM; |
| 71 | + std::unique_ptr<Module> M; |
| 72 | + Function *F; |
| 73 | + std::unique_ptr<MachineFunction> MF; |
| 74 | + std::unique_ptr<SelectionDAG> DAG; |
| 75 | +}; |
| 76 | + |
| 77 | +// Test that v16i32 is legal for VBMI2 (should be combined) |
| 78 | +TEST_F(X86FunnelShiftCombineTest, TestFSHLv16i32Legal) { |
| 79 | + MVT VT = MVT::v16i32; |
| 80 | + const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 81 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHL, VT)); |
| 82 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHR, VT)); |
| 83 | +} |
| 84 | + |
| 85 | +// Test that v8i64 is legal for VBMI2 (should be combined) |
| 86 | +TEST_F(X86FunnelShiftCombineTest, TestFSHRv8i64Legal) { |
| 87 | + MVT VT = MVT::v8i64; |
| 88 | + const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 89 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHL, VT)); |
| 90 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHR, VT)); |
| 91 | +} |
| 92 | + |
| 93 | +// Test that v2i32 is NOT legal for VBMI2 (should NOT be combined) |
| 94 | +TEST_F(X86FunnelShiftCombineTest, TestFSHLv2i32NonLegal) { |
| 95 | + MVT VT = MVT::v2i32; |
| 96 | + const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 97 | + EXPECT_FALSE(TLI.isOperationLegal(ISD::FSHL, VT)); |
| 98 | + EXPECT_FALSE(TLI.isOperationLegal(ISD::FSHR, VT)); |
| 99 | +} |
| 100 | + |
| 101 | +// Test that v32i16 is legal for VBMI2 (should be combined) |
| 102 | +TEST_F(X86FunnelShiftCombineTest, TestFSHLv32i16Legal) { |
| 103 | + MVT VT = MVT::v32i16; |
| 104 | + const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 105 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHL, VT)); |
| 106 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHR, VT)); |
| 107 | +} |
| 108 | + |
| 109 | +// Test that v8i16 with VLX is legal |
| 110 | +TEST_F(X86FunnelShiftCombineTest, TestFSHLv8i16WithVLX) { |
| 111 | + Triple TargetTriple("x86_64-unknown-unknown"); |
| 112 | + std::string Error; |
| 113 | + const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error); |
| 114 | + ASSERT_TRUE(T); |
| 115 | + |
| 116 | + TargetOptions Options; |
| 117 | + TM = std::unique_ptr<TargetMachine>(T->createTargetMachine( |
| 118 | + TargetTriple, "", "+avx512f,+avx512vbmi2,+avx512vl", Options, |
| 119 | + std::nullopt, std::nullopt, CodeGenOptLevel::Default)); |
| 120 | + ASSERT_TRUE(TM); |
| 121 | + |
| 122 | + MachineModuleInfo MMI(TM.get()); |
| 123 | + MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F), |
| 124 | + MMI.getContext(), 0); |
| 125 | + DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::Default); |
| 126 | + OptimizationRemarkEmitter ORE(F); |
| 127 | + DAG->init(*MF, ORE, nullptr, nullptr, nullptr, nullptr, nullptr, MMI, |
| 128 | + nullptr); |
| 129 | + |
| 130 | + MVT VT = MVT::v8i16; |
| 131 | + const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 132 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHL, VT)); |
| 133 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHR, VT)); |
| 134 | +} |
| 135 | + |
| 136 | +// Test that v4i32 with VLX is legal |
| 137 | +TEST_F(X86FunnelShiftCombineTest, TestFSHLv4i32WithVLX) { |
| 138 | + Triple TargetTriple("x86_64-unknown-unknown"); |
| 139 | + std::string Error; |
| 140 | + const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error); |
| 141 | + ASSERT_TRUE(T); |
| 142 | + |
| 143 | + TargetOptions Options; |
| 144 | + TM = std::unique_ptr<TargetMachine>(T->createTargetMachine( |
| 145 | + TargetTriple, "", "+avx512f,+avx512vbmi2,+avx512vl", Options, |
| 146 | + std::nullopt, std::nullopt, CodeGenOptLevel::Default)); |
| 147 | + ASSERT_TRUE(TM); |
| 148 | + |
| 149 | + MachineModuleInfo MMI(TM.get()); |
| 150 | + MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F), |
| 151 | + MMI.getContext(), 0); |
| 152 | + DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::Default); |
| 153 | + OptimizationRemarkEmitter ORE(F); |
| 154 | + DAG->init(*MF, ORE, nullptr, nullptr, nullptr, nullptr, nullptr, MMI, |
| 155 | + nullptr); |
| 156 | + |
| 157 | + MVT VT = MVT::v4i32; |
| 158 | + const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 159 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHL, VT)); |
| 160 | + EXPECT_TRUE(TLI.isOperationLegal(ISD::FSHR, VT)); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace |
0 commit comments