LegalizeDAG.cpp revision 7720cb3823d5b5868f9b88b0127277820edcb562
1//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the SelectionDAG::Legalize method. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/SelectionDAG.h" 15#include "llvm/CodeGen/MachineFunction.h" 16#include "llvm/CodeGen/MachineFrameInfo.h" 17#include "llvm/CodeGen/MachineJumpTableInfo.h" 18#include "llvm/CodeGen/MachineModuleInfo.h" 19#include "llvm/Analysis/DebugInfo.h" 20#include "llvm/CodeGen/PseudoSourceValue.h" 21#include "llvm/Target/TargetFrameInfo.h" 22#include "llvm/Target/TargetLowering.h" 23#include "llvm/Target/TargetData.h" 24#include "llvm/Target/TargetMachine.h" 25#include "llvm/Target/TargetOptions.h" 26#include "llvm/CallingConv.h" 27#include "llvm/Constants.h" 28#include "llvm/DerivedTypes.h" 29#include "llvm/Function.h" 30#include "llvm/GlobalVariable.h" 31#include "llvm/LLVMContext.h" 32#include "llvm/Support/CommandLine.h" 33#include "llvm/Support/Debug.h" 34#include "llvm/Support/MathExtras.h" 35#include "llvm/Support/raw_ostream.h" 36#include "llvm/ADT/DenseMap.h" 37#include "llvm/ADT/SmallVector.h" 38#include "llvm/ADT/SmallPtrSet.h" 39using namespace llvm; 40 41//===----------------------------------------------------------------------===// 42/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and 43/// hacks on it until the target machine can handle it. This involves 44/// eliminating value sizes the machine cannot handle (promoting small sizes to 45/// large sizes or splitting up large values into small values) as well as 46/// eliminating operations the machine cannot handle. 47/// 48/// This code also does a small amount of optimization and recognition of idioms 49/// as part of its processing. For example, if a target does not support a 50/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 51/// will attempt merge setcc and brc instructions into brcc's. 52/// 53namespace { 54class SelectionDAGLegalize { 55 const TargetMachine &TM; 56 const TargetLowering &TLI; 57 SelectionDAG &DAG; 58 CodeGenOpt::Level OptLevel; 59 60 // Libcall insertion helpers. 61 62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been 63 /// legalized. We use this to ensure that calls are properly serialized 64 /// against each other, including inserted libcalls. 65 SDValue LastCALLSEQ_END; 66 67 /// IsLegalizingCall - This member is used *only* for purposes of providing 68 /// helpful assertions that a libcall isn't created while another call is 69 /// being legalized (which could lead to non-serialized call sequences). 70 bool IsLegalizingCall; 71 72 enum LegalizeAction { 73 Legal, // The target natively supports this operation. 74 Promote, // This operation should be executed in a larger type. 75 Expand // Try to expand this to other ops, otherwise use a libcall. 76 }; 77 78 /// ValueTypeActions - This is a bitvector that contains two bits for each 79 /// value type, where the two bits correspond to the LegalizeAction enum. 80 /// This can be queried with "getTypeAction(VT)". 81 TargetLowering::ValueTypeActionImpl ValueTypeActions; 82 83 /// LegalizedNodes - For nodes that are of legal width, and that have more 84 /// than one use, this map indicates what regularized operand to use. This 85 /// allows us to avoid legalizing the same thing more than once. 86 DenseMap<SDValue, SDValue> LegalizedNodes; 87 88 void AddLegalizedOperand(SDValue From, SDValue To) { 89 LegalizedNodes.insert(std::make_pair(From, To)); 90 // If someone requests legalization of the new node, return itself. 91 if (From != To) 92 LegalizedNodes.insert(std::make_pair(To, To)); 93 } 94 95public: 96 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol); 97 98 /// getTypeAction - Return how we should legalize values of this type, either 99 /// it is already legal or we need to expand it into multiple registers of 100 /// smaller integer type, or we need to promote it to a larger type. 101 LegalizeAction getTypeAction(EVT VT) const { 102 return 103 (LegalizeAction)ValueTypeActions.getTypeAction(*DAG.getContext(), VT); 104 } 105 106 /// isTypeLegal - Return true if this type is legal on this target. 107 /// 108 bool isTypeLegal(EVT VT) const { 109 return getTypeAction(VT) == Legal; 110 } 111 112 void LegalizeDAG(); 113 114private: 115 /// LegalizeOp - We know that the specified value has a legal type. 116 /// Recursively ensure that the operands have legal types, then return the 117 /// result. 118 SDValue LegalizeOp(SDValue O); 119 120 SDValue OptimizeFloatStore(StoreSDNode *ST); 121 122 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable 123 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it 124 /// is necessary to spill the vector being inserted into to memory, perform 125 /// the insert there, and then read the result back. 126 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, 127 SDValue Idx, DebugLoc dl); 128 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, 129 SDValue Idx, DebugLoc dl); 130 131 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which 132 /// performs the same shuffe in terms of order or result bytes, but on a type 133 /// whose vector element type is narrower than the original shuffle type. 134 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 135 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl, 136 SDValue N1, SDValue N2, 137 SmallVectorImpl<int> &Mask) const; 138 139 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, 140 SmallPtrSet<SDNode*, 32> &NodesLeadingTo); 141 142 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, 143 DebugLoc dl); 144 145 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned); 146 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32, 147 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80, 148 RTLIB::Libcall Call_PPCF128); 149 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, 150 RTLIB::Libcall Call_I8, 151 RTLIB::Libcall Call_I16, 152 RTLIB::Libcall Call_I32, 153 RTLIB::Libcall Call_I64, 154 RTLIB::Libcall Call_I128); 155 156 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl); 157 SDValue ExpandBUILD_VECTOR(SDNode *Node); 158 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node); 159 void ExpandDYNAMIC_STACKALLOC(SDNode *Node, 160 SmallVectorImpl<SDValue> &Results); 161 SDValue ExpandFCOPYSIGN(SDNode *Node); 162 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT, 163 DebugLoc dl); 164 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned, 165 DebugLoc dl); 166 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned, 167 DebugLoc dl); 168 169 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl); 170 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl); 171 172 SDValue ExpandExtractFromVectorThroughStack(SDValue Op); 173 SDValue ExpandVectorBuildThroughStack(SDNode* Node); 174 175 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results); 176 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results); 177}; 178} 179 180/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which 181/// performs the same shuffe in terms of order or result bytes, but on a type 182/// whose vector element type is narrower than the original shuffle type. 183/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 184SDValue 185SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl, 186 SDValue N1, SDValue N2, 187 SmallVectorImpl<int> &Mask) const { 188 unsigned NumMaskElts = VT.getVectorNumElements(); 189 unsigned NumDestElts = NVT.getVectorNumElements(); 190 unsigned NumEltsGrowth = NumDestElts / NumMaskElts; 191 192 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); 193 194 if (NumEltsGrowth == 1) 195 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]); 196 197 SmallVector<int, 8> NewMask; 198 for (unsigned i = 0; i != NumMaskElts; ++i) { 199 int Idx = Mask[i]; 200 for (unsigned j = 0; j != NumEltsGrowth; ++j) { 201 if (Idx < 0) 202 NewMask.push_back(-1); 203 else 204 NewMask.push_back(Idx * NumEltsGrowth + j); 205 } 206 } 207 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?"); 208 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?"); 209 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]); 210} 211 212SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, 213 CodeGenOpt::Level ol) 214 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()), 215 DAG(dag), OptLevel(ol), 216 ValueTypeActions(TLI.getValueTypeActions()) { 217 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE && 218 "Too many value types for ValueTypeActions to hold!"); 219} 220 221void SelectionDAGLegalize::LegalizeDAG() { 222 LastCALLSEQ_END = DAG.getEntryNode(); 223 IsLegalizingCall = false; 224 225 // The legalize process is inherently a bottom-up recursive process (users 226 // legalize their uses before themselves). Given infinite stack space, we 227 // could just start legalizing on the root and traverse the whole graph. In 228 // practice however, this causes us to run out of stack space on large basic 229 // blocks. To avoid this problem, compute an ordering of the nodes where each 230 // node is only legalized after all of its operands are legalized. 231 DAG.AssignTopologicalOrder(); 232 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 233 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I) 234 LegalizeOp(SDValue(I, 0)); 235 236 // Finally, it's possible the root changed. Get the new root. 237 SDValue OldRoot = DAG.getRoot(); 238 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); 239 DAG.setRoot(LegalizedNodes[OldRoot]); 240 241 LegalizedNodes.clear(); 242 243 // Remove dead nodes now. 244 DAG.RemoveDeadNodes(); 245} 246 247 248/// FindCallEndFromCallStart - Given a chained node that is part of a call 249/// sequence, find the CALLSEQ_END node that terminates the call sequence. 250static SDNode *FindCallEndFromCallStart(SDNode *Node) { 251 if (Node->getOpcode() == ISD::CALLSEQ_END) 252 return Node; 253 if (Node->use_empty()) 254 return 0; // No CallSeqEnd 255 256 // The chain is usually at the end. 257 SDValue TheChain(Node, Node->getNumValues()-1); 258 if (TheChain.getValueType() != MVT::Other) { 259 // Sometimes it's at the beginning. 260 TheChain = SDValue(Node, 0); 261 if (TheChain.getValueType() != MVT::Other) { 262 // Otherwise, hunt for it. 263 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) 264 if (Node->getValueType(i) == MVT::Other) { 265 TheChain = SDValue(Node, i); 266 break; 267 } 268 269 // Otherwise, we walked into a node without a chain. 270 if (TheChain.getValueType() != MVT::Other) 271 return 0; 272 } 273 } 274 275 for (SDNode::use_iterator UI = Node->use_begin(), 276 E = Node->use_end(); UI != E; ++UI) { 277 278 // Make sure to only follow users of our token chain. 279 SDNode *User = *UI; 280 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 281 if (User->getOperand(i) == TheChain) 282 if (SDNode *Result = FindCallEndFromCallStart(User)) 283 return Result; 284 } 285 return 0; 286} 287 288/// FindCallStartFromCallEnd - Given a chained node that is part of a call 289/// sequence, find the CALLSEQ_START node that initiates the call sequence. 290static SDNode *FindCallStartFromCallEnd(SDNode *Node) { 291 assert(Node && "Didn't find callseq_start for a call??"); 292 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; 293 294 assert(Node->getOperand(0).getValueType() == MVT::Other && 295 "Node doesn't have a token chain argument!"); 296 return FindCallStartFromCallEnd(Node->getOperand(0).getNode()); 297} 298 299/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to 300/// see if any uses can reach Dest. If no dest operands can get to dest, 301/// legalize them, legalize ourself, and return false, otherwise, return true. 302/// 303/// Keep track of the nodes we fine that actually do lead to Dest in 304/// NodesLeadingTo. This avoids retraversing them exponential number of times. 305/// 306bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, 307 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) { 308 if (N == Dest) return true; // N certainly leads to Dest :) 309 310 // If we've already processed this node and it does lead to Dest, there is no 311 // need to reprocess it. 312 if (NodesLeadingTo.count(N)) return true; 313 314 // If the first result of this node has been already legalized, then it cannot 315 // reach N. 316 if (LegalizedNodes.count(SDValue(N, 0))) return false; 317 318 // Okay, this node has not already been legalized. Check and legalize all 319 // operands. If none lead to Dest, then we can legalize this node. 320 bool OperandsLeadToDest = false; 321 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 322 OperandsLeadToDest |= // If an operand leads to Dest, so do we. 323 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo); 324 325 if (OperandsLeadToDest) { 326 NodesLeadingTo.insert(N); 327 return true; 328 } 329 330 // Okay, this node looks safe, legalize it and return false. 331 LegalizeOp(SDValue(N, 0)); 332 return false; 333} 334 335/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or 336/// a load from the constant pool. 337static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, 338 SelectionDAG &DAG, const TargetLowering &TLI) { 339 bool Extend = false; 340 DebugLoc dl = CFP->getDebugLoc(); 341 342 // If a FP immediate is precise when represented as a float and if the 343 // target can do an extending load from float to double, we put it into 344 // the constant pool as a float, even if it's is statically typed as a 345 // double. This shrinks FP constants and canonicalizes them for targets where 346 // an FP extending load is the same cost as a normal load (such as on the x87 347 // fp stack or PPC FP unit). 348 EVT VT = CFP->getValueType(0); 349 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue()); 350 if (!UseCP) { 351 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion"); 352 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), 353 (VT == MVT::f64) ? MVT::i64 : MVT::i32); 354 } 355 356 EVT OrigVT = VT; 357 EVT SVT = VT; 358 while (SVT != MVT::f32) { 359 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1); 360 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) && 361 // Only do this if the target has a native EXTLOAD instruction from 362 // smaller type. 363 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) && 364 TLI.ShouldShrinkFPConstant(OrigVT)) { 365 const Type *SType = SVT.getTypeForEVT(*DAG.getContext()); 366 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType)); 367 VT = SVT; 368 Extend = true; 369 } 370 } 371 372 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); 373 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 374 if (Extend) 375 return DAG.getExtLoad(ISD::EXTLOAD, dl, 376 OrigVT, DAG.getEntryNode(), 377 CPIdx, PseudoSourceValue::getConstantPool(), 378 0, VT, false, false, Alignment); 379 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx, 380 PseudoSourceValue::getConstantPool(), 0, false, false, 381 Alignment); 382} 383 384/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores. 385static 386SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG, 387 const TargetLowering &TLI) { 388 SDValue Chain = ST->getChain(); 389 SDValue Ptr = ST->getBasePtr(); 390 SDValue Val = ST->getValue(); 391 EVT VT = Val.getValueType(); 392 int Alignment = ST->getAlignment(); 393 int SVOffset = ST->getSrcValueOffset(); 394 DebugLoc dl = ST->getDebugLoc(); 395 if (ST->getMemoryVT().isFloatingPoint() || 396 ST->getMemoryVT().isVector()) { 397 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); 398 if (TLI.isTypeLegal(intVT)) { 399 // Expand to a bitconvert of the value to the integer type of the 400 // same size, then a (misaligned) int store. 401 // FIXME: Does not handle truncating floating point stores! 402 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val); 403 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(), 404 SVOffset, ST->isVolatile(), ST->isNonTemporal(), 405 Alignment); 406 } else { 407 // Do a (aligned) store to a stack slot, then copy from the stack slot 408 // to the final destination using (unaligned) integer loads and stores. 409 EVT StoredVT = ST->getMemoryVT(); 410 EVT RegVT = 411 TLI.getRegisterType(*DAG.getContext(), 412 EVT::getIntegerVT(*DAG.getContext(), 413 StoredVT.getSizeInBits())); 414 unsigned StoredBytes = StoredVT.getSizeInBits() / 8; 415 unsigned RegBytes = RegVT.getSizeInBits() / 8; 416 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes; 417 418 // Make sure the stack slot is also aligned for the register type. 419 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT); 420 421 // Perform the original store, only redirected to the stack slot. 422 SDValue Store = DAG.getTruncStore(Chain, dl, 423 Val, StackPtr, NULL, 0, StoredVT, 424 false, false, 0); 425 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy()); 426 SmallVector<SDValue, 8> Stores; 427 unsigned Offset = 0; 428 429 // Do all but one copies using the full register width. 430 for (unsigned i = 1; i < NumRegs; i++) { 431 // Load one integer register's worth from the stack slot. 432 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0, 433 false, false, 0); 434 // Store it to the final location. Remember the store. 435 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr, 436 ST->getSrcValue(), SVOffset + Offset, 437 ST->isVolatile(), ST->isNonTemporal(), 438 MinAlign(ST->getAlignment(), Offset))); 439 // Increment the pointers. 440 Offset += RegBytes; 441 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, 442 Increment); 443 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); 444 } 445 446 // The last store may be partial. Do a truncating store. On big-endian 447 // machines this requires an extending load from the stack slot to ensure 448 // that the bits are in the right place. 449 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), 450 8 * (StoredBytes - Offset)); 451 452 // Load from the stack slot. 453 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr, 454 NULL, 0, MemVT, false, false, 0); 455 456 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr, 457 ST->getSrcValue(), SVOffset + Offset, 458 MemVT, ST->isVolatile(), 459 ST->isNonTemporal(), 460 MinAlign(ST->getAlignment(), Offset))); 461 // The order of the stores doesn't matter - say it with a TokenFactor. 462 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0], 463 Stores.size()); 464 } 465 } 466 assert(ST->getMemoryVT().isInteger() && 467 !ST->getMemoryVT().isVector() && 468 "Unaligned store of unknown type."); 469 // Get the half-size VT 470 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext()); 471 int NumBits = NewStoredVT.getSizeInBits(); 472 int IncrementSize = NumBits / 8; 473 474 // Divide the stored value in two parts. 475 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); 476 SDValue Lo = Val; 477 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount); 478 479 // Store the two parts 480 SDValue Store1, Store2; 481 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr, 482 ST->getSrcValue(), SVOffset, NewStoredVT, 483 ST->isVolatile(), ST->isNonTemporal(), Alignment); 484 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 485 DAG.getConstant(IncrementSize, TLI.getPointerTy())); 486 Alignment = MinAlign(Alignment, IncrementSize); 487 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr, 488 ST->getSrcValue(), SVOffset + IncrementSize, 489 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(), 490 Alignment); 491 492 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); 493} 494 495/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads. 496static 497SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG, 498 const TargetLowering &TLI) { 499 int SVOffset = LD->getSrcValueOffset(); 500 SDValue Chain = LD->getChain(); 501 SDValue Ptr = LD->getBasePtr(); 502 EVT VT = LD->getValueType(0); 503 EVT LoadedVT = LD->getMemoryVT(); 504 DebugLoc dl = LD->getDebugLoc(); 505 if (VT.isFloatingPoint() || VT.isVector()) { 506 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits()); 507 if (TLI.isTypeLegal(intVT)) { 508 // Expand to a (misaligned) integer load of the same size, 509 // then bitconvert to floating point or vector. 510 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(), 511 SVOffset, LD->isVolatile(), 512 LD->isNonTemporal(), LD->getAlignment()); 513 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad); 514 if (VT.isFloatingPoint() && LoadedVT != VT) 515 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result); 516 517 SDValue Ops[] = { Result, Chain }; 518 return DAG.getMergeValues(Ops, 2, dl); 519 } else { 520 // Copy the value to a (aligned) stack slot using (unaligned) integer 521 // loads and stores, then do a (aligned) load from the stack slot. 522 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT); 523 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8; 524 unsigned RegBytes = RegVT.getSizeInBits() / 8; 525 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes; 526 527 // Make sure the stack slot is also aligned for the register type. 528 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT); 529 530 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy()); 531 SmallVector<SDValue, 8> Stores; 532 SDValue StackPtr = StackBase; 533 unsigned Offset = 0; 534 535 // Do all but one copies using the full register width. 536 for (unsigned i = 1; i < NumRegs; i++) { 537 // Load one integer register's worth from the original location. 538 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(), 539 SVOffset + Offset, LD->isVolatile(), 540 LD->isNonTemporal(), 541 MinAlign(LD->getAlignment(), Offset)); 542 // Follow the load with a store to the stack slot. Remember the store. 543 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr, 544 NULL, 0, false, false, 0)); 545 // Increment the pointers. 546 Offset += RegBytes; 547 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); 548 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, 549 Increment); 550 } 551 552 // The last copy may be partial. Do an extending load. 553 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), 554 8 * (LoadedBytes - Offset)); 555 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr, 556 LD->getSrcValue(), SVOffset + Offset, 557 MemVT, LD->isVolatile(), 558 LD->isNonTemporal(), 559 MinAlign(LD->getAlignment(), Offset)); 560 // Follow the load with a store to the stack slot. Remember the store. 561 // On big-endian machines this requires a truncating store to ensure 562 // that the bits end up in the right place. 563 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr, 564 NULL, 0, MemVT, false, false, 0)); 565 566 // The order of the stores doesn't matter - say it with a TokenFactor. 567 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0], 568 Stores.size()); 569 570 // Finally, perform the original load only redirected to the stack slot. 571 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase, 572 NULL, 0, LoadedVT, false, false, 0); 573 574 // Callers expect a MERGE_VALUES node. 575 SDValue Ops[] = { Load, TF }; 576 return DAG.getMergeValues(Ops, 2, dl); 577 } 578 } 579 assert(LoadedVT.isInteger() && !LoadedVT.isVector() && 580 "Unaligned load of unsupported type."); 581 582 // Compute the new VT that is half the size of the old one. This is an 583 // integer MVT. 584 unsigned NumBits = LoadedVT.getSizeInBits(); 585 EVT NewLoadedVT; 586 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2); 587 NumBits >>= 1; 588 589 unsigned Alignment = LD->getAlignment(); 590 unsigned IncrementSize = NumBits / 8; 591 ISD::LoadExtType HiExtType = LD->getExtensionType(); 592 593 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. 594 if (HiExtType == ISD::NON_EXTLOAD) 595 HiExtType = ISD::ZEXTLOAD; 596 597 // Load the value in two parts 598 SDValue Lo, Hi; 599 if (TLI.isLittleEndian()) { 600 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(), 601 SVOffset, NewLoadedVT, LD->isVolatile(), 602 LD->isNonTemporal(), Alignment); 603 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 604 DAG.getConstant(IncrementSize, TLI.getPointerTy())); 605 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(), 606 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), 607 LD->isNonTemporal(), MinAlign(Alignment, IncrementSize)); 608 } else { 609 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(), 610 SVOffset, NewLoadedVT, LD->isVolatile(), 611 LD->isNonTemporal(), Alignment); 612 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 613 DAG.getConstant(IncrementSize, TLI.getPointerTy())); 614 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(), 615 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), 616 LD->isNonTemporal(), MinAlign(Alignment, IncrementSize)); 617 } 618 619 // aggregate the two parts 620 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); 621 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount); 622 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo); 623 624 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 625 Hi.getValue(1)); 626 627 SDValue Ops[] = { Result, TF }; 628 return DAG.getMergeValues(Ops, 2, dl); 629} 630 631/// PerformInsertVectorEltInMemory - Some target cannot handle a variable 632/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it 633/// is necessary to spill the vector being inserted into to memory, perform 634/// the insert there, and then read the result back. 635SDValue SelectionDAGLegalize:: 636PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx, 637 DebugLoc dl) { 638 SDValue Tmp1 = Vec; 639 SDValue Tmp2 = Val; 640 SDValue Tmp3 = Idx; 641 642 // If the target doesn't support this, we have to spill the input vector 643 // to a temporary stack slot, update the element, then reload it. This is 644 // badness. We could also load the value into a vector register (either 645 // with a "move to register" or "extload into register" instruction, then 646 // permute it into place, if the idx is a constant and if the idx is 647 // supported by the target. 648 EVT VT = Tmp1.getValueType(); 649 EVT EltVT = VT.getVectorElementType(); 650 EVT IdxVT = Tmp3.getValueType(); 651 EVT PtrVT = TLI.getPointerTy(); 652 SDValue StackPtr = DAG.CreateStackTemporary(VT); 653 654 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 655 656 // Store the vector. 657 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr, 658 PseudoSourceValue::getFixedStack(SPFI), 0, 659 false, false, 0); 660 661 // Truncate or zero extend offset to target pointer type. 662 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; 663 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3); 664 // Add the offset to the index. 665 unsigned EltSize = EltVT.getSizeInBits()/8; 666 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); 667 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr); 668 // Store the scalar value. 669 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, 670 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT, 671 false, false, 0); 672 // Load the updated vector. 673 return DAG.getLoad(VT, dl, Ch, StackPtr, 674 PseudoSourceValue::getFixedStack(SPFI), 0, 675 false, false, 0); 676} 677 678 679SDValue SelectionDAGLegalize:: 680ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) { 681 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) { 682 // SCALAR_TO_VECTOR requires that the type of the value being inserted 683 // match the element type of the vector being created, except for 684 // integers in which case the inserted value can be over width. 685 EVT EltVT = Vec.getValueType().getVectorElementType(); 686 if (Val.getValueType() == EltVT || 687 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) { 688 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 689 Vec.getValueType(), Val); 690 691 unsigned NumElts = Vec.getValueType().getVectorNumElements(); 692 // We generate a shuffle of InVec and ScVec, so the shuffle mask 693 // should be 0,1,2,3,4,5... with the appropriate element replaced with 694 // elt 0 of the RHS. 695 SmallVector<int, 8> ShufOps; 696 for (unsigned i = 0; i != NumElts; ++i) 697 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts); 698 699 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, 700 &ShufOps[0]); 701 } 702 } 703 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl); 704} 705 706SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) { 707 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 708 // FIXME: We shouldn't do this for TargetConstantFP's. 709 // FIXME: move this to the DAG Combiner! Note that we can't regress due 710 // to phase ordering between legalized code and the dag combiner. This 711 // probably means that we need to integrate dag combiner and legalizer 712 // together. 713 // We generally can't do this one for long doubles. 714 SDValue Tmp1 = ST->getChain(); 715 SDValue Tmp2 = ST->getBasePtr(); 716 SDValue Tmp3; 717 int SVOffset = ST->getSrcValueOffset(); 718 unsigned Alignment = ST->getAlignment(); 719 bool isVolatile = ST->isVolatile(); 720 bool isNonTemporal = ST->isNonTemporal(); 721 DebugLoc dl = ST->getDebugLoc(); 722 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { 723 if (CFP->getValueType(0) == MVT::f32 && 724 getTypeAction(MVT::i32) == Legal) { 725 Tmp3 = DAG.getConstant(CFP->getValueAPF(). 726 bitcastToAPInt().zextOrTrunc(32), 727 MVT::i32); 728 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), 729 SVOffset, isVolatile, isNonTemporal, Alignment); 730 } else if (CFP->getValueType(0) == MVT::f64) { 731 // If this target supports 64-bit registers, do a single 64-bit store. 732 if (getTypeAction(MVT::i64) == Legal) { 733 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). 734 zextOrTrunc(64), MVT::i64); 735 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), 736 SVOffset, isVolatile, isNonTemporal, Alignment); 737 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) { 738 // Otherwise, if the target supports 32-bit registers, use 2 32-bit 739 // stores. If the target supports neither 32- nor 64-bits, this 740 // xform is certainly not worth it. 741 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt(); 742 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32); 743 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32); 744 if (TLI.isBigEndian()) std::swap(Lo, Hi); 745 746 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(), 747 SVOffset, isVolatile, isNonTemporal, Alignment); 748 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, 749 DAG.getIntPtrConstant(4)); 750 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4, 751 isVolatile, isNonTemporal, MinAlign(Alignment, 4U)); 752 753 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 754 } 755 } 756 } 757 return SDValue(); 758} 759 760/// LegalizeOp - We know that the specified value has a legal type, and 761/// that its operands are legal. Now ensure that the operation itself 762/// is legal, recursively ensuring that the operands' operations remain 763/// legal. 764SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) { 765 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes. 766 return Op; 767 768 SDNode *Node = Op.getNode(); 769 DebugLoc dl = Node->getDebugLoc(); 770 771 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 772 assert(getTypeAction(Node->getValueType(i)) == Legal && 773 "Unexpected illegal type!"); 774 775 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) 776 assert((isTypeLegal(Node->getOperand(i).getValueType()) || 777 Node->getOperand(i).getOpcode() == ISD::TargetConstant) && 778 "Unexpected illegal type!"); 779 780 // Note that LegalizeOp may be reentered even from single-use nodes, which 781 // means that we always must cache transformed nodes. 782 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); 783 if (I != LegalizedNodes.end()) return I->second; 784 785 SDValue Tmp1, Tmp2, Tmp3, Tmp4; 786 SDValue Result = Op; 787 bool isCustom = false; 788 789 // Figure out the correct action; the way to query this varies by opcode 790 TargetLowering::LegalizeAction Action; 791 bool SimpleFinishLegalizing = true; 792 switch (Node->getOpcode()) { 793 case ISD::INTRINSIC_W_CHAIN: 794 case ISD::INTRINSIC_WO_CHAIN: 795 case ISD::INTRINSIC_VOID: 796 case ISD::VAARG: 797 case ISD::STACKSAVE: 798 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 799 break; 800 case ISD::SINT_TO_FP: 801 case ISD::UINT_TO_FP: 802 case ISD::EXTRACT_VECTOR_ELT: 803 Action = TLI.getOperationAction(Node->getOpcode(), 804 Node->getOperand(0).getValueType()); 805 break; 806 case ISD::FP_ROUND_INREG: 807 case ISD::SIGN_EXTEND_INREG: { 808 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT(); 809 Action = TLI.getOperationAction(Node->getOpcode(), InnerType); 810 break; 811 } 812 case ISD::SELECT_CC: 813 case ISD::SETCC: 814 case ISD::BR_CC: { 815 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 : 816 Node->getOpcode() == ISD::SETCC ? 2 : 1; 817 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0; 818 EVT OpVT = Node->getOperand(CompareOperand).getValueType(); 819 ISD::CondCode CCCode = 820 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get(); 821 Action = TLI.getCondCodeAction(CCCode, OpVT); 822 if (Action == TargetLowering::Legal) { 823 if (Node->getOpcode() == ISD::SELECT_CC) 824 Action = TLI.getOperationAction(Node->getOpcode(), 825 Node->getValueType(0)); 826 else 827 Action = TLI.getOperationAction(Node->getOpcode(), OpVT); 828 } 829 break; 830 } 831 case ISD::LOAD: 832 case ISD::STORE: 833 // FIXME: Model these properly. LOAD and STORE are complicated, and 834 // STORE expects the unlegalized operand in some cases. 835 SimpleFinishLegalizing = false; 836 break; 837 case ISD::CALLSEQ_START: 838 case ISD::CALLSEQ_END: 839 // FIXME: This shouldn't be necessary. These nodes have special properties 840 // dealing with the recursive nature of legalization. Removing this 841 // special case should be done as part of making LegalizeDAG non-recursive. 842 SimpleFinishLegalizing = false; 843 break; 844 case ISD::EXTRACT_ELEMENT: 845 case ISD::FLT_ROUNDS_: 846 case ISD::SADDO: 847 case ISD::SSUBO: 848 case ISD::UADDO: 849 case ISD::USUBO: 850 case ISD::SMULO: 851 case ISD::UMULO: 852 case ISD::FPOWI: 853 case ISD::MERGE_VALUES: 854 case ISD::EH_RETURN: 855 case ISD::FRAME_TO_ARGS_OFFSET: 856 // These operations lie about being legal: when they claim to be legal, 857 // they should actually be expanded. 858 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 859 if (Action == TargetLowering::Legal) 860 Action = TargetLowering::Expand; 861 break; 862 case ISD::TRAMPOLINE: 863 case ISD::FRAMEADDR: 864 case ISD::RETURNADDR: 865 case ISD::EH_SJLJ_SETJMP: 866 case ISD::EH_SJLJ_LONGJMP: 867 // These operations lie about being legal: when they claim to be legal, 868 // they should actually be custom-lowered. 869 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 870 if (Action == TargetLowering::Legal) 871 Action = TargetLowering::Custom; 872 break; 873 case ISD::BUILD_VECTOR: 874 // A weird case: legalization for BUILD_VECTOR never legalizes the 875 // operands! 876 // FIXME: This really sucks... changing it isn't semantically incorrect, 877 // but it massively pessimizes the code for floating-point BUILD_VECTORs 878 // because ConstantFP operands get legalized into constant pool loads 879 // before the BUILD_VECTOR code can see them. It doesn't usually bite, 880 // though, because BUILD_VECTORS usually get lowered into other nodes 881 // which get legalized properly. 882 SimpleFinishLegalizing = false; 883 break; 884 default: 885 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 886 Action = TargetLowering::Legal; 887 } else { 888 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 889 } 890 break; 891 } 892 893 if (SimpleFinishLegalizing) { 894 SmallVector<SDValue, 8> Ops, ResultVals; 895 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) 896 Ops.push_back(LegalizeOp(Node->getOperand(i))); 897 switch (Node->getOpcode()) { 898 default: break; 899 case ISD::BR: 900 case ISD::BRIND: 901 case ISD::BR_JT: 902 case ISD::BR_CC: 903 case ISD::BRCOND: 904 // Branches tweak the chain to include LastCALLSEQ_END 905 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0], 906 LastCALLSEQ_END); 907 Ops[0] = LegalizeOp(Ops[0]); 908 LastCALLSEQ_END = DAG.getEntryNode(); 909 break; 910 case ISD::SHL: 911 case ISD::SRL: 912 case ISD::SRA: 913 case ISD::ROTL: 914 case ISD::ROTR: 915 // Legalizing shifts/rotates requires adjusting the shift amount 916 // to the appropriate width. 917 if (!Ops[1].getValueType().isVector()) 918 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1])); 919 break; 920 case ISD::SRL_PARTS: 921 case ISD::SRA_PARTS: 922 case ISD::SHL_PARTS: 923 // Legalizing shifts/rotates requires adjusting the shift amount 924 // to the appropriate width. 925 if (!Ops[2].getValueType().isVector()) 926 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[2])); 927 break; 928 } 929 930 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(), 931 Ops.size()); 932 switch (Action) { 933 case TargetLowering::Legal: 934 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 935 ResultVals.push_back(Result.getValue(i)); 936 break; 937 case TargetLowering::Custom: 938 // FIXME: The handling for custom lowering with multiple results is 939 // a complete mess. 940 Tmp1 = TLI.LowerOperation(Result, DAG); 941 if (Tmp1.getNode()) { 942 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { 943 if (e == 1) 944 ResultVals.push_back(Tmp1); 945 else 946 ResultVals.push_back(Tmp1.getValue(i)); 947 } 948 break; 949 } 950 951 // FALL THROUGH 952 case TargetLowering::Expand: 953 ExpandNode(Result.getNode(), ResultVals); 954 break; 955 case TargetLowering::Promote: 956 PromoteNode(Result.getNode(), ResultVals); 957 break; 958 } 959 if (!ResultVals.empty()) { 960 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) { 961 if (ResultVals[i] != SDValue(Node, i)) 962 ResultVals[i] = LegalizeOp(ResultVals[i]); 963 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]); 964 } 965 return ResultVals[Op.getResNo()]; 966 } 967 } 968 969 switch (Node->getOpcode()) { 970 default: 971#ifndef NDEBUG 972 dbgs() << "NODE: "; 973 Node->dump( &DAG); 974 dbgs() << "\n"; 975#endif 976 assert(0 && "Do not know how to legalize this operator!"); 977 978 case ISD::BUILD_VECTOR: 979 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { 980 default: assert(0 && "This action is not supported yet!"); 981 case TargetLowering::Custom: 982 Tmp3 = TLI.LowerOperation(Result, DAG); 983 if (Tmp3.getNode()) { 984 Result = Tmp3; 985 break; 986 } 987 // FALLTHROUGH 988 case TargetLowering::Expand: 989 Result = ExpandBUILD_VECTOR(Result.getNode()); 990 break; 991 } 992 break; 993 case ISD::CALLSEQ_START: { 994 SDNode *CallEnd = FindCallEndFromCallStart(Node); 995 996 // Recursively Legalize all of the inputs of the call end that do not lead 997 // to this call start. This ensures that any libcalls that need be inserted 998 // are inserted *before* the CALLSEQ_START. 999 {SmallPtrSet<SDNode*, 32> NodesLeadingTo; 1000 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) 1001 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node, 1002 NodesLeadingTo); 1003 } 1004 1005 // Now that we legalized all of the inputs (which may have inserted 1006 // libcalls) create the new CALLSEQ_START node. 1007 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1008 1009 // Merge in the last call, to ensure that this call start after the last 1010 // call ended. 1011 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { 1012 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1013 Tmp1, LastCALLSEQ_END); 1014 Tmp1 = LegalizeOp(Tmp1); 1015 } 1016 1017 // Do not try to legalize the target-specific arguments (#1+). 1018 if (Tmp1 != Node->getOperand(0)) { 1019 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); 1020 Ops[0] = Tmp1; 1021 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); 1022 } 1023 1024 // Remember that the CALLSEQ_START is legalized. 1025 AddLegalizedOperand(Op.getValue(0), Result); 1026 if (Node->getNumValues() == 2) // If this has a flag result, remember it. 1027 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 1028 1029 // Now that the callseq_start and all of the non-call nodes above this call 1030 // sequence have been legalized, legalize the call itself. During this 1031 // process, no libcalls can/will be inserted, guaranteeing that no calls 1032 // can overlap. 1033 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); 1034 // Note that we are selecting this call! 1035 LastCALLSEQ_END = SDValue(CallEnd, 0); 1036 IsLegalizingCall = true; 1037 1038 // Legalize the call, starting from the CALLSEQ_END. 1039 LegalizeOp(LastCALLSEQ_END); 1040 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); 1041 return Result; 1042 } 1043 case ISD::CALLSEQ_END: 1044 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This 1045 // will cause this node to be legalized as well as handling libcalls right. 1046 if (LastCALLSEQ_END.getNode() != Node) { 1047 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0)); 1048 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); 1049 assert(I != LegalizedNodes.end() && 1050 "Legalizing the call start should have legalized this node!"); 1051 return I->second; 1052 } 1053 1054 // Otherwise, the call start has been legalized and everything is going 1055 // according to plan. Just legalize ourselves normally here. 1056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1057 // Do not try to legalize the target-specific arguments (#1+), except for 1058 // an optional flag input. 1059 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ 1060 if (Tmp1 != Node->getOperand(0)) { 1061 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); 1062 Ops[0] = Tmp1; 1063 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); 1064 } 1065 } else { 1066 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); 1067 if (Tmp1 != Node->getOperand(0) || 1068 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { 1069 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); 1070 Ops[0] = Tmp1; 1071 Ops.back() = Tmp2; 1072 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); 1073 } 1074 } 1075 assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); 1076 // This finishes up call legalization. 1077 IsLegalizingCall = false; 1078 1079 // If the CALLSEQ_END node has a flag, remember that we legalized it. 1080 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0)); 1081 if (Node->getNumValues() == 2) 1082 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1)); 1083 return Result.getValue(Op.getResNo()); 1084 case ISD::LOAD: { 1085 LoadSDNode *LD = cast<LoadSDNode>(Node); 1086 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. 1087 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. 1088 1089 ISD::LoadExtType ExtType = LD->getExtensionType(); 1090 if (ExtType == ISD::NON_EXTLOAD) { 1091 EVT VT = Node->getValueType(0); 1092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); 1093 Tmp3 = Result.getValue(0); 1094 Tmp4 = Result.getValue(1); 1095 1096 switch (TLI.getOperationAction(Node->getOpcode(), VT)) { 1097 default: assert(0 && "This action is not supported yet!"); 1098 case TargetLowering::Legal: 1099 // If this is an unaligned load and the target doesn't support it, 1100 // expand it. 1101 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) { 1102 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext()); 1103 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty); 1104 if (LD->getAlignment() < ABIAlignment){ 1105 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), 1106 DAG, TLI); 1107 Tmp3 = Result.getOperand(0); 1108 Tmp4 = Result.getOperand(1); 1109 Tmp3 = LegalizeOp(Tmp3); 1110 Tmp4 = LegalizeOp(Tmp4); 1111 } 1112 } 1113 break; 1114 case TargetLowering::Custom: 1115 Tmp1 = TLI.LowerOperation(Tmp3, DAG); 1116 if (Tmp1.getNode()) { 1117 Tmp3 = LegalizeOp(Tmp1); 1118 Tmp4 = LegalizeOp(Tmp1.getValue(1)); 1119 } 1120 break; 1121 case TargetLowering::Promote: { 1122 // Only promote a load of vector type to another. 1123 assert(VT.isVector() && "Cannot promote this load!"); 1124 // Change base type to a different vector type. 1125 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 1126 1127 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(), 1128 LD->getSrcValueOffset(), 1129 LD->isVolatile(), LD->isNonTemporal(), 1130 LD->getAlignment()); 1131 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1)); 1132 Tmp4 = LegalizeOp(Tmp1.getValue(1)); 1133 break; 1134 } 1135 } 1136 // Since loads produce two values, make sure to remember that we 1137 // legalized both of them. 1138 AddLegalizedOperand(SDValue(Node, 0), Tmp3); 1139 AddLegalizedOperand(SDValue(Node, 1), Tmp4); 1140 return Op.getResNo() ? Tmp4 : Tmp3; 1141 } else { 1142 EVT SrcVT = LD->getMemoryVT(); 1143 unsigned SrcWidth = SrcVT.getSizeInBits(); 1144 int SVOffset = LD->getSrcValueOffset(); 1145 unsigned Alignment = LD->getAlignment(); 1146 bool isVolatile = LD->isVolatile(); 1147 bool isNonTemporal = LD->isNonTemporal(); 1148 1149 if (SrcWidth != SrcVT.getStoreSizeInBits() && 1150 // Some targets pretend to have an i1 loading operation, and actually 1151 // load an i8. This trick is correct for ZEXTLOAD because the top 7 1152 // bits are guaranteed to be zero; it helps the optimizers understand 1153 // that these bits are zero. It is also useful for EXTLOAD, since it 1154 // tells the optimizers that those bits are undefined. It would be 1155 // nice to have an effective generic way of getting these benefits... 1156 // Until such a way is found, don't insist on promoting i1 here. 1157 (SrcVT != MVT::i1 || 1158 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) { 1159 // Promote to a byte-sized load if not loading an integral number of 1160 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. 1161 unsigned NewWidth = SrcVT.getStoreSizeInBits(); 1162 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth); 1163 SDValue Ch; 1164 1165 // The extra bits are guaranteed to be zero, since we stored them that 1166 // way. A zext load from NVT thus automatically gives zext from SrcVT. 1167 1168 ISD::LoadExtType NewExtType = 1169 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; 1170 1171 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), 1172 Tmp1, Tmp2, LD->getSrcValue(), SVOffset, 1173 NVT, isVolatile, isNonTemporal, Alignment); 1174 1175 Ch = Result.getValue(1); // The chain. 1176 1177 if (ExtType == ISD::SEXTLOAD) 1178 // Having the top bits zero doesn't help when sign extending. 1179 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 1180 Result.getValueType(), 1181 Result, DAG.getValueType(SrcVT)); 1182 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) 1183 // All the top bits are guaranteed to be zero - inform the optimizers. 1184 Result = DAG.getNode(ISD::AssertZext, dl, 1185 Result.getValueType(), Result, 1186 DAG.getValueType(SrcVT)); 1187 1188 Tmp1 = LegalizeOp(Result); 1189 Tmp2 = LegalizeOp(Ch); 1190 } else if (SrcWidth & (SrcWidth - 1)) { 1191 // If not loading a power-of-2 number of bits, expand as two loads. 1192 assert(!SrcVT.isVector() && "Unsupported extload!"); 1193 unsigned RoundWidth = 1 << Log2_32(SrcWidth); 1194 assert(RoundWidth < SrcWidth); 1195 unsigned ExtraWidth = SrcWidth - RoundWidth; 1196 assert(ExtraWidth < RoundWidth); 1197 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 1198 "Load size not an integral number of bytes!"); 1199 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 1200 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 1201 SDValue Lo, Hi, Ch; 1202 unsigned IncrementSize; 1203 1204 if (TLI.isLittleEndian()) { 1205 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) 1206 // Load the bottom RoundWidth bits. 1207 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, 1208 Node->getValueType(0), Tmp1, Tmp2, 1209 LD->getSrcValue(), SVOffset, RoundVT, isVolatile, 1210 isNonTemporal, Alignment); 1211 1212 // Load the remaining ExtraWidth bits. 1213 IncrementSize = RoundWidth / 8; 1214 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, 1215 DAG.getIntPtrConstant(IncrementSize)); 1216 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2, 1217 LD->getSrcValue(), SVOffset + IncrementSize, 1218 ExtraVT, isVolatile, isNonTemporal, 1219 MinAlign(Alignment, IncrementSize)); 1220 1221 // Build a factor node to remember that this load is independent of the 1222 // other one. 1223 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 1224 Hi.getValue(1)); 1225 1226 // Move the top bits to the right place. 1227 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi, 1228 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); 1229 1230 // Join the hi and lo parts. 1231 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 1232 } else { 1233 // Big endian - avoid unaligned loads. 1234 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 1235 // Load the top RoundWidth bits. 1236 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2, 1237 LD->getSrcValue(), SVOffset, RoundVT, isVolatile, 1238 isNonTemporal, Alignment); 1239 1240 // Load the remaining ExtraWidth bits. 1241 IncrementSize = RoundWidth / 8; 1242 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, 1243 DAG.getIntPtrConstant(IncrementSize)); 1244 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, 1245 Node->getValueType(0), Tmp1, Tmp2, 1246 LD->getSrcValue(), SVOffset + IncrementSize, 1247 ExtraVT, isVolatile, isNonTemporal, 1248 MinAlign(Alignment, IncrementSize)); 1249 1250 // Build a factor node to remember that this load is independent of the 1251 // other one. 1252 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 1253 Hi.getValue(1)); 1254 1255 // Move the top bits to the right place. 1256 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi, 1257 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); 1258 1259 // Join the hi and lo parts. 1260 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 1261 } 1262 1263 Tmp1 = LegalizeOp(Result); 1264 Tmp2 = LegalizeOp(Ch); 1265 } else { 1266 switch (TLI.getLoadExtAction(ExtType, SrcVT)) { 1267 default: assert(0 && "This action is not supported yet!"); 1268 case TargetLowering::Custom: 1269 isCustom = true; 1270 // FALLTHROUGH 1271 case TargetLowering::Legal: 1272 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); 1273 Tmp1 = Result.getValue(0); 1274 Tmp2 = Result.getValue(1); 1275 1276 if (isCustom) { 1277 Tmp3 = TLI.LowerOperation(Result, DAG); 1278 if (Tmp3.getNode()) { 1279 Tmp1 = LegalizeOp(Tmp3); 1280 Tmp2 = LegalizeOp(Tmp3.getValue(1)); 1281 } 1282 } else { 1283 // If this is an unaligned load and the target doesn't support it, 1284 // expand it. 1285 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) { 1286 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext()); 1287 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty); 1288 if (LD->getAlignment() < ABIAlignment){ 1289 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), 1290 DAG, TLI); 1291 Tmp1 = Result.getOperand(0); 1292 Tmp2 = Result.getOperand(1); 1293 Tmp1 = LegalizeOp(Tmp1); 1294 Tmp2 = LegalizeOp(Tmp2); 1295 } 1296 } 1297 } 1298 break; 1299 case TargetLowering::Expand: 1300 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND 1301 // f128 = EXTLOAD {f32,f64} too 1302 if ((SrcVT == MVT::f32 && (Node->getValueType(0) == MVT::f64 || 1303 Node->getValueType(0) == MVT::f128)) || 1304 (SrcVT == MVT::f64 && Node->getValueType(0) == MVT::f128)) { 1305 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(), 1306 LD->getSrcValueOffset(), 1307 LD->isVolatile(), LD->isNonTemporal(), 1308 LD->getAlignment()); 1309 Result = DAG.getNode(ISD::FP_EXTEND, dl, 1310 Node->getValueType(0), Load); 1311 Tmp1 = LegalizeOp(Result); // Relegalize new nodes. 1312 Tmp2 = LegalizeOp(Load.getValue(1)); 1313 break; 1314 } 1315 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!"); 1316 // Turn the unsupported load into an EXTLOAD followed by an explicit 1317 // zero/sign extend inreg. 1318 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0), 1319 Tmp1, Tmp2, LD->getSrcValue(), 1320 LD->getSrcValueOffset(), SrcVT, 1321 LD->isVolatile(), LD->isNonTemporal(), 1322 LD->getAlignment()); 1323 SDValue ValRes; 1324 if (ExtType == ISD::SEXTLOAD) 1325 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 1326 Result.getValueType(), 1327 Result, DAG.getValueType(SrcVT)); 1328 else 1329 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT); 1330 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. 1331 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. 1332 break; 1333 } 1334 } 1335 1336 // Since loads produce two values, make sure to remember that we legalized 1337 // both of them. 1338 AddLegalizedOperand(SDValue(Node, 0), Tmp1); 1339 AddLegalizedOperand(SDValue(Node, 1), Tmp2); 1340 return Op.getResNo() ? Tmp2 : Tmp1; 1341 } 1342 } 1343 case ISD::STORE: { 1344 StoreSDNode *ST = cast<StoreSDNode>(Node); 1345 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. 1346 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. 1347 int SVOffset = ST->getSrcValueOffset(); 1348 unsigned Alignment = ST->getAlignment(); 1349 bool isVolatile = ST->isVolatile(); 1350 bool isNonTemporal = ST->isNonTemporal(); 1351 1352 if (!ST->isTruncatingStore()) { 1353 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) { 1354 Result = SDValue(OptStore, 0); 1355 break; 1356 } 1357 1358 { 1359 Tmp3 = LegalizeOp(ST->getValue()); 1360 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, 1361 ST->getOffset()); 1362 1363 EVT VT = Tmp3.getValueType(); 1364 switch (TLI.getOperationAction(ISD::STORE, VT)) { 1365 default: assert(0 && "This action is not supported yet!"); 1366 case TargetLowering::Legal: 1367 // If this is an unaligned store and the target doesn't support it, 1368 // expand it. 1369 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) { 1370 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext()); 1371 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty); 1372 if (ST->getAlignment() < ABIAlignment) 1373 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), 1374 DAG, TLI); 1375 } 1376 break; 1377 case TargetLowering::Custom: 1378 Tmp1 = TLI.LowerOperation(Result, DAG); 1379 if (Tmp1.getNode()) Result = Tmp1; 1380 break; 1381 case TargetLowering::Promote: 1382 assert(VT.isVector() && "Unknown legal promote case!"); 1383 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl, 1384 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); 1385 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, 1386 ST->getSrcValue(), SVOffset, isVolatile, 1387 isNonTemporal, Alignment); 1388 break; 1389 } 1390 break; 1391 } 1392 } else { 1393 Tmp3 = LegalizeOp(ST->getValue()); 1394 1395 EVT StVT = ST->getMemoryVT(); 1396 unsigned StWidth = StVT.getSizeInBits(); 1397 1398 if (StWidth != StVT.getStoreSizeInBits()) { 1399 // Promote to a byte-sized store with upper bits zero if not 1400 // storing an integral number of bytes. For example, promote 1401 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) 1402 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), 1403 StVT.getStoreSizeInBits()); 1404 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT); 1405 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), 1406 SVOffset, NVT, isVolatile, isNonTemporal, 1407 Alignment); 1408 } else if (StWidth & (StWidth - 1)) { 1409 // If not storing a power-of-2 number of bits, expand as two stores. 1410 assert(!StVT.isVector() && "Unsupported truncstore!"); 1411 unsigned RoundWidth = 1 << Log2_32(StWidth); 1412 assert(RoundWidth < StWidth); 1413 unsigned ExtraWidth = StWidth - RoundWidth; 1414 assert(ExtraWidth < RoundWidth); 1415 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 1416 "Store size not an integral number of bytes!"); 1417 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 1418 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 1419 SDValue Lo, Hi; 1420 unsigned IncrementSize; 1421 1422 if (TLI.isLittleEndian()) { 1423 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) 1424 // Store the bottom RoundWidth bits. 1425 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), 1426 SVOffset, RoundVT, 1427 isVolatile, isNonTemporal, Alignment); 1428 1429 // Store the remaining ExtraWidth bits. 1430 IncrementSize = RoundWidth / 8; 1431 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, 1432 DAG.getIntPtrConstant(IncrementSize)); 1433 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3, 1434 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); 1435 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), 1436 SVOffset + IncrementSize, ExtraVT, isVolatile, 1437 isNonTemporal, 1438 MinAlign(Alignment, IncrementSize)); 1439 } else { 1440 // Big endian - avoid unaligned stores. 1441 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X 1442 // Store the top RoundWidth bits. 1443 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3, 1444 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); 1445 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), 1446 SVOffset, RoundVT, isVolatile, isNonTemporal, 1447 Alignment); 1448 1449 // Store the remaining ExtraWidth bits. 1450 IncrementSize = RoundWidth / 8; 1451 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, 1452 DAG.getIntPtrConstant(IncrementSize)); 1453 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), 1454 SVOffset + IncrementSize, ExtraVT, isVolatile, 1455 isNonTemporal, 1456 MinAlign(Alignment, IncrementSize)); 1457 } 1458 1459 // The order of the stores doesn't matter. 1460 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 1461 } else { 1462 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || 1463 Tmp2 != ST->getBasePtr()) 1464 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, 1465 ST->getOffset()); 1466 1467 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { 1468 default: assert(0 && "This action is not supported yet!"); 1469 case TargetLowering::Legal: 1470 // If this is an unaligned store and the target doesn't support it, 1471 // expand it. 1472 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) { 1473 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext()); 1474 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty); 1475 if (ST->getAlignment() < ABIAlignment) 1476 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), 1477 DAG, TLI); 1478 } 1479 break; 1480 case TargetLowering::Custom: 1481 Result = TLI.LowerOperation(Result, DAG); 1482 break; 1483 case Expand: 1484 // TRUNCSTORE:i16 i32 -> STORE i16 1485 assert(isTypeLegal(StVT) && "Do not know how to expand this store!"); 1486 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3); 1487 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), 1488 SVOffset, isVolatile, isNonTemporal, 1489 Alignment); 1490 break; 1491 } 1492 } 1493 } 1494 break; 1495 } 1496 } 1497 assert(Result.getValueType() == Op.getValueType() && 1498 "Bad legalization!"); 1499 1500 // Make sure that the generated code is itself legal. 1501 if (Result != Op) 1502 Result = LegalizeOp(Result); 1503 1504 // Note that LegalizeOp may be reentered even from single-use nodes, which 1505 // means that we always must cache transformed nodes. 1506 AddLegalizedOperand(Op, Result); 1507 return Result; 1508} 1509 1510SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { 1511 SDValue Vec = Op.getOperand(0); 1512 SDValue Idx = Op.getOperand(1); 1513 DebugLoc dl = Op.getDebugLoc(); 1514 // Store the value to a temporary stack slot, then LOAD the returned part. 1515 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); 1516 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0, 1517 false, false, 0); 1518 1519 // Add the offset to the index. 1520 unsigned EltSize = 1521 Vec.getValueType().getVectorElementType().getSizeInBits()/8; 1522 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx, 1523 DAG.getConstant(EltSize, Idx.getValueType())); 1524 1525 if (Idx.getValueType().bitsGT(TLI.getPointerTy())) 1526 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx); 1527 else 1528 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx); 1529 1530 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr); 1531 1532 if (Op.getValueType().isVector()) 1533 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0, 1534 false, false, 0); 1535 else 1536 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, 1537 NULL, 0, Vec.getValueType().getVectorElementType(), 1538 false, false, 0); 1539} 1540 1541SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) { 1542 // We can't handle this case efficiently. Allocate a sufficiently 1543 // aligned object on the stack, store each element into it, then load 1544 // the result as a vector. 1545 // Create the stack frame object. 1546 EVT VT = Node->getValueType(0); 1547 EVT EltVT = VT.getVectorElementType(); 1548 DebugLoc dl = Node->getDebugLoc(); 1549 SDValue FIPtr = DAG.CreateStackTemporary(VT); 1550 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); 1551 const Value *SV = PseudoSourceValue::getFixedStack(FI); 1552 1553 // Emit a store of each element to the stack slot. 1554 SmallVector<SDValue, 8> Stores; 1555 unsigned TypeByteSize = EltVT.getSizeInBits() / 8; 1556 // Store (in the right endianness) the elements to memory. 1557 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 1558 // Ignore undef elements. 1559 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; 1560 1561 unsigned Offset = TypeByteSize*i; 1562 1563 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType()); 1564 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx); 1565 1566 // If the destination vector element type is narrower than the source 1567 // element type, only store the bits necessary. 1568 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) { 1569 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, 1570 Node->getOperand(i), Idx, SV, Offset, 1571 EltVT, false, false, 0)); 1572 } else 1573 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, 1574 Node->getOperand(i), Idx, SV, Offset, 1575 false, false, 0)); 1576 } 1577 1578 SDValue StoreChain; 1579 if (!Stores.empty()) // Not all undef elements? 1580 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 1581 &Stores[0], Stores.size()); 1582 else 1583 StoreChain = DAG.getEntryNode(); 1584 1585 // Result is a load from the stack slot. 1586 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0, false, false, 0); 1587} 1588 1589SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) { 1590 DebugLoc dl = Node->getDebugLoc(); 1591 SDValue Tmp1 = Node->getOperand(0); 1592 SDValue Tmp2 = Node->getOperand(1); 1593 1594 // Get the sign bit of the RHS. First obtain a value that has the same 1595 // sign as the sign bit, i.e. negative if and only if the sign bit is 1. 1596 SDValue SignBit; 1597 EVT FloatVT = Tmp2.getValueType(); 1598 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits()); 1599 if (isTypeLegal(IVT)) { 1600 // Convert to an integer with the same sign bit. 1601 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2); 1602 } else { 1603 // Store the float to memory, then load the sign part out as an integer. 1604 MVT LoadTy = TLI.getPointerTy(); 1605 // First create a temporary that is aligned for both the load and store. 1606 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy); 1607 // Then store the float to it. 1608 SDValue Ch = 1609 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, NULL, 0, 1610 false, false, 0); 1611 if (TLI.isBigEndian()) { 1612 assert(FloatVT.isByteSized() && "Unsupported floating point type!"); 1613 // Load out a legal integer with the same sign bit as the float. 1614 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, NULL, 0, false, false, 0); 1615 } else { // Little endian 1616 SDValue LoadPtr = StackPtr; 1617 // The float may be wider than the integer we are going to load. Advance 1618 // the pointer so that the loaded integer will contain the sign bit. 1619 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits(); 1620 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8; 1621 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(), 1622 LoadPtr, DAG.getIntPtrConstant(ByteOffset)); 1623 // Load a legal integer containing the sign bit. 1624 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, NULL, 0, false, false, 0); 1625 // Move the sign bit to the top bit of the loaded integer. 1626 unsigned BitShift = LoadTy.getSizeInBits() - 1627 (FloatVT.getSizeInBits() - 8 * ByteOffset); 1628 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?"); 1629 if (BitShift) 1630 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit, 1631 DAG.getConstant(BitShift,TLI.getShiftAmountTy())); 1632 } 1633 } 1634 // Now get the sign bit proper, by seeing whether the value is negative. 1635 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()), 1636 SignBit, DAG.getConstant(0, SignBit.getValueType()), 1637 ISD::SETLT); 1638 // Get the absolute value of the result. 1639 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1); 1640 // Select between the nabs and abs value based on the sign bit of 1641 // the input. 1642 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit, 1643 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal), 1644 AbsVal); 1645} 1646 1647void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node, 1648 SmallVectorImpl<SDValue> &Results) { 1649 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); 1650 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 1651 " not tell us which reg is the stack pointer!"); 1652 DebugLoc dl = Node->getDebugLoc(); 1653 EVT VT = Node->getValueType(0); 1654 SDValue Tmp1 = SDValue(Node, 0); 1655 SDValue Tmp2 = SDValue(Node, 1); 1656 SDValue Tmp3 = Node->getOperand(2); 1657 SDValue Chain = Tmp1.getOperand(0); 1658 1659 // Chain the dynamic stack allocation so that it doesn't modify the stack 1660 // pointer when other instructions are using the stack. 1661 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true)); 1662 1663 SDValue Size = Tmp2.getOperand(1); 1664 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); 1665 Chain = SP.getValue(1); 1666 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue(); 1667 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment(); 1668 if (Align > StackAlign) 1669 SP = DAG.getNode(ISD::AND, dl, VT, SP, 1670 DAG.getConstant(-(uint64_t)Align, VT)); 1671 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value 1672 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain 1673 1674 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true), 1675 DAG.getIntPtrConstant(0, true), SDValue()); 1676 1677 Results.push_back(Tmp1); 1678 Results.push_back(Tmp2); 1679} 1680 1681/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and 1682/// condition code CC on the current target. This routine expands SETCC with 1683/// illegal condition code into AND / OR of multiple SETCC values. 1684void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT, 1685 SDValue &LHS, SDValue &RHS, 1686 SDValue &CC, 1687 DebugLoc dl) { 1688 EVT OpVT = LHS.getValueType(); 1689 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); 1690 switch (TLI.getCondCodeAction(CCCode, OpVT)) { 1691 default: assert(0 && "Unknown condition code action!"); 1692 case TargetLowering::Legal: 1693 // Nothing to do. 1694 break; 1695 case TargetLowering::Expand: { 1696 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID; 1697 unsigned Opc = 0; 1698 switch (CCCode) { 1699 default: assert(0 && "Don't know how to expand this condition!"); 1700 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break; 1701 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break; 1702 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break; 1703 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break; 1704 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break; 1705 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break; 1706 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break; 1707 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break; 1708 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break; 1709 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break; 1710 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break; 1711 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break; 1712 // FIXME: Implement more expansions. 1713 } 1714 1715 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1); 1716 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2); 1717 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2); 1718 RHS = SDValue(); 1719 CC = SDValue(); 1720 break; 1721 } 1722 } 1723} 1724 1725/// EmitStackConvert - Emit a store/load combination to the stack. This stores 1726/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does 1727/// a load from the stack slot to DestVT, extending it if needed. 1728/// The resultant code need not be legal. 1729SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, 1730 EVT SlotVT, 1731 EVT DestVT, 1732 DebugLoc dl) { 1733 // Create the stack frame object. 1734 unsigned SrcAlign = 1735 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType(). 1736 getTypeForEVT(*DAG.getContext())); 1737 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign); 1738 1739 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); 1740 int SPFI = StackPtrFI->getIndex(); 1741 const Value *SV = PseudoSourceValue::getFixedStack(SPFI); 1742 1743 unsigned SrcSize = SrcOp.getValueType().getSizeInBits(); 1744 unsigned SlotSize = SlotVT.getSizeInBits(); 1745 unsigned DestSize = DestVT.getSizeInBits(); 1746 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext()); 1747 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType); 1748 1749 // Emit a store to the stack slot. Use a truncstore if the input value is 1750 // later than DestVT. 1751 SDValue Store; 1752 1753 if (SrcSize > SlotSize) 1754 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, 1755 SV, 0, SlotVT, false, false, SrcAlign); 1756 else { 1757 assert(SrcSize == SlotSize && "Invalid store"); 1758 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, 1759 SV, 0, false, false, SrcAlign); 1760 } 1761 1762 // Result is a load from the stack slot. 1763 if (SlotSize == DestSize) 1764 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, false, 1765 DestAlign); 1766 1767 assert(SlotSize < DestSize && "Unknown extension!"); 1768 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT, 1769 false, false, DestAlign); 1770} 1771 1772SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { 1773 DebugLoc dl = Node->getDebugLoc(); 1774 // Create a vector sized/aligned stack slot, store the value to element #0, 1775 // then load the whole vector back out. 1776 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); 1777 1778 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); 1779 int SPFI = StackPtrFI->getIndex(); 1780 1781 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0), 1782 StackPtr, 1783 PseudoSourceValue::getFixedStack(SPFI), 0, 1784 Node->getValueType(0).getVectorElementType(), 1785 false, false, 0); 1786 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr, 1787 PseudoSourceValue::getFixedStack(SPFI), 0, 1788 false, false, 0); 1789} 1790 1791 1792/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't 1793/// support the operation, but do support the resultant vector type. 1794SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { 1795 unsigned NumElems = Node->getNumOperands(); 1796 SDValue Value1, Value2; 1797 DebugLoc dl = Node->getDebugLoc(); 1798 EVT VT = Node->getValueType(0); 1799 EVT OpVT = Node->getOperand(0).getValueType(); 1800 EVT EltVT = VT.getVectorElementType(); 1801 1802 // If the only non-undef value is the low element, turn this into a 1803 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. 1804 bool isOnlyLowElement = true; 1805 bool MoreThanTwoValues = false; 1806 bool isConstant = true; 1807 for (unsigned i = 0; i < NumElems; ++i) { 1808 SDValue V = Node->getOperand(i); 1809 if (V.getOpcode() == ISD::UNDEF) 1810 continue; 1811 if (i > 0) 1812 isOnlyLowElement = false; 1813 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 1814 isConstant = false; 1815 1816 if (!Value1.getNode()) { 1817 Value1 = V; 1818 } else if (!Value2.getNode()) { 1819 if (V != Value1) 1820 Value2 = V; 1821 } else if (V != Value1 && V != Value2) { 1822 MoreThanTwoValues = true; 1823 } 1824 } 1825 1826 if (!Value1.getNode()) 1827 return DAG.getUNDEF(VT); 1828 1829 if (isOnlyLowElement) 1830 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); 1831 1832 // If all elements are constants, create a load from the constant pool. 1833 if (isConstant) { 1834 std::vector<Constant*> CV; 1835 for (unsigned i = 0, e = NumElems; i != e; ++i) { 1836 if (ConstantFPSDNode *V = 1837 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { 1838 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); 1839 } else if (ConstantSDNode *V = 1840 dyn_cast<ConstantSDNode>(Node->getOperand(i))) { 1841 if (OpVT==EltVT) 1842 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); 1843 else { 1844 // If OpVT and EltVT don't match, EltVT is not legal and the 1845 // element values have been promoted/truncated earlier. Undo this; 1846 // we don't want a v16i8 to become a v16i32 for example. 1847 const ConstantInt *CI = V->getConstantIntValue(); 1848 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()), 1849 CI->getZExtValue())); 1850 } 1851 } else { 1852 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); 1853 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext()); 1854 CV.push_back(UndefValue::get(OpNTy)); 1855 } 1856 } 1857 Constant *CP = ConstantVector::get(CV); 1858 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); 1859 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 1860 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, 1861 PseudoSourceValue::getConstantPool(), 0, 1862 false, false, Alignment); 1863 } 1864 1865 if (!MoreThanTwoValues) { 1866 SmallVector<int, 8> ShuffleVec(NumElems, -1); 1867 for (unsigned i = 0; i < NumElems; ++i) { 1868 SDValue V = Node->getOperand(i); 1869 if (V.getOpcode() == ISD::UNDEF) 1870 continue; 1871 ShuffleVec[i] = V == Value1 ? 0 : NumElems; 1872 } 1873 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) { 1874 // Get the splatted value into the low element of a vector register. 1875 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1); 1876 SDValue Vec2; 1877 if (Value2.getNode()) 1878 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2); 1879 else 1880 Vec2 = DAG.getUNDEF(VT); 1881 1882 // Return shuffle(LowValVec, undef, <0,0,0,0>) 1883 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data()); 1884 } 1885 } 1886 1887 // Otherwise, we can't handle this case efficiently. 1888 return ExpandVectorBuildThroughStack(Node); 1889} 1890 1891// ExpandLibCall - Expand a node into a call to a libcall. If the result value 1892// does not fit into a register, return the lo part and set the hi part to the 1893// by-reg argument. If it does fit into a single register, return the result 1894// and leave the Hi part unset. 1895SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 1896 bool isSigned) { 1897 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); 1898 // The input chain to this libcall is the entry node of the function. 1899 // Legalizing the call will automatically add the previous call to the 1900 // dependence. 1901 SDValue InChain = DAG.getEntryNode(); 1902 1903 TargetLowering::ArgListTy Args; 1904 TargetLowering::ArgListEntry Entry; 1905 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 1906 EVT ArgVT = Node->getOperand(i).getValueType(); 1907 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 1908 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; 1909 Entry.isSExt = isSigned; 1910 Entry.isZExt = !isSigned; 1911 Args.push_back(Entry); 1912 } 1913 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 1914 TLI.getPointerTy()); 1915 1916 // Splice the libcall in wherever FindInputOutputChains tells us to. 1917 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext()); 1918 std::pair<SDValue, SDValue> CallInfo = 1919 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false, 1920 0, TLI.getLibcallCallingConv(LC), false, 1921 /*isReturnValueUsed=*/true, 1922 Callee, Args, DAG, Node->getDebugLoc()); 1923 1924 // Legalize the call sequence, starting with the chain. This will advance 1925 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that 1926 // was added by LowerCallTo (guaranteeing proper serialization of calls). 1927 LegalizeOp(CallInfo.second); 1928 return CallInfo.first; 1929} 1930 1931SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 1932 RTLIB::Libcall Call_F32, 1933 RTLIB::Libcall Call_F64, 1934 RTLIB::Libcall Call_F80, 1935 RTLIB::Libcall Call_PPCF128) { 1936 RTLIB::Libcall LC; 1937 switch (Node->getValueType(0).getSimpleVT().SimpleTy) { 1938 default: assert(0 && "Unexpected request for libcall!"); 1939 case MVT::f32: LC = Call_F32; break; 1940 case MVT::f64: LC = Call_F64; break; 1941 case MVT::f80: LC = Call_F80; break; 1942 case MVT::ppcf128: LC = Call_PPCF128; break; 1943 } 1944 return ExpandLibCall(LC, Node, false); 1945} 1946 1947SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned, 1948 RTLIB::Libcall Call_I8, 1949 RTLIB::Libcall Call_I16, 1950 RTLIB::Libcall Call_I32, 1951 RTLIB::Libcall Call_I64, 1952 RTLIB::Libcall Call_I128) { 1953 RTLIB::Libcall LC; 1954 switch (Node->getValueType(0).getSimpleVT().SimpleTy) { 1955 default: assert(0 && "Unexpected request for libcall!"); 1956 case MVT::i8: LC = Call_I8; break; 1957 case MVT::i16: LC = Call_I16; break; 1958 case MVT::i32: LC = Call_I32; break; 1959 case MVT::i64: LC = Call_I64; break; 1960 case MVT::i128: LC = Call_I128; break; 1961 } 1962 return ExpandLibCall(LC, Node, isSigned); 1963} 1964 1965/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a 1966/// INT_TO_FP operation of the specified operand when the target requests that 1967/// we expand it. At this point, we know that the result and operand types are 1968/// legal for the target. 1969SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, 1970 SDValue Op0, 1971 EVT DestVT, 1972 DebugLoc dl) { 1973 if (Op0.getValueType() == MVT::i32) { 1974 // simple 32-bit [signed|unsigned] integer to float/double expansion 1975 1976 // Get the stack frame index of a 8 byte buffer. 1977 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); 1978 1979 // word offset constant for Hi/Lo address computation 1980 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); 1981 // set up Hi and Lo (into buffer) address based on endian 1982 SDValue Hi = StackSlot; 1983 SDValue Lo = DAG.getNode(ISD::ADD, dl, 1984 TLI.getPointerTy(), StackSlot, WordOff); 1985 if (TLI.isLittleEndian()) 1986 std::swap(Hi, Lo); 1987 1988 // if signed map to unsigned space 1989 SDValue Op0Mapped; 1990 if (isSigned) { 1991 // constant used to invert sign bit (signed to unsigned mapping) 1992 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32); 1993 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit); 1994 } else { 1995 Op0Mapped = Op0; 1996 } 1997 // store the lo of the constructed double - based on integer input 1998 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, 1999 Op0Mapped, Lo, NULL, 0, 2000 false, false, 0); 2001 // initial hi portion of constructed double 2002 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32); 2003 // store the hi of the constructed double - biased exponent 2004 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0, 2005 false, false, 0); 2006 // load the constructed double 2007 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0, 2008 false, false, 0); 2009 // FP constant to bias correct the final result 2010 SDValue Bias = DAG.getConstantFP(isSigned ? 2011 BitsToDouble(0x4330000080000000ULL) : 2012 BitsToDouble(0x4330000000000000ULL), 2013 MVT::f64); 2014 // subtract the bias 2015 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); 2016 // final result 2017 SDValue Result; 2018 // handle final rounding 2019 if (DestVT == MVT::f64) { 2020 // do nothing 2021 Result = Sub; 2022 } else if (DestVT.bitsLT(MVT::f64)) { 2023 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, 2024 DAG.getIntPtrConstant(0)); 2025 } else if (DestVT.bitsGT(MVT::f64)) { 2026 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); 2027 } 2028 return Result; 2029 } 2030 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); 2031 // Code below here assumes !isSigned without checking again. 2032 2033 // Implementation of unsigned i64 to f64 following the algorithm in 2034 // __floatundidf in compiler_rt. This implementation has the advantage 2035 // of performing rounding correctly, both in the default rounding mode 2036 // and in all alternate rounding modes. 2037 // TODO: Generalize this for use with other types. 2038 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) { 2039 SDValue TwoP52 = 2040 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64); 2041 SDValue TwoP84PlusTwoP52 = 2042 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64); 2043 SDValue TwoP84 = 2044 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64); 2045 2046 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32); 2047 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, 2048 DAG.getConstant(32, MVT::i64)); 2049 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52); 2050 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84); 2051 SDValue LoFlt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, LoOr); 2052 SDValue HiFlt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, HiOr); 2053 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt, TwoP84PlusTwoP52); 2054 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub); 2055 } 2056 2057 // Implementation of unsigned i64 to f32. This implementation has the 2058 // advantage of performing rounding correctly. 2059 // TODO: Generalize this for use with other types. 2060 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) { 2061 EVT SHVT = TLI.getShiftAmountTy(); 2062 2063 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, 2064 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64)); 2065 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, 2066 DAG.getConstant(UINT64_C(0x800), MVT::i64)); 2067 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, 2068 DAG.getConstant(UINT64_C(0x7ff), MVT::i64)); 2069 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64), 2070 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE); 2071 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0); 2072 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64), 2073 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64), 2074 ISD::SETUGE); 2075 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0); 2076 2077 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2, 2078 DAG.getConstant(32, SHVT)); 2079 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh); 2080 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc); 2081 SDValue TwoP32 = 2082 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64); 2083 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt); 2084 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2); 2085 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo); 2086 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2); 2087 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd, 2088 DAG.getIntPtrConstant(0)); 2089 2090 } 2091 2092 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 2093 2094 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()), 2095 Op0, DAG.getConstant(0, Op0.getValueType()), 2096 ISD::SETLT); 2097 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4); 2098 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), 2099 SignSet, Four, Zero); 2100 2101 // If the sign bit of the integer is set, the large number will be treated 2102 // as a negative number. To counteract this, the dynamic code adds an 2103 // offset depending on the data type. 2104 uint64_t FF; 2105 switch (Op0.getValueType().getSimpleVT().SimpleTy) { 2106 default: assert(0 && "Unsupported integer type!"); 2107 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 2108 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 2109 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 2110 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 2111 } 2112 if (TLI.isLittleEndian()) FF <<= 32; 2113 Constant *FudgeFactor = ConstantInt::get( 2114 Type::getInt64Ty(*DAG.getContext()), FF); 2115 2116 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); 2117 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 2118 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset); 2119 Alignment = std::min(Alignment, 4u); 2120 SDValue FudgeInReg; 2121 if (DestVT == MVT::f32) 2122 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx, 2123 PseudoSourceValue::getConstantPool(), 0, 2124 false, false, Alignment); 2125 else { 2126 FudgeInReg = 2127 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, 2128 DAG.getEntryNode(), CPIdx, 2129 PseudoSourceValue::getConstantPool(), 0, 2130 MVT::f32, false, false, Alignment)); 2131 } 2132 2133 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); 2134} 2135 2136/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a 2137/// *INT_TO_FP operation of the specified operand when the target requests that 2138/// we promote it. At this point, we know that the result and operand types are 2139/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 2140/// operation that takes a larger input. 2141SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp, 2142 EVT DestVT, 2143 bool isSigned, 2144 DebugLoc dl) { 2145 // First step, figure out the appropriate *INT_TO_FP operation to use. 2146 EVT NewInTy = LegalOp.getValueType(); 2147 2148 unsigned OpToUse = 0; 2149 2150 // Scan for the appropriate larger type to use. 2151 while (1) { 2152 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1); 2153 assert(NewInTy.isInteger() && "Ran out of possibilities!"); 2154 2155 // If the target supports SINT_TO_FP of this type, use it. 2156 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) { 2157 OpToUse = ISD::SINT_TO_FP; 2158 break; 2159 } 2160 if (isSigned) continue; 2161 2162 // If the target supports UINT_TO_FP of this type, use it. 2163 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) { 2164 OpToUse = ISD::UINT_TO_FP; 2165 break; 2166 } 2167 2168 // Otherwise, try a larger type. 2169 } 2170 2171 // Okay, we found the operation and type to use. Zero extend our input to the 2172 // desired type then run the operation on it. 2173 return DAG.getNode(OpToUse, dl, DestVT, 2174 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2175 dl, NewInTy, LegalOp)); 2176} 2177 2178/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a 2179/// FP_TO_*INT operation of the specified operand when the target requests that 2180/// we promote it. At this point, we know that the result and operand types are 2181/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 2182/// operation that returns a larger result. 2183SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp, 2184 EVT DestVT, 2185 bool isSigned, 2186 DebugLoc dl) { 2187 // First step, figure out the appropriate FP_TO*INT operation to use. 2188 EVT NewOutTy = DestVT; 2189 2190 unsigned OpToUse = 0; 2191 2192 // Scan for the appropriate larger type to use. 2193 while (1) { 2194 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1); 2195 assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 2196 2197 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) { 2198 OpToUse = ISD::FP_TO_SINT; 2199 break; 2200 } 2201 2202 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) { 2203 OpToUse = ISD::FP_TO_UINT; 2204 break; 2205 } 2206 2207 // Otherwise, try a larger type. 2208 } 2209 2210 2211 // Okay, we found the operation and type to use. 2212 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); 2213 2214 // Truncate the result of the extended FP_TO_*INT operation to the desired 2215 // size. 2216 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); 2217} 2218 2219/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. 2220/// 2221SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) { 2222 EVT VT = Op.getValueType(); 2223 EVT SHVT = TLI.getShiftAmountTy(); 2224 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; 2225 switch (VT.getSimpleVT().SimpleTy) { 2226 default: assert(0 && "Unhandled Expand type in BSWAP!"); 2227 case MVT::i16: 2228 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); 2229 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); 2230 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 2231 case MVT::i32: 2232 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT)); 2233 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); 2234 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); 2235 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT)); 2236 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); 2237 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT)); 2238 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); 2239 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); 2240 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); 2241 case MVT::i64: 2242 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT)); 2243 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT)); 2244 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT)); 2245 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); 2246 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); 2247 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT)); 2248 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT)); 2249 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT)); 2250 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); 2251 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); 2252 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); 2253 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); 2254 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); 2255 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); 2256 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7); 2257 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5); 2258 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); 2259 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); 2260 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6); 2261 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); 2262 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4); 2263 } 2264} 2265 2266/// ExpandBitCount - Expand the specified bitcount instruction into operations. 2267/// 2268SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op, 2269 DebugLoc dl) { 2270 switch (Opc) { 2271 default: assert(0 && "Cannot expand this yet!"); 2272 case ISD::CTPOP: { 2273 static const uint64_t mask[6] = { 2274 0x5555555555555555ULL, 0x3333333333333333ULL, 2275 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, 2276 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL 2277 }; 2278 EVT VT = Op.getValueType(); 2279 EVT ShVT = TLI.getShiftAmountTy(); 2280 unsigned len = VT.getSizeInBits(); 2281 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { 2282 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) 2283 unsigned EltSize = VT.isVector() ? 2284 VT.getVectorElementType().getSizeInBits() : len; 2285 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT); 2286 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT); 2287 Op = DAG.getNode(ISD::ADD, dl, VT, 2288 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2), 2289 DAG.getNode(ISD::AND, dl, VT, 2290 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3), 2291 Tmp2)); 2292 } 2293 return Op; 2294 } 2295 case ISD::CTLZ: { 2296 // for now, we do this: 2297 // x = x | (x >> 1); 2298 // x = x | (x >> 2); 2299 // ... 2300 // x = x | (x >>16); 2301 // x = x | (x >>32); // for 64-bit input 2302 // return popcount(~x); 2303 // 2304 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc 2305 EVT VT = Op.getValueType(); 2306 EVT ShVT = TLI.getShiftAmountTy(); 2307 unsigned len = VT.getSizeInBits(); 2308 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { 2309 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT); 2310 Op = DAG.getNode(ISD::OR, dl, VT, Op, 2311 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3)); 2312 } 2313 Op = DAG.getNOT(dl, Op, VT); 2314 return DAG.getNode(ISD::CTPOP, dl, VT, Op); 2315 } 2316 case ISD::CTTZ: { 2317 // for now, we use: { return popcount(~x & (x - 1)); } 2318 // unless the target has ctlz but not ctpop, in which case we use: 2319 // { return 32 - nlz(~x & (x-1)); } 2320 // see also http://www.hackersdelight.org/HDcode/ntz.cc 2321 EVT VT = Op.getValueType(); 2322 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT, 2323 DAG.getNOT(dl, Op, VT), 2324 DAG.getNode(ISD::SUB, dl, VT, Op, 2325 DAG.getConstant(1, VT))); 2326 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. 2327 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) && 2328 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT)) 2329 return DAG.getNode(ISD::SUB, dl, VT, 2330 DAG.getConstant(VT.getSizeInBits(), VT), 2331 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3)); 2332 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3); 2333 } 2334 } 2335} 2336 2337void SelectionDAGLegalize::ExpandNode(SDNode *Node, 2338 SmallVectorImpl<SDValue> &Results) { 2339 DebugLoc dl = Node->getDebugLoc(); 2340 SDValue Tmp1, Tmp2, Tmp3, Tmp4; 2341 switch (Node->getOpcode()) { 2342 case ISD::CTPOP: 2343 case ISD::CTLZ: 2344 case ISD::CTTZ: 2345 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl); 2346 Results.push_back(Tmp1); 2347 break; 2348 case ISD::BSWAP: 2349 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl)); 2350 break; 2351 case ISD::FRAMEADDR: 2352 case ISD::RETURNADDR: 2353 case ISD::FRAME_TO_ARGS_OFFSET: 2354 Results.push_back(DAG.getConstant(0, Node->getValueType(0))); 2355 break; 2356 case ISD::FLT_ROUNDS_: 2357 Results.push_back(DAG.getConstant(1, Node->getValueType(0))); 2358 break; 2359 case ISD::EH_RETURN: 2360 case ISD::EH_LABEL: 2361 case ISD::PREFETCH: 2362 case ISD::VAEND: 2363 Results.push_back(Node->getOperand(0)); 2364 break; 2365 case ISD::MEMBARRIER: { 2366 // If the target didn't lower this, lower it to '__sync_synchronize()' call 2367 TargetLowering::ArgListTy Args; 2368 std::pair<SDValue, SDValue> CallResult = 2369 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()), 2370 false, false, false, false, 0, CallingConv::C, false, 2371 /*isReturnValueUsed=*/true, 2372 DAG.getExternalSymbol("__sync_synchronize", 2373 TLI.getPointerTy()), 2374 Args, DAG, dl); 2375 Results.push_back(CallResult.second); 2376 break; 2377 } 2378 // By default, atomic intrinsics are marked Legal and lowered. Targets 2379 // which don't support them directly, however, may want libcalls, in which 2380 // case they mark them Expand, and we get here. 2381 // FIXME: Unimplemented for now. Add libcalls. 2382 case ISD::ATOMIC_SWAP: 2383 case ISD::ATOMIC_LOAD_ADD: 2384 case ISD::ATOMIC_LOAD_SUB: 2385 case ISD::ATOMIC_LOAD_AND: 2386 case ISD::ATOMIC_LOAD_OR: 2387 case ISD::ATOMIC_LOAD_XOR: 2388 case ISD::ATOMIC_LOAD_NAND: 2389 case ISD::ATOMIC_LOAD_MIN: 2390 case ISD::ATOMIC_LOAD_MAX: 2391 case ISD::ATOMIC_LOAD_UMIN: 2392 case ISD::ATOMIC_LOAD_UMAX: 2393 case ISD::ATOMIC_CMP_SWAP: { 2394 assert (0 && "atomic intrinsic not lowered!"); 2395 Results.push_back(Node->getOperand(0)); 2396 break; 2397 } 2398 case ISD::DYNAMIC_STACKALLOC: 2399 ExpandDYNAMIC_STACKALLOC(Node, Results); 2400 break; 2401 case ISD::MERGE_VALUES: 2402 for (unsigned i = 0; i < Node->getNumValues(); i++) 2403 Results.push_back(Node->getOperand(i)); 2404 break; 2405 case ISD::UNDEF: { 2406 EVT VT = Node->getValueType(0); 2407 if (VT.isInteger()) 2408 Results.push_back(DAG.getConstant(0, VT)); 2409 else { 2410 assert(VT.isFloatingPoint() && "Unknown value type!"); 2411 Results.push_back(DAG.getConstantFP(0, VT)); 2412 } 2413 break; 2414 } 2415 case ISD::TRAP: { 2416 // If this operation is not supported, lower it to 'abort()' call 2417 TargetLowering::ArgListTy Args; 2418 std::pair<SDValue, SDValue> CallResult = 2419 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()), 2420 false, false, false, false, 0, CallingConv::C, false, 2421 /*isReturnValueUsed=*/true, 2422 DAG.getExternalSymbol("abort", TLI.getPointerTy()), 2423 Args, DAG, dl); 2424 Results.push_back(CallResult.second); 2425 break; 2426 } 2427 case ISD::FP_ROUND: 2428 case ISD::BIT_CONVERT: 2429 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), 2430 Node->getValueType(0), dl); 2431 Results.push_back(Tmp1); 2432 break; 2433 case ISD::FP_EXTEND: 2434 Tmp1 = EmitStackConvert(Node->getOperand(0), 2435 Node->getOperand(0).getValueType(), 2436 Node->getValueType(0), dl); 2437 Results.push_back(Tmp1); 2438 break; 2439 case ISD::SIGN_EXTEND_INREG: { 2440 // NOTE: we could fall back on load/store here too for targets without 2441 // SAR. However, it is doubtful that any exist. 2442 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 2443 EVT VT = Node->getValueType(0); 2444 EVT ShiftAmountTy = TLI.getShiftAmountTy(); 2445 if (VT.isVector()) 2446 ShiftAmountTy = VT; 2447 unsigned BitsDiff = VT.getScalarType().getSizeInBits() - 2448 ExtraVT.getScalarType().getSizeInBits(); 2449 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy); 2450 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), 2451 Node->getOperand(0), ShiftCst); 2452 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); 2453 Results.push_back(Tmp1); 2454 break; 2455 } 2456 case ISD::FP_ROUND_INREG: { 2457 // The only way we can lower this is to turn it into a TRUNCSTORE, 2458 // EXTLOAD pair, targetting a temporary location (a stack slot). 2459 2460 // NOTE: there is a choice here between constantly creating new stack 2461 // slots and always reusing the same one. We currently always create 2462 // new ones, as reuse may inhibit scheduling. 2463 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 2464 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT, 2465 Node->getValueType(0), dl); 2466 Results.push_back(Tmp1); 2467 break; 2468 } 2469 case ISD::SINT_TO_FP: 2470 case ISD::UINT_TO_FP: 2471 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP, 2472 Node->getOperand(0), Node->getValueType(0), dl); 2473 Results.push_back(Tmp1); 2474 break; 2475 case ISD::FP_TO_UINT: { 2476 SDValue True, False; 2477 EVT VT = Node->getOperand(0).getValueType(); 2478 EVT NVT = Node->getValueType(0); 2479 const uint64_t zero[] = {0, 0}; 2480 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero)); 2481 APInt x = APInt::getSignBit(NVT.getSizeInBits()); 2482 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven); 2483 Tmp1 = DAG.getConstantFP(apf, VT); 2484 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), 2485 Node->getOperand(0), 2486 Tmp1, ISD::SETLT); 2487 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0)); 2488 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, 2489 DAG.getNode(ISD::FSUB, dl, VT, 2490 Node->getOperand(0), Tmp1)); 2491 False = DAG.getNode(ISD::XOR, dl, NVT, False, 2492 DAG.getConstant(x, NVT)); 2493 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False); 2494 Results.push_back(Tmp1); 2495 break; 2496 } 2497 case ISD::VAARG: { 2498 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); 2499 EVT VT = Node->getValueType(0); 2500 Tmp1 = Node->getOperand(0); 2501 Tmp2 = Node->getOperand(1); 2502 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0, 2503 false, false, 0); 2504 // Increment the pointer, VAList, to the next vaarg 2505 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList, 2506 DAG.getConstant(TLI.getTargetData()-> 2507 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())), 2508 TLI.getPointerTy())); 2509 // Store the incremented VAList to the legalized pointer 2510 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0, 2511 false, false, 0); 2512 // Load the actual argument out of the pointer VAList 2513 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0, 2514 false, false, 0)); 2515 Results.push_back(Results[0].getValue(1)); 2516 break; 2517 } 2518 case ISD::VACOPY: { 2519 // This defaults to loading a pointer from the input and storing it to the 2520 // output, returning the chain. 2521 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue(); 2522 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue(); 2523 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0), 2524 Node->getOperand(2), VS, 0, false, false, 0); 2525 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0, 2526 false, false, 0); 2527 Results.push_back(Tmp1); 2528 break; 2529 } 2530 case ISD::EXTRACT_VECTOR_ELT: 2531 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1) 2532 // This must be an access of the only element. Return it. 2533 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), 2534 Node->getOperand(0)); 2535 else 2536 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); 2537 Results.push_back(Tmp1); 2538 break; 2539 case ISD::EXTRACT_SUBVECTOR: 2540 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); 2541 break; 2542 case ISD::CONCAT_VECTORS: { 2543 Results.push_back(ExpandVectorBuildThroughStack(Node)); 2544 break; 2545 } 2546 case ISD::SCALAR_TO_VECTOR: 2547 Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); 2548 break; 2549 case ISD::INSERT_VECTOR_ELT: 2550 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), 2551 Node->getOperand(1), 2552 Node->getOperand(2), dl)); 2553 break; 2554 case ISD::VECTOR_SHUFFLE: { 2555 SmallVector<int, 8> Mask; 2556 cast<ShuffleVectorSDNode>(Node)->getMask(Mask); 2557 2558 EVT VT = Node->getValueType(0); 2559 EVT EltVT = VT.getVectorElementType(); 2560 if (getTypeAction(EltVT) == Promote) 2561 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT); 2562 unsigned NumElems = VT.getVectorNumElements(); 2563 SmallVector<SDValue, 8> Ops; 2564 for (unsigned i = 0; i != NumElems; ++i) { 2565 if (Mask[i] < 0) { 2566 Ops.push_back(DAG.getUNDEF(EltVT)); 2567 continue; 2568 } 2569 unsigned Idx = Mask[i]; 2570 if (Idx < NumElems) 2571 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 2572 Node->getOperand(0), 2573 DAG.getIntPtrConstant(Idx))); 2574 else 2575 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 2576 Node->getOperand(1), 2577 DAG.getIntPtrConstant(Idx - NumElems))); 2578 } 2579 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size()); 2580 Results.push_back(Tmp1); 2581 break; 2582 } 2583 case ISD::EXTRACT_ELEMENT: { 2584 EVT OpTy = Node->getOperand(0).getValueType(); 2585 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { 2586 // 1 -> Hi 2587 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), 2588 DAG.getConstant(OpTy.getSizeInBits()/2, 2589 TLI.getShiftAmountTy())); 2590 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); 2591 } else { 2592 // 0 -> Lo 2593 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), 2594 Node->getOperand(0)); 2595 } 2596 Results.push_back(Tmp1); 2597 break; 2598 } 2599 case ISD::STACKSAVE: 2600 // Expand to CopyFromReg if the target set 2601 // StackPointerRegisterToSaveRestore. 2602 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 2603 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, 2604 Node->getValueType(0))); 2605 Results.push_back(Results[0].getValue(1)); 2606 } else { 2607 Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 2608 Results.push_back(Node->getOperand(0)); 2609 } 2610 break; 2611 case ISD::STACKRESTORE: 2612 // Expand to CopyToReg if the target set 2613 // StackPointerRegisterToSaveRestore. 2614 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 2615 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, 2616 Node->getOperand(1))); 2617 } else { 2618 Results.push_back(Node->getOperand(0)); 2619 } 2620 break; 2621 case ISD::FCOPYSIGN: 2622 Results.push_back(ExpandFCOPYSIGN(Node)); 2623 break; 2624 case ISD::FNEG: 2625 // Expand Y = FNEG(X) -> Y = SUB -0.0, X 2626 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0)); 2627 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1, 2628 Node->getOperand(0)); 2629 Results.push_back(Tmp1); 2630 break; 2631 case ISD::FABS: { 2632 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). 2633 EVT VT = Node->getValueType(0); 2634 Tmp1 = Node->getOperand(0); 2635 Tmp2 = DAG.getConstantFP(0.0, VT); 2636 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), 2637 Tmp1, Tmp2, ISD::SETUGT); 2638 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1); 2639 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3); 2640 Results.push_back(Tmp1); 2641 break; 2642 } 2643 case ISD::FSQRT: 2644 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64, 2645 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128)); 2646 break; 2647 case ISD::FSIN: 2648 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64, 2649 RTLIB::SIN_F80, RTLIB::SIN_PPCF128)); 2650 break; 2651 case ISD::FCOS: 2652 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64, 2653 RTLIB::COS_F80, RTLIB::COS_PPCF128)); 2654 break; 2655 case ISD::FLOG: 2656 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, 2657 RTLIB::LOG_F80, RTLIB::LOG_PPCF128)); 2658 break; 2659 case ISD::FLOG2: 2660 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, 2661 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128)); 2662 break; 2663 case ISD::FLOG10: 2664 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, 2665 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128)); 2666 break; 2667 case ISD::FEXP: 2668 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, 2669 RTLIB::EXP_F80, RTLIB::EXP_PPCF128)); 2670 break; 2671 case ISD::FEXP2: 2672 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, 2673 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128)); 2674 break; 2675 case ISD::FTRUNC: 2676 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, 2677 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128)); 2678 break; 2679 case ISD::FFLOOR: 2680 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, 2681 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128)); 2682 break; 2683 case ISD::FCEIL: 2684 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64, 2685 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128)); 2686 break; 2687 case ISD::FRINT: 2688 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64, 2689 RTLIB::RINT_F80, RTLIB::RINT_PPCF128)); 2690 break; 2691 case ISD::FNEARBYINT: 2692 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32, 2693 RTLIB::NEARBYINT_F64, 2694 RTLIB::NEARBYINT_F80, 2695 RTLIB::NEARBYINT_PPCF128)); 2696 break; 2697 case ISD::FPOWI: 2698 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64, 2699 RTLIB::POWI_F80, RTLIB::POWI_PPCF128)); 2700 break; 2701 case ISD::FPOW: 2702 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, 2703 RTLIB::POW_F80, RTLIB::POW_PPCF128)); 2704 break; 2705 case ISD::FDIV: 2706 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64, 2707 RTLIB::DIV_F80, RTLIB::DIV_PPCF128)); 2708 break; 2709 case ISD::FREM: 2710 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64, 2711 RTLIB::REM_F80, RTLIB::REM_PPCF128)); 2712 break; 2713 case ISD::FP16_TO_FP32: 2714 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false)); 2715 break; 2716 case ISD::FP32_TO_FP16: 2717 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false)); 2718 break; 2719 case ISD::ConstantFP: { 2720 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 2721 // Check to see if this FP immediate is already legal. 2722 // If this is a legal constant, turn it into a TargetConstantFP node. 2723 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0))) 2724 Results.push_back(SDValue(Node, 0)); 2725 else 2726 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI)); 2727 break; 2728 } 2729 case ISD::EHSELECTION: { 2730 unsigned Reg = TLI.getExceptionSelectorRegister(); 2731 assert(Reg && "Can't expand to unknown register!"); 2732 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg, 2733 Node->getValueType(0))); 2734 Results.push_back(Results[0].getValue(1)); 2735 break; 2736 } 2737 case ISD::EXCEPTIONADDR: { 2738 unsigned Reg = TLI.getExceptionAddressRegister(); 2739 assert(Reg && "Can't expand to unknown register!"); 2740 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg, 2741 Node->getValueType(0))); 2742 Results.push_back(Results[0].getValue(1)); 2743 break; 2744 } 2745 case ISD::SUB: { 2746 EVT VT = Node->getValueType(0); 2747 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) && 2748 TLI.isOperationLegalOrCustom(ISD::XOR, VT) && 2749 "Don't know how to expand this subtraction!"); 2750 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1), 2751 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT)); 2752 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT)); 2753 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1)); 2754 break; 2755 } 2756 case ISD::UREM: 2757 case ISD::SREM: { 2758 EVT VT = Node->getValueType(0); 2759 SDVTList VTs = DAG.getVTList(VT, VT); 2760 bool isSigned = Node->getOpcode() == ISD::SREM; 2761 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV; 2762 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 2763 Tmp2 = Node->getOperand(0); 2764 Tmp3 = Node->getOperand(1); 2765 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 2766 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1); 2767 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) { 2768 // X % Y -> X-X/Y*Y 2769 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3); 2770 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3); 2771 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1); 2772 } else if (isSigned) { 2773 Tmp1 = ExpandIntLibCall(Node, true, 2774 RTLIB::SREM_I8, 2775 RTLIB::SREM_I16, RTLIB::SREM_I32, 2776 RTLIB::SREM_I64, RTLIB::SREM_I128); 2777 } else { 2778 Tmp1 = ExpandIntLibCall(Node, false, 2779 RTLIB::UREM_I8, 2780 RTLIB::UREM_I16, RTLIB::UREM_I32, 2781 RTLIB::UREM_I64, RTLIB::UREM_I128); 2782 } 2783 Results.push_back(Tmp1); 2784 break; 2785 } 2786 case ISD::UDIV: 2787 case ISD::SDIV: { 2788 bool isSigned = Node->getOpcode() == ISD::SDIV; 2789 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 2790 EVT VT = Node->getValueType(0); 2791 SDVTList VTs = DAG.getVTList(VT, VT); 2792 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) 2793 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0), 2794 Node->getOperand(1)); 2795 else if (isSigned) 2796 Tmp1 = ExpandIntLibCall(Node, true, 2797 RTLIB::SDIV_I8, 2798 RTLIB::SDIV_I16, RTLIB::SDIV_I32, 2799 RTLIB::SDIV_I64, RTLIB::SDIV_I128); 2800 else 2801 Tmp1 = ExpandIntLibCall(Node, false, 2802 RTLIB::UDIV_I8, 2803 RTLIB::UDIV_I16, RTLIB::UDIV_I32, 2804 RTLIB::UDIV_I64, RTLIB::UDIV_I128); 2805 Results.push_back(Tmp1); 2806 break; 2807 } 2808 case ISD::MULHU: 2809 case ISD::MULHS: { 2810 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : 2811 ISD::SMUL_LOHI; 2812 EVT VT = Node->getValueType(0); 2813 SDVTList VTs = DAG.getVTList(VT, VT); 2814 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) && 2815 "If this wasn't legal, it shouldn't have been created!"); 2816 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0), 2817 Node->getOperand(1)); 2818 Results.push_back(Tmp1.getValue(1)); 2819 break; 2820 } 2821 case ISD::MUL: { 2822 EVT VT = Node->getValueType(0); 2823 SDVTList VTs = DAG.getVTList(VT, VT); 2824 // See if multiply or divide can be lowered using two-result operations. 2825 // We just need the low half of the multiply; try both the signed 2826 // and unsigned forms. If the target supports both SMUL_LOHI and 2827 // UMUL_LOHI, form a preference by checking which forms of plain 2828 // MULH it supports. 2829 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); 2830 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); 2831 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); 2832 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); 2833 unsigned OpToUse = 0; 2834 if (HasSMUL_LOHI && !HasMULHS) { 2835 OpToUse = ISD::SMUL_LOHI; 2836 } else if (HasUMUL_LOHI && !HasMULHU) { 2837 OpToUse = ISD::UMUL_LOHI; 2838 } else if (HasSMUL_LOHI) { 2839 OpToUse = ISD::SMUL_LOHI; 2840 } else if (HasUMUL_LOHI) { 2841 OpToUse = ISD::UMUL_LOHI; 2842 } 2843 if (OpToUse) { 2844 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0), 2845 Node->getOperand(1))); 2846 break; 2847 } 2848 Tmp1 = ExpandIntLibCall(Node, false, 2849 RTLIB::MUL_I8, 2850 RTLIB::MUL_I16, RTLIB::MUL_I32, 2851 RTLIB::MUL_I64, RTLIB::MUL_I128); 2852 Results.push_back(Tmp1); 2853 break; 2854 } 2855 case ISD::SADDO: 2856 case ISD::SSUBO: { 2857 SDValue LHS = Node->getOperand(0); 2858 SDValue RHS = Node->getOperand(1); 2859 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ? 2860 ISD::ADD : ISD::SUB, dl, LHS.getValueType(), 2861 LHS, RHS); 2862 Results.push_back(Sum); 2863 EVT OType = Node->getValueType(1); 2864 2865 SDValue Zero = DAG.getConstant(0, LHS.getValueType()); 2866 2867 // LHSSign -> LHS >= 0 2868 // RHSSign -> RHS >= 0 2869 // SumSign -> Sum >= 0 2870 // 2871 // Add: 2872 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign) 2873 // Sub: 2874 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign) 2875 // 2876 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE); 2877 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE); 2878 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign, 2879 Node->getOpcode() == ISD::SADDO ? 2880 ISD::SETEQ : ISD::SETNE); 2881 2882 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE); 2883 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE); 2884 2885 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE); 2886 Results.push_back(Cmp); 2887 break; 2888 } 2889 case ISD::UADDO: 2890 case ISD::USUBO: { 2891 SDValue LHS = Node->getOperand(0); 2892 SDValue RHS = Node->getOperand(1); 2893 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ? 2894 ISD::ADD : ISD::SUB, dl, LHS.getValueType(), 2895 LHS, RHS); 2896 Results.push_back(Sum); 2897 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS, 2898 Node->getOpcode () == ISD::UADDO ? 2899 ISD::SETULT : ISD::SETUGT)); 2900 break; 2901 } 2902 case ISD::UMULO: 2903 case ISD::SMULO: { 2904 EVT VT = Node->getValueType(0); 2905 SDValue LHS = Node->getOperand(0); 2906 SDValue RHS = Node->getOperand(1); 2907 SDValue BottomHalf; 2908 SDValue TopHalf; 2909 static const unsigned Ops[2][3] = 2910 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND }, 2911 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }}; 2912 bool isSigned = Node->getOpcode() == ISD::SMULO; 2913 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) { 2914 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS); 2915 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS); 2916 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) { 2917 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS, 2918 RHS); 2919 TopHalf = BottomHalf.getValue(1); 2920 } else { 2921 // FIXME: We should be able to fall back to a libcall with an illegal 2922 // type in some cases. 2923 // Also, we can fall back to a division in some cases, but that's a big 2924 // performance hit in the general case. 2925 assert(TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(), 2926 VT.getSizeInBits() * 2)) && 2927 "Don't know how to expand this operation yet!"); 2928 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2); 2929 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS); 2930 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS); 2931 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS); 2932 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1, 2933 DAG.getIntPtrConstant(0)); 2934 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1, 2935 DAG.getIntPtrConstant(1)); 2936 } 2937 if (isSigned) { 2938 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1, TLI.getShiftAmountTy()); 2939 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1); 2940 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1, 2941 ISD::SETNE); 2942 } else { 2943 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, 2944 DAG.getConstant(0, VT), ISD::SETNE); 2945 } 2946 Results.push_back(BottomHalf); 2947 Results.push_back(TopHalf); 2948 break; 2949 } 2950 case ISD::BUILD_PAIR: { 2951 EVT PairTy = Node->getValueType(0); 2952 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0)); 2953 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1)); 2954 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2, 2955 DAG.getConstant(PairTy.getSizeInBits()/2, 2956 TLI.getShiftAmountTy())); 2957 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2)); 2958 break; 2959 } 2960 case ISD::SELECT: 2961 Tmp1 = Node->getOperand(0); 2962 Tmp2 = Node->getOperand(1); 2963 Tmp3 = Node->getOperand(2); 2964 if (Tmp1.getOpcode() == ISD::SETCC) { 2965 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), 2966 Tmp2, Tmp3, 2967 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 2968 } else { 2969 Tmp1 = DAG.getSelectCC(dl, Tmp1, 2970 DAG.getConstant(0, Tmp1.getValueType()), 2971 Tmp2, Tmp3, ISD::SETNE); 2972 } 2973 Results.push_back(Tmp1); 2974 break; 2975 case ISD::BR_JT: { 2976 SDValue Chain = Node->getOperand(0); 2977 SDValue Table = Node->getOperand(1); 2978 SDValue Index = Node->getOperand(2); 2979 2980 EVT PTy = TLI.getPointerTy(); 2981 2982 const TargetData &TD = *TLI.getTargetData(); 2983 unsigned EntrySize = 2984 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD); 2985 2986 Index = DAG.getNode(ISD::MUL, dl, PTy, 2987 Index, DAG.getConstant(EntrySize, PTy)); 2988 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); 2989 2990 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); 2991 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr, 2992 PseudoSourceValue::getJumpTable(), 0, MemVT, 2993 false, false, 0); 2994 Addr = LD; 2995 if (TM.getRelocationModel() == Reloc::PIC_) { 2996 // For PIC, the sequence is: 2997 // BRIND(load(Jumptable + index) + RelocBase) 2998 // RelocBase can be JumpTable, GOT or some sort of global base. 2999 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, 3000 TLI.getPICJumpTableRelocBase(Table, DAG)); 3001 } 3002 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr); 3003 Results.push_back(Tmp1); 3004 break; 3005 } 3006 case ISD::BRCOND: 3007 // Expand brcond's setcc into its constituent parts and create a BR_CC 3008 // Node. 3009 Tmp1 = Node->getOperand(0); 3010 Tmp2 = Node->getOperand(1); 3011 if (Tmp2.getOpcode() == ISD::SETCC) { 3012 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, 3013 Tmp1, Tmp2.getOperand(2), 3014 Tmp2.getOperand(0), Tmp2.getOperand(1), 3015 Node->getOperand(2)); 3016 } else { 3017 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, 3018 DAG.getCondCode(ISD::SETNE), Tmp2, 3019 DAG.getConstant(0, Tmp2.getValueType()), 3020 Node->getOperand(2)); 3021 } 3022 Results.push_back(Tmp1); 3023 break; 3024 case ISD::SETCC: { 3025 Tmp1 = Node->getOperand(0); 3026 Tmp2 = Node->getOperand(1); 3027 Tmp3 = Node->getOperand(2); 3028 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl); 3029 3030 // If we expanded the SETCC into an AND/OR, return the new node 3031 if (Tmp2.getNode() == 0) { 3032 Results.push_back(Tmp1); 3033 break; 3034 } 3035 3036 // Otherwise, SETCC for the given comparison type must be completely 3037 // illegal; expand it into a SELECT_CC. 3038 EVT VT = Node->getValueType(0); 3039 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, 3040 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3); 3041 Results.push_back(Tmp1); 3042 break; 3043 } 3044 case ISD::SELECT_CC: { 3045 Tmp1 = Node->getOperand(0); // LHS 3046 Tmp2 = Node->getOperand(1); // RHS 3047 Tmp3 = Node->getOperand(2); // True 3048 Tmp4 = Node->getOperand(3); // False 3049 SDValue CC = Node->getOperand(4); 3050 3051 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()), 3052 Tmp1, Tmp2, CC, dl); 3053 3054 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!"); 3055 Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); 3056 CC = DAG.getCondCode(ISD::SETNE); 3057 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2, 3058 Tmp3, Tmp4, CC); 3059 Results.push_back(Tmp1); 3060 break; 3061 } 3062 case ISD::BR_CC: { 3063 Tmp1 = Node->getOperand(0); // Chain 3064 Tmp2 = Node->getOperand(2); // LHS 3065 Tmp3 = Node->getOperand(3); // RHS 3066 Tmp4 = Node->getOperand(1); // CC 3067 3068 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()), 3069 Tmp2, Tmp3, Tmp4, dl); 3070 LastCALLSEQ_END = DAG.getEntryNode(); 3071 3072 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!"); 3073 Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); 3074 Tmp4 = DAG.getCondCode(ISD::SETNE); 3075 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2, 3076 Tmp3, Node->getOperand(4)); 3077 Results.push_back(Tmp1); 3078 break; 3079 } 3080 case ISD::GLOBAL_OFFSET_TABLE: 3081 case ISD::GlobalAddress: 3082 case ISD::GlobalTLSAddress: 3083 case ISD::ExternalSymbol: 3084 case ISD::ConstantPool: 3085 case ISD::JumpTable: 3086 case ISD::INTRINSIC_W_CHAIN: 3087 case ISD::INTRINSIC_WO_CHAIN: 3088 case ISD::INTRINSIC_VOID: 3089 // FIXME: Custom lowering for these operations shouldn't return null! 3090 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 3091 Results.push_back(SDValue(Node, i)); 3092 break; 3093 } 3094} 3095void SelectionDAGLegalize::PromoteNode(SDNode *Node, 3096 SmallVectorImpl<SDValue> &Results) { 3097 EVT OVT = Node->getValueType(0); 3098 if (Node->getOpcode() == ISD::UINT_TO_FP || 3099 Node->getOpcode() == ISD::SINT_TO_FP || 3100 Node->getOpcode() == ISD::SETCC) { 3101 OVT = Node->getOperand(0).getValueType(); 3102 } 3103 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 3104 DebugLoc dl = Node->getDebugLoc(); 3105 SDValue Tmp1, Tmp2, Tmp3; 3106 switch (Node->getOpcode()) { 3107 case ISD::CTTZ: 3108 case ISD::CTLZ: 3109 case ISD::CTPOP: 3110 // Zero extend the argument. 3111 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 3112 // Perform the larger operation. 3113 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 3114 if (Node->getOpcode() == ISD::CTTZ) { 3115 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) 3116 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), 3117 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT), 3118 ISD::SETEQ); 3119 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, 3120 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1); 3121 } else if (Node->getOpcode() == ISD::CTLZ) { 3122 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 3123 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, 3124 DAG.getConstant(NVT.getSizeInBits() - 3125 OVT.getSizeInBits(), NVT)); 3126 } 3127 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 3128 break; 3129 case ISD::BSWAP: { 3130 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); 3131 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 3132 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1); 3133 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, 3134 DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); 3135 Results.push_back(Tmp1); 3136 break; 3137 } 3138 case ISD::FP_TO_UINT: 3139 case ISD::FP_TO_SINT: 3140 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0), 3141 Node->getOpcode() == ISD::FP_TO_SINT, dl); 3142 Results.push_back(Tmp1); 3143 break; 3144 case ISD::UINT_TO_FP: 3145 case ISD::SINT_TO_FP: 3146 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0), 3147 Node->getOpcode() == ISD::SINT_TO_FP, dl); 3148 Results.push_back(Tmp1); 3149 break; 3150 case ISD::AND: 3151 case ISD::OR: 3152 case ISD::XOR: { 3153 unsigned ExtOp, TruncOp; 3154 if (OVT.isVector()) { 3155 ExtOp = ISD::BIT_CONVERT; 3156 TruncOp = ISD::BIT_CONVERT; 3157 } else { 3158 assert(OVT.isInteger() && "Cannot promote logic operation"); 3159 ExtOp = ISD::ANY_EXTEND; 3160 TruncOp = ISD::TRUNCATE; 3161 } 3162 // Promote each of the values to the new type. 3163 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 3164 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 3165 // Perform the larger operation, then convert back 3166 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 3167 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1)); 3168 break; 3169 } 3170 case ISD::SELECT: { 3171 unsigned ExtOp, TruncOp; 3172 if (Node->getValueType(0).isVector()) { 3173 ExtOp = ISD::BIT_CONVERT; 3174 TruncOp = ISD::BIT_CONVERT; 3175 } else if (Node->getValueType(0).isInteger()) { 3176 ExtOp = ISD::ANY_EXTEND; 3177 TruncOp = ISD::TRUNCATE; 3178 } else { 3179 ExtOp = ISD::FP_EXTEND; 3180 TruncOp = ISD::FP_ROUND; 3181 } 3182 Tmp1 = Node->getOperand(0); 3183 // Promote each of the values to the new type. 3184 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 3185 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 3186 // Perform the larger operation, then round down. 3187 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3); 3188 if (TruncOp != ISD::FP_ROUND) 3189 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1); 3190 else 3191 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1, 3192 DAG.getIntPtrConstant(0)); 3193 Results.push_back(Tmp1); 3194 break; 3195 } 3196 case ISD::VECTOR_SHUFFLE: { 3197 SmallVector<int, 8> Mask; 3198 cast<ShuffleVectorSDNode>(Node)->getMask(Mask); 3199 3200 // Cast the two input vectors. 3201 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0)); 3202 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(1)); 3203 3204 // Convert the shuffle mask to the right # elements. 3205 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); 3206 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Tmp1); 3207 Results.push_back(Tmp1); 3208 break; 3209 } 3210 case ISD::SETCC: { 3211 unsigned ExtOp = ISD::FP_EXTEND; 3212 if (NVT.isInteger()) { 3213 ISD::CondCode CCCode = 3214 cast<CondCodeSDNode>(Node->getOperand(2))->get(); 3215 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 3216 } 3217 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 3218 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 3219 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), 3220 Tmp1, Tmp2, Node->getOperand(2))); 3221 break; 3222 } 3223 } 3224} 3225 3226// SelectionDAG::Legalize - This is the entry point for the file. 3227// 3228void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) { 3229 /// run - This is the main entry point to this class. 3230 /// 3231 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG(); 3232} 3233 3234