diff --git a/llvm/include/llvm/CodeGen/SelectionDAG.h b/llvm/include/llvm/CodeGen/SelectionDAG.h --- a/llvm/include/llvm/CodeGen/SelectionDAG.h +++ b/llvm/include/llvm/CodeGen/SelectionDAG.h @@ -700,9 +700,19 @@ SDValue getTargetJumpTable(int JTI, EVT VT, unsigned TargetFlags = 0) { return getJumpTable(JTI, VT, true, TargetFlags); } - SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align = None, - int Offs = 0, bool isT = false, + + /// Create a ConstantPoolSDNode wrapping a constant value. + /// Provide the alignment or none and return the final alignment in \p + /// NewAlign. + SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Alignment, + Align &NewAlign, int Offs = 0, bool isT = false, unsigned TargetFlags = 0); + SDValue getConstantPool(const Constant *C, EVT VT, + MaybeAlign Alignment = None, int Offs = 0, + bool isT = false, unsigned TargetFlags = 0) { + Align Dummy; + return getConstantPool(C, VT, Alignment, Dummy, Offs, isT, TargetFlags); + } SDValue getTargetConstantPool(const Constant *C, EVT VT, MaybeAlign Align = None, int Offset = 0, unsigned TargetFlags = 0) { diff --git a/llvm/include/llvm/CodeGen/SelectionDAGTargetInfo.h b/llvm/include/llvm/CodeGen/SelectionDAGTargetInfo.h --- a/llvm/include/llvm/CodeGen/SelectionDAGTargetInfo.h +++ b/llvm/include/llvm/CodeGen/SelectionDAGTargetInfo.h @@ -155,6 +155,14 @@ return SDValue(); } + /// Emit target-specific code to customize the lowering of constant pool. + virtual SDValue + EmitTargetCodeForConstantPool(SelectionDAG &DAG, const Constant *C, EVT VT, + Align Alignment, Align &NewAlign, int Offset, + bool isTarget, unsigned TargetFlags) const { + return SDValue(); + } + // Return true when the decision to generate FMA's (or FMS, FMLA etc) rather // than FMUL and ADD is delegated to the machine combiner. virtual bool generateFMAsInMachineCombiner(CodeGenOpt::Level OptLevel) const { diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -21798,10 +21798,10 @@ const DataLayout &TD = DAG.getDataLayout(); // Create a ConstantArray of the two constants. + Align Alignment; Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts); SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(DAG.getDataLayout()), - TD.getPrefTypeAlign(FPTy)); - Align Alignment = cast(CPIdx)->getAlign(); + TD.getPrefTypeAlign(FPTy), Alignment); // Get offsets to the 0 and 1 elements of the array, so we can select between // them. diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp --- a/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp @@ -329,9 +329,9 @@ } } - SDValue CPIdx = - DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout())); - Align Alignment = cast(CPIdx)->getAlign(); + Align Alignment; + SDValue CPIdx = DAG.getConstantPool( + LLVMC, TLI.getPointerTy(DAG.getDataLayout()), None, Alignment); if (Extend) { SDValue Result = DAG.getExtLoad( ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx, @@ -349,9 +349,10 @@ SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) { SDLoc dl(CP); EVT VT = CP->getValueType(0); + Align Alignment; SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(), - TLI.getPointerTy(DAG.getDataLayout())); - Align Alignment = cast(CPIdx)->getAlign(); + TLI.getPointerTy(DAG.getDataLayout()), + None, Alignment); SDValue Result = DAG.getLoad( VT, dl, DAG.getEntryNode(), CPIdx, MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment); @@ -2036,10 +2037,10 @@ CV.push_back(UndefValue::get(OpNTy)); } } + Align Alignment; Constant *CP = ConstantVector::get(CV); - SDValue CPIdx = - DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout())); - Align Alignment = cast(CPIdx)->getAlign(); + SDValue CPIdx = DAG.getConstantPool( + CP, TLI.getPointerTy(DAG.getDataLayout()), None, Alignment); return DAG.getLoad( VT, dl, DAG.getEntryNode(), CPIdx, MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), @@ -2594,9 +2595,9 @@ Constant *FudgeFactor = ConstantInt::get( Type::getInt64Ty(*DAG.getContext()), FF); - SDValue CPIdx = - DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout())); - Align Alignment = cast(CPIdx)->getAlign(); + Align Alignment; + SDValue CPIdx = DAG.getConstantPool( + FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()), None, Alignment); CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset); Alignment = commonAlignment(Alignment, 4); SDValue FudgeInReg; diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -1524,14 +1524,26 @@ } SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT, - MaybeAlign Alignment, int Offset, - bool isTarget, unsigned TargetFlags) { + MaybeAlign Alignment, Align &NewAlign, + int Offset, bool isTarget, + unsigned TargetFlags) { assert((TargetFlags == 0 || isTarget) && "Cannot set target flags on target-independent globals"); if (!Alignment) Alignment = shouldOptForSize() ? getDataLayout().getABITypeAlign(C->getType()) : getDataLayout().getPrefTypeAlign(C->getType()); + + // Check to see if we should lower the constant pool with target-specific + // code. + if (TSI) { + SDValue Result = TSI->EmitTargetCodeForConstantPool( + *this, C, VT, *Alignment, NewAlign, Offset, isTarget, TargetFlags); + if (Result.getNode()) + return Result; + } + + NewAlign = *Alignment; unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; FoldingSetNodeID ID; AddNodeIDNode(ID, Opc, getVTList(VT), None); diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -8808,13 +8808,13 @@ (SplatBitSize < 32 && Subtarget.hasAVX2())) { // Splatted value can fit in one INTEGER constant in constant pool. // Load the constant and broadcast it. + Align Alignment; MVT CVT = MVT::getIntegerVT(SplatBitSize); Type *ScalarTy = Type::getIntNTy(*Ctx, SplatBitSize); Constant *C = Constant::getIntegerValue(ScalarTy, SplatValue); - SDValue CP = DAG.getConstantPool(C, PVT); + SDValue CP = DAG.getConstantPool(C, PVT, None, Alignment); unsigned Repeat = VT.getSizeInBits() / SplatBitSize; - Align Alignment = cast(CP)->getAlign(); SDVTList Tys = DAG.getVTList(MVT::getVectorVT(CVT, Repeat), MVT::Other); SDValue Ops[] = {DAG.getEntryNode(), CP}; @@ -8827,12 +8827,12 @@ } if (SplatBitSize > 64) { // Load the vector of constants and broadcast it. + Align Alignment; Constant *VecC = getConstantVector(VT, SplatValue, SplatBitSize, *Ctx); - SDValue VCP = DAG.getConstantPool(VecC, PVT); + SDValue VCP = DAG.getConstantPool(VecC, PVT, None, Alignment); unsigned NumElm = SplatBitSize / VT.getScalarSizeInBits(); MVT VVT = MVT::getVectorVT(VT.getScalarType(), NumElm); - Align Alignment = cast(VCP)->getAlign(); SDVTList Tys = DAG.getVTList(VT, MVT::Other); SDValue Ops[] = {DAG.getEntryNode(), VCP}; MachinePointerInfo MPI = @@ -8902,10 +8902,10 @@ assert(C && "Invalid constant type"); + Align Alignment; const TargetLowering &TLI = DAG.getTargetLoweringInfo(); - SDValue CP = - DAG.getConstantPool(C, TLI.getPointerTy(DAG.getDataLayout())); - Align Alignment = cast(CP)->getAlign(); + SDValue CP = DAG.getConstantPool(C, TLI.getPointerTy(DAG.getDataLayout()), + None, Alignment); SDVTList Tys = DAG.getVTList(VT, MVT::Other); SDValue Ops[] = {DAG.getEntryNode(), CP}; @@ -20422,10 +20422,10 @@ Op.getOperand(OpNo), DAG.getConstant(0, dl, MVT::i64), ISD::SETLT); // Build a 64 bit pair (FF, 0) in the constant pool, with FF in the hi bits. + Align CPAlignment; APInt FF(64, 0x5F80000000000000ULL); SDValue FudgePtr = DAG.getConstantPool( - ConstantInt::get(*DAG.getContext(), FF), PtrVT); - Align CPAlignment = cast(FudgePtr)->getAlign(); + ConstantInt::get(*DAG.getContext(), FF), PtrVT, None, CPAlignment); // Get a pointer to FF if the sign bit was set, or to 0 otherwise. SDValue Zero = DAG.getIntPtrConstant(0, dl); @@ -36976,11 +36976,12 @@ ConstantVec[0] = const_cast(C->getConstantIntValue()); // Load the vector constant from constant pool. + Align Alignment; MVT PVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); - SDValue CP = DAG.getConstantPool(ConstantVector::get(ConstantVec), PVT); + SDValue CP = DAG.getConstantPool(ConstantVector::get(ConstantVec), PVT, + None, Alignment); MachinePointerInfo MPI = MachinePointerInfo::getConstantPool(DAG.getMachineFunction()); - Align Alignment = cast(CP)->getAlign(); return DAG.getLoad(VT, DL, DAG.getEntryNode(), CP, MPI, Alignment, MachineMemOperand::MOLoad); }