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