1 //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines a pattern matching instruction selector for PowerPC, 11 // converting from a legalized dag to a PPC dag. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "PPC.h" 16 #include "MCTargetDesc/PPCPredicates.h" 17 #include "PPCMachineFunctionInfo.h" 18 #include "PPCTargetMachine.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineInstrBuilder.h" 21 #include "llvm/CodeGen/MachineRegisterInfo.h" 22 #include "llvm/CodeGen/SelectionDAG.h" 23 #include "llvm/CodeGen/SelectionDAGISel.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/Function.h" 26 #include "llvm/IR/GlobalAlias.h" 27 #include "llvm/IR/GlobalValue.h" 28 #include "llvm/IR/GlobalVariable.h" 29 #include "llvm/IR/Intrinsics.h" 30 #include "llvm/IR/Module.h" 31 #include "llvm/Support/CommandLine.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/MathExtras.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include "llvm/Target/TargetOptions.h" 37 using namespace llvm; 38 39 #define DEBUG_TYPE "ppc-codegen" 40 41 // FIXME: Remove this once the bug has been fixed! 42 cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug", 43 cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden); 44 45 static cl::opt<bool> 46 UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true), 47 cl::desc("use aggressive ppc isel for bit permutations"), 48 cl::Hidden); 49 static cl::opt<bool> BPermRewriterNoMasking( 50 "ppc-bit-perm-rewriter-stress-rotates", 51 cl::desc("stress rotate selection in aggressive ppc isel for " 52 "bit permutations"), 53 cl::Hidden); 54 55 namespace llvm { 56 void initializePPCDAGToDAGISelPass(PassRegistry&); 57 } 58 59 namespace { 60 //===--------------------------------------------------------------------===// 61 /// PPCDAGToDAGISel - PPC specific code to select PPC machine 62 /// instructions for SelectionDAG operations. 63 /// 64 class PPCDAGToDAGISel : public SelectionDAGISel { 65 const PPCTargetMachine &TM; 66 const PPCSubtarget *PPCSubTarget; 67 const PPCTargetLowering *PPCLowering; 68 unsigned GlobalBaseReg; 69 public: 70 explicit PPCDAGToDAGISel(PPCTargetMachine &tm) 71 : SelectionDAGISel(tm), TM(tm) { 72 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry()); 73 } 74 75 bool runOnMachineFunction(MachineFunction &MF) override { 76 // Make sure we re-emit a set of the global base reg if necessary 77 GlobalBaseReg = 0; 78 PPCSubTarget = &MF.getSubtarget<PPCSubtarget>(); 79 PPCLowering = PPCSubTarget->getTargetLowering(); 80 SelectionDAGISel::runOnMachineFunction(MF); 81 82 if (!PPCSubTarget->isSVR4ABI()) 83 InsertVRSaveCode(MF); 84 85 return true; 86 } 87 88 void PreprocessISelDAG() override; 89 void PostprocessISelDAG() override; 90 91 /// getI32Imm - Return a target constant with the specified value, of type 92 /// i32. 93 inline SDValue getI32Imm(unsigned Imm, SDLoc dl) { 94 return CurDAG->getTargetConstant(Imm, dl, MVT::i32); 95 } 96 97 /// getI64Imm - Return a target constant with the specified value, of type 98 /// i64. 99 inline SDValue getI64Imm(uint64_t Imm, SDLoc dl) { 100 return CurDAG->getTargetConstant(Imm, dl, MVT::i64); 101 } 102 103 /// getSmallIPtrImm - Return a target constant of pointer type. 104 inline SDValue getSmallIPtrImm(unsigned Imm, SDLoc dl) { 105 return CurDAG->getTargetConstant(Imm, dl, PPCLowering->getPointerTy()); 106 } 107 108 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a 109 /// rotate and mask opcode and mask operation. 110 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask, 111 unsigned &SH, unsigned &MB, unsigned &ME); 112 113 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC 114 /// base register. Return the virtual register that holds this value. 115 SDNode *getGlobalBaseReg(); 116 117 SDNode *getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0); 118 119 // Select - Convert the specified operand from a target-independent to a 120 // target-specific node if it hasn't already been changed. 121 SDNode *Select(SDNode *N) override; 122 123 SDNode *SelectBitfieldInsert(SDNode *N); 124 SDNode *SelectBitPermutation(SDNode *N); 125 126 /// SelectCC - Select a comparison of the specified values with the 127 /// specified condition code, returning the CR# of the expression. 128 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl); 129 130 /// SelectAddrImm - Returns true if the address N can be represented by 131 /// a base register plus a signed 16-bit displacement [r+imm]. 132 bool SelectAddrImm(SDValue N, SDValue &Disp, 133 SDValue &Base) { 134 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false); 135 } 136 137 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc 138 /// immediate field. Note that the operand at this point is already the 139 /// result of a prior SelectAddressRegImm call. 140 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const { 141 if (N.getOpcode() == ISD::TargetConstant || 142 N.getOpcode() == ISD::TargetGlobalAddress) { 143 Out = N; 144 return true; 145 } 146 147 return false; 148 } 149 150 /// SelectAddrIdx - Given the specified addressed, check to see if it can be 151 /// represented as an indexed [r+r] operation. Returns false if it can 152 /// be represented by [r+imm], which are preferred. 153 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) { 154 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG); 155 } 156 157 /// SelectAddrIdxOnly - Given the specified addressed, force it to be 158 /// represented as an indexed [r+r] operation. 159 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) { 160 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG); 161 } 162 163 /// SelectAddrImmX4 - Returns true if the address N can be represented by 164 /// a base register plus a signed 16-bit displacement that is a multiple of 4. 165 /// Suitable for use by STD and friends. 166 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) { 167 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true); 168 } 169 170 // Select an address into a single register. 171 bool SelectAddr(SDValue N, SDValue &Base) { 172 Base = N; 173 return true; 174 } 175 176 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for 177 /// inline asm expressions. It is always correct to compute the value into 178 /// a register. The case of adding a (possibly relocatable) constant to a 179 /// register can be improved, but it is wrong to substitute Reg+Reg for 180 /// Reg in an asm, because the load or store opcode would have to change. 181 bool SelectInlineAsmMemoryOperand(const SDValue &Op, 182 unsigned ConstraintID, 183 std::vector<SDValue> &OutOps) override { 184 185 switch(ConstraintID) { 186 default: 187 errs() << "ConstraintID: " << ConstraintID << "\n"; 188 llvm_unreachable("Unexpected asm memory constraint"); 189 case InlineAsm::Constraint_es: 190 case InlineAsm::Constraint_i: 191 case InlineAsm::Constraint_m: 192 case InlineAsm::Constraint_o: 193 case InlineAsm::Constraint_Q: 194 case InlineAsm::Constraint_Z: 195 case InlineAsm::Constraint_Zy: 196 // We need to make sure that this one operand does not end up in r0 197 // (because we might end up lowering this as 0(%op)). 198 const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo(); 199 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1); 200 SDLoc dl(Op); 201 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32); 202 SDValue NewOp = 203 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 204 dl, Op.getValueType(), 205 Op, RC), 0); 206 207 OutOps.push_back(NewOp); 208 return false; 209 } 210 return true; 211 } 212 213 void InsertVRSaveCode(MachineFunction &MF); 214 215 const char *getPassName() const override { 216 return "PowerPC DAG->DAG Pattern Instruction Selection"; 217 } 218 219 // Include the pieces autogenerated from the target description. 220 #include "PPCGenDAGISel.inc" 221 222 private: 223 SDNode *SelectSETCC(SDNode *N); 224 225 void PeepholePPC64(); 226 void PeepholePPC64ZExt(); 227 void PeepholeCROps(); 228 229 SDValue combineToCMPB(SDNode *N); 230 void foldBoolExts(SDValue &Res, SDNode *&N); 231 232 bool AllUsersSelectZero(SDNode *N); 233 void SwapAllSelectUsers(SDNode *N); 234 235 SDNode *transferMemOperands(SDNode *N, SDNode *Result); 236 }; 237 } 238 239 /// InsertVRSaveCode - Once the entire function has been instruction selected, 240 /// all virtual registers are created and all machine instructions are built, 241 /// check to see if we need to save/restore VRSAVE. If so, do it. 242 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) { 243 // Check to see if this function uses vector registers, which means we have to 244 // save and restore the VRSAVE register and update it with the regs we use. 245 // 246 // In this case, there will be virtual registers of vector type created 247 // by the scheduler. Detect them now. 248 bool HasVectorVReg = false; 249 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) { 250 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 251 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) { 252 HasVectorVReg = true; 253 break; 254 } 255 } 256 if (!HasVectorVReg) return; // nothing to do. 257 258 // If we have a vector register, we want to emit code into the entry and exit 259 // blocks to save and restore the VRSAVE register. We do this here (instead 260 // of marking all vector instructions as clobbering VRSAVE) for two reasons: 261 // 262 // 1. This (trivially) reduces the load on the register allocator, by not 263 // having to represent the live range of the VRSAVE register. 264 // 2. This (more significantly) allows us to create a temporary virtual 265 // register to hold the saved VRSAVE value, allowing this temporary to be 266 // register allocated, instead of forcing it to be spilled to the stack. 267 268 // Create two vregs - one to hold the VRSAVE register that is live-in to the 269 // function and one for the value after having bits or'd into it. 270 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); 271 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); 272 273 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo(); 274 MachineBasicBlock &EntryBB = *Fn.begin(); 275 DebugLoc dl; 276 // Emit the following code into the entry block: 277 // InVRSAVE = MFVRSAVE 278 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE 279 // MTVRSAVE UpdatedVRSAVE 280 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point 281 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE); 282 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE), 283 UpdatedVRSAVE).addReg(InVRSAVE); 284 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE); 285 286 // Find all return blocks, outputting a restore in each epilog. 287 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { 288 if (!BB->empty() && BB->back().isReturn()) { 289 IP = BB->end(); --IP; 290 291 // Skip over all terminator instructions, which are part of the return 292 // sequence. 293 MachineBasicBlock::iterator I2 = IP; 294 while (I2 != BB->begin() && (--I2)->isTerminator()) 295 IP = I2; 296 297 // Emit: MTVRSAVE InVRSave 298 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE); 299 } 300 } 301 } 302 303 304 /// getGlobalBaseReg - Output the instructions required to put the 305 /// base address to use for accessing globals into a register. 306 /// 307 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() { 308 if (!GlobalBaseReg) { 309 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo(); 310 // Insert the set of GlobalBaseReg into the first MBB of the function 311 MachineBasicBlock &FirstMBB = MF->front(); 312 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 313 const Module *M = MF->getFunction()->getParent(); 314 DebugLoc dl; 315 316 if (PPCLowering->getPointerTy() == MVT::i32) { 317 if (PPCSubTarget->isTargetELF()) { 318 GlobalBaseReg = PPC::R30; 319 if (M->getPICLevel() == PICLevel::Small) { 320 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR)); 321 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); 322 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true); 323 } else { 324 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); 325 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); 326 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); 327 BuildMI(FirstMBB, MBBI, dl, 328 TII.get(PPC::UpdateGBR), GlobalBaseReg) 329 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg); 330 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true); 331 } 332 } else { 333 GlobalBaseReg = 334 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass); 335 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); 336 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); 337 } 338 } else { 339 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass); 340 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8)); 341 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg); 342 } 343 } 344 return CurDAG->getRegister(GlobalBaseReg, 345 PPCLowering->getPointerTy()).getNode(); 346 } 347 348 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit 349 /// or 64-bit immediate, and if the value can be accurately represented as a 350 /// sign extension from a 16-bit value. If so, this returns true and the 351 /// immediate. 352 static bool isIntS16Immediate(SDNode *N, short &Imm) { 353 if (N->getOpcode() != ISD::Constant) 354 return false; 355 356 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); 357 if (N->getValueType(0) == MVT::i32) 358 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); 359 else 360 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); 361 } 362 363 static bool isIntS16Immediate(SDValue Op, short &Imm) { 364 return isIntS16Immediate(Op.getNode(), Imm); 365 } 366 367 368 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant 369 /// operand. If so Imm will receive the 32-bit value. 370 static bool isInt32Immediate(SDNode *N, unsigned &Imm) { 371 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { 372 Imm = cast<ConstantSDNode>(N)->getZExtValue(); 373 return true; 374 } 375 return false; 376 } 377 378 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant 379 /// operand. If so Imm will receive the 64-bit value. 380 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) { 381 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) { 382 Imm = cast<ConstantSDNode>(N)->getZExtValue(); 383 return true; 384 } 385 return false; 386 } 387 388 // isInt32Immediate - This method tests to see if a constant operand. 389 // If so Imm will receive the 32 bit value. 390 static bool isInt32Immediate(SDValue N, unsigned &Imm) { 391 return isInt32Immediate(N.getNode(), Imm); 392 } 393 394 395 // isOpcWithIntImmediate - This method tests to see if the node is a specific 396 // opcode and that it has a immediate integer right operand. 397 // If so Imm will receive the 32 bit value. 398 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { 399 return N->getOpcode() == Opc 400 && isInt32Immediate(N->getOperand(1).getNode(), Imm); 401 } 402 403 SDNode *PPCDAGToDAGISel::getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) { 404 SDLoc dl(SN); 405 int FI = cast<FrameIndexSDNode>(N)->getIndex(); 406 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0)); 407 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8; 408 if (SN->hasOneUse()) 409 return CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI, 410 getSmallIPtrImm(Offset, dl)); 411 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI, 412 getSmallIPtrImm(Offset, dl)); 413 } 414 415 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, 416 bool isShiftMask, unsigned &SH, 417 unsigned &MB, unsigned &ME) { 418 // Don't even go down this path for i64, since different logic will be 419 // necessary for rldicl/rldicr/rldimi. 420 if (N->getValueType(0) != MVT::i32) 421 return false; 422 423 unsigned Shift = 32; 424 unsigned Indeterminant = ~0; // bit mask marking indeterminant results 425 unsigned Opcode = N->getOpcode(); 426 if (N->getNumOperands() != 2 || 427 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) 428 return false; 429 430 if (Opcode == ISD::SHL) { 431 // apply shift left to mask if it comes first 432 if (isShiftMask) Mask = Mask << Shift; 433 // determine which bits are made indeterminant by shift 434 Indeterminant = ~(0xFFFFFFFFu << Shift); 435 } else if (Opcode == ISD::SRL) { 436 // apply shift right to mask if it comes first 437 if (isShiftMask) Mask = Mask >> Shift; 438 // determine which bits are made indeterminant by shift 439 Indeterminant = ~(0xFFFFFFFFu >> Shift); 440 // adjust for the left rotate 441 Shift = 32 - Shift; 442 } else if (Opcode == ISD::ROTL) { 443 Indeterminant = 0; 444 } else { 445 return false; 446 } 447 448 // if the mask doesn't intersect any Indeterminant bits 449 if (Mask && !(Mask & Indeterminant)) { 450 SH = Shift & 31; 451 // make sure the mask is still a mask (wrap arounds may not be) 452 return isRunOfOnes(Mask, MB, ME); 453 } 454 return false; 455 } 456 457 /// SelectBitfieldInsert - turn an or of two masked values into 458 /// the rotate left word immediate then mask insert (rlwimi) instruction. 459 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) { 460 SDValue Op0 = N->getOperand(0); 461 SDValue Op1 = N->getOperand(1); 462 SDLoc dl(N); 463 464 APInt LKZ, LKO, RKZ, RKO; 465 CurDAG->computeKnownBits(Op0, LKZ, LKO); 466 CurDAG->computeKnownBits(Op1, RKZ, RKO); 467 468 unsigned TargetMask = LKZ.getZExtValue(); 469 unsigned InsertMask = RKZ.getZExtValue(); 470 471 if ((TargetMask | InsertMask) == 0xFFFFFFFF) { 472 unsigned Op0Opc = Op0.getOpcode(); 473 unsigned Op1Opc = Op1.getOpcode(); 474 unsigned Value, SH = 0; 475 TargetMask = ~TargetMask; 476 InsertMask = ~InsertMask; 477 478 // If the LHS has a foldable shift and the RHS does not, then swap it to the 479 // RHS so that we can fold the shift into the insert. 480 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { 481 if (Op0.getOperand(0).getOpcode() == ISD::SHL || 482 Op0.getOperand(0).getOpcode() == ISD::SRL) { 483 if (Op1.getOperand(0).getOpcode() != ISD::SHL && 484 Op1.getOperand(0).getOpcode() != ISD::SRL) { 485 std::swap(Op0, Op1); 486 std::swap(Op0Opc, Op1Opc); 487 std::swap(TargetMask, InsertMask); 488 } 489 } 490 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) { 491 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL && 492 Op1.getOperand(0).getOpcode() != ISD::SRL) { 493 std::swap(Op0, Op1); 494 std::swap(Op0Opc, Op1Opc); 495 std::swap(TargetMask, InsertMask); 496 } 497 } 498 499 unsigned MB, ME; 500 if (isRunOfOnes(InsertMask, MB, ME)) { 501 SDValue Tmp1, Tmp2; 502 503 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) && 504 isInt32Immediate(Op1.getOperand(1), Value)) { 505 Op1 = Op1.getOperand(0); 506 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value; 507 } 508 if (Op1Opc == ISD::AND) { 509 // The AND mask might not be a constant, and we need to make sure that 510 // if we're going to fold the masking with the insert, all bits not 511 // know to be zero in the mask are known to be one. 512 APInt MKZ, MKO; 513 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO); 514 bool CanFoldMask = InsertMask == MKO.getZExtValue(); 515 516 unsigned SHOpc = Op1.getOperand(0).getOpcode(); 517 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask && 518 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) { 519 // Note that Value must be in range here (less than 32) because 520 // otherwise there would not be any bits set in InsertMask. 521 Op1 = Op1.getOperand(0).getOperand(0); 522 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value; 523 } 524 } 525 526 SH &= 31; 527 SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl), 528 getI32Imm(ME, dl) }; 529 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops); 530 } 531 } 532 return nullptr; 533 } 534 535 // Predict the number of instructions that would be generated by calling 536 // SelectInt64(N). 537 static unsigned SelectInt64CountDirect(int64_t Imm) { 538 // Assume no remaining bits. 539 unsigned Remainder = 0; 540 // Assume no shift required. 541 unsigned Shift = 0; 542 543 // If it can't be represented as a 32 bit value. 544 if (!isInt<32>(Imm)) { 545 Shift = countTrailingZeros<uint64_t>(Imm); 546 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; 547 548 // If the shifted value fits 32 bits. 549 if (isInt<32>(ImmSh)) { 550 // Go with the shifted value. 551 Imm = ImmSh; 552 } else { 553 // Still stuck with a 64 bit value. 554 Remainder = Imm; 555 Shift = 32; 556 Imm >>= 32; 557 } 558 } 559 560 // Intermediate operand. 561 unsigned Result = 0; 562 563 // Handle first 32 bits. 564 unsigned Lo = Imm & 0xFFFF; 565 unsigned Hi = (Imm >> 16) & 0xFFFF; 566 567 // Simple value. 568 if (isInt<16>(Imm)) { 569 // Just the Lo bits. 570 ++Result; 571 } else if (Lo) { 572 // Handle the Hi bits and Lo bits. 573 Result += 2; 574 } else { 575 // Just the Hi bits. 576 ++Result; 577 } 578 579 // If no shift, we're done. 580 if (!Shift) return Result; 581 582 // Shift for next step if the upper 32-bits were not zero. 583 if (Imm) 584 ++Result; 585 586 // Add in the last bits as required. 587 if ((Hi = (Remainder >> 16) & 0xFFFF)) 588 ++Result; 589 if ((Lo = Remainder & 0xFFFF)) 590 ++Result; 591 592 return Result; 593 } 594 595 static uint64_t Rot64(uint64_t Imm, unsigned R) { 596 return (Imm << R) | (Imm >> (64 - R)); 597 } 598 599 static unsigned SelectInt64Count(int64_t Imm) { 600 unsigned Count = SelectInt64CountDirect(Imm); 601 if (Count == 1) 602 return Count; 603 604 for (unsigned r = 1; r < 63; ++r) { 605 uint64_t RImm = Rot64(Imm, r); 606 unsigned RCount = SelectInt64CountDirect(RImm) + 1; 607 Count = std::min(Count, RCount); 608 609 // See comments in SelectInt64 for an explanation of the logic below. 610 unsigned LS = findLastSet(RImm); 611 if (LS != r-1) 612 continue; 613 614 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1)); 615 uint64_t RImmWithOnes = RImm | OnesMask; 616 617 RCount = SelectInt64CountDirect(RImmWithOnes) + 1; 618 Count = std::min(Count, RCount); 619 } 620 621 return Count; 622 } 623 624 // Select a 64-bit constant. For cost-modeling purposes, SelectInt64Count 625 // (above) needs to be kept in sync with this function. 626 static SDNode *SelectInt64Direct(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) { 627 // Assume no remaining bits. 628 unsigned Remainder = 0; 629 // Assume no shift required. 630 unsigned Shift = 0; 631 632 // If it can't be represented as a 32 bit value. 633 if (!isInt<32>(Imm)) { 634 Shift = countTrailingZeros<uint64_t>(Imm); 635 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; 636 637 // If the shifted value fits 32 bits. 638 if (isInt<32>(ImmSh)) { 639 // Go with the shifted value. 640 Imm = ImmSh; 641 } else { 642 // Still stuck with a 64 bit value. 643 Remainder = Imm; 644 Shift = 32; 645 Imm >>= 32; 646 } 647 } 648 649 // Intermediate operand. 650 SDNode *Result; 651 652 // Handle first 32 bits. 653 unsigned Lo = Imm & 0xFFFF; 654 unsigned Hi = (Imm >> 16) & 0xFFFF; 655 656 auto getI32Imm = [CurDAG, dl](unsigned Imm) { 657 return CurDAG->getTargetConstant(Imm, dl, MVT::i32); 658 }; 659 660 // Simple value. 661 if (isInt<16>(Imm)) { 662 // Just the Lo bits. 663 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo)); 664 } else if (Lo) { 665 // Handle the Hi bits. 666 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8; 667 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi)); 668 // And Lo bits. 669 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, 670 SDValue(Result, 0), getI32Imm(Lo)); 671 } else { 672 // Just the Hi bits. 673 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi)); 674 } 675 676 // If no shift, we're done. 677 if (!Shift) return Result; 678 679 // Shift for next step if the upper 32-bits were not zero. 680 if (Imm) { 681 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, 682 SDValue(Result, 0), 683 getI32Imm(Shift), 684 getI32Imm(63 - Shift)); 685 } 686 687 // Add in the last bits as required. 688 if ((Hi = (Remainder >> 16) & 0xFFFF)) { 689 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64, 690 SDValue(Result, 0), getI32Imm(Hi)); 691 } 692 if ((Lo = Remainder & 0xFFFF)) { 693 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, 694 SDValue(Result, 0), getI32Imm(Lo)); 695 } 696 697 return Result; 698 } 699 700 static SDNode *SelectInt64(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) { 701 unsigned Count = SelectInt64CountDirect(Imm); 702 if (Count == 1) 703 return SelectInt64Direct(CurDAG, dl, Imm); 704 705 unsigned RMin = 0; 706 707 int64_t MatImm; 708 unsigned MaskEnd; 709 710 for (unsigned r = 1; r < 63; ++r) { 711 uint64_t RImm = Rot64(Imm, r); 712 unsigned RCount = SelectInt64CountDirect(RImm) + 1; 713 if (RCount < Count) { 714 Count = RCount; 715 RMin = r; 716 MatImm = RImm; 717 MaskEnd = 63; 718 } 719 720 // If the immediate to generate has many trailing zeros, it might be 721 // worthwhile to generate a rotated value with too many leading ones 722 // (because that's free with li/lis's sign-extension semantics), and then 723 // mask them off after rotation. 724 725 unsigned LS = findLastSet(RImm); 726 // We're adding (63-LS) higher-order ones, and we expect to mask them off 727 // after performing the inverse rotation by (64-r). So we need that: 728 // 63-LS == 64-r => LS == r-1 729 if (LS != r-1) 730 continue; 731 732 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1)); 733 uint64_t RImmWithOnes = RImm | OnesMask; 734 735 RCount = SelectInt64CountDirect(RImmWithOnes) + 1; 736 if (RCount < Count) { 737 Count = RCount; 738 RMin = r; 739 MatImm = RImmWithOnes; 740 MaskEnd = LS; 741 } 742 } 743 744 if (!RMin) 745 return SelectInt64Direct(CurDAG, dl, Imm); 746 747 auto getI32Imm = [CurDAG, dl](unsigned Imm) { 748 return CurDAG->getTargetConstant(Imm, dl, MVT::i32); 749 }; 750 751 SDValue Val = SDValue(SelectInt64Direct(CurDAG, dl, MatImm), 0); 752 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val, 753 getI32Imm(64 - RMin), getI32Imm(MaskEnd)); 754 } 755 756 // Select a 64-bit constant. 757 static SDNode *SelectInt64(SelectionDAG *CurDAG, SDNode *N) { 758 SDLoc dl(N); 759 760 // Get 64 bit value. 761 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue(); 762 return SelectInt64(CurDAG, dl, Imm); 763 } 764 765 namespace { 766 class BitPermutationSelector { 767 struct ValueBit { 768 SDValue V; 769 770 // The bit number in the value, using a convention where bit 0 is the 771 // lowest-order bit. 772 unsigned Idx; 773 774 enum Kind { 775 ConstZero, 776 Variable 777 } K; 778 779 ValueBit(SDValue V, unsigned I, Kind K = Variable) 780 : V(V), Idx(I), K(K) {} 781 ValueBit(Kind K = Variable) 782 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {} 783 784 bool isZero() const { 785 return K == ConstZero; 786 } 787 788 bool hasValue() const { 789 return K == Variable; 790 } 791 792 SDValue getValue() const { 793 assert(hasValue() && "Cannot get the value of a constant bit"); 794 return V; 795 } 796 797 unsigned getValueBitIndex() const { 798 assert(hasValue() && "Cannot get the value bit index of a constant bit"); 799 return Idx; 800 } 801 }; 802 803 // A bit group has the same underlying value and the same rotate factor. 804 struct BitGroup { 805 SDValue V; 806 unsigned RLAmt; 807 unsigned StartIdx, EndIdx; 808 809 // This rotation amount assumes that the lower 32 bits of the quantity are 810 // replicated in the high 32 bits by the rotation operator (which is done 811 // by rlwinm and friends in 64-bit mode). 812 bool Repl32; 813 // Did converting to Repl32 == true change the rotation factor? If it did, 814 // it decreased it by 32. 815 bool Repl32CR; 816 // Was this group coalesced after setting Repl32 to true? 817 bool Repl32Coalesced; 818 819 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E) 820 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false), 821 Repl32Coalesced(false) { 822 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R << 823 " [" << S << ", " << E << "]\n"); 824 } 825 }; 826 827 // Information on each (Value, RLAmt) pair (like the number of groups 828 // associated with each) used to choose the lowering method. 829 struct ValueRotInfo { 830 SDValue V; 831 unsigned RLAmt; 832 unsigned NumGroups; 833 unsigned FirstGroupStartIdx; 834 bool Repl32; 835 836 ValueRotInfo() 837 : RLAmt(UINT32_MAX), NumGroups(0), FirstGroupStartIdx(UINT32_MAX), 838 Repl32(false) {} 839 840 // For sorting (in reverse order) by NumGroups, and then by 841 // FirstGroupStartIdx. 842 bool operator < (const ValueRotInfo &Other) const { 843 // We need to sort so that the non-Repl32 come first because, when we're 844 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit 845 // masking operation. 846 if (Repl32 < Other.Repl32) 847 return true; 848 else if (Repl32 > Other.Repl32) 849 return false; 850 else if (NumGroups > Other.NumGroups) 851 return true; 852 else if (NumGroups < Other.NumGroups) 853 return false; 854 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx) 855 return true; 856 return false; 857 } 858 }; 859 860 // Return true if something interesting was deduced, return false if we're 861 // providing only a generic representation of V (or something else likewise 862 // uninteresting for instruction selection). 863 bool getValueBits(SDValue V, SmallVector<ValueBit, 64> &Bits) { 864 switch (V.getOpcode()) { 865 default: break; 866 case ISD::ROTL: 867 if (isa<ConstantSDNode>(V.getOperand(1))) { 868 unsigned RotAmt = V.getConstantOperandVal(1); 869 870 SmallVector<ValueBit, 64> LHSBits(Bits.size()); 871 getValueBits(V.getOperand(0), LHSBits); 872 873 for (unsigned i = 0; i < Bits.size(); ++i) 874 Bits[i] = LHSBits[i < RotAmt ? i + (Bits.size() - RotAmt) : i - RotAmt]; 875 876 return true; 877 } 878 break; 879 case ISD::SHL: 880 if (isa<ConstantSDNode>(V.getOperand(1))) { 881 unsigned ShiftAmt = V.getConstantOperandVal(1); 882 883 SmallVector<ValueBit, 64> LHSBits(Bits.size()); 884 getValueBits(V.getOperand(0), LHSBits); 885 886 for (unsigned i = ShiftAmt; i < Bits.size(); ++i) 887 Bits[i] = LHSBits[i - ShiftAmt]; 888 889 for (unsigned i = 0; i < ShiftAmt; ++i) 890 Bits[i] = ValueBit(ValueBit::ConstZero); 891 892 return true; 893 } 894 break; 895 case ISD::SRL: 896 if (isa<ConstantSDNode>(V.getOperand(1))) { 897 unsigned ShiftAmt = V.getConstantOperandVal(1); 898 899 SmallVector<ValueBit, 64> LHSBits(Bits.size()); 900 getValueBits(V.getOperand(0), LHSBits); 901 902 for (unsigned i = 0; i < Bits.size() - ShiftAmt; ++i) 903 Bits[i] = LHSBits[i + ShiftAmt]; 904 905 for (unsigned i = Bits.size() - ShiftAmt; i < Bits.size(); ++i) 906 Bits[i] = ValueBit(ValueBit::ConstZero); 907 908 return true; 909 } 910 break; 911 case ISD::AND: 912 if (isa<ConstantSDNode>(V.getOperand(1))) { 913 uint64_t Mask = V.getConstantOperandVal(1); 914 915 SmallVector<ValueBit, 64> LHSBits(Bits.size()); 916 bool LHSTrivial = getValueBits(V.getOperand(0), LHSBits); 917 918 for (unsigned i = 0; i < Bits.size(); ++i) 919 if (((Mask >> i) & 1) == 1) 920 Bits[i] = LHSBits[i]; 921 else 922 Bits[i] = ValueBit(ValueBit::ConstZero); 923 924 // Mark this as interesting, only if the LHS was also interesting. This 925 // prevents the overall procedure from matching a single immediate 'and' 926 // (which is non-optimal because such an and might be folded with other 927 // things if we don't select it here). 928 return LHSTrivial; 929 } 930 break; 931 case ISD::OR: { 932 SmallVector<ValueBit, 64> LHSBits(Bits.size()), RHSBits(Bits.size()); 933 getValueBits(V.getOperand(0), LHSBits); 934 getValueBits(V.getOperand(1), RHSBits); 935 936 bool AllDisjoint = true; 937 for (unsigned i = 0; i < Bits.size(); ++i) 938 if (LHSBits[i].isZero()) 939 Bits[i] = RHSBits[i]; 940 else if (RHSBits[i].isZero()) 941 Bits[i] = LHSBits[i]; 942 else { 943 AllDisjoint = false; 944 break; 945 } 946 947 if (!AllDisjoint) 948 break; 949 950 return true; 951 } 952 } 953 954 for (unsigned i = 0; i < Bits.size(); ++i) 955 Bits[i] = ValueBit(V, i); 956 957 return false; 958 } 959 960 // For each value (except the constant ones), compute the left-rotate amount 961 // to get it from its original to final position. 962 void computeRotationAmounts() { 963 HasZeros = false; 964 RLAmt.resize(Bits.size()); 965 for (unsigned i = 0; i < Bits.size(); ++i) 966 if (Bits[i].hasValue()) { 967 unsigned VBI = Bits[i].getValueBitIndex(); 968 if (i >= VBI) 969 RLAmt[i] = i - VBI; 970 else 971 RLAmt[i] = Bits.size() - (VBI - i); 972 } else if (Bits[i].isZero()) { 973 HasZeros = true; 974 RLAmt[i] = UINT32_MAX; 975 } else { 976 llvm_unreachable("Unknown value bit type"); 977 } 978 } 979 980 // Collect groups of consecutive bits with the same underlying value and 981 // rotation factor. If we're doing late masking, we ignore zeros, otherwise 982 // they break up groups. 983 void collectBitGroups(bool LateMask) { 984 BitGroups.clear(); 985 986 unsigned LastRLAmt = RLAmt[0]; 987 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue(); 988 unsigned LastGroupStartIdx = 0; 989 for (unsigned i = 1; i < Bits.size(); ++i) { 990 unsigned ThisRLAmt = RLAmt[i]; 991 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue(); 992 if (LateMask && !ThisValue) { 993 ThisValue = LastValue; 994 ThisRLAmt = LastRLAmt; 995 // If we're doing late masking, then the first bit group always starts 996 // at zero (even if the first bits were zero). 997 if (BitGroups.empty()) 998 LastGroupStartIdx = 0; 999 } 1000 1001 // If this bit has the same underlying value and the same rotate factor as 1002 // the last one, then they're part of the same group. 1003 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue) 1004 continue; 1005 1006 if (LastValue.getNode()) 1007 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx, 1008 i-1)); 1009 LastRLAmt = ThisRLAmt; 1010 LastValue = ThisValue; 1011 LastGroupStartIdx = i; 1012 } 1013 if (LastValue.getNode()) 1014 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx, 1015 Bits.size()-1)); 1016 1017 if (BitGroups.empty()) 1018 return; 1019 1020 // We might be able to combine the first and last groups. 1021 if (BitGroups.size() > 1) { 1022 // If the first and last groups are the same, then remove the first group 1023 // in favor of the last group, making the ending index of the last group 1024 // equal to the ending index of the to-be-removed first group. 1025 if (BitGroups[0].StartIdx == 0 && 1026 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 && 1027 BitGroups[0].V == BitGroups[BitGroups.size()-1].V && 1028 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) { 1029 DEBUG(dbgs() << "\tcombining final bit group with inital one\n"); 1030 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx; 1031 BitGroups.erase(BitGroups.begin()); 1032 } 1033 } 1034 } 1035 1036 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups 1037 // associated with each. If there is a degeneracy, pick the one that occurs 1038 // first (in the final value). 1039 void collectValueRotInfo() { 1040 ValueRots.clear(); 1041 1042 for (auto &BG : BitGroups) { 1043 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0); 1044 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)]; 1045 VRI.V = BG.V; 1046 VRI.RLAmt = BG.RLAmt; 1047 VRI.Repl32 = BG.Repl32; 1048 VRI.NumGroups += 1; 1049 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx); 1050 } 1051 1052 // Now that we've collected the various ValueRotInfo instances, we need to 1053 // sort them. 1054 ValueRotsVec.clear(); 1055 for (auto &I : ValueRots) { 1056 ValueRotsVec.push_back(I.second); 1057 } 1058 std::sort(ValueRotsVec.begin(), ValueRotsVec.end()); 1059 } 1060 1061 // In 64-bit mode, rlwinm and friends have a rotation operator that 1062 // replicates the low-order 32 bits into the high-order 32-bits. The mask 1063 // indices of these instructions can only be in the lower 32 bits, so they 1064 // can only represent some 64-bit bit groups. However, when they can be used, 1065 // the 32-bit replication can be used to represent, as a single bit group, 1066 // otherwise separate bit groups. We'll convert to replicated-32-bit bit 1067 // groups when possible. Returns true if any of the bit groups were 1068 // converted. 1069 void assignRepl32BitGroups() { 1070 // If we have bits like this: 1071 // 1072 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1073 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24 1074 // Groups: | RLAmt = 8 | RLAmt = 40 | 1075 // 1076 // But, making use of a 32-bit operation that replicates the low-order 32 1077 // bits into the high-order 32 bits, this can be one bit group with a RLAmt 1078 // of 8. 1079 1080 auto IsAllLow32 = [this](BitGroup & BG) { 1081 if (BG.StartIdx <= BG.EndIdx) { 1082 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) { 1083 if (!Bits[i].hasValue()) 1084 continue; 1085 if (Bits[i].getValueBitIndex() >= 32) 1086 return false; 1087 } 1088 } else { 1089 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) { 1090 if (!Bits[i].hasValue()) 1091 continue; 1092 if (Bits[i].getValueBitIndex() >= 32) 1093 return false; 1094 } 1095 for (unsigned i = 0; i <= BG.EndIdx; ++i) { 1096 if (!Bits[i].hasValue()) 1097 continue; 1098 if (Bits[i].getValueBitIndex() >= 32) 1099 return false; 1100 } 1101 } 1102 1103 return true; 1104 }; 1105 1106 for (auto &BG : BitGroups) { 1107 if (BG.StartIdx < 32 && BG.EndIdx < 32) { 1108 if (IsAllLow32(BG)) { 1109 if (BG.RLAmt >= 32) { 1110 BG.RLAmt -= 32; 1111 BG.Repl32CR = true; 1112 } 1113 1114 BG.Repl32 = true; 1115 1116 DEBUG(dbgs() << "\t32-bit replicated bit group for " << 1117 BG.V.getNode() << " RLAmt = " << BG.RLAmt << 1118 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n"); 1119 } 1120 } 1121 } 1122 1123 // Now walk through the bit groups, consolidating where possible. 1124 for (auto I = BitGroups.begin(); I != BitGroups.end();) { 1125 // We might want to remove this bit group by merging it with the previous 1126 // group (which might be the ending group). 1127 auto IP = (I == BitGroups.begin()) ? 1128 std::prev(BitGroups.end()) : std::prev(I); 1129 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt && 1130 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) { 1131 1132 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " << 1133 I->V.getNode() << " RLAmt = " << I->RLAmt << 1134 " [" << I->StartIdx << ", " << I->EndIdx << 1135 "] with group with range [" << 1136 IP->StartIdx << ", " << IP->EndIdx << "]\n"); 1137 1138 IP->EndIdx = I->EndIdx; 1139 IP->Repl32CR = IP->Repl32CR || I->Repl32CR; 1140 IP->Repl32Coalesced = true; 1141 I = BitGroups.erase(I); 1142 continue; 1143 } else { 1144 // There is a special case worth handling: If there is a single group 1145 // covering the entire upper 32 bits, and it can be merged with both 1146 // the next and previous groups (which might be the same group), then 1147 // do so. If it is the same group (so there will be only one group in 1148 // total), then we need to reverse the order of the range so that it 1149 // covers the entire 64 bits. 1150 if (I->StartIdx == 32 && I->EndIdx == 63) { 1151 assert(std::next(I) == BitGroups.end() && 1152 "bit group ends at index 63 but there is another?"); 1153 auto IN = BitGroups.begin(); 1154 1155 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V && 1156 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt && 1157 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP && 1158 IsAllLow32(*I)) { 1159 1160 DEBUG(dbgs() << "\tcombining bit group for " << 1161 I->V.getNode() << " RLAmt = " << I->RLAmt << 1162 " [" << I->StartIdx << ", " << I->EndIdx << 1163 "] with 32-bit replicated groups with ranges [" << 1164 IP->StartIdx << ", " << IP->EndIdx << "] and [" << 1165 IN->StartIdx << ", " << IN->EndIdx << "]\n"); 1166 1167 if (IP == IN) { 1168 // There is only one other group; change it to cover the whole 1169 // range (backward, so that it can still be Repl32 but cover the 1170 // whole 64-bit range). 1171 IP->StartIdx = 31; 1172 IP->EndIdx = 30; 1173 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32; 1174 IP->Repl32Coalesced = true; 1175 I = BitGroups.erase(I); 1176 } else { 1177 // There are two separate groups, one before this group and one 1178 // after us (at the beginning). We're going to remove this group, 1179 // but also the group at the very beginning. 1180 IP->EndIdx = IN->EndIdx; 1181 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32; 1182 IP->Repl32Coalesced = true; 1183 I = BitGroups.erase(I); 1184 BitGroups.erase(BitGroups.begin()); 1185 } 1186 1187 // This must be the last group in the vector (and we might have 1188 // just invalidated the iterator above), so break here. 1189 break; 1190 } 1191 } 1192 } 1193 1194 ++I; 1195 } 1196 } 1197 1198 SDValue getI32Imm(unsigned Imm, SDLoc dl) { 1199 return CurDAG->getTargetConstant(Imm, dl, MVT::i32); 1200 } 1201 1202 uint64_t getZerosMask() { 1203 uint64_t Mask = 0; 1204 for (unsigned i = 0; i < Bits.size(); ++i) { 1205 if (Bits[i].hasValue()) 1206 continue; 1207 Mask |= (UINT64_C(1) << i); 1208 } 1209 1210 return ~Mask; 1211 } 1212 1213 // Depending on the number of groups for a particular value, it might be 1214 // better to rotate, mask explicitly (using andi/andis), and then or the 1215 // result. Select this part of the result first. 1216 void SelectAndParts32(SDLoc dl, SDValue &Res, unsigned *InstCnt) { 1217 if (BPermRewriterNoMasking) 1218 return; 1219 1220 for (ValueRotInfo &VRI : ValueRotsVec) { 1221 unsigned Mask = 0; 1222 for (unsigned i = 0; i < Bits.size(); ++i) { 1223 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V) 1224 continue; 1225 if (RLAmt[i] != VRI.RLAmt) 1226 continue; 1227 Mask |= (1u << i); 1228 } 1229 1230 // Compute the masks for andi/andis that would be necessary. 1231 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16; 1232 assert((ANDIMask != 0 || ANDISMask != 0) && 1233 "No set bits in mask for value bit groups"); 1234 bool NeedsRotate = VRI.RLAmt != 0; 1235 1236 // We're trying to minimize the number of instructions. If we have one 1237 // group, using one of andi/andis can break even. If we have three 1238 // groups, we can use both andi and andis and break even (to use both 1239 // andi and andis we also need to or the results together). We need four 1240 // groups if we also need to rotate. To use andi/andis we need to do more 1241 // than break even because rotate-and-mask instructions tend to be easier 1242 // to schedule. 1243 1244 // FIXME: We've biased here against using andi/andis, which is right for 1245 // POWER cores, but not optimal everywhere. For example, on the A2, 1246 // andi/andis have single-cycle latency whereas the rotate-and-mask 1247 // instructions take two cycles, and it would be better to bias toward 1248 // andi/andis in break-even cases. 1249 1250 unsigned NumAndInsts = (unsigned) NeedsRotate + 1251 (unsigned) (ANDIMask != 0) + 1252 (unsigned) (ANDISMask != 0) + 1253 (unsigned) (ANDIMask != 0 && ANDISMask != 0) + 1254 (unsigned) (bool) Res; 1255 1256 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() << 1257 " RL: " << VRI.RLAmt << ":" << 1258 "\n\t\t\tisel using masking: " << NumAndInsts << 1259 " using rotates: " << VRI.NumGroups << "\n"); 1260 1261 if (NumAndInsts >= VRI.NumGroups) 1262 continue; 1263 1264 DEBUG(dbgs() << "\t\t\t\tusing masking\n"); 1265 1266 if (InstCnt) *InstCnt += NumAndInsts; 1267 1268 SDValue VRot; 1269 if (VRI.RLAmt) { 1270 SDValue Ops[] = 1271 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl), 1272 getI32Imm(31, dl) }; 1273 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, 1274 Ops), 0); 1275 } else { 1276 VRot = VRI.V; 1277 } 1278 1279 SDValue ANDIVal, ANDISVal; 1280 if (ANDIMask != 0) 1281 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32, 1282 VRot, getI32Imm(ANDIMask, dl)), 0); 1283 if (ANDISMask != 0) 1284 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32, 1285 VRot, getI32Imm(ANDISMask, dl)), 0); 1286 1287 SDValue TotalVal; 1288 if (!ANDIVal) 1289 TotalVal = ANDISVal; 1290 else if (!ANDISVal) 1291 TotalVal = ANDIVal; 1292 else 1293 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, 1294 ANDIVal, ANDISVal), 0); 1295 1296 if (!Res) 1297 Res = TotalVal; 1298 else 1299 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, 1300 Res, TotalVal), 0); 1301 1302 // Now, remove all groups with this underlying value and rotation 1303 // factor. 1304 for (auto I = BitGroups.begin(); I != BitGroups.end();) { 1305 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt) 1306 I = BitGroups.erase(I); 1307 else 1308 ++I; 1309 } 1310 } 1311 } 1312 1313 // Instruction selection for the 32-bit case. 1314 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) { 1315 SDLoc dl(N); 1316 SDValue Res; 1317 1318 if (InstCnt) *InstCnt = 0; 1319 1320 // Take care of cases that should use andi/andis first. 1321 SelectAndParts32(dl, Res, InstCnt); 1322 1323 // If we've not yet selected a 'starting' instruction, and we have no zeros 1324 // to fill in, select the (Value, RLAmt) with the highest priority (largest 1325 // number of groups), and start with this rotated value. 1326 if ((!HasZeros || LateMask) && !Res) { 1327 ValueRotInfo &VRI = ValueRotsVec[0]; 1328 if (VRI.RLAmt) { 1329 if (InstCnt) *InstCnt += 1; 1330 SDValue Ops[] = 1331 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl), 1332 getI32Imm(31, dl) }; 1333 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 1334 0); 1335 } else { 1336 Res = VRI.V; 1337 } 1338 1339 // Now, remove all groups with this underlying value and rotation factor. 1340 for (auto I = BitGroups.begin(); I != BitGroups.end();) { 1341 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt) 1342 I = BitGroups.erase(I); 1343 else 1344 ++I; 1345 } 1346 } 1347 1348 if (InstCnt) *InstCnt += BitGroups.size(); 1349 1350 // Insert the other groups (one at a time). 1351 for (auto &BG : BitGroups) { 1352 if (!Res) { 1353 SDValue Ops[] = 1354 { BG.V, getI32Imm(BG.RLAmt, dl), 1355 getI32Imm(Bits.size() - BG.EndIdx - 1, dl), 1356 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) }; 1357 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); 1358 } else { 1359 SDValue Ops[] = 1360 { Res, BG.V, getI32Imm(BG.RLAmt, dl), 1361 getI32Imm(Bits.size() - BG.EndIdx - 1, dl), 1362 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) }; 1363 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0); 1364 } 1365 } 1366 1367 if (LateMask) { 1368 unsigned Mask = (unsigned) getZerosMask(); 1369 1370 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16; 1371 assert((ANDIMask != 0 || ANDISMask != 0) && 1372 "No set bits in zeros mask?"); 1373 1374 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) + 1375 (unsigned) (ANDISMask != 0) + 1376 (unsigned) (ANDIMask != 0 && ANDISMask != 0); 1377 1378 SDValue ANDIVal, ANDISVal; 1379 if (ANDIMask != 0) 1380 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32, 1381 Res, getI32Imm(ANDIMask, dl)), 0); 1382 if (ANDISMask != 0) 1383 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32, 1384 Res, getI32Imm(ANDISMask, dl)), 0); 1385 1386 if (!ANDIVal) 1387 Res = ANDISVal; 1388 else if (!ANDISVal) 1389 Res = ANDIVal; 1390 else 1391 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, 1392 ANDIVal, ANDISVal), 0); 1393 } 1394 1395 return Res.getNode(); 1396 } 1397 1398 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32, 1399 unsigned MaskStart, unsigned MaskEnd, 1400 bool IsIns) { 1401 // In the notation used by the instructions, 'start' and 'end' are reversed 1402 // because bits are counted from high to low order. 1403 unsigned InstMaskStart = 64 - MaskEnd - 1, 1404 InstMaskEnd = 64 - MaskStart - 1; 1405 1406 if (Repl32) 1407 return 1; 1408 1409 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) || 1410 InstMaskEnd == 63 - RLAmt) 1411 return 1; 1412 1413 return 2; 1414 } 1415 1416 // For 64-bit values, not all combinations of rotates and masks are 1417 // available. Produce one if it is available. 1418 SDValue SelectRotMask64(SDValue V, SDLoc dl, unsigned RLAmt, bool Repl32, 1419 unsigned MaskStart, unsigned MaskEnd, 1420 unsigned *InstCnt = nullptr) { 1421 // In the notation used by the instructions, 'start' and 'end' are reversed 1422 // because bits are counted from high to low order. 1423 unsigned InstMaskStart = 64 - MaskEnd - 1, 1424 InstMaskEnd = 64 - MaskStart - 1; 1425 1426 if (InstCnt) *InstCnt += 1; 1427 1428 if (Repl32) { 1429 // This rotation amount assumes that the lower 32 bits of the quantity 1430 // are replicated in the high 32 bits by the rotation operator (which is 1431 // done by rlwinm and friends). 1432 assert(InstMaskStart >= 32 && "Mask cannot start out of range"); 1433 assert(InstMaskEnd >= 32 && "Mask cannot end out of range"); 1434 SDValue Ops[] = 1435 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl), 1436 getI32Imm(InstMaskEnd - 32, dl) }; 1437 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64, 1438 Ops), 0); 1439 } 1440 1441 if (InstMaskEnd == 63) { 1442 SDValue Ops[] = 1443 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) }; 1444 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0); 1445 } 1446 1447 if (InstMaskStart == 0) { 1448 SDValue Ops[] = 1449 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskEnd, dl) }; 1450 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0); 1451 } 1452 1453 if (InstMaskEnd == 63 - RLAmt) { 1454 SDValue Ops[] = 1455 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) }; 1456 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0); 1457 } 1458 1459 // We cannot do this with a single instruction, so we'll use two. The 1460 // problem is that we're not free to choose both a rotation amount and mask 1461 // start and end independently. We can choose an arbitrary mask start and 1462 // end, but then the rotation amount is fixed. Rotation, however, can be 1463 // inverted, and so by applying an "inverse" rotation first, we can get the 1464 // desired result. 1465 if (InstCnt) *InstCnt += 1; 1466 1467 // The rotation mask for the second instruction must be MaskStart. 1468 unsigned RLAmt2 = MaskStart; 1469 // The first instruction must rotate V so that the overall rotation amount 1470 // is RLAmt. 1471 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64; 1472 if (RLAmt1) 1473 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63); 1474 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd); 1475 } 1476 1477 // For 64-bit values, not all combinations of rotates and masks are 1478 // available. Produce a rotate-mask-and-insert if one is available. 1479 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, SDLoc dl, unsigned RLAmt, 1480 bool Repl32, unsigned MaskStart, 1481 unsigned MaskEnd, unsigned *InstCnt = nullptr) { 1482 // In the notation used by the instructions, 'start' and 'end' are reversed 1483 // because bits are counted from high to low order. 1484 unsigned InstMaskStart = 64 - MaskEnd - 1, 1485 InstMaskEnd = 64 - MaskStart - 1; 1486 1487 if (InstCnt) *InstCnt += 1; 1488 1489 if (Repl32) { 1490 // This rotation amount assumes that the lower 32 bits of the quantity 1491 // are replicated in the high 32 bits by the rotation operator (which is 1492 // done by rlwinm and friends). 1493 assert(InstMaskStart >= 32 && "Mask cannot start out of range"); 1494 assert(InstMaskEnd >= 32 && "Mask cannot end out of range"); 1495 SDValue Ops[] = 1496 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl), 1497 getI32Imm(InstMaskEnd - 32, dl) }; 1498 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64, 1499 Ops), 0); 1500 } 1501 1502 if (InstMaskEnd == 63 - RLAmt) { 1503 SDValue Ops[] = 1504 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) }; 1505 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0); 1506 } 1507 1508 // We cannot do this with a single instruction, so we'll use two. The 1509 // problem is that we're not free to choose both a rotation amount and mask 1510 // start and end independently. We can choose an arbitrary mask start and 1511 // end, but then the rotation amount is fixed. Rotation, however, can be 1512 // inverted, and so by applying an "inverse" rotation first, we can get the 1513 // desired result. 1514 if (InstCnt) *InstCnt += 1; 1515 1516 // The rotation mask for the second instruction must be MaskStart. 1517 unsigned RLAmt2 = MaskStart; 1518 // The first instruction must rotate V so that the overall rotation amount 1519 // is RLAmt. 1520 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64; 1521 if (RLAmt1) 1522 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63); 1523 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd); 1524 } 1525 1526 void SelectAndParts64(SDLoc dl, SDValue &Res, unsigned *InstCnt) { 1527 if (BPermRewriterNoMasking) 1528 return; 1529 1530 // The idea here is the same as in the 32-bit version, but with additional 1531 // complications from the fact that Repl32 might be true. Because we 1532 // aggressively convert bit groups to Repl32 form (which, for small 1533 // rotation factors, involves no other change), and then coalesce, it might 1534 // be the case that a single 64-bit masking operation could handle both 1535 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32 1536 // form allowed coalescing, then we must use a 32-bit rotaton in order to 1537 // completely capture the new combined bit group. 1538 1539 for (ValueRotInfo &VRI : ValueRotsVec) { 1540 uint64_t Mask = 0; 1541 1542 // We need to add to the mask all bits from the associated bit groups. 1543 // If Repl32 is false, we need to add bits from bit groups that have 1544 // Repl32 true, but are trivially convertable to Repl32 false. Such a 1545 // group is trivially convertable if it overlaps only with the lower 32 1546 // bits, and the group has not been coalesced. 1547 auto MatchingBG = [VRI](BitGroup &BG) { 1548 if (VRI.V != BG.V) 1549 return false; 1550 1551 unsigned EffRLAmt = BG.RLAmt; 1552 if (!VRI.Repl32 && BG.Repl32) { 1553 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx && 1554 !BG.Repl32Coalesced) { 1555 if (BG.Repl32CR) 1556 EffRLAmt += 32; 1557 } else { 1558 return false; 1559 } 1560 } else if (VRI.Repl32 != BG.Repl32) { 1561 return false; 1562 } 1563 1564 if (VRI.RLAmt != EffRLAmt) 1565 return false; 1566 1567 return true; 1568 }; 1569 1570 for (auto &BG : BitGroups) { 1571 if (!MatchingBG(BG)) 1572 continue; 1573 1574 if (BG.StartIdx <= BG.EndIdx) { 1575 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) 1576 Mask |= (UINT64_C(1) << i); 1577 } else { 1578 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) 1579 Mask |= (UINT64_C(1) << i); 1580 for (unsigned i = 0; i <= BG.EndIdx; ++i) 1581 Mask |= (UINT64_C(1) << i); 1582 } 1583 } 1584 1585 // We can use the 32-bit andi/andis technique if the mask does not 1586 // require any higher-order bits. This can save an instruction compared 1587 // to always using the general 64-bit technique. 1588 bool Use32BitInsts = isUInt<32>(Mask); 1589 // Compute the masks for andi/andis that would be necessary. 1590 unsigned ANDIMask = (Mask & UINT16_MAX), 1591 ANDISMask = (Mask >> 16) & UINT16_MAX; 1592 1593 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)); 1594 1595 unsigned NumAndInsts = (unsigned) NeedsRotate + 1596 (unsigned) (bool) Res; 1597 if (Use32BitInsts) 1598 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) + 1599 (unsigned) (ANDIMask != 0 && ANDISMask != 0); 1600 else 1601 NumAndInsts += SelectInt64Count(Mask) + /* and */ 1; 1602 1603 unsigned NumRLInsts = 0; 1604 bool FirstBG = true; 1605 for (auto &BG : BitGroups) { 1606 if (!MatchingBG(BG)) 1607 continue; 1608 NumRLInsts += 1609 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx, 1610 !FirstBG); 1611 FirstBG = false; 1612 } 1613 1614 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() << 1615 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") << 1616 "\n\t\t\tisel using masking: " << NumAndInsts << 1617 " using rotates: " << NumRLInsts << "\n"); 1618 1619 // When we'd use andi/andis, we bias toward using the rotates (andi only 1620 // has a record form, and is cracked on POWER cores). However, when using 1621 // general 64-bit constant formation, bias toward the constant form, 1622 // because that exposes more opportunities for CSE. 1623 if (NumAndInsts > NumRLInsts) 1624 continue; 1625 if (Use32BitInsts && NumAndInsts == NumRLInsts) 1626 continue; 1627 1628 DEBUG(dbgs() << "\t\t\t\tusing masking\n"); 1629 1630 if (InstCnt) *InstCnt += NumAndInsts; 1631 1632 SDValue VRot; 1633 // We actually need to generate a rotation if we have a non-zero rotation 1634 // factor or, in the Repl32 case, if we care about any of the 1635 // higher-order replicated bits. In the latter case, we generate a mask 1636 // backward so that it actually includes the entire 64 bits. 1637 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask))) 1638 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32, 1639 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63); 1640 else 1641 VRot = VRI.V; 1642 1643 SDValue TotalVal; 1644 if (Use32BitInsts) { 1645 assert((ANDIMask != 0 || ANDISMask != 0) && 1646 "No set bits in mask when using 32-bit ands for 64-bit value"); 1647 1648 SDValue ANDIVal, ANDISVal; 1649 if (ANDIMask != 0) 1650 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64, 1651 VRot, getI32Imm(ANDIMask, dl)), 0); 1652 if (ANDISMask != 0) 1653 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64, 1654 VRot, getI32Imm(ANDISMask, dl)), 0); 1655 1656 if (!ANDIVal) 1657 TotalVal = ANDISVal; 1658 else if (!ANDISVal) 1659 TotalVal = ANDIVal; 1660 else 1661 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, 1662 ANDIVal, ANDISVal), 0); 1663 } else { 1664 TotalVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0); 1665 TotalVal = 1666 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64, 1667 VRot, TotalVal), 0); 1668 } 1669 1670 if (!Res) 1671 Res = TotalVal; 1672 else 1673 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, 1674 Res, TotalVal), 0); 1675 1676 // Now, remove all groups with this underlying value and rotation 1677 // factor. 1678 for (auto I = BitGroups.begin(); I != BitGroups.end();) { 1679 if (MatchingBG(*I)) 1680 I = BitGroups.erase(I); 1681 else 1682 ++I; 1683 } 1684 } 1685 } 1686 1687 // Instruction selection for the 64-bit case. 1688 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) { 1689 SDLoc dl(N); 1690 SDValue Res; 1691 1692 if (InstCnt) *InstCnt = 0; 1693 1694 // Take care of cases that should use andi/andis first. 1695 SelectAndParts64(dl, Res, InstCnt); 1696 1697 // If we've not yet selected a 'starting' instruction, and we have no zeros 1698 // to fill in, select the (Value, RLAmt) with the highest priority (largest 1699 // number of groups), and start with this rotated value. 1700 if ((!HasZeros || LateMask) && !Res) { 1701 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32 1702 // groups will come first, and so the VRI representing the largest number 1703 // of groups might not be first (it might be the first Repl32 groups). 1704 unsigned MaxGroupsIdx = 0; 1705 if (!ValueRotsVec[0].Repl32) { 1706 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i) 1707 if (ValueRotsVec[i].Repl32) { 1708 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups) 1709 MaxGroupsIdx = i; 1710 break; 1711 } 1712 } 1713 1714 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx]; 1715 bool NeedsRotate = false; 1716 if (VRI.RLAmt) { 1717 NeedsRotate = true; 1718 } else if (VRI.Repl32) { 1719 for (auto &BG : BitGroups) { 1720 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt || 1721 BG.Repl32 != VRI.Repl32) 1722 continue; 1723 1724 // We don't need a rotate if the bit group is confined to the lower 1725 // 32 bits. 1726 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx) 1727 continue; 1728 1729 NeedsRotate = true; 1730 break; 1731 } 1732 } 1733 1734 if (NeedsRotate) 1735 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32, 1736 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63, 1737 InstCnt); 1738 else 1739 Res = VRI.V; 1740 1741 // Now, remove all groups with this underlying value and rotation factor. 1742 if (Res) 1743 for (auto I = BitGroups.begin(); I != BitGroups.end();) { 1744 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt && I->Repl32 == VRI.Repl32) 1745 I = BitGroups.erase(I); 1746 else 1747 ++I; 1748 } 1749 } 1750 1751 // Because 64-bit rotates are more flexible than inserts, we might have a 1752 // preference regarding which one we do first (to save one instruction). 1753 if (!Res) 1754 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) { 1755 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx, 1756 false) < 1757 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx, 1758 true)) { 1759 if (I != BitGroups.begin()) { 1760 BitGroup BG = *I; 1761 BitGroups.erase(I); 1762 BitGroups.insert(BitGroups.begin(), BG); 1763 } 1764 1765 break; 1766 } 1767 } 1768 1769 // Insert the other groups (one at a time). 1770 for (auto &BG : BitGroups) { 1771 if (!Res) 1772 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx, 1773 BG.EndIdx, InstCnt); 1774 else 1775 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32, 1776 BG.StartIdx, BG.EndIdx, InstCnt); 1777 } 1778 1779 if (LateMask) { 1780 uint64_t Mask = getZerosMask(); 1781 1782 // We can use the 32-bit andi/andis technique if the mask does not 1783 // require any higher-order bits. This can save an instruction compared 1784 // to always using the general 64-bit technique. 1785 bool Use32BitInsts = isUInt<32>(Mask); 1786 // Compute the masks for andi/andis that would be necessary. 1787 unsigned ANDIMask = (Mask & UINT16_MAX), 1788 ANDISMask = (Mask >> 16) & UINT16_MAX; 1789 1790 if (Use32BitInsts) { 1791 assert((ANDIMask != 0 || ANDISMask != 0) && 1792 "No set bits in mask when using 32-bit ands for 64-bit value"); 1793 1794 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) + 1795 (unsigned) (ANDISMask != 0) + 1796 (unsigned) (ANDIMask != 0 && ANDISMask != 0); 1797 1798 SDValue ANDIVal, ANDISVal; 1799 if (ANDIMask != 0) 1800 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64, 1801 Res, getI32Imm(ANDIMask, dl)), 0); 1802 if (ANDISMask != 0) 1803 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64, 1804 Res, getI32Imm(ANDISMask, dl)), 0); 1805 1806 if (!ANDIVal) 1807 Res = ANDISVal; 1808 else if (!ANDISVal) 1809 Res = ANDIVal; 1810 else 1811 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, 1812 ANDIVal, ANDISVal), 0); 1813 } else { 1814 if (InstCnt) *InstCnt += SelectInt64Count(Mask) + /* and */ 1; 1815 1816 SDValue MaskVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0); 1817 Res = 1818 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64, 1819 Res, MaskVal), 0); 1820 } 1821 } 1822 1823 return Res.getNode(); 1824 } 1825 1826 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) { 1827 // Fill in BitGroups. 1828 collectBitGroups(LateMask); 1829 if (BitGroups.empty()) 1830 return nullptr; 1831 1832 // For 64-bit values, figure out when we can use 32-bit instructions. 1833 if (Bits.size() == 64) 1834 assignRepl32BitGroups(); 1835 1836 // Fill in ValueRotsVec. 1837 collectValueRotInfo(); 1838 1839 if (Bits.size() == 32) { 1840 return Select32(N, LateMask, InstCnt); 1841 } else { 1842 assert(Bits.size() == 64 && "Not 64 bits here?"); 1843 return Select64(N, LateMask, InstCnt); 1844 } 1845 1846 return nullptr; 1847 } 1848 1849 SmallVector<ValueBit, 64> Bits; 1850 1851 bool HasZeros; 1852 SmallVector<unsigned, 64> RLAmt; 1853 1854 SmallVector<BitGroup, 16> BitGroups; 1855 1856 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots; 1857 SmallVector<ValueRotInfo, 16> ValueRotsVec; 1858 1859 SelectionDAG *CurDAG; 1860 1861 public: 1862 BitPermutationSelector(SelectionDAG *DAG) 1863 : CurDAG(DAG) {} 1864 1865 // Here we try to match complex bit permutations into a set of 1866 // rotate-and-shift/shift/and/or instructions, using a set of heuristics 1867 // known to produce optimial code for common cases (like i32 byte swapping). 1868 SDNode *Select(SDNode *N) { 1869 Bits.resize(N->getValueType(0).getSizeInBits()); 1870 if (!getValueBits(SDValue(N, 0), Bits)) 1871 return nullptr; 1872 1873 DEBUG(dbgs() << "Considering bit-permutation-based instruction" 1874 " selection for: "); 1875 DEBUG(N->dump(CurDAG)); 1876 1877 // Fill it RLAmt and set HasZeros. 1878 computeRotationAmounts(); 1879 1880 if (!HasZeros) 1881 return Select(N, false); 1882 1883 // We currently have two techniques for handling results with zeros: early 1884 // masking (the default) and late masking. Late masking is sometimes more 1885 // efficient, but because the structure of the bit groups is different, it 1886 // is hard to tell without generating both and comparing the results. With 1887 // late masking, we ignore zeros in the resulting value when inserting each 1888 // set of bit groups, and then mask in the zeros at the end. With early 1889 // masking, we only insert the non-zero parts of the result at every step. 1890 1891 unsigned InstCnt, InstCntLateMask; 1892 DEBUG(dbgs() << "\tEarly masking:\n"); 1893 SDNode *RN = Select(N, false, &InstCnt); 1894 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n"); 1895 1896 DEBUG(dbgs() << "\tLate masking:\n"); 1897 SDNode *RNLM = Select(N, true, &InstCntLateMask); 1898 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask << 1899 " instructions\n"); 1900 1901 if (InstCnt <= InstCntLateMask) { 1902 DEBUG(dbgs() << "\tUsing early-masking for isel\n"); 1903 return RN; 1904 } 1905 1906 DEBUG(dbgs() << "\tUsing late-masking for isel\n"); 1907 return RNLM; 1908 } 1909 }; 1910 } // anonymous namespace 1911 1912 SDNode *PPCDAGToDAGISel::SelectBitPermutation(SDNode *N) { 1913 if (N->getValueType(0) != MVT::i32 && 1914 N->getValueType(0) != MVT::i64) 1915 return nullptr; 1916 1917 if (!UseBitPermRewriter) 1918 return nullptr; 1919 1920 switch (N->getOpcode()) { 1921 default: break; 1922 case ISD::ROTL: 1923 case ISD::SHL: 1924 case ISD::SRL: 1925 case ISD::AND: 1926 case ISD::OR: { 1927 BitPermutationSelector BPS(CurDAG); 1928 return BPS.Select(N); 1929 } 1930 } 1931 1932 return nullptr; 1933 } 1934 1935 /// SelectCC - Select a comparison of the specified values with the specified 1936 /// condition code, returning the CR# of the expression. 1937 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, 1938 ISD::CondCode CC, SDLoc dl) { 1939 // Always select the LHS. 1940 unsigned Opc; 1941 1942 if (LHS.getValueType() == MVT::i32) { 1943 unsigned Imm; 1944 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 1945 if (isInt32Immediate(RHS, Imm)) { 1946 // SETEQ/SETNE comparison with 16-bit immediate, fold it. 1947 if (isUInt<16>(Imm)) 1948 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, 1949 getI32Imm(Imm & 0xFFFF, dl)), 1950 0); 1951 // If this is a 16-bit signed immediate, fold it. 1952 if (isInt<16>((int)Imm)) 1953 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, 1954 getI32Imm(Imm & 0xFFFF, dl)), 1955 0); 1956 1957 // For non-equality comparisons, the default code would materialize the 1958 // constant, then compare against it, like this: 1959 // lis r2, 4660 1960 // ori r2, r2, 22136 1961 // cmpw cr0, r3, r2 1962 // Since we are just comparing for equality, we can emit this instead: 1963 // xoris r0,r3,0x1234 1964 // cmplwi cr0,r0,0x5678 1965 // beq cr0,L6 1966 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS, 1967 getI32Imm(Imm >> 16, dl)), 0); 1968 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor, 1969 getI32Imm(Imm & 0xFFFF, dl)), 0); 1970 } 1971 Opc = PPC::CMPLW; 1972 } else if (ISD::isUnsignedIntSetCC(CC)) { 1973 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm)) 1974 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, 1975 getI32Imm(Imm & 0xFFFF, dl)), 0); 1976 Opc = PPC::CMPLW; 1977 } else { 1978 short SImm; 1979 if (isIntS16Immediate(RHS, SImm)) 1980 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, 1981 getI32Imm((int)SImm & 0xFFFF, 1982 dl)), 1983 0); 1984 Opc = PPC::CMPW; 1985 } 1986 } else if (LHS.getValueType() == MVT::i64) { 1987 uint64_t Imm; 1988 if (CC == ISD::SETEQ || CC == ISD::SETNE) { 1989 if (isInt64Immediate(RHS.getNode(), Imm)) { 1990 // SETEQ/SETNE comparison with 16-bit immediate, fold it. 1991 if (isUInt<16>(Imm)) 1992 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, 1993 getI32Imm(Imm & 0xFFFF, dl)), 1994 0); 1995 // If this is a 16-bit signed immediate, fold it. 1996 if (isInt<16>(Imm)) 1997 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, 1998 getI32Imm(Imm & 0xFFFF, dl)), 1999 0); 2000 2001 // For non-equality comparisons, the default code would materialize the 2002 // constant, then compare against it, like this: 2003 // lis r2, 4660 2004 // ori r2, r2, 22136 2005 // cmpd cr0, r3, r2 2006 // Since we are just comparing for equality, we can emit this instead: 2007 // xoris r0,r3,0x1234 2008 // cmpldi cr0,r0,0x5678 2009 // beq cr0,L6 2010 if (isUInt<32>(Imm)) { 2011 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS, 2012 getI64Imm(Imm >> 16, dl)), 0); 2013 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor, 2014 getI64Imm(Imm & 0xFFFF, dl)), 2015 0); 2016 } 2017 } 2018 Opc = PPC::CMPLD; 2019 } else if (ISD::isUnsignedIntSetCC(CC)) { 2020 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm)) 2021 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, 2022 getI64Imm(Imm & 0xFFFF, dl)), 0); 2023 Opc = PPC::CMPLD; 2024 } else { 2025 short SImm; 2026 if (isIntS16Immediate(RHS, SImm)) 2027 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, 2028 getI64Imm(SImm & 0xFFFF, dl)), 2029 0); 2030 Opc = PPC::CMPD; 2031 } 2032 } else if (LHS.getValueType() == MVT::f32) { 2033 Opc = PPC::FCMPUS; 2034 } else { 2035 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!"); 2036 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD; 2037 } 2038 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0); 2039 } 2040 2041 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) { 2042 switch (CC) { 2043 case ISD::SETUEQ: 2044 case ISD::SETONE: 2045 case ISD::SETOLE: 2046 case ISD::SETOGE: 2047 llvm_unreachable("Should be lowered by legalize!"); 2048 default: llvm_unreachable("Unknown condition!"); 2049 case ISD::SETOEQ: 2050 case ISD::SETEQ: return PPC::PRED_EQ; 2051 case ISD::SETUNE: 2052 case ISD::SETNE: return PPC::PRED_NE; 2053 case ISD::SETOLT: 2054 case ISD::SETLT: return PPC::PRED_LT; 2055 case ISD::SETULE: 2056 case ISD::SETLE: return PPC::PRED_LE; 2057 case ISD::SETOGT: 2058 case ISD::SETGT: return PPC::PRED_GT; 2059 case ISD::SETUGE: 2060 case ISD::SETGE: return PPC::PRED_GE; 2061 case ISD::SETO: return PPC::PRED_NU; 2062 case ISD::SETUO: return PPC::PRED_UN; 2063 // These two are invalid for floating point. Assume we have int. 2064 case ISD::SETULT: return PPC::PRED_LT; 2065 case ISD::SETUGT: return PPC::PRED_GT; 2066 } 2067 } 2068 2069 /// getCRIdxForSetCC - Return the index of the condition register field 2070 /// associated with the SetCC condition, and whether or not the field is 2071 /// treated as inverted. That is, lt = 0; ge = 0 inverted. 2072 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) { 2073 Invert = false; 2074 switch (CC) { 2075 default: llvm_unreachable("Unknown condition!"); 2076 case ISD::SETOLT: 2077 case ISD::SETLT: return 0; // Bit #0 = SETOLT 2078 case ISD::SETOGT: 2079 case ISD::SETGT: return 1; // Bit #1 = SETOGT 2080 case ISD::SETOEQ: 2081 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ 2082 case ISD::SETUO: return 3; // Bit #3 = SETUO 2083 case ISD::SETUGE: 2084 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE 2085 case ISD::SETULE: 2086 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE 2087 case ISD::SETUNE: 2088 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE 2089 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO 2090 case ISD::SETUEQ: 2091 case ISD::SETOGE: 2092 case ISD::SETOLE: 2093 case ISD::SETONE: 2094 llvm_unreachable("Invalid branch code: should be expanded by legalize"); 2095 // These are invalid for floating point. Assume integer. 2096 case ISD::SETULT: return 0; 2097 case ISD::SETUGT: return 1; 2098 } 2099 } 2100 2101 // getVCmpInst: return the vector compare instruction for the specified 2102 // vector type and condition code. Since this is for altivec specific code, 2103 // only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32). 2104 static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC, 2105 bool HasVSX, bool &Swap, bool &Negate) { 2106 Swap = false; 2107 Negate = false; 2108 2109 if (VecVT.isFloatingPoint()) { 2110 /* Handle some cases by swapping input operands. */ 2111 switch (CC) { 2112 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break; 2113 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break; 2114 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break; 2115 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break; 2116 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break; 2117 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break; 2118 default: break; 2119 } 2120 /* Handle some cases by negating the result. */ 2121 switch (CC) { 2122 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break; 2123 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break; 2124 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break; 2125 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break; 2126 default: break; 2127 } 2128 /* We have instructions implementing the remaining cases. */ 2129 switch (CC) { 2130 case ISD::SETEQ: 2131 case ISD::SETOEQ: 2132 if (VecVT == MVT::v4f32) 2133 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP; 2134 else if (VecVT == MVT::v2f64) 2135 return PPC::XVCMPEQDP; 2136 break; 2137 case ISD::SETGT: 2138 case ISD::SETOGT: 2139 if (VecVT == MVT::v4f32) 2140 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP; 2141 else if (VecVT == MVT::v2f64) 2142 return PPC::XVCMPGTDP; 2143 break; 2144 case ISD::SETGE: 2145 case ISD::SETOGE: 2146 if (VecVT == MVT::v4f32) 2147 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP; 2148 else if (VecVT == MVT::v2f64) 2149 return PPC::XVCMPGEDP; 2150 break; 2151 default: 2152 break; 2153 } 2154 llvm_unreachable("Invalid floating-point vector compare condition"); 2155 } else { 2156 /* Handle some cases by swapping input operands. */ 2157 switch (CC) { 2158 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break; 2159 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break; 2160 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break; 2161 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break; 2162 default: break; 2163 } 2164 /* Handle some cases by negating the result. */ 2165 switch (CC) { 2166 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break; 2167 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break; 2168 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break; 2169 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break; 2170 default: break; 2171 } 2172 /* We have instructions implementing the remaining cases. */ 2173 switch (CC) { 2174 case ISD::SETEQ: 2175 case ISD::SETUEQ: 2176 if (VecVT == MVT::v16i8) 2177 return PPC::VCMPEQUB; 2178 else if (VecVT == MVT::v8i16) 2179 return PPC::VCMPEQUH; 2180 else if (VecVT == MVT::v4i32) 2181 return PPC::VCMPEQUW; 2182 else if (VecVT == MVT::v2i64) 2183 return PPC::VCMPEQUD; 2184 break; 2185 case ISD::SETGT: 2186 if (VecVT == MVT::v16i8) 2187 return PPC::VCMPGTSB; 2188 else if (VecVT == MVT::v8i16) 2189 return PPC::VCMPGTSH; 2190 else if (VecVT == MVT::v4i32) 2191 return PPC::VCMPGTSW; 2192 else if (VecVT == MVT::v2i64) 2193 return PPC::VCMPGTSD; 2194 break; 2195 case ISD::SETUGT: 2196 if (VecVT == MVT::v16i8) 2197 return PPC::VCMPGTUB; 2198 else if (VecVT == MVT::v8i16) 2199 return PPC::VCMPGTUH; 2200 else if (VecVT == MVT::v4i32) 2201 return PPC::VCMPGTUW; 2202 else if (VecVT == MVT::v2i64) 2203 return PPC::VCMPGTUD; 2204 break; 2205 default: 2206 break; 2207 } 2208 llvm_unreachable("Invalid integer vector compare condition"); 2209 } 2210 } 2211 2212 SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) { 2213 SDLoc dl(N); 2214 unsigned Imm; 2215 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 2216 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy(); 2217 bool isPPC64 = (PtrVT == MVT::i64); 2218 2219 if (!PPCSubTarget->useCRBits() && 2220 isInt32Immediate(N->getOperand(1), Imm)) { 2221 // We can codegen setcc op, imm very efficiently compared to a brcond. 2222 // Check for those cases here. 2223 // setcc op, 0 2224 if (Imm == 0) { 2225 SDValue Op = N->getOperand(0); 2226 switch (CC) { 2227 default: break; 2228 case ISD::SETEQ: { 2229 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0); 2230 SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl), 2231 getI32Imm(31, dl) }; 2232 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2233 } 2234 case ISD::SETNE: { 2235 if (isPPC64) break; 2236 SDValue AD = 2237 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, 2238 Op, getI32Imm(~0U, dl)), 0); 2239 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, 2240 AD.getValue(1)); 2241 } 2242 case ISD::SETLT: { 2243 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl), 2244 getI32Imm(31, dl) }; 2245 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2246 } 2247 case ISD::SETGT: { 2248 SDValue T = 2249 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0); 2250 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0); 2251 SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl), 2252 getI32Imm(31, dl) }; 2253 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2254 } 2255 } 2256 } else if (Imm == ~0U) { // setcc op, -1 2257 SDValue Op = N->getOperand(0); 2258 switch (CC) { 2259 default: break; 2260 case ISD::SETEQ: 2261 if (isPPC64) break; 2262 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, 2263 Op, getI32Imm(1, dl)), 0); 2264 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 2265 SDValue(CurDAG->getMachineNode(PPC::LI, dl, 2266 MVT::i32, 2267 getI32Imm(0, dl)), 2268 0), Op.getValue(1)); 2269 case ISD::SETNE: { 2270 if (isPPC64) break; 2271 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0); 2272 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, 2273 Op, getI32Imm(~0U, dl)); 2274 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), 2275 Op, SDValue(AD, 1)); 2276 } 2277 case ISD::SETLT: { 2278 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op, 2279 getI32Imm(1, dl)), 0); 2280 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD, 2281 Op), 0); 2282 SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl), 2283 getI32Imm(31, dl) }; 2284 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2285 } 2286 case ISD::SETGT: { 2287 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl), 2288 getI32Imm(31, dl) }; 2289 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); 2290 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, 2291 getI32Imm(1, dl)); 2292 } 2293 } 2294 } 2295 } 2296 2297 SDValue LHS = N->getOperand(0); 2298 SDValue RHS = N->getOperand(1); 2299 2300 // Altivec Vector compare instructions do not set any CR register by default and 2301 // vector compare operations return the same type as the operands. 2302 if (LHS.getValueType().isVector()) { 2303 if (PPCSubTarget->hasQPX()) 2304 return nullptr; 2305 2306 EVT VecVT = LHS.getValueType(); 2307 bool Swap, Negate; 2308 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC, 2309 PPCSubTarget->hasVSX(), Swap, Negate); 2310 if (Swap) 2311 std::swap(LHS, RHS); 2312 2313 if (Negate) { 2314 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0); 2315 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR : 2316 PPC::VNOR, 2317 VecVT, VCmp, VCmp); 2318 } 2319 2320 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS); 2321 } 2322 2323 if (PPCSubTarget->useCRBits()) 2324 return nullptr; 2325 2326 bool Inv; 2327 unsigned Idx = getCRIdxForSetCC(CC, Inv); 2328 SDValue CCReg = SelectCC(LHS, RHS, CC, dl); 2329 SDValue IntCR; 2330 2331 // Force the ccreg into CR7. 2332 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); 2333 2334 SDValue InFlag(nullptr, 0); // Null incoming flag value. 2335 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, 2336 InFlag).getValue(1); 2337 2338 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg, 2339 CCReg), 0); 2340 2341 SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl), 2342 getI32Imm(31, dl), getI32Imm(31, dl) }; 2343 if (!Inv) 2344 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2345 2346 // Get the specified bit. 2347 SDValue Tmp = 2348 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); 2349 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl)); 2350 } 2351 2352 SDNode *PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) { 2353 // Transfer memoperands. 2354 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); 2355 MemOp[0] = cast<MemSDNode>(N)->getMemOperand(); 2356 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1); 2357 return Result; 2358 } 2359 2360 2361 // Select - Convert the specified operand from a target-independent to a 2362 // target-specific node if it hasn't already been changed. 2363 SDNode *PPCDAGToDAGISel::Select(SDNode *N) { 2364 SDLoc dl(N); 2365 if (N->isMachineOpcode()) { 2366 N->setNodeId(-1); 2367 return nullptr; // Already selected. 2368 } 2369 2370 // In case any misguided DAG-level optimizations form an ADD with a 2371 // TargetConstant operand, crash here instead of miscompiling (by selecting 2372 // an r+r add instead of some kind of r+i add). 2373 if (N->getOpcode() == ISD::ADD && 2374 N->getOperand(1).getOpcode() == ISD::TargetConstant) 2375 llvm_unreachable("Invalid ADD with TargetConstant operand"); 2376 2377 // Try matching complex bit permutations before doing anything else. 2378 if (SDNode *NN = SelectBitPermutation(N)) 2379 return NN; 2380 2381 switch (N->getOpcode()) { 2382 default: break; 2383 2384 case ISD::Constant: { 2385 if (N->getValueType(0) == MVT::i64) 2386 return SelectInt64(CurDAG, N); 2387 break; 2388 } 2389 2390 case ISD::SETCC: { 2391 SDNode *SN = SelectSETCC(N); 2392 if (SN) 2393 return SN; 2394 break; 2395 } 2396 case PPCISD::GlobalBaseReg: 2397 return getGlobalBaseReg(); 2398 2399 case ISD::FrameIndex: 2400 return getFrameIndex(N, N); 2401 2402 case PPCISD::MFOCRF: { 2403 SDValue InFlag = N->getOperand(1); 2404 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, 2405 N->getOperand(0), InFlag); 2406 } 2407 2408 case PPCISD::READ_TIME_BASE: { 2409 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32, 2410 MVT::Other, N->getOperand(0)); 2411 } 2412 2413 case PPCISD::SRA_ADDZE: { 2414 SDValue N0 = N->getOperand(0); 2415 SDValue ShiftAmt = 2416 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))-> 2417 getConstantIntValue(), dl, 2418 N->getValueType(0)); 2419 if (N->getValueType(0) == MVT::i64) { 2420 SDNode *Op = 2421 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue, 2422 N0, ShiftAmt); 2423 return CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64, 2424 SDValue(Op, 0), SDValue(Op, 1)); 2425 } else { 2426 assert(N->getValueType(0) == MVT::i32 && 2427 "Expecting i64 or i32 in PPCISD::SRA_ADDZE"); 2428 SDNode *Op = 2429 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue, 2430 N0, ShiftAmt); 2431 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 2432 SDValue(Op, 0), SDValue(Op, 1)); 2433 } 2434 } 2435 2436 case ISD::LOAD: { 2437 // Handle preincrement loads. 2438 LoadSDNode *LD = cast<LoadSDNode>(N); 2439 EVT LoadedVT = LD->getMemoryVT(); 2440 2441 // Normal loads are handled by code generated from the .td file. 2442 if (LD->getAddressingMode() != ISD::PRE_INC) 2443 break; 2444 2445 SDValue Offset = LD->getOffset(); 2446 if (Offset.getOpcode() == ISD::TargetConstant || 2447 Offset.getOpcode() == ISD::TargetGlobalAddress) { 2448 2449 unsigned Opcode; 2450 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; 2451 if (LD->getValueType(0) != MVT::i64) { 2452 // Handle PPC32 integer and normal FP loads. 2453 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); 2454 switch (LoadedVT.getSimpleVT().SimpleTy) { 2455 default: llvm_unreachable("Invalid PPC load type!"); 2456 case MVT::f64: Opcode = PPC::LFDU; break; 2457 case MVT::f32: Opcode = PPC::LFSU; break; 2458 case MVT::i32: Opcode = PPC::LWZU; break; 2459 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break; 2460 case MVT::i1: 2461 case MVT::i8: Opcode = PPC::LBZU; break; 2462 } 2463 } else { 2464 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); 2465 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); 2466 switch (LoadedVT.getSimpleVT().SimpleTy) { 2467 default: llvm_unreachable("Invalid PPC load type!"); 2468 case MVT::i64: Opcode = PPC::LDU; break; 2469 case MVT::i32: Opcode = PPC::LWZU8; break; 2470 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break; 2471 case MVT::i1: 2472 case MVT::i8: Opcode = PPC::LBZU8; break; 2473 } 2474 } 2475 2476 SDValue Chain = LD->getChain(); 2477 SDValue Base = LD->getBasePtr(); 2478 SDValue Ops[] = { Offset, Base, Chain }; 2479 return transferMemOperands(N, CurDAG->getMachineNode(Opcode, dl, 2480 LD->getValueType(0), 2481 PPCLowering->getPointerTy(), 2482 MVT::Other, Ops)); 2483 } else { 2484 unsigned Opcode; 2485 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; 2486 if (LD->getValueType(0) != MVT::i64) { 2487 // Handle PPC32 integer and normal FP loads. 2488 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); 2489 switch (LoadedVT.getSimpleVT().SimpleTy) { 2490 default: llvm_unreachable("Invalid PPC load type!"); 2491 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX 2492 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX 2493 case MVT::f64: Opcode = PPC::LFDUX; break; 2494 case MVT::f32: Opcode = PPC::LFSUX; break; 2495 case MVT::i32: Opcode = PPC::LWZUX; break; 2496 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break; 2497 case MVT::i1: 2498 case MVT::i8: Opcode = PPC::LBZUX; break; 2499 } 2500 } else { 2501 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); 2502 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) && 2503 "Invalid sext update load"); 2504 switch (LoadedVT.getSimpleVT().SimpleTy) { 2505 default: llvm_unreachable("Invalid PPC load type!"); 2506 case MVT::i64: Opcode = PPC::LDUX; break; 2507 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break; 2508 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break; 2509 case MVT::i1: 2510 case MVT::i8: Opcode = PPC::LBZUX8; break; 2511 } 2512 } 2513 2514 SDValue Chain = LD->getChain(); 2515 SDValue Base = LD->getBasePtr(); 2516 SDValue Ops[] = { Base, Offset, Chain }; 2517 return transferMemOperands(N, CurDAG->getMachineNode(Opcode, dl, 2518 LD->getValueType(0), 2519 PPCLowering->getPointerTy(), 2520 MVT::Other, Ops)); 2521 } 2522 } 2523 2524 case ISD::AND: { 2525 unsigned Imm, Imm2, SH, MB, ME; 2526 uint64_t Imm64; 2527 2528 // If this is an and of a value rotated between 0 and 31 bits and then and'd 2529 // with a mask, emit rlwinm 2530 if (isInt32Immediate(N->getOperand(1), Imm) && 2531 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) { 2532 SDValue Val = N->getOperand(0).getOperand(0); 2533 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl), 2534 getI32Imm(ME, dl) }; 2535 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2536 } 2537 // If this is just a masked value where the input is not handled above, and 2538 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm 2539 if (isInt32Immediate(N->getOperand(1), Imm) && 2540 isRunOfOnes(Imm, MB, ME) && 2541 N->getOperand(0).getOpcode() != ISD::ROTL) { 2542 SDValue Val = N->getOperand(0); 2543 SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl), 2544 getI32Imm(ME, dl) }; 2545 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2546 } 2547 // If this is a 64-bit zero-extension mask, emit rldicl. 2548 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) && 2549 isMask_64(Imm64)) { 2550 SDValue Val = N->getOperand(0); 2551 MB = 64 - countTrailingOnes(Imm64); 2552 SH = 0; 2553 2554 // If the operand is a logical right shift, we can fold it into this 2555 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb) 2556 // for n <= mb. The right shift is really a left rotate followed by a 2557 // mask, and this mask is a more-restrictive sub-mask of the mask implied 2558 // by the shift. 2559 if (Val.getOpcode() == ISD::SRL && 2560 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) { 2561 assert(Imm < 64 && "Illegal shift amount"); 2562 Val = Val.getOperand(0); 2563 SH = 64 - Imm; 2564 } 2565 2566 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) }; 2567 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops); 2568 } 2569 // AND X, 0 -> 0, not "rlwinm 32". 2570 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) { 2571 ReplaceUses(SDValue(N, 0), N->getOperand(1)); 2572 return nullptr; 2573 } 2574 // ISD::OR doesn't get all the bitfield insertion fun. 2575 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert 2576 if (isInt32Immediate(N->getOperand(1), Imm) && 2577 N->getOperand(0).getOpcode() == ISD::OR && 2578 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) { 2579 unsigned MB, ME; 2580 Imm = ~(Imm^Imm2); 2581 if (isRunOfOnes(Imm, MB, ME)) { 2582 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2583 N->getOperand(0).getOperand(1), 2584 getI32Imm(0, dl), getI32Imm(MB, dl), 2585 getI32Imm(ME, dl) }; 2586 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops); 2587 } 2588 } 2589 2590 // Other cases are autogenerated. 2591 break; 2592 } 2593 case ISD::OR: { 2594 if (N->getValueType(0) == MVT::i32) 2595 if (SDNode *I = SelectBitfieldInsert(N)) 2596 return I; 2597 2598 short Imm; 2599 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex && 2600 isIntS16Immediate(N->getOperand(1), Imm)) { 2601 APInt LHSKnownZero, LHSKnownOne; 2602 CurDAG->computeKnownBits(N->getOperand(0), LHSKnownZero, LHSKnownOne); 2603 2604 // If this is equivalent to an add, then we can fold it with the 2605 // FrameIndex calculation. 2606 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) 2607 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm); 2608 } 2609 2610 // Other cases are autogenerated. 2611 break; 2612 } 2613 case ISD::ADD: { 2614 short Imm; 2615 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex && 2616 isIntS16Immediate(N->getOperand(1), Imm)) 2617 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm); 2618 2619 break; 2620 } 2621 case ISD::SHL: { 2622 unsigned Imm, SH, MB, ME; 2623 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && 2624 isRotateAndMask(N, Imm, true, SH, MB, ME)) { 2625 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2626 getI32Imm(SH, dl), getI32Imm(MB, dl), 2627 getI32Imm(ME, dl) }; 2628 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2629 } 2630 2631 // Other cases are autogenerated. 2632 break; 2633 } 2634 case ISD::SRL: { 2635 unsigned Imm, SH, MB, ME; 2636 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && 2637 isRotateAndMask(N, Imm, true, SH, MB, ME)) { 2638 SDValue Ops[] = { N->getOperand(0).getOperand(0), 2639 getI32Imm(SH, dl), getI32Imm(MB, dl), 2640 getI32Imm(ME, dl) }; 2641 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); 2642 } 2643 2644 // Other cases are autogenerated. 2645 break; 2646 } 2647 // FIXME: Remove this once the ANDI glue bug is fixed: 2648 case PPCISD::ANDIo_1_EQ_BIT: 2649 case PPCISD::ANDIo_1_GT_BIT: { 2650 if (!ANDIGlueBug) 2651 break; 2652 2653 EVT InVT = N->getOperand(0).getValueType(); 2654 assert((InVT == MVT::i64 || InVT == MVT::i32) && 2655 "Invalid input type for ANDIo_1_EQ_BIT"); 2656 2657 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo; 2658 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue, 2659 N->getOperand(0), 2660 CurDAG->getTargetConstant(1, dl, InVT)), 2661 0); 2662 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32); 2663 SDValue SRIdxVal = 2664 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ? 2665 PPC::sub_eq : PPC::sub_gt, dl, MVT::i32); 2666 2667 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1, 2668 CR0Reg, SRIdxVal, 2669 SDValue(AndI.getNode(), 1) /* glue */); 2670 } 2671 case ISD::SELECT_CC: { 2672 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 2673 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy(); 2674 bool isPPC64 = (PtrVT == MVT::i64); 2675 2676 // If this is a select of i1 operands, we'll pattern match it. 2677 if (PPCSubTarget->useCRBits() && 2678 N->getOperand(0).getValueType() == MVT::i1) 2679 break; 2680 2681 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc 2682 if (!isPPC64) 2683 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1))) 2684 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2))) 2685 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3))) 2686 if (N1C->isNullValue() && N3C->isNullValue() && 2687 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE && 2688 // FIXME: Implement this optzn for PPC64. 2689 N->getValueType(0) == MVT::i32) { 2690 SDNode *Tmp = 2691 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, 2692 N->getOperand(0), getI32Imm(~0U, dl)); 2693 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, 2694 SDValue(Tmp, 0), N->getOperand(0), 2695 SDValue(Tmp, 1)); 2696 } 2697 2698 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); 2699 2700 if (N->getValueType(0) == MVT::i1) { 2701 // An i1 select is: (c & t) | (!c & f). 2702 bool Inv; 2703 unsigned Idx = getCRIdxForSetCC(CC, Inv); 2704 2705 unsigned SRI; 2706 switch (Idx) { 2707 default: llvm_unreachable("Invalid CC index"); 2708 case 0: SRI = PPC::sub_lt; break; 2709 case 1: SRI = PPC::sub_gt; break; 2710 case 2: SRI = PPC::sub_eq; break; 2711 case 3: SRI = PPC::sub_un; break; 2712 } 2713 2714 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg); 2715 2716 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1, 2717 CCBit, CCBit), 0); 2718 SDValue C = Inv ? NotCCBit : CCBit, 2719 NotC = Inv ? CCBit : NotCCBit; 2720 2721 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1, 2722 C, N->getOperand(2)), 0); 2723 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1, 2724 NotC, N->getOperand(3)), 0); 2725 2726 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF); 2727 } 2728 2729 unsigned BROpc = getPredicateForSetCC(CC); 2730 2731 unsigned SelectCCOp; 2732 if (N->getValueType(0) == MVT::i32) 2733 SelectCCOp = PPC::SELECT_CC_I4; 2734 else if (N->getValueType(0) == MVT::i64) 2735 SelectCCOp = PPC::SELECT_CC_I8; 2736 else if (N->getValueType(0) == MVT::f32) 2737 SelectCCOp = PPC::SELECT_CC_F4; 2738 else if (N->getValueType(0) == MVT::f64) 2739 if (PPCSubTarget->hasVSX()) 2740 SelectCCOp = PPC::SELECT_CC_VSFRC; 2741 else 2742 SelectCCOp = PPC::SELECT_CC_F8; 2743 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64) 2744 SelectCCOp = PPC::SELECT_CC_QFRC; 2745 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32) 2746 SelectCCOp = PPC::SELECT_CC_QSRC; 2747 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1) 2748 SelectCCOp = PPC::SELECT_CC_QBRC; 2749 else if (N->getValueType(0) == MVT::v2f64 || 2750 N->getValueType(0) == MVT::v2i64) 2751 SelectCCOp = PPC::SELECT_CC_VSRC; 2752 else 2753 SelectCCOp = PPC::SELECT_CC_VRRC; 2754 2755 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3), 2756 getI32Imm(BROpc, dl) }; 2757 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops); 2758 } 2759 case ISD::VSELECT: 2760 if (PPCSubTarget->hasVSX()) { 2761 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) }; 2762 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops); 2763 } 2764 2765 break; 2766 case ISD::VECTOR_SHUFFLE: 2767 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 || 2768 N->getValueType(0) == MVT::v2i64)) { 2769 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 2770 2771 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1), 2772 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1); 2773 unsigned DM[2]; 2774 2775 for (int i = 0; i < 2; ++i) 2776 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2) 2777 DM[i] = 0; 2778 else 2779 DM[i] = 1; 2780 2781 // For little endian, we must swap the input operands and adjust 2782 // the mask elements (reverse and invert them). 2783 if (PPCSubTarget->isLittleEndian()) { 2784 std::swap(Op1, Op2); 2785 unsigned tmp = DM[0]; 2786 DM[0] = 1 - DM[1]; 2787 DM[1] = 1 - tmp; 2788 } 2789 2790 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl, 2791 MVT::i32); 2792 2793 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 && 2794 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR && 2795 isa<LoadSDNode>(Op1.getOperand(0))) { 2796 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0)); 2797 SDValue Base, Offset; 2798 2799 if (LD->isUnindexed() && 2800 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) { 2801 SDValue Chain = LD->getChain(); 2802 SDValue Ops[] = { Base, Offset, Chain }; 2803 return CurDAG->SelectNodeTo(N, PPC::LXVDSX, 2804 N->getValueType(0), Ops); 2805 } 2806 } 2807 2808 SDValue Ops[] = { Op1, Op2, DMV }; 2809 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops); 2810 } 2811 2812 break; 2813 case PPCISD::BDNZ: 2814 case PPCISD::BDZ: { 2815 bool IsPPC64 = PPCSubTarget->isPPC64(); 2816 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) }; 2817 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ? 2818 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 2819 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ), 2820 MVT::Other, Ops); 2821 } 2822 case PPCISD::COND_BRANCH: { 2823 // Op #0 is the Chain. 2824 // Op #1 is the PPC::PRED_* number. 2825 // Op #2 is the CR# 2826 // Op #3 is the Dest MBB 2827 // Op #4 is the Flag. 2828 // Prevent PPC::PRED_* from being selected into LI. 2829 SDValue Pred = 2830 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(), dl); 2831 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3), 2832 N->getOperand(0), N->getOperand(4) }; 2833 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); 2834 } 2835 case ISD::BR_CC: { 2836 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); 2837 unsigned PCC = getPredicateForSetCC(CC); 2838 2839 if (N->getOperand(2).getValueType() == MVT::i1) { 2840 unsigned Opc; 2841 bool Swap; 2842 switch (PCC) { 2843 default: llvm_unreachable("Unexpected Boolean-operand predicate"); 2844 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break; 2845 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break; 2846 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break; 2847 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break; 2848 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break; 2849 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break; 2850 } 2851 2852 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1, 2853 N->getOperand(Swap ? 3 : 2), 2854 N->getOperand(Swap ? 2 : 3)), 0); 2855 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other, 2856 BitComp, N->getOperand(4), N->getOperand(0)); 2857 } 2858 2859 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl); 2860 SDValue Ops[] = { getI32Imm(PCC, dl), CondCode, 2861 N->getOperand(4), N->getOperand(0) }; 2862 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); 2863 } 2864 case ISD::BRIND: { 2865 // FIXME: Should custom lower this. 2866 SDValue Chain = N->getOperand(0); 2867 SDValue Target = N->getOperand(1); 2868 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8; 2869 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8; 2870 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target, 2871 Chain), 0); 2872 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain); 2873 } 2874 case PPCISD::TOC_ENTRY: { 2875 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) && 2876 "Only supported for 64-bit ABI and 32-bit SVR4"); 2877 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) { 2878 SDValue GA = N->getOperand(0); 2879 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LWZtoc, dl, 2880 MVT::i32, GA, N->getOperand(1))); 2881 } 2882 2883 // For medium and large code model, we generate two instructions as 2884 // described below. Otherwise we allow SelectCodeCommon to handle this, 2885 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA. 2886 CodeModel::Model CModel = TM.getCodeModel(); 2887 if (CModel != CodeModel::Medium && CModel != CodeModel::Large) 2888 break; 2889 2890 // The first source operand is a TargetGlobalAddress or a TargetJumpTable. 2891 // If it is an externally defined symbol, a symbol with common linkage, 2892 // a non-local function address, or a jump table address, or if we are 2893 // generating code for large code model, we generate: 2894 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>)) 2895 // Otherwise we generate: 2896 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>) 2897 SDValue GA = N->getOperand(0); 2898 SDValue TOCbase = N->getOperand(1); 2899 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64, 2900 TOCbase, GA); 2901 2902 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) || 2903 CModel == CodeModel::Large) 2904 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl, 2905 MVT::i64, GA, SDValue(Tmp, 0))); 2906 2907 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) { 2908 const GlobalValue *GValue = G->getGlobal(); 2909 if ((GValue->getType()->getElementType()->isFunctionTy() && 2910 (GValue->isDeclaration() || GValue->isWeakForLinker())) || 2911 GValue->isDeclaration() || GValue->hasCommonLinkage() || 2912 GValue->hasAvailableExternallyLinkage()) 2913 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl, 2914 MVT::i64, GA, SDValue(Tmp, 0))); 2915 } 2916 2917 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64, 2918 SDValue(Tmp, 0), GA); 2919 } 2920 case PPCISD::PPC32_PICGOT: { 2921 // Generate a PIC-safe GOT reference. 2922 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() && 2923 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4"); 2924 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32); 2925 } 2926 case PPCISD::VADD_SPLAT: { 2927 // This expands into one of three sequences, depending on whether 2928 // the first operand is odd or even, positive or negative. 2929 assert(isa<ConstantSDNode>(N->getOperand(0)) && 2930 isa<ConstantSDNode>(N->getOperand(1)) && 2931 "Invalid operand on VADD_SPLAT!"); 2932 2933 int Elt = N->getConstantOperandVal(0); 2934 int EltSize = N->getConstantOperandVal(1); 2935 unsigned Opc1, Opc2, Opc3; 2936 EVT VT; 2937 2938 if (EltSize == 1) { 2939 Opc1 = PPC::VSPLTISB; 2940 Opc2 = PPC::VADDUBM; 2941 Opc3 = PPC::VSUBUBM; 2942 VT = MVT::v16i8; 2943 } else if (EltSize == 2) { 2944 Opc1 = PPC::VSPLTISH; 2945 Opc2 = PPC::VADDUHM; 2946 Opc3 = PPC::VSUBUHM; 2947 VT = MVT::v8i16; 2948 } else { 2949 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!"); 2950 Opc1 = PPC::VSPLTISW; 2951 Opc2 = PPC::VADDUWM; 2952 Opc3 = PPC::VSUBUWM; 2953 VT = MVT::v4i32; 2954 } 2955 2956 if ((Elt & 1) == 0) { 2957 // Elt is even, in the range [-32,-18] + [16,30]. 2958 // 2959 // Convert: VADD_SPLAT elt, size 2960 // Into: tmp = VSPLTIS[BHW] elt 2961 // VADDU[BHW]M tmp, tmp 2962 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4 2963 SDValue EltVal = getI32Imm(Elt >> 1, dl); 2964 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); 2965 SDValue TmpVal = SDValue(Tmp, 0); 2966 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal); 2967 2968 } else if (Elt > 0) { 2969 // Elt is odd and positive, in the range [17,31]. 2970 // 2971 // Convert: VADD_SPLAT elt, size 2972 // Into: tmp1 = VSPLTIS[BHW] elt-16 2973 // tmp2 = VSPLTIS[BHW] -16 2974 // VSUBU[BHW]M tmp1, tmp2 2975 SDValue EltVal = getI32Imm(Elt - 16, dl); 2976 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); 2977 EltVal = getI32Imm(-16, dl); 2978 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); 2979 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0), 2980 SDValue(Tmp2, 0)); 2981 2982 } else { 2983 // Elt is odd and negative, in the range [-31,-17]. 2984 // 2985 // Convert: VADD_SPLAT elt, size 2986 // Into: tmp1 = VSPLTIS[BHW] elt+16 2987 // tmp2 = VSPLTIS[BHW] -16 2988 // VADDU[BHW]M tmp1, tmp2 2989 SDValue EltVal = getI32Imm(Elt + 16, dl); 2990 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); 2991 EltVal = getI32Imm(-16, dl); 2992 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); 2993 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0), 2994 SDValue(Tmp2, 0)); 2995 } 2996 } 2997 } 2998 2999 return SelectCode(N); 3000 } 3001 3002 // If the target supports the cmpb instruction, do the idiom recognition here. 3003 // We don't do this as a DAG combine because we don't want to do it as nodes 3004 // are being combined (because we might miss part of the eventual idiom). We 3005 // don't want to do it during instruction selection because we want to reuse 3006 // the logic for lowering the masking operations already part of the 3007 // instruction selector. 3008 SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) { 3009 SDLoc dl(N); 3010 3011 assert(N->getOpcode() == ISD::OR && 3012 "Only OR nodes are supported for CMPB"); 3013 3014 SDValue Res; 3015 if (!PPCSubTarget->hasCMPB()) 3016 return Res; 3017 3018 if (N->getValueType(0) != MVT::i32 && 3019 N->getValueType(0) != MVT::i64) 3020 return Res; 3021 3022 EVT VT = N->getValueType(0); 3023 3024 SDValue RHS, LHS; 3025 bool BytesFound[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 3026 uint64_t Mask = 0, Alt = 0; 3027 3028 auto IsByteSelectCC = [this](SDValue O, unsigned &b, 3029 uint64_t &Mask, uint64_t &Alt, 3030 SDValue &LHS, SDValue &RHS) { 3031 if (O.getOpcode() != ISD::SELECT_CC) 3032 return false; 3033 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get(); 3034 3035 if (!isa<ConstantSDNode>(O.getOperand(2)) || 3036 !isa<ConstantSDNode>(O.getOperand(3))) 3037 return false; 3038 3039 uint64_t PM = O.getConstantOperandVal(2); 3040 uint64_t PAlt = O.getConstantOperandVal(3); 3041 for (b = 0; b < 8; ++b) { 3042 uint64_t Mask = UINT64_C(0xFF) << (8*b); 3043 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt) 3044 break; 3045 } 3046 3047 if (b == 8) 3048 return false; 3049 Mask |= PM; 3050 Alt |= PAlt; 3051 3052 if (!isa<ConstantSDNode>(O.getOperand(1)) || 3053 O.getConstantOperandVal(1) != 0) { 3054 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1); 3055 if (Op0.getOpcode() == ISD::TRUNCATE) 3056 Op0 = Op0.getOperand(0); 3057 if (Op1.getOpcode() == ISD::TRUNCATE) 3058 Op1 = Op1.getOperand(0); 3059 3060 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL && 3061 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ && 3062 isa<ConstantSDNode>(Op0.getOperand(1))) { 3063 3064 unsigned Bits = Op0.getValueType().getSizeInBits(); 3065 if (b != Bits/8-1) 3066 return false; 3067 if (Op0.getConstantOperandVal(1) != Bits-8) 3068 return false; 3069 3070 LHS = Op0.getOperand(0); 3071 RHS = Op1.getOperand(0); 3072 return true; 3073 } 3074 3075 // When we have small integers (i16 to be specific), the form present 3076 // post-legalization uses SETULT in the SELECT_CC for the 3077 // higher-order byte, depending on the fact that the 3078 // even-higher-order bytes are known to all be zero, for example: 3079 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult 3080 // (so when the second byte is the same, because all higher-order 3081 // bits from bytes 3 and 4 are known to be zero, the result of the 3082 // xor can be at most 255) 3083 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT && 3084 isa<ConstantSDNode>(O.getOperand(1))) { 3085 3086 uint64_t ULim = O.getConstantOperandVal(1); 3087 if (ULim != (UINT64_C(1) << b*8)) 3088 return false; 3089 3090 // Now we need to make sure that the upper bytes are known to be 3091 // zero. 3092 unsigned Bits = Op0.getValueType().getSizeInBits(); 3093 if (!CurDAG->MaskedValueIsZero(Op0, 3094 APInt::getHighBitsSet(Bits, Bits - (b+1)*8))) 3095 return false; 3096 3097 LHS = Op0.getOperand(0); 3098 RHS = Op0.getOperand(1); 3099 return true; 3100 } 3101 3102 return false; 3103 } 3104 3105 if (CC != ISD::SETEQ) 3106 return false; 3107 3108 SDValue Op = O.getOperand(0); 3109 if (Op.getOpcode() == ISD::AND) { 3110 if (!isa<ConstantSDNode>(Op.getOperand(1))) 3111 return false; 3112 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b))) 3113 return false; 3114 3115 SDValue XOR = Op.getOperand(0); 3116 if (XOR.getOpcode() == ISD::TRUNCATE) 3117 XOR = XOR.getOperand(0); 3118 if (XOR.getOpcode() != ISD::XOR) 3119 return false; 3120 3121 LHS = XOR.getOperand(0); 3122 RHS = XOR.getOperand(1); 3123 return true; 3124 } else if (Op.getOpcode() == ISD::SRL) { 3125 if (!isa<ConstantSDNode>(Op.getOperand(1))) 3126 return false; 3127 unsigned Bits = Op.getValueType().getSizeInBits(); 3128 if (b != Bits/8-1) 3129 return false; 3130 if (Op.getConstantOperandVal(1) != Bits-8) 3131 return false; 3132 3133 SDValue XOR = Op.getOperand(0); 3134 if (XOR.getOpcode() == ISD::TRUNCATE) 3135 XOR = XOR.getOperand(0); 3136 if (XOR.getOpcode() != ISD::XOR) 3137 return false; 3138 3139 LHS = XOR.getOperand(0); 3140 RHS = XOR.getOperand(1); 3141 return true; 3142 } 3143 3144 return false; 3145 }; 3146 3147 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0)); 3148 while (!Queue.empty()) { 3149 SDValue V = Queue.pop_back_val(); 3150 3151 for (const SDValue &O : V.getNode()->ops()) { 3152 unsigned b; 3153 uint64_t M = 0, A = 0; 3154 SDValue OLHS, ORHS; 3155 if (O.getOpcode() == ISD::OR) { 3156 Queue.push_back(O); 3157 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) { 3158 if (!LHS) { 3159 LHS = OLHS; 3160 RHS = ORHS; 3161 BytesFound[b] = true; 3162 Mask |= M; 3163 Alt |= A; 3164 } else if ((LHS == ORHS && RHS == OLHS) || 3165 (RHS == ORHS && LHS == OLHS)) { 3166 BytesFound[b] = true; 3167 Mask |= M; 3168 Alt |= A; 3169 } else { 3170 return Res; 3171 } 3172 } else { 3173 return Res; 3174 } 3175 } 3176 } 3177 3178 unsigned LastB = 0, BCnt = 0; 3179 for (unsigned i = 0; i < 8; ++i) 3180 if (BytesFound[LastB]) { 3181 ++BCnt; 3182 LastB = i; 3183 } 3184 3185 if (!LastB || BCnt < 2) 3186 return Res; 3187 3188 // Because we'll be zero-extending the output anyway if don't have a specific 3189 // value for each input byte (via the Mask), we can 'anyext' the inputs. 3190 if (LHS.getValueType() != VT) { 3191 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT); 3192 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT); 3193 } 3194 3195 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS); 3196 3197 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1); 3198 if (NonTrivialMask && !Alt) { 3199 // Res = Mask & CMPB 3200 Res = CurDAG->getNode(ISD::AND, dl, VT, Res, 3201 CurDAG->getConstant(Mask, dl, VT)); 3202 } else if (Alt) { 3203 // Res = (CMPB & Mask) | (~CMPB & Alt) 3204 // Which, as suggested here: 3205 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge 3206 // can be written as: 3207 // Res = Alt ^ ((Alt ^ Mask) & CMPB) 3208 // useful because the (Alt ^ Mask) can be pre-computed. 3209 Res = CurDAG->getNode(ISD::AND, dl, VT, Res, 3210 CurDAG->getConstant(Mask ^ Alt, dl, VT)); 3211 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res, 3212 CurDAG->getConstant(Alt, dl, VT)); 3213 } 3214 3215 return Res; 3216 } 3217 3218 // When CR bit registers are enabled, an extension of an i1 variable to a i32 3219 // or i64 value is lowered in terms of a SELECT_I[48] operation, and thus 3220 // involves constant materialization of a 0 or a 1 or both. If the result of 3221 // the extension is then operated upon by some operator that can be constant 3222 // folded with a constant 0 or 1, and that constant can be materialized using 3223 // only one instruction (like a zero or one), then we should fold in those 3224 // operations with the select. 3225 void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) { 3226 if (!PPCSubTarget->useCRBits()) 3227 return; 3228 3229 if (N->getOpcode() != ISD::ZERO_EXTEND && 3230 N->getOpcode() != ISD::SIGN_EXTEND && 3231 N->getOpcode() != ISD::ANY_EXTEND) 3232 return; 3233 3234 if (N->getOperand(0).getValueType() != MVT::i1) 3235 return; 3236 3237 if (!N->hasOneUse()) 3238 return; 3239 3240 SDLoc dl(N); 3241 EVT VT = N->getValueType(0); 3242 SDValue Cond = N->getOperand(0); 3243 SDValue ConstTrue = 3244 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT); 3245 SDValue ConstFalse = CurDAG->getConstant(0, dl, VT); 3246 3247 do { 3248 SDNode *User = *N->use_begin(); 3249 if (User->getNumOperands() != 2) 3250 break; 3251 3252 auto TryFold = [this, N, User, dl](SDValue Val) { 3253 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1); 3254 SDValue O0 = UserO0.getNode() == N ? Val : UserO0; 3255 SDValue O1 = UserO1.getNode() == N ? Val : UserO1; 3256 3257 return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl, 3258 User->getValueType(0), 3259 O0.getNode(), O1.getNode()); 3260 }; 3261 3262 SDValue TrueRes = TryFold(ConstTrue); 3263 if (!TrueRes) 3264 break; 3265 SDValue FalseRes = TryFold(ConstFalse); 3266 if (!FalseRes) 3267 break; 3268 3269 // For us to materialize these using one instruction, we must be able to 3270 // represent them as signed 16-bit integers. 3271 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(), 3272 False = cast<ConstantSDNode>(FalseRes)->getZExtValue(); 3273 if (!isInt<16>(True) || !isInt<16>(False)) 3274 break; 3275 3276 // We can replace User with a new SELECT node, and try again to see if we 3277 // can fold the select with its user. 3278 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes); 3279 N = User; 3280 ConstTrue = TrueRes; 3281 ConstFalse = FalseRes; 3282 } while (N->hasOneUse()); 3283 } 3284 3285 void PPCDAGToDAGISel::PreprocessISelDAG() { 3286 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); 3287 ++Position; 3288 3289 bool MadeChange = false; 3290 while (Position != CurDAG->allnodes_begin()) { 3291 SDNode *N = --Position; 3292 if (N->use_empty()) 3293 continue; 3294 3295 SDValue Res; 3296 switch (N->getOpcode()) { 3297 default: break; 3298 case ISD::OR: 3299 Res = combineToCMPB(N); 3300 break; 3301 } 3302 3303 if (!Res) 3304 foldBoolExts(Res, N); 3305 3306 if (Res) { 3307 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: "); 3308 DEBUG(N->dump(CurDAG)); 3309 DEBUG(dbgs() << "\nNew: "); 3310 DEBUG(Res.getNode()->dump(CurDAG)); 3311 DEBUG(dbgs() << "\n"); 3312 3313 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res); 3314 MadeChange = true; 3315 } 3316 } 3317 3318 if (MadeChange) 3319 CurDAG->RemoveDeadNodes(); 3320 } 3321 3322 /// PostprocessISelDAG - Perform some late peephole optimizations 3323 /// on the DAG representation. 3324 void PPCDAGToDAGISel::PostprocessISelDAG() { 3325 3326 // Skip peepholes at -O0. 3327 if (TM.getOptLevel() == CodeGenOpt::None) 3328 return; 3329 3330 PeepholePPC64(); 3331 PeepholeCROps(); 3332 PeepholePPC64ZExt(); 3333 } 3334 3335 // Check if all users of this node will become isel where the second operand 3336 // is the constant zero. If this is so, and if we can negate the condition, 3337 // then we can flip the true and false operands. This will allow the zero to 3338 // be folded with the isel so that we don't need to materialize a register 3339 // containing zero. 3340 bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) { 3341 // If we're not using isel, then this does not matter. 3342 if (!PPCSubTarget->hasISEL()) 3343 return false; 3344 3345 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 3346 UI != UE; ++UI) { 3347 SDNode *User = *UI; 3348 if (!User->isMachineOpcode()) 3349 return false; 3350 if (User->getMachineOpcode() != PPC::SELECT_I4 && 3351 User->getMachineOpcode() != PPC::SELECT_I8) 3352 return false; 3353 3354 SDNode *Op2 = User->getOperand(2).getNode(); 3355 if (!Op2->isMachineOpcode()) 3356 return false; 3357 3358 if (Op2->getMachineOpcode() != PPC::LI && 3359 Op2->getMachineOpcode() != PPC::LI8) 3360 return false; 3361 3362 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0)); 3363 if (!C) 3364 return false; 3365 3366 if (!C->isNullValue()) 3367 return false; 3368 } 3369 3370 return true; 3371 } 3372 3373 void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) { 3374 SmallVector<SDNode *, 4> ToReplace; 3375 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); 3376 UI != UE; ++UI) { 3377 SDNode *User = *UI; 3378 assert((User->getMachineOpcode() == PPC::SELECT_I4 || 3379 User->getMachineOpcode() == PPC::SELECT_I8) && 3380 "Must have all select users"); 3381 ToReplace.push_back(User); 3382 } 3383 3384 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(), 3385 UE = ToReplace.end(); UI != UE; ++UI) { 3386 SDNode *User = *UI; 3387 SDNode *ResNode = 3388 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User), 3389 User->getValueType(0), User->getOperand(0), 3390 User->getOperand(2), 3391 User->getOperand(1)); 3392 3393 DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); 3394 DEBUG(User->dump(CurDAG)); 3395 DEBUG(dbgs() << "\nNew: "); 3396 DEBUG(ResNode->dump(CurDAG)); 3397 DEBUG(dbgs() << "\n"); 3398 3399 ReplaceUses(User, ResNode); 3400 } 3401 } 3402 3403 void PPCDAGToDAGISel::PeepholeCROps() { 3404 bool IsModified; 3405 do { 3406 IsModified = false; 3407 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), 3408 E = CurDAG->allnodes_end(); I != E; ++I) { 3409 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I); 3410 if (!MachineNode || MachineNode->use_empty()) 3411 continue; 3412 SDNode *ResNode = MachineNode; 3413 3414 bool Op1Set = false, Op1Unset = false, 3415 Op1Not = false, 3416 Op2Set = false, Op2Unset = false, 3417 Op2Not = false; 3418 3419 unsigned Opcode = MachineNode->getMachineOpcode(); 3420 switch (Opcode) { 3421 default: break; 3422 case PPC::CRAND: 3423 case PPC::CRNAND: 3424 case PPC::CROR: 3425 case PPC::CRXOR: 3426 case PPC::CRNOR: 3427 case PPC::CREQV: 3428 case PPC::CRANDC: 3429 case PPC::CRORC: { 3430 SDValue Op = MachineNode->getOperand(1); 3431 if (Op.isMachineOpcode()) { 3432 if (Op.getMachineOpcode() == PPC::CRSET) 3433 Op2Set = true; 3434 else if (Op.getMachineOpcode() == PPC::CRUNSET) 3435 Op2Unset = true; 3436 else if (Op.getMachineOpcode() == PPC::CRNOR && 3437 Op.getOperand(0) == Op.getOperand(1)) 3438 Op2Not = true; 3439 } 3440 } // fallthrough 3441 case PPC::BC: 3442 case PPC::BCn: 3443 case PPC::SELECT_I4: 3444 case PPC::SELECT_I8: 3445 case PPC::SELECT_F4: 3446 case PPC::SELECT_F8: 3447 case PPC::SELECT_QFRC: 3448 case PPC::SELECT_QSRC: 3449 case PPC::SELECT_QBRC: 3450 case PPC::SELECT_VRRC: 3451 case PPC::SELECT_VSFRC: 3452 case PPC::SELECT_VSRC: { 3453 SDValue Op = MachineNode->getOperand(0); 3454 if (Op.isMachineOpcode()) { 3455 if (Op.getMachineOpcode() == PPC::CRSET) 3456 Op1Set = true; 3457 else if (Op.getMachineOpcode() == PPC::CRUNSET) 3458 Op1Unset = true; 3459 else if (Op.getMachineOpcode() == PPC::CRNOR && 3460 Op.getOperand(0) == Op.getOperand(1)) 3461 Op1Not = true; 3462 } 3463 } 3464 break; 3465 } 3466 3467 bool SelectSwap = false; 3468 switch (Opcode) { 3469 default: break; 3470 case PPC::CRAND: 3471 if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) 3472 // x & x = x 3473 ResNode = MachineNode->getOperand(0).getNode(); 3474 else if (Op1Set) 3475 // 1 & y = y 3476 ResNode = MachineNode->getOperand(1).getNode(); 3477 else if (Op2Set) 3478 // x & 1 = x 3479 ResNode = MachineNode->getOperand(0).getNode(); 3480 else if (Op1Unset || Op2Unset) 3481 // x & 0 = 0 & y = 0 3482 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), 3483 MVT::i1); 3484 else if (Op1Not) 3485 // ~x & y = andc(y, x) 3486 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), 3487 MVT::i1, MachineNode->getOperand(1), 3488 MachineNode->getOperand(0). 3489 getOperand(0)); 3490 else if (Op2Not) 3491 // x & ~y = andc(x, y) 3492 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), 3493 MVT::i1, MachineNode->getOperand(0), 3494 MachineNode->getOperand(1). 3495 getOperand(0)); 3496 else if (AllUsersSelectZero(MachineNode)) 3497 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), 3498 MVT::i1, MachineNode->getOperand(0), 3499 MachineNode->getOperand(1)), 3500 SelectSwap = true; 3501 break; 3502 case PPC::CRNAND: 3503 if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) 3504 // nand(x, x) -> nor(x, x) 3505 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3506 MVT::i1, MachineNode->getOperand(0), 3507 MachineNode->getOperand(0)); 3508 else if (Op1Set) 3509 // nand(1, y) -> nor(y, y) 3510 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3511 MVT::i1, MachineNode->getOperand(1), 3512 MachineNode->getOperand(1)); 3513 else if (Op2Set) 3514 // nand(x, 1) -> nor(x, x) 3515 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3516 MVT::i1, MachineNode->getOperand(0), 3517 MachineNode->getOperand(0)); 3518 else if (Op1Unset || Op2Unset) 3519 // nand(x, 0) = nand(0, y) = 1 3520 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), 3521 MVT::i1); 3522 else if (Op1Not) 3523 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y) 3524 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), 3525 MVT::i1, MachineNode->getOperand(0). 3526 getOperand(0), 3527 MachineNode->getOperand(1)); 3528 else if (Op2Not) 3529 // nand(x, ~y) = ~x | y = orc(y, x) 3530 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), 3531 MVT::i1, MachineNode->getOperand(1). 3532 getOperand(0), 3533 MachineNode->getOperand(0)); 3534 else if (AllUsersSelectZero(MachineNode)) 3535 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), 3536 MVT::i1, MachineNode->getOperand(0), 3537 MachineNode->getOperand(1)), 3538 SelectSwap = true; 3539 break; 3540 case PPC::CROR: 3541 if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) 3542 // x | x = x 3543 ResNode = MachineNode->getOperand(0).getNode(); 3544 else if (Op1Set || Op2Set) 3545 // x | 1 = 1 | y = 1 3546 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), 3547 MVT::i1); 3548 else if (Op1Unset) 3549 // 0 | y = y 3550 ResNode = MachineNode->getOperand(1).getNode(); 3551 else if (Op2Unset) 3552 // x | 0 = x 3553 ResNode = MachineNode->getOperand(0).getNode(); 3554 else if (Op1Not) 3555 // ~x | y = orc(y, x) 3556 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), 3557 MVT::i1, MachineNode->getOperand(1), 3558 MachineNode->getOperand(0). 3559 getOperand(0)); 3560 else if (Op2Not) 3561 // x | ~y = orc(x, y) 3562 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), 3563 MVT::i1, MachineNode->getOperand(0), 3564 MachineNode->getOperand(1). 3565 getOperand(0)); 3566 else if (AllUsersSelectZero(MachineNode)) 3567 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3568 MVT::i1, MachineNode->getOperand(0), 3569 MachineNode->getOperand(1)), 3570 SelectSwap = true; 3571 break; 3572 case PPC::CRXOR: 3573 if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) 3574 // xor(x, x) = 0 3575 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), 3576 MVT::i1); 3577 else if (Op1Set) 3578 // xor(1, y) -> nor(y, y) 3579 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3580 MVT::i1, MachineNode->getOperand(1), 3581 MachineNode->getOperand(1)); 3582 else if (Op2Set) 3583 // xor(x, 1) -> nor(x, x) 3584 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3585 MVT::i1, MachineNode->getOperand(0), 3586 MachineNode->getOperand(0)); 3587 else if (Op1Unset) 3588 // xor(0, y) = y 3589 ResNode = MachineNode->getOperand(1).getNode(); 3590 else if (Op2Unset) 3591 // xor(x, 0) = x 3592 ResNode = MachineNode->getOperand(0).getNode(); 3593 else if (Op1Not) 3594 // xor(~x, y) = eqv(x, y) 3595 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), 3596 MVT::i1, MachineNode->getOperand(0). 3597 getOperand(0), 3598 MachineNode->getOperand(1)); 3599 else if (Op2Not) 3600 // xor(x, ~y) = eqv(x, y) 3601 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), 3602 MVT::i1, MachineNode->getOperand(0), 3603 MachineNode->getOperand(1). 3604 getOperand(0)); 3605 else if (AllUsersSelectZero(MachineNode)) 3606 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), 3607 MVT::i1, MachineNode->getOperand(0), 3608 MachineNode->getOperand(1)), 3609 SelectSwap = true; 3610 break; 3611 case PPC::CRNOR: 3612 if (Op1Set || Op2Set) 3613 // nor(1, y) -> 0 3614 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), 3615 MVT::i1); 3616 else if (Op1Unset) 3617 // nor(0, y) = ~y -> nor(y, y) 3618 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3619 MVT::i1, MachineNode->getOperand(1), 3620 MachineNode->getOperand(1)); 3621 else if (Op2Unset) 3622 // nor(x, 0) = ~x 3623 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3624 MVT::i1, MachineNode->getOperand(0), 3625 MachineNode->getOperand(0)); 3626 else if (Op1Not) 3627 // nor(~x, y) = andc(x, y) 3628 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), 3629 MVT::i1, MachineNode->getOperand(0). 3630 getOperand(0), 3631 MachineNode->getOperand(1)); 3632 else if (Op2Not) 3633 // nor(x, ~y) = andc(y, x) 3634 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), 3635 MVT::i1, MachineNode->getOperand(1). 3636 getOperand(0), 3637 MachineNode->getOperand(0)); 3638 else if (AllUsersSelectZero(MachineNode)) 3639 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), 3640 MVT::i1, MachineNode->getOperand(0), 3641 MachineNode->getOperand(1)), 3642 SelectSwap = true; 3643 break; 3644 case PPC::CREQV: 3645 if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) 3646 // eqv(x, x) = 1 3647 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), 3648 MVT::i1); 3649 else if (Op1Set) 3650 // eqv(1, y) = y 3651 ResNode = MachineNode->getOperand(1).getNode(); 3652 else if (Op2Set) 3653 // eqv(x, 1) = x 3654 ResNode = MachineNode->getOperand(0).getNode(); 3655 else if (Op1Unset) 3656 // eqv(0, y) = ~y -> nor(y, y) 3657 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3658 MVT::i1, MachineNode->getOperand(1), 3659 MachineNode->getOperand(1)); 3660 else if (Op2Unset) 3661 // eqv(x, 0) = ~x 3662 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3663 MVT::i1, MachineNode->getOperand(0), 3664 MachineNode->getOperand(0)); 3665 else if (Op1Not) 3666 // eqv(~x, y) = xor(x, y) 3667 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), 3668 MVT::i1, MachineNode->getOperand(0). 3669 getOperand(0), 3670 MachineNode->getOperand(1)); 3671 else if (Op2Not) 3672 // eqv(x, ~y) = xor(x, y) 3673 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), 3674 MVT::i1, MachineNode->getOperand(0), 3675 MachineNode->getOperand(1). 3676 getOperand(0)); 3677 else if (AllUsersSelectZero(MachineNode)) 3678 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), 3679 MVT::i1, MachineNode->getOperand(0), 3680 MachineNode->getOperand(1)), 3681 SelectSwap = true; 3682 break; 3683 case PPC::CRANDC: 3684 if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) 3685 // andc(x, x) = 0 3686 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), 3687 MVT::i1); 3688 else if (Op1Set) 3689 // andc(1, y) = ~y 3690 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3691 MVT::i1, MachineNode->getOperand(1), 3692 MachineNode->getOperand(1)); 3693 else if (Op1Unset || Op2Set) 3694 // andc(0, y) = andc(x, 1) = 0 3695 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), 3696 MVT::i1); 3697 else if (Op2Unset) 3698 // andc(x, 0) = x 3699 ResNode = MachineNode->getOperand(0).getNode(); 3700 else if (Op1Not) 3701 // andc(~x, y) = ~(x | y) = nor(x, y) 3702 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3703 MVT::i1, MachineNode->getOperand(0). 3704 getOperand(0), 3705 MachineNode->getOperand(1)); 3706 else if (Op2Not) 3707 // andc(x, ~y) = x & y 3708 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), 3709 MVT::i1, MachineNode->getOperand(0), 3710 MachineNode->getOperand(1). 3711 getOperand(0)); 3712 else if (AllUsersSelectZero(MachineNode)) 3713 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), 3714 MVT::i1, MachineNode->getOperand(1), 3715 MachineNode->getOperand(0)), 3716 SelectSwap = true; 3717 break; 3718 case PPC::CRORC: 3719 if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) 3720 // orc(x, x) = 1 3721 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), 3722 MVT::i1); 3723 else if (Op1Set || Op2Unset) 3724 // orc(1, y) = orc(x, 0) = 1 3725 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), 3726 MVT::i1); 3727 else if (Op2Set) 3728 // orc(x, 1) = x 3729 ResNode = MachineNode->getOperand(0).getNode(); 3730 else if (Op1Unset) 3731 // orc(0, y) = ~y 3732 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), 3733 MVT::i1, MachineNode->getOperand(1), 3734 MachineNode->getOperand(1)); 3735 else if (Op1Not) 3736 // orc(~x, y) = ~(x & y) = nand(x, y) 3737 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), 3738 MVT::i1, MachineNode->getOperand(0). 3739 getOperand(0), 3740 MachineNode->getOperand(1)); 3741 else if (Op2Not) 3742 // orc(x, ~y) = x | y 3743 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), 3744 MVT::i1, MachineNode->getOperand(0), 3745 MachineNode->getOperand(1). 3746 getOperand(0)); 3747 else if (AllUsersSelectZero(MachineNode)) 3748 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), 3749 MVT::i1, MachineNode->getOperand(1), 3750 MachineNode->getOperand(0)), 3751 SelectSwap = true; 3752 break; 3753 case PPC::SELECT_I4: 3754 case PPC::SELECT_I8: 3755 case PPC::SELECT_F4: 3756 case PPC::SELECT_F8: 3757 case PPC::SELECT_QFRC: 3758 case PPC::SELECT_QSRC: 3759 case PPC::SELECT_QBRC: 3760 case PPC::SELECT_VRRC: 3761 case PPC::SELECT_VSFRC: 3762 case PPC::SELECT_VSRC: 3763 if (Op1Set) 3764 ResNode = MachineNode->getOperand(1).getNode(); 3765 else if (Op1Unset) 3766 ResNode = MachineNode->getOperand(2).getNode(); 3767 else if (Op1Not) 3768 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(), 3769 SDLoc(MachineNode), 3770 MachineNode->getValueType(0), 3771 MachineNode->getOperand(0). 3772 getOperand(0), 3773 MachineNode->getOperand(2), 3774 MachineNode->getOperand(1)); 3775 break; 3776 case PPC::BC: 3777 case PPC::BCn: 3778 if (Op1Not) 3779 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn : 3780 PPC::BC, 3781 SDLoc(MachineNode), 3782 MVT::Other, 3783 MachineNode->getOperand(0). 3784 getOperand(0), 3785 MachineNode->getOperand(1), 3786 MachineNode->getOperand(2)); 3787 // FIXME: Handle Op1Set, Op1Unset here too. 3788 break; 3789 } 3790 3791 // If we're inverting this node because it is used only by selects that 3792 // we'd like to swap, then swap the selects before the node replacement. 3793 if (SelectSwap) 3794 SwapAllSelectUsers(MachineNode); 3795 3796 if (ResNode != MachineNode) { 3797 DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); 3798 DEBUG(MachineNode->dump(CurDAG)); 3799 DEBUG(dbgs() << "\nNew: "); 3800 DEBUG(ResNode->dump(CurDAG)); 3801 DEBUG(dbgs() << "\n"); 3802 3803 ReplaceUses(MachineNode, ResNode); 3804 IsModified = true; 3805 } 3806 } 3807 if (IsModified) 3808 CurDAG->RemoveDeadNodes(); 3809 } while (IsModified); 3810 } 3811 3812 // Gather the set of 32-bit operations that are known to have their 3813 // higher-order 32 bits zero, where ToPromote contains all such operations. 3814 static bool PeepholePPC64ZExtGather(SDValue Op32, 3815 SmallPtrSetImpl<SDNode *> &ToPromote) { 3816 if (!Op32.isMachineOpcode()) 3817 return false; 3818 3819 // First, check for the "frontier" instructions (those that will clear the 3820 // higher-order 32 bits. 3821 3822 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap 3823 // around. If it does not, then these instructions will clear the 3824 // higher-order bits. 3825 if ((Op32.getMachineOpcode() == PPC::RLWINM || 3826 Op32.getMachineOpcode() == PPC::RLWNM) && 3827 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) { 3828 ToPromote.insert(Op32.getNode()); 3829 return true; 3830 } 3831 3832 // SLW and SRW always clear the higher-order bits. 3833 if (Op32.getMachineOpcode() == PPC::SLW || 3834 Op32.getMachineOpcode() == PPC::SRW) { 3835 ToPromote.insert(Op32.getNode()); 3836 return true; 3837 } 3838 3839 // For LI and LIS, we need the immediate to be positive (so that it is not 3840 // sign extended). 3841 if (Op32.getMachineOpcode() == PPC::LI || 3842 Op32.getMachineOpcode() == PPC::LIS) { 3843 if (!isUInt<15>(Op32.getConstantOperandVal(0))) 3844 return false; 3845 3846 ToPromote.insert(Op32.getNode()); 3847 return true; 3848 } 3849 3850 // LHBRX and LWBRX always clear the higher-order bits. 3851 if (Op32.getMachineOpcode() == PPC::LHBRX || 3852 Op32.getMachineOpcode() == PPC::LWBRX) { 3853 ToPromote.insert(Op32.getNode()); 3854 return true; 3855 } 3856 3857 // CNTLZW always produces a 64-bit value in [0,32], and so is zero extended. 3858 if (Op32.getMachineOpcode() == PPC::CNTLZW) { 3859 ToPromote.insert(Op32.getNode()); 3860 return true; 3861 } 3862 3863 // Next, check for those instructions we can look through. 3864 3865 // Assuming the mask does not wrap around, then the higher-order bits are 3866 // taken directly from the first operand. 3867 if (Op32.getMachineOpcode() == PPC::RLWIMI && 3868 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) { 3869 SmallPtrSet<SDNode *, 16> ToPromote1; 3870 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1)) 3871 return false; 3872 3873 ToPromote.insert(Op32.getNode()); 3874 ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); 3875 return true; 3876 } 3877 3878 // For OR, the higher-order bits are zero if that is true for both operands. 3879 // For SELECT_I4, the same is true (but the relevant operand numbers are 3880 // shifted by 1). 3881 if (Op32.getMachineOpcode() == PPC::OR || 3882 Op32.getMachineOpcode() == PPC::SELECT_I4) { 3883 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0; 3884 SmallPtrSet<SDNode *, 16> ToPromote1; 3885 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1)) 3886 return false; 3887 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1)) 3888 return false; 3889 3890 ToPromote.insert(Op32.getNode()); 3891 ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); 3892 return true; 3893 } 3894 3895 // For ORI and ORIS, we need the higher-order bits of the first operand to be 3896 // zero, and also for the constant to be positive (so that it is not sign 3897 // extended). 3898 if (Op32.getMachineOpcode() == PPC::ORI || 3899 Op32.getMachineOpcode() == PPC::ORIS) { 3900 SmallPtrSet<SDNode *, 16> ToPromote1; 3901 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1)) 3902 return false; 3903 if (!isUInt<15>(Op32.getConstantOperandVal(1))) 3904 return false; 3905 3906 ToPromote.insert(Op32.getNode()); 3907 ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); 3908 return true; 3909 } 3910 3911 // The higher-order bits of AND are zero if that is true for at least one of 3912 // the operands. 3913 if (Op32.getMachineOpcode() == PPC::AND) { 3914 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2; 3915 bool Op0OK = 3916 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1); 3917 bool Op1OK = 3918 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2); 3919 if (!Op0OK && !Op1OK) 3920 return false; 3921 3922 ToPromote.insert(Op32.getNode()); 3923 3924 if (Op0OK) 3925 ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); 3926 3927 if (Op1OK) 3928 ToPromote.insert(ToPromote2.begin(), ToPromote2.end()); 3929 3930 return true; 3931 } 3932 3933 // For ANDI and ANDIS, the higher-order bits are zero if either that is true 3934 // of the first operand, or if the second operand is positive (so that it is 3935 // not sign extended). 3936 if (Op32.getMachineOpcode() == PPC::ANDIo || 3937 Op32.getMachineOpcode() == PPC::ANDISo) { 3938 SmallPtrSet<SDNode *, 16> ToPromote1; 3939 bool Op0OK = 3940 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1); 3941 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1)); 3942 if (!Op0OK && !Op1OK) 3943 return false; 3944 3945 ToPromote.insert(Op32.getNode()); 3946 3947 if (Op0OK) 3948 ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); 3949 3950 return true; 3951 } 3952 3953 return false; 3954 } 3955 3956 void PPCDAGToDAGISel::PeepholePPC64ZExt() { 3957 if (!PPCSubTarget->isPPC64()) 3958 return; 3959 3960 // When we zero-extend from i32 to i64, we use a pattern like this: 3961 // def : Pat<(i64 (zext i32:$in)), 3962 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32), 3963 // 0, 32)>; 3964 // There are several 32-bit shift/rotate instructions, however, that will 3965 // clear the higher-order bits of their output, rendering the RLDICL 3966 // unnecessary. When that happens, we remove it here, and redefine the 3967 // relevant 32-bit operation to be a 64-bit operation. 3968 3969 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); 3970 ++Position; 3971 3972 bool MadeChange = false; 3973 while (Position != CurDAG->allnodes_begin()) { 3974 SDNode *N = --Position; 3975 // Skip dead nodes and any non-machine opcodes. 3976 if (N->use_empty() || !N->isMachineOpcode()) 3977 continue; 3978 3979 if (N->getMachineOpcode() != PPC::RLDICL) 3980 continue; 3981 3982 if (N->getConstantOperandVal(1) != 0 || 3983 N->getConstantOperandVal(2) != 32) 3984 continue; 3985 3986 SDValue ISR = N->getOperand(0); 3987 if (!ISR.isMachineOpcode() || 3988 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG) 3989 continue; 3990 3991 if (!ISR.hasOneUse()) 3992 continue; 3993 3994 if (ISR.getConstantOperandVal(2) != PPC::sub_32) 3995 continue; 3996 3997 SDValue IDef = ISR.getOperand(0); 3998 if (!IDef.isMachineOpcode() || 3999 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF) 4000 continue; 4001 4002 // We now know that we're looking at a canonical i32 -> i64 zext. See if we 4003 // can get rid of it. 4004 4005 SDValue Op32 = ISR->getOperand(1); 4006 if (!Op32.isMachineOpcode()) 4007 continue; 4008 4009 // There are some 32-bit instructions that always clear the high-order 32 4010 // bits, there are also some instructions (like AND) that we can look 4011 // through. 4012 SmallPtrSet<SDNode *, 16> ToPromote; 4013 if (!PeepholePPC64ZExtGather(Op32, ToPromote)) 4014 continue; 4015 4016 // If the ToPromote set contains nodes that have uses outside of the set 4017 // (except for the original INSERT_SUBREG), then abort the transformation. 4018 bool OutsideUse = false; 4019 for (SDNode *PN : ToPromote) { 4020 for (SDNode *UN : PN->uses()) { 4021 if (!ToPromote.count(UN) && UN != ISR.getNode()) { 4022 OutsideUse = true; 4023 break; 4024 } 4025 } 4026 4027 if (OutsideUse) 4028 break; 4029 } 4030 if (OutsideUse) 4031 continue; 4032 4033 MadeChange = true; 4034 4035 // We now know that this zero extension can be removed by promoting to 4036 // nodes in ToPromote to 64-bit operations, where for operations in the 4037 // frontier of the set, we need to insert INSERT_SUBREGs for their 4038 // operands. 4039 for (SDNode *PN : ToPromote) { 4040 unsigned NewOpcode; 4041 switch (PN->getMachineOpcode()) { 4042 default: 4043 llvm_unreachable("Don't know the 64-bit variant of this instruction"); 4044 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break; 4045 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break; 4046 case PPC::SLW: NewOpcode = PPC::SLW8; break; 4047 case PPC::SRW: NewOpcode = PPC::SRW8; break; 4048 case PPC::LI: NewOpcode = PPC::LI8; break; 4049 case PPC::LIS: NewOpcode = PPC::LIS8; break; 4050 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break; 4051 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break; 4052 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break; 4053 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break; 4054 case PPC::OR: NewOpcode = PPC::OR8; break; 4055 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break; 4056 case PPC::ORI: NewOpcode = PPC::ORI8; break; 4057 case PPC::ORIS: NewOpcode = PPC::ORIS8; break; 4058 case PPC::AND: NewOpcode = PPC::AND8; break; 4059 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break; 4060 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break; 4061 } 4062 4063 // Note: During the replacement process, the nodes will be in an 4064 // inconsistent state (some instructions will have operands with values 4065 // of the wrong type). Once done, however, everything should be right 4066 // again. 4067 4068 SmallVector<SDValue, 4> Ops; 4069 for (const SDValue &V : PN->ops()) { 4070 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 && 4071 !isa<ConstantSDNode>(V)) { 4072 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) }; 4073 SDNode *ReplOp = 4074 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V), 4075 ISR.getNode()->getVTList(), ReplOpOps); 4076 Ops.push_back(SDValue(ReplOp, 0)); 4077 } else { 4078 Ops.push_back(V); 4079 } 4080 } 4081 4082 // Because all to-be-promoted nodes only have users that are other 4083 // promoted nodes (or the original INSERT_SUBREG), we can safely replace 4084 // the i32 result value type with i64. 4085 4086 SmallVector<EVT, 2> NewVTs; 4087 SDVTList VTs = PN->getVTList(); 4088 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i) 4089 if (VTs.VTs[i] == MVT::i32) 4090 NewVTs.push_back(MVT::i64); 4091 else 4092 NewVTs.push_back(VTs.VTs[i]); 4093 4094 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: "); 4095 DEBUG(PN->dump(CurDAG)); 4096 4097 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops); 4098 4099 DEBUG(dbgs() << "\nNew: "); 4100 DEBUG(PN->dump(CurDAG)); 4101 DEBUG(dbgs() << "\n"); 4102 } 4103 4104 // Now we replace the original zero extend and its associated INSERT_SUBREG 4105 // with the value feeding the INSERT_SUBREG (which has now been promoted to 4106 // return an i64). 4107 4108 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: "); 4109 DEBUG(N->dump(CurDAG)); 4110 DEBUG(dbgs() << "\nNew: "); 4111 DEBUG(Op32.getNode()->dump(CurDAG)); 4112 DEBUG(dbgs() << "\n"); 4113 4114 ReplaceUses(N, Op32.getNode()); 4115 } 4116 4117 if (MadeChange) 4118 CurDAG->RemoveDeadNodes(); 4119 } 4120 4121 void PPCDAGToDAGISel::PeepholePPC64() { 4122 // These optimizations are currently supported only for 64-bit SVR4. 4123 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64()) 4124 return; 4125 4126 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); 4127 ++Position; 4128 4129 while (Position != CurDAG->allnodes_begin()) { 4130 SDNode *N = --Position; 4131 // Skip dead nodes and any non-machine opcodes. 4132 if (N->use_empty() || !N->isMachineOpcode()) 4133 continue; 4134 4135 unsigned FirstOp; 4136 unsigned StorageOpcode = N->getMachineOpcode(); 4137 4138 switch (StorageOpcode) { 4139 default: continue; 4140 4141 case PPC::LBZ: 4142 case PPC::LBZ8: 4143 case PPC::LD: 4144 case PPC::LFD: 4145 case PPC::LFS: 4146 case PPC::LHA: 4147 case PPC::LHA8: 4148 case PPC::LHZ: 4149 case PPC::LHZ8: 4150 case PPC::LWA: 4151 case PPC::LWZ: 4152 case PPC::LWZ8: 4153 FirstOp = 0; 4154 break; 4155 4156 case PPC::STB: 4157 case PPC::STB8: 4158 case PPC::STD: 4159 case PPC::STFD: 4160 case PPC::STFS: 4161 case PPC::STH: 4162 case PPC::STH8: 4163 case PPC::STW: 4164 case PPC::STW8: 4165 FirstOp = 1; 4166 break; 4167 } 4168 4169 // If this is a load or store with a zero offset, we may be able to 4170 // fold an add-immediate into the memory operation. 4171 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) || 4172 N->getConstantOperandVal(FirstOp) != 0) 4173 continue; 4174 4175 SDValue Base = N->getOperand(FirstOp + 1); 4176 if (!Base.isMachineOpcode()) 4177 continue; 4178 4179 unsigned Flags = 0; 4180 bool ReplaceFlags = true; 4181 4182 // When the feeding operation is an add-immediate of some sort, 4183 // determine whether we need to add relocation information to the 4184 // target flags on the immediate operand when we fold it into the 4185 // load instruction. 4186 // 4187 // For something like ADDItocL, the relocation information is 4188 // inferred from the opcode; when we process it in the AsmPrinter, 4189 // we add the necessary relocation there. A load, though, can receive 4190 // relocation from various flavors of ADDIxxx, so we need to carry 4191 // the relocation information in the target flags. 4192 switch (Base.getMachineOpcode()) { 4193 default: continue; 4194 4195 case PPC::ADDI8: 4196 case PPC::ADDI: 4197 // In some cases (such as TLS) the relocation information 4198 // is already in place on the operand, so copying the operand 4199 // is sufficient. 4200 ReplaceFlags = false; 4201 // For these cases, the immediate may not be divisible by 4, in 4202 // which case the fold is illegal for DS-form instructions. (The 4203 // other cases provide aligned addresses and are always safe.) 4204 if ((StorageOpcode == PPC::LWA || 4205 StorageOpcode == PPC::LD || 4206 StorageOpcode == PPC::STD) && 4207 (!isa<ConstantSDNode>(Base.getOperand(1)) || 4208 Base.getConstantOperandVal(1) % 4 != 0)) 4209 continue; 4210 break; 4211 case PPC::ADDIdtprelL: 4212 Flags = PPCII::MO_DTPREL_LO; 4213 break; 4214 case PPC::ADDItlsldL: 4215 Flags = PPCII::MO_TLSLD_LO; 4216 break; 4217 case PPC::ADDItocL: 4218 Flags = PPCII::MO_TOC_LO; 4219 break; 4220 } 4221 4222 // We found an opportunity. Reverse the operands from the add 4223 // immediate and substitute them into the load or store. If 4224 // needed, update the target flags for the immediate operand to 4225 // reflect the necessary relocation information. 4226 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); 4227 DEBUG(Base->dump(CurDAG)); 4228 DEBUG(dbgs() << "\nN: "); 4229 DEBUG(N->dump(CurDAG)); 4230 DEBUG(dbgs() << "\n"); 4231 4232 SDValue ImmOpnd = Base.getOperand(1); 4233 4234 // If the relocation information isn't already present on the 4235 // immediate operand, add it now. 4236 if (ReplaceFlags) { 4237 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { 4238 SDLoc dl(GA); 4239 const GlobalValue *GV = GA->getGlobal(); 4240 // We can't perform this optimization for data whose alignment 4241 // is insufficient for the instruction encoding. 4242 if (GV->getAlignment() < 4 && 4243 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD || 4244 StorageOpcode == PPC::LWA)) { 4245 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n"); 4246 continue; 4247 } 4248 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags); 4249 } else if (ConstantPoolSDNode *CP = 4250 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) { 4251 const Constant *C = CP->getConstVal(); 4252 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64, 4253 CP->getAlignment(), 4254 0, Flags); 4255 } 4256 } 4257 4258 if (FirstOp == 1) // Store 4259 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd, 4260 Base.getOperand(0), N->getOperand(3)); 4261 else // Load 4262 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0), 4263 N->getOperand(2)); 4264 4265 // The add-immediate may now be dead, in which case remove it. 4266 if (Base.getNode()->use_empty()) 4267 CurDAG->RemoveDeadNode(Base.getNode()); 4268 } 4269 } 4270 4271 4272 /// createPPCISelDag - This pass converts a legalized DAG into a 4273 /// PowerPC-specific DAG, ready for instruction scheduling. 4274 /// 4275 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) { 4276 return new PPCDAGToDAGISel(TM); 4277 } 4278 4279 static void initializePassOnce(PassRegistry &Registry) { 4280 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection"; 4281 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 4282 nullptr, false, false); 4283 Registry.registerPass(*PI, true); 4284 } 4285 4286 void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) { 4287 CALL_ONCE_INITIALIZATION(initializePassOnce); 4288 } 4289 4290