Matt Arsenault | 7836f89 | 2016-01-20 21:22:21 +0000 | [diff] [blame] | 1 | //===-- AMDGPUISelDAGToDAG.cpp - A dag to dag inst selector for AMDGPU ----===// |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //==-----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// \brief Defines an instruction selector for the AMDGPU target. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 592d068 | 2015-12-01 23:04:05 +0000 | [diff] [blame] | 14 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 15 | #include "AMDGPUInstrInfo.h" |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame^] | 16 | #include "AMDGPUIntrinsicInfo.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 17 | #include "AMDGPUISelLowering.h" // For AMDGPUISD |
| 18 | #include "AMDGPURegisterInfo.h" |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 19 | #include "AMDGPUSubtarget.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 20 | #include "R600InstrInfo.h" |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 21 | #include "SIDefines.h" |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 22 | #include "SIISelLowering.h" |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 23 | #include "SIMachineFunctionInfo.h" |
Tom Stellard | 58ac744 | 2014-04-29 23:12:48 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Matt Arsenault | d9d659a | 2015-11-03 22:30:08 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/SelectionDAG.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DiagnosticInfo.h" |
Tom Stellard | 58ac744 | 2014-04-29 23:12:48 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Function.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Instruction Selector Implementation |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | namespace { |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame^] | 40 | |
| 41 | static bool isCBranchSCC(const SDNode *N) { |
| 42 | assert(N->getOpcode() == ISD::BRCOND); |
| 43 | if (!N->hasOneUse()) |
| 44 | return false; |
| 45 | |
| 46 | SDValue Cond = N->getOperand(1); |
| 47 | if (Cond.getOpcode() == ISD::CopyToReg) |
| 48 | Cond = Cond.getOperand(2); |
| 49 | return Cond.getOpcode() == ISD::SETCC && |
| 50 | Cond.getOperand(0).getValueType() == MVT::i32 && |
| 51 | Cond.hasOneUse(); |
| 52 | } |
| 53 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 54 | /// AMDGPU specific code to select AMDGPU machine instructions for |
| 55 | /// SelectionDAG operations. |
| 56 | class AMDGPUDAGToDAGISel : public SelectionDAGISel { |
| 57 | // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can |
| 58 | // make the right decision when generating code for different targets. |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 59 | const AMDGPUSubtarget *Subtarget; |
NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 60 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 61 | public: |
| 62 | AMDGPUDAGToDAGISel(TargetMachine &TM); |
| 63 | virtual ~AMDGPUDAGToDAGISel(); |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 64 | bool runOnMachineFunction(MachineFunction &MF) override; |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 65 | SDNode *Select(SDNode *N) override; |
| 66 | const char *getPassName() const override; |
Matt Arsenault | 4bf43d4 | 2015-09-25 17:27:08 +0000 | [diff] [blame] | 67 | void PreprocessISelDAG() override; |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 68 | void PostprocessISelDAG() override; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 69 | |
| 70 | private: |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 71 | bool isInlineImmediate(SDNode *N) const; |
Vincent Lejeune | c689679 | 2013-06-04 23:17:15 +0000 | [diff] [blame] | 72 | bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs, |
Tom Stellard | 8402144 | 2013-07-23 01:48:24 +0000 | [diff] [blame] | 73 | const R600InstrInfo *TII); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 74 | bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); |
Vincent Lejeune | c689679 | 2013-06-04 23:17:15 +0000 | [diff] [blame] | 75 | bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 76 | |
| 77 | // Complex pattern selectors |
| 78 | bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2); |
| 79 | bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2); |
| 80 | bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2); |
| 81 | |
| 82 | static bool checkType(const Value *ptr, unsigned int addrspace); |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 83 | static bool checkPrivateAddress(const MachineMemOperand *Op); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 84 | |
| 85 | static bool isGlobalStore(const StoreSDNode *N); |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 86 | static bool isFlatStore(const StoreSDNode *N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 87 | static bool isPrivateStore(const StoreSDNode *N); |
| 88 | static bool isLocalStore(const StoreSDNode *N); |
| 89 | static bool isRegionStore(const StoreSDNode *N); |
| 90 | |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 91 | bool isCPLoad(const LoadSDNode *N) const; |
| 92 | bool isConstantLoad(const LoadSDNode *N, int cbID) const; |
| 93 | bool isGlobalLoad(const LoadSDNode *N) const; |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 94 | bool isFlatLoad(const LoadSDNode *N) const; |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 95 | bool isParamLoad(const LoadSDNode *N) const; |
| 96 | bool isPrivateLoad(const LoadSDNode *N) const; |
| 97 | bool isLocalLoad(const LoadSDNode *N) const; |
| 98 | bool isRegionLoad(const LoadSDNode *N) const; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 99 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame^] | 100 | bool isUniformBr(const SDNode *N) const; |
| 101 | |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 102 | SDNode *glueCopyToM0(SDNode *N) const; |
| 103 | |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 104 | const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const; |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 105 | bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr); |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 106 | bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg, |
| 107 | SDValue& Offset); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 108 | bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 109 | bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 110 | bool isDSOffsetLegal(const SDValue &Base, unsigned Offset, |
| 111 | unsigned OffsetBits) const; |
| 112 | bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const; |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 113 | bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0, |
| 114 | SDValue &Offset1) const; |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 115 | bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 116 | SDValue &SOffset, SDValue &Offset, SDValue &Offen, |
| 117 | SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC, |
| 118 | SDValue &TFE) const; |
| 119 | bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 120 | SDValue &SOffset, SDValue &Offset, SDValue &GLC, |
| 121 | SDValue &SLC, SDValue &TFE) const; |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 122 | bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, |
Tom Stellard | c53861a | 2015-02-11 00:34:32 +0000 | [diff] [blame] | 123 | SDValue &VAddr, SDValue &SOffset, SDValue &Offset, |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 124 | SDValue &SLC) const; |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 125 | bool SelectMUBUFScratch(SDValue Addr, SDValue &RSrc, SDValue &VAddr, |
| 126 | SDValue &SOffset, SDValue &ImmOffset) const; |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 127 | bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset, |
| 128 | SDValue &Offset, SDValue &GLC, SDValue &SLC, |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 129 | SDValue &TFE) const; |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 130 | bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, |
| 131 | SDValue &Offset, SDValue &GLC) const; |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 132 | bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset, |
| 133 | bool &Imm) const; |
| 134 | bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset, |
| 135 | bool &Imm) const; |
| 136 | bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 137 | bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const; |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 138 | bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const; |
| 139 | bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 140 | bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const; |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 141 | bool SelectSMRDBufferSgpr(SDValue Addr, SDValue &Offset) const; |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 142 | SDNode *SelectAddrSpaceCast(SDNode *N); |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 143 | bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const; |
Tom Stellard | db5a11f | 2015-07-13 15:47:57 +0000 | [diff] [blame] | 144 | bool SelectVOP3NoMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 145 | bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 146 | SDValue &Clamp, SDValue &Omod) const; |
Tom Stellard | db5a11f | 2015-07-13 15:47:57 +0000 | [diff] [blame] | 147 | bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 148 | SDValue &Clamp, SDValue &Omod) const; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 149 | |
Matt Arsenault | 1cffa4c | 2014-11-13 19:49:04 +0000 | [diff] [blame] | 150 | bool SelectVOP3Mods0Clamp(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 151 | SDValue &Omod) const; |
Matt Arsenault | 4831ce5 | 2015-01-06 23:00:37 +0000 | [diff] [blame] | 152 | bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 153 | SDValue &Clamp, |
| 154 | SDValue &Omod) const; |
Matt Arsenault | 1cffa4c | 2014-11-13 19:49:04 +0000 | [diff] [blame] | 155 | |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 156 | SDNode *SelectADD_SUB_I64(SDNode *N); |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 157 | SDNode *SelectDIV_SCALE(SDNode *N); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 158 | |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 159 | SDNode *getS_BFE(unsigned Opcode, SDLoc DL, SDValue Val, |
| 160 | uint32_t Offset, uint32_t Width); |
| 161 | SDNode *SelectS_BFEFromShifts(SDNode *N); |
| 162 | SDNode *SelectS_BFE(SDNode *N); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame^] | 163 | SDNode *SelectBRCOND(SDNode *N); |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 164 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 165 | // Include the pieces autogenerated from the target description. |
| 166 | #include "AMDGPUGenDAGISel.inc" |
| 167 | }; |
| 168 | } // end anonymous namespace |
| 169 | |
| 170 | /// \brief This pass converts a legalized DAG into a AMDGPU-specific |
| 171 | // DAG, ready for instruction scheduling. |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 172 | FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 173 | return new AMDGPUDAGToDAGISel(TM); |
| 174 | } |
| 175 | |
Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 176 | AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM) |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 177 | : SelectionDAGISel(TM) {} |
| 178 | |
| 179 | bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { |
| 180 | Subtarget = &static_cast<const AMDGPUSubtarget &>(MF.getSubtarget()); |
| 181 | return SelectionDAGISel::runOnMachineFunction(MF); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() { |
| 185 | } |
| 186 | |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 187 | bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const { |
| 188 | const SITargetLowering *TL |
| 189 | = static_cast<const SITargetLowering *>(getTargetLowering()); |
| 190 | return TL->analyzeImmediate(N) == 0; |
| 191 | } |
| 192 | |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 193 | /// \brief Determine the register class for \p OpNo |
| 194 | /// \returns The register class of the virtual register that will be used for |
| 195 | /// the given operand number \OpNo or NULL if the register class cannot be |
| 196 | /// determined. |
| 197 | const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N, |
| 198 | unsigned OpNo) const { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 199 | if (!N->isMachineOpcode()) |
| 200 | return nullptr; |
| 201 | |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 202 | switch (N->getMachineOpcode()) { |
| 203 | default: { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 204 | const MCInstrDesc &Desc = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 205 | Subtarget->getInstrInfo()->get(N->getMachineOpcode()); |
Alexey Samsonov | 3186eb3 | 2013-08-15 07:11:34 +0000 | [diff] [blame] | 206 | unsigned OpIdx = Desc.getNumDefs() + OpNo; |
| 207 | if (OpIdx >= Desc.getNumOperands()) |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 208 | return nullptr; |
Alexey Samsonov | 3186eb3 | 2013-08-15 07:11:34 +0000 | [diff] [blame] | 209 | int RegClass = Desc.OpInfo[OpIdx].RegClass; |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 210 | if (RegClass == -1) |
| 211 | return nullptr; |
| 212 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 213 | return Subtarget->getRegisterInfo()->getRegClass(RegClass); |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 214 | } |
| 215 | case AMDGPU::REG_SEQUENCE: { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 216 | unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 217 | const TargetRegisterClass *SuperRC = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 218 | Subtarget->getRegisterInfo()->getRegClass(RCID); |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 219 | |
| 220 | SDValue SubRegOp = N->getOperand(OpNo + 1); |
| 221 | unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue(); |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 222 | return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC, |
| 223 | SubRegIdx); |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 228 | bool AMDGPUDAGToDAGISel::SelectADDRParam( |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 229 | SDValue Addr, SDValue& R1, SDValue& R2) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 230 | |
| 231 | if (Addr.getOpcode() == ISD::FrameIndex) { |
| 232 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
| 233 | R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 234 | R2 = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 235 | } else { |
| 236 | R1 = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 237 | R2 = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 238 | } |
| 239 | } else if (Addr.getOpcode() == ISD::ADD) { |
| 240 | R1 = Addr.getOperand(0); |
| 241 | R2 = Addr.getOperand(1); |
| 242 | } else { |
| 243 | R1 = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 244 | R2 = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) { |
| 250 | if (Addr.getOpcode() == ISD::TargetExternalSymbol || |
| 251 | Addr.getOpcode() == ISD::TargetGlobalAddress) { |
| 252 | return false; |
| 253 | } |
| 254 | return SelectADDRParam(Addr, R1, R2); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) { |
| 259 | if (Addr.getOpcode() == ISD::TargetExternalSymbol || |
| 260 | Addr.getOpcode() == ISD::TargetGlobalAddress) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | if (Addr.getOpcode() == ISD::FrameIndex) { |
| 265 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
| 266 | R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 267 | R2 = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i64); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 268 | } else { |
| 269 | R1 = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 270 | R2 = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i64); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 271 | } |
| 272 | } else if (Addr.getOpcode() == ISD::ADD) { |
| 273 | R1 = Addr.getOperand(0); |
| 274 | R2 = Addr.getOperand(1); |
| 275 | } else { |
| 276 | R1 = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 277 | R2 = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i64); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 278 | } |
| 279 | return true; |
| 280 | } |
| 281 | |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 282 | SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N) const { |
| 283 | if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS || |
| 284 | !checkType(cast<MemSDNode>(N)->getMemOperand()->getValue(), |
| 285 | AMDGPUAS::LOCAL_ADDRESS)) |
| 286 | return N; |
| 287 | |
| 288 | const SITargetLowering& Lowering = |
| 289 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
| 290 | |
| 291 | // Write max value to m0 before each load operation |
| 292 | |
| 293 | SDValue M0 = Lowering.copyToM0(*CurDAG, CurDAG->getEntryNode(), SDLoc(N), |
| 294 | CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32)); |
| 295 | |
| 296 | SDValue Glue = M0.getValue(1); |
| 297 | |
| 298 | SmallVector <SDValue, 8> Ops; |
| 299 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 300 | Ops.push_back(N->getOperand(i)); |
| 301 | } |
| 302 | Ops.push_back(Glue); |
| 303 | CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops); |
| 304 | |
| 305 | return N; |
| 306 | } |
| 307 | |
Matt Arsenault | 61cb6fa | 2015-11-11 00:01:36 +0000 | [diff] [blame] | 308 | static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) { |
Matt Arsenault | f1aebbf | 2015-11-02 23:30:48 +0000 | [diff] [blame] | 309 | switch (NumVectorElts) { |
| 310 | case 1: |
| 311 | return AMDGPU::SReg_32RegClassID; |
| 312 | case 2: |
| 313 | return AMDGPU::SReg_64RegClassID; |
| 314 | case 4: |
| 315 | return AMDGPU::SReg_128RegClassID; |
| 316 | case 8: |
| 317 | return AMDGPU::SReg_256RegClassID; |
| 318 | case 16: |
| 319 | return AMDGPU::SReg_512RegClassID; |
| 320 | } |
| 321 | |
| 322 | llvm_unreachable("invalid vector size"); |
| 323 | } |
| 324 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 325 | SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) { |
| 326 | unsigned int Opc = N->getOpcode(); |
| 327 | if (N->isMachineOpcode()) { |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 328 | N->setNodeId(-1); |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 329 | return nullptr; // Already selected. |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 330 | } |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 331 | |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 332 | if (isa<AtomicSDNode>(N)) |
| 333 | N = glueCopyToM0(N); |
| 334 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 335 | switch (Opc) { |
| 336 | default: break; |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 337 | // We are selecting i64 ADD here instead of custom lower it during |
| 338 | // DAG legalization, so we can fold some i64 ADDs used for address |
| 339 | // calculation into the LOAD and STORE instructions. |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 340 | case ISD::ADD: |
| 341 | case ISD::SUB: { |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 342 | if (N->getValueType(0) != MVT::i64 || |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 343 | Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 344 | break; |
| 345 | |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 346 | return SelectADD_SUB_I64(N); |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 347 | } |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 348 | case ISD::SCALAR_TO_VECTOR: |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 349 | case AMDGPUISD::BUILD_VERTICAL_VECTOR: |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 350 | case ISD::BUILD_VECTOR: { |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 351 | unsigned RegClassID; |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 352 | const AMDGPURegisterInfo *TRI = Subtarget->getRegisterInfo(); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 353 | EVT VT = N->getValueType(0); |
| 354 | unsigned NumVectorElts = VT.getVectorNumElements(); |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 355 | EVT EltVT = VT.getVectorElementType(); |
| 356 | assert(EltVT.bitsEq(MVT::i32)); |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 357 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
Matt Arsenault | 61cb6fa | 2015-11-11 00:01:36 +0000 | [diff] [blame] | 358 | RegClassID = selectSGPRVectorRegClassID(NumVectorElts); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 359 | } else { |
| 360 | // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG |
| 361 | // that adds a 128 bits reg copy when going through TwoAddressInstructions |
| 362 | // pass. We want to avoid 128 bits copies as much as possible because they |
| 363 | // can't be bundled by our scheduler. |
| 364 | switch(NumVectorElts) { |
| 365 | case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 366 | case 4: |
| 367 | if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR) |
| 368 | RegClassID = AMDGPU::R600_Reg128VerticalRegClassID; |
| 369 | else |
| 370 | RegClassID = AMDGPU::R600_Reg128RegClassID; |
| 371 | break; |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 372 | default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR"); |
| 373 | } |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 374 | } |
Tom Stellard | 0344cdf | 2013-08-01 15:23:42 +0000 | [diff] [blame] | 375 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 376 | SDLoc DL(N); |
| 377 | SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 378 | |
| 379 | if (NumVectorElts == 1) { |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 380 | return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 381 | N->getOperand(0), RegClass); |
Tom Stellard | 0344cdf | 2013-08-01 15:23:42 +0000 | [diff] [blame] | 382 | } |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 383 | |
| 384 | assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not " |
| 385 | "supported yet"); |
| 386 | // 16 = Max Num Vector Elements |
| 387 | // 2 = 2 REG_SEQUENCE operands per element (value, subreg index) |
| 388 | // 1 = Vector Register Class |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 389 | SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 390 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 391 | RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 392 | bool IsRegSeq = true; |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 393 | unsigned NOps = N->getNumOperands(); |
| 394 | for (unsigned i = 0; i < NOps; i++) { |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 395 | // XXX: Why is this here? |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 396 | if (isa<RegisterSDNode>(N->getOperand(i))) { |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 397 | IsRegSeq = false; |
| 398 | break; |
| 399 | } |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 400 | RegSeqArgs[1 + (2 * i)] = N->getOperand(i); |
| 401 | RegSeqArgs[1 + (2 * i) + 1] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 402 | CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, |
| 403 | MVT::i32); |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 404 | } |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 405 | |
| 406 | if (NOps != NumVectorElts) { |
| 407 | // Fill in the missing undef elements if this was a scalar_to_vector. |
| 408 | assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts); |
| 409 | |
| 410 | MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 411 | DL, EltVT); |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 412 | for (unsigned i = NOps; i < NumVectorElts; ++i) { |
| 413 | RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0); |
| 414 | RegSeqArgs[1 + (2 * i) + 1] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 415 | CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, MVT::i32); |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 419 | if (!IsRegSeq) |
| 420 | break; |
| 421 | return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), |
Craig Topper | 481fb28 | 2014-04-27 19:21:11 +0000 | [diff] [blame] | 422 | RegSeqArgs); |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 423 | } |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 424 | case ISD::BUILD_PAIR: { |
| 425 | SDValue RC, SubReg0, SubReg1; |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 426 | if (Subtarget->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) { |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 427 | break; |
| 428 | } |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 429 | SDLoc DL(N); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 430 | if (N->getValueType(0) == MVT::i128) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 431 | RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32); |
| 432 | SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32); |
| 433 | SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 434 | } else if (N->getValueType(0) == MVT::i64) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 435 | RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32); |
| 436 | SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); |
| 437 | SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 438 | } else { |
| 439 | llvm_unreachable("Unhandled value type for BUILD_PAIR"); |
| 440 | } |
| 441 | const SDValue Ops[] = { RC, N->getOperand(0), SubReg0, |
| 442 | N->getOperand(1), SubReg1 }; |
| 443 | return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 444 | DL, N->getValueType(0), Ops); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 445 | } |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 446 | |
| 447 | case ISD::Constant: |
| 448 | case ISD::ConstantFP: { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 449 | if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS || |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 450 | N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N)) |
| 451 | break; |
| 452 | |
| 453 | uint64_t Imm; |
| 454 | if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N)) |
| 455 | Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 456 | else { |
Tom Stellard | 3cbe014 | 2014-04-07 19:31:13 +0000 | [diff] [blame] | 457 | ConstantSDNode *C = cast<ConstantSDNode>(N); |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 458 | Imm = C->getZExtValue(); |
| 459 | } |
| 460 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 461 | SDLoc DL(N); |
| 462 | SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
| 463 | CurDAG->getConstant(Imm & 0xFFFFFFFF, DL, |
| 464 | MVT::i32)); |
| 465 | SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
| 466 | CurDAG->getConstant(Imm >> 32, DL, MVT::i32)); |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 467 | const SDValue Ops[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 468 | CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), |
| 469 | SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
| 470 | SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32) |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 473 | return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 474 | N->getValueType(0), Ops); |
| 475 | } |
Matt Arsenault | 4bf43d4 | 2015-09-25 17:27:08 +0000 | [diff] [blame] | 476 | case ISD::LOAD: |
Tom Stellard | 096b8c1 | 2015-02-04 20:49:49 +0000 | [diff] [blame] | 477 | case ISD::STORE: { |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 478 | N = glueCopyToM0(N); |
Tom Stellard | 096b8c1 | 2015-02-04 20:49:49 +0000 | [diff] [blame] | 479 | break; |
| 480 | } |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 481 | |
| 482 | case AMDGPUISD::BFE_I32: |
| 483 | case AMDGPUISD::BFE_U32: { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 484 | if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 485 | break; |
| 486 | |
| 487 | // There is a scalar version available, but unlike the vector version which |
| 488 | // has a separate operand for the offset and width, the scalar version packs |
| 489 | // the width and offset into a single operand. Try to move to the scalar |
| 490 | // version if the offsets are constant, so that we can try to keep extended |
| 491 | // loads of kernel arguments in SGPRs. |
| 492 | |
| 493 | // TODO: Technically we could try to pattern match scalar bitshifts of |
| 494 | // dynamic values, but it's probably not useful. |
| 495 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 496 | if (!Offset) |
| 497 | break; |
| 498 | |
| 499 | ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); |
| 500 | if (!Width) |
| 501 | break; |
| 502 | |
| 503 | bool Signed = Opc == AMDGPUISD::BFE_I32; |
| 504 | |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 505 | uint32_t OffsetVal = Offset->getZExtValue(); |
| 506 | uint32_t WidthVal = Width->getZExtValue(); |
| 507 | |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 508 | return getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32, SDLoc(N), |
| 509 | N->getOperand(0), OffsetVal, WidthVal); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 510 | } |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 511 | case AMDGPUISD::DIV_SCALE: { |
| 512 | return SelectDIV_SCALE(N); |
| 513 | } |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 514 | case ISD::CopyToReg: { |
| 515 | const SITargetLowering& Lowering = |
| 516 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
| 517 | Lowering.legalizeTargetIndependentNode(N, *CurDAG); |
| 518 | break; |
| 519 | } |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 520 | case ISD::ADDRSPACECAST: |
| 521 | return SelectAddrSpaceCast(N); |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 522 | case ISD::AND: |
| 523 | case ISD::SRL: |
| 524 | case ISD::SRA: |
| 525 | if (N->getValueType(0) != MVT::i32 || |
| 526 | Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) |
| 527 | break; |
| 528 | |
| 529 | return SelectS_BFE(N); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame^] | 530 | case ISD::BRCOND: |
| 531 | return SelectBRCOND(N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 532 | } |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 533 | |
Vincent Lejeune | 0167a31 | 2013-09-12 23:45:00 +0000 | [diff] [blame] | 534 | return SelectCode(N); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 537 | bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) { |
| 538 | assert(AS != 0 && "Use checkPrivateAddress instead."); |
| 539 | if (!Ptr) |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 540 | return false; |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 541 | |
| 542 | return Ptr->getType()->getPointerAddressSpace() == AS; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 545 | bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 546 | if (Op->getPseudoValue()) |
| 547 | return true; |
| 548 | |
| 549 | if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType())) |
| 550 | return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS; |
| 551 | |
| 552 | return false; |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 555 | bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 556 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 560 | const Value *MemVal = N->getMemOperand()->getValue(); |
| 561 | return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) && |
| 562 | !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) && |
| 563 | !checkType(MemVal, AMDGPUAS::REGION_ADDRESS)); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 567 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 570 | bool AMDGPUDAGToDAGISel::isFlatStore(const StoreSDNode *N) { |
| 571 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::FLAT_ADDRESS); |
| 572 | } |
| 573 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 574 | bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 575 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Tom Stellard | 1e80309 | 2013-07-23 01:48:18 +0000 | [diff] [blame] | 578 | bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 579 | const Value *MemVal = N->getMemOperand()->getValue(); |
| 580 | if (CbId == -1) |
| 581 | return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS); |
| 582 | |
| 583 | return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 586 | bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 587 | if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) |
| 588 | if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS || |
| 589 | N->getMemoryVT().bitsLT(MVT::i32)) |
Tom Stellard | 8cb0e47 | 2013-07-23 23:54:56 +0000 | [diff] [blame] | 590 | return true; |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 591 | |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 592 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 595 | bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 596 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 599 | bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 600 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 603 | bool AMDGPUDAGToDAGISel::isFlatLoad(const LoadSDNode *N) const { |
| 604 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::FLAT_ADDRESS); |
| 605 | } |
| 606 | |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 607 | bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 608 | return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 611 | bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 612 | MachineMemOperand *MMO = N->getMemOperand(); |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 613 | if (checkPrivateAddress(N->getMemOperand())) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 614 | if (MMO) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 615 | const PseudoSourceValue *PSV = MMO->getPseudoValue(); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 616 | if (PSV && PSV->isConstantPool()) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 617 | return true; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | return false; |
| 622 | } |
| 623 | |
Matt Arsenault | 2aabb06 | 2013-06-18 23:37:58 +0000 | [diff] [blame] | 624 | bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 625 | if (checkPrivateAddress(N->getMemOperand())) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 626 | // Check to make sure we are not a constant pool load or a constant load |
| 627 | // that is marked as a private load |
| 628 | if (isCPLoad(N) || isConstantLoad(N, -1)) { |
| 629 | return false; |
| 630 | } |
| 631 | } |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 632 | |
| 633 | const Value *MemVal = N->getMemOperand()->getValue(); |
| 634 | if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) && |
| 635 | !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) && |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 636 | !checkType(MemVal, AMDGPUAS::FLAT_ADDRESS) && |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 637 | !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) && |
| 638 | !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) && |
| 639 | !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) && |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 640 | !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | return false; |
| 644 | } |
| 645 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame^] | 646 | bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const { |
| 647 | const BasicBlock *BB = FuncInfo->MBB->getBasicBlock(); |
| 648 | return BB->getTerminator()->getMetadata("amdgpu.uniform"); |
| 649 | } |
| 650 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 651 | const char *AMDGPUDAGToDAGISel::getPassName() const { |
| 652 | return "AMDGPU DAG->DAG Pattern Instruction Selection"; |
| 653 | } |
| 654 | |
| 655 | #ifdef DEBUGTMP |
| 656 | #undef INT64_C |
| 657 | #endif |
| 658 | #undef DEBUGTMP |
| 659 | |
Tom Stellard | 41fc785 | 2013-07-23 01:48:42 +0000 | [diff] [blame] | 660 | //===----------------------------------------------------------------------===// |
| 661 | // Complex Patterns |
| 662 | //===----------------------------------------------------------------------===// |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 663 | |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 664 | bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr, |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 665 | SDValue& IntPtr) { |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 666 | if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 667 | IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr), |
| 668 | true); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 669 | return true; |
| 670 | } |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr, |
| 675 | SDValue& BaseReg, SDValue &Offset) { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 676 | if (!isa<ConstantSDNode>(Addr)) { |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 677 | BaseReg = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 678 | Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 679 | return true; |
| 680 | } |
| 681 | return false; |
| 682 | } |
| 683 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 684 | bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, |
| 685 | SDValue &Offset) { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 686 | ConstantSDNode *IMMOffset; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 687 | |
| 688 | if (Addr.getOpcode() == ISD::ADD |
| 689 | && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) |
| 690 | && isInt<16>(IMMOffset->getZExtValue())) { |
| 691 | |
| 692 | Base = Addr.getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 693 | Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), |
| 694 | MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 695 | return true; |
| 696 | // If the pointer address is constant, we can move it to the offset field. |
| 697 | } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr)) |
| 698 | && isInt<16>(IMMOffset->getZExtValue())) { |
| 699 | Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 700 | SDLoc(CurDAG->getEntryNode()), |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 701 | AMDGPU::ZERO, MVT::i32); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 702 | Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), |
| 703 | MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | |
| 707 | // Default case, no offset |
| 708 | Base = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 709 | Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 713 | bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, |
| 714 | SDValue &Offset) { |
| 715 | ConstantSDNode *C; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 716 | SDLoc DL(Addr); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 717 | |
| 718 | if ((C = dyn_cast<ConstantSDNode>(Addr))) { |
| 719 | Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 720 | Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 721 | } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && |
| 722 | (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { |
| 723 | Base = Addr.getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 724 | Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 725 | } else { |
| 726 | Base = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 727 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | return true; |
| 731 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 732 | |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 733 | SDNode *AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) { |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 734 | SDLoc DL(N); |
| 735 | SDValue LHS = N->getOperand(0); |
| 736 | SDValue RHS = N->getOperand(1); |
| 737 | |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 738 | bool IsAdd = (N->getOpcode() == ISD::ADD); |
| 739 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 740 | SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); |
| 741 | SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 742 | |
| 743 | SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 744 | DL, MVT::i32, LHS, Sub0); |
| 745 | SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 746 | DL, MVT::i32, LHS, Sub1); |
| 747 | |
| 748 | SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 749 | DL, MVT::i32, RHS, Sub0); |
| 750 | SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 751 | DL, MVT::i32, RHS, Sub1); |
| 752 | |
| 753 | SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 754 | SDValue AddLoArgs[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) }; |
| 755 | |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 756 | |
Tom Stellard | 80942a1 | 2014-09-05 14:07:59 +0000 | [diff] [blame] | 757 | unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32; |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 758 | unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32; |
| 759 | |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 760 | SDNode *AddLo = CurDAG->getMachineNode( Opc, DL, VTList, AddLoArgs); |
| 761 | SDValue Carry(AddLo, 1); |
| 762 | SDNode *AddHi |
| 763 | = CurDAG->getMachineNode(CarryOpc, DL, MVT::i32, |
| 764 | SDValue(Hi0, 0), SDValue(Hi1, 0), Carry); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 765 | |
| 766 | SDValue Args[5] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 767 | CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 768 | SDValue(AddLo,0), |
| 769 | Sub0, |
| 770 | SDValue(AddHi,0), |
| 771 | Sub1, |
| 772 | }; |
| 773 | return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args); |
| 774 | } |
| 775 | |
Matt Arsenault | 044f1d1 | 2015-02-14 04:24:28 +0000 | [diff] [blame] | 776 | // We need to handle this here because tablegen doesn't support matching |
| 777 | // instructions with multiple outputs. |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 778 | SDNode *AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) { |
| 779 | SDLoc SL(N); |
| 780 | EVT VT = N->getValueType(0); |
| 781 | |
| 782 | assert(VT == MVT::f32 || VT == MVT::f64); |
| 783 | |
| 784 | unsigned Opc |
| 785 | = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32; |
| 786 | |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 787 | // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, |
| 788 | // omod |
Matt Arsenault | 044f1d1 | 2015-02-14 04:24:28 +0000 | [diff] [blame] | 789 | SDValue Ops[8]; |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 790 | |
Matt Arsenault | 044f1d1 | 2015-02-14 04:24:28 +0000 | [diff] [blame] | 791 | SelectVOP3Mods0(N->getOperand(0), Ops[1], Ops[0], Ops[6], Ops[7]); |
| 792 | SelectVOP3Mods(N->getOperand(1), Ops[3], Ops[2]); |
| 793 | SelectVOP3Mods(N->getOperand(2), Ops[5], Ops[4]); |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 794 | return CurDAG->SelectNodeTo(N, Opc, VT, MVT::i1, Ops); |
| 795 | } |
| 796 | |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 797 | bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset, |
| 798 | unsigned OffsetBits) const { |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 799 | if ((OffsetBits == 16 && !isUInt<16>(Offset)) || |
| 800 | (OffsetBits == 8 && !isUInt<8>(Offset))) |
| 801 | return false; |
| 802 | |
Matt Arsenault | 706f930 | 2015-07-06 16:01:58 +0000 | [diff] [blame] | 803 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS || |
| 804 | Subtarget->unsafeDSOffsetFoldingEnabled()) |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 805 | return true; |
| 806 | |
| 807 | // On Southern Islands instruction with a negative base value and an offset |
| 808 | // don't seem to work. |
| 809 | return CurDAG->SignBitIsZero(Base); |
| 810 | } |
| 811 | |
| 812 | bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base, |
| 813 | SDValue &Offset) const { |
| 814 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 815 | SDValue N0 = Addr.getOperand(0); |
| 816 | SDValue N1 = Addr.getOperand(1); |
| 817 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 818 | if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) { |
| 819 | // (add n0, c0) |
| 820 | Base = N0; |
| 821 | Offset = N1; |
| 822 | return true; |
| 823 | } |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 824 | } else if (Addr.getOpcode() == ISD::SUB) { |
| 825 | // sub C, x -> add (sub 0, x), C |
| 826 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { |
| 827 | int64_t ByteOffset = C->getSExtValue(); |
| 828 | if (isUInt<16>(ByteOffset)) { |
| 829 | SDLoc DL(Addr); |
| 830 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 831 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 832 | // XXX - This is kind of hacky. Create a dummy sub node so we can check |
| 833 | // the known bits in isDSOffsetLegal. We need to emit the selected node |
| 834 | // here, so this is thrown away. |
| 835 | SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, |
| 836 | Zero, Addr.getOperand(1)); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 837 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 838 | if (isDSOffsetLegal(Sub, ByteOffset, 16)) { |
| 839 | MachineSDNode *MachineSub |
| 840 | = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, |
| 841 | Zero, Addr.getOperand(1)); |
| 842 | |
| 843 | Base = SDValue(MachineSub, 0); |
| 844 | Offset = Addr.getOperand(0); |
| 845 | return true; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { |
| 850 | // If we have a constant address, prefer to put the constant into the |
| 851 | // offset. This can save moves to load the constant address since multiple |
| 852 | // operations can share the zero base address register, and enables merging |
| 853 | // into read2 / write2 instructions. |
| 854 | |
| 855 | SDLoc DL(Addr); |
| 856 | |
Matt Arsenault | e775f5f | 2014-10-14 17:21:19 +0000 | [diff] [blame] | 857 | if (isUInt<16>(CAddr->getZExtValue())) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 858 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | c8d7920 | 2014-10-15 21:08:59 +0000 | [diff] [blame] | 859 | MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 860 | DL, MVT::i32, Zero); |
Tom Stellard | c8d7920 | 2014-10-15 21:08:59 +0000 | [diff] [blame] | 861 | Base = SDValue(MovZero, 0); |
Matt Arsenault | e775f5f | 2014-10-14 17:21:19 +0000 | [diff] [blame] | 862 | Offset = Addr; |
| 863 | return true; |
| 864 | } |
| 865 | } |
| 866 | |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 867 | // default case |
| 868 | Base = Addr; |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 869 | Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 870 | return true; |
| 871 | } |
| 872 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 873 | // TODO: If offset is too big, put low 16-bit into offset. |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 874 | bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base, |
| 875 | SDValue &Offset0, |
| 876 | SDValue &Offset1) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 877 | SDLoc DL(Addr); |
| 878 | |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 879 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 880 | SDValue N0 = Addr.getOperand(0); |
| 881 | SDValue N1 = Addr.getOperand(1); |
| 882 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 883 | unsigned DWordOffset0 = C1->getZExtValue() / 4; |
| 884 | unsigned DWordOffset1 = DWordOffset0 + 1; |
| 885 | // (add n0, c0) |
| 886 | if (isDSOffsetLegal(N0, DWordOffset1, 8)) { |
| 887 | Base = N0; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 888 | Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); |
| 889 | Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 890 | return true; |
| 891 | } |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 892 | } else if (Addr.getOpcode() == ISD::SUB) { |
| 893 | // sub C, x -> add (sub 0, x), C |
| 894 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { |
| 895 | unsigned DWordOffset0 = C->getZExtValue() / 4; |
| 896 | unsigned DWordOffset1 = DWordOffset0 + 1; |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 897 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 898 | if (isUInt<8>(DWordOffset0)) { |
| 899 | SDLoc DL(Addr); |
| 900 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
| 901 | |
| 902 | // XXX - This is kind of hacky. Create a dummy sub node so we can check |
| 903 | // the known bits in isDSOffsetLegal. We need to emit the selected node |
| 904 | // here, so this is thrown away. |
| 905 | SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, |
| 906 | Zero, Addr.getOperand(1)); |
| 907 | |
| 908 | if (isDSOffsetLegal(Sub, DWordOffset1, 8)) { |
| 909 | MachineSDNode *MachineSub |
| 910 | = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, |
| 911 | Zero, Addr.getOperand(1)); |
| 912 | |
| 913 | Base = SDValue(MachineSub, 0); |
| 914 | Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); |
| 915 | Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); |
| 916 | return true; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 921 | unsigned DWordOffset0 = CAddr->getZExtValue() / 4; |
| 922 | unsigned DWordOffset1 = DWordOffset0 + 1; |
| 923 | assert(4 * DWordOffset0 == CAddr->getZExtValue()); |
| 924 | |
| 925 | if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 926 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 927 | MachineSDNode *MovZero |
| 928 | = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 929 | DL, MVT::i32, Zero); |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 930 | Base = SDValue(MovZero, 0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 931 | Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); |
| 932 | Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 933 | return true; |
| 934 | } |
| 935 | } |
| 936 | |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 937 | // default case |
| 938 | Base = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 939 | Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8); |
| 940 | Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8); |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 941 | return true; |
| 942 | } |
| 943 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 944 | static bool isLegalMUBUFImmOffset(const ConstantSDNode *Imm) { |
| 945 | return isUInt<12>(Imm->getZExtValue()); |
| 946 | } |
| 947 | |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 948 | bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr, |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 949 | SDValue &VAddr, SDValue &SOffset, |
| 950 | SDValue &Offset, SDValue &Offen, |
| 951 | SDValue &Idxen, SDValue &Addr64, |
| 952 | SDValue &GLC, SDValue &SLC, |
| 953 | SDValue &TFE) const { |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 954 | // Subtarget prefers to use flat instruction |
| 955 | if (Subtarget->useFlatForGlobal()) |
| 956 | return false; |
| 957 | |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 958 | SDLoc DL(Addr); |
| 959 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 960 | GLC = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 961 | SLC = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 962 | TFE = CurDAG->getTargetConstant(0, DL, MVT::i1); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 963 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 964 | Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 965 | Offen = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 966 | Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 967 | SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 968 | |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 969 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 970 | SDValue N0 = Addr.getOperand(0); |
| 971 | SDValue N1 = Addr.getOperand(1); |
| 972 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 973 | |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 974 | if (N0.getOpcode() == ISD::ADD) { |
| 975 | // (add (add N2, N3), C1) -> addr64 |
| 976 | SDValue N2 = N0.getOperand(0); |
| 977 | SDValue N3 = N0.getOperand(1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 978 | Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 979 | Ptr = N2; |
| 980 | VAddr = N3; |
| 981 | } else { |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 982 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 983 | // (add N0, C1) -> offset |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 984 | VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 985 | Ptr = N0; |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | if (isLegalMUBUFImmOffset(C1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 989 | Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 990 | return true; |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 991 | } else if (isUInt<32>(C1->getZExtValue())) { |
| 992 | // Illegal offset, store it in soffset. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 993 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 994 | SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 995 | CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)), |
| 996 | 0); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 997 | return true; |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 1000 | |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 1001 | if (Addr.getOpcode() == ISD::ADD) { |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1002 | // (add N0, N1) -> addr64 |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 1003 | SDValue N0 = Addr.getOperand(0); |
| 1004 | SDValue N1 = Addr.getOperand(1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1005 | Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1006 | Ptr = N0; |
| 1007 | VAddr = N1; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1008 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1009 | return true; |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1012 | // default case -> offset |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1013 | VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1014 | Ptr = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1015 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1016 | |
| 1017 | return true; |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, |
Tom Stellard | c53861a | 2015-02-11 00:34:32 +0000 | [diff] [blame] | 1021 | SDValue &VAddr, SDValue &SOffset, |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 1022 | SDValue &Offset, SDValue &GLC, |
| 1023 | SDValue &SLC, SDValue &TFE) const { |
| 1024 | SDValue Ptr, Offen, Idxen, Addr64; |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1025 | |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 1026 | // addr64 bit was removed for volcanic islands. |
| 1027 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 1028 | return false; |
| 1029 | |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1030 | if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, |
| 1031 | GLC, SLC, TFE)) |
| 1032 | return false; |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1033 | |
| 1034 | ConstantSDNode *C = cast<ConstantSDNode>(Addr64); |
| 1035 | if (C->getSExtValue()) { |
| 1036 | SDLoc DL(Addr); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 1037 | |
| 1038 | const SITargetLowering& Lowering = |
| 1039 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
| 1040 | |
| 1041 | SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1042 | return true; |
| 1043 | } |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 1044 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1045 | return false; |
| 1046 | } |
| 1047 | |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1048 | bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, |
Tom Stellard | c53861a | 2015-02-11 00:34:32 +0000 | [diff] [blame] | 1049 | SDValue &VAddr, SDValue &SOffset, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 1050 | SDValue &Offset, |
| 1051 | SDValue &SLC) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1052 | SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1); |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 1053 | SDValue GLC, TFE; |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1054 | |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 1055 | return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE); |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1058 | bool AMDGPUDAGToDAGISel::SelectMUBUFScratch(SDValue Addr, SDValue &Rsrc, |
| 1059 | SDValue &VAddr, SDValue &SOffset, |
| 1060 | SDValue &ImmOffset) const { |
| 1061 | |
| 1062 | SDLoc DL(Addr); |
| 1063 | MachineFunction &MF = CurDAG->getMachineFunction(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 1064 | const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1065 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 1066 | Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1067 | SOffset = CurDAG->getRegister(Info->getScratchWaveOffsetReg(), MVT::i32); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1068 | |
| 1069 | // (add n0, c1) |
| 1070 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
Tom Stellard | 78655fc | 2015-07-16 19:40:09 +0000 | [diff] [blame] | 1071 | SDValue N0 = Addr.getOperand(0); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1072 | SDValue N1 = Addr.getOperand(1); |
Tom Stellard | 78655fc | 2015-07-16 19:40:09 +0000 | [diff] [blame] | 1073 | // Offsets in vaddr must be positive. |
| 1074 | if (CurDAG->SignBitIsZero(N0)) { |
| 1075 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 1076 | if (isLegalMUBUFImmOffset(C1)) { |
| 1077 | VAddr = N0; |
| 1078 | ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); |
| 1079 | return true; |
| 1080 | } |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1084 | // (node) |
| 1085 | VAddr = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1086 | ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1087 | return true; |
| 1088 | } |
| 1089 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1090 | bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, |
| 1091 | SDValue &SOffset, SDValue &Offset, |
| 1092 | SDValue &GLC, SDValue &SLC, |
| 1093 | SDValue &TFE) const { |
| 1094 | SDValue Ptr, VAddr, Offen, Idxen, Addr64; |
Tom Stellard | 794c8c0 | 2014-12-02 17:05:41 +0000 | [diff] [blame] | 1095 | const SIInstrInfo *TII = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 1096 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1097 | |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1098 | if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, |
| 1099 | GLC, SLC, TFE)) |
| 1100 | return false; |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1101 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1102 | if (!cast<ConstantSDNode>(Offen)->getSExtValue() && |
| 1103 | !cast<ConstantSDNode>(Idxen)->getSExtValue() && |
| 1104 | !cast<ConstantSDNode>(Addr64)->getSExtValue()) { |
Tom Stellard | 794c8c0 | 2014-12-02 17:05:41 +0000 | [diff] [blame] | 1105 | uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1106 | APInt::getAllOnesValue(32).getZExtValue(); // Size |
| 1107 | SDLoc DL(Addr); |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 1108 | |
| 1109 | const SITargetLowering& Lowering = |
| 1110 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
| 1111 | |
| 1112 | SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1113 | return true; |
| 1114 | } |
| 1115 | return false; |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1118 | bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, |
| 1119 | SDValue &Soffset, SDValue &Offset, |
| 1120 | SDValue &GLC) const { |
| 1121 | SDValue SLC, TFE; |
| 1122 | |
| 1123 | return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); |
| 1124 | } |
| 1125 | |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1126 | /// |
| 1127 | /// \param EncodedOffset This is the immediate value that will be encoded |
| 1128 | /// directly into the instruction. On SI/CI the \p EncodedOffset |
| 1129 | /// will be in units of dwords and on VI+ it will be units of bytes. |
| 1130 | static bool isLegalSMRDImmOffset(const AMDGPUSubtarget *ST, |
| 1131 | int64_t EncodedOffset) { |
| 1132 | return ST->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS ? |
| 1133 | isUInt<8>(EncodedOffset) : isUInt<20>(EncodedOffset); |
| 1134 | } |
| 1135 | |
| 1136 | bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode, |
| 1137 | SDValue &Offset, bool &Imm) const { |
| 1138 | |
| 1139 | // FIXME: Handle non-constant offsets. |
| 1140 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode); |
| 1141 | if (!C) |
| 1142 | return false; |
| 1143 | |
| 1144 | SDLoc SL(ByteOffsetNode); |
| 1145 | AMDGPUSubtarget::Generation Gen = Subtarget->getGeneration(); |
| 1146 | int64_t ByteOffset = C->getSExtValue(); |
| 1147 | int64_t EncodedOffset = Gen < AMDGPUSubtarget::VOLCANIC_ISLANDS ? |
| 1148 | ByteOffset >> 2 : ByteOffset; |
| 1149 | |
| 1150 | if (isLegalSMRDImmOffset(Subtarget, EncodedOffset)) { |
| 1151 | Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); |
| 1152 | Imm = true; |
| 1153 | return true; |
| 1154 | } |
| 1155 | |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1156 | if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset)) |
| 1157 | return false; |
| 1158 | |
| 1159 | if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) { |
| 1160 | // 32-bit Immediates are supported on Sea Islands. |
| 1161 | Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); |
| 1162 | } else { |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1163 | SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32); |
| 1164 | Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, |
| 1165 | C32Bit), 0); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1166 | } |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1167 | Imm = false; |
| 1168 | return true; |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase, |
| 1172 | SDValue &Offset, bool &Imm) const { |
| 1173 | |
| 1174 | SDLoc SL(Addr); |
| 1175 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 1176 | SDValue N0 = Addr.getOperand(0); |
| 1177 | SDValue N1 = Addr.getOperand(1); |
| 1178 | |
| 1179 | if (SelectSMRDOffset(N1, Offset, Imm)) { |
| 1180 | SBase = N0; |
| 1181 | return true; |
| 1182 | } |
| 1183 | } |
| 1184 | SBase = Addr; |
| 1185 | Offset = CurDAG->getTargetConstant(0, SL, MVT::i32); |
| 1186 | Imm = true; |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
| 1190 | bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase, |
| 1191 | SDValue &Offset) const { |
| 1192 | bool Imm; |
| 1193 | return SelectSMRD(Addr, SBase, Offset, Imm) && Imm; |
| 1194 | } |
| 1195 | |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1196 | bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase, |
| 1197 | SDValue &Offset) const { |
| 1198 | |
| 1199 | if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) |
| 1200 | return false; |
| 1201 | |
| 1202 | bool Imm; |
| 1203 | if (!SelectSMRD(Addr, SBase, Offset, Imm)) |
| 1204 | return false; |
| 1205 | |
| 1206 | return !Imm && isa<ConstantSDNode>(Offset); |
| 1207 | } |
| 1208 | |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1209 | bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase, |
| 1210 | SDValue &Offset) const { |
| 1211 | bool Imm; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1212 | return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm && |
| 1213 | !isa<ConstantSDNode>(Offset); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr, |
| 1217 | SDValue &Offset) const { |
| 1218 | bool Imm; |
| 1219 | return SelectSMRDOffset(Addr, Offset, Imm) && Imm; |
| 1220 | } |
| 1221 | |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1222 | bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr, |
| 1223 | SDValue &Offset) const { |
| 1224 | if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) |
| 1225 | return false; |
| 1226 | |
| 1227 | bool Imm; |
| 1228 | if (!SelectSMRDOffset(Addr, Offset, Imm)) |
| 1229 | return false; |
| 1230 | |
| 1231 | return !Imm && isa<ConstantSDNode>(Offset); |
| 1232 | } |
| 1233 | |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1234 | bool AMDGPUDAGToDAGISel::SelectSMRDBufferSgpr(SDValue Addr, |
| 1235 | SDValue &Offset) const { |
| 1236 | bool Imm; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1237 | return SelectSMRDOffset(Addr, Offset, Imm) && !Imm && |
| 1238 | !isa<ConstantSDNode>(Offset); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1241 | // FIXME: This is incorrect and only enough to be able to compile. |
| 1242 | SDNode *AMDGPUDAGToDAGISel::SelectAddrSpaceCast(SDNode *N) { |
| 1243 | AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(N); |
| 1244 | SDLoc DL(N); |
| 1245 | |
Matt Arsenault | 592d068 | 2015-12-01 23:04:05 +0000 | [diff] [blame] | 1246 | const MachineFunction &MF = CurDAG->getMachineFunction(); |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 1247 | DiagnosticInfoUnsupported NotImplemented( |
| 1248 | *MF.getFunction(), "addrspacecast not implemented", DL.getDebugLoc()); |
Matt Arsenault | 592d068 | 2015-12-01 23:04:05 +0000 | [diff] [blame] | 1249 | CurDAG->getContext()->diagnose(NotImplemented); |
| 1250 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 1251 | assert(Subtarget->hasFlatAddressSpace() && |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1252 | "addrspacecast only supported with flat address space!"); |
| 1253 | |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1254 | assert((ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS || |
| 1255 | ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) && |
| 1256 | "Can only cast to / from flat address space!"); |
| 1257 | |
| 1258 | // The flat instructions read the address as the index of the VGPR holding the |
| 1259 | // address, so casting should just be reinterpreting the base VGPR, so just |
| 1260 | // insert trunc / bitcast / zext. |
| 1261 | |
| 1262 | SDValue Src = ASC->getOperand(0); |
| 1263 | EVT DestVT = ASC->getValueType(0); |
| 1264 | EVT SrcVT = Src.getValueType(); |
| 1265 | |
| 1266 | unsigned SrcSize = SrcVT.getSizeInBits(); |
| 1267 | unsigned DestSize = DestVT.getSizeInBits(); |
| 1268 | |
| 1269 | if (SrcSize > DestSize) { |
| 1270 | assert(SrcSize == 64 && DestSize == 32); |
| 1271 | return CurDAG->getMachineNode( |
| 1272 | TargetOpcode::EXTRACT_SUBREG, |
| 1273 | DL, |
| 1274 | DestVT, |
| 1275 | Src, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1276 | CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32)); |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1279 | if (DestSize > SrcSize) { |
| 1280 | assert(SrcSize == 32 && DestSize == 64); |
| 1281 | |
Tom Stellard | b655052 | 2015-01-12 19:33:18 +0000 | [diff] [blame] | 1282 | // FIXME: This is probably wrong, we should never be defining |
| 1283 | // a register class with both VGPRs and SGPRs |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1284 | SDValue RC = CurDAG->getTargetConstant(AMDGPU::VS_64RegClassID, DL, |
| 1285 | MVT::i32); |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1286 | |
| 1287 | const SDValue Ops[] = { |
| 1288 | RC, |
| 1289 | Src, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1290 | CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
| 1291 | SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
| 1292 | CurDAG->getConstant(0, DL, MVT::i32)), 0), |
| 1293 | CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32) |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1294 | }; |
| 1295 | |
| 1296 | return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1297 | DL, N->getValueType(0), Ops); |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | assert(SrcSize == 64 && DestSize == 64); |
| 1301 | return CurDAG->getNode(ISD::BITCAST, DL, DestVT, Src).getNode(); |
| 1302 | } |
| 1303 | |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1304 | SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, SDLoc DL, SDValue Val, |
| 1305 | uint32_t Offset, uint32_t Width) { |
| 1306 | // Transformation function, pack the offset and width of a BFE into |
| 1307 | // the format expected by the S_BFE_I32 / S_BFE_U32. In the second |
| 1308 | // source, bits [5:0] contain the offset and bits [22:16] the width. |
| 1309 | uint32_t PackedVal = Offset | (Width << 16); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1310 | SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32); |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1311 | |
| 1312 | return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst); |
| 1313 | } |
| 1314 | |
| 1315 | SDNode *AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) { |
| 1316 | // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c) |
| 1317 | // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c) |
| 1318 | // Predicate: 0 < b <= c < 32 |
| 1319 | |
| 1320 | const SDValue &Shl = N->getOperand(0); |
| 1321 | ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1)); |
| 1322 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 1323 | |
| 1324 | if (B && C) { |
| 1325 | uint32_t BVal = B->getZExtValue(); |
| 1326 | uint32_t CVal = C->getZExtValue(); |
| 1327 | |
| 1328 | if (0 < BVal && BVal <= CVal && CVal < 32) { |
| 1329 | bool Signed = N->getOpcode() == ISD::SRA; |
| 1330 | unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32; |
| 1331 | |
| 1332 | return getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), |
| 1333 | CVal - BVal, 32 - CVal); |
| 1334 | } |
| 1335 | } |
| 1336 | return SelectCode(N); |
| 1337 | } |
| 1338 | |
| 1339 | SDNode *AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) { |
| 1340 | switch (N->getOpcode()) { |
| 1341 | case ISD::AND: |
| 1342 | if (N->getOperand(0).getOpcode() == ISD::SRL) { |
| 1343 | // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)" |
| 1344 | // Predicate: isMask(mask) |
| 1345 | const SDValue &Srl = N->getOperand(0); |
| 1346 | ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1)); |
| 1347 | ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 1348 | |
| 1349 | if (Shift && Mask) { |
| 1350 | uint32_t ShiftVal = Shift->getZExtValue(); |
| 1351 | uint32_t MaskVal = Mask->getZExtValue(); |
| 1352 | |
| 1353 | if (isMask_32(MaskVal)) { |
| 1354 | uint32_t WidthVal = countPopulation(MaskVal); |
| 1355 | |
| 1356 | return getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), Srl.getOperand(0), |
| 1357 | ShiftVal, WidthVal); |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | break; |
| 1362 | case ISD::SRL: |
| 1363 | if (N->getOperand(0).getOpcode() == ISD::AND) { |
| 1364 | // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)" |
| 1365 | // Predicate: isMask(mask >> b) |
| 1366 | const SDValue &And = N->getOperand(0); |
| 1367 | ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 1368 | ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1)); |
| 1369 | |
| 1370 | if (Shift && Mask) { |
| 1371 | uint32_t ShiftVal = Shift->getZExtValue(); |
| 1372 | uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal; |
| 1373 | |
| 1374 | if (isMask_32(MaskVal)) { |
| 1375 | uint32_t WidthVal = countPopulation(MaskVal); |
| 1376 | |
| 1377 | return getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), And.getOperand(0), |
| 1378 | ShiftVal, WidthVal); |
| 1379 | } |
| 1380 | } |
| 1381 | } else if (N->getOperand(0).getOpcode() == ISD::SHL) |
| 1382 | return SelectS_BFEFromShifts(N); |
| 1383 | break; |
| 1384 | case ISD::SRA: |
| 1385 | if (N->getOperand(0).getOpcode() == ISD::SHL) |
| 1386 | return SelectS_BFEFromShifts(N); |
| 1387 | break; |
| 1388 | } |
| 1389 | |
| 1390 | return SelectCode(N); |
| 1391 | } |
| 1392 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame^] | 1393 | SDNode *AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) { |
| 1394 | SDValue Cond = N->getOperand(1); |
| 1395 | |
| 1396 | if (isCBranchSCC(N)) { |
| 1397 | // This brcond will use S_CBRANCH_SCC*, so let tablegen handle it. |
| 1398 | return SelectCode(N); |
| 1399 | } |
| 1400 | |
| 1401 | // The result of VOPC instructions is or'd against ~EXEC before it is |
| 1402 | // written to vcc or another SGPR. This means that the value '1' is always |
| 1403 | // written to the corresponding bit for results that are masked. In order |
| 1404 | // to correctly check against vccz, we need to and VCC with the EXEC |
| 1405 | // register in order to clear the value from the masked bits. |
| 1406 | |
| 1407 | SDLoc SL(N); |
| 1408 | |
| 1409 | SDNode *MaskedCond = |
| 1410 | CurDAG->getMachineNode(AMDGPU::S_AND_B64, SL, MVT::i1, |
| 1411 | CurDAG->getRegister(AMDGPU::EXEC, MVT::i1), |
| 1412 | Cond); |
| 1413 | SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, AMDGPU::VCC, |
| 1414 | SDValue(MaskedCond, 0), |
| 1415 | SDValue()); // Passing SDValue() adds a |
| 1416 | // glue output. |
| 1417 | return CurDAG->SelectNodeTo(N, AMDGPU::S_CBRANCH_VCCNZ, MVT::Other, |
| 1418 | N->getOperand(2), // Basic Block |
| 1419 | VCC.getValue(0), // Chain |
| 1420 | VCC.getValue(1)); // Glue |
| 1421 | } |
| 1422 | |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1423 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src, |
| 1424 | SDValue &SrcMods) const { |
| 1425 | |
| 1426 | unsigned Mods = 0; |
| 1427 | |
| 1428 | Src = In; |
| 1429 | |
| 1430 | if (Src.getOpcode() == ISD::FNEG) { |
| 1431 | Mods |= SISrcMods::NEG; |
| 1432 | Src = Src.getOperand(0); |
| 1433 | } |
| 1434 | |
| 1435 | if (Src.getOpcode() == ISD::FABS) { |
| 1436 | Mods |= SISrcMods::ABS; |
| 1437 | Src = Src.getOperand(0); |
| 1438 | } |
| 1439 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1440 | SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1441 | |
| 1442 | return true; |
| 1443 | } |
| 1444 | |
Tom Stellard | db5a11f | 2015-07-13 15:47:57 +0000 | [diff] [blame] | 1445 | bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src, |
| 1446 | SDValue &SrcMods) const { |
| 1447 | bool Res = SelectVOP3Mods(In, Src, SrcMods); |
| 1448 | return Res && cast<ConstantSDNode>(SrcMods)->isNullValue(); |
| 1449 | } |
| 1450 | |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1451 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src, |
| 1452 | SDValue &SrcMods, SDValue &Clamp, |
| 1453 | SDValue &Omod) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1454 | SDLoc DL(In); |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1455 | // FIXME: Handle Clamp and Omod |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1456 | Clamp = CurDAG->getTargetConstant(0, DL, MVT::i32); |
| 1457 | Omod = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1458 | |
| 1459 | return SelectVOP3Mods(In, Src, SrcMods); |
| 1460 | } |
| 1461 | |
Tom Stellard | db5a11f | 2015-07-13 15:47:57 +0000 | [diff] [blame] | 1462 | bool AMDGPUDAGToDAGISel::SelectVOP3NoMods0(SDValue In, SDValue &Src, |
| 1463 | SDValue &SrcMods, SDValue &Clamp, |
| 1464 | SDValue &Omod) const { |
| 1465 | bool Res = SelectVOP3Mods0(In, Src, SrcMods, Clamp, Omod); |
| 1466 | |
| 1467 | return Res && cast<ConstantSDNode>(SrcMods)->isNullValue() && |
| 1468 | cast<ConstantSDNode>(Clamp)->isNullValue() && |
| 1469 | cast<ConstantSDNode>(Omod)->isNullValue(); |
| 1470 | } |
| 1471 | |
Matt Arsenault | 1cffa4c | 2014-11-13 19:49:04 +0000 | [diff] [blame] | 1472 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp(SDValue In, SDValue &Src, |
| 1473 | SDValue &SrcMods, |
| 1474 | SDValue &Omod) const { |
| 1475 | // FIXME: Handle Omod |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1476 | Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); |
Matt Arsenault | 1cffa4c | 2014-11-13 19:49:04 +0000 | [diff] [blame] | 1477 | |
| 1478 | return SelectVOP3Mods(In, Src, SrcMods); |
| 1479 | } |
| 1480 | |
Matt Arsenault | 4831ce5 | 2015-01-06 23:00:37 +0000 | [diff] [blame] | 1481 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, |
| 1482 | SDValue &SrcMods, |
| 1483 | SDValue &Clamp, |
| 1484 | SDValue &Omod) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1485 | Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); |
Matt Arsenault | 4831ce5 | 2015-01-06 23:00:37 +0000 | [diff] [blame] | 1486 | return SelectVOP3Mods(In, Src, SrcMods); |
| 1487 | } |
| 1488 | |
Matt Arsenault | 4bf43d4 | 2015-09-25 17:27:08 +0000 | [diff] [blame] | 1489 | void AMDGPUDAGToDAGISel::PreprocessISelDAG() { |
| 1490 | bool Modified = false; |
| 1491 | |
| 1492 | // XXX - Other targets seem to be able to do this without a worklist. |
| 1493 | SmallVector<LoadSDNode *, 8> LoadsToReplace; |
| 1494 | SmallVector<StoreSDNode *, 8> StoresToReplace; |
| 1495 | |
| 1496 | for (SDNode &Node : CurDAG->allnodes()) { |
| 1497 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(&Node)) { |
| 1498 | EVT VT = LD->getValueType(0); |
| 1499 | if (VT != MVT::i64 || LD->getExtensionType() != ISD::NON_EXTLOAD) |
| 1500 | continue; |
| 1501 | |
| 1502 | // To simplify the TableGen patters, we replace all i64 loads with v2i32 |
| 1503 | // loads. Alternatively, we could promote i64 loads to v2i32 during DAG |
| 1504 | // legalization, however, so places (ExpandUnalignedLoad) in the DAG |
| 1505 | // legalizer assume that if i64 is legal, so doing this promotion early |
| 1506 | // can cause problems. |
| 1507 | LoadsToReplace.push_back(LD); |
| 1508 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(&Node)) { |
| 1509 | // Handle i64 stores here for the same reason mentioned above for loads. |
| 1510 | SDValue Value = ST->getValue(); |
| 1511 | if (Value.getValueType() != MVT::i64 || ST->isTruncatingStore()) |
| 1512 | continue; |
| 1513 | StoresToReplace.push_back(ST); |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | for (LoadSDNode *LD : LoadsToReplace) { |
| 1518 | SDLoc SL(LD); |
| 1519 | |
| 1520 | SDValue NewLoad = CurDAG->getLoad(MVT::v2i32, SL, LD->getChain(), |
| 1521 | LD->getBasePtr(), LD->getMemOperand()); |
| 1522 | SDValue BitCast = CurDAG->getNode(ISD::BITCAST, SL, |
| 1523 | MVT::i64, NewLoad); |
| 1524 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLoad.getValue(1)); |
| 1525 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(LD, 0), BitCast); |
| 1526 | Modified = true; |
| 1527 | } |
| 1528 | |
| 1529 | for (StoreSDNode *ST : StoresToReplace) { |
| 1530 | SDValue NewValue = CurDAG->getNode(ISD::BITCAST, SDLoc(ST), |
| 1531 | MVT::v2i32, ST->getValue()); |
| 1532 | const SDValue StoreOps[] = { |
| 1533 | ST->getChain(), |
| 1534 | NewValue, |
| 1535 | ST->getBasePtr(), |
| 1536 | ST->getOffset() |
| 1537 | }; |
| 1538 | |
| 1539 | CurDAG->UpdateNodeOperands(ST, StoreOps); |
| 1540 | Modified = true; |
| 1541 | } |
| 1542 | |
| 1543 | // XXX - Is this necessary? |
| 1544 | if (Modified) |
| 1545 | CurDAG->RemoveDeadNodes(); |
| 1546 | } |
| 1547 | |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1548 | void AMDGPUDAGToDAGISel::PostprocessISelDAG() { |
Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1549 | const AMDGPUTargetLowering& Lowering = |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 1550 | *static_cast<const AMDGPUTargetLowering*>(getTargetLowering()); |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1551 | bool IsModified = false; |
| 1552 | do { |
| 1553 | IsModified = false; |
| 1554 | // Go over all selected nodes and try to fold them a bit more |
Pete Cooper | 65c6940 | 2015-07-14 22:10:54 +0000 | [diff] [blame] | 1555 | for (SDNode &Node : CurDAG->allnodes()) { |
| 1556 | MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node); |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1557 | if (!MachineNode) |
| 1558 | continue; |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1559 | |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1560 | SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG); |
Pete Cooper | 65c6940 | 2015-07-14 22:10:54 +0000 | [diff] [blame] | 1561 | if (ResNode != &Node) { |
| 1562 | ReplaceUses(&Node, ResNode); |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1563 | IsModified = true; |
| 1564 | } |
Tom Stellard | 2183b70 | 2013-06-03 17:39:46 +0000 | [diff] [blame] | 1565 | } |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1566 | CurDAG->RemoveDeadNodes(); |
| 1567 | } while (IsModified); |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1568 | } |