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