Skip to content

Commit 7c8e165

Browse files
committed
[RISCV] add computeKnownBitsForTargetNode for RISCVISD::SRLW
Signed-off-by: Shreeyash Pandey <[email protected]>
1 parent 8bf105c commit 7c8e165

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21340,6 +21340,15 @@ void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2134021340
Known = Known.sext(BitWidth);
2134121341
break;
2134221342
}
21343+
case RISCVISD::SRLW: {
21344+
KnownBits Known2;
21345+
Known = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
21346+
Known2 = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
21347+
Known = KnownBits::lshr(Known.trunc(32), Known2.trunc(5).zext(32));
21348+
// Restore the original width by sign extending.
21349+
Known = Known.sext(BitWidth);
21350+
break;
21351+
}
2134321352
case RISCVISD::CTZW: {
2134421353
KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
2134521354
unsigned PossibleTZ = Known2.trunc(32).countMaxTrailingZeros();

llvm/unittests/Target/RISCV/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ set(LLVM_LINK_COMPONENTS
1919
add_llvm_target_unittest(RISCVTests
2020
MCInstrAnalysisTest.cpp
2121
RISCVInstrInfoTest.cpp
22+
RISCVSelectionDAGTest.cpp
2223
)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//===----------------------------------------------------------------------===//
2+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
//===----------------------------------------------------------------------===//
7+
8+
#include "RISCVISelLowering.h"
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/MDBuilder.h"
15+
#include "llvm/IR/Module.h"
16+
#include "llvm/MC/TargetRegistry.h"
17+
#include "llvm/Support/KnownBits.h"
18+
#include "llvm/Support/SourceMgr.h"
19+
#include "llvm/Support/TargetSelect.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
#include "llvm/Target/TargetMachine.h"
22+
#include "gtest/gtest.h"
23+
24+
namespace llvm {
25+
26+
class RISCVSelectionDAGTest : public testing::Test {
27+
28+
protected:
29+
static void SetUpTestCase() {
30+
LLVMInitializeRISCVTargetInfo();
31+
LLVMInitializeRISCVTarget();
32+
LLVMInitializeRISCVTargetMC();
33+
}
34+
35+
void SetUp() override {
36+
StringRef Assembly = "define void @f() { ret void }";
37+
38+
Triple TargetTriple("riscv64", "unknown", "linux");
39+
40+
std::string Error;
41+
const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error);
42+
43+
TargetOptions Options;
44+
TM = std::unique_ptr<TargetMachine>(T->createTargetMachine(
45+
TargetTriple, "generic", "", Options, std::nullopt, std::nullopt,
46+
CodeGenOptLevel::Default));
47+
48+
SMDiagnostic SMError;
49+
M = parseAssemblyString(Assembly, SMError, Context);
50+
if (!M)
51+
report_fatal_error(SMError.getMessage());
52+
M->setDataLayout(TM->createDataLayout());
53+
54+
F = M->getFunction("f");
55+
if (!F)
56+
report_fatal_error("Function 'f' not found");
57+
58+
MachineModuleInfo MMI(TM.get());
59+
60+
MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F),
61+
MMI.getContext(), /*FunctionNum*/ 0);
62+
63+
DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::None);
64+
if (!DAG)
65+
report_fatal_error("SelectionDAG allocation failed");
66+
67+
OptimizationRemarkEmitter ORE(F);
68+
DAG->init(*MF, ORE, /*LibInfo*/ nullptr, /*AA*/ nullptr,
69+
/*AC*/ nullptr, /*MDT*/ nullptr, /*MSDT*/ nullptr, MMI, nullptr);
70+
}
71+
72+
LLVMContext Context;
73+
std::unique_ptr<TargetMachine> TM;
74+
std::unique_ptr<Module> M;
75+
Function *F = nullptr;
76+
std::unique_ptr<MachineFunction> MF;
77+
std::unique_ptr<SelectionDAG> DAG;
78+
};
79+
80+
/// SRLW: Logical Shift Right
81+
TEST_F(RISCVSelectionDAGTest, computeKnownBits_SRLW) {
82+
// Following DAG is created from this IR snippet:
83+
//
84+
// define i64 @f(i32 %x, i32 %y) {
85+
// %a = and i32 %x, 2147483647 ; zeros the MSB for %x
86+
// %b = lshr i32 %a, %y
87+
// %c = zext i32 %b to i64 ; makes the most significant 32 bits 0
88+
// ret i64 %c
89+
// }
90+
SDLoc Loc;
91+
auto IntVT = EVT::getIntegerVT(Context, 32);
92+
auto Int64VT = EVT::getIntegerVT(Context, 64);
93+
auto Px = DAG->getRegister(0, IntVT);
94+
auto Py = DAG->getConstant(2147483647, Loc, IntVT);
95+
auto N1 = DAG->getNode(ISD::AND, Loc, IntVT, Px, Py);
96+
auto Qx = DAG->getRegister(0, IntVT);
97+
auto N2 = DAG->getNode(ISD::SRL, Loc, IntVT, N1, Qx);
98+
auto N3 = DAG->getNode(ISD::ZERO_EXTEND, Loc, Int64VT, N2);
99+
// N1 = 0???????????????????????????????
100+
// N2 = 0???????????????????????????????
101+
// N3 = 000000000000000000000000000000000???????????????????????????????
102+
// After zero extend, we expect 33 most significant zeros to be known:
103+
// 32 from sign extension and 1 from AND operation
104+
KnownBits Known = DAG->computeKnownBits(N3);
105+
EXPECT_EQ(Known.Zero, APInt(64, -2147483648));
106+
EXPECT_EQ(Known.One, APInt(64, 0));
107+
}
108+
109+
} // end namespace llvm

0 commit comments

Comments
 (0)