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