Nate Begeman | 6cca84e | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 1 | //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===// |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Nate Begeman | 6cca84e | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 10 | // This file defines a pattern matching instruction selector for PowerPC, |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 11 | // converting from a legalized dag to a PPC dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | bfca1ab | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 15 | #include "PPC.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/PPCPredicates.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "PPCTargetMachine.h" |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAG.h" |
| 22 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/Function.h" |
Chandler Carruth | 1fe21fc | 2013-01-19 08:03:47 +0000 | [diff] [blame] | 25 | #include "llvm/IR/GlobalAlias.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/GlobalValue.h" |
| 27 | #include "llvm/IR/GlobalVariable.h" |
| 28 | #include "llvm/IR/Intrinsics.h" |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "ppc-codegen" |
| 38 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 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 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 43 | namespace llvm { |
| 44 | void initializePPCDAGToDAGISelPass(PassRegistry&); |
| 45 | } |
| 46 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 47 | namespace { |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 48 | //===--------------------------------------------------------------------===// |
Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 49 | /// PPCDAGToDAGISel - PPC specific code to select PPC machine |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 50 | /// instructions for SelectionDAG operations. |
| 51 | /// |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 52 | class PPCDAGToDAGISel : public SelectionDAGISel { |
Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 53 | const PPCTargetMachine &TM; |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 54 | const PPCTargetLowering *PPCLowering; |
| 55 | const PPCSubtarget *PPCSubTarget; |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 56 | unsigned GlobalBaseReg; |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 57 | public: |
Dan Gohman | 56e3f63 | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 58 | explicit PPCDAGToDAGISel(PPCTargetMachine &tm) |
Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 59 | : SelectionDAGISel(tm), TM(tm), |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 60 | PPCLowering(TM.getTargetLowering()), |
| 61 | PPCSubTarget(TM.getSubtargetImpl()) { |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 62 | initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry()); |
| 63 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 64 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 65 | bool runOnMachineFunction(MachineFunction &MF) override { |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 66 | // Make sure we re-emit a set of the global base reg if necessary |
| 67 | GlobalBaseReg = 0; |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 68 | PPCLowering = TM.getTargetLowering(); |
| 69 | PPCSubTarget = TM.getSubtargetImpl(); |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 70 | SelectionDAGISel::runOnMachineFunction(MF); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 71 | |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 72 | if (!PPCSubTarget->isSVR4ABI()) |
Bill Schmidt | 38d9458 | 2012-10-10 20:54:15 +0000 | [diff] [blame] | 73 | InsertVRSaveCode(MF); |
| 74 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 75 | return true; |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 76 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 77 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 78 | void PostprocessISelDAG() override; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 80 | /// getI32Imm - Return a target constant with the specified value, of type |
| 81 | /// i32. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 82 | inline SDValue getI32Imm(unsigned Imm) { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 83 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 86 | /// getI64Imm - Return a target constant with the specified value, of type |
| 87 | /// i64. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 88 | inline SDValue getI64Imm(uint64_t Imm) { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 89 | return CurDAG->getTargetConstant(Imm, MVT::i64); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 90 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 92 | /// getSmallIPtrImm - Return a target constant of pointer type. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 93 | inline SDValue getSmallIPtrImm(unsigned Imm) { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 94 | return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy()); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 95 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 96 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 97 | /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 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. |
Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 106 | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask, |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 107 | unsigned &SH, unsigned &MB, unsigned &ME); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 109 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 110 | /// base register. Return the virtual register that holds this value. |
Evan Cheng | 61413a3 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 111 | SDNode *getGlobalBaseReg(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 113 | // Select - Convert the specified operand from a target-independent to a |
| 114 | // target-specific node if it hasn't already been changed. |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 115 | SDNode *Select(SDNode *N) override; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 116 | |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 117 | SDNode *SelectBitfieldInsert(SDNode *N); |
| 118 | |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 119 | /// SelectCC - Select a comparison of the specified values with the |
| 120 | /// specified condition code, returning the CR# of the expression. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 121 | SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl); |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 122 | |
Nate Begeman | 8e6a8af | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 123 | /// SelectAddrImm - Returns true if the address N can be represented by |
| 124 | /// a base register plus a signed 16-bit displacement [r+imm]. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 125 | bool SelectAddrImm(SDValue N, SDValue &Disp, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 126 | SDValue &Base) { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 127 | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 128 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 6f5840c | 2006-11-16 00:41:37 +0000 | [diff] [blame] | 130 | /// SelectAddrImmOffs - Return true if the operand is valid for a preinc |
Ulrich Weigand | d1b99d3 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 131 | /// immediate field. Note that the operand at this point is already the |
| 132 | /// result of a prior SelectAddressRegImm call. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 133 | bool SelectAddrImmOffs(SDValue N, SDValue &Out) const { |
Ulrich Weigand | d1b99d3 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 134 | if (N.getOpcode() == ISD::TargetConstant || |
Hal Finkel | a86b0f2 | 2012-06-21 20:10:48 +0000 | [diff] [blame] | 135 | N.getOpcode() == ISD::TargetGlobalAddress) { |
Hal Finkel | 1cc27e4 | 2012-06-19 02:34:32 +0000 | [diff] [blame] | 136 | Out = N; |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
Nate Begeman | 8e6a8af | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 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. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 146 | bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 147 | return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 148 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 149 | |
Nate Begeman | 8e6a8af | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 150 | /// SelectAddrIdxOnly - Given the specified addressed, force it to be |
| 151 | /// represented as an indexed [r+r] operation. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 152 | bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 153 | return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | c5292ec | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 155 | |
Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 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. |
| 159 | bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 160 | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 161 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 162 | |
Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 163 | // Select an address into a single register. |
| 164 | bool SelectAddr(SDValue N, SDValue &Base) { |
| 165 | Base = N; |
| 166 | return true; |
| 167 | } |
| 168 | |
Chris Lattner | a1ec1dd | 2006-02-24 02:13:12 +0000 | [diff] [blame] | 169 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
Dale Johannesen | 4a50e68 | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 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. |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 174 | bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
| 175 | char ConstraintCode, |
| 176 | std::vector<SDValue> &OutOps) override { |
Dale Johannesen | 4a50e68 | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 177 | OutOps.push_back(Op); |
Chris Lattner | a1ec1dd | 2006-02-24 02:13:12 +0000 | [diff] [blame] | 178 | return false; |
| 179 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 180 | |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 181 | void InsertVRSaveCode(MachineFunction &MF); |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 182 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 183 | const char *getPassName() const override { |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 184 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | 03e08ee | 2005-09-13 22:03:06 +0000 | [diff] [blame] | 187 | // Include the pieces autogenerated from the target description. |
Chris Lattner | 0921e3b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 188 | #include "PPCGenDAGISel.inc" |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 259e6c7 | 2005-10-06 18:45:51 +0000 | [diff] [blame] | 190 | private: |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 191 | SDNode *SelectSETCC(SDNode *N); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 192 | |
| 193 | void PeepholePPC64(); |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 194 | void PeepholeCROps(); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 195 | |
| 196 | bool AllUsersSelectZero(SDNode *N); |
| 197 | void SwapAllSelectUsers(SDNode *N); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 198 | }; |
| 199 | } |
| 200 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 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. |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 204 | void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) { |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 205 | // Check to see if this function uses vector registers, which means we have to |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 206 | // save and restore the VRSAVE register and update it with the regs we use. |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 207 | // |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 208 | // In this case, there will be virtual registers of vector type created |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 209 | // by the scheduler. Detect them now. |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 210 | bool HasVectorVReg = false; |
Jakob Stoklund Olesen | 4a7b48d | 2011-01-08 23:11:11 +0000 | [diff] [blame] | 211 | for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) { |
| 212 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 213 | if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) { |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 214 | HasVectorVReg = true; |
| 215 | break; |
| 216 | } |
Jakob Stoklund Olesen | 4a7b48d | 2011-01-08 23:11:11 +0000 | [diff] [blame] | 217 | } |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 218 | if (!HasVectorVReg) return; // nothing to do. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 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. |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 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. |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 232 | unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
| 233 | unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 234 | |
Evan Cheng | 20350c4 | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 235 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 236 | MachineBasicBlock &EntryBB = *Fn.begin(); |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 237 | DebugLoc dl; |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 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 |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 243 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE); |
| 244 | BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE), |
Chris Lattner | a98c679 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 245 | UpdatedVRSAVE).addReg(InVRSAVE); |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 246 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 248 | // Find all return blocks, outputting a restore in each epilog. |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 249 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 250 | if (!BB->empty() && BB->back().isReturn()) { |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 251 | IP = BB->end(); --IP; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 253 | // Skip over all terminator instructions, which are part of the return |
| 254 | // sequence. |
| 255 | MachineBasicBlock::iterator I2 = IP; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 256 | while (I2 != BB->begin() && (--I2)->isTerminator()) |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 257 | IP = I2; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 259 | // Emit: MTVRSAVE InVRSave |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 260 | BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | 259e6c7 | 2005-10-06 18:45:51 +0000 | [diff] [blame] | 263 | } |
Chris Lattner | 8ae9525 | 2005-09-03 01:17:22 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 266 | /// getGlobalBaseReg - Output the instructions required to put the |
| 267 | /// base address to use for accessing globals into a register. |
| 268 | /// |
Evan Cheng | 61413a3 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 269 | SDNode *PPCDAGToDAGISel::getGlobalBaseReg() { |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 270 | if (!GlobalBaseReg) { |
Evan Cheng | 20350c4 | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 271 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 272 | // Insert the set of GlobalBaseReg into the first MBB of the function |
Dan Gohman | fca8968 | 2009-08-15 02:07:36 +0000 | [diff] [blame] | 273 | MachineBasicBlock &FirstMBB = MF->front(); |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 274 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 275 | DebugLoc dl; |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 276 | |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 277 | if (PPCLowering->getPointerTy() == MVT::i32) { |
Hal Finkel | 6daf2aa | 2014-03-06 01:28:23 +0000 | [diff] [blame] | 278 | GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass); |
Cameron Zwarich | dadd733 | 2011-05-19 02:56:28 +0000 | [diff] [blame] | 279 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 280 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
Chris Lattner | b542925 | 2006-11-14 18:43:11 +0000 | [diff] [blame] | 281 | } else { |
Hal Finkel | 6daf2aa | 2014-03-06 01:28:23 +0000 | [diff] [blame] | 282 | GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass); |
Cameron Zwarich | dadd733 | 2011-05-19 02:56:28 +0000 | [diff] [blame] | 283 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8)); |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 284 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg); |
Chris Lattner | b542925 | 2006-11-14 18:43:11 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 286 | } |
Gabor Greif | 81d6a38 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 287 | return CurDAG->getRegister(GlobalBaseReg, |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 288 | PPCLowering->getPointerTy()).getNode(); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 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. |
| 295 | static bool isIntS16Immediate(SDNode *N, short &Imm) { |
| 296 | if (N->getOpcode() != ISD::Constant) |
| 297 | return false; |
| 298 | |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 299 | Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 300 | if (N->getValueType(0) == MVT::i32) |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 301 | return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 302 | else |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 303 | return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 306 | static bool isIntS16Immediate(SDValue Op, short &Imm) { |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 307 | return isIntS16Immediate(Op.getNode(), Imm); |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 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. |
| 313 | static bool isInt32Immediate(SDNode *N, unsigned &Imm) { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 314 | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 315 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 316 | return true; |
| 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 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. |
| 323 | static bool isInt64Immediate(SDNode *N, uint64_t &Imm) { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 324 | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) { |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 325 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 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. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 333 | static bool isInt32Immediate(SDValue N, unsigned &Imm) { |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 334 | return isInt32Immediate(N.getNode(), Imm); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 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. |
| 341 | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
Gabor Greif | 81d6a38 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 342 | return N->getOpcode() == Opc |
| 343 | && isInt32Immediate(N->getOperand(1).getNode(), Imm); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 346 | bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
Hal Finkel | ff3ea80 | 2013-07-11 16:31:51 +0000 | [diff] [blame] | 347 | if (!Val) |
| 348 | return false; |
| 349 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 350 | if (isShiftedMask_32(Val)) { |
| 351 | // look for the first non-zero bit |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 352 | MB = countLeadingZeros(Val); |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 353 | // look for the first zero bit after the run of ones |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 354 | ME = countLeadingZeros((Val - 1) ^ Val); |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 355 | return true; |
Chris Lattner | 666512c | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 356 | } else { |
| 357 | Val = ~Val; // invert mask |
| 358 | if (isShiftedMask_32(Val)) { |
| 359 | // effectively look for the first zero bit |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 360 | ME = countLeadingZeros(Val) - 1; |
Chris Lattner | 666512c | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 361 | // effectively look for the first one bit after the run of zeros |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 362 | MB = countLeadingZeros((Val - 1) ^ Val) + 1; |
Chris Lattner | 666512c | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 363 | return true; |
| 364 | } |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 365 | } |
| 366 | // no run present |
| 367 | return false; |
| 368 | } |
| 369 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 370 | bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, |
| 371 | bool isShiftMask, unsigned &SH, |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 372 | unsigned &MB, unsigned &ME) { |
Nate Begeman | 92e7750 | 2005-10-19 00:05:37 +0000 | [diff] [blame] | 373 | // Don't even go down this path for i64, since different logic will be |
| 374 | // necessary for rldicl/rldicr/rldimi. |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 375 | if (N->getValueType(0) != MVT::i32) |
Nate Begeman | 92e7750 | 2005-10-19 00:05:37 +0000 | [diff] [blame] | 376 | return false; |
| 377 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 378 | unsigned Shift = 32; |
| 379 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 380 | unsigned Opcode = N->getOpcode(); |
Chris Lattner | e413b60 | 2005-08-30 00:59:16 +0000 | [diff] [blame] | 381 | if (N->getNumOperands() != 2 || |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 382 | !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 383 | return false; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 384 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 385 | if (Opcode == ISD::SHL) { |
| 386 | // apply shift left to mask if it comes first |
Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 387 | if (isShiftMask) Mask = Mask << Shift; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 388 | // determine which bits are made indeterminant by shift |
| 389 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 390 | } else if (Opcode == ISD::SRL) { |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 391 | // apply shift right to mask if it comes first |
Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 392 | if (isShiftMask) Mask = Mask >> Shift; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 393 | // determine which bits are made indeterminant by shift |
| 394 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 395 | // adjust for the left rotate |
| 396 | Shift = 32 - Shift; |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 397 | } else if (Opcode == ISD::ROTL) { |
| 398 | Indeterminant = 0; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 399 | } else { |
| 400 | return false; |
| 401 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 402 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 403 | // if the mask doesn't intersect any Indeterminant bits |
| 404 | if (Mask && !(Mask & Indeterminant)) { |
Chris Lattner | a296339 | 2006-05-12 16:29:37 +0000 | [diff] [blame] | 405 | SH = Shift & 31; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 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 | |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 412 | /// SelectBitfieldInsert - turn an or of two masked values into |
| 413 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 414 | SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 415 | SDValue Op0 = N->getOperand(0); |
| 416 | SDValue Op1 = N->getOperand(1); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 417 | SDLoc dl(N); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 418 | |
Dan Gohman | f19609a | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 419 | APInt LKZ, LKO, RKZ, RKO; |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 420 | CurDAG->computeKnownBits(Op0, LKZ, LKO); |
| 421 | CurDAG->computeKnownBits(Op1, RKZ, RKO); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 422 | |
Dan Gohman | f19609a | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 423 | unsigned TargetMask = LKZ.getZExtValue(); |
| 424 | unsigned InsertMask = RKZ.getZExtValue(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 425 | |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 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; |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 432 | |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 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. |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 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); |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 442 | std::swap(TargetMask, InsertMask); |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 443 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 444 | } |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 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 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 452 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 453 | |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 454 | unsigned MB, ME; |
Hal Finkel | ff3ea80 | 2013-07-11 16:31:51 +0000 | [diff] [blame] | 455 | if (isRunOfOnes(InsertMask, MB, ME)) { |
Dale Johannesen | 8495a50 | 2009-11-20 22:16:40 +0000 | [diff] [blame] | 456 | SDValue Tmp1, Tmp2; |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 457 | |
| 458 | if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) && |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 459 | isInt32Immediate(Op1.getOperand(1), Value)) { |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 460 | Op1 = Op1.getOperand(0); |
| 461 | SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value; |
| 462 | } |
| 463 | if (Op1Opc == ISD::AND) { |
Hal Finkel | d9963c7 | 2014-04-13 17:10:58 +0000 | [diff] [blame] | 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; |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 468 | CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO); |
Hal Finkel | d9963c7 | 2014-04-13 17:10:58 +0000 | [diff] [blame] | 469 | bool CanFoldMask = InsertMask == MKO.getZExtValue(); |
| 470 | |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 471 | unsigned SHOpc = Op1.getOperand(0).getOpcode(); |
Hal Finkel | d9963c7 | 2014-04-13 17:10:58 +0000 | [diff] [blame] | 472 | if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask && |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 473 | isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) { |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 474 | // Note that Value must be in range here (less than 32) because |
| 475 | // otherwise there would not be any bits set in InsertMask. |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 476 | Op1 = Op1.getOperand(0).getOperand(0); |
| 477 | SH = (SHOpc == ISD::SHL) ? Value : 32 - Value; |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
Dale Johannesen | 8495a50 | 2009-11-20 22:16:40 +0000 | [diff] [blame] | 480 | |
Chris Lattner | a296339 | 2006-05-12 16:29:37 +0000 | [diff] [blame] | 481 | SH &= 31; |
Dale Johannesen | 8495a50 | 2009-11-20 22:16:40 +0000 | [diff] [blame] | 482 | SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB), |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 483 | getI32Imm(ME) }; |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 484 | return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops); |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 485 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 486 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 487 | return nullptr; |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 490 | /// SelectCC - Select a comparison of the specified values with the specified |
| 491 | /// condition code, returning the CR# of the expression. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 492 | SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 493 | ISD::CondCode CC, SDLoc dl) { |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 494 | // Always select the LHS. |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 495 | unsigned Opc; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 496 | |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 497 | if (LHS.getValueType() == MVT::i32) { |
Chris Lattner | 9a40cca | 2006-06-27 00:10:13 +0000 | [diff] [blame] | 498 | unsigned Imm; |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 499 | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
| 500 | if (isInt32Immediate(RHS, Imm)) { |
| 501 | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 502 | if (isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 503 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
| 504 | getI32Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 505 | // If this is a 16-bit signed immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 506 | if (isInt<16>((int)Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 507 | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
| 508 | getI32Imm(Imm & 0xFFFF)), 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 509 | |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 510 | // For non-equality comparisons, the default code would materialize the |
| 511 | // constant, then compare against it, like this: |
| 512 | // lis r2, 4660 |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 513 | // ori r2, r2, 22136 |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 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 |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 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); |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 523 | } |
| 524 | Opc = PPC::CMPLW; |
| 525 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 526 | if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 527 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
| 528 | getI32Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 529 | Opc = PPC::CMPLW; |
| 530 | } else { |
| 531 | short SImm; |
| 532 | if (isIntS16Immediate(RHS, SImm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 533 | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
| 534 | getI32Imm((int)SImm & 0xFFFF)), |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 535 | 0); |
| 536 | Opc = PPC::CMPW; |
| 537 | } |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 538 | } else if (LHS.getValueType() == MVT::i64) { |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 539 | uint64_t Imm; |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 540 | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 541 | if (isInt64Immediate(RHS.getNode(), Imm)) { |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 542 | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 543 | if (isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 544 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
| 545 | getI32Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 546 | // If this is a 16-bit signed immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 547 | if (isInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 548 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
| 549 | getI32Imm(Imm & 0xFFFF)), 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 550 | |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 551 | // For non-equality comparisons, the default code would materialize the |
| 552 | // constant, then compare against it, like this: |
| 553 | // lis r2, 4660 |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 554 | // ori r2, r2, 22136 |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 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 |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 560 | if (isUInt<32>(Imm)) { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 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); |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | Opc = PPC::CMPLD; |
| 568 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 569 | if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 570 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
| 571 | getI64Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 572 | Opc = PPC::CMPLD; |
| 573 | } else { |
| 574 | short SImm; |
| 575 | if (isIntS16Immediate(RHS, SImm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 576 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
| 577 | getI64Imm(SImm & 0xFFFF)), |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 578 | 0); |
| 579 | Opc = PPC::CMPD; |
| 580 | } |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 581 | } else if (LHS.getValueType() == MVT::f32) { |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 582 | Opc = PPC::FCMPUS; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 583 | } else { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 584 | assert(LHS.getValueType() == MVT::f64 && "Unknown vt!"); |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 585 | Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 586 | } |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 587 | return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0); |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 590 | static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) { |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 591 | switch (CC) { |
Chris Lattner | 630bbce | 2006-05-25 16:54:16 +0000 | [diff] [blame] | 592 | case ISD::SETUEQ: |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 593 | case ISD::SETONE: |
| 594 | case ISD::SETOLE: |
| 595 | case ISD::SETOGE: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 596 | llvm_unreachable("Should be lowered by legalize!"); |
| 597 | default: llvm_unreachable("Unknown condition!"); |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 598 | case ISD::SETOEQ: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 599 | case ISD::SETEQ: return PPC::PRED_EQ; |
Chris Lattner | 630bbce | 2006-05-25 16:54:16 +0000 | [diff] [blame] | 600 | case ISD::SETUNE: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 601 | case ISD::SETNE: return PPC::PRED_NE; |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 602 | case ISD::SETOLT: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 603 | case ISD::SETLT: return PPC::PRED_LT; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 604 | case ISD::SETULE: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 605 | case ISD::SETLE: return PPC::PRED_LE; |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 606 | case ISD::SETOGT: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 607 | case ISD::SETGT: return PPC::PRED_GT; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 608 | case ISD::SETUGE: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 609 | case ISD::SETGE: return PPC::PRED_GE; |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 610 | case ISD::SETO: return PPC::PRED_NU; |
| 611 | case ISD::SETUO: return PPC::PRED_UN; |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 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; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 615 | } |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 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. |
Ulrich Weigand | 47e9328 | 2013-07-03 15:13:30 +0000 | [diff] [blame] | 621 | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) { |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 622 | Invert = false; |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 623 | switch (CC) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 624 | default: llvm_unreachable("Unknown condition!"); |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 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 |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 632 | case ISD::SETUGE: |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 633 | case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 634 | case ISD::SETULE: |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 635 | case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE |
Chris Lattner | 1fbb0d3 | 2006-05-25 18:06:16 +0000 | [diff] [blame] | 636 | case ISD::SETUNE: |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 637 | case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE |
| 638 | case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 639 | case ISD::SETUEQ: |
| 640 | case ISD::SETOGE: |
| 641 | case ISD::SETOLE: |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 642 | case ISD::SETONE: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 643 | llvm_unreachable("Invalid branch code: should be expanded by legalize"); |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 644 | // These are invalid for floating point. Assume integer. |
| 645 | case ISD::SETULT: return 0; |
| 646 | case ISD::SETUGT: return 1; |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 647 | } |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | c5292ec | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 649 | |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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). |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 653 | static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC, |
| 654 | bool HasVSX) { |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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) |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 668 | return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP; |
| 669 | else if (VecVT == MVT::v2f64) |
| 670 | return PPC::XVCMPEQDP; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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) |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 683 | return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP; |
| 684 | else if (VecVT == MVT::v2f64) |
| 685 | return PPC::XVCMPGTDP; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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) |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 700 | return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP; |
| 701 | else if (VecVT == MVT::v2f64) |
| 702 | return PPC::XVCMPEQDP; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 703 | break; |
| 704 | case ISD::SETOLT: |
| 705 | case ISD::SETOGT: |
| 706 | case ISD::SETOLE: |
| 707 | if (VecVT == MVT::v4f32) |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 708 | return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP; |
| 709 | else if (VecVT == MVT::v2f64) |
| 710 | return PPC::XVCMPGTDP; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 711 | break; |
| 712 | case ISD::SETOGE: |
| 713 | if (VecVT == MVT::v4f32) |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 714 | return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP; |
| 715 | else if (VecVT == MVT::v2f64) |
| 716 | return PPC::XVCMPGEDP; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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). |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 727 | static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT, bool HasVSX) { |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 736 | return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP; |
| 737 | case MVT::v2f64: |
| 738 | return PPC::XVCMPEQDP; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 739 | default: |
| 740 | llvm_unreachable("Invalid integer vector compare condition"); |
| 741 | } |
| 742 | } |
| 743 | |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 744 | SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 745 | SDLoc dl(N); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 746 | unsigned Imm; |
| 747 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 748 | EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy(); |
| 749 | bool isPPC64 = (PtrVT == MVT::i64); |
| 750 | |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 751 | if (!PPCSubTarget->useCRBits() && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 752 | isInt32Immediate(N->getOperand(1), Imm)) { |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 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) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 757 | SDValue Op = N->getOperand(0); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 758 | switch (CC) { |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 759 | default: break; |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 760 | case ISD::SETEQ: { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 761 | Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 762 | SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 763 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 764 | } |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 765 | case ISD::SETNE: { |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 766 | if (isPPC64) break; |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 767 | SDValue AD = |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 768 | SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 769 | Op, getI32Imm(~0U)), 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 770 | return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, |
Evan Cheng | 34b70ee | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 771 | AD.getValue(1)); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 772 | } |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 773 | case ISD::SETLT: { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 774 | SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 775 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 776 | } |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 777 | case ISD::SETGT: { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 778 | SDValue T = |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 779 | SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0); |
| 780 | T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 781 | SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 782 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 785 | } else if (Imm == ~0U) { // setcc op, -1 |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 786 | SDValue Op = N->getOperand(0); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 787 | switch (CC) { |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 788 | default: break; |
| 789 | case ISD::SETEQ: |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 790 | if (isPPC64) break; |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 791 | Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 792 | Op, getI32Imm(1)), 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 793 | return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
| 794 | SDValue(CurDAG->getMachineNode(PPC::LI, dl, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 795 | MVT::i32, |
| 796 | getI32Imm(0)), 0), |
Dale Johannesen | f08a47b | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 797 | Op.getValue(1)); |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 798 | case ISD::SETNE: { |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 799 | if (isPPC64) break; |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 800 | Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0); |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 801 | SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 802 | Op, getI32Imm(~0U)); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 803 | return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 804 | Op, SDValue(AD, 1)); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 805 | } |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 806 | case ISD::SETLT: { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 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); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 811 | SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 812 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 813 | } |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 814 | case ISD::SETGT: { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 815 | SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 816 | Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), |
Dale Johannesen | f08a47b | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 817 | 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 818 | return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, |
Evan Cheng | 34b70ee | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 819 | getI32Imm(1)); |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 820 | } |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 821 | } |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 824 | |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 825 | SDValue LHS = N->getOperand(0); |
| 826 | SDValue RHS = N->getOperand(1); |
| 827 | |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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. |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 830 | if (LHS.getValueType().isVector()) { |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 831 | EVT VecVT = LHS.getValueType(); |
| 832 | MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy; |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 833 | unsigned int VCmpInst = getVCmpInst(VT, CC, PPCSubTarget->hasVSX()); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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); |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 844 | return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR : |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 845 | PPC::VNOR, |
| 846 | VecVT, VCmp, VCmp); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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); |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 866 | unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget->hasVSX()); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 867 | SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0); |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 868 | return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLOR : |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 869 | PPC::VOR, |
| 870 | VecVT, VCmpGT, VCmpEQ); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 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); |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 877 | unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget->hasVSX()); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 878 | SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0); |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 879 | return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLOR : |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 880 | PPC::VOR, |
| 881 | VecVT, VCmpLE, VCmpEQ); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 882 | } |
| 883 | default: |
| 884 | llvm_unreachable("Invalid vector compare type: should be expanded by legalize"); |
| 885 | } |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 888 | if (PPCSubTarget->useCRBits()) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 889 | return nullptr; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 891 | bool Inv; |
Ulrich Weigand | 47e9328 | 2013-07-03 15:13:30 +0000 | [diff] [blame] | 892 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 893 | SDValue CCReg = SelectCC(LHS, RHS, CC, dl); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 894 | SDValue IntCR; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 896 | // Force the ccreg into CR7. |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 897 | SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 898 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 899 | SDValue InFlag(nullptr, 0); // Null incoming flag value. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 900 | CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, |
Chris Lattner | bd09910 | 2005-12-01 03:50:19 +0000 | [diff] [blame] | 901 | InFlag).getValue(1); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 902 | |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 903 | IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg, |
| 904 | CCReg), 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 905 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 906 | SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31), |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 907 | getI32Imm(31), getI32Imm(31) }; |
Ulrich Weigand | 47e9328 | 2013-07-03 15:13:30 +0000 | [diff] [blame] | 908 | if (!Inv) |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 909 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 910 | |
| 911 | // Get the specified bit. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 912 | SDValue Tmp = |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 913 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
Ulrich Weigand | 47e9328 | 2013-07-03 15:13:30 +0000 | [diff] [blame] | 914 | return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 915 | } |
Chris Lattner | 502a369 | 2005-10-06 18:56:10 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 318622f | 2005-10-06 19:07:45 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 918 | // Select - Convert the specified operand from a target-independent to a |
| 919 | // target-specific node if it hasn't already been changed. |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 920 | SDNode *PPCDAGToDAGISel::Select(SDNode *N) { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 921 | SDLoc dl(N); |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 922 | if (N->isMachineOpcode()) { |
| 923 | N->setNodeId(-1); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 924 | return nullptr; // Already selected. |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 925 | } |
Chris Lattner | 08c319f | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 926 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 927 | switch (N->getOpcode()) { |
Chris Lattner | 498915d | 2005-09-07 23:45:15 +0000 | [diff] [blame] | 928 | default: break; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 929 | |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 930 | case ISD::Constant: { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 931 | if (N->getValueType(0) == MVT::i64) { |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 932 | // Get 64 bit value. |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 933 | int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 934 | // Assume no remaining bits. |
| 935 | unsigned Remainder = 0; |
| 936 | // Assume no shift required. |
| 937 | unsigned Shift = 0; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 938 | |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 939 | // If it can't be represented as a 32 bit value. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 940 | if (!isInt<32>(Imm)) { |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 941 | Shift = countTrailingZeros<uint64_t>(Imm); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 942 | int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 943 | |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 944 | // If the shifted value fits 32 bits. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 945 | if (isInt<32>(ImmSh)) { |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 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 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 955 | |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 956 | // Intermediate operand. |
| 957 | SDNode *Result; |
| 958 | |
| 959 | // Handle first 32 bits. |
| 960 | unsigned Lo = Imm & 0xFFFF; |
| 961 | unsigned Hi = (Imm >> 16) & 0xFFFF; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 962 | |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 963 | // Simple value. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 964 | if (isInt<16>(Imm)) { |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 965 | // Just the Lo bits. |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 966 | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo)); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 967 | } else if (Lo) { |
| 968 | // Handle the Hi bits. |
| 969 | unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8; |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 970 | Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi)); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 971 | // And Lo bits. |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 972 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
| 973 | SDValue(Result, 0), getI32Imm(Lo)); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 974 | } else { |
| 975 | // Just the Hi bits. |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 976 | Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi)); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 977 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 978 | |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 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) { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 984 | Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, |
| 985 | SDValue(Result, 0), |
| 986 | getI32Imm(Shift), |
| 987 | getI32Imm(63 - Shift)); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | // Add in the last bits as required. |
| 991 | if ((Hi = (Remainder >> 16) & 0xFFFF)) { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 992 | Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64, |
| 993 | SDValue(Result, 0), getI32Imm(Hi)); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 994 | } |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 995 | if ((Lo = Remainder & 0xFFFF)) { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 996 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
| 997 | SDValue(Result, 0), getI32Imm(Lo)); |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 998 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 999 | |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 1000 | return Result; |
| 1001 | } |
| 1002 | break; |
| 1003 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1004 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1005 | case ISD::SETCC: { |
| 1006 | SDNode *SN = SelectSETCC(N); |
| 1007 | if (SN) |
| 1008 | return SN; |
| 1009 | break; |
| 1010 | } |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1011 | case PPCISD::GlobalBaseReg: |
Evan Cheng | 61413a3 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1012 | return getGlobalBaseReg(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | e4c338d | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 1014 | case ISD::FrameIndex: { |
| 1015 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1016 | SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0)); |
| 1017 | unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8; |
Chris Lattner | bc485fd | 2006-08-15 23:48:22 +0000 | [diff] [blame] | 1018 | if (N->hasOneUse()) |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1019 | return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI, |
Evan Cheng | 34b70ee | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 1020 | getSmallIPtrImm(0)); |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1021 | return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1022 | getSmallIPtrImm(0)); |
Chris Lattner | e4c338d | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 1023 | } |
Chris Lattner | 6961fc7 | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 1024 | |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 1025 | case PPCISD::MFOCRF: { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1026 | SDValue InFlag = N->getOperand(1); |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 1027 | return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, |
| 1028 | N->getOperand(0), InFlag); |
Chris Lattner | 6961fc7 | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 1029 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | 5769311 | 2005-09-28 22:50:24 +0000 | [diff] [blame] | 1031 | case ISD::SDIV: { |
Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 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. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1034 | // FIXME: It sure would be nice if we could cheaply recognize the |
Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 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. |
Chris Lattner | dc66457 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1037 | unsigned Imm; |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1038 | if (isInt32Immediate(N->getOperand(1), Imm)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1039 | SDValue N0 = N->getOperand(0); |
Chris Lattner | dc66457 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1040 | if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1041 | SDNode *Op = |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1042 | CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1043 | N0, getI32Imm(Log2_32(Imm))); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1044 | return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1045 | SDValue(Op, 0), SDValue(Op, 1)); |
Chris Lattner | dc66457 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1046 | } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1047 | SDNode *Op = |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1048 | CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1049 | N0, getI32Imm(Log2_32(-Imm))); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1050 | SDValue PT = |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1051 | SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32, |
| 1052 | SDValue(Op, 0), SDValue(Op, 1)), |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1053 | 0); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1054 | return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT); |
Chris Lattner | dc66457 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | 1de5706 | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 1058 | // Other cases are autogenerated. |
| 1059 | break; |
Chris Lattner | 6e184f2 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 1060 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1062 | case ISD::LOAD: { |
| 1063 | // Handle preincrement loads. |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1064 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1065 | EVT LoadedVT = LD->getMemoryVT(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1067 | // Normal loads are handled by code generated from the .td file. |
| 1068 | if (LD->getAddressingMode() != ISD::PRE_INC) |
| 1069 | break; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1070 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1071 | SDValue Offset = LD->getOffset(); |
Ulrich Weigand | d1b99d3 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 1072 | if (Offset.getOpcode() == ISD::TargetConstant || |
Chris Lattner | c5102bf | 2006-11-11 04:53:30 +0000 | [diff] [blame] | 1073 | Offset.getOpcode() == ISD::TargetGlobalAddress) { |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1075 | unsigned Opcode; |
| 1076 | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1077 | if (LD->getValueType(0) != MVT::i64) { |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1078 | // Handle PPC32 integer and normal FP loads. |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1079 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
| 1080 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1081 | default: llvm_unreachable("Invalid PPC load type!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 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; |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } else { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 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) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1093 | default: llvm_unreachable("Invalid PPC load type!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 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; |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1101 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1102 | SDValue Chain = LD->getChain(); |
| 1103 | SDValue Base = LD->getBasePtr(); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1104 | SDValue Ops[] = { Offset, Base, Chain }; |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1105 | return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0), |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1106 | PPCLowering->getPointerTy(), |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1107 | MVT::Other, Ops); |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1108 | } else { |
Hal Finkel | ca542be | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 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(); |
Ulrich Weigand | e90b022 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 1139 | SDValue Ops[] = { Base, Offset, Chain }; |
Hal Finkel | ca542be | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 1140 | return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0), |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1141 | PPCLowering->getPointerTy(), |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1142 | MVT::Other, Ops); |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1145 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 1146 | case ISD::AND: { |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1147 | unsigned Imm, Imm2, SH, MB, ME; |
Hal Finkel | e39526a | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 1148 | uint64_t Imm64; |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1149 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 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 |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1152 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1153 | isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1154 | SDValue Val = N->getOperand(0).getOperand(0); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1155 | SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1156 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 1157 | } |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 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) && |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1161 | isRunOfOnes(Imm, MB, ME) && |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1162 | N->getOperand(0).getOpcode() != ISD::ROTL) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1163 | SDValue Val = N->getOperand(0); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1164 | SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1165 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1166 | } |
Hal Finkel | e39526a | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 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); |
Hal Finkel | 22498fa | 2013-11-20 01:10:15 +0000 | [diff] [blame] | 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) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1187 | return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops); |
Hal Finkel | e39526a | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 1188 | } |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1189 | // AND X, 0 -> 0, not "rlwinm 32". |
| 1190 | if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1191 | ReplaceUses(SDValue(N, 0), N->getOperand(1)); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1192 | return nullptr; |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1193 | } |
Nate Begeman | 9aea6e4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 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 |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1196 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Nate Begeman | 9aea6e4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 1197 | N->getOperand(0).getOpcode() == ISD::OR && |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1198 | isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) { |
Chris Lattner | 20c88df | 2006-01-05 18:32:49 +0000 | [diff] [blame] | 1199 | unsigned MB, ME; |
Nate Begeman | 9aea6e4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 1200 | Imm = ~(Imm^Imm2); |
| 1201 | if (isRunOfOnes(Imm, MB, ME)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1202 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1203 | N->getOperand(0).getOperand(1), |
| 1204 | getI32Imm(0), getI32Imm(MB),getI32Imm(ME) }; |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1205 | return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops); |
Nate Begeman | 9aea6e4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 1206 | } |
| 1207 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | 1de5706 | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 1209 | // Other cases are autogenerated. |
| 1210 | break; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 1211 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 1212 | case ISD::OR: |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1213 | if (N->getValueType(0) == MVT::i32) |
Chris Lattner | bc485fd | 2006-08-15 23:48:22 +0000 | [diff] [blame] | 1214 | if (SDNode *I = SelectBitfieldInsert(N)) |
| 1215 | return I; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | 1de5706 | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 1217 | // Other cases are autogenerated. |
| 1218 | break; |
Nate Begeman | 33acb2c | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1219 | case ISD::SHL: { |
| 1220 | unsigned Imm, SH, MB, ME; |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1221 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Nate Begeman | 9f3c26c | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 1222 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1223 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1224 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1225 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Nate Begeman | 9eaa6ba | 2005-10-19 01:12:32 +0000 | [diff] [blame] | 1226 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1227 | |
Nate Begeman | 9f3c26c | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 1228 | // Other cases are autogenerated. |
| 1229 | break; |
Nate Begeman | 33acb2c | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1230 | } |
| 1231 | case ISD::SRL: { |
| 1232 | unsigned Imm, SH, MB, ME; |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1233 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1234 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1235 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1236 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1237 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
Nate Begeman | 9eaa6ba | 2005-10-19 01:12:32 +0000 | [diff] [blame] | 1238 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1239 | |
Nate Begeman | 9f3c26c | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 1240 | // Other cases are autogenerated. |
| 1241 | break; |
Nate Begeman | 33acb2c | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1242 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | bec817c | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1266 | case ISD::SELECT_CC: { |
| 1267 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 1268 | EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy(); |
| 1269 | bool isPPC64 = (PtrVT == MVT::i64); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1270 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1271 | // If this is a select of i1 operands, we'll pattern match it. |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1272 | if (PPCSubTarget->useCRBits() && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1273 | N->getOperand(0).getValueType() == MVT::i1) |
| 1274 | break; |
| 1275 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1276 | // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1292 | |
Dale Johannesen | ab8e442 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1293 | SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 1324 | unsigned BROpc = getPredicateForSetCC(CC); |
Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | d3eee1a | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1326 | unsigned SelectCCOp; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1327 | if (N->getValueType(0) == MVT::i32) |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1328 | SelectCCOp = PPC::SELECT_CC_I4; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1329 | else if (N->getValueType(0) == MVT::i64) |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1330 | SelectCCOp = PPC::SELECT_CC_I8; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1331 | else if (N->getValueType(0) == MVT::f32) |
Chris Lattner | d3eee1a | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1332 | SelectCCOp = PPC::SELECT_CC_F4; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1333 | else if (N->getValueType(0) == MVT::f64) |
Chris Lattner | d3eee1a | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1334 | SelectCCOp = PPC::SELECT_CC_F8; |
Chris Lattner | 0a3d1bb | 2006-04-08 22:45:08 +0000 | [diff] [blame] | 1335 | else |
| 1336 | SelectCCOp = PPC::SELECT_CC_VRRC; |
| 1337 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1338 | SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3), |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1339 | getI32Imm(BROpc) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1340 | return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops); |
Chris Lattner | bec817c | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1341 | } |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 1342 | case ISD::VSELECT: |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1343 | if (PPCSubTarget->hasVSX()) { |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 1344 | SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1345 | return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops); |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | break; |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 1349 | case ISD::VECTOR_SHUFFLE: |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1350 | if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 || |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 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 | |
Hal Finkel | 2583b06 | 2014-03-28 20:24:55 +0000 | [diff] [blame] | 1364 | SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32); |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 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, |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1377 | N->getValueType(0), Ops); |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | SDValue Ops[] = { Op1, Op2, DMV }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1382 | return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops); |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | break; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 1386 | case PPCISD::BDNZ: |
| 1387 | case PPCISD::BDZ: { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1388 | bool IsPPC64 = PPCSubTarget->isPPC64(); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 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), |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1393 | MVT::Other, Ops); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 1394 | } |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1395 | case PPCISD::COND_BRANCH: { |
Dan Gohman | 7a638a8 | 2008-11-05 17:16:24 +0000 | [diff] [blame] | 1396 | // Op #0 is the Chain. |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1397 | // Op #1 is the PPC::PRED_* number. |
| 1398 | // Op #2 is the CR# |
| 1399 | // Op #3 is the Dest MBB |
Dan Gohman | f14b77e | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1400 | // Op #4 is the Flag. |
Evan Cheng | 58d1eac | 2007-06-29 01:25:06 +0000 | [diff] [blame] | 1401 | // Prevent PPC::PRED_* from being selected into LI. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1402 | SDValue Pred = |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1403 | getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1404 | SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3), |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1405 | N->getOperand(0), N->getOperand(4) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1406 | return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1407 | } |
Nate Begeman | bb01d4f | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 1408 | case ISD::BR_CC: { |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1409 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 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 | |
Dale Johannesen | ab8e442 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1432 | SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1433 | SDValue Ops[] = { getI32Imm(PCC), CondCode, |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1434 | N->getOperand(4), N->getOperand(0) }; |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 1435 | return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1436 | } |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1437 | case ISD::BRIND: { |
Chris Lattner | b055c87 | 2006-06-10 01:15:02 +0000 | [diff] [blame] | 1438 | // FIXME: Should custom lower this. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1439 | SDValue Chain = N->getOperand(0); |
| 1440 | SDValue Target = N->getOperand(1); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1441 | unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8; |
Roman Divacky | a4a59ae | 2011-06-03 15:47:49 +0000 | [diff] [blame] | 1442 | unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8; |
Hal Finkel | 528ff4b | 2011-12-08 04:36:44 +0000 | [diff] [blame] | 1443 | Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1444 | Chain), 0); |
Roman Divacky | a4a59ae | 2011-06-03 15:47:49 +0000 | [diff] [blame] | 1445 | return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain); |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1446 | } |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1447 | case PPCISD::TOC_ENTRY: { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1448 | assert (PPCSubTarget->isPPC64() && "Only supported for 64-bit ABI"); |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1449 | |
Bill Schmidt | 2791778 | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 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) |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | |
| 1457 | // The first source operand is a TargetGlobalAddress or a |
| 1458 | // TargetJumpTable. If it is an externally defined symbol, a symbol |
| 1459 | // with common linkage, a function address, or a jump table address, |
Bill Schmidt | 2791778 | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 1460 | // or if we are generating code for large code model, we generate: |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 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 | |
Bill Schmidt | 2791778 | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 1469 | if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large) |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 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(); |
Bill Schmidt | 9b1e3e2 | 2013-01-07 19:29:18 +0000 | [diff] [blame] | 1475 | const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue); |
Rafael Espindola | e009892 | 2014-05-16 22:37:03 +0000 | [diff] [blame] | 1476 | const GlobalValue *RealGValue = GAlias ? GAlias->getAliasee() : GValue; |
Bill Schmidt | 9b1e3e2 | 2013-01-07 19:29:18 +0000 | [diff] [blame] | 1477 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue); |
| 1478 | assert((GVar || isa<Function>(RealGValue)) && |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1479 | "Unexpected global value subclass!"); |
| 1480 | |
| 1481 | // An external variable is one without an initializer. For these, |
| 1482 | // for variables with common linkage, and for Functions, generate |
| 1483 | // the LDtocL form. |
Bill Schmidt | 9b1e3e2 | 2013-01-07 19:29:18 +0000 | [diff] [blame] | 1484 | if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() || |
| 1485 | RealGValue->hasAvailableExternallyLinkage()) |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1486 | return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA, |
| 1487 | SDValue(Tmp, 0)); |
| 1488 | } |
| 1489 | |
| 1490 | return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64, |
| 1491 | SDValue(Tmp, 0), GA); |
| 1492 | } |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1493 | case PPCISD::VADD_SPLAT: { |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1494 | // This expands into one of three sequences, depending on whether |
| 1495 | // the first operand is odd or even, positive or negative. |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1496 | assert(isa<ConstantSDNode>(N->getOperand(0)) && |
| 1497 | isa<ConstantSDNode>(N->getOperand(1)) && |
| 1498 | "Invalid operand on VADD_SPLAT!"); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1499 | |
| 1500 | int Elt = N->getConstantOperandVal(0); |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1501 | int EltSize = N->getConstantOperandVal(1); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1502 | unsigned Opc1, Opc2, Opc3; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1503 | EVT VT; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1504 | |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1505 | if (EltSize == 1) { |
| 1506 | Opc1 = PPC::VSPLTISB; |
| 1507 | Opc2 = PPC::VADDUBM; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1508 | Opc3 = PPC::VSUBUBM; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1509 | VT = MVT::v16i8; |
| 1510 | } else if (EltSize == 2) { |
| 1511 | Opc1 = PPC::VSPLTISH; |
| 1512 | Opc2 = PPC::VADDUHM; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1513 | Opc3 = PPC::VSUBUHM; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1514 | VT = MVT::v8i16; |
| 1515 | } else { |
| 1516 | assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!"); |
| 1517 | Opc1 = PPC::VSPLTISW; |
| 1518 | Opc2 = PPC::VADDUWM; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1519 | Opc3 = PPC::VSUBUWM; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1520 | VT = MVT::v4i32; |
| 1521 | } |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1522 | |
| 1523 | if ((Elt & 1) == 0) { |
| 1524 | // Elt is even, in the range [-32,-18] + [16,30]. |
| 1525 | // |
| 1526 | // Convert: VADD_SPLAT elt, size |
| 1527 | // Into: tmp = VSPLTIS[BHW] elt |
| 1528 | // VADDU[BHW]M tmp, tmp |
| 1529 | // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4 |
| 1530 | SDValue EltVal = getI32Imm(Elt >> 1); |
| 1531 | SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1532 | SDValue TmpVal = SDValue(Tmp, 0); |
| 1533 | return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal); |
| 1534 | |
| 1535 | } else if (Elt > 0) { |
| 1536 | // Elt is odd and positive, in the range [17,31]. |
| 1537 | // |
| 1538 | // Convert: VADD_SPLAT elt, size |
| 1539 | // Into: tmp1 = VSPLTIS[BHW] elt-16 |
| 1540 | // tmp2 = VSPLTIS[BHW] -16 |
| 1541 | // VSUBU[BHW]M tmp1, tmp2 |
| 1542 | SDValue EltVal = getI32Imm(Elt - 16); |
| 1543 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1544 | EltVal = getI32Imm(-16); |
| 1545 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1546 | return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0), |
| 1547 | SDValue(Tmp2, 0)); |
| 1548 | |
| 1549 | } else { |
| 1550 | // Elt is odd and negative, in the range [-31,-17]. |
| 1551 | // |
| 1552 | // Convert: VADD_SPLAT elt, size |
| 1553 | // Into: tmp1 = VSPLTIS[BHW] elt+16 |
| 1554 | // tmp2 = VSPLTIS[BHW] -16 |
| 1555 | // VADDU[BHW]M tmp1, tmp2 |
| 1556 | SDValue EltVal = getI32Imm(Elt + 16); |
| 1557 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1558 | EltVal = getI32Imm(-16); |
| 1559 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1560 | return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0), |
| 1561 | SDValue(Tmp2, 0)); |
| 1562 | } |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1563 | } |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1564 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1565 | |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1566 | return SelectCode(N); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Hal Finkel | 860fa90 | 2014-01-02 22:09:39 +0000 | [diff] [blame] | 1569 | /// PostprocessISelDAG - Perform some late peephole optimizations |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 1570 | /// on the DAG representation. |
| 1571 | void PPCDAGToDAGISel::PostprocessISelDAG() { |
| 1572 | |
| 1573 | // Skip peepholes at -O0. |
| 1574 | if (TM.getOptLevel() == CodeGenOpt::None) |
| 1575 | return; |
| 1576 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1577 | PeepholePPC64(); |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 1578 | PeepholeCROps(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1581 | // Check if all users of this node will become isel where the second operand |
| 1582 | // is the constant zero. If this is so, and if we can negate the condition, |
| 1583 | // then we can flip the true and false operands. This will allow the zero to |
| 1584 | // be folded with the isel so that we don't need to materialize a register |
| 1585 | // containing zero. |
| 1586 | bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) { |
| 1587 | // If we're not using isel, then this does not matter. |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 1588 | if (!PPCSubTarget->hasISEL()) |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1589 | return false; |
| 1590 | |
| 1591 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 1592 | UI != UE; ++UI) { |
| 1593 | SDNode *User = *UI; |
| 1594 | if (!User->isMachineOpcode()) |
| 1595 | return false; |
| 1596 | if (User->getMachineOpcode() != PPC::SELECT_I4 && |
| 1597 | User->getMachineOpcode() != PPC::SELECT_I8) |
| 1598 | return false; |
| 1599 | |
| 1600 | SDNode *Op2 = User->getOperand(2).getNode(); |
| 1601 | if (!Op2->isMachineOpcode()) |
| 1602 | return false; |
| 1603 | |
| 1604 | if (Op2->getMachineOpcode() != PPC::LI && |
| 1605 | Op2->getMachineOpcode() != PPC::LI8) |
| 1606 | return false; |
| 1607 | |
| 1608 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0)); |
| 1609 | if (!C) |
| 1610 | return false; |
| 1611 | |
| 1612 | if (!C->isNullValue()) |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
| 1616 | return true; |
| 1617 | } |
| 1618 | |
| 1619 | void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) { |
| 1620 | SmallVector<SDNode *, 4> ToReplace; |
| 1621 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 1622 | UI != UE; ++UI) { |
| 1623 | SDNode *User = *UI; |
| 1624 | assert((User->getMachineOpcode() == PPC::SELECT_I4 || |
| 1625 | User->getMachineOpcode() == PPC::SELECT_I8) && |
| 1626 | "Must have all select users"); |
| 1627 | ToReplace.push_back(User); |
| 1628 | } |
| 1629 | |
| 1630 | for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(), |
| 1631 | UE = ToReplace.end(); UI != UE; ++UI) { |
| 1632 | SDNode *User = *UI; |
| 1633 | SDNode *ResNode = |
| 1634 | CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User), |
| 1635 | User->getValueType(0), User->getOperand(0), |
| 1636 | User->getOperand(2), |
| 1637 | User->getOperand(1)); |
| 1638 | |
| 1639 | DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); |
| 1640 | DEBUG(User->dump(CurDAG)); |
| 1641 | DEBUG(dbgs() << "\nNew: "); |
| 1642 | DEBUG(ResNode->dump(CurDAG)); |
| 1643 | DEBUG(dbgs() << "\n"); |
| 1644 | |
| 1645 | ReplaceUses(User, ResNode); |
| 1646 | } |
| 1647 | } |
| 1648 | |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 1649 | void PPCDAGToDAGISel::PeepholeCROps() { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1650 | bool IsModified; |
| 1651 | do { |
| 1652 | IsModified = false; |
| 1653 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 1654 | E = CurDAG->allnodes_end(); I != E; ++I) { |
| 1655 | MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I); |
| 1656 | if (!MachineNode || MachineNode->use_empty()) |
| 1657 | continue; |
| 1658 | SDNode *ResNode = MachineNode; |
| 1659 | |
| 1660 | bool Op1Set = false, Op1Unset = false, |
| 1661 | Op1Not = false, |
| 1662 | Op2Set = false, Op2Unset = false, |
| 1663 | Op2Not = false; |
| 1664 | |
| 1665 | unsigned Opcode = MachineNode->getMachineOpcode(); |
| 1666 | switch (Opcode) { |
| 1667 | default: break; |
| 1668 | case PPC::CRAND: |
| 1669 | case PPC::CRNAND: |
| 1670 | case PPC::CROR: |
| 1671 | case PPC::CRXOR: |
| 1672 | case PPC::CRNOR: |
| 1673 | case PPC::CREQV: |
| 1674 | case PPC::CRANDC: |
| 1675 | case PPC::CRORC: { |
| 1676 | SDValue Op = MachineNode->getOperand(1); |
| 1677 | if (Op.isMachineOpcode()) { |
| 1678 | if (Op.getMachineOpcode() == PPC::CRSET) |
| 1679 | Op2Set = true; |
| 1680 | else if (Op.getMachineOpcode() == PPC::CRUNSET) |
| 1681 | Op2Unset = true; |
| 1682 | else if (Op.getMachineOpcode() == PPC::CRNOR && |
| 1683 | Op.getOperand(0) == Op.getOperand(1)) |
| 1684 | Op2Not = true; |
| 1685 | } |
| 1686 | } // fallthrough |
| 1687 | case PPC::BC: |
| 1688 | case PPC::BCn: |
| 1689 | case PPC::SELECT_I4: |
| 1690 | case PPC::SELECT_I8: |
| 1691 | case PPC::SELECT_F4: |
| 1692 | case PPC::SELECT_F8: |
| 1693 | case PPC::SELECT_VRRC: { |
| 1694 | SDValue Op = MachineNode->getOperand(0); |
| 1695 | if (Op.isMachineOpcode()) { |
| 1696 | if (Op.getMachineOpcode() == PPC::CRSET) |
| 1697 | Op1Set = true; |
| 1698 | else if (Op.getMachineOpcode() == PPC::CRUNSET) |
| 1699 | Op1Unset = true; |
| 1700 | else if (Op.getMachineOpcode() == PPC::CRNOR && |
| 1701 | Op.getOperand(0) == Op.getOperand(1)) |
| 1702 | Op1Not = true; |
| 1703 | } |
| 1704 | } |
| 1705 | break; |
| 1706 | } |
| 1707 | |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1708 | bool SelectSwap = false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1709 | switch (Opcode) { |
| 1710 | default: break; |
| 1711 | case PPC::CRAND: |
| 1712 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 1713 | // x & x = x |
| 1714 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1715 | else if (Op1Set) |
| 1716 | // 1 & y = y |
| 1717 | ResNode = MachineNode->getOperand(1).getNode(); |
| 1718 | else if (Op2Set) |
| 1719 | // x & 1 = x |
| 1720 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1721 | else if (Op1Unset || Op2Unset) |
| 1722 | // x & 0 = 0 & y = 0 |
| 1723 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 1724 | MVT::i1); |
| 1725 | else if (Op1Not) |
| 1726 | // ~x & y = andc(y, x) |
| 1727 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 1728 | MVT::i1, MachineNode->getOperand(1), |
| 1729 | MachineNode->getOperand(0). |
| 1730 | getOperand(0)); |
| 1731 | else if (Op2Not) |
| 1732 | // x & ~y = andc(x, y) |
| 1733 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 1734 | MVT::i1, MachineNode->getOperand(0), |
| 1735 | MachineNode->getOperand(1). |
| 1736 | getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1737 | else if (AllUsersSelectZero(MachineNode)) |
| 1738 | ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), |
| 1739 | MVT::i1, MachineNode->getOperand(0), |
| 1740 | MachineNode->getOperand(1)), |
| 1741 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1742 | break; |
| 1743 | case PPC::CRNAND: |
| 1744 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 1745 | // nand(x, x) -> nor(x, x) |
| 1746 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1747 | MVT::i1, MachineNode->getOperand(0), |
| 1748 | MachineNode->getOperand(0)); |
| 1749 | else if (Op1Set) |
| 1750 | // nand(1, y) -> nor(y, y) |
| 1751 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1752 | MVT::i1, MachineNode->getOperand(1), |
| 1753 | MachineNode->getOperand(1)); |
| 1754 | else if (Op2Set) |
| 1755 | // nand(x, 1) -> nor(x, x) |
| 1756 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1757 | MVT::i1, MachineNode->getOperand(0), |
| 1758 | MachineNode->getOperand(0)); |
| 1759 | else if (Op1Unset || Op2Unset) |
| 1760 | // nand(x, 0) = nand(0, y) = 1 |
| 1761 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 1762 | MVT::i1); |
| 1763 | else if (Op1Not) |
| 1764 | // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y) |
| 1765 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 1766 | MVT::i1, MachineNode->getOperand(0). |
| 1767 | getOperand(0), |
| 1768 | MachineNode->getOperand(1)); |
| 1769 | else if (Op2Not) |
| 1770 | // nand(x, ~y) = ~x | y = orc(y, x) |
| 1771 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 1772 | MVT::i1, MachineNode->getOperand(1). |
| 1773 | getOperand(0), |
| 1774 | MachineNode->getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1775 | else if (AllUsersSelectZero(MachineNode)) |
| 1776 | ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), |
| 1777 | MVT::i1, MachineNode->getOperand(0), |
| 1778 | MachineNode->getOperand(1)), |
| 1779 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1780 | break; |
| 1781 | case PPC::CROR: |
| 1782 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 1783 | // x | x = x |
| 1784 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1785 | else if (Op1Set || Op2Set) |
| 1786 | // x | 1 = 1 | y = 1 |
| 1787 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 1788 | MVT::i1); |
| 1789 | else if (Op1Unset) |
| 1790 | // 0 | y = y |
| 1791 | ResNode = MachineNode->getOperand(1).getNode(); |
| 1792 | else if (Op2Unset) |
| 1793 | // x | 0 = x |
| 1794 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1795 | else if (Op1Not) |
| 1796 | // ~x | y = orc(y, x) |
| 1797 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 1798 | MVT::i1, MachineNode->getOperand(1), |
| 1799 | MachineNode->getOperand(0). |
| 1800 | getOperand(0)); |
| 1801 | else if (Op2Not) |
| 1802 | // x | ~y = orc(x, y) |
| 1803 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 1804 | MVT::i1, MachineNode->getOperand(0), |
| 1805 | MachineNode->getOperand(1). |
| 1806 | getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1807 | else if (AllUsersSelectZero(MachineNode)) |
| 1808 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1809 | MVT::i1, MachineNode->getOperand(0), |
| 1810 | MachineNode->getOperand(1)), |
| 1811 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1812 | break; |
| 1813 | case PPC::CRXOR: |
| 1814 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 1815 | // xor(x, x) = 0 |
| 1816 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 1817 | MVT::i1); |
| 1818 | else if (Op1Set) |
| 1819 | // xor(1, y) -> nor(y, y) |
| 1820 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1821 | MVT::i1, MachineNode->getOperand(1), |
| 1822 | MachineNode->getOperand(1)); |
| 1823 | else if (Op2Set) |
| 1824 | // xor(x, 1) -> nor(x, x) |
| 1825 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1826 | MVT::i1, MachineNode->getOperand(0), |
| 1827 | MachineNode->getOperand(0)); |
| 1828 | else if (Op1Unset) |
| 1829 | // xor(0, y) = y |
| 1830 | ResNode = MachineNode->getOperand(1).getNode(); |
| 1831 | else if (Op2Unset) |
| 1832 | // xor(x, 0) = x |
| 1833 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1834 | else if (Op1Not) |
| 1835 | // xor(~x, y) = eqv(x, y) |
| 1836 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
| 1837 | MVT::i1, MachineNode->getOperand(0). |
| 1838 | getOperand(0), |
| 1839 | MachineNode->getOperand(1)); |
| 1840 | else if (Op2Not) |
| 1841 | // xor(x, ~y) = eqv(x, y) |
| 1842 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
| 1843 | MVT::i1, MachineNode->getOperand(0), |
| 1844 | MachineNode->getOperand(1). |
| 1845 | getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1846 | else if (AllUsersSelectZero(MachineNode)) |
| 1847 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
| 1848 | MVT::i1, MachineNode->getOperand(0), |
| 1849 | MachineNode->getOperand(1)), |
| 1850 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1851 | break; |
| 1852 | case PPC::CRNOR: |
| 1853 | if (Op1Set || Op2Set) |
| 1854 | // nor(1, y) -> 0 |
| 1855 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 1856 | MVT::i1); |
| 1857 | else if (Op1Unset) |
| 1858 | // nor(0, y) = ~y -> nor(y, y) |
| 1859 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1860 | MVT::i1, MachineNode->getOperand(1), |
| 1861 | MachineNode->getOperand(1)); |
| 1862 | else if (Op2Unset) |
| 1863 | // nor(x, 0) = ~x |
| 1864 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1865 | MVT::i1, MachineNode->getOperand(0), |
| 1866 | MachineNode->getOperand(0)); |
| 1867 | else if (Op1Not) |
| 1868 | // nor(~x, y) = andc(x, y) |
| 1869 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 1870 | MVT::i1, MachineNode->getOperand(0). |
| 1871 | getOperand(0), |
| 1872 | MachineNode->getOperand(1)); |
| 1873 | else if (Op2Not) |
| 1874 | // nor(x, ~y) = andc(y, x) |
| 1875 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 1876 | MVT::i1, MachineNode->getOperand(1). |
| 1877 | getOperand(0), |
| 1878 | MachineNode->getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1879 | else if (AllUsersSelectZero(MachineNode)) |
| 1880 | ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), |
| 1881 | MVT::i1, MachineNode->getOperand(0), |
| 1882 | MachineNode->getOperand(1)), |
| 1883 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1884 | break; |
| 1885 | case PPC::CREQV: |
| 1886 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 1887 | // eqv(x, x) = 1 |
| 1888 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 1889 | MVT::i1); |
| 1890 | else if (Op1Set) |
| 1891 | // eqv(1, y) = y |
| 1892 | ResNode = MachineNode->getOperand(1).getNode(); |
| 1893 | else if (Op2Set) |
| 1894 | // eqv(x, 1) = x |
| 1895 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1896 | else if (Op1Unset) |
| 1897 | // eqv(0, y) = ~y -> nor(y, y) |
| 1898 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1899 | MVT::i1, MachineNode->getOperand(1), |
| 1900 | MachineNode->getOperand(1)); |
| 1901 | else if (Op2Unset) |
| 1902 | // eqv(x, 0) = ~x |
| 1903 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1904 | MVT::i1, MachineNode->getOperand(0), |
| 1905 | MachineNode->getOperand(0)); |
| 1906 | else if (Op1Not) |
| 1907 | // eqv(~x, y) = xor(x, y) |
| 1908 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
| 1909 | MVT::i1, MachineNode->getOperand(0). |
| 1910 | getOperand(0), |
| 1911 | MachineNode->getOperand(1)); |
| 1912 | else if (Op2Not) |
| 1913 | // eqv(x, ~y) = xor(x, y) |
| 1914 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
| 1915 | MVT::i1, MachineNode->getOperand(0), |
| 1916 | MachineNode->getOperand(1). |
| 1917 | getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1918 | else if (AllUsersSelectZero(MachineNode)) |
| 1919 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
| 1920 | MVT::i1, MachineNode->getOperand(0), |
| 1921 | MachineNode->getOperand(1)), |
| 1922 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | case PPC::CRANDC: |
| 1925 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 1926 | // andc(x, x) = 0 |
| 1927 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 1928 | MVT::i1); |
| 1929 | else if (Op1Set) |
| 1930 | // andc(1, y) = ~y |
| 1931 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1932 | MVT::i1, MachineNode->getOperand(1), |
| 1933 | MachineNode->getOperand(1)); |
| 1934 | else if (Op1Unset || Op2Set) |
| 1935 | // andc(0, y) = andc(x, 1) = 0 |
| 1936 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 1937 | MVT::i1); |
| 1938 | else if (Op2Unset) |
| 1939 | // andc(x, 0) = x |
| 1940 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1941 | else if (Op1Not) |
| 1942 | // andc(~x, y) = ~(x | y) = nor(x, y) |
| 1943 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1944 | MVT::i1, MachineNode->getOperand(0). |
| 1945 | getOperand(0), |
| 1946 | MachineNode->getOperand(1)); |
| 1947 | else if (Op2Not) |
| 1948 | // andc(x, ~y) = x & y |
| 1949 | ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), |
| 1950 | MVT::i1, MachineNode->getOperand(0), |
| 1951 | MachineNode->getOperand(1). |
| 1952 | getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1953 | else if (AllUsersSelectZero(MachineNode)) |
| 1954 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 1955 | MVT::i1, MachineNode->getOperand(1), |
| 1956 | MachineNode->getOperand(0)), |
| 1957 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1958 | break; |
| 1959 | case PPC::CRORC: |
| 1960 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 1961 | // orc(x, x) = 1 |
| 1962 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 1963 | MVT::i1); |
| 1964 | else if (Op1Set || Op2Unset) |
| 1965 | // orc(1, y) = orc(x, 0) = 1 |
| 1966 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 1967 | MVT::i1); |
| 1968 | else if (Op2Set) |
| 1969 | // orc(x, 1) = x |
| 1970 | ResNode = MachineNode->getOperand(0).getNode(); |
| 1971 | else if (Op1Unset) |
| 1972 | // orc(0, y) = ~y |
| 1973 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 1974 | MVT::i1, MachineNode->getOperand(1), |
| 1975 | MachineNode->getOperand(1)); |
| 1976 | else if (Op1Not) |
| 1977 | // orc(~x, y) = ~(x & y) = nand(x, y) |
| 1978 | ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), |
| 1979 | MVT::i1, MachineNode->getOperand(0). |
| 1980 | getOperand(0), |
| 1981 | MachineNode->getOperand(1)); |
| 1982 | else if (Op2Not) |
| 1983 | // orc(x, ~y) = x | y |
| 1984 | ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), |
| 1985 | MVT::i1, MachineNode->getOperand(0), |
| 1986 | MachineNode->getOperand(1). |
| 1987 | getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 1988 | else if (AllUsersSelectZero(MachineNode)) |
| 1989 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 1990 | MVT::i1, MachineNode->getOperand(1), |
| 1991 | MachineNode->getOperand(0)), |
| 1992 | SelectSwap = true; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1993 | break; |
| 1994 | case PPC::SELECT_I4: |
| 1995 | case PPC::SELECT_I8: |
| 1996 | case PPC::SELECT_F4: |
| 1997 | case PPC::SELECT_F8: |
| 1998 | case PPC::SELECT_VRRC: |
| 1999 | if (Op1Set) |
| 2000 | ResNode = MachineNode->getOperand(1).getNode(); |
| 2001 | else if (Op1Unset) |
| 2002 | ResNode = MachineNode->getOperand(2).getNode(); |
| 2003 | else if (Op1Not) |
| 2004 | ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(), |
| 2005 | SDLoc(MachineNode), |
| 2006 | MachineNode->getValueType(0), |
| 2007 | MachineNode->getOperand(0). |
| 2008 | getOperand(0), |
| 2009 | MachineNode->getOperand(2), |
| 2010 | MachineNode->getOperand(1)); |
| 2011 | break; |
| 2012 | case PPC::BC: |
| 2013 | case PPC::BCn: |
| 2014 | if (Op1Not) |
| 2015 | ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn : |
| 2016 | PPC::BC, |
| 2017 | SDLoc(MachineNode), |
| 2018 | MVT::Other, |
| 2019 | MachineNode->getOperand(0). |
| 2020 | getOperand(0), |
| 2021 | MachineNode->getOperand(1), |
| 2022 | MachineNode->getOperand(2)); |
| 2023 | // FIXME: Handle Op1Set, Op1Unset here too. |
| 2024 | break; |
| 2025 | } |
| 2026 | |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 2027 | // If we're inverting this node because it is used only by selects that |
| 2028 | // we'd like to swap, then swap the selects before the node replacement. |
| 2029 | if (SelectSwap) |
| 2030 | SwapAllSelectUsers(MachineNode); |
| 2031 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 2032 | if (ResNode != MachineNode) { |
| 2033 | DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); |
| 2034 | DEBUG(MachineNode->dump(CurDAG)); |
| 2035 | DEBUG(dbgs() << "\nNew: "); |
| 2036 | DEBUG(ResNode->dump(CurDAG)); |
| 2037 | DEBUG(dbgs() << "\n"); |
| 2038 | |
| 2039 | ReplaceUses(MachineNode, ResNode); |
| 2040 | IsModified = true; |
| 2041 | } |
| 2042 | } |
| 2043 | if (IsModified) |
| 2044 | CurDAG->RemoveDeadNodes(); |
| 2045 | } while (IsModified); |
| 2046 | } |
| 2047 | |
| 2048 | void PPCDAGToDAGISel::PeepholePPC64() { |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2049 | // These optimizations are currently supported only for 64-bit SVR4. |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame^] | 2050 | if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64()) |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2051 | return; |
| 2052 | |
| 2053 | SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); |
| 2054 | ++Position; |
| 2055 | |
| 2056 | while (Position != CurDAG->allnodes_begin()) { |
| 2057 | SDNode *N = --Position; |
| 2058 | // Skip dead nodes and any non-machine opcodes. |
| 2059 | if (N->use_empty() || !N->isMachineOpcode()) |
| 2060 | continue; |
| 2061 | |
| 2062 | unsigned FirstOp; |
| 2063 | unsigned StorageOpcode = N->getMachineOpcode(); |
| 2064 | |
| 2065 | switch (StorageOpcode) { |
| 2066 | default: continue; |
| 2067 | |
| 2068 | case PPC::LBZ: |
| 2069 | case PPC::LBZ8: |
| 2070 | case PPC::LD: |
| 2071 | case PPC::LFD: |
| 2072 | case PPC::LFS: |
| 2073 | case PPC::LHA: |
| 2074 | case PPC::LHA8: |
| 2075 | case PPC::LHZ: |
| 2076 | case PPC::LHZ8: |
| 2077 | case PPC::LWA: |
| 2078 | case PPC::LWZ: |
| 2079 | case PPC::LWZ8: |
| 2080 | FirstOp = 0; |
| 2081 | break; |
| 2082 | |
| 2083 | case PPC::STB: |
| 2084 | case PPC::STB8: |
| 2085 | case PPC::STD: |
| 2086 | case PPC::STFD: |
| 2087 | case PPC::STFS: |
| 2088 | case PPC::STH: |
| 2089 | case PPC::STH8: |
| 2090 | case PPC::STW: |
| 2091 | case PPC::STW8: |
| 2092 | FirstOp = 1; |
| 2093 | break; |
| 2094 | } |
| 2095 | |
| 2096 | // If this is a load or store with a zero offset, we may be able to |
| 2097 | // fold an add-immediate into the memory operation. |
| 2098 | if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) || |
| 2099 | N->getConstantOperandVal(FirstOp) != 0) |
| 2100 | continue; |
| 2101 | |
| 2102 | SDValue Base = N->getOperand(FirstOp + 1); |
| 2103 | if (!Base.isMachineOpcode()) |
| 2104 | continue; |
| 2105 | |
| 2106 | unsigned Flags = 0; |
| 2107 | bool ReplaceFlags = true; |
| 2108 | |
| 2109 | // When the feeding operation is an add-immediate of some sort, |
| 2110 | // determine whether we need to add relocation information to the |
| 2111 | // target flags on the immediate operand when we fold it into the |
| 2112 | // load instruction. |
| 2113 | // |
| 2114 | // For something like ADDItocL, the relocation information is |
| 2115 | // inferred from the opcode; when we process it in the AsmPrinter, |
| 2116 | // we add the necessary relocation there. A load, though, can receive |
| 2117 | // relocation from various flavors of ADDIxxx, so we need to carry |
| 2118 | // the relocation information in the target flags. |
| 2119 | switch (Base.getMachineOpcode()) { |
| 2120 | default: continue; |
| 2121 | |
| 2122 | case PPC::ADDI8: |
Ulrich Weigand | 35f9fdf | 2013-03-26 10:55:20 +0000 | [diff] [blame] | 2123 | case PPC::ADDI: |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2124 | // In some cases (such as TLS) the relocation information |
| 2125 | // is already in place on the operand, so copying the operand |
| 2126 | // is sufficient. |
| 2127 | ReplaceFlags = false; |
| 2128 | // For these cases, the immediate may not be divisible by 4, in |
| 2129 | // which case the fold is illegal for DS-form instructions. (The |
| 2130 | // other cases provide aligned addresses and are always safe.) |
| 2131 | if ((StorageOpcode == PPC::LWA || |
| 2132 | StorageOpcode == PPC::LD || |
| 2133 | StorageOpcode == PPC::STD) && |
| 2134 | (!isa<ConstantSDNode>(Base.getOperand(1)) || |
| 2135 | Base.getConstantOperandVal(1) % 4 != 0)) |
| 2136 | continue; |
| 2137 | break; |
| 2138 | case PPC::ADDIdtprelL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 2139 | Flags = PPCII::MO_DTPREL_LO; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2140 | break; |
| 2141 | case PPC::ADDItlsldL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 2142 | Flags = PPCII::MO_TLSLD_LO; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2143 | break; |
| 2144 | case PPC::ADDItocL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 2145 | Flags = PPCII::MO_TOC_LO; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2146 | break; |
| 2147 | } |
| 2148 | |
| 2149 | // We found an opportunity. Reverse the operands from the add |
| 2150 | // immediate and substitute them into the load or store. If |
| 2151 | // needed, update the target flags for the immediate operand to |
| 2152 | // reflect the necessary relocation information. |
| 2153 | DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); |
| 2154 | DEBUG(Base->dump(CurDAG)); |
| 2155 | DEBUG(dbgs() << "\nN: "); |
| 2156 | DEBUG(N->dump(CurDAG)); |
| 2157 | DEBUG(dbgs() << "\n"); |
| 2158 | |
| 2159 | SDValue ImmOpnd = Base.getOperand(1); |
| 2160 | |
| 2161 | // If the relocation information isn't already present on the |
| 2162 | // immediate operand, add it now. |
| 2163 | if (ReplaceFlags) { |
Bill Schmidt | 49498da | 2013-02-21 14:35:42 +0000 | [diff] [blame] | 2164 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2165 | SDLoc dl(GA); |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2166 | const GlobalValue *GV = GA->getGlobal(); |
Bill Schmidt | 48fc20a | 2013-07-01 20:52:27 +0000 | [diff] [blame] | 2167 | // We can't perform this optimization for data whose alignment |
| 2168 | // is insufficient for the instruction encoding. |
| 2169 | if (GV->getAlignment() < 4 && |
| 2170 | (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD || |
| 2171 | StorageOpcode == PPC::LWA)) { |
| 2172 | DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n"); |
| 2173 | continue; |
| 2174 | } |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2175 | ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags); |
Bill Schmidt | 836c45b | 2013-02-21 17:26:05 +0000 | [diff] [blame] | 2176 | } else if (ConstantPoolSDNode *CP = |
| 2177 | dyn_cast<ConstantPoolSDNode>(ImmOpnd)) { |
Bill Schmidt | 49498da | 2013-02-21 14:35:42 +0000 | [diff] [blame] | 2178 | const Constant *C = CP->getConstVal(); |
| 2179 | ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64, |
| 2180 | CP->getAlignment(), |
| 2181 | 0, Flags); |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | if (FirstOp == 1) // Store |
| 2186 | (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd, |
| 2187 | Base.getOperand(0), N->getOperand(3)); |
| 2188 | else // Load |
| 2189 | (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0), |
| 2190 | N->getOperand(2)); |
| 2191 | |
| 2192 | // The add-immediate may now be dead, in which case remove it. |
| 2193 | if (Base.getNode()->use_empty()) |
| 2194 | CurDAG->RemoveDeadNode(Base.getNode()); |
| 2195 | } |
| 2196 | } |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 2197 | |
Chris Lattner | b055c87 | 2006-06-10 01:15:02 +0000 | [diff] [blame] | 2198 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 2199 | /// createPPCISelDag - This pass converts a legalized DAG into a |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 2200 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 2201 | /// |
Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 2202 | FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) { |
Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 2203 | return new PPCDAGToDAGISel(TM); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 2206 | static void initializePassOnce(PassRegistry &Registry) { |
| 2207 | const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection"; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2208 | PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, |
| 2209 | nullptr, false, false); |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 2210 | Registry.registerPass(*PI, true); |
| 2211 | } |
| 2212 | |
| 2213 | void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) { |
| 2214 | CALL_ONCE_INITIALIZATION(initializePassOnce); |
| 2215 | } |
| 2216 | |