Skip to content

Commit fdb1bf9

Browse files
authored
[LoongArch] Use getSignedConstant() where necessary (#117172)
To prevent assertion failures when we disable implicit truncation in getConstant().
1 parent cc721db commit fdb1bf9

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void LoongArchDAGToDAGISel::Select(SDNode *Node) {
6161
SDValue SrcReg = CurDAG->getRegister(LoongArch::R0, GRLenVT);
6262
// The instructions in the sequence are handled here.
6363
for (LoongArchMatInt::Inst &Inst : LoongArchMatInt::generateInstSeq(Imm)) {
64-
SDValue SDImm = CurDAG->getTargetConstant(Inst.Imm, DL, GRLenVT);
64+
SDValue SDImm = CurDAG->getSignedTargetConstant(Inst.Imm, DL, GRLenVT);
6565
switch (Inst.Opc) {
6666
case LoongArch::LU12I_W:
6767
Result = CurDAG->getMachineNode(Inst.Opc, DL, GRLenVT, SDImm);

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ SDValue LoongArchTargetLowering::lowerFRAMEADDR(SDValue Op,
15331533
while (Depth--) {
15341534
int Offset = -(GRLenInBytes * 2);
15351535
SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1536-
DAG.getIntPtrConstant(Offset, DL));
1536+
DAG.getSignedConstant(Offset, DL, VT));
15371537
FrameAddr =
15381538
DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
15391539
}
@@ -2548,7 +2548,8 @@ SDValue LoongArchTargetLowering::lowerShiftLeftParts(SDValue Op,
25482548

25492549
SDValue Zero = DAG.getConstant(0, DL, VT);
25502550
SDValue One = DAG.getConstant(1, DL, VT);
2551-
SDValue MinusGRLen = DAG.getConstant(-(int)Subtarget.getGRLen(), DL, VT);
2551+
SDValue MinusGRLen =
2552+
DAG.getSignedConstant(-(int)Subtarget.getGRLen(), DL, VT);
25522553
SDValue GRLenMinus1 = DAG.getConstant(Subtarget.getGRLen() - 1, DL, VT);
25532554
SDValue ShamtMinusGRLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusGRLen);
25542555
SDValue GRLenMinus1Shamt = DAG.getNode(ISD::XOR, DL, VT, Shamt, GRLenMinus1);
@@ -2599,7 +2600,8 @@ SDValue LoongArchTargetLowering::lowerShiftRightParts(SDValue Op,
25992600

26002601
SDValue Zero = DAG.getConstant(0, DL, VT);
26012602
SDValue One = DAG.getConstant(1, DL, VT);
2602-
SDValue MinusGRLen = DAG.getConstant(-(int)Subtarget.getGRLen(), DL, VT);
2603+
SDValue MinusGRLen =
2604+
DAG.getSignedConstant(-(int)Subtarget.getGRLen(), DL, VT);
26032605
SDValue GRLenMinus1 = DAG.getConstant(Subtarget.getGRLen() - 1, DL, VT);
26042606
SDValue ShamtMinusGRLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusGRLen);
26052607
SDValue GRLenMinus1Shamt = DAG.getNode(ISD::XOR, DL, VT, Shamt, GRLenMinus1);
@@ -6123,17 +6125,17 @@ void LoongArchTargetLowering::LowerAsmOperandForConstraint(
61236125
if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
61246126
uint64_t CVal = C->getSExtValue();
61256127
if (isInt<16>(CVal))
6126-
Ops.push_back(
6127-
DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getGRLenVT()));
6128+
Ops.push_back(DAG.getSignedTargetConstant(CVal, SDLoc(Op),
6129+
Subtarget.getGRLenVT()));
61286130
}
61296131
return;
61306132
case 'I':
61316133
// Validate & create a 12-bit signed immediate operand.
61326134
if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
61336135
uint64_t CVal = C->getSExtValue();
61346136
if (isInt<12>(CVal))
6135-
Ops.push_back(
6136-
DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getGRLenVT()));
6137+
Ops.push_back(DAG.getSignedTargetConstant(CVal, SDLoc(Op),
6138+
Subtarget.getGRLenVT()));
61376139
}
61386140
return;
61396141
case 'J':

llvm/lib/Target/LoongArch/LoongArchInstrInfo.td

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,8 @@ def simm12_plus1 : ImmLeaf<GRLenVT,
481481

482482
// Return the negation of an immediate value.
483483
def NegImm : SDNodeXForm<imm, [{
484-
return CurDAG->getTargetConstant(-N->getSExtValue(), SDLoc(N),
485-
N->getValueType(0));
484+
return CurDAG->getSignedTargetConstant(-N->getSExtValue(), SDLoc(N),
485+
N->getValueType(0));
486486
}]>;
487487

488488
// FP immediate patterns.
@@ -538,16 +538,16 @@ def AddiPair : PatLeaf<(imm), [{
538538
// Return -2048 if immediate is negative or 2047 if positive.
539539
def AddiPairImmLarge : SDNodeXForm<imm, [{
540540
int64_t Imm = N->getSExtValue() < 0 ? -2048 : 2047;
541-
return CurDAG->getTargetConstant(Imm, SDLoc(N),
542-
N->getValueType(0));
541+
return CurDAG->getSignedTargetConstant(Imm, SDLoc(N),
542+
N->getValueType(0));
543543
}]>;
544544

545545
// Return imm - (imm < 0 ? -2048 : 2047).
546546
def AddiPairImmSmall : SDNodeXForm<imm, [{
547547
int64_t Imm = N->getSExtValue();
548548
int64_t Adj = Imm < 0 ? -2048 : 2047;
549-
return CurDAG->getTargetConstant(Imm - Adj, SDLoc(N),
550-
N->getValueType(0));
549+
return CurDAG->getSignedTargetConstant(Imm - Adj, SDLoc(N),
550+
N->getValueType(0));
551551
}]>;
552552

553553
// Check if (mul r, imm) can be optimized to (SLLI (ALSL r, r, i0), i1),

llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,14 @@ def f64imm_vldi : PatLeaf<(fpimm), [{
223223
def to_f32imm_vldi : SDNodeXForm<fpimm, [{
224224
uint64_t x = N->getValueAPF().bitcastToAPInt().getZExtValue();
225225
x = (0b11011 << 8) | (((x >> 24) & 0xc0) ^ 0x40) | ((x >> 19) & 0x3f);
226-
return CurDAG->getTargetConstant(SignExtend32<13>(x), SDLoc(N), MVT::i32);
226+
return CurDAG->getSignedTargetConstant(SignExtend32<13>(x), SDLoc(N),
227+
MVT::i32);
227228
}]>;
228229
def to_f64imm_vldi : SDNodeXForm<fpimm, [{
229230
uint64_t x = N->getValueAPF().bitcastToAPInt().getZExtValue();
230231
x = (0b11100 << 8) | (((x >> 56) & 0xc0) ^ 0x40) | ((x >> 48) & 0x3f);
231-
return CurDAG->getTargetConstant(SignExtend32<13>(x), SDLoc(N), MVT::i32);
232+
return CurDAG->getSignedTargetConstant(SignExtend32<13>(x), SDLoc(N),
233+
MVT::i32);
232234
}]>;
233235

234236
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)