Skip to content

Commit 7afb51e

Browse files
authored
[SelectionDAG][X86] Add SelectionDAG::getSignedConstant and use it in a few places. (#104555)
PR #80309 proposes to have users of APInt's uint64_t constructor opt-in to implicit truncation. Currently, that patch requires SelectionDAG::getConstant to opt-in. This patch adds getSignedConstant so we can start fixing some of the cases that require implicit truncation.
1 parent 1164e4a commit 7afb51e

File tree

7 files changed

+32
-17
lines changed

7 files changed

+32
-17
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,9 @@ class SelectionDAG {
672672
SDValue getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
673673
bool isTarget = false, bool isOpaque = false);
674674

675+
SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT,
676+
bool isTarget = false, bool isOpaque = false);
677+
675678
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget = false,
676679
bool IsOpaque = false);
677680

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
26012601
SDValue IsDenormal =
26022602
DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
26032603

2604-
SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
2604+
SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
26052605
SDValue Zero = DAG.getConstant(0, dl, ExpVT);
26062606

26072607
SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,15 @@ SDValue SelectionDAG::getConstant(const ConstantInt &Val, const SDLoc &DL,
17471747
return Result;
17481748
}
17491749

1750+
SDValue SelectionDAG::getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT,
1751+
bool isT, bool isO) {
1752+
unsigned Size = VT.getScalarSizeInBits();
1753+
assert(
1754+
isIntN(Size, Val) &&
1755+
"getSignedConstant with a int64_t value that doesn't fit in the type!");
1756+
return getConstant(APInt(Size, Val, true), DL, VT, isT, isO);
1757+
}
1758+
17501759
SDValue SelectionDAG::getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget,
17511760
bool IsOpaque) {
17521761
return getConstant(APInt::getAllOnes(VT.getScalarSizeInBits()), DL, VT,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4474,7 +4474,7 @@ void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
44744474

44754475
// Mask out the low bits for alignment purposes.
44764476
AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4477-
DAG.getConstant(~StackAlignMask, dl, IntPtr));
4477+
DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
44784478

44794479
SDValue Ops[] = {
44804480
getRoot(), AllocSize,

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6349,9 +6349,9 @@ SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
63496349
}
63506350

63516351
MagicFactors.push_back(DAG.getConstant(magics.Magic, dl, SVT));
6352-
Factors.push_back(DAG.getConstant(NumeratorFactor, dl, SVT));
6352+
Factors.push_back(DAG.getSignedConstant(NumeratorFactor, dl, SVT));
63536353
Shifts.push_back(DAG.getConstant(magics.ShiftAmount, dl, ShSVT));
6354-
ShiftMasks.push_back(DAG.getConstant(ShiftMask, dl, SVT));
6354+
ShiftMasks.push_back(DAG.getSignedConstant(ShiftMask, dl, SVT));
63556355
return true;
63566356
};
63576357

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ namespace {
314314
Disp = CurDAG->getTargetBlockAddress(AM.BlockAddr, MVT::i32, AM.Disp,
315315
AM.SymbolFlags);
316316
else
317-
Disp = CurDAG->getTargetConstant(AM.Disp, DL, MVT::i32);
317+
Disp =
318+
CurDAG->getSignedConstant(AM.Disp, DL, MVT::i32, /*isTarget=*/true);
318319

319320
if (AM.Segment.getNode())
320321
Segment = AM.Segment;
@@ -2130,7 +2131,7 @@ static bool foldMaskedShiftToScaledMask(SelectionDAG &DAG, SDValue N,
21302131
X = NewX;
21312132
}
21322133

2133-
SDValue NewMask = DAG.getConstant(Mask >> ShiftAmt, DL, VT);
2134+
SDValue NewMask = DAG.getSignedConstant(Mask >> ShiftAmt, DL, VT);
21342135
SDValue NewAnd = DAG.getNode(ISD::AND, DL, VT, X, NewMask);
21352136
SDValue NewShift = DAG.getNode(ISD::SHL, DL, VT, NewAnd, Shift.getOperand(1));
21362137

@@ -3733,7 +3734,8 @@ bool X86DAGToDAGISel::foldLoadStoreIntoMemOperand(SDNode *Node) {
37333734
}
37343735

37353736
if (MemVT != MVT::i64 || isInt<32>(OperandV)) {
3736-
Operand = CurDAG->getTargetConstant(OperandV, SDLoc(Node), MemVT);
3737+
Operand = CurDAG->getSignedConstant(OperandV, SDLoc(Node), MemVT,
3738+
/*isTarget=*/true);
37373739
NewOpc = SelectImmOpcode(Opc);
37383740
}
37393741
}
@@ -4507,7 +4509,7 @@ bool X86DAGToDAGISel::tryShrinkShlLogicImm(SDNode *N) {
45074509
X = NewX;
45084510
}
45094511

4510-
SDValue NewCst = CurDAG->getConstant(ShiftedVal, dl, NVT);
4512+
SDValue NewCst = CurDAG->getSignedConstant(ShiftedVal, dl, NVT);
45114513
insertDAGNode(*CurDAG, SDValue(N, 0), NewCst);
45124514
SDValue NewBinOp = CurDAG->getNode(Opcode, dl, NVT, X, NewCst);
45134515
insertDAGNode(*CurDAG, SDValue(N, 0), NewBinOp);

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10763,9 +10763,9 @@ static SDValue lowerShuffleAsBlend(const SDLoc &DL, MVT VT, SDValue V1,
1076310763
for (int i = 0, Size = Mask.size(); i < Size; ++i)
1076410764
for (int j = 0; j < Scale; ++j)
1076510765
VSELECTMask.push_back(
10766-
Mask[i] < 0 ? DAG.getUNDEF(MVT::i8)
10767-
: DAG.getConstant(Mask[i] < Size ? -1 : 0, DL,
10768-
MVT::i8));
10766+
Mask[i] < 0
10767+
? DAG.getUNDEF(MVT::i8)
10768+
: DAG.getSignedConstant(Mask[i] < Size ? -1 : 0, DL, MVT::i8));
1076910769

1077010770
V1 = DAG.getBitcast(BlendVT, V1);
1077110771
V2 = DAG.getBitcast(BlendVT, V2);
@@ -18654,7 +18654,7 @@ SDValue X86TargetLowering::LowerGlobalOrExternal(SDValue Op, SelectionDAG &DAG,
1865418654
// addition for it.
1865518655
if (Offset != 0)
1865618656
Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result,
18657-
DAG.getConstant(Offset, dl, PtrVT));
18657+
DAG.getSignedConstant(Offset, dl, PtrVT));
1865818658

1865918659
return Result;
1866018660
}
@@ -24975,9 +24975,9 @@ X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
2497524975
Result = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
2497624976
}
2497724977
if (Alignment && *Alignment > StackAlign)
24978-
Result =
24979-
DAG.getNode(ISD::AND, dl, VT, Result,
24980-
DAG.getConstant(~(Alignment->value() - 1ULL), dl, VT));
24978+
Result = DAG.getNode(
24979+
ISD::AND, dl, VT, Result,
24980+
DAG.getSignedConstant(~(Alignment->value() - 1ULL), dl, VT));
2498124981
Chain = DAG.getCopyToReg(Chain, dl, SPReg, Result); // Output chain
2498224982
} else if (SplitStack) {
2498324983
MachineRegisterInfo &MRI = MF.getRegInfo();
@@ -25009,8 +25009,9 @@ X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
2500925009
Chain = SP.getValue(1);
2501025010

2501125011
if (Alignment) {
25012-
SP = DAG.getNode(ISD::AND, dl, VT, SP.getValue(0),
25013-
DAG.getConstant(~(Alignment->value() - 1ULL), dl, VT));
25012+
SP = DAG.getNode(
25013+
ISD::AND, dl, VT, SP.getValue(0),
25014+
DAG.getSignedConstant(~(Alignment->value() - 1ULL), dl, VT));
2501425015
Chain = DAG.getCopyToReg(Chain, dl, SPReg, SP);
2501525016
}
2501625017

0 commit comments

Comments
 (0)