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