Skip to content

Commit 267d86c

Browse files
toppercakiramenai
authored andcommitted
[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 b233ab5 commit 267d86c

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
@@ -670,6 +670,9 @@ class SelectionDAG {
670670
SDValue getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
671671
bool isTarget = false, bool isOpaque = false);
672672

673+
SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT,
674+
bool isTarget = false, bool isOpaque = false);
675+
673676
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget = false,
674677
bool IsOpaque = false) {
675678
return getConstant(APInt::getAllOnes(VT.getScalarSizeInBits()), DL, VT,

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

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

2595-
SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
2595+
SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
25962596
SDValue Zero = DAG.getConstant(0, dl, ExpVT);
25972597

25982598
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
@@ -1751,6 +1751,15 @@ SDValue SelectionDAG::getConstant(const ConstantInt &Val, const SDLoc &DL,
17511751
return Result;
17521752
}
17531753

1754+
SDValue SelectionDAG::getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT,
1755+
bool isT, bool isO) {
1756+
unsigned Size = VT.getScalarSizeInBits();
1757+
assert(
1758+
isIntN(Size, Val) &&
1759+
"getSignedConstant with a int64_t value that doesn't fit in the type!");
1760+
return getConstant(APInt(Size, Val, true), DL, VT, isT, isO);
1761+
}
1762+
17541763
SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, const SDLoc &DL,
17551764
bool isTarget) {
17561765
return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

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

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

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

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

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

63446344
MagicFactors.push_back(DAG.getConstant(magics.Magic, dl, SVT));
6345-
Factors.push_back(DAG.getConstant(NumeratorFactor, dl, SVT));
6345+
Factors.push_back(DAG.getSignedConstant(NumeratorFactor, dl, SVT));
63466346
Shifts.push_back(DAG.getConstant(magics.ShiftAmount, dl, ShSVT));
6347-
ShiftMasks.push_back(DAG.getConstant(ShiftMask, dl, SVT));
6347+
ShiftMasks.push_back(DAG.getSignedConstant(ShiftMask, dl, SVT));
63486348
return true;
63496349
};
63506350

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
@@ -10762,9 +10762,9 @@ static SDValue lowerShuffleAsBlend(const SDLoc &DL, MVT VT, SDValue V1,
1076210762
for (int i = 0, Size = Mask.size(); i < Size; ++i)
1076310763
for (int j = 0; j < Scale; ++j)
1076410764
VSELECTMask.push_back(
10765-
Mask[i] < 0 ? DAG.getUNDEF(MVT::i8)
10766-
: DAG.getConstant(Mask[i] < Size ? -1 : 0, DL,
10767-
MVT::i8));
10765+
Mask[i] < 0
10766+
? DAG.getUNDEF(MVT::i8)
10767+
: DAG.getSignedConstant(Mask[i] < Size ? -1 : 0, DL, MVT::i8));
1076810768

1076910769
V1 = DAG.getBitcast(BlendVT, V1);
1077010770
V2 = DAG.getBitcast(BlendVT, V2);
@@ -18649,7 +18649,7 @@ SDValue X86TargetLowering::LowerGlobalOrExternal(SDValue Op, SelectionDAG &DAG,
1864918649
// addition for it.
1865018650
if (Offset != 0)
1865118651
Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result,
18652-
DAG.getConstant(Offset, dl, PtrVT));
18652+
DAG.getSignedConstant(Offset, dl, PtrVT));
1865318653

1865418654
return Result;
1865518655
}
@@ -24969,9 +24969,9 @@ X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
2496924969
Result = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
2497024970
}
2497124971
if (Alignment && *Alignment > StackAlign)
24972-
Result =
24973-
DAG.getNode(ISD::AND, dl, VT, Result,
24974-
DAG.getConstant(~(Alignment->value() - 1ULL), dl, VT));
24972+
Result = DAG.getNode(
24973+
ISD::AND, dl, VT, Result,
24974+
DAG.getSignedConstant(~(Alignment->value() - 1ULL), dl, VT));
2497524975
Chain = DAG.getCopyToReg(Chain, dl, SPReg, Result); // Output chain
2497624976
} else if (SplitStack) {
2497724977
MachineRegisterInfo &MRI = MF.getRegInfo();
@@ -25003,8 +25003,9 @@ X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
2500325003
Chain = SP.getValue(1);
2500425004

2500525005
if (Alignment) {
25006-
SP = DAG.getNode(ISD::AND, dl, VT, SP.getValue(0),
25007-
DAG.getConstant(~(Alignment->value() - 1ULL), dl, VT));
25006+
SP = DAG.getNode(
25007+
ISD::AND, dl, VT, SP.getValue(0),
25008+
DAG.getSignedConstant(~(Alignment->value() - 1ULL), dl, VT));
2500825009
Chain = DAG.getCopyToReg(Chain, dl, SPReg, SP);
2500925010
}
2501025011

0 commit comments

Comments
 (0)