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 | |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/PPCPredicates.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 17 | #include "PPC.h" |
| 18 | #include "PPCISelLowering.h" |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 19 | #include "PPCMachineFunctionInfo.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 20 | #include "PPCSubtarget.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "PPCTargetMachine.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/APInt.h" |
| 23 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| 29 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/ISDOpcodes.h" |
| 31 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineValueType.h" |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/SelectionDAG.h" |
| 37 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/ValueTypes.h" |
| 42 | #include "llvm/IR/BasicBlock.h" |
| 43 | #include "llvm/IR/DebugLoc.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Function.h" |
| 45 | #include "llvm/IR/GlobalValue.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 46 | #include "llvm/IR/InlineAsm.h" |
| 47 | #include "llvm/IR/InstrTypes.h" |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 48 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Casting.h" |
| 50 | #include "llvm/Support/CodeGen.h" |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 51 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Debug.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 54 | #include "llvm/Support/ErrorHandling.h" |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 55 | #include "llvm/Support/KnownBits.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 56 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 57 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 58 | #include <algorithm> |
| 59 | #include <cassert> |
| 60 | #include <cstdint> |
| 61 | #include <iterator> |
| 62 | #include <limits> |
| 63 | #include <memory> |
| 64 | #include <new> |
| 65 | #include <tuple> |
| 66 | #include <utility> |
| 67 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 68 | using namespace llvm; |
| 69 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 70 | #define DEBUG_TYPE "ppc-codegen" |
| 71 | |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 72 | STATISTIC(NumSextSetcc, |
| 73 | "Number of (sext(setcc)) nodes expanded into GPR sequence."); |
| 74 | STATISTIC(NumZextSetcc, |
| 75 | "Number of (zext(setcc)) nodes expanded into GPR sequence."); |
| 76 | STATISTIC(SignExtensionsAdded, |
| 77 | "Number of sign extensions for compare inputs added."); |
| 78 | STATISTIC(ZeroExtensionsAdded, |
| 79 | "Number of zero extensions for compare inputs added."); |
| 80 | STATISTIC(NumLogicOpsOnComparison, |
| 81 | "Number of logical ops on i1 values calculated in GPR."); |
| 82 | STATISTIC(OmittedForNonExtendUses, |
| 83 | "Number of compares not eliminated as they have non-extending uses."); |
| 84 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 85 | // FIXME: Remove this once the bug has been fixed! |
| 86 | cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug", |
| 87 | cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden); |
| 88 | |
Benjamin Kramer | 970eac4 | 2015-02-06 17:51:54 +0000 | [diff] [blame] | 89 | static cl::opt<bool> |
| 90 | UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true), |
| 91 | cl::desc("use aggressive ppc isel for bit permutations"), |
| 92 | cl::Hidden); |
| 93 | static cl::opt<bool> BPermRewriterNoMasking( |
| 94 | "ppc-bit-perm-rewriter-stress-rotates", |
| 95 | cl::desc("stress rotate selection in aggressive ppc isel for " |
| 96 | "bit permutations"), |
| 97 | cl::Hidden); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 98 | |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 99 | static cl::opt<bool> EnableBranchHint( |
| 100 | "ppc-use-branch-hint", cl::init(true), |
| 101 | cl::desc("Enable static hinting of branches on ppc"), |
| 102 | cl::Hidden); |
| 103 | |
Zaara Syeda | 1110c4d | 2018-03-15 15:34:41 +0000 | [diff] [blame^] | 104 | static cl::opt<bool> EnableTLSOpt( |
| 105 | "ppc-tls-opt", cl::init(true), |
| 106 | cl::desc("Enable tls optimization peephole"), |
| 107 | cl::Hidden); |
| 108 | |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 109 | enum ICmpInGPRType { ICGPR_All, ICGPR_None, ICGPR_I32, ICGPR_I64, |
| 110 | ICGPR_NonExtIn, ICGPR_Zext, ICGPR_Sext, ICGPR_ZextI32, |
| 111 | ICGPR_SextI32, ICGPR_ZextI64, ICGPR_SextI64 }; |
| 112 | |
| 113 | static cl::opt<ICmpInGPRType> CmpInGPR( |
Nemanja Ivanovic | 4364513 | 2017-12-01 12:02:59 +0000 | [diff] [blame] | 114 | "ppc-gpr-icmps", cl::Hidden, cl::init(ICGPR_All), |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 115 | cl::desc("Specify the types of comparisons to emit GPR-only code for."), |
| 116 | cl::values(clEnumValN(ICGPR_None, "none", "Do not modify integer comparisons."), |
| 117 | clEnumValN(ICGPR_All, "all", "All possible int comparisons in GPRs."), |
| 118 | clEnumValN(ICGPR_I32, "i32", "Only i32 comparisons in GPRs."), |
| 119 | clEnumValN(ICGPR_I64, "i64", "Only i64 comparisons in GPRs."), |
| 120 | clEnumValN(ICGPR_NonExtIn, "nonextin", |
| 121 | "Only comparisons where inputs don't need [sz]ext."), |
| 122 | clEnumValN(ICGPR_Zext, "zext", "Only comparisons with zext result."), |
| 123 | clEnumValN(ICGPR_ZextI32, "zexti32", |
| 124 | "Only i32 comparisons with zext result."), |
| 125 | clEnumValN(ICGPR_ZextI64, "zexti64", |
| 126 | "Only i64 comparisons with zext result."), |
| 127 | clEnumValN(ICGPR_Sext, "sext", "Only comparisons with sext result."), |
| 128 | clEnumValN(ICGPR_SextI32, "sexti32", |
| 129 | "Only i32 comparisons with sext result."), |
| 130 | clEnumValN(ICGPR_SextI64, "sexti64", |
| 131 | "Only i64 comparisons with sext result."))); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 132 | namespace { |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 134 | //===--------------------------------------------------------------------===// |
Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 135 | /// PPCDAGToDAGISel - PPC specific code to select PPC machine |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 136 | /// instructions for SelectionDAG operations. |
| 137 | /// |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 138 | class PPCDAGToDAGISel : public SelectionDAGISel { |
Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 139 | const PPCTargetMachine &TM; |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 140 | const PPCSubtarget *PPCSubTarget; |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 141 | const PPCTargetLowering *PPCLowering; |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 142 | unsigned GlobalBaseReg; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 144 | public: |
Hiroshi Inoue | 5102028 | 2017-06-27 04:52:17 +0000 | [diff] [blame] | 145 | explicit PPCDAGToDAGISel(PPCTargetMachine &tm, CodeGenOpt::Level OptLevel) |
| 146 | : SelectionDAGISel(tm, OptLevel), TM(tm) {} |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 147 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 148 | bool runOnMachineFunction(MachineFunction &MF) override { |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 149 | // Make sure we re-emit a set of the global base reg if necessary |
| 150 | GlobalBaseReg = 0; |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 151 | PPCSubTarget = &MF.getSubtarget<PPCSubtarget>(); |
| 152 | PPCLowering = PPCSubTarget->getTargetLowering(); |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 153 | SelectionDAGISel::runOnMachineFunction(MF); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 154 | |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 155 | if (!PPCSubTarget->isSVR4ABI()) |
Bill Schmidt | 38d9458 | 2012-10-10 20:54:15 +0000 | [diff] [blame] | 156 | InsertVRSaveCode(MF); |
| 157 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 158 | return true; |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 159 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 160 | |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 161 | void PreprocessISelDAG() override; |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 162 | void PostprocessISelDAG() override; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 163 | |
Hiroshi Inoue | cc555bd | 2017-08-23 08:55:18 +0000 | [diff] [blame] | 164 | /// getI16Imm - Return a target constant with the specified value, of type |
| 165 | /// i16. |
| 166 | inline SDValue getI16Imm(unsigned Imm, const SDLoc &dl) { |
| 167 | return CurDAG->getTargetConstant(Imm, dl, MVT::i16); |
| 168 | } |
| 169 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 170 | /// getI32Imm - Return a target constant with the specified value, of type |
| 171 | /// i32. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 172 | inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 173 | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 176 | /// getI64Imm - Return a target constant with the specified value, of type |
| 177 | /// i64. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 178 | inline SDValue getI64Imm(uint64_t Imm, const SDLoc &dl) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 179 | return CurDAG->getTargetConstant(Imm, dl, MVT::i64); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 180 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 182 | /// getSmallIPtrImm - Return a target constant of pointer type. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 183 | inline SDValue getSmallIPtrImm(unsigned Imm, const SDLoc &dl) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 184 | return CurDAG->getTargetConstant( |
| 185 | Imm, dl, PPCLowering->getPointerTy(CurDAG->getDataLayout())); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 186 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 187 | |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 188 | /// isRotateAndMask - Returns true if Mask and Shift can be folded into a |
| 189 | /// rotate and mask opcode and mask operation. |
Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 190 | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask, |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 191 | unsigned &SH, unsigned &MB, unsigned &ME); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 193 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 194 | /// base register. Return the virtual register that holds this value. |
Evan Cheng | 61413a3 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 195 | SDNode *getGlobalBaseReg(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 196 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 197 | void selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0); |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 199 | // Select - Convert the specified operand from a target-independent to a |
| 200 | // target-specific node if it hasn't already been changed. |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 201 | void Select(SDNode *N) override; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 202 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 203 | bool tryBitfieldInsert(SDNode *N); |
| 204 | bool tryBitPermutation(SDNode *N); |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 205 | bool tryIntCompareInGPR(SDNode *N); |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 206 | |
Zaara Syeda | 1110c4d | 2018-03-15 15:34:41 +0000 | [diff] [blame^] | 207 | // tryTLSXFormLoad - Convert an ISD::LOAD fed by a PPCISD::ADD_TLS into |
| 208 | // an X-Form load instruction with the offset being a relocation coming from |
| 209 | // the PPCISD::ADD_TLS. |
| 210 | bool tryTLSXFormLoad(LoadSDNode *N); |
| 211 | // tryTLSXFormStore - Convert an ISD::STORE fed by a PPCISD::ADD_TLS into |
| 212 | // an X-Form store instruction with the offset being a relocation coming from |
| 213 | // the PPCISD::ADD_TLS. |
| 214 | bool tryTLSXFormStore(StoreSDNode *N); |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 215 | /// SelectCC - Select a comparison of the specified values with the |
| 216 | /// specified condition code, returning the CR# of the expression. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 217 | SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
| 218 | const SDLoc &dl); |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 219 | |
Nate Begeman | 8e6a8af | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 220 | /// SelectAddrImm - Returns true if the address N can be represented by |
| 221 | /// a base register plus a signed 16-bit displacement [r+imm]. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 222 | bool SelectAddrImm(SDValue N, SDValue &Disp, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 223 | SDValue &Base) { |
Nemanja Ivanovic | 3c7e276d | 2017-07-13 18:17:10 +0000 | [diff] [blame] | 224 | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, 0); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 225 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 6f5840c | 2006-11-16 00:41:37 +0000 | [diff] [blame] | 227 | /// SelectAddrImmOffs - Return true if the operand is valid for a preinc |
Ulrich Weigand | d1b99d3 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 228 | /// immediate field. Note that the operand at this point is already the |
| 229 | /// result of a prior SelectAddressRegImm call. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 230 | bool SelectAddrImmOffs(SDValue N, SDValue &Out) const { |
Ulrich Weigand | d1b99d3 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 231 | if (N.getOpcode() == ISD::TargetConstant || |
Hal Finkel | a86b0f2 | 2012-06-21 20:10:48 +0000 | [diff] [blame] | 232 | N.getOpcode() == ISD::TargetGlobalAddress) { |
Hal Finkel | 1cc27e4 | 2012-06-19 02:34:32 +0000 | [diff] [blame] | 233 | Out = N; |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | return false; |
| 238 | } |
| 239 | |
Nate Begeman | 8e6a8af | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 240 | /// SelectAddrIdx - Given the specified addressed, check to see if it can be |
| 241 | /// represented as an indexed [r+r] operation. Returns false if it can |
| 242 | /// be represented by [r+imm], which are preferred. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 243 | bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 244 | return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 245 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 246 | |
Nate Begeman | 8e6a8af | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 247 | /// SelectAddrIdxOnly - Given the specified addressed, force it to be |
| 248 | /// represented as an indexed [r+r] operation. |
Chris Lattner | 0e023ea | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 249 | bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 250 | return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | c5292ec | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 252 | |
Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 253 | /// SelectAddrImmX4 - Returns true if the address N can be represented by |
| 254 | /// a base register plus a signed 16-bit displacement that is a multiple of 4. |
| 255 | /// Suitable for use by STD and friends. |
| 256 | bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) { |
Nemanja Ivanovic | 3c7e276d | 2017-07-13 18:17:10 +0000 | [diff] [blame] | 257 | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, 4); |
| 258 | } |
| 259 | |
| 260 | bool SelectAddrImmX16(SDValue N, SDValue &Disp, SDValue &Base) { |
| 261 | return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, 16); |
Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 262 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 263 | |
Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 264 | // Select an address into a single register. |
| 265 | bool SelectAddr(SDValue N, SDValue &Base) { |
| 266 | Base = N; |
| 267 | return true; |
| 268 | } |
| 269 | |
Chris Lattner | a1ec1dd | 2006-02-24 02:13:12 +0000 | [diff] [blame] | 270 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
Dale Johannesen | 4a50e68 | 2009-08-18 00:18:39 +0000 | [diff] [blame] | 271 | /// inline asm expressions. It is always correct to compute the value into |
| 272 | /// a register. The case of adding a (possibly relocatable) constant to a |
| 273 | /// register can be improved, but it is wrong to substitute Reg+Reg for |
| 274 | /// 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] | 275 | bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 276 | unsigned ConstraintID, |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 277 | std::vector<SDValue> &OutOps) override { |
Daniel Sanders | 0828860 | 2015-03-17 11:09:13 +0000 | [diff] [blame] | 278 | switch(ConstraintID) { |
| 279 | default: |
| 280 | errs() << "ConstraintID: " << ConstraintID << "\n"; |
| 281 | llvm_unreachable("Unexpected asm memory constraint"); |
| 282 | case InlineAsm::Constraint_es: |
Daniel Sanders | 914b947 | 2015-03-17 12:00:04 +0000 | [diff] [blame] | 283 | case InlineAsm::Constraint_i: |
Daniel Sanders | 0828860 | 2015-03-17 11:09:13 +0000 | [diff] [blame] | 284 | case InlineAsm::Constraint_m: |
| 285 | case InlineAsm::Constraint_o: |
| 286 | case InlineAsm::Constraint_Q: |
| 287 | case InlineAsm::Constraint_Z: |
| 288 | case InlineAsm::Constraint_Zy: |
| 289 | // We need to make sure that this one operand does not end up in r0 |
| 290 | // (because we might end up lowering this as 0(%op)). |
| 291 | const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo(); |
| 292 | const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 293 | SDLoc dl(Op); |
| 294 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32); |
Daniel Sanders | 0828860 | 2015-03-17 11:09:13 +0000 | [diff] [blame] | 295 | SDValue NewOp = |
| 296 | SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 297 | dl, Op.getValueType(), |
Daniel Sanders | 0828860 | 2015-03-17 11:09:13 +0000 | [diff] [blame] | 298 | Op, RC), 0); |
| 299 | |
| 300 | OutOps.push_back(NewOp); |
| 301 | return false; |
| 302 | } |
| 303 | return true; |
Chris Lattner | a1ec1dd | 2006-02-24 02:13:12 +0000 | [diff] [blame] | 304 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 305 | |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 306 | void InsertVRSaveCode(MachineFunction &MF); |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 307 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 308 | StringRef getPassName() const override { |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 309 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Chris Lattner | 03e08ee | 2005-09-13 22:03:06 +0000 | [diff] [blame] | 312 | // Include the pieces autogenerated from the target description. |
Chris Lattner | 0921e3b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 313 | #include "PPCGenDAGISel.inc" |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 259e6c7 | 2005-10-06 18:45:51 +0000 | [diff] [blame] | 315 | private: |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 316 | bool trySETCC(SDNode *N); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 317 | |
| 318 | void PeepholePPC64(); |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 319 | void PeepholePPC64ZExt(); |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 320 | void PeepholeCROps(); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 321 | |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 322 | SDValue combineToCMPB(SDNode *N); |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 323 | void foldBoolExts(SDValue &Res, SDNode *&N); |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 324 | |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 325 | bool AllUsersSelectZero(SDNode *N); |
| 326 | void SwapAllSelectUsers(SDNode *N); |
Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 327 | |
Nemanja Ivanovic | 3c7e276d | 2017-07-13 18:17:10 +0000 | [diff] [blame] | 328 | bool isOffsetMultipleOf(SDNode *N, unsigned Val) const; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 329 | void transferMemOperands(SDNode *N, SDNode *Result); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 330 | }; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 331 | |
| 332 | } // end anonymous namespace |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 334 | /// InsertVRSaveCode - Once the entire function has been instruction selected, |
| 335 | /// all virtual registers are created and all machine instructions are built, |
| 336 | /// 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] | 337 | void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) { |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 338 | // 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] | 339 | // 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] | 340 | // |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 341 | // In this case, there will be virtual registers of vector type created |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 342 | // by the scheduler. Detect them now. |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 343 | bool HasVectorVReg = false; |
Jakob Stoklund Olesen | 4a7b48d | 2011-01-08 23:11:11 +0000 | [diff] [blame] | 344 | for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) { |
| 345 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 346 | if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) { |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 347 | HasVectorVReg = true; |
| 348 | break; |
| 349 | } |
Jakob Stoklund Olesen | 4a7b48d | 2011-01-08 23:11:11 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 351 | if (!HasVectorVReg) return; // nothing to do. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 353 | // If we have a vector register, we want to emit code into the entry and exit |
| 354 | // blocks to save and restore the VRSAVE register. We do this here (instead |
| 355 | // of marking all vector instructions as clobbering VRSAVE) for two reasons: |
| 356 | // |
| 357 | // 1. This (trivially) reduces the load on the register allocator, by not |
| 358 | // having to represent the live range of the VRSAVE register. |
| 359 | // 2. This (more significantly) allows us to create a temporary virtual |
| 360 | // register to hold the saved VRSAVE value, allowing this temporary to be |
| 361 | // register allocated, instead of forcing it to be spilled to the stack. |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 362 | |
| 363 | // Create two vregs - one to hold the VRSAVE register that is live-in to the |
| 364 | // 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] | 365 | unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
| 366 | unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 367 | |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 368 | const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo(); |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 369 | MachineBasicBlock &EntryBB = *Fn.begin(); |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 370 | DebugLoc dl; |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 371 | // Emit the following code into the entry block: |
| 372 | // InVRSAVE = MFVRSAVE |
| 373 | // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE |
| 374 | // MTVRSAVE UpdatedVRSAVE |
| 375 | MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 376 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE); |
| 377 | BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE), |
Chris Lattner | a98c679 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 378 | UpdatedVRSAVE).addReg(InVRSAVE); |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 379 | BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 381 | // Find all return blocks, outputting a restore in each epilog. |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 382 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
Matthias Braun | c2d4bef | 2015-09-25 21:25:19 +0000 | [diff] [blame] | 383 | if (BB->isReturnBlock()) { |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 384 | IP = BB->end(); --IP; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 386 | // Skip over all terminator instructions, which are part of the return |
| 387 | // sequence. |
| 388 | MachineBasicBlock::iterator I2 = IP; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 389 | while (I2 != BB->begin() && (--I2)->isTerminator()) |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 390 | IP = I2; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 1678a6c | 2006-03-16 18:25:23 +0000 | [diff] [blame] | 392 | // Emit: MTVRSAVE InVRSave |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 393 | BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | 02e2c18 | 2006-03-13 21:52:10 +0000 | [diff] [blame] | 395 | } |
Chris Lattner | 259e6c7 | 2005-10-06 18:45:51 +0000 | [diff] [blame] | 396 | } |
Chris Lattner | 8ae9525 | 2005-09-03 01:17:22 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 398 | /// getGlobalBaseReg - Output the instructions required to put the |
| 399 | /// base address to use for accessing globals into a register. |
| 400 | /// |
Evan Cheng | 61413a3 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 401 | SDNode *PPCDAGToDAGISel::getGlobalBaseReg() { |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 402 | if (!GlobalBaseReg) { |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 403 | const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo(); |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 404 | // Insert the set of GlobalBaseReg into the first MBB of the function |
Dan Gohman | fca8968 | 2009-08-15 02:07:36 +0000 | [diff] [blame] | 405 | MachineBasicBlock &FirstMBB = MF->front(); |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 406 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 407 | const Module *M = MF->getFunction().getParent(); |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 408 | DebugLoc dl; |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 409 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 410 | if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) == MVT::i32) { |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 411 | if (PPCSubTarget->isTargetELF()) { |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 412 | GlobalBaseReg = PPC::R30; |
Davide Italiano | 4cccc48 | 2016-06-17 18:07:14 +0000 | [diff] [blame] | 413 | if (M->getPICLevel() == PICLevel::SmallPIC) { |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 414 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR)); |
| 415 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
Justin Hibbits | 98a532d | 2015-01-08 15:47:19 +0000 | [diff] [blame] | 416 | MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true); |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 417 | } else { |
| 418 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); |
| 419 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
| 420 | unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass); |
| 421 | BuildMI(FirstMBB, MBBI, dl, |
Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 422 | TII.get(PPC::UpdateGBR), GlobalBaseReg) |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 423 | .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg); |
| 424 | MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true); |
| 425 | } |
| 426 | } else { |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 427 | GlobalBaseReg = |
Joerg Sonnenberger | bef3621 | 2016-11-02 15:00:31 +0000 | [diff] [blame] | 428 | RegInfo->createVirtualRegister(&PPC::GPRC_and_GPRC_NOR0RegClass); |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 429 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR)); |
| 430 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg); |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 431 | } |
Chris Lattner | b542925 | 2006-11-14 18:43:11 +0000 | [diff] [blame] | 432 | } else { |
Nemanja Ivanovic | bcc82c9 | 2018-02-23 23:08:34 +0000 | [diff] [blame] | 433 | // We must ensure that this sequence is dominated by the prologue. |
| 434 | // FIXME: This is a bit of a big hammer since we don't get the benefits |
| 435 | // of shrink-wrapping whenever we emit this instruction. Considering |
| 436 | // this is used in any function where we emit a jump table, this may be |
| 437 | // a significant limitation. We should consider inserting this in the |
| 438 | // block where it is used and then commoning this sequence up if it |
| 439 | // appears in multiple places. |
| 440 | // Note: on ISA 3.0 cores, we can use lnia (addpcis) insteand of |
| 441 | // MovePCtoLR8. |
| 442 | MF->getInfo<PPCFunctionInfo>()->setShrinkWrapDisabled(true); |
Joerg Sonnenberger | bef3621 | 2016-11-02 15:00:31 +0000 | [diff] [blame] | 443 | GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_and_G8RC_NOX0RegClass); |
Cameron Zwarich | dadd733 | 2011-05-19 02:56:28 +0000 | [diff] [blame] | 444 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8)); |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 445 | BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg); |
Chris Lattner | b542925 | 2006-11-14 18:43:11 +0000 | [diff] [blame] | 446 | } |
Chris Lattner | 4564039 | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 447 | } |
Gabor Greif | 81d6a38 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 448 | return CurDAG->getRegister(GlobalBaseReg, |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 449 | PPCLowering->getPointerTy(CurDAG->getDataLayout())) |
| 450 | .getNode(); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 453 | /// isInt32Immediate - This method tests to see if the node is a 32-bit constant |
| 454 | /// operand. If so Imm will receive the 32-bit value. |
| 455 | static bool isInt32Immediate(SDNode *N, unsigned &Imm) { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 456 | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) { |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 457 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Nate Begeman | 72d6f88 | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 458 | return true; |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 463 | /// isInt64Immediate - This method tests to see if the node is a 64-bit constant |
| 464 | /// operand. If so Imm will receive the 64-bit value. |
| 465 | static bool isInt64Immediate(SDNode *N, uint64_t &Imm) { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 466 | if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) { |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 467 | Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | // isInt32Immediate - This method tests to see if a constant operand. |
| 474 | // If so Imm will receive the 32 bit value. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 475 | static bool isInt32Immediate(SDValue N, unsigned &Imm) { |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 476 | return isInt32Immediate(N.getNode(), Imm); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Hiroshi Inoue | cc555bd | 2017-08-23 08:55:18 +0000 | [diff] [blame] | 479 | /// isInt64Immediate - This method tests to see if the value is a 64-bit |
| 480 | /// constant operand. If so Imm will receive the 64-bit value. |
| 481 | static bool isInt64Immediate(SDValue N, uint64_t &Imm) { |
| 482 | return isInt64Immediate(N.getNode(), Imm); |
| 483 | } |
| 484 | |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 485 | static unsigned getBranchHint(unsigned PCC, FunctionLoweringInfo *FuncInfo, |
| 486 | const SDValue &DestMBB) { |
| 487 | assert(isa<BasicBlockSDNode>(DestMBB)); |
| 488 | |
| 489 | if (!FuncInfo->BPI) return PPC::BR_NO_HINT; |
| 490 | |
| 491 | const BasicBlock *BB = FuncInfo->MBB->getBasicBlock(); |
| 492 | const TerminatorInst *BBTerm = BB->getTerminator(); |
| 493 | |
| 494 | if (BBTerm->getNumSuccessors() != 2) return PPC::BR_NO_HINT; |
| 495 | |
| 496 | const BasicBlock *TBB = BBTerm->getSuccessor(0); |
| 497 | const BasicBlock *FBB = BBTerm->getSuccessor(1); |
| 498 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 499 | auto TProb = FuncInfo->BPI->getEdgeProbability(BB, TBB); |
| 500 | auto FProb = FuncInfo->BPI->getEdgeProbability(BB, FBB); |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 501 | |
| 502 | // We only want to handle cases which are easy to predict at static time, e.g. |
| 503 | // C++ throw statement, that is very likely not taken, or calling never |
| 504 | // returned function, e.g. stdlib exit(). So we set Threshold to filter |
| 505 | // unwanted cases. |
| 506 | // |
| 507 | // Below is LLVM branch weight table, we only want to handle case 1, 2 |
| 508 | // |
| 509 | // Case Taken:Nontaken Example |
| 510 | // 1. Unreachable 1048575:1 C++ throw, stdlib exit(), |
| 511 | // 2. Invoke-terminating 1:1048575 |
| 512 | // 3. Coldblock 4:64 __builtin_expect |
| 513 | // 4. Loop Branch 124:4 For loop |
| 514 | // 5. PH/ZH/FPH 20:12 |
| 515 | const uint32_t Threshold = 10000; |
| 516 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 517 | if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb)) |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 518 | return PPC::BR_NO_HINT; |
| 519 | |
| 520 | DEBUG(dbgs() << "Use branch hint for '" << FuncInfo->Fn->getName() << "::" |
| 521 | << BB->getName() << "'\n" |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 522 | << " -> " << TBB->getName() << ": " << TProb << "\n" |
| 523 | << " -> " << FBB->getName() << ": " << FProb << "\n"); |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 524 | |
| 525 | const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB); |
| 526 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 527 | // If Dest BasicBlock is False-BasicBlock (FBB), swap branch probabilities, |
| 528 | // because we want 'TProb' stands for 'branch probability' to Dest BasicBlock |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 529 | if (BBDN->getBasicBlock()->getBasicBlock() != TBB) |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 530 | std::swap(TProb, FProb); |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 531 | |
Cong Hou | e93b8e1 | 2015-12-22 18:56:14 +0000 | [diff] [blame] | 532 | return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT; |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 533 | } |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 534 | |
| 535 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 536 | // opcode and that it has a immediate integer right operand. |
| 537 | // If so Imm will receive the 32 bit value. |
| 538 | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
Gabor Greif | 81d6a38 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 539 | return N->getOpcode() == Opc |
| 540 | && isInt32Immediate(N->getOperand(1).getNode(), Imm); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 543 | void PPCDAGToDAGISel::selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) { |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 544 | SDLoc dl(SN); |
| 545 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
| 546 | SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0)); |
| 547 | unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8; |
| 548 | if (SN->hasOneUse()) |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 549 | CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI, |
| 550 | getSmallIPtrImm(Offset, dl)); |
| 551 | else |
| 552 | ReplaceNode(SN, CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI, |
| 553 | getSmallIPtrImm(Offset, dl))); |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 556 | bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, |
| 557 | bool isShiftMask, unsigned &SH, |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 558 | unsigned &MB, unsigned &ME) { |
Nate Begeman | 92e7750 | 2005-10-19 00:05:37 +0000 | [diff] [blame] | 559 | // Don't even go down this path for i64, since different logic will be |
| 560 | // necessary for rldicl/rldicr/rldimi. |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 561 | if (N->getValueType(0) != MVT::i32) |
Nate Begeman | 92e7750 | 2005-10-19 00:05:37 +0000 | [diff] [blame] | 562 | return false; |
| 563 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 564 | unsigned Shift = 32; |
| 565 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 566 | unsigned Opcode = N->getOpcode(); |
Chris Lattner | e413b60 | 2005-08-30 00:59:16 +0000 | [diff] [blame] | 567 | if (N->getNumOperands() != 2 || |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 568 | !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31)) |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 569 | return false; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 570 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 571 | if (Opcode == ISD::SHL) { |
| 572 | // apply shift left to mask if it comes first |
Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 573 | if (isShiftMask) Mask = Mask << Shift; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 574 | // determine which bits are made indeterminant by shift |
| 575 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 576 | } else if (Opcode == ISD::SRL) { |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 577 | // apply shift right to mask if it comes first |
Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 578 | if (isShiftMask) Mask = Mask >> Shift; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 579 | // determine which bits are made indeterminant by shift |
| 580 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 581 | // adjust for the left rotate |
| 582 | Shift = 32 - Shift; |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 583 | } else if (Opcode == ISD::ROTL) { |
| 584 | Indeterminant = 0; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 585 | } else { |
| 586 | return false; |
| 587 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 588 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 589 | // if the mask doesn't intersect any Indeterminant bits |
| 590 | if (Mask && !(Mask & Indeterminant)) { |
Chris Lattner | a296339 | 2006-05-12 16:29:37 +0000 | [diff] [blame] | 591 | SH = Shift & 31; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 592 | // make sure the mask is still a mask (wrap arounds may not be) |
| 593 | return isRunOfOnes(Mask, MB, ME); |
| 594 | } |
| 595 | return false; |
| 596 | } |
| 597 | |
Zaara Syeda | 1110c4d | 2018-03-15 15:34:41 +0000 | [diff] [blame^] | 598 | bool PPCDAGToDAGISel::tryTLSXFormStore(StoreSDNode *ST) { |
| 599 | SDValue Base = ST->getBasePtr(); |
| 600 | if (Base.getOpcode() != PPCISD::ADD_TLS) |
| 601 | return false; |
| 602 | SDValue Offset = ST->getOffset(); |
| 603 | if (!Offset.isUndef()) |
| 604 | return false; |
| 605 | |
| 606 | SDLoc dl(ST); |
| 607 | EVT MemVT = ST->getMemoryVT(); |
| 608 | EVT RegVT = ST->getValue().getValueType(); |
| 609 | |
| 610 | unsigned Opcode; |
| 611 | switch (MemVT.getSimpleVT().SimpleTy) { |
| 612 | default: |
| 613 | return false; |
| 614 | case MVT::i8: { |
| 615 | Opcode = (RegVT == MVT::i32) ? PPC::STBXTLS_32 : PPC::STBXTLS; |
| 616 | break; |
| 617 | } |
| 618 | case MVT::i16: { |
| 619 | Opcode = (RegVT == MVT::i32) ? PPC::STHXTLS_32 : PPC::STHXTLS; |
| 620 | break; |
| 621 | } |
| 622 | case MVT::i32: { |
| 623 | Opcode = (RegVT == MVT::i32) ? PPC::STWXTLS_32 : PPC::STWXTLS; |
| 624 | break; |
| 625 | } |
| 626 | case MVT::i64: { |
| 627 | Opcode = PPC::STDXTLS; |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | SDValue Chain = ST->getChain(); |
| 632 | SDVTList VTs = ST->getVTList(); |
| 633 | SDValue Ops[] = {ST->getValue(), Base.getOperand(0), Base.getOperand(1), |
| 634 | Chain}; |
| 635 | SDNode *MN = CurDAG->getMachineNode(Opcode, dl, VTs, Ops); |
| 636 | transferMemOperands(ST, MN); |
| 637 | ReplaceNode(ST, MN); |
| 638 | return true; |
| 639 | } |
| 640 | |
| 641 | bool PPCDAGToDAGISel::tryTLSXFormLoad(LoadSDNode *LD) { |
| 642 | SDValue Base = LD->getBasePtr(); |
| 643 | if (Base.getOpcode() != PPCISD::ADD_TLS) |
| 644 | return false; |
| 645 | SDValue Offset = LD->getOffset(); |
| 646 | if (!Offset.isUndef()) |
| 647 | return false; |
| 648 | |
| 649 | SDLoc dl(LD); |
| 650 | EVT MemVT = LD->getMemoryVT(); |
| 651 | EVT RegVT = LD->getValueType(0); |
| 652 | unsigned Opcode; |
| 653 | switch (MemVT.getSimpleVT().SimpleTy) { |
| 654 | default: |
| 655 | return false; |
| 656 | case MVT::i8: { |
| 657 | Opcode = (RegVT == MVT::i32) ? PPC::LBZXTLS_32 : PPC::LBZXTLS; |
| 658 | break; |
| 659 | } |
| 660 | case MVT::i16: { |
| 661 | Opcode = (RegVT == MVT::i32) ? PPC::LHZXTLS_32 : PPC::LHZXTLS; |
| 662 | break; |
| 663 | } |
| 664 | case MVT::i32: { |
| 665 | Opcode = (RegVT == MVT::i32) ? PPC::LWZXTLS_32 : PPC::LWZXTLS; |
| 666 | break; |
| 667 | } |
| 668 | case MVT::i64: { |
| 669 | Opcode = PPC::LDXTLS; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | SDValue Chain = LD->getChain(); |
| 674 | SDVTList VTs = LD->getVTList(); |
| 675 | SDValue Ops[] = {Base.getOperand(0), Base.getOperand(1), Chain}; |
| 676 | SDNode *MN = CurDAG->getMachineNode(Opcode, dl, VTs, Ops); |
| 677 | transferMemOperands(LD, MN); |
| 678 | ReplaceNode(LD, MN); |
| 679 | return true; |
| 680 | } |
| 681 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 682 | /// Turn an or of two masked values into the rotate left word immediate then |
| 683 | /// mask insert (rlwimi) instruction. |
| 684 | bool PPCDAGToDAGISel::tryBitfieldInsert(SDNode *N) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 685 | SDValue Op0 = N->getOperand(0); |
| 686 | SDValue Op1 = N->getOperand(1); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 687 | SDLoc dl(N); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 688 | |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 689 | KnownBits LKnown, RKnown; |
| 690 | CurDAG->computeKnownBits(Op0, LKnown); |
| 691 | CurDAG->computeKnownBits(Op1, RKnown); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 692 | |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 693 | unsigned TargetMask = LKnown.Zero.getZExtValue(); |
| 694 | unsigned InsertMask = RKnown.Zero.getZExtValue(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 695 | |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 696 | if ((TargetMask | InsertMask) == 0xFFFFFFFF) { |
| 697 | unsigned Op0Opc = Op0.getOpcode(); |
| 698 | unsigned Op1Opc = Op1.getOpcode(); |
| 699 | unsigned Value, SH = 0; |
| 700 | TargetMask = ~TargetMask; |
| 701 | InsertMask = ~InsertMask; |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 702 | |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 703 | // If the LHS has a foldable shift and the RHS does not, then swap it to the |
| 704 | // RHS so that we can fold the shift into the insert. |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 705 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
| 706 | if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 707 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
| 708 | if (Op1.getOperand(0).getOpcode() != ISD::SHL && |
| 709 | Op1.getOperand(0).getOpcode() != ISD::SRL) { |
| 710 | std::swap(Op0, Op1); |
| 711 | std::swap(Op0Opc, Op1Opc); |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 712 | std::swap(TargetMask, InsertMask); |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 713 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 714 | } |
Nate Begeman | 9b6d4c2 | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 715 | } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) { |
| 716 | if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL && |
| 717 | Op1.getOperand(0).getOpcode() != ISD::SRL) { |
| 718 | std::swap(Op0, Op1); |
| 719 | std::swap(Op0Opc, Op1Opc); |
| 720 | std::swap(TargetMask, InsertMask); |
| 721 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 722 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 723 | |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 724 | unsigned MB, ME; |
Hal Finkel | ff3ea80 | 2013-07-11 16:31:51 +0000 | [diff] [blame] | 725 | if (isRunOfOnes(InsertMask, MB, ME)) { |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 726 | if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) && |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 727 | isInt32Immediate(Op1.getOperand(1), Value)) { |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 728 | Op1 = Op1.getOperand(0); |
| 729 | SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value; |
| 730 | } |
| 731 | if (Op1Opc == ISD::AND) { |
Hal Finkel | d9963c7 | 2014-04-13 17:10:58 +0000 | [diff] [blame] | 732 | // The AND mask might not be a constant, and we need to make sure that |
| 733 | // if we're going to fold the masking with the insert, all bits not |
| 734 | // know to be zero in the mask are known to be one. |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 735 | KnownBits MKnown; |
| 736 | CurDAG->computeKnownBits(Op1.getOperand(1), MKnown); |
| 737 | bool CanFoldMask = InsertMask == MKnown.One.getZExtValue(); |
Hal Finkel | d9963c7 | 2014-04-13 17:10:58 +0000 | [diff] [blame] | 738 | |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 739 | unsigned SHOpc = Op1.getOperand(0).getOpcode(); |
Hal Finkel | d9963c7 | 2014-04-13 17:10:58 +0000 | [diff] [blame] | 740 | if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask && |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 741 | isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) { |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 742 | // Note that Value must be in range here (less than 32) because |
| 743 | // otherwise there would not be any bits set in InsertMask. |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 744 | Op1 = Op1.getOperand(0).getOperand(0); |
| 745 | SH = (SHOpc == ISD::SHL) ? Value : 32 - Value; |
Nate Begeman | 1333cea | 2006-05-07 00:23:38 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
Dale Johannesen | 8495a50 | 2009-11-20 22:16:40 +0000 | [diff] [blame] | 748 | |
Chris Lattner | a296339 | 2006-05-12 16:29:37 +0000 | [diff] [blame] | 749 | SH &= 31; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 750 | SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl), |
| 751 | getI32Imm(ME, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 752 | ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops)); |
| 753 | return true; |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 754 | } |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 755 | } |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 756 | return false; |
Nate Begeman | 93c4bc6 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 759 | // Predict the number of instructions that would be generated by calling |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 760 | // selectI64Imm(N). |
| 761 | static unsigned selectI64ImmInstrCountDirect(int64_t Imm) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 762 | // Assume no remaining bits. |
| 763 | unsigned Remainder = 0; |
| 764 | // Assume no shift required. |
| 765 | unsigned Shift = 0; |
| 766 | |
| 767 | // If it can't be represented as a 32 bit value. |
| 768 | if (!isInt<32>(Imm)) { |
| 769 | Shift = countTrailingZeros<uint64_t>(Imm); |
| 770 | int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; |
| 771 | |
| 772 | // If the shifted value fits 32 bits. |
| 773 | if (isInt<32>(ImmSh)) { |
| 774 | // Go with the shifted value. |
| 775 | Imm = ImmSh; |
| 776 | } else { |
| 777 | // Still stuck with a 64 bit value. |
| 778 | Remainder = Imm; |
| 779 | Shift = 32; |
| 780 | Imm >>= 32; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // Intermediate operand. |
| 785 | unsigned Result = 0; |
| 786 | |
| 787 | // Handle first 32 bits. |
| 788 | unsigned Lo = Imm & 0xFFFF; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 789 | |
| 790 | // Simple value. |
| 791 | if (isInt<16>(Imm)) { |
| 792 | // Just the Lo bits. |
| 793 | ++Result; |
| 794 | } else if (Lo) { |
| 795 | // Handle the Hi bits and Lo bits. |
| 796 | Result += 2; |
| 797 | } else { |
| 798 | // Just the Hi bits. |
| 799 | ++Result; |
| 800 | } |
| 801 | |
| 802 | // If no shift, we're done. |
| 803 | if (!Shift) return Result; |
| 804 | |
Guozhi Wei | 0cd6542 | 2016-10-14 20:41:50 +0000 | [diff] [blame] | 805 | // If Hi word == Lo word, |
| 806 | // we can use rldimi to insert the Lo word into Hi word. |
| 807 | if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) { |
| 808 | ++Result; |
| 809 | return Result; |
| 810 | } |
| 811 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 812 | // Shift for next step if the upper 32-bits were not zero. |
| 813 | if (Imm) |
| 814 | ++Result; |
| 815 | |
| 816 | // Add in the last bits as required. |
Tilmann Scheller | 990a8d8 | 2015-11-10 12:29:37 +0000 | [diff] [blame] | 817 | if ((Remainder >> 16) & 0xFFFF) |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 818 | ++Result; |
Tilmann Scheller | 990a8d8 | 2015-11-10 12:29:37 +0000 | [diff] [blame] | 819 | if (Remainder & 0xFFFF) |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 820 | ++Result; |
| 821 | |
| 822 | return Result; |
| 823 | } |
| 824 | |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 825 | static uint64_t Rot64(uint64_t Imm, unsigned R) { |
| 826 | return (Imm << R) | (Imm >> (64 - R)); |
| 827 | } |
| 828 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 829 | static unsigned selectI64ImmInstrCount(int64_t Imm) { |
| 830 | unsigned Count = selectI64ImmInstrCountDirect(Imm); |
Hiroshi Inoue | f55ee1b | 2017-07-11 05:28:26 +0000 | [diff] [blame] | 831 | |
| 832 | // If the instruction count is 1 or 2, we do not need further analysis |
| 833 | // since rotate + load constant requires at least 2 instructions. |
| 834 | if (Count <= 2) |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 835 | return Count; |
Hal Finkel | ca6375f | 2015-01-04 12:35:03 +0000 | [diff] [blame] | 836 | |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 837 | for (unsigned r = 1; r < 63; ++r) { |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 838 | uint64_t RImm = Rot64(Imm, r); |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 839 | unsigned RCount = selectI64ImmInstrCountDirect(RImm) + 1; |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 840 | Count = std::min(Count, RCount); |
| 841 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 842 | // See comments in selectI64Imm for an explanation of the logic below. |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 843 | unsigned LS = findLastSet(RImm); |
| 844 | if (LS != r-1) |
| 845 | continue; |
| 846 | |
| 847 | uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1)); |
| 848 | uint64_t RImmWithOnes = RImm | OnesMask; |
| 849 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 850 | RCount = selectI64ImmInstrCountDirect(RImmWithOnes) + 1; |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 851 | Count = std::min(Count, RCount); |
| 852 | } |
Hal Finkel | ca6375f | 2015-01-04 12:35:03 +0000 | [diff] [blame] | 853 | |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 854 | return Count; |
Hal Finkel | ca6375f | 2015-01-04 12:35:03 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 857 | // Select a 64-bit constant. For cost-modeling purposes, selectI64ImmInstrCount |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 858 | // (above) needs to be kept in sync with this function. |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 859 | static SDNode *selectI64ImmDirect(SelectionDAG *CurDAG, const SDLoc &dl, |
| 860 | int64_t Imm) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 861 | // Assume no remaining bits. |
| 862 | unsigned Remainder = 0; |
| 863 | // Assume no shift required. |
| 864 | unsigned Shift = 0; |
| 865 | |
| 866 | // If it can't be represented as a 32 bit value. |
| 867 | if (!isInt<32>(Imm)) { |
| 868 | Shift = countTrailingZeros<uint64_t>(Imm); |
| 869 | int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; |
| 870 | |
| 871 | // If the shifted value fits 32 bits. |
| 872 | if (isInt<32>(ImmSh)) { |
| 873 | // Go with the shifted value. |
| 874 | Imm = ImmSh; |
| 875 | } else { |
| 876 | // Still stuck with a 64 bit value. |
| 877 | Remainder = Imm; |
| 878 | Shift = 32; |
| 879 | Imm >>= 32; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | // Intermediate operand. |
| 884 | SDNode *Result; |
| 885 | |
| 886 | // Handle first 32 bits. |
| 887 | unsigned Lo = Imm & 0xFFFF; |
| 888 | unsigned Hi = (Imm >> 16) & 0xFFFF; |
| 889 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 890 | auto getI32Imm = [CurDAG, dl](unsigned Imm) { |
| 891 | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 892 | }; |
| 893 | |
| 894 | // Simple value. |
| 895 | if (isInt<16>(Imm)) { |
Nemanja Ivanovic | b0783cc | 2017-12-12 12:09:34 +0000 | [diff] [blame] | 896 | uint64_t SextImm = SignExtend64(Lo, 16); |
| 897 | SDValue SDImm = CurDAG->getTargetConstant(SextImm, dl, MVT::i64); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 898 | // Just the Lo bits. |
Nemanja Ivanovic | b0783cc | 2017-12-12 12:09:34 +0000 | [diff] [blame] | 899 | Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, SDImm); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 900 | } else if (Lo) { |
| 901 | // Handle the Hi bits. |
| 902 | unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8; |
| 903 | Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi)); |
| 904 | // And Lo bits. |
| 905 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
| 906 | SDValue(Result, 0), getI32Imm(Lo)); |
| 907 | } else { |
| 908 | // Just the Hi bits. |
| 909 | Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi)); |
| 910 | } |
| 911 | |
| 912 | // If no shift, we're done. |
| 913 | if (!Shift) return Result; |
| 914 | |
Guozhi Wei | 0cd6542 | 2016-10-14 20:41:50 +0000 | [diff] [blame] | 915 | // If Hi word == Lo word, |
| 916 | // we can use rldimi to insert the Lo word into Hi word. |
| 917 | if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) { |
| 918 | SDValue Ops[] = |
| 919 | { SDValue(Result, 0), SDValue(Result, 0), getI32Imm(Shift), getI32Imm(0)}; |
| 920 | return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops); |
| 921 | } |
| 922 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 923 | // Shift for next step if the upper 32-bits were not zero. |
| 924 | if (Imm) { |
| 925 | Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, |
| 926 | SDValue(Result, 0), |
| 927 | getI32Imm(Shift), |
| 928 | getI32Imm(63 - Shift)); |
| 929 | } |
| 930 | |
| 931 | // Add in the last bits as required. |
| 932 | if ((Hi = (Remainder >> 16) & 0xFFFF)) { |
| 933 | Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64, |
| 934 | SDValue(Result, 0), getI32Imm(Hi)); |
| 935 | } |
| 936 | if ((Lo = Remainder & 0xFFFF)) { |
| 937 | Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
| 938 | SDValue(Result, 0), getI32Imm(Lo)); |
| 939 | } |
| 940 | |
| 941 | return Result; |
| 942 | } |
| 943 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 944 | static SDNode *selectI64Imm(SelectionDAG *CurDAG, const SDLoc &dl, |
| 945 | int64_t Imm) { |
| 946 | unsigned Count = selectI64ImmInstrCountDirect(Imm); |
Hiroshi Inoue | f55ee1b | 2017-07-11 05:28:26 +0000 | [diff] [blame] | 947 | |
| 948 | // If the instruction count is 1 or 2, we do not need further analysis |
| 949 | // since rotate + load constant requires at least 2 instructions. |
| 950 | if (Count <= 2) |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 951 | return selectI64ImmDirect(CurDAG, dl, Imm); |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 952 | |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 953 | unsigned RMin = 0; |
Hal Finkel | ca6375f | 2015-01-04 12:35:03 +0000 | [diff] [blame] | 954 | |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 955 | int64_t MatImm; |
| 956 | unsigned MaskEnd; |
| 957 | |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 958 | for (unsigned r = 1; r < 63; ++r) { |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 959 | uint64_t RImm = Rot64(Imm, r); |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 960 | unsigned RCount = selectI64ImmInstrCountDirect(RImm) + 1; |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 961 | if (RCount < Count) { |
| 962 | Count = RCount; |
| 963 | RMin = r; |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 964 | MatImm = RImm; |
| 965 | MaskEnd = 63; |
| 966 | } |
| 967 | |
| 968 | // If the immediate to generate has many trailing zeros, it might be |
| 969 | // worthwhile to generate a rotated value with too many leading ones |
| 970 | // (because that's free with li/lis's sign-extension semantics), and then |
| 971 | // mask them off after rotation. |
| 972 | |
| 973 | unsigned LS = findLastSet(RImm); |
| 974 | // We're adding (63-LS) higher-order ones, and we expect to mask them off |
| 975 | // after performing the inverse rotation by (64-r). So we need that: |
| 976 | // 63-LS == 64-r => LS == r-1 |
| 977 | if (LS != r-1) |
| 978 | continue; |
| 979 | |
| 980 | uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1)); |
| 981 | uint64_t RImmWithOnes = RImm | OnesMask; |
| 982 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 983 | RCount = selectI64ImmInstrCountDirect(RImmWithOnes) + 1; |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 984 | if (RCount < Count) { |
| 985 | Count = RCount; |
| 986 | RMin = r; |
| 987 | MatImm = RImmWithOnes; |
| 988 | MaskEnd = LS; |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 989 | } |
Hal Finkel | ca6375f | 2015-01-04 12:35:03 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 992 | if (!RMin) |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 993 | return selectI64ImmDirect(CurDAG, dl, Imm); |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 994 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 995 | auto getI32Imm = [CurDAG, dl](unsigned Imm) { |
| 996 | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
Hal Finkel | 241ba79 | 2015-01-04 15:43:55 +0000 | [diff] [blame] | 997 | }; |
| 998 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 999 | SDValue Val = SDValue(selectI64ImmDirect(CurDAG, dl, MatImm), 0); |
Hal Finkel | 2f61879 | 2015-01-05 03:41:38 +0000 | [diff] [blame] | 1000 | return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val, |
| 1001 | getI32Imm(64 - RMin), getI32Imm(MaskEnd)); |
Hal Finkel | ca6375f | 2015-01-04 12:35:03 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Nemanja Ivanovic | b0783cc | 2017-12-12 12:09:34 +0000 | [diff] [blame] | 1004 | static unsigned allUsesTruncate(SelectionDAG *CurDAG, SDNode *N) { |
| 1005 | unsigned MaxTruncation = 0; |
| 1006 | // Cannot use range-based for loop here as we need the actual use (i.e. we |
| 1007 | // need the operand number corresponding to the use). A range-based for |
| 1008 | // will unbox the use and provide an SDNode*. |
| 1009 | for (SDNode::use_iterator Use = N->use_begin(), UseEnd = N->use_end(); |
| 1010 | Use != UseEnd; ++Use) { |
| 1011 | unsigned Opc = |
| 1012 | Use->isMachineOpcode() ? Use->getMachineOpcode() : Use->getOpcode(); |
| 1013 | switch (Opc) { |
| 1014 | default: return 0; |
| 1015 | case ISD::TRUNCATE: |
| 1016 | if (Use->isMachineOpcode()) |
| 1017 | return 0; |
| 1018 | MaxTruncation = |
| 1019 | std::max(MaxTruncation, Use->getValueType(0).getSizeInBits()); |
| 1020 | continue; |
| 1021 | case ISD::STORE: { |
| 1022 | if (Use->isMachineOpcode()) |
| 1023 | return 0; |
| 1024 | StoreSDNode *STN = cast<StoreSDNode>(*Use); |
| 1025 | unsigned MemVTSize = STN->getMemoryVT().getSizeInBits(); |
| 1026 | if (MemVTSize == 64 || Use.getOperandNo() != 0) |
| 1027 | return 0; |
| 1028 | MaxTruncation = std::max(MaxTruncation, MemVTSize); |
| 1029 | continue; |
| 1030 | } |
| 1031 | case PPC::STW8: |
| 1032 | case PPC::STWX8: |
| 1033 | case PPC::STWU8: |
| 1034 | case PPC::STWUX8: |
| 1035 | if (Use.getOperandNo() != 0) |
| 1036 | return 0; |
| 1037 | MaxTruncation = std::max(MaxTruncation, 32u); |
| 1038 | continue; |
| 1039 | case PPC::STH8: |
| 1040 | case PPC::STHX8: |
| 1041 | case PPC::STHU8: |
| 1042 | case PPC::STHUX8: |
| 1043 | if (Use.getOperandNo() != 0) |
| 1044 | return 0; |
| 1045 | MaxTruncation = std::max(MaxTruncation, 16u); |
| 1046 | continue; |
| 1047 | case PPC::STB8: |
| 1048 | case PPC::STBX8: |
| 1049 | case PPC::STBU8: |
| 1050 | case PPC::STBUX8: |
| 1051 | if (Use.getOperandNo() != 0) |
| 1052 | return 0; |
| 1053 | MaxTruncation = std::max(MaxTruncation, 8u); |
| 1054 | continue; |
| 1055 | } |
| 1056 | } |
| 1057 | return MaxTruncation; |
| 1058 | } |
| 1059 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1060 | // Select a 64-bit constant. |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 1061 | static SDNode *selectI64Imm(SelectionDAG *CurDAG, SDNode *N) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1062 | SDLoc dl(N); |
| 1063 | |
| 1064 | // Get 64 bit value. |
| 1065 | int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue(); |
Nemanja Ivanovic | b0783cc | 2017-12-12 12:09:34 +0000 | [diff] [blame] | 1066 | if (unsigned MinSize = allUsesTruncate(CurDAG, N)) { |
| 1067 | uint64_t SextImm = SignExtend64(Imm, MinSize); |
| 1068 | SDValue SDImm = CurDAG->getTargetConstant(SextImm, dl, MVT::i64); |
| 1069 | if (isInt<16>(SextImm)) |
| 1070 | return CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, SDImm); |
| 1071 | } |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 1072 | return selectI64Imm(CurDAG, dl, Imm); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1075 | namespace { |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 1076 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1077 | class BitPermutationSelector { |
| 1078 | struct ValueBit { |
| 1079 | SDValue V; |
| 1080 | |
| 1081 | // The bit number in the value, using a convention where bit 0 is the |
| 1082 | // lowest-order bit. |
| 1083 | unsigned Idx; |
| 1084 | |
| 1085 | enum Kind { |
| 1086 | ConstZero, |
| 1087 | Variable |
| 1088 | } K; |
| 1089 | |
| 1090 | ValueBit(SDValue V, unsigned I, Kind K = Variable) |
| 1091 | : V(V), Idx(I), K(K) {} |
| 1092 | ValueBit(Kind K = Variable) |
| 1093 | : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {} |
| 1094 | |
| 1095 | bool isZero() const { |
| 1096 | return K == ConstZero; |
| 1097 | } |
| 1098 | |
| 1099 | bool hasValue() const { |
| 1100 | return K == Variable; |
| 1101 | } |
| 1102 | |
| 1103 | SDValue getValue() const { |
| 1104 | assert(hasValue() && "Cannot get the value of a constant bit"); |
| 1105 | return V; |
| 1106 | } |
| 1107 | |
| 1108 | unsigned getValueBitIndex() const { |
| 1109 | assert(hasValue() && "Cannot get the value bit index of a constant bit"); |
| 1110 | return Idx; |
| 1111 | } |
| 1112 | }; |
| 1113 | |
| 1114 | // A bit group has the same underlying value and the same rotate factor. |
| 1115 | struct BitGroup { |
| 1116 | SDValue V; |
| 1117 | unsigned RLAmt; |
| 1118 | unsigned StartIdx, EndIdx; |
| 1119 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1120 | // This rotation amount assumes that the lower 32 bits of the quantity are |
| 1121 | // replicated in the high 32 bits by the rotation operator (which is done |
| 1122 | // by rlwinm and friends in 64-bit mode). |
| 1123 | bool Repl32; |
| 1124 | // Did converting to Repl32 == true change the rotation factor? If it did, |
| 1125 | // it decreased it by 32. |
| 1126 | bool Repl32CR; |
| 1127 | // Was this group coalesced after setting Repl32 to true? |
| 1128 | bool Repl32Coalesced; |
| 1129 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1130 | BitGroup(SDValue V, unsigned R, unsigned S, unsigned E) |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1131 | : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false), |
| 1132 | Repl32Coalesced(false) { |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1133 | DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R << |
| 1134 | " [" << S << ", " << E << "]\n"); |
| 1135 | } |
| 1136 | }; |
| 1137 | |
| 1138 | // Information on each (Value, RLAmt) pair (like the number of groups |
| 1139 | // associated with each) used to choose the lowering method. |
| 1140 | struct ValueRotInfo { |
| 1141 | SDValue V; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 1142 | unsigned RLAmt = std::numeric_limits<unsigned>::max(); |
| 1143 | unsigned NumGroups = 0; |
| 1144 | unsigned FirstGroupStartIdx = std::numeric_limits<unsigned>::max(); |
| 1145 | bool Repl32 = false; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1146 | |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 1147 | ValueRotInfo() = default; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1148 | |
| 1149 | // For sorting (in reverse order) by NumGroups, and then by |
| 1150 | // FirstGroupStartIdx. |
| 1151 | bool operator < (const ValueRotInfo &Other) const { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1152 | // We need to sort so that the non-Repl32 come first because, when we're |
| 1153 | // doing masking, the Repl32 bit groups might be subsumed into the 64-bit |
| 1154 | // masking operation. |
| 1155 | if (Repl32 < Other.Repl32) |
| 1156 | return true; |
| 1157 | else if (Repl32 > Other.Repl32) |
| 1158 | return false; |
| 1159 | else if (NumGroups > Other.NumGroups) |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1160 | return true; |
| 1161 | else if (NumGroups < Other.NumGroups) |
| 1162 | return false; |
| 1163 | else if (FirstGroupStartIdx < Other.FirstGroupStartIdx) |
| 1164 | return true; |
| 1165 | return false; |
| 1166 | } |
| 1167 | }; |
| 1168 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1169 | using ValueBitsMemoizedValue = std::pair<bool, SmallVector<ValueBit, 64>>; |
| 1170 | using ValueBitsMemoizer = |
| 1171 | DenseMap<SDValue, std::unique_ptr<ValueBitsMemoizedValue>>; |
| 1172 | ValueBitsMemoizer Memoizer; |
| 1173 | |
| 1174 | // Return a pair of bool and a SmallVector pointer to a memoization entry. |
| 1175 | // 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] | 1176 | // providing only a generic representation of V (or something else likewise |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1177 | // uninteresting for instruction selection) through the SmallVector. |
| 1178 | std::pair<bool, SmallVector<ValueBit, 64> *> getValueBits(SDValue V, |
| 1179 | unsigned NumBits) { |
| 1180 | auto &ValueEntry = Memoizer[V]; |
| 1181 | if (ValueEntry) |
| 1182 | return std::make_pair(ValueEntry->first, &ValueEntry->second); |
| 1183 | ValueEntry.reset(new ValueBitsMemoizedValue()); |
| 1184 | bool &Interesting = ValueEntry->first; |
| 1185 | SmallVector<ValueBit, 64> &Bits = ValueEntry->second; |
| 1186 | Bits.resize(NumBits); |
| 1187 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1188 | switch (V.getOpcode()) { |
| 1189 | default: break; |
| 1190 | case ISD::ROTL: |
| 1191 | if (isa<ConstantSDNode>(V.getOperand(1))) { |
| 1192 | unsigned RotAmt = V.getConstantOperandVal(1); |
| 1193 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1194 | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1195 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1196 | for (unsigned i = 0; i < NumBits; ++i) |
| 1197 | Bits[i] = LHSBits[i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt]; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1198 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1199 | return std::make_pair(Interesting = true, &Bits); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1200 | } |
| 1201 | break; |
| 1202 | case ISD::SHL: |
| 1203 | if (isa<ConstantSDNode>(V.getOperand(1))) { |
| 1204 | unsigned ShiftAmt = V.getConstantOperandVal(1); |
| 1205 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1206 | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1207 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1208 | for (unsigned i = ShiftAmt; i < NumBits; ++i) |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1209 | Bits[i] = LHSBits[i - ShiftAmt]; |
| 1210 | |
| 1211 | for (unsigned i = 0; i < ShiftAmt; ++i) |
| 1212 | Bits[i] = ValueBit(ValueBit::ConstZero); |
| 1213 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1214 | return std::make_pair(Interesting = true, &Bits); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1215 | } |
| 1216 | break; |
| 1217 | case ISD::SRL: |
| 1218 | if (isa<ConstantSDNode>(V.getOperand(1))) { |
| 1219 | unsigned ShiftAmt = V.getConstantOperandVal(1); |
| 1220 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1221 | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1222 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1223 | for (unsigned i = 0; i < NumBits - ShiftAmt; ++i) |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1224 | Bits[i] = LHSBits[i + ShiftAmt]; |
| 1225 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1226 | for (unsigned i = NumBits - ShiftAmt; i < NumBits; ++i) |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1227 | Bits[i] = ValueBit(ValueBit::ConstZero); |
| 1228 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1229 | return std::make_pair(Interesting = true, &Bits); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1230 | } |
| 1231 | break; |
| 1232 | case ISD::AND: |
| 1233 | if (isa<ConstantSDNode>(V.getOperand(1))) { |
| 1234 | uint64_t Mask = V.getConstantOperandVal(1); |
| 1235 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1236 | const SmallVector<ValueBit, 64> *LHSBits; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1237 | // Mark this as interesting, only if the LHS was also interesting. This |
| 1238 | // prevents the overall procedure from matching a single immediate 'and' |
| 1239 | // (which is non-optimal because such an and might be folded with other |
| 1240 | // things if we don't select it here). |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1241 | std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), NumBits); |
| 1242 | |
| 1243 | for (unsigned i = 0; i < NumBits; ++i) |
| 1244 | if (((Mask >> i) & 1) == 1) |
| 1245 | Bits[i] = (*LHSBits)[i]; |
| 1246 | else |
| 1247 | Bits[i] = ValueBit(ValueBit::ConstZero); |
| 1248 | |
| 1249 | return std::make_pair(Interesting, &Bits); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1250 | } |
| 1251 | break; |
| 1252 | case ISD::OR: { |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1253 | const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second; |
| 1254 | const auto &RHSBits = *getValueBits(V.getOperand(1), NumBits).second; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1255 | |
| 1256 | bool AllDisjoint = true; |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1257 | for (unsigned i = 0; i < NumBits; ++i) |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1258 | if (LHSBits[i].isZero()) |
| 1259 | Bits[i] = RHSBits[i]; |
| 1260 | else if (RHSBits[i].isZero()) |
| 1261 | Bits[i] = LHSBits[i]; |
| 1262 | else { |
| 1263 | AllDisjoint = false; |
| 1264 | break; |
| 1265 | } |
| 1266 | |
| 1267 | if (!AllDisjoint) |
| 1268 | break; |
| 1269 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1270 | return std::make_pair(Interesting = true, &Bits); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1271 | } |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1272 | case ISD::ZERO_EXTEND: { |
| 1273 | // We support only the case with zero extension from i32 to i64 so far. |
| 1274 | if (V.getValueType() != MVT::i64 || |
| 1275 | V.getOperand(0).getValueType() != MVT::i32) |
| 1276 | break; |
| 1277 | |
| 1278 | const SmallVector<ValueBit, 64> *LHSBits; |
| 1279 | const unsigned NumOperandBits = 32; |
| 1280 | std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), |
| 1281 | NumOperandBits); |
| 1282 | |
| 1283 | for (unsigned i = 0; i < NumOperandBits; ++i) |
| 1284 | Bits[i] = (*LHSBits)[i]; |
| 1285 | |
| 1286 | for (unsigned i = NumOperandBits; i < NumBits; ++i) |
| 1287 | Bits[i] = ValueBit(ValueBit::ConstZero); |
| 1288 | |
| 1289 | return std::make_pair(Interesting, &Bits); |
| 1290 | } |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1293 | for (unsigned i = 0; i < NumBits; ++i) |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1294 | Bits[i] = ValueBit(V, i); |
| 1295 | |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 1296 | return std::make_pair(Interesting = false, &Bits); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // For each value (except the constant ones), compute the left-rotate amount |
| 1300 | // to get it from its original to final position. |
| 1301 | void computeRotationAmounts() { |
| 1302 | HasZeros = false; |
| 1303 | RLAmt.resize(Bits.size()); |
| 1304 | for (unsigned i = 0; i < Bits.size(); ++i) |
| 1305 | if (Bits[i].hasValue()) { |
| 1306 | unsigned VBI = Bits[i].getValueBitIndex(); |
| 1307 | if (i >= VBI) |
| 1308 | RLAmt[i] = i - VBI; |
| 1309 | else |
| 1310 | RLAmt[i] = Bits.size() - (VBI - i); |
| 1311 | } else if (Bits[i].isZero()) { |
| 1312 | HasZeros = true; |
| 1313 | RLAmt[i] = UINT32_MAX; |
| 1314 | } else { |
| 1315 | llvm_unreachable("Unknown value bit type"); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | // Collect groups of consecutive bits with the same underlying value and |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1320 | // rotation factor. If we're doing late masking, we ignore zeros, otherwise |
| 1321 | // they break up groups. |
| 1322 | void collectBitGroups(bool LateMask) { |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1323 | BitGroups.clear(); |
| 1324 | |
| 1325 | unsigned LastRLAmt = RLAmt[0]; |
| 1326 | SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue(); |
| 1327 | unsigned LastGroupStartIdx = 0; |
| 1328 | for (unsigned i = 1; i < Bits.size(); ++i) { |
| 1329 | unsigned ThisRLAmt = RLAmt[i]; |
| 1330 | SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue(); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1331 | if (LateMask && !ThisValue) { |
| 1332 | ThisValue = LastValue; |
| 1333 | ThisRLAmt = LastRLAmt; |
| 1334 | // If we're doing late masking, then the first bit group always starts |
| 1335 | // at zero (even if the first bits were zero). |
| 1336 | if (BitGroups.empty()) |
| 1337 | LastGroupStartIdx = 0; |
| 1338 | } |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1339 | |
| 1340 | // If this bit has the same underlying value and the same rotate factor as |
| 1341 | // the last one, then they're part of the same group. |
| 1342 | if (ThisRLAmt == LastRLAmt && ThisValue == LastValue) |
| 1343 | continue; |
| 1344 | |
| 1345 | if (LastValue.getNode()) |
| 1346 | BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx, |
| 1347 | i-1)); |
| 1348 | LastRLAmt = ThisRLAmt; |
| 1349 | LastValue = ThisValue; |
| 1350 | LastGroupStartIdx = i; |
| 1351 | } |
| 1352 | if (LastValue.getNode()) |
| 1353 | BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx, |
| 1354 | Bits.size()-1)); |
| 1355 | |
| 1356 | if (BitGroups.empty()) |
| 1357 | return; |
| 1358 | |
| 1359 | // We might be able to combine the first and last groups. |
| 1360 | if (BitGroups.size() > 1) { |
| 1361 | // If the first and last groups are the same, then remove the first group |
| 1362 | // in favor of the last group, making the ending index of the last group |
| 1363 | // equal to the ending index of the to-be-removed first group. |
| 1364 | if (BitGroups[0].StartIdx == 0 && |
| 1365 | BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 && |
| 1366 | BitGroups[0].V == BitGroups[BitGroups.size()-1].V && |
| 1367 | BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1368 | DEBUG(dbgs() << "\tcombining final bit group with initial one\n"); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1369 | BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx; |
| 1370 | BitGroups.erase(BitGroups.begin()); |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | // Take all (SDValue, RLAmt) pairs and sort them by the number of groups |
| 1376 | // associated with each. If there is a degeneracy, pick the one that occurs |
| 1377 | // first (in the final value). |
| 1378 | void collectValueRotInfo() { |
| 1379 | ValueRots.clear(); |
| 1380 | |
| 1381 | for (auto &BG : BitGroups) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1382 | unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0); |
| 1383 | ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)]; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1384 | VRI.V = BG.V; |
| 1385 | VRI.RLAmt = BG.RLAmt; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1386 | VRI.Repl32 = BG.Repl32; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1387 | VRI.NumGroups += 1; |
| 1388 | VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx); |
| 1389 | } |
| 1390 | |
| 1391 | // Now that we've collected the various ValueRotInfo instances, we need to |
| 1392 | // sort them. |
| 1393 | ValueRotsVec.clear(); |
| 1394 | for (auto &I : ValueRots) { |
| 1395 | ValueRotsVec.push_back(I.second); |
| 1396 | } |
| 1397 | std::sort(ValueRotsVec.begin(), ValueRotsVec.end()); |
| 1398 | } |
| 1399 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1400 | // In 64-bit mode, rlwinm and friends have a rotation operator that |
| 1401 | // replicates the low-order 32 bits into the high-order 32-bits. The mask |
| 1402 | // indices of these instructions can only be in the lower 32 bits, so they |
| 1403 | // can only represent some 64-bit bit groups. However, when they can be used, |
| 1404 | // the 32-bit replication can be used to represent, as a single bit group, |
| 1405 | // otherwise separate bit groups. We'll convert to replicated-32-bit bit |
| 1406 | // groups when possible. Returns true if any of the bit groups were |
| 1407 | // converted. |
| 1408 | void assignRepl32BitGroups() { |
| 1409 | // If we have bits like this: |
| 1410 | // |
| 1411 | // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 1412 | // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24 |
| 1413 | // Groups: | RLAmt = 8 | RLAmt = 40 | |
| 1414 | // |
| 1415 | // But, making use of a 32-bit operation that replicates the low-order 32 |
| 1416 | // bits into the high-order 32 bits, this can be one bit group with a RLAmt |
| 1417 | // of 8. |
| 1418 | |
| 1419 | auto IsAllLow32 = [this](BitGroup & BG) { |
| 1420 | if (BG.StartIdx <= BG.EndIdx) { |
| 1421 | for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) { |
| 1422 | if (!Bits[i].hasValue()) |
| 1423 | continue; |
| 1424 | if (Bits[i].getValueBitIndex() >= 32) |
| 1425 | return false; |
| 1426 | } |
| 1427 | } else { |
| 1428 | for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) { |
| 1429 | if (!Bits[i].hasValue()) |
| 1430 | continue; |
| 1431 | if (Bits[i].getValueBitIndex() >= 32) |
| 1432 | return false; |
| 1433 | } |
| 1434 | for (unsigned i = 0; i <= BG.EndIdx; ++i) { |
| 1435 | if (!Bits[i].hasValue()) |
| 1436 | continue; |
| 1437 | if (Bits[i].getValueBitIndex() >= 32) |
| 1438 | return false; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | return true; |
| 1443 | }; |
| 1444 | |
| 1445 | for (auto &BG : BitGroups) { |
| 1446 | if (BG.StartIdx < 32 && BG.EndIdx < 32) { |
| 1447 | if (IsAllLow32(BG)) { |
| 1448 | if (BG.RLAmt >= 32) { |
| 1449 | BG.RLAmt -= 32; |
| 1450 | BG.Repl32CR = true; |
| 1451 | } |
| 1452 | |
| 1453 | BG.Repl32 = true; |
| 1454 | |
| 1455 | DEBUG(dbgs() << "\t32-bit replicated bit group for " << |
| 1456 | BG.V.getNode() << " RLAmt = " << BG.RLAmt << |
| 1457 | " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n"); |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | // Now walk through the bit groups, consolidating where possible. |
| 1463 | for (auto I = BitGroups.begin(); I != BitGroups.end();) { |
| 1464 | // We might want to remove this bit group by merging it with the previous |
| 1465 | // group (which might be the ending group). |
| 1466 | auto IP = (I == BitGroups.begin()) ? |
| 1467 | std::prev(BitGroups.end()) : std::prev(I); |
| 1468 | if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt && |
| 1469 | I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) { |
| 1470 | |
| 1471 | DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " << |
| 1472 | I->V.getNode() << " RLAmt = " << I->RLAmt << |
| 1473 | " [" << I->StartIdx << ", " << I->EndIdx << |
| 1474 | "] with group with range [" << |
| 1475 | IP->StartIdx << ", " << IP->EndIdx << "]\n"); |
| 1476 | |
| 1477 | IP->EndIdx = I->EndIdx; |
| 1478 | IP->Repl32CR = IP->Repl32CR || I->Repl32CR; |
| 1479 | IP->Repl32Coalesced = true; |
| 1480 | I = BitGroups.erase(I); |
| 1481 | continue; |
| 1482 | } else { |
| 1483 | // There is a special case worth handling: If there is a single group |
| 1484 | // covering the entire upper 32 bits, and it can be merged with both |
| 1485 | // the next and previous groups (which might be the same group), then |
| 1486 | // do so. If it is the same group (so there will be only one group in |
| 1487 | // total), then we need to reverse the order of the range so that it |
| 1488 | // covers the entire 64 bits. |
| 1489 | if (I->StartIdx == 32 && I->EndIdx == 63) { |
| 1490 | assert(std::next(I) == BitGroups.end() && |
| 1491 | "bit group ends at index 63 but there is another?"); |
| 1492 | auto IN = BitGroups.begin(); |
| 1493 | |
Justin Bogner | b012699 | 2016-05-05 23:19:08 +0000 | [diff] [blame] | 1494 | 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] | 1495 | (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt && |
| 1496 | IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP && |
| 1497 | IsAllLow32(*I)) { |
| 1498 | |
| 1499 | DEBUG(dbgs() << "\tcombining bit group for " << |
| 1500 | I->V.getNode() << " RLAmt = " << I->RLAmt << |
| 1501 | " [" << I->StartIdx << ", " << I->EndIdx << |
| 1502 | "] with 32-bit replicated groups with ranges [" << |
| 1503 | IP->StartIdx << ", " << IP->EndIdx << "] and [" << |
| 1504 | IN->StartIdx << ", " << IN->EndIdx << "]\n"); |
| 1505 | |
| 1506 | if (IP == IN) { |
| 1507 | // There is only one other group; change it to cover the whole |
| 1508 | // range (backward, so that it can still be Repl32 but cover the |
| 1509 | // whole 64-bit range). |
| 1510 | IP->StartIdx = 31; |
| 1511 | IP->EndIdx = 30; |
| 1512 | IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32; |
| 1513 | IP->Repl32Coalesced = true; |
| 1514 | I = BitGroups.erase(I); |
| 1515 | } else { |
| 1516 | // There are two separate groups, one before this group and one |
| 1517 | // after us (at the beginning). We're going to remove this group, |
| 1518 | // but also the group at the very beginning. |
| 1519 | IP->EndIdx = IN->EndIdx; |
| 1520 | IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32; |
| 1521 | IP->Repl32Coalesced = true; |
| 1522 | I = BitGroups.erase(I); |
| 1523 | BitGroups.erase(BitGroups.begin()); |
| 1524 | } |
| 1525 | |
| 1526 | // This must be the last group in the vector (and we might have |
| 1527 | // just invalidated the iterator above), so break here. |
| 1528 | break; |
| 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | ++I; |
| 1534 | } |
| 1535 | } |
| 1536 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1537 | SDValue getI32Imm(unsigned Imm, const SDLoc &dl) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1538 | return CurDAG->getTargetConstant(Imm, dl, MVT::i32); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1541 | uint64_t getZerosMask() { |
| 1542 | uint64_t Mask = 0; |
| 1543 | for (unsigned i = 0; i < Bits.size(); ++i) { |
| 1544 | if (Bits[i].hasValue()) |
| 1545 | continue; |
Hal Finkel | ddf8d7d | 2015-01-01 19:33:59 +0000 | [diff] [blame] | 1546 | Mask |= (UINT64_C(1) << i); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | return ~Mask; |
| 1550 | } |
| 1551 | |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1552 | // This method extends an input value to 64 bit if input is 32-bit integer. |
| 1553 | // While selecting instructions in BitPermutationSelector in 64-bit mode, |
| 1554 | // an input value can be a 32-bit integer if a ZERO_EXTEND node is included. |
| 1555 | // In such case, we extend it to 64 bit to be consistent with other values. |
| 1556 | SDValue ExtendToInt64(SDValue V, const SDLoc &dl) { |
| 1557 | if (V.getValueSizeInBits() == 64) |
| 1558 | return V; |
| 1559 | |
| 1560 | assert(V.getValueSizeInBits() == 32); |
| 1561 | SDValue SubRegIdx = CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32); |
| 1562 | SDValue ImDef = SDValue(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, |
| 1563 | MVT::i64), 0); |
| 1564 | SDValue ExtVal = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, |
| 1565 | MVT::i64, ImDef, V, |
| 1566 | SubRegIdx), 0); |
| 1567 | return ExtVal; |
| 1568 | } |
| 1569 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1570 | // Depending on the number of groups for a particular value, it might be |
| 1571 | // better to rotate, mask explicitly (using andi/andis), and then or the |
| 1572 | // result. Select this part of the result first. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1573 | void SelectAndParts32(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1574 | if (BPermRewriterNoMasking) |
| 1575 | return; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1576 | |
| 1577 | for (ValueRotInfo &VRI : ValueRotsVec) { |
| 1578 | unsigned Mask = 0; |
| 1579 | for (unsigned i = 0; i < Bits.size(); ++i) { |
| 1580 | if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V) |
| 1581 | continue; |
| 1582 | if (RLAmt[i] != VRI.RLAmt) |
| 1583 | continue; |
| 1584 | Mask |= (1u << i); |
| 1585 | } |
| 1586 | |
| 1587 | // Compute the masks for andi/andis that would be necessary. |
| 1588 | unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16; |
| 1589 | assert((ANDIMask != 0 || ANDISMask != 0) && |
| 1590 | "No set bits in mask for value bit groups"); |
| 1591 | bool NeedsRotate = VRI.RLAmt != 0; |
| 1592 | |
| 1593 | // We're trying to minimize the number of instructions. If we have one |
| 1594 | // group, using one of andi/andis can break even. If we have three |
| 1595 | // groups, we can use both andi and andis and break even (to use both |
| 1596 | // andi and andis we also need to or the results together). We need four |
| 1597 | // groups if we also need to rotate. To use andi/andis we need to do more |
| 1598 | // than break even because rotate-and-mask instructions tend to be easier |
| 1599 | // to schedule. |
| 1600 | |
| 1601 | // FIXME: We've biased here against using andi/andis, which is right for |
| 1602 | // POWER cores, but not optimal everywhere. For example, on the A2, |
| 1603 | // andi/andis have single-cycle latency whereas the rotate-and-mask |
| 1604 | // instructions take two cycles, and it would be better to bias toward |
| 1605 | // andi/andis in break-even cases. |
| 1606 | |
| 1607 | unsigned NumAndInsts = (unsigned) NeedsRotate + |
| 1608 | (unsigned) (ANDIMask != 0) + |
| 1609 | (unsigned) (ANDISMask != 0) + |
| 1610 | (unsigned) (ANDIMask != 0 && ANDISMask != 0) + |
| 1611 | (unsigned) (bool) Res; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1612 | |
| 1613 | DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() << |
| 1614 | " RL: " << VRI.RLAmt << ":" << |
| 1615 | "\n\t\t\tisel using masking: " << NumAndInsts << |
| 1616 | " using rotates: " << VRI.NumGroups << "\n"); |
| 1617 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1618 | if (NumAndInsts >= VRI.NumGroups) |
| 1619 | continue; |
| 1620 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1621 | DEBUG(dbgs() << "\t\t\t\tusing masking\n"); |
| 1622 | |
| 1623 | if (InstCnt) *InstCnt += NumAndInsts; |
| 1624 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1625 | SDValue VRot; |
| 1626 | if (VRI.RLAmt) { |
| 1627 | SDValue Ops[] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1628 | { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl), |
| 1629 | getI32Imm(31, dl) }; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1630 | VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
| 1631 | Ops), 0); |
| 1632 | } else { |
| 1633 | VRot = VRI.V; |
| 1634 | } |
| 1635 | |
| 1636 | SDValue ANDIVal, ANDISVal; |
| 1637 | if (ANDIMask != 0) |
| 1638 | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1639 | VRot, getI32Imm(ANDIMask, dl)), 0); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1640 | if (ANDISMask != 0) |
| 1641 | ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1642 | VRot, getI32Imm(ANDISMask, dl)), 0); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1643 | |
| 1644 | SDValue TotalVal; |
| 1645 | if (!ANDIVal) |
| 1646 | TotalVal = ANDISVal; |
| 1647 | else if (!ANDISVal) |
| 1648 | TotalVal = ANDIVal; |
| 1649 | else |
| 1650 | TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, |
| 1651 | ANDIVal, ANDISVal), 0); |
| 1652 | |
| 1653 | if (!Res) |
| 1654 | Res = TotalVal; |
| 1655 | else |
| 1656 | Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, |
| 1657 | Res, TotalVal), 0); |
| 1658 | |
| 1659 | // Now, remove all groups with this underlying value and rotation |
| 1660 | // factor. |
Benjamin Kramer | e7561b8 | 2015-06-20 15:59:41 +0000 | [diff] [blame] | 1661 | eraseMatchingBitGroups([VRI](const BitGroup &BG) { |
| 1662 | return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt; |
| 1663 | }); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | // Instruction selection for the 32-bit case. |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1668 | SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) { |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1669 | SDLoc dl(N); |
| 1670 | SDValue Res; |
| 1671 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1672 | if (InstCnt) *InstCnt = 0; |
| 1673 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1674 | // Take care of cases that should use andi/andis first. |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1675 | SelectAndParts32(dl, Res, InstCnt); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1676 | |
| 1677 | // If we've not yet selected a 'starting' instruction, and we have no zeros |
| 1678 | // to fill in, select the (Value, RLAmt) with the highest priority (largest |
| 1679 | // number of groups), and start with this rotated value. |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1680 | if ((!HasZeros || LateMask) && !Res) { |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1681 | ValueRotInfo &VRI = ValueRotsVec[0]; |
| 1682 | if (VRI.RLAmt) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1683 | if (InstCnt) *InstCnt += 1; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1684 | SDValue Ops[] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1685 | { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl), |
| 1686 | getI32Imm(31, dl) }; |
| 1687 | Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), |
| 1688 | 0); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1689 | } else { |
| 1690 | Res = VRI.V; |
| 1691 | } |
| 1692 | |
| 1693 | // Now, remove all groups with this underlying value and rotation factor. |
Benjamin Kramer | e7561b8 | 2015-06-20 15:59:41 +0000 | [diff] [blame] | 1694 | eraseMatchingBitGroups([VRI](const BitGroup &BG) { |
| 1695 | return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt; |
| 1696 | }); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1699 | if (InstCnt) *InstCnt += BitGroups.size(); |
| 1700 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1701 | // Insert the other groups (one at a time). |
| 1702 | for (auto &BG : BitGroups) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1703 | if (!Res) { |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1704 | SDValue Ops[] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1705 | { BG.V, getI32Imm(BG.RLAmt, dl), |
| 1706 | getI32Imm(Bits.size() - BG.EndIdx - 1, dl), |
| 1707 | getI32Imm(Bits.size() - BG.StartIdx - 1, dl) }; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1708 | Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
| 1709 | } else { |
| 1710 | SDValue Ops[] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1711 | { Res, BG.V, getI32Imm(BG.RLAmt, dl), |
| 1712 | getI32Imm(Bits.size() - BG.EndIdx - 1, dl), |
| 1713 | getI32Imm(Bits.size() - BG.StartIdx - 1, dl) }; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1714 | Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0); |
| 1715 | } |
| 1716 | } |
| 1717 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1718 | if (LateMask) { |
| 1719 | unsigned Mask = (unsigned) getZerosMask(); |
| 1720 | |
| 1721 | unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16; |
| 1722 | assert((ANDIMask != 0 || ANDISMask != 0) && |
| 1723 | "No set bits in zeros mask?"); |
| 1724 | |
| 1725 | if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) + |
| 1726 | (unsigned) (ANDISMask != 0) + |
| 1727 | (unsigned) (ANDIMask != 0 && ANDISMask != 0); |
| 1728 | |
| 1729 | SDValue ANDIVal, ANDISVal; |
| 1730 | if (ANDIMask != 0) |
| 1731 | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1732 | Res, getI32Imm(ANDIMask, dl)), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1733 | if (ANDISMask != 0) |
| 1734 | ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1735 | Res, getI32Imm(ANDISMask, dl)), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1736 | |
| 1737 | if (!ANDIVal) |
| 1738 | Res = ANDISVal; |
| 1739 | else if (!ANDISVal) |
| 1740 | Res = ANDIVal; |
| 1741 | else |
| 1742 | Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32, |
| 1743 | ANDIVal, ANDISVal), 0); |
| 1744 | } |
| 1745 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 1746 | return Res.getNode(); |
| 1747 | } |
| 1748 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1749 | unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32, |
| 1750 | unsigned MaskStart, unsigned MaskEnd, |
| 1751 | bool IsIns) { |
| 1752 | // In the notation used by the instructions, 'start' and 'end' are reversed |
| 1753 | // because bits are counted from high to low order. |
| 1754 | unsigned InstMaskStart = 64 - MaskEnd - 1, |
| 1755 | InstMaskEnd = 64 - MaskStart - 1; |
| 1756 | |
| 1757 | if (Repl32) |
| 1758 | return 1; |
| 1759 | |
| 1760 | if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) || |
| 1761 | InstMaskEnd == 63 - RLAmt) |
| 1762 | return 1; |
| 1763 | |
| 1764 | return 2; |
| 1765 | } |
| 1766 | |
| 1767 | // For 64-bit values, not all combinations of rotates and masks are |
| 1768 | // available. Produce one if it is available. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1769 | SDValue SelectRotMask64(SDValue V, const SDLoc &dl, unsigned RLAmt, |
| 1770 | bool Repl32, unsigned MaskStart, unsigned MaskEnd, |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1771 | unsigned *InstCnt = nullptr) { |
| 1772 | // In the notation used by the instructions, 'start' and 'end' are reversed |
| 1773 | // because bits are counted from high to low order. |
| 1774 | unsigned InstMaskStart = 64 - MaskEnd - 1, |
| 1775 | InstMaskEnd = 64 - MaskStart - 1; |
| 1776 | |
| 1777 | if (InstCnt) *InstCnt += 1; |
| 1778 | |
| 1779 | if (Repl32) { |
| 1780 | // This rotation amount assumes that the lower 32 bits of the quantity |
| 1781 | // are replicated in the high 32 bits by the rotation operator (which is |
| 1782 | // done by rlwinm and friends). |
| 1783 | assert(InstMaskStart >= 32 && "Mask cannot start out of range"); |
| 1784 | assert(InstMaskEnd >= 32 && "Mask cannot end out of range"); |
| 1785 | SDValue Ops[] = |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1786 | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
| 1787 | getI32Imm(InstMaskStart - 32, dl), getI32Imm(InstMaskEnd - 32, dl) }; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1788 | return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64, |
| 1789 | Ops), 0); |
| 1790 | } |
| 1791 | |
| 1792 | if (InstMaskEnd == 63) { |
| 1793 | SDValue Ops[] = |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1794 | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
| 1795 | getI32Imm(InstMaskStart, dl) }; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1796 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0); |
| 1797 | } |
| 1798 | |
| 1799 | if (InstMaskStart == 0) { |
| 1800 | SDValue Ops[] = |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1801 | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
| 1802 | getI32Imm(InstMaskEnd, dl) }; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1803 | return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0); |
| 1804 | } |
| 1805 | |
| 1806 | if (InstMaskEnd == 63 - RLAmt) { |
| 1807 | SDValue Ops[] = |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1808 | { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
| 1809 | getI32Imm(InstMaskStart, dl) }; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1810 | return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0); |
| 1811 | } |
| 1812 | |
| 1813 | // We cannot do this with a single instruction, so we'll use two. The |
| 1814 | // problem is that we're not free to choose both a rotation amount and mask |
| 1815 | // start and end independently. We can choose an arbitrary mask start and |
| 1816 | // end, but then the rotation amount is fixed. Rotation, however, can be |
| 1817 | // inverted, and so by applying an "inverse" rotation first, we can get the |
| 1818 | // desired result. |
| 1819 | if (InstCnt) *InstCnt += 1; |
| 1820 | |
| 1821 | // The rotation mask for the second instruction must be MaskStart. |
| 1822 | unsigned RLAmt2 = MaskStart; |
| 1823 | // The first instruction must rotate V so that the overall rotation amount |
| 1824 | // is RLAmt. |
| 1825 | unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64; |
| 1826 | if (RLAmt1) |
| 1827 | V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63); |
| 1828 | return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd); |
| 1829 | } |
| 1830 | |
| 1831 | // For 64-bit values, not all combinations of rotates and masks are |
| 1832 | // available. Produce a rotate-mask-and-insert if one is available. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1833 | SDValue SelectRotMaskIns64(SDValue Base, SDValue V, const SDLoc &dl, |
| 1834 | unsigned RLAmt, bool Repl32, unsigned MaskStart, |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1835 | unsigned MaskEnd, unsigned *InstCnt = nullptr) { |
| 1836 | // In the notation used by the instructions, 'start' and 'end' are reversed |
| 1837 | // because bits are counted from high to low order. |
| 1838 | unsigned InstMaskStart = 64 - MaskEnd - 1, |
| 1839 | InstMaskEnd = 64 - MaskStart - 1; |
| 1840 | |
| 1841 | if (InstCnt) *InstCnt += 1; |
| 1842 | |
| 1843 | if (Repl32) { |
| 1844 | // This rotation amount assumes that the lower 32 bits of the quantity |
| 1845 | // are replicated in the high 32 bits by the rotation operator (which is |
| 1846 | // done by rlwinm and friends). |
| 1847 | assert(InstMaskStart >= 32 && "Mask cannot start out of range"); |
| 1848 | assert(InstMaskEnd >= 32 && "Mask cannot end out of range"); |
| 1849 | SDValue Ops[] = |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1850 | { ExtendToInt64(Base, dl), ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
| 1851 | getI32Imm(InstMaskStart - 32, dl), getI32Imm(InstMaskEnd - 32, dl) }; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1852 | return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64, |
| 1853 | Ops), 0); |
| 1854 | } |
| 1855 | |
| 1856 | if (InstMaskEnd == 63 - RLAmt) { |
| 1857 | SDValue Ops[] = |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 1858 | { ExtendToInt64(Base, dl), ExtendToInt64(V, dl), getI32Imm(RLAmt, dl), |
| 1859 | getI32Imm(InstMaskStart, dl) }; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1860 | return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0); |
| 1861 | } |
| 1862 | |
| 1863 | // We cannot do this with a single instruction, so we'll use two. The |
| 1864 | // problem is that we're not free to choose both a rotation amount and mask |
| 1865 | // start and end independently. We can choose an arbitrary mask start and |
| 1866 | // end, but then the rotation amount is fixed. Rotation, however, can be |
| 1867 | // inverted, and so by applying an "inverse" rotation first, we can get the |
| 1868 | // desired result. |
| 1869 | if (InstCnt) *InstCnt += 1; |
| 1870 | |
| 1871 | // The rotation mask for the second instruction must be MaskStart. |
| 1872 | unsigned RLAmt2 = MaskStart; |
| 1873 | // The first instruction must rotate V so that the overall rotation amount |
| 1874 | // is RLAmt. |
| 1875 | unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64; |
| 1876 | if (RLAmt1) |
| 1877 | V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63); |
| 1878 | return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd); |
| 1879 | } |
| 1880 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1881 | void SelectAndParts64(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1882 | if (BPermRewriterNoMasking) |
| 1883 | return; |
| 1884 | |
| 1885 | // The idea here is the same as in the 32-bit version, but with additional |
| 1886 | // complications from the fact that Repl32 might be true. Because we |
| 1887 | // aggressively convert bit groups to Repl32 form (which, for small |
| 1888 | // rotation factors, involves no other change), and then coalesce, it might |
| 1889 | // be the case that a single 64-bit masking operation could handle both |
| 1890 | // some Repl32 groups and some non-Repl32 groups. If converting to Repl32 |
| 1891 | // form allowed coalescing, then we must use a 32-bit rotaton in order to |
| 1892 | // completely capture the new combined bit group. |
| 1893 | |
| 1894 | for (ValueRotInfo &VRI : ValueRotsVec) { |
| 1895 | uint64_t Mask = 0; |
| 1896 | |
| 1897 | // We need to add to the mask all bits from the associated bit groups. |
| 1898 | // If Repl32 is false, we need to add bits from bit groups that have |
| 1899 | // Repl32 true, but are trivially convertable to Repl32 false. Such a |
| 1900 | // group is trivially convertable if it overlaps only with the lower 32 |
| 1901 | // bits, and the group has not been coalesced. |
Benjamin Kramer | e7561b8 | 2015-06-20 15:59:41 +0000 | [diff] [blame] | 1902 | auto MatchingBG = [VRI](const BitGroup &BG) { |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1903 | if (VRI.V != BG.V) |
| 1904 | return false; |
| 1905 | |
| 1906 | unsigned EffRLAmt = BG.RLAmt; |
| 1907 | if (!VRI.Repl32 && BG.Repl32) { |
| 1908 | if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx && |
| 1909 | !BG.Repl32Coalesced) { |
| 1910 | if (BG.Repl32CR) |
| 1911 | EffRLAmt += 32; |
| 1912 | } else { |
| 1913 | return false; |
| 1914 | } |
| 1915 | } else if (VRI.Repl32 != BG.Repl32) { |
| 1916 | return false; |
| 1917 | } |
| 1918 | |
Alexander Kornienko | 175a7cb | 2015-12-28 13:38:42 +0000 | [diff] [blame] | 1919 | return VRI.RLAmt == EffRLAmt; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1920 | }; |
| 1921 | |
| 1922 | for (auto &BG : BitGroups) { |
| 1923 | if (!MatchingBG(BG)) |
| 1924 | continue; |
| 1925 | |
| 1926 | if (BG.StartIdx <= BG.EndIdx) { |
| 1927 | for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) |
Hal Finkel | ddf8d7d | 2015-01-01 19:33:59 +0000 | [diff] [blame] | 1928 | Mask |= (UINT64_C(1) << i); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1929 | } else { |
| 1930 | for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) |
Hal Finkel | ddf8d7d | 2015-01-01 19:33:59 +0000 | [diff] [blame] | 1931 | Mask |= (UINT64_C(1) << i); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1932 | for (unsigned i = 0; i <= BG.EndIdx; ++i) |
Hal Finkel | ddf8d7d | 2015-01-01 19:33:59 +0000 | [diff] [blame] | 1933 | Mask |= (UINT64_C(1) << i); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | // We can use the 32-bit andi/andis technique if the mask does not |
| 1938 | // require any higher-order bits. This can save an instruction compared |
| 1939 | // to always using the general 64-bit technique. |
| 1940 | bool Use32BitInsts = isUInt<32>(Mask); |
| 1941 | // Compute the masks for andi/andis that would be necessary. |
| 1942 | unsigned ANDIMask = (Mask & UINT16_MAX), |
| 1943 | ANDISMask = (Mask >> 16) & UINT16_MAX; |
| 1944 | |
| 1945 | bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)); |
| 1946 | |
| 1947 | unsigned NumAndInsts = (unsigned) NeedsRotate + |
| 1948 | (unsigned) (bool) Res; |
| 1949 | if (Use32BitInsts) |
| 1950 | NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) + |
| 1951 | (unsigned) (ANDIMask != 0 && ANDISMask != 0); |
| 1952 | else |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 1953 | NumAndInsts += selectI64ImmInstrCount(Mask) + /* and */ 1; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1954 | |
| 1955 | unsigned NumRLInsts = 0; |
| 1956 | bool FirstBG = true; |
Guozhi Wei | 0cd6542 | 2016-10-14 20:41:50 +0000 | [diff] [blame] | 1957 | bool MoreBG = false; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1958 | for (auto &BG : BitGroups) { |
Guozhi Wei | 0cd6542 | 2016-10-14 20:41:50 +0000 | [diff] [blame] | 1959 | if (!MatchingBG(BG)) { |
| 1960 | MoreBG = true; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1961 | continue; |
Guozhi Wei | 0cd6542 | 2016-10-14 20:41:50 +0000 | [diff] [blame] | 1962 | } |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1963 | NumRLInsts += |
| 1964 | SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx, |
| 1965 | !FirstBG); |
| 1966 | FirstBG = false; |
| 1967 | } |
| 1968 | |
| 1969 | DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() << |
| 1970 | " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") << |
| 1971 | "\n\t\t\tisel using masking: " << NumAndInsts << |
| 1972 | " using rotates: " << NumRLInsts << "\n"); |
| 1973 | |
| 1974 | // When we'd use andi/andis, we bias toward using the rotates (andi only |
| 1975 | // has a record form, and is cracked on POWER cores). However, when using |
| 1976 | // general 64-bit constant formation, bias toward the constant form, |
| 1977 | // because that exposes more opportunities for CSE. |
| 1978 | if (NumAndInsts > NumRLInsts) |
| 1979 | continue; |
Guozhi Wei | 0cd6542 | 2016-10-14 20:41:50 +0000 | [diff] [blame] | 1980 | // When merging multiple bit groups, instruction or is used. |
| 1981 | // But when rotate is used, rldimi can inert the rotated value into any |
| 1982 | // register, so instruction or can be avoided. |
| 1983 | if ((Use32BitInsts || MoreBG) && NumAndInsts == NumRLInsts) |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 1984 | continue; |
| 1985 | |
| 1986 | DEBUG(dbgs() << "\t\t\t\tusing masking\n"); |
| 1987 | |
| 1988 | if (InstCnt) *InstCnt += NumAndInsts; |
| 1989 | |
| 1990 | SDValue VRot; |
| 1991 | // We actually need to generate a rotation if we have a non-zero rotation |
| 1992 | // factor or, in the Repl32 case, if we care about any of the |
| 1993 | // higher-order replicated bits. In the latter case, we generate a mask |
| 1994 | // backward so that it actually includes the entire 64 bits. |
| 1995 | if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask))) |
| 1996 | VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32, |
| 1997 | VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63); |
| 1998 | else |
| 1999 | VRot = VRI.V; |
| 2000 | |
| 2001 | SDValue TotalVal; |
| 2002 | if (Use32BitInsts) { |
| 2003 | assert((ANDIMask != 0 || ANDISMask != 0) && |
| 2004 | "No set bits in mask when using 32-bit ands for 64-bit value"); |
| 2005 | |
| 2006 | SDValue ANDIVal, ANDISVal; |
| 2007 | if (ANDIMask != 0) |
| 2008 | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2009 | ExtendToInt64(VRot, dl), |
| 2010 | getI32Imm(ANDIMask, dl)), |
| 2011 | 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2012 | if (ANDISMask != 0) |
| 2013 | ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2014 | ExtendToInt64(VRot, dl), |
| 2015 | getI32Imm(ANDISMask, dl)), |
| 2016 | 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (!ANDIVal) |
| 2019 | TotalVal = ANDISVal; |
| 2020 | else if (!ANDISVal) |
| 2021 | TotalVal = ANDIVal; |
| 2022 | else |
| 2023 | TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2024 | ExtendToInt64(ANDIVal, dl), ANDISVal), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2025 | } else { |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 2026 | TotalVal = SDValue(selectI64Imm(CurDAG, dl, Mask), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2027 | TotalVal = |
| 2028 | SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2029 | ExtendToInt64(VRot, dl), TotalVal), |
| 2030 | 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | if (!Res) |
| 2034 | Res = TotalVal; |
| 2035 | else |
| 2036 | Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2037 | ExtendToInt64(Res, dl), TotalVal), |
| 2038 | 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Now, remove all groups with this underlying value and rotation |
| 2041 | // factor. |
Benjamin Kramer | e7561b8 | 2015-06-20 15:59:41 +0000 | [diff] [blame] | 2042 | eraseMatchingBitGroups(MatchingBG); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | // Instruction selection for the 64-bit case. |
| 2047 | SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) { |
| 2048 | SDLoc dl(N); |
| 2049 | SDValue Res; |
| 2050 | |
| 2051 | if (InstCnt) *InstCnt = 0; |
| 2052 | |
| 2053 | // Take care of cases that should use andi/andis first. |
| 2054 | SelectAndParts64(dl, Res, InstCnt); |
| 2055 | |
| 2056 | // If we've not yet selected a 'starting' instruction, and we have no zeros |
| 2057 | // to fill in, select the (Value, RLAmt) with the highest priority (largest |
| 2058 | // number of groups), and start with this rotated value. |
| 2059 | if ((!HasZeros || LateMask) && !Res) { |
| 2060 | // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32 |
| 2061 | // groups will come first, and so the VRI representing the largest number |
| 2062 | // of groups might not be first (it might be the first Repl32 groups). |
| 2063 | unsigned MaxGroupsIdx = 0; |
| 2064 | if (!ValueRotsVec[0].Repl32) { |
| 2065 | for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i) |
| 2066 | if (ValueRotsVec[i].Repl32) { |
| 2067 | if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups) |
| 2068 | MaxGroupsIdx = i; |
| 2069 | break; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx]; |
| 2074 | bool NeedsRotate = false; |
| 2075 | if (VRI.RLAmt) { |
| 2076 | NeedsRotate = true; |
| 2077 | } else if (VRI.Repl32) { |
| 2078 | for (auto &BG : BitGroups) { |
| 2079 | if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt || |
| 2080 | BG.Repl32 != VRI.Repl32) |
| 2081 | continue; |
| 2082 | |
| 2083 | // We don't need a rotate if the bit group is confined to the lower |
| 2084 | // 32 bits. |
| 2085 | if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx) |
| 2086 | continue; |
| 2087 | |
| 2088 | NeedsRotate = true; |
| 2089 | break; |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | if (NeedsRotate) |
| 2094 | Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32, |
| 2095 | VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63, |
| 2096 | InstCnt); |
| 2097 | else |
| 2098 | Res = VRI.V; |
| 2099 | |
| 2100 | // Now, remove all groups with this underlying value and rotation factor. |
| 2101 | if (Res) |
Benjamin Kramer | e7561b8 | 2015-06-20 15:59:41 +0000 | [diff] [blame] | 2102 | eraseMatchingBitGroups([VRI](const BitGroup &BG) { |
| 2103 | return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt && |
| 2104 | BG.Repl32 == VRI.Repl32; |
| 2105 | }); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | // Because 64-bit rotates are more flexible than inserts, we might have a |
| 2109 | // preference regarding which one we do first (to save one instruction). |
| 2110 | if (!Res) |
| 2111 | for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) { |
| 2112 | if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx, |
| 2113 | false) < |
| 2114 | SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx, |
| 2115 | true)) { |
| 2116 | if (I != BitGroups.begin()) { |
| 2117 | BitGroup BG = *I; |
| 2118 | BitGroups.erase(I); |
| 2119 | BitGroups.insert(BitGroups.begin(), BG); |
| 2120 | } |
| 2121 | |
| 2122 | break; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | // Insert the other groups (one at a time). |
| 2127 | for (auto &BG : BitGroups) { |
| 2128 | if (!Res) |
| 2129 | Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx, |
| 2130 | BG.EndIdx, InstCnt); |
| 2131 | else |
| 2132 | Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32, |
| 2133 | BG.StartIdx, BG.EndIdx, InstCnt); |
| 2134 | } |
| 2135 | |
| 2136 | if (LateMask) { |
| 2137 | uint64_t Mask = getZerosMask(); |
| 2138 | |
| 2139 | // We can use the 32-bit andi/andis technique if the mask does not |
| 2140 | // require any higher-order bits. This can save an instruction compared |
| 2141 | // to always using the general 64-bit technique. |
| 2142 | bool Use32BitInsts = isUInt<32>(Mask); |
| 2143 | // Compute the masks for andi/andis that would be necessary. |
| 2144 | unsigned ANDIMask = (Mask & UINT16_MAX), |
| 2145 | ANDISMask = (Mask >> 16) & UINT16_MAX; |
| 2146 | |
| 2147 | if (Use32BitInsts) { |
| 2148 | assert((ANDIMask != 0 || ANDISMask != 0) && |
| 2149 | "No set bits in mask when using 32-bit ands for 64-bit value"); |
| 2150 | |
| 2151 | if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) + |
| 2152 | (unsigned) (ANDISMask != 0) + |
| 2153 | (unsigned) (ANDIMask != 0 && ANDISMask != 0); |
| 2154 | |
| 2155 | SDValue ANDIVal, ANDISVal; |
| 2156 | if (ANDIMask != 0) |
| 2157 | ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2158 | ExtendToInt64(Res, dl), getI32Imm(ANDIMask, dl)), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2159 | if (ANDISMask != 0) |
| 2160 | ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2161 | ExtendToInt64(Res, dl), getI32Imm(ANDISMask, dl)), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2162 | |
| 2163 | if (!ANDIVal) |
| 2164 | Res = ANDISVal; |
| 2165 | else if (!ANDISVal) |
| 2166 | Res = ANDIVal; |
| 2167 | else |
| 2168 | Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2169 | ExtendToInt64(ANDIVal, dl), ANDISVal), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2170 | } else { |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 2171 | if (InstCnt) *InstCnt += selectI64ImmInstrCount(Mask) + /* and */ 1; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2172 | |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 2173 | SDValue MaskVal = SDValue(selectI64Imm(CurDAG, dl, Mask), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2174 | Res = |
| 2175 | SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64, |
Hiroshi Inoue | dcedd66 | 2017-10-02 09:24:00 +0000 | [diff] [blame] | 2176 | ExtendToInt64(Res, dl), MaskVal), 0); |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | return Res.getNode(); |
| 2181 | } |
| 2182 | |
| 2183 | SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) { |
| 2184 | // Fill in BitGroups. |
| 2185 | collectBitGroups(LateMask); |
| 2186 | if (BitGroups.empty()) |
| 2187 | return nullptr; |
| 2188 | |
| 2189 | // For 64-bit values, figure out when we can use 32-bit instructions. |
| 2190 | if (Bits.size() == 64) |
| 2191 | assignRepl32BitGroups(); |
| 2192 | |
| 2193 | // Fill in ValueRotsVec. |
| 2194 | collectValueRotInfo(); |
| 2195 | |
| 2196 | if (Bits.size() == 32) { |
| 2197 | return Select32(N, LateMask, InstCnt); |
| 2198 | } else { |
| 2199 | assert(Bits.size() == 64 && "Not 64 bits here?"); |
| 2200 | return Select64(N, LateMask, InstCnt); |
| 2201 | } |
| 2202 | |
| 2203 | return nullptr; |
| 2204 | } |
| 2205 | |
Benjamin Kramer | e7561b8 | 2015-06-20 15:59:41 +0000 | [diff] [blame] | 2206 | void eraseMatchingBitGroups(function_ref<bool(const BitGroup &)> F) { |
David Majnemer | c700490 | 2016-08-12 04:32:37 +0000 | [diff] [blame] | 2207 | BitGroups.erase(remove_if(BitGroups, F), BitGroups.end()); |
Benjamin Kramer | e7561b8 | 2015-06-20 15:59:41 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 2210 | SmallVector<ValueBit, 64> Bits; |
| 2211 | |
| 2212 | bool HasZeros; |
| 2213 | SmallVector<unsigned, 64> RLAmt; |
| 2214 | |
| 2215 | SmallVector<BitGroup, 16> BitGroups; |
| 2216 | |
| 2217 | DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots; |
| 2218 | SmallVector<ValueRotInfo, 16> ValueRotsVec; |
| 2219 | |
| 2220 | SelectionDAG *CurDAG; |
| 2221 | |
| 2222 | public: |
| 2223 | BitPermutationSelector(SelectionDAG *DAG) |
| 2224 | : CurDAG(DAG) {} |
| 2225 | |
| 2226 | // Here we try to match complex bit permutations into a set of |
| 2227 | // rotate-and-shift/shift/and/or instructions, using a set of heuristics |
| 2228 | // known to produce optimial code for common cases (like i32 byte swapping). |
| 2229 | SDNode *Select(SDNode *N) { |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 2230 | Memoizer.clear(); |
| 2231 | auto Result = |
| 2232 | getValueBits(SDValue(N, 0), N->getValueType(0).getSizeInBits()); |
| 2233 | if (!Result.first) |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 2234 | return nullptr; |
Tim Shen | dc698c3 | 2016-08-12 18:40:04 +0000 | [diff] [blame] | 2235 | Bits = std::move(*Result.second); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 2236 | |
| 2237 | DEBUG(dbgs() << "Considering bit-permutation-based instruction" |
| 2238 | " selection for: "); |
| 2239 | DEBUG(N->dump(CurDAG)); |
| 2240 | |
| 2241 | // Fill it RLAmt and set HasZeros. |
| 2242 | computeRotationAmounts(); |
| 2243 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2244 | if (!HasZeros) |
| 2245 | return Select(N, false); |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 2246 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2247 | // We currently have two techniques for handling results with zeros: early |
| 2248 | // masking (the default) and late masking. Late masking is sometimes more |
| 2249 | // efficient, but because the structure of the bit groups is different, it |
| 2250 | // is hard to tell without generating both and comparing the results. With |
| 2251 | // late masking, we ignore zeros in the resulting value when inserting each |
| 2252 | // set of bit groups, and then mask in the zeros at the end. With early |
| 2253 | // 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] | 2254 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2255 | unsigned InstCnt, InstCntLateMask; |
| 2256 | DEBUG(dbgs() << "\tEarly masking:\n"); |
| 2257 | SDNode *RN = Select(N, false, &InstCnt); |
| 2258 | DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n"); |
| 2259 | |
| 2260 | DEBUG(dbgs() << "\tLate masking:\n"); |
| 2261 | SDNode *RNLM = Select(N, true, &InstCntLateMask); |
| 2262 | DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask << |
| 2263 | " instructions\n"); |
| 2264 | |
| 2265 | if (InstCnt <= InstCntLateMask) { |
| 2266 | DEBUG(dbgs() << "\tUsing early-masking for isel\n"); |
| 2267 | return RN; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 2270 | DEBUG(dbgs() << "\tUsing late-masking for isel\n"); |
| 2271 | return RNLM; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 2272 | } |
| 2273 | }; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 2274 | |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 2275 | class IntegerCompareEliminator { |
| 2276 | SelectionDAG *CurDAG; |
| 2277 | PPCDAGToDAGISel *S; |
| 2278 | // Conversion type for interpreting results of a 32-bit instruction as |
| 2279 | // a 64-bit value or vice versa. |
| 2280 | enum ExtOrTruncConversion { Ext, Trunc }; |
| 2281 | |
| 2282 | // Modifiers to guide how an ISD::SETCC node's result is to be computed |
| 2283 | // in a GPR. |
| 2284 | // ZExtOrig - use the original condition code, zero-extend value |
| 2285 | // ZExtInvert - invert the condition code, zero-extend value |
| 2286 | // SExtOrig - use the original condition code, sign-extend value |
| 2287 | // SExtInvert - invert the condition code, sign-extend value |
| 2288 | enum SetccInGPROpts { ZExtOrig, ZExtInvert, SExtOrig, SExtInvert }; |
| 2289 | |
| 2290 | // Comparisons against zero to emit GPR code sequences for. Each of these |
| 2291 | // sequences may need to be emitted for two or more equivalent patterns. |
| 2292 | // For example (a >= 0) == (a > -1). The direction of the comparison (</>) |
| 2293 | // matters as well as the extension type: sext (-1/0), zext (1/0). |
| 2294 | // GEZExt - (zext (LHS >= 0)) |
| 2295 | // GESExt - (sext (LHS >= 0)) |
| 2296 | // LEZExt - (zext (LHS <= 0)) |
| 2297 | // LESExt - (sext (LHS <= 0)) |
| 2298 | enum ZeroCompare { GEZExt, GESExt, LEZExt, LESExt }; |
| 2299 | |
| 2300 | SDNode *tryEXTEND(SDNode *N); |
| 2301 | SDNode *tryLogicOpOfCompares(SDNode *N); |
| 2302 | SDValue computeLogicOpInGPR(SDValue LogicOp); |
| 2303 | SDValue signExtendInputIfNeeded(SDValue Input); |
| 2304 | SDValue zeroExtendInputIfNeeded(SDValue Input); |
| 2305 | SDValue addExtOrTrunc(SDValue NatWidthRes, ExtOrTruncConversion Conv); |
| 2306 | SDValue getCompoundZeroComparisonInGPR(SDValue LHS, SDLoc dl, |
| 2307 | ZeroCompare CmpTy); |
| 2308 | SDValue get32BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
| 2309 | int64_t RHSValue, SDLoc dl); |
| 2310 | SDValue get32BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
| 2311 | int64_t RHSValue, SDLoc dl); |
| 2312 | SDValue get64BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
| 2313 | int64_t RHSValue, SDLoc dl); |
| 2314 | SDValue get64BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
| 2315 | int64_t RHSValue, SDLoc dl); |
| 2316 | SDValue getSETCCInGPR(SDValue Compare, SetccInGPROpts ConvOpts); |
| 2317 | |
| 2318 | public: |
| 2319 | IntegerCompareEliminator(SelectionDAG *DAG, |
| 2320 | PPCDAGToDAGISel *Sel) : CurDAG(DAG), S(Sel) { |
| 2321 | assert(CurDAG->getTargetLoweringInfo() |
| 2322 | .getPointerTy(CurDAG->getDataLayout()).getSizeInBits() == 64 && |
| 2323 | "Only expecting to use this on 64 bit targets."); |
| 2324 | } |
| 2325 | SDNode *Select(SDNode *N) { |
| 2326 | if (CmpInGPR == ICGPR_None) |
| 2327 | return nullptr; |
| 2328 | switch (N->getOpcode()) { |
| 2329 | default: break; |
| 2330 | case ISD::ZERO_EXTEND: |
| 2331 | if (CmpInGPR == ICGPR_Sext || CmpInGPR == ICGPR_SextI32 || |
| 2332 | CmpInGPR == ICGPR_SextI64) |
| 2333 | return nullptr; |
Nemanja Ivanovic | 1794cdc | 2017-12-15 11:47:48 +0000 | [diff] [blame] | 2334 | LLVM_FALLTHROUGH; |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 2335 | case ISD::SIGN_EXTEND: |
| 2336 | if (CmpInGPR == ICGPR_Zext || CmpInGPR == ICGPR_ZextI32 || |
| 2337 | CmpInGPR == ICGPR_ZextI64) |
| 2338 | return nullptr; |
| 2339 | return tryEXTEND(N); |
| 2340 | case ISD::AND: |
| 2341 | case ISD::OR: |
| 2342 | case ISD::XOR: |
| 2343 | return tryLogicOpOfCompares(N); |
| 2344 | } |
| 2345 | return nullptr; |
| 2346 | } |
| 2347 | }; |
| 2348 | |
| 2349 | static bool isLogicOp(unsigned Opc) { |
| 2350 | return Opc == ISD::AND || Opc == ISD::OR || Opc == ISD::XOR; |
| 2351 | } |
| 2352 | // The obvious case for wanting to keep the value in a GPR. Namely, the |
| 2353 | // result of the comparison is actually needed in a GPR. |
| 2354 | SDNode *IntegerCompareEliminator::tryEXTEND(SDNode *N) { |
| 2355 | assert((N->getOpcode() == ISD::ZERO_EXTEND || |
| 2356 | N->getOpcode() == ISD::SIGN_EXTEND) && |
| 2357 | "Expecting a zero/sign extend node!"); |
| 2358 | SDValue WideRes; |
| 2359 | // If we are zero-extending the result of a logical operation on i1 |
| 2360 | // values, we can keep the values in GPRs. |
| 2361 | if (isLogicOp(N->getOperand(0).getOpcode()) && |
| 2362 | N->getOperand(0).getValueType() == MVT::i1 && |
| 2363 | N->getOpcode() == ISD::ZERO_EXTEND) |
| 2364 | WideRes = computeLogicOpInGPR(N->getOperand(0)); |
| 2365 | else if (N->getOperand(0).getOpcode() != ISD::SETCC) |
| 2366 | return nullptr; |
| 2367 | else |
| 2368 | WideRes = |
| 2369 | getSETCCInGPR(N->getOperand(0), |
| 2370 | N->getOpcode() == ISD::SIGN_EXTEND ? |
| 2371 | SetccInGPROpts::SExtOrig : SetccInGPROpts::ZExtOrig); |
| 2372 | |
| 2373 | if (!WideRes) |
| 2374 | return nullptr; |
| 2375 | |
| 2376 | SDLoc dl(N); |
| 2377 | bool Input32Bit = WideRes.getValueType() == MVT::i32; |
| 2378 | bool Output32Bit = N->getValueType(0) == MVT::i32; |
| 2379 | |
| 2380 | NumSextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 1 : 0; |
| 2381 | NumZextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 0 : 1; |
| 2382 | |
| 2383 | SDValue ConvOp = WideRes; |
| 2384 | if (Input32Bit != Output32Bit) |
| 2385 | ConvOp = addExtOrTrunc(WideRes, Input32Bit ? ExtOrTruncConversion::Ext : |
| 2386 | ExtOrTruncConversion::Trunc); |
| 2387 | return ConvOp.getNode(); |
| 2388 | } |
| 2389 | |
| 2390 | // Attempt to perform logical operations on the results of comparisons while |
| 2391 | // keeping the values in GPRs. Without doing so, these would end up being |
| 2392 | // lowered to CR-logical operations which suffer from significant latency and |
| 2393 | // low ILP. |
| 2394 | SDNode *IntegerCompareEliminator::tryLogicOpOfCompares(SDNode *N) { |
| 2395 | if (N->getValueType(0) != MVT::i1) |
| 2396 | return nullptr; |
| 2397 | assert(isLogicOp(N->getOpcode()) && |
| 2398 | "Expected a logic operation on setcc results."); |
| 2399 | SDValue LoweredLogical = computeLogicOpInGPR(SDValue(N, 0)); |
| 2400 | if (!LoweredLogical) |
| 2401 | return nullptr; |
| 2402 | |
| 2403 | SDLoc dl(N); |
| 2404 | bool IsBitwiseNegate = LoweredLogical.getMachineOpcode() == PPC::XORI8; |
| 2405 | unsigned SubRegToExtract = IsBitwiseNegate ? PPC::sub_eq : PPC::sub_gt; |
| 2406 | SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32); |
| 2407 | SDValue LHS = LoweredLogical.getOperand(0); |
| 2408 | SDValue RHS = LoweredLogical.getOperand(1); |
| 2409 | SDValue WideOp; |
| 2410 | SDValue OpToConvToRecForm; |
| 2411 | |
| 2412 | // Look through any 32-bit to 64-bit implicit extend nodes to find the |
| 2413 | // opcode that is input to the XORI. |
| 2414 | if (IsBitwiseNegate && |
| 2415 | LoweredLogical.getOperand(0).getMachineOpcode() == PPC::INSERT_SUBREG) |
| 2416 | OpToConvToRecForm = LoweredLogical.getOperand(0).getOperand(1); |
| 2417 | else if (IsBitwiseNegate) |
| 2418 | // If the input to the XORI isn't an extension, that's what we're after. |
| 2419 | OpToConvToRecForm = LoweredLogical.getOperand(0); |
| 2420 | else |
| 2421 | // If this is not an XORI, it is a reg-reg logical op and we can convert |
| 2422 | // it to record-form. |
| 2423 | OpToConvToRecForm = LoweredLogical; |
| 2424 | |
| 2425 | // Get the record-form version of the node we're looking to use to get the |
| 2426 | // CR result from. |
| 2427 | uint16_t NonRecOpc = OpToConvToRecForm.getMachineOpcode(); |
| 2428 | int NewOpc = PPCInstrInfo::getRecordFormOpcode(NonRecOpc); |
| 2429 | |
| 2430 | // Convert the right node to record-form. This is either the logical we're |
| 2431 | // looking at or it is the input node to the negation (if we're looking at |
| 2432 | // a bitwise negation). |
| 2433 | if (NewOpc != -1 && IsBitwiseNegate) { |
| 2434 | // The input to the XORI has a record-form. Use it. |
| 2435 | assert(LoweredLogical.getConstantOperandVal(1) == 1 && |
| 2436 | "Expected a PPC::XORI8 only for bitwise negation."); |
| 2437 | // Emit the record-form instruction. |
| 2438 | std::vector<SDValue> Ops; |
| 2439 | for (int i = 0, e = OpToConvToRecForm.getNumOperands(); i < e; i++) |
| 2440 | Ops.push_back(OpToConvToRecForm.getOperand(i)); |
| 2441 | |
| 2442 | WideOp = |
| 2443 | SDValue(CurDAG->getMachineNode(NewOpc, dl, |
| 2444 | OpToConvToRecForm.getValueType(), |
| 2445 | MVT::Glue, Ops), 0); |
| 2446 | } else { |
| 2447 | assert((NewOpc != -1 || !IsBitwiseNegate) && |
| 2448 | "No record form available for AND8/OR8/XOR8?"); |
| 2449 | WideOp = |
| 2450 | SDValue(CurDAG->getMachineNode(NewOpc == -1 ? PPC::ANDIo8 : NewOpc, dl, |
| 2451 | MVT::i64, MVT::Glue, LHS, RHS), 0); |
| 2452 | } |
| 2453 | |
| 2454 | // Select this node to a single bit from CR0 set by the record-form node |
| 2455 | // just created. For bitwise negation, use the EQ bit which is the equivalent |
| 2456 | // of negating the result (i.e. it is a bit set when the result of the |
| 2457 | // operation is zero). |
| 2458 | SDValue SRIdxVal = |
| 2459 | CurDAG->getTargetConstant(SubRegToExtract, dl, MVT::i32); |
| 2460 | SDValue CRBit = |
| 2461 | SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, |
| 2462 | MVT::i1, CR0Reg, SRIdxVal, |
| 2463 | WideOp.getValue(1)), 0); |
| 2464 | return CRBit.getNode(); |
| 2465 | } |
| 2466 | |
| 2467 | // Lower a logical operation on i1 values into a GPR sequence if possible. |
| 2468 | // The result can be kept in a GPR if requested. |
| 2469 | // Three types of inputs can be handled: |
| 2470 | // - SETCC |
| 2471 | // - TRUNCATE |
| 2472 | // - Logical operation (AND/OR/XOR) |
| 2473 | // There is also a special case that is handled (namely a complement operation |
| 2474 | // achieved with xor %a, -1). |
| 2475 | SDValue IntegerCompareEliminator::computeLogicOpInGPR(SDValue LogicOp) { |
| 2476 | assert(isLogicOp(LogicOp.getOpcode()) && |
| 2477 | "Can only handle logic operations here."); |
| 2478 | assert(LogicOp.getValueType() == MVT::i1 && |
| 2479 | "Can only handle logic operations on i1 values here."); |
| 2480 | SDLoc dl(LogicOp); |
| 2481 | SDValue LHS, RHS; |
| 2482 | |
| 2483 | // Special case: xor %a, -1 |
| 2484 | bool IsBitwiseNegation = isBitwiseNot(LogicOp); |
| 2485 | |
| 2486 | // Produces a GPR sequence for each operand of the binary logic operation. |
| 2487 | // For SETCC, it produces the respective comparison, for TRUNCATE it truncates |
| 2488 | // the value in a GPR and for logic operations, it will recursively produce |
| 2489 | // a GPR sequence for the operation. |
| 2490 | auto getLogicOperand = [&] (SDValue Operand) -> SDValue { |
| 2491 | unsigned OperandOpcode = Operand.getOpcode(); |
| 2492 | if (OperandOpcode == ISD::SETCC) |
| 2493 | return getSETCCInGPR(Operand, SetccInGPROpts::ZExtOrig); |
| 2494 | else if (OperandOpcode == ISD::TRUNCATE) { |
| 2495 | SDValue InputOp = Operand.getOperand(0); |
| 2496 | EVT InVT = InputOp.getValueType(); |
| 2497 | return SDValue(CurDAG->getMachineNode(InVT == MVT::i32 ? PPC::RLDICL_32 : |
| 2498 | PPC::RLDICL, dl, InVT, InputOp, |
| 2499 | S->getI64Imm(0, dl), |
| 2500 | S->getI64Imm(63, dl)), 0); |
| 2501 | } else if (isLogicOp(OperandOpcode)) |
| 2502 | return computeLogicOpInGPR(Operand); |
| 2503 | return SDValue(); |
| 2504 | }; |
| 2505 | LHS = getLogicOperand(LogicOp.getOperand(0)); |
| 2506 | RHS = getLogicOperand(LogicOp.getOperand(1)); |
| 2507 | |
| 2508 | // If a GPR sequence can't be produced for the LHS we can't proceed. |
| 2509 | // Not producing a GPR sequence for the RHS is only a problem if this isn't |
| 2510 | // a bitwise negation operation. |
| 2511 | if (!LHS || (!RHS && !IsBitwiseNegation)) |
| 2512 | return SDValue(); |
| 2513 | |
| 2514 | NumLogicOpsOnComparison++; |
| 2515 | |
| 2516 | // We will use the inputs as 64-bit values. |
| 2517 | if (LHS.getValueType() == MVT::i32) |
| 2518 | LHS = addExtOrTrunc(LHS, ExtOrTruncConversion::Ext); |
| 2519 | if (!IsBitwiseNegation && RHS.getValueType() == MVT::i32) |
| 2520 | RHS = addExtOrTrunc(RHS, ExtOrTruncConversion::Ext); |
| 2521 | |
| 2522 | unsigned NewOpc; |
| 2523 | switch (LogicOp.getOpcode()) { |
| 2524 | default: llvm_unreachable("Unknown logic operation."); |
| 2525 | case ISD::AND: NewOpc = PPC::AND8; break; |
| 2526 | case ISD::OR: NewOpc = PPC::OR8; break; |
| 2527 | case ISD::XOR: NewOpc = PPC::XOR8; break; |
| 2528 | } |
| 2529 | |
| 2530 | if (IsBitwiseNegation) { |
| 2531 | RHS = S->getI64Imm(1, dl); |
| 2532 | NewOpc = PPC::XORI8; |
| 2533 | } |
| 2534 | |
| 2535 | return SDValue(CurDAG->getMachineNode(NewOpc, dl, MVT::i64, LHS, RHS), 0); |
| 2536 | |
| 2537 | } |
| 2538 | |
| 2539 | /// If the value isn't guaranteed to be sign-extended to 64-bits, extend it. |
| 2540 | /// Otherwise just reinterpret it as a 64-bit value. |
| 2541 | /// Useful when emitting comparison code for 32-bit values without using |
| 2542 | /// the compare instruction (which only considers the lower 32-bits). |
| 2543 | SDValue IntegerCompareEliminator::signExtendInputIfNeeded(SDValue Input) { |
| 2544 | assert(Input.getValueType() == MVT::i32 && |
| 2545 | "Can only sign-extend 32-bit values here."); |
| 2546 | unsigned Opc = Input.getOpcode(); |
| 2547 | |
| 2548 | // The value was sign extended and then truncated to 32-bits. No need to |
| 2549 | // sign extend it again. |
| 2550 | if (Opc == ISD::TRUNCATE && |
| 2551 | (Input.getOperand(0).getOpcode() == ISD::AssertSext || |
| 2552 | Input.getOperand(0).getOpcode() == ISD::SIGN_EXTEND)) |
| 2553 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
| 2554 | |
| 2555 | LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input); |
| 2556 | // The input is a sign-extending load. All ppc sign-extending loads |
| 2557 | // sign-extend to the full 64-bits. |
| 2558 | if (InputLoad && InputLoad->getExtensionType() == ISD::SEXTLOAD) |
| 2559 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
| 2560 | |
| 2561 | ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input); |
| 2562 | // We don't sign-extend constants. |
| 2563 | if (InputConst) |
| 2564 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
| 2565 | |
| 2566 | SDLoc dl(Input); |
| 2567 | SignExtensionsAdded++; |
| 2568 | return SDValue(CurDAG->getMachineNode(PPC::EXTSW_32_64, dl, |
| 2569 | MVT::i64, Input), 0); |
| 2570 | } |
| 2571 | |
| 2572 | /// If the value isn't guaranteed to be zero-extended to 64-bits, extend it. |
| 2573 | /// Otherwise just reinterpret it as a 64-bit value. |
| 2574 | /// Useful when emitting comparison code for 32-bit values without using |
| 2575 | /// the compare instruction (which only considers the lower 32-bits). |
| 2576 | SDValue IntegerCompareEliminator::zeroExtendInputIfNeeded(SDValue Input) { |
| 2577 | assert(Input.getValueType() == MVT::i32 && |
| 2578 | "Can only zero-extend 32-bit values here."); |
| 2579 | unsigned Opc = Input.getOpcode(); |
| 2580 | |
| 2581 | // The only condition under which we can omit the actual extend instruction: |
| 2582 | // - The value is a positive constant |
| 2583 | // - The value comes from a load that isn't a sign-extending load |
| 2584 | // An ISD::TRUNCATE needs to be zero-extended unless it is fed by a zext. |
| 2585 | bool IsTruncateOfZExt = Opc == ISD::TRUNCATE && |
| 2586 | (Input.getOperand(0).getOpcode() == ISD::AssertZext || |
| 2587 | Input.getOperand(0).getOpcode() == ISD::ZERO_EXTEND); |
| 2588 | if (IsTruncateOfZExt) |
| 2589 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
| 2590 | |
| 2591 | ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input); |
| 2592 | if (InputConst && InputConst->getSExtValue() >= 0) |
| 2593 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
| 2594 | |
| 2595 | LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input); |
| 2596 | // The input is a load that doesn't sign-extend (it will be zero-extended). |
| 2597 | if (InputLoad && InputLoad->getExtensionType() != ISD::SEXTLOAD) |
| 2598 | return addExtOrTrunc(Input, ExtOrTruncConversion::Ext); |
| 2599 | |
| 2600 | // None of the above, need to zero-extend. |
| 2601 | SDLoc dl(Input); |
| 2602 | ZeroExtensionsAdded++; |
| 2603 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL_32_64, dl, MVT::i64, Input, |
| 2604 | S->getI64Imm(0, dl), |
| 2605 | S->getI64Imm(32, dl)), 0); |
| 2606 | } |
| 2607 | |
| 2608 | // Handle a 32-bit value in a 64-bit register and vice-versa. These are of |
| 2609 | // course not actual zero/sign extensions that will generate machine code, |
| 2610 | // they're just a way to reinterpret a 32 bit value in a register as a |
| 2611 | // 64 bit value and vice-versa. |
| 2612 | SDValue IntegerCompareEliminator::addExtOrTrunc(SDValue NatWidthRes, |
| 2613 | ExtOrTruncConversion Conv) { |
| 2614 | SDLoc dl(NatWidthRes); |
| 2615 | |
| 2616 | // For reinterpreting 32-bit values as 64 bit values, we generate |
| 2617 | // INSERT_SUBREG IMPLICIT_DEF:i64, <input>, TargetConstant:i32<1> |
| 2618 | if (Conv == ExtOrTruncConversion::Ext) { |
| 2619 | SDValue ImDef(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, MVT::i64), 0); |
| 2620 | SDValue SubRegIdx = |
| 2621 | CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32); |
| 2622 | return SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, MVT::i64, |
| 2623 | ImDef, NatWidthRes, SubRegIdx), 0); |
| 2624 | } |
| 2625 | |
| 2626 | assert(Conv == ExtOrTruncConversion::Trunc && |
| 2627 | "Unknown convertion between 32 and 64 bit values."); |
| 2628 | // For reinterpreting 64-bit values as 32-bit values, we just need to |
| 2629 | // EXTRACT_SUBREG (i.e. extract the low word). |
| 2630 | SDValue SubRegIdx = |
| 2631 | CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32); |
| 2632 | return SDValue(CurDAG->getMachineNode(PPC::EXTRACT_SUBREG, dl, MVT::i32, |
| 2633 | NatWidthRes, SubRegIdx), 0); |
| 2634 | } |
| 2635 | |
| 2636 | // Produce a GPR sequence for compound comparisons (<=, >=) against zero. |
| 2637 | // Handle both zero-extensions and sign-extensions. |
| 2638 | SDValue |
| 2639 | IntegerCompareEliminator::getCompoundZeroComparisonInGPR(SDValue LHS, SDLoc dl, |
| 2640 | ZeroCompare CmpTy) { |
| 2641 | EVT InVT = LHS.getValueType(); |
| 2642 | bool Is32Bit = InVT == MVT::i32; |
| 2643 | SDValue ToExtend; |
| 2644 | |
| 2645 | // Produce the value that needs to be either zero or sign extended. |
| 2646 | switch (CmpTy) { |
| 2647 | case ZeroCompare::GEZExt: |
| 2648 | case ZeroCompare::GESExt: |
| 2649 | ToExtend = SDValue(CurDAG->getMachineNode(Is32Bit ? PPC::NOR : PPC::NOR8, |
| 2650 | dl, InVT, LHS, LHS), 0); |
| 2651 | break; |
| 2652 | case ZeroCompare::LEZExt: |
| 2653 | case ZeroCompare::LESExt: { |
| 2654 | if (Is32Bit) { |
| 2655 | // Upper 32 bits cannot be undefined for this sequence. |
| 2656 | LHS = signExtendInputIfNeeded(LHS); |
| 2657 | SDValue Neg = |
| 2658 | SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0); |
| 2659 | ToExtend = |
| 2660 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 2661 | Neg, S->getI64Imm(1, dl), |
| 2662 | S->getI64Imm(63, dl)), 0); |
| 2663 | } else { |
| 2664 | SDValue Addi = |
| 2665 | SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS, |
| 2666 | S->getI64Imm(~0ULL, dl)), 0); |
| 2667 | ToExtend = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64, |
| 2668 | Addi, LHS), 0); |
| 2669 | } |
| 2670 | break; |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | // For 64-bit sequences, the extensions are the same for the GE/LE cases. |
| 2675 | if (!Is32Bit && |
| 2676 | (CmpTy == ZeroCompare::GEZExt || CmpTy == ZeroCompare::LEZExt)) |
| 2677 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 2678 | ToExtend, S->getI64Imm(1, dl), |
| 2679 | S->getI64Imm(63, dl)), 0); |
| 2680 | if (!Is32Bit && |
| 2681 | (CmpTy == ZeroCompare::GESExt || CmpTy == ZeroCompare::LESExt)) |
| 2682 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, ToExtend, |
| 2683 | S->getI64Imm(63, dl)), 0); |
| 2684 | |
| 2685 | assert(Is32Bit && "Should have handled the 32-bit sequences above."); |
| 2686 | // For 32-bit sequences, the extensions differ between GE/LE cases. |
| 2687 | switch (CmpTy) { |
| 2688 | case ZeroCompare::GEZExt: { |
| 2689 | SDValue ShiftOps[] = { ToExtend, S->getI32Imm(1, dl), S->getI32Imm(31, dl), |
| 2690 | S->getI32Imm(31, dl) }; |
| 2691 | return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
| 2692 | ShiftOps), 0); |
| 2693 | } |
| 2694 | case ZeroCompare::GESExt: |
| 2695 | return SDValue(CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, ToExtend, |
| 2696 | S->getI32Imm(31, dl)), 0); |
| 2697 | case ZeroCompare::LEZExt: |
| 2698 | return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, ToExtend, |
| 2699 | S->getI32Imm(1, dl)), 0); |
| 2700 | case ZeroCompare::LESExt: |
| 2701 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, ToExtend, |
| 2702 | S->getI32Imm(-1, dl)), 0); |
| 2703 | } |
| 2704 | |
| 2705 | // The above case covers all the enumerators so it can't have a default clause |
| 2706 | // to avoid compiler warnings. |
| 2707 | llvm_unreachable("Unknown zero-comparison type."); |
| 2708 | } |
| 2709 | |
| 2710 | /// Produces a zero-extended result of comparing two 32-bit values according to |
| 2711 | /// the passed condition code. |
| 2712 | SDValue |
| 2713 | IntegerCompareEliminator::get32BitZExtCompare(SDValue LHS, SDValue RHS, |
| 2714 | ISD::CondCode CC, |
| 2715 | int64_t RHSValue, SDLoc dl) { |
| 2716 | if (CmpInGPR == ICGPR_I64 || CmpInGPR == ICGPR_SextI64 || |
| 2717 | CmpInGPR == ICGPR_ZextI64 || CmpInGPR == ICGPR_Sext) |
| 2718 | return SDValue(); |
| 2719 | bool IsRHSZero = RHSValue == 0; |
| 2720 | bool IsRHSOne = RHSValue == 1; |
| 2721 | bool IsRHSNegOne = RHSValue == -1LL; |
| 2722 | switch (CC) { |
| 2723 | default: return SDValue(); |
| 2724 | case ISD::SETEQ: { |
| 2725 | // (zext (setcc %a, %b, seteq)) -> (lshr (cntlzw (xor %a, %b)), 5) |
| 2726 | // (zext (setcc %a, 0, seteq)) -> (lshr (cntlzw %a), 5) |
| 2727 | SDValue Xor = IsRHSZero ? LHS : |
| 2728 | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
| 2729 | SDValue Clz = |
| 2730 | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0); |
| 2731 | SDValue ShiftOps[] = { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl), |
| 2732 | S->getI32Imm(31, dl) }; |
| 2733 | return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
| 2734 | ShiftOps), 0); |
| 2735 | } |
| 2736 | case ISD::SETNE: { |
| 2737 | // (zext (setcc %a, %b, setne)) -> (xor (lshr (cntlzw (xor %a, %b)), 5), 1) |
| 2738 | // (zext (setcc %a, 0, setne)) -> (xor (lshr (cntlzw %a), 5), 1) |
| 2739 | SDValue Xor = IsRHSZero ? LHS : |
| 2740 | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
| 2741 | SDValue Clz = |
| 2742 | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0); |
| 2743 | SDValue ShiftOps[] = { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl), |
| 2744 | S->getI32Imm(31, dl) }; |
| 2745 | SDValue Shift = |
| 2746 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0); |
| 2747 | return SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift, |
| 2748 | S->getI32Imm(1, dl)), 0); |
| 2749 | } |
| 2750 | case ISD::SETGE: { |
| 2751 | // (zext (setcc %a, %b, setge)) -> (xor (lshr (sub %a, %b), 63), 1) |
| 2752 | // (zext (setcc %a, 0, setge)) -> (lshr (~ %a), 31) |
| 2753 | if(IsRHSZero) |
| 2754 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
| 2755 | |
| 2756 | // Not a special case (i.e. RHS == 0). Handle (%a >= %b) as (%b <= %a) |
| 2757 | // by swapping inputs and falling through. |
| 2758 | std::swap(LHS, RHS); |
| 2759 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 2760 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 2761 | LLVM_FALLTHROUGH; |
| 2762 | } |
| 2763 | case ISD::SETLE: { |
| 2764 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2765 | return SDValue(); |
| 2766 | // (zext (setcc %a, %b, setle)) -> (xor (lshr (sub %b, %a), 63), 1) |
| 2767 | // (zext (setcc %a, 0, setle)) -> (xor (lshr (- %a), 63), 1) |
| 2768 | if(IsRHSZero) { |
| 2769 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2770 | return SDValue(); |
| 2771 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
| 2772 | } |
| 2773 | |
| 2774 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 2775 | LHS = signExtendInputIfNeeded(LHS); |
| 2776 | RHS = signExtendInputIfNeeded(RHS); |
| 2777 | SDValue Sub = |
| 2778 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0); |
| 2779 | SDValue Shift = |
| 2780 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Sub, |
| 2781 | S->getI64Imm(1, dl), S->getI64Imm(63, dl)), |
| 2782 | 0); |
| 2783 | return |
| 2784 | SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, |
| 2785 | MVT::i64, Shift, S->getI32Imm(1, dl)), 0); |
| 2786 | } |
| 2787 | case ISD::SETGT: { |
| 2788 | // (zext (setcc %a, %b, setgt)) -> (lshr (sub %b, %a), 63) |
| 2789 | // (zext (setcc %a, -1, setgt)) -> (lshr (~ %a), 31) |
| 2790 | // (zext (setcc %a, 0, setgt)) -> (lshr (- %a), 63) |
| 2791 | // Handle SETLT -1 (which is equivalent to SETGE 0). |
| 2792 | if (IsRHSNegOne) |
| 2793 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
| 2794 | |
| 2795 | if (IsRHSZero) { |
| 2796 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2797 | return SDValue(); |
| 2798 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 2799 | LHS = signExtendInputIfNeeded(LHS); |
| 2800 | RHS = signExtendInputIfNeeded(RHS); |
| 2801 | SDValue Neg = |
| 2802 | SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0); |
| 2803 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 2804 | Neg, S->getI32Imm(1, dl), S->getI32Imm(63, dl)), 0); |
| 2805 | } |
| 2806 | // Not a special case (i.e. RHS == 0 or RHS == -1). Handle (%a > %b) as |
| 2807 | // (%b < %a) by swapping inputs and falling through. |
| 2808 | std::swap(LHS, RHS); |
| 2809 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 2810 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 2811 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
| 2812 | LLVM_FALLTHROUGH; |
| 2813 | } |
| 2814 | case ISD::SETLT: { |
| 2815 | // (zext (setcc %a, %b, setlt)) -> (lshr (sub %a, %b), 63) |
| 2816 | // (zext (setcc %a, 1, setlt)) -> (xor (lshr (- %a), 63), 1) |
| 2817 | // (zext (setcc %a, 0, setlt)) -> (lshr %a, 31) |
| 2818 | // Handle SETLT 1 (which is equivalent to SETLE 0). |
| 2819 | if (IsRHSOne) { |
| 2820 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2821 | return SDValue(); |
| 2822 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
| 2823 | } |
| 2824 | |
| 2825 | if (IsRHSZero) { |
| 2826 | SDValue ShiftOps[] = { LHS, S->getI32Imm(1, dl), S->getI32Imm(31, dl), |
| 2827 | S->getI32Imm(31, dl) }; |
| 2828 | return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, |
| 2829 | ShiftOps), 0); |
| 2830 | } |
| 2831 | |
| 2832 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2833 | return SDValue(); |
| 2834 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 2835 | LHS = signExtendInputIfNeeded(LHS); |
| 2836 | RHS = signExtendInputIfNeeded(RHS); |
| 2837 | SDValue SUBFNode = |
| 2838 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
| 2839 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 2840 | SUBFNode, S->getI64Imm(1, dl), |
| 2841 | S->getI64Imm(63, dl)), 0); |
| 2842 | } |
| 2843 | case ISD::SETUGE: |
| 2844 | // (zext (setcc %a, %b, setuge)) -> (xor (lshr (sub %b, %a), 63), 1) |
| 2845 | // (zext (setcc %a, %b, setule)) -> (xor (lshr (sub %a, %b), 63), 1) |
| 2846 | std::swap(LHS, RHS); |
| 2847 | LLVM_FALLTHROUGH; |
| 2848 | case ISD::SETULE: { |
| 2849 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2850 | return SDValue(); |
| 2851 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 2852 | LHS = zeroExtendInputIfNeeded(LHS); |
| 2853 | RHS = zeroExtendInputIfNeeded(RHS); |
| 2854 | SDValue Subtract = |
| 2855 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0); |
| 2856 | SDValue SrdiNode = |
| 2857 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 2858 | Subtract, S->getI64Imm(1, dl), |
| 2859 | S->getI64Imm(63, dl)), 0); |
| 2860 | return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, SrdiNode, |
| 2861 | S->getI32Imm(1, dl)), 0); |
| 2862 | } |
| 2863 | case ISD::SETUGT: |
| 2864 | // (zext (setcc %a, %b, setugt)) -> (lshr (sub %b, %a), 63) |
| 2865 | // (zext (setcc %a, %b, setult)) -> (lshr (sub %a, %b), 63) |
| 2866 | std::swap(LHS, RHS); |
| 2867 | LLVM_FALLTHROUGH; |
| 2868 | case ISD::SETULT: { |
| 2869 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2870 | return SDValue(); |
| 2871 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 2872 | LHS = zeroExtendInputIfNeeded(LHS); |
| 2873 | RHS = zeroExtendInputIfNeeded(RHS); |
| 2874 | SDValue Subtract = |
| 2875 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
| 2876 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 2877 | Subtract, S->getI64Imm(1, dl), |
| 2878 | S->getI64Imm(63, dl)), 0); |
| 2879 | } |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | /// Produces a sign-extended result of comparing two 32-bit values according to |
| 2884 | /// the passed condition code. |
| 2885 | SDValue |
| 2886 | IntegerCompareEliminator::get32BitSExtCompare(SDValue LHS, SDValue RHS, |
| 2887 | ISD::CondCode CC, |
| 2888 | int64_t RHSValue, SDLoc dl) { |
| 2889 | if (CmpInGPR == ICGPR_I64 || CmpInGPR == ICGPR_SextI64 || |
| 2890 | CmpInGPR == ICGPR_ZextI64 || CmpInGPR == ICGPR_Zext) |
| 2891 | return SDValue(); |
| 2892 | bool IsRHSZero = RHSValue == 0; |
| 2893 | bool IsRHSOne = RHSValue == 1; |
| 2894 | bool IsRHSNegOne = RHSValue == -1LL; |
| 2895 | |
| 2896 | switch (CC) { |
| 2897 | default: return SDValue(); |
| 2898 | case ISD::SETEQ: { |
| 2899 | // (sext (setcc %a, %b, seteq)) -> |
| 2900 | // (ashr (shl (ctlz (xor %a, %b)), 58), 63) |
| 2901 | // (sext (setcc %a, 0, seteq)) -> |
| 2902 | // (ashr (shl (ctlz %a), 58), 63) |
| 2903 | SDValue CountInput = IsRHSZero ? LHS : |
| 2904 | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
| 2905 | SDValue Cntlzw = |
| 2906 | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, CountInput), 0); |
| 2907 | SDValue SHLOps[] = { Cntlzw, S->getI32Imm(27, dl), |
| 2908 | S->getI32Imm(5, dl), S->getI32Imm(31, dl) }; |
| 2909 | SDValue Slwi = |
| 2910 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, SHLOps), 0); |
| 2911 | return SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Slwi), 0); |
| 2912 | } |
| 2913 | case ISD::SETNE: { |
| 2914 | // Bitwise xor the operands, count leading zeros, shift right by 5 bits and |
| 2915 | // flip the bit, finally take 2's complement. |
| 2916 | // (sext (setcc %a, %b, setne)) -> |
| 2917 | // (neg (xor (lshr (ctlz (xor %a, %b)), 5), 1)) |
| 2918 | // Same as above, but the first xor is not needed. |
| 2919 | // (sext (setcc %a, 0, setne)) -> |
| 2920 | // (neg (xor (lshr (ctlz %a), 5), 1)) |
| 2921 | SDValue Xor = IsRHSZero ? LHS : |
| 2922 | SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0); |
| 2923 | SDValue Clz = |
| 2924 | SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0); |
| 2925 | SDValue ShiftOps[] = |
| 2926 | { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl), S->getI32Imm(31, dl) }; |
| 2927 | SDValue Shift = |
| 2928 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0); |
| 2929 | SDValue Xori = |
| 2930 | SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift, |
| 2931 | S->getI32Imm(1, dl)), 0); |
| 2932 | return SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Xori), 0); |
| 2933 | } |
| 2934 | case ISD::SETGE: { |
| 2935 | // (sext (setcc %a, %b, setge)) -> (add (lshr (sub %a, %b), 63), -1) |
| 2936 | // (sext (setcc %a, 0, setge)) -> (ashr (~ %a), 31) |
| 2937 | if (IsRHSZero) |
| 2938 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
| 2939 | |
| 2940 | // Not a special case (i.e. RHS == 0). Handle (%a >= %b) as (%b <= %a) |
| 2941 | // by swapping inputs and falling through. |
| 2942 | std::swap(LHS, RHS); |
| 2943 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 2944 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 2945 | LLVM_FALLTHROUGH; |
| 2946 | } |
| 2947 | case ISD::SETLE: { |
| 2948 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2949 | return SDValue(); |
| 2950 | // (sext (setcc %a, %b, setge)) -> (add (lshr (sub %b, %a), 63), -1) |
| 2951 | // (sext (setcc %a, 0, setle)) -> (add (lshr (- %a), 63), -1) |
| 2952 | if (IsRHSZero) |
| 2953 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
| 2954 | |
| 2955 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 2956 | LHS = signExtendInputIfNeeded(LHS); |
| 2957 | RHS = signExtendInputIfNeeded(RHS); |
| 2958 | SDValue SUBFNode = |
| 2959 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, MVT::Glue, |
| 2960 | LHS, RHS), 0); |
| 2961 | SDValue Srdi = |
| 2962 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 2963 | SUBFNode, S->getI64Imm(1, dl), |
| 2964 | S->getI64Imm(63, dl)), 0); |
| 2965 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, Srdi, |
| 2966 | S->getI32Imm(-1, dl)), 0); |
| 2967 | } |
| 2968 | case ISD::SETGT: { |
| 2969 | // (sext (setcc %a, %b, setgt)) -> (ashr (sub %b, %a), 63) |
| 2970 | // (sext (setcc %a, -1, setgt)) -> (ashr (~ %a), 31) |
| 2971 | // (sext (setcc %a, 0, setgt)) -> (ashr (- %a), 63) |
| 2972 | if (IsRHSNegOne) |
| 2973 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
| 2974 | if (IsRHSZero) { |
| 2975 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2976 | return SDValue(); |
| 2977 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 2978 | LHS = signExtendInputIfNeeded(LHS); |
| 2979 | RHS = signExtendInputIfNeeded(RHS); |
| 2980 | SDValue Neg = |
| 2981 | SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0); |
| 2982 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, Neg, |
| 2983 | S->getI64Imm(63, dl)), 0); |
| 2984 | } |
| 2985 | // Not a special case (i.e. RHS == 0 or RHS == -1). Handle (%a > %b) as |
| 2986 | // (%b < %a) by swapping inputs and falling through. |
| 2987 | std::swap(LHS, RHS); |
| 2988 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 2989 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 2990 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
| 2991 | LLVM_FALLTHROUGH; |
| 2992 | } |
| 2993 | case ISD::SETLT: { |
| 2994 | // (sext (setcc %a, %b, setgt)) -> (ashr (sub %a, %b), 63) |
| 2995 | // (sext (setcc %a, 1, setgt)) -> (add (lshr (- %a), 63), -1) |
| 2996 | // (sext (setcc %a, 0, setgt)) -> (ashr %a, 31) |
| 2997 | if (IsRHSOne) { |
| 2998 | if (CmpInGPR == ICGPR_NonExtIn) |
| 2999 | return SDValue(); |
| 3000 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
| 3001 | } |
| 3002 | if (IsRHSZero) |
| 3003 | return SDValue(CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, LHS, |
| 3004 | S->getI32Imm(31, dl)), 0); |
| 3005 | |
| 3006 | if (CmpInGPR == ICGPR_NonExtIn) |
| 3007 | return SDValue(); |
| 3008 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 3009 | LHS = signExtendInputIfNeeded(LHS); |
| 3010 | RHS = signExtendInputIfNeeded(RHS); |
| 3011 | SDValue SUBFNode = |
| 3012 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
| 3013 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
| 3014 | SUBFNode, S->getI64Imm(63, dl)), 0); |
| 3015 | } |
| 3016 | case ISD::SETUGE: |
| 3017 | // (sext (setcc %a, %b, setuge)) -> (add (lshr (sub %a, %b), 63), -1) |
| 3018 | // (sext (setcc %a, %b, setule)) -> (add (lshr (sub %b, %a), 63), -1) |
| 3019 | std::swap(LHS, RHS); |
| 3020 | LLVM_FALLTHROUGH; |
| 3021 | case ISD::SETULE: { |
| 3022 | if (CmpInGPR == ICGPR_NonExtIn) |
| 3023 | return SDValue(); |
| 3024 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 3025 | LHS = zeroExtendInputIfNeeded(LHS); |
| 3026 | RHS = zeroExtendInputIfNeeded(RHS); |
| 3027 | SDValue Subtract = |
| 3028 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0); |
| 3029 | SDValue Shift = |
| 3030 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Subtract, |
| 3031 | S->getI32Imm(1, dl), S->getI32Imm(63,dl)), |
| 3032 | 0); |
| 3033 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, Shift, |
| 3034 | S->getI32Imm(-1, dl)), 0); |
| 3035 | } |
| 3036 | case ISD::SETUGT: |
| 3037 | // (sext (setcc %a, %b, setugt)) -> (ashr (sub %b, %a), 63) |
| 3038 | // (sext (setcc %a, %b, setugt)) -> (ashr (sub %a, %b), 63) |
| 3039 | std::swap(LHS, RHS); |
| 3040 | LLVM_FALLTHROUGH; |
| 3041 | case ISD::SETULT: { |
| 3042 | if (CmpInGPR == ICGPR_NonExtIn) |
| 3043 | return SDValue(); |
| 3044 | // The upper 32-bits of the register can't be undefined for this sequence. |
| 3045 | LHS = zeroExtendInputIfNeeded(LHS); |
| 3046 | RHS = zeroExtendInputIfNeeded(RHS); |
| 3047 | SDValue Subtract = |
| 3048 | SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0); |
| 3049 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
| 3050 | Subtract, S->getI64Imm(63, dl)), 0); |
| 3051 | } |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | /// Produces a zero-extended result of comparing two 64-bit values according to |
| 3056 | /// the passed condition code. |
| 3057 | SDValue |
| 3058 | IntegerCompareEliminator::get64BitZExtCompare(SDValue LHS, SDValue RHS, |
| 3059 | ISD::CondCode CC, |
| 3060 | int64_t RHSValue, SDLoc dl) { |
| 3061 | if (CmpInGPR == ICGPR_I32 || CmpInGPR == ICGPR_SextI32 || |
| 3062 | CmpInGPR == ICGPR_ZextI32 || CmpInGPR == ICGPR_Sext) |
| 3063 | return SDValue(); |
| 3064 | bool IsRHSZero = RHSValue == 0; |
| 3065 | bool IsRHSOne = RHSValue == 1; |
| 3066 | bool IsRHSNegOne = RHSValue == -1LL; |
| 3067 | switch (CC) { |
| 3068 | default: return SDValue(); |
| 3069 | case ISD::SETEQ: { |
| 3070 | // (zext (setcc %a, %b, seteq)) -> (lshr (ctlz (xor %a, %b)), 6) |
| 3071 | // (zext (setcc %a, 0, seteq)) -> (lshr (ctlz %a), 6) |
| 3072 | SDValue Xor = IsRHSZero ? LHS : |
| 3073 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
| 3074 | SDValue Clz = |
| 3075 | SDValue(CurDAG->getMachineNode(PPC::CNTLZD, dl, MVT::i64, Xor), 0); |
| 3076 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Clz, |
| 3077 | S->getI64Imm(58, dl), |
| 3078 | S->getI64Imm(63, dl)), 0); |
| 3079 | } |
| 3080 | case ISD::SETNE: { |
| 3081 | // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1) |
| 3082 | // (zext (setcc %a, %b, setne)) -> (sube addc.reg, addc.reg, addc.CA) |
| 3083 | // {addcz.reg, addcz.CA} = (addcarry %a, -1) |
| 3084 | // (zext (setcc %a, 0, setne)) -> (sube addcz.reg, addcz.reg, addcz.CA) |
| 3085 | SDValue Xor = IsRHSZero ? LHS : |
| 3086 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
| 3087 | SDValue AC = |
| 3088 | SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue, |
| 3089 | Xor, S->getI32Imm(~0U, dl)), 0); |
| 3090 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, AC, |
| 3091 | Xor, AC.getValue(1)), 0); |
| 3092 | } |
| 3093 | case ISD::SETGE: { |
| 3094 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3095 | // (zext (setcc %a, %b, setge)) -> |
| 3096 | // (adde (lshr %b, 63), (ashr %a, 63), subc.CA) |
| 3097 | // (zext (setcc %a, 0, setge)) -> (lshr (~ %a), 63) |
| 3098 | if (IsRHSZero) |
| 3099 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
| 3100 | std::swap(LHS, RHS); |
| 3101 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 3102 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 3103 | LLVM_FALLTHROUGH; |
| 3104 | } |
| 3105 | case ISD::SETLE: { |
| 3106 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3107 | // (zext (setcc %a, %b, setge)) -> |
| 3108 | // (adde (lshr %a, 63), (ashr %b, 63), subc.CA) |
| 3109 | // (zext (setcc %a, 0, setge)) -> (lshr (or %a, (add %a, -1)), 63) |
| 3110 | if (IsRHSZero) |
| 3111 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
| 3112 | SDValue ShiftL = |
| 3113 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS, |
| 3114 | S->getI64Imm(1, dl), |
| 3115 | S->getI64Imm(63, dl)), 0); |
| 3116 | SDValue ShiftR = |
| 3117 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, RHS, |
| 3118 | S->getI64Imm(63, dl)), 0); |
| 3119 | SDValue SubtractCarry = |
| 3120 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3121 | LHS, RHS), 1); |
| 3122 | return SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue, |
| 3123 | ShiftR, ShiftL, SubtractCarry), 0); |
| 3124 | } |
| 3125 | case ISD::SETGT: { |
| 3126 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3127 | // (zext (setcc %a, %b, setgt)) -> |
| 3128 | // (xor (adde (lshr %a, 63), (ashr %b, 63), subc.CA), 1) |
| 3129 | // (zext (setcc %a, 0, setgt)) -> (lshr (nor (add %a, -1), %a), 63) |
| 3130 | if (IsRHSNegOne) |
| 3131 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt); |
| 3132 | if (IsRHSZero) { |
| 3133 | SDValue Addi = |
| 3134 | SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS, |
| 3135 | S->getI64Imm(~0ULL, dl)), 0); |
| 3136 | SDValue Nor = |
| 3137 | SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, Addi, LHS), 0); |
| 3138 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Nor, |
| 3139 | S->getI64Imm(1, dl), |
| 3140 | S->getI64Imm(63, dl)), 0); |
| 3141 | } |
| 3142 | std::swap(LHS, RHS); |
| 3143 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 3144 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 3145 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
| 3146 | LLVM_FALLTHROUGH; |
| 3147 | } |
| 3148 | case ISD::SETLT: { |
| 3149 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3150 | // (zext (setcc %a, %b, setlt)) -> |
| 3151 | // (xor (adde (lshr %b, 63), (ashr %a, 63), subc.CA), 1) |
| 3152 | // (zext (setcc %a, 0, setlt)) -> (lshr %a, 63) |
| 3153 | if (IsRHSOne) |
| 3154 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt); |
| 3155 | if (IsRHSZero) |
| 3156 | return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS, |
| 3157 | S->getI64Imm(1, dl), |
| 3158 | S->getI64Imm(63, dl)), 0); |
| 3159 | SDValue SRADINode = |
| 3160 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
| 3161 | LHS, S->getI64Imm(63, dl)), 0); |
| 3162 | SDValue SRDINode = |
| 3163 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 3164 | RHS, S->getI64Imm(1, dl), |
| 3165 | S->getI64Imm(63, dl)), 0); |
| 3166 | SDValue SUBFC8Carry = |
| 3167 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3168 | RHS, LHS), 1); |
| 3169 | SDValue ADDE8Node = |
| 3170 | SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue, |
| 3171 | SRDINode, SRADINode, SUBFC8Carry), 0); |
| 3172 | return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, |
| 3173 | ADDE8Node, S->getI64Imm(1, dl)), 0); |
| 3174 | } |
| 3175 | case ISD::SETUGE: |
| 3176 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3177 | // (zext (setcc %a, %b, setuge)) -> (add (sube %b, %b, subc.CA), 1) |
| 3178 | std::swap(LHS, RHS); |
| 3179 | LLVM_FALLTHROUGH; |
| 3180 | case ISD::SETULE: { |
| 3181 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3182 | // (zext (setcc %a, %b, setule)) -> (add (sube %a, %a, subc.CA), 1) |
| 3183 | SDValue SUBFC8Carry = |
| 3184 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3185 | LHS, RHS), 1); |
| 3186 | SDValue SUBFE8Node = |
| 3187 | SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, MVT::Glue, |
| 3188 | LHS, LHS, SUBFC8Carry), 0); |
| 3189 | return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, |
| 3190 | SUBFE8Node, S->getI64Imm(1, dl)), 0); |
| 3191 | } |
| 3192 | case ISD::SETUGT: |
| 3193 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3194 | // (zext (setcc %a, %b, setugt)) -> -(sube %b, %b, subc.CA) |
| 3195 | std::swap(LHS, RHS); |
| 3196 | LLVM_FALLTHROUGH; |
| 3197 | case ISD::SETULT: { |
| 3198 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3199 | // (zext (setcc %a, %b, setult)) -> -(sube %a, %a, subc.CA) |
| 3200 | SDValue SubtractCarry = |
| 3201 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3202 | RHS, LHS), 1); |
| 3203 | SDValue ExtSub = |
| 3204 | SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, |
| 3205 | LHS, LHS, SubtractCarry), 0); |
| 3206 | return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, |
| 3207 | ExtSub), 0); |
| 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | /// Produces a sign-extended result of comparing two 64-bit values according to |
| 3213 | /// the passed condition code. |
| 3214 | SDValue |
| 3215 | IntegerCompareEliminator::get64BitSExtCompare(SDValue LHS, SDValue RHS, |
| 3216 | ISD::CondCode CC, |
| 3217 | int64_t RHSValue, SDLoc dl) { |
| 3218 | if (CmpInGPR == ICGPR_I32 || CmpInGPR == ICGPR_SextI32 || |
| 3219 | CmpInGPR == ICGPR_ZextI32 || CmpInGPR == ICGPR_Zext) |
| 3220 | return SDValue(); |
| 3221 | bool IsRHSZero = RHSValue == 0; |
| 3222 | bool IsRHSOne = RHSValue == 1; |
| 3223 | bool IsRHSNegOne = RHSValue == -1LL; |
| 3224 | switch (CC) { |
| 3225 | default: return SDValue(); |
| 3226 | case ISD::SETEQ: { |
| 3227 | // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1) |
| 3228 | // (sext (setcc %a, %b, seteq)) -> (sube addc.reg, addc.reg, addc.CA) |
| 3229 | // {addcz.reg, addcz.CA} = (addcarry %a, -1) |
| 3230 | // (sext (setcc %a, 0, seteq)) -> (sube addcz.reg, addcz.reg, addcz.CA) |
| 3231 | SDValue AddInput = IsRHSZero ? LHS : |
| 3232 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
| 3233 | SDValue Addic = |
| 3234 | SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue, |
| 3235 | AddInput, S->getI32Imm(~0U, dl)), 0); |
| 3236 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, Addic, |
| 3237 | Addic, Addic.getValue(1)), 0); |
| 3238 | } |
| 3239 | case ISD::SETNE: { |
| 3240 | // {subfc.reg, subfc.CA} = (subcarry 0, (xor %a, %b)) |
| 3241 | // (sext (setcc %a, %b, setne)) -> (sube subfc.reg, subfc.reg, subfc.CA) |
| 3242 | // {subfcz.reg, subfcz.CA} = (subcarry 0, %a) |
| 3243 | // (sext (setcc %a, 0, setne)) -> (sube subfcz.reg, subfcz.reg, subfcz.CA) |
| 3244 | SDValue Xor = IsRHSZero ? LHS : |
| 3245 | SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0); |
| 3246 | SDValue SC = |
| 3247 | SDValue(CurDAG->getMachineNode(PPC::SUBFIC8, dl, MVT::i64, MVT::Glue, |
| 3248 | Xor, S->getI32Imm(0, dl)), 0); |
| 3249 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, SC, |
| 3250 | SC, SC.getValue(1)), 0); |
| 3251 | } |
| 3252 | case ISD::SETGE: { |
| 3253 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3254 | // (zext (setcc %a, %b, setge)) -> |
| 3255 | // (- (adde (lshr %b, 63), (ashr %a, 63), subc.CA)) |
| 3256 | // (zext (setcc %a, 0, setge)) -> (~ (ashr %a, 63)) |
| 3257 | if (IsRHSZero) |
| 3258 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
| 3259 | std::swap(LHS, RHS); |
| 3260 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 3261 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 3262 | LLVM_FALLTHROUGH; |
| 3263 | } |
| 3264 | case ISD::SETLE: { |
| 3265 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3266 | // (zext (setcc %a, %b, setge)) -> |
| 3267 | // (- (adde (lshr %a, 63), (ashr %b, 63), subc.CA)) |
| 3268 | // (zext (setcc %a, 0, setge)) -> (ashr (or %a, (add %a, -1)), 63) |
| 3269 | if (IsRHSZero) |
| 3270 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
| 3271 | SDValue ShiftR = |
| 3272 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, RHS, |
| 3273 | S->getI64Imm(63, dl)), 0); |
| 3274 | SDValue ShiftL = |
| 3275 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS, |
| 3276 | S->getI64Imm(1, dl), |
| 3277 | S->getI64Imm(63, dl)), 0); |
| 3278 | SDValue SubtractCarry = |
| 3279 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3280 | LHS, RHS), 1); |
| 3281 | SDValue Adde = |
| 3282 | SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue, |
| 3283 | ShiftR, ShiftL, SubtractCarry), 0); |
| 3284 | return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, Adde), 0); |
| 3285 | } |
| 3286 | case ISD::SETGT: { |
| 3287 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3288 | // (zext (setcc %a, %b, setgt)) -> |
| 3289 | // -(xor (adde (lshr %a, 63), (ashr %b, 63), subc.CA), 1) |
| 3290 | // (zext (setcc %a, 0, setgt)) -> (ashr (nor (add %a, -1), %a), 63) |
| 3291 | if (IsRHSNegOne) |
| 3292 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt); |
| 3293 | if (IsRHSZero) { |
| 3294 | SDValue Add = |
| 3295 | SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS, |
| 3296 | S->getI64Imm(-1, dl)), 0); |
| 3297 | SDValue Nor = |
| 3298 | SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, Add, LHS), 0); |
| 3299 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, Nor, |
| 3300 | S->getI64Imm(63, dl)), 0); |
| 3301 | } |
| 3302 | std::swap(LHS, RHS); |
| 3303 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 3304 | IsRHSZero = RHSConst && RHSConst->isNullValue(); |
| 3305 | IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1; |
| 3306 | LLVM_FALLTHROUGH; |
| 3307 | } |
| 3308 | case ISD::SETLT: { |
| 3309 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3310 | // (zext (setcc %a, %b, setlt)) -> |
| 3311 | // -(xor (adde (lshr %b, 63), (ashr %a, 63), subc.CA), 1) |
| 3312 | // (zext (setcc %a, 0, setlt)) -> (ashr %a, 63) |
| 3313 | if (IsRHSOne) |
| 3314 | return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt); |
| 3315 | if (IsRHSZero) { |
| 3316 | return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, LHS, |
| 3317 | S->getI64Imm(63, dl)), 0); |
| 3318 | } |
| 3319 | SDValue SRADINode = |
| 3320 | SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, |
| 3321 | LHS, S->getI64Imm(63, dl)), 0); |
| 3322 | SDValue SRDINode = |
| 3323 | SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, |
| 3324 | RHS, S->getI64Imm(1, dl), |
| 3325 | S->getI64Imm(63, dl)), 0); |
| 3326 | SDValue SUBFC8Carry = |
| 3327 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3328 | RHS, LHS), 1); |
| 3329 | SDValue ADDE8Node = |
| 3330 | SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, |
| 3331 | SRDINode, SRADINode, SUBFC8Carry), 0); |
| 3332 | SDValue XORI8Node = |
| 3333 | SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, |
| 3334 | ADDE8Node, S->getI64Imm(1, dl)), 0); |
| 3335 | return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, |
| 3336 | XORI8Node), 0); |
| 3337 | } |
| 3338 | case ISD::SETUGE: |
| 3339 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3340 | // (sext (setcc %a, %b, setuge)) -> ~(sube %b, %b, subc.CA) |
| 3341 | std::swap(LHS, RHS); |
| 3342 | LLVM_FALLTHROUGH; |
| 3343 | case ISD::SETULE: { |
| 3344 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3345 | // (sext (setcc %a, %b, setule)) -> ~(sube %a, %a, subc.CA) |
| 3346 | SDValue SubtractCarry = |
| 3347 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3348 | LHS, RHS), 1); |
| 3349 | SDValue ExtSub = |
| 3350 | SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, MVT::Glue, LHS, |
| 3351 | LHS, SubtractCarry), 0); |
| 3352 | return SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, |
| 3353 | ExtSub, ExtSub), 0); |
| 3354 | } |
| 3355 | case ISD::SETUGT: |
| 3356 | // {subc.reg, subc.CA} = (subcarry %b, %a) |
| 3357 | // (sext (setcc %a, %b, setugt)) -> (sube %b, %b, subc.CA) |
| 3358 | std::swap(LHS, RHS); |
| 3359 | LLVM_FALLTHROUGH; |
| 3360 | case ISD::SETULT: { |
| 3361 | // {subc.reg, subc.CA} = (subcarry %a, %b) |
| 3362 | // (sext (setcc %a, %b, setult)) -> (sube %a, %a, subc.CA) |
| 3363 | SDValue SubCarry = |
| 3364 | SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue, |
| 3365 | RHS, LHS), 1); |
| 3366 | return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, |
| 3367 | LHS, LHS, SubCarry), 0); |
| 3368 | } |
| 3369 | } |
| 3370 | } |
| 3371 | |
| 3372 | /// Do all uses of this SDValue need the result in a GPR? |
| 3373 | /// This is meant to be used on values that have type i1 since |
| 3374 | /// it is somewhat meaningless to ask if values of other types |
| 3375 | /// should be kept in GPR's. |
| 3376 | static bool allUsesExtend(SDValue Compare, SelectionDAG *CurDAG) { |
| 3377 | assert(Compare.getOpcode() == ISD::SETCC && |
| 3378 | "An ISD::SETCC node required here."); |
| 3379 | |
| 3380 | // For values that have a single use, the caller should obviously already have |
| 3381 | // checked if that use is an extending use. We check the other uses here. |
| 3382 | if (Compare.hasOneUse()) |
| 3383 | return true; |
| 3384 | // We want the value in a GPR if it is being extended, used for a select, or |
| 3385 | // used in logical operations. |
| 3386 | for (auto CompareUse : Compare.getNode()->uses()) |
| 3387 | if (CompareUse->getOpcode() != ISD::SIGN_EXTEND && |
| 3388 | CompareUse->getOpcode() != ISD::ZERO_EXTEND && |
| 3389 | CompareUse->getOpcode() != ISD::SELECT && |
| 3390 | !isLogicOp(CompareUse->getOpcode())) { |
| 3391 | OmittedForNonExtendUses++; |
| 3392 | return false; |
| 3393 | } |
| 3394 | return true; |
| 3395 | } |
| 3396 | |
| 3397 | /// Returns an equivalent of a SETCC node but with the result the same width as |
| 3398 | /// the inputs. This can nalso be used for SELECT_CC if either the true or false |
| 3399 | /// values is a power of two while the other is zero. |
| 3400 | SDValue IntegerCompareEliminator::getSETCCInGPR(SDValue Compare, |
| 3401 | SetccInGPROpts ConvOpts) { |
| 3402 | assert((Compare.getOpcode() == ISD::SETCC || |
| 3403 | Compare.getOpcode() == ISD::SELECT_CC) && |
| 3404 | "An ISD::SETCC node required here."); |
| 3405 | |
| 3406 | // Don't convert this comparison to a GPR sequence because there are uses |
| 3407 | // of the i1 result (i.e. uses that require the result in the CR). |
| 3408 | if ((Compare.getOpcode() == ISD::SETCC) && !allUsesExtend(Compare, CurDAG)) |
| 3409 | return SDValue(); |
| 3410 | |
| 3411 | SDValue LHS = Compare.getOperand(0); |
| 3412 | SDValue RHS = Compare.getOperand(1); |
| 3413 | |
| 3414 | // The condition code is operand 2 for SETCC and operand 4 for SELECT_CC. |
| 3415 | int CCOpNum = Compare.getOpcode() == ISD::SELECT_CC ? 4 : 2; |
| 3416 | ISD::CondCode CC = |
| 3417 | cast<CondCodeSDNode>(Compare.getOperand(CCOpNum))->get(); |
| 3418 | EVT InputVT = LHS.getValueType(); |
| 3419 | if (InputVT != MVT::i32 && InputVT != MVT::i64) |
| 3420 | return SDValue(); |
| 3421 | |
| 3422 | if (ConvOpts == SetccInGPROpts::ZExtInvert || |
| 3423 | ConvOpts == SetccInGPROpts::SExtInvert) |
| 3424 | CC = ISD::getSetCCInverse(CC, true); |
| 3425 | |
| 3426 | bool Inputs32Bit = InputVT == MVT::i32; |
| 3427 | |
| 3428 | SDLoc dl(Compare); |
| 3429 | ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS); |
| 3430 | int64_t RHSValue = RHSConst ? RHSConst->getSExtValue() : INT64_MAX; |
| 3431 | bool IsSext = ConvOpts == SetccInGPROpts::SExtOrig || |
| 3432 | ConvOpts == SetccInGPROpts::SExtInvert; |
| 3433 | |
| 3434 | if (IsSext && Inputs32Bit) |
| 3435 | return get32BitSExtCompare(LHS, RHS, CC, RHSValue, dl); |
| 3436 | else if (Inputs32Bit) |
| 3437 | return get32BitZExtCompare(LHS, RHS, CC, RHSValue, dl); |
| 3438 | else if (IsSext) |
| 3439 | return get64BitSExtCompare(LHS, RHS, CC, RHSValue, dl); |
| 3440 | return get64BitZExtCompare(LHS, RHS, CC, RHSValue, dl); |
| 3441 | } |
| 3442 | |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 3443 | } // end anonymous namespace |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3444 | |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 3445 | bool PPCDAGToDAGISel::tryIntCompareInGPR(SDNode *N) { |
| 3446 | if (N->getValueType(0) != MVT::i32 && |
| 3447 | N->getValueType(0) != MVT::i64) |
| 3448 | return false; |
| 3449 | |
| 3450 | // This optimization will emit code that assumes 64-bit registers |
| 3451 | // so we don't want to run it in 32-bit mode. Also don't run it |
| 3452 | // on functions that are not to be optimized. |
| 3453 | if (TM.getOptLevel() == CodeGenOpt::None || !TM.isPPC64()) |
| 3454 | return false; |
| 3455 | |
| 3456 | switch (N->getOpcode()) { |
| 3457 | default: break; |
| 3458 | case ISD::ZERO_EXTEND: |
| 3459 | case ISD::SIGN_EXTEND: |
| 3460 | case ISD::AND: |
| 3461 | case ISD::OR: |
| 3462 | case ISD::XOR: { |
| 3463 | IntegerCompareEliminator ICmpElim(CurDAG, this); |
| 3464 | if (SDNode *New = ICmpElim.Select(N)) { |
| 3465 | ReplaceNode(N, New); |
| 3466 | return true; |
| 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | return false; |
| 3471 | } |
| 3472 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3473 | bool PPCDAGToDAGISel::tryBitPermutation(SDNode *N) { |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3474 | if (N->getValueType(0) != MVT::i32 && |
| 3475 | N->getValueType(0) != MVT::i64) |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3476 | return false; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3477 | |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 3478 | if (!UseBitPermRewriter) |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3479 | return false; |
Hal Finkel | c58ce41 | 2015-01-01 02:53:29 +0000 | [diff] [blame] | 3480 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3481 | switch (N->getOpcode()) { |
| 3482 | default: break; |
| 3483 | case ISD::ROTL: |
| 3484 | case ISD::SHL: |
| 3485 | case ISD::SRL: |
| 3486 | case ISD::AND: |
| 3487 | case ISD::OR: { |
| 3488 | BitPermutationSelector BPS(CurDAG); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3489 | if (SDNode *New = BPS.Select(N)) { |
| 3490 | ReplaceNode(N, New); |
| 3491 | return true; |
| 3492 | } |
| 3493 | return false; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3494 | } |
| 3495 | } |
| 3496 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3497 | return false; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3500 | /// SelectCC - Select a comparison of the specified values with the specified |
| 3501 | /// condition code, returning the CR# of the expression. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3502 | SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, |
| 3503 | const SDLoc &dl) { |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3504 | // Always select the LHS. |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3505 | unsigned Opc; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3506 | |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3507 | if (LHS.getValueType() == MVT::i32) { |
Chris Lattner | 9a40cca | 2006-06-27 00:10:13 +0000 | [diff] [blame] | 3508 | unsigned Imm; |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 3509 | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
| 3510 | if (isInt32Immediate(RHS, Imm)) { |
| 3511 | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3512 | if (isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3513 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3514 | getI32Imm(Imm & 0xFFFF, dl)), |
| 3515 | 0); |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 3516 | // If this is a 16-bit signed immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3517 | if (isInt<16>((int)Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3518 | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3519 | getI32Imm(Imm & 0xFFFF, dl)), |
| 3520 | 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3521 | |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 3522 | // For non-equality comparisons, the default code would materialize the |
| 3523 | // constant, then compare against it, like this: |
| 3524 | // lis r2, 4660 |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3525 | // ori r2, r2, 22136 |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 3526 | // cmpw cr0, r3, r2 |
| 3527 | // Since we are just comparing for equality, we can emit this instead: |
| 3528 | // xoris r0,r3,0x1234 |
| 3529 | // cmplwi cr0,r0,0x5678 |
| 3530 | // beq cr0,L6 |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3531 | SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3532 | getI32Imm(Imm >> 16, dl)), 0); |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3533 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3534 | getI32Imm(Imm & 0xFFFF, dl)), 0); |
Chris Lattner | aa3926b | 2006-09-20 04:25:47 +0000 | [diff] [blame] | 3535 | } |
| 3536 | Opc = PPC::CMPLW; |
| 3537 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3538 | if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3539 | return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3540 | getI32Imm(Imm & 0xFFFF, dl)), 0); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3541 | Opc = PPC::CMPLW; |
| 3542 | } else { |
Lei Huang | 3171041 | 2017-07-07 21:12:35 +0000 | [diff] [blame] | 3543 | int16_t SImm; |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3544 | if (isIntS16Immediate(RHS, SImm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3545 | return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3546 | getI32Imm((int)SImm & 0xFFFF, |
| 3547 | dl)), |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3548 | 0); |
| 3549 | Opc = PPC::CMPW; |
| 3550 | } |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3551 | } else if (LHS.getValueType() == MVT::i64) { |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3552 | uint64_t Imm; |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 3553 | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3554 | if (isInt64Immediate(RHS.getNode(), Imm)) { |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 3555 | // SETEQ/SETNE comparison with 16-bit immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3556 | if (isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3557 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3558 | getI32Imm(Imm & 0xFFFF, dl)), |
| 3559 | 0); |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 3560 | // If this is a 16-bit signed immediate, fold it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3561 | if (isInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3562 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3563 | getI32Imm(Imm & 0xFFFF, dl)), |
| 3564 | 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3565 | |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 3566 | // For non-equality comparisons, the default code would materialize the |
| 3567 | // constant, then compare against it, like this: |
| 3568 | // lis r2, 4660 |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3569 | // ori r2, r2, 22136 |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 3570 | // cmpd cr0, r3, r2 |
| 3571 | // Since we are just comparing for equality, we can emit this instead: |
| 3572 | // xoris r0,r3,0x1234 |
| 3573 | // cmpldi cr0,r0,0x5678 |
| 3574 | // beq cr0,L6 |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3575 | if (isUInt<32>(Imm)) { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3576 | SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3577 | getI64Imm(Imm >> 16, dl)), 0); |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3578 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3579 | getI64Imm(Imm & 0xFFFF, dl)), |
| 3580 | 0); |
Chris Lattner | da9b1a9 | 2006-09-20 04:33:27 +0000 | [diff] [blame] | 3581 | } |
| 3582 | } |
| 3583 | Opc = PPC::CMPLD; |
| 3584 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3585 | if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3586 | return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3587 | getI64Imm(Imm & 0xFFFF, dl)), 0); |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3588 | Opc = PPC::CMPLD; |
| 3589 | } else { |
Lei Huang | 3171041 | 2017-07-07 21:12:35 +0000 | [diff] [blame] | 3590 | int16_t SImm; |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3591 | if (isIntS16Immediate(RHS, SImm)) |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3592 | return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3593 | getI64Imm(SImm & 0xFFFF, dl)), |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3594 | 0); |
| 3595 | Opc = PPC::CMPD; |
| 3596 | } |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3597 | } else if (LHS.getValueType() == MVT::f32) { |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 3598 | Opc = PPC::FCMPUS; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3599 | } else { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3600 | assert(LHS.getValueType() == MVT::f64 && "Unknown vt!"); |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 3601 | Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3602 | } |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3603 | return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0); |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3606 | static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) { |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3607 | switch (CC) { |
Chris Lattner | 630bbce | 2006-05-25 16:54:16 +0000 | [diff] [blame] | 3608 | case ISD::SETUEQ: |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 3609 | case ISD::SETONE: |
| 3610 | case ISD::SETOLE: |
| 3611 | case ISD::SETOGE: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3612 | llvm_unreachable("Should be lowered by legalize!"); |
| 3613 | default: llvm_unreachable("Unknown condition!"); |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 3614 | case ISD::SETOEQ: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3615 | case ISD::SETEQ: return PPC::PRED_EQ; |
Chris Lattner | 630bbce | 2006-05-25 16:54:16 +0000 | [diff] [blame] | 3616 | case ISD::SETUNE: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3617 | case ISD::SETNE: return PPC::PRED_NE; |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 3618 | case ISD::SETOLT: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3619 | case ISD::SETLT: return PPC::PRED_LT; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3620 | case ISD::SETULE: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3621 | case ISD::SETLE: return PPC::PRED_LE; |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 3622 | case ISD::SETOGT: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3623 | case ISD::SETGT: return PPC::PRED_GT; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3624 | case ISD::SETUGE: |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3625 | case ISD::SETGE: return PPC::PRED_GE; |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 3626 | case ISD::SETO: return PPC::PRED_NU; |
| 3627 | case ISD::SETUO: return PPC::PRED_UN; |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 3628 | // These two are invalid for floating point. Assume we have int. |
| 3629 | case ISD::SETULT: return PPC::PRED_LT; |
| 3630 | case ISD::SETUGT: return PPC::PRED_GT; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3631 | } |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 3634 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 3635 | /// associated with the SetCC condition, and whether or not the field is |
| 3636 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
Ulrich Weigand | 47e9328 | 2013-07-03 15:13:30 +0000 | [diff] [blame] | 3637 | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) { |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 3638 | Invert = false; |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 3639 | switch (CC) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3640 | default: llvm_unreachable("Unknown condition!"); |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 3641 | case ISD::SETOLT: |
| 3642 | case ISD::SETLT: return 0; // Bit #0 = SETOLT |
| 3643 | case ISD::SETOGT: |
| 3644 | case ISD::SETGT: return 1; // Bit #1 = SETOGT |
| 3645 | case ISD::SETOEQ: |
| 3646 | case ISD::SETEQ: return 2; // Bit #2 = SETOEQ |
| 3647 | case ISD::SETUO: return 3; // Bit #3 = SETUO |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 3648 | case ISD::SETUGE: |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 3649 | case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 3650 | case ISD::SETULE: |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 3651 | case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE |
Chris Lattner | 1fbb0d3 | 2006-05-25 18:06:16 +0000 | [diff] [blame] | 3652 | case ISD::SETUNE: |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 3653 | case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE |
| 3654 | case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3655 | case ISD::SETUEQ: |
| 3656 | case ISD::SETOGE: |
| 3657 | case ISD::SETOLE: |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 3658 | case ISD::SETONE: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3659 | llvm_unreachable("Invalid branch code: should be expanded by legalize"); |
Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 3660 | // These are invalid for floating point. Assume integer. |
| 3661 | case ISD::SETULT: return 0; |
| 3662 | case ISD::SETUGT: return 1; |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 3663 | } |
Chris Lattner | 3dcd75b | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 3664 | } |
Chris Lattner | c5292ec | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 3665 | |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 3666 | // getVCmpInst: return the vector compare instruction for the specified |
| 3667 | // vector type and condition code. Since this is for altivec specific code, |
Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 3668 | // only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32). |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3669 | static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC, |
| 3670 | bool HasVSX, bool &Swap, bool &Negate) { |
| 3671 | Swap = false; |
| 3672 | Negate = false; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 3673 | |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3674 | if (VecVT.isFloatingPoint()) { |
| 3675 | /* Handle some cases by swapping input operands. */ |
| 3676 | switch (CC) { |
| 3677 | case ISD::SETLE: CC = ISD::SETGE; Swap = true; break; |
| 3678 | case ISD::SETLT: CC = ISD::SETGT; Swap = true; break; |
| 3679 | case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break; |
| 3680 | case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break; |
| 3681 | case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break; |
| 3682 | case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break; |
| 3683 | default: break; |
| 3684 | } |
| 3685 | /* Handle some cases by negating the result. */ |
| 3686 | switch (CC) { |
| 3687 | case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break; |
| 3688 | case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break; |
| 3689 | case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break; |
| 3690 | case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break; |
| 3691 | default: break; |
| 3692 | } |
| 3693 | /* We have instructions implementing the remaining cases. */ |
| 3694 | switch (CC) { |
| 3695 | case ISD::SETEQ: |
| 3696 | case ISD::SETOEQ: |
| 3697 | if (VecVT == MVT::v4f32) |
| 3698 | return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP; |
| 3699 | else if (VecVT == MVT::v2f64) |
| 3700 | return PPC::XVCMPEQDP; |
| 3701 | break; |
| 3702 | case ISD::SETGT: |
| 3703 | case ISD::SETOGT: |
| 3704 | if (VecVT == MVT::v4f32) |
| 3705 | return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP; |
| 3706 | else if (VecVT == MVT::v2f64) |
| 3707 | return PPC::XVCMPGTDP; |
| 3708 | break; |
| 3709 | case ISD::SETGE: |
| 3710 | case ISD::SETOGE: |
| 3711 | if (VecVT == MVT::v4f32) |
| 3712 | return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP; |
| 3713 | else if (VecVT == MVT::v2f64) |
| 3714 | return PPC::XVCMPGEDP; |
| 3715 | break; |
| 3716 | default: |
| 3717 | break; |
| 3718 | } |
| 3719 | llvm_unreachable("Invalid floating-point vector compare condition"); |
| 3720 | } else { |
| 3721 | /* Handle some cases by swapping input operands. */ |
| 3722 | switch (CC) { |
| 3723 | case ISD::SETGE: CC = ISD::SETLE; Swap = true; break; |
| 3724 | case ISD::SETLT: CC = ISD::SETGT; Swap = true; break; |
| 3725 | case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break; |
| 3726 | case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break; |
| 3727 | default: break; |
| 3728 | } |
| 3729 | /* Handle some cases by negating the result. */ |
| 3730 | switch (CC) { |
| 3731 | case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break; |
| 3732 | case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break; |
| 3733 | case ISD::SETLE: CC = ISD::SETGT; Negate = true; break; |
| 3734 | case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break; |
| 3735 | default: break; |
| 3736 | } |
| 3737 | /* We have instructions implementing the remaining cases. */ |
| 3738 | switch (CC) { |
| 3739 | case ISD::SETEQ: |
| 3740 | case ISD::SETUEQ: |
| 3741 | if (VecVT == MVT::v16i8) |
| 3742 | return PPC::VCMPEQUB; |
| 3743 | else if (VecVT == MVT::v8i16) |
| 3744 | return PPC::VCMPEQUH; |
| 3745 | else if (VecVT == MVT::v4i32) |
| 3746 | return PPC::VCMPEQUW; |
Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 3747 | else if (VecVT == MVT::v2i64) |
| 3748 | return PPC::VCMPEQUD; |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3749 | break; |
| 3750 | case ISD::SETGT: |
| 3751 | if (VecVT == MVT::v16i8) |
| 3752 | return PPC::VCMPGTSB; |
| 3753 | else if (VecVT == MVT::v8i16) |
| 3754 | return PPC::VCMPGTSH; |
| 3755 | else if (VecVT == MVT::v4i32) |
| 3756 | return PPC::VCMPGTSW; |
Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 3757 | else if (VecVT == MVT::v2i64) |
| 3758 | return PPC::VCMPGTSD; |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3759 | break; |
| 3760 | case ISD::SETUGT: |
| 3761 | if (VecVT == MVT::v16i8) |
| 3762 | return PPC::VCMPGTUB; |
| 3763 | else if (VecVT == MVT::v8i16) |
| 3764 | return PPC::VCMPGTUH; |
| 3765 | else if (VecVT == MVT::v4i32) |
| 3766 | return PPC::VCMPGTUW; |
Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 3767 | else if (VecVT == MVT::v2i64) |
| 3768 | return PPC::VCMPGTUD; |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3769 | break; |
| 3770 | default: |
| 3771 | break; |
| 3772 | } |
| 3773 | llvm_unreachable("Invalid integer vector compare condition"); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3777 | bool PPCDAGToDAGISel::trySETCC(SDNode *N) { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3778 | SDLoc dl(N); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3779 | unsigned Imm; |
| 3780 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3781 | EVT PtrVT = |
| 3782 | CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout()); |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 3783 | bool isPPC64 = (PtrVT == MVT::i64); |
| 3784 | |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 3785 | if (!PPCSubTarget->useCRBits() && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3786 | isInt32Immediate(N->getOperand(1), Imm)) { |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3787 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 3788 | // Check for those cases here. |
| 3789 | // setcc op, 0 |
| 3790 | if (Imm == 0) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3791 | SDValue Op = N->getOperand(0); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3792 | switch (CC) { |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3793 | default: break; |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 3794 | case ISD::SETEQ: { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3795 | Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3796 | SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl), |
| 3797 | getI32Imm(31, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3798 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 3799 | return true; |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 3800 | } |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3801 | case ISD::SETNE: { |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 3802 | if (isPPC64) break; |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3803 | SDValue AD = |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3804 | SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3805 | Op, getI32Imm(~0U, dl)), 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3806 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); |
| 3807 | return true; |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3808 | } |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 3809 | case ISD::SETLT: { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3810 | SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl), |
| 3811 | getI32Imm(31, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3812 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 3813 | return true; |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 3814 | } |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3815 | case ISD::SETGT: { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3816 | SDValue T = |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3817 | SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0); |
| 3818 | T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3819 | SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl), |
| 3820 | getI32Imm(31, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3821 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 3822 | return true; |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3823 | } |
| 3824 | } |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3825 | } else if (Imm == ~0U) { // setcc op, -1 |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3826 | SDValue Op = N->getOperand(0); |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3827 | switch (CC) { |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3828 | default: break; |
| 3829 | case ISD::SETEQ: |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 3830 | if (isPPC64) break; |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3831 | Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3832 | Op, getI32Imm(1, dl)), 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3833 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
| 3834 | SDValue(CurDAG->getMachineNode(PPC::LI, dl, |
| 3835 | MVT::i32, |
| 3836 | getI32Imm(0, dl)), |
| 3837 | 0), Op.getValue(1)); |
| 3838 | return true; |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3839 | case ISD::SETNE: { |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 3840 | if (isPPC64) break; |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3841 | Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0); |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3842 | SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3843 | Op, getI32Imm(~0U, dl)); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3844 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), Op, |
| 3845 | SDValue(AD, 1)); |
| 3846 | return true; |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3847 | } |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3848 | case ISD::SETLT: { |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3849 | SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3850 | getI32Imm(1, dl)), 0); |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 3851 | SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD, |
| 3852 | Op), 0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3853 | SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl), |
| 3854 | getI32Imm(31, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3855 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 3856 | return true; |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3857 | } |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 3858 | case ISD::SETGT: { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3859 | SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl), |
| 3860 | getI32Imm(31, dl) }; |
| 3861 | Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3862 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1, dl)); |
| 3863 | return true; |
Chris Lattner | e296949 | 2005-10-21 21:17:10 +0000 | [diff] [blame] | 3864 | } |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 3865 | } |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3866 | } |
| 3867 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3868 | |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 3869 | SDValue LHS = N->getOperand(0); |
| 3870 | SDValue RHS = N->getOperand(1); |
| 3871 | |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 3872 | // Altivec Vector compare instructions do not set any CR register by default and |
| 3873 | // vector compare operations return the same type as the operands. |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 3874 | if (LHS.getValueType().isVector()) { |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3875 | if (PPCSubTarget->hasQPX()) |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3876 | return false; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3877 | |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 3878 | EVT VecVT = LHS.getValueType(); |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3879 | bool Swap, Negate; |
| 3880 | unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC, |
| 3881 | PPCSubTarget->hasVSX(), Swap, Negate); |
| 3882 | if (Swap) |
| 3883 | std::swap(LHS, RHS); |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 3884 | |
Hal Finkel | 9fdce9a | 2015-08-20 03:02:02 +0000 | [diff] [blame] | 3885 | EVT ResVT = VecVT.changeVectorElementTypeToInteger(); |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3886 | if (Negate) { |
Hal Finkel | 9fdce9a | 2015-08-20 03:02:02 +0000 | [diff] [blame] | 3887 | SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, ResVT, LHS, RHS), 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3888 | CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR : PPC::VNOR, |
| 3889 | ResVT, VCmp, VCmp); |
| 3890 | return true; |
Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 3891 | } |
Ulrich Weigand | c4cc7fe | 2014-08-04 13:13:57 +0000 | [diff] [blame] | 3892 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3893 | CurDAG->SelectNodeTo(N, VCmpInst, ResVT, LHS, RHS); |
| 3894 | return true; |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 3897 | if (PPCSubTarget->useCRBits()) |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3898 | return false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3899 | |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3900 | bool Inv; |
Ulrich Weigand | 47e9328 | 2013-07-03 15:13:30 +0000 | [diff] [blame] | 3901 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 3902 | SDValue CCReg = SelectCC(LHS, RHS, CC, dl); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3903 | SDValue IntCR; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3904 | |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3905 | // Force the ccreg into CR7. |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3906 | SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3907 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 3908 | SDValue InFlag(nullptr, 0); // Null incoming flag value. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3909 | CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, |
Chris Lattner | bd09910 | 2005-12-01 03:50:19 +0000 | [diff] [blame] | 3910 | InFlag).getValue(1); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3911 | |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 3912 | IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg, |
| 3913 | CCReg), 0); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3914 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3915 | SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl), |
| 3916 | getI32Imm(31, dl), getI32Imm(31, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3917 | if (!Inv) { |
| 3918 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 3919 | return true; |
| 3920 | } |
Chris Lattner | 89f36e6 | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 3921 | |
| 3922 | // Get the specified bit. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3923 | SDValue Tmp = |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 3924 | SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3925 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl)); |
| 3926 | return true; |
Chris Lattner | 491b829 | 2005-10-06 19:03:35 +0000 | [diff] [blame] | 3927 | } |
Chris Lattner | 502a369 | 2005-10-06 18:56:10 +0000 | [diff] [blame] | 3928 | |
Nemanja Ivanovic | 3c7e276d | 2017-07-13 18:17:10 +0000 | [diff] [blame] | 3929 | /// Does this node represent a load/store node whose address can be represented |
| 3930 | /// with a register plus an immediate that's a multiple of \p Val: |
| 3931 | bool PPCDAGToDAGISel::isOffsetMultipleOf(SDNode *N, unsigned Val) const { |
| 3932 | LoadSDNode *LDN = dyn_cast<LoadSDNode>(N); |
| 3933 | StoreSDNode *STN = dyn_cast<StoreSDNode>(N); |
| 3934 | SDValue AddrOp; |
| 3935 | if (LDN) |
| 3936 | AddrOp = LDN->getOperand(1); |
| 3937 | else if (STN) |
| 3938 | AddrOp = STN->getOperand(2); |
| 3939 | |
| 3940 | short Imm = 0; |
Kyle Butt | 8c0314c | 2017-09-09 00:37:56 +0000 | [diff] [blame] | 3941 | if (AddrOp.getOpcode() == ISD::ADD) { |
| 3942 | // If op0 is a frame index that is under aligned, we can't do it either, |
| 3943 | // because it is translated to r31 or r1 + slot + offset. We won't know the |
| 3944 | // slot number until the stack frame is finalized. |
| 3945 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(AddrOp.getOperand(0))) { |
| 3946 | const MachineFrameInfo &MFI = CurDAG->getMachineFunction().getFrameInfo(); |
| 3947 | unsigned SlotAlign = MFI.getObjectAlignment(FI->getIndex()); |
| 3948 | if ((SlotAlign % Val) != 0) |
| 3949 | return false; |
| 3950 | } |
Nemanja Ivanovic | 3c7e276d | 2017-07-13 18:17:10 +0000 | [diff] [blame] | 3951 | return isIntS16Immediate(AddrOp.getOperand(1), Imm) && !(Imm % Val); |
Kyle Butt | 8c0314c | 2017-09-09 00:37:56 +0000 | [diff] [blame] | 3952 | } |
Nemanja Ivanovic | 3c7e276d | 2017-07-13 18:17:10 +0000 | [diff] [blame] | 3953 | |
| 3954 | // If the address comes from the outside, the offset will be zero. |
| 3955 | return AddrOp.getOpcode() == ISD::CopyFromReg; |
| 3956 | } |
| 3957 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3958 | void PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) { |
Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 3959 | // Transfer memoperands. |
| 3960 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); |
| 3961 | MemOp[0] = cast<MemSDNode>(N)->getMemOperand(); |
| 3962 | cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1); |
Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 3965 | // Select - Convert the specified operand from a target-independent to a |
| 3966 | // target-specific node if it hasn't already been changed. |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3967 | void PPCDAGToDAGISel::Select(SDNode *N) { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3968 | SDLoc dl(N); |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 3969 | if (N->isMachineOpcode()) { |
| 3970 | N->setNodeId(-1); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3971 | return; // Already selected. |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 3972 | } |
Chris Lattner | 08c319f | 2005-09-29 00:59:32 +0000 | [diff] [blame] | 3973 | |
Hal Finkel | 51b3fd1 | 2014-09-02 06:23:54 +0000 | [diff] [blame] | 3974 | // In case any misguided DAG-level optimizations form an ADD with a |
| 3975 | // TargetConstant operand, crash here instead of miscompiling (by selecting |
| 3976 | // an r+r add instead of some kind of r+i add). |
| 3977 | if (N->getOpcode() == ISD::ADD && |
| 3978 | N->getOperand(1).getOpcode() == ISD::TargetConstant) |
| 3979 | llvm_unreachable("Invalid ADD with TargetConstant operand"); |
| 3980 | |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3981 | // Try matching complex bit permutations before doing anything else. |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3982 | if (tryBitPermutation(N)) |
| 3983 | return; |
Hal Finkel | 8adf225 | 2014-12-16 05:51:41 +0000 | [diff] [blame] | 3984 | |
Nemanja Ivanovic | db7e770 | 2017-11-30 13:39:10 +0000 | [diff] [blame] | 3985 | // Try to emit integer compares as GPR-only sequences (i.e. no use of CR). |
| 3986 | if (tryIntCompareInGPR(N)) |
| 3987 | return; |
| 3988 | |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 3989 | switch (N->getOpcode()) { |
Chris Lattner | 498915d | 2005-09-07 23:45:15 +0000 | [diff] [blame] | 3990 | default: break; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3991 | |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 3992 | case ISD::Constant: |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3993 | if (N->getValueType(0) == MVT::i64) { |
Hiroshi Inoue | 5703fe3 | 2017-07-31 06:27:09 +0000 | [diff] [blame] | 3994 | ReplaceNode(N, selectI64Imm(CurDAG, N)); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 3995 | return; |
| 3996 | } |
Jim Laskey | 095e6f3 | 2006-12-12 13:23:43 +0000 | [diff] [blame] | 3997 | break; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 3998 | |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 3999 | case ISD::SETCC: |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4000 | if (trySETCC(N)) |
| 4001 | return; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4002 | break; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 4003 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 4004 | case PPCISD::GlobalBaseReg: |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4005 | ReplaceNode(N, getGlobalBaseReg()); |
| 4006 | return; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4007 | |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4008 | case ISD::FrameIndex: |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4009 | selectFrameIndex(N, N); |
| 4010 | return; |
Chris Lattner | 6961fc7 | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 4011 | |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 4012 | case PPCISD::MFOCRF: { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4013 | SDValue InFlag = N->getOperand(1); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4014 | ReplaceNode(N, CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, |
| 4015 | N->getOperand(0), InFlag)); |
| 4016 | return; |
Chris Lattner | 6961fc7 | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 4017 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4018 | |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 4019 | case PPCISD::READ_TIME_BASE: |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4020 | ReplaceNode(N, CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32, |
| 4021 | MVT::Other, N->getOperand(0))); |
| 4022 | return; |
Hal Finkel | bbdee93 | 2014-12-02 22:01:00 +0000 | [diff] [blame] | 4023 | |
Hal Finkel | 13d104b | 2014-12-11 18:37:52 +0000 | [diff] [blame] | 4024 | case PPCISD::SRA_ADDZE: { |
| 4025 | SDValue N0 = N->getOperand(0); |
| 4026 | SDValue ShiftAmt = |
| 4027 | CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))-> |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4028 | getConstantIntValue(), dl, |
| 4029 | N->getValueType(0)); |
Hal Finkel | 13d104b | 2014-12-11 18:37:52 +0000 | [diff] [blame] | 4030 | if (N->getValueType(0) == MVT::i64) { |
| 4031 | SDNode *Op = |
| 4032 | CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue, |
| 4033 | N0, ShiftAmt); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4034 | CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64, SDValue(Op, 0), |
| 4035 | SDValue(Op, 1)); |
| 4036 | return; |
Hal Finkel | 13d104b | 2014-12-11 18:37:52 +0000 | [diff] [blame] | 4037 | } else { |
| 4038 | assert(N->getValueType(0) == MVT::i32 && |
| 4039 | "Expecting i64 or i32 in PPCISD::SRA_ADDZE"); |
| 4040 | SDNode *Op = |
| 4041 | CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue, |
| 4042 | N0, ShiftAmt); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4043 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, SDValue(Op, 0), |
| 4044 | SDValue(Op, 1)); |
| 4045 | return; |
Chris Lattner | dc66457 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 4046 | } |
Chris Lattner | 6e184f2 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 4047 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4048 | |
Zaara Syeda | 1110c4d | 2018-03-15 15:34:41 +0000 | [diff] [blame^] | 4049 | case ISD::STORE: { |
| 4050 | // Change TLS initial-exec D-form stores to X-form stores. |
| 4051 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| 4052 | if (EnableTLSOpt && PPCSubTarget->isELFv2ABI() && |
| 4053 | ST->getAddressingMode() != ISD::PRE_INC) |
| 4054 | if (tryTLSXFormStore(ST)) |
| 4055 | return; |
| 4056 | break; |
| 4057 | } |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 4058 | case ISD::LOAD: { |
| 4059 | // Handle preincrement loads. |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 4060 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4061 | EVT LoadedVT = LD->getMemoryVT(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4062 | |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 4063 | // Normal loads are handled by code generated from the .td file. |
Zaara Syeda | 1110c4d | 2018-03-15 15:34:41 +0000 | [diff] [blame^] | 4064 | if (LD->getAddressingMode() != ISD::PRE_INC) { |
| 4065 | // Change TLS initial-exec D-form loads to X-form loads. |
| 4066 | if (EnableTLSOpt && PPCSubTarget->isELFv2ABI()) |
| 4067 | if (tryTLSXFormLoad(LD)) |
| 4068 | return; |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 4069 | break; |
Zaara Syeda | 1110c4d | 2018-03-15 15:34:41 +0000 | [diff] [blame^] | 4070 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4071 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4072 | SDValue Offset = LD->getOffset(); |
Ulrich Weigand | d1b99d3 | 2013-03-22 14:58:17 +0000 | [diff] [blame] | 4073 | if (Offset.getOpcode() == ISD::TargetConstant || |
Chris Lattner | c5102bf | 2006-11-11 04:53:30 +0000 | [diff] [blame] | 4074 | Offset.getOpcode() == ISD::TargetGlobalAddress) { |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4075 | |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 4076 | unsigned Opcode; |
| 4077 | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4078 | if (LD->getValueType(0) != MVT::i64) { |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 4079 | // Handle PPC32 integer and normal FP loads. |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4080 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
| 4081 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4082 | default: llvm_unreachable("Invalid PPC load type!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4083 | case MVT::f64: Opcode = PPC::LFDU; break; |
| 4084 | case MVT::f32: Opcode = PPC::LFSU; break; |
| 4085 | case MVT::i32: Opcode = PPC::LWZU; break; |
| 4086 | case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break; |
| 4087 | case MVT::i1: |
| 4088 | case MVT::i8: Opcode = PPC::LBZU; break; |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 4089 | } |
| 4090 | } else { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4091 | assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); |
| 4092 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
| 4093 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4094 | default: llvm_unreachable("Invalid PPC load type!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4095 | case MVT::i64: Opcode = PPC::LDU; break; |
| 4096 | case MVT::i32: Opcode = PPC::LWZU8; break; |
| 4097 | case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break; |
| 4098 | case MVT::i1: |
| 4099 | case MVT::i8: Opcode = PPC::LBZU8; break; |
Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 4100 | } |
| 4101 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4102 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4103 | SDValue Chain = LD->getChain(); |
| 4104 | SDValue Base = LD->getBasePtr(); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4105 | SDValue Ops[] = { Offset, Base, Chain }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4106 | SDNode *MN = CurDAG->getMachineNode( |
| 4107 | Opcode, dl, LD->getValueType(0), |
| 4108 | PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops); |
| 4109 | transferMemOperands(N, MN); |
| 4110 | ReplaceNode(N, MN); |
| 4111 | return; |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 4112 | } else { |
Hal Finkel | ca542be | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 4113 | unsigned Opcode; |
| 4114 | bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD; |
| 4115 | if (LD->getValueType(0) != MVT::i64) { |
| 4116 | // Handle PPC32 integer and normal FP loads. |
| 4117 | assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load"); |
| 4118 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
| 4119 | default: llvm_unreachable("Invalid PPC load type!"); |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 4120 | case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX |
| 4121 | case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX |
Hal Finkel | ca542be | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 4122 | case MVT::f64: Opcode = PPC::LFDUX; break; |
| 4123 | case MVT::f32: Opcode = PPC::LFSUX; break; |
| 4124 | case MVT::i32: Opcode = PPC::LWZUX; break; |
| 4125 | case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break; |
| 4126 | case MVT::i1: |
| 4127 | case MVT::i8: Opcode = PPC::LBZUX; break; |
| 4128 | } |
| 4129 | } else { |
| 4130 | assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!"); |
| 4131 | assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) && |
| 4132 | "Invalid sext update load"); |
| 4133 | switch (LoadedVT.getSimpleVT().SimpleTy) { |
| 4134 | default: llvm_unreachable("Invalid PPC load type!"); |
| 4135 | case MVT::i64: Opcode = PPC::LDUX; break; |
| 4136 | case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break; |
| 4137 | case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break; |
| 4138 | case MVT::i1: |
| 4139 | case MVT::i8: Opcode = PPC::LBZUX8; break; |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | SDValue Chain = LD->getChain(); |
| 4144 | SDValue Base = LD->getBasePtr(); |
Ulrich Weigand | e90b022 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 4145 | SDValue Ops[] = { Base, Offset, Chain }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4146 | SDNode *MN = CurDAG->getMachineNode( |
| 4147 | Opcode, dl, LD->getValueType(0), |
| 4148 | PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops); |
| 4149 | transferMemOperands(N, MN); |
| 4150 | ReplaceNode(N, MN); |
| 4151 | return; |
Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 4152 | } |
| 4153 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4154 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 4155 | case ISD::AND: { |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 4156 | unsigned Imm, Imm2, SH, MB, ME; |
Hal Finkel | e39526a | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 4157 | uint64_t Imm64; |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 4158 | |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 4159 | // If this is an and of a value rotated between 0 and 31 bits and then and'd |
| 4160 | // with a mask, emit rlwinm |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 4161 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4162 | isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4163 | SDValue Val = N->getOperand(0).getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4164 | SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl), |
| 4165 | getI32Imm(ME, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4166 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 4167 | return; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 4168 | } |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 4169 | // If this is just a masked value where the input is not handled above, and |
| 4170 | // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm |
| 4171 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4172 | isRunOfOnes(Imm, MB, ME) && |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 4173 | N->getOperand(0).getOpcode() != ISD::ROTL) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4174 | SDValue Val = N->getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4175 | SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl), |
| 4176 | getI32Imm(ME, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4177 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 4178 | return; |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 4179 | } |
Hal Finkel | e39526a | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 4180 | // If this is a 64-bit zero-extension mask, emit rldicl. |
| 4181 | if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) && |
| 4182 | isMask_64(Imm64)) { |
| 4183 | SDValue Val = N->getOperand(0); |
Benjamin Kramer | 5f6a907 | 2015-02-12 15:35:40 +0000 | [diff] [blame] | 4184 | MB = 64 - countTrailingOnes(Imm64); |
Hal Finkel | 22498fa | 2013-11-20 01:10:15 +0000 | [diff] [blame] | 4185 | SH = 0; |
| 4186 | |
Ehsan Amiri | 1f31e91 | 2016-10-24 15:46:58 +0000 | [diff] [blame] | 4187 | if (Val.getOpcode() == ISD::ANY_EXTEND) { |
| 4188 | auto Op0 = Val.getOperand(0); |
| 4189 | if ( Op0.getOpcode() == ISD::SRL && |
| 4190 | isInt32Immediate(Op0.getOperand(1).getNode(), Imm) && Imm <= MB) { |
| 4191 | |
| 4192 | auto ResultType = Val.getNode()->getValueType(0); |
| 4193 | auto ImDef = CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, |
| 4194 | ResultType); |
| 4195 | SDValue IDVal (ImDef, 0); |
| 4196 | |
| 4197 | Val = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, |
| 4198 | ResultType, IDVal, Op0.getOperand(0), |
| 4199 | getI32Imm(1, dl)), 0); |
| 4200 | SH = 64 - Imm; |
| 4201 | } |
| 4202 | } |
| 4203 | |
Hal Finkel | 22498fa | 2013-11-20 01:10:15 +0000 | [diff] [blame] | 4204 | // If the operand is a logical right shift, we can fold it into this |
| 4205 | // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb) |
| 4206 | // for n <= mb. The right shift is really a left rotate followed by a |
| 4207 | // mask, and this mask is a more-restrictive sub-mask of the mask implied |
| 4208 | // by the shift. |
| 4209 | if (Val.getOpcode() == ISD::SRL && |
| 4210 | isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) { |
| 4211 | assert(Imm < 64 && "Illegal shift amount"); |
| 4212 | Val = Val.getOperand(0); |
| 4213 | SH = 64 - Imm; |
| 4214 | } |
| 4215 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4216 | SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4217 | CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops); |
| 4218 | return; |
Hal Finkel | e39526a | 2012-08-28 02:10:15 +0000 | [diff] [blame] | 4219 | } |
Nemanja Ivanovic | 82d53ed | 2017-02-24 18:03:16 +0000 | [diff] [blame] | 4220 | // If this is a negated 64-bit zero-extension mask, |
| 4221 | // i.e. the immediate is a sequence of ones from most significant side |
| 4222 | // and all zero for reminder, we should use rldicr. |
| 4223 | if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) && |
| 4224 | isMask_64(~Imm64)) { |
| 4225 | SDValue Val = N->getOperand(0); |
| 4226 | MB = 63 - countTrailingOnes(~Imm64); |
| 4227 | SH = 0; |
| 4228 | SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) }; |
| 4229 | CurDAG->SelectNodeTo(N, PPC::RLDICR, MVT::i64, Ops); |
| 4230 | return; |
| 4231 | } |
| 4232 | |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 4233 | // AND X, 0 -> 0, not "rlwinm 32". |
| 4234 | if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4235 | ReplaceUses(SDValue(N, 0), N->getOperand(1)); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4236 | return; |
Nate Begeman | d31efd1 | 2006-09-22 05:01:56 +0000 | [diff] [blame] | 4237 | } |
Nate Begeman | 9aea6e4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 4238 | // ISD::OR doesn't get all the bitfield insertion fun. |
Hal Finkel | b1518d6 | 2015-09-05 00:02:59 +0000 | [diff] [blame] | 4239 | // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a |
| 4240 | // bitfield insert. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4241 | if (isInt32Immediate(N->getOperand(1), Imm) && |
Nate Begeman | 9aea6e4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 4242 | N->getOperand(0).getOpcode() == ISD::OR && |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 4243 | isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) { |
Hal Finkel | b1518d6 | 2015-09-05 00:02:59 +0000 | [diff] [blame] | 4244 | // The idea here is to check whether this is equivalent to: |
| 4245 | // (c1 & m) | (x & ~m) |
| 4246 | // where m is a run-of-ones mask. The logic here is that, for each bit in |
| 4247 | // c1 and c2: |
| 4248 | // - if both are 1, then the output will be 1. |
| 4249 | // - if both are 0, then the output will be 0. |
| 4250 | // - if the bit in c1 is 0, and the bit in c2 is 1, then the output will |
| 4251 | // come from x. |
| 4252 | // - if the bit in c1 is 1, and the bit in c2 is 0, then the output will |
| 4253 | // be 0. |
| 4254 | // If that last condition is never the case, then we can form m from the |
| 4255 | // bits that are the same between c1 and c2. |
Chris Lattner | 20c88df | 2006-01-05 18:32:49 +0000 | [diff] [blame] | 4256 | unsigned MB, ME; |
Hal Finkel | b1518d6 | 2015-09-05 00:02:59 +0000 | [diff] [blame] | 4257 | if (isRunOfOnes(~(Imm^Imm2), MB, ME) && !(~Imm & Imm2)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4258 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 4259 | N->getOperand(0).getOperand(1), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4260 | getI32Imm(0, dl), getI32Imm(MB, dl), |
| 4261 | getI32Imm(ME, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4262 | ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops)); |
| 4263 | return; |
Nate Begeman | 9aea6e4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 4264 | } |
| 4265 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4266 | |
Chris Lattner | 1de5706 | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 4267 | // Other cases are autogenerated. |
| 4268 | break; |
Nate Begeman | b3821a3 | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 4269 | } |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4270 | case ISD::OR: { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4271 | if (N->getValueType(0) == MVT::i32) |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4272 | if (tryBitfieldInsert(N)) |
| 4273 | return; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4274 | |
Lei Huang | 3171041 | 2017-07-07 21:12:35 +0000 | [diff] [blame] | 4275 | int16_t Imm; |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4276 | if (N->getOperand(0)->getOpcode() == ISD::FrameIndex && |
| 4277 | isIntS16Immediate(N->getOperand(1), Imm)) { |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 4278 | KnownBits LHSKnown; |
| 4279 | CurDAG->computeKnownBits(N->getOperand(0), LHSKnown); |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4280 | |
| 4281 | // If this is equivalent to an add, then we can fold it with the |
| 4282 | // FrameIndex calculation. |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 4283 | if ((LHSKnown.Zero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) { |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4284 | selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm); |
| 4285 | return; |
| 4286 | } |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
Hiroshi Inoue | cc555bd | 2017-08-23 08:55:18 +0000 | [diff] [blame] | 4289 | // OR with a 32-bit immediate can be handled by ori + oris |
| 4290 | // without creating an immediate in a GPR. |
| 4291 | uint64_t Imm64 = 0; |
| 4292 | bool IsPPC64 = PPCSubTarget->isPPC64(); |
| 4293 | if (IsPPC64 && isInt64Immediate(N->getOperand(1), Imm64) && |
| 4294 | (Imm64 & ~0xFFFFFFFFuLL) == 0) { |
| 4295 | // If ImmHi (ImmHi) is zero, only one ori (oris) is generated later. |
| 4296 | uint64_t ImmHi = Imm64 >> 16; |
| 4297 | uint64_t ImmLo = Imm64 & 0xFFFF; |
| 4298 | if (ImmHi != 0 && ImmLo != 0) { |
| 4299 | SDNode *Lo = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64, |
| 4300 | N->getOperand(0), |
| 4301 | getI16Imm(ImmLo, dl)); |
| 4302 | SDValue Ops1[] = { SDValue(Lo, 0), getI16Imm(ImmHi, dl)}; |
| 4303 | CurDAG->SelectNodeTo(N, PPC::ORIS8, MVT::i64, Ops1); |
| 4304 | return; |
| 4305 | } |
| 4306 | } |
| 4307 | |
Chris Lattner | 1de5706 | 2005-09-29 23:33:31 +0000 | [diff] [blame] | 4308 | // Other cases are autogenerated. |
| 4309 | break; |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4310 | } |
Nemanja Ivanovic | e597bd8 | 2017-05-31 05:40:25 +0000 | [diff] [blame] | 4311 | case ISD::XOR: { |
Hiroshi Inoue | cc555bd | 2017-08-23 08:55:18 +0000 | [diff] [blame] | 4312 | // XOR with a 32-bit immediate can be handled by xori + xoris |
| 4313 | // without creating an immediate in a GPR. |
| 4314 | uint64_t Imm64 = 0; |
| 4315 | bool IsPPC64 = PPCSubTarget->isPPC64(); |
| 4316 | if (IsPPC64 && isInt64Immediate(N->getOperand(1), Imm64) && |
| 4317 | (Imm64 & ~0xFFFFFFFFuLL) == 0) { |
| 4318 | // If ImmHi (ImmHi) is zero, only one xori (xoris) is generated later. |
| 4319 | uint64_t ImmHi = Imm64 >> 16; |
| 4320 | uint64_t ImmLo = Imm64 & 0xFFFF; |
| 4321 | if (ImmHi != 0 && ImmLo != 0) { |
| 4322 | SDNode *Lo = CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, |
| 4323 | N->getOperand(0), |
| 4324 | getI16Imm(ImmLo, dl)); |
| 4325 | SDValue Ops1[] = { SDValue(Lo, 0), getI16Imm(ImmHi, dl)}; |
| 4326 | CurDAG->SelectNodeTo(N, PPC::XORIS8, MVT::i64, Ops1); |
| 4327 | return; |
| 4328 | } |
| 4329 | } |
| 4330 | |
Nemanja Ivanovic | e597bd8 | 2017-05-31 05:40:25 +0000 | [diff] [blame] | 4331 | break; |
| 4332 | } |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4333 | case ISD::ADD: { |
Lei Huang | 3171041 | 2017-07-07 21:12:35 +0000 | [diff] [blame] | 4334 | int16_t Imm; |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4335 | if (N->getOperand(0)->getOpcode() == ISD::FrameIndex && |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4336 | isIntS16Immediate(N->getOperand(1), Imm)) { |
| 4337 | selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm); |
| 4338 | return; |
| 4339 | } |
Hal Finkel | b5e9b04 | 2014-12-11 22:51:06 +0000 | [diff] [blame] | 4340 | |
| 4341 | break; |
| 4342 | } |
Nate Begeman | 33acb2c | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 4343 | case ISD::SHL: { |
| 4344 | unsigned Imm, SH, MB, ME; |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4345 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Nate Begeman | 9f3c26c | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 4346 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4347 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4348 | getI32Imm(SH, dl), getI32Imm(MB, dl), |
| 4349 | getI32Imm(ME, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4350 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 4351 | return; |
Nate Begeman | 9eaa6ba | 2005-10-19 01:12:32 +0000 | [diff] [blame] | 4352 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4353 | |
Nate Begeman | 9f3c26c | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 4354 | // Other cases are autogenerated. |
| 4355 | break; |
Nate Begeman | 33acb2c | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 4356 | } |
| 4357 | case ISD::SRL: { |
| 4358 | unsigned Imm, SH, MB, ME; |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4359 | if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) && |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4360 | isRotateAndMask(N, Imm, true, SH, MB, ME)) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4361 | SDValue Ops[] = { N->getOperand(0).getOperand(0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4362 | getI32Imm(SH, dl), getI32Imm(MB, dl), |
| 4363 | getI32Imm(ME, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4364 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops); |
| 4365 | return; |
Nate Begeman | 9eaa6ba | 2005-10-19 01:12:32 +0000 | [diff] [blame] | 4366 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4367 | |
Nate Begeman | 9f3c26c | 2005-10-19 18:42:01 +0000 | [diff] [blame] | 4368 | // Other cases are autogenerated. |
| 4369 | break; |
Nate Begeman | 33acb2c | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 4370 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4371 | // FIXME: Remove this once the ANDI glue bug is fixed: |
| 4372 | case PPCISD::ANDIo_1_EQ_BIT: |
| 4373 | case PPCISD::ANDIo_1_GT_BIT: { |
| 4374 | if (!ANDIGlueBug) |
| 4375 | break; |
| 4376 | |
| 4377 | EVT InVT = N->getOperand(0).getValueType(); |
| 4378 | assert((InVT == MVT::i64 || InVT == MVT::i32) && |
| 4379 | "Invalid input type for ANDIo_1_EQ_BIT"); |
| 4380 | |
| 4381 | unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo; |
| 4382 | SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue, |
| 4383 | N->getOperand(0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4384 | CurDAG->getTargetConstant(1, dl, InVT)), |
| 4385 | 0); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4386 | SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32); |
| 4387 | SDValue SRIdxVal = |
| 4388 | CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ? |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4389 | PPC::sub_eq : PPC::sub_gt, dl, MVT::i32); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4390 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4391 | CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1, CR0Reg, |
| 4392 | SRIdxVal, SDValue(AndI.getNode(), 1) /* glue */); |
| 4393 | return; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4394 | } |
Chris Lattner | bec817c | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 4395 | case ISD::SELECT_CC: { |
| 4396 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4397 | EVT PtrVT = |
| 4398 | CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout()); |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 4399 | bool isPPC64 = (PtrVT == MVT::i64); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4400 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4401 | // 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] | 4402 | if (PPCSubTarget->useCRBits() && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4403 | N->getOperand(0).getValueType() == MVT::i1) |
| 4404 | break; |
| 4405 | |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 4406 | // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 4407 | if (!isPPC64) |
| 4408 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 4409 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 4410 | if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 4411 | if (N1C->isNullValue() && N3C->isNullValue() && |
| 4412 | N2C->getZExtValue() == 1ULL && CC == ISD::SETNE && |
| 4413 | // FIXME: Implement this optzn for PPC64. |
| 4414 | N->getValueType(0) == MVT::i32) { |
| 4415 | SDNode *Tmp = |
| 4416 | CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4417 | N->getOperand(0), getI32Imm(~0U, dl)); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4418 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(Tmp, 0), |
| 4419 | N->getOperand(0), SDValue(Tmp, 1)); |
| 4420 | return; |
Roman Divacky | 254f821 | 2011-06-20 15:28:39 +0000 | [diff] [blame] | 4421 | } |
Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 4422 | |
Dale Johannesen | ab8e442 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 4423 | SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4424 | |
| 4425 | if (N->getValueType(0) == MVT::i1) { |
| 4426 | // An i1 select is: (c & t) | (!c & f). |
| 4427 | bool Inv; |
| 4428 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
| 4429 | |
| 4430 | unsigned SRI; |
| 4431 | switch (Idx) { |
| 4432 | default: llvm_unreachable("Invalid CC index"); |
| 4433 | case 0: SRI = PPC::sub_lt; break; |
| 4434 | case 1: SRI = PPC::sub_gt; break; |
| 4435 | case 2: SRI = PPC::sub_eq; break; |
| 4436 | case 3: SRI = PPC::sub_un; break; |
| 4437 | } |
| 4438 | |
| 4439 | SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg); |
| 4440 | |
| 4441 | SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1, |
| 4442 | CCBit, CCBit), 0); |
| 4443 | SDValue C = Inv ? NotCCBit : CCBit, |
| 4444 | NotC = Inv ? CCBit : NotCCBit; |
| 4445 | |
| 4446 | SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1, |
| 4447 | C, N->getOperand(2)), 0); |
| 4448 | SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1, |
| 4449 | NotC, N->getOperand(3)), 0); |
| 4450 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4451 | CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF); |
| 4452 | return; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 4455 | unsigned BROpc = getPredicateForSetCC(CC); |
Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 4456 | |
Chris Lattner | d3eee1a | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 4457 | unsigned SelectCCOp; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4458 | if (N->getValueType(0) == MVT::i32) |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 4459 | SelectCCOp = PPC::SELECT_CC_I4; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4460 | else if (N->getValueType(0) == MVT::i64) |
Chris Lattner | 97b3da1 | 2006-06-27 00:04:13 +0000 | [diff] [blame] | 4461 | SelectCCOp = PPC::SELECT_CC_I8; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4462 | else if (N->getValueType(0) == MVT::f32) |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 4463 | if (PPCSubTarget->hasP8Vector()) |
| 4464 | SelectCCOp = PPC::SELECT_CC_VSSRC; |
| 4465 | else |
| 4466 | SelectCCOp = PPC::SELECT_CC_F4; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4467 | else if (N->getValueType(0) == MVT::f64) |
Bill Schmidt | 9c54bbd | 2014-10-22 16:58:20 +0000 | [diff] [blame] | 4468 | if (PPCSubTarget->hasVSX()) |
| 4469 | SelectCCOp = PPC::SELECT_CC_VSFRC; |
| 4470 | else |
| 4471 | SelectCCOp = PPC::SELECT_CC_F8; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 4472 | else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64) |
| 4473 | SelectCCOp = PPC::SELECT_CC_QFRC; |
| 4474 | else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32) |
| 4475 | SelectCCOp = PPC::SELECT_CC_QSRC; |
| 4476 | else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1) |
| 4477 | SelectCCOp = PPC::SELECT_CC_QBRC; |
Bill Schmidt | 61e6523 | 2014-10-22 13:13:40 +0000 | [diff] [blame] | 4478 | else if (N->getValueType(0) == MVT::v2f64 || |
| 4479 | N->getValueType(0) == MVT::v2i64) |
| 4480 | SelectCCOp = PPC::SELECT_CC_VSRC; |
Chris Lattner | 0a3d1bb | 2006-04-08 22:45:08 +0000 | [diff] [blame] | 4481 | else |
| 4482 | SelectCCOp = PPC::SELECT_CC_VRRC; |
| 4483 | |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4484 | SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4485 | getI32Imm(BROpc, dl) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4486 | CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops); |
| 4487 | return; |
Chris Lattner | bec817c | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 4488 | } |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 4489 | case ISD::VSELECT: |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 4490 | if (PPCSubTarget->hasVSX()) { |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 4491 | SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4492 | CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops); |
| 4493 | return; |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 4494 | } |
Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 4495 | break; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 4496 | |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4497 | case ISD::VECTOR_SHUFFLE: |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 4498 | if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 || |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4499 | N->getValueType(0) == MVT::v2i64)) { |
| 4500 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
Kyle Butt | 015f4fc | 2015-12-02 18:53:33 +0000 | [diff] [blame] | 4501 | |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4502 | SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1), |
| 4503 | Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1); |
| 4504 | unsigned DM[2]; |
| 4505 | |
| 4506 | for (int i = 0; i < 2; ++i) |
| 4507 | if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2) |
| 4508 | DM[i] = 0; |
| 4509 | else |
| 4510 | DM[i] = 1; |
| 4511 | |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4512 | if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 && |
| 4513 | Op1.getOpcode() == ISD::SCALAR_TO_VECTOR && |
| 4514 | isa<LoadSDNode>(Op1.getOperand(0))) { |
| 4515 | LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0)); |
| 4516 | SDValue Base, Offset; |
| 4517 | |
Nemanja Ivanovic | be5f0c0 | 2015-11-02 14:01:11 +0000 | [diff] [blame] | 4518 | if (LD->isUnindexed() && LD->hasOneUse() && Op1.hasOneUse() && |
Bill Schmidt | 048cc97 | 2015-10-14 20:45:00 +0000 | [diff] [blame] | 4519 | (LD->getMemoryVT() == MVT::f64 || |
| 4520 | LD->getMemoryVT() == MVT::i64) && |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4521 | SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) { |
| 4522 | SDValue Chain = LD->getChain(); |
| 4523 | SDValue Ops[] = { Base, Offset, Chain }; |
Sean Fertile | 3c8c385 | 2017-01-26 18:59:15 +0000 | [diff] [blame] | 4524 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); |
| 4525 | MemOp[0] = LD->getMemOperand(); |
Benjamin Kramer | 58dadd5 | 2017-04-20 18:29:14 +0000 | [diff] [blame] | 4526 | SDNode *NewN = CurDAG->SelectNodeTo(N, PPC::LXVDSX, |
| 4527 | N->getValueType(0), Ops); |
Sean Fertile | 3c8c385 | 2017-01-26 18:59:15 +0000 | [diff] [blame] | 4528 | cast<MachineSDNode>(NewN)->setMemRefs(MemOp, MemOp + 1); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4529 | return; |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4530 | } |
| 4531 | } |
| 4532 | |
Bill Schmidt | ae94f11 | 2015-07-01 19:40:07 +0000 | [diff] [blame] | 4533 | // For little endian, we must swap the input operands and adjust |
| 4534 | // the mask elements (reverse and invert them). |
| 4535 | if (PPCSubTarget->isLittleEndian()) { |
| 4536 | std::swap(Op1, Op2); |
| 4537 | unsigned tmp = DM[0]; |
| 4538 | DM[0] = 1 - DM[1]; |
| 4539 | DM[1] = 1 - tmp; |
| 4540 | } |
| 4541 | |
| 4542 | SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl, |
| 4543 | MVT::i32); |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4544 | SDValue Ops[] = { Op1, Op2, DMV }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4545 | CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops); |
| 4546 | return; |
Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | break; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 4550 | case PPCISD::BDNZ: |
| 4551 | case PPCISD::BDZ: { |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 4552 | bool IsPPC64 = PPCSubTarget->isPPC64(); |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 4553 | SDValue Ops[] = { N->getOperand(1), N->getOperand(0) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4554 | CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ |
| 4555 | ? (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) |
| 4556 | : (IsPPC64 ? PPC::BDZ8 : PPC::BDZ), |
| 4557 | MVT::Other, Ops); |
| 4558 | return; |
Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 4559 | } |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 4560 | case PPCISD::COND_BRANCH: { |
Dan Gohman | 7a638a8 | 2008-11-05 17:16:24 +0000 | [diff] [blame] | 4561 | // Op #0 is the Chain. |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 4562 | // Op #1 is the PPC::PRED_* number. |
| 4563 | // Op #2 is the CR# |
| 4564 | // Op #3 is the Dest MBB |
Dan Gohman | f14b77e | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 4565 | // Op #4 is the Flag. |
Evan Cheng | 58d1eac | 2007-06-29 01:25:06 +0000 | [diff] [blame] | 4566 | // Prevent PPC::PRED_* from being selected into LI. |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 4567 | unsigned PCC = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); |
| 4568 | if (EnableBranchHint) |
| 4569 | PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(3)); |
| 4570 | |
| 4571 | SDValue Pred = getI32Imm(PCC, dl); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4572 | SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3), |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 4573 | N->getOperand(0), N->getOperand(4) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4574 | CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); |
| 4575 | return; |
Chris Lattner | be9377a | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 4576 | } |
Nate Begeman | bb01d4f | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 4577 | case ISD::BR_CC: { |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 4578 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4579 | unsigned PCC = getPredicateForSetCC(CC); |
| 4580 | |
| 4581 | if (N->getOperand(2).getValueType() == MVT::i1) { |
| 4582 | unsigned Opc; |
| 4583 | bool Swap; |
| 4584 | switch (PCC) { |
| 4585 | default: llvm_unreachable("Unexpected Boolean-operand predicate"); |
| 4586 | case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break; |
| 4587 | case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break; |
| 4588 | case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break; |
| 4589 | case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break; |
| 4590 | case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break; |
| 4591 | case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break; |
| 4592 | } |
| 4593 | |
| 4594 | SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1, |
| 4595 | N->getOperand(Swap ? 3 : 2), |
| 4596 | N->getOperand(Swap ? 2 : 3)), 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4597 | CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other, BitComp, N->getOperand(4), |
| 4598 | N->getOperand(0)); |
| 4599 | return; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Hal Finkel | 65539e3 | 2015-12-12 00:32:00 +0000 | [diff] [blame] | 4602 | if (EnableBranchHint) |
| 4603 | PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(4)); |
| 4604 | |
Dale Johannesen | ab8e442 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 4605 | SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4606 | SDValue Ops[] = { getI32Imm(PCC, dl), CondCode, |
Evan Cheng | c3acfc0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 4607 | N->getOperand(4), N->getOperand(0) }; |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4608 | CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops); |
| 4609 | return; |
Chris Lattner | 2a1823d | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 4610 | } |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4611 | case ISD::BRIND: { |
Chris Lattner | b055c87 | 2006-06-10 01:15:02 +0000 | [diff] [blame] | 4612 | // FIXME: Should custom lower this. |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4613 | SDValue Chain = N->getOperand(0); |
| 4614 | SDValue Target = N->getOperand(1); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4615 | unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8; |
Roman Divacky | a4a59ae | 2011-06-03 15:47:49 +0000 | [diff] [blame] | 4616 | unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8; |
Hal Finkel | 528ff4b | 2011-12-08 04:36:44 +0000 | [diff] [blame] | 4617 | Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target, |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 4618 | Chain), 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4619 | CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain); |
| 4620 | return; |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4621 | } |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4622 | case PPCISD::TOC_ENTRY: { |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 4623 | assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) && |
| 4624 | "Only supported for 64-bit ABI and 32-bit SVR4"); |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 4625 | if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) { |
| 4626 | SDValue GA = N->getOperand(0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4627 | SDNode *MN = CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA, |
| 4628 | N->getOperand(1)); |
| 4629 | transferMemOperands(N, MN); |
| 4630 | ReplaceNode(N, MN); |
| 4631 | return; |
Justin Hibbits | 3476db4 | 2014-08-28 04:40:55 +0000 | [diff] [blame] | 4632 | } |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4633 | |
Bill Schmidt | 2791778 | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 4634 | // For medium and large code model, we generate two instructions as |
| 4635 | // described below. Otherwise we allow SelectCodeCommon to handle this, |
Ulrich Weigand | c8c2ea2 | 2014-10-31 10:33:14 +0000 | [diff] [blame] | 4636 | // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA. |
Bill Schmidt | 2791778 | 2013-02-21 17:12:27 +0000 | [diff] [blame] | 4637 | CodeModel::Model CModel = TM.getCodeModel(); |
| 4638 | if (CModel != CodeModel::Medium && CModel != CodeModel::Large) |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4639 | break; |
| 4640 | |
Bill Schmidt | 5d82f09 | 2014-06-16 21:36:02 +0000 | [diff] [blame] | 4641 | // The first source operand is a TargetGlobalAddress or a TargetJumpTable. |
Eric Christopher | c180836 | 2015-11-20 20:51:31 +0000 | [diff] [blame] | 4642 | // If it must be toc-referenced according to PPCSubTarget, we generate: |
Francis Visoiu Mistrih | 5df3bbf | 2017-12-14 10:03:09 +0000 | [diff] [blame] | 4643 | // LDtocL(@sym, ADDIStocHA(%x2, @sym)) |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4644 | // Otherwise we generate: |
Francis Visoiu Mistrih | 5df3bbf | 2017-12-14 10:03:09 +0000 | [diff] [blame] | 4645 | // ADDItocL(ADDIStocHA(%x2, @sym), @sym) |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4646 | SDValue GA = N->getOperand(0); |
| 4647 | SDValue TOCbase = N->getOperand(1); |
| 4648 | SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64, |
Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 4649 | TOCbase, GA); |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4650 | |
Ulrich Weigand | c8c2ea2 | 2014-10-31 10:33:14 +0000 | [diff] [blame] | 4651 | if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) || |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4652 | CModel == CodeModel::Large) { |
| 4653 | SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA, |
| 4654 | SDValue(Tmp, 0)); |
| 4655 | transferMemOperands(N, MN); |
| 4656 | ReplaceNode(N, MN); |
| 4657 | return; |
| 4658 | } |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4659 | |
| 4660 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) { |
Eric Christopher | c180836 | 2015-11-20 20:51:31 +0000 | [diff] [blame] | 4661 | const GlobalValue *GV = G->getGlobal(); |
| 4662 | unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV); |
| 4663 | if (GVFlags & PPCII::MO_NLP_FLAG) { |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4664 | SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA, |
| 4665 | SDValue(Tmp, 0)); |
| 4666 | transferMemOperands(N, MN); |
| 4667 | ReplaceNode(N, MN); |
| 4668 | return; |
Eric Christopher | c180836 | 2015-11-20 20:51:31 +0000 | [diff] [blame] | 4669 | } |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4672 | ReplaceNode(N, CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64, |
| 4673 | SDValue(Tmp, 0), GA)); |
| 4674 | return; |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 4675 | } |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 4676 | case PPCISD::PPC32_PICGOT: |
Hal Finkel | 7c8ae53 | 2014-07-25 17:47:22 +0000 | [diff] [blame] | 4677 | // Generate a PIC-safe GOT reference. |
| 4678 | assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() && |
| 4679 | "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4"); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4680 | CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, |
| 4681 | PPCLowering->getPointerTy(CurDAG->getDataLayout()), |
| 4682 | MVT::i32); |
| 4683 | return; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 4684 | |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4685 | case PPCISD::VADD_SPLAT: { |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4686 | // This expands into one of three sequences, depending on whether |
| 4687 | // the first operand is odd or even, positive or negative. |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4688 | assert(isa<ConstantSDNode>(N->getOperand(0)) && |
| 4689 | isa<ConstantSDNode>(N->getOperand(1)) && |
| 4690 | "Invalid operand on VADD_SPLAT!"); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4691 | |
| 4692 | int Elt = N->getConstantOperandVal(0); |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4693 | int EltSize = N->getConstantOperandVal(1); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4694 | unsigned Opc1, Opc2, Opc3; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4695 | EVT VT; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4696 | |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4697 | if (EltSize == 1) { |
| 4698 | Opc1 = PPC::VSPLTISB; |
| 4699 | Opc2 = PPC::VADDUBM; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4700 | Opc3 = PPC::VSUBUBM; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4701 | VT = MVT::v16i8; |
| 4702 | } else if (EltSize == 2) { |
| 4703 | Opc1 = PPC::VSPLTISH; |
| 4704 | Opc2 = PPC::VADDUHM; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4705 | Opc3 = PPC::VSUBUHM; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4706 | VT = MVT::v8i16; |
| 4707 | } else { |
| 4708 | assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!"); |
| 4709 | Opc1 = PPC::VSPLTISW; |
| 4710 | Opc2 = PPC::VADDUWM; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4711 | Opc3 = PPC::VSUBUWM; |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4712 | VT = MVT::v4i32; |
| 4713 | } |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4714 | |
| 4715 | if ((Elt & 1) == 0) { |
| 4716 | // Elt is even, in the range [-32,-18] + [16,30]. |
| 4717 | // |
| 4718 | // Convert: VADD_SPLAT elt, size |
| 4719 | // Into: tmp = VSPLTIS[BHW] elt |
| 4720 | // VADDU[BHW]M tmp, tmp |
| 4721 | // 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] | 4722 | SDValue EltVal = getI32Imm(Elt >> 1, dl); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4723 | SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
| 4724 | SDValue TmpVal = SDValue(Tmp, 0); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4725 | ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal)); |
| 4726 | return; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4727 | } else if (Elt > 0) { |
| 4728 | // Elt is odd and positive, in the range [17,31]. |
| 4729 | // |
| 4730 | // Convert: VADD_SPLAT elt, size |
| 4731 | // Into: tmp1 = VSPLTIS[BHW] elt-16 |
| 4732 | // tmp2 = VSPLTIS[BHW] -16 |
| 4733 | // VSUBU[BHW]M tmp1, tmp2 |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4734 | SDValue EltVal = getI32Imm(Elt - 16, dl); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4735 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4736 | EltVal = getI32Imm(-16, dl); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4737 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4738 | ReplaceNode(N, CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0), |
| 4739 | SDValue(Tmp2, 0))); |
| 4740 | return; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4741 | } else { |
| 4742 | // Elt is odd and negative, in the range [-31,-17]. |
| 4743 | // |
| 4744 | // Convert: VADD_SPLAT elt, size |
| 4745 | // Into: tmp1 = VSPLTIS[BHW] elt+16 |
| 4746 | // tmp2 = VSPLTIS[BHW] -16 |
| 4747 | // VADDU[BHW]M tmp1, tmp2 |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4748 | SDValue EltVal = getI32Imm(Elt + 16, dl); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4749 | SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4750 | EltVal = getI32Imm(-16, dl); |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4751 | SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal); |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4752 | ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0), |
| 4753 | SDValue(Tmp2, 0))); |
| 4754 | return; |
Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 4755 | } |
Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 4756 | } |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 4757 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 4758 | |
Justin Bogner | dc8af06 | 2016-05-20 21:43:23 +0000 | [diff] [blame] | 4759 | SelectCode(N); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4762 | // If the target supports the cmpb instruction, do the idiom recognition here. |
| 4763 | // We don't do this as a DAG combine because we don't want to do it as nodes |
| 4764 | // are being combined (because we might miss part of the eventual idiom). We |
| 4765 | // don't want to do it during instruction selection because we want to reuse |
| 4766 | // the logic for lowering the masking operations already part of the |
| 4767 | // instruction selector. |
| 4768 | SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) { |
| 4769 | SDLoc dl(N); |
| 4770 | |
| 4771 | assert(N->getOpcode() == ISD::OR && |
| 4772 | "Only OR nodes are supported for CMPB"); |
| 4773 | |
| 4774 | SDValue Res; |
| 4775 | if (!PPCSubTarget->hasCMPB()) |
| 4776 | return Res; |
| 4777 | |
| 4778 | if (N->getValueType(0) != MVT::i32 && |
| 4779 | N->getValueType(0) != MVT::i64) |
| 4780 | return Res; |
| 4781 | |
| 4782 | EVT VT = N->getValueType(0); |
| 4783 | |
| 4784 | SDValue RHS, LHS; |
Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 4785 | bool BytesFound[8] = {false, false, false, false, false, false, false, false}; |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4786 | uint64_t Mask = 0, Alt = 0; |
| 4787 | |
| 4788 | auto IsByteSelectCC = [this](SDValue O, unsigned &b, |
| 4789 | uint64_t &Mask, uint64_t &Alt, |
| 4790 | SDValue &LHS, SDValue &RHS) { |
| 4791 | if (O.getOpcode() != ISD::SELECT_CC) |
| 4792 | return false; |
| 4793 | ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get(); |
| 4794 | |
| 4795 | if (!isa<ConstantSDNode>(O.getOperand(2)) || |
| 4796 | !isa<ConstantSDNode>(O.getOperand(3))) |
| 4797 | return false; |
| 4798 | |
| 4799 | uint64_t PM = O.getConstantOperandVal(2); |
| 4800 | uint64_t PAlt = O.getConstantOperandVal(3); |
| 4801 | for (b = 0; b < 8; ++b) { |
| 4802 | uint64_t Mask = UINT64_C(0xFF) << (8*b); |
| 4803 | if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt) |
| 4804 | break; |
| 4805 | } |
| 4806 | |
| 4807 | if (b == 8) |
| 4808 | return false; |
| 4809 | Mask |= PM; |
| 4810 | Alt |= PAlt; |
| 4811 | |
| 4812 | if (!isa<ConstantSDNode>(O.getOperand(1)) || |
| 4813 | O.getConstantOperandVal(1) != 0) { |
| 4814 | SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1); |
| 4815 | if (Op0.getOpcode() == ISD::TRUNCATE) |
| 4816 | Op0 = Op0.getOperand(0); |
| 4817 | if (Op1.getOpcode() == ISD::TRUNCATE) |
| 4818 | Op1 = Op1.getOperand(0); |
| 4819 | |
| 4820 | if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL && |
| 4821 | Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ && |
| 4822 | isa<ConstantSDNode>(Op0.getOperand(1))) { |
| 4823 | |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 4824 | unsigned Bits = Op0.getValueSizeInBits(); |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4825 | if (b != Bits/8-1) |
| 4826 | return false; |
| 4827 | if (Op0.getConstantOperandVal(1) != Bits-8) |
| 4828 | return false; |
| 4829 | |
| 4830 | LHS = Op0.getOperand(0); |
| 4831 | RHS = Op1.getOperand(0); |
| 4832 | return true; |
| 4833 | } |
| 4834 | |
| 4835 | // When we have small integers (i16 to be specific), the form present |
| 4836 | // post-legalization uses SETULT in the SELECT_CC for the |
| 4837 | // higher-order byte, depending on the fact that the |
| 4838 | // even-higher-order bytes are known to all be zero, for example: |
| 4839 | // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult |
| 4840 | // (so when the second byte is the same, because all higher-order |
| 4841 | // bits from bytes 3 and 4 are known to be zero, the result of the |
| 4842 | // xor can be at most 255) |
| 4843 | if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT && |
| 4844 | isa<ConstantSDNode>(O.getOperand(1))) { |
| 4845 | |
| 4846 | uint64_t ULim = O.getConstantOperandVal(1); |
| 4847 | if (ULim != (UINT64_C(1) << b*8)) |
| 4848 | return false; |
| 4849 | |
| 4850 | // Now we need to make sure that the upper bytes are known to be |
| 4851 | // zero. |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 4852 | unsigned Bits = Op0.getValueSizeInBits(); |
| 4853 | if (!CurDAG->MaskedValueIsZero( |
| 4854 | Op0, APInt::getHighBitsSet(Bits, Bits - (b + 1) * 8))) |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4855 | return false; |
Kyle Butt | 015f4fc | 2015-12-02 18:53:33 +0000 | [diff] [blame] | 4856 | |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4857 | LHS = Op0.getOperand(0); |
| 4858 | RHS = Op0.getOperand(1); |
| 4859 | return true; |
| 4860 | } |
| 4861 | |
| 4862 | return false; |
| 4863 | } |
| 4864 | |
| 4865 | if (CC != ISD::SETEQ) |
| 4866 | return false; |
| 4867 | |
| 4868 | SDValue Op = O.getOperand(0); |
| 4869 | if (Op.getOpcode() == ISD::AND) { |
| 4870 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
| 4871 | return false; |
| 4872 | if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b))) |
| 4873 | return false; |
| 4874 | |
| 4875 | SDValue XOR = Op.getOperand(0); |
| 4876 | if (XOR.getOpcode() == ISD::TRUNCATE) |
| 4877 | XOR = XOR.getOperand(0); |
| 4878 | if (XOR.getOpcode() != ISD::XOR) |
| 4879 | return false; |
| 4880 | |
| 4881 | LHS = XOR.getOperand(0); |
| 4882 | RHS = XOR.getOperand(1); |
| 4883 | return true; |
| 4884 | } else if (Op.getOpcode() == ISD::SRL) { |
| 4885 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
| 4886 | return false; |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 4887 | unsigned Bits = Op.getValueSizeInBits(); |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4888 | if (b != Bits/8-1) |
| 4889 | return false; |
| 4890 | if (Op.getConstantOperandVal(1) != Bits-8) |
| 4891 | return false; |
| 4892 | |
| 4893 | SDValue XOR = Op.getOperand(0); |
| 4894 | if (XOR.getOpcode() == ISD::TRUNCATE) |
| 4895 | XOR = XOR.getOperand(0); |
| 4896 | if (XOR.getOpcode() != ISD::XOR) |
| 4897 | return false; |
| 4898 | |
| 4899 | LHS = XOR.getOperand(0); |
| 4900 | RHS = XOR.getOperand(1); |
| 4901 | return true; |
| 4902 | } |
| 4903 | |
| 4904 | return false; |
| 4905 | }; |
| 4906 | |
| 4907 | SmallVector<SDValue, 8> Queue(1, SDValue(N, 0)); |
| 4908 | while (!Queue.empty()) { |
| 4909 | SDValue V = Queue.pop_back_val(); |
| 4910 | |
| 4911 | for (const SDValue &O : V.getNode()->ops()) { |
| 4912 | unsigned b; |
| 4913 | uint64_t M = 0, A = 0; |
| 4914 | SDValue OLHS, ORHS; |
| 4915 | if (O.getOpcode() == ISD::OR) { |
| 4916 | Queue.push_back(O); |
| 4917 | } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) { |
| 4918 | if (!LHS) { |
| 4919 | LHS = OLHS; |
| 4920 | RHS = ORHS; |
| 4921 | BytesFound[b] = true; |
| 4922 | Mask |= M; |
| 4923 | Alt |= A; |
| 4924 | } else if ((LHS == ORHS && RHS == OLHS) || |
| 4925 | (RHS == ORHS && LHS == OLHS)) { |
| 4926 | BytesFound[b] = true; |
| 4927 | Mask |= M; |
| 4928 | Alt |= A; |
| 4929 | } else { |
| 4930 | return Res; |
| 4931 | } |
| 4932 | } else { |
| 4933 | return Res; |
| 4934 | } |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | unsigned LastB = 0, BCnt = 0; |
| 4939 | for (unsigned i = 0; i < 8; ++i) |
| 4940 | if (BytesFound[LastB]) { |
| 4941 | ++BCnt; |
| 4942 | LastB = i; |
| 4943 | } |
| 4944 | |
| 4945 | if (!LastB || BCnt < 2) |
| 4946 | return Res; |
| 4947 | |
| 4948 | // Because we'll be zero-extending the output anyway if don't have a specific |
| 4949 | // value for each input byte (via the Mask), we can 'anyext' the inputs. |
| 4950 | if (LHS.getValueType() != VT) { |
| 4951 | LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT); |
| 4952 | RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT); |
| 4953 | } |
| 4954 | |
| 4955 | Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS); |
| 4956 | |
| 4957 | bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1); |
| 4958 | if (NonTrivialMask && !Alt) { |
| 4959 | // Res = Mask & CMPB |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4960 | Res = CurDAG->getNode(ISD::AND, dl, VT, Res, |
| 4961 | CurDAG->getConstant(Mask, dl, VT)); |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4962 | } else if (Alt) { |
| 4963 | // Res = (CMPB & Mask) | (~CMPB & Alt) |
| 4964 | // Which, as suggested here: |
| 4965 | // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge |
| 4966 | // can be written as: |
| 4967 | // Res = Alt ^ ((Alt ^ Mask) & CMPB) |
| 4968 | // useful because the (Alt ^ Mask) can be pre-computed. |
| 4969 | Res = CurDAG->getNode(ISD::AND, dl, VT, Res, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4970 | CurDAG->getConstant(Mask ^ Alt, dl, VT)); |
| 4971 | Res = CurDAG->getNode(ISD::XOR, dl, VT, Res, |
| 4972 | CurDAG->getConstant(Alt, dl, VT)); |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | return Res; |
| 4976 | } |
| 4977 | |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 4978 | // When CR bit registers are enabled, an extension of an i1 variable to a i32 |
| 4979 | // or i64 value is lowered in terms of a SELECT_I[48] operation, and thus |
| 4980 | // involves constant materialization of a 0 or a 1 or both. If the result of |
| 4981 | // the extension is then operated upon by some operator that can be constant |
| 4982 | // folded with a constant 0 or 1, and that constant can be materialized using |
| 4983 | // only one instruction (like a zero or one), then we should fold in those |
| 4984 | // operations with the select. |
| 4985 | void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) { |
| 4986 | if (!PPCSubTarget->useCRBits()) |
| 4987 | return; |
| 4988 | |
| 4989 | if (N->getOpcode() != ISD::ZERO_EXTEND && |
| 4990 | N->getOpcode() != ISD::SIGN_EXTEND && |
| 4991 | N->getOpcode() != ISD::ANY_EXTEND) |
| 4992 | return; |
| 4993 | |
| 4994 | if (N->getOperand(0).getValueType() != MVT::i1) |
| 4995 | return; |
| 4996 | |
| 4997 | if (!N->hasOneUse()) |
| 4998 | return; |
| 4999 | |
| 5000 | SDLoc dl(N); |
| 5001 | EVT VT = N->getValueType(0); |
| 5002 | SDValue Cond = N->getOperand(0); |
| 5003 | SDValue ConstTrue = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5004 | CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT); |
| 5005 | SDValue ConstFalse = CurDAG->getConstant(0, dl, VT); |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 5006 | |
| 5007 | do { |
| 5008 | SDNode *User = *N->use_begin(); |
| 5009 | if (User->getNumOperands() != 2) |
| 5010 | break; |
| 5011 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5012 | auto TryFold = [this, N, User, dl](SDValue Val) { |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 5013 | SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1); |
| 5014 | SDValue O0 = UserO0.getNode() == N ? Val : UserO0; |
| 5015 | SDValue O1 = UserO1.getNode() == N ? Val : UserO1; |
| 5016 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5017 | return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl, |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 5018 | User->getValueType(0), |
| 5019 | O0.getNode(), O1.getNode()); |
| 5020 | }; |
| 5021 | |
Nemanja Ivanovic | 845a796 | 2017-07-05 04:51:29 +0000 | [diff] [blame] | 5022 | // FIXME: When the semantics of the interaction between select and undef |
| 5023 | // are clearly defined, it may turn out to be unnecessary to break here. |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 5024 | SDValue TrueRes = TryFold(ConstTrue); |
Nemanja Ivanovic | 845a796 | 2017-07-05 04:51:29 +0000 | [diff] [blame] | 5025 | if (!TrueRes || TrueRes.isUndef()) |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 5026 | break; |
| 5027 | SDValue FalseRes = TryFold(ConstFalse); |
Nemanja Ivanovic | 845a796 | 2017-07-05 04:51:29 +0000 | [diff] [blame] | 5028 | if (!FalseRes || FalseRes.isUndef()) |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 5029 | break; |
| 5030 | |
| 5031 | // For us to materialize these using one instruction, we must be able to |
| 5032 | // represent them as signed 16-bit integers. |
| 5033 | uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(), |
| 5034 | False = cast<ConstantSDNode>(FalseRes)->getZExtValue(); |
| 5035 | if (!isInt<16>(True) || !isInt<16>(False)) |
| 5036 | break; |
| 5037 | |
| 5038 | // We can replace User with a new SELECT node, and try again to see if we |
| 5039 | // can fold the select with its user. |
| 5040 | Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes); |
| 5041 | N = User; |
| 5042 | ConstTrue = TrueRes; |
| 5043 | ConstFalse = FalseRes; |
| 5044 | } while (N->hasOneUse()); |
| 5045 | } |
| 5046 | |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 5047 | void PPCDAGToDAGISel::PreprocessISelDAG() { |
| 5048 | SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); |
| 5049 | ++Position; |
| 5050 | |
| 5051 | bool MadeChange = false; |
| 5052 | while (Position != CurDAG->allnodes_begin()) { |
Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 5053 | SDNode *N = &*--Position; |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 5054 | if (N->use_empty()) |
| 5055 | continue; |
| 5056 | |
| 5057 | SDValue Res; |
| 5058 | switch (N->getOpcode()) { |
| 5059 | default: break; |
| 5060 | case ISD::OR: |
| 5061 | Res = combineToCMPB(N); |
| 5062 | break; |
| 5063 | } |
| 5064 | |
Hal Finkel | 200d2ad | 2015-01-05 21:10:24 +0000 | [diff] [blame] | 5065 | if (!Res) |
| 5066 | foldBoolExts(Res, N); |
| 5067 | |
Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 5068 | if (Res) { |
| 5069 | DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: "); |
| 5070 | DEBUG(N->dump(CurDAG)); |
| 5071 | DEBUG(dbgs() << "\nNew: "); |
| 5072 | DEBUG(Res.getNode()->dump(CurDAG)); |
| 5073 | DEBUG(dbgs() << "\n"); |
| 5074 | |
| 5075 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res); |
| 5076 | MadeChange = true; |
| 5077 | } |
| 5078 | } |
| 5079 | |
| 5080 | if (MadeChange) |
| 5081 | CurDAG->RemoveDeadNodes(); |
| 5082 | } |
| 5083 | |
Hal Finkel | 860fa90 | 2014-01-02 22:09:39 +0000 | [diff] [blame] | 5084 | /// PostprocessISelDAG - Perform some late peephole optimizations |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5085 | /// on the DAG representation. |
| 5086 | void PPCDAGToDAGISel::PostprocessISelDAG() { |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5087 | // Skip peepholes at -O0. |
| 5088 | if (TM.getOptLevel() == CodeGenOpt::None) |
| 5089 | return; |
| 5090 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5091 | PeepholePPC64(); |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 5092 | PeepholeCROps(); |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 5093 | PeepholePPC64ZExt(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5096 | // Check if all users of this node will become isel where the second operand |
| 5097 | // is the constant zero. If this is so, and if we can negate the condition, |
| 5098 | // then we can flip the true and false operands. This will allow the zero to |
| 5099 | // be folded with the isel so that we don't need to materialize a register |
| 5100 | // containing zero. |
| 5101 | bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5102 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 5103 | UI != UE; ++UI) { |
| 5104 | SDNode *User = *UI; |
| 5105 | if (!User->isMachineOpcode()) |
| 5106 | return false; |
| 5107 | if (User->getMachineOpcode() != PPC::SELECT_I4 && |
| 5108 | User->getMachineOpcode() != PPC::SELECT_I8) |
| 5109 | return false; |
| 5110 | |
| 5111 | SDNode *Op2 = User->getOperand(2).getNode(); |
| 5112 | if (!Op2->isMachineOpcode()) |
| 5113 | return false; |
| 5114 | |
| 5115 | if (Op2->getMachineOpcode() != PPC::LI && |
| 5116 | Op2->getMachineOpcode() != PPC::LI8) |
| 5117 | return false; |
| 5118 | |
| 5119 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0)); |
| 5120 | if (!C) |
| 5121 | return false; |
| 5122 | |
| 5123 | if (!C->isNullValue()) |
| 5124 | return false; |
| 5125 | } |
| 5126 | |
| 5127 | return true; |
| 5128 | } |
| 5129 | |
| 5130 | void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) { |
| 5131 | SmallVector<SDNode *, 4> ToReplace; |
| 5132 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 5133 | UI != UE; ++UI) { |
| 5134 | SDNode *User = *UI; |
| 5135 | assert((User->getMachineOpcode() == PPC::SELECT_I4 || |
| 5136 | User->getMachineOpcode() == PPC::SELECT_I8) && |
| 5137 | "Must have all select users"); |
| 5138 | ToReplace.push_back(User); |
| 5139 | } |
| 5140 | |
| 5141 | for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(), |
| 5142 | UE = ToReplace.end(); UI != UE; ++UI) { |
| 5143 | SDNode *User = *UI; |
| 5144 | SDNode *ResNode = |
| 5145 | CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User), |
| 5146 | User->getValueType(0), User->getOperand(0), |
| 5147 | User->getOperand(2), |
| 5148 | User->getOperand(1)); |
| 5149 | |
| 5150 | DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); |
| 5151 | DEBUG(User->dump(CurDAG)); |
| 5152 | DEBUG(dbgs() << "\nNew: "); |
| 5153 | DEBUG(ResNode->dump(CurDAG)); |
| 5154 | DEBUG(dbgs() << "\n"); |
| 5155 | |
| 5156 | ReplaceUses(User, ResNode); |
| 5157 | } |
| 5158 | } |
| 5159 | |
Eric Christopher | 02e1804 | 2014-05-14 00:31:15 +0000 | [diff] [blame] | 5160 | void PPCDAGToDAGISel::PeepholeCROps() { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5161 | bool IsModified; |
| 5162 | do { |
| 5163 | IsModified = false; |
Pete Cooper | 65c6940 | 2015-07-14 22:10:54 +0000 | [diff] [blame] | 5164 | for (SDNode &Node : CurDAG->allnodes()) { |
| 5165 | MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5166 | if (!MachineNode || MachineNode->use_empty()) |
| 5167 | continue; |
| 5168 | SDNode *ResNode = MachineNode; |
| 5169 | |
| 5170 | bool Op1Set = false, Op1Unset = false, |
| 5171 | Op1Not = false, |
| 5172 | Op2Set = false, Op2Unset = false, |
| 5173 | Op2Not = false; |
| 5174 | |
| 5175 | unsigned Opcode = MachineNode->getMachineOpcode(); |
| 5176 | switch (Opcode) { |
| 5177 | default: break; |
| 5178 | case PPC::CRAND: |
| 5179 | case PPC::CRNAND: |
| 5180 | case PPC::CROR: |
| 5181 | case PPC::CRXOR: |
| 5182 | case PPC::CRNOR: |
| 5183 | case PPC::CREQV: |
| 5184 | case PPC::CRANDC: |
| 5185 | case PPC::CRORC: { |
| 5186 | SDValue Op = MachineNode->getOperand(1); |
| 5187 | if (Op.isMachineOpcode()) { |
| 5188 | if (Op.getMachineOpcode() == PPC::CRSET) |
| 5189 | Op2Set = true; |
| 5190 | else if (Op.getMachineOpcode() == PPC::CRUNSET) |
| 5191 | Op2Unset = true; |
| 5192 | else if (Op.getMachineOpcode() == PPC::CRNOR && |
| 5193 | Op.getOperand(0) == Op.getOperand(1)) |
| 5194 | Op2Not = true; |
| 5195 | } |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 5196 | LLVM_FALLTHROUGH; |
| 5197 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5198 | case PPC::BC: |
| 5199 | case PPC::BCn: |
| 5200 | case PPC::SELECT_I4: |
| 5201 | case PPC::SELECT_I8: |
| 5202 | case PPC::SELECT_F4: |
| 5203 | case PPC::SELECT_F8: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5204 | case PPC::SELECT_QFRC: |
| 5205 | case PPC::SELECT_QSRC: |
| 5206 | case PPC::SELECT_QBRC: |
Bill Schmidt | 61e6523 | 2014-10-22 13:13:40 +0000 | [diff] [blame] | 5207 | case PPC::SELECT_VRRC: |
Bill Schmidt | 9c54bbd | 2014-10-22 16:58:20 +0000 | [diff] [blame] | 5208 | case PPC::SELECT_VSFRC: |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 5209 | case PPC::SELECT_VSSRC: |
Bill Schmidt | 61e6523 | 2014-10-22 13:13:40 +0000 | [diff] [blame] | 5210 | case PPC::SELECT_VSRC: { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5211 | SDValue Op = MachineNode->getOperand(0); |
| 5212 | if (Op.isMachineOpcode()) { |
| 5213 | if (Op.getMachineOpcode() == PPC::CRSET) |
| 5214 | Op1Set = true; |
| 5215 | else if (Op.getMachineOpcode() == PPC::CRUNSET) |
| 5216 | Op1Unset = true; |
| 5217 | else if (Op.getMachineOpcode() == PPC::CRNOR && |
| 5218 | Op.getOperand(0) == Op.getOperand(1)) |
| 5219 | Op1Not = true; |
| 5220 | } |
| 5221 | } |
| 5222 | break; |
| 5223 | } |
| 5224 | |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5225 | bool SelectSwap = false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5226 | switch (Opcode) { |
| 5227 | default: break; |
| 5228 | case PPC::CRAND: |
| 5229 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 5230 | // x & x = x |
| 5231 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5232 | else if (Op1Set) |
| 5233 | // 1 & y = y |
| 5234 | ResNode = MachineNode->getOperand(1).getNode(); |
| 5235 | else if (Op2Set) |
| 5236 | // x & 1 = x |
| 5237 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5238 | else if (Op1Unset || Op2Unset) |
| 5239 | // x & 0 = 0 & y = 0 |
| 5240 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 5241 | MVT::i1); |
| 5242 | else if (Op1Not) |
| 5243 | // ~x & y = andc(y, x) |
| 5244 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 5245 | MVT::i1, MachineNode->getOperand(1), |
| 5246 | MachineNode->getOperand(0). |
| 5247 | getOperand(0)); |
| 5248 | else if (Op2Not) |
| 5249 | // x & ~y = andc(x, y) |
| 5250 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 5251 | MVT::i1, MachineNode->getOperand(0), |
| 5252 | MachineNode->getOperand(1). |
| 5253 | getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5254 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5255 | ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), |
| 5256 | MVT::i1, MachineNode->getOperand(0), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5257 | MachineNode->getOperand(1)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5258 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5259 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5260 | break; |
| 5261 | case PPC::CRNAND: |
| 5262 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 5263 | // nand(x, x) -> nor(x, x) |
| 5264 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5265 | MVT::i1, MachineNode->getOperand(0), |
| 5266 | MachineNode->getOperand(0)); |
| 5267 | else if (Op1Set) |
| 5268 | // nand(1, y) -> nor(y, y) |
| 5269 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5270 | MVT::i1, MachineNode->getOperand(1), |
| 5271 | MachineNode->getOperand(1)); |
| 5272 | else if (Op2Set) |
| 5273 | // nand(x, 1) -> nor(x, x) |
| 5274 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5275 | MVT::i1, MachineNode->getOperand(0), |
| 5276 | MachineNode->getOperand(0)); |
| 5277 | else if (Op1Unset || Op2Unset) |
| 5278 | // nand(x, 0) = nand(0, y) = 1 |
| 5279 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 5280 | MVT::i1); |
| 5281 | else if (Op1Not) |
| 5282 | // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y) |
| 5283 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 5284 | MVT::i1, MachineNode->getOperand(0). |
| 5285 | getOperand(0), |
| 5286 | MachineNode->getOperand(1)); |
| 5287 | else if (Op2Not) |
| 5288 | // nand(x, ~y) = ~x | y = orc(y, x) |
| 5289 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 5290 | MVT::i1, MachineNode->getOperand(1). |
| 5291 | getOperand(0), |
| 5292 | MachineNode->getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5293 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5294 | ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), |
| 5295 | MVT::i1, MachineNode->getOperand(0), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5296 | MachineNode->getOperand(1)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5297 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5298 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5299 | break; |
| 5300 | case PPC::CROR: |
| 5301 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 5302 | // x | x = x |
| 5303 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5304 | else if (Op1Set || Op2Set) |
| 5305 | // x | 1 = 1 | y = 1 |
| 5306 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 5307 | MVT::i1); |
| 5308 | else if (Op1Unset) |
| 5309 | // 0 | y = y |
| 5310 | ResNode = MachineNode->getOperand(1).getNode(); |
| 5311 | else if (Op2Unset) |
| 5312 | // x | 0 = x |
| 5313 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5314 | else if (Op1Not) |
| 5315 | // ~x | y = orc(y, x) |
| 5316 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 5317 | MVT::i1, MachineNode->getOperand(1), |
| 5318 | MachineNode->getOperand(0). |
| 5319 | getOperand(0)); |
| 5320 | else if (Op2Not) |
| 5321 | // x | ~y = orc(x, y) |
| 5322 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 5323 | MVT::i1, MachineNode->getOperand(0), |
| 5324 | MachineNode->getOperand(1). |
| 5325 | getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5326 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5327 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5328 | MVT::i1, MachineNode->getOperand(0), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5329 | MachineNode->getOperand(1)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5330 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5331 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5332 | break; |
| 5333 | case PPC::CRXOR: |
| 5334 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 5335 | // xor(x, x) = 0 |
| 5336 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 5337 | MVT::i1); |
| 5338 | else if (Op1Set) |
| 5339 | // xor(1, y) -> nor(y, y) |
| 5340 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5341 | MVT::i1, MachineNode->getOperand(1), |
| 5342 | MachineNode->getOperand(1)); |
| 5343 | else if (Op2Set) |
| 5344 | // xor(x, 1) -> nor(x, x) |
| 5345 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5346 | MVT::i1, MachineNode->getOperand(0), |
| 5347 | MachineNode->getOperand(0)); |
| 5348 | else if (Op1Unset) |
| 5349 | // xor(0, y) = y |
| 5350 | ResNode = MachineNode->getOperand(1).getNode(); |
| 5351 | else if (Op2Unset) |
| 5352 | // xor(x, 0) = x |
| 5353 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5354 | else if (Op1Not) |
| 5355 | // xor(~x, y) = eqv(x, y) |
| 5356 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
| 5357 | MVT::i1, MachineNode->getOperand(0). |
| 5358 | getOperand(0), |
| 5359 | MachineNode->getOperand(1)); |
| 5360 | else if (Op2Not) |
| 5361 | // xor(x, ~y) = eqv(x, y) |
| 5362 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
| 5363 | MVT::i1, MachineNode->getOperand(0), |
| 5364 | MachineNode->getOperand(1). |
| 5365 | getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5366 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5367 | ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode), |
| 5368 | MVT::i1, MachineNode->getOperand(0), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5369 | MachineNode->getOperand(1)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5370 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5371 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5372 | break; |
| 5373 | case PPC::CRNOR: |
| 5374 | if (Op1Set || Op2Set) |
| 5375 | // nor(1, y) -> 0 |
| 5376 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 5377 | MVT::i1); |
| 5378 | else if (Op1Unset) |
| 5379 | // nor(0, y) = ~y -> nor(y, y) |
| 5380 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5381 | MVT::i1, MachineNode->getOperand(1), |
| 5382 | MachineNode->getOperand(1)); |
| 5383 | else if (Op2Unset) |
| 5384 | // nor(x, 0) = ~x |
| 5385 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5386 | MVT::i1, MachineNode->getOperand(0), |
| 5387 | MachineNode->getOperand(0)); |
| 5388 | else if (Op1Not) |
| 5389 | // nor(~x, y) = andc(x, y) |
| 5390 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 5391 | MVT::i1, MachineNode->getOperand(0). |
| 5392 | getOperand(0), |
| 5393 | MachineNode->getOperand(1)); |
| 5394 | else if (Op2Not) |
| 5395 | // nor(x, ~y) = andc(y, x) |
| 5396 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 5397 | MVT::i1, MachineNode->getOperand(1). |
| 5398 | getOperand(0), |
| 5399 | MachineNode->getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5400 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5401 | ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), |
| 5402 | MVT::i1, MachineNode->getOperand(0), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5403 | MachineNode->getOperand(1)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5404 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5405 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5406 | break; |
| 5407 | case PPC::CREQV: |
| 5408 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 5409 | // eqv(x, x) = 1 |
| 5410 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 5411 | MVT::i1); |
| 5412 | else if (Op1Set) |
| 5413 | // eqv(1, y) = y |
| 5414 | ResNode = MachineNode->getOperand(1).getNode(); |
| 5415 | else if (Op2Set) |
| 5416 | // eqv(x, 1) = x |
| 5417 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5418 | else if (Op1Unset) |
| 5419 | // eqv(0, y) = ~y -> nor(y, y) |
| 5420 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5421 | MVT::i1, MachineNode->getOperand(1), |
| 5422 | MachineNode->getOperand(1)); |
| 5423 | else if (Op2Unset) |
| 5424 | // eqv(x, 0) = ~x |
| 5425 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5426 | MVT::i1, MachineNode->getOperand(0), |
| 5427 | MachineNode->getOperand(0)); |
| 5428 | else if (Op1Not) |
| 5429 | // eqv(~x, y) = xor(x, y) |
| 5430 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
| 5431 | MVT::i1, MachineNode->getOperand(0). |
| 5432 | getOperand(0), |
| 5433 | MachineNode->getOperand(1)); |
| 5434 | else if (Op2Not) |
| 5435 | // eqv(x, ~y) = xor(x, y) |
| 5436 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
| 5437 | MVT::i1, MachineNode->getOperand(0), |
| 5438 | MachineNode->getOperand(1). |
| 5439 | getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5440 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5441 | ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode), |
| 5442 | MVT::i1, MachineNode->getOperand(0), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5443 | MachineNode->getOperand(1)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5444 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5445 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5446 | break; |
| 5447 | case PPC::CRANDC: |
| 5448 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 5449 | // andc(x, x) = 0 |
| 5450 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 5451 | MVT::i1); |
| 5452 | else if (Op1Set) |
| 5453 | // andc(1, y) = ~y |
| 5454 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5455 | MVT::i1, MachineNode->getOperand(1), |
| 5456 | MachineNode->getOperand(1)); |
| 5457 | else if (Op1Unset || Op2Set) |
| 5458 | // andc(0, y) = andc(x, 1) = 0 |
| 5459 | ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode), |
| 5460 | MVT::i1); |
| 5461 | else if (Op2Unset) |
| 5462 | // andc(x, 0) = x |
| 5463 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5464 | else if (Op1Not) |
| 5465 | // andc(~x, y) = ~(x | y) = nor(x, y) |
| 5466 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5467 | MVT::i1, MachineNode->getOperand(0). |
| 5468 | getOperand(0), |
| 5469 | MachineNode->getOperand(1)); |
| 5470 | else if (Op2Not) |
| 5471 | // andc(x, ~y) = x & y |
| 5472 | ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode), |
| 5473 | MVT::i1, MachineNode->getOperand(0), |
| 5474 | MachineNode->getOperand(1). |
| 5475 | getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5476 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5477 | ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode), |
| 5478 | MVT::i1, MachineNode->getOperand(1), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5479 | MachineNode->getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5480 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5481 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5482 | break; |
| 5483 | case PPC::CRORC: |
| 5484 | if (MachineNode->getOperand(0) == MachineNode->getOperand(1)) |
| 5485 | // orc(x, x) = 1 |
| 5486 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 5487 | MVT::i1); |
| 5488 | else if (Op1Set || Op2Unset) |
| 5489 | // orc(1, y) = orc(x, 0) = 1 |
| 5490 | ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode), |
| 5491 | MVT::i1); |
| 5492 | else if (Op2Set) |
| 5493 | // orc(x, 1) = x |
| 5494 | ResNode = MachineNode->getOperand(0).getNode(); |
| 5495 | else if (Op1Unset) |
| 5496 | // orc(0, y) = ~y |
| 5497 | ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode), |
| 5498 | MVT::i1, MachineNode->getOperand(1), |
| 5499 | MachineNode->getOperand(1)); |
| 5500 | else if (Op1Not) |
| 5501 | // orc(~x, y) = ~(x & y) = nand(x, y) |
| 5502 | ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode), |
| 5503 | MVT::i1, MachineNode->getOperand(0). |
| 5504 | getOperand(0), |
| 5505 | MachineNode->getOperand(1)); |
| 5506 | else if (Op2Not) |
| 5507 | // orc(x, ~y) = x | y |
| 5508 | ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode), |
| 5509 | MVT::i1, MachineNode->getOperand(0), |
| 5510 | MachineNode->getOperand(1). |
| 5511 | getOperand(0)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5512 | else if (AllUsersSelectZero(MachineNode)) { |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5513 | ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode), |
| 5514 | MVT::i1, MachineNode->getOperand(1), |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5515 | MachineNode->getOperand(0)); |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5516 | SelectSwap = true; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 5517 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5518 | break; |
| 5519 | case PPC::SELECT_I4: |
| 5520 | case PPC::SELECT_I8: |
| 5521 | case PPC::SELECT_F4: |
| 5522 | case PPC::SELECT_F8: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5523 | case PPC::SELECT_QFRC: |
| 5524 | case PPC::SELECT_QSRC: |
| 5525 | case PPC::SELECT_QBRC: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5526 | case PPC::SELECT_VRRC: |
Bill Schmidt | 9c54bbd | 2014-10-22 16:58:20 +0000 | [diff] [blame] | 5527 | case PPC::SELECT_VSFRC: |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 5528 | case PPC::SELECT_VSSRC: |
Bill Schmidt | 61e6523 | 2014-10-22 13:13:40 +0000 | [diff] [blame] | 5529 | case PPC::SELECT_VSRC: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5530 | if (Op1Set) |
| 5531 | ResNode = MachineNode->getOperand(1).getNode(); |
| 5532 | else if (Op1Unset) |
| 5533 | ResNode = MachineNode->getOperand(2).getNode(); |
| 5534 | else if (Op1Not) |
| 5535 | ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(), |
| 5536 | SDLoc(MachineNode), |
| 5537 | MachineNode->getValueType(0), |
| 5538 | MachineNode->getOperand(0). |
| 5539 | getOperand(0), |
| 5540 | MachineNode->getOperand(2), |
| 5541 | MachineNode->getOperand(1)); |
| 5542 | break; |
| 5543 | case PPC::BC: |
| 5544 | case PPC::BCn: |
| 5545 | if (Op1Not) |
| 5546 | ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn : |
| 5547 | PPC::BC, |
| 5548 | SDLoc(MachineNode), |
| 5549 | MVT::Other, |
| 5550 | MachineNode->getOperand(0). |
| 5551 | getOperand(0), |
| 5552 | MachineNode->getOperand(1), |
| 5553 | MachineNode->getOperand(2)); |
| 5554 | // FIXME: Handle Op1Set, Op1Unset here too. |
| 5555 | break; |
| 5556 | } |
| 5557 | |
Hal Finkel | b998915 | 2014-02-28 06:11:16 +0000 | [diff] [blame] | 5558 | // If we're inverting this node because it is used only by selects that |
| 5559 | // we'd like to swap, then swap the selects before the node replacement. |
| 5560 | if (SelectSwap) |
| 5561 | SwapAllSelectUsers(MachineNode); |
| 5562 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5563 | if (ResNode != MachineNode) { |
| 5564 | DEBUG(dbgs() << "CR Peephole replacing:\nOld: "); |
| 5565 | DEBUG(MachineNode->dump(CurDAG)); |
| 5566 | DEBUG(dbgs() << "\nNew: "); |
| 5567 | DEBUG(ResNode->dump(CurDAG)); |
| 5568 | DEBUG(dbgs() << "\n"); |
| 5569 | |
| 5570 | ReplaceUses(MachineNode, ResNode); |
| 5571 | IsModified = true; |
| 5572 | } |
| 5573 | } |
| 5574 | if (IsModified) |
| 5575 | CurDAG->RemoveDeadNodes(); |
| 5576 | } while (IsModified); |
| 5577 | } |
| 5578 | |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 5579 | // Gather the set of 32-bit operations that are known to have their |
| 5580 | // higher-order 32 bits zero, where ToPromote contains all such operations. |
| 5581 | static bool PeepholePPC64ZExtGather(SDValue Op32, |
| 5582 | SmallPtrSetImpl<SDNode *> &ToPromote) { |
| 5583 | if (!Op32.isMachineOpcode()) |
| 5584 | return false; |
| 5585 | |
| 5586 | // First, check for the "frontier" instructions (those that will clear the |
| 5587 | // higher-order 32 bits. |
| 5588 | |
| 5589 | // For RLWINM and RLWNM, we need to make sure that the mask does not wrap |
| 5590 | // around. If it does not, then these instructions will clear the |
| 5591 | // higher-order bits. |
| 5592 | if ((Op32.getMachineOpcode() == PPC::RLWINM || |
| 5593 | Op32.getMachineOpcode() == PPC::RLWNM) && |
| 5594 | Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) { |
| 5595 | ToPromote.insert(Op32.getNode()); |
| 5596 | return true; |
| 5597 | } |
| 5598 | |
| 5599 | // SLW and SRW always clear the higher-order bits. |
| 5600 | if (Op32.getMachineOpcode() == PPC::SLW || |
| 5601 | Op32.getMachineOpcode() == PPC::SRW) { |
| 5602 | ToPromote.insert(Op32.getNode()); |
| 5603 | return true; |
| 5604 | } |
| 5605 | |
| 5606 | // For LI and LIS, we need the immediate to be positive (so that it is not |
| 5607 | // sign extended). |
| 5608 | if (Op32.getMachineOpcode() == PPC::LI || |
| 5609 | Op32.getMachineOpcode() == PPC::LIS) { |
| 5610 | if (!isUInt<15>(Op32.getConstantOperandVal(0))) |
| 5611 | return false; |
| 5612 | |
| 5613 | ToPromote.insert(Op32.getNode()); |
| 5614 | return true; |
| 5615 | } |
| 5616 | |
Hal Finkel | 4e2c782 | 2015-01-05 18:09:06 +0000 | [diff] [blame] | 5617 | // LHBRX and LWBRX always clear the higher-order bits. |
| 5618 | if (Op32.getMachineOpcode() == PPC::LHBRX || |
| 5619 | Op32.getMachineOpcode() == PPC::LWBRX) { |
| 5620 | ToPromote.insert(Op32.getNode()); |
| 5621 | return true; |
| 5622 | } |
| 5623 | |
Nemanja Ivanovic | 32b5fed | 2016-10-27 05:17:58 +0000 | [diff] [blame] | 5624 | // CNT[LT]ZW always produce a 64-bit value in [0,32], and so is zero extended. |
| 5625 | if (Op32.getMachineOpcode() == PPC::CNTLZW || |
| 5626 | Op32.getMachineOpcode() == PPC::CNTTZW) { |
Hal Finkel | 49557f1 | 2015-01-05 18:52:29 +0000 | [diff] [blame] | 5627 | ToPromote.insert(Op32.getNode()); |
| 5628 | return true; |
| 5629 | } |
| 5630 | |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 5631 | // Next, check for those instructions we can look through. |
| 5632 | |
| 5633 | // Assuming the mask does not wrap around, then the higher-order bits are |
| 5634 | // taken directly from the first operand. |
| 5635 | if (Op32.getMachineOpcode() == PPC::RLWIMI && |
| 5636 | Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) { |
| 5637 | SmallPtrSet<SDNode *, 16> ToPromote1; |
| 5638 | if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1)) |
| 5639 | return false; |
| 5640 | |
| 5641 | ToPromote.insert(Op32.getNode()); |
| 5642 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
| 5643 | return true; |
| 5644 | } |
| 5645 | |
| 5646 | // For OR, the higher-order bits are zero if that is true for both operands. |
| 5647 | // For SELECT_I4, the same is true (but the relevant operand numbers are |
| 5648 | // shifted by 1). |
| 5649 | if (Op32.getMachineOpcode() == PPC::OR || |
| 5650 | Op32.getMachineOpcode() == PPC::SELECT_I4) { |
| 5651 | unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0; |
| 5652 | SmallPtrSet<SDNode *, 16> ToPromote1; |
| 5653 | if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1)) |
| 5654 | return false; |
| 5655 | if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1)) |
| 5656 | return false; |
| 5657 | |
| 5658 | ToPromote.insert(Op32.getNode()); |
| 5659 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
| 5660 | return true; |
| 5661 | } |
| 5662 | |
| 5663 | // For ORI and ORIS, we need the higher-order bits of the first operand to be |
| 5664 | // zero, and also for the constant to be positive (so that it is not sign |
| 5665 | // extended). |
| 5666 | if (Op32.getMachineOpcode() == PPC::ORI || |
| 5667 | Op32.getMachineOpcode() == PPC::ORIS) { |
| 5668 | SmallPtrSet<SDNode *, 16> ToPromote1; |
| 5669 | if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1)) |
| 5670 | return false; |
| 5671 | if (!isUInt<15>(Op32.getConstantOperandVal(1))) |
| 5672 | return false; |
| 5673 | |
| 5674 | ToPromote.insert(Op32.getNode()); |
| 5675 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
| 5676 | return true; |
| 5677 | } |
| 5678 | |
| 5679 | // The higher-order bits of AND are zero if that is true for at least one of |
| 5680 | // the operands. |
| 5681 | if (Op32.getMachineOpcode() == PPC::AND) { |
| 5682 | SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2; |
| 5683 | bool Op0OK = |
| 5684 | PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1); |
| 5685 | bool Op1OK = |
| 5686 | PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2); |
| 5687 | if (!Op0OK && !Op1OK) |
| 5688 | return false; |
| 5689 | |
| 5690 | ToPromote.insert(Op32.getNode()); |
| 5691 | |
| 5692 | if (Op0OK) |
| 5693 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
| 5694 | |
| 5695 | if (Op1OK) |
| 5696 | ToPromote.insert(ToPromote2.begin(), ToPromote2.end()); |
| 5697 | |
| 5698 | return true; |
| 5699 | } |
| 5700 | |
| 5701 | // For ANDI and ANDIS, the higher-order bits are zero if either that is true |
| 5702 | // of the first operand, or if the second operand is positive (so that it is |
| 5703 | // not sign extended). |
| 5704 | if (Op32.getMachineOpcode() == PPC::ANDIo || |
| 5705 | Op32.getMachineOpcode() == PPC::ANDISo) { |
| 5706 | SmallPtrSet<SDNode *, 16> ToPromote1; |
| 5707 | bool Op0OK = |
| 5708 | PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1); |
| 5709 | bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1)); |
| 5710 | if (!Op0OK && !Op1OK) |
| 5711 | return false; |
| 5712 | |
| 5713 | ToPromote.insert(Op32.getNode()); |
| 5714 | |
| 5715 | if (Op0OK) |
| 5716 | ToPromote.insert(ToPromote1.begin(), ToPromote1.end()); |
| 5717 | |
| 5718 | return true; |
| 5719 | } |
| 5720 | |
| 5721 | return false; |
| 5722 | } |
| 5723 | |
| 5724 | void PPCDAGToDAGISel::PeepholePPC64ZExt() { |
| 5725 | if (!PPCSubTarget->isPPC64()) |
| 5726 | return; |
| 5727 | |
| 5728 | // When we zero-extend from i32 to i64, we use a pattern like this: |
| 5729 | // def : Pat<(i64 (zext i32:$in)), |
| 5730 | // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32), |
| 5731 | // 0, 32)>; |
| 5732 | // There are several 32-bit shift/rotate instructions, however, that will |
| 5733 | // clear the higher-order bits of their output, rendering the RLDICL |
| 5734 | // unnecessary. When that happens, we remove it here, and redefine the |
| 5735 | // relevant 32-bit operation to be a 64-bit operation. |
| 5736 | |
| 5737 | SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); |
| 5738 | ++Position; |
| 5739 | |
| 5740 | bool MadeChange = false; |
| 5741 | while (Position != CurDAG->allnodes_begin()) { |
Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 5742 | SDNode *N = &*--Position; |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 5743 | // Skip dead nodes and any non-machine opcodes. |
| 5744 | if (N->use_empty() || !N->isMachineOpcode()) |
| 5745 | continue; |
| 5746 | |
| 5747 | if (N->getMachineOpcode() != PPC::RLDICL) |
| 5748 | continue; |
| 5749 | |
| 5750 | if (N->getConstantOperandVal(1) != 0 || |
| 5751 | N->getConstantOperandVal(2) != 32) |
| 5752 | continue; |
| 5753 | |
| 5754 | SDValue ISR = N->getOperand(0); |
| 5755 | if (!ISR.isMachineOpcode() || |
| 5756 | ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG) |
| 5757 | continue; |
| 5758 | |
| 5759 | if (!ISR.hasOneUse()) |
| 5760 | continue; |
| 5761 | |
| 5762 | if (ISR.getConstantOperandVal(2) != PPC::sub_32) |
| 5763 | continue; |
| 5764 | |
| 5765 | SDValue IDef = ISR.getOperand(0); |
| 5766 | if (!IDef.isMachineOpcode() || |
| 5767 | IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF) |
| 5768 | continue; |
| 5769 | |
| 5770 | // We now know that we're looking at a canonical i32 -> i64 zext. See if we |
| 5771 | // can get rid of it. |
| 5772 | |
| 5773 | SDValue Op32 = ISR->getOperand(1); |
| 5774 | if (!Op32.isMachineOpcode()) |
| 5775 | continue; |
| 5776 | |
| 5777 | // There are some 32-bit instructions that always clear the high-order 32 |
| 5778 | // bits, there are also some instructions (like AND) that we can look |
| 5779 | // through. |
| 5780 | SmallPtrSet<SDNode *, 16> ToPromote; |
| 5781 | if (!PeepholePPC64ZExtGather(Op32, ToPromote)) |
| 5782 | continue; |
| 5783 | |
| 5784 | // If the ToPromote set contains nodes that have uses outside of the set |
| 5785 | // (except for the original INSERT_SUBREG), then abort the transformation. |
| 5786 | bool OutsideUse = false; |
| 5787 | for (SDNode *PN : ToPromote) { |
| 5788 | for (SDNode *UN : PN->uses()) { |
| 5789 | if (!ToPromote.count(UN) && UN != ISR.getNode()) { |
| 5790 | OutsideUse = true; |
| 5791 | break; |
| 5792 | } |
| 5793 | } |
| 5794 | |
| 5795 | if (OutsideUse) |
| 5796 | break; |
| 5797 | } |
| 5798 | if (OutsideUse) |
| 5799 | continue; |
| 5800 | |
| 5801 | MadeChange = true; |
| 5802 | |
| 5803 | // We now know that this zero extension can be removed by promoting to |
| 5804 | // nodes in ToPromote to 64-bit operations, where for operations in the |
| 5805 | // frontier of the set, we need to insert INSERT_SUBREGs for their |
| 5806 | // operands. |
| 5807 | for (SDNode *PN : ToPromote) { |
| 5808 | unsigned NewOpcode; |
| 5809 | switch (PN->getMachineOpcode()) { |
| 5810 | default: |
| 5811 | llvm_unreachable("Don't know the 64-bit variant of this instruction"); |
| 5812 | case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break; |
| 5813 | case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break; |
| 5814 | case PPC::SLW: NewOpcode = PPC::SLW8; break; |
| 5815 | case PPC::SRW: NewOpcode = PPC::SRW8; break; |
| 5816 | case PPC::LI: NewOpcode = PPC::LI8; break; |
| 5817 | case PPC::LIS: NewOpcode = PPC::LIS8; break; |
Hal Finkel | 4e2c782 | 2015-01-05 18:09:06 +0000 | [diff] [blame] | 5818 | case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break; |
| 5819 | case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break; |
Hal Finkel | 49557f1 | 2015-01-05 18:52:29 +0000 | [diff] [blame] | 5820 | case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break; |
Nemanja Ivanovic | 32b5fed | 2016-10-27 05:17:58 +0000 | [diff] [blame] | 5821 | case PPC::CNTTZW: NewOpcode = PPC::CNTTZW8; break; |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 5822 | case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break; |
| 5823 | case PPC::OR: NewOpcode = PPC::OR8; break; |
| 5824 | case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break; |
| 5825 | case PPC::ORI: NewOpcode = PPC::ORI8; break; |
| 5826 | case PPC::ORIS: NewOpcode = PPC::ORIS8; break; |
| 5827 | case PPC::AND: NewOpcode = PPC::AND8; break; |
| 5828 | case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break; |
| 5829 | case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break; |
| 5830 | } |
| 5831 | |
| 5832 | // Note: During the replacement process, the nodes will be in an |
| 5833 | // inconsistent state (some instructions will have operands with values |
| 5834 | // of the wrong type). Once done, however, everything should be right |
| 5835 | // again. |
| 5836 | |
| 5837 | SmallVector<SDValue, 4> Ops; |
| 5838 | for (const SDValue &V : PN->ops()) { |
| 5839 | if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 && |
| 5840 | !isa<ConstantSDNode>(V)) { |
| 5841 | SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) }; |
| 5842 | SDNode *ReplOp = |
| 5843 | CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V), |
| 5844 | ISR.getNode()->getVTList(), ReplOpOps); |
| 5845 | Ops.push_back(SDValue(ReplOp, 0)); |
| 5846 | } else { |
| 5847 | Ops.push_back(V); |
| 5848 | } |
| 5849 | } |
| 5850 | |
| 5851 | // Because all to-be-promoted nodes only have users that are other |
| 5852 | // promoted nodes (or the original INSERT_SUBREG), we can safely replace |
| 5853 | // the i32 result value type with i64. |
| 5854 | |
| 5855 | SmallVector<EVT, 2> NewVTs; |
| 5856 | SDVTList VTs = PN->getVTList(); |
| 5857 | for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i) |
| 5858 | if (VTs.VTs[i] == MVT::i32) |
| 5859 | NewVTs.push_back(MVT::i64); |
| 5860 | else |
| 5861 | NewVTs.push_back(VTs.VTs[i]); |
| 5862 | |
| 5863 | DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: "); |
| 5864 | DEBUG(PN->dump(CurDAG)); |
| 5865 | |
| 5866 | CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops); |
| 5867 | |
| 5868 | DEBUG(dbgs() << "\nNew: "); |
| 5869 | DEBUG(PN->dump(CurDAG)); |
| 5870 | DEBUG(dbgs() << "\n"); |
| 5871 | } |
| 5872 | |
| 5873 | // Now we replace the original zero extend and its associated INSERT_SUBREG |
| 5874 | // with the value feeding the INSERT_SUBREG (which has now been promoted to |
| 5875 | // return an i64). |
| 5876 | |
| 5877 | DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: "); |
| 5878 | DEBUG(N->dump(CurDAG)); |
| 5879 | DEBUG(dbgs() << "\nNew: "); |
| 5880 | DEBUG(Op32.getNode()->dump(CurDAG)); |
| 5881 | DEBUG(dbgs() << "\n"); |
| 5882 | |
| 5883 | ReplaceUses(N, Op32.getNode()); |
| 5884 | } |
| 5885 | |
| 5886 | if (MadeChange) |
| 5887 | CurDAG->RemoveDeadNodes(); |
| 5888 | } |
| 5889 | |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5890 | void PPCDAGToDAGISel::PeepholePPC64() { |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5891 | // These optimizations are currently supported only for 64-bit SVR4. |
Eric Christopher | 1b8e763 | 2014-05-22 01:07:24 +0000 | [diff] [blame] | 5892 | if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64()) |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5893 | return; |
| 5894 | |
| 5895 | SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode()); |
| 5896 | ++Position; |
| 5897 | |
| 5898 | while (Position != CurDAG->allnodes_begin()) { |
Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 5899 | SDNode *N = &*--Position; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5900 | // Skip dead nodes and any non-machine opcodes. |
| 5901 | if (N->use_empty() || !N->isMachineOpcode()) |
| 5902 | continue; |
| 5903 | |
| 5904 | unsigned FirstOp; |
| 5905 | unsigned StorageOpcode = N->getMachineOpcode(); |
| 5906 | |
| 5907 | switch (StorageOpcode) { |
| 5908 | default: continue; |
| 5909 | |
| 5910 | case PPC::LBZ: |
| 5911 | case PPC::LBZ8: |
| 5912 | case PPC::LD: |
| 5913 | case PPC::LFD: |
| 5914 | case PPC::LFS: |
| 5915 | case PPC::LHA: |
| 5916 | case PPC::LHA8: |
| 5917 | case PPC::LHZ: |
| 5918 | case PPC::LHZ8: |
| 5919 | case PPC::LWA: |
| 5920 | case PPC::LWZ: |
| 5921 | case PPC::LWZ8: |
| 5922 | FirstOp = 0; |
| 5923 | break; |
| 5924 | |
| 5925 | case PPC::STB: |
| 5926 | case PPC::STB8: |
| 5927 | case PPC::STD: |
| 5928 | case PPC::STFD: |
| 5929 | case PPC::STFS: |
| 5930 | case PPC::STH: |
| 5931 | case PPC::STH8: |
| 5932 | case PPC::STW: |
| 5933 | case PPC::STW8: |
| 5934 | FirstOp = 1; |
| 5935 | break; |
| 5936 | } |
| 5937 | |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 5938 | // If this is a load or store with a zero offset, or within the alignment, |
| 5939 | // we may be able to fold an add-immediate into the memory operation. |
| 5940 | // The check against alignment is below, as it can't occur until we check |
| 5941 | // the arguments to N |
| 5942 | if (!isa<ConstantSDNode>(N->getOperand(FirstOp))) |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5943 | continue; |
| 5944 | |
| 5945 | SDValue Base = N->getOperand(FirstOp + 1); |
| 5946 | if (!Base.isMachineOpcode()) |
| 5947 | continue; |
| 5948 | |
| 5949 | unsigned Flags = 0; |
| 5950 | bool ReplaceFlags = true; |
| 5951 | |
| 5952 | // When the feeding operation is an add-immediate of some sort, |
| 5953 | // determine whether we need to add relocation information to the |
| 5954 | // target flags on the immediate operand when we fold it into the |
| 5955 | // load instruction. |
| 5956 | // |
| 5957 | // For something like ADDItocL, the relocation information is |
| 5958 | // inferred from the opcode; when we process it in the AsmPrinter, |
| 5959 | // we add the necessary relocation there. A load, though, can receive |
| 5960 | // relocation from various flavors of ADDIxxx, so we need to carry |
| 5961 | // the relocation information in the target flags. |
| 5962 | switch (Base.getMachineOpcode()) { |
| 5963 | default: continue; |
| 5964 | |
| 5965 | case PPC::ADDI8: |
Ulrich Weigand | 35f9fdf | 2013-03-26 10:55:20 +0000 | [diff] [blame] | 5966 | case PPC::ADDI: |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5967 | // In some cases (such as TLS) the relocation information |
| 5968 | // is already in place on the operand, so copying the operand |
| 5969 | // is sufficient. |
| 5970 | ReplaceFlags = false; |
| 5971 | // For these cases, the immediate may not be divisible by 4, in |
| 5972 | // which case the fold is illegal for DS-form instructions. (The |
| 5973 | // other cases provide aligned addresses and are always safe.) |
| 5974 | if ((StorageOpcode == PPC::LWA || |
| 5975 | StorageOpcode == PPC::LD || |
| 5976 | StorageOpcode == PPC::STD) && |
| 5977 | (!isa<ConstantSDNode>(Base.getOperand(1)) || |
| 5978 | Base.getConstantOperandVal(1) % 4 != 0)) |
| 5979 | continue; |
| 5980 | break; |
| 5981 | case PPC::ADDIdtprelL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 5982 | Flags = PPCII::MO_DTPREL_LO; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5983 | break; |
| 5984 | case PPC::ADDItlsldL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 5985 | Flags = PPCII::MO_TLSLD_LO; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5986 | break; |
| 5987 | case PPC::ADDItocL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 5988 | Flags = PPCII::MO_TOC_LO; |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 5989 | break; |
| 5990 | } |
| 5991 | |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 5992 | SDValue ImmOpnd = Base.getOperand(1); |
Hal Finkel | b54579f | 2016-09-02 00:28:20 +0000 | [diff] [blame] | 5993 | |
| 5994 | // On PPC64, the TOC base pointer is guaranteed by the ABI only to have |
| 5995 | // 8-byte alignment, and so we can only use offsets less than 8 (otherwise, |
| 5996 | // we might have needed different @ha relocation values for the offset |
| 5997 | // pointers). |
| 5998 | int MaxDisplacement = 7; |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 5999 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { |
| 6000 | const GlobalValue *GV = GA->getGlobal(); |
Hal Finkel | b54579f | 2016-09-02 00:28:20 +0000 | [diff] [blame] | 6001 | MaxDisplacement = std::min((int) GV->getAlignment() - 1, MaxDisplacement); |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
Hal Finkel | 7b104d4 | 2016-09-02 21:37:07 +0000 | [diff] [blame] | 6004 | bool UpdateHBase = false; |
| 6005 | SDValue HBase = Base.getOperand(0); |
| 6006 | |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 6007 | int Offset = N->getConstantOperandVal(FirstOp); |
Hal Finkel | 42c83f1 | 2016-09-07 07:36:11 +0000 | [diff] [blame] | 6008 | if (ReplaceFlags) { |
| 6009 | if (Offset < 0 || Offset > MaxDisplacement) { |
| 6010 | // If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only |
| 6011 | // one use, then we can do this for any offset, we just need to also |
| 6012 | // update the offset (i.e. the symbol addend) on the addis also. |
| 6013 | if (Base.getMachineOpcode() != PPC::ADDItocL) |
| 6014 | continue; |
Hal Finkel | 7b104d4 | 2016-09-02 21:37:07 +0000 | [diff] [blame] | 6015 | |
Hal Finkel | 42c83f1 | 2016-09-07 07:36:11 +0000 | [diff] [blame] | 6016 | if (!HBase.isMachineOpcode() || |
| 6017 | HBase.getMachineOpcode() != PPC::ADDIStocHA) |
| 6018 | continue; |
Hal Finkel | 7b104d4 | 2016-09-02 21:37:07 +0000 | [diff] [blame] | 6019 | |
Hal Finkel | 42c83f1 | 2016-09-07 07:36:11 +0000 | [diff] [blame] | 6020 | if (!Base.hasOneUse() || !HBase.hasOneUse()) |
| 6021 | continue; |
Hal Finkel | 7b104d4 | 2016-09-02 21:37:07 +0000 | [diff] [blame] | 6022 | |
Hal Finkel | 42c83f1 | 2016-09-07 07:36:11 +0000 | [diff] [blame] | 6023 | SDValue HImmOpnd = HBase.getOperand(1); |
| 6024 | if (HImmOpnd != ImmOpnd) |
| 6025 | continue; |
Hal Finkel | 7b104d4 | 2016-09-02 21:37:07 +0000 | [diff] [blame] | 6026 | |
Hal Finkel | 42c83f1 | 2016-09-07 07:36:11 +0000 | [diff] [blame] | 6027 | UpdateHBase = true; |
| 6028 | } |
| 6029 | } else { |
| 6030 | // If we're directly folding the addend from an addi instruction, then: |
| 6031 | // 1. In general, the offset on the memory access must be zero. |
| 6032 | // 2. If the addend is a constant, then it can be combined with a |
| 6033 | // non-zero offset, but only if the result meets the encoding |
| 6034 | // requirements. |
| 6035 | if (auto *C = dyn_cast<ConstantSDNode>(ImmOpnd)) { |
| 6036 | Offset += C->getSExtValue(); |
| 6037 | |
| 6038 | if ((StorageOpcode == PPC::LWA || StorageOpcode == PPC::LD || |
| 6039 | StorageOpcode == PPC::STD) && (Offset % 4) != 0) |
| 6040 | continue; |
| 6041 | |
| 6042 | if (!isInt<16>(Offset)) |
| 6043 | continue; |
| 6044 | |
| 6045 | ImmOpnd = CurDAG->getTargetConstant(Offset, SDLoc(ImmOpnd), |
| 6046 | ImmOpnd.getValueType()); |
| 6047 | } else if (Offset != 0) { |
| 6048 | continue; |
| 6049 | } |
Hal Finkel | 7b104d4 | 2016-09-02 21:37:07 +0000 | [diff] [blame] | 6050 | } |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 6051 | |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 6052 | // We found an opportunity. Reverse the operands from the add |
| 6053 | // immediate and substitute them into the load or store. If |
| 6054 | // needed, update the target flags for the immediate operand to |
| 6055 | // reflect the necessary relocation information. |
| 6056 | DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: "); |
| 6057 | DEBUG(Base->dump(CurDAG)); |
| 6058 | DEBUG(dbgs() << "\nN: "); |
| 6059 | DEBUG(N->dump(CurDAG)); |
| 6060 | DEBUG(dbgs() << "\n"); |
| 6061 | |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 6062 | // If the relocation information isn't already present on the |
| 6063 | // immediate operand, add it now. |
| 6064 | if (ReplaceFlags) { |
Bill Schmidt | 49498da | 2013-02-21 14:35:42 +0000 | [diff] [blame] | 6065 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6066 | SDLoc dl(GA); |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 6067 | const GlobalValue *GV = GA->getGlobal(); |
Bill Schmidt | 48fc20a | 2013-07-01 20:52:27 +0000 | [diff] [blame] | 6068 | // We can't perform this optimization for data whose alignment |
| 6069 | // is insufficient for the instruction encoding. |
| 6070 | if (GV->getAlignment() < 4 && |
| 6071 | (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD || |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 6072 | StorageOpcode == PPC::LWA || (Offset % 4) != 0)) { |
Bill Schmidt | 48fc20a | 2013-07-01 20:52:27 +0000 | [diff] [blame] | 6073 | DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n"); |
| 6074 | continue; |
| 6075 | } |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 6076 | ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, Offset, Flags); |
Bill Schmidt | 836c45b | 2013-02-21 17:26:05 +0000 | [diff] [blame] | 6077 | } else if (ConstantPoolSDNode *CP = |
| 6078 | dyn_cast<ConstantPoolSDNode>(ImmOpnd)) { |
Bill Schmidt | 49498da | 2013-02-21 14:35:42 +0000 | [diff] [blame] | 6079 | const Constant *C = CP->getConstVal(); |
| 6080 | ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64, |
| 6081 | CP->getAlignment(), |
Kyle Butt | 1452b76 | 2015-12-11 00:47:36 +0000 | [diff] [blame] | 6082 | Offset, Flags); |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 6083 | } |
| 6084 | } |
| 6085 | |
| 6086 | if (FirstOp == 1) // Store |
| 6087 | (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd, |
| 6088 | Base.getOperand(0), N->getOperand(3)); |
| 6089 | else // Load |
| 6090 | (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0), |
| 6091 | N->getOperand(2)); |
| 6092 | |
Hal Finkel | 7b104d4 | 2016-09-02 21:37:07 +0000 | [diff] [blame] | 6093 | if (UpdateHBase) |
| 6094 | (void)CurDAG->UpdateNodeOperands(HBase.getNode(), HBase.getOperand(0), |
| 6095 | ImmOpnd); |
| 6096 | |
Bill Schmidt | f5b474c | 2013-02-21 00:38:25 +0000 | [diff] [blame] | 6097 | // The add-immediate may now be dead, in which case remove it. |
| 6098 | if (Base.getNode()->use_empty()) |
| 6099 | CurDAG->RemoveDeadNode(Base.getNode()); |
| 6100 | } |
| 6101 | } |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 6102 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 6103 | /// createPPCISelDag - This pass converts a legalized DAG into a |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 6104 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 6105 | /// |
Hiroshi Inoue | 5102028 | 2017-06-27 04:52:17 +0000 | [diff] [blame] | 6106 | FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM, |
| 6107 | CodeGenOpt::Level OptLevel) { |
| 6108 | return new PPCDAGToDAGISel(TM, OptLevel); |
Chris Lattner | 43ff01e | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 6109 | } |