Skip to content

Commit 9d7b01d

Browse files
committed
[RISCV] Implement RISCVTargetLowering::getTargetConstantFromLoad.
This allows computeKnownBits to see the constant being loaded. This recovers the rv64zbp test case changes from D127520. Reviewed By: reames Differential Revision: https://reviews.llvm.org/D127679
1 parent 5afdceb commit 9d7b01d

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9382,6 +9382,51 @@ unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
93829382
return 1;
93839383
}
93849384

9385+
const Constant *
9386+
RISCVTargetLowering::getTargetConstantFromLoad(LoadSDNode *Ld) const {
9387+
assert(Ld && "Unexpected null LoadSDNode");
9388+
if (!ISD::isNormalLoad(Ld))
9389+
return nullptr;
9390+
9391+
SDValue Ptr = Ld->getBasePtr();
9392+
9393+
// Only constant pools with no offset are supported.
9394+
auto GetSupportedConstantPool = [](SDValue Ptr) -> ConstantPoolSDNode * {
9395+
auto *CNode = dyn_cast<ConstantPoolSDNode>(Ptr);
9396+
if (!CNode || CNode->isMachineConstantPoolEntry() ||
9397+
CNode->getOffset() != 0)
9398+
return nullptr;
9399+
9400+
return CNode;
9401+
};
9402+
9403+
// Simple case, LLA.
9404+
if (Ptr.getOpcode() == RISCVISD::LLA) {
9405+
auto *CNode = GetSupportedConstantPool(Ptr);
9406+
if (!CNode || CNode->getTargetFlags() != 0)
9407+
return nullptr;
9408+
9409+
return CNode->getConstVal();
9410+
}
9411+
9412+
// Look for a HI and ADD_LO pair.
9413+
if (Ptr.getOpcode() != RISCVISD::ADD_LO ||
9414+
Ptr.getOperand(0).getOpcode() != RISCVISD::HI)
9415+
return nullptr;
9416+
9417+
auto *CNodeLo = GetSupportedConstantPool(Ptr.getOperand(1));
9418+
auto *CNodeHi = GetSupportedConstantPool(Ptr.getOperand(0).getOperand(0));
9419+
9420+
if (!CNodeLo || CNodeLo->getTargetFlags() != RISCVII::MO_LO ||
9421+
!CNodeHi || CNodeHi->getTargetFlags() != RISCVII::MO_HI)
9422+
return nullptr;
9423+
9424+
if (CNodeLo->getConstVal() != CNodeHi->getConstVal())
9425+
return nullptr;
9426+
9427+
return CNodeLo->getConstVal();
9428+
}
9429+
93859430
static MachineBasicBlock *emitReadCycleWidePseudo(MachineInstr &MI,
93869431
MachineBasicBlock *BB) {
93879432
assert(MI.getOpcode() == RISCV::ReadCycleWide && "Unexpected instruction");

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ class RISCVTargetLowering : public TargetLowering {
410410
const SelectionDAG &DAG,
411411
unsigned Depth) const override;
412412

413+
const Constant *getTargetConstantFromLoad(LoadSDNode *LD) const override;
414+
413415
// This method returns the name of a target specific DAG node.
414416
const char *getTargetNodeName(unsigned Opcode) const override;
415417

llvm/test/CodeGen/RISCV/rv64zbp-intrinsic.ll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,8 @@ define i64 @gorci64_knownbits(i64 %a) nounwind {
530530
; RV64ZBP: # %bb.0:
531531
; RV64ZBP-NEXT: lui a1, %hi(.LCPI54_0)
532532
; RV64ZBP-NEXT: ld a1, %lo(.LCPI54_0)(a1)
533-
; RV64ZBP-NEXT: lui a2, %hi(.LCPI54_1)
534-
; RV64ZBP-NEXT: ld a2, %lo(.LCPI54_1)(a2)
535533
; RV64ZBP-NEXT: or a0, a0, a1
536534
; RV64ZBP-NEXT: orc32 a0, a0
537-
; RV64ZBP-NEXT: or a0, a0, a2
538535
; RV64ZBP-NEXT: ret
539536
%tmp = or i64 %a, 72624976668147840 ; 0x102040810204080
540537
%tmp2 = call i64 @llvm.riscv.gorc.i64(i64 %tmp, i64 32)

llvm/test/CodeGen/RISCV/rv64zbp.ll

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,20 +1007,19 @@ define i64 @gorc2b_i64(i64 %a) nounwind {
10071007
;
10081008
; RV64ZBP-LABEL: gorc2b_i64:
10091009
; RV64ZBP: # %bb.0:
1010-
; RV64ZBP-NEXT: lui a1, %hi(.LCPI26_0)
1011-
; RV64ZBP-NEXT: ld a1, %lo(.LCPI26_0)(a1)
1012-
; RV64ZBP-NEXT: srli a2, a0, 2
1013-
; RV64ZBP-NEXT: and a2, a2, a1
1010+
; RV64ZBP-NEXT: srli a1, a0, 2
1011+
; RV64ZBP-NEXT: or a1, a1, a0
1012+
; RV64ZBP-NEXT: orc2.n a0, a0
1013+
; RV64ZBP-NEXT: lui a2, %hi(.LCPI26_0)
1014+
; RV64ZBP-NEXT: ld a2, %lo(.LCPI26_0)(a2)
10141015
; RV64ZBP-NEXT: lui a3, %hi(.LCPI26_1)
10151016
; RV64ZBP-NEXT: ld a3, %lo(.LCPI26_1)(a3)
1016-
; RV64ZBP-NEXT: or a2, a2, a0
1017-
; RV64ZBP-NEXT: orc2.n a0, a0
1018-
; RV64ZBP-NEXT: slli a2, a2, 2
1017+
; RV64ZBP-NEXT: slli a1, a1, 2
1018+
; RV64ZBP-NEXT: and a1, a1, a2
1019+
; RV64ZBP-NEXT: srli a2, a0, 2
10191020
; RV64ZBP-NEXT: and a2, a2, a3
1020-
; RV64ZBP-NEXT: srli a3, a0, 2
1021-
; RV64ZBP-NEXT: and a1, a3, a1
1022-
; RV64ZBP-NEXT: or a0, a1, a0
1023-
; RV64ZBP-NEXT: or a0, a0, a2
1021+
; RV64ZBP-NEXT: or a0, a2, a0
1022+
; RV64ZBP-NEXT: or a0, a0, a1
10241023
; RV64ZBP-NEXT: ret
10251024
%and1 = shl i64 %a, 2
10261025
%shl1 = and i64 %and1, -3689348814741910324

0 commit comments

Comments
 (0)