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 "PPCTargetMachine.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/SelectionDAGISel.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalValue.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/Intrinsics.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetOptions.h"
35 using namespace llvm;
36
37 #define DEBUG_TYPE "ppc-codegen"
38
39 // FIXME: Remove this once the bug has been fixed!
40 cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
41 cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
42
43 namespace llvm {
44 void initializePPCDAGToDAGISelPass(PassRegistry&);
45 }
46
47 namespace {
48 //===--------------------------------------------------------------------===//
49 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
50 /// instructions for SelectionDAG operations.
51 ///
52 class PPCDAGToDAGISel : public SelectionDAGISel {
53 const PPCTargetMachine &TM;
54 const PPCTargetLowering *PPCLowering;
55 const PPCSubtarget *PPCSubTarget;
56 unsigned GlobalBaseReg;
57 public:
PPCDAGToDAGISel(PPCTargetMachine & tm)58 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
59 : SelectionDAGISel(tm), TM(tm),
60 PPCLowering(TM.getTargetLowering()),
61 PPCSubTarget(TM.getSubtargetImpl()) {
62 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
63 }
64
runOnMachineFunction(MachineFunction & MF)65 bool runOnMachineFunction(MachineFunction &MF) override {
66 // Make sure we re-emit a set of the global base reg if necessary
67 GlobalBaseReg = 0;
68 PPCLowering = TM.getTargetLowering();
69 PPCSubTarget = TM.getSubtargetImpl();
70 SelectionDAGISel::runOnMachineFunction(MF);
71
72 if (!PPCSubTarget->isSVR4ABI())
73 InsertVRSaveCode(MF);
74
75 return true;
76 }
77
78 void PostprocessISelDAG() override;
79
80 /// getI32Imm - Return a target constant with the specified value, of type
81 /// i32.
getI32Imm(unsigned Imm)82 inline SDValue getI32Imm(unsigned Imm) {
83 return CurDAG->getTargetConstant(Imm, MVT::i32);
84 }
85
86 /// getI64Imm - Return a target constant with the specified value, of type
87 /// i64.
getI64Imm(uint64_t Imm)88 inline SDValue getI64Imm(uint64_t Imm) {
89 return CurDAG->getTargetConstant(Imm, MVT::i64);
90 }
91
92 /// getSmallIPtrImm - Return a target constant of pointer type.
getSmallIPtrImm(unsigned Imm)93 inline SDValue getSmallIPtrImm(unsigned Imm) {
94 return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy());
95 }
96
97 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
98 /// with any number of 0s on either side. The 1s are allowed to wrap from
99 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
100 /// 0x0F0F0000 is not, since all 1s are not contiguous.
101 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
102
103
104 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
105 /// rotate and mask opcode and mask operation.
106 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
107 unsigned &SH, unsigned &MB, unsigned &ME);
108
109 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
110 /// base register. Return the virtual register that holds this value.
111 SDNode *getGlobalBaseReg();
112
113 // Select - Convert the specified operand from a target-independent to a
114 // target-specific node if it hasn't already been changed.
115 SDNode *Select(SDNode *N) override;
116
117 SDNode *SelectBitfieldInsert(SDNode *N);
118
119 /// SelectCC - Select a comparison of the specified values with the
120 /// specified condition code, returning the CR# of the expression.
121 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
122
123 /// SelectAddrImm - Returns true if the address N can be represented by
124 /// a base register plus a signed 16-bit displacement [r+imm].
SelectAddrImm(SDValue N,SDValue & Disp,SDValue & Base)125 bool SelectAddrImm(SDValue N, SDValue &Disp,
126 SDValue &Base) {
127 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
128 }
129
130 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
131 /// immediate field. Note that the operand at this point is already the
132 /// result of a prior SelectAddressRegImm call.
SelectAddrImmOffs(SDValue N,SDValue & Out) const133 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
134 if (N.getOpcode() == ISD::TargetConstant ||
135 N.getOpcode() == ISD::TargetGlobalAddress) {
136 Out = N;
137 return true;
138 }
139
140 return false;
141 }
142
143 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
144 /// represented as an indexed [r+r] operation. Returns false if it can
145 /// be represented by [r+imm], which are preferred.
SelectAddrIdx(SDValue N,SDValue & Base,SDValue & Index)146 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
147 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
148 }
149
150 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
151 /// represented as an indexed [r+r] operation.
SelectAddrIdxOnly(SDValue N,SDValue & Base,SDValue & Index)152 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
153 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
154 }
155
156 /// SelectAddrImmX4 - Returns true if the address N can be represented by
157 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
158 /// Suitable for use by STD and friends.
SelectAddrImmX4(SDValue N,SDValue & Disp,SDValue & Base)159 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
160 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
161 }
162
163 // Select an address into a single register.
SelectAddr(SDValue N,SDValue & Base)164 bool SelectAddr(SDValue N, SDValue &Base) {
165 Base = N;
166 return true;
167 }
168
169 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
170 /// inline asm expressions. It is always correct to compute the value into
171 /// a register. The case of adding a (possibly relocatable) constant to a
172 /// register can be improved, but it is wrong to substitute Reg+Reg for
173 /// Reg in an asm, because the load or store opcode would have to change.
SelectInlineAsmMemoryOperand(const SDValue & Op,char ConstraintCode,std::vector<SDValue> & OutOps)174 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
175 char ConstraintCode,
176 std::vector<SDValue> &OutOps) override {
177 OutOps.push_back(Op);
178 return false;
179 }
180
181 void InsertVRSaveCode(MachineFunction &MF);
182
getPassName() const183 const char *getPassName() const override {
184 return "PowerPC DAG->DAG Pattern Instruction Selection";
185 }
186
187 // Include the pieces autogenerated from the target description.
188 #include "PPCGenDAGISel.inc"
189
190 private:
191 SDNode *SelectSETCC(SDNode *N);
192
193 void PeepholePPC64();
194 void PeepholeCROps();
195
196 bool AllUsersSelectZero(SDNode *N);
197 void SwapAllSelectUsers(SDNode *N);
198 };
199 }
200
201 /// InsertVRSaveCode - Once the entire function has been instruction selected,
202 /// all virtual registers are created and all machine instructions are built,
203 /// check to see if we need to save/restore VRSAVE. If so, do it.
InsertVRSaveCode(MachineFunction & Fn)204 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
205 // Check to see if this function uses vector registers, which means we have to
206 // save and restore the VRSAVE register and update it with the regs we use.
207 //
208 // In this case, there will be virtual registers of vector type created
209 // by the scheduler. Detect them now.
210 bool HasVectorVReg = false;
211 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
212 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
213 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
214 HasVectorVReg = true;
215 break;
216 }
217 }
218 if (!HasVectorVReg) return; // nothing to do.
219
220 // If we have a vector register, we want to emit code into the entry and exit
221 // blocks to save and restore the VRSAVE register. We do this here (instead
222 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
223 //
224 // 1. This (trivially) reduces the load on the register allocator, by not
225 // having to represent the live range of the VRSAVE register.
226 // 2. This (more significantly) allows us to create a temporary virtual
227 // register to hold the saved VRSAVE value, allowing this temporary to be
228 // register allocated, instead of forcing it to be spilled to the stack.
229
230 // Create two vregs - one to hold the VRSAVE register that is live-in to the
231 // function and one for the value after having bits or'd into it.
232 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
233 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
234
235 const TargetInstrInfo &TII = *TM.getInstrInfo();
236 MachineBasicBlock &EntryBB = *Fn.begin();
237 DebugLoc dl;
238 // Emit the following code into the entry block:
239 // InVRSAVE = MFVRSAVE
240 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
241 // MTVRSAVE UpdatedVRSAVE
242 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
243 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
244 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
245 UpdatedVRSAVE).addReg(InVRSAVE);
246 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
247
248 // Find all return blocks, outputting a restore in each epilog.
249 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
250 if (!BB->empty() && BB->back().isReturn()) {
251 IP = BB->end(); --IP;
252
253 // Skip over all terminator instructions, which are part of the return
254 // sequence.
255 MachineBasicBlock::iterator I2 = IP;
256 while (I2 != BB->begin() && (--I2)->isTerminator())
257 IP = I2;
258
259 // Emit: MTVRSAVE InVRSave
260 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
261 }
262 }
263 }
264
265
266 /// getGlobalBaseReg - Output the instructions required to put the
267 /// base address to use for accessing globals into a register.
268 ///
getGlobalBaseReg()269 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
270 if (!GlobalBaseReg) {
271 const TargetInstrInfo &TII = *TM.getInstrInfo();
272 // Insert the set of GlobalBaseReg into the first MBB of the function
273 MachineBasicBlock &FirstMBB = MF->front();
274 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
275 DebugLoc dl;
276
277 if (PPCLowering->getPointerTy() == MVT::i32) {
278 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
279 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
280 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
281 } else {
282 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
283 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
284 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
285 }
286 }
287 return CurDAG->getRegister(GlobalBaseReg,
288 PPCLowering->getPointerTy()).getNode();
289 }
290
291 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
292 /// or 64-bit immediate, and if the value can be accurately represented as a
293 /// sign extension from a 16-bit value. If so, this returns true and the
294 /// immediate.
isIntS16Immediate(SDNode * N,short & Imm)295 static bool isIntS16Immediate(SDNode *N, short &Imm) {
296 if (N->getOpcode() != ISD::Constant)
297 return false;
298
299 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
300 if (N->getValueType(0) == MVT::i32)
301 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
302 else
303 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
304 }
305
isIntS16Immediate(SDValue Op,short & Imm)306 static bool isIntS16Immediate(SDValue Op, short &Imm) {
307 return isIntS16Immediate(Op.getNode(), Imm);
308 }
309
310
311 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
312 /// operand. If so Imm will receive the 32-bit value.
isInt32Immediate(SDNode * N,unsigned & Imm)313 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
314 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
315 Imm = cast<ConstantSDNode>(N)->getZExtValue();
316 return true;
317 }
318 return false;
319 }
320
321 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
322 /// operand. If so Imm will receive the 64-bit value.
isInt64Immediate(SDNode * N,uint64_t & Imm)323 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
324 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
325 Imm = cast<ConstantSDNode>(N)->getZExtValue();
326 return true;
327 }
328 return false;
329 }
330
331 // isInt32Immediate - This method tests to see if a constant operand.
332 // If so Imm will receive the 32 bit value.
isInt32Immediate(SDValue N,unsigned & Imm)333 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
334 return isInt32Immediate(N.getNode(), Imm);
335 }
336
337
338 // isOpcWithIntImmediate - This method tests to see if the node is a specific
339 // opcode and that it has a immediate integer right operand.
340 // If so Imm will receive the 32 bit value.
isOpcWithIntImmediate(SDNode * N,unsigned Opc,unsigned & Imm)341 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
342 return N->getOpcode() == Opc
343 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
344 }
345
isRunOfOnes(unsigned Val,unsigned & MB,unsigned & ME)346 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
347 if (!Val)
348 return false;
349
350 if (isShiftedMask_32(Val)) {
351 // look for the first non-zero bit
352 MB = countLeadingZeros(Val);
353 // look for the first zero bit after the run of ones
354 ME = countLeadingZeros((Val - 1) ^ Val);
355 return true;
356 } else {
357 Val = ~Val; // invert mask
358 if (isShiftedMask_32(Val)) {
359 // effectively look for the first zero bit
360 ME = countLeadingZeros(Val) - 1;
361 // effectively look for the first one bit after the run of zeros
362 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
363 return true;
364 }
365 }
366 // no run present
367 return false;
368 }
369
isRotateAndMask(SDNode * N,unsigned Mask,bool isShiftMask,unsigned & SH,unsigned & MB,unsigned & ME)370 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
371 bool isShiftMask, unsigned &SH,
372 unsigned &MB, unsigned &ME) {
373 // Don't even go down this path for i64, since different logic will be
374 // necessary for rldicl/rldicr/rldimi.
375 if (N->getValueType(0) != MVT::i32)
376 return false;
377
378 unsigned Shift = 32;
379 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
380 unsigned Opcode = N->getOpcode();
381 if (N->getNumOperands() != 2 ||
382 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
383 return false;
384
385 if (Opcode == ISD::SHL) {
386 // apply shift left to mask if it comes first
387 if (isShiftMask) Mask = Mask << Shift;
388 // determine which bits are made indeterminant by shift
389 Indeterminant = ~(0xFFFFFFFFu << Shift);
390 } else if (Opcode == ISD::SRL) {
391 // apply shift right to mask if it comes first
392 if (isShiftMask) Mask = Mask >> Shift;
393 // determine which bits are made indeterminant by shift
394 Indeterminant = ~(0xFFFFFFFFu >> Shift);
395 // adjust for the left rotate
396 Shift = 32 - Shift;
397 } else if (Opcode == ISD::ROTL) {
398 Indeterminant = 0;
399 } else {
400 return false;
401 }
402
403 // if the mask doesn't intersect any Indeterminant bits
404 if (Mask && !(Mask & Indeterminant)) {
405 SH = Shift & 31;
406 // make sure the mask is still a mask (wrap arounds may not be)
407 return isRunOfOnes(Mask, MB, ME);
408 }
409 return false;
410 }
411
412 /// SelectBitfieldInsert - turn an or of two masked values into
413 /// the rotate left word immediate then mask insert (rlwimi) instruction.
SelectBitfieldInsert(SDNode * N)414 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
415 SDValue Op0 = N->getOperand(0);
416 SDValue Op1 = N->getOperand(1);
417 SDLoc dl(N);
418
419 APInt LKZ, LKO, RKZ, RKO;
420 CurDAG->computeKnownBits(Op0, LKZ, LKO);
421 CurDAG->computeKnownBits(Op1, RKZ, RKO);
422
423 unsigned TargetMask = LKZ.getZExtValue();
424 unsigned InsertMask = RKZ.getZExtValue();
425
426 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
427 unsigned Op0Opc = Op0.getOpcode();
428 unsigned Op1Opc = Op1.getOpcode();
429 unsigned Value, SH = 0;
430 TargetMask = ~TargetMask;
431 InsertMask = ~InsertMask;
432
433 // If the LHS has a foldable shift and the RHS does not, then swap it to the
434 // RHS so that we can fold the shift into the insert.
435 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
436 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
437 Op0.getOperand(0).getOpcode() == ISD::SRL) {
438 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
439 Op1.getOperand(0).getOpcode() != ISD::SRL) {
440 std::swap(Op0, Op1);
441 std::swap(Op0Opc, Op1Opc);
442 std::swap(TargetMask, InsertMask);
443 }
444 }
445 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
446 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
447 Op1.getOperand(0).getOpcode() != ISD::SRL) {
448 std::swap(Op0, Op1);
449 std::swap(Op0Opc, Op1Opc);
450 std::swap(TargetMask, InsertMask);
451 }
452 }
453
454 unsigned MB, ME;
455 if (isRunOfOnes(InsertMask, MB, ME)) {
456 SDValue Tmp1, Tmp2;
457
458 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
459 isInt32Immediate(Op1.getOperand(1), Value)) {
460 Op1 = Op1.getOperand(0);
461 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
462 }
463 if (Op1Opc == ISD::AND) {
464 // The AND mask might not be a constant, and we need to make sure that
465 // if we're going to fold the masking with the insert, all bits not
466 // know to be zero in the mask are known to be one.
467 APInt MKZ, MKO;
468 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
469 bool CanFoldMask = InsertMask == MKO.getZExtValue();
470
471 unsigned SHOpc = Op1.getOperand(0).getOpcode();
472 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
473 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
474 // Note that Value must be in range here (less than 32) because
475 // otherwise there would not be any bits set in InsertMask.
476 Op1 = Op1.getOperand(0).getOperand(0);
477 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
478 }
479 }
480
481 SH &= 31;
482 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
483 getI32Imm(ME) };
484 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
485 }
486 }
487 return nullptr;
488 }
489
490 /// SelectCC - Select a comparison of the specified values with the specified
491 /// condition code, returning the CR# of the expression.
SelectCC(SDValue LHS,SDValue RHS,ISD::CondCode CC,SDLoc dl)492 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
493 ISD::CondCode CC, SDLoc dl) {
494 // Always select the LHS.
495 unsigned Opc;
496
497 if (LHS.getValueType() == MVT::i32) {
498 unsigned Imm;
499 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
500 if (isInt32Immediate(RHS, Imm)) {
501 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
502 if (isUInt<16>(Imm))
503 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
504 getI32Imm(Imm & 0xFFFF)), 0);
505 // If this is a 16-bit signed immediate, fold it.
506 if (isInt<16>((int)Imm))
507 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
508 getI32Imm(Imm & 0xFFFF)), 0);
509
510 // For non-equality comparisons, the default code would materialize the
511 // constant, then compare against it, like this:
512 // lis r2, 4660
513 // ori r2, r2, 22136
514 // cmpw cr0, r3, r2
515 // Since we are just comparing for equality, we can emit this instead:
516 // xoris r0,r3,0x1234
517 // cmplwi cr0,r0,0x5678
518 // beq cr0,L6
519 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
520 getI32Imm(Imm >> 16)), 0);
521 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
522 getI32Imm(Imm & 0xFFFF)), 0);
523 }
524 Opc = PPC::CMPLW;
525 } else if (ISD::isUnsignedIntSetCC(CC)) {
526 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
527 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
528 getI32Imm(Imm & 0xFFFF)), 0);
529 Opc = PPC::CMPLW;
530 } else {
531 short SImm;
532 if (isIntS16Immediate(RHS, SImm))
533 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
534 getI32Imm((int)SImm & 0xFFFF)),
535 0);
536 Opc = PPC::CMPW;
537 }
538 } else if (LHS.getValueType() == MVT::i64) {
539 uint64_t Imm;
540 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
541 if (isInt64Immediate(RHS.getNode(), Imm)) {
542 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
543 if (isUInt<16>(Imm))
544 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
545 getI32Imm(Imm & 0xFFFF)), 0);
546 // If this is a 16-bit signed immediate, fold it.
547 if (isInt<16>(Imm))
548 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
549 getI32Imm(Imm & 0xFFFF)), 0);
550
551 // For non-equality comparisons, the default code would materialize the
552 // constant, then compare against it, like this:
553 // lis r2, 4660
554 // ori r2, r2, 22136
555 // cmpd cr0, r3, r2
556 // Since we are just comparing for equality, we can emit this instead:
557 // xoris r0,r3,0x1234
558 // cmpldi cr0,r0,0x5678
559 // beq cr0,L6
560 if (isUInt<32>(Imm)) {
561 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
562 getI64Imm(Imm >> 16)), 0);
563 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
564 getI64Imm(Imm & 0xFFFF)), 0);
565 }
566 }
567 Opc = PPC::CMPLD;
568 } else if (ISD::isUnsignedIntSetCC(CC)) {
569 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
570 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
571 getI64Imm(Imm & 0xFFFF)), 0);
572 Opc = PPC::CMPLD;
573 } else {
574 short SImm;
575 if (isIntS16Immediate(RHS, SImm))
576 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
577 getI64Imm(SImm & 0xFFFF)),
578 0);
579 Opc = PPC::CMPD;
580 }
581 } else if (LHS.getValueType() == MVT::f32) {
582 Opc = PPC::FCMPUS;
583 } else {
584 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
585 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
586 }
587 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
588 }
589
getPredicateForSetCC(ISD::CondCode CC)590 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
591 switch (CC) {
592 case ISD::SETUEQ:
593 case ISD::SETONE:
594 case ISD::SETOLE:
595 case ISD::SETOGE:
596 llvm_unreachable("Should be lowered by legalize!");
597 default: llvm_unreachable("Unknown condition!");
598 case ISD::SETOEQ:
599 case ISD::SETEQ: return PPC::PRED_EQ;
600 case ISD::SETUNE:
601 case ISD::SETNE: return PPC::PRED_NE;
602 case ISD::SETOLT:
603 case ISD::SETLT: return PPC::PRED_LT;
604 case ISD::SETULE:
605 case ISD::SETLE: return PPC::PRED_LE;
606 case ISD::SETOGT:
607 case ISD::SETGT: return PPC::PRED_GT;
608 case ISD::SETUGE:
609 case ISD::SETGE: return PPC::PRED_GE;
610 case ISD::SETO: return PPC::PRED_NU;
611 case ISD::SETUO: return PPC::PRED_UN;
612 // These two are invalid for floating point. Assume we have int.
613 case ISD::SETULT: return PPC::PRED_LT;
614 case ISD::SETUGT: return PPC::PRED_GT;
615 }
616 }
617
618 /// getCRIdxForSetCC - Return the index of the condition register field
619 /// associated with the SetCC condition, and whether or not the field is
620 /// treated as inverted. That is, lt = 0; ge = 0 inverted.
getCRIdxForSetCC(ISD::CondCode CC,bool & Invert)621 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
622 Invert = false;
623 switch (CC) {
624 default: llvm_unreachable("Unknown condition!");
625 case ISD::SETOLT:
626 case ISD::SETLT: return 0; // Bit #0 = SETOLT
627 case ISD::SETOGT:
628 case ISD::SETGT: return 1; // Bit #1 = SETOGT
629 case ISD::SETOEQ:
630 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
631 case ISD::SETUO: return 3; // Bit #3 = SETUO
632 case ISD::SETUGE:
633 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
634 case ISD::SETULE:
635 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
636 case ISD::SETUNE:
637 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
638 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
639 case ISD::SETUEQ:
640 case ISD::SETOGE:
641 case ISD::SETOLE:
642 case ISD::SETONE:
643 llvm_unreachable("Invalid branch code: should be expanded by legalize");
644 // These are invalid for floating point. Assume integer.
645 case ISD::SETULT: return 0;
646 case ISD::SETUGT: return 1;
647 }
648 }
649
650 // getVCmpInst: return the vector compare instruction for the specified
651 // vector type and condition code. Since this is for altivec specific code,
652 // only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
getVCmpInst(MVT::SimpleValueType VecVT,ISD::CondCode CC,bool HasVSX)653 static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC,
654 bool HasVSX) {
655 switch (CC) {
656 case ISD::SETEQ:
657 case ISD::SETUEQ:
658 case ISD::SETNE:
659 case ISD::SETUNE:
660 if (VecVT == MVT::v16i8)
661 return PPC::VCMPEQUB;
662 else if (VecVT == MVT::v8i16)
663 return PPC::VCMPEQUH;
664 else if (VecVT == MVT::v4i32)
665 return PPC::VCMPEQUW;
666 // v4f32 != v4f32 could be translate to unordered not equal
667 else if (VecVT == MVT::v4f32)
668 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
669 else if (VecVT == MVT::v2f64)
670 return PPC::XVCMPEQDP;
671 break;
672 case ISD::SETLT:
673 case ISD::SETGT:
674 case ISD::SETLE:
675 case ISD::SETGE:
676 if (VecVT == MVT::v16i8)
677 return PPC::VCMPGTSB;
678 else if (VecVT == MVT::v8i16)
679 return PPC::VCMPGTSH;
680 else if (VecVT == MVT::v4i32)
681 return PPC::VCMPGTSW;
682 else if (VecVT == MVT::v4f32)
683 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
684 else if (VecVT == MVT::v2f64)
685 return PPC::XVCMPGTDP;
686 break;
687 case ISD::SETULT:
688 case ISD::SETUGT:
689 case ISD::SETUGE:
690 case ISD::SETULE:
691 if (VecVT == MVT::v16i8)
692 return PPC::VCMPGTUB;
693 else if (VecVT == MVT::v8i16)
694 return PPC::VCMPGTUH;
695 else if (VecVT == MVT::v4i32)
696 return PPC::VCMPGTUW;
697 break;
698 case ISD::SETOEQ:
699 if (VecVT == MVT::v4f32)
700 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
701 else if (VecVT == MVT::v2f64)
702 return PPC::XVCMPEQDP;
703 break;
704 case ISD::SETOLT:
705 case ISD::SETOGT:
706 case ISD::SETOLE:
707 if (VecVT == MVT::v4f32)
708 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
709 else if (VecVT == MVT::v2f64)
710 return PPC::XVCMPGTDP;
711 break;
712 case ISD::SETOGE:
713 if (VecVT == MVT::v4f32)
714 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
715 else if (VecVT == MVT::v2f64)
716 return PPC::XVCMPGEDP;
717 break;
718 default:
719 break;
720 }
721 llvm_unreachable("Invalid integer vector compare condition");
722 }
723
724 // getVCmpEQInst: return the equal compare instruction for the specified vector
725 // type. Since this is for altivec specific code, only support the altivec
726 // types (v16i8, v8i16, v4i32, and v4f32).
getVCmpEQInst(MVT::SimpleValueType VecVT,bool HasVSX)727 static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT, bool HasVSX) {
728 switch (VecVT) {
729 case MVT::v16i8:
730 return PPC::VCMPEQUB;
731 case MVT::v8i16:
732 return PPC::VCMPEQUH;
733 case MVT::v4i32:
734 return PPC::VCMPEQUW;
735 case MVT::v4f32:
736 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
737 case MVT::v2f64:
738 return PPC::XVCMPEQDP;
739 default:
740 llvm_unreachable("Invalid integer vector compare condition");
741 }
742 }
743
SelectSETCC(SDNode * N)744 SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
745 SDLoc dl(N);
746 unsigned Imm;
747 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
748 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
749 bool isPPC64 = (PtrVT == MVT::i64);
750
751 if (!PPCSubTarget->useCRBits() &&
752 isInt32Immediate(N->getOperand(1), Imm)) {
753 // We can codegen setcc op, imm very efficiently compared to a brcond.
754 // Check for those cases here.
755 // setcc op, 0
756 if (Imm == 0) {
757 SDValue Op = N->getOperand(0);
758 switch (CC) {
759 default: break;
760 case ISD::SETEQ: {
761 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
762 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
763 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
764 }
765 case ISD::SETNE: {
766 if (isPPC64) break;
767 SDValue AD =
768 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
769 Op, getI32Imm(~0U)), 0);
770 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
771 AD.getValue(1));
772 }
773 case ISD::SETLT: {
774 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
775 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
776 }
777 case ISD::SETGT: {
778 SDValue T =
779 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
780 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
781 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
782 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
783 }
784 }
785 } else if (Imm == ~0U) { // setcc op, -1
786 SDValue Op = N->getOperand(0);
787 switch (CC) {
788 default: break;
789 case ISD::SETEQ:
790 if (isPPC64) break;
791 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
792 Op, getI32Imm(1)), 0);
793 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
794 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
795 MVT::i32,
796 getI32Imm(0)), 0),
797 Op.getValue(1));
798 case ISD::SETNE: {
799 if (isPPC64) break;
800 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
801 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
802 Op, getI32Imm(~0U));
803 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
804 Op, SDValue(AD, 1));
805 }
806 case ISD::SETLT: {
807 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
808 getI32Imm(1)), 0);
809 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
810 Op), 0);
811 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
812 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
813 }
814 case ISD::SETGT: {
815 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
816 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
817 0);
818 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
819 getI32Imm(1));
820 }
821 }
822 }
823 }
824
825 SDValue LHS = N->getOperand(0);
826 SDValue RHS = N->getOperand(1);
827
828 // Altivec Vector compare instructions do not set any CR register by default and
829 // vector compare operations return the same type as the operands.
830 if (LHS.getValueType().isVector()) {
831 EVT VecVT = LHS.getValueType();
832 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
833 unsigned int VCmpInst = getVCmpInst(VT, CC, PPCSubTarget->hasVSX());
834
835 switch (CC) {
836 case ISD::SETEQ:
837 case ISD::SETOEQ:
838 case ISD::SETUEQ:
839 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
840 case ISD::SETNE:
841 case ISD::SETONE:
842 case ISD::SETUNE: {
843 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
844 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
845 PPC::VNOR,
846 VecVT, VCmp, VCmp);
847 }
848 case ISD::SETLT:
849 case ISD::SETOLT:
850 case ISD::SETULT:
851 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
852 case ISD::SETGT:
853 case ISD::SETOGT:
854 case ISD::SETUGT:
855 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
856 case ISD::SETGE:
857 case ISD::SETOGE:
858 case ISD::SETUGE: {
859 // Small optimization: Altivec provides a 'Vector Compare Greater Than
860 // or Equal To' instruction (vcmpgefp), so in this case there is no
861 // need for extra logic for the equal compare.
862 if (VecVT.getSimpleVT().isFloatingPoint()) {
863 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
864 } else {
865 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
866 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget->hasVSX());
867 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
868 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLOR :
869 PPC::VOR,
870 VecVT, VCmpGT, VCmpEQ);
871 }
872 }
873 case ISD::SETLE:
874 case ISD::SETOLE:
875 case ISD::SETULE: {
876 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
877 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget->hasVSX());
878 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
879 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLOR :
880 PPC::VOR,
881 VecVT, VCmpLE, VCmpEQ);
882 }
883 default:
884 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
885 }
886 }
887
888 if (PPCSubTarget->useCRBits())
889 return nullptr;
890
891 bool Inv;
892 unsigned Idx = getCRIdxForSetCC(CC, Inv);
893 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
894 SDValue IntCR;
895
896 // Force the ccreg into CR7.
897 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
898
899 SDValue InFlag(nullptr, 0); // Null incoming flag value.
900 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
901 InFlag).getValue(1);
902
903 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
904 CCReg), 0);
905
906 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
907 getI32Imm(31), getI32Imm(31) };
908 if (!Inv)
909 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
910
911 // Get the specified bit.
912 SDValue Tmp =
913 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
914 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
915 }
916
917
918 // Select - Convert the specified operand from a target-independent to a
919 // target-specific node if it hasn't already been changed.
Select(SDNode * N)920 SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
921 SDLoc dl(N);
922 if (N->isMachineOpcode()) {
923 N->setNodeId(-1);
924 return nullptr; // Already selected.
925 }
926
927 switch (N->getOpcode()) {
928 default: break;
929
930 case ISD::Constant: {
931 if (N->getValueType(0) == MVT::i64) {
932 // Get 64 bit value.
933 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
934 // Assume no remaining bits.
935 unsigned Remainder = 0;
936 // Assume no shift required.
937 unsigned Shift = 0;
938
939 // If it can't be represented as a 32 bit value.
940 if (!isInt<32>(Imm)) {
941 Shift = countTrailingZeros<uint64_t>(Imm);
942 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
943
944 // If the shifted value fits 32 bits.
945 if (isInt<32>(ImmSh)) {
946 // Go with the shifted value.
947 Imm = ImmSh;
948 } else {
949 // Still stuck with a 64 bit value.
950 Remainder = Imm;
951 Shift = 32;
952 Imm >>= 32;
953 }
954 }
955
956 // Intermediate operand.
957 SDNode *Result;
958
959 // Handle first 32 bits.
960 unsigned Lo = Imm & 0xFFFF;
961 unsigned Hi = (Imm >> 16) & 0xFFFF;
962
963 // Simple value.
964 if (isInt<16>(Imm)) {
965 // Just the Lo bits.
966 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
967 } else if (Lo) {
968 // Handle the Hi bits.
969 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
970 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
971 // And Lo bits.
972 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
973 SDValue(Result, 0), getI32Imm(Lo));
974 } else {
975 // Just the Hi bits.
976 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
977 }
978
979 // If no shift, we're done.
980 if (!Shift) return Result;
981
982 // Shift for next step if the upper 32-bits were not zero.
983 if (Imm) {
984 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
985 SDValue(Result, 0),
986 getI32Imm(Shift),
987 getI32Imm(63 - Shift));
988 }
989
990 // Add in the last bits as required.
991 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
992 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
993 SDValue(Result, 0), getI32Imm(Hi));
994 }
995 if ((Lo = Remainder & 0xFFFF)) {
996 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
997 SDValue(Result, 0), getI32Imm(Lo));
998 }
999
1000 return Result;
1001 }
1002 break;
1003 }
1004
1005 case ISD::SETCC: {
1006 SDNode *SN = SelectSETCC(N);
1007 if (SN)
1008 return SN;
1009 break;
1010 }
1011 case PPCISD::GlobalBaseReg:
1012 return getGlobalBaseReg();
1013
1014 case ISD::FrameIndex: {
1015 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1016 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
1017 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
1018 if (N->hasOneUse())
1019 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
1020 getSmallIPtrImm(0));
1021 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
1022 getSmallIPtrImm(0));
1023 }
1024
1025 case PPCISD::MFOCRF: {
1026 SDValue InFlag = N->getOperand(1);
1027 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
1028 N->getOperand(0), InFlag);
1029 }
1030
1031 case ISD::SDIV: {
1032 // FIXME: since this depends on the setting of the carry flag from the srawi
1033 // we should really be making notes about that for the scheduler.
1034 // FIXME: It sure would be nice if we could cheaply recognize the
1035 // srl/add/sra pattern the dag combiner will generate for this as
1036 // sra/addze rather than having to handle sdiv ourselves. oh well.
1037 unsigned Imm;
1038 if (isInt32Immediate(N->getOperand(1), Imm)) {
1039 SDValue N0 = N->getOperand(0);
1040 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
1041 SDNode *Op =
1042 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1043 N0, getI32Imm(Log2_32(Imm)));
1044 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
1045 SDValue(Op, 0), SDValue(Op, 1));
1046 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
1047 SDNode *Op =
1048 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1049 N0, getI32Imm(Log2_32(-Imm)));
1050 SDValue PT =
1051 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1052 SDValue(Op, 0), SDValue(Op, 1)),
1053 0);
1054 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
1055 }
1056 }
1057
1058 // Other cases are autogenerated.
1059 break;
1060 }
1061
1062 case ISD::LOAD: {
1063 // Handle preincrement loads.
1064 LoadSDNode *LD = cast<LoadSDNode>(N);
1065 EVT LoadedVT = LD->getMemoryVT();
1066
1067 // Normal loads are handled by code generated from the .td file.
1068 if (LD->getAddressingMode() != ISD::PRE_INC)
1069 break;
1070
1071 SDValue Offset = LD->getOffset();
1072 if (Offset.getOpcode() == ISD::TargetConstant ||
1073 Offset.getOpcode() == ISD::TargetGlobalAddress) {
1074
1075 unsigned Opcode;
1076 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1077 if (LD->getValueType(0) != MVT::i64) {
1078 // Handle PPC32 integer and normal FP loads.
1079 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1080 switch (LoadedVT.getSimpleVT().SimpleTy) {
1081 default: llvm_unreachable("Invalid PPC load type!");
1082 case MVT::f64: Opcode = PPC::LFDU; break;
1083 case MVT::f32: Opcode = PPC::LFSU; break;
1084 case MVT::i32: Opcode = PPC::LWZU; break;
1085 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1086 case MVT::i1:
1087 case MVT::i8: Opcode = PPC::LBZU; break;
1088 }
1089 } else {
1090 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1091 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1092 switch (LoadedVT.getSimpleVT().SimpleTy) {
1093 default: llvm_unreachable("Invalid PPC load type!");
1094 case MVT::i64: Opcode = PPC::LDU; break;
1095 case MVT::i32: Opcode = PPC::LWZU8; break;
1096 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1097 case MVT::i1:
1098 case MVT::i8: Opcode = PPC::LBZU8; break;
1099 }
1100 }
1101
1102 SDValue Chain = LD->getChain();
1103 SDValue Base = LD->getBasePtr();
1104 SDValue Ops[] = { Offset, Base, Chain };
1105 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1106 PPCLowering->getPointerTy(),
1107 MVT::Other, Ops);
1108 } else {
1109 unsigned Opcode;
1110 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1111 if (LD->getValueType(0) != MVT::i64) {
1112 // Handle PPC32 integer and normal FP loads.
1113 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1114 switch (LoadedVT.getSimpleVT().SimpleTy) {
1115 default: llvm_unreachable("Invalid PPC load type!");
1116 case MVT::f64: Opcode = PPC::LFDUX; break;
1117 case MVT::f32: Opcode = PPC::LFSUX; break;
1118 case MVT::i32: Opcode = PPC::LWZUX; break;
1119 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1120 case MVT::i1:
1121 case MVT::i8: Opcode = PPC::LBZUX; break;
1122 }
1123 } else {
1124 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1125 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1126 "Invalid sext update load");
1127 switch (LoadedVT.getSimpleVT().SimpleTy) {
1128 default: llvm_unreachable("Invalid PPC load type!");
1129 case MVT::i64: Opcode = PPC::LDUX; break;
1130 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1131 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1132 case MVT::i1:
1133 case MVT::i8: Opcode = PPC::LBZUX8; break;
1134 }
1135 }
1136
1137 SDValue Chain = LD->getChain();
1138 SDValue Base = LD->getBasePtr();
1139 SDValue Ops[] = { Base, Offset, Chain };
1140 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1141 PPCLowering->getPointerTy(),
1142 MVT::Other, Ops);
1143 }
1144 }
1145
1146 case ISD::AND: {
1147 unsigned Imm, Imm2, SH, MB, ME;
1148 uint64_t Imm64;
1149
1150 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1151 // with a mask, emit rlwinm
1152 if (isInt32Immediate(N->getOperand(1), Imm) &&
1153 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
1154 SDValue Val = N->getOperand(0).getOperand(0);
1155 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1156 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
1157 }
1158 // If this is just a masked value where the input is not handled above, and
1159 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1160 if (isInt32Immediate(N->getOperand(1), Imm) &&
1161 isRunOfOnes(Imm, MB, ME) &&
1162 N->getOperand(0).getOpcode() != ISD::ROTL) {
1163 SDValue Val = N->getOperand(0);
1164 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
1165 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
1166 }
1167 // If this is a 64-bit zero-extension mask, emit rldicl.
1168 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1169 isMask_64(Imm64)) {
1170 SDValue Val = N->getOperand(0);
1171 MB = 64 - CountTrailingOnes_64(Imm64);
1172 SH = 0;
1173
1174 // If the operand is a logical right shift, we can fold it into this
1175 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1176 // for n <= mb. The right shift is really a left rotate followed by a
1177 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1178 // by the shift.
1179 if (Val.getOpcode() == ISD::SRL &&
1180 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1181 assert(Imm < 64 && "Illegal shift amount");
1182 Val = Val.getOperand(0);
1183 SH = 64 - Imm;
1184 }
1185
1186 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
1187 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
1188 }
1189 // AND X, 0 -> 0, not "rlwinm 32".
1190 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
1191 ReplaceUses(SDValue(N, 0), N->getOperand(1));
1192 return nullptr;
1193 }
1194 // ISD::OR doesn't get all the bitfield insertion fun.
1195 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1196 if (isInt32Immediate(N->getOperand(1), Imm) &&
1197 N->getOperand(0).getOpcode() == ISD::OR &&
1198 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
1199 unsigned MB, ME;
1200 Imm = ~(Imm^Imm2);
1201 if (isRunOfOnes(Imm, MB, ME)) {
1202 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1203 N->getOperand(0).getOperand(1),
1204 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
1205 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
1206 }
1207 }
1208
1209 // Other cases are autogenerated.
1210 break;
1211 }
1212 case ISD::OR:
1213 if (N->getValueType(0) == MVT::i32)
1214 if (SDNode *I = SelectBitfieldInsert(N))
1215 return I;
1216
1217 // Other cases are autogenerated.
1218 break;
1219 case ISD::SHL: {
1220 unsigned Imm, SH, MB, ME;
1221 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1222 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1223 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1224 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1225 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
1226 }
1227
1228 // Other cases are autogenerated.
1229 break;
1230 }
1231 case ISD::SRL: {
1232 unsigned Imm, SH, MB, ME;
1233 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1234 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1235 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1236 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1237 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
1238 }
1239
1240 // Other cases are autogenerated.
1241 break;
1242 }
1243 // FIXME: Remove this once the ANDI glue bug is fixed:
1244 case PPCISD::ANDIo_1_EQ_BIT:
1245 case PPCISD::ANDIo_1_GT_BIT: {
1246 if (!ANDIGlueBug)
1247 break;
1248
1249 EVT InVT = N->getOperand(0).getValueType();
1250 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1251 "Invalid input type for ANDIo_1_EQ_BIT");
1252
1253 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1254 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1255 N->getOperand(0),
1256 CurDAG->getTargetConstant(1, InVT)), 0);
1257 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1258 SDValue SRIdxVal =
1259 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1260 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1261
1262 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1263 CR0Reg, SRIdxVal,
1264 SDValue(AndI.getNode(), 1) /* glue */);
1265 }
1266 case ISD::SELECT_CC: {
1267 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1268 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1269 bool isPPC64 = (PtrVT == MVT::i64);
1270
1271 // If this is a select of i1 operands, we'll pattern match it.
1272 if (PPCSubTarget->useCRBits() &&
1273 N->getOperand(0).getValueType() == MVT::i1)
1274 break;
1275
1276 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1277 if (!isPPC64)
1278 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1279 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1280 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1281 if (N1C->isNullValue() && N3C->isNullValue() &&
1282 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1283 // FIXME: Implement this optzn for PPC64.
1284 N->getValueType(0) == MVT::i32) {
1285 SDNode *Tmp =
1286 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1287 N->getOperand(0), getI32Imm(~0U));
1288 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1289 SDValue(Tmp, 0), N->getOperand(0),
1290 SDValue(Tmp, 1));
1291 }
1292
1293 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
1294
1295 if (N->getValueType(0) == MVT::i1) {
1296 // An i1 select is: (c & t) | (!c & f).
1297 bool Inv;
1298 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1299
1300 unsigned SRI;
1301 switch (Idx) {
1302 default: llvm_unreachable("Invalid CC index");
1303 case 0: SRI = PPC::sub_lt; break;
1304 case 1: SRI = PPC::sub_gt; break;
1305 case 2: SRI = PPC::sub_eq; break;
1306 case 3: SRI = PPC::sub_un; break;
1307 }
1308
1309 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1310
1311 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1312 CCBit, CCBit), 0);
1313 SDValue C = Inv ? NotCCBit : CCBit,
1314 NotC = Inv ? CCBit : NotCCBit;
1315
1316 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1317 C, N->getOperand(2)), 0);
1318 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1319 NotC, N->getOperand(3)), 0);
1320
1321 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1322 }
1323
1324 unsigned BROpc = getPredicateForSetCC(CC);
1325
1326 unsigned SelectCCOp;
1327 if (N->getValueType(0) == MVT::i32)
1328 SelectCCOp = PPC::SELECT_CC_I4;
1329 else if (N->getValueType(0) == MVT::i64)
1330 SelectCCOp = PPC::SELECT_CC_I8;
1331 else if (N->getValueType(0) == MVT::f32)
1332 SelectCCOp = PPC::SELECT_CC_F4;
1333 else if (N->getValueType(0) == MVT::f64)
1334 SelectCCOp = PPC::SELECT_CC_F8;
1335 else
1336 SelectCCOp = PPC::SELECT_CC_VRRC;
1337
1338 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
1339 getI32Imm(BROpc) };
1340 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
1341 }
1342 case ISD::VSELECT:
1343 if (PPCSubTarget->hasVSX()) {
1344 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
1345 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
1346 }
1347
1348 break;
1349 case ISD::VECTOR_SHUFFLE:
1350 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
1351 N->getValueType(0) == MVT::v2i64)) {
1352 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1353
1354 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1355 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1356 unsigned DM[2];
1357
1358 for (int i = 0; i < 2; ++i)
1359 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1360 DM[i] = 0;
1361 else
1362 DM[i] = 1;
1363
1364 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
1365
1366 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1367 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1368 isa<LoadSDNode>(Op1.getOperand(0))) {
1369 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1370 SDValue Base, Offset;
1371
1372 if (LD->isUnindexed() &&
1373 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1374 SDValue Chain = LD->getChain();
1375 SDValue Ops[] = { Base, Offset, Chain };
1376 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
1377 N->getValueType(0), Ops);
1378 }
1379 }
1380
1381 SDValue Ops[] = { Op1, Op2, DMV };
1382 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
1383 }
1384
1385 break;
1386 case PPCISD::BDNZ:
1387 case PPCISD::BDZ: {
1388 bool IsPPC64 = PPCSubTarget->isPPC64();
1389 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1390 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1391 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1392 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
1393 MVT::Other, Ops);
1394 }
1395 case PPCISD::COND_BRANCH: {
1396 // Op #0 is the Chain.
1397 // Op #1 is the PPC::PRED_* number.
1398 // Op #2 is the CR#
1399 // Op #3 is the Dest MBB
1400 // Op #4 is the Flag.
1401 // Prevent PPC::PRED_* from being selected into LI.
1402 SDValue Pred =
1403 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
1404 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
1405 N->getOperand(0), N->getOperand(4) };
1406 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
1407 }
1408 case ISD::BR_CC: {
1409 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1410 unsigned PCC = getPredicateForSetCC(CC);
1411
1412 if (N->getOperand(2).getValueType() == MVT::i1) {
1413 unsigned Opc;
1414 bool Swap;
1415 switch (PCC) {
1416 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1417 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1418 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1419 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1420 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1421 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1422 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1423 }
1424
1425 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1426 N->getOperand(Swap ? 3 : 2),
1427 N->getOperand(Swap ? 2 : 3)), 0);
1428 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1429 BitComp, N->getOperand(4), N->getOperand(0));
1430 }
1431
1432 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
1433 SDValue Ops[] = { getI32Imm(PCC), CondCode,
1434 N->getOperand(4), N->getOperand(0) };
1435 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
1436 }
1437 case ISD::BRIND: {
1438 // FIXME: Should custom lower this.
1439 SDValue Chain = N->getOperand(0);
1440 SDValue Target = N->getOperand(1);
1441 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1442 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
1443 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
1444 Chain), 0);
1445 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
1446 }
1447 case PPCISD::TOC_ENTRY: {
1448 assert (PPCSubTarget->isPPC64() && "Only supported for 64-bit ABI");
1449
1450 // For medium and large code model, we generate two instructions as
1451 // described below. Otherwise we allow SelectCodeCommon to handle this,
1452 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1453 CodeModel::Model CModel = TM.getCodeModel();
1454 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
1455 break;
1456
1457 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
1458 // If it is an externally defined symbol, a symbol with common linkage,
1459 // a non-local function address, or a jump table address, or if we are
1460 // generating code for large code model, we generate:
1461 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1462 // Otherwise we generate:
1463 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1464 SDValue GA = N->getOperand(0);
1465 SDValue TOCbase = N->getOperand(1);
1466 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1467 TOCbase, GA);
1468
1469 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
1470 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1471 SDValue(Tmp, 0));
1472
1473 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1474 const GlobalValue *GValue = G->getGlobal();
1475 if ((GValue->getType()->getElementType()->isFunctionTy() &&
1476 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
1477 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
1478 GValue->hasAvailableExternallyLinkage())
1479 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1480 SDValue(Tmp, 0));
1481 }
1482
1483 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1484 SDValue(Tmp, 0), GA);
1485 }
1486 case PPCISD::VADD_SPLAT: {
1487 // This expands into one of three sequences, depending on whether
1488 // the first operand is odd or even, positive or negative.
1489 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1490 isa<ConstantSDNode>(N->getOperand(1)) &&
1491 "Invalid operand on VADD_SPLAT!");
1492
1493 int Elt = N->getConstantOperandVal(0);
1494 int EltSize = N->getConstantOperandVal(1);
1495 unsigned Opc1, Opc2, Opc3;
1496 EVT VT;
1497
1498 if (EltSize == 1) {
1499 Opc1 = PPC::VSPLTISB;
1500 Opc2 = PPC::VADDUBM;
1501 Opc3 = PPC::VSUBUBM;
1502 VT = MVT::v16i8;
1503 } else if (EltSize == 2) {
1504 Opc1 = PPC::VSPLTISH;
1505 Opc2 = PPC::VADDUHM;
1506 Opc3 = PPC::VSUBUHM;
1507 VT = MVT::v8i16;
1508 } else {
1509 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1510 Opc1 = PPC::VSPLTISW;
1511 Opc2 = PPC::VADDUWM;
1512 Opc3 = PPC::VSUBUWM;
1513 VT = MVT::v4i32;
1514 }
1515
1516 if ((Elt & 1) == 0) {
1517 // Elt is even, in the range [-32,-18] + [16,30].
1518 //
1519 // Convert: VADD_SPLAT elt, size
1520 // Into: tmp = VSPLTIS[BHW] elt
1521 // VADDU[BHW]M tmp, tmp
1522 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1523 SDValue EltVal = getI32Imm(Elt >> 1);
1524 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1525 SDValue TmpVal = SDValue(Tmp, 0);
1526 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1527
1528 } else if (Elt > 0) {
1529 // Elt is odd and positive, in the range [17,31].
1530 //
1531 // Convert: VADD_SPLAT elt, size
1532 // Into: tmp1 = VSPLTIS[BHW] elt-16
1533 // tmp2 = VSPLTIS[BHW] -16
1534 // VSUBU[BHW]M tmp1, tmp2
1535 SDValue EltVal = getI32Imm(Elt - 16);
1536 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1537 EltVal = getI32Imm(-16);
1538 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1539 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1540 SDValue(Tmp2, 0));
1541
1542 } else {
1543 // Elt is odd and negative, in the range [-31,-17].
1544 //
1545 // Convert: VADD_SPLAT elt, size
1546 // Into: tmp1 = VSPLTIS[BHW] elt+16
1547 // tmp2 = VSPLTIS[BHW] -16
1548 // VADDU[BHW]M tmp1, tmp2
1549 SDValue EltVal = getI32Imm(Elt + 16);
1550 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1551 EltVal = getI32Imm(-16);
1552 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1553 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1554 SDValue(Tmp2, 0));
1555 }
1556 }
1557 }
1558
1559 return SelectCode(N);
1560 }
1561
1562 /// PostprocessISelDAG - Perform some late peephole optimizations
1563 /// on the DAG representation.
PostprocessISelDAG()1564 void PPCDAGToDAGISel::PostprocessISelDAG() {
1565
1566 // Skip peepholes at -O0.
1567 if (TM.getOptLevel() == CodeGenOpt::None)
1568 return;
1569
1570 PeepholePPC64();
1571 PeepholeCROps();
1572 }
1573
1574 // Check if all users of this node will become isel where the second operand
1575 // is the constant zero. If this is so, and if we can negate the condition,
1576 // then we can flip the true and false operands. This will allow the zero to
1577 // be folded with the isel so that we don't need to materialize a register
1578 // containing zero.
AllUsersSelectZero(SDNode * N)1579 bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1580 // If we're not using isel, then this does not matter.
1581 if (!PPCSubTarget->hasISEL())
1582 return false;
1583
1584 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1585 UI != UE; ++UI) {
1586 SDNode *User = *UI;
1587 if (!User->isMachineOpcode())
1588 return false;
1589 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1590 User->getMachineOpcode() != PPC::SELECT_I8)
1591 return false;
1592
1593 SDNode *Op2 = User->getOperand(2).getNode();
1594 if (!Op2->isMachineOpcode())
1595 return false;
1596
1597 if (Op2->getMachineOpcode() != PPC::LI &&
1598 Op2->getMachineOpcode() != PPC::LI8)
1599 return false;
1600
1601 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1602 if (!C)
1603 return false;
1604
1605 if (!C->isNullValue())
1606 return false;
1607 }
1608
1609 return true;
1610 }
1611
SwapAllSelectUsers(SDNode * N)1612 void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1613 SmallVector<SDNode *, 4> ToReplace;
1614 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1615 UI != UE; ++UI) {
1616 SDNode *User = *UI;
1617 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1618 User->getMachineOpcode() == PPC::SELECT_I8) &&
1619 "Must have all select users");
1620 ToReplace.push_back(User);
1621 }
1622
1623 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1624 UE = ToReplace.end(); UI != UE; ++UI) {
1625 SDNode *User = *UI;
1626 SDNode *ResNode =
1627 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1628 User->getValueType(0), User->getOperand(0),
1629 User->getOperand(2),
1630 User->getOperand(1));
1631
1632 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1633 DEBUG(User->dump(CurDAG));
1634 DEBUG(dbgs() << "\nNew: ");
1635 DEBUG(ResNode->dump(CurDAG));
1636 DEBUG(dbgs() << "\n");
1637
1638 ReplaceUses(User, ResNode);
1639 }
1640 }
1641
PeepholeCROps()1642 void PPCDAGToDAGISel::PeepholeCROps() {
1643 bool IsModified;
1644 do {
1645 IsModified = false;
1646 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1647 E = CurDAG->allnodes_end(); I != E; ++I) {
1648 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1649 if (!MachineNode || MachineNode->use_empty())
1650 continue;
1651 SDNode *ResNode = MachineNode;
1652
1653 bool Op1Set = false, Op1Unset = false,
1654 Op1Not = false,
1655 Op2Set = false, Op2Unset = false,
1656 Op2Not = false;
1657
1658 unsigned Opcode = MachineNode->getMachineOpcode();
1659 switch (Opcode) {
1660 default: break;
1661 case PPC::CRAND:
1662 case PPC::CRNAND:
1663 case PPC::CROR:
1664 case PPC::CRXOR:
1665 case PPC::CRNOR:
1666 case PPC::CREQV:
1667 case PPC::CRANDC:
1668 case PPC::CRORC: {
1669 SDValue Op = MachineNode->getOperand(1);
1670 if (Op.isMachineOpcode()) {
1671 if (Op.getMachineOpcode() == PPC::CRSET)
1672 Op2Set = true;
1673 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1674 Op2Unset = true;
1675 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1676 Op.getOperand(0) == Op.getOperand(1))
1677 Op2Not = true;
1678 }
1679 } // fallthrough
1680 case PPC::BC:
1681 case PPC::BCn:
1682 case PPC::SELECT_I4:
1683 case PPC::SELECT_I8:
1684 case PPC::SELECT_F4:
1685 case PPC::SELECT_F8:
1686 case PPC::SELECT_VRRC: {
1687 SDValue Op = MachineNode->getOperand(0);
1688 if (Op.isMachineOpcode()) {
1689 if (Op.getMachineOpcode() == PPC::CRSET)
1690 Op1Set = true;
1691 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1692 Op1Unset = true;
1693 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1694 Op.getOperand(0) == Op.getOperand(1))
1695 Op1Not = true;
1696 }
1697 }
1698 break;
1699 }
1700
1701 bool SelectSwap = false;
1702 switch (Opcode) {
1703 default: break;
1704 case PPC::CRAND:
1705 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1706 // x & x = x
1707 ResNode = MachineNode->getOperand(0).getNode();
1708 else if (Op1Set)
1709 // 1 & y = y
1710 ResNode = MachineNode->getOperand(1).getNode();
1711 else if (Op2Set)
1712 // x & 1 = x
1713 ResNode = MachineNode->getOperand(0).getNode();
1714 else if (Op1Unset || Op2Unset)
1715 // x & 0 = 0 & y = 0
1716 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1717 MVT::i1);
1718 else if (Op1Not)
1719 // ~x & y = andc(y, x)
1720 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1721 MVT::i1, MachineNode->getOperand(1),
1722 MachineNode->getOperand(0).
1723 getOperand(0));
1724 else if (Op2Not)
1725 // x & ~y = andc(x, y)
1726 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1727 MVT::i1, MachineNode->getOperand(0),
1728 MachineNode->getOperand(1).
1729 getOperand(0));
1730 else if (AllUsersSelectZero(MachineNode))
1731 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1732 MVT::i1, MachineNode->getOperand(0),
1733 MachineNode->getOperand(1)),
1734 SelectSwap = true;
1735 break;
1736 case PPC::CRNAND:
1737 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1738 // nand(x, x) -> nor(x, x)
1739 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1740 MVT::i1, MachineNode->getOperand(0),
1741 MachineNode->getOperand(0));
1742 else if (Op1Set)
1743 // nand(1, y) -> nor(y, y)
1744 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1745 MVT::i1, MachineNode->getOperand(1),
1746 MachineNode->getOperand(1));
1747 else if (Op2Set)
1748 // nand(x, 1) -> nor(x, x)
1749 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1750 MVT::i1, MachineNode->getOperand(0),
1751 MachineNode->getOperand(0));
1752 else if (Op1Unset || Op2Unset)
1753 // nand(x, 0) = nand(0, y) = 1
1754 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1755 MVT::i1);
1756 else if (Op1Not)
1757 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1758 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1759 MVT::i1, MachineNode->getOperand(0).
1760 getOperand(0),
1761 MachineNode->getOperand(1));
1762 else if (Op2Not)
1763 // nand(x, ~y) = ~x | y = orc(y, x)
1764 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1765 MVT::i1, MachineNode->getOperand(1).
1766 getOperand(0),
1767 MachineNode->getOperand(0));
1768 else if (AllUsersSelectZero(MachineNode))
1769 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1770 MVT::i1, MachineNode->getOperand(0),
1771 MachineNode->getOperand(1)),
1772 SelectSwap = true;
1773 break;
1774 case PPC::CROR:
1775 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1776 // x | x = x
1777 ResNode = MachineNode->getOperand(0).getNode();
1778 else if (Op1Set || Op2Set)
1779 // x | 1 = 1 | y = 1
1780 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1781 MVT::i1);
1782 else if (Op1Unset)
1783 // 0 | y = y
1784 ResNode = MachineNode->getOperand(1).getNode();
1785 else if (Op2Unset)
1786 // x | 0 = x
1787 ResNode = MachineNode->getOperand(0).getNode();
1788 else if (Op1Not)
1789 // ~x | y = orc(y, x)
1790 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1791 MVT::i1, MachineNode->getOperand(1),
1792 MachineNode->getOperand(0).
1793 getOperand(0));
1794 else if (Op2Not)
1795 // x | ~y = orc(x, y)
1796 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1797 MVT::i1, MachineNode->getOperand(0),
1798 MachineNode->getOperand(1).
1799 getOperand(0));
1800 else if (AllUsersSelectZero(MachineNode))
1801 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1802 MVT::i1, MachineNode->getOperand(0),
1803 MachineNode->getOperand(1)),
1804 SelectSwap = true;
1805 break;
1806 case PPC::CRXOR:
1807 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1808 // xor(x, x) = 0
1809 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1810 MVT::i1);
1811 else if (Op1Set)
1812 // xor(1, y) -> nor(y, y)
1813 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1814 MVT::i1, MachineNode->getOperand(1),
1815 MachineNode->getOperand(1));
1816 else if (Op2Set)
1817 // xor(x, 1) -> nor(x, x)
1818 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1819 MVT::i1, MachineNode->getOperand(0),
1820 MachineNode->getOperand(0));
1821 else if (Op1Unset)
1822 // xor(0, y) = y
1823 ResNode = MachineNode->getOperand(1).getNode();
1824 else if (Op2Unset)
1825 // xor(x, 0) = x
1826 ResNode = MachineNode->getOperand(0).getNode();
1827 else if (Op1Not)
1828 // xor(~x, y) = eqv(x, y)
1829 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1830 MVT::i1, MachineNode->getOperand(0).
1831 getOperand(0),
1832 MachineNode->getOperand(1));
1833 else if (Op2Not)
1834 // xor(x, ~y) = eqv(x, y)
1835 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1836 MVT::i1, MachineNode->getOperand(0),
1837 MachineNode->getOperand(1).
1838 getOperand(0));
1839 else if (AllUsersSelectZero(MachineNode))
1840 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1841 MVT::i1, MachineNode->getOperand(0),
1842 MachineNode->getOperand(1)),
1843 SelectSwap = true;
1844 break;
1845 case PPC::CRNOR:
1846 if (Op1Set || Op2Set)
1847 // nor(1, y) -> 0
1848 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1849 MVT::i1);
1850 else if (Op1Unset)
1851 // nor(0, y) = ~y -> nor(y, y)
1852 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1853 MVT::i1, MachineNode->getOperand(1),
1854 MachineNode->getOperand(1));
1855 else if (Op2Unset)
1856 // nor(x, 0) = ~x
1857 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1858 MVT::i1, MachineNode->getOperand(0),
1859 MachineNode->getOperand(0));
1860 else if (Op1Not)
1861 // nor(~x, y) = andc(x, y)
1862 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1863 MVT::i1, MachineNode->getOperand(0).
1864 getOperand(0),
1865 MachineNode->getOperand(1));
1866 else if (Op2Not)
1867 // nor(x, ~y) = andc(y, x)
1868 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1869 MVT::i1, MachineNode->getOperand(1).
1870 getOperand(0),
1871 MachineNode->getOperand(0));
1872 else if (AllUsersSelectZero(MachineNode))
1873 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1874 MVT::i1, MachineNode->getOperand(0),
1875 MachineNode->getOperand(1)),
1876 SelectSwap = true;
1877 break;
1878 case PPC::CREQV:
1879 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1880 // eqv(x, x) = 1
1881 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1882 MVT::i1);
1883 else if (Op1Set)
1884 // eqv(1, y) = y
1885 ResNode = MachineNode->getOperand(1).getNode();
1886 else if (Op2Set)
1887 // eqv(x, 1) = x
1888 ResNode = MachineNode->getOperand(0).getNode();
1889 else if (Op1Unset)
1890 // eqv(0, y) = ~y -> nor(y, y)
1891 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1892 MVT::i1, MachineNode->getOperand(1),
1893 MachineNode->getOperand(1));
1894 else if (Op2Unset)
1895 // eqv(x, 0) = ~x
1896 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1897 MVT::i1, MachineNode->getOperand(0),
1898 MachineNode->getOperand(0));
1899 else if (Op1Not)
1900 // eqv(~x, y) = xor(x, y)
1901 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1902 MVT::i1, MachineNode->getOperand(0).
1903 getOperand(0),
1904 MachineNode->getOperand(1));
1905 else if (Op2Not)
1906 // eqv(x, ~y) = xor(x, y)
1907 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1908 MVT::i1, MachineNode->getOperand(0),
1909 MachineNode->getOperand(1).
1910 getOperand(0));
1911 else if (AllUsersSelectZero(MachineNode))
1912 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1913 MVT::i1, MachineNode->getOperand(0),
1914 MachineNode->getOperand(1)),
1915 SelectSwap = true;
1916 break;
1917 case PPC::CRANDC:
1918 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1919 // andc(x, x) = 0
1920 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1921 MVT::i1);
1922 else if (Op1Set)
1923 // andc(1, y) = ~y
1924 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1925 MVT::i1, MachineNode->getOperand(1),
1926 MachineNode->getOperand(1));
1927 else if (Op1Unset || Op2Set)
1928 // andc(0, y) = andc(x, 1) = 0
1929 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1930 MVT::i1);
1931 else if (Op2Unset)
1932 // andc(x, 0) = x
1933 ResNode = MachineNode->getOperand(0).getNode();
1934 else if (Op1Not)
1935 // andc(~x, y) = ~(x | y) = nor(x, y)
1936 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1937 MVT::i1, MachineNode->getOperand(0).
1938 getOperand(0),
1939 MachineNode->getOperand(1));
1940 else if (Op2Not)
1941 // andc(x, ~y) = x & y
1942 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1943 MVT::i1, MachineNode->getOperand(0),
1944 MachineNode->getOperand(1).
1945 getOperand(0));
1946 else if (AllUsersSelectZero(MachineNode))
1947 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1948 MVT::i1, MachineNode->getOperand(1),
1949 MachineNode->getOperand(0)),
1950 SelectSwap = true;
1951 break;
1952 case PPC::CRORC:
1953 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1954 // orc(x, x) = 1
1955 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1956 MVT::i1);
1957 else if (Op1Set || Op2Unset)
1958 // orc(1, y) = orc(x, 0) = 1
1959 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1960 MVT::i1);
1961 else if (Op2Set)
1962 // orc(x, 1) = x
1963 ResNode = MachineNode->getOperand(0).getNode();
1964 else if (Op1Unset)
1965 // orc(0, y) = ~y
1966 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1967 MVT::i1, MachineNode->getOperand(1),
1968 MachineNode->getOperand(1));
1969 else if (Op1Not)
1970 // orc(~x, y) = ~(x & y) = nand(x, y)
1971 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1972 MVT::i1, MachineNode->getOperand(0).
1973 getOperand(0),
1974 MachineNode->getOperand(1));
1975 else if (Op2Not)
1976 // orc(x, ~y) = x | y
1977 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1978 MVT::i1, MachineNode->getOperand(0),
1979 MachineNode->getOperand(1).
1980 getOperand(0));
1981 else if (AllUsersSelectZero(MachineNode))
1982 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1983 MVT::i1, MachineNode->getOperand(1),
1984 MachineNode->getOperand(0)),
1985 SelectSwap = true;
1986 break;
1987 case PPC::SELECT_I4:
1988 case PPC::SELECT_I8:
1989 case PPC::SELECT_F4:
1990 case PPC::SELECT_F8:
1991 case PPC::SELECT_VRRC:
1992 if (Op1Set)
1993 ResNode = MachineNode->getOperand(1).getNode();
1994 else if (Op1Unset)
1995 ResNode = MachineNode->getOperand(2).getNode();
1996 else if (Op1Not)
1997 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
1998 SDLoc(MachineNode),
1999 MachineNode->getValueType(0),
2000 MachineNode->getOperand(0).
2001 getOperand(0),
2002 MachineNode->getOperand(2),
2003 MachineNode->getOperand(1));
2004 break;
2005 case PPC::BC:
2006 case PPC::BCn:
2007 if (Op1Not)
2008 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2009 PPC::BC,
2010 SDLoc(MachineNode),
2011 MVT::Other,
2012 MachineNode->getOperand(0).
2013 getOperand(0),
2014 MachineNode->getOperand(1),
2015 MachineNode->getOperand(2));
2016 // FIXME: Handle Op1Set, Op1Unset here too.
2017 break;
2018 }
2019
2020 // If we're inverting this node because it is used only by selects that
2021 // we'd like to swap, then swap the selects before the node replacement.
2022 if (SelectSwap)
2023 SwapAllSelectUsers(MachineNode);
2024
2025 if (ResNode != MachineNode) {
2026 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2027 DEBUG(MachineNode->dump(CurDAG));
2028 DEBUG(dbgs() << "\nNew: ");
2029 DEBUG(ResNode->dump(CurDAG));
2030 DEBUG(dbgs() << "\n");
2031
2032 ReplaceUses(MachineNode, ResNode);
2033 IsModified = true;
2034 }
2035 }
2036 if (IsModified)
2037 CurDAG->RemoveDeadNodes();
2038 } while (IsModified);
2039 }
2040
PeepholePPC64()2041 void PPCDAGToDAGISel::PeepholePPC64() {
2042 // These optimizations are currently supported only for 64-bit SVR4.
2043 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
2044 return;
2045
2046 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2047 ++Position;
2048
2049 while (Position != CurDAG->allnodes_begin()) {
2050 SDNode *N = --Position;
2051 // Skip dead nodes and any non-machine opcodes.
2052 if (N->use_empty() || !N->isMachineOpcode())
2053 continue;
2054
2055 unsigned FirstOp;
2056 unsigned StorageOpcode = N->getMachineOpcode();
2057
2058 switch (StorageOpcode) {
2059 default: continue;
2060
2061 case PPC::LBZ:
2062 case PPC::LBZ8:
2063 case PPC::LD:
2064 case PPC::LFD:
2065 case PPC::LFS:
2066 case PPC::LHA:
2067 case PPC::LHA8:
2068 case PPC::LHZ:
2069 case PPC::LHZ8:
2070 case PPC::LWA:
2071 case PPC::LWZ:
2072 case PPC::LWZ8:
2073 FirstOp = 0;
2074 break;
2075
2076 case PPC::STB:
2077 case PPC::STB8:
2078 case PPC::STD:
2079 case PPC::STFD:
2080 case PPC::STFS:
2081 case PPC::STH:
2082 case PPC::STH8:
2083 case PPC::STW:
2084 case PPC::STW8:
2085 FirstOp = 1;
2086 break;
2087 }
2088
2089 // If this is a load or store with a zero offset, we may be able to
2090 // fold an add-immediate into the memory operation.
2091 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2092 N->getConstantOperandVal(FirstOp) != 0)
2093 continue;
2094
2095 SDValue Base = N->getOperand(FirstOp + 1);
2096 if (!Base.isMachineOpcode())
2097 continue;
2098
2099 unsigned Flags = 0;
2100 bool ReplaceFlags = true;
2101
2102 // When the feeding operation is an add-immediate of some sort,
2103 // determine whether we need to add relocation information to the
2104 // target flags on the immediate operand when we fold it into the
2105 // load instruction.
2106 //
2107 // For something like ADDItocL, the relocation information is
2108 // inferred from the opcode; when we process it in the AsmPrinter,
2109 // we add the necessary relocation there. A load, though, can receive
2110 // relocation from various flavors of ADDIxxx, so we need to carry
2111 // the relocation information in the target flags.
2112 switch (Base.getMachineOpcode()) {
2113 default: continue;
2114
2115 case PPC::ADDI8:
2116 case PPC::ADDI:
2117 // In some cases (such as TLS) the relocation information
2118 // is already in place on the operand, so copying the operand
2119 // is sufficient.
2120 ReplaceFlags = false;
2121 // For these cases, the immediate may not be divisible by 4, in
2122 // which case the fold is illegal for DS-form instructions. (The
2123 // other cases provide aligned addresses and are always safe.)
2124 if ((StorageOpcode == PPC::LWA ||
2125 StorageOpcode == PPC::LD ||
2126 StorageOpcode == PPC::STD) &&
2127 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2128 Base.getConstantOperandVal(1) % 4 != 0))
2129 continue;
2130 break;
2131 case PPC::ADDIdtprelL:
2132 Flags = PPCII::MO_DTPREL_LO;
2133 break;
2134 case PPC::ADDItlsldL:
2135 Flags = PPCII::MO_TLSLD_LO;
2136 break;
2137 case PPC::ADDItocL:
2138 Flags = PPCII::MO_TOC_LO;
2139 break;
2140 }
2141
2142 // We found an opportunity. Reverse the operands from the add
2143 // immediate and substitute them into the load or store. If
2144 // needed, update the target flags for the immediate operand to
2145 // reflect the necessary relocation information.
2146 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2147 DEBUG(Base->dump(CurDAG));
2148 DEBUG(dbgs() << "\nN: ");
2149 DEBUG(N->dump(CurDAG));
2150 DEBUG(dbgs() << "\n");
2151
2152 SDValue ImmOpnd = Base.getOperand(1);
2153
2154 // If the relocation information isn't already present on the
2155 // immediate operand, add it now.
2156 if (ReplaceFlags) {
2157 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
2158 SDLoc dl(GA);
2159 const GlobalValue *GV = GA->getGlobal();
2160 // We can't perform this optimization for data whose alignment
2161 // is insufficient for the instruction encoding.
2162 if (GV->getAlignment() < 4 &&
2163 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2164 StorageOpcode == PPC::LWA)) {
2165 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2166 continue;
2167 }
2168 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
2169 } else if (ConstantPoolSDNode *CP =
2170 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
2171 const Constant *C = CP->getConstVal();
2172 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2173 CP->getAlignment(),
2174 0, Flags);
2175 }
2176 }
2177
2178 if (FirstOp == 1) // Store
2179 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2180 Base.getOperand(0), N->getOperand(3));
2181 else // Load
2182 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2183 N->getOperand(2));
2184
2185 // The add-immediate may now be dead, in which case remove it.
2186 if (Base.getNode()->use_empty())
2187 CurDAG->RemoveDeadNode(Base.getNode());
2188 }
2189 }
2190
2191
2192 /// createPPCISelDag - This pass converts a legalized DAG into a
2193 /// PowerPC-specific DAG, ready for instruction scheduling.
2194 ///
createPPCISelDag(PPCTargetMachine & TM)2195 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
2196 return new PPCDAGToDAGISel(TM);
2197 }
2198
initializePassOnce(PassRegistry & Registry)2199 static void initializePassOnce(PassRegistry &Registry) {
2200 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
2201 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
2202 nullptr, false, false);
2203 Registry.registerPass(*PI, true);
2204 }
2205
initializePPCDAGToDAGISelPass(PassRegistry & Registry)2206 void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2207 CALL_ONCE_INITIALIZATION(initializePassOnce);
2208 }
2209
2210