LegalizeDAG.cpp revision 7cd2997a83444b4ce14644db1a985f6c717f765e
1//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source 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/Support/MathExtras.h" 18#include "llvm/Target/TargetLowering.h" 19#include "llvm/Target/TargetData.h" 20#include "llvm/Target/TargetOptions.h" 21#include "llvm/CallingConv.h" 22#include "llvm/Constants.h" 23#include <iostream> 24#include <set> 25using namespace llvm; 26 27//===----------------------------------------------------------------------===// 28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and 29/// hacks on it until the target machine can handle it. This involves 30/// eliminating value sizes the machine cannot handle (promoting small sizes to 31/// large sizes or splitting up large values into small values) as well as 32/// eliminating operations the machine cannot handle. 33/// 34/// This code also does a small amount of optimization and recognition of idioms 35/// as part of its processing. For example, if a target does not support a 36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 37/// will attempt merge setcc and brc instructions into brcc's. 38/// 39namespace { 40class SelectionDAGLegalize { 41 TargetLowering &TLI; 42 SelectionDAG &DAG; 43 44 /// LegalizeAction - This enum indicates what action we should take for each 45 /// value type the can occur in the program. 46 enum LegalizeAction { 47 Legal, // The target natively supports this value type. 48 Promote, // This should be promoted to the next larger type. 49 Expand, // This integer type should be broken into smaller pieces. 50 }; 51 52 /// ValueTypeActions - This is a bitvector that contains two bits for each 53 /// value type, where the two bits correspond to the LegalizeAction enum. 54 /// This can be queried with "getTypeAction(VT)". 55 unsigned long long ValueTypeActions; 56 57 /// LegalizedNodes - For nodes that are of legal width, and that have more 58 /// than one use, this map indicates what regularized operand to use. This 59 /// allows us to avoid legalizing the same thing more than once. 60 std::map<SDOperand, SDOperand> LegalizedNodes; 61 62 /// PromotedNodes - For nodes that are below legal width, and that have more 63 /// than one use, this map indicates what promoted value to use. This allows 64 /// us to avoid promoting the same thing more than once. 65 std::map<SDOperand, SDOperand> PromotedNodes; 66 67 /// ExpandedNodes - For nodes that need to be expanded, and which have more 68 /// than one use, this map indicates which which operands are the expanded 69 /// version of the input. This allows us to avoid expanding the same node 70 /// more than once. 71 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; 72 73 void AddLegalizedOperand(SDOperand From, SDOperand To) { 74 LegalizedNodes.insert(std::make_pair(From, To)); 75 // If someone requests legalization of the new node, return itself. 76 if (From != To) 77 LegalizedNodes.insert(std::make_pair(To, To)); 78 } 79 void AddPromotedOperand(SDOperand From, SDOperand To) { 80 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; 81 assert(isNew && "Got into the map somehow?"); 82 // If someone requests legalization of the new node, return itself. 83 LegalizedNodes.insert(std::make_pair(To, To)); 84 } 85 86public: 87 88 SelectionDAGLegalize(SelectionDAG &DAG); 89 90 /// getTypeAction - Return how we should legalize values of this type, either 91 /// it is already legal or we need to expand it into multiple registers of 92 /// smaller integer type, or we need to promote it to a larger type. 93 LegalizeAction getTypeAction(MVT::ValueType VT) const { 94 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3); 95 } 96 97 /// isTypeLegal - Return true if this type is legal on this target. 98 /// 99 bool isTypeLegal(MVT::ValueType VT) const { 100 return getTypeAction(VT) == Legal; 101 } 102 103 void LegalizeDAG(); 104 105private: 106 107 SDOperand LegalizeOp(SDOperand O); 108 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); 109 SDOperand PromoteOp(SDOperand O); 110 111 SDOperand ExpandLibCall(const char *Name, SDNode *Node, 112 SDOperand &Hi); 113 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, 114 SDOperand Source); 115 116 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); 117 SDOperand ExpandLegalINT_TO_FP(bool isSigned, 118 SDOperand LegalOp, 119 MVT::ValueType DestVT); 120 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, 121 bool isSigned); 122 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, 123 bool isSigned); 124 125 SDOperand ExpandBSWAP(SDOperand Op); 126 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); 127 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, 128 SDOperand &Lo, SDOperand &Hi); 129 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, 130 SDOperand &Lo, SDOperand &Hi); 131 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain); 132 133 SDOperand getIntPtrConstant(uint64_t Val) { 134 return DAG.getConstant(Val, TLI.getPointerTy()); 135 } 136}; 137} 138 139static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) { 140 switch (VecOp) { 141 default: assert(0 && "Don't know how to scalarize this opcode!"); 142 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD; 143 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB; 144 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL; 145 } 146} 147 148SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) 149 : TLI(dag.getTargetLoweringInfo()), DAG(dag), 150 ValueTypeActions(TLI.getValueTypeActions()) { 151 assert(MVT::LAST_VALUETYPE <= 32 && 152 "Too many value types for ValueTypeActions to hold!"); 153} 154 155/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a 156/// INT_TO_FP operation of the specified operand when the target requests that 157/// we expand it. At this point, we know that the result and operand types are 158/// legal for the target. 159SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, 160 SDOperand Op0, 161 MVT::ValueType DestVT) { 162 if (Op0.getValueType() == MVT::i32) { 163 // simple 32-bit [signed|unsigned] integer to float/double expansion 164 165 // get the stack frame index of a 8 byte buffer 166 MachineFunction &MF = DAG.getMachineFunction(); 167 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); 168 // get address of 8 byte buffer 169 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); 170 // word offset constant for Hi/Lo address computation 171 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); 172 // set up Hi and Lo (into buffer) address based on endian 173 SDOperand Hi, Lo; 174 if (TLI.isLittleEndian()) { 175 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff); 176 Lo = StackSlot; 177 } else { 178 Hi = StackSlot; 179 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff); 180 } 181 // if signed map to unsigned space 182 SDOperand Op0Mapped; 183 if (isSigned) { 184 // constant used to invert sign bit (signed to unsigned mapping) 185 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); 186 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); 187 } else { 188 Op0Mapped = Op0; 189 } 190 // store the lo of the constructed double - based on integer input 191 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), 192 Op0Mapped, Lo, DAG.getSrcValue(NULL)); 193 // initial hi portion of constructed double 194 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); 195 // store the hi of the constructed double - biased exponent 196 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1, 197 InitialHi, Hi, DAG.getSrcValue(NULL)); 198 // load the constructed double 199 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, 200 DAG.getSrcValue(NULL)); 201 // FP constant to bias correct the final result 202 SDOperand Bias = DAG.getConstantFP(isSigned ? 203 BitsToDouble(0x4330000080000000ULL) 204 : BitsToDouble(0x4330000000000000ULL), 205 MVT::f64); 206 // subtract the bias 207 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); 208 // final result 209 SDOperand Result; 210 // handle final rounding 211 if (DestVT == MVT::f64) { 212 // do nothing 213 Result = Sub; 214 } else { 215 // if f32 then cast to f32 216 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); 217 } 218 return Result; 219 } 220 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); 221 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); 222 223 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, 224 DAG.getConstant(0, Op0.getValueType()), 225 ISD::SETLT); 226 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); 227 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), 228 SignSet, Four, Zero); 229 230 // If the sign bit of the integer is set, the large number will be treated 231 // as a negative number. To counteract this, the dynamic code adds an 232 // offset depending on the data type. 233 uint64_t FF; 234 switch (Op0.getValueType()) { 235 default: assert(0 && "Unsupported integer type!"); 236 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 237 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 238 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 239 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 240 } 241 if (TLI.isLittleEndian()) FF <<= 32; 242 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); 243 244 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); 245 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); 246 SDOperand FudgeInReg; 247 if (DestVT == MVT::f32) 248 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, 249 DAG.getSrcValue(NULL)); 250 else { 251 assert(DestVT == MVT::f64 && "Unexpected conversion"); 252 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, 253 DAG.getEntryNode(), CPIdx, 254 DAG.getSrcValue(NULL), MVT::f32)); 255 } 256 257 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); 258} 259 260/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a 261/// *INT_TO_FP operation of the specified operand when the target requests that 262/// we promote it. At this point, we know that the result and operand types are 263/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 264/// operation that takes a larger input. 265SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, 266 MVT::ValueType DestVT, 267 bool isSigned) { 268 // First step, figure out the appropriate *INT_TO_FP operation to use. 269 MVT::ValueType NewInTy = LegalOp.getValueType(); 270 271 unsigned OpToUse = 0; 272 273 // Scan for the appropriate larger type to use. 274 while (1) { 275 NewInTy = (MVT::ValueType)(NewInTy+1); 276 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); 277 278 // If the target supports SINT_TO_FP of this type, use it. 279 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { 280 default: break; 281 case TargetLowering::Legal: 282 if (!TLI.isTypeLegal(NewInTy)) 283 break; // Can't use this datatype. 284 // FALL THROUGH. 285 case TargetLowering::Custom: 286 OpToUse = ISD::SINT_TO_FP; 287 break; 288 } 289 if (OpToUse) break; 290 if (isSigned) continue; 291 292 // If the target supports UINT_TO_FP of this type, use it. 293 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { 294 default: break; 295 case TargetLowering::Legal: 296 if (!TLI.isTypeLegal(NewInTy)) 297 break; // Can't use this datatype. 298 // FALL THROUGH. 299 case TargetLowering::Custom: 300 OpToUse = ISD::UINT_TO_FP; 301 break; 302 } 303 if (OpToUse) break; 304 305 // Otherwise, try a larger type. 306 } 307 308 // Okay, we found the operation and type to use. Zero extend our input to the 309 // desired type then run the operation on it. 310 return DAG.getNode(OpToUse, DestVT, 311 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 312 NewInTy, LegalOp)); 313} 314 315/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a 316/// FP_TO_*INT operation of the specified operand when the target requests that 317/// we promote it. At this point, we know that the result and operand types are 318/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 319/// operation that returns a larger result. 320SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, 321 MVT::ValueType DestVT, 322 bool isSigned) { 323 // First step, figure out the appropriate FP_TO*INT operation to use. 324 MVT::ValueType NewOutTy = DestVT; 325 326 unsigned OpToUse = 0; 327 328 // Scan for the appropriate larger type to use. 329 while (1) { 330 NewOutTy = (MVT::ValueType)(NewOutTy+1); 331 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); 332 333 // If the target supports FP_TO_SINT returning this type, use it. 334 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { 335 default: break; 336 case TargetLowering::Legal: 337 if (!TLI.isTypeLegal(NewOutTy)) 338 break; // Can't use this datatype. 339 // FALL THROUGH. 340 case TargetLowering::Custom: 341 OpToUse = ISD::FP_TO_SINT; 342 break; 343 } 344 if (OpToUse) break; 345 346 // If the target supports FP_TO_UINT of this type, use it. 347 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { 348 default: break; 349 case TargetLowering::Legal: 350 if (!TLI.isTypeLegal(NewOutTy)) 351 break; // Can't use this datatype. 352 // FALL THROUGH. 353 case TargetLowering::Custom: 354 OpToUse = ISD::FP_TO_UINT; 355 break; 356 } 357 if (OpToUse) break; 358 359 // Otherwise, try a larger type. 360 } 361 362 // Okay, we found the operation and type to use. Truncate the result of the 363 // extended FP_TO_*INT operation to the desired size. 364 return DAG.getNode(ISD::TRUNCATE, DestVT, 365 DAG.getNode(OpToUse, NewOutTy, LegalOp)); 366} 367 368/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. 369/// 370SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { 371 MVT::ValueType VT = Op.getValueType(); 372 MVT::ValueType SHVT = TLI.getShiftAmountTy(); 373 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; 374 switch (VT) { 375 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); 376 case MVT::i16: 377 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); 378 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); 379 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); 380 case MVT::i32: 381 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); 382 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); 383 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); 384 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); 385 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); 386 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); 387 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); 388 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); 389 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); 390 case MVT::i64: 391 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); 392 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); 393 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); 394 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); 395 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); 396 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); 397 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); 398 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); 399 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); 400 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); 401 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); 402 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); 403 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); 404 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); 405 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); 406 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); 407 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); 408 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); 409 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); 410 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); 411 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); 412 } 413} 414 415/// ExpandBitCount - Expand the specified bitcount instruction into operations. 416/// 417SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { 418 switch (Opc) { 419 default: assert(0 && "Cannot expand this yet!"); 420 case ISD::CTPOP: { 421 static const uint64_t mask[6] = { 422 0x5555555555555555ULL, 0x3333333333333333ULL, 423 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, 424 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL 425 }; 426 MVT::ValueType VT = Op.getValueType(); 427 MVT::ValueType ShVT = TLI.getShiftAmountTy(); 428 unsigned len = getSizeInBits(VT); 429 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { 430 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) 431 SDOperand Tmp2 = DAG.getConstant(mask[i], VT); 432 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); 433 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), 434 DAG.getNode(ISD::AND, VT, 435 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); 436 } 437 return Op; 438 } 439 case ISD::CTLZ: { 440 // for now, we do this: 441 // x = x | (x >> 1); 442 // x = x | (x >> 2); 443 // ... 444 // x = x | (x >>16); 445 // x = x | (x >>32); // for 64-bit input 446 // return popcount(~x); 447 // 448 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc 449 MVT::ValueType VT = Op.getValueType(); 450 MVT::ValueType ShVT = TLI.getShiftAmountTy(); 451 unsigned len = getSizeInBits(VT); 452 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { 453 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); 454 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); 455 } 456 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); 457 return DAG.getNode(ISD::CTPOP, VT, Op); 458 } 459 case ISD::CTTZ: { 460 // for now, we use: { return popcount(~x & (x - 1)); } 461 // unless the target has ctlz but not ctpop, in which case we use: 462 // { return 32 - nlz(~x & (x-1)); } 463 // see also http://www.hackersdelight.org/HDcode/ntz.cc 464 MVT::ValueType VT = Op.getValueType(); 465 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); 466 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, 467 DAG.getNode(ISD::XOR, VT, Op, Tmp2), 468 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); 469 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. 470 if (!TLI.isOperationLegal(ISD::CTPOP, VT) && 471 TLI.isOperationLegal(ISD::CTLZ, VT)) 472 return DAG.getNode(ISD::SUB, VT, 473 DAG.getConstant(getSizeInBits(VT), VT), 474 DAG.getNode(ISD::CTLZ, VT, Tmp3)); 475 return DAG.getNode(ISD::CTPOP, VT, Tmp3); 476 } 477 } 478} 479 480 481/// ComputeTopDownOrdering - Add the specified node to the Order list if it has 482/// not been visited yet and if all of its operands have already been visited. 483static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order, 484 std::map<SDNode*, unsigned> &Visited) { 485 if (++Visited[N] != N->getNumOperands()) 486 return; // Haven't visited all operands yet 487 488 Order.push_back(N); 489 490 if (N->hasOneUse()) { // Tail recurse in common case. 491 ComputeTopDownOrdering(*N->use_begin(), Order, Visited); 492 return; 493 } 494 495 // Now that we have N in, add anything that uses it if all of their operands 496 // are now done. 497 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI) 498 ComputeTopDownOrdering(*UI, Order, Visited); 499} 500 501 502void SelectionDAGLegalize::LegalizeDAG() { 503 // The legalize process is inherently a bottom-up recursive process (users 504 // legalize their uses before themselves). Given infinite stack space, we 505 // could just start legalizing on the root and traverse the whole graph. In 506 // practice however, this causes us to run out of stack space on large basic 507 // blocks. To avoid this problem, compute an ordering of the nodes where each 508 // node is only legalized after all of its operands are legalized. 509 std::map<SDNode*, unsigned> Visited; 510 std::vector<SDNode*> Order; 511 512 // Compute ordering from all of the leaves in the graphs, those (like the 513 // entry node) that have no operands. 514 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 515 E = DAG.allnodes_end(); I != E; ++I) { 516 if (I->getNumOperands() == 0) { 517 Visited[I] = 0 - 1U; 518 ComputeTopDownOrdering(I, Order, Visited); 519 } 520 } 521 522 assert(Order.size() == Visited.size() && 523 Order.size() == 524 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && 525 "Error: DAG is cyclic!"); 526 Visited.clear(); 527 528 for (unsigned i = 0, e = Order.size(); i != e; ++i) { 529 SDNode *N = Order[i]; 530 switch (getTypeAction(N->getValueType(0))) { 531 default: assert(0 && "Bad type action!"); 532 case Legal: 533 LegalizeOp(SDOperand(N, 0)); 534 break; 535 case Promote: 536 PromoteOp(SDOperand(N, 0)); 537 break; 538 case Expand: { 539 SDOperand X, Y; 540 ExpandOp(SDOperand(N, 0), X, Y); 541 break; 542 } 543 } 544 } 545 546 // Finally, it's possible the root changed. Get the new root. 547 SDOperand OldRoot = DAG.getRoot(); 548 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); 549 DAG.setRoot(LegalizedNodes[OldRoot]); 550 551 ExpandedNodes.clear(); 552 LegalizedNodes.clear(); 553 PromotedNodes.clear(); 554 555 // Remove dead nodes now. 556 DAG.RemoveDeadNodes(OldRoot.Val); 557} 558 559SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { 560 assert(isTypeLegal(Op.getValueType()) && 561 "Caller should expand or promote operands that are not legal!"); 562 SDNode *Node = Op.Val; 563 564 // If this operation defines any values that cannot be represented in a 565 // register on this target, make sure to expand or promote them. 566 if (Node->getNumValues() > 1) { 567 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 568 switch (getTypeAction(Node->getValueType(i))) { 569 case Legal: break; // Nothing to do. 570 case Expand: { 571 SDOperand T1, T2; 572 ExpandOp(Op.getValue(i), T1, T2); 573 assert(LegalizedNodes.count(Op) && 574 "Expansion didn't add legal operands!"); 575 return LegalizedNodes[Op]; 576 } 577 case Promote: 578 PromoteOp(Op.getValue(i)); 579 assert(LegalizedNodes.count(Op) && 580 "Promotion didn't add legal operands!"); 581 return LegalizedNodes[Op]; 582 } 583 } 584 585 // Note that LegalizeOp may be reentered even from single-use nodes, which 586 // means that we always must cache transformed nodes. 587 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); 588 if (I != LegalizedNodes.end()) return I->second; 589 590 SDOperand Tmp1, Tmp2, Tmp3, Tmp4; 591 SDOperand Result = Op; 592 bool isCustom = false; 593 594 switch (Node->getOpcode()) { 595 default: 596 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 597 // If this is a target node, legalize it by legalizing the operands then 598 // passing it through. 599 std::vector<SDOperand> Ops; 600 bool Changed = false; 601 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 602 Ops.push_back(LegalizeOp(Node->getOperand(i))); 603 Changed = Changed || Node->getOperand(i) != Ops.back(); 604 } 605 if (Changed) 606 if (Node->getNumValues() == 1) 607 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); 608 else { 609 std::vector<MVT::ValueType> VTs(Node->value_begin(), 610 Node->value_end()); 611 Result = DAG.getNode(Node->getOpcode(), VTs, Ops); 612 } 613 614 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 615 AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); 616 return Result.getValue(Op.ResNo); 617 } 618 // Otherwise this is an unhandled builtin node. splat. 619 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 620 assert(0 && "Do not know how to legalize this operator!"); 621 abort(); 622 case ISD::EntryToken: 623 case ISD::FrameIndex: 624 case ISD::TargetFrameIndex: 625 case ISD::Register: 626 case ISD::TargetConstant: 627 case ISD::TargetConstantPool: 628 case ISD::GlobalAddress: 629 case ISD::TargetGlobalAddress: 630 case ISD::ExternalSymbol: 631 case ISD::TargetExternalSymbol: 632 case ISD::ConstantPool: // Nothing to do. 633 case ISD::BasicBlock: 634 case ISD::CONDCODE: 635 case ISD::VALUETYPE: 636 case ISD::SRCVALUE: 637 case ISD::STRING: 638 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 639 default: assert(0 && "This action is not supported yet!"); 640 case TargetLowering::Custom: { 641 SDOperand Tmp = TLI.LowerOperation(Op, DAG); 642 if (Tmp.Val) { 643 Result = Tmp; 644 break; 645 } 646 } // FALLTHROUGH if the target doesn't want to lower this op after all. 647 case TargetLowering::Legal: 648 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!"); 649 break; 650 } 651 break; 652 case ISD::AssertSext: 653 case ISD::AssertZext: 654 Tmp1 = LegalizeOp(Node->getOperand(0)); 655 if (Tmp1 != Node->getOperand(0)) 656 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, 657 Node->getOperand(1)); 658 break; 659 case ISD::MERGE_VALUES: 660 Result = Node->getOperand(Op.ResNo); 661 break; 662 case ISD::CopyFromReg: 663 Tmp1 = LegalizeOp(Node->getOperand(0)); 664 Result = Op.getValue(0); 665 if (Node->getNumValues() == 2) { 666 if (Tmp1 != Node->getOperand(0)) 667 Result = DAG.getCopyFromReg(Tmp1, 668 cast<RegisterSDNode>(Node->getOperand(1))->getReg(), 669 Node->getValueType(0)); 670 } else { 671 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); 672 if (Node->getNumOperands() == 3) 673 Tmp2 = LegalizeOp(Node->getOperand(2)); 674 if (Tmp1 != Node->getOperand(0) || 675 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2))) 676 Result = DAG.getCopyFromReg(Tmp1, 677 cast<RegisterSDNode>(Node->getOperand(1))->getReg(), 678 Node->getValueType(0), Tmp2); 679 AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); 680 } 681 // Since CopyFromReg produces two values, make sure to remember that we 682 // legalized both of them. 683 AddLegalizedOperand(Op.getValue(0), Result); 684 AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); 685 return Result.getValue(Op.ResNo); 686 case ISD::UNDEF: { 687 MVT::ValueType VT = Op.getValueType(); 688 switch (TLI.getOperationAction(ISD::UNDEF, VT)) { 689 default: assert(0 && "This action is not supported yet!"); 690 case TargetLowering::Expand: 691 if (MVT::isInteger(VT)) 692 Result = DAG.getConstant(0, VT); 693 else if (MVT::isFloatingPoint(VT)) 694 Result = DAG.getConstantFP(0, VT); 695 else 696 assert(0 && "Unknown value type!"); 697 break; 698 case TargetLowering::Legal: 699 break; 700 } 701 break; 702 } 703 704 case ISD::LOCATION: 705 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); 706 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. 707 708 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { 709 case TargetLowering::Promote: 710 default: assert(0 && "This action is not supported yet!"); 711 case TargetLowering::Expand: { 712 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); 713 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); 714 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other); 715 716 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) { 717 const std::string &FName = 718 cast<StringSDNode>(Node->getOperand(3))->getValue(); 719 const std::string &DirName = 720 cast<StringSDNode>(Node->getOperand(4))->getValue(); 721 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName); 722 723 std::vector<SDOperand> Ops; 724 Ops.push_back(Tmp1); // chain 725 SDOperand LineOp = Node->getOperand(1); 726 SDOperand ColOp = Node->getOperand(2); 727 728 if (useDEBUG_LOC) { 729 Ops.push_back(LineOp); // line # 730 Ops.push_back(ColOp); // col # 731 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id 732 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops); 733 } else { 734 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue(); 735 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue(); 736 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile); 737 Ops.push_back(DAG.getConstant(ID, MVT::i32)); 738 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops); 739 } 740 } else { 741 Result = Tmp1; // chain 742 } 743 break; 744 } 745 case TargetLowering::Legal: 746 if (Tmp1 != Node->getOperand(0) || 747 getTypeAction(Node->getOperand(1).getValueType()) == Promote) { 748 std::vector<SDOperand> Ops; 749 Ops.push_back(Tmp1); 750 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { 751 Ops.push_back(Node->getOperand(1)); // line # must be legal. 752 Ops.push_back(Node->getOperand(2)); // col # must be legal. 753 } else { 754 // Otherwise promote them. 755 Ops.push_back(PromoteOp(Node->getOperand(1))); 756 Ops.push_back(PromoteOp(Node->getOperand(2))); 757 } 758 Ops.push_back(Node->getOperand(3)); // filename must be legal. 759 Ops.push_back(Node->getOperand(4)); // working dir # must be legal. 760 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops); 761 } 762 break; 763 } 764 break; 765 766 case ISD::DEBUG_LOC: 767 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); 768 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { 769 default: assert(0 && "This action is not supported yet!"); 770 case TargetLowering::Legal: 771 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 772 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. 773 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. 774 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. 775 776 if (Tmp1 != Node->getOperand(0) || 777 Tmp2 != Node->getOperand(1) || 778 Tmp3 != Node->getOperand(2) || 779 Tmp4 != Node->getOperand(3)) { 780 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4); 781 } 782 break; 783 } 784 break; 785 786 case ISD::DEBUG_LABEL: 787 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!"); 788 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) { 789 default: assert(0 && "This action is not supported yet!"); 790 case TargetLowering::Legal: 791 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 792 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. 793 794 if (Tmp1 != Node->getOperand(0) || 795 Tmp2 != Node->getOperand(1)) { 796 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Tmp1, Tmp2); 797 } 798 break; 799 } 800 break; 801 802 case ISD::Constant: 803 // We know we don't need to expand constants here, constants only have one 804 // value and we check that it is fine above. 805 806 // FIXME: Maybe we should handle things like targets that don't support full 807 // 32-bit immediates? 808 break; 809 case ISD::ConstantFP: { 810 // Spill FP immediates to the constant pool if the target cannot directly 811 // codegen them. Targets often have some immediate values that can be 812 // efficiently generated into an FP register without a load. We explicitly 813 // leave these constants as ConstantFP nodes for the target to deal with. 814 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 815 816 // Check to see if this FP immediate is already legal. 817 bool isLegal = false; 818 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), 819 E = TLI.legal_fpimm_end(); I != E; ++I) 820 if (CFP->isExactlyValue(*I)) { 821 isLegal = true; 822 break; 823 } 824 825 if (!isLegal) { 826 // Otherwise we need to spill the constant to memory. 827 bool Extend = false; 828 829 // If a FP immediate is precise when represented as a float and if the 830 // target can do an extending load from float to double, we put it into 831 // the constant pool as a float, even if it's is statically typed as a 832 // double. 833 MVT::ValueType VT = CFP->getValueType(0); 834 bool isDouble = VT == MVT::f64; 835 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : 836 Type::FloatTy, CFP->getValue()); 837 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && 838 // Only do this if the target has a native EXTLOAD instruction from 839 // f32. 840 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) { 841 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); 842 VT = MVT::f32; 843 Extend = true; 844 } 845 846 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); 847 if (Extend) { 848 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), 849 CPIdx, DAG.getSrcValue(NULL), MVT::f32); 850 } else { 851 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, 852 DAG.getSrcValue(NULL)); 853 } 854 } 855 break; 856 } 857 case ISD::ConstantVec: { 858 // We assume that vector constants are not legal, and will be immediately 859 // spilled to the constant pool. 860 // 861 // FIXME: Allow custom lowering to TargetConstantVec's. 862 // 863 // Create a ConstantPacked, and put it in the constant pool. 864 std::vector<Constant*> CV; 865 MVT::ValueType VT = Node->getValueType(0); 866 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) { 867 SDOperand OpN = Node->getOperand(I); 868 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType()); 869 if (MVT::isFloatingPoint(VT)) 870 CV.push_back(ConstantFP::get(OpNTy, 871 cast<ConstantFPSDNode>(OpN)->getValue())); 872 else 873 CV.push_back(ConstantUInt::get(OpNTy, 874 cast<ConstantSDNode>(OpN)->getValue())); 875 } 876 Constant *CP = ConstantPacked::get(CV); 877 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); 878 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL)); 879 break; 880 } 881 case ISD::TokenFactor: 882 if (Node->getNumOperands() == 2) { 883 bool Changed = false; 884 SDOperand Op0 = LegalizeOp(Node->getOperand(0)); 885 SDOperand Op1 = LegalizeOp(Node->getOperand(1)); 886 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1)) 887 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1); 888 } else { 889 std::vector<SDOperand> Ops; 890 bool Changed = false; 891 // Legalize the operands. 892 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 893 SDOperand Op = Node->getOperand(i); 894 Ops.push_back(LegalizeOp(Op)); 895 Changed |= Ops[i] != Op; 896 } 897 if (Changed) 898 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops); 899 } 900 break; 901 902 case ISD::CALLSEQ_START: 903 case ISD::CALLSEQ_END: 904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 905 // Do not try to legalize the target-specific arguments (#1+) 906 Tmp2 = Node->getOperand(0); 907 if (Tmp1 != Tmp2) 908 Node->setAdjCallChain(Tmp1); 909 910 // If this has a flag input, do legalize it. 911 if (Node->getOperand(Node->getNumOperands()-1).getValueType() == MVT::Flag){ 912 Tmp1 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); 913 if (Tmp1 != Node->getOperand(Node->getNumOperands()-1)) 914 Node->setAdjCallFlag(Tmp1); 915 } 916 917 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These 918 // nodes are treated specially and are mutated in place. This makes the dag 919 // legalization process more efficient and also makes libcall insertion 920 // easier. 921 break; 922 case ISD::DYNAMIC_STACKALLOC: { 923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 924 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. 925 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. 926 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 927 Tmp3 != Node->getOperand(2)) { 928 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); 929 std::vector<SDOperand> Ops; 930 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); 931 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops); 932 } else 933 Result = Op.getValue(0); 934 935 Tmp1 = Result; 936 Tmp2 = Result.getValue(1); 937 switch (TLI.getOperationAction(Node->getOpcode(), 938 Node->getValueType(0))) { 939 default: assert(0 && "This action is not supported yet!"); 940 case TargetLowering::Expand: { 941 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); 942 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 943 " not tell us which reg is the stack pointer!"); 944 SDOperand Chain = Tmp1.getOperand(0); 945 SDOperand Size = Tmp2.getOperand(1); 946 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); 947 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value 948 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain 949 break; 950 } 951 case TargetLowering::Custom: 952 Tmp3 = TLI.LowerOperation(Tmp1, DAG); 953 if (Tmp3.Val) { 954 Tmp1 = Tmp3; 955 Tmp2 = Tmp3.getValue(1); 956 } 957 break; 958 case TargetLowering::Legal: 959 break; 960 } 961 // Since this op produce two values, make sure to remember that we 962 // legalized both of them. 963 AddLegalizedOperand(SDOperand(Node, 0), LegalizeOp(Tmp1)); 964 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Tmp2)); 965 return Op.ResNo ? Tmp2 : Tmp1; 966 } 967 case ISD::INLINEASM: 968 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain. 969 Tmp2 = Node->getOperand(Node->getNumOperands()-1); 970 if (Tmp2.getValueType() != MVT::Flag) // Legalize Flag if it exists. 971 Tmp2 = Tmp3 = SDOperand(0, 0); 972 else 973 Tmp3 = LegalizeOp(Tmp2); 974 975 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) { 976 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); 977 Ops[0] = Tmp1; 978 Ops.back() = Tmp3; 979 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); 980 Result = DAG.getNode(ISD::INLINEASM, VTs, Ops); 981 } else { 982 Result = SDOperand(Node, 0); 983 } 984 985 // INLINE asm returns a chain and flag, make sure to add both to the map. 986 AddLegalizedOperand(SDOperand(Node, 0), Result); 987 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 988 return Result.getValue(Op.ResNo); 989 case ISD::BR: 990 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 991 if (Tmp1 != Node->getOperand(0)) 992 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1)); 993 break; 994 995 case ISD::BRCOND: 996 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 997 998 switch (getTypeAction(Node->getOperand(1).getValueType())) { 999 case Expand: assert(0 && "It's impossible to expand bools"); 1000 case Legal: 1001 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. 1002 break; 1003 case Promote: 1004 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. 1005 break; 1006 } 1007 1008 // Basic block destination (Op#2) is always legal. 1009 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 1010 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, 1011 Node->getOperand(2)); 1012 1013 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { 1014 default: assert(0 && "This action is not supported yet!"); 1015 case TargetLowering::Legal: break; 1016 case TargetLowering::Custom: 1017 Tmp1 = TLI.LowerOperation(Result, DAG); 1018 if (Tmp1.Val) Result = Tmp1; 1019 break; 1020 case TargetLowering::Expand: 1021 // Expand brcond's setcc into its constituent parts and create a BR_CC 1022 // Node. 1023 if (Tmp2.getOpcode() == ISD::SETCC) { 1024 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), 1025 Tmp2.getOperand(0), Tmp2.getOperand(1), 1026 Node->getOperand(2)); 1027 } else { 1028 // Make sure the condition is either zero or one. It may have been 1029 // promoted from something else. 1030 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); 1031 1032 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, 1033 DAG.getCondCode(ISD::SETNE), Tmp2, 1034 DAG.getConstant(0, Tmp2.getValueType()), 1035 Node->getOperand(2)); 1036 } 1037 break; 1038 } 1039 break; 1040 case ISD::BR_CC: 1041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1042 if (!isTypeLegal(Node->getOperand(2).getValueType())) { 1043 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), 1044 Node->getOperand(2), // LHS 1045 Node->getOperand(3), // RHS 1046 Node->getOperand(1))); 1047 // If we get a SETCC back from legalizing the SETCC node we just 1048 // created, then use its LHS, RHS, and CC directly in creating a new 1049 // node. Otherwise, select between the true and false value based on 1050 // comparing the result of the legalized with zero. 1051 if (Tmp2.getOpcode() == ISD::SETCC) { 1052 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), 1053 Tmp2.getOperand(0), Tmp2.getOperand(1), 1054 Node->getOperand(4)); 1055 } else { 1056 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, 1057 DAG.getCondCode(ISD::SETNE), 1058 Tmp2, DAG.getConstant(0, Tmp2.getValueType()), 1059 Node->getOperand(4)); 1060 } 1061 break; 1062 } 1063 1064 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS 1065 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS 1066 1067 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) || 1068 Tmp3 != Node->getOperand(3)) { 1069 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1), 1070 Tmp2, Tmp3, Node->getOperand(4)); 1071 } 1072 1073 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { 1074 default: assert(0 && "Unexpected action for BR_CC!"); 1075 case TargetLowering::Legal: break; 1076 case TargetLowering::Custom: 1077 Tmp4 = TLI.LowerOperation(Result, DAG); 1078 if (Tmp4.Val) Result = Tmp4; 1079 break; 1080 } 1081 break; 1082 case ISD::BRCONDTWOWAY: 1083 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1084 switch (getTypeAction(Node->getOperand(1).getValueType())) { 1085 case Expand: assert(0 && "It's impossible to expand bools"); 1086 case Legal: 1087 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. 1088 break; 1089 case Promote: 1090 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. 1091 break; 1092 } 1093 1094 1095 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR 1096 // pair. 1097 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) { 1098 case TargetLowering::Promote: 1099 default: assert(0 && "This action is not supported yet!"); 1100 case TargetLowering::Legal: 1101 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { 1102 std::vector<SDOperand> Ops; 1103 Ops.push_back(Tmp1); 1104 Ops.push_back(Tmp2); 1105 Ops.push_back(Node->getOperand(2)); 1106 Ops.push_back(Node->getOperand(3)); 1107 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops); 1108 } 1109 break; 1110 case TargetLowering::Expand: 1111 // If BRTWOWAY_CC is legal for this target, then simply expand this node 1112 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a 1113 // BRCOND/BR pair. 1114 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) { 1115 if (Tmp2.getOpcode() == ISD::SETCC) { 1116 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2), 1117 Tmp2.getOperand(0), Tmp2.getOperand(1), 1118 Node->getOperand(2), Node->getOperand(3)); 1119 } else { 1120 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2, 1121 DAG.getConstant(0, Tmp2.getValueType()), 1122 Node->getOperand(2), Node->getOperand(3)); 1123 } 1124 } else { 1125 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, 1126 Node->getOperand(2)); 1127 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3)); 1128 } 1129 break; 1130 } 1131 break; 1132 case ISD::BRTWOWAY_CC: 1133 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1134 if (isTypeLegal(Node->getOperand(2).getValueType())) { 1135 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS 1136 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS 1137 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) || 1138 Tmp3 != Node->getOperand(3)) { 1139 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3, 1140 Node->getOperand(4), Node->getOperand(5)); 1141 } 1142 break; 1143 } else { 1144 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), 1145 Node->getOperand(2), // LHS 1146 Node->getOperand(3), // RHS 1147 Node->getOperand(1))); 1148 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR 1149 // pair. 1150 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) { 1151 default: assert(0 && "This action is not supported yet!"); 1152 case TargetLowering::Legal: 1153 // If we get a SETCC back from legalizing the SETCC node we just 1154 // created, then use its LHS, RHS, and CC directly in creating a new 1155 // node. Otherwise, select between the true and false value based on 1156 // comparing the result of the legalized with zero. 1157 if (Tmp2.getOpcode() == ISD::SETCC) { 1158 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2), 1159 Tmp2.getOperand(0), Tmp2.getOperand(1), 1160 Node->getOperand(4), Node->getOperand(5)); 1161 } else { 1162 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2, 1163 DAG.getConstant(0, Tmp2.getValueType()), 1164 Node->getOperand(4), Node->getOperand(5)); 1165 } 1166 break; 1167 case TargetLowering::Expand: 1168 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2, 1169 Node->getOperand(4)); 1170 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5)); 1171 break; 1172 } 1173 } 1174 break; 1175 case ISD::LOAD: { 1176 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1177 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1178 1179 MVT::ValueType VT = Node->getValueType(0); 1180 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 1181 Result = DAG.getLoad(VT, Tmp1, Tmp2, Node->getOperand(2)); 1182 else 1183 Result = SDOperand(Node, 0); 1184 1185 switch (TLI.getOperationAction(Node->getOpcode(), VT)) { 1186 default: assert(0 && "This action is not supported yet!"); 1187 case TargetLowering::Custom: 1188 Tmp1 = TLI.LowerOperation(Result, DAG); 1189 if (Tmp1.Val) { 1190 // Since loads produce two values, make sure to remember that we 1191 // legalized both of them. 1192 AddLegalizedOperand(SDOperand(Node, 0), LegalizeOp(Tmp1)); 1193 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Tmp1.getValue(1))); 1194 return Tmp1.getValue(Op.ResNo); 1195 } 1196 // FALLTHROUGH if the target thinks it is legal. 1197 case TargetLowering::Legal: 1198 // Since loads produce two values, make sure to remember that we legalized 1199 // both of them. 1200 AddLegalizedOperand(SDOperand(Node, 0), Result); 1201 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1202 return Result.getValue(Op.ResNo); 1203 } 1204 assert(0 && "Unreachable"); 1205 } 1206 case ISD::EXTLOAD: 1207 case ISD::SEXTLOAD: 1208 case ISD::ZEXTLOAD: { 1209 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1210 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1211 1212 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 1213 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { 1214 default: assert(0 && "This action is not supported yet!"); 1215 case TargetLowering::Promote: 1216 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!"); 1217 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0), 1218 Tmp1, Tmp2, Node->getOperand(2), MVT::i8); 1219 // Since loads produce two values, make sure to remember that we legalized 1220 // both of them. 1221 AddLegalizedOperand(SDOperand(Node, 0), Result); 1222 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1223 return Result.getValue(Op.ResNo); 1224 1225 case TargetLowering::Custom: 1226 isCustom = true; 1227 // FALLTHROUGH 1228 case TargetLowering::Legal: 1229 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 1230 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0), 1231 Tmp1, Tmp2, Node->getOperand(2), SrcVT); 1232 else 1233 Result = SDOperand(Node, 0); 1234 Tmp1 = Result.getValue(1); 1235 1236 if (isCustom) { 1237 Tmp3 = TLI.LowerOperation(Tmp3, DAG); 1238 if (Tmp3.Val) { 1239 Result = LegalizeOp(Tmp3); 1240 Tmp1 = LegalizeOp(Tmp3.getValue(1)); 1241 } 1242 } 1243 1244 // Since loads produce two values, make sure to remember that we legalized 1245 // both of them. 1246 AddLegalizedOperand(SDOperand(Node, 0), Result); 1247 AddLegalizedOperand(SDOperand(Node, 1), Tmp1); 1248 return Op.ResNo ? Tmp1 : Result; 1249 case TargetLowering::Expand: 1250 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND 1251 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { 1252 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2)); 1253 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); 1254 Result = LegalizeOp(Result); // Relegalize new nodes. 1255 Load = LegalizeOp(Load); 1256 AddLegalizedOperand(SDOperand(Node, 0), Result); 1257 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1)); 1258 return Op.ResNo ? Load.getValue(1) : Result; 1259 } 1260 assert(Node->getOpcode() != ISD::EXTLOAD && 1261 "EXTLOAD should always be supported!"); 1262 // Turn the unsupported load into an EXTLOAD followed by an explicit 1263 // zero/sign extend inreg. 1264 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), 1265 Tmp1, Tmp2, Node->getOperand(2), SrcVT); 1266 SDOperand ValRes; 1267 if (Node->getOpcode() == ISD::SEXTLOAD) 1268 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 1269 Result, DAG.getValueType(SrcVT)); 1270 else 1271 ValRes = DAG.getZeroExtendInReg(Result, SrcVT); 1272 Result = LegalizeOp(Result); // Relegalize new nodes. 1273 ValRes = LegalizeOp(ValRes); // Relegalize new nodes. 1274 AddLegalizedOperand(SDOperand(Node, 0), ValRes); 1275 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1276 return Op.ResNo ? Result.getValue(1) : ValRes; 1277 } 1278 assert(0 && "Unreachable"); 1279 } 1280 case ISD::EXTRACT_ELEMENT: { 1281 MVT::ValueType OpTy = Node->getOperand(0).getValueType(); 1282 switch (getTypeAction(OpTy)) { 1283 default: 1284 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); 1285 break; 1286 case Legal: 1287 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { 1288 // 1 -> Hi 1289 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), 1290 DAG.getConstant(MVT::getSizeInBits(OpTy)/2, 1291 TLI.getShiftAmountTy())); 1292 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); 1293 } else { 1294 // 0 -> Lo 1295 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), 1296 Node->getOperand(0)); 1297 } 1298 break; 1299 case Expand: 1300 // Get both the low and high parts. 1301 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 1302 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) 1303 Result = Tmp2; // 1 -> Hi 1304 else 1305 Result = Tmp1; // 0 -> Lo 1306 break; 1307 } 1308 break; 1309 } 1310 1311 case ISD::CopyToReg: 1312 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1313 1314 assert(isTypeLegal(Node->getOperand(2).getValueType()) && 1315 "Register type must be legal!"); 1316 // Legalize the incoming value (must be a legal type). 1317 Tmp2 = LegalizeOp(Node->getOperand(2)); 1318 if (Node->getNumValues() == 1) { 1319 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2)) 1320 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1, 1321 Node->getOperand(1), Tmp2); 1322 } else { 1323 assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); 1324 if (Node->getNumOperands() == 4) 1325 Tmp3 = LegalizeOp(Node->getOperand(3)); 1326 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) || 1327 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) { 1328 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 1329 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3); 1330 } 1331 1332 // Since this produces two values, make sure to remember that we legalized 1333 // both of them. 1334 AddLegalizedOperand(SDOperand(Node, 0), Result); 1335 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1336 return Result.getValue(Op.ResNo); 1337 } 1338 break; 1339 1340 case ISD::RET: 1341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1342 switch (Node->getNumOperands()) { 1343 case 2: // ret val 1344 switch (getTypeAction(Node->getOperand(1).getValueType())) { 1345 case Legal: 1346 Tmp2 = LegalizeOp(Node->getOperand(1)); 1347 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 1348 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); 1349 break; 1350 case Expand: { 1351 SDOperand Lo, Hi; 1352 ExpandOp(Node->getOperand(1), Lo, Hi); 1353 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); 1354 break; 1355 } 1356 case Promote: 1357 Tmp2 = PromoteOp(Node->getOperand(1)); 1358 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2); 1359 break; 1360 } 1361 break; 1362 case 1: // ret void 1363 if (Tmp1 != Node->getOperand(0)) 1364 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1); 1365 break; 1366 default: { // ret <values> 1367 std::vector<SDOperand> NewValues; 1368 NewValues.push_back(Tmp1); 1369 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) 1370 switch (getTypeAction(Node->getOperand(i).getValueType())) { 1371 case Legal: 1372 NewValues.push_back(LegalizeOp(Node->getOperand(i))); 1373 break; 1374 case Expand: { 1375 SDOperand Lo, Hi; 1376 ExpandOp(Node->getOperand(i), Lo, Hi); 1377 NewValues.push_back(Lo); 1378 NewValues.push_back(Hi); 1379 break; 1380 } 1381 case Promote: 1382 assert(0 && "Can't promote multiple return value yet!"); 1383 } 1384 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); 1385 break; 1386 } 1387 } 1388 1389 switch (TLI.getOperationAction(Node->getOpcode(), 1390 Node->getValueType(0))) { 1391 default: assert(0 && "This action is not supported yet!"); 1392 case TargetLowering::Legal: break; 1393 case TargetLowering::Custom: 1394 Tmp1 = TLI.LowerOperation(Result, DAG); 1395 if (Tmp1.Val) Result = Tmp1; 1396 break; 1397 } 1398 break; 1399 case ISD::STORE: { 1400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1401 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 1402 1403 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 1404 // FIXME: We shouldn't do this for TargetConstantFP's. 1405 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ 1406 if (CFP->getValueType(0) == MVT::f32) { 1407 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 1408 DAG.getConstant(FloatToBits(CFP->getValue()), 1409 MVT::i32), 1410 Tmp2, Node->getOperand(3)); 1411 } else { 1412 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); 1413 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, 1414 DAG.getConstant(DoubleToBits(CFP->getValue()), 1415 MVT::i64), 1416 Tmp2, Node->getOperand(3)); 1417 } 1418 break; 1419 } 1420 1421 switch (getTypeAction(Node->getOperand(1).getValueType())) { 1422 case Legal: { 1423 SDOperand Val = LegalizeOp(Node->getOperand(1)); 1424 if (Tmp1 != Node->getOperand(0) || Val != Node->getOperand(1) || 1425 Tmp2 != Node->getOperand(2)) 1426 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2, 1427 Node->getOperand(3)); 1428 1429 MVT::ValueType VT = Result.Val->getOperand(1).getValueType(); 1430 switch (TLI.getOperationAction(ISD::STORE, VT)) { 1431 default: assert(0 && "This action is not supported yet!"); 1432 case TargetLowering::Legal: break; 1433 case TargetLowering::Custom: 1434 Tmp1 = TLI.LowerOperation(Result, DAG); 1435 if (Tmp1.Val) Result = Tmp1; 1436 break; 1437 } 1438 break; 1439 } 1440 case Promote: 1441 // Truncate the value and store the result. 1442 Tmp3 = PromoteOp(Node->getOperand(1)); 1443 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, 1444 Node->getOperand(3), 1445 DAG.getValueType(Node->getOperand(1).getValueType())); 1446 break; 1447 1448 case Expand: 1449 SDOperand Lo, Hi; 1450 ExpandOp(Node->getOperand(1), Lo, Hi); 1451 1452 if (!TLI.isLittleEndian()) 1453 std::swap(Lo, Hi); 1454 1455 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2, 1456 Node->getOperand(3)); 1457 // If this is a vector type, then we have to calculate the increment as 1458 // the product of the element size in bytes, and the number of elements 1459 // in the high half of the vector. 1460 unsigned IncrementSize; 1461 if (MVT::Vector == Hi.getValueType()) { 1462 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue(); 1463 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT(); 1464 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8; 1465 } else { 1466 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; 1467 } 1468 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, 1469 getIntPtrConstant(IncrementSize)); 1470 assert(isTypeLegal(Tmp2.getValueType()) && 1471 "Pointers must be legal!"); 1472 // FIXME: This sets the srcvalue of both halves to be the same, which is 1473 // wrong. 1474 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, 1475 Node->getOperand(3)); 1476 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 1477 break; 1478 } 1479 break; 1480 } 1481 case ISD::PCMARKER: 1482 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1483 if (Tmp1 != Node->getOperand(0)) 1484 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1)); 1485 break; 1486 case ISD::STACKSAVE: 1487 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1488 if (Tmp1 != Node->getOperand(0)) { 1489 std::vector<MVT::ValueType> VTs; 1490 VTs.push_back(Node->getValueType(0)); 1491 VTs.push_back(MVT::Other); 1492 std::vector<SDOperand> Ops; 1493 Ops.push_back(Tmp1); 1494 Result = DAG.getNode(ISD::STACKSAVE, VTs, Ops); 1495 } 1496 1497 Tmp1 = Result.getValue(1); 1498 1499 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { 1500 default: assert(0 && "This action is not supported yet!"); 1501 case TargetLowering::Legal: break; 1502 case TargetLowering::Custom: 1503 Tmp2 = TLI.LowerOperation(Result, DAG); 1504 if (Tmp2.Val) { 1505 Result = LegalizeOp(Tmp2); 1506 Tmp1 = LegalizeOp(Tmp2.getValue(1)); 1507 } 1508 break; 1509 case TargetLowering::Expand: 1510 // Expand to CopyFromReg if the target set 1511 // StackPointerRegisterToSaveRestore. 1512 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 1513 Tmp2 = DAG.getCopyFromReg(Result.getOperand(0), SP, 1514 Node->getValueType(0)); 1515 Result = Tmp2; 1516 Tmp1 = Tmp2.getValue(1); 1517 } else { 1518 Result = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); 1519 Tmp1 = Node->getOperand(0); 1520 } 1521 break; 1522 } 1523 1524 // Since stacksave produce two values, make sure to remember that we 1525 // legalized both of them. 1526 AddLegalizedOperand(SDOperand(Node, 0), Result); 1527 AddLegalizedOperand(SDOperand(Node, 1), Tmp1); 1528 return Op.ResNo ? Tmp1 : Result; 1529 1530 case ISD::STACKRESTORE: 1531 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1532 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 1533 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 1534 Result = DAG.getNode(ISD::STACKRESTORE, MVT::Other, Tmp1, Tmp2); 1535 1536 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { 1537 default: assert(0 && "This action is not supported yet!"); 1538 case TargetLowering::Legal: break; 1539 case TargetLowering::Custom: 1540 Tmp1 = TLI.LowerOperation(Result, DAG); 1541 if (Tmp1.Val) Result = Tmp1; 1542 break; 1543 case TargetLowering::Expand: 1544 // Expand to CopyToReg if the target set 1545 // StackPointerRegisterToSaveRestore. 1546 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 1547 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); 1548 } else { 1549 Result = Tmp1; 1550 } 1551 break; 1552 } 1553 break; 1554 1555 case ISD::READCYCLECOUNTER: 1556 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain 1557 if (Tmp1 != Node->getOperand(0)) { 1558 std::vector<MVT::ValueType> rtypes; 1559 std::vector<SDOperand> rvals; 1560 rtypes.push_back(MVT::i64); 1561 rtypes.push_back(MVT::Other); 1562 rvals.push_back(Tmp1); 1563 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals); 1564 } 1565 1566 // Since rdcc produce two values, make sure to remember that we legalized 1567 // both of them. 1568 AddLegalizedOperand(SDOperand(Node, 0), Result); 1569 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1570 return Result.getValue(Op.ResNo); 1571 1572 case ISD::TRUNCSTORE: { 1573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 1574 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. 1575 1576 assert(isTypeLegal(Node->getOperand(1).getValueType()) && 1577 "Cannot handle illegal TRUNCSTORE yet!"); 1578 Tmp2 = LegalizeOp(Node->getOperand(1)); 1579 1580 // The only promote case we handle is TRUNCSTORE:i1 X into 1581 // -> TRUNCSTORE:i8 (and X, 1) 1582 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 && 1583 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) == 1584 TargetLowering::Promote) { 1585 // Promote the bool to a mask then store. 1586 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2, 1587 DAG.getConstant(1, Tmp2.getValueType())); 1588 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, 1589 Node->getOperand(3), DAG.getValueType(MVT::i8)); 1590 1591 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 1592 Tmp3 != Node->getOperand(2)) { 1593 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, 1594 Node->getOperand(3), Node->getOperand(4)); 1595 } 1596 1597 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT(); 1598 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) { 1599 default: assert(0 && "This action is not supported yet!"); 1600 case TargetLowering::Legal: break; 1601 case TargetLowering::Custom: 1602 Tmp1 = TLI.LowerOperation(Result, DAG); 1603 if (Tmp1.Val) Result = Tmp1; 1604 break; 1605 } 1606 break; 1607 } 1608 case ISD::SELECT: 1609 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1610 case Expand: assert(0 && "It's impossible to expand bools"); 1611 case Legal: 1612 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. 1613 break; 1614 case Promote: 1615 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. 1616 break; 1617 } 1618 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal 1619 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal 1620 1621 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 1622 Tmp3 != Node->getOperand(2)) 1623 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), 1624 Tmp1, Tmp2, Tmp3); 1625 1626 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { 1627 default: assert(0 && "This action is not supported yet!"); 1628 case TargetLowering::Legal: break; 1629 case TargetLowering::Custom: { 1630 Tmp1 = TLI.LowerOperation(Result, DAG); 1631 if (Tmp1.Val) Result = Tmp1; 1632 break; 1633 } 1634 case TargetLowering::Expand: 1635 if (Tmp1.getOpcode() == ISD::SETCC) { 1636 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), 1637 Tmp2, Tmp3, 1638 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 1639 } else { 1640 // Make sure the condition is either zero or one. It may have been 1641 // promoted from something else. 1642 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); 1643 Result = DAG.getSelectCC(Tmp1, 1644 DAG.getConstant(0, Tmp1.getValueType()), 1645 Tmp2, Tmp3, ISD::SETNE); 1646 } 1647 break; 1648 case TargetLowering::Promote: { 1649 MVT::ValueType NVT = 1650 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); 1651 unsigned ExtOp, TruncOp; 1652 if (MVT::isInteger(Tmp2.getValueType())) { 1653 ExtOp = ISD::ANY_EXTEND; 1654 TruncOp = ISD::TRUNCATE; 1655 } else { 1656 ExtOp = ISD::FP_EXTEND; 1657 TruncOp = ISD::FP_ROUND; 1658 } 1659 // Promote each of the values to the new type. 1660 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); 1661 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); 1662 // Perform the larger operation, then round down. 1663 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); 1664 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); 1665 break; 1666 } 1667 } 1668 break; 1669 case ISD::SELECT_CC: 1670 Tmp3 = LegalizeOp(Node->getOperand(2)); // True 1671 Tmp4 = LegalizeOp(Node->getOperand(3)); // False 1672 1673 if (isTypeLegal(Node->getOperand(0).getValueType())) { 1674 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 1675 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 1676 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 1677 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) { 1678 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2, 1679 Tmp3, Tmp4, Node->getOperand(4)); 1680 } 1681 1682 // Everything is legal, see if we should expand this op or something. 1683 switch (TLI.getOperationAction(ISD::SELECT_CC, 1684 Node->getOperand(0).getValueType())) { 1685 default: assert(0 && "This action is not supported yet!"); 1686 case TargetLowering::Legal: break; 1687 case TargetLowering::Custom: 1688 Tmp1 = TLI.LowerOperation(Result, DAG); 1689 if (Tmp1.Val) Result = Tmp1; 1690 break; 1691 } 1692 break; 1693 } else { 1694 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), 1695 Node->getOperand(0), // LHS 1696 Node->getOperand(1), // RHS 1697 Node->getOperand(4))); 1698 // If we get a SETCC back from legalizing the SETCC node we just 1699 // created, then use its LHS, RHS, and CC directly in creating a new 1700 // node. Otherwise, select between the true and false value based on 1701 // comparing the result of the legalized with zero. 1702 if (Tmp1.getOpcode() == ISD::SETCC) { 1703 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(), 1704 Tmp1.getOperand(0), Tmp1.getOperand(1), 1705 Tmp3, Tmp4, Tmp1.getOperand(2)); 1706 } else { 1707 Result = DAG.getSelectCC(Tmp1, 1708 DAG.getConstant(0, Tmp1.getValueType()), 1709 Tmp3, Tmp4, ISD::SETNE); 1710 } 1711 } 1712 break; 1713 case ISD::SETCC: 1714 switch (getTypeAction(Node->getOperand(0).getValueType())) { 1715 case Legal: 1716 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 1717 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 1718 break; 1719 case Promote: 1720 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS 1721 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS 1722 1723 // If this is an FP compare, the operands have already been extended. 1724 if (MVT::isInteger(Node->getOperand(0).getValueType())) { 1725 MVT::ValueType VT = Node->getOperand(0).getValueType(); 1726 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 1727 1728 // Otherwise, we have to insert explicit sign or zero extends. Note 1729 // that we could insert sign extends for ALL conditions, but zero extend 1730 // is cheaper on many machines (an AND instead of two shifts), so prefer 1731 // it. 1732 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) { 1733 default: assert(0 && "Unknown integer comparison!"); 1734 case ISD::SETEQ: 1735 case ISD::SETNE: 1736 case ISD::SETUGE: 1737 case ISD::SETUGT: 1738 case ISD::SETULE: 1739 case ISD::SETULT: 1740 // ALL of these operations will work if we either sign or zero extend 1741 // the operands (including the unsigned comparisons!). Zero extend is 1742 // usually a simpler/cheaper operation, so prefer it. 1743 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); 1744 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); 1745 break; 1746 case ISD::SETGE: 1747 case ISD::SETGT: 1748 case ISD::SETLT: 1749 case ISD::SETLE: 1750 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, 1751 DAG.getValueType(VT)); 1752 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, 1753 DAG.getValueType(VT)); 1754 break; 1755 } 1756 } 1757 break; 1758 case Expand: 1759 SDOperand LHSLo, LHSHi, RHSLo, RHSHi; 1760 ExpandOp(Node->getOperand(0), LHSLo, LHSHi); 1761 ExpandOp(Node->getOperand(1), RHSLo, RHSHi); 1762 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) { 1763 case ISD::SETEQ: 1764 case ISD::SETNE: 1765 if (RHSLo == RHSHi) 1766 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) 1767 if (RHSCST->isAllOnesValue()) { 1768 // Comparison to -1. 1769 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); 1770 Tmp2 = RHSLo; 1771 break; 1772 } 1773 1774 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); 1775 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); 1776 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); 1777 Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); 1778 break; 1779 default: 1780 // If this is a comparison of the sign bit, just look at the top part. 1781 // X > -1, x < 0 1782 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1))) 1783 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT && 1784 CST->getValue() == 0) || // X < 0 1785 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT && 1786 (CST->isAllOnesValue()))) { // X > -1 1787 Tmp1 = LHSHi; 1788 Tmp2 = RHSHi; 1789 break; 1790 } 1791 1792 // FIXME: This generated code sucks. 1793 ISD::CondCode LowCC; 1794 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) { 1795 default: assert(0 && "Unknown integer setcc!"); 1796 case ISD::SETLT: 1797 case ISD::SETULT: LowCC = ISD::SETULT; break; 1798 case ISD::SETGT: 1799 case ISD::SETUGT: LowCC = ISD::SETUGT; break; 1800 case ISD::SETLE: 1801 case ISD::SETULE: LowCC = ISD::SETULE; break; 1802 case ISD::SETGE: 1803 case ISD::SETUGE: LowCC = ISD::SETUGE; break; 1804 } 1805 1806 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison 1807 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands 1808 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; 1809 1810 // NOTE: on targets without efficient SELECT of bools, we can always use 1811 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) 1812 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC); 1813 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi, 1814 Node->getOperand(2)); 1815 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ); 1816 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), 1817 Result, Tmp1, Tmp2)); 1818 AddLegalizedOperand(SDOperand(Node, 0), Result); 1819 return Result; 1820 } 1821 } 1822 1823 switch (TLI.getOperationAction(ISD::SETCC, 1824 Node->getOperand(0).getValueType())) { 1825 default: assert(0 && "Cannot handle this action for SETCC yet!"); 1826 case TargetLowering::Custom: 1827 isCustom = true; 1828 // FALLTHROUGH. 1829 case TargetLowering::Legal: 1830 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 1831 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2, 1832 Node->getOperand(2)); 1833 if (isCustom) { 1834 Tmp3 = TLI.LowerOperation(Result, DAG); 1835 if (Tmp3.Val) Result = Tmp3; 1836 } 1837 break; 1838 case TargetLowering::Promote: { 1839 // First step, figure out the appropriate operation to use. 1840 // Allow SETCC to not be supported for all legal data types 1841 // Mostly this targets FP 1842 MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); 1843 MVT::ValueType OldVT = NewInTy; 1844 1845 // Scan for the appropriate larger type to use. 1846 while (1) { 1847 NewInTy = (MVT::ValueType)(NewInTy+1); 1848 1849 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && 1850 "Fell off of the edge of the integer world"); 1851 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && 1852 "Fell off of the edge of the floating point world"); 1853 1854 // If the target supports SETCC of this type, use it. 1855 if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) 1856 break; 1857 } 1858 if (MVT::isInteger(NewInTy)) 1859 assert(0 && "Cannot promote Legal Integer SETCC yet"); 1860 else { 1861 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); 1862 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); 1863 } 1864 1865 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2, 1866 Node->getOperand(2)); 1867 Result = LegalizeOp(Result); 1868 break; 1869 } 1870 case TargetLowering::Expand: 1871 // Expand a setcc node into a select_cc of the same condition, lhs, and 1872 // rhs that selects between const 1 (true) and const 0 (false). 1873 MVT::ValueType VT = Node->getValueType(0); 1874 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, 1875 DAG.getConstant(1, VT), DAG.getConstant(0, VT), 1876 Node->getOperand(2)); 1877 Result = LegalizeOp(Result); 1878 break; 1879 } 1880 break; 1881 case ISD::MEMSET: 1882 case ISD::MEMCPY: 1883 case ISD::MEMMOVE: { 1884 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain 1885 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer 1886 1887 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte 1888 switch (getTypeAction(Node->getOperand(2).getValueType())) { 1889 case Expand: assert(0 && "Cannot expand a byte!"); 1890 case Legal: 1891 Tmp3 = LegalizeOp(Node->getOperand(2)); 1892 break; 1893 case Promote: 1894 Tmp3 = PromoteOp(Node->getOperand(2)); 1895 break; 1896 } 1897 } else { 1898 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, 1899 } 1900 1901 SDOperand Tmp4; 1902 switch (getTypeAction(Node->getOperand(3).getValueType())) { 1903 case Expand: { 1904 // Length is too big, just take the lo-part of the length. 1905 SDOperand HiPart; 1906 ExpandOp(Node->getOperand(3), HiPart, Tmp4); 1907 break; 1908 } 1909 case Legal: 1910 Tmp4 = LegalizeOp(Node->getOperand(3)); 1911 break; 1912 case Promote: 1913 Tmp4 = PromoteOp(Node->getOperand(3)); 1914 break; 1915 } 1916 1917 SDOperand Tmp5; 1918 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint 1919 case Expand: assert(0 && "Cannot expand this yet!"); 1920 case Legal: 1921 Tmp5 = LegalizeOp(Node->getOperand(4)); 1922 break; 1923 case Promote: 1924 Tmp5 = PromoteOp(Node->getOperand(4)); 1925 break; 1926 } 1927 1928 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { 1929 default: assert(0 && "This action not implemented for this operation!"); 1930 case TargetLowering::Custom: 1931 isCustom = true; 1932 // FALLTHROUGH 1933 case TargetLowering::Legal: 1934 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 1935 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) || 1936 Tmp5 != Node->getOperand(4)) { 1937 std::vector<SDOperand> Ops; 1938 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3); 1939 Ops.push_back(Tmp4); Ops.push_back(Tmp5); 1940 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops); 1941 } 1942 if (isCustom) { 1943 Tmp1 = TLI.LowerOperation(Result, DAG); 1944 if (Tmp1.Val) Result = Tmp1; 1945 } 1946 break; 1947 case TargetLowering::Expand: { 1948 // Otherwise, the target does not support this operation. Lower the 1949 // operation to an explicit libcall as appropriate. 1950 MVT::ValueType IntPtr = TLI.getPointerTy(); 1951 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); 1952 std::vector<std::pair<SDOperand, const Type*> > Args; 1953 1954 const char *FnName = 0; 1955 if (Node->getOpcode() == ISD::MEMSET) { 1956 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 1957 // Extend the ubyte argument to be an int value for the call. 1958 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); 1959 Args.push_back(std::make_pair(Tmp3, Type::IntTy)); 1960 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 1961 1962 FnName = "memset"; 1963 } else if (Node->getOpcode() == ISD::MEMCPY || 1964 Node->getOpcode() == ISD::MEMMOVE) { 1965 Args.push_back(std::make_pair(Tmp2, IntPtrTy)); 1966 Args.push_back(std::make_pair(Tmp3, IntPtrTy)); 1967 Args.push_back(std::make_pair(Tmp4, IntPtrTy)); 1968 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; 1969 } else { 1970 assert(0 && "Unknown op!"); 1971 } 1972 1973 std::pair<SDOperand,SDOperand> CallResult = 1974 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false, 1975 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); 1976 Result = CallResult.second; 1977 break; 1978 } 1979 } 1980 break; 1981 } 1982 1983 case ISD::READPORT: 1984 Tmp1 = LegalizeOp(Node->getOperand(0)); 1985 Tmp2 = LegalizeOp(Node->getOperand(1)); 1986 1987 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { 1988 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); 1989 std::vector<SDOperand> Ops; 1990 Ops.push_back(Tmp1); 1991 Ops.push_back(Tmp2); 1992 Result = DAG.getNode(ISD::READPORT, VTs, Ops); 1993 } else 1994 Result = SDOperand(Node, 0); 1995 // Since these produce two values, make sure to remember that we legalized 1996 // both of them. 1997 AddLegalizedOperand(SDOperand(Node, 0), Result); 1998 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 1999 return Result.getValue(Op.ResNo); 2000 case ISD::WRITEPORT: 2001 Tmp1 = LegalizeOp(Node->getOperand(0)); 2002 Tmp2 = LegalizeOp(Node->getOperand(1)); 2003 Tmp3 = LegalizeOp(Node->getOperand(2)); 2004 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 2005 Tmp3 != Node->getOperand(2)) 2006 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3); 2007 break; 2008 2009 case ISD::READIO: 2010 Tmp1 = LegalizeOp(Node->getOperand(0)); 2011 Tmp2 = LegalizeOp(Node->getOperand(1)); 2012 2013 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2014 case TargetLowering::Custom: 2015 default: assert(0 && "This action not implemented for this operation!"); 2016 case TargetLowering::Legal: 2017 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) { 2018 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); 2019 std::vector<SDOperand> Ops; 2020 Ops.push_back(Tmp1); 2021 Ops.push_back(Tmp2); 2022 Result = DAG.getNode(ISD::READPORT, VTs, Ops); 2023 } else 2024 Result = SDOperand(Node, 0); 2025 break; 2026 case TargetLowering::Expand: 2027 // Replace this with a load from memory. 2028 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0), 2029 Node->getOperand(1), DAG.getSrcValue(NULL)); 2030 Result = LegalizeOp(Result); 2031 break; 2032 } 2033 2034 // Since these produce two values, make sure to remember that we legalized 2035 // both of them. 2036 AddLegalizedOperand(SDOperand(Node, 0), Result); 2037 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); 2038 return Result.getValue(Op.ResNo); 2039 2040 case ISD::WRITEIO: 2041 Tmp1 = LegalizeOp(Node->getOperand(0)); 2042 Tmp2 = LegalizeOp(Node->getOperand(1)); 2043 Tmp3 = LegalizeOp(Node->getOperand(2)); 2044 2045 switch (TLI.getOperationAction(Node->getOpcode(), 2046 Node->getOperand(1).getValueType())) { 2047 case TargetLowering::Custom: 2048 default: assert(0 && "This action not implemented for this operation!"); 2049 case TargetLowering::Legal: 2050 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 2051 Tmp3 != Node->getOperand(2)) 2052 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3); 2053 break; 2054 case TargetLowering::Expand: 2055 // Replace this with a store to memory. 2056 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0), 2057 Node->getOperand(1), Node->getOperand(2), 2058 DAG.getSrcValue(NULL)); 2059 break; 2060 } 2061 break; 2062 2063 case ISD::ADD_PARTS: 2064 case ISD::SUB_PARTS: 2065 case ISD::SHL_PARTS: 2066 case ISD::SRA_PARTS: 2067 case ISD::SRL_PARTS: { 2068 std::vector<SDOperand> Ops; 2069 bool Changed = false; 2070 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 2071 Ops.push_back(LegalizeOp(Node->getOperand(i))); 2072 Changed |= Ops.back() != Node->getOperand(i); 2073 } 2074 if (Changed) { 2075 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end()); 2076 Result = DAG.getNode(Node->getOpcode(), VTs, Ops); 2077 } 2078 2079 switch (TLI.getOperationAction(Node->getOpcode(), 2080 Node->getValueType(0))) { 2081 default: assert(0 && "This action is not supported yet!"); 2082 case TargetLowering::Legal: break; 2083 case TargetLowering::Custom: 2084 Tmp1 = TLI.LowerOperation(Result, DAG); 2085 if (Tmp1.Val) { 2086 SDOperand Tmp2, RetVal(0, 0); 2087 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { 2088 Tmp2 = LegalizeOp(Tmp1.getValue(i)); 2089 AddLegalizedOperand(SDOperand(Node, i), Tmp2); 2090 if (i == Op.ResNo) 2091 RetVal = Tmp2; 2092 } 2093 assert(RetVal.Val && "Illegal result number"); 2094 return RetVal; 2095 } 2096 break; 2097 } 2098 2099 // Since these produce multiple values, make sure to remember that we 2100 // legalized all of them. 2101 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 2102 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); 2103 return Result.getValue(Op.ResNo); 2104 } 2105 2106 // Binary operators 2107 case ISD::ADD: 2108 case ISD::SUB: 2109 case ISD::MUL: 2110 case ISD::MULHS: 2111 case ISD::MULHU: 2112 case ISD::UDIV: 2113 case ISD::SDIV: 2114 case ISD::AND: 2115 case ISD::OR: 2116 case ISD::XOR: 2117 case ISD::SHL: 2118 case ISD::SRL: 2119 case ISD::SRA: 2120 case ISD::FADD: 2121 case ISD::FSUB: 2122 case ISD::FMUL: 2123 case ISD::FDIV: 2124 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 2125 switch (getTypeAction(Node->getOperand(1).getValueType())) { 2126 case Expand: assert(0 && "Not possible"); 2127 case Legal: 2128 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. 2129 break; 2130 case Promote: 2131 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. 2132 break; 2133 } 2134 2135 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 2136 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); 2137 2138 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2139 default: assert(0 && "Operation not supported"); 2140 case TargetLowering::Legal: break; 2141 case TargetLowering::Custom: 2142 Tmp1 = TLI.LowerOperation(Result, DAG); 2143 if (Tmp1.Val) Result = Tmp1; 2144 break; 2145 } 2146 break; 2147 2148 case ISD::BUILD_PAIR: { 2149 MVT::ValueType PairTy = Node->getValueType(0); 2150 // TODO: handle the case where the Lo and Hi operands are not of legal type 2151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo 2152 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi 2153 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { 2154 case TargetLowering::Promote: 2155 case TargetLowering::Custom: 2156 assert(0 && "Cannot promote/custom this yet!"); 2157 case TargetLowering::Legal: 2158 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 2159 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); 2160 break; 2161 case TargetLowering::Expand: 2162 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); 2163 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); 2164 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, 2165 DAG.getConstant(MVT::getSizeInBits(PairTy)/2, 2166 TLI.getShiftAmountTy())); 2167 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); 2168 break; 2169 } 2170 break; 2171 } 2172 2173 case ISD::UREM: 2174 case ISD::SREM: 2175 case ISD::FREM: 2176 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 2177 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 2178 2179 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2180 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); 2181 case TargetLowering::Custom: 2182 isCustom = true; 2183 // FALLTHROUGH 2184 case TargetLowering::Legal: 2185 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 2186 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), 2187 Tmp1, Tmp2); 2188 if (isCustom) { 2189 Tmp1 = TLI.LowerOperation(Result, DAG); 2190 if (Tmp1.Val) Result = Tmp1; 2191 } 2192 break; 2193 case TargetLowering::Expand: 2194 if (MVT::isInteger(Node->getValueType(0))) { 2195 // X % Y -> X-X/Y*Y 2196 MVT::ValueType VT = Node->getValueType(0); 2197 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV; 2198 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); 2199 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); 2200 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); 2201 } else { 2202 // Floating point mod -> fmod libcall. 2203 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod"; 2204 SDOperand Dummy; 2205 Result = ExpandLibCall(FnName, Node, Dummy); 2206 } 2207 break; 2208 } 2209 break; 2210 case ISD::VAARG: { 2211 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 2212 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 2213 2214 MVT::ValueType VT = Node->getValueType(0); 2215 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { 2216 default: assert(0 && "This action is not supported yet!"); 2217 case TargetLowering::Custom: 2218 isCustom = true; 2219 // FALLTHROUGH 2220 case TargetLowering::Legal: 2221 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 2222 Result = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); 2223 else 2224 Result = SDOperand(Node, 0); 2225 Tmp1 = Result.getValue(1); 2226 2227 if (isCustom) { 2228 Tmp2 = TLI.LowerOperation(Result, DAG); 2229 if (Tmp2.Val) { 2230 Result = LegalizeOp(Tmp2); 2231 Tmp1 = LegalizeOp(Tmp2.getValue(1)); 2232 } 2233 } 2234 break; 2235 case TargetLowering::Expand: { 2236 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, 2237 Node->getOperand(2)); 2238 // Increment the pointer, VAList, to the next vaarg 2239 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, 2240 DAG.getConstant(MVT::getSizeInBits(VT)/8, 2241 TLI.getPointerTy())); 2242 // Store the incremented VAList to the legalized pointer 2243 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, 2244 Node->getOperand(2)); 2245 // Load the actual argument out of the pointer VAList 2246 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0)); 2247 Tmp1 = LegalizeOp(Result.getValue(1)); 2248 Result = LegalizeOp(Result); 2249 break; 2250 } 2251 } 2252 // Since VAARG produces two values, make sure to remember that we 2253 // legalized both of them. 2254 AddLegalizedOperand(SDOperand(Node, 0), Result); 2255 AddLegalizedOperand(SDOperand(Node, 1), Tmp1); 2256 return Op.ResNo ? Tmp1 : Result; 2257 } 2258 2259 case ISD::VACOPY: 2260 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 2261 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. 2262 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. 2263 2264 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { 2265 default: assert(0 && "This action is not supported yet!"); 2266 case TargetLowering::Custom: 2267 isCustom = true; 2268 // FALLTHROUGH 2269 case TargetLowering::Legal: 2270 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || 2271 Tmp3 != Node->getOperand(2)) 2272 Result = DAG.getNode(ISD::VACOPY, MVT::Other, Tmp1, Tmp2, Tmp3, 2273 Node->getOperand(3), Node->getOperand(4)); 2274 if (isCustom) { 2275 Tmp1 = TLI.LowerOperation(Result, DAG); 2276 if (Tmp1.Val) Result = Tmp1; 2277 } 2278 break; 2279 case TargetLowering::Expand: 2280 // This defaults to loading a pointer from the input and storing it to the 2281 // output, returning the chain. 2282 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3)); 2283 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2, 2284 Node->getOperand(4)); 2285 break; 2286 } 2287 break; 2288 2289 case ISD::VAEND: 2290 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 2291 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 2292 2293 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { 2294 default: assert(0 && "This action is not supported yet!"); 2295 case TargetLowering::Custom: 2296 isCustom = true; 2297 // FALLTHROUGH 2298 case TargetLowering::Legal: 2299 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 2300 Result = DAG.getNode(ISD::VAEND, MVT::Other, Tmp1, Tmp2, 2301 Node->getOperand(2)); 2302 if (isCustom) { 2303 Tmp1 = TLI.LowerOperation(Tmp1, DAG); 2304 if (Tmp1.Val) Result = Tmp1; 2305 } 2306 break; 2307 case TargetLowering::Expand: 2308 Result = Tmp1; // Default to a no-op, return the chain 2309 break; 2310 } 2311 break; 2312 2313 case ISD::VASTART: 2314 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. 2315 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. 2316 2317 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 2318 Result = DAG.getNode(ISD::VASTART, MVT::Other, Tmp1, Tmp2, 2319 Node->getOperand(2)); 2320 2321 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { 2322 default: assert(0 && "This action is not supported yet!"); 2323 case TargetLowering::Legal: break; 2324 case TargetLowering::Custom: 2325 Tmp1 = TLI.LowerOperation(Result, DAG); 2326 if (Tmp1.Val) Result = Tmp1; 2327 break; 2328 } 2329 break; 2330 2331 case ISD::ROTL: 2332 case ISD::ROTR: 2333 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS 2334 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS 2335 2336 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && 2337 "Cannot handle this yet!"); 2338 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) 2339 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2); 2340 break; 2341 2342 case ISD::BSWAP: 2343 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op 2344 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2345 case TargetLowering::Custom: 2346 assert(0 && "Cannot custom legalize this yet!"); 2347 case TargetLowering::Legal: 2348 if (Tmp1 != Node->getOperand(0)) 2349 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2350 break; 2351 case TargetLowering::Promote: { 2352 MVT::ValueType OVT = Tmp1.getValueType(); 2353 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 2354 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT); 2355 2356 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); 2357 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); 2358 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, 2359 DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); 2360 break; 2361 } 2362 case TargetLowering::Expand: 2363 Result = ExpandBSWAP(Tmp1); 2364 break; 2365 } 2366 break; 2367 2368 case ISD::CTPOP: 2369 case ISD::CTTZ: 2370 case ISD::CTLZ: 2371 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op 2372 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2373 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); 2374 case TargetLowering::Legal: 2375 if (Tmp1 != Node->getOperand(0)) 2376 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2377 break; 2378 case TargetLowering::Promote: { 2379 MVT::ValueType OVT = Tmp1.getValueType(); 2380 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 2381 2382 // Zero extend the argument. 2383 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); 2384 // Perform the larger operation, then subtract if needed. 2385 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2386 switch (Node->getOpcode()) { 2387 case ISD::CTPOP: 2388 Result = Tmp1; 2389 break; 2390 case ISD::CTTZ: 2391 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) 2392 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, 2393 DAG.getConstant(getSizeInBits(NVT), NVT), 2394 ISD::SETEQ); 2395 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, 2396 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); 2397 break; 2398 case ISD::CTLZ: 2399 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 2400 Result = DAG.getNode(ISD::SUB, NVT, Tmp1, 2401 DAG.getConstant(getSizeInBits(NVT) - 2402 getSizeInBits(OVT), NVT)); 2403 break; 2404 } 2405 break; 2406 } 2407 case TargetLowering::Expand: 2408 Result = ExpandBitCount(Node->getOpcode(), Tmp1); 2409 break; 2410 } 2411 break; 2412 2413 // Unary operators 2414 case ISD::FABS: 2415 case ISD::FNEG: 2416 case ISD::FSQRT: 2417 case ISD::FSIN: 2418 case ISD::FCOS: 2419 Tmp1 = LegalizeOp(Node->getOperand(0)); 2420 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { 2421 case TargetLowering::Promote: 2422 case TargetLowering::Custom: 2423 assert(0 && "Cannot promote/custom handle this yet!"); 2424 case TargetLowering::Legal: 2425 if (Tmp1 != Node->getOperand(0)) 2426 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2427 break; 2428 case TargetLowering::Expand: 2429 switch (Node->getOpcode()) { 2430 default: assert(0 && "Unreachable!"); 2431 case ISD::FNEG: 2432 // Expand Y = FNEG(X) -> Y = SUB -0.0, X 2433 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); 2434 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); 2435 break; 2436 case ISD::FABS: { 2437 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). 2438 MVT::ValueType VT = Node->getValueType(0); 2439 Tmp2 = DAG.getConstantFP(0.0, VT); 2440 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); 2441 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); 2442 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); 2443 break; 2444 } 2445 case ISD::FSQRT: 2446 case ISD::FSIN: 2447 case ISD::FCOS: { 2448 MVT::ValueType VT = Node->getValueType(0); 2449 const char *FnName = 0; 2450 switch(Node->getOpcode()) { 2451 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break; 2452 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break; 2453 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break; 2454 default: assert(0 && "Unreachable!"); 2455 } 2456 SDOperand Dummy; 2457 Result = ExpandLibCall(FnName, Node, Dummy); 2458 break; 2459 } 2460 } 2461 break; 2462 } 2463 break; 2464 2465 case ISD::BIT_CONVERT: 2466 if (!isTypeLegal(Node->getOperand(0).getValueType())) { 2467 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); 2468 } else { 2469 switch (TLI.getOperationAction(ISD::BIT_CONVERT, 2470 Node->getOperand(0).getValueType())) { 2471 default: assert(0 && "Unknown operation action!"); 2472 case TargetLowering::Expand: 2473 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); 2474 break; 2475 case TargetLowering::Legal: 2476 Tmp1 = LegalizeOp(Node->getOperand(0)); 2477 if (Tmp1 != Node->getOperand(0)) 2478 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1); 2479 break; 2480 } 2481 } 2482 break; 2483 // Conversion operators. The source and destination have different types. 2484 case ISD::SINT_TO_FP: 2485 case ISD::UINT_TO_FP: { 2486 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; 2487 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2488 case Legal: 2489 switch (TLI.getOperationAction(Node->getOpcode(), 2490 Node->getOperand(0).getValueType())) { 2491 default: assert(0 && "Unknown operation action!"); 2492 case TargetLowering::Custom: 2493 isCustom = true; 2494 // FALLTHROUGH 2495 case TargetLowering::Legal: 2496 Tmp1 = LegalizeOp(Node->getOperand(0)); 2497 if (Tmp1 != Node->getOperand(0)) 2498 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2499 if (isCustom) { 2500 Tmp1 = TLI.LowerOperation(Result, DAG); 2501 if (Tmp1.Val) Result = Tmp1; 2502 } 2503 break; 2504 case TargetLowering::Expand: 2505 Result = ExpandLegalINT_TO_FP(isSigned, 2506 LegalizeOp(Node->getOperand(0)), 2507 Node->getValueType(0)); 2508 break; 2509 case TargetLowering::Promote: 2510 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), 2511 Node->getValueType(0), 2512 isSigned); 2513 break; 2514 } 2515 break; 2516 case Expand: 2517 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, 2518 Node->getValueType(0), Node->getOperand(0)); 2519 break; 2520 case Promote: 2521 if (isSigned) { 2522 Result = PromoteOp(Node->getOperand(0)); 2523 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 2524 Result, DAG.getValueType(Node->getOperand(0).getValueType())); 2525 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result); 2526 } else { 2527 Result = PromoteOp(Node->getOperand(0)); 2528 Result = DAG.getZeroExtendInReg(Result, 2529 Node->getOperand(0).getValueType()); 2530 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result); 2531 } 2532 break; 2533 } 2534 break; 2535 } 2536 case ISD::TRUNCATE: 2537 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2538 case Legal: 2539 Tmp1 = LegalizeOp(Node->getOperand(0)); 2540 if (Tmp1 != Node->getOperand(0)) 2541 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2542 break; 2543 case Expand: 2544 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 2545 2546 // Since the result is legal, we should just be able to truncate the low 2547 // part of the source. 2548 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); 2549 break; 2550 case Promote: 2551 Result = PromoteOp(Node->getOperand(0)); 2552 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); 2553 break; 2554 } 2555 break; 2556 2557 case ISD::FP_TO_SINT: 2558 case ISD::FP_TO_UINT: 2559 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2560 case Legal: 2561 Tmp1 = LegalizeOp(Node->getOperand(0)); 2562 2563 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ 2564 default: assert(0 && "Unknown operation action!"); 2565 case TargetLowering::Custom: 2566 isCustom = true; 2567 // FALLTHROUGH 2568 case TargetLowering::Legal: 2569 if (Tmp1 != Node->getOperand(0)) 2570 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2571 2572 if (isCustom) { 2573 Tmp1 = TLI.LowerOperation(Result, DAG); 2574 if (Tmp1.Val) Result = Tmp1; 2575 } 2576 break; 2577 case TargetLowering::Promote: 2578 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), 2579 Node->getOpcode() == ISD::FP_TO_SINT); 2580 break; 2581 case TargetLowering::Expand: 2582 if (Node->getOpcode() == ISD::FP_TO_UINT) { 2583 SDOperand True, False; 2584 MVT::ValueType VT = Node->getOperand(0).getValueType(); 2585 MVT::ValueType NVT = Node->getValueType(0); 2586 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; 2587 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); 2588 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), 2589 Node->getOperand(0), Tmp2, ISD::SETLT); 2590 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); 2591 False = DAG.getNode(ISD::FP_TO_SINT, NVT, 2592 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), 2593 Tmp2)); 2594 False = DAG.getNode(ISD::XOR, NVT, False, 2595 DAG.getConstant(1ULL << ShiftAmt, NVT)); 2596 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); 2597 break; 2598 } else { 2599 assert(0 && "Do not know how to expand FP_TO_SINT yet!"); 2600 } 2601 break; 2602 } 2603 break; 2604 case Expand: 2605 assert(0 && "Shouldn't need to expand other operators here!"); 2606 case Promote: 2607 Result = PromoteOp(Node->getOperand(0)); 2608 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); 2609 break; 2610 } 2611 break; 2612 2613 case ISD::ANY_EXTEND: 2614 case ISD::ZERO_EXTEND: 2615 case ISD::SIGN_EXTEND: 2616 case ISD::FP_EXTEND: 2617 case ISD::FP_ROUND: 2618 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2619 case Expand: assert(0 && "Shouldn't need to expand other operators here!"); 2620 case Legal: 2621 Tmp1 = LegalizeOp(Node->getOperand(0)); 2622 if (Tmp1 != Node->getOperand(0)) 2623 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); 2624 break; 2625 case Promote: 2626 switch (Node->getOpcode()) { 2627 case ISD::ANY_EXTEND: 2628 Result = PromoteOp(Node->getOperand(0)); 2629 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); 2630 break; 2631 case ISD::ZERO_EXTEND: 2632 Result = PromoteOp(Node->getOperand(0)); 2633 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); 2634 Result = DAG.getZeroExtendInReg(Result, 2635 Node->getOperand(0).getValueType()); 2636 break; 2637 case ISD::SIGN_EXTEND: 2638 Result = PromoteOp(Node->getOperand(0)); 2639 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); 2640 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 2641 Result, 2642 DAG.getValueType(Node->getOperand(0).getValueType())); 2643 break; 2644 case ISD::FP_EXTEND: 2645 Result = PromoteOp(Node->getOperand(0)); 2646 if (Result.getValueType() != Op.getValueType()) 2647 // Dynamically dead while we have only 2 FP types. 2648 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); 2649 break; 2650 case ISD::FP_ROUND: 2651 Result = PromoteOp(Node->getOperand(0)); 2652 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); 2653 break; 2654 } 2655 } 2656 break; 2657 case ISD::FP_ROUND_INREG: 2658 case ISD::SIGN_EXTEND_INREG: { 2659 Tmp1 = LegalizeOp(Node->getOperand(0)); 2660 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 2661 2662 // If this operation is not supported, convert it to a shl/shr or load/store 2663 // pair. 2664 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { 2665 default: assert(0 && "This action not supported for this op yet!"); 2666 case TargetLowering::Legal: 2667 if (Tmp1 != Node->getOperand(0)) 2668 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, 2669 DAG.getValueType(ExtraVT)); 2670 break; 2671 case TargetLowering::Expand: 2672 // If this is an integer extend and shifts are supported, do that. 2673 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { 2674 // NOTE: we could fall back on load/store here too for targets without 2675 // SAR. However, it is doubtful that any exist. 2676 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - 2677 MVT::getSizeInBits(ExtraVT); 2678 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); 2679 Result = DAG.getNode(ISD::SHL, Node->getValueType(0), 2680 Node->getOperand(0), ShiftCst); 2681 Result = DAG.getNode(ISD::SRA, Node->getValueType(0), 2682 Result, ShiftCst); 2683 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { 2684 // The only way we can lower this is to turn it into a STORETRUNC, 2685 // EXTLOAD pair, targetting a temporary location (a stack slot). 2686 2687 // NOTE: there is a choice here between constantly creating new stack 2688 // slots and always reusing the same one. We currently always create 2689 // new ones, as reuse may inhibit scheduling. 2690 const Type *Ty = MVT::getTypeForValueType(ExtraVT); 2691 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); 2692 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); 2693 MachineFunction &MF = DAG.getMachineFunction(); 2694 int SSFI = 2695 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); 2696 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); 2697 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), 2698 Node->getOperand(0), StackSlot, 2699 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT)); 2700 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), 2701 Result, StackSlot, DAG.getSrcValue(NULL), 2702 ExtraVT); 2703 } else { 2704 assert(0 && "Unknown op"); 2705 } 2706 break; 2707 } 2708 break; 2709 } 2710 } 2711 2712 // Make sure that the generated code is itself legal. 2713 if (Result != Op) 2714 Result = LegalizeOp(Result); 2715 2716 // Note that LegalizeOp may be reentered even from single-use nodes, which 2717 // means that we always must cache transformed nodes. 2718 AddLegalizedOperand(Op, Result); 2719 return Result; 2720} 2721 2722/// PromoteOp - Given an operation that produces a value in an invalid type, 2723/// promote it to compute the value into a larger type. The produced value will 2724/// have the correct bits for the low portion of the register, but no guarantee 2725/// is made about the top bits: it may be zero, sign-extended, or garbage. 2726SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { 2727 MVT::ValueType VT = Op.getValueType(); 2728 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 2729 assert(getTypeAction(VT) == Promote && 2730 "Caller should expand or legalize operands that are not promotable!"); 2731 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && 2732 "Cannot promote to smaller type!"); 2733 2734 SDOperand Tmp1, Tmp2, Tmp3; 2735 SDOperand Result; 2736 SDNode *Node = Op.Val; 2737 2738 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); 2739 if (I != PromotedNodes.end()) return I->second; 2740 2741 switch (Node->getOpcode()) { 2742 case ISD::CopyFromReg: 2743 assert(0 && "CopyFromReg must be legal!"); 2744 default: 2745 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 2746 assert(0 && "Do not know how to promote this operator!"); 2747 abort(); 2748 case ISD::UNDEF: 2749 Result = DAG.getNode(ISD::UNDEF, NVT); 2750 break; 2751 case ISD::Constant: 2752 if (VT != MVT::i1) 2753 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); 2754 else 2755 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); 2756 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); 2757 break; 2758 case ISD::ConstantFP: 2759 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); 2760 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); 2761 break; 2762 2763 case ISD::SETCC: 2764 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); 2765 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), 2766 Node->getOperand(1), Node->getOperand(2)); 2767 break; 2768 2769 case ISD::TRUNCATE: 2770 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2771 case Legal: 2772 Result = LegalizeOp(Node->getOperand(0)); 2773 assert(Result.getValueType() >= NVT && 2774 "This truncation doesn't make sense!"); 2775 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT 2776 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); 2777 break; 2778 case Promote: 2779 // The truncation is not required, because we don't guarantee anything 2780 // about high bits anyway. 2781 Result = PromoteOp(Node->getOperand(0)); 2782 break; 2783 case Expand: 2784 ExpandOp(Node->getOperand(0), Tmp1, Tmp2); 2785 // Truncate the low part of the expanded value to the result type 2786 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); 2787 } 2788 break; 2789 case ISD::SIGN_EXTEND: 2790 case ISD::ZERO_EXTEND: 2791 case ISD::ANY_EXTEND: 2792 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2793 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); 2794 case Legal: 2795 // Input is legal? Just do extend all the way to the larger type. 2796 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); 2797 break; 2798 case Promote: 2799 // Promote the reg if it's smaller. 2800 Result = PromoteOp(Node->getOperand(0)); 2801 // The high bits are not guaranteed to be anything. Insert an extend. 2802 if (Node->getOpcode() == ISD::SIGN_EXTEND) 2803 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, 2804 DAG.getValueType(Node->getOperand(0).getValueType())); 2805 else if (Node->getOpcode() == ISD::ZERO_EXTEND) 2806 Result = DAG.getZeroExtendInReg(Result, 2807 Node->getOperand(0).getValueType()); 2808 break; 2809 } 2810 break; 2811 case ISD::BIT_CONVERT: 2812 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); 2813 Result = PromoteOp(Result); 2814 break; 2815 2816 case ISD::FP_EXTEND: 2817 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); 2818 case ISD::FP_ROUND: 2819 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2820 case Expand: assert(0 && "BUG: Cannot expand FP regs!"); 2821 case Promote: assert(0 && "Unreachable with 2 FP types!"); 2822 case Legal: 2823 // Input is legal? Do an FP_ROUND_INREG. 2824 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), 2825 DAG.getValueType(VT)); 2826 break; 2827 } 2828 break; 2829 2830 case ISD::SINT_TO_FP: 2831 case ISD::UINT_TO_FP: 2832 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2833 case Legal: 2834 // No extra round required here. 2835 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); 2836 break; 2837 2838 case Promote: 2839 Result = PromoteOp(Node->getOperand(0)); 2840 if (Node->getOpcode() == ISD::SINT_TO_FP) 2841 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), 2842 Result, 2843 DAG.getValueType(Node->getOperand(0).getValueType())); 2844 else 2845 Result = DAG.getZeroExtendInReg(Result, 2846 Node->getOperand(0).getValueType()); 2847 // No extra round required here. 2848 Result = DAG.getNode(Node->getOpcode(), NVT, Result); 2849 break; 2850 case Expand: 2851 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, 2852 Node->getOperand(0)); 2853 // Round if we cannot tolerate excess precision. 2854 if (NoExcessFPPrecision) 2855 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2856 DAG.getValueType(VT)); 2857 break; 2858 } 2859 break; 2860 2861 case ISD::SIGN_EXTEND_INREG: 2862 Result = PromoteOp(Node->getOperand(0)); 2863 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, 2864 Node->getOperand(1)); 2865 break; 2866 case ISD::FP_TO_SINT: 2867 case ISD::FP_TO_UINT: 2868 switch (getTypeAction(Node->getOperand(0).getValueType())) { 2869 case Legal: 2870 Tmp1 = Node->getOperand(0); 2871 break; 2872 case Promote: 2873 // The input result is prerounded, so we don't have to do anything 2874 // special. 2875 Tmp1 = PromoteOp(Node->getOperand(0)); 2876 break; 2877 case Expand: 2878 assert(0 && "not implemented"); 2879 } 2880 // If we're promoting a UINT to a larger size, check to see if the new node 2881 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since 2882 // we can use that instead. This allows us to generate better code for 2883 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not 2884 // legal, such as PowerPC. 2885 if (Node->getOpcode() == ISD::FP_TO_UINT && 2886 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && 2887 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || 2888 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ 2889 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); 2890 } else { 2891 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 2892 } 2893 break; 2894 2895 case ISD::FABS: 2896 case ISD::FNEG: 2897 Tmp1 = PromoteOp(Node->getOperand(0)); 2898 assert(Tmp1.getValueType() == NVT); 2899 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 2900 // NOTE: we do not have to do any extra rounding here for 2901 // NoExcessFPPrecision, because we know the input will have the appropriate 2902 // precision, and these operations don't modify precision at all. 2903 break; 2904 2905 case ISD::FSQRT: 2906 case ISD::FSIN: 2907 case ISD::FCOS: 2908 Tmp1 = PromoteOp(Node->getOperand(0)); 2909 assert(Tmp1.getValueType() == NVT); 2910 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 2911 if (NoExcessFPPrecision) 2912 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2913 DAG.getValueType(VT)); 2914 break; 2915 2916 case ISD::AND: 2917 case ISD::OR: 2918 case ISD::XOR: 2919 case ISD::ADD: 2920 case ISD::SUB: 2921 case ISD::MUL: 2922 // The input may have strange things in the top bits of the registers, but 2923 // these operations don't care. They may have weird bits going out, but 2924 // that too is okay if they are integer operations. 2925 Tmp1 = PromoteOp(Node->getOperand(0)); 2926 Tmp2 = PromoteOp(Node->getOperand(1)); 2927 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); 2928 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2929 break; 2930 case ISD::FADD: 2931 case ISD::FSUB: 2932 case ISD::FMUL: 2933 Tmp1 = PromoteOp(Node->getOperand(0)); 2934 Tmp2 = PromoteOp(Node->getOperand(1)); 2935 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); 2936 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2937 2938 // Floating point operations will give excess precision that we may not be 2939 // able to tolerate. If we DO allow excess precision, just leave it, 2940 // otherwise excise it. 2941 // FIXME: Why would we need to round FP ops more than integer ones? 2942 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C)) 2943 if (NoExcessFPPrecision) 2944 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2945 DAG.getValueType(VT)); 2946 break; 2947 2948 case ISD::SDIV: 2949 case ISD::SREM: 2950 // These operators require that their input be sign extended. 2951 Tmp1 = PromoteOp(Node->getOperand(0)); 2952 Tmp2 = PromoteOp(Node->getOperand(1)); 2953 if (MVT::isInteger(NVT)) { 2954 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, 2955 DAG.getValueType(VT)); 2956 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, 2957 DAG.getValueType(VT)); 2958 } 2959 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2960 2961 // Perform FP_ROUND: this is probably overly pessimistic. 2962 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) 2963 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2964 DAG.getValueType(VT)); 2965 break; 2966 case ISD::FDIV: 2967 case ISD::FREM: 2968 // These operators require that their input be fp extended. 2969 Tmp1 = PromoteOp(Node->getOperand(0)); 2970 Tmp2 = PromoteOp(Node->getOperand(1)); 2971 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2972 2973 // Perform FP_ROUND: this is probably overly pessimistic. 2974 if (NoExcessFPPrecision) 2975 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, 2976 DAG.getValueType(VT)); 2977 break; 2978 2979 case ISD::UDIV: 2980 case ISD::UREM: 2981 // These operators require that their input be zero extended. 2982 Tmp1 = PromoteOp(Node->getOperand(0)); 2983 Tmp2 = PromoteOp(Node->getOperand(1)); 2984 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); 2985 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); 2986 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); 2987 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); 2988 break; 2989 2990 case ISD::SHL: 2991 Tmp1 = PromoteOp(Node->getOperand(0)); 2992 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); 2993 break; 2994 case ISD::SRA: 2995 // The input value must be properly sign extended. 2996 Tmp1 = PromoteOp(Node->getOperand(0)); 2997 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, 2998 DAG.getValueType(VT)); 2999 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); 3000 break; 3001 case ISD::SRL: 3002 // The input value must be properly zero extended. 3003 Tmp1 = PromoteOp(Node->getOperand(0)); 3004 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); 3005 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); 3006 break; 3007 3008 case ISD::VAARG: 3009 Tmp1 = Node->getOperand(0); // Get the chain. 3010 Tmp2 = Node->getOperand(1); // Get the pointer. 3011 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { 3012 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); 3013 Result = TLI.CustomPromoteOperation(Tmp3, DAG); 3014 } else { 3015 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, 3016 Node->getOperand(2)); 3017 // Increment the pointer, VAList, to the next vaarg 3018 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, 3019 DAG.getConstant(MVT::getSizeInBits(VT)/8, 3020 TLI.getPointerTy())); 3021 // Store the incremented VAList to the legalized pointer 3022 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, 3023 Node->getOperand(2)); 3024 // Load the actual argument out of the pointer VAList 3025 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, 3026 DAG.getSrcValue(0), VT); 3027 } 3028 // Remember that we legalized the chain. 3029 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); 3030 break; 3031 3032 case ISD::LOAD: 3033 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0), 3034 Node->getOperand(1), Node->getOperand(2), VT); 3035 // Remember that we legalized the chain. 3036 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); 3037 break; 3038 case ISD::SEXTLOAD: 3039 case ISD::ZEXTLOAD: 3040 case ISD::EXTLOAD: 3041 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0), 3042 Node->getOperand(1), Node->getOperand(2), 3043 cast<VTSDNode>(Node->getOperand(3))->getVT()); 3044 // Remember that we legalized the chain. 3045 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); 3046 break; 3047 case ISD::SELECT: 3048 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 3049 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 3050 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); 3051 break; 3052 case ISD::SELECT_CC: 3053 Tmp2 = PromoteOp(Node->getOperand(2)); // True 3054 Tmp3 = PromoteOp(Node->getOperand(3)); // False 3055 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), 3056 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); 3057 break; 3058 case ISD::BSWAP: 3059 Tmp1 = Node->getOperand(0); 3060 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); 3061 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); 3062 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, 3063 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT), 3064 TLI.getShiftAmountTy())); 3065 break; 3066 case ISD::CTPOP: 3067 case ISD::CTTZ: 3068 case ISD::CTLZ: 3069 // Zero extend the argument 3070 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); 3071 // Perform the larger operation, then subtract if needed. 3072 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); 3073 switch(Node->getOpcode()) { 3074 case ISD::CTPOP: 3075 Result = Tmp1; 3076 break; 3077 case ISD::CTTZ: 3078 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) 3079 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, 3080 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ); 3081 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, 3082 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1); 3083 break; 3084 case ISD::CTLZ: 3085 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 3086 Result = DAG.getNode(ISD::SUB, NVT, Tmp1, 3087 DAG.getConstant(getSizeInBits(NVT) - 3088 getSizeInBits(VT), NVT)); 3089 break; 3090 } 3091 break; 3092 } 3093 3094 assert(Result.Val && "Didn't set a result!"); 3095 3096 // Make sure the result is itself legal. 3097 Result = LegalizeOp(Result); 3098 3099 // Remember that we promoted this! 3100 AddPromotedOperand(Op, Result); 3101 return Result; 3102} 3103 3104/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. 3105/// The resultant code need not be legal. Note that SrcOp is the input operand 3106/// to the BIT_CONVERT, not the BIT_CONVERT node itself. 3107SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, 3108 SDOperand SrcOp) { 3109 // Create the stack frame object. 3110 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 3111 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8; 3112 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize); 3113 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); 3114 3115 // Emit a store to the stack slot. 3116 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), 3117 SrcOp, FIPtr, DAG.getSrcValue(NULL)); 3118 // Result is a load from the stack slot. 3119 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0)); 3120} 3121 3122void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, 3123 SDOperand Op, SDOperand Amt, 3124 SDOperand &Lo, SDOperand &Hi) { 3125 // Expand the subcomponents. 3126 SDOperand LHSL, LHSH; 3127 ExpandOp(Op, LHSL, LHSH); 3128 3129 std::vector<SDOperand> Ops; 3130 Ops.push_back(LHSL); 3131 Ops.push_back(LHSH); 3132 Ops.push_back(Amt); 3133 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType()); 3134 Lo = DAG.getNode(NodeOp, VTs, Ops); 3135 Hi = Lo.getValue(1); 3136} 3137 3138 3139/// ExpandShift - Try to find a clever way to expand this shift operation out to 3140/// smaller elements. If we can't find a way that is more efficient than a 3141/// libcall on this target, return false. Otherwise, return true with the 3142/// low-parts expanded into Lo and Hi. 3143bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, 3144 SDOperand &Lo, SDOperand &Hi) { 3145 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && 3146 "This is not a shift!"); 3147 3148 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); 3149 SDOperand ShAmt = LegalizeOp(Amt); 3150 MVT::ValueType ShTy = ShAmt.getValueType(); 3151 unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); 3152 unsigned NVTBits = MVT::getSizeInBits(NVT); 3153 3154 // Handle the case when Amt is an immediate. Other cases are currently broken 3155 // and are disabled. 3156 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { 3157 unsigned Cst = CN->getValue(); 3158 // Expand the incoming operand to be shifted, so that we have its parts 3159 SDOperand InL, InH; 3160 ExpandOp(Op, InL, InH); 3161 switch(Opc) { 3162 case ISD::SHL: 3163 if (Cst > VTBits) { 3164 Lo = DAG.getConstant(0, NVT); 3165 Hi = DAG.getConstant(0, NVT); 3166 } else if (Cst > NVTBits) { 3167 Lo = DAG.getConstant(0, NVT); 3168 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); 3169 } else if (Cst == NVTBits) { 3170 Lo = DAG.getConstant(0, NVT); 3171 Hi = InL; 3172 } else { 3173 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); 3174 Hi = DAG.getNode(ISD::OR, NVT, 3175 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), 3176 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); 3177 } 3178 return true; 3179 case ISD::SRL: 3180 if (Cst > VTBits) { 3181 Lo = DAG.getConstant(0, NVT); 3182 Hi = DAG.getConstant(0, NVT); 3183 } else if (Cst > NVTBits) { 3184 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); 3185 Hi = DAG.getConstant(0, NVT); 3186 } else if (Cst == NVTBits) { 3187 Lo = InH; 3188 Hi = DAG.getConstant(0, NVT); 3189 } else { 3190 Lo = DAG.getNode(ISD::OR, NVT, 3191 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), 3192 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); 3193 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); 3194 } 3195 return true; 3196 case ISD::SRA: 3197 if (Cst > VTBits) { 3198 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, 3199 DAG.getConstant(NVTBits-1, ShTy)); 3200 } else if (Cst > NVTBits) { 3201 Lo = DAG.getNode(ISD::SRA, NVT, InH, 3202 DAG.getConstant(Cst-NVTBits, ShTy)); 3203 Hi = DAG.getNode(ISD::SRA, NVT, InH, 3204 DAG.getConstant(NVTBits-1, ShTy)); 3205 } else if (Cst == NVTBits) { 3206 Lo = InH; 3207 Hi = DAG.getNode(ISD::SRA, NVT, InH, 3208 DAG.getConstant(NVTBits-1, ShTy)); 3209 } else { 3210 Lo = DAG.getNode(ISD::OR, NVT, 3211 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), 3212 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); 3213 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); 3214 } 3215 return true; 3216 } 3217 } 3218 return false; 3219} 3220 3221/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest 3222/// NodeDepth) node that is an CallSeqStart operation and occurs later than 3223/// Found. 3224static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found, 3225 std::set<SDNode*> &Visited) { 3226 if (Node->getNodeDepth() <= Found->getNodeDepth() || 3227 !Visited.insert(Node).second) return; 3228 3229 // If we found an CALLSEQ_START, we already know this node occurs later 3230 // than the Found node. Just remember this node and return. 3231 if (Node->getOpcode() == ISD::CALLSEQ_START) { 3232 Found = Node; 3233 return; 3234 } 3235 3236 // Otherwise, scan the operands of Node to see if any of them is a call. 3237 assert(Node->getNumOperands() != 0 && 3238 "All leaves should have depth equal to the entry node!"); 3239 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i) 3240 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited); 3241 3242 // Tail recurse for the last iteration. 3243 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val, 3244 Found, Visited); 3245} 3246 3247 3248/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest 3249/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent 3250/// than Found. 3251static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found, 3252 std::set<SDNode*> &Visited) { 3253 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) || 3254 !Visited.insert(Node).second) return; 3255 3256 // If we found an CALLSEQ_END, we already know this node occurs earlier 3257 // than the Found node. Just remember this node and return. 3258 if (Node->getOpcode() == ISD::CALLSEQ_END) { 3259 Found = Node; 3260 return; 3261 } 3262 3263 // Otherwise, scan the operands of Node to see if any of them is a call. 3264 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); 3265 if (UI == E) return; 3266 for (--E; UI != E; ++UI) 3267 FindEarliestCallSeqEnd(*UI, Found, Visited); 3268 3269 // Tail recurse for the last iteration. 3270 FindEarliestCallSeqEnd(*UI, Found, Visited); 3271} 3272 3273/// FindCallSeqEnd - Given a chained node that is part of a call sequence, 3274/// find the CALLSEQ_END node that terminates the call sequence. 3275static SDNode *FindCallSeqEnd(SDNode *Node) { 3276 if (Node->getOpcode() == ISD::CALLSEQ_END) 3277 return Node; 3278 if (Node->use_empty()) 3279 return 0; // No CallSeqEnd 3280 3281 // The chain is usually at the end. 3282 SDOperand TheChain(Node, Node->getNumValues()-1); 3283 if (TheChain.getValueType() != MVT::Other) { 3284 // Sometimes it's at the beginning. 3285 TheChain = SDOperand(Node, 0); 3286 if (TheChain.getValueType() != MVT::Other) { 3287 // Otherwise, hunt for it. 3288 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) 3289 if (Node->getValueType(i) == MVT::Other) { 3290 TheChain = SDOperand(Node, i); 3291 break; 3292 } 3293 3294 // Otherwise, we walked into a node without a chain. 3295 if (TheChain.getValueType() != MVT::Other) 3296 return 0; 3297 } 3298 } 3299 3300 for (SDNode::use_iterator UI = Node->use_begin(), 3301 E = Node->use_end(); UI != E; ++UI) { 3302 3303 // Make sure to only follow users of our token chain. 3304 SDNode *User = *UI; 3305 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) 3306 if (User->getOperand(i) == TheChain) 3307 if (SDNode *Result = FindCallSeqEnd(User)) 3308 return Result; 3309 } 3310 return 0; 3311} 3312 3313/// FindCallSeqStart - Given a chained node that is part of a call sequence, 3314/// find the CALLSEQ_START node that initiates the call sequence. 3315static SDNode *FindCallSeqStart(SDNode *Node) { 3316 assert(Node && "Didn't find callseq_start for a call??"); 3317 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; 3318 3319 assert(Node->getOperand(0).getValueType() == MVT::Other && 3320 "Node doesn't have a token chain argument!"); 3321 return FindCallSeqStart(Node->getOperand(0).Val); 3322} 3323 3324 3325/// FindInputOutputChains - If we are replacing an operation with a call we need 3326/// to find the call that occurs before and the call that occurs after it to 3327/// properly serialize the calls in the block. The returned operand is the 3328/// input chain value for the new call (e.g. the entry node or the previous 3329/// call), and OutChain is set to be the chain node to update to point to the 3330/// end of the call chain. 3331static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain, 3332 SDOperand Entry) { 3333 SDNode *LatestCallSeqStart = Entry.Val; 3334 SDNode *LatestCallSeqEnd = 0; 3335 std::set<SDNode*> Visited; 3336 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited); 3337 Visited.clear(); 3338 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n"; 3339 3340 // It is possible that no ISD::CALLSEQ_START was found because there is no 3341 // previous call in the function. LatestCallStackDown may in that case be 3342 // the entry node itself. Do not attempt to find a matching CALLSEQ_END 3343 // unless LatestCallStackDown is an CALLSEQ_START. 3344 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) { 3345 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart); 3346 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n"; 3347 } else { 3348 LatestCallSeqEnd = Entry.Val; 3349 } 3350 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd"); 3351 3352 // Finally, find the first call that this must come before, first we find the 3353 // CallSeqEnd that ends the call. 3354 OutChain = 0; 3355 FindEarliestCallSeqEnd(OpNode, OutChain, Visited); 3356 Visited.clear(); 3357 3358 // If we found one, translate from the adj up to the callseq_start. 3359 if (OutChain) 3360 OutChain = FindCallSeqStart(OutChain); 3361 3362 return SDOperand(LatestCallSeqEnd, 0); 3363} 3364 3365/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a 3366void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult, 3367 SDNode *OutChain) { 3368 // Nothing to splice it into? 3369 if (OutChain == 0) return; 3370 3371 assert(OutChain->getOperand(0).getValueType() == MVT::Other); 3372 //OutChain->dump(); 3373 3374 // Form a token factor node merging the old inval and the new inval. 3375 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult, 3376 OutChain->getOperand(0)); 3377 // Change the node to refer to the new token. 3378 OutChain->setAdjCallChain(InToken); 3379} 3380 3381 3382// ExpandLibCall - Expand a node into a call to a libcall. If the result value 3383// does not fit into a register, return the lo part and set the hi part to the 3384// by-reg argument. If it does fit into a single register, return the result 3385// and leave the Hi part unset. 3386SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, 3387 SDOperand &Hi) { 3388 SDNode *OutChain; 3389 SDOperand InChain = FindInputOutputChains(Node, OutChain, 3390 DAG.getEntryNode()); 3391 if (InChain.Val == 0) 3392 InChain = DAG.getEntryNode(); 3393 3394 TargetLowering::ArgListTy Args; 3395 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 3396 MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); 3397 const Type *ArgTy = MVT::getTypeForValueType(ArgVT); 3398 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); 3399 } 3400 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); 3401 3402 // Splice the libcall in wherever FindInputOutputChains tells us to. 3403 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); 3404 std::pair<SDOperand,SDOperand> CallInfo = 3405 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false, 3406 Callee, Args, DAG); 3407 3408 SDOperand Result; 3409 switch (getTypeAction(CallInfo.first.getValueType())) { 3410 default: assert(0 && "Unknown thing"); 3411 case Legal: 3412 Result = CallInfo.first; 3413 break; 3414 case Expand: 3415 ExpandOp(CallInfo.first, Result, Hi); 3416 break; 3417 } 3418 3419 CallInfo.second = LegalizeOp(CallInfo.second); 3420 SpliceCallInto(CallInfo.second, OutChain); 3421 return Result; 3422} 3423 3424 3425/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the 3426/// destination type is legal. 3427SDOperand SelectionDAGLegalize:: 3428ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { 3429 assert(isTypeLegal(DestTy) && "Destination type is not legal!"); 3430 assert(getTypeAction(Source.getValueType()) == Expand && 3431 "This is not an expansion!"); 3432 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); 3433 3434 if (!isSigned) { 3435 assert(Source.getValueType() == MVT::i64 && 3436 "This only works for 64-bit -> FP"); 3437 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the 3438 // incoming integer is set. To handle this, we dynamically test to see if 3439 // it is set, and, if so, add a fudge factor. 3440 SDOperand Lo, Hi; 3441 ExpandOp(Source, Lo, Hi); 3442 3443 // If this is unsigned, and not supported, first perform the conversion to 3444 // signed, then adjust the result if the sign bit is set. 3445 SDOperand SignedConv = ExpandIntToFP(true, DestTy, 3446 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); 3447 3448 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, 3449 DAG.getConstant(0, Hi.getValueType()), 3450 ISD::SETLT); 3451 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); 3452 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), 3453 SignSet, Four, Zero); 3454 uint64_t FF = 0x5f800000ULL; 3455 if (TLI.isLittleEndian()) FF <<= 32; 3456 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); 3457 3458 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); 3459 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); 3460 SDOperand FudgeInReg; 3461 if (DestTy == MVT::f32) 3462 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, 3463 DAG.getSrcValue(NULL)); 3464 else { 3465 assert(DestTy == MVT::f64 && "Unexpected conversion"); 3466 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), 3467 CPIdx, DAG.getSrcValue(NULL), MVT::f32); 3468 } 3469 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); 3470 } 3471 3472 // Check to see if the target has a custom way to lower this. If so, use it. 3473 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { 3474 default: assert(0 && "This action not implemented for this operation!"); 3475 case TargetLowering::Legal: 3476 case TargetLowering::Expand: 3477 break; // This case is handled below. 3478 case TargetLowering::Custom: { 3479 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, 3480 Source), DAG); 3481 if (NV.Val) 3482 return LegalizeOp(NV); 3483 break; // The target decided this was legal after all 3484 } 3485 } 3486 3487 // Expand the source, then glue it back together for the call. We must expand 3488 // the source in case it is shared (this pass of legalize must traverse it). 3489 SDOperand SrcLo, SrcHi; 3490 ExpandOp(Source, SrcLo, SrcHi); 3491 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); 3492 3493 SDNode *OutChain = 0; 3494 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain, 3495 DAG.getEntryNode()); 3496 const char *FnName = 0; 3497 if (DestTy == MVT::f32) 3498 FnName = "__floatdisf"; 3499 else { 3500 assert(DestTy == MVT::f64 && "Unknown fp value type!"); 3501 FnName = "__floatdidf"; 3502 } 3503 3504 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy()); 3505 3506 TargetLowering::ArgListTy Args; 3507 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType()); 3508 3509 Args.push_back(std::make_pair(Source, ArgTy)); 3510 3511 // We don't care about token chains for libcalls. We just use the entry 3512 // node as our input and ignore the output chain. This allows us to place 3513 // calls wherever we need them to satisfy data dependences. 3514 const Type *RetTy = MVT::getTypeForValueType(DestTy); 3515 3516 std::pair<SDOperand,SDOperand> CallResult = 3517 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true, 3518 Callee, Args, DAG); 3519 3520 SpliceCallInto(CallResult.second, OutChain); 3521 return CallResult.first; 3522} 3523 3524 3525 3526/// ExpandOp - Expand the specified SDOperand into its two component pieces 3527/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the 3528/// LegalizeNodes map is filled in for any results that are not expanded, the 3529/// ExpandedNodes map is filled in for any results that are expanded, and the 3530/// Lo/Hi values are returned. 3531void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ 3532 MVT::ValueType VT = Op.getValueType(); 3533 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); 3534 SDNode *Node = Op.Val; 3535 assert(getTypeAction(VT) == Expand && "Not an expanded type!"); 3536 assert((MVT::isInteger(VT) || VT == MVT::Vector) && 3537 "Cannot expand FP values!"); 3538 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) && 3539 "Cannot expand to FP value or to larger int value!"); 3540 3541 // See if we already expanded it. 3542 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I 3543 = ExpandedNodes.find(Op); 3544 if (I != ExpandedNodes.end()) { 3545 Lo = I->second.first; 3546 Hi = I->second.second; 3547 return; 3548 } 3549 3550 switch (Node->getOpcode()) { 3551 case ISD::CopyFromReg: 3552 assert(0 && "CopyFromReg must be legal!"); 3553 default: 3554 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; 3555 assert(0 && "Do not know how to expand this operator!"); 3556 abort(); 3557 case ISD::UNDEF: 3558 Lo = DAG.getNode(ISD::UNDEF, NVT); 3559 Hi = DAG.getNode(ISD::UNDEF, NVT); 3560 break; 3561 case ISD::Constant: { 3562 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); 3563 Lo = DAG.getConstant(Cst, NVT); 3564 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); 3565 break; 3566 } 3567 case ISD::ConstantVec: { 3568 unsigned NumElements = Node->getNumOperands(); 3569 // If we only have two elements left in the constant vector, just break it 3570 // apart into the two scalar constants it contains. Otherwise, bisect the 3571 // ConstantVec, and return each half as a new ConstantVec. 3572 // FIXME: this is hard coded as big endian, it may have to change to support 3573 // SSE and Alpha MVI 3574 if (NumElements == 2) { 3575 Hi = Node->getOperand(0); 3576 Lo = Node->getOperand(1); 3577 } else { 3578 NumElements /= 2; 3579 std::vector<SDOperand> LoOps, HiOps; 3580 for (unsigned I = 0, E = NumElements; I < E; ++I) { 3581 HiOps.push_back(Node->getOperand(I)); 3582 LoOps.push_back(Node->getOperand(I+NumElements)); 3583 } 3584 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps); 3585 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps); 3586 } 3587 break; 3588 } 3589 3590 case ISD::BUILD_PAIR: 3591 // Return the operands. 3592 Lo = Node->getOperand(0); 3593 Hi = Node->getOperand(1); 3594 break; 3595 3596 case ISD::SIGN_EXTEND_INREG: 3597 ExpandOp(Node->getOperand(0), Lo, Hi); 3598 // Sign extend the lo-part. 3599 Hi = DAG.getNode(ISD::SRA, NVT, Lo, 3600 DAG.getConstant(MVT::getSizeInBits(NVT)-1, 3601 TLI.getShiftAmountTy())); 3602 // sext_inreg the low part if needed. 3603 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); 3604 break; 3605 3606 case ISD::BSWAP: { 3607 ExpandOp(Node->getOperand(0), Lo, Hi); 3608 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); 3609 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); 3610 Lo = TempLo; 3611 break; 3612 } 3613 3614 case ISD::CTPOP: 3615 ExpandOp(Node->getOperand(0), Lo, Hi); 3616 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) 3617 DAG.getNode(ISD::CTPOP, NVT, Lo), 3618 DAG.getNode(ISD::CTPOP, NVT, Hi)); 3619 Hi = DAG.getConstant(0, NVT); 3620 break; 3621 3622 case ISD::CTLZ: { 3623 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) 3624 ExpandOp(Node->getOperand(0), Lo, Hi); 3625 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); 3626 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); 3627 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, 3628 ISD::SETNE); 3629 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); 3630 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); 3631 3632 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); 3633 Hi = DAG.getConstant(0, NVT); 3634 break; 3635 } 3636 3637 case ISD::CTTZ: { 3638 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) 3639 ExpandOp(Node->getOperand(0), Lo, Hi); 3640 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); 3641 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); 3642 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, 3643 ISD::SETNE); 3644 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); 3645 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); 3646 3647 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); 3648 Hi = DAG.getConstant(0, NVT); 3649 break; 3650 } 3651 3652 case ISD::VAARG: { 3653 SDOperand Ch = Node->getOperand(0); // Legalize the chain. 3654 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. 3655 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); 3656 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); 3657 3658 // Remember that we legalized the chain. 3659 Hi = LegalizeOp(Hi); 3660 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); 3661 if (!TLI.isLittleEndian()) 3662 std::swap(Lo, Hi); 3663 break; 3664 } 3665 3666 case ISD::LOAD: { 3667 SDOperand Ch = Node->getOperand(0); // Legalize the chain. 3668 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. 3669 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); 3670 3671 // Increment the pointer to the other half. 3672 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; 3673 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 3674 getIntPtrConstant(IncrementSize)); 3675 // FIXME: This creates a bogus srcvalue! 3676 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); 3677 3678 // Build a factor node to remember that this load is independent of the 3679 // other one. 3680 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 3681 Hi.getValue(1)); 3682 3683 // Remember that we legalized the chain. 3684 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); 3685 if (!TLI.isLittleEndian()) 3686 std::swap(Lo, Hi); 3687 break; 3688 } 3689 case ISD::VLOAD: { 3690 SDOperand Ch = Node->getOperand(0); // Legalize the chain. 3691 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. 3692 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue(); 3693 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3694 3695 // If we only have two elements, turn into a pair of scalar loads. 3696 // FIXME: handle case where a vector of two elements is fine, such as 3697 // 2 x double on SSE2. 3698 if (NumElements == 2) { 3699 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4)); 3700 // Increment the pointer to the other half. 3701 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8; 3702 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 3703 getIntPtrConstant(IncrementSize)); 3704 // FIXME: This creates a bogus srcvalue! 3705 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4)); 3706 } else { 3707 NumElements /= 2; // Split the vector in half 3708 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4)); 3709 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8; 3710 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 3711 getIntPtrConstant(IncrementSize)); 3712 // FIXME: This creates a bogus srcvalue! 3713 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4)); 3714 } 3715 3716 // Build a factor node to remember that this load is independent of the 3717 // other one. 3718 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 3719 Hi.getValue(1)); 3720 3721 // Remember that we legalized the chain. 3722 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); 3723 if (!TLI.isLittleEndian()) 3724 std::swap(Lo, Hi); 3725 break; 3726 } 3727 case ISD::VADD: 3728 case ISD::VSUB: 3729 case ISD::VMUL: { 3730 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue(); 3731 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3732 SDOperand LL, LH, RL, RH; 3733 3734 ExpandOp(Node->getOperand(0), LL, LH); 3735 ExpandOp(Node->getOperand(1), RL, RH); 3736 3737 // If we only have two elements, turn into a pair of scalar loads. 3738 // FIXME: handle case where a vector of two elements is fine, such as 3739 // 2 x double on SSE2. 3740 if (NumElements == 2) { 3741 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT); 3742 Lo = DAG.getNode(Opc, EVT, LL, RL); 3743 Hi = DAG.getNode(Opc, EVT, LH, RH); 3744 } else { 3745 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2), 3746 LL.getOperand(3)); 3747 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2), 3748 LH.getOperand(3)); 3749 } 3750 break; 3751 } 3752 case ISD::AND: 3753 case ISD::OR: 3754 case ISD::XOR: { // Simple logical operators -> two trivial pieces. 3755 SDOperand LL, LH, RL, RH; 3756 ExpandOp(Node->getOperand(0), LL, LH); 3757 ExpandOp(Node->getOperand(1), RL, RH); 3758 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); 3759 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); 3760 break; 3761 } 3762 case ISD::SELECT: { 3763 SDOperand LL, LH, RL, RH; 3764 ExpandOp(Node->getOperand(1), LL, LH); 3765 ExpandOp(Node->getOperand(2), RL, RH); 3766 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); 3767 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); 3768 break; 3769 } 3770 case ISD::SELECT_CC: { 3771 SDOperand TL, TH, FL, FH; 3772 ExpandOp(Node->getOperand(2), TL, TH); 3773 ExpandOp(Node->getOperand(3), FL, FH); 3774 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), 3775 Node->getOperand(1), TL, FL, Node->getOperand(4)); 3776 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), 3777 Node->getOperand(1), TH, FH, Node->getOperand(4)); 3778 break; 3779 } 3780 case ISD::SEXTLOAD: { 3781 SDOperand Chain = Node->getOperand(0); 3782 SDOperand Ptr = Node->getOperand(1); 3783 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3784 3785 if (EVT == NVT) 3786 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); 3787 else 3788 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), 3789 EVT); 3790 3791 // Remember that we legalized the chain. 3792 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); 3793 3794 // The high part is obtained by SRA'ing all but one of the bits of the lo 3795 // part. 3796 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); 3797 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, 3798 TLI.getShiftAmountTy())); 3799 break; 3800 } 3801 case ISD::ZEXTLOAD: { 3802 SDOperand Chain = Node->getOperand(0); 3803 SDOperand Ptr = Node->getOperand(1); 3804 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3805 3806 if (EVT == NVT) 3807 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); 3808 else 3809 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), 3810 EVT); 3811 3812 // Remember that we legalized the chain. 3813 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); 3814 3815 // The high part is just a zero. 3816 Hi = DAG.getConstant(0, NVT); 3817 break; 3818 } 3819 case ISD::EXTLOAD: { 3820 SDOperand Chain = Node->getOperand(0); 3821 SDOperand Ptr = Node->getOperand(1); 3822 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); 3823 3824 if (EVT == NVT) 3825 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); 3826 else 3827 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), 3828 EVT); 3829 3830 // Remember that we legalized the chain. 3831 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); 3832 3833 // The high part is undefined. 3834 Hi = DAG.getNode(ISD::UNDEF, NVT); 3835 break; 3836 } 3837 case ISD::ANY_EXTEND: 3838 // The low part is any extension of the input (which degenerates to a copy). 3839 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); 3840 // The high part is undefined. 3841 Hi = DAG.getNode(ISD::UNDEF, NVT); 3842 break; 3843 case ISD::SIGN_EXTEND: { 3844 // The low part is just a sign extension of the input (which degenerates to 3845 // a copy). 3846 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); 3847 3848 // The high part is obtained by SRA'ing all but one of the bits of the lo 3849 // part. 3850 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); 3851 Hi = DAG.getNode(ISD::SRA, NVT, Lo, 3852 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); 3853 break; 3854 } 3855 case ISD::ZERO_EXTEND: 3856 // The low part is just a zero extension of the input (which degenerates to 3857 // a copy). 3858 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); 3859 3860 // The high part is just a zero. 3861 Hi = DAG.getConstant(0, NVT); 3862 break; 3863 3864 case ISD::BIT_CONVERT: { 3865 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0), 3866 Node->getOperand(0)); 3867 ExpandOp(Tmp, Lo, Hi); 3868 break; 3869 } 3870 3871 case ISD::READCYCLECOUNTER: 3872 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == 3873 TargetLowering::Custom && 3874 "Must custom expand ReadCycleCounter"); 3875 Lo = TLI.LowerOperation(Op, DAG); 3876 assert(Lo.Val && "Node must be custom expanded!"); 3877 Hi = Lo.getValue(1); 3878 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. 3879 LegalizeOp(Lo.getValue(2))); 3880 break; 3881 3882 // These operators cannot be expanded directly, emit them as calls to 3883 // library functions. 3884 case ISD::FP_TO_SINT: 3885 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { 3886 SDOperand Op; 3887 switch (getTypeAction(Node->getOperand(0).getValueType())) { 3888 case Expand: assert(0 && "cannot expand FP!"); 3889 case Legal: Op = LegalizeOp(Node->getOperand(0)); break; 3890 case Promote: Op = PromoteOp (Node->getOperand(0)); break; 3891 } 3892 3893 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); 3894 3895 // Now that the custom expander is done, expand the result, which is still 3896 // VT. 3897 if (Op.Val) { 3898 ExpandOp(Op, Lo, Hi); 3899 break; 3900 } 3901 } 3902 3903 if (Node->getOperand(0).getValueType() == MVT::f32) 3904 Lo = ExpandLibCall("__fixsfdi", Node, Hi); 3905 else 3906 Lo = ExpandLibCall("__fixdfdi", Node, Hi); 3907 break; 3908 3909 case ISD::FP_TO_UINT: 3910 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { 3911 SDOperand Op; 3912 switch (getTypeAction(Node->getOperand(0).getValueType())) { 3913 case Expand: assert(0 && "cannot expand FP!"); 3914 case Legal: Op = LegalizeOp(Node->getOperand(0)); break; 3915 case Promote: Op = PromoteOp (Node->getOperand(0)); break; 3916 } 3917 3918 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); 3919 3920 // Now that the custom expander is done, expand the result. 3921 if (Op.Val) { 3922 ExpandOp(Op, Lo, Hi); 3923 break; 3924 } 3925 } 3926 3927 if (Node->getOperand(0).getValueType() == MVT::f32) 3928 Lo = ExpandLibCall("__fixunssfdi", Node, Hi); 3929 else 3930 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); 3931 break; 3932 3933 case ISD::SHL: { 3934 // If the target wants custom lowering, do so. 3935 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); 3936 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { 3937 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); 3938 Op = TLI.LowerOperation(Op, DAG); 3939 if (Op.Val) { 3940 // Now that the custom expander is done, expand the result, which is 3941 // still VT. 3942 ExpandOp(Op, Lo, Hi); 3943 break; 3944 } 3945 } 3946 3947 // If we can emit an efficient shift operation, do so now. 3948 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) 3949 break; 3950 3951 // If this target supports SHL_PARTS, use it. 3952 TargetLowering::LegalizeAction Action = 3953 TLI.getOperationAction(ISD::SHL_PARTS, NVT); 3954 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || 3955 Action == TargetLowering::Custom) { 3956 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); 3957 break; 3958 } 3959 3960 // Otherwise, emit a libcall. 3961 Lo = ExpandLibCall("__ashldi3", Node, Hi); 3962 break; 3963 } 3964 3965 case ISD::SRA: { 3966 // If the target wants custom lowering, do so. 3967 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); 3968 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { 3969 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); 3970 Op = TLI.LowerOperation(Op, DAG); 3971 if (Op.Val) { 3972 // Now that the custom expander is done, expand the result, which is 3973 // still VT. 3974 ExpandOp(Op, Lo, Hi); 3975 break; 3976 } 3977 } 3978 3979 // If we can emit an efficient shift operation, do so now. 3980 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) 3981 break; 3982 3983 // If this target supports SRA_PARTS, use it. 3984 TargetLowering::LegalizeAction Action = 3985 TLI.getOperationAction(ISD::SRA_PARTS, NVT); 3986 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || 3987 Action == TargetLowering::Custom) { 3988 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); 3989 break; 3990 } 3991 3992 // Otherwise, emit a libcall. 3993 Lo = ExpandLibCall("__ashrdi3", Node, Hi); 3994 break; 3995 } 3996 3997 case ISD::SRL: { 3998 // If the target wants custom lowering, do so. 3999 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); 4000 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { 4001 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); 4002 Op = TLI.LowerOperation(Op, DAG); 4003 if (Op.Val) { 4004 // Now that the custom expander is done, expand the result, which is 4005 // still VT. 4006 ExpandOp(Op, Lo, Hi); 4007 break; 4008 } 4009 } 4010 4011 // If we can emit an efficient shift operation, do so now. 4012 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) 4013 break; 4014 4015 // If this target supports SRL_PARTS, use it. 4016 TargetLowering::LegalizeAction Action = 4017 TLI.getOperationAction(ISD::SRL_PARTS, NVT); 4018 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || 4019 Action == TargetLowering::Custom) { 4020 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); 4021 break; 4022 } 4023 4024 // Otherwise, emit a libcall. 4025 Lo = ExpandLibCall("__lshrdi3", Node, Hi); 4026 break; 4027 } 4028 4029 case ISD::ADD: 4030 case ISD::SUB: { 4031 // If the target wants to custom expand this, let them. 4032 if (TLI.getOperationAction(Node->getOpcode(), VT) == 4033 TargetLowering::Custom) { 4034 Op = TLI.LowerOperation(Op, DAG); 4035 if (Op.Val) { 4036 ExpandOp(Op, Lo, Hi); 4037 break; 4038 } 4039 } 4040 4041 // Expand the subcomponents. 4042 SDOperand LHSL, LHSH, RHSL, RHSH; 4043 ExpandOp(Node->getOperand(0), LHSL, LHSH); 4044 ExpandOp(Node->getOperand(1), RHSL, RHSH); 4045 4046 std::vector<SDOperand> Ops; 4047 Ops.push_back(LHSL); 4048 Ops.push_back(LHSH); 4049 Ops.push_back(RHSL); 4050 Ops.push_back(RHSH); 4051 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType()); 4052 unsigned Opc = 4053 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS; 4054 Lo = DAG.getNode(Opc, VTs, Ops); 4055 Hi = Lo.getValue(1); 4056 break; 4057 } 4058 case ISD::MUL: { 4059 if (TLI.isOperationLegal(ISD::MULHU, NVT)) { 4060 SDOperand LL, LH, RL, RH; 4061 ExpandOp(Node->getOperand(0), LL, LH); 4062 ExpandOp(Node->getOperand(1), RL, RH); 4063 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; 4064 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp 4065 // extended the sign bit of the low half through the upper half, and if so 4066 // emit a MULHS instead of the alternate sequence that is valid for any 4067 // i64 x i64 multiply. 4068 if (TLI.isOperationLegal(ISD::MULHS, NVT) && 4069 // is RH an extension of the sign bit of RL? 4070 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && 4071 RH.getOperand(1).getOpcode() == ISD::Constant && 4072 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && 4073 // is LH an extension of the sign bit of LL? 4074 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && 4075 LH.getOperand(1).getOpcode() == ISD::Constant && 4076 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { 4077 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); 4078 } else { 4079 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); 4080 RH = DAG.getNode(ISD::MUL, NVT, LL, RH); 4081 LH = DAG.getNode(ISD::MUL, NVT, LH, RL); 4082 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); 4083 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); 4084 } 4085 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); 4086 } else { 4087 Lo = ExpandLibCall("__muldi3" , Node, Hi); 4088 } 4089 break; 4090 } 4091 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; 4092 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; 4093 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; 4094 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; 4095 } 4096 4097 // Make sure the resultant values have been legalized themselves, unless this 4098 // is a type that requires multi-step expansion. 4099 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { 4100 Lo = LegalizeOp(Lo); 4101 Hi = LegalizeOp(Hi); 4102 } 4103 4104 // Remember in a map if the values will be reused later. 4105 bool isNew = 4106 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; 4107 assert(isNew && "Value already expanded?!?"); 4108} 4109 4110 4111// SelectionDAG::Legalize - This is the entry point for the file. 4112// 4113void SelectionDAG::Legalize() { 4114 /// run - This is the main entry point to this class. 4115 /// 4116 SelectionDAGLegalize(*this).LegalizeDAG(); 4117} 4118 4119