1 //===- LegalizeDAG.cpp - Implement SelectionDAG::Legalize -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the SelectionDAG::Legalize method. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ADT/APFloat.h" 14 #include "llvm/ADT/APInt.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/SetVector.h" 17 #include "llvm/ADT/SmallPtrSet.h" 18 #include "llvm/ADT/SmallSet.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/CodeGen/ISDOpcodes.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineJumpTableInfo.h" 23 #include "llvm/CodeGen/MachineMemOperand.h" 24 #include "llvm/CodeGen/RuntimeLibcalls.h" 25 #include "llvm/CodeGen/SelectionDAG.h" 26 #include "llvm/CodeGen/SelectionDAGNodes.h" 27 #include "llvm/CodeGen/TargetFrameLowering.h" 28 #include "llvm/CodeGen/TargetLowering.h" 29 #include "llvm/CodeGen/TargetSubtargetInfo.h" 30 #include "llvm/CodeGen/ValueTypes.h" 31 #include "llvm/IR/CallingConv.h" 32 #include "llvm/IR/Constants.h" 33 #include "llvm/IR/DataLayout.h" 34 #include "llvm/IR/DerivedTypes.h" 35 #include "llvm/IR/Function.h" 36 #include "llvm/IR/Metadata.h" 37 #include "llvm/IR/Type.h" 38 #include "llvm/Support/Casting.h" 39 #include "llvm/Support/Compiler.h" 40 #include "llvm/Support/Debug.h" 41 #include "llvm/Support/ErrorHandling.h" 42 #include "llvm/Support/MachineValueType.h" 43 #include "llvm/Support/MathExtras.h" 44 #include "llvm/Support/raw_ostream.h" 45 #include "llvm/Target/TargetMachine.h" 46 #include "llvm/Target/TargetOptions.h" 47 #include <algorithm> 48 #include <cassert> 49 #include <cstdint> 50 #include <tuple> 51 #include <utility> 52 53 using namespace llvm; 54 55 #define DEBUG_TYPE "legalizedag" 56 57 namespace { 58 59 /// Keeps track of state when getting the sign of a floating-point value as an 60 /// integer. 61 struct FloatSignAsInt { 62 EVT FloatVT; 63 SDValue Chain; 64 SDValue FloatPtr; 65 SDValue IntPtr; 66 MachinePointerInfo IntPointerInfo; 67 MachinePointerInfo FloatPointerInfo; 68 SDValue IntValue; 69 APInt SignMask; 70 uint8_t SignBit; 71 }; 72 73 //===----------------------------------------------------------------------===// 74 /// This takes an arbitrary SelectionDAG as input and 75 /// hacks on it until the target machine can handle it. This involves 76 /// eliminating value sizes the machine cannot handle (promoting small sizes to 77 /// large sizes or splitting up large values into small values) as well as 78 /// eliminating operations the machine cannot handle. 79 /// 80 /// This code also does a small amount of optimization and recognition of idioms 81 /// as part of its processing. For example, if a target does not support a 82 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 83 /// will attempt merge setcc and brc instructions into brcc's. 84 class SelectionDAGLegalize { 85 const TargetMachine &TM; 86 const TargetLowering &TLI; 87 SelectionDAG &DAG; 88 89 /// The set of nodes which have already been legalized. We hold a 90 /// reference to it in order to update as necessary on node deletion. 91 SmallPtrSetImpl<SDNode *> &LegalizedNodes; 92 93 /// A set of all the nodes updated during legalization. 94 SmallSetVector<SDNode *, 16> *UpdatedNodes; 95 96 EVT getSetCCResultType(EVT VT) const { 97 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 98 } 99 100 // Libcall insertion helpers. 101 102 public: 103 SelectionDAGLegalize(SelectionDAG &DAG, 104 SmallPtrSetImpl<SDNode *> &LegalizedNodes, 105 SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr) 106 : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG), 107 LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {} 108 109 /// Legalizes the given operation. 110 void LegalizeOp(SDNode *Node); 111 112 private: 113 SDValue OptimizeFloatStore(StoreSDNode *ST); 114 115 void LegalizeLoadOps(SDNode *Node); 116 void LegalizeStoreOps(SDNode *Node); 117 118 /// Some targets cannot handle a variable 119 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it 120 /// is necessary to spill the vector being inserted into to memory, perform 121 /// the insert there, and then read the result back. 122 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx, 123 const SDLoc &dl); 124 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, 125 const SDLoc &dl); 126 127 /// Return a vector shuffle operation which 128 /// performs the same shuffe in terms of order or result bytes, but on a type 129 /// whose vector element type is narrower than the original shuffle type. 130 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 131 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl, 132 SDValue N1, SDValue N2, 133 ArrayRef<int> Mask) const; 134 135 bool LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, 136 bool &NeedInvert, const SDLoc &dl); 137 138 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned); 139 140 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC, 141 SDNode *Node, bool isSigned); 142 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32, 143 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80, 144 RTLIB::Libcall Call_F128, 145 RTLIB::Libcall Call_PPCF128); 146 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, 147 RTLIB::Libcall Call_I8, 148 RTLIB::Libcall Call_I16, 149 RTLIB::Libcall Call_I32, 150 RTLIB::Libcall Call_I64, 151 RTLIB::Libcall Call_I128); 152 SDValue ExpandArgFPLibCall(SDNode *Node, 153 RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64, 154 RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128, 155 RTLIB::Libcall Call_PPCF128); 156 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 157 void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 158 159 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, 160 const SDLoc &dl); 161 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, 162 const SDLoc &dl, SDValue ChainIn); 163 SDValue ExpandBUILD_VECTOR(SDNode *Node); 164 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node); 165 void ExpandDYNAMIC_STACKALLOC(SDNode *Node, 166 SmallVectorImpl<SDValue> &Results); 167 void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL, 168 SDValue Value) const; 169 SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL, 170 SDValue NewIntValue) const; 171 SDValue ExpandFCOPYSIGN(SDNode *Node) const; 172 SDValue ExpandFABS(SDNode *Node) const; 173 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue Op0, EVT DestVT, 174 const SDLoc &dl); 175 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned, 176 const SDLoc &dl); 177 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned, 178 const SDLoc &dl); 179 180 SDValue ExpandBITREVERSE(SDValue Op, const SDLoc &dl); 181 SDValue ExpandBSWAP(SDValue Op, const SDLoc &dl); 182 183 SDValue ExpandExtractFromVectorThroughStack(SDValue Op); 184 SDValue ExpandInsertToVectorThroughStack(SDValue Op); 185 SDValue ExpandVectorBuildThroughStack(SDNode* Node); 186 187 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP); 188 SDValue ExpandConstant(ConstantSDNode *CP); 189 190 // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall 191 bool ExpandNode(SDNode *Node); 192 void ConvertNodeToLibcall(SDNode *Node); 193 void PromoteNode(SDNode *Node); 194 195 public: 196 // Node replacement helpers 197 198 void ReplacedNode(SDNode *N) { 199 LegalizedNodes.erase(N); 200 if (UpdatedNodes) 201 UpdatedNodes->insert(N); 202 } 203 204 void ReplaceNode(SDNode *Old, SDNode *New) { 205 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 206 dbgs() << " with: "; New->dump(&DAG)); 207 208 assert(Old->getNumValues() == New->getNumValues() && 209 "Replacing one node with another that produces a different number " 210 "of values!"); 211 DAG.ReplaceAllUsesWith(Old, New); 212 if (UpdatedNodes) 213 UpdatedNodes->insert(New); 214 ReplacedNode(Old); 215 } 216 217 void ReplaceNode(SDValue Old, SDValue New) { 218 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 219 dbgs() << " with: "; New->dump(&DAG)); 220 221 DAG.ReplaceAllUsesWith(Old, New); 222 if (UpdatedNodes) 223 UpdatedNodes->insert(New.getNode()); 224 ReplacedNode(Old.getNode()); 225 } 226 227 void ReplaceNode(SDNode *Old, const SDValue *New) { 228 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG)); 229 230 DAG.ReplaceAllUsesWith(Old, New); 231 for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) { 232 LLVM_DEBUG(dbgs() << (i == 0 ? " with: " : " and: "); 233 New[i]->dump(&DAG)); 234 if (UpdatedNodes) 235 UpdatedNodes->insert(New[i].getNode()); 236 } 237 ReplacedNode(Old); 238 } 239 }; 240 241 } // end anonymous namespace 242 243 /// Return a vector shuffle operation which 244 /// performs the same shuffle in terms of order or result bytes, but on a type 245 /// whose vector element type is narrower than the original shuffle type. 246 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 247 SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType( 248 EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, 249 ArrayRef<int> Mask) const { 250 unsigned NumMaskElts = VT.getVectorNumElements(); 251 unsigned NumDestElts = NVT.getVectorNumElements(); 252 unsigned NumEltsGrowth = NumDestElts / NumMaskElts; 253 254 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); 255 256 if (NumEltsGrowth == 1) 257 return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask); 258 259 SmallVector<int, 8> NewMask; 260 for (unsigned i = 0; i != NumMaskElts; ++i) { 261 int Idx = Mask[i]; 262 for (unsigned j = 0; j != NumEltsGrowth; ++j) { 263 if (Idx < 0) 264 NewMask.push_back(-1); 265 else 266 NewMask.push_back(Idx * NumEltsGrowth + j); 267 } 268 } 269 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?"); 270 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?"); 271 return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask); 272 } 273 274 /// Expands the ConstantFP node to an integer constant or 275 /// a load from the constant pool. 276 SDValue 277 SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) { 278 bool Extend = false; 279 SDLoc dl(CFP); 280 281 // If a FP immediate is precise when represented as a float and if the 282 // target can do an extending load from float to double, we put it into 283 // the constant pool as a float, even if it's is statically typed as a 284 // double. This shrinks FP constants and canonicalizes them for targets where 285 // an FP extending load is the same cost as a normal load (such as on the x87 286 // fp stack or PPC FP unit). 287 EVT VT = CFP->getValueType(0); 288 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue()); 289 if (!UseCP) { 290 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion"); 291 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl, 292 (VT == MVT::f64) ? MVT::i64 : MVT::i32); 293 } 294 295 APFloat APF = CFP->getValueAPF(); 296 EVT OrigVT = VT; 297 EVT SVT = VT; 298 299 // We don't want to shrink SNaNs. Converting the SNaN back to its real type 300 // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ). 301 if (!APF.isSignaling()) { 302 while (SVT != MVT::f32 && SVT != MVT::f16) { 303 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1); 304 if (ConstantFPSDNode::isValueValidForType(SVT, APF) && 305 // Only do this if the target has a native EXTLOAD instruction from 306 // smaller type. 307 TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) && 308 TLI.ShouldShrinkFPConstant(OrigVT)) { 309 Type *SType = SVT.getTypeForEVT(*DAG.getContext()); 310 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType)); 311 VT = SVT; 312 Extend = true; 313 } 314 } 315 } 316 317 SDValue CPIdx = 318 DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout())); 319 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 320 if (Extend) { 321 SDValue Result = DAG.getExtLoad( 322 ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx, 323 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT, 324 Alignment); 325 return Result; 326 } 327 SDValue Result = DAG.getLoad( 328 OrigVT, dl, DAG.getEntryNode(), CPIdx, 329 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment); 330 return Result; 331 } 332 333 /// Expands the Constant node to a load from the constant pool. 334 SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) { 335 SDLoc dl(CP); 336 EVT VT = CP->getValueType(0); 337 SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(), 338 TLI.getPointerTy(DAG.getDataLayout())); 339 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 340 SDValue Result = DAG.getLoad( 341 VT, dl, DAG.getEntryNode(), CPIdx, 342 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment); 343 return Result; 344 } 345 346 /// Some target cannot handle a variable insertion index for the 347 /// INSERT_VECTOR_ELT instruction. In this case, it 348 /// is necessary to spill the vector being inserted into to memory, perform 349 /// the insert there, and then read the result back. 350 SDValue SelectionDAGLegalize::PerformInsertVectorEltInMemory(SDValue Vec, 351 SDValue Val, 352 SDValue Idx, 353 const SDLoc &dl) { 354 SDValue Tmp1 = Vec; 355 SDValue Tmp2 = Val; 356 SDValue Tmp3 = Idx; 357 358 // If the target doesn't support this, we have to spill the input vector 359 // to a temporary stack slot, update the element, then reload it. This is 360 // badness. We could also load the value into a vector register (either 361 // with a "move to register" or "extload into register" instruction, then 362 // permute it into place, if the idx is a constant and if the idx is 363 // supported by the target. 364 EVT VT = Tmp1.getValueType(); 365 EVT EltVT = VT.getVectorElementType(); 366 SDValue StackPtr = DAG.CreateStackTemporary(VT); 367 368 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 369 370 // Store the vector. 371 SDValue Ch = DAG.getStore( 372 DAG.getEntryNode(), dl, Tmp1, StackPtr, 373 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 374 375 SDValue StackPtr2 = TLI.getVectorElementPointer(DAG, StackPtr, VT, Tmp3); 376 377 // Store the scalar value. 378 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT); 379 // Load the updated vector. 380 return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack( 381 DAG.getMachineFunction(), SPFI)); 382 } 383 384 SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, 385 SDValue Idx, 386 const SDLoc &dl) { 387 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) { 388 // SCALAR_TO_VECTOR requires that the type of the value being inserted 389 // match the element type of the vector being created, except for 390 // integers in which case the inserted value can be over width. 391 EVT EltVT = Vec.getValueType().getVectorElementType(); 392 if (Val.getValueType() == EltVT || 393 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) { 394 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 395 Vec.getValueType(), Val); 396 397 unsigned NumElts = Vec.getValueType().getVectorNumElements(); 398 // We generate a shuffle of InVec and ScVec, so the shuffle mask 399 // should be 0,1,2,3,4,5... with the appropriate element replaced with 400 // elt 0 of the RHS. 401 SmallVector<int, 8> ShufOps; 402 for (unsigned i = 0; i != NumElts; ++i) 403 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts); 404 405 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps); 406 } 407 } 408 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl); 409 } 410 411 SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) { 412 LLVM_DEBUG(dbgs() << "Optimizing float store operations\n"); 413 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 414 // FIXME: We shouldn't do this for TargetConstantFP's. 415 // FIXME: move this to the DAG Combiner! Note that we can't regress due 416 // to phase ordering between legalized code and the dag combiner. This 417 // probably means that we need to integrate dag combiner and legalizer 418 // together. 419 // We generally can't do this one for long doubles. 420 SDValue Chain = ST->getChain(); 421 SDValue Ptr = ST->getBasePtr(); 422 unsigned Alignment = ST->getAlignment(); 423 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 424 AAMDNodes AAInfo = ST->getAAInfo(); 425 SDLoc dl(ST); 426 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { 427 if (CFP->getValueType(0) == MVT::f32 && 428 TLI.isTypeLegal(MVT::i32)) { 429 SDValue Con = DAG.getConstant(CFP->getValueAPF(). 430 bitcastToAPInt().zextOrTrunc(32), 431 SDLoc(CFP), MVT::i32); 432 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), Alignment, 433 MMOFlags, AAInfo); 434 } 435 436 if (CFP->getValueType(0) == MVT::f64) { 437 // If this target supports 64-bit registers, do a single 64-bit store. 438 if (TLI.isTypeLegal(MVT::i64)) { 439 SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). 440 zextOrTrunc(64), SDLoc(CFP), MVT::i64); 441 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), 442 Alignment, MMOFlags, AAInfo); 443 } 444 445 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) { 446 // Otherwise, if the target supports 32-bit registers, use 2 32-bit 447 // stores. If the target supports neither 32- nor 64-bits, this 448 // xform is certainly not worth it. 449 const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt(); 450 SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32); 451 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32); 452 if (DAG.getDataLayout().isBigEndian()) 453 std::swap(Lo, Hi); 454 455 Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), Alignment, 456 MMOFlags, AAInfo); 457 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 458 DAG.getConstant(4, dl, Ptr.getValueType())); 459 Hi = DAG.getStore(Chain, dl, Hi, Ptr, 460 ST->getPointerInfo().getWithOffset(4), 461 MinAlign(Alignment, 4U), MMOFlags, AAInfo); 462 463 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 464 } 465 } 466 } 467 return SDValue(nullptr, 0); 468 } 469 470 void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) { 471 StoreSDNode *ST = cast<StoreSDNode>(Node); 472 SDValue Chain = ST->getChain(); 473 SDValue Ptr = ST->getBasePtr(); 474 SDLoc dl(Node); 475 476 unsigned Alignment = ST->getAlignment(); 477 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 478 AAMDNodes AAInfo = ST->getAAInfo(); 479 480 if (!ST->isTruncatingStore()) { 481 LLVM_DEBUG(dbgs() << "Legalizing store operation\n"); 482 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) { 483 ReplaceNode(ST, OptStore); 484 return; 485 } 486 487 SDValue Value = ST->getValue(); 488 MVT VT = Value.getSimpleValueType(); 489 switch (TLI.getOperationAction(ISD::STORE, VT)) { 490 default: llvm_unreachable("This action is not supported yet!"); 491 case TargetLowering::Legal: { 492 // If this is an unaligned store and the target doesn't support it, 493 // expand it. 494 EVT MemVT = ST->getMemoryVT(); 495 unsigned AS = ST->getAddressSpace(); 496 unsigned Align = ST->getAlignment(); 497 const DataLayout &DL = DAG.getDataLayout(); 498 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) { 499 LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n"); 500 SDValue Result = TLI.expandUnalignedStore(ST, DAG); 501 ReplaceNode(SDValue(ST, 0), Result); 502 } else 503 LLVM_DEBUG(dbgs() << "Legal store\n"); 504 break; 505 } 506 case TargetLowering::Custom: { 507 LLVM_DEBUG(dbgs() << "Trying custom lowering\n"); 508 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 509 if (Res && Res != SDValue(Node, 0)) 510 ReplaceNode(SDValue(Node, 0), Res); 511 return; 512 } 513 case TargetLowering::Promote: { 514 MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT); 515 assert(NVT.getSizeInBits() == VT.getSizeInBits() && 516 "Can only promote stores to same size type"); 517 Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value); 518 SDValue Result = 519 DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 520 Alignment, MMOFlags, AAInfo); 521 ReplaceNode(SDValue(Node, 0), Result); 522 break; 523 } 524 } 525 return; 526 } 527 528 LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n"); 529 SDValue Value = ST->getValue(); 530 EVT StVT = ST->getMemoryVT(); 531 unsigned StWidth = StVT.getSizeInBits(); 532 auto &DL = DAG.getDataLayout(); 533 534 if (StWidth != StVT.getStoreSizeInBits()) { 535 // Promote to a byte-sized store with upper bits zero if not 536 // storing an integral number of bytes. For example, promote 537 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) 538 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), 539 StVT.getStoreSizeInBits()); 540 Value = DAG.getZeroExtendInReg(Value, dl, StVT); 541 SDValue Result = 542 DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT, 543 Alignment, MMOFlags, AAInfo); 544 ReplaceNode(SDValue(Node, 0), Result); 545 } else if (StWidth & (StWidth - 1)) { 546 // If not storing a power-of-2 number of bits, expand as two stores. 547 assert(!StVT.isVector() && "Unsupported truncstore!"); 548 unsigned LogStWidth = Log2_32(StWidth); 549 assert(LogStWidth < 32); 550 unsigned RoundWidth = 1 << LogStWidth; 551 assert(RoundWidth < StWidth); 552 unsigned ExtraWidth = StWidth - RoundWidth; 553 assert(ExtraWidth < RoundWidth); 554 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 555 "Store size not an integral number of bytes!"); 556 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 557 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 558 SDValue Lo, Hi; 559 unsigned IncrementSize; 560 561 if (DL.isLittleEndian()) { 562 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) 563 // Store the bottom RoundWidth bits. 564 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 565 RoundVT, Alignment, MMOFlags, AAInfo); 566 567 // Store the remaining ExtraWidth bits. 568 IncrementSize = RoundWidth / 8; 569 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 570 DAG.getConstant(IncrementSize, dl, 571 Ptr.getValueType())); 572 Hi = DAG.getNode( 573 ISD::SRL, dl, Value.getValueType(), Value, 574 DAG.getConstant(RoundWidth, dl, 575 TLI.getShiftAmountTy(Value.getValueType(), DL))); 576 Hi = DAG.getTruncStore( 577 Chain, dl, Hi, Ptr, 578 ST->getPointerInfo().getWithOffset(IncrementSize), ExtraVT, 579 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo); 580 } else { 581 // Big endian - avoid unaligned stores. 582 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X 583 // Store the top RoundWidth bits. 584 Hi = DAG.getNode( 585 ISD::SRL, dl, Value.getValueType(), Value, 586 DAG.getConstant(ExtraWidth, dl, 587 TLI.getShiftAmountTy(Value.getValueType(), DL))); 588 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), 589 RoundVT, Alignment, MMOFlags, AAInfo); 590 591 // Store the remaining ExtraWidth bits. 592 IncrementSize = RoundWidth / 8; 593 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 594 DAG.getConstant(IncrementSize, dl, 595 Ptr.getValueType())); 596 Lo = DAG.getTruncStore( 597 Chain, dl, Value, Ptr, 598 ST->getPointerInfo().getWithOffset(IncrementSize), ExtraVT, 599 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo); 600 } 601 602 // The order of the stores doesn't matter. 603 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 604 ReplaceNode(SDValue(Node, 0), Result); 605 } else { 606 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { 607 default: llvm_unreachable("This action is not supported yet!"); 608 case TargetLowering::Legal: { 609 EVT MemVT = ST->getMemoryVT(); 610 unsigned AS = ST->getAddressSpace(); 611 unsigned Align = ST->getAlignment(); 612 // If this is an unaligned store and the target doesn't support it, 613 // expand it. 614 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) { 615 SDValue Result = TLI.expandUnalignedStore(ST, DAG); 616 ReplaceNode(SDValue(ST, 0), Result); 617 } 618 break; 619 } 620 case TargetLowering::Custom: { 621 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 622 if (Res && Res != SDValue(Node, 0)) 623 ReplaceNode(SDValue(Node, 0), Res); 624 return; 625 } 626 case TargetLowering::Expand: 627 assert(!StVT.isVector() && 628 "Vector Stores are handled in LegalizeVectorOps"); 629 630 SDValue Result; 631 632 // TRUNCSTORE:i16 i32 -> STORE i16 633 if (TLI.isTypeLegal(StVT)) { 634 Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value); 635 Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 636 Alignment, MMOFlags, AAInfo); 637 } else { 638 // The in-memory type isn't legal. Truncate to the type it would promote 639 // to, and then do a truncstore. 640 Value = DAG.getNode(ISD::TRUNCATE, dl, 641 TLI.getTypeToTransformTo(*DAG.getContext(), StVT), 642 Value); 643 Result = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 644 StVT, Alignment, MMOFlags, AAInfo); 645 } 646 647 ReplaceNode(SDValue(Node, 0), Result); 648 break; 649 } 650 } 651 } 652 653 void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) { 654 LoadSDNode *LD = cast<LoadSDNode>(Node); 655 SDValue Chain = LD->getChain(); // The chain. 656 SDValue Ptr = LD->getBasePtr(); // The base pointer. 657 SDValue Value; // The value returned by the load op. 658 SDLoc dl(Node); 659 660 ISD::LoadExtType ExtType = LD->getExtensionType(); 661 if (ExtType == ISD::NON_EXTLOAD) { 662 LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n"); 663 MVT VT = Node->getSimpleValueType(0); 664 SDValue RVal = SDValue(Node, 0); 665 SDValue RChain = SDValue(Node, 1); 666 667 switch (TLI.getOperationAction(Node->getOpcode(), VT)) { 668 default: llvm_unreachable("This action is not supported yet!"); 669 case TargetLowering::Legal: { 670 EVT MemVT = LD->getMemoryVT(); 671 unsigned AS = LD->getAddressSpace(); 672 unsigned Align = LD->getAlignment(); 673 const DataLayout &DL = DAG.getDataLayout(); 674 // If this is an unaligned load and the target doesn't support it, 675 // expand it. 676 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) { 677 std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG); 678 } 679 break; 680 } 681 case TargetLowering::Custom: 682 if (SDValue Res = TLI.LowerOperation(RVal, DAG)) { 683 RVal = Res; 684 RChain = Res.getValue(1); 685 } 686 break; 687 688 case TargetLowering::Promote: { 689 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 690 assert(NVT.getSizeInBits() == VT.getSizeInBits() && 691 "Can only promote loads to same size type"); 692 693 SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand()); 694 RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res); 695 RChain = Res.getValue(1); 696 break; 697 } 698 } 699 if (RChain.getNode() != Node) { 700 assert(RVal.getNode() != Node && "Load must be completely replaced"); 701 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal); 702 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain); 703 if (UpdatedNodes) { 704 UpdatedNodes->insert(RVal.getNode()); 705 UpdatedNodes->insert(RChain.getNode()); 706 } 707 ReplacedNode(Node); 708 } 709 return; 710 } 711 712 LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n"); 713 EVT SrcVT = LD->getMemoryVT(); 714 unsigned SrcWidth = SrcVT.getSizeInBits(); 715 unsigned Alignment = LD->getAlignment(); 716 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags(); 717 AAMDNodes AAInfo = LD->getAAInfo(); 718 719 if (SrcWidth != SrcVT.getStoreSizeInBits() && 720 // Some targets pretend to have an i1 loading operation, and actually 721 // load an i8. This trick is correct for ZEXTLOAD because the top 7 722 // bits are guaranteed to be zero; it helps the optimizers understand 723 // that these bits are zero. It is also useful for EXTLOAD, since it 724 // tells the optimizers that those bits are undefined. It would be 725 // nice to have an effective generic way of getting these benefits... 726 // Until such a way is found, don't insist on promoting i1 here. 727 (SrcVT != MVT::i1 || 728 TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) == 729 TargetLowering::Promote)) { 730 // Promote to a byte-sized load if not loading an integral number of 731 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. 732 unsigned NewWidth = SrcVT.getStoreSizeInBits(); 733 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth); 734 SDValue Ch; 735 736 // The extra bits are guaranteed to be zero, since we stored them that 737 // way. A zext load from NVT thus automatically gives zext from SrcVT. 738 739 ISD::LoadExtType NewExtType = 740 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; 741 742 SDValue Result = 743 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), Chain, Ptr, 744 LD->getPointerInfo(), NVT, Alignment, MMOFlags, AAInfo); 745 746 Ch = Result.getValue(1); // The chain. 747 748 if (ExtType == ISD::SEXTLOAD) 749 // Having the top bits zero doesn't help when sign extending. 750 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 751 Result.getValueType(), 752 Result, DAG.getValueType(SrcVT)); 753 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) 754 // All the top bits are guaranteed to be zero - inform the optimizers. 755 Result = DAG.getNode(ISD::AssertZext, dl, 756 Result.getValueType(), Result, 757 DAG.getValueType(SrcVT)); 758 759 Value = Result; 760 Chain = Ch; 761 } else if (SrcWidth & (SrcWidth - 1)) { 762 // If not loading a power-of-2 number of bits, expand as two loads. 763 assert(!SrcVT.isVector() && "Unsupported extload!"); 764 unsigned LogSrcWidth = Log2_32(SrcWidth); 765 assert(LogSrcWidth < 32); 766 unsigned RoundWidth = 1 << LogSrcWidth; 767 assert(RoundWidth < SrcWidth); 768 unsigned ExtraWidth = SrcWidth - RoundWidth; 769 assert(ExtraWidth < RoundWidth); 770 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 771 "Load size not an integral number of bytes!"); 772 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 773 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 774 SDValue Lo, Hi, Ch; 775 unsigned IncrementSize; 776 auto &DL = DAG.getDataLayout(); 777 778 if (DL.isLittleEndian()) { 779 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) 780 // Load the bottom RoundWidth bits. 781 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr, 782 LD->getPointerInfo(), RoundVT, Alignment, MMOFlags, 783 AAInfo); 784 785 // Load the remaining ExtraWidth bits. 786 IncrementSize = RoundWidth / 8; 787 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 788 DAG.getConstant(IncrementSize, dl, 789 Ptr.getValueType())); 790 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 791 LD->getPointerInfo().getWithOffset(IncrementSize), 792 ExtraVT, MinAlign(Alignment, IncrementSize), MMOFlags, 793 AAInfo); 794 795 // Build a factor node to remember that this load is independent of 796 // the other one. 797 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 798 Hi.getValue(1)); 799 800 // Move the top bits to the right place. 801 Hi = DAG.getNode( 802 ISD::SHL, dl, Hi.getValueType(), Hi, 803 DAG.getConstant(RoundWidth, dl, 804 TLI.getShiftAmountTy(Hi.getValueType(), DL))); 805 806 // Join the hi and lo parts. 807 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 808 } else { 809 // Big endian - avoid unaligned loads. 810 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 811 // Load the top RoundWidth bits. 812 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 813 LD->getPointerInfo(), RoundVT, Alignment, MMOFlags, 814 AAInfo); 815 816 // Load the remaining ExtraWidth bits. 817 IncrementSize = RoundWidth / 8; 818 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 819 DAG.getConstant(IncrementSize, dl, 820 Ptr.getValueType())); 821 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr, 822 LD->getPointerInfo().getWithOffset(IncrementSize), 823 ExtraVT, MinAlign(Alignment, IncrementSize), MMOFlags, 824 AAInfo); 825 826 // Build a factor node to remember that this load is independent of 827 // the other one. 828 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 829 Hi.getValue(1)); 830 831 // Move the top bits to the right place. 832 Hi = DAG.getNode( 833 ISD::SHL, dl, Hi.getValueType(), Hi, 834 DAG.getConstant(ExtraWidth, dl, 835 TLI.getShiftAmountTy(Hi.getValueType(), DL))); 836 837 // Join the hi and lo parts. 838 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 839 } 840 841 Chain = Ch; 842 } else { 843 bool isCustom = false; 844 switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0), 845 SrcVT.getSimpleVT())) { 846 default: llvm_unreachable("This action is not supported yet!"); 847 case TargetLowering::Custom: 848 isCustom = true; 849 LLVM_FALLTHROUGH; 850 case TargetLowering::Legal: 851 Value = SDValue(Node, 0); 852 Chain = SDValue(Node, 1); 853 854 if (isCustom) { 855 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 856 Value = Res; 857 Chain = Res.getValue(1); 858 } 859 } else { 860 // If this is an unaligned load and the target doesn't support it, 861 // expand it. 862 EVT MemVT = LD->getMemoryVT(); 863 unsigned AS = LD->getAddressSpace(); 864 unsigned Align = LD->getAlignment(); 865 const DataLayout &DL = DAG.getDataLayout(); 866 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) { 867 std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG); 868 } 869 } 870 break; 871 872 case TargetLowering::Expand: { 873 EVT DestVT = Node->getValueType(0); 874 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) { 875 // If the source type is not legal, see if there is a legal extload to 876 // an intermediate type that we can then extend further. 877 EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT()); 878 if (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT? 879 TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT)) { 880 // If we are loading a legal type, this is a non-extload followed by a 881 // full extend. 882 ISD::LoadExtType MidExtType = 883 (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType; 884 885 SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr, 886 SrcVT, LD->getMemOperand()); 887 unsigned ExtendOp = 888 ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType); 889 Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load); 890 Chain = Load.getValue(1); 891 break; 892 } 893 894 // Handle the special case of fp16 extloads. EXTLOAD doesn't have the 895 // normal undefined upper bits behavior to allow using an in-reg extend 896 // with the illegal FP type, so load as an integer and do the 897 // from-integer conversion. 898 if (SrcVT.getScalarType() == MVT::f16) { 899 EVT ISrcVT = SrcVT.changeTypeToInteger(); 900 EVT IDestVT = DestVT.changeTypeToInteger(); 901 EVT LoadVT = TLI.getRegisterType(IDestVT.getSimpleVT()); 902 903 SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, LoadVT, 904 Chain, Ptr, ISrcVT, 905 LD->getMemOperand()); 906 Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result); 907 Chain = Result.getValue(1); 908 break; 909 } 910 } 911 912 assert(!SrcVT.isVector() && 913 "Vector Loads are handled in LegalizeVectorOps"); 914 915 // FIXME: This does not work for vectors on most targets. Sign- 916 // and zero-extend operations are currently folded into extending 917 // loads, whether they are legal or not, and then we end up here 918 // without any support for legalizing them. 919 assert(ExtType != ISD::EXTLOAD && 920 "EXTLOAD should always be supported!"); 921 // Turn the unsupported load into an EXTLOAD followed by an 922 // explicit zero/sign extend inreg. 923 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, 924 Node->getValueType(0), 925 Chain, Ptr, SrcVT, 926 LD->getMemOperand()); 927 SDValue ValRes; 928 if (ExtType == ISD::SEXTLOAD) 929 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 930 Result.getValueType(), 931 Result, DAG.getValueType(SrcVT)); 932 else 933 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType()); 934 Value = ValRes; 935 Chain = Result.getValue(1); 936 break; 937 } 938 } 939 } 940 941 // Since loads produce two values, make sure to remember that we legalized 942 // both of them. 943 if (Chain.getNode() != Node) { 944 assert(Value.getNode() != Node && "Load must be completely replaced"); 945 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value); 946 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 947 if (UpdatedNodes) { 948 UpdatedNodes->insert(Value.getNode()); 949 UpdatedNodes->insert(Chain.getNode()); 950 } 951 ReplacedNode(Node); 952 } 953 } 954 955 /// Return a legal replacement for the given operation, with all legal operands. 956 void SelectionDAGLegalize::LegalizeOp(SDNode *Node) { 957 LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG)); 958 959 // Allow illegal target nodes and illegal registers. 960 if (Node->getOpcode() == ISD::TargetConstant || 961 Node->getOpcode() == ISD::Register) 962 return; 963 964 #ifndef NDEBUG 965 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 966 assert((TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) == 967 TargetLowering::TypeLegal || 968 TLI.isTypeLegal(Node->getValueType(i))) && 969 "Unexpected illegal type!"); 970 971 for (const SDValue &Op : Node->op_values()) 972 assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) == 973 TargetLowering::TypeLegal || 974 TLI.isTypeLegal(Op.getValueType()) || 975 Op.getOpcode() == ISD::TargetConstant || 976 Op.getOpcode() == ISD::Register) && 977 "Unexpected illegal type!"); 978 #endif 979 980 // Figure out the correct action; the way to query this varies by opcode 981 TargetLowering::LegalizeAction Action = TargetLowering::Legal; 982 bool SimpleFinishLegalizing = true; 983 switch (Node->getOpcode()) { 984 case ISD::INTRINSIC_W_CHAIN: 985 case ISD::INTRINSIC_WO_CHAIN: 986 case ISD::INTRINSIC_VOID: 987 case ISD::STACKSAVE: 988 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 989 break; 990 case ISD::GET_DYNAMIC_AREA_OFFSET: 991 Action = TLI.getOperationAction(Node->getOpcode(), 992 Node->getValueType(0)); 993 break; 994 case ISD::VAARG: 995 Action = TLI.getOperationAction(Node->getOpcode(), 996 Node->getValueType(0)); 997 if (Action != TargetLowering::Promote) 998 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 999 break; 1000 case ISD::FP_TO_FP16: 1001 case ISD::SINT_TO_FP: 1002 case ISD::UINT_TO_FP: 1003 case ISD::EXTRACT_VECTOR_ELT: 1004 case ISD::LROUND: 1005 case ISD::LLROUND: 1006 case ISD::LRINT: 1007 case ISD::LLRINT: 1008 Action = TLI.getOperationAction(Node->getOpcode(), 1009 Node->getOperand(0).getValueType()); 1010 break; 1011 case ISD::FP_ROUND_INREG: 1012 case ISD::SIGN_EXTEND_INREG: { 1013 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT(); 1014 Action = TLI.getOperationAction(Node->getOpcode(), InnerType); 1015 break; 1016 } 1017 case ISD::ATOMIC_STORE: 1018 Action = TLI.getOperationAction(Node->getOpcode(), 1019 Node->getOperand(2).getValueType()); 1020 break; 1021 case ISD::SELECT_CC: 1022 case ISD::SETCC: 1023 case ISD::BR_CC: { 1024 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 : 1025 Node->getOpcode() == ISD::SETCC ? 2 : 1; 1026 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0; 1027 MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType(); 1028 ISD::CondCode CCCode = 1029 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get(); 1030 Action = TLI.getCondCodeAction(CCCode, OpVT); 1031 if (Action == TargetLowering::Legal) { 1032 if (Node->getOpcode() == ISD::SELECT_CC) 1033 Action = TLI.getOperationAction(Node->getOpcode(), 1034 Node->getValueType(0)); 1035 else 1036 Action = TLI.getOperationAction(Node->getOpcode(), OpVT); 1037 } 1038 break; 1039 } 1040 case ISD::LOAD: 1041 case ISD::STORE: 1042 // FIXME: Model these properly. LOAD and STORE are complicated, and 1043 // STORE expects the unlegalized operand in some cases. 1044 SimpleFinishLegalizing = false; 1045 break; 1046 case ISD::CALLSEQ_START: 1047 case ISD::CALLSEQ_END: 1048 // FIXME: This shouldn't be necessary. These nodes have special properties 1049 // dealing with the recursive nature of legalization. Removing this 1050 // special case should be done as part of making LegalizeDAG non-recursive. 1051 SimpleFinishLegalizing = false; 1052 break; 1053 case ISD::EXTRACT_ELEMENT: 1054 case ISD::FLT_ROUNDS_: 1055 case ISD::MERGE_VALUES: 1056 case ISD::EH_RETURN: 1057 case ISD::FRAME_TO_ARGS_OFFSET: 1058 case ISD::EH_DWARF_CFA: 1059 case ISD::EH_SJLJ_SETJMP: 1060 case ISD::EH_SJLJ_LONGJMP: 1061 case ISD::EH_SJLJ_SETUP_DISPATCH: 1062 // These operations lie about being legal: when they claim to be legal, 1063 // they should actually be expanded. 1064 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1065 if (Action == TargetLowering::Legal) 1066 Action = TargetLowering::Expand; 1067 break; 1068 case ISD::INIT_TRAMPOLINE: 1069 case ISD::ADJUST_TRAMPOLINE: 1070 case ISD::FRAMEADDR: 1071 case ISD::RETURNADDR: 1072 case ISD::ADDROFRETURNADDR: 1073 case ISD::SPONENTRY: 1074 // These operations lie about being legal: when they claim to be legal, 1075 // they should actually be custom-lowered. 1076 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1077 if (Action == TargetLowering::Legal) 1078 Action = TargetLowering::Custom; 1079 break; 1080 case ISD::READCYCLECOUNTER: 1081 // READCYCLECOUNTER returns an i64, even if type legalization might have 1082 // expanded that to several smaller types. 1083 Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64); 1084 break; 1085 case ISD::READ_REGISTER: 1086 case ISD::WRITE_REGISTER: 1087 // Named register is legal in the DAG, but blocked by register name 1088 // selection if not implemented by target (to chose the correct register) 1089 // They'll be converted to Copy(To/From)Reg. 1090 Action = TargetLowering::Legal; 1091 break; 1092 case ISD::DEBUGTRAP: 1093 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1094 if (Action == TargetLowering::Expand) { 1095 // replace ISD::DEBUGTRAP with ISD::TRAP 1096 SDValue NewVal; 1097 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(), 1098 Node->getOperand(0)); 1099 ReplaceNode(Node, NewVal.getNode()); 1100 LegalizeOp(NewVal.getNode()); 1101 return; 1102 } 1103 break; 1104 case ISD::STRICT_FADD: 1105 case ISD::STRICT_FSUB: 1106 case ISD::STRICT_FMUL: 1107 case ISD::STRICT_FDIV: 1108 case ISD::STRICT_FREM: 1109 case ISD::STRICT_FSQRT: 1110 case ISD::STRICT_FMA: 1111 case ISD::STRICT_FPOW: 1112 case ISD::STRICT_FPOWI: 1113 case ISD::STRICT_FSIN: 1114 case ISD::STRICT_FCOS: 1115 case ISD::STRICT_FEXP: 1116 case ISD::STRICT_FEXP2: 1117 case ISD::STRICT_FLOG: 1118 case ISD::STRICT_FLOG10: 1119 case ISD::STRICT_FLOG2: 1120 case ISD::STRICT_FRINT: 1121 case ISD::STRICT_FNEARBYINT: 1122 case ISD::STRICT_FMAXNUM: 1123 case ISD::STRICT_FMINNUM: 1124 case ISD::STRICT_FCEIL: 1125 case ISD::STRICT_FFLOOR: 1126 case ISD::STRICT_FROUND: 1127 case ISD::STRICT_FTRUNC: 1128 case ISD::STRICT_FP_ROUND: 1129 case ISD::STRICT_FP_EXTEND: 1130 // These pseudo-ops get legalized as if they were their non-strict 1131 // equivalent. For instance, if ISD::FSQRT is legal then ISD::STRICT_FSQRT 1132 // is also legal, but if ISD::FSQRT requires expansion then so does 1133 // ISD::STRICT_FSQRT. 1134 Action = TLI.getStrictFPOperationAction(Node->getOpcode(), 1135 Node->getValueType(0)); 1136 break; 1137 case ISD::SADDSAT: 1138 case ISD::UADDSAT: 1139 case ISD::SSUBSAT: 1140 case ISD::USUBSAT: { 1141 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1142 break; 1143 } 1144 case ISD::SMULFIX: 1145 case ISD::SMULFIXSAT: 1146 case ISD::UMULFIX: { 1147 unsigned Scale = Node->getConstantOperandVal(2); 1148 Action = TLI.getFixedPointOperationAction(Node->getOpcode(), 1149 Node->getValueType(0), Scale); 1150 break; 1151 } 1152 case ISD::MSCATTER: 1153 Action = TLI.getOperationAction(Node->getOpcode(), 1154 cast<MaskedScatterSDNode>(Node)->getValue().getValueType()); 1155 break; 1156 case ISD::MSTORE: 1157 Action = TLI.getOperationAction(Node->getOpcode(), 1158 cast<MaskedStoreSDNode>(Node)->getValue().getValueType()); 1159 break; 1160 case ISD::VECREDUCE_FADD: 1161 case ISD::VECREDUCE_FMUL: 1162 case ISD::VECREDUCE_ADD: 1163 case ISD::VECREDUCE_MUL: 1164 case ISD::VECREDUCE_AND: 1165 case ISD::VECREDUCE_OR: 1166 case ISD::VECREDUCE_XOR: 1167 case ISD::VECREDUCE_SMAX: 1168 case ISD::VECREDUCE_SMIN: 1169 case ISD::VECREDUCE_UMAX: 1170 case ISD::VECREDUCE_UMIN: 1171 case ISD::VECREDUCE_FMAX: 1172 case ISD::VECREDUCE_FMIN: 1173 Action = TLI.getOperationAction( 1174 Node->getOpcode(), Node->getOperand(0).getValueType()); 1175 break; 1176 default: 1177 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 1178 Action = TargetLowering::Legal; 1179 } else { 1180 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1181 } 1182 break; 1183 } 1184 1185 if (SimpleFinishLegalizing) { 1186 SDNode *NewNode = Node; 1187 switch (Node->getOpcode()) { 1188 default: break; 1189 case ISD::SHL: 1190 case ISD::SRL: 1191 case ISD::SRA: 1192 case ISD::ROTL: 1193 case ISD::ROTR: { 1194 // Legalizing shifts/rotates requires adjusting the shift amount 1195 // to the appropriate width. 1196 SDValue Op0 = Node->getOperand(0); 1197 SDValue Op1 = Node->getOperand(1); 1198 if (!Op1.getValueType().isVector()) { 1199 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1); 1200 // The getShiftAmountOperand() may create a new operand node or 1201 // return the existing one. If new operand is created we need 1202 // to update the parent node. 1203 // Do not try to legalize SAO here! It will be automatically legalized 1204 // in the next round. 1205 if (SAO != Op1) 1206 NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO); 1207 } 1208 } 1209 break; 1210 case ISD::FSHL: 1211 case ISD::FSHR: 1212 case ISD::SRL_PARTS: 1213 case ISD::SRA_PARTS: 1214 case ISD::SHL_PARTS: { 1215 // Legalizing shifts/rotates requires adjusting the shift amount 1216 // to the appropriate width. 1217 SDValue Op0 = Node->getOperand(0); 1218 SDValue Op1 = Node->getOperand(1); 1219 SDValue Op2 = Node->getOperand(2); 1220 if (!Op2.getValueType().isVector()) { 1221 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2); 1222 // The getShiftAmountOperand() may create a new operand node or 1223 // return the existing one. If new operand is created we need 1224 // to update the parent node. 1225 if (SAO != Op2) 1226 NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO); 1227 } 1228 break; 1229 } 1230 } 1231 1232 if (NewNode != Node) { 1233 ReplaceNode(Node, NewNode); 1234 Node = NewNode; 1235 } 1236 switch (Action) { 1237 case TargetLowering::Legal: 1238 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n"); 1239 return; 1240 case TargetLowering::Custom: 1241 LLVM_DEBUG(dbgs() << "Trying custom legalization\n"); 1242 // FIXME: The handling for custom lowering with multiple results is 1243 // a complete mess. 1244 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 1245 if (!(Res.getNode() != Node || Res.getResNo() != 0)) 1246 return; 1247 1248 if (Node->getNumValues() == 1) { 1249 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 1250 // We can just directly replace this node with the lowered value. 1251 ReplaceNode(SDValue(Node, 0), Res); 1252 return; 1253 } 1254 1255 SmallVector<SDValue, 8> ResultVals; 1256 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 1257 ResultVals.push_back(Res.getValue(i)); 1258 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 1259 ReplaceNode(Node, ResultVals.data()); 1260 return; 1261 } 1262 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n"); 1263 LLVM_FALLTHROUGH; 1264 case TargetLowering::Expand: 1265 if (ExpandNode(Node)) 1266 return; 1267 LLVM_FALLTHROUGH; 1268 case TargetLowering::LibCall: 1269 ConvertNodeToLibcall(Node); 1270 return; 1271 case TargetLowering::Promote: 1272 PromoteNode(Node); 1273 return; 1274 } 1275 } 1276 1277 switch (Node->getOpcode()) { 1278 default: 1279 #ifndef NDEBUG 1280 dbgs() << "NODE: "; 1281 Node->dump( &DAG); 1282 dbgs() << "\n"; 1283 #endif 1284 llvm_unreachable("Do not know how to legalize this operator!"); 1285 1286 case ISD::CALLSEQ_START: 1287 case ISD::CALLSEQ_END: 1288 break; 1289 case ISD::LOAD: 1290 return LegalizeLoadOps(Node); 1291 case ISD::STORE: 1292 return LegalizeStoreOps(Node); 1293 } 1294 } 1295 1296 SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { 1297 SDValue Vec = Op.getOperand(0); 1298 SDValue Idx = Op.getOperand(1); 1299 SDLoc dl(Op); 1300 1301 // Before we generate a new store to a temporary stack slot, see if there is 1302 // already one that we can use. There often is because when we scalarize 1303 // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole 1304 // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in 1305 // the vector. If all are expanded here, we don't want one store per vector 1306 // element. 1307 1308 // Caches for hasPredecessorHelper 1309 SmallPtrSet<const SDNode *, 32> Visited; 1310 SmallVector<const SDNode *, 16> Worklist; 1311 Visited.insert(Op.getNode()); 1312 Worklist.push_back(Idx.getNode()); 1313 SDValue StackPtr, Ch; 1314 for (SDNode::use_iterator UI = Vec.getNode()->use_begin(), 1315 UE = Vec.getNode()->use_end(); UI != UE; ++UI) { 1316 SDNode *User = *UI; 1317 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) { 1318 if (ST->isIndexed() || ST->isTruncatingStore() || 1319 ST->getValue() != Vec) 1320 continue; 1321 1322 // Make sure that nothing else could have stored into the destination of 1323 // this store. 1324 if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode())) 1325 continue; 1326 1327 // If the index is dependent on the store we will introduce a cycle when 1328 // creating the load (the load uses the index, and by replacing the chain 1329 // we will make the index dependent on the load). Also, the store might be 1330 // dependent on the extractelement and introduce a cycle when creating 1331 // the load. 1332 if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) || 1333 ST->hasPredecessor(Op.getNode())) 1334 continue; 1335 1336 StackPtr = ST->getBasePtr(); 1337 Ch = SDValue(ST, 0); 1338 break; 1339 } 1340 } 1341 1342 EVT VecVT = Vec.getValueType(); 1343 1344 if (!Ch.getNode()) { 1345 // Store the value to a temporary stack slot, then LOAD the returned part. 1346 StackPtr = DAG.CreateStackTemporary(VecVT); 1347 Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, 1348 MachinePointerInfo()); 1349 } 1350 1351 StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx); 1352 1353 SDValue NewLoad; 1354 1355 if (Op.getValueType().isVector()) 1356 NewLoad = 1357 DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, MachinePointerInfo()); 1358 else 1359 NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, 1360 MachinePointerInfo(), 1361 VecVT.getVectorElementType()); 1362 1363 // Replace the chain going out of the store, by the one out of the load. 1364 DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1)); 1365 1366 // We introduced a cycle though, so update the loads operands, making sure 1367 // to use the original store's chain as an incoming chain. 1368 SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(), 1369 NewLoad->op_end()); 1370 NewLoadOperands[0] = Ch; 1371 NewLoad = 1372 SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0); 1373 return NewLoad; 1374 } 1375 1376 SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) { 1377 assert(Op.getValueType().isVector() && "Non-vector insert subvector!"); 1378 1379 SDValue Vec = Op.getOperand(0); 1380 SDValue Part = Op.getOperand(1); 1381 SDValue Idx = Op.getOperand(2); 1382 SDLoc dl(Op); 1383 1384 // Store the value to a temporary stack slot, then LOAD the returned part. 1385 EVT VecVT = Vec.getValueType(); 1386 SDValue StackPtr = DAG.CreateStackTemporary(VecVT); 1387 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 1388 MachinePointerInfo PtrInfo = 1389 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 1390 1391 // First store the whole vector. 1392 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo); 1393 1394 // Then store the inserted part. 1395 SDValue SubStackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx); 1396 1397 // Store the subvector. 1398 Ch = DAG.getStore(Ch, dl, Part, SubStackPtr, MachinePointerInfo()); 1399 1400 // Finally, load the updated vector. 1401 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo); 1402 } 1403 1404 SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) { 1405 // We can't handle this case efficiently. Allocate a sufficiently 1406 // aligned object on the stack, store each element into it, then load 1407 // the result as a vector. 1408 // Create the stack frame object. 1409 EVT VT = Node->getValueType(0); 1410 EVT EltVT = VT.getVectorElementType(); 1411 SDLoc dl(Node); 1412 SDValue FIPtr = DAG.CreateStackTemporary(VT); 1413 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); 1414 MachinePointerInfo PtrInfo = 1415 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 1416 1417 // Emit a store of each element to the stack slot. 1418 SmallVector<SDValue, 8> Stores; 1419 unsigned TypeByteSize = EltVT.getSizeInBits() / 8; 1420 assert(TypeByteSize > 0 && "Vector element type too small for stack store!"); 1421 // Store (in the right endianness) the elements to memory. 1422 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 1423 // Ignore undef elements. 1424 if (Node->getOperand(i).isUndef()) continue; 1425 1426 unsigned Offset = TypeByteSize*i; 1427 1428 SDValue Idx = DAG.getConstant(Offset, dl, FIPtr.getValueType()); 1429 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx); 1430 1431 // If the destination vector element type is narrower than the source 1432 // element type, only store the bits necessary. 1433 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) { 1434 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, 1435 Node->getOperand(i), Idx, 1436 PtrInfo.getWithOffset(Offset), EltVT)); 1437 } else 1438 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i), 1439 Idx, PtrInfo.getWithOffset(Offset))); 1440 } 1441 1442 SDValue StoreChain; 1443 if (!Stores.empty()) // Not all undef elements? 1444 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 1445 else 1446 StoreChain = DAG.getEntryNode(); 1447 1448 // Result is a load from the stack slot. 1449 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo); 1450 } 1451 1452 /// Bitcast a floating-point value to an integer value. Only bitcast the part 1453 /// containing the sign bit if the target has no integer value capable of 1454 /// holding all bits of the floating-point value. 1455 void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State, 1456 const SDLoc &DL, 1457 SDValue Value) const { 1458 EVT FloatVT = Value.getValueType(); 1459 unsigned NumBits = FloatVT.getSizeInBits(); 1460 State.FloatVT = FloatVT; 1461 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits); 1462 // Convert to an integer of the same size. 1463 if (TLI.isTypeLegal(IVT)) { 1464 State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value); 1465 State.SignMask = APInt::getSignMask(NumBits); 1466 State.SignBit = NumBits - 1; 1467 return; 1468 } 1469 1470 auto &DataLayout = DAG.getDataLayout(); 1471 // Store the float to memory, then load the sign part out as an integer. 1472 MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8); 1473 // First create a temporary that is aligned for both the load and store. 1474 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy); 1475 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 1476 // Then store the float to it. 1477 State.FloatPtr = StackPtr; 1478 MachineFunction &MF = DAG.getMachineFunction(); 1479 State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI); 1480 State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr, 1481 State.FloatPointerInfo); 1482 1483 SDValue IntPtr; 1484 if (DataLayout.isBigEndian()) { 1485 assert(FloatVT.isByteSized() && "Unsupported floating point type!"); 1486 // Load out a legal integer with the same sign bit as the float. 1487 IntPtr = StackPtr; 1488 State.IntPointerInfo = State.FloatPointerInfo; 1489 } else { 1490 // Advance the pointer so that the loaded byte will contain the sign bit. 1491 unsigned ByteOffset = (FloatVT.getSizeInBits() / 8) - 1; 1492 IntPtr = DAG.getNode(ISD::ADD, DL, StackPtr.getValueType(), StackPtr, 1493 DAG.getConstant(ByteOffset, DL, StackPtr.getValueType())); 1494 State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI, 1495 ByteOffset); 1496 } 1497 1498 State.IntPtr = IntPtr; 1499 State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr, 1500 State.IntPointerInfo, MVT::i8); 1501 State.SignMask = APInt::getOneBitSet(LoadTy.getSizeInBits(), 7); 1502 State.SignBit = 7; 1503 } 1504 1505 /// Replace the integer value produced by getSignAsIntValue() with a new value 1506 /// and cast the result back to a floating-point type. 1507 SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State, 1508 const SDLoc &DL, 1509 SDValue NewIntValue) const { 1510 if (!State.Chain) 1511 return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue); 1512 1513 // Override the part containing the sign bit in the value stored on the stack. 1514 SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr, 1515 State.IntPointerInfo, MVT::i8); 1516 return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr, 1517 State.FloatPointerInfo); 1518 } 1519 1520 SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const { 1521 SDLoc DL(Node); 1522 SDValue Mag = Node->getOperand(0); 1523 SDValue Sign = Node->getOperand(1); 1524 1525 // Get sign bit into an integer value. 1526 FloatSignAsInt SignAsInt; 1527 getSignAsIntValue(SignAsInt, DL, Sign); 1528 1529 EVT IntVT = SignAsInt.IntValue.getValueType(); 1530 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 1531 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue, 1532 SignMask); 1533 1534 // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X) 1535 EVT FloatVT = Mag.getValueType(); 1536 if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) && 1537 TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) { 1538 SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag); 1539 SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue); 1540 SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit, 1541 DAG.getConstant(0, DL, IntVT), ISD::SETNE); 1542 return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue); 1543 } 1544 1545 // Transform Mag value to integer, and clear the sign bit. 1546 FloatSignAsInt MagAsInt; 1547 getSignAsIntValue(MagAsInt, DL, Mag); 1548 EVT MagVT = MagAsInt.IntValue.getValueType(); 1549 SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT); 1550 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue, 1551 ClearSignMask); 1552 1553 // Get the signbit at the right position for MagAsInt. 1554 int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit; 1555 EVT ShiftVT = IntVT; 1556 if (SignBit.getValueSizeInBits() < ClearedSign.getValueSizeInBits()) { 1557 SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit); 1558 ShiftVT = MagVT; 1559 } 1560 if (ShiftAmount > 0) { 1561 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT); 1562 SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst); 1563 } else if (ShiftAmount < 0) { 1564 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT); 1565 SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst); 1566 } 1567 if (SignBit.getValueSizeInBits() > ClearedSign.getValueSizeInBits()) { 1568 SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit); 1569 } 1570 1571 // Store the part with the modified sign and convert back to float. 1572 SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit); 1573 return modifySignAsInt(MagAsInt, DL, CopiedSign); 1574 } 1575 1576 SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const { 1577 SDLoc DL(Node); 1578 SDValue Value = Node->getOperand(0); 1579 1580 // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal. 1581 EVT FloatVT = Value.getValueType(); 1582 if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) { 1583 SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT); 1584 return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero); 1585 } 1586 1587 // Transform value to integer, clear the sign bit and transform back. 1588 FloatSignAsInt ValueAsInt; 1589 getSignAsIntValue(ValueAsInt, DL, Value); 1590 EVT IntVT = ValueAsInt.IntValue.getValueType(); 1591 SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT); 1592 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue, 1593 ClearSignMask); 1594 return modifySignAsInt(ValueAsInt, DL, ClearedSign); 1595 } 1596 1597 void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node, 1598 SmallVectorImpl<SDValue> &Results) { 1599 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); 1600 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 1601 " not tell us which reg is the stack pointer!"); 1602 SDLoc dl(Node); 1603 EVT VT = Node->getValueType(0); 1604 SDValue Tmp1 = SDValue(Node, 0); 1605 SDValue Tmp2 = SDValue(Node, 1); 1606 SDValue Tmp3 = Node->getOperand(2); 1607 SDValue Chain = Tmp1.getOperand(0); 1608 1609 // Chain the dynamic stack allocation so that it doesn't modify the stack 1610 // pointer when other instructions are using the stack. 1611 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl); 1612 1613 SDValue Size = Tmp2.getOperand(1); 1614 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); 1615 Chain = SP.getValue(1); 1616 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue(); 1617 unsigned StackAlign = 1618 DAG.getSubtarget().getFrameLowering()->getStackAlignment(); 1619 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value 1620 if (Align > StackAlign) 1621 Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1, 1622 DAG.getConstant(-(uint64_t)Align, dl, VT)); 1623 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain 1624 1625 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true), 1626 DAG.getIntPtrConstant(0, dl, true), SDValue(), dl); 1627 1628 Results.push_back(Tmp1); 1629 Results.push_back(Tmp2); 1630 } 1631 1632 /// Legalize a SETCC with given LHS and RHS and condition code CC on the current 1633 /// target. 1634 /// 1635 /// If the SETCC has been legalized using AND / OR, then the legalized node 1636 /// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert 1637 /// will be set to false. 1638 /// 1639 /// If the SETCC has been legalized by using getSetCCSwappedOperands(), 1640 /// then the values of LHS and RHS will be swapped, CC will be set to the 1641 /// new condition, and NeedInvert will be set to false. 1642 /// 1643 /// If the SETCC has been legalized using the inverse condcode, then LHS and 1644 /// RHS will be unchanged, CC will set to the inverted condcode, and NeedInvert 1645 /// will be set to true. The caller must invert the result of the SETCC with 1646 /// SelectionDAG::getLogicalNOT() or take equivalent action to swap the effect 1647 /// of a true/false result. 1648 /// 1649 /// \returns true if the SetCC has been legalized, false if it hasn't. 1650 bool SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT, SDValue &LHS, 1651 SDValue &RHS, SDValue &CC, 1652 bool &NeedInvert, 1653 const SDLoc &dl) { 1654 MVT OpVT = LHS.getSimpleValueType(); 1655 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); 1656 NeedInvert = false; 1657 bool NeedSwap = false; 1658 switch (TLI.getCondCodeAction(CCCode, OpVT)) { 1659 default: llvm_unreachable("Unknown condition code action!"); 1660 case TargetLowering::Legal: 1661 // Nothing to do. 1662 break; 1663 case TargetLowering::Expand: { 1664 ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode); 1665 if (TLI.isCondCodeLegalOrCustom(InvCC, OpVT)) { 1666 std::swap(LHS, RHS); 1667 CC = DAG.getCondCode(InvCC); 1668 return true; 1669 } 1670 // Swapping operands didn't work. Try inverting the condition. 1671 InvCC = getSetCCInverse(CCCode, OpVT.isInteger()); 1672 if (!TLI.isCondCodeLegalOrCustom(InvCC, OpVT)) { 1673 // If inverting the condition is not enough, try swapping operands 1674 // on top of it. 1675 InvCC = ISD::getSetCCSwappedOperands(InvCC); 1676 NeedSwap = true; 1677 } 1678 if (TLI.isCondCodeLegalOrCustom(InvCC, OpVT)) { 1679 CC = DAG.getCondCode(InvCC); 1680 NeedInvert = true; 1681 if (NeedSwap) 1682 std::swap(LHS, RHS); 1683 return true; 1684 } 1685 1686 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID; 1687 unsigned Opc = 0; 1688 switch (CCCode) { 1689 default: llvm_unreachable("Don't know how to expand this condition!"); 1690 case ISD::SETO: 1691 assert(TLI.isCondCodeLegal(ISD::SETOEQ, OpVT) 1692 && "If SETO is expanded, SETOEQ must be legal!"); 1693 CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break; 1694 case ISD::SETUO: 1695 assert(TLI.isCondCodeLegal(ISD::SETUNE, OpVT) 1696 && "If SETUO is expanded, SETUNE must be legal!"); 1697 CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR; break; 1698 case ISD::SETOEQ: 1699 case ISD::SETOGT: 1700 case ISD::SETOGE: 1701 case ISD::SETOLT: 1702 case ISD::SETOLE: 1703 case ISD::SETONE: 1704 case ISD::SETUEQ: 1705 case ISD::SETUNE: 1706 case ISD::SETUGT: 1707 case ISD::SETUGE: 1708 case ISD::SETULT: 1709 case ISD::SETULE: 1710 // If we are floating point, assign and break, otherwise fall through. 1711 if (!OpVT.isInteger()) { 1712 // We can use the 4th bit to tell if we are the unordered 1713 // or ordered version of the opcode. 1714 CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO; 1715 Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND; 1716 CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10); 1717 break; 1718 } 1719 // Fallthrough if we are unsigned integer. 1720 LLVM_FALLTHROUGH; 1721 case ISD::SETLE: 1722 case ISD::SETGT: 1723 case ISD::SETGE: 1724 case ISD::SETLT: 1725 case ISD::SETNE: 1726 case ISD::SETEQ: 1727 // If all combinations of inverting the condition and swapping operands 1728 // didn't work then we have no means to expand the condition. 1729 llvm_unreachable("Don't know how to expand this condition!"); 1730 } 1731 1732 SDValue SetCC1, SetCC2; 1733 if (CCCode != ISD::SETO && CCCode != ISD::SETUO) { 1734 // If we aren't the ordered or unorder operation, 1735 // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS). 1736 SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1); 1737 SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2); 1738 } else { 1739 // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS) 1740 SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1); 1741 SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2); 1742 } 1743 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2); 1744 RHS = SDValue(); 1745 CC = SDValue(); 1746 return true; 1747 } 1748 } 1749 return false; 1750 } 1751 1752 /// Emit a store/load combination to the stack. This stores 1753 /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does 1754 /// a load from the stack slot to DestVT, extending it if needed. 1755 /// The resultant code need not be legal. 1756 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 1757 EVT DestVT, const SDLoc &dl) { 1758 return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode()); 1759 } 1760 1761 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 1762 EVT DestVT, const SDLoc &dl, 1763 SDValue Chain) { 1764 // Create the stack frame object. 1765 unsigned SrcAlign = DAG.getDataLayout().getPrefTypeAlignment( 1766 SrcOp.getValueType().getTypeForEVT(*DAG.getContext())); 1767 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign); 1768 1769 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); 1770 int SPFI = StackPtrFI->getIndex(); 1771 MachinePointerInfo PtrInfo = 1772 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); 1773 1774 unsigned SrcSize = SrcOp.getValueSizeInBits(); 1775 unsigned SlotSize = SlotVT.getSizeInBits(); 1776 unsigned DestSize = DestVT.getSizeInBits(); 1777 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext()); 1778 unsigned DestAlign = DAG.getDataLayout().getPrefTypeAlignment(DestType); 1779 1780 // Emit a store to the stack slot. Use a truncstore if the input value is 1781 // later than DestVT. 1782 SDValue Store; 1783 1784 if (SrcSize > SlotSize) 1785 Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo, 1786 SlotVT, SrcAlign); 1787 else { 1788 assert(SrcSize == SlotSize && "Invalid store"); 1789 Store = 1790 DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign); 1791 } 1792 1793 // Result is a load from the stack slot. 1794 if (SlotSize == DestSize) 1795 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign); 1796 1797 assert(SlotSize < DestSize && "Unknown extension!"); 1798 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT, 1799 DestAlign); 1800 } 1801 1802 SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { 1803 SDLoc dl(Node); 1804 // Create a vector sized/aligned stack slot, store the value to element #0, 1805 // then load the whole vector back out. 1806 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); 1807 1808 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); 1809 int SPFI = StackPtrFI->getIndex(); 1810 1811 SDValue Ch = DAG.getTruncStore( 1812 DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr, 1813 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), 1814 Node->getValueType(0).getVectorElementType()); 1815 return DAG.getLoad( 1816 Node->getValueType(0), dl, Ch, StackPtr, 1817 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 1818 } 1819 1820 static bool 1821 ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG, 1822 const TargetLowering &TLI, SDValue &Res) { 1823 unsigned NumElems = Node->getNumOperands(); 1824 SDLoc dl(Node); 1825 EVT VT = Node->getValueType(0); 1826 1827 // Try to group the scalars into pairs, shuffle the pairs together, then 1828 // shuffle the pairs of pairs together, etc. until the vector has 1829 // been built. This will work only if all of the necessary shuffle masks 1830 // are legal. 1831 1832 // We do this in two phases; first to check the legality of the shuffles, 1833 // and next, assuming that all shuffles are legal, to create the new nodes. 1834 for (int Phase = 0; Phase < 2; ++Phase) { 1835 SmallVector<std::pair<SDValue, SmallVector<int, 16>>, 16> IntermedVals, 1836 NewIntermedVals; 1837 for (unsigned i = 0; i < NumElems; ++i) { 1838 SDValue V = Node->getOperand(i); 1839 if (V.isUndef()) 1840 continue; 1841 1842 SDValue Vec; 1843 if (Phase) 1844 Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V); 1845 IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i))); 1846 } 1847 1848 while (IntermedVals.size() > 2) { 1849 NewIntermedVals.clear(); 1850 for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) { 1851 // This vector and the next vector are shuffled together (simply to 1852 // append the one to the other). 1853 SmallVector<int, 16> ShuffleVec(NumElems, -1); 1854 1855 SmallVector<int, 16> FinalIndices; 1856 FinalIndices.reserve(IntermedVals[i].second.size() + 1857 IntermedVals[i+1].second.size()); 1858 1859 int k = 0; 1860 for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f; 1861 ++j, ++k) { 1862 ShuffleVec[k] = j; 1863 FinalIndices.push_back(IntermedVals[i].second[j]); 1864 } 1865 for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f; 1866 ++j, ++k) { 1867 ShuffleVec[k] = NumElems + j; 1868 FinalIndices.push_back(IntermedVals[i+1].second[j]); 1869 } 1870 1871 SDValue Shuffle; 1872 if (Phase) 1873 Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first, 1874 IntermedVals[i+1].first, 1875 ShuffleVec); 1876 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 1877 return false; 1878 NewIntermedVals.push_back( 1879 std::make_pair(Shuffle, std::move(FinalIndices))); 1880 } 1881 1882 // If we had an odd number of defined values, then append the last 1883 // element to the array of new vectors. 1884 if ((IntermedVals.size() & 1) != 0) 1885 NewIntermedVals.push_back(IntermedVals.back()); 1886 1887 IntermedVals.swap(NewIntermedVals); 1888 } 1889 1890 assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 && 1891 "Invalid number of intermediate vectors"); 1892 SDValue Vec1 = IntermedVals[0].first; 1893 SDValue Vec2; 1894 if (IntermedVals.size() > 1) 1895 Vec2 = IntermedVals[1].first; 1896 else if (Phase) 1897 Vec2 = DAG.getUNDEF(VT); 1898 1899 SmallVector<int, 16> ShuffleVec(NumElems, -1); 1900 for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i) 1901 ShuffleVec[IntermedVals[0].second[i]] = i; 1902 for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i) 1903 ShuffleVec[IntermedVals[1].second[i]] = NumElems + i; 1904 1905 if (Phase) 1906 Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 1907 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 1908 return false; 1909 } 1910 1911 return true; 1912 } 1913 1914 /// Expand a BUILD_VECTOR node on targets that don't 1915 /// support the operation, but do support the resultant vector type. 1916 SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { 1917 unsigned NumElems = Node->getNumOperands(); 1918 SDValue Value1, Value2; 1919 SDLoc dl(Node); 1920 EVT VT = Node->getValueType(0); 1921 EVT OpVT = Node->getOperand(0).getValueType(); 1922 EVT EltVT = VT.getVectorElementType(); 1923 1924 // If the only non-undef value is the low element, turn this into a 1925 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. 1926 bool isOnlyLowElement = true; 1927 bool MoreThanTwoValues = false; 1928 bool isConstant = true; 1929 for (unsigned i = 0; i < NumElems; ++i) { 1930 SDValue V = Node->getOperand(i); 1931 if (V.isUndef()) 1932 continue; 1933 if (i > 0) 1934 isOnlyLowElement = false; 1935 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 1936 isConstant = false; 1937 1938 if (!Value1.getNode()) { 1939 Value1 = V; 1940 } else if (!Value2.getNode()) { 1941 if (V != Value1) 1942 Value2 = V; 1943 } else if (V != Value1 && V != Value2) { 1944 MoreThanTwoValues = true; 1945 } 1946 } 1947 1948 if (!Value1.getNode()) 1949 return DAG.getUNDEF(VT); 1950 1951 if (isOnlyLowElement) 1952 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); 1953 1954 // If all elements are constants, create a load from the constant pool. 1955 if (isConstant) { 1956 SmallVector<Constant*, 16> CV; 1957 for (unsigned i = 0, e = NumElems; i != e; ++i) { 1958 if (ConstantFPSDNode *V = 1959 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { 1960 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); 1961 } else if (ConstantSDNode *V = 1962 dyn_cast<ConstantSDNode>(Node->getOperand(i))) { 1963 if (OpVT==EltVT) 1964 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); 1965 else { 1966 // If OpVT and EltVT don't match, EltVT is not legal and the 1967 // element values have been promoted/truncated earlier. Undo this; 1968 // we don't want a v16i8 to become a v16i32 for example. 1969 const ConstantInt *CI = V->getConstantIntValue(); 1970 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()), 1971 CI->getZExtValue())); 1972 } 1973 } else { 1974 assert(Node->getOperand(i).isUndef()); 1975 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext()); 1976 CV.push_back(UndefValue::get(OpNTy)); 1977 } 1978 } 1979 Constant *CP = ConstantVector::get(CV); 1980 SDValue CPIdx = 1981 DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout())); 1982 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 1983 return DAG.getLoad( 1984 VT, dl, DAG.getEntryNode(), CPIdx, 1985 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 1986 Alignment); 1987 } 1988 1989 SmallSet<SDValue, 16> DefinedValues; 1990 for (unsigned i = 0; i < NumElems; ++i) { 1991 if (Node->getOperand(i).isUndef()) 1992 continue; 1993 DefinedValues.insert(Node->getOperand(i)); 1994 } 1995 1996 if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) { 1997 if (!MoreThanTwoValues) { 1998 SmallVector<int, 8> ShuffleVec(NumElems, -1); 1999 for (unsigned i = 0; i < NumElems; ++i) { 2000 SDValue V = Node->getOperand(i); 2001 if (V.isUndef()) 2002 continue; 2003 ShuffleVec[i] = V == Value1 ? 0 : NumElems; 2004 } 2005 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) { 2006 // Get the splatted value into the low element of a vector register. 2007 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1); 2008 SDValue Vec2; 2009 if (Value2.getNode()) 2010 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2); 2011 else 2012 Vec2 = DAG.getUNDEF(VT); 2013 2014 // Return shuffle(LowValVec, undef, <0,0,0,0>) 2015 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 2016 } 2017 } else { 2018 SDValue Res; 2019 if (ExpandBVWithShuffles(Node, DAG, TLI, Res)) 2020 return Res; 2021 } 2022 } 2023 2024 // Otherwise, we can't handle this case efficiently. 2025 return ExpandVectorBuildThroughStack(Node); 2026 } 2027 2028 // Expand a node into a call to a libcall. If the result value 2029 // does not fit into a register, return the lo part and set the hi part to the 2030 // by-reg argument. If it does fit into a single register, return the result 2031 // and leave the Hi part unset. 2032 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 2033 bool isSigned) { 2034 TargetLowering::ArgListTy Args; 2035 TargetLowering::ArgListEntry Entry; 2036 for (const SDValue &Op : Node->op_values()) { 2037 EVT ArgVT = Op.getValueType(); 2038 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 2039 Entry.Node = Op; 2040 Entry.Ty = ArgTy; 2041 Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned); 2042 Entry.IsZExt = !TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned); 2043 Args.push_back(Entry); 2044 } 2045 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2046 TLI.getPointerTy(DAG.getDataLayout())); 2047 2048 EVT RetVT = Node->getValueType(0); 2049 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 2050 2051 // By default, the input chain to this libcall is the entry node of the 2052 // function. If the libcall is going to be emitted as a tail call then 2053 // TLI.isUsedByReturnOnly will change it to the right chain if the return 2054 // node which is being folded has a non-entry input chain. 2055 SDValue InChain = DAG.getEntryNode(); 2056 2057 // isTailCall may be true since the callee does not reference caller stack 2058 // frame. Check if it's in the right position and that the return types match. 2059 SDValue TCChain = InChain; 2060 const Function &F = DAG.getMachineFunction().getFunction(); 2061 bool isTailCall = 2062 TLI.isInTailCallPosition(DAG, Node, TCChain) && 2063 (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy()); 2064 if (isTailCall) 2065 InChain = TCChain; 2066 2067 TargetLowering::CallLoweringInfo CLI(DAG); 2068 bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned); 2069 CLI.setDebugLoc(SDLoc(Node)) 2070 .setChain(InChain) 2071 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 2072 std::move(Args)) 2073 .setTailCall(isTailCall) 2074 .setSExtResult(signExtend) 2075 .setZExtResult(!signExtend) 2076 .setIsPostTypeLegalization(true); 2077 2078 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2079 2080 if (!CallInfo.second.getNode()) { 2081 LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump()); 2082 // It's a tailcall, return the chain (which is the DAG root). 2083 return DAG.getRoot(); 2084 } 2085 2086 LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump()); 2087 return CallInfo.first; 2088 } 2089 2090 // Expand a node into a call to a libcall. Similar to 2091 // ExpandLibCall except that the first operand is the in-chain. 2092 std::pair<SDValue, SDValue> 2093 SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC, 2094 SDNode *Node, 2095 bool isSigned) { 2096 SDValue InChain = Node->getOperand(0); 2097 2098 TargetLowering::ArgListTy Args; 2099 TargetLowering::ArgListEntry Entry; 2100 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) { 2101 EVT ArgVT = Node->getOperand(i).getValueType(); 2102 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 2103 Entry.Node = Node->getOperand(i); 2104 Entry.Ty = ArgTy; 2105 Entry.IsSExt = isSigned; 2106 Entry.IsZExt = !isSigned; 2107 Args.push_back(Entry); 2108 } 2109 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2110 TLI.getPointerTy(DAG.getDataLayout())); 2111 2112 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext()); 2113 2114 TargetLowering::CallLoweringInfo CLI(DAG); 2115 CLI.setDebugLoc(SDLoc(Node)) 2116 .setChain(InChain) 2117 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 2118 std::move(Args)) 2119 .setSExtResult(isSigned) 2120 .setZExtResult(!isSigned); 2121 2122 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2123 2124 return CallInfo; 2125 } 2126 2127 SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2128 RTLIB::Libcall Call_F32, 2129 RTLIB::Libcall Call_F64, 2130 RTLIB::Libcall Call_F80, 2131 RTLIB::Libcall Call_F128, 2132 RTLIB::Libcall Call_PPCF128) { 2133 if (Node->isStrictFPOpcode()) 2134 Node = DAG.mutateStrictFPToFP(Node); 2135 2136 RTLIB::Libcall LC; 2137 switch (Node->getSimpleValueType(0).SimpleTy) { 2138 default: llvm_unreachable("Unexpected request for libcall!"); 2139 case MVT::f32: LC = Call_F32; break; 2140 case MVT::f64: LC = Call_F64; break; 2141 case MVT::f80: LC = Call_F80; break; 2142 case MVT::f128: LC = Call_F128; break; 2143 case MVT::ppcf128: LC = Call_PPCF128; break; 2144 } 2145 return ExpandLibCall(LC, Node, false); 2146 } 2147 2148 SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned, 2149 RTLIB::Libcall Call_I8, 2150 RTLIB::Libcall Call_I16, 2151 RTLIB::Libcall Call_I32, 2152 RTLIB::Libcall Call_I64, 2153 RTLIB::Libcall Call_I128) { 2154 RTLIB::Libcall LC; 2155 switch (Node->getSimpleValueType(0).SimpleTy) { 2156 default: llvm_unreachable("Unexpected request for libcall!"); 2157 case MVT::i8: LC = Call_I8; break; 2158 case MVT::i16: LC = Call_I16; break; 2159 case MVT::i32: LC = Call_I32; break; 2160 case MVT::i64: LC = Call_I64; break; 2161 case MVT::i128: LC = Call_I128; break; 2162 } 2163 return ExpandLibCall(LC, Node, isSigned); 2164 } 2165 2166 /// Expand the node to a libcall based on first argument type (for instance 2167 /// lround and its variant). 2168 SDValue SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node, 2169 RTLIB::Libcall Call_F32, 2170 RTLIB::Libcall Call_F64, 2171 RTLIB::Libcall Call_F80, 2172 RTLIB::Libcall Call_F128, 2173 RTLIB::Libcall Call_PPCF128) { 2174 RTLIB::Libcall LC; 2175 switch (Node->getOperand(0).getValueType().getSimpleVT().SimpleTy) { 2176 default: llvm_unreachable("Unexpected request for libcall!"); 2177 case MVT::f32: LC = Call_F32; break; 2178 case MVT::f64: LC = Call_F64; break; 2179 case MVT::f80: LC = Call_F80; break; 2180 case MVT::f128: LC = Call_F128; break; 2181 case MVT::ppcf128: LC = Call_PPCF128; break; 2182 } 2183 2184 return ExpandLibCall(LC, Node, false); 2185 } 2186 2187 /// Issue libcalls to __{u}divmod to compute div / rem pairs. 2188 void 2189 SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node, 2190 SmallVectorImpl<SDValue> &Results) { 2191 unsigned Opcode = Node->getOpcode(); 2192 bool isSigned = Opcode == ISD::SDIVREM; 2193 2194 RTLIB::Libcall LC; 2195 switch (Node->getSimpleValueType(0).SimpleTy) { 2196 default: llvm_unreachable("Unexpected request for libcall!"); 2197 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 2198 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 2199 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 2200 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 2201 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; 2202 } 2203 2204 // The input chain to this libcall is the entry node of the function. 2205 // Legalizing the call will automatically add the previous call to the 2206 // dependence. 2207 SDValue InChain = DAG.getEntryNode(); 2208 2209 EVT RetVT = Node->getValueType(0); 2210 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 2211 2212 TargetLowering::ArgListTy Args; 2213 TargetLowering::ArgListEntry Entry; 2214 for (const SDValue &Op : Node->op_values()) { 2215 EVT ArgVT = Op.getValueType(); 2216 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 2217 Entry.Node = Op; 2218 Entry.Ty = ArgTy; 2219 Entry.IsSExt = isSigned; 2220 Entry.IsZExt = !isSigned; 2221 Args.push_back(Entry); 2222 } 2223 2224 // Also pass the return address of the remainder. 2225 SDValue FIPtr = DAG.CreateStackTemporary(RetVT); 2226 Entry.Node = FIPtr; 2227 Entry.Ty = RetTy->getPointerTo(); 2228 Entry.IsSExt = isSigned; 2229 Entry.IsZExt = !isSigned; 2230 Args.push_back(Entry); 2231 2232 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2233 TLI.getPointerTy(DAG.getDataLayout())); 2234 2235 SDLoc dl(Node); 2236 TargetLowering::CallLoweringInfo CLI(DAG); 2237 CLI.setDebugLoc(dl) 2238 .setChain(InChain) 2239 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 2240 std::move(Args)) 2241 .setSExtResult(isSigned) 2242 .setZExtResult(!isSigned); 2243 2244 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2245 2246 // Remainder is loaded back from the stack frame. 2247 SDValue Rem = 2248 DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo()); 2249 Results.push_back(CallInfo.first); 2250 Results.push_back(Rem); 2251 } 2252 2253 /// Return true if sincos libcall is available. 2254 static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) { 2255 RTLIB::Libcall LC; 2256 switch (Node->getSimpleValueType(0).SimpleTy) { 2257 default: llvm_unreachable("Unexpected request for libcall!"); 2258 case MVT::f32: LC = RTLIB::SINCOS_F32; break; 2259 case MVT::f64: LC = RTLIB::SINCOS_F64; break; 2260 case MVT::f80: LC = RTLIB::SINCOS_F80; break; 2261 case MVT::f128: LC = RTLIB::SINCOS_F128; break; 2262 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 2263 } 2264 return TLI.getLibcallName(LC) != nullptr; 2265 } 2266 2267 /// Only issue sincos libcall if both sin and cos are needed. 2268 static bool useSinCos(SDNode *Node) { 2269 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN 2270 ? ISD::FCOS : ISD::FSIN; 2271 2272 SDValue Op0 = Node->getOperand(0); 2273 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(), 2274 UE = Op0.getNode()->use_end(); UI != UE; ++UI) { 2275 SDNode *User = *UI; 2276 if (User == Node) 2277 continue; 2278 // The other user might have been turned into sincos already. 2279 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS) 2280 return true; 2281 } 2282 return false; 2283 } 2284 2285 /// Issue libcalls to sincos to compute sin / cos pairs. 2286 void 2287 SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node, 2288 SmallVectorImpl<SDValue> &Results) { 2289 RTLIB::Libcall LC; 2290 switch (Node->getSimpleValueType(0).SimpleTy) { 2291 default: llvm_unreachable("Unexpected request for libcall!"); 2292 case MVT::f32: LC = RTLIB::SINCOS_F32; break; 2293 case MVT::f64: LC = RTLIB::SINCOS_F64; break; 2294 case MVT::f80: LC = RTLIB::SINCOS_F80; break; 2295 case MVT::f128: LC = RTLIB::SINCOS_F128; break; 2296 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 2297 } 2298 2299 // The input chain to this libcall is the entry node of the function. 2300 // Legalizing the call will automatically add the previous call to the 2301 // dependence. 2302 SDValue InChain = DAG.getEntryNode(); 2303 2304 EVT RetVT = Node->getValueType(0); 2305 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 2306 2307 TargetLowering::ArgListTy Args; 2308 TargetLowering::ArgListEntry Entry; 2309 2310 // Pass the argument. 2311 Entry.Node = Node->getOperand(0); 2312 Entry.Ty = RetTy; 2313 Entry.IsSExt = false; 2314 Entry.IsZExt = false; 2315 Args.push_back(Entry); 2316 2317 // Pass the return address of sin. 2318 SDValue SinPtr = DAG.CreateStackTemporary(RetVT); 2319 Entry.Node = SinPtr; 2320 Entry.Ty = RetTy->getPointerTo(); 2321 Entry.IsSExt = false; 2322 Entry.IsZExt = false; 2323 Args.push_back(Entry); 2324 2325 // Also pass the return address of the cos. 2326 SDValue CosPtr = DAG.CreateStackTemporary(RetVT); 2327 Entry.Node = CosPtr; 2328 Entry.Ty = RetTy->getPointerTo(); 2329 Entry.IsSExt = false; 2330 Entry.IsZExt = false; 2331 Args.push_back(Entry); 2332 2333 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2334 TLI.getPointerTy(DAG.getDataLayout())); 2335 2336 SDLoc dl(Node); 2337 TargetLowering::CallLoweringInfo CLI(DAG); 2338 CLI.setDebugLoc(dl).setChain(InChain).setLibCallee( 2339 TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee, 2340 std::move(Args)); 2341 2342 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2343 2344 Results.push_back( 2345 DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo())); 2346 Results.push_back( 2347 DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo())); 2348 } 2349 2350 /// This function is responsible for legalizing a 2351 /// INT_TO_FP operation of the specified operand when the target requests that 2352 /// we expand it. At this point, we know that the result and operand types are 2353 /// legal for the target. 2354 SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, SDValue Op0, 2355 EVT DestVT, 2356 const SDLoc &dl) { 2357 EVT SrcVT = Op0.getValueType(); 2358 2359 // TODO: Should any fast-math-flags be set for the created nodes? 2360 LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n"); 2361 if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64)) { 2362 LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double " 2363 "expansion\n"); 2364 2365 // Get the stack frame index of a 8 byte buffer. 2366 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); 2367 2368 // word offset constant for Hi/Lo address computation 2369 SDValue WordOff = DAG.getConstant(sizeof(int), dl, 2370 StackSlot.getValueType()); 2371 // set up Hi and Lo (into buffer) address based on endian 2372 SDValue Hi = StackSlot; 2373 SDValue Lo = DAG.getNode(ISD::ADD, dl, StackSlot.getValueType(), 2374 StackSlot, WordOff); 2375 if (DAG.getDataLayout().isLittleEndian()) 2376 std::swap(Hi, Lo); 2377 2378 // if signed map to unsigned space 2379 SDValue Op0Mapped; 2380 if (isSigned) { 2381 // constant used to invert sign bit (signed to unsigned mapping) 2382 SDValue SignBit = DAG.getConstant(0x80000000u, dl, MVT::i32); 2383 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit); 2384 } else { 2385 Op0Mapped = Op0; 2386 } 2387 // store the lo of the constructed double - based on integer input 2388 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op0Mapped, Lo, 2389 MachinePointerInfo()); 2390 // initial hi portion of constructed double 2391 SDValue InitialHi = DAG.getConstant(0x43300000u, dl, MVT::i32); 2392 // store the hi of the constructed double - biased exponent 2393 SDValue Store2 = 2394 DAG.getStore(Store1, dl, InitialHi, Hi, MachinePointerInfo()); 2395 // load the constructed double 2396 SDValue Load = 2397 DAG.getLoad(MVT::f64, dl, Store2, StackSlot, MachinePointerInfo()); 2398 // FP constant to bias correct the final result 2399 SDValue Bias = DAG.getConstantFP(isSigned ? 2400 BitsToDouble(0x4330000080000000ULL) : 2401 BitsToDouble(0x4330000000000000ULL), 2402 dl, MVT::f64); 2403 // subtract the bias 2404 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); 2405 // final result 2406 SDValue Result = DAG.getFPExtendOrRound(Sub, dl, DestVT); 2407 return Result; 2408 } 2409 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); 2410 // Code below here assumes !isSigned without checking again. 2411 2412 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 2413 2414 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0, 2415 DAG.getConstant(0, dl, SrcVT), ISD::SETLT); 2416 SDValue Zero = DAG.getIntPtrConstant(0, dl), 2417 Four = DAG.getIntPtrConstant(4, dl); 2418 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(), 2419 SignSet, Four, Zero); 2420 2421 // If the sign bit of the integer is set, the large number will be treated 2422 // as a negative number. To counteract this, the dynamic code adds an 2423 // offset depending on the data type. 2424 uint64_t FF; 2425 switch (SrcVT.getSimpleVT().SimpleTy) { 2426 default: llvm_unreachable("Unsupported integer type!"); 2427 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 2428 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 2429 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 2430 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 2431 } 2432 if (DAG.getDataLayout().isLittleEndian()) 2433 FF <<= 32; 2434 Constant *FudgeFactor = ConstantInt::get( 2435 Type::getInt64Ty(*DAG.getContext()), FF); 2436 2437 SDValue CPIdx = 2438 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout())); 2439 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 2440 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset); 2441 Alignment = std::min(Alignment, 4u); 2442 SDValue FudgeInReg; 2443 if (DestVT == MVT::f32) 2444 FudgeInReg = DAG.getLoad( 2445 MVT::f32, dl, DAG.getEntryNode(), CPIdx, 2446 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 2447 Alignment); 2448 else { 2449 SDValue Load = DAG.getExtLoad( 2450 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx, 2451 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32, 2452 Alignment); 2453 HandleSDNode Handle(Load); 2454 LegalizeOp(Load.getNode()); 2455 FudgeInReg = Handle.getValue(); 2456 } 2457 2458 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); 2459 } 2460 2461 /// This function is responsible for legalizing a 2462 /// *INT_TO_FP operation of the specified operand when the target requests that 2463 /// we promote it. At this point, we know that the result and operand types are 2464 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 2465 /// operation that takes a larger input. 2466 SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, 2467 bool isSigned, 2468 const SDLoc &dl) { 2469 // First step, figure out the appropriate *INT_TO_FP operation to use. 2470 EVT NewInTy = LegalOp.getValueType(); 2471 2472 unsigned OpToUse = 0; 2473 2474 // Scan for the appropriate larger type to use. 2475 while (true) { 2476 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1); 2477 assert(NewInTy.isInteger() && "Ran out of possibilities!"); 2478 2479 // If the target supports SINT_TO_FP of this type, use it. 2480 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) { 2481 OpToUse = ISD::SINT_TO_FP; 2482 break; 2483 } 2484 if (isSigned) continue; 2485 2486 // If the target supports UINT_TO_FP of this type, use it. 2487 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) { 2488 OpToUse = ISD::UINT_TO_FP; 2489 break; 2490 } 2491 2492 // Otherwise, try a larger type. 2493 } 2494 2495 // Okay, we found the operation and type to use. Zero extend our input to the 2496 // desired type then run the operation on it. 2497 return DAG.getNode(OpToUse, dl, DestVT, 2498 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2499 dl, NewInTy, LegalOp)); 2500 } 2501 2502 /// This function is responsible for legalizing a 2503 /// FP_TO_*INT operation of the specified operand when the target requests that 2504 /// we promote it. At this point, we know that the result and operand types are 2505 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 2506 /// operation that returns a larger result. 2507 SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, 2508 bool isSigned, 2509 const SDLoc &dl) { 2510 // First step, figure out the appropriate FP_TO*INT operation to use. 2511 EVT NewOutTy = DestVT; 2512 2513 unsigned OpToUse = 0; 2514 2515 // Scan for the appropriate larger type to use. 2516 while (true) { 2517 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1); 2518 assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 2519 2520 // A larger signed type can hold all unsigned values of the requested type, 2521 // so using FP_TO_SINT is valid 2522 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) { 2523 OpToUse = ISD::FP_TO_SINT; 2524 break; 2525 } 2526 2527 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT. 2528 if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) { 2529 OpToUse = ISD::FP_TO_UINT; 2530 break; 2531 } 2532 2533 // Otherwise, try a larger type. 2534 } 2535 2536 // Okay, we found the operation and type to use. 2537 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); 2538 2539 // Truncate the result of the extended FP_TO_*INT operation to the desired 2540 // size. 2541 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); 2542 } 2543 2544 /// Legalize a BITREVERSE scalar/vector operation as a series of mask + shifts. 2545 SDValue SelectionDAGLegalize::ExpandBITREVERSE(SDValue Op, const SDLoc &dl) { 2546 EVT VT = Op.getValueType(); 2547 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2548 unsigned Sz = VT.getScalarSizeInBits(); 2549 2550 SDValue Tmp, Tmp2, Tmp3; 2551 2552 // If we can, perform BSWAP first and then the mask+swap the i4, then i2 2553 // and finally the i1 pairs. 2554 // TODO: We can easily support i4/i2 legal types if any target ever does. 2555 if (Sz >= 8 && isPowerOf2_32(Sz)) { 2556 // Create the masks - repeating the pattern every byte. 2557 APInt MaskHi4 = APInt::getSplat(Sz, APInt(8, 0xF0)); 2558 APInt MaskHi2 = APInt::getSplat(Sz, APInt(8, 0xCC)); 2559 APInt MaskHi1 = APInt::getSplat(Sz, APInt(8, 0xAA)); 2560 APInt MaskLo4 = APInt::getSplat(Sz, APInt(8, 0x0F)); 2561 APInt MaskLo2 = APInt::getSplat(Sz, APInt(8, 0x33)); 2562 APInt MaskLo1 = APInt::getSplat(Sz, APInt(8, 0x55)); 2563 2564 // BSWAP if the type is wider than a single byte. 2565 Tmp = (Sz > 8 ? DAG.getNode(ISD::BSWAP, dl, VT, Op) : Op); 2566 2567 // swap i4: ((V & 0xF0) >> 4) | ((V & 0x0F) << 4) 2568 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi4, dl, VT)); 2569 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo4, dl, VT)); 2570 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(4, dl, SHVT)); 2571 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(4, dl, SHVT)); 2572 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); 2573 2574 // swap i2: ((V & 0xCC) >> 2) | ((V & 0x33) << 2) 2575 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi2, dl, VT)); 2576 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo2, dl, VT)); 2577 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(2, dl, SHVT)); 2578 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(2, dl, SHVT)); 2579 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); 2580 2581 // swap i1: ((V & 0xAA) >> 1) | ((V & 0x55) << 1) 2582 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi1, dl, VT)); 2583 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo1, dl, VT)); 2584 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(1, dl, SHVT)); 2585 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(1, dl, SHVT)); 2586 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); 2587 return Tmp; 2588 } 2589 2590 Tmp = DAG.getConstant(0, dl, VT); 2591 for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) { 2592 if (I < J) 2593 Tmp2 = 2594 DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT)); 2595 else 2596 Tmp2 = 2597 DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(I - J, dl, SHVT)); 2598 2599 APInt Shift(Sz, 1); 2600 Shift <<= J; 2601 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(Shift, dl, VT)); 2602 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp, Tmp2); 2603 } 2604 2605 return Tmp; 2606 } 2607 2608 /// Open code the operations for BSWAP of the specified operation. 2609 SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, const SDLoc &dl) { 2610 EVT VT = Op.getValueType(); 2611 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2612 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; 2613 switch (VT.getSimpleVT().getScalarType().SimpleTy) { 2614 default: llvm_unreachable("Unhandled Expand type in BSWAP!"); 2615 case MVT::i16: 2616 // Use a rotate by 8. This can be further expanded if necessary. 2617 return DAG.getNode(ISD::ROTL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2618 case MVT::i32: 2619 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2620 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2621 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2622 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2623 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, 2624 DAG.getConstant(0xFF0000, dl, VT)); 2625 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT)); 2626 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); 2627 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); 2628 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); 2629 case MVT::i64: 2630 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, dl, SHVT)); 2631 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, dl, SHVT)); 2632 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2633 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2634 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2635 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2636 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT)); 2637 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT)); 2638 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, 2639 DAG.getConstant(255ULL<<48, dl, VT)); 2640 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, 2641 DAG.getConstant(255ULL<<40, dl, VT)); 2642 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, 2643 DAG.getConstant(255ULL<<32, dl, VT)); 2644 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, 2645 DAG.getConstant(255ULL<<24, dl, VT)); 2646 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, 2647 DAG.getConstant(255ULL<<16, dl, VT)); 2648 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, 2649 DAG.getConstant(255ULL<<8 , dl, VT)); 2650 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7); 2651 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5); 2652 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); 2653 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); 2654 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6); 2655 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); 2656 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4); 2657 } 2658 } 2659 2660 bool SelectionDAGLegalize::ExpandNode(SDNode *Node) { 2661 LLVM_DEBUG(dbgs() << "Trying to expand node\n"); 2662 SmallVector<SDValue, 8> Results; 2663 SDLoc dl(Node); 2664 SDValue Tmp1, Tmp2, Tmp3, Tmp4; 2665 bool NeedInvert; 2666 switch (Node->getOpcode()) { 2667 case ISD::ABS: 2668 if (TLI.expandABS(Node, Tmp1, DAG)) 2669 Results.push_back(Tmp1); 2670 break; 2671 case ISD::CTPOP: 2672 if (TLI.expandCTPOP(Node, Tmp1, DAG)) 2673 Results.push_back(Tmp1); 2674 break; 2675 case ISD::CTLZ: 2676 case ISD::CTLZ_ZERO_UNDEF: 2677 if (TLI.expandCTLZ(Node, Tmp1, DAG)) 2678 Results.push_back(Tmp1); 2679 break; 2680 case ISD::CTTZ: 2681 case ISD::CTTZ_ZERO_UNDEF: 2682 if (TLI.expandCTTZ(Node, Tmp1, DAG)) 2683 Results.push_back(Tmp1); 2684 break; 2685 case ISD::BITREVERSE: 2686 Results.push_back(ExpandBITREVERSE(Node->getOperand(0), dl)); 2687 break; 2688 case ISD::BSWAP: 2689 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl)); 2690 break; 2691 case ISD::FRAMEADDR: 2692 case ISD::RETURNADDR: 2693 case ISD::FRAME_TO_ARGS_OFFSET: 2694 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 2695 break; 2696 case ISD::EH_DWARF_CFA: { 2697 SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl, 2698 TLI.getPointerTy(DAG.getDataLayout())); 2699 SDValue Offset = DAG.getNode(ISD::ADD, dl, 2700 CfaArg.getValueType(), 2701 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, 2702 CfaArg.getValueType()), 2703 CfaArg); 2704 SDValue FA = DAG.getNode( 2705 ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()), 2706 DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()))); 2707 Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(), 2708 FA, Offset)); 2709 break; 2710 } 2711 case ISD::FLT_ROUNDS_: 2712 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0))); 2713 break; 2714 case ISD::EH_RETURN: 2715 case ISD::EH_LABEL: 2716 case ISD::PREFETCH: 2717 case ISD::VAEND: 2718 case ISD::EH_SJLJ_LONGJMP: 2719 // If the target didn't expand these, there's nothing to do, so just 2720 // preserve the chain and be done. 2721 Results.push_back(Node->getOperand(0)); 2722 break; 2723 case ISD::READCYCLECOUNTER: 2724 // If the target didn't expand this, just return 'zero' and preserve the 2725 // chain. 2726 Results.append(Node->getNumValues() - 1, 2727 DAG.getConstant(0, dl, Node->getValueType(0))); 2728 Results.push_back(Node->getOperand(0)); 2729 break; 2730 case ISD::EH_SJLJ_SETJMP: 2731 // If the target didn't expand this, just return 'zero' and preserve the 2732 // chain. 2733 Results.push_back(DAG.getConstant(0, dl, MVT::i32)); 2734 Results.push_back(Node->getOperand(0)); 2735 break; 2736 case ISD::ATOMIC_LOAD: { 2737 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP. 2738 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0)); 2739 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 2740 SDValue Swap = DAG.getAtomicCmpSwap( 2741 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 2742 Node->getOperand(0), Node->getOperand(1), Zero, Zero, 2743 cast<AtomicSDNode>(Node)->getMemOperand()); 2744 Results.push_back(Swap.getValue(0)); 2745 Results.push_back(Swap.getValue(1)); 2746 break; 2747 } 2748 case ISD::ATOMIC_STORE: { 2749 // There is no libcall for atomic store; fake it with ATOMIC_SWAP. 2750 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl, 2751 cast<AtomicSDNode>(Node)->getMemoryVT(), 2752 Node->getOperand(0), 2753 Node->getOperand(1), Node->getOperand(2), 2754 cast<AtomicSDNode>(Node)->getMemOperand()); 2755 Results.push_back(Swap.getValue(1)); 2756 break; 2757 } 2758 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 2759 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and 2760 // splits out the success value as a comparison. Expanding the resulting 2761 // ATOMIC_CMP_SWAP will produce a libcall. 2762 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 2763 SDValue Res = DAG.getAtomicCmpSwap( 2764 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 2765 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2), 2766 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand()); 2767 2768 SDValue ExtRes = Res; 2769 SDValue LHS = Res; 2770 SDValue RHS = Node->getOperand(1); 2771 2772 EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT(); 2773 EVT OuterType = Node->getValueType(0); 2774 switch (TLI.getExtendForAtomicOps()) { 2775 case ISD::SIGN_EXTEND: 2776 LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res, 2777 DAG.getValueType(AtomicType)); 2778 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType, 2779 Node->getOperand(2), DAG.getValueType(AtomicType)); 2780 ExtRes = LHS; 2781 break; 2782 case ISD::ZERO_EXTEND: 2783 LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res, 2784 DAG.getValueType(AtomicType)); 2785 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 2786 ExtRes = LHS; 2787 break; 2788 case ISD::ANY_EXTEND: 2789 LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType); 2790 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 2791 break; 2792 default: 2793 llvm_unreachable("Invalid atomic op extension"); 2794 } 2795 2796 SDValue Success = 2797 DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ); 2798 2799 Results.push_back(ExtRes.getValue(0)); 2800 Results.push_back(Success); 2801 Results.push_back(Res.getValue(1)); 2802 break; 2803 } 2804 case ISD::DYNAMIC_STACKALLOC: 2805 ExpandDYNAMIC_STACKALLOC(Node, Results); 2806 break; 2807 case ISD::MERGE_VALUES: 2808 for (unsigned i = 0; i < Node->getNumValues(); i++) 2809 Results.push_back(Node->getOperand(i)); 2810 break; 2811 case ISD::UNDEF: { 2812 EVT VT = Node->getValueType(0); 2813 if (VT.isInteger()) 2814 Results.push_back(DAG.getConstant(0, dl, VT)); 2815 else { 2816 assert(VT.isFloatingPoint() && "Unknown value type!"); 2817 Results.push_back(DAG.getConstantFP(0, dl, VT)); 2818 } 2819 break; 2820 } 2821 case ISD::STRICT_FP_ROUND: 2822 Tmp1 = EmitStackConvert(Node->getOperand(1), 2823 Node->getValueType(0), 2824 Node->getValueType(0), dl, Node->getOperand(0)); 2825 ReplaceNode(Node, Tmp1.getNode()); 2826 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n"); 2827 return true; 2828 case ISD::FP_ROUND: 2829 case ISD::BITCAST: 2830 Tmp1 = EmitStackConvert(Node->getOperand(0), 2831 Node->getValueType(0), 2832 Node->getValueType(0), dl); 2833 Results.push_back(Tmp1); 2834 break; 2835 case ISD::STRICT_FP_EXTEND: 2836 Tmp1 = EmitStackConvert(Node->getOperand(1), 2837 Node->getOperand(1).getValueType(), 2838 Node->getValueType(0), dl, Node->getOperand(0)); 2839 ReplaceNode(Node, Tmp1.getNode()); 2840 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n"); 2841 return true; 2842 case ISD::FP_EXTEND: 2843 Tmp1 = EmitStackConvert(Node->getOperand(0), 2844 Node->getOperand(0).getValueType(), 2845 Node->getValueType(0), dl); 2846 Results.push_back(Tmp1); 2847 break; 2848 case ISD::SIGN_EXTEND_INREG: { 2849 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 2850 EVT VT = Node->getValueType(0); 2851 2852 // An in-register sign-extend of a boolean is a negation: 2853 // 'true' (1) sign-extended is -1. 2854 // 'false' (0) sign-extended is 0. 2855 // However, we must mask the high bits of the source operand because the 2856 // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero. 2857 2858 // TODO: Do this for vectors too? 2859 if (ExtraVT.getSizeInBits() == 1) { 2860 SDValue One = DAG.getConstant(1, dl, VT); 2861 SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One); 2862 SDValue Zero = DAG.getConstant(0, dl, VT); 2863 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And); 2864 Results.push_back(Neg); 2865 break; 2866 } 2867 2868 // NOTE: we could fall back on load/store here too for targets without 2869 // SRA. However, it is doubtful that any exist. 2870 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2871 unsigned BitsDiff = VT.getScalarSizeInBits() - 2872 ExtraVT.getScalarSizeInBits(); 2873 SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy); 2874 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), 2875 Node->getOperand(0), ShiftCst); 2876 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); 2877 Results.push_back(Tmp1); 2878 break; 2879 } 2880 case ISD::FP_ROUND_INREG: { 2881 // The only way we can lower this is to turn it into a TRUNCSTORE, 2882 // EXTLOAD pair, targeting a temporary location (a stack slot). 2883 2884 // NOTE: there is a choice here between constantly creating new stack 2885 // slots and always reusing the same one. We currently always create 2886 // new ones, as reuse may inhibit scheduling. 2887 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 2888 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT, 2889 Node->getValueType(0), dl); 2890 Results.push_back(Tmp1); 2891 break; 2892 } 2893 case ISD::UINT_TO_FP: 2894 if (TLI.expandUINT_TO_FP(Node, Tmp1, DAG)) { 2895 Results.push_back(Tmp1); 2896 break; 2897 } 2898 LLVM_FALLTHROUGH; 2899 case ISD::SINT_TO_FP: 2900 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP, 2901 Node->getOperand(0), Node->getValueType(0), dl); 2902 Results.push_back(Tmp1); 2903 break; 2904 case ISD::FP_TO_SINT: 2905 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) 2906 Results.push_back(Tmp1); 2907 break; 2908 case ISD::FP_TO_UINT: 2909 if (TLI.expandFP_TO_UINT(Node, Tmp1, DAG)) 2910 Results.push_back(Tmp1); 2911 break; 2912 case ISD::LROUND: 2913 Results.push_back(ExpandArgFPLibCall(Node, RTLIB::LROUND_F32, 2914 RTLIB::LROUND_F64, RTLIB::LROUND_F80, 2915 RTLIB::LROUND_F128, 2916 RTLIB::LROUND_PPCF128)); 2917 break; 2918 case ISD::LLROUND: 2919 Results.push_back(ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32, 2920 RTLIB::LLROUND_F64, RTLIB::LLROUND_F80, 2921 RTLIB::LLROUND_F128, 2922 RTLIB::LLROUND_PPCF128)); 2923 break; 2924 case ISD::LRINT: 2925 Results.push_back(ExpandArgFPLibCall(Node, RTLIB::LRINT_F32, 2926 RTLIB::LRINT_F64, RTLIB::LRINT_F80, 2927 RTLIB::LRINT_F128, 2928 RTLIB::LRINT_PPCF128)); 2929 break; 2930 case ISD::LLRINT: 2931 Results.push_back(ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32, 2932 RTLIB::LLRINT_F64, RTLIB::LLRINT_F80, 2933 RTLIB::LLRINT_F128, 2934 RTLIB::LLRINT_PPCF128)); 2935 break; 2936 case ISD::VAARG: 2937 Results.push_back(DAG.expandVAArg(Node)); 2938 Results.push_back(Results[0].getValue(1)); 2939 break; 2940 case ISD::VACOPY: 2941 Results.push_back(DAG.expandVACopy(Node)); 2942 break; 2943 case ISD::EXTRACT_VECTOR_ELT: 2944 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1) 2945 // This must be an access of the only element. Return it. 2946 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), 2947 Node->getOperand(0)); 2948 else 2949 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); 2950 Results.push_back(Tmp1); 2951 break; 2952 case ISD::EXTRACT_SUBVECTOR: 2953 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); 2954 break; 2955 case ISD::INSERT_SUBVECTOR: 2956 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0))); 2957 break; 2958 case ISD::CONCAT_VECTORS: 2959 Results.push_back(ExpandVectorBuildThroughStack(Node)); 2960 break; 2961 case ISD::SCALAR_TO_VECTOR: 2962 Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); 2963 break; 2964 case ISD::INSERT_VECTOR_ELT: 2965 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), 2966 Node->getOperand(1), 2967 Node->getOperand(2), dl)); 2968 break; 2969 case ISD::VECTOR_SHUFFLE: { 2970 SmallVector<int, 32> NewMask; 2971 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 2972 2973 EVT VT = Node->getValueType(0); 2974 EVT EltVT = VT.getVectorElementType(); 2975 SDValue Op0 = Node->getOperand(0); 2976 SDValue Op1 = Node->getOperand(1); 2977 if (!TLI.isTypeLegal(EltVT)) { 2978 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT); 2979 2980 // BUILD_VECTOR operands are allowed to be wider than the element type. 2981 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept 2982 // it. 2983 if (NewEltVT.bitsLT(EltVT)) { 2984 // Convert shuffle node. 2985 // If original node was v4i64 and the new EltVT is i32, 2986 // cast operands to v8i32 and re-build the mask. 2987 2988 // Calculate new VT, the size of the new VT should be equal to original. 2989 EVT NewVT = 2990 EVT::getVectorVT(*DAG.getContext(), NewEltVT, 2991 VT.getSizeInBits() / NewEltVT.getSizeInBits()); 2992 assert(NewVT.bitsEq(VT)); 2993 2994 // cast operands to new VT 2995 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0); 2996 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1); 2997 2998 // Convert the shuffle mask 2999 unsigned int factor = 3000 NewVT.getVectorNumElements()/VT.getVectorNumElements(); 3001 3002 // EltVT gets smaller 3003 assert(factor > 0); 3004 3005 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 3006 if (Mask[i] < 0) { 3007 for (unsigned fi = 0; fi < factor; ++fi) 3008 NewMask.push_back(Mask[i]); 3009 } 3010 else { 3011 for (unsigned fi = 0; fi < factor; ++fi) 3012 NewMask.push_back(Mask[i]*factor+fi); 3013 } 3014 } 3015 Mask = NewMask; 3016 VT = NewVT; 3017 } 3018 EltVT = NewEltVT; 3019 } 3020 unsigned NumElems = VT.getVectorNumElements(); 3021 SmallVector<SDValue, 16> Ops; 3022 for (unsigned i = 0; i != NumElems; ++i) { 3023 if (Mask[i] < 0) { 3024 Ops.push_back(DAG.getUNDEF(EltVT)); 3025 continue; 3026 } 3027 unsigned Idx = Mask[i]; 3028 if (Idx < NumElems) 3029 Ops.push_back(DAG.getNode( 3030 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0, 3031 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())))); 3032 else 3033 Ops.push_back(DAG.getNode( 3034 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1, 3035 DAG.getConstant(Idx - NumElems, dl, 3036 TLI.getVectorIdxTy(DAG.getDataLayout())))); 3037 } 3038 3039 Tmp1 = DAG.getBuildVector(VT, dl, Ops); 3040 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type. 3041 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1); 3042 Results.push_back(Tmp1); 3043 break; 3044 } 3045 case ISD::EXTRACT_ELEMENT: { 3046 EVT OpTy = Node->getOperand(0).getValueType(); 3047 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { 3048 // 1 -> Hi 3049 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), 3050 DAG.getConstant(OpTy.getSizeInBits() / 2, dl, 3051 TLI.getShiftAmountTy( 3052 Node->getOperand(0).getValueType(), 3053 DAG.getDataLayout()))); 3054 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); 3055 } else { 3056 // 0 -> Lo 3057 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), 3058 Node->getOperand(0)); 3059 } 3060 Results.push_back(Tmp1); 3061 break; 3062 } 3063 case ISD::STACKSAVE: 3064 // Expand to CopyFromReg if the target set 3065 // StackPointerRegisterToSaveRestore. 3066 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 3067 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, 3068 Node->getValueType(0))); 3069 Results.push_back(Results[0].getValue(1)); 3070 } else { 3071 Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 3072 Results.push_back(Node->getOperand(0)); 3073 } 3074 break; 3075 case ISD::STACKRESTORE: 3076 // Expand to CopyToReg if the target set 3077 // StackPointerRegisterToSaveRestore. 3078 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 3079 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, 3080 Node->getOperand(1))); 3081 } else { 3082 Results.push_back(Node->getOperand(0)); 3083 } 3084 break; 3085 case ISD::GET_DYNAMIC_AREA_OFFSET: 3086 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 3087 Results.push_back(Results[0].getValue(0)); 3088 break; 3089 case ISD::FCOPYSIGN: 3090 Results.push_back(ExpandFCOPYSIGN(Node)); 3091 break; 3092 case ISD::FNEG: 3093 // Expand Y = FNEG(X) -> Y = SUB -0.0, X 3094 Tmp1 = DAG.getConstantFP(-0.0, dl, Node->getValueType(0)); 3095 // TODO: If FNEG has fast-math-flags, propagate them to the FSUB. 3096 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1, 3097 Node->getOperand(0)); 3098 Results.push_back(Tmp1); 3099 break; 3100 case ISD::FABS: 3101 Results.push_back(ExpandFABS(Node)); 3102 break; 3103 case ISD::SMIN: 3104 case ISD::SMAX: 3105 case ISD::UMIN: 3106 case ISD::UMAX: { 3107 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B 3108 ISD::CondCode Pred; 3109 switch (Node->getOpcode()) { 3110 default: llvm_unreachable("How did we get here?"); 3111 case ISD::SMAX: Pred = ISD::SETGT; break; 3112 case ISD::SMIN: Pred = ISD::SETLT; break; 3113 case ISD::UMAX: Pred = ISD::SETUGT; break; 3114 case ISD::UMIN: Pred = ISD::SETULT; break; 3115 } 3116 Tmp1 = Node->getOperand(0); 3117 Tmp2 = Node->getOperand(1); 3118 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred); 3119 Results.push_back(Tmp1); 3120 break; 3121 } 3122 case ISD::FMINNUM: 3123 case ISD::FMAXNUM: { 3124 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) 3125 Results.push_back(Expanded); 3126 break; 3127 } 3128 case ISD::FSIN: 3129 case ISD::FCOS: { 3130 EVT VT = Node->getValueType(0); 3131 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin / 3132 // fcos which share the same operand and both are used. 3133 if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) || 3134 isSinCosLibcallAvailable(Node, TLI)) 3135 && useSinCos(Node)) { 3136 SDVTList VTs = DAG.getVTList(VT, VT); 3137 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0)); 3138 if (Node->getOpcode() == ISD::FCOS) 3139 Tmp1 = Tmp1.getValue(1); 3140 Results.push_back(Tmp1); 3141 } 3142 break; 3143 } 3144 case ISD::FMAD: 3145 llvm_unreachable("Illegal fmad should never be formed"); 3146 3147 case ISD::FP16_TO_FP: 3148 if (Node->getValueType(0) != MVT::f32) { 3149 // We can extend to types bigger than f32 in two steps without changing 3150 // the result. Since "f16 -> f32" is much more commonly available, give 3151 // CodeGen the option of emitting that before resorting to a libcall. 3152 SDValue Res = 3153 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0)); 3154 Results.push_back( 3155 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res)); 3156 } 3157 break; 3158 case ISD::FP_TO_FP16: 3159 LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n"); 3160 if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) { 3161 SDValue Op = Node->getOperand(0); 3162 MVT SVT = Op.getSimpleValueType(); 3163 if ((SVT == MVT::f64 || SVT == MVT::f80) && 3164 TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) { 3165 // Under fastmath, we can expand this node into a fround followed by 3166 // a float-half conversion. 3167 SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op, 3168 DAG.getIntPtrConstant(0, dl)); 3169 Results.push_back( 3170 DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal)); 3171 } 3172 } 3173 break; 3174 case ISD::ConstantFP: { 3175 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 3176 // Check to see if this FP immediate is already legal. 3177 // If this is a legal constant, turn it into a TargetConstantFP node. 3178 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0), 3179 DAG.getMachineFunction().getFunction().hasOptSize())) 3180 Results.push_back(ExpandConstantFP(CFP, true)); 3181 break; 3182 } 3183 case ISD::Constant: { 3184 ConstantSDNode *CP = cast<ConstantSDNode>(Node); 3185 Results.push_back(ExpandConstant(CP)); 3186 break; 3187 } 3188 case ISD::FSUB: { 3189 EVT VT = Node->getValueType(0); 3190 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) && 3191 TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) { 3192 const SDNodeFlags Flags = Node->getFlags(); 3193 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1)); 3194 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags); 3195 Results.push_back(Tmp1); 3196 } 3197 break; 3198 } 3199 case ISD::SUB: { 3200 EVT VT = Node->getValueType(0); 3201 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) && 3202 TLI.isOperationLegalOrCustom(ISD::XOR, VT) && 3203 "Don't know how to expand this subtraction!"); 3204 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1), 3205 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 3206 VT)); 3207 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT)); 3208 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1)); 3209 break; 3210 } 3211 case ISD::UREM: 3212 case ISD::SREM: { 3213 EVT VT = Node->getValueType(0); 3214 bool isSigned = Node->getOpcode() == ISD::SREM; 3215 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV; 3216 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 3217 Tmp2 = Node->getOperand(0); 3218 Tmp3 = Node->getOperand(1); 3219 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 3220 SDVTList VTs = DAG.getVTList(VT, VT); 3221 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1); 3222 Results.push_back(Tmp1); 3223 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) { 3224 // X % Y -> X-X/Y*Y 3225 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3); 3226 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3); 3227 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1); 3228 Results.push_back(Tmp1); 3229 } 3230 break; 3231 } 3232 case ISD::UDIV: 3233 case ISD::SDIV: { 3234 bool isSigned = Node->getOpcode() == ISD::SDIV; 3235 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 3236 EVT VT = Node->getValueType(0); 3237 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 3238 SDVTList VTs = DAG.getVTList(VT, VT); 3239 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0), 3240 Node->getOperand(1)); 3241 Results.push_back(Tmp1); 3242 } 3243 break; 3244 } 3245 case ISD::MULHU: 3246 case ISD::MULHS: { 3247 unsigned ExpandOpcode = 3248 Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI; 3249 EVT VT = Node->getValueType(0); 3250 SDVTList VTs = DAG.getVTList(VT, VT); 3251 3252 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0), 3253 Node->getOperand(1)); 3254 Results.push_back(Tmp1.getValue(1)); 3255 break; 3256 } 3257 case ISD::UMUL_LOHI: 3258 case ISD::SMUL_LOHI: { 3259 SDValue LHS = Node->getOperand(0); 3260 SDValue RHS = Node->getOperand(1); 3261 MVT VT = LHS.getSimpleValueType(); 3262 unsigned MULHOpcode = 3263 Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS; 3264 3265 if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) { 3266 Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS)); 3267 Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS)); 3268 break; 3269 } 3270 3271 SmallVector<SDValue, 4> Halves; 3272 EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext()); 3273 assert(TLI.isTypeLegal(HalfType)); 3274 if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, Node, LHS, RHS, Halves, 3275 HalfType, DAG, 3276 TargetLowering::MulExpansionKind::Always)) { 3277 for (unsigned i = 0; i < 2; ++i) { 3278 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]); 3279 SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]); 3280 SDValue Shift = DAG.getConstant( 3281 HalfType.getScalarSizeInBits(), dl, 3282 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 3283 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 3284 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 3285 } 3286 break; 3287 } 3288 break; 3289 } 3290 case ISD::MUL: { 3291 EVT VT = Node->getValueType(0); 3292 SDVTList VTs = DAG.getVTList(VT, VT); 3293 // See if multiply or divide can be lowered using two-result operations. 3294 // We just need the low half of the multiply; try both the signed 3295 // and unsigned forms. If the target supports both SMUL_LOHI and 3296 // UMUL_LOHI, form a preference by checking which forms of plain 3297 // MULH it supports. 3298 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); 3299 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); 3300 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); 3301 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); 3302 unsigned OpToUse = 0; 3303 if (HasSMUL_LOHI && !HasMULHS) { 3304 OpToUse = ISD::SMUL_LOHI; 3305 } else if (HasUMUL_LOHI && !HasMULHU) { 3306 OpToUse = ISD::UMUL_LOHI; 3307 } else if (HasSMUL_LOHI) { 3308 OpToUse = ISD::SMUL_LOHI; 3309 } else if (HasUMUL_LOHI) { 3310 OpToUse = ISD::UMUL_LOHI; 3311 } 3312 if (OpToUse) { 3313 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0), 3314 Node->getOperand(1))); 3315 break; 3316 } 3317 3318 SDValue Lo, Hi; 3319 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext()); 3320 if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) && 3321 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) && 3322 TLI.isOperationLegalOrCustom(ISD::SHL, VT) && 3323 TLI.isOperationLegalOrCustom(ISD::OR, VT) && 3324 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG, 3325 TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) { 3326 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo); 3327 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi); 3328 SDValue Shift = 3329 DAG.getConstant(HalfType.getSizeInBits(), dl, 3330 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 3331 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 3332 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 3333 } 3334 break; 3335 } 3336 case ISD::FSHL: 3337 case ISD::FSHR: 3338 if (TLI.expandFunnelShift(Node, Tmp1, DAG)) 3339 Results.push_back(Tmp1); 3340 break; 3341 case ISD::ROTL: 3342 case ISD::ROTR: 3343 if (TLI.expandROT(Node, Tmp1, DAG)) 3344 Results.push_back(Tmp1); 3345 break; 3346 case ISD::SADDSAT: 3347 case ISD::UADDSAT: 3348 case ISD::SSUBSAT: 3349 case ISD::USUBSAT: 3350 Results.push_back(TLI.expandAddSubSat(Node, DAG)); 3351 break; 3352 case ISD::SMULFIX: 3353 case ISD::SMULFIXSAT: 3354 case ISD::UMULFIX: 3355 Results.push_back(TLI.expandFixedPointMul(Node, DAG)); 3356 break; 3357 case ISD::ADDCARRY: 3358 case ISD::SUBCARRY: { 3359 SDValue LHS = Node->getOperand(0); 3360 SDValue RHS = Node->getOperand(1); 3361 SDValue Carry = Node->getOperand(2); 3362 3363 bool IsAdd = Node->getOpcode() == ISD::ADDCARRY; 3364 3365 // Initial add of the 2 operands. 3366 unsigned Op = IsAdd ? ISD::ADD : ISD::SUB; 3367 EVT VT = LHS.getValueType(); 3368 SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS); 3369 3370 // Initial check for overflow. 3371 EVT CarryType = Node->getValueType(1); 3372 EVT SetCCType = getSetCCResultType(Node->getValueType(0)); 3373 ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT; 3374 SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC); 3375 3376 // Add of the sum and the carry. 3377 SDValue CarryExt = 3378 DAG.getZeroExtendInReg(DAG.getZExtOrTrunc(Carry, dl, VT), dl, MVT::i1); 3379 SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt); 3380 3381 // Second check for overflow. If we are adding, we can only overflow if the 3382 // initial sum is all 1s ang the carry is set, resulting in a new sum of 0. 3383 // If we are subtracting, we can only overflow if the initial sum is 0 and 3384 // the carry is set, resulting in a new sum of all 1s. 3385 SDValue Zero = DAG.getConstant(0, dl, VT); 3386 SDValue Overflow2 = 3387 IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ) 3388 : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ); 3389 Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2, 3390 DAG.getZExtOrTrunc(Carry, dl, SetCCType)); 3391 3392 SDValue ResultCarry = 3393 DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2); 3394 3395 Results.push_back(Sum2); 3396 Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT)); 3397 break; 3398 } 3399 case ISD::SADDO: 3400 case ISD::SSUBO: { 3401 SDValue Result, Overflow; 3402 TLI.expandSADDSUBO(Node, Result, Overflow, DAG); 3403 Results.push_back(Result); 3404 Results.push_back(Overflow); 3405 break; 3406 } 3407 case ISD::UADDO: 3408 case ISD::USUBO: { 3409 SDValue Result, Overflow; 3410 TLI.expandUADDSUBO(Node, Result, Overflow, DAG); 3411 Results.push_back(Result); 3412 Results.push_back(Overflow); 3413 break; 3414 } 3415 case ISD::UMULO: 3416 case ISD::SMULO: { 3417 SDValue Result, Overflow; 3418 if (TLI.expandMULO(Node, Result, Overflow, DAG)) { 3419 Results.push_back(Result); 3420 Results.push_back(Overflow); 3421 } 3422 break; 3423 } 3424 case ISD::BUILD_PAIR: { 3425 EVT PairTy = Node->getValueType(0); 3426 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0)); 3427 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1)); 3428 Tmp2 = DAG.getNode( 3429 ISD::SHL, dl, PairTy, Tmp2, 3430 DAG.getConstant(PairTy.getSizeInBits() / 2, dl, 3431 TLI.getShiftAmountTy(PairTy, DAG.getDataLayout()))); 3432 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2)); 3433 break; 3434 } 3435 case ISD::SELECT: 3436 Tmp1 = Node->getOperand(0); 3437 Tmp2 = Node->getOperand(1); 3438 Tmp3 = Node->getOperand(2); 3439 if (Tmp1.getOpcode() == ISD::SETCC) { 3440 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), 3441 Tmp2, Tmp3, 3442 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 3443 } else { 3444 Tmp1 = DAG.getSelectCC(dl, Tmp1, 3445 DAG.getConstant(0, dl, Tmp1.getValueType()), 3446 Tmp2, Tmp3, ISD::SETNE); 3447 } 3448 Results.push_back(Tmp1); 3449 break; 3450 case ISD::BR_JT: { 3451 SDValue Chain = Node->getOperand(0); 3452 SDValue Table = Node->getOperand(1); 3453 SDValue Index = Node->getOperand(2); 3454 3455 const DataLayout &TD = DAG.getDataLayout(); 3456 EVT PTy = TLI.getPointerTy(TD); 3457 3458 unsigned EntrySize = 3459 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD); 3460 3461 // For power-of-two jumptable entry sizes convert multiplication to a shift. 3462 // This transformation needs to be done here since otherwise the MIPS 3463 // backend will end up emitting a three instruction multiply sequence 3464 // instead of a single shift and MSP430 will call a runtime function. 3465 if (llvm::isPowerOf2_32(EntrySize)) 3466 Index = DAG.getNode( 3467 ISD::SHL, dl, Index.getValueType(), Index, 3468 DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType())); 3469 else 3470 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index, 3471 DAG.getConstant(EntrySize, dl, Index.getValueType())); 3472 SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(), 3473 Index, Table); 3474 3475 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); 3476 SDValue LD = DAG.getExtLoad( 3477 ISD::SEXTLOAD, dl, PTy, Chain, Addr, 3478 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT); 3479 Addr = LD; 3480 if (TLI.isJumpTableRelative()) { 3481 // For PIC, the sequence is: 3482 // BRIND(load(Jumptable + index) + RelocBase) 3483 // RelocBase can be JumpTable, GOT or some sort of global base. 3484 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, 3485 TLI.getPICJumpTableRelocBase(Table, DAG)); 3486 } 3487 3488 Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, DAG); 3489 Results.push_back(Tmp1); 3490 break; 3491 } 3492 case ISD::BRCOND: 3493 // Expand brcond's setcc into its constituent parts and create a BR_CC 3494 // Node. 3495 Tmp1 = Node->getOperand(0); 3496 Tmp2 = Node->getOperand(1); 3497 if (Tmp2.getOpcode() == ISD::SETCC) { 3498 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, 3499 Tmp1, Tmp2.getOperand(2), 3500 Tmp2.getOperand(0), Tmp2.getOperand(1), 3501 Node->getOperand(2)); 3502 } else { 3503 // We test only the i1 bit. Skip the AND if UNDEF or another AND. 3504 if (Tmp2.isUndef() || 3505 (Tmp2.getOpcode() == ISD::AND && 3506 isa<ConstantSDNode>(Tmp2.getOperand(1)) && 3507 cast<ConstantSDNode>(Tmp2.getOperand(1))->getZExtValue() == 1)) 3508 Tmp3 = Tmp2; 3509 else 3510 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2, 3511 DAG.getConstant(1, dl, Tmp2.getValueType())); 3512 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, 3513 DAG.getCondCode(ISD::SETNE), Tmp3, 3514 DAG.getConstant(0, dl, Tmp3.getValueType()), 3515 Node->getOperand(2)); 3516 } 3517 Results.push_back(Tmp1); 3518 break; 3519 case ISD::SETCC: { 3520 Tmp1 = Node->getOperand(0); 3521 Tmp2 = Node->getOperand(1); 3522 Tmp3 = Node->getOperand(2); 3523 bool Legalized = LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, 3524 Tmp3, NeedInvert, dl); 3525 3526 if (Legalized) { 3527 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 3528 // condition code, create a new SETCC node. 3529 if (Tmp3.getNode()) 3530 Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), 3531 Tmp1, Tmp2, Tmp3); 3532 3533 // If we expanded the SETCC by inverting the condition code, then wrap 3534 // the existing SETCC in a NOT to restore the intended condition. 3535 if (NeedInvert) 3536 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0)); 3537 3538 Results.push_back(Tmp1); 3539 break; 3540 } 3541 3542 // Otherwise, SETCC for the given comparison type must be completely 3543 // illegal; expand it into a SELECT_CC. 3544 EVT VT = Node->getValueType(0); 3545 int TrueValue; 3546 switch (TLI.getBooleanContents(Tmp1.getValueType())) { 3547 case TargetLowering::ZeroOrOneBooleanContent: 3548 case TargetLowering::UndefinedBooleanContent: 3549 TrueValue = 1; 3550 break; 3551 case TargetLowering::ZeroOrNegativeOneBooleanContent: 3552 TrueValue = -1; 3553 break; 3554 } 3555 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, 3556 DAG.getConstant(TrueValue, dl, VT), 3557 DAG.getConstant(0, dl, VT), 3558 Tmp3); 3559 Results.push_back(Tmp1); 3560 break; 3561 } 3562 case ISD::SELECT_CC: { 3563 Tmp1 = Node->getOperand(0); // LHS 3564 Tmp2 = Node->getOperand(1); // RHS 3565 Tmp3 = Node->getOperand(2); // True 3566 Tmp4 = Node->getOperand(3); // False 3567 EVT VT = Node->getValueType(0); 3568 SDValue CC = Node->getOperand(4); 3569 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get(); 3570 3571 if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) { 3572 // If the condition code is legal, then we need to expand this 3573 // node using SETCC and SELECT. 3574 EVT CmpVT = Tmp1.getValueType(); 3575 assert(!TLI.isOperationExpand(ISD::SELECT, VT) && 3576 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be " 3577 "expanded."); 3578 EVT CCVT = 3579 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT); 3580 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC); 3581 Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4)); 3582 break; 3583 } 3584 3585 // SELECT_CC is legal, so the condition code must not be. 3586 bool Legalized = false; 3587 // Try to legalize by inverting the condition. This is for targets that 3588 // might support an ordered version of a condition, but not the unordered 3589 // version (or vice versa). 3590 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, 3591 Tmp1.getValueType().isInteger()); 3592 if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) { 3593 // Use the new condition code and swap true and false 3594 Legalized = true; 3595 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC); 3596 } else { 3597 // If The inverse is not legal, then try to swap the arguments using 3598 // the inverse condition code. 3599 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC); 3600 if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) { 3601 // The swapped inverse condition is legal, so swap true and false, 3602 // lhs and rhs. 3603 Legalized = true; 3604 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC); 3605 } 3606 } 3607 3608 if (!Legalized) { 3609 Legalized = LegalizeSetCCCondCode( 3610 getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, NeedInvert, 3611 dl); 3612 3613 assert(Legalized && "Can't legalize SELECT_CC with legal condition!"); 3614 3615 // If we expanded the SETCC by inverting the condition code, then swap 3616 // the True/False operands to match. 3617 if (NeedInvert) 3618 std::swap(Tmp3, Tmp4); 3619 3620 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 3621 // condition code, create a new SELECT_CC node. 3622 if (CC.getNode()) { 3623 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), 3624 Tmp1, Tmp2, Tmp3, Tmp4, CC); 3625 } else { 3626 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType()); 3627 CC = DAG.getCondCode(ISD::SETNE); 3628 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, 3629 Tmp2, Tmp3, Tmp4, CC); 3630 } 3631 } 3632 Results.push_back(Tmp1); 3633 break; 3634 } 3635 case ISD::BR_CC: { 3636 Tmp1 = Node->getOperand(0); // Chain 3637 Tmp2 = Node->getOperand(2); // LHS 3638 Tmp3 = Node->getOperand(3); // RHS 3639 Tmp4 = Node->getOperand(1); // CC 3640 3641 bool Legalized = LegalizeSetCCCondCode(getSetCCResultType( 3642 Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, NeedInvert, dl); 3643 (void)Legalized; 3644 assert(Legalized && "Can't legalize BR_CC with legal condition!"); 3645 3646 assert(!NeedInvert && "Don't know how to invert BR_CC!"); 3647 3648 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC 3649 // node. 3650 if (Tmp4.getNode()) { 3651 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, 3652 Tmp4, Tmp2, Tmp3, Node->getOperand(4)); 3653 } else { 3654 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType()); 3655 Tmp4 = DAG.getCondCode(ISD::SETNE); 3656 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, 3657 Tmp2, Tmp3, Node->getOperand(4)); 3658 } 3659 Results.push_back(Tmp1); 3660 break; 3661 } 3662 case ISD::BUILD_VECTOR: 3663 Results.push_back(ExpandBUILD_VECTOR(Node)); 3664 break; 3665 case ISD::SRA: 3666 case ISD::SRL: 3667 case ISD::SHL: { 3668 // Scalarize vector SRA/SRL/SHL. 3669 EVT VT = Node->getValueType(0); 3670 assert(VT.isVector() && "Unable to legalize non-vector shift"); 3671 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal"); 3672 unsigned NumElem = VT.getVectorNumElements(); 3673 3674 SmallVector<SDValue, 8> Scalars; 3675 for (unsigned Idx = 0; Idx < NumElem; Idx++) { 3676 SDValue Ex = DAG.getNode( 3677 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(0), 3678 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 3679 SDValue Sh = DAG.getNode( 3680 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(1), 3681 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 3682 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl, 3683 VT.getScalarType(), Ex, Sh)); 3684 } 3685 3686 SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars); 3687 ReplaceNode(SDValue(Node, 0), Result); 3688 break; 3689 } 3690 case ISD::VECREDUCE_FADD: 3691 case ISD::VECREDUCE_FMUL: 3692 case ISD::VECREDUCE_ADD: 3693 case ISD::VECREDUCE_MUL: 3694 case ISD::VECREDUCE_AND: 3695 case ISD::VECREDUCE_OR: 3696 case ISD::VECREDUCE_XOR: 3697 case ISD::VECREDUCE_SMAX: 3698 case ISD::VECREDUCE_SMIN: 3699 case ISD::VECREDUCE_UMAX: 3700 case ISD::VECREDUCE_UMIN: 3701 case ISD::VECREDUCE_FMAX: 3702 case ISD::VECREDUCE_FMIN: 3703 Results.push_back(TLI.expandVecReduce(Node, DAG)); 3704 break; 3705 case ISD::GLOBAL_OFFSET_TABLE: 3706 case ISD::GlobalAddress: 3707 case ISD::GlobalTLSAddress: 3708 case ISD::ExternalSymbol: 3709 case ISD::ConstantPool: 3710 case ISD::JumpTable: 3711 case ISD::INTRINSIC_W_CHAIN: 3712 case ISD::INTRINSIC_WO_CHAIN: 3713 case ISD::INTRINSIC_VOID: 3714 // FIXME: Custom lowering for these operations shouldn't return null! 3715 break; 3716 } 3717 3718 // Replace the original node with the legalized result. 3719 if (Results.empty()) { 3720 LLVM_DEBUG(dbgs() << "Cannot expand node\n"); 3721 return false; 3722 } 3723 3724 LLVM_DEBUG(dbgs() << "Successfully expanded node\n"); 3725 ReplaceNode(Node, Results.data()); 3726 return true; 3727 } 3728 3729 void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) { 3730 LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n"); 3731 SmallVector<SDValue, 8> Results; 3732 SDLoc dl(Node); 3733 // FIXME: Check flags on the node to see if we can use a finite call. 3734 bool CanUseFiniteLibCall = TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath; 3735 unsigned Opc = Node->getOpcode(); 3736 switch (Opc) { 3737 case ISD::ATOMIC_FENCE: { 3738 // If the target didn't lower this, lower it to '__sync_synchronize()' call 3739 // FIXME: handle "fence singlethread" more efficiently. 3740 TargetLowering::ArgListTy Args; 3741 3742 TargetLowering::CallLoweringInfo CLI(DAG); 3743 CLI.setDebugLoc(dl) 3744 .setChain(Node->getOperand(0)) 3745 .setLibCallee( 3746 CallingConv::C, Type::getVoidTy(*DAG.getContext()), 3747 DAG.getExternalSymbol("__sync_synchronize", 3748 TLI.getPointerTy(DAG.getDataLayout())), 3749 std::move(Args)); 3750 3751 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 3752 3753 Results.push_back(CallResult.second); 3754 break; 3755 } 3756 // By default, atomic intrinsics are marked Legal and lowered. Targets 3757 // which don't support them directly, however, may want libcalls, in which 3758 // case they mark them Expand, and we get here. 3759 case ISD::ATOMIC_SWAP: 3760 case ISD::ATOMIC_LOAD_ADD: 3761 case ISD::ATOMIC_LOAD_SUB: 3762 case ISD::ATOMIC_LOAD_AND: 3763 case ISD::ATOMIC_LOAD_CLR: 3764 case ISD::ATOMIC_LOAD_OR: 3765 case ISD::ATOMIC_LOAD_XOR: 3766 case ISD::ATOMIC_LOAD_NAND: 3767 case ISD::ATOMIC_LOAD_MIN: 3768 case ISD::ATOMIC_LOAD_MAX: 3769 case ISD::ATOMIC_LOAD_UMIN: 3770 case ISD::ATOMIC_LOAD_UMAX: 3771 case ISD::ATOMIC_CMP_SWAP: { 3772 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT(); 3773 RTLIB::Libcall LC = RTLIB::getSYNC(Opc, VT); 3774 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!"); 3775 3776 std::pair<SDValue, SDValue> Tmp = ExpandChainLibCall(LC, Node, false); 3777 Results.push_back(Tmp.first); 3778 Results.push_back(Tmp.second); 3779 break; 3780 } 3781 case ISD::TRAP: { 3782 // If this operation is not supported, lower it to 'abort()' call 3783 TargetLowering::ArgListTy Args; 3784 TargetLowering::CallLoweringInfo CLI(DAG); 3785 CLI.setDebugLoc(dl) 3786 .setChain(Node->getOperand(0)) 3787 .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), 3788 DAG.getExternalSymbol( 3789 "abort", TLI.getPointerTy(DAG.getDataLayout())), 3790 std::move(Args)); 3791 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 3792 3793 Results.push_back(CallResult.second); 3794 break; 3795 } 3796 case ISD::FMINNUM: 3797 case ISD::STRICT_FMINNUM: 3798 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64, 3799 RTLIB::FMIN_F80, RTLIB::FMIN_F128, 3800 RTLIB::FMIN_PPCF128)); 3801 break; 3802 case ISD::FMAXNUM: 3803 case ISD::STRICT_FMAXNUM: 3804 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64, 3805 RTLIB::FMAX_F80, RTLIB::FMAX_F128, 3806 RTLIB::FMAX_PPCF128)); 3807 break; 3808 case ISD::FSQRT: 3809 case ISD::STRICT_FSQRT: 3810 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64, 3811 RTLIB::SQRT_F80, RTLIB::SQRT_F128, 3812 RTLIB::SQRT_PPCF128)); 3813 break; 3814 case ISD::FCBRT: 3815 Results.push_back(ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64, 3816 RTLIB::CBRT_F80, RTLIB::CBRT_F128, 3817 RTLIB::CBRT_PPCF128)); 3818 break; 3819 case ISD::FSIN: 3820 case ISD::STRICT_FSIN: 3821 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64, 3822 RTLIB::SIN_F80, RTLIB::SIN_F128, 3823 RTLIB::SIN_PPCF128)); 3824 break; 3825 case ISD::FCOS: 3826 case ISD::STRICT_FCOS: 3827 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64, 3828 RTLIB::COS_F80, RTLIB::COS_F128, 3829 RTLIB::COS_PPCF128)); 3830 break; 3831 case ISD::FSINCOS: 3832 // Expand into sincos libcall. 3833 ExpandSinCosLibCall(Node, Results); 3834 break; 3835 case ISD::FLOG: 3836 case ISD::STRICT_FLOG: 3837 if (CanUseFiniteLibCall && DAG.getLibInfo().has(LibFunc_log_finite)) 3838 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_FINITE_F32, 3839 RTLIB::LOG_FINITE_F64, 3840 RTLIB::LOG_FINITE_F80, 3841 RTLIB::LOG_FINITE_F128, 3842 RTLIB::LOG_FINITE_PPCF128)); 3843 else 3844 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, 3845 RTLIB::LOG_F80, RTLIB::LOG_F128, 3846 RTLIB::LOG_PPCF128)); 3847 break; 3848 case ISD::FLOG2: 3849 case ISD::STRICT_FLOG2: 3850 if (CanUseFiniteLibCall && DAG.getLibInfo().has(LibFunc_log2_finite)) 3851 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_FINITE_F32, 3852 RTLIB::LOG2_FINITE_F64, 3853 RTLIB::LOG2_FINITE_F80, 3854 RTLIB::LOG2_FINITE_F128, 3855 RTLIB::LOG2_FINITE_PPCF128)); 3856 else 3857 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, 3858 RTLIB::LOG2_F80, RTLIB::LOG2_F128, 3859 RTLIB::LOG2_PPCF128)); 3860 break; 3861 case ISD::FLOG10: 3862 case ISD::STRICT_FLOG10: 3863 if (CanUseFiniteLibCall && DAG.getLibInfo().has(LibFunc_log10_finite)) 3864 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_FINITE_F32, 3865 RTLIB::LOG10_FINITE_F64, 3866 RTLIB::LOG10_FINITE_F80, 3867 RTLIB::LOG10_FINITE_F128, 3868 RTLIB::LOG10_FINITE_PPCF128)); 3869 else 3870 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, 3871 RTLIB::LOG10_F80, RTLIB::LOG10_F128, 3872 RTLIB::LOG10_PPCF128)); 3873 break; 3874 case ISD::FEXP: 3875 case ISD::STRICT_FEXP: 3876 if (CanUseFiniteLibCall && DAG.getLibInfo().has(LibFunc_exp_finite)) 3877 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_FINITE_F32, 3878 RTLIB::EXP_FINITE_F64, 3879 RTLIB::EXP_FINITE_F80, 3880 RTLIB::EXP_FINITE_F128, 3881 RTLIB::EXP_FINITE_PPCF128)); 3882 else 3883 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, 3884 RTLIB::EXP_F80, RTLIB::EXP_F128, 3885 RTLIB::EXP_PPCF128)); 3886 break; 3887 case ISD::FEXP2: 3888 case ISD::STRICT_FEXP2: 3889 if (CanUseFiniteLibCall && DAG.getLibInfo().has(LibFunc_exp2_finite)) 3890 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_FINITE_F32, 3891 RTLIB::EXP2_FINITE_F64, 3892 RTLIB::EXP2_FINITE_F80, 3893 RTLIB::EXP2_FINITE_F128, 3894 RTLIB::EXP2_FINITE_PPCF128)); 3895 else 3896 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, 3897 RTLIB::EXP2_F80, RTLIB::EXP2_F128, 3898 RTLIB::EXP2_PPCF128)); 3899 break; 3900 case ISD::FTRUNC: 3901 case ISD::STRICT_FTRUNC: 3902 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, 3903 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128, 3904 RTLIB::TRUNC_PPCF128)); 3905 break; 3906 case ISD::FFLOOR: 3907 case ISD::STRICT_FFLOOR: 3908 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, 3909 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128, 3910 RTLIB::FLOOR_PPCF128)); 3911 break; 3912 case ISD::FCEIL: 3913 case ISD::STRICT_FCEIL: 3914 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64, 3915 RTLIB::CEIL_F80, RTLIB::CEIL_F128, 3916 RTLIB::CEIL_PPCF128)); 3917 break; 3918 case ISD::FRINT: 3919 case ISD::STRICT_FRINT: 3920 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64, 3921 RTLIB::RINT_F80, RTLIB::RINT_F128, 3922 RTLIB::RINT_PPCF128)); 3923 break; 3924 case ISD::FNEARBYINT: 3925 case ISD::STRICT_FNEARBYINT: 3926 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32, 3927 RTLIB::NEARBYINT_F64, 3928 RTLIB::NEARBYINT_F80, 3929 RTLIB::NEARBYINT_F128, 3930 RTLIB::NEARBYINT_PPCF128)); 3931 break; 3932 case ISD::FROUND: 3933 case ISD::STRICT_FROUND: 3934 Results.push_back(ExpandFPLibCall(Node, RTLIB::ROUND_F32, 3935 RTLIB::ROUND_F64, 3936 RTLIB::ROUND_F80, 3937 RTLIB::ROUND_F128, 3938 RTLIB::ROUND_PPCF128)); 3939 break; 3940 case ISD::FPOWI: 3941 case ISD::STRICT_FPOWI: 3942 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64, 3943 RTLIB::POWI_F80, RTLIB::POWI_F128, 3944 RTLIB::POWI_PPCF128)); 3945 break; 3946 case ISD::FPOW: 3947 case ISD::STRICT_FPOW: 3948 if (CanUseFiniteLibCall && DAG.getLibInfo().has(LibFunc_pow_finite)) 3949 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_FINITE_F32, 3950 RTLIB::POW_FINITE_F64, 3951 RTLIB::POW_FINITE_F80, 3952 RTLIB::POW_FINITE_F128, 3953 RTLIB::POW_FINITE_PPCF128)); 3954 else 3955 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, 3956 RTLIB::POW_F80, RTLIB::POW_F128, 3957 RTLIB::POW_PPCF128)); 3958 break; 3959 case ISD::FDIV: 3960 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64, 3961 RTLIB::DIV_F80, RTLIB::DIV_F128, 3962 RTLIB::DIV_PPCF128)); 3963 break; 3964 case ISD::FREM: 3965 case ISD::STRICT_FREM: 3966 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64, 3967 RTLIB::REM_F80, RTLIB::REM_F128, 3968 RTLIB::REM_PPCF128)); 3969 break; 3970 case ISD::FMA: 3971 case ISD::STRICT_FMA: 3972 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64, 3973 RTLIB::FMA_F80, RTLIB::FMA_F128, 3974 RTLIB::FMA_PPCF128)); 3975 break; 3976 case ISD::FADD: 3977 Results.push_back(ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64, 3978 RTLIB::ADD_F80, RTLIB::ADD_F128, 3979 RTLIB::ADD_PPCF128)); 3980 break; 3981 case ISD::FMUL: 3982 Results.push_back(ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64, 3983 RTLIB::MUL_F80, RTLIB::MUL_F128, 3984 RTLIB::MUL_PPCF128)); 3985 break; 3986 case ISD::FP16_TO_FP: 3987 if (Node->getValueType(0) == MVT::f32) { 3988 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false)); 3989 } 3990 break; 3991 case ISD::FP_TO_FP16: { 3992 RTLIB::Libcall LC = 3993 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16); 3994 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16"); 3995 Results.push_back(ExpandLibCall(LC, Node, false)); 3996 break; 3997 } 3998 case ISD::FSUB: 3999 Results.push_back(ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64, 4000 RTLIB::SUB_F80, RTLIB::SUB_F128, 4001 RTLIB::SUB_PPCF128)); 4002 break; 4003 case ISD::SREM: 4004 Results.push_back(ExpandIntLibCall(Node, true, 4005 RTLIB::SREM_I8, 4006 RTLIB::SREM_I16, RTLIB::SREM_I32, 4007 RTLIB::SREM_I64, RTLIB::SREM_I128)); 4008 break; 4009 case ISD::UREM: 4010 Results.push_back(ExpandIntLibCall(Node, false, 4011 RTLIB::UREM_I8, 4012 RTLIB::UREM_I16, RTLIB::UREM_I32, 4013 RTLIB::UREM_I64, RTLIB::UREM_I128)); 4014 break; 4015 case ISD::SDIV: 4016 Results.push_back(ExpandIntLibCall(Node, true, 4017 RTLIB::SDIV_I8, 4018 RTLIB::SDIV_I16, RTLIB::SDIV_I32, 4019 RTLIB::SDIV_I64, RTLIB::SDIV_I128)); 4020 break; 4021 case ISD::UDIV: 4022 Results.push_back(ExpandIntLibCall(Node, false, 4023 RTLIB::UDIV_I8, 4024 RTLIB::UDIV_I16, RTLIB::UDIV_I32, 4025 RTLIB::UDIV_I64, RTLIB::UDIV_I128)); 4026 break; 4027 case ISD::SDIVREM: 4028 case ISD::UDIVREM: 4029 // Expand into divrem libcall 4030 ExpandDivRemLibCall(Node, Results); 4031 break; 4032 case ISD::MUL: 4033 Results.push_back(ExpandIntLibCall(Node, false, 4034 RTLIB::MUL_I8, 4035 RTLIB::MUL_I16, RTLIB::MUL_I32, 4036 RTLIB::MUL_I64, RTLIB::MUL_I128)); 4037 break; 4038 case ISD::CTLZ_ZERO_UNDEF: 4039 switch (Node->getSimpleValueType(0).SimpleTy) { 4040 default: 4041 llvm_unreachable("LibCall explicitly requested, but not available"); 4042 case MVT::i32: 4043 Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false)); 4044 break; 4045 case MVT::i64: 4046 Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false)); 4047 break; 4048 case MVT::i128: 4049 Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false)); 4050 break; 4051 } 4052 break; 4053 } 4054 4055 // Replace the original node with the legalized result. 4056 if (!Results.empty()) { 4057 LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n"); 4058 ReplaceNode(Node, Results.data()); 4059 } else 4060 LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n"); 4061 } 4062 4063 // Determine the vector type to use in place of an original scalar element when 4064 // promoting equally sized vectors. 4065 static MVT getPromotedVectorElementType(const TargetLowering &TLI, 4066 MVT EltVT, MVT NewEltVT) { 4067 unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits(); 4068 MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt); 4069 assert(TLI.isTypeLegal(MidVT) && "unexpected"); 4070 return MidVT; 4071 } 4072 4073 void SelectionDAGLegalize::PromoteNode(SDNode *Node) { 4074 LLVM_DEBUG(dbgs() << "Trying to promote node\n"); 4075 SmallVector<SDValue, 8> Results; 4076 MVT OVT = Node->getSimpleValueType(0); 4077 if (Node->getOpcode() == ISD::UINT_TO_FP || 4078 Node->getOpcode() == ISD::SINT_TO_FP || 4079 Node->getOpcode() == ISD::SETCC || 4080 Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT || 4081 Node->getOpcode() == ISD::INSERT_VECTOR_ELT) { 4082 OVT = Node->getOperand(0).getSimpleValueType(); 4083 } 4084 if (Node->getOpcode() == ISD::BR_CC) 4085 OVT = Node->getOperand(2).getSimpleValueType(); 4086 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 4087 SDLoc dl(Node); 4088 SDValue Tmp1, Tmp2, Tmp3; 4089 switch (Node->getOpcode()) { 4090 case ISD::CTTZ: 4091 case ISD::CTTZ_ZERO_UNDEF: 4092 case ISD::CTLZ: 4093 case ISD::CTLZ_ZERO_UNDEF: 4094 case ISD::CTPOP: 4095 // Zero extend the argument. 4096 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 4097 if (Node->getOpcode() == ISD::CTTZ) { 4098 // The count is the same in the promoted type except if the original 4099 // value was zero. This can be handled by setting the bit just off 4100 // the top of the original type. 4101 auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(), 4102 OVT.getSizeInBits()); 4103 Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1, 4104 DAG.getConstant(TopBit, dl, NVT)); 4105 } 4106 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is 4107 // already the correct result. 4108 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 4109 if (Node->getOpcode() == ISD::CTLZ || 4110 Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) { 4111 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 4112 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, 4113 DAG.getConstant(NVT.getSizeInBits() - 4114 OVT.getSizeInBits(), dl, NVT)); 4115 } 4116 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 4117 break; 4118 case ISD::BITREVERSE: 4119 case ISD::BSWAP: { 4120 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); 4121 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 4122 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 4123 Tmp1 = DAG.getNode( 4124 ISD::SRL, dl, NVT, Tmp1, 4125 DAG.getConstant(DiffBits, dl, 4126 TLI.getShiftAmountTy(NVT, DAG.getDataLayout()))); 4127 4128 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 4129 break; 4130 } 4131 case ISD::FP_TO_UINT: 4132 case ISD::FP_TO_SINT: 4133 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0), 4134 Node->getOpcode() == ISD::FP_TO_SINT, dl); 4135 Results.push_back(Tmp1); 4136 break; 4137 case ISD::UINT_TO_FP: 4138 case ISD::SINT_TO_FP: 4139 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0), 4140 Node->getOpcode() == ISD::SINT_TO_FP, dl); 4141 Results.push_back(Tmp1); 4142 break; 4143 case ISD::VAARG: { 4144 SDValue Chain = Node->getOperand(0); // Get the chain. 4145 SDValue Ptr = Node->getOperand(1); // Get the pointer. 4146 4147 unsigned TruncOp; 4148 if (OVT.isVector()) { 4149 TruncOp = ISD::BITCAST; 4150 } else { 4151 assert(OVT.isInteger() 4152 && "VAARG promotion is supported only for vectors or integer types"); 4153 TruncOp = ISD::TRUNCATE; 4154 } 4155 4156 // Perform the larger operation, then convert back 4157 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2), 4158 Node->getConstantOperandVal(3)); 4159 Chain = Tmp1.getValue(1); 4160 4161 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1); 4162 4163 // Modified the chain result - switch anything that used the old chain to 4164 // use the new one. 4165 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2); 4166 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 4167 if (UpdatedNodes) { 4168 UpdatedNodes->insert(Tmp2.getNode()); 4169 UpdatedNodes->insert(Chain.getNode()); 4170 } 4171 ReplacedNode(Node); 4172 break; 4173 } 4174 case ISD::MUL: 4175 case ISD::SDIV: 4176 case ISD::SREM: 4177 case ISD::UDIV: 4178 case ISD::UREM: 4179 case ISD::AND: 4180 case ISD::OR: 4181 case ISD::XOR: { 4182 unsigned ExtOp, TruncOp; 4183 if (OVT.isVector()) { 4184 ExtOp = ISD::BITCAST; 4185 TruncOp = ISD::BITCAST; 4186 } else { 4187 assert(OVT.isInteger() && "Cannot promote logic operation"); 4188 4189 switch (Node->getOpcode()) { 4190 default: 4191 ExtOp = ISD::ANY_EXTEND; 4192 break; 4193 case ISD::SDIV: 4194 case ISD::SREM: 4195 ExtOp = ISD::SIGN_EXTEND; 4196 break; 4197 case ISD::UDIV: 4198 case ISD::UREM: 4199 ExtOp = ISD::ZERO_EXTEND; 4200 break; 4201 } 4202 TruncOp = ISD::TRUNCATE; 4203 } 4204 // Promote each of the values to the new type. 4205 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 4206 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4207 // Perform the larger operation, then convert back 4208 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 4209 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1)); 4210 break; 4211 } 4212 case ISD::UMUL_LOHI: 4213 case ISD::SMUL_LOHI: { 4214 // Promote to a multiply in a wider integer type. 4215 unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND 4216 : ISD::SIGN_EXTEND; 4217 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 4218 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4219 Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2); 4220 4221 auto &DL = DAG.getDataLayout(); 4222 unsigned OriginalSize = OVT.getScalarSizeInBits(); 4223 Tmp2 = DAG.getNode( 4224 ISD::SRL, dl, NVT, Tmp1, 4225 DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT))); 4226 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 4227 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2)); 4228 break; 4229 } 4230 case ISD::SELECT: { 4231 unsigned ExtOp, TruncOp; 4232 if (Node->getValueType(0).isVector() || 4233 Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) { 4234 ExtOp = ISD::BITCAST; 4235 TruncOp = ISD::BITCAST; 4236 } else if (Node->getValueType(0).isInteger()) { 4237 ExtOp = ISD::ANY_EXTEND; 4238 TruncOp = ISD::TRUNCATE; 4239 } else { 4240 ExtOp = ISD::FP_EXTEND; 4241 TruncOp = ISD::FP_ROUND; 4242 } 4243 Tmp1 = Node->getOperand(0); 4244 // Promote each of the values to the new type. 4245 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4246 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 4247 // Perform the larger operation, then round down. 4248 Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3); 4249 if (TruncOp != ISD::FP_ROUND) 4250 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1); 4251 else 4252 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1, 4253 DAG.getIntPtrConstant(0, dl)); 4254 Results.push_back(Tmp1); 4255 break; 4256 } 4257 case ISD::VECTOR_SHUFFLE: { 4258 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 4259 4260 // Cast the two input vectors. 4261 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0)); 4262 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1)); 4263 4264 // Convert the shuffle mask to the right # elements. 4265 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); 4266 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1); 4267 Results.push_back(Tmp1); 4268 break; 4269 } 4270 case ISD::SETCC: { 4271 unsigned ExtOp = ISD::FP_EXTEND; 4272 if (NVT.isInteger()) { 4273 ISD::CondCode CCCode = 4274 cast<CondCodeSDNode>(Node->getOperand(2))->get(); 4275 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4276 } 4277 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 4278 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4279 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), 4280 Tmp1, Tmp2, Node->getOperand(2))); 4281 break; 4282 } 4283 case ISD::BR_CC: { 4284 unsigned ExtOp = ISD::FP_EXTEND; 4285 if (NVT.isInteger()) { 4286 ISD::CondCode CCCode = 4287 cast<CondCodeSDNode>(Node->getOperand(1))->get(); 4288 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4289 } 4290 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 4291 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 4292 Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), 4293 Node->getOperand(0), Node->getOperand(1), 4294 Tmp1, Tmp2, Node->getOperand(4))); 4295 break; 4296 } 4297 case ISD::FADD: 4298 case ISD::FSUB: 4299 case ISD::FMUL: 4300 case ISD::FDIV: 4301 case ISD::FREM: 4302 case ISD::FMINNUM: 4303 case ISD::FMAXNUM: 4304 case ISD::FPOW: 4305 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4306 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 4307 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, 4308 Node->getFlags()); 4309 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 4310 Tmp3, DAG.getIntPtrConstant(0, dl))); 4311 break; 4312 case ISD::FMA: 4313 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4314 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 4315 Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2)); 4316 Results.push_back( 4317 DAG.getNode(ISD::FP_ROUND, dl, OVT, 4318 DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3), 4319 DAG.getIntPtrConstant(0, dl))); 4320 break; 4321 case ISD::FCOPYSIGN: 4322 case ISD::FPOWI: { 4323 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4324 Tmp2 = Node->getOperand(1); 4325 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 4326 4327 // fcopysign doesn't change anything but the sign bit, so 4328 // (fp_round (fcopysign (fpext a), b)) 4329 // is as precise as 4330 // (fp_round (fpext a)) 4331 // which is a no-op. Mark it as a TRUNCating FP_ROUND. 4332 const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN); 4333 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 4334 Tmp3, DAG.getIntPtrConstant(isTrunc, dl))); 4335 break; 4336 } 4337 case ISD::FFLOOR: 4338 case ISD::FCEIL: 4339 case ISD::FRINT: 4340 case ISD::FNEARBYINT: 4341 case ISD::FROUND: 4342 case ISD::FTRUNC: 4343 case ISD::FNEG: 4344 case ISD::FSQRT: 4345 case ISD::FSIN: 4346 case ISD::FCOS: 4347 case ISD::FLOG: 4348 case ISD::FLOG2: 4349 case ISD::FLOG10: 4350 case ISD::FABS: 4351 case ISD::FEXP: 4352 case ISD::FEXP2: 4353 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4354 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 4355 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 4356 Tmp2, DAG.getIntPtrConstant(0, dl))); 4357 break; 4358 case ISD::BUILD_VECTOR: { 4359 MVT EltVT = OVT.getVectorElementType(); 4360 MVT NewEltVT = NVT.getVectorElementType(); 4361 4362 // Handle bitcasts to a different vector type with the same total bit size 4363 // 4364 // e.g. v2i64 = build_vector i64:x, i64:y => v4i32 4365 // => 4366 // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y)) 4367 4368 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 4369 "Invalid promote type for build_vector"); 4370 assert(NewEltVT.bitsLT(EltVT) && "not handled"); 4371 4372 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4373 4374 SmallVector<SDValue, 8> NewOps; 4375 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) { 4376 SDValue Op = Node->getOperand(I); 4377 NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op)); 4378 } 4379 4380 SDLoc SL(Node); 4381 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps); 4382 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 4383 Results.push_back(CvtVec); 4384 break; 4385 } 4386 case ISD::EXTRACT_VECTOR_ELT: { 4387 MVT EltVT = OVT.getVectorElementType(); 4388 MVT NewEltVT = NVT.getVectorElementType(); 4389 4390 // Handle bitcasts to a different vector type with the same total bit size. 4391 // 4392 // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32 4393 // => 4394 // v4i32:castx = bitcast x:v2i64 4395 // 4396 // i64 = bitcast 4397 // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))), 4398 // (i32 (extract_vector_elt castx, (2 * y + 1))) 4399 // 4400 4401 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 4402 "Invalid promote type for extract_vector_elt"); 4403 assert(NewEltVT.bitsLT(EltVT) && "not handled"); 4404 4405 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4406 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 4407 4408 SDValue Idx = Node->getOperand(1); 4409 EVT IdxVT = Idx.getValueType(); 4410 SDLoc SL(Node); 4411 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT); 4412 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 4413 4414 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 4415 4416 SmallVector<SDValue, 8> NewOps; 4417 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 4418 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 4419 SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 4420 4421 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 4422 CastVec, TmpIdx); 4423 NewOps.push_back(Elt); 4424 } 4425 4426 SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps); 4427 Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec)); 4428 break; 4429 } 4430 case ISD::INSERT_VECTOR_ELT: { 4431 MVT EltVT = OVT.getVectorElementType(); 4432 MVT NewEltVT = NVT.getVectorElementType(); 4433 4434 // Handle bitcasts to a different vector type with the same total bit size 4435 // 4436 // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32 4437 // => 4438 // v4i32:castx = bitcast x:v2i64 4439 // v2i32:casty = bitcast y:i64 4440 // 4441 // v2i64 = bitcast 4442 // (v4i32 insert_vector_elt 4443 // (v4i32 insert_vector_elt v4i32:castx, 4444 // (extract_vector_elt casty, 0), 2 * z), 4445 // (extract_vector_elt casty, 1), (2 * z + 1)) 4446 4447 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 4448 "Invalid promote type for insert_vector_elt"); 4449 assert(NewEltVT.bitsLT(EltVT) && "not handled"); 4450 4451 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4452 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 4453 4454 SDValue Val = Node->getOperand(1); 4455 SDValue Idx = Node->getOperand(2); 4456 EVT IdxVT = Idx.getValueType(); 4457 SDLoc SL(Node); 4458 4459 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT); 4460 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 4461 4462 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 4463 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 4464 4465 SDValue NewVec = CastVec; 4466 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 4467 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 4468 SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 4469 4470 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 4471 CastVal, IdxOffset); 4472 4473 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT, 4474 NewVec, Elt, InEltIdx); 4475 } 4476 4477 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec)); 4478 break; 4479 } 4480 case ISD::SCALAR_TO_VECTOR: { 4481 MVT EltVT = OVT.getVectorElementType(); 4482 MVT NewEltVT = NVT.getVectorElementType(); 4483 4484 // Handle bitcasts to different vector type with the same total bit size. 4485 // 4486 // e.g. v2i64 = scalar_to_vector x:i64 4487 // => 4488 // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef) 4489 // 4490 4491 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4492 SDValue Val = Node->getOperand(0); 4493 SDLoc SL(Node); 4494 4495 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 4496 SDValue Undef = DAG.getUNDEF(MidVT); 4497 4498 SmallVector<SDValue, 8> NewElts; 4499 NewElts.push_back(CastVal); 4500 for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I) 4501 NewElts.push_back(Undef); 4502 4503 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts); 4504 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 4505 Results.push_back(CvtVec); 4506 break; 4507 } 4508 case ISD::ATOMIC_SWAP: { 4509 AtomicSDNode *AM = cast<AtomicSDNode>(Node); 4510 SDLoc SL(Node); 4511 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal()); 4512 assert(NVT.getSizeInBits() == OVT.getSizeInBits() && 4513 "unexpected promotion type"); 4514 assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() && 4515 "unexpected atomic_swap with illegal type"); 4516 4517 SDValue NewAtomic 4518 = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, NVT, 4519 DAG.getVTList(NVT, MVT::Other), 4520 { AM->getChain(), AM->getBasePtr(), CastVal }, 4521 AM->getMemOperand()); 4522 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic)); 4523 Results.push_back(NewAtomic.getValue(1)); 4524 break; 4525 } 4526 } 4527 4528 // Replace the original node with the legalized result. 4529 if (!Results.empty()) { 4530 LLVM_DEBUG(dbgs() << "Successfully promoted node\n"); 4531 ReplaceNode(Node, Results.data()); 4532 } else 4533 LLVM_DEBUG(dbgs() << "Could not promote node\n"); 4534 } 4535 4536 /// This is the entry point for the file. 4537 void SelectionDAG::Legalize() { 4538 AssignTopologicalOrder(); 4539 4540 SmallPtrSet<SDNode *, 16> LegalizedNodes; 4541 // Use a delete listener to remove nodes which were deleted during 4542 // legalization from LegalizeNodes. This is needed to handle the situation 4543 // where a new node is allocated by the object pool to the same address of a 4544 // previously deleted node. 4545 DAGNodeDeletedListener DeleteListener( 4546 *this, 4547 [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); }); 4548 4549 SelectionDAGLegalize Legalizer(*this, LegalizedNodes); 4550 4551 // Visit all the nodes. We start in topological order, so that we see 4552 // nodes with their original operands intact. Legalization can produce 4553 // new nodes which may themselves need to be legalized. Iterate until all 4554 // nodes have been legalized. 4555 while (true) { 4556 bool AnyLegalized = false; 4557 for (auto NI = allnodes_end(); NI != allnodes_begin();) { 4558 --NI; 4559 4560 SDNode *N = &*NI; 4561 if (N->use_empty() && N != getRoot().getNode()) { 4562 ++NI; 4563 DeleteNode(N); 4564 continue; 4565 } 4566 4567 if (LegalizedNodes.insert(N).second) { 4568 AnyLegalized = true; 4569 Legalizer.LegalizeOp(N); 4570 4571 if (N->use_empty() && N != getRoot().getNode()) { 4572 ++NI; 4573 DeleteNode(N); 4574 } 4575 } 4576 } 4577 if (!AnyLegalized) 4578 break; 4579 4580 } 4581 4582 // Remove dead nodes now. 4583 RemoveDeadNodes(); 4584 } 4585 4586 bool SelectionDAG::LegalizeOp(SDNode *N, 4587 SmallSetVector<SDNode *, 16> &UpdatedNodes) { 4588 SmallPtrSet<SDNode *, 16> LegalizedNodes; 4589 SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes); 4590 4591 // Directly insert the node in question, and legalize it. This will recurse 4592 // as needed through operands. 4593 LegalizedNodes.insert(N); 4594 Legalizer.LegalizeOp(N); 4595 4596 return LegalizedNodes.count(N); 4597 } 4598