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