Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 1 | //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===// |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 10 | // This file defines a pattern matching instruction selector for PowerPC, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 11 | // converting from a legalized dag to a PPC dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "ppc-codegen" |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 16 | #include "PPC.h" |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/PPCPredicates.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "PPCTargetMachine.h" |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/SelectionDAG.h" |
| 23 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/Function.h" |
Chandler Carruth | 90230c8 | 2013-01-19 08:03:47 +0000 | [diff] [blame] | 26 | #include "llvm/IR/GlobalAlias.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GlobalValue.h" |
| 28 | #include "llvm/IR/GlobalVariable.h" |
| 29 | #include "llvm/IR/Intrinsics.h" |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Krzysztof Parzyszek | 96848df | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 37 | namespace llvm { |
| 38 | void initializePPCDAGToDAGISelPass(PassRegistry&); |
| 39 | } |
| 40 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 41 | namespace { |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 42 | //===--------------------------------------------------------------------===// |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 43 | /// PPCDAGToDAGISel - PPC specific code to select PPC machine |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 44 | /// instructions for SelectionDAG operations. |
| 45 | /// |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 46 | class PPCDAGToDAGISel : public SelectionDAGISel { |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 47 | const PPCTargetMachine &TM; |
| 48 | const PPCTargetLowering &PPCLowering; |
Evan Cheng | 152b7e1 | 2007-10-23 06:42:42 +0000 | [diff] [blame] | 49 | const PPCSubtarget &PPCSubTarget; |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 50 | unsigned GlobalBaseReg; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 51 | public: |
Dan Gohman | 1002c02 | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 52 | explicit PPCDAGToDAGISel(PPCTargetMachine &tm) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 53 | : SelectionDAGISel(tm), TM(tm), |
Evan Cheng | 152b7e1 | 2007-10-23 06:42:42 +0000 | [diff] [blame] | 54 | PPCLowering(*TM.getTargetLowering()), |
Krzysztof Parzyszek | 96848df | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 55 | PPCSubTarget(*TM.getSubtargetImpl()) { |
| 56 | initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry()); |
| 57 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 58 | |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 59 | virtual bool runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 60 | // Make sure we re-emit a set of the global base reg if necessary |
| 61 | GlobalBaseReg = 0; |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 62 | SelectionDAGISel::runOnMachineFunction(MF); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 63 | |
Bill Schmidt | a5d0ab5 | 2012-10-10 20:54:15 +0000 | [diff] [blame] | 64 | if (!PPCSubTarget.isSVR4ABI()) |
| 65 | InsertVRSaveCode(MF); |
| 66 | |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 67 | return true; |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 68 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 69 | |
Bill Schmidt | 4210211 | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 70 | virtual void PostprocessISelDAG(); |
| 71 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 72 | /// getI32Imm - Return a target constant with the specified value, of type |
| 73 | /// i32. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 74 | inline SDValue getI32Imm(unsigned Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 75 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 77 | |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 78 | /// getI64Imm - Return a target constant with the specified value, of type |
| 79 | /// i64. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 80 | inline SDValue getI64Imm(uint64_t Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 81 | return CurDAG->getTargetConstant(Imm, MVT::i64); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 82 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 83 | |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 84 | /// getSmallIPtrImm - Return a target constant of pointer type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 85 | inline SDValue getSmallIPtrImm(unsigned Imm) { |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 86 | return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy()); |
| 87 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 88 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 89 | /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 90 | /// with any number of 0s on either side. The 1s are allowed to wrap from |
| 91 | /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. |
| 92 | /// 0x0F0F0000 is not, since all 1s are not contiguous. |
| 93 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME); |
| 94 | |
| 95 | |
| 96 | /// isRotateAndMask - Returns true if Mask and Shift can be folded into a |
| 97 | /// rotate and mask opcode and mask operation. |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 98 | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask, |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 99 | unsigned &SH, unsigned &MB, unsigned &ME); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 101 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 102 | /// base register. Return the virtual register that holds this value. |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 103 | SDNode *getGlobalBaseReg(); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 104 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 105 | // Select - Convert the specified operand from a target-independent to a |
| 106 | // target-specific node if it hasn't already been changed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 107 | SDNode *Select(SDNode *N); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 108 | |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 109 | SDNode *SelectBitfieldInsert(SDNode *N); |
| 110 | |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 111 | /// SelectCC - Select a comparison of the specified values with the |
| 112 | /// specified condition code, returning the CR# of the expression. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 113 | SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 114 | |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 115 | /// SelectAddrImm - Returns true if the address N can be represented by |
| 116 | /// a base register plus a signed 16-bit displacement [r+imm]. |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 117 | bool SelectAddrImm(SDValue N, SDValue &Disp, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 118 | SDValue &Base) { |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 119 | return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, false); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 120 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 74531e4 | 2006-11-16 00:41:37 +0000 | [diff] [blame] | 122 | /// SelectAddrImmOffs - Return true if the operand is valid for a preinc |
Ulrich Weigand | 0301e79 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 123 | /// immediate field. Note that the operand at this point is already the |
| 124 | /// result of a prior SelectAddressRegImm call. |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 125 | bool SelectAddrImmOffs(SDValue N, SDValue &Out) const { |
Ulrich Weigand | 0301e79 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 126 | if (N.getOpcode() == ISD::TargetConstant || |
Hal Finkel | 2bbc919 | 2012-06-21 20:10:48 +0000 | [diff] [blame] | 127 | N.getOpcode() == ISD::TargetGlobalAddress) { |
Hal Finkel | ac81cc3 | 2012-06-19 02:34:32 +0000 | [diff] [blame] | 128 | Out = N; |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 135 | /// SelectAddrIdx - Given the specified addressed, check to see if it can be |
| 136 | /// represented as an indexed [r+r] operation. Returns false if it can |
| 137 | /// be represented by [r+imm], which are preferred. |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 138 | bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 139 | return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG); |
| 140 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 141 | |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 142 | /// SelectAddrIdxOnly - Given the specified addressed, force it to be |
| 143 | /// represented as an indexed [r+r] operation. |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 144 | bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 145 | return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG); |
| 146 | } |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 147 | |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 148 | /// SelectAddrImmX4 - Returns true if the address N can be represented by |
| 149 | /// a base register plus a signed 16-bit displacement that is a multiple of 4. |
| 150 | /// Suitable for use by STD and friends. |
| 151 | bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) { |
| 152 | return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, true); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 153 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 154 | |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 155 | // Select an address into a single register. |
| 156 | bool SelectAddr(SDValue N, SDValue &Base) { |
| 157 | Base = N; |
| 158 | return true; |
| 159 | } |
| 160 | |
Chris Lattner | e5d8861 | 2006-02-24 02:13:12 +0000 | [diff] [blame] | 161 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
Dale Johannesen | 5cfd4dd | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 162 | /// inline asm expressions. It is always correct to compute the value into |
| 163 | /// a register. The case of adding a (possibly relocatable) constant to a |
| 164 | /// register can be improved, but it is wrong to substitute Reg+Reg for |
| 165 | /// Reg in an asm, because the load or store opcode would have to change. |
| 166 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Chris Lattner | e5d8861 | 2006-02-24 02:13:12 +0000 | [diff] [blame] | 167 | char ConstraintCode, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 168 | std::vector<SDValue> &OutOps) { |
Dale Johannesen | 5cfd4dd | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 169 | OutOps.push_back(Op); |
Chris Lattner | e5d8861 | 2006-02-24 02:13:12 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 172 | |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 173 | void InsertVRSaveCode(MachineFunction &MF); |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 174 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 175 | virtual const char *getPassName() const { |
| 176 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | af16538 | 2005-09-13 22:03:06 +0000 | [diff] [blame] | 179 | // Include the pieces autogenerated from the target description. |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 180 | #include "PPCGenDAGISel.inc" |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 181 | |
Chris Lattner | bd937b9 | 2005-10-06 18:45:51 +0000 | [diff] [blame] | 182 | private: |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 183 | SDNode *SelectSETCC(SDNode *N); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 184 | }; |
| 185 | } |
| 186 | |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 187 | /// InsertVRSaveCode - Once the entire function has been instruction selected, |
| 188 | /// all virtual registers are created and all machine instructions are built, |
| 189 | /// check to see if we need to save/restore VRSAVE. If so, do it. |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 190 | void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) { |
Chris Lattner | 1877ec9 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 191 | // Check to see if this function uses vector registers, which means we have to |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 192 | // save and restore the VRSAVE register and update it with the regs we use. |
Chris Lattner | 1877ec9 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 193 | // |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 194 | // In this case, there will be virtual registers of vector type created |
Chris Lattner | 1877ec9 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 195 | // by the scheduler. Detect them now. |
Chris Lattner | 1877ec9 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 196 | bool HasVectorVReg = false; |
Jakob Stoklund Olesen | b258135 | 2011-01-08 23:11:11 +0000 | [diff] [blame] | 197 | for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) { |
| 198 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 199 | if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) { |
Chris Lattner | 1877ec9 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 200 | HasVectorVReg = true; |
| 201 | break; |
| 202 | } |
Jakob Stoklund Olesen | b258135 | 2011-01-08 23:11:11 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 204 | if (!HasVectorVReg) return; // nothing to do. |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 1877ec9 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 206 | // If we have a vector register, we want to emit code into the entry and exit |
| 207 | // blocks to save and restore the VRSAVE register. We do this here (instead |
| 208 | // of marking all vector instructions as clobbering VRSAVE) for two reasons: |
| 209 | // |
| 210 | // 1. This (trivially) reduces the load on the register allocator, by not |
| 211 | // having to represent the live range of the VRSAVE register. |
| 212 | // 2. This (more significantly) allows us to create a temporary virtual |
| 213 | // register to hold the saved VRSAVE value, allowing this temporary to be |
| 214 | // register allocated, instead of forcing it to be spilled to the stack. |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 215 | |
| 216 | // Create two vregs - one to hold the VRSAVE register that is live-in to the |
| 217 | // function and one for the value after having bits or'd into it. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 218 | unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
| 219 | unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 220 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 221 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 222 | MachineBasicBlock &EntryBB = *Fn.begin(); |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 223 | DebugLoc dl; |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 224 | // Emit the following code into the entry block: |
| 225 | // InVRSAVE = MFVRSAVE |
| 226 | // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE |
| 227 | // MTVRSAVE UpdatedVRSAVE |
| 228 | MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 229 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE); |
| 230 | BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE), |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 231 | UpdatedVRSAVE).addReg(InVRSAVE); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 232 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 234 | // Find all return blocks, outputting a restore in each epilog. |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 235 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 236 | if (!BB->empty() && BB->back().isReturn()) { |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 237 | IP = BB->end(); --IP; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 239 | // Skip over all terminator instructions, which are part of the return |
| 240 | // sequence. |
| 241 | MachineBasicBlock::iterator I2 = IP; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 242 | while (I2 != BB->begin() && (--I2)->isTerminator()) |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 243 | IP = I2; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 245 | // Emit: MTVRSAVE InVRSave |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 246 | BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 247 | } |
Chris Lattner | 1877ec9 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | bd937b9 | 2005-10-06 18:45:51 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 6cd40d5 | 2005-09-03 01:17:22 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 4bb1895 | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 252 | /// getGlobalBaseReg - Output the instructions required to put the |
| 253 | /// base address to use for accessing globals into a register. |
| 254 | /// |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 255 | SDNode *PPCDAGToDAGISel::getGlobalBaseReg() { |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 256 | if (!GlobalBaseReg) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 257 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 258 | // Insert the set of GlobalBaseReg into the first MBB of the function |
Dan Gohman | bd51c67 | 2009-08-15 02:07:36 +0000 | [diff] [blame] | 259 | MachineBasicBlock &FirstMBB = MF->front(); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 260 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 261 | DebugLoc dl; |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 262 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 263 | if (PPCLowering.getPointerTy() == MVT::i32) { |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 264 | GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
Cameron Zwarich | 0113e4e | 2011-05-19 02:56:28 +0000 | [diff] [blame] | 265 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 266 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
Chris Lattner | d104342 | 2006-11-14 18:43:11 +0000 | [diff] [blame] | 267 | } else { |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 268 | GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RCRegClass); |
Cameron Zwarich | 0113e4e | 2011-05-19 02:56:28 +0000 | [diff] [blame] | 269 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8)); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 270 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg); |
Chris Lattner | d104342 | 2006-11-14 18:43:11 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 272 | } |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 273 | return CurDAG->getRegister(GlobalBaseReg, |
| 274 | PPCLowering.getPointerTy()).getNode(); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /// isIntS16Immediate - This method tests to see if the node is either a 32-bit |
| 278 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 279 | /// sign extension from a 16-bit value. If so, this returns true and the |
| 280 | /// immediate. |
| 281 | static bool isIntS16Immediate(SDNode *N, short &Imm) { |
| 282 | if (N->getOpcode() != ISD::Constant) |
| 283 | return false; |
| 284 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 285 | Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 286 | if (N->getValueType(0) == MVT::i32) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 287 | return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 288 | else |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 289 | return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 292 | static bool isIntS16Immediate(SDValue Op, short &Imm) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 293 | return isIntS16Immediate(Op.getNode(), Imm); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 297 | /// isInt32Immediate - This method tests to see if the node is a 32-bit constant |
| 298 | /// operand. If so Imm will receive the 32-bit value. |
| 299 | static bool isInt32Immediate(SDNode *N, unsigned &Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 300 | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 301 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 302 | return true; |
| 303 | } |
| 304 | return false; |
| 305 | } |
| 306 | |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 307 | /// isInt64Immediate - This method tests to see if the node is a 64-bit constant |
| 308 | /// operand. If so Imm will receive the 64-bit value. |
| 309 | static bool isInt64Immediate(SDNode *N, uint64_t &Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 310 | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 311 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | // isInt32Immediate - This method tests to see if a constant operand. |
| 318 | // If so Imm will receive the 32 bit value. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 319 | static bool isInt32Immediate(SDValue N, unsigned &Imm) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 320 | return isInt32Immediate(N.getNode(), Imm); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
| 324 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 325 | // opcode and that it has a immediate integer right operand. |
| 326 | // If so Imm will receive the 32 bit value. |
| 327 | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 328 | return N->getOpcode() == Opc |
| 329 | && isInt32Immediate(N->getOperand(1).getNode(), Imm); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 332 | bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 333 | if (isShiftedMask_32(Val)) { |
| 334 | // look for the first non-zero bit |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 335 | MB = countLeadingZeros(Val); |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 336 | // look for the first zero bit after the run of ones |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 337 | ME = countLeadingZeros((Val - 1) ^ Val); |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 338 | return true; |
Chris Lattner | 2fe76e5 | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 339 | } else { |
| 340 | Val = ~Val; // invert mask |
| 341 | if (isShiftedMask_32(Val)) { |
| 342 | // effectively look for the first zero bit |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 343 | ME = countLeadingZeros(Val) - 1; |
Chris Lattner | 2fe76e5 | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 344 | // effectively look for the first one bit after the run of zeros |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 345 | MB = countLeadingZeros((Val - 1) ^ Val) + 1; |
Chris Lattner | 2fe76e5 | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 346 | return true; |
| 347 | } |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 348 | } |
| 349 | // no run present |
| 350 | return false; |
| 351 | } |
| 352 | |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 353 | bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, |
| 354 | bool isShiftMask, unsigned &SH, |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 355 | unsigned &MB, unsigned &ME) { |
Nate Begeman | da32c9e | 2005-10-19 00:05:37 +0000 | [diff] [blame] | 356 | // Don't even go down this path for i64, since different logic will be |
| 357 | // necessary for rldicl/rldicr/rldimi. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 358 | if (N->getValueType(0) != MVT::i32) |
Nate Begeman | da32c9e | 2005-10-19 00:05:37 +0000 | [diff] [blame] | 359 | return false; |
| 360 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 361 | unsigned Shift = 32; |
| 362 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 363 | unsigned Opcode = N->getOpcode(); |
Chris Lattner | 1505573 | 2005-08-30 00:59:16 +0000 | [diff] [blame] | 364 | if (N->getNumOperands() != 2 || |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 365 | !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 366 | return false; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 367 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 368 | if (Opcode == ISD::SHL) { |
| 369 | // apply shift left to mask if it comes first |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 370 | if (isShiftMask) Mask = Mask << Shift; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 371 | // determine which bits are made indeterminant by shift |
| 372 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 373 | } else if (Opcode == ISD::SRL) { |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 374 | // apply shift right to mask if it comes first |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 375 | if (isShiftMask) Mask = Mask >> Shift; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 376 | // determine which bits are made indeterminant by shift |
| 377 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 378 | // adjust for the left rotate |
| 379 | Shift = 32 - Shift; |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 380 | } else if (Opcode == ISD::ROTL) { |
| 381 | Indeterminant = 0; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 382 | } else { |
| 383 | return false; |
| 384 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 385 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 386 | // if the mask doesn't intersect any Indeterminant bits |
| 387 | if (Mask && !(Mask & Indeterminant)) { |
Chris Lattner | 0949ed5 | 2006-05-12 16:29:37 +0000 | [diff] [blame] | 388 | SH = Shift & 31; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 389 | // make sure the mask is still a mask (wrap arounds may not be) |
| 390 | return isRunOfOnes(Mask, MB, ME); |
| 391 | } |
| 392 | return false; |
| 393 | } |
| 394 | |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 395 | /// SelectBitfieldInsert - turn an or of two masked values into |
| 396 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 397 | SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 398 | SDValue Op0 = N->getOperand(0); |
| 399 | SDValue Op1 = N->getOperand(1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 400 | SDLoc dl(N); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 401 | |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 402 | APInt LKZ, LKO, RKZ, RKO; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 403 | CurDAG->ComputeMaskedBits(Op0, LKZ, LKO); |
| 404 | CurDAG->ComputeMaskedBits(Op1, RKZ, RKO); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 405 | |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 406 | unsigned TargetMask = LKZ.getZExtValue(); |
| 407 | unsigned InsertMask = RKZ.getZExtValue(); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 408 | |
Nate Begeman | 4667f2c | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 409 | if ((TargetMask | InsertMask) == 0xFFFFFFFF) { |
| 410 | unsigned Op0Opc = Op0.getOpcode(); |
| 411 | unsigned Op1Opc = Op1.getOpcode(); |
| 412 | unsigned Value, SH = 0; |
| 413 | TargetMask = ~TargetMask; |
| 414 | InsertMask = ~InsertMask; |
Nate Begeman | 77f361f | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 415 | |
Nate Begeman | 4667f2c | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 416 | // If the LHS has a foldable shift and the RHS does not, then swap it to the |
| 417 | // RHS so that we can fold the shift into the insert. |
Nate Begeman | 77f361f | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 418 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
| 419 | if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 420 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
| 421 | if (Op1.getOperand(0).getOpcode() != ISD::SHL && |
| 422 | Op1.getOperand(0).getOpcode() != ISD::SRL) { |
| 423 | std::swap(Op0, Op1); |
| 424 | std::swap(Op0Opc, Op1Opc); |
Nate Begeman | 4667f2c | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 425 | std::swap(TargetMask, InsertMask); |
Nate Begeman | 77f361f | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 426 | } |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 427 | } |
Nate Begeman | 4667f2c | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 428 | } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) { |
| 429 | if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL && |
| 430 | Op1.getOperand(0).getOpcode() != ISD::SRL) { |
| 431 | std::swap(Op0, Op1); |
| 432 | std::swap(Op0Opc, Op1Opc); |
| 433 | std::swap(TargetMask, InsertMask); |
| 434 | } |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 435 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 436 | |
Nate Begeman | 77f361f | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 437 | unsigned MB, ME; |
Chris Lattner | 0949ed5 | 2006-05-12 16:29:37 +0000 | [diff] [blame] | 438 | if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) { |
Dale Johannesen | 5ca1246 | 2009-11-20 22:16:40 +0000 | [diff] [blame] | 439 | SDValue Tmp1, Tmp2; |
Nate Begeman | 77f361f | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 440 | |
| 441 | if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) && |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 442 | isInt32Immediate(Op1.getOperand(1), Value)) { |
Nate Begeman | 77f361f | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 443 | Op1 = Op1.getOperand(0); |
| 444 | SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value; |
| 445 | } |
| 446 | if (Op1Opc == ISD::AND) { |
| 447 | unsigned SHOpc = Op1.getOperand(0).getOpcode(); |
| 448 | if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 449 | isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) { |
Nate Begeman | 77f361f | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 450 | Op1 = Op1.getOperand(0).getOperand(0); |
| 451 | SH = (SHOpc == ISD::SHL) ? Value : 32 - Value; |
| 452 | } else { |
| 453 | Op1 = Op1.getOperand(0); |
| 454 | } |
| 455 | } |
Dale Johannesen | 5ca1246 | 2009-11-20 22:16:40 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 0949ed5 | 2006-05-12 16:29:37 +0000 | [diff] [blame] | 457 | SH &= 31; |
Dale Johannesen | 5ca1246 | 2009-11-20 22:16:40 +0000 | [diff] [blame] | 458 | SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB), |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 459 | getI32Imm(ME) }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 460 | return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops); |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 461 | } |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 466 | /// SelectCC - Select a comparison of the specified values with the specified |
| 467 | /// condition code, returning the CR# of the expression. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 468 | SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 469 | ISD::CondCode CC, SDLoc dl) { |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 470 | // Always select the LHS. |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 471 | unsigned Opc; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 472 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 473 | if (LHS.getValueType() == MVT::i32) { |
Chris Lattner | 529c233 | 2006-06-27 00:10:13 +0000 | [diff] [blame] | 474 | unsigned Imm; |
Chris Lattner | 3836dbd | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 475 | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
| 476 | if (isInt32Immediate(RHS, Imm)) { |
| 477 | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 478 | if (isUInt<16>(Imm)) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 479 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
| 480 | getI32Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | 3836dbd | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 481 | // If this is a 16-bit signed immediate, fold it. |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 482 | if (isInt<16>((int)Imm)) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 483 | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
| 484 | getI32Imm(Imm & 0xFFFF)), 0); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 3836dbd | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 486 | // For non-equality comparisons, the default code would materialize the |
| 487 | // constant, then compare against it, like this: |
| 488 | // lis r2, 4660 |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 489 | // ori r2, r2, 22136 |
Chris Lattner | 3836dbd | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 490 | // cmpw cr0, r3, r2 |
| 491 | // Since we are just comparing for equality, we can emit this instead: |
| 492 | // xoris r0,r3,0x1234 |
| 493 | // cmplwi cr0,r0,0x5678 |
| 494 | // beq cr0,L6 |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 495 | SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS, |
| 496 | getI32Imm(Imm >> 16)), 0); |
| 497 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor, |
| 498 | getI32Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | 3836dbd | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 499 | } |
| 500 | Opc = PPC::CMPLW; |
| 501 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 502 | if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm)) |
Dan Gohman | 602b0c8 | 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 | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 505 | Opc = PPC::CMPLW; |
| 506 | } else { |
| 507 | short SImm; |
| 508 | if (isIntS16Immediate(RHS, SImm)) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 509 | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
| 510 | getI32Imm((int)SImm & 0xFFFF)), |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 511 | 0); |
| 512 | Opc = PPC::CMPW; |
| 513 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 514 | } else if (LHS.getValueType() == MVT::i64) { |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 515 | uint64_t Imm; |
Chris Lattner | 7117624 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 516 | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 517 | if (isInt64Immediate(RHS.getNode(), Imm)) { |
Chris Lattner | 7117624 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 518 | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 519 | if (isUInt<16>(Imm)) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 520 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
| 521 | getI32Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | 7117624 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 522 | // If this is a 16-bit signed immediate, fold it. |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 523 | if (isInt<16>(Imm)) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 524 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
| 525 | getI32Imm(Imm & 0xFFFF)), 0); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 7117624 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 527 | // For non-equality comparisons, the default code would materialize the |
| 528 | // constant, then compare against it, like this: |
| 529 | // lis r2, 4660 |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 530 | // ori r2, r2, 22136 |
Chris Lattner | 7117624 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 531 | // cmpd cr0, r3, r2 |
| 532 | // Since we are just comparing for equality, we can emit this instead: |
| 533 | // xoris r0,r3,0x1234 |
| 534 | // cmpldi cr0,r0,0x5678 |
| 535 | // beq cr0,L6 |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 536 | if (isUInt<32>(Imm)) { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 537 | SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS, |
| 538 | getI64Imm(Imm >> 16)), 0); |
| 539 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor, |
| 540 | getI64Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | 7117624 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | Opc = PPC::CMPLD; |
| 544 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 545 | if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm)) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 546 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
| 547 | getI64Imm(Imm & 0xFFFF)), 0); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 548 | Opc = PPC::CMPLD; |
| 549 | } else { |
| 550 | short SImm; |
| 551 | if (isIntS16Immediate(RHS, SImm)) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 552 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
| 553 | getI64Imm(SImm & 0xFFFF)), |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 554 | 0); |
| 555 | Opc = PPC::CMPD; |
| 556 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 557 | } else if (LHS.getValueType() == MVT::f32) { |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 558 | Opc = PPC::FCMPUS; |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 559 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 560 | assert(LHS.getValueType() == MVT::f64 && "Unknown vt!"); |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 561 | Opc = PPC::FCMPUD; |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 562 | } |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 563 | return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 566 | static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) { |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 567 | switch (CC) { |
Chris Lattner | 5d634ce | 2006-05-25 16:54:16 +0000 | [diff] [blame] | 568 | case ISD::SETUEQ: |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 569 | case ISD::SETONE: |
| 570 | case ISD::SETOLE: |
| 571 | case ISD::SETOGE: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 572 | llvm_unreachable("Should be lowered by legalize!"); |
| 573 | default: llvm_unreachable("Unknown condition!"); |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 574 | case ISD::SETOEQ: |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 575 | case ISD::SETEQ: return PPC::PRED_EQ; |
Chris Lattner | 5d634ce | 2006-05-25 16:54:16 +0000 | [diff] [blame] | 576 | case ISD::SETUNE: |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 577 | case ISD::SETNE: return PPC::PRED_NE; |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 578 | case ISD::SETOLT: |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 579 | case ISD::SETLT: return PPC::PRED_LT; |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 580 | case ISD::SETULE: |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 581 | case ISD::SETLE: return PPC::PRED_LE; |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 582 | case ISD::SETOGT: |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 583 | case ISD::SETGT: return PPC::PRED_GT; |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 584 | case ISD::SETUGE: |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 585 | case ISD::SETGE: return PPC::PRED_GE; |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 586 | case ISD::SETO: return PPC::PRED_NU; |
| 587 | case ISD::SETUO: return PPC::PRED_UN; |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 588 | // These two are invalid for floating point. Assume we have int. |
| 589 | case ISD::SETULT: return PPC::PRED_LT; |
| 590 | case ISD::SETUGT: return PPC::PRED_GT; |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 591 | } |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 594 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 595 | /// associated with the SetCC condition, and whether or not the field is |
| 596 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 597 | /// |
| 598 | /// If this returns with Other != -1, then the returned comparison is an or of |
| 599 | /// two simpler comparisons. In this case, Invert is guaranteed to be false. |
| 600 | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) { |
| 601 | Invert = false; |
| 602 | Other = -1; |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 603 | switch (CC) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 604 | default: llvm_unreachable("Unknown condition!"); |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 605 | case ISD::SETOLT: |
| 606 | case ISD::SETLT: return 0; // Bit #0 = SETOLT |
| 607 | case ISD::SETOGT: |
| 608 | case ISD::SETGT: return 1; // Bit #1 = SETOGT |
| 609 | case ISD::SETOEQ: |
| 610 | case ISD::SETEQ: return 2; // Bit #2 = SETOEQ |
| 611 | case ISD::SETUO: return 3; // Bit #3 = SETUO |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 612 | case ISD::SETUGE: |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 613 | case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 614 | case ISD::SETULE: |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 615 | case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE |
Chris Lattner | 8e2a04e | 2006-05-25 18:06:16 +0000 | [diff] [blame] | 616 | case ISD::SETUNE: |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 617 | case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE |
| 618 | case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 619 | case ISD::SETUEQ: |
| 620 | case ISD::SETOGE: |
| 621 | case ISD::SETOLE: |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 622 | case ISD::SETONE: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 623 | llvm_unreachable("Invalid branch code: should be expanded by legalize"); |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 624 | // These are invalid for floating point. Assume integer. |
| 625 | case ISD::SETULT: return 0; |
| 626 | case ISD::SETUGT: return 1; |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 627 | } |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 628 | } |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 629 | |
Adhemerval Zanella | 5f41fd6 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 630 | // getVCmpInst: return the vector compare instruction for the specified |
| 631 | // vector type and condition code. Since this is for altivec specific code, |
| 632 | // only support the altivec types (v16i8, v8i16, v4i32, and v4f32). |
| 633 | static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC) { |
| 634 | switch (CC) { |
| 635 | case ISD::SETEQ: |
| 636 | case ISD::SETUEQ: |
| 637 | case ISD::SETNE: |
| 638 | case ISD::SETUNE: |
| 639 | if (VecVT == MVT::v16i8) |
| 640 | return PPC::VCMPEQUB; |
| 641 | else if (VecVT == MVT::v8i16) |
| 642 | return PPC::VCMPEQUH; |
| 643 | else if (VecVT == MVT::v4i32) |
| 644 | return PPC::VCMPEQUW; |
| 645 | // v4f32 != v4f32 could be translate to unordered not equal |
| 646 | else if (VecVT == MVT::v4f32) |
| 647 | return PPC::VCMPEQFP; |
| 648 | break; |
| 649 | case ISD::SETLT: |
| 650 | case ISD::SETGT: |
| 651 | case ISD::SETLE: |
| 652 | case ISD::SETGE: |
| 653 | if (VecVT == MVT::v16i8) |
| 654 | return PPC::VCMPGTSB; |
| 655 | else if (VecVT == MVT::v8i16) |
| 656 | return PPC::VCMPGTSH; |
| 657 | else if (VecVT == MVT::v4i32) |
| 658 | return PPC::VCMPGTSW; |
| 659 | else if (VecVT == MVT::v4f32) |
| 660 | return PPC::VCMPGTFP; |
| 661 | break; |
| 662 | case ISD::SETULT: |
| 663 | case ISD::SETUGT: |
| 664 | case ISD::SETUGE: |
| 665 | case ISD::SETULE: |
| 666 | if (VecVT == MVT::v16i8) |
| 667 | return PPC::VCMPGTUB; |
| 668 | else if (VecVT == MVT::v8i16) |
| 669 | return PPC::VCMPGTUH; |
| 670 | else if (VecVT == MVT::v4i32) |
| 671 | return PPC::VCMPGTUW; |
| 672 | break; |
| 673 | case ISD::SETOEQ: |
| 674 | if (VecVT == MVT::v4f32) |
| 675 | return PPC::VCMPEQFP; |
| 676 | break; |
| 677 | case ISD::SETOLT: |
| 678 | case ISD::SETOGT: |
| 679 | case ISD::SETOLE: |
| 680 | if (VecVT == MVT::v4f32) |
| 681 | return PPC::VCMPGTFP; |
| 682 | break; |
| 683 | case ISD::SETOGE: |
| 684 | if (VecVT == MVT::v4f32) |
| 685 | return PPC::VCMPGEFP; |
| 686 | break; |
| 687 | default: |
| 688 | break; |
| 689 | } |
| 690 | llvm_unreachable("Invalid integer vector compare condition"); |
| 691 | } |
| 692 | |
| 693 | // getVCmpEQInst: return the equal compare instruction for the specified vector |
| 694 | // type. Since this is for altivec specific code, only support the altivec |
| 695 | // types (v16i8, v8i16, v4i32, and v4f32). |
| 696 | static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT) { |
| 697 | switch (VecVT) { |
| 698 | case MVT::v16i8: |
| 699 | return PPC::VCMPEQUB; |
| 700 | case MVT::v8i16: |
| 701 | return PPC::VCMPEQUH; |
| 702 | case MVT::v4i32: |
| 703 | return PPC::VCMPEQUW; |
| 704 | case MVT::v4f32: |
| 705 | return PPC::VCMPEQFP; |
| 706 | default: |
| 707 | llvm_unreachable("Invalid integer vector compare condition"); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 712 | SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 713 | SDLoc dl(N); |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 714 | unsigned Imm; |
| 715 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
Roman Divacky | 8e9d672 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 716 | EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy(); |
| 717 | bool isPPC64 = (PtrVT == MVT::i64); |
| 718 | |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 719 | if (isInt32Immediate(N->getOperand(1), Imm)) { |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 720 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 721 | // Check for those cases here. |
| 722 | // setcc op, 0 |
| 723 | if (Imm == 0) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 724 | SDValue Op = N->getOperand(0); |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 725 | switch (CC) { |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 726 | default: break; |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 727 | case ISD::SETEQ: { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 728 | Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 729 | SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 730 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 731 | } |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 732 | case ISD::SETNE: { |
Roman Divacky | 8e9d672 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 733 | if (isPPC64) break; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 734 | SDValue AD = |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 735 | SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 736 | Op, getI32Imm(~0U)), 0); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 737 | return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, |
Evan Cheng | 95514ba | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 738 | AD.getValue(1)); |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 739 | } |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 740 | case ISD::SETLT: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 741 | SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 742 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 743 | } |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 744 | case ISD::SETGT: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 745 | SDValue T = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 746 | SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0); |
| 747 | T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 748 | SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 749 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 750 | } |
| 751 | } |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 752 | } else if (Imm == ~0U) { // setcc op, -1 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 753 | SDValue Op = N->getOperand(0); |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 754 | switch (CC) { |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 755 | default: break; |
| 756 | case ISD::SETEQ: |
Roman Divacky | 8e9d672 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 757 | if (isPPC64) break; |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 758 | Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 759 | Op, getI32Imm(1)), 0); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 760 | return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
| 761 | SDValue(CurDAG->getMachineNode(PPC::LI, dl, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 762 | MVT::i32, |
| 763 | getI32Imm(0)), 0), |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 764 | Op.getValue(1)); |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 765 | case ISD::SETNE: { |
Roman Divacky | 8e9d672 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 766 | if (isPPC64) break; |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 767 | Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 768 | SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 769 | Op, getI32Imm(~0U)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 770 | return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 771 | Op, SDValue(AD, 1)); |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 772 | } |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 773 | case ISD::SETLT: { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 774 | SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op, |
| 775 | getI32Imm(1)), 0); |
| 776 | SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD, |
| 777 | Op), 0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 778 | SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 779 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 780 | } |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 781 | case ISD::SETGT: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 782 | SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 783 | Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 784 | 0); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 785 | return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, |
Evan Cheng | 95514ba | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 786 | getI32Imm(1)); |
Chris Lattner | dabb829 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 787 | } |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 788 | } |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 791 | |
Adhemerval Zanella | 1c7d69b | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 792 | SDValue LHS = N->getOperand(0); |
| 793 | SDValue RHS = N->getOperand(1); |
| 794 | |
Adhemerval Zanella | 5f41fd6 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 795 | // Altivec Vector compare instructions do not set any CR register by default and |
| 796 | // vector compare operations return the same type as the operands. |
Adhemerval Zanella | 1c7d69b | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 797 | if (LHS.getValueType().isVector()) { |
Adhemerval Zanella | 5f41fd6 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 798 | EVT VecVT = LHS.getValueType(); |
| 799 | MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy; |
| 800 | unsigned int VCmpInst = getVCmpInst(VT, CC); |
| 801 | |
| 802 | switch (CC) { |
| 803 | case ISD::SETEQ: |
| 804 | case ISD::SETOEQ: |
| 805 | case ISD::SETUEQ: |
| 806 | return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS); |
| 807 | case ISD::SETNE: |
| 808 | case ISD::SETONE: |
| 809 | case ISD::SETUNE: { |
| 810 | SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0); |
| 811 | return CurDAG->SelectNodeTo(N, PPC::VNOR, VecVT, VCmp, VCmp); |
| 812 | } |
| 813 | case ISD::SETLT: |
| 814 | case ISD::SETOLT: |
| 815 | case ISD::SETULT: |
| 816 | return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS); |
| 817 | case ISD::SETGT: |
| 818 | case ISD::SETOGT: |
| 819 | case ISD::SETUGT: |
| 820 | return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS); |
| 821 | case ISD::SETGE: |
| 822 | case ISD::SETOGE: |
| 823 | case ISD::SETUGE: { |
| 824 | // Small optimization: Altivec provides a 'Vector Compare Greater Than |
| 825 | // or Equal To' instruction (vcmpgefp), so in this case there is no |
| 826 | // need for extra logic for the equal compare. |
| 827 | if (VecVT.getSimpleVT().isFloatingPoint()) { |
| 828 | return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS); |
| 829 | } else { |
| 830 | SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0); |
| 831 | unsigned int VCmpEQInst = getVCmpEQInst(VT); |
| 832 | SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0); |
| 833 | return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpGT, VCmpEQ); |
| 834 | } |
| 835 | } |
| 836 | case ISD::SETLE: |
| 837 | case ISD::SETOLE: |
| 838 | case ISD::SETULE: { |
| 839 | SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0); |
| 840 | unsigned int VCmpEQInst = getVCmpEQInst(VT); |
| 841 | SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0); |
| 842 | return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpLE, VCmpEQ); |
| 843 | } |
| 844 | default: |
| 845 | llvm_unreachable("Invalid vector compare type: should be expanded by legalize"); |
| 846 | } |
Adhemerval Zanella | 1c7d69b | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 849 | bool Inv; |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 850 | int OtherCondIdx; |
| 851 | unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx); |
Adhemerval Zanella | 1c7d69b | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 852 | SDValue CCReg = SelectCC(LHS, RHS, CC, dl); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 853 | SDValue IntCR; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 855 | // Force the ccreg into CR7. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 856 | SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 857 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 858 | SDValue InFlag(0, 0); // Null incoming flag value. |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 859 | CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, |
Chris Lattner | db1cb2b | 2005-12-01 03:50:19 +0000 | [diff] [blame] | 860 | InFlag).getValue(1); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 861 | |
Hal Finkel | bd5cafd | 2012-06-11 19:57:01 +0000 | [diff] [blame] | 862 | if (PPCSubTarget.hasMFOCRF() && OtherCondIdx == -1) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 863 | IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg, |
| 864 | CCReg), 0); |
Adhemerval Zanella | 1c7d69b | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 865 | else |
Dale Johannesen | 5f07d52 | 2010-05-20 17:48:26 +0000 | [diff] [blame] | 866 | IntCR = SDValue(CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32, |
| 867 | CR7Reg, CCReg), 0); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 868 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 869 | SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31), |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 870 | getI32Imm(31), getI32Imm(31) }; |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 871 | if (OtherCondIdx == -1 && !Inv) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 872 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 873 | |
| 874 | // Get the specified bit. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 875 | SDValue Tmp = |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 876 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 877 | if (Inv) { |
| 878 | assert(OtherCondIdx == -1 && "Can't have split plus negation"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 879 | return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 880 | } |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 881 | |
| 882 | // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT. |
| 883 | // We already got the bit for the first part of the comparison (e.g. SETULE). |
| 884 | |
| 885 | // Get the other bit of the comparison. |
| 886 | Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 887 | SDValue OtherCond = |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 888 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 889 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 890 | return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond); |
Chris Lattner | 222adac | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 891 | } |
Chris Lattner | 2b63e4c | 2005-10-06 18:56:10 +0000 | [diff] [blame] | 892 | |
Chris Lattner | 6a16f6a | 2005-10-06 19:07:45 +0000 | [diff] [blame] | 893 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 894 | // Select - Convert the specified operand from a target-independent to a |
| 895 | // target-specific node if it hasn't already been changed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 896 | SDNode *PPCDAGToDAGISel::Select(SDNode *N) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 897 | SDLoc dl(N); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 898 | if (N->isMachineOpcode()) |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 899 | return NULL; // Already selected. |
Chris Lattner | d3d2cf5 | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 900 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 901 | switch (N->getOpcode()) { |
Chris Lattner | 19c0907 | 2005-09-07 23:45:15 +0000 | [diff] [blame] | 902 | default: break; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 903 | |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 904 | case ISD::Constant: { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 905 | if (N->getValueType(0) == MVT::i64) { |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 906 | // Get 64 bit value. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 907 | int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 908 | // Assume no remaining bits. |
| 909 | unsigned Remainder = 0; |
| 910 | // Assume no shift required. |
| 911 | unsigned Shift = 0; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 912 | |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 913 | // If it can't be represented as a 32 bit value. |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 914 | if (!isInt<32>(Imm)) { |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 915 | Shift = countTrailingZeros<uint64_t>(Imm); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 916 | int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 917 | |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 918 | // If the shifted value fits 32 bits. |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 919 | if (isInt<32>(ImmSh)) { |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 920 | // Go with the shifted value. |
| 921 | Imm = ImmSh; |
| 922 | } else { |
| 923 | // Still stuck with a 64 bit value. |
| 924 | Remainder = Imm; |
| 925 | Shift = 32; |
| 926 | Imm >>= 32; |
| 927 | } |
| 928 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 929 | |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 930 | // Intermediate operand. |
| 931 | SDNode *Result; |
| 932 | |
| 933 | // Handle first 32 bits. |
| 934 | unsigned Lo = Imm & 0xFFFF; |
| 935 | unsigned Hi = (Imm >> 16) & 0xFFFF; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 936 | |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 937 | // Simple value. |
Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 938 | if (isInt<16>(Imm)) { |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 939 | // Just the Lo bits. |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 940 | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo)); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 941 | } else if (Lo) { |
| 942 | // Handle the Hi bits. |
| 943 | unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8; |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 944 | Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi)); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 945 | // And Lo bits. |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 946 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
| 947 | SDValue(Result, 0), getI32Imm(Lo)); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 948 | } else { |
| 949 | // Just the Hi bits. |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 950 | Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi)); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 951 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 952 | |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 953 | // If no shift, we're done. |
| 954 | if (!Shift) return Result; |
| 955 | |
| 956 | // Shift for next step if the upper 32-bits were not zero. |
| 957 | if (Imm) { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 958 | Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, |
| 959 | SDValue(Result, 0), |
| 960 | getI32Imm(Shift), |
| 961 | getI32Imm(63 - Shift)); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // Add in the last bits as required. |
| 965 | if ((Hi = (Remainder >> 16) & 0xFFFF)) { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 966 | Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64, |
| 967 | SDValue(Result, 0), getI32Imm(Hi)); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 968 | } |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 969 | if ((Lo = Remainder & 0xFFFF)) { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 970 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
| 971 | SDValue(Result, 0), getI32Imm(Lo)); |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 972 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 973 | |
Jim Laskey | 78f97f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 974 | return Result; |
| 975 | } |
| 976 | break; |
| 977 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 978 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 979 | case ISD::SETCC: |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 980 | return SelectSETCC(N); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 981 | case PPCISD::GlobalBaseReg: |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 982 | return getGlobalBaseReg(); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 983 | |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 984 | case ISD::FrameIndex: { |
| 985 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 986 | SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0)); |
| 987 | unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8; |
Chris Lattner | ccbe2ec | 2006-08-15 23:48:22 +0000 | [diff] [blame] | 988 | if (N->hasOneUse()) |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 989 | return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI, |
Evan Cheng | 95514ba | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 990 | getSmallIPtrImm(0)); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 991 | return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 992 | getSmallIPtrImm(0)); |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 993 | } |
Chris Lattner | 6d92cad | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 994 | |
| 995 | case PPCISD::MFCR: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 996 | SDValue InFlag = N->getOperand(1); |
Chris Lattner | 6d92cad | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 997 | // Use MFOCRF if supported. |
Hal Finkel | bd5cafd | 2012-06-11 19:57:01 +0000 | [diff] [blame] | 998 | if (PPCSubTarget.hasMFOCRF()) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 999 | return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, |
| 1000 | N->getOperand(0), InFlag); |
Chris Lattner | 6d92cad | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 1001 | else |
Dale Johannesen | 5f07d52 | 2010-05-20 17:48:26 +0000 | [diff] [blame] | 1002 | return CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32, |
| 1003 | N->getOperand(0), InFlag); |
Chris Lattner | 6d92cad | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 1004 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | 88add10 | 2005-09-28 22:50:24 +0000 | [diff] [blame] | 1006 | case ISD::SDIV: { |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1007 | // FIXME: since this depends on the setting of the carry flag from the srawi |
| 1008 | // we should really be making notes about that for the scheduler. |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1009 | // FIXME: It sure would be nice if we could cheaply recognize the |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1010 | // srl/add/sra pattern the dag combiner will generate for this as |
| 1011 | // sra/addze rather than having to handle sdiv ourselves. oh well. |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1012 | unsigned Imm; |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1013 | if (isInt32Immediate(N->getOperand(1), Imm)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1014 | SDValue N0 = N->getOperand(0); |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1015 | if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1016 | SDNode *Op = |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1017 | CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1018 | N0, getI32Imm(Log2_32(Imm))); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1019 | return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1020 | SDValue(Op, 0), SDValue(Op, 1)); |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1021 | } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1022 | SDNode *Op = |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1023 | CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1024 | N0, getI32Imm(Log2_32(-Imm))); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1025 | SDValue PT = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1026 | SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32, |
| 1027 | SDValue(Op, 0), SDValue(Op, 1)), |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1028 | 0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1029 | return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT); |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | 237733e | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 1033 | // Other cases are autogenerated. |
| 1034 | break; |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 1035 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1037 | case ISD::LOAD: { |
| 1038 | // Handle preincrement loads. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1039 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1040 | EVT LoadedVT = LD->getMemoryVT(); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1041 | |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1042 | // Normal loads are handled by code generated from the .td file. |
| 1043 | if (LD->getAddressingMode() != ISD::PRE_INC) |
| 1044 | break; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1045 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1046 | SDValue Offset = LD->getOffset(); |
Ulrich Weigand | 0301e79 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 1047 | if (Offset.getOpcode() == ISD::TargetConstant || |
Chris Lattner | 5b3bbc7 | 2006-11-11 04:53:30 +0000 | [diff] [blame] | 1048 | Offset.getOpcode() == ISD::TargetGlobalAddress) { |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1050 | unsigned Opcode; |
| 1051 | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1052 | if (LD->getValueType(0) != MVT::i64) { |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1053 | // Handle PPC32 integer and normal FP loads. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1054 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
| 1055 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1056 | default: llvm_unreachable("Invalid PPC load type!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1057 | case MVT::f64: Opcode = PPC::LFDU; break; |
| 1058 | case MVT::f32: Opcode = PPC::LFSU; break; |
| 1059 | case MVT::i32: Opcode = PPC::LWZU; break; |
| 1060 | case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break; |
| 1061 | case MVT::i1: |
| 1062 | case MVT::i8: Opcode = PPC::LBZU; break; |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1065 | assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); |
| 1066 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
| 1067 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1068 | default: llvm_unreachable("Invalid PPC load type!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1069 | case MVT::i64: Opcode = PPC::LDU; break; |
| 1070 | case MVT::i32: Opcode = PPC::LWZU8; break; |
| 1071 | case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break; |
| 1072 | case MVT::i1: |
| 1073 | case MVT::i8: Opcode = PPC::LBZU8; break; |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1076 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1077 | SDValue Chain = LD->getChain(); |
| 1078 | SDValue Base = LD->getBasePtr(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1079 | SDValue Ops[] = { Offset, Base, Chain }; |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1080 | return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0), |
| 1081 | PPCLowering.getPointerTy(), |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1082 | MVT::Other, Ops); |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1083 | } else { |
Hal Finkel | 0fcdd8b | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 1084 | unsigned Opcode; |
| 1085 | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
| 1086 | if (LD->getValueType(0) != MVT::i64) { |
| 1087 | // Handle PPC32 integer and normal FP loads. |
| 1088 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
| 1089 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
| 1090 | default: llvm_unreachable("Invalid PPC load type!"); |
| 1091 | case MVT::f64: Opcode = PPC::LFDUX; break; |
| 1092 | case MVT::f32: Opcode = PPC::LFSUX; break; |
| 1093 | case MVT::i32: Opcode = PPC::LWZUX; break; |
| 1094 | case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break; |
| 1095 | case MVT::i1: |
| 1096 | case MVT::i8: Opcode = PPC::LBZUX; break; |
| 1097 | } |
| 1098 | } else { |
| 1099 | assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); |
| 1100 | assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) && |
| 1101 | "Invalid sext update load"); |
| 1102 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
| 1103 | default: llvm_unreachable("Invalid PPC load type!"); |
| 1104 | case MVT::i64: Opcode = PPC::LDUX; break; |
| 1105 | case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break; |
| 1106 | case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break; |
| 1107 | case MVT::i1: |
| 1108 | case MVT::i8: Opcode = PPC::LBZUX8; break; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | SDValue Chain = LD->getChain(); |
| 1113 | SDValue Base = LD->getBasePtr(); |
Ulrich Weigand | 881a715 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 1114 | SDValue Ops[] = { Base, Offset, Chain }; |
Hal Finkel | 0fcdd8b | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 1115 | return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0), |
| 1116 | PPCLowering.getPointerTy(), |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1117 | MVT::Other, Ops); |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1120 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 1121 | case ISD::AND: { |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1122 | unsigned Imm, Imm2, SH, MB, ME; |
Hal Finkel | 97d047d | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 1123 | uint64_t Imm64; |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1124 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 1125 | // If this is an and of a value rotated between 0 and 31 bits and then and'd |
| 1126 | // with a mask, emit rlwinm |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1127 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1128 | isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1129 | SDValue Val = N->getOperand(0).getOperand(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1130 | SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1131 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 1132 | } |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1133 | // If this is just a masked value where the input is not handled above, and |
| 1134 | // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm |
| 1135 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1136 | isRunOfOnes(Imm, MB, ME) && |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1137 | N->getOperand(0).getOpcode() != ISD::ROTL) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1138 | SDValue Val = N->getOperand(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1139 | SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1140 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1141 | } |
Hal Finkel | 97d047d | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 1142 | // If this is a 64-bit zero-extension mask, emit rldicl. |
| 1143 | if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) && |
| 1144 | isMask_64(Imm64)) { |
| 1145 | SDValue Val = N->getOperand(0); |
| 1146 | MB = 64 - CountTrailingOnes_64(Imm64); |
| 1147 | SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB) }; |
| 1148 | return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3); |
| 1149 | } |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1150 | // AND X, 0 -> 0, not "rlwinm 32". |
| 1151 | if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1152 | ReplaceUses(SDValue(N, 0), N->getOperand(1)); |
Nate Begeman | f42f133 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 1153 | return NULL; |
| 1154 | } |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 1155 | // ISD::OR doesn't get all the bitfield insertion fun. |
| 1156 | // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1157 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 1158 | N->getOperand(0).getOpcode() == ISD::OR && |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1159 | isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) { |
Chris Lattner | c9a5ef5 | 2006-01-05 18:32:49 +0000 | [diff] [blame] | 1160 | unsigned MB, ME; |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 1161 | Imm = ~(Imm^Imm2); |
| 1162 | if (isRunOfOnes(Imm, MB, ME)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1163 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1164 | N->getOperand(0).getOperand(1), |
| 1165 | getI32Imm(0), getI32Imm(MB),getI32Imm(ME) }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1166 | return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops); |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | 237733e | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 1170 | // Other cases are autogenerated. |
| 1171 | break; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 1172 | } |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 1173 | case ISD::OR: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1174 | if (N->getValueType(0) == MVT::i32) |
Chris Lattner | ccbe2ec | 2006-08-15 23:48:22 +0000 | [diff] [blame] | 1175 | if (SDNode *I = SelectBitfieldInsert(N)) |
| 1176 | return I; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | 237733e | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 1178 | // Other cases are autogenerated. |
| 1179 | break; |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1180 | case ISD::SHL: { |
| 1181 | unsigned Imm, SH, MB, ME; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1182 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Nate Begeman | 2d5aff7 | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 1183 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1184 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1185 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1186 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Nate Begeman | 8d94832 | 2005-10-19 01:12:32 +0000 | [diff] [blame] | 1187 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1188 | |
Nate Begeman | 2d5aff7 | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 1189 | // Other cases are autogenerated. |
| 1190 | break; |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1191 | } |
| 1192 | case ISD::SRL: { |
| 1193 | unsigned Imm, SH, MB, ME; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1194 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1195 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1196 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1197 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1198 | return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4); |
Nate Begeman | 8d94832 | 2005-10-19 01:12:32 +0000 | [diff] [blame] | 1199 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1200 | |
Nate Begeman | 2d5aff7 | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 1201 | // Other cases are autogenerated. |
| 1202 | break; |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1203 | } |
Chris Lattner | 13794f5 | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1204 | case ISD::SELECT_CC: { |
| 1205 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); |
Roman Divacky | 8e9d672 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 1206 | EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy(); |
| 1207 | bool isPPC64 = (PtrVT == MVT::i64); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1209 | // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
Roman Divacky | 8e9d672 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 1210 | if (!isPPC64) |
| 1211 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 1212 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 1213 | if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 1214 | if (N1C->isNullValue() && N3C->isNullValue() && |
| 1215 | N2C->getZExtValue() == 1ULL && CC == ISD::SETNE && |
| 1216 | // FIXME: Implement this optzn for PPC64. |
| 1217 | N->getValueType(0) == MVT::i32) { |
| 1218 | SDNode *Tmp = |
| 1219 | CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
| 1220 | N->getOperand(0), getI32Imm(~0U)); |
| 1221 | return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, |
| 1222 | SDValue(Tmp, 0), N->getOperand(0), |
| 1223 | SDValue(Tmp, 1)); |
| 1224 | } |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1225 | |
Dale Johannesen | f5f5dce | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1226 | SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 1227 | unsigned BROpc = getPredicateForSetCC(CC); |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1229 | unsigned SelectCCOp; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1230 | if (N->getValueType(0) == MVT::i32) |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1231 | SelectCCOp = PPC::SELECT_CC_I4; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1232 | else if (N->getValueType(0) == MVT::i64) |
Chris Lattner | c08f902 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 1233 | SelectCCOp = PPC::SELECT_CC_I8; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1234 | else if (N->getValueType(0) == MVT::f32) |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1235 | SelectCCOp = PPC::SELECT_CC_F4; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1236 | else if (N->getValueType(0) == MVT::f64) |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1237 | SelectCCOp = PPC::SELECT_CC_F8; |
Chris Lattner | 710ff32 | 2006-04-08 22:45:08 +0000 | [diff] [blame] | 1238 | else |
| 1239 | SelectCCOp = PPC::SELECT_CC_VRRC; |
| 1240 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1241 | SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3), |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1242 | getI32Imm(BROpc) }; |
| 1243 | return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4); |
Chris Lattner | 13794f5 | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1244 | } |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 1245 | case PPCISD::BDNZ: |
| 1246 | case PPCISD::BDZ: { |
| 1247 | bool IsPPC64 = PPCSubTarget.isPPC64(); |
| 1248 | SDValue Ops[] = { N->getOperand(1), N->getOperand(0) }; |
| 1249 | return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ? |
| 1250 | (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 1251 | (IsPPC64 ? PPC::BDZ8 : PPC::BDZ), |
| 1252 | MVT::Other, Ops, 2); |
| 1253 | } |
Chris Lattner | 18258c6 | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1254 | case PPCISD::COND_BRANCH: { |
Dan Gohman | cbb7ab2 | 2008-11-05 17:16:24 +0000 | [diff] [blame] | 1255 | // Op #0 is the Chain. |
Chris Lattner | 18258c6 | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1256 | // Op #1 is the PPC::PRED_* number. |
| 1257 | // Op #2 is the CR# |
| 1258 | // Op #3 is the Dest MBB |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1259 | // Op #4 is the Flag. |
Evan Cheng | 2bda17c | 2007-06-29 01:25:06 +0000 | [diff] [blame] | 1260 | // Prevent PPC::PRED_* from being selected into LI. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1261 | SDValue Pred = |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1262 | getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1263 | SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3), |
Chris Lattner | 18258c6 | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1264 | N->getOperand(0), N->getOperand(4) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1265 | return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5); |
Chris Lattner | 18258c6 | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 1266 | } |
Nate Begeman | 81e8097 | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 1267 | case ISD::BR_CC: { |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1268 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
Dale Johannesen | f5f5dce | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1269 | SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1270 | SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode, |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1271 | N->getOperand(4), N->getOperand(0) }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1272 | return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1273 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1274 | case ISD::BRIND: { |
Chris Lattner | cf00631 | 2006-06-10 01:15:02 +0000 | [diff] [blame] | 1275 | // FIXME: Should custom lower this. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1276 | SDValue Chain = N->getOperand(0); |
| 1277 | SDValue Target = N->getOperand(1); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1278 | unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8; |
Roman Divacky | 0c9b559 | 2011-06-03 15:47:49 +0000 | [diff] [blame] | 1279 | unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8; |
Hal Finkel | 6772452 | 2011-12-08 04:36:44 +0000 | [diff] [blame] | 1280 | Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1281 | Chain), 0); |
Roman Divacky | 0c9b559 | 2011-06-03 15:47:49 +0000 | [diff] [blame] | 1282 | return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1283 | } |
Bill Schmidt | 34a9d4b | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1284 | case PPCISD::TOC_ENTRY: { |
| 1285 | assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI"); |
| 1286 | |
Bill Schmidt | 53b0b0e | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 1287 | // For medium and large code model, we generate two instructions as |
| 1288 | // described below. Otherwise we allow SelectCodeCommon to handle this, |
| 1289 | // selecting one of LDtoc, LDtocJTI, and LDtocCPT. |
| 1290 | CodeModel::Model CModel = TM.getCodeModel(); |
| 1291 | if (CModel != CodeModel::Medium && CModel != CodeModel::Large) |
Bill Schmidt | 34a9d4b | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | |
| 1294 | // The first source operand is a TargetGlobalAddress or a |
| 1295 | // TargetJumpTable. If it is an externally defined symbol, a symbol |
| 1296 | // with common linkage, a function address, or a jump table address, |
Bill Schmidt | 53b0b0e | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 1297 | // or if we are generating code for large code model, we generate: |
Bill Schmidt | 34a9d4b | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1298 | // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>)) |
| 1299 | // Otherwise we generate: |
| 1300 | // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>) |
| 1301 | SDValue GA = N->getOperand(0); |
| 1302 | SDValue TOCbase = N->getOperand(1); |
| 1303 | SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64, |
| 1304 | TOCbase, GA); |
| 1305 | |
Bill Schmidt | 53b0b0e | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 1306 | if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large) |
Bill Schmidt | 34a9d4b | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1307 | return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA, |
| 1308 | SDValue(Tmp, 0)); |
| 1309 | |
| 1310 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) { |
| 1311 | const GlobalValue *GValue = G->getGlobal(); |
Bill Schmidt | 5b7f921 | 2013-01-07 19:29:18 +0000 | [diff] [blame] | 1312 | const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue); |
| 1313 | const GlobalValue *RealGValue = GAlias ? |
| 1314 | GAlias->resolveAliasedGlobal(false) : GValue; |
| 1315 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue); |
| 1316 | assert((GVar || isa<Function>(RealGValue)) && |
Bill Schmidt | 34a9d4b | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1317 | "Unexpected global value subclass!"); |
| 1318 | |
| 1319 | // An external variable is one without an initializer. For these, |
| 1320 | // for variables with common linkage, and for Functions, generate |
| 1321 | // the LDtocL form. |
Bill Schmidt | 5b7f921 | 2013-01-07 19:29:18 +0000 | [diff] [blame] | 1322 | if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() || |
| 1323 | RealGValue->hasAvailableExternallyLinkage()) |
Bill Schmidt | 34a9d4b | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 1324 | return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA, |
| 1325 | SDValue(Tmp, 0)); |
| 1326 | } |
| 1327 | |
| 1328 | return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64, |
| 1329 | SDValue(Tmp, 0), GA); |
| 1330 | } |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1331 | case PPCISD::VADD_SPLAT: { |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1332 | // This expands into one of three sequences, depending on whether |
| 1333 | // the first operand is odd or even, positive or negative. |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1334 | assert(isa<ConstantSDNode>(N->getOperand(0)) && |
| 1335 | isa<ConstantSDNode>(N->getOperand(1)) && |
| 1336 | "Invalid operand on VADD_SPLAT!"); |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1337 | |
| 1338 | int Elt = N->getConstantOperandVal(0); |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1339 | int EltSize = N->getConstantOperandVal(1); |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1340 | unsigned Opc1, Opc2, Opc3; |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1341 | EVT VT; |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1342 | |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1343 | if (EltSize == 1) { |
| 1344 | Opc1 = PPC::VSPLTISB; |
| 1345 | Opc2 = PPC::VADDUBM; |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1346 | Opc3 = PPC::VSUBUBM; |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1347 | VT = MVT::v16i8; |
| 1348 | } else if (EltSize == 2) { |
| 1349 | Opc1 = PPC::VSPLTISH; |
| 1350 | Opc2 = PPC::VADDUHM; |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1351 | Opc3 = PPC::VSUBUHM; |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1352 | VT = MVT::v8i16; |
| 1353 | } else { |
| 1354 | assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!"); |
| 1355 | Opc1 = PPC::VSPLTISW; |
| 1356 | Opc2 = PPC::VADDUWM; |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1357 | Opc3 = PPC::VSUBUWM; |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1358 | VT = MVT::v4i32; |
| 1359 | } |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 1360 | |
| 1361 | if ((Elt & 1) == 0) { |
| 1362 | // Elt is even, in the range [-32,-18] + [16,30]. |
| 1363 | // |
| 1364 | // Convert: VADD_SPLAT elt, size |
| 1365 | // Into: tmp = VSPLTIS[BHW] elt |
| 1366 | // VADDU[BHW]M tmp, tmp |
| 1367 | // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4 |
| 1368 | SDValue EltVal = getI32Imm(Elt >> 1); |
| 1369 | SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1370 | SDValue TmpVal = SDValue(Tmp, 0); |
| 1371 | return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal); |
| 1372 | |
| 1373 | } else if (Elt > 0) { |
| 1374 | // Elt is odd and positive, in the range [17,31]. |
| 1375 | // |
| 1376 | // Convert: VADD_SPLAT elt, size |
| 1377 | // Into: tmp1 = VSPLTIS[BHW] elt-16 |
| 1378 | // tmp2 = VSPLTIS[BHW] -16 |
| 1379 | // VSUBU[BHW]M tmp1, tmp2 |
| 1380 | SDValue EltVal = getI32Imm(Elt - 16); |
| 1381 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1382 | EltVal = getI32Imm(-16); |
| 1383 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1384 | return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0), |
| 1385 | SDValue(Tmp2, 0)); |
| 1386 | |
| 1387 | } else { |
| 1388 | // Elt is odd and negative, in the range [-31,-17]. |
| 1389 | // |
| 1390 | // Convert: VADD_SPLAT elt, size |
| 1391 | // Into: tmp1 = VSPLTIS[BHW] elt+16 |
| 1392 | // tmp2 = VSPLTIS[BHW] -16 |
| 1393 | // VADDU[BHW]M tmp1, tmp2 |
| 1394 | SDValue EltVal = getI32Imm(Elt + 16); |
| 1395 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1396 | EltVal = getI32Imm(-16); |
| 1397 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 1398 | return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0), |
| 1399 | SDValue(Tmp2, 0)); |
| 1400 | } |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1401 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1402 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1403 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1404 | return SelectCode(N); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Bill Schmidt | 4210211 | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 1407 | /// PostProcessISelDAG - Perform some late peephole optimizations |
| 1408 | /// on the DAG representation. |
| 1409 | void PPCDAGToDAGISel::PostprocessISelDAG() { |
| 1410 | |
| 1411 | // Skip peepholes at -O0. |
| 1412 | if (TM.getOptLevel() == CodeGenOpt::None) |
| 1413 | return; |
| 1414 | |
| 1415 | // These optimizations are currently supported only for 64-bit SVR4. |
| 1416 | if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64()) |
| 1417 | return; |
| 1418 | |
| 1419 | SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); |
| 1420 | ++Position; |
| 1421 | |
| 1422 | while (Position != CurDAG->allnodes_begin()) { |
| 1423 | SDNode *N = --Position; |
| 1424 | // Skip dead nodes and any non-machine opcodes. |
| 1425 | if (N->use_empty() || !N->isMachineOpcode()) |
| 1426 | continue; |
| 1427 | |
| 1428 | unsigned FirstOp; |
| 1429 | unsigned StorageOpcode = N->getMachineOpcode(); |
| 1430 | |
| 1431 | switch (StorageOpcode) { |
| 1432 | default: continue; |
| 1433 | |
| 1434 | case PPC::LBZ: |
| 1435 | case PPC::LBZ8: |
| 1436 | case PPC::LD: |
| 1437 | case PPC::LFD: |
| 1438 | case PPC::LFS: |
| 1439 | case PPC::LHA: |
| 1440 | case PPC::LHA8: |
| 1441 | case PPC::LHZ: |
| 1442 | case PPC::LHZ8: |
| 1443 | case PPC::LWA: |
| 1444 | case PPC::LWZ: |
| 1445 | case PPC::LWZ8: |
| 1446 | FirstOp = 0; |
| 1447 | break; |
| 1448 | |
| 1449 | case PPC::STB: |
| 1450 | case PPC::STB8: |
| 1451 | case PPC::STD: |
| 1452 | case PPC::STFD: |
| 1453 | case PPC::STFS: |
| 1454 | case PPC::STH: |
| 1455 | case PPC::STH8: |
| 1456 | case PPC::STW: |
| 1457 | case PPC::STW8: |
| 1458 | FirstOp = 1; |
| 1459 | break; |
| 1460 | } |
| 1461 | |
| 1462 | // If this is a load or store with a zero offset, we may be able to |
| 1463 | // fold an add-immediate into the memory operation. |
| 1464 | if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) || |
| 1465 | N->getConstantOperandVal(FirstOp) != 0) |
| 1466 | continue; |
| 1467 | |
| 1468 | SDValue Base = N->getOperand(FirstOp + 1); |
| 1469 | if (!Base.isMachineOpcode()) |
| 1470 | continue; |
| 1471 | |
| 1472 | unsigned Flags = 0; |
| 1473 | bool ReplaceFlags = true; |
| 1474 | |
| 1475 | // When the feeding operation is an add-immediate of some sort, |
| 1476 | // determine whether we need to add relocation information to the |
| 1477 | // target flags on the immediate operand when we fold it into the |
| 1478 | // load instruction. |
| 1479 | // |
| 1480 | // For something like ADDItocL, the relocation information is |
| 1481 | // inferred from the opcode; when we process it in the AsmPrinter, |
| 1482 | // we add the necessary relocation there. A load, though, can receive |
| 1483 | // relocation from various flavors of ADDIxxx, so we need to carry |
| 1484 | // the relocation information in the target flags. |
| 1485 | switch (Base.getMachineOpcode()) { |
| 1486 | default: continue; |
| 1487 | |
| 1488 | case PPC::ADDI8: |
Ulrich Weigand | 2b0850b | 2013-03-26 10:55:20 +0000 | [diff] [blame] | 1489 | case PPC::ADDI: |
Bill Schmidt | 4210211 | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 1490 | // In some cases (such as TLS) the relocation information |
| 1491 | // is already in place on the operand, so copying the operand |
| 1492 | // is sufficient. |
| 1493 | ReplaceFlags = false; |
| 1494 | // For these cases, the immediate may not be divisible by 4, in |
| 1495 | // which case the fold is illegal for DS-form instructions. (The |
| 1496 | // other cases provide aligned addresses and are always safe.) |
| 1497 | if ((StorageOpcode == PPC::LWA || |
| 1498 | StorageOpcode == PPC::LD || |
| 1499 | StorageOpcode == PPC::STD) && |
| 1500 | (!isa<ConstantSDNode>(Base.getOperand(1)) || |
| 1501 | Base.getConstantOperandVal(1) % 4 != 0)) |
| 1502 | continue; |
| 1503 | break; |
| 1504 | case PPC::ADDIdtprelL: |
| 1505 | Flags = PPCII::MO_DTPREL16_LO; |
| 1506 | break; |
| 1507 | case PPC::ADDItlsldL: |
| 1508 | Flags = PPCII::MO_TLSLD16_LO; |
| 1509 | break; |
| 1510 | case PPC::ADDItocL: |
| 1511 | Flags = PPCII::MO_TOC16_LO; |
| 1512 | break; |
| 1513 | } |
| 1514 | |
| 1515 | // We found an opportunity. Reverse the operands from the add |
| 1516 | // immediate and substitute them into the load or store. If |
| 1517 | // needed, update the target flags for the immediate operand to |
| 1518 | // reflect the necessary relocation information. |
| 1519 | DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); |
| 1520 | DEBUG(Base->dump(CurDAG)); |
| 1521 | DEBUG(dbgs() << "\nN: "); |
| 1522 | DEBUG(N->dump(CurDAG)); |
| 1523 | DEBUG(dbgs() << "\n"); |
| 1524 | |
| 1525 | SDValue ImmOpnd = Base.getOperand(1); |
| 1526 | |
| 1527 | // If the relocation information isn't already present on the |
| 1528 | // immediate operand, add it now. |
| 1529 | if (ReplaceFlags) { |
Bill Schmidt | 0514595 | 2013-02-21 14:35:42 +0000 | [diff] [blame] | 1530 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1531 | SDLoc dl(GA); |
Bill Schmidt | 4210211 | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 1532 | const GlobalValue *GV = GA->getGlobal(); |
| 1533 | ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags); |
Bill Schmidt | 399eafb | 2013-02-21 17:26:05 +0000 | [diff] [blame] | 1534 | } else if (ConstantPoolSDNode *CP = |
| 1535 | dyn_cast<ConstantPoolSDNode>(ImmOpnd)) { |
Bill Schmidt | 0514595 | 2013-02-21 14:35:42 +0000 | [diff] [blame] | 1536 | const Constant *C = CP->getConstVal(); |
| 1537 | ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64, |
| 1538 | CP->getAlignment(), |
| 1539 | 0, Flags); |
Bill Schmidt | 4210211 | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | if (FirstOp == 1) // Store |
| 1544 | (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd, |
| 1545 | Base.getOperand(0), N->getOperand(3)); |
| 1546 | else // Load |
| 1547 | (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0), |
| 1548 | N->getOperand(2)); |
| 1549 | |
| 1550 | // The add-immediate may now be dead, in which case remove it. |
| 1551 | if (Base.getNode()->use_empty()) |
| 1552 | CurDAG->RemoveDeadNode(Base.getNode()); |
| 1553 | } |
| 1554 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1555 | |
Chris Lattner | cf00631 | 2006-06-10 01:15:02 +0000 | [diff] [blame] | 1556 | |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 1557 | /// createPPCISelDag - This pass converts a legalized DAG into a |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1558 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 1559 | /// |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 1560 | FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) { |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 1561 | return new PPCDAGToDAGISel(TM); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Krzysztof Parzyszek | 96848df | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 1564 | static void initializePassOnce(PassRegistry &Registry) { |
| 1565 | const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection"; |
| 1566 | PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0, |
| 1567 | false, false); |
| 1568 | Registry.registerPass(*PI, true); |
| 1569 | } |
| 1570 | |
| 1571 | void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) { |
| 1572 | CALL_ONCE_INITIALIZATION(initializePassOnce); |
| 1573 | } |
| 1574 | |