PPCISelDAGToDAG.cpp revision 303b555164e82139e0f88c183f1fc176fbb2d16c
1//===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines a pattern matching instruction selector for 32 bit PowerPC, 11// converting from a legalized dag to a PPC dag. 12// 13//===----------------------------------------------------------------------===// 14 15#include "PowerPC.h" 16#include "PPC32TargetMachine.h" 17#include "PPC32ISelLowering.h" 18#include "llvm/CodeGen/MachineInstrBuilder.h" 19#include "llvm/CodeGen/MachineFunction.h" 20#include "llvm/CodeGen/SSARegMap.h" 21#include "llvm/CodeGen/SelectionDAG.h" 22#include "llvm/CodeGen/SelectionDAGISel.h" 23#include "llvm/Target/TargetOptions.h" 24#include "llvm/ADT/Statistic.h" 25#include "llvm/Constants.h" 26#include "llvm/GlobalValue.h" 27#include "llvm/Support/Debug.h" 28#include "llvm/Support/MathExtras.h" 29using namespace llvm; 30 31namespace { 32 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations"); 33 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); 34 35 //===--------------------------------------------------------------------===// 36 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine 37 /// instructions for SelectionDAG operations. 38 /// 39 class PPC32DAGToDAGISel : public SelectionDAGISel { 40 PPC32TargetLowering PPC32Lowering; 41 unsigned GlobalBaseReg; 42 public: 43 PPC32DAGToDAGISel(TargetMachine &TM) 44 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {} 45 46 virtual bool runOnFunction(Function &Fn) { 47 // Make sure we re-emit a set of the global base reg if necessary 48 GlobalBaseReg = 0; 49 return SelectionDAGISel::runOnFunction(Fn); 50 } 51 52 /// getI32Imm - Return a target constant with the specified value, of type 53 /// i32. 54 inline SDOperand getI32Imm(unsigned Imm) { 55 return CurDAG->getTargetConstant(Imm, MVT::i32); 56 } 57 58 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC 59 /// base register. Return the virtual register that holds this value. 60 SDOperand getGlobalBaseReg(); 61 62 // Select - Convert the specified operand from a target-independent to a 63 // target-specific node if it hasn't already been changed. 64 SDOperand Select(SDOperand Op); 65 66 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, 67 unsigned OCHi, unsigned OCLo, 68 bool IsArithmetic = false, 69 bool Negate = false); 70 SDNode *SelectBitfieldInsert(SDNode *N); 71 72 /// SelectCC - Select a comparison of the specified values with the 73 /// specified condition code, returning the CR# of the expression. 74 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC); 75 76 /// SelectAddr - Given the specified address, return the two operands for a 77 /// load/store instruction, and return true if it should be an indexed [r+r] 78 /// operation. 79 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2); 80 81 SDOperand BuildSDIVSequence(SDNode *N); 82 SDOperand BuildUDIVSequence(SDNode *N); 83 84 /// InstructionSelectBasicBlock - This callback is invoked by 85 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. 86 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { 87 DEBUG(BB->dump()); 88 // Select target instructions for the DAG. 89 DAG.setRoot(Select(DAG.getRoot())); 90 DAG.RemoveDeadNodes(); 91 92 // Emit machine code to BB. 93 ScheduleAndEmitDAG(DAG); 94 } 95 96 virtual const char *getPassName() const { 97 return "PowerPC DAG->DAG Pattern Instruction Selection"; 98 } 99 100// Include the pieces autogenerated from the target description. 101#include "PPC32GenDAGISel.inc" 102 }; 103} 104 105 106/// getGlobalBaseReg - Output the instructions required to put the 107/// base address to use for accessing globals into a register. 108/// 109SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() { 110 if (!GlobalBaseReg) { 111 // Insert the set of GlobalBaseReg into the first MBB of the function 112 MachineBasicBlock &FirstMBB = BB->getParent()->front(); 113 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 114 SSARegMap *RegMap = BB->getParent()->getSSARegMap(); 115 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass); 116 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); 117 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg); 118 } 119 return CurDAG->getRegister(GlobalBaseReg, MVT::i32); 120} 121 122 123// isIntImmediate - This method tests to see if a constant operand. 124// If so Imm will receive the 32 bit value. 125static bool isIntImmediate(SDNode *N, unsigned& Imm) { 126 if (N->getOpcode() == ISD::Constant) { 127 Imm = cast<ConstantSDNode>(N)->getValue(); 128 return true; 129 } 130 return false; 131} 132 133// isOprShiftImm - Returns true if the specified operand is a shift opcode with 134// a immediate shift count less than 32. 135static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) { 136 Opc = N->getOpcode(); 137 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && 138 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32; 139} 140 141// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with 142// any number of 0s on either side. The 1s are allowed to wrap from LSB to 143// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is 144// not, since all 1s are not contiguous. 145static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { 146 if (isShiftedMask_32(Val)) { 147 // look for the first non-zero bit 148 MB = CountLeadingZeros_32(Val); 149 // look for the first zero bit after the run of ones 150 ME = CountLeadingZeros_32((Val - 1) ^ Val); 151 return true; 152 } else { 153 Val = ~Val; // invert mask 154 if (isShiftedMask_32(Val)) { 155 // effectively look for the first zero bit 156 ME = CountLeadingZeros_32(Val) - 1; 157 // effectively look for the first one bit after the run of zeros 158 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; 159 return true; 160 } 161 } 162 // no run present 163 return false; 164} 165 166// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate 167// and mask opcode and mask operation. 168static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask, 169 unsigned &SH, unsigned &MB, unsigned &ME) { 170 unsigned Shift = 32; 171 unsigned Indeterminant = ~0; // bit mask marking indeterminant results 172 unsigned Opcode = N->getOpcode(); 173 if (N->getNumOperands() != 2 || 174 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31)) 175 return false; 176 177 if (Opcode == ISD::SHL) { 178 // apply shift left to mask if it comes first 179 if (IsShiftMask) Mask = Mask << Shift; 180 // determine which bits are made indeterminant by shift 181 Indeterminant = ~(0xFFFFFFFFu << Shift); 182 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { 183 // apply shift right to mask if it comes first 184 if (IsShiftMask) Mask = Mask >> Shift; 185 // determine which bits are made indeterminant by shift 186 Indeterminant = ~(0xFFFFFFFFu >> Shift); 187 // adjust for the left rotate 188 Shift = 32 - Shift; 189 } else { 190 return false; 191 } 192 193 // if the mask doesn't intersect any Indeterminant bits 194 if (Mask && !(Mask & Indeterminant)) { 195 SH = Shift; 196 // make sure the mask is still a mask (wrap arounds may not be) 197 return isRunOfOnes(Mask, MB, ME); 198 } 199 return false; 200} 201 202// isOpcWithIntImmediate - This method tests to see if the node is a specific 203// opcode and that it has a immediate integer right operand. 204// If so Imm will receive the 32 bit value. 205static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { 206 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm); 207} 208 209// isOprNot - Returns true if the specified operand is an xor with immediate -1. 210static bool isOprNot(SDNode *N) { 211 unsigned Imm; 212 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1; 213} 214 215// Immediate constant composers. 216// Lo16 - grabs the lo 16 bits from a 32 bit constant. 217// Hi16 - grabs the hi 16 bits from a 32 bit constant. 218// HA16 - computes the hi bits required if the lo bits are add/subtracted in 219// arithmethically. 220static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } 221static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } 222static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } 223 224// isIntImmediate - This method tests to see if a constant operand. 225// If so Imm will receive the 32 bit value. 226static bool isIntImmediate(SDOperand N, unsigned& Imm) { 227 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { 228 Imm = (unsigned)CN->getSignExtended(); 229 return true; 230 } 231 return false; 232} 233 234/// SelectBitfieldInsert - turn an or of two masked values into 235/// the rotate left word immediate then mask insert (rlwimi) instruction. 236/// Returns true on success, false if the caller still needs to select OR. 237/// 238/// Patterns matched: 239/// 1. or shl, and 5. or and, and 240/// 2. or and, shl 6. or shl, shr 241/// 3. or shr, and 7. or shr, shl 242/// 4. or and, shr 243SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) { 244 bool IsRotate = false; 245 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0; 246 unsigned Value; 247 248 SDOperand Op0 = N->getOperand(0); 249 SDOperand Op1 = N->getOperand(1); 250 251 unsigned Op0Opc = Op0.getOpcode(); 252 unsigned Op1Opc = Op1.getOpcode(); 253 254 // Verify that we have the correct opcodes 255 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) 256 return false; 257 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) 258 return false; 259 260 // Generate Mask value for Target 261 if (isIntImmediate(Op0.getOperand(1), Value)) { 262 switch(Op0Opc) { 263 case ISD::SHL: TgtMask <<= Value; break; 264 case ISD::SRL: TgtMask >>= Value; break; 265 case ISD::AND: TgtMask &= Value; break; 266 } 267 } else { 268 return 0; 269 } 270 271 // Generate Mask value for Insert 272 if (!isIntImmediate(Op1.getOperand(1), Value)) 273 return 0; 274 275 switch(Op1Opc) { 276 case ISD::SHL: 277 SH = Value; 278 InsMask <<= SH; 279 if (Op0Opc == ISD::SRL) IsRotate = true; 280 break; 281 case ISD::SRL: 282 SH = Value; 283 InsMask >>= SH; 284 SH = 32-SH; 285 if (Op0Opc == ISD::SHL) IsRotate = true; 286 break; 287 case ISD::AND: 288 InsMask &= Value; 289 break; 290 } 291 292 // If both of the inputs are ANDs and one of them has a logical shift by 293 // constant as its input, make that AND the inserted value so that we can 294 // combine the shift into the rotate part of the rlwimi instruction 295 bool IsAndWithShiftOp = false; 296 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { 297 if (Op1.getOperand(0).getOpcode() == ISD::SHL || 298 Op1.getOperand(0).getOpcode() == ISD::SRL) { 299 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) { 300 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; 301 IsAndWithShiftOp = true; 302 } 303 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || 304 Op0.getOperand(0).getOpcode() == ISD::SRL) { 305 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) { 306 std::swap(Op0, Op1); 307 std::swap(TgtMask, InsMask); 308 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; 309 IsAndWithShiftOp = true; 310 } 311 } 312 } 313 314 // Verify that the Target mask and Insert mask together form a full word mask 315 // and that the Insert mask is a run of set bits (which implies both are runs 316 // of set bits). Given that, Select the arguments and generate the rlwimi 317 // instruction. 318 unsigned MB, ME; 319 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) { 320 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; 321 bool Op0IsAND = Op0Opc == ISD::AND; 322 // Check for rotlwi / rotrwi here, a special case of bitfield insert 323 // where both bitfield halves are sourced from the same value. 324 if (IsRotate && fullMask && 325 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) { 326 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, 327 Select(N->getOperand(0).getOperand(0)), 328 getI32Imm(SH), getI32Imm(0), getI32Imm(31)); 329 return Op0.Val; 330 } 331 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0)) 332 : Select(Op0); 333 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0)) 334 : Select(Op1.getOperand(0)); 335 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2, 336 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); 337 return Op0.Val; 338 } 339 return 0; 340} 341 342// SelectIntImmediateExpr - Choose code for integer operations with an immediate 343// operand. 344SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, 345 unsigned OCHi, unsigned OCLo, 346 bool IsArithmetic, 347 bool Negate) { 348 // Check to make sure this is a constant. 349 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS); 350 // Exit if not a constant. 351 if (!CN) return 0; 352 // Extract immediate. 353 unsigned C = (unsigned)CN->getValue(); 354 // Negate if required (ISD::SUB). 355 if (Negate) C = -C; 356 // Get the hi and lo portions of constant. 357 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); 358 unsigned Lo = Lo16(C); 359 360 // If two instructions are needed and usage indicates it would be better to 361 // load immediate into a register, bail out. 362 if (Hi && Lo && CN->use_size() > 2) return false; 363 364 // Select the first operand. 365 SDOperand Opr0 = Select(LHS); 366 367 if (Lo) // Add in the lo-part. 368 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo)); 369 if (Hi) // Add in the hi-part. 370 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi)); 371 return Opr0.Val; 372} 373 374/// SelectAddr - Given the specified address, return the two operands for a 375/// load/store instruction, and return true if it should be an indexed [r+r] 376/// operation. 377bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1, 378 SDOperand &Op2) { 379 unsigned imm = 0; 380 if (Addr.getOpcode() == ISD::ADD) { 381 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) { 382 Op1 = getI32Imm(Lo16(imm)); 383 if (FrameIndexSDNode *FI = 384 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) { 385 ++FrameOff; 386 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32); 387 } else { 388 Op2 = Select(Addr.getOperand(0)); 389 } 390 return false; 391 } else { 392 Op1 = Select(Addr.getOperand(0)); 393 Op2 = Select(Addr.getOperand(1)); 394 return true; // [r+r] 395 } 396 } 397 398 // Now check if we're dealing with a global, and whether or not we should emit 399 // an optimized load or store for statics. 400 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) { 401 GlobalValue *GV = GN->getGlobal(); 402 if (!GV->hasWeakLinkage() && !GV->isExternal()) { 403 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32); 404 if (PICEnabled) 405 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), 406 Op1); 407 else 408 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1); 409 return false; 410 } 411 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) { 412 Op1 = getI32Imm(0); 413 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32); 414 return false; 415 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) { 416 Op1 = Addr; 417 if (PICEnabled) 418 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1); 419 else 420 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1); 421 return false; 422 } 423 Op1 = getI32Imm(0); 424 Op2 = Select(Addr); 425 return false; 426} 427 428/// SelectCC - Select a comparison of the specified values with the specified 429/// condition code, returning the CR# of the expression. 430SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS, 431 ISD::CondCode CC) { 432 // Always select the LHS. 433 LHS = Select(LHS); 434 435 // Use U to determine whether the SETCC immediate range is signed or not. 436 if (MVT::isInteger(LHS.getValueType())) { 437 bool U = ISD::isUnsignedIntSetCC(CC); 438 unsigned Imm; 439 if (isIntImmediate(RHS, Imm) && 440 ((U && isUInt16(Imm)) || (!U && isInt16(Imm)))) 441 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32, 442 LHS, getI32Imm(Lo16(Imm))); 443 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32, 444 LHS, Select(RHS)); 445 } else { 446 return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS)); 447 } 448} 449 450/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding 451/// to Condition. 452static unsigned getBCCForSetCC(ISD::CondCode CC) { 453 switch (CC) { 454 default: assert(0 && "Unknown condition!"); abort(); 455 case ISD::SETEQ: return PPC::BEQ; 456 case ISD::SETNE: return PPC::BNE; 457 case ISD::SETULT: 458 case ISD::SETLT: return PPC::BLT; 459 case ISD::SETULE: 460 case ISD::SETLE: return PPC::BLE; 461 case ISD::SETUGT: 462 case ISD::SETGT: return PPC::BGT; 463 case ISD::SETUGE: 464 case ISD::SETGE: return PPC::BGE; 465 } 466 return 0; 467} 468 469/// getCRIdxForSetCC - Return the index of the condition register field 470/// associated with the SetCC condition, and whether or not the field is 471/// treated as inverted. That is, lt = 0; ge = 0 inverted. 472static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) { 473 switch (CC) { 474 default: assert(0 && "Unknown condition!"); abort(); 475 case ISD::SETULT: 476 case ISD::SETLT: Inv = false; return 0; 477 case ISD::SETUGE: 478 case ISD::SETGE: Inv = true; return 0; 479 case ISD::SETUGT: 480 case ISD::SETGT: Inv = false; return 1; 481 case ISD::SETULE: 482 case ISD::SETLE: Inv = true; return 1; 483 case ISD::SETEQ: Inv = false; return 2; 484 case ISD::SETNE: Inv = true; return 2; 485 } 486 return 0; 487} 488 489// Structure used to return the necessary information to codegen an SDIV as 490// a multiply. 491struct ms { 492 int m; // magic number 493 int s; // shift amount 494}; 495 496struct mu { 497 unsigned int m; // magic number 498 int a; // add indicator 499 int s; // shift amount 500}; 501 502/// magic - calculate the magic numbers required to codegen an integer sdiv as 503/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, 504/// or -1. 505static struct ms magic(int d) { 506 int p; 507 unsigned int ad, anc, delta, q1, r1, q2, r2, t; 508 const unsigned int two31 = 0x80000000U; 509 struct ms mag; 510 511 ad = abs(d); 512 t = two31 + ((unsigned int)d >> 31); 513 anc = t - 1 - t%ad; // absolute value of nc 514 p = 31; // initialize p 515 q1 = two31/anc; // initialize q1 = 2p/abs(nc) 516 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) 517 q2 = two31/ad; // initialize q2 = 2p/abs(d) 518 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) 519 do { 520 p = p + 1; 521 q1 = 2*q1; // update q1 = 2p/abs(nc) 522 r1 = 2*r1; // update r1 = rem(2p/abs(nc)) 523 if (r1 >= anc) { // must be unsigned comparison 524 q1 = q1 + 1; 525 r1 = r1 - anc; 526 } 527 q2 = 2*q2; // update q2 = 2p/abs(d) 528 r2 = 2*r2; // update r2 = rem(2p/abs(d)) 529 if (r2 >= ad) { // must be unsigned comparison 530 q2 = q2 + 1; 531 r2 = r2 - ad; 532 } 533 delta = ad - r2; 534 } while (q1 < delta || (q1 == delta && r1 == 0)); 535 536 mag.m = q2 + 1; 537 if (d < 0) mag.m = -mag.m; // resulting magic number 538 mag.s = p - 32; // resulting shift 539 return mag; 540} 541 542/// magicu - calculate the magic numbers required to codegen an integer udiv as 543/// a sequence of multiply, add and shifts. Requires that the divisor not be 0. 544static struct mu magicu(unsigned d) 545{ 546 int p; 547 unsigned int nc, delta, q1, r1, q2, r2; 548 struct mu magu; 549 magu.a = 0; // initialize "add" indicator 550 nc = - 1 - (-d)%d; 551 p = 31; // initialize p 552 q1 = 0x80000000/nc; // initialize q1 = 2p/nc 553 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) 554 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d 555 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) 556 do { 557 p = p + 1; 558 if (r1 >= nc - r1 ) { 559 q1 = 2*q1 + 1; // update q1 560 r1 = 2*r1 - nc; // update r1 561 } 562 else { 563 q1 = 2*q1; // update q1 564 r1 = 2*r1; // update r1 565 } 566 if (r2 + 1 >= d - r2) { 567 if (q2 >= 0x7FFFFFFF) magu.a = 1; 568 q2 = 2*q2 + 1; // update q2 569 r2 = 2*r2 + 1 - d; // update r2 570 } 571 else { 572 if (q2 >= 0x80000000) magu.a = 1; 573 q2 = 2*q2; // update q2 574 r2 = 2*r2 + 1; // update r2 575 } 576 delta = d - 1 - r2; 577 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); 578 magu.m = q2 + 1; // resulting magic number 579 magu.s = p - 32; // resulting shift 580 return magu; 581} 582 583/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, 584/// return a DAG expression to select that will generate the same value by 585/// multiplying by a magic number. See: 586/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> 587SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) { 588 int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue(); 589 ms magics = magic(d); 590 // Multiply the numerator (operand 0) by the magic value 591 SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0), 592 CurDAG->getConstant(magics.m, MVT::i32)); 593 // If d > 0 and m < 0, add the numerator 594 if (d > 0 && magics.m < 0) 595 Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0)); 596 // If d < 0 and m > 0, subtract the numerator. 597 if (d < 0 && magics.m > 0) 598 Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0)); 599 // Shift right algebraic if shift value is nonzero 600 if (magics.s > 0) 601 Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q, 602 CurDAG->getConstant(magics.s, MVT::i32)); 603 // Extract the sign bit and add it to the quotient 604 SDOperand T = 605 CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32)); 606 return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T); 607} 608 609/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, 610/// return a DAG expression to select that will generate the same value by 611/// multiplying by a magic number. See: 612/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> 613SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) { 614 unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue(); 615 mu magics = magicu(d); 616 // Multiply the numerator (operand 0) by the magic value 617 SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0), 618 CurDAG->getConstant(magics.m, MVT::i32)); 619 if (magics.a == 0) { 620 return CurDAG->getNode(ISD::SRL, MVT::i32, Q, 621 CurDAG->getConstant(magics.s, MVT::i32)); 622 } else { 623 SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q); 624 NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ, 625 CurDAG->getConstant(1, MVT::i32)); 626 NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); 627 return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ, 628 CurDAG->getConstant(magics.s-1, MVT::i32)); 629 } 630} 631 632// Select - Convert the specified operand from a target-independent to a 633// target-specific node if it hasn't already been changed. 634SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) { 635 SDNode *N = Op.Val; 636 if (N->getOpcode() >= ISD::BUILTIN_OP_END && 637 N->getOpcode() < PPCISD::FIRST_NUMBER) 638 return Op; // Already selected. 639 640 switch (N->getOpcode()) { 641 default: break; 642 case ISD::TokenFactor: { 643 SDOperand New; 644 if (N->getNumOperands() == 2) { 645 SDOperand Op0 = Select(N->getOperand(0)); 646 SDOperand Op1 = Select(N->getOperand(1)); 647 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1); 648 } else { 649 std::vector<SDOperand> Ops; 650 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 651 Ops.push_back(Select(N->getOperand(i))); 652 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops); 653 } 654 655 if (New.Val != N) { 656 CurDAG->ReplaceAllUsesWith(Op, New); 657 N = New.Val; 658 } 659 return SDOperand(N, 0); 660 } 661 case ISD::CopyFromReg: { 662 SDOperand Chain = Select(N->getOperand(0)); 663 if (Chain == N->getOperand(0)) return Op; // No change 664 SDOperand New = CurDAG->getCopyFromReg(Chain, 665 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0)); 666 return New.getValue(Op.ResNo); 667 } 668 case ISD::CopyToReg: { 669 SDOperand Chain = Select(N->getOperand(0)); 670 SDOperand Reg = N->getOperand(1); 671 SDOperand Val = Select(N->getOperand(2)); 672 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) { 673 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other, 674 Chain, Reg, Val); 675 CurDAG->ReplaceAllUsesWith(Op, New); 676 N = New.Val; 677 } 678 return SDOperand(N, 0); 679 } 680 case ISD::Constant: { 681 assert(N->getValueType(0) == MVT::i32); 682 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue(); 683 684 // NOTE: This doesn't use SelectNodeTo, because doing that will prevent 685 // folding shared immediates into other the second instruction that 686 // uses it. 687 if (isInt16(v)) 688 return CurDAG->getTargetNode(PPC::LI, MVT::i32, getI32Imm(v)); 689 690 unsigned Hi = Hi16(v); 691 unsigned Lo = Lo16(v); 692 693 if (!Lo) 694 return CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(Hi)); 695 696 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(Hi)); 697 return CurDAG->getTargetNode(PPC::ORI, MVT::i32, Top, getI32Imm(Lo)); 698 } 699 case ISD::UNDEF: 700 if (N->getValueType(0) == MVT::i32) 701 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32); 702 else 703 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_FP, N->getValueType(0)); 704 return SDOperand(N, 0); 705 case ISD::FrameIndex: { 706 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 707 CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32, 708 CurDAG->getTargetFrameIndex(FI, MVT::i32), 709 getI32Imm(0)); 710 return SDOperand(N, 0); 711 } 712 case ISD::ConstantPool: { 713 Constant *C = cast<ConstantPoolSDNode>(N)->get(); 714 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32); 715 if (PICEnabled) 716 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI); 717 else 718 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI); 719 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI); 720 return SDOperand(N, 0); 721 } 722 case ISD::GlobalAddress: { 723 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); 724 SDOperand Tmp; 725 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32); 726 if (PICEnabled) 727 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA); 728 else 729 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA); 730 731 if (GV->hasWeakLinkage() || GV->isExternal()) 732 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp); 733 else 734 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA); 735 return SDOperand(N, 0); 736 } 737 case ISD::DYNAMIC_STACKALLOC: { 738 // FIXME: We are currently ignoring the requested alignment for handling 739 // greater than the stack alignment. This will need to be revisited at some 740 // point. Align = N.getOperand(2); 741 if (!isa<ConstantSDNode>(N->getOperand(2)) || 742 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) { 743 std::cerr << "Cannot allocate stack object with greater alignment than" 744 << " the stack alignment yet!"; 745 abort(); 746 } 747 SDOperand Chain = Select(N->getOperand(0)); 748 SDOperand Amt = Select(N->getOperand(1)); 749 750 SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32); 751 752 SDOperand R1Val = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32); 753 Chain = R1Val.getValue(1); 754 755 // Subtract the amount (guaranteed to be a multiple of the stack alignment) 756 // from the stack pointer, giving us the result pointer. 757 SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Val); 758 759 // Copy this result back into R1. 760 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result); 761 762 // Copy this result back out of R1 to make sure we're not using the stack 763 // space without decrementing the stack pointer. 764 Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32); 765 766 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg. 767 CurDAG->ReplaceAllUsesWith(N, Result.Val); 768 return SDOperand(Result.Val, Op.ResNo); 769 } 770 case ISD::SIGN_EXTEND_INREG: 771 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) { 772 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break; 773 case MVT::i16: 774 CurDAG->SelectNodeTo(N, PPC::EXTSH, MVT::i32, Select(N->getOperand(0))); 775 break; 776 case MVT::i8: 777 CurDAG->SelectNodeTo(N, PPC::EXTSB, MVT::i32, Select(N->getOperand(0))); 778 break; 779 } 780 return SDOperand(N, 0); 781 case ISD::CTLZ: 782 assert(N->getValueType(0) == MVT::i32); 783 CurDAG->SelectNodeTo(N, PPC::CNTLZW, MVT::i32, Select(N->getOperand(0))); 784 return SDOperand(N, 0); 785 case PPCISD::FSEL: 786 CurDAG->SelectNodeTo(N, PPC::FSEL, N->getValueType(0), 787 Select(N->getOperand(0)), 788 Select(N->getOperand(1)), 789 Select(N->getOperand(2))); 790 return SDOperand(N, 0); 791 case PPCISD::FCFID: 792 CurDAG->SelectNodeTo(N, PPC::FCFID, N->getValueType(0), 793 Select(N->getOperand(0))); 794 return SDOperand(N, 0); 795 case PPCISD::FCTIDZ: 796 CurDAG->SelectNodeTo(N, PPC::FCTIDZ, N->getValueType(0), 797 Select(N->getOperand(0))); 798 return SDOperand(N, 0); 799 case PPCISD::FCTIWZ: 800 CurDAG->SelectNodeTo(N, PPC::FCTIWZ, N->getValueType(0), 801 Select(N->getOperand(0))); 802 return SDOperand(N, 0); 803 case ISD::ADD: { 804 MVT::ValueType Ty = N->getValueType(0); 805 if (Ty == MVT::i32) { 806 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), 807 PPC::ADDIS, PPC::ADDI, true)) { 808 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); 809 N = I; 810 } else { 811 CurDAG->SelectNodeTo(N, PPC::ADD, MVT::i32, Select(N->getOperand(0)), 812 Select(N->getOperand(1))); 813 } 814 return SDOperand(N, 0); 815 } 816 817 if (!NoExcessFPPrecision) { // Match FMA ops 818 if (N->getOperand(0).getOpcode() == ISD::MUL && 819 N->getOperand(0).Val->hasOneUse()) { 820 ++FusedFP; // Statistic 821 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty, 822 Select(N->getOperand(0).getOperand(0)), 823 Select(N->getOperand(0).getOperand(1)), 824 Select(N->getOperand(1))); 825 return SDOperand(N, 0); 826 } else if (N->getOperand(1).getOpcode() == ISD::MUL && 827 N->getOperand(1).hasOneUse()) { 828 ++FusedFP; // Statistic 829 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty, 830 Select(N->getOperand(1).getOperand(0)), 831 Select(N->getOperand(1).getOperand(1)), 832 Select(N->getOperand(0))); 833 return SDOperand(N, 0); 834 } 835 } 836 837 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty, 838 Select(N->getOperand(0)), Select(N->getOperand(1))); 839 return SDOperand(N, 0); 840 } 841 case ISD::SUB: { 842 MVT::ValueType Ty = N->getValueType(0); 843 if (Ty == MVT::i32) { 844 unsigned Imm; 845 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) { 846 if (0 == Imm) 847 CurDAG->SelectNodeTo(N, PPC::NEG, Ty, Select(N->getOperand(1))); 848 else 849 CurDAG->SelectNodeTo(N, PPC::SUBFIC, Ty, Select(N->getOperand(1)), 850 getI32Imm(Lo16(Imm))); 851 return SDOperand(N, 0); 852 } 853 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), 854 PPC::ADDIS, PPC::ADDI, true, true)) { 855 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); 856 N = I; 857 } else { 858 CurDAG->SelectNodeTo(N, PPC::SUBF, Ty, Select(N->getOperand(1)), 859 Select(N->getOperand(0))); 860 } 861 return SDOperand(N, 0); 862 } 863 864 if (!NoExcessFPPrecision) { // Match FMA ops 865 if (N->getOperand(0).getOpcode() == ISD::MUL && 866 N->getOperand(0).Val->hasOneUse()) { 867 ++FusedFP; // Statistic 868 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty, 869 Select(N->getOperand(0).getOperand(0)), 870 Select(N->getOperand(0).getOperand(1)), 871 Select(N->getOperand(1))); 872 return SDOperand(N, 0); 873 } else if (N->getOperand(1).getOpcode() == ISD::MUL && 874 N->getOperand(1).Val->hasOneUse()) { 875 ++FusedFP; // Statistic 876 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty, 877 Select(N->getOperand(1).getOperand(0)), 878 Select(N->getOperand(1).getOperand(1)), 879 Select(N->getOperand(0))); 880 return SDOperand(N, 0); 881 } 882 } 883 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty, 884 Select(N->getOperand(0)), 885 Select(N->getOperand(1))); 886 return SDOperand(N, 0); 887 } 888 case ISD::MUL: { 889 unsigned Imm, Opc; 890 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) { 891 CurDAG->SelectNodeTo(N, PPC::MULLI, MVT::i32, 892 Select(N->getOperand(0)), getI32Imm(Lo16(Imm))); 893 return SDOperand(N, 0); 894 } 895 switch (N->getValueType(0)) { 896 default: assert(0 && "Unhandled multiply type!"); 897 case MVT::i32: Opc = PPC::MULLW; break; 898 case MVT::f32: Opc = PPC::FMULS; break; 899 case MVT::f64: Opc = PPC::FMUL; break; 900 } 901 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)), 902 Select(N->getOperand(1))); 903 return SDOperand(N, 0); 904 } 905 case ISD::SDIV: { 906 unsigned Imm; 907 if (isIntImmediate(N->getOperand(1), Imm)) { 908 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { 909 SDOperand Op = 910 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, 911 Select(N->getOperand(0)), 912 getI32Imm(Log2_32(Imm))); 913 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 914 Op.getValue(0), Op.getValue(1)); 915 return SDOperand(N, 0); 916 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { 917 SDOperand Op = 918 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, 919 Select(N->getOperand(0)), 920 getI32Imm(Log2_32(-Imm))); 921 SDOperand PT = 922 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0), 923 Op.getValue(1)); 924 CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT); 925 return SDOperand(N, 0); 926 } else if (Imm) { 927 SDOperand Result = Select(BuildSDIVSequence(N)); 928 assert(Result.ResNo == 0); 929 CurDAG->ReplaceAllUsesWith(Op, Result); 930 N = Result.Val; 931 return SDOperand(N, 0); 932 } 933 } 934 935 unsigned Opc; 936 switch (N->getValueType(0)) { 937 default: assert(0 && "Unknown type to ISD::SDIV"); 938 case MVT::i32: Opc = PPC::DIVW; break; 939 case MVT::f32: Opc = PPC::FDIVS; break; 940 case MVT::f64: Opc = PPC::FDIV; break; 941 } 942 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)), 943 Select(N->getOperand(1))); 944 return SDOperand(N, 0); 945 } 946 case ISD::UDIV: { 947 // If this is a divide by constant, we can emit code using some magic 948 // constants to implement it as a multiply instead. 949 unsigned Imm; 950 if (isIntImmediate(N->getOperand(1), Imm) && Imm) { 951 SDOperand Result = Select(BuildUDIVSequence(N)); 952 assert(Result.ResNo == 0); 953 CurDAG->ReplaceAllUsesWith(Op, Result); 954 N = Result.Val; 955 return SDOperand(N, 0); 956 } 957 958 CurDAG->SelectNodeTo(N, PPC::DIVWU, MVT::i32, Select(N->getOperand(0)), 959 Select(N->getOperand(1))); 960 return SDOperand(N, 0); 961 } 962 case ISD::MULHS: 963 assert(N->getValueType(0) == MVT::i32); 964 CurDAG->SelectNodeTo(N, PPC::MULHW, MVT::i32, Select(N->getOperand(0)), 965 Select(N->getOperand(1))); 966 return SDOperand(N, 0); 967 case ISD::MULHU: 968 assert(N->getValueType(0) == MVT::i32); 969 CurDAG->SelectNodeTo(N, PPC::MULHWU, MVT::i32, Select(N->getOperand(0)), 970 Select(N->getOperand(1))); 971 return SDOperand(N, 0); 972 case ISD::AND: { 973 unsigned Imm; 974 // If this is an and of a value rotated between 0 and 31 bits and then and'd 975 // with a mask, emit rlwinm 976 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) || 977 isShiftedMask_32(~Imm))) { 978 SDOperand Val; 979 unsigned SH, MB, ME; 980 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) { 981 Val = Select(N->getOperand(0).getOperand(0)); 982 } else { 983 Val = Select(N->getOperand(0)); 984 isRunOfOnes(Imm, MB, ME); 985 SH = 0; 986 } 987 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH), 988 getI32Imm(MB), getI32Imm(ME)); 989 return SDOperand(N, 0); 990 } 991 // Finally, check for the case where we are being asked to select 992 // and (not(a), b) or and (a, not(b)) which can be selected as andc. 993 if (isOprNot(N->getOperand(0).Val)) 994 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(1)), 995 Select(N->getOperand(0).getOperand(0))); 996 else if (isOprNot(N->getOperand(1).Val)) 997 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(0)), 998 Select(N->getOperand(1).getOperand(0))); 999 else 1000 CurDAG->SelectNodeTo(N, PPC::AND, MVT::i32, Select(N->getOperand(0)), 1001 Select(N->getOperand(1))); 1002 return SDOperand(N, 0); 1003 } 1004 case ISD::OR: 1005 if (SDNode *I = SelectBitfieldInsert(N)) { 1006 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); 1007 N = I; 1008 return SDOperand(N, 0); 1009 } 1010 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), 1011 N->getOperand(1), 1012 PPC::ORIS, PPC::ORI)) { 1013 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); 1014 N = I; 1015 return SDOperand(N, 0); 1016 } 1017 // Finally, check for the case where we are being asked to select 1018 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc. 1019 if (isOprNot(N->getOperand(0).Val)) 1020 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(1)), 1021 Select(N->getOperand(0).getOperand(0))); 1022 else if (isOprNot(N->getOperand(1).Val)) 1023 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(0)), 1024 Select(N->getOperand(1).getOperand(0))); 1025 else 1026 CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Select(N->getOperand(0)), 1027 Select(N->getOperand(1))); 1028 return SDOperand(N, 0); 1029 case ISD::XOR: 1030 // Check whether or not this node is a logical 'not'. This is represented 1031 // by llvm as a xor with the constant value -1 (all bits set). If this is a 1032 // 'not', then fold 'or' into 'nor', and so forth for the supported ops. 1033 if (isOprNot(N)) { 1034 unsigned Opc; 1035 SDOperand Val = Select(N->getOperand(0)); 1036 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) { 1037 default: Opc = 0; break; 1038 case PPC::OR: Opc = PPC::NOR; break; 1039 case PPC::AND: Opc = PPC::NAND; break; 1040 case PPC::XOR: Opc = PPC::EQV; break; 1041 } 1042 if (Opc) 1043 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Val.getOperand(0), 1044 Val.getOperand(1)); 1045 else 1046 CurDAG->SelectNodeTo(N, PPC::NOR, MVT::i32, Val, Val); 1047 return SDOperand(N, 0); 1048 } 1049 // If this is a xor with an immediate other than -1, then codegen it as high 1050 // and low 16 bit immediate xors. 1051 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), 1052 N->getOperand(1), 1053 PPC::XORIS, PPC::XORI)) { 1054 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); 1055 N = I; 1056 return SDOperand(N, 0); 1057 } 1058 // Finally, check for the case where we are being asked to select 1059 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv 1060 if (isOprNot(N->getOperand(0).Val)) 1061 CurDAG->SelectNodeTo(N, PPC::EQV, MVT::i32, 1062 Select(N->getOperand(0).getOperand(0)), 1063 Select(N->getOperand(1))); 1064 else 1065 CurDAG->SelectNodeTo(N, PPC::XOR, MVT::i32, Select(N->getOperand(0)), 1066 Select(N->getOperand(1))); 1067 return SDOperand(N, 0); 1068 case ISD::SHL: { 1069 unsigned Imm, SH, MB, ME; 1070 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && 1071 isRotateAndMask(N, Imm, true, SH, MB, ME)) 1072 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, 1073 Select(N->getOperand(0).getOperand(0)), 1074 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); 1075 else if (isIntImmediate(N->getOperand(1), Imm)) 1076 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)), 1077 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm)); 1078 else 1079 CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)), 1080 Select(N->getOperand(1))); 1081 return SDOperand(N, 0); 1082 } 1083 case ISD::SRL: { 1084 unsigned Imm, SH, MB, ME; 1085 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && 1086 isRotateAndMask(N, Imm, true, SH, MB, ME)) 1087 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, 1088 Select(N->getOperand(0).getOperand(0)), 1089 getI32Imm(SH & 0x1F), getI32Imm(MB), getI32Imm(ME)); 1090 else if (isIntImmediate(N->getOperand(1), Imm)) 1091 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)), 1092 getI32Imm((32-Imm) & 0x1F), getI32Imm(Imm), 1093 getI32Imm(31)); 1094 else 1095 CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)), 1096 Select(N->getOperand(1))); 1097 return SDOperand(N, 0); 1098 } 1099 case ISD::SRA: { 1100 unsigned Imm, SH, MB, ME; 1101 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && 1102 isRotateAndMask(N, Imm, true, SH, MB, ME)) 1103 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, 1104 Select(N->getOperand(0).getOperand(0)), 1105 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); 1106 else if (isIntImmediate(N->getOperand(1), Imm)) 1107 CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)), 1108 getI32Imm(Imm)); 1109 else 1110 CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)), 1111 Select(N->getOperand(1))); 1112 return SDOperand(N, 0); 1113 } 1114 case ISD::FABS: 1115 CurDAG->SelectNodeTo(N, PPC::FABS, N->getValueType(0), 1116 Select(N->getOperand(0))); 1117 return SDOperand(N, 0); 1118 case ISD::FP_EXTEND: 1119 assert(MVT::f64 == N->getValueType(0) && 1120 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND"); 1121 // We need to emit an FMR to make sure that the result has the right value 1122 // type. 1123 CurDAG->SelectNodeTo(N, PPC::FMR, MVT::f64, Select(N->getOperand(0))); 1124 return SDOperand(N, 0); 1125 case ISD::FP_ROUND: 1126 assert(MVT::f32 == N->getValueType(0) && 1127 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND"); 1128 CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0))); 1129 return SDOperand(N, 0); 1130 case ISD::FNEG: { 1131 SDOperand Val = Select(N->getOperand(0)); 1132 MVT::ValueType Ty = N->getValueType(0); 1133 if (Val.Val->hasOneUse()) { 1134 unsigned Opc; 1135 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) { 1136 default: Opc = 0; break; 1137 case PPC::FABS: Opc = PPC::FNABS; break; 1138 case PPC::FMADD: Opc = PPC::FNMADD; break; 1139 case PPC::FMADDS: Opc = PPC::FNMADDS; break; 1140 case PPC::FMSUB: Opc = PPC::FNMSUB; break; 1141 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break; 1142 } 1143 // If we inverted the opcode, then emit the new instruction with the 1144 // inverted opcode and the original instruction's operands. Otherwise, 1145 // fall through and generate a fneg instruction. 1146 if (Opc) { 1147 if (PPC::FNABS == Opc) 1148 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0)); 1149 else 1150 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0), 1151 Val.getOperand(1), Val.getOperand(2)); 1152 return SDOperand(N, 0); 1153 } 1154 } 1155 CurDAG->SelectNodeTo(N, PPC::FNEG, Ty, Val); 1156 return SDOperand(N, 0); 1157 } 1158 case ISD::FSQRT: { 1159 MVT::ValueType Ty = N->getValueType(0); 1160 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty, 1161 Select(N->getOperand(0))); 1162 return SDOperand(N, 0); 1163 } 1164 1165 case ISD::ADD_PARTS: { 1166 SDOperand LHSL = Select(N->getOperand(0)); 1167 SDOperand LHSH = Select(N->getOperand(1)); 1168 1169 unsigned Imm; 1170 bool ME = false, ZE = false; 1171 if (isIntImmediate(N->getOperand(3), Imm)) { 1172 ME = (signed)Imm == -1; 1173 ZE = Imm == 0; 1174 } 1175 1176 std::vector<SDOperand> Result; 1177 SDOperand CarryFromLo; 1178 if (isIntImmediate(N->getOperand(2), Imm) && 1179 ((signed)Imm >= -32768 || (signed)Imm < 32768)) { 1180 // Codegen the low 32 bits of the add. Interestingly, there is no 1181 // shifted form of add immediate carrying. 1182 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 1183 LHSL, getI32Imm(Imm)); 1184 } else { 1185 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag, 1186 LHSL, Select(N->getOperand(2))); 1187 } 1188 CarryFromLo = CarryFromLo.getValue(1); 1189 1190 // Codegen the high 32 bits, adding zero, minus one, or the full value 1191 // along with the carry flag produced by addc/addic. 1192 SDOperand ResultHi; 1193 if (ZE) 1194 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo); 1195 else if (ME) 1196 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo); 1197 else 1198 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH, 1199 Select(N->getOperand(3)), CarryFromLo); 1200 Result.push_back(CarryFromLo.getValue(0)); 1201 Result.push_back(ResultHi); 1202 CurDAG->ReplaceAllUsesWith(N, Result); 1203 return Result[Op.ResNo]; 1204 } 1205 case ISD::SUB_PARTS: { 1206 SDOperand LHSL = Select(N->getOperand(0)); 1207 SDOperand LHSH = Select(N->getOperand(1)); 1208 SDOperand RHSL = Select(N->getOperand(2)); 1209 SDOperand RHSH = Select(N->getOperand(3)); 1210 1211 std::vector<SDOperand> Result; 1212 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag, 1213 RHSL, LHSL)); 1214 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH, 1215 Result[0].getValue(1))); 1216 CurDAG->ReplaceAllUsesWith(N, Result); 1217 return Result[Op.ResNo]; 1218 } 1219 1220 case ISD::LOAD: 1221 case ISD::EXTLOAD: 1222 case ISD::ZEXTLOAD: 1223 case ISD::SEXTLOAD: { 1224 SDOperand Op1, Op2; 1225 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2); 1226 1227 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ? 1228 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT(); 1229 unsigned Opc; 1230 switch (TypeBeingLoaded) { 1231 default: N->dump(); assert(0 && "Cannot load this type!"); 1232 case MVT::i1: 1233 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break; 1234 case MVT::i16: 1235 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load? 1236 Opc = isIdx ? PPC::LHAX : PPC::LHA; 1237 } else { 1238 Opc = isIdx ? PPC::LHZX : PPC::LHZ; 1239 } 1240 break; 1241 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break; 1242 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break; 1243 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break; 1244 } 1245 1246 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other, 1247 Op1, Op2, Select(N->getOperand(0))); 1248 return SDOperand(N, Op.ResNo); 1249 } 1250 1251 case ISD::TRUNCSTORE: 1252 case ISD::STORE: { 1253 SDOperand AddrOp1, AddrOp2; 1254 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2); 1255 1256 unsigned Opc; 1257 if (N->getOpcode() == ISD::STORE) { 1258 switch (N->getOperand(1).getValueType()) { 1259 default: assert(0 && "unknown Type in store"); 1260 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break; 1261 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break; 1262 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break; 1263 } 1264 } else { //ISD::TRUNCSTORE 1265 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) { 1266 default: assert(0 && "unknown Type in store"); 1267 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break; 1268 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break; 1269 } 1270 } 1271 1272 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)), 1273 AddrOp1, AddrOp2, Select(N->getOperand(0))); 1274 return SDOperand(N, 0); 1275 } 1276 1277 case ISD::SETCC: { 1278 unsigned Imm; 1279 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 1280 if (isIntImmediate(N->getOperand(1), Imm)) { 1281 // We can codegen setcc op, imm very efficiently compared to a brcond. 1282 // Check for those cases here. 1283 // setcc op, 0 1284 if (Imm == 0) { 1285 SDOperand Op = Select(N->getOperand(0)); 1286 switch (CC) { 1287 default: assert(0 && "Unhandled SetCC condition"); abort(); 1288 case ISD::SETEQ: 1289 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op); 1290 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27), 1291 getI32Imm(5), getI32Imm(31)); 1292 break; 1293 case ISD::SETNE: { 1294 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 1295 Op, getI32Imm(~0U)); 1296 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); 1297 break; 1298 } 1299 case ISD::SETLT: 1300 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1), 1301 getI32Imm(31), getI32Imm(31)); 1302 break; 1303 case ISD::SETGT: { 1304 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op); 1305 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);; 1306 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1), 1307 getI32Imm(31), getI32Imm(31)); 1308 break; 1309 } 1310 } 1311 return SDOperand(N, 0); 1312 } else if (Imm == ~0U) { // setcc op, -1 1313 SDOperand Op = Select(N->getOperand(0)); 1314 switch (CC) { 1315 default: assert(0 && "Unhandled SetCC condition"); abort(); 1316 case ISD::SETEQ: 1317 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 1318 Op, getI32Imm(1)); 1319 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 1320 CurDAG->getTargetNode(PPC::LI, MVT::i32, 1321 getI32Imm(0)), 1322 Op.getValue(1)); 1323 break; 1324 case ISD::SETNE: { 1325 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op); 1326 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 1327 Op, getI32Imm(~0U)); 1328 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); 1329 break; 1330 } 1331 case ISD::SETLT: { 1332 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op, 1333 getI32Imm(1)); 1334 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op); 1335 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1), 1336 getI32Imm(31), getI32Imm(31)); 1337 break; 1338 } 1339 case ISD::SETGT: 1340 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1), 1341 getI32Imm(31), getI32Imm(31)); 1342 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1)); 1343 break; 1344 } 1345 return SDOperand(N, 0); 1346 } 1347 } 1348 1349 bool Inv; 1350 unsigned Idx = getCRIdxForSetCC(CC, Inv); 1351 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC); 1352 SDOperand IntCR; 1353 1354 // Force the ccreg into CR7. 1355 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); 1356 1357 std::vector<MVT::ValueType> VTs; 1358 VTs.push_back(MVT::Other); 1359 VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag 1360 std::vector<SDOperand> Ops; 1361 Ops.push_back(CurDAG->getEntryNode()); 1362 Ops.push_back(CR7Reg); 1363 Ops.push_back(CCReg); 1364 CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1); 1365 1366 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor()) 1367 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg); 1368 else 1369 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg); 1370 1371 if (!Inv) { 1372 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR, 1373 getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31)); 1374 } else { 1375 SDOperand Tmp = 1376 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR, 1377 getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31)); 1378 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); 1379 } 1380 1381 return SDOperand(N, 0); 1382 } 1383 1384 case ISD::SELECT_CC: { 1385 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 1386 1387 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc 1388 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1))) 1389 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2))) 1390 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3))) 1391 if (N1C->isNullValue() && N3C->isNullValue() && 1392 N2C->getValue() == 1ULL && CC == ISD::SETNE) { 1393 SDOperand LHS = Select(N->getOperand(0)); 1394 SDOperand Tmp = 1395 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, 1396 LHS, getI32Imm(~0U)); 1397 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS, 1398 Tmp.getValue(1)); 1399 return SDOperand(N, 0); 1400 } 1401 1402 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC); 1403 unsigned BROpc = getBCCForSetCC(CC); 1404 1405 bool isFP = MVT::isFloatingPoint(N->getValueType(0)); 1406 unsigned SelectCCOp = isFP ? PPC::SELECT_CC_FP : PPC::SELECT_CC_Int; 1407 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg, 1408 Select(N->getOperand(2)), Select(N->getOperand(3)), 1409 getI32Imm(BROpc)); 1410 return SDOperand(N, 0); 1411 } 1412 1413 case ISD::CALLSEQ_START: 1414 case ISD::CALLSEQ_END: { 1415 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue(); 1416 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ? 1417 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP; 1418 CurDAG->SelectNodeTo(N, Opc, MVT::Other, 1419 getI32Imm(Amt), Select(N->getOperand(0))); 1420 return SDOperand(N, 0); 1421 } 1422 case ISD::CALL: 1423 case ISD::TAILCALL: { 1424 SDOperand Chain = Select(N->getOperand(0)); 1425 1426 unsigned CallOpcode; 1427 std::vector<SDOperand> CallOperands; 1428 1429 if (GlobalAddressSDNode *GASD = 1430 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) { 1431 CallOpcode = PPC::CALLpcrel; 1432 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(), 1433 MVT::i32)); 1434 } else if (ExternalSymbolSDNode *ESSDN = 1435 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) { 1436 CallOpcode = PPC::CALLpcrel; 1437 CallOperands.push_back(N->getOperand(1)); 1438 } else { 1439 // Copy the callee address into the CTR register. 1440 SDOperand Callee = Select(N->getOperand(1)); 1441 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain); 1442 1443 // Copy the callee address into R12 on darwin. 1444 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32); 1445 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee); 1446 1447 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall 1448 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall 1449 CallOperands.push_back(R12); 1450 CallOpcode = PPC::CALLindirect; 1451 } 1452 1453 unsigned GPR_idx = 0, FPR_idx = 0; 1454 static const unsigned GPR[] = { 1455 PPC::R3, PPC::R4, PPC::R5, PPC::R6, 1456 PPC::R7, PPC::R8, PPC::R9, PPC::R10, 1457 }; 1458 static const unsigned FPR[] = { 1459 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, 1460 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 1461 }; 1462 1463 SDOperand InFlag; // Null incoming flag value. 1464 1465 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) { 1466 unsigned DestReg = 0; 1467 MVT::ValueType RegTy = N->getOperand(i).getValueType(); 1468 if (RegTy == MVT::i32) { 1469 assert(GPR_idx < 8 && "Too many int args"); 1470 DestReg = GPR[GPR_idx++]; 1471 } else { 1472 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) && 1473 "Unpromoted integer arg?"); 1474 assert(FPR_idx < 13 && "Too many fp args"); 1475 DestReg = FPR[FPR_idx++]; 1476 } 1477 1478 if (N->getOperand(i).getOpcode() != ISD::UNDEF) { 1479 SDOperand Val = Select(N->getOperand(i)); 1480 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag); 1481 InFlag = Chain.getValue(1); 1482 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy)); 1483 } 1484 } 1485 1486 // Finally, once everything is in registers to pass to the call, emit the 1487 // call itself. 1488 if (InFlag.Val) 1489 CallOperands.push_back(InFlag); // Strong dep on register copies. 1490 else 1491 CallOperands.push_back(Chain); // Weak dep on whatever occurs before 1492 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, 1493 CallOperands); 1494 1495 std::vector<SDOperand> CallResults; 1496 1497 // If the call has results, copy the values out of the ret val registers. 1498 switch (N->getValueType(0)) { 1499 default: assert(0 && "Unexpected ret value!"); 1500 case MVT::Other: break; 1501 case MVT::i32: 1502 if (N->getValueType(1) == MVT::i32) { 1503 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32, 1504 Chain.getValue(1)).getValue(1); 1505 CallResults.push_back(Chain.getValue(0)); 1506 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32, 1507 Chain.getValue(1)).getValue(1); 1508 CallResults.push_back(Chain.getValue(0)); 1509 } else { 1510 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32, 1511 Chain.getValue(1)).getValue(1); 1512 CallResults.push_back(Chain.getValue(0)); 1513 } 1514 break; 1515 case MVT::f32: 1516 case MVT::f64: 1517 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0), 1518 Chain.getValue(1)).getValue(1); 1519 CallResults.push_back(Chain.getValue(0)); 1520 break; 1521 } 1522 1523 CallResults.push_back(Chain); 1524 CurDAG->ReplaceAllUsesWith(N, CallResults); 1525 return CallResults[Op.ResNo]; 1526 } 1527 case ISD::RET: { 1528 SDOperand Chain = Select(N->getOperand(0)); // Token chain. 1529 1530 if (N->getNumOperands() == 2) { 1531 SDOperand Val = Select(N->getOperand(1)); 1532 if (N->getOperand(1).getValueType() == MVT::i32) { 1533 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val); 1534 } else { 1535 assert(MVT::isFloatingPoint(N->getOperand(1).getValueType())); 1536 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val); 1537 } 1538 } else if (N->getNumOperands() > 1) { 1539 assert(N->getOperand(1).getValueType() == MVT::i32 && 1540 N->getOperand(2).getValueType() == MVT::i32 && 1541 N->getNumOperands() == 3 && "Unknown two-register ret value!"); 1542 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Select(N->getOperand(1))); 1543 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Select(N->getOperand(2))); 1544 } 1545 1546 // Finally, select this to a blr (return) instruction. 1547 CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain); 1548 return SDOperand(N, 0); 1549 } 1550 case ISD::BR: 1551 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1), 1552 Select(N->getOperand(0))); 1553 return SDOperand(N, 0); 1554 case ISD::BR_CC: 1555 case ISD::BRTWOWAY_CC: { 1556 SDOperand Chain = Select(N->getOperand(0)); 1557 MachineBasicBlock *Dest = 1558 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock(); 1559 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); 1560 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC); 1561 unsigned Opc = getBCCForSetCC(CC); 1562 1563 // If this is a two way branch, then grab the fallthrough basic block 1564 // argument and build a PowerPC branch pseudo-op, suitable for long branch 1565 // conversion if necessary by the branch selection pass. Otherwise, emit a 1566 // standard conditional branch. 1567 if (N->getOpcode() == ISD::BRTWOWAY_CC) { 1568 MachineBasicBlock *Fallthrough = 1569 cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock(); 1570 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other, 1571 CondCode, getI32Imm(Opc), 1572 N->getOperand(4), N->getOperand(5), 1573 Chain); 1574 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(5), CB); 1575 } else { 1576 // Iterate to the next basic block 1577 ilist<MachineBasicBlock>::iterator It = BB; 1578 ++It; 1579 1580 // If the fallthrough path is off the end of the function, which would be 1581 // undefined behavior, set it to be the same as the current block because 1582 // we have nothing better to set it to, and leaving it alone will cause 1583 // the PowerPC Branch Selection pass to crash. 1584 if (It == BB->getParent()->end()) It = Dest; 1585 CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode, 1586 getI32Imm(Opc), N->getOperand(4), 1587 CurDAG->getBasicBlock(It), Chain); 1588 } 1589 return SDOperand(N, 0); 1590 } 1591 } 1592 1593 return SelectCode(Op); 1594} 1595 1596 1597/// createPPC32ISelDag - This pass converts a legalized DAG into a 1598/// PowerPC-specific DAG, ready for instruction scheduling. 1599/// 1600FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) { 1601 return new PPC32DAGToDAGISel(TM); 1602} 1603 1604