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 | |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 15 | #include "AMDGPU.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "AMDGPUISelLowering.h" // For AMDGPUISD |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 17 | #include "AMDGPUInstrInfo.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 18 | #include "AMDGPURegisterInfo.h" |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 19 | #include "AMDGPUSubtarget.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 20 | #include "SIDefines.h" |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 21 | #include "SIISelLowering.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 22 | #include "SIInstrInfo.h" |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 23 | #include "SIMachineFunctionInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 24 | #include "SIRegisterInfo.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/APInt.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
| 27 | #include "llvm/ADT/StringRef.h" |
Jan Vesely | f97de00 | 2016-05-13 20:39:29 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ValueTracking.h" |
Tom Stellard | 58ac744 | 2014-04-29 23:12:48 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/ISDOpcodes.h" |
| 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 33 | #include "llvm/CodeGen/MachineValueType.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/SelectionDAG.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
| 37 | #include "llvm/CodeGen/ValueTypes.h" |
| 38 | #include "llvm/IR/BasicBlock.h" |
| 39 | #include "llvm/IR/Instruction.h" |
| 40 | #include "llvm/MC/MCInstrDesc.h" |
| 41 | #include "llvm/Support/Casting.h" |
| 42 | #include "llvm/Support/CodeGen.h" |
| 43 | #include "llvm/Support/ErrorHandling.h" |
| 44 | #include "llvm/Support/MathExtras.h" |
| 45 | #include <cassert> |
| 46 | #include <cstdint> |
| 47 | #include <new> |
| 48 | #include <vector> |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 49 | |
| 50 | using namespace llvm; |
| 51 | |
Matt Arsenault | d275921 | 2016-02-13 01:24:08 +0000 | [diff] [blame] | 52 | namespace llvm { |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 53 | |
Matt Arsenault | d275921 | 2016-02-13 01:24:08 +0000 | [diff] [blame] | 54 | class R600InstrInfo; |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 55 | |
| 56 | } // end namespace llvm |
Matt Arsenault | d275921 | 2016-02-13 01:24:08 +0000 | [diff] [blame] | 57 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
| 59 | // Instruction Selector Implementation |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | namespace { |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 63 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 64 | /// AMDGPU specific code to select AMDGPU machine instructions for |
| 65 | /// SelectionDAG operations. |
| 66 | class AMDGPUDAGToDAGISel : public SelectionDAGISel { |
| 67 | // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can |
| 68 | // make the right decision when generating code for different targets. |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 69 | const AMDGPUSubtarget *Subtarget; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 70 | AMDGPUAS AMDGPUASI; |
NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 71 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 72 | public: |
Konstantin Zhuravlyov | 60a8373 | 2016-10-03 18:47:26 +0000 | [diff] [blame] | 73 | explicit AMDGPUDAGToDAGISel(TargetMachine &TM, CodeGenOpt::Level OptLevel) |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 74 | : SelectionDAGISel(TM, OptLevel){ |
| 75 | AMDGPUASI = AMDGPU::getAMDGPUAS(TM); |
| 76 | } |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 77 | ~AMDGPUDAGToDAGISel() override = default; |
Konstantin Zhuravlyov | 60a8373 | 2016-10-03 18:47:26 +0000 | [diff] [blame] | 78 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 79 | bool runOnMachineFunction(MachineFunction &MF) override; |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 80 | void Select(SDNode *N) override; |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 81 | StringRef getPassName() const override; |
Craig Topper | 5656db4 | 2014-04-29 07:57:24 +0000 | [diff] [blame] | 82 | void PostprocessISelDAG() override; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 85 | std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const; |
Matt Arsenault | f84e5d9 | 2017-01-31 03:07:46 +0000 | [diff] [blame] | 86 | bool isNoNanSrc(SDValue N) const; |
Matt Arsenault | fe26775 | 2016-07-28 00:32:02 +0000 | [diff] [blame] | 87 | bool isInlineImmediate(const SDNode *N) const; |
Vincent Lejeune | c689679 | 2013-06-04 23:17:15 +0000 | [diff] [blame] | 88 | bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs, |
Tom Stellard | 8402144 | 2013-07-23 01:48:24 +0000 | [diff] [blame] | 89 | const R600InstrInfo *TII); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 90 | bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); |
Vincent Lejeune | c689679 | 2013-06-04 23:17:15 +0000 | [diff] [blame] | 91 | bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 92 | |
Jan Vesely | 43b7b5b | 2016-04-07 19:23:11 +0000 | [diff] [blame] | 93 | bool isConstantLoad(const MemSDNode *N, int cbID) const; |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 94 | bool isUniformBr(const SDNode *N) const; |
| 95 | |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 96 | SDNode *glueCopyToM0(SDNode *N) const; |
| 97 | |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 98 | const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const; |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 99 | bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr); |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 100 | bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg, |
| 101 | SDValue& Offset); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 102 | bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 103 | bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 104 | bool isDSOffsetLegal(const SDValue &Base, unsigned Offset, |
| 105 | unsigned OffsetBits) const; |
| 106 | bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const; |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 107 | bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0, |
| 108 | SDValue &Offset1) const; |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 109 | bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 110 | SDValue &SOffset, SDValue &Offset, SDValue &Offen, |
| 111 | SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC, |
| 112 | SDValue &TFE) const; |
| 113 | bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 114 | SDValue &SOffset, SDValue &Offset, SDValue &GLC, |
| 115 | SDValue &SLC, SDValue &TFE) const; |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 116 | bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, |
Tom Stellard | c53861a | 2015-02-11 00:34:32 +0000 | [diff] [blame] | 117 | SDValue &VAddr, SDValue &SOffset, SDValue &Offset, |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 118 | SDValue &SLC) const; |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 119 | bool SelectMUBUFScratchOffen(SDNode *Root, |
| 120 | SDValue Addr, SDValue &RSrc, SDValue &VAddr, |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 121 | SDValue &SOffset, SDValue &ImmOffset) const; |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 122 | bool SelectMUBUFScratchOffset(SDNode *Root, |
| 123 | SDValue Addr, SDValue &SRsrc, SDValue &Soffset, |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 124 | SDValue &Offset) const; |
| 125 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 126 | bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset, |
| 127 | SDValue &Offset, SDValue &GLC, SDValue &SLC, |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 128 | SDValue &TFE) const; |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 129 | bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 130 | SDValue &Offset, SDValue &SLC) const; |
Jan Vesely | 43b7b5b | 2016-04-07 19:23:11 +0000 | [diff] [blame] | 131 | bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, |
| 132 | SDValue &Offset) const; |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 133 | bool SelectMUBUFConstant(SDValue Constant, |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 134 | SDValue &SOffset, |
| 135 | SDValue &ImmOffset) const; |
| 136 | bool SelectMUBUFIntrinsicOffset(SDValue Offset, SDValue &SOffset, |
| 137 | SDValue &ImmOffset) const; |
| 138 | bool SelectMUBUFIntrinsicVOffset(SDValue Offset, SDValue &SOffset, |
| 139 | SDValue &ImmOffset, SDValue &VOffset) const; |
Matt Arsenault | 7757c59 | 2016-06-09 23:42:54 +0000 | [diff] [blame] | 140 | |
Matt Arsenault | db7c6a8 | 2017-06-12 16:53:51 +0000 | [diff] [blame] | 141 | bool SelectFlatAtomic(SDValue Addr, SDValue &VAddr, |
| 142 | SDValue &Offset, SDValue &SLC) const; |
| 143 | bool SelectFlatOffset(SDValue Addr, SDValue &VAddr, |
| 144 | SDValue &Offset, SDValue &SLC) const; |
Matt Arsenault | 7757c59 | 2016-06-09 23:42:54 +0000 | [diff] [blame] | 145 | |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 146 | bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset, |
| 147 | bool &Imm) const; |
| 148 | bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset, |
| 149 | bool &Imm) const; |
| 150 | bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const; |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 151 | bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const; |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 152 | bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const; |
| 153 | bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const; |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 154 | bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const; |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 155 | bool SelectSMRDBufferSgpr(SDValue Addr, SDValue &Offset) const; |
Nicolai Haehnle | 7968c34 | 2016-07-12 08:12:16 +0000 | [diff] [blame] | 156 | bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const; |
Matt Arsenault | f84e5d9 | 2017-01-31 03:07:46 +0000 | [diff] [blame] | 157 | |
| 158 | bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const; |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 159 | bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const; |
Matt Arsenault | df58e82 | 2017-04-25 21:17:38 +0000 | [diff] [blame] | 160 | bool SelectVOP3NoMods(SDValue In, SDValue &Src) const; |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 161 | bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 162 | SDValue &Clamp, SDValue &Omod) const; |
Tom Stellard | db5a11f | 2015-07-13 15:47:57 +0000 | [diff] [blame] | 163 | bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 164 | SDValue &Clamp, SDValue &Omod) const; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 165 | |
Matt Arsenault | 4831ce5 | 2015-01-06 23:00:37 +0000 | [diff] [blame] | 166 | bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 167 | SDValue &Clamp, |
| 168 | SDValue &Omod) const; |
Matt Arsenault | 1cffa4c | 2014-11-13 19:49:04 +0000 | [diff] [blame] | 169 | |
Dmitry Preobrazhensky | c512d44 | 2017-03-27 15:57:17 +0000 | [diff] [blame] | 170 | bool SelectVOP3OMods(SDValue In, SDValue &Src, |
| 171 | SDValue &Clamp, SDValue &Omod) const; |
| 172 | |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 173 | bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; |
| 174 | bool SelectVOP3PMods0(SDValue In, SDValue &Src, SDValue &SrcMods, |
| 175 | SDValue &Clamp) const; |
| 176 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 177 | void SelectADD_SUB_I64(SDNode *N); |
Matt Arsenault | ee3f0ac | 2017-01-30 18:11:38 +0000 | [diff] [blame] | 178 | void SelectUADDO_USUBO(SDNode *N); |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 179 | void SelectDIV_SCALE(SDNode *N); |
Tom Stellard | 8485fa0 | 2016-12-07 02:42:15 +0000 | [diff] [blame] | 180 | void SelectFMA_W_CHAIN(SDNode *N); |
| 181 | void SelectFMUL_W_CHAIN(SDNode *N); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 182 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 183 | SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val, |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 184 | uint32_t Offset, uint32_t Width); |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 185 | void SelectS_BFEFromShifts(SDNode *N); |
| 186 | void SelectS_BFE(SDNode *N); |
Matt Arsenault | 7b1dc2c | 2016-09-17 02:02:19 +0000 | [diff] [blame] | 187 | bool isCBranchSCC(const SDNode *N) const; |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 188 | void SelectBRCOND(SDNode *N); |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 189 | void SelectATOMIC_CMP_SWAP(SDNode *N); |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 190 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 191 | // Include the pieces autogenerated from the target description. |
| 192 | #include "AMDGPUGenDAGISel.inc" |
| 193 | }; |
Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 194 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 195 | } // end anonymous namespace |
| 196 | |
| 197 | /// \brief This pass converts a legalized DAG into a AMDGPU-specific |
| 198 | // DAG, ready for instruction scheduling. |
Konstantin Zhuravlyov | 60a8373 | 2016-10-03 18:47:26 +0000 | [diff] [blame] | 199 | FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM, |
| 200 | CodeGenOpt::Level OptLevel) { |
| 201 | return new AMDGPUDAGToDAGISel(TM, OptLevel); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 204 | bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 205 | Subtarget = &MF.getSubtarget<AMDGPUSubtarget>(); |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 206 | return SelectionDAGISel::runOnMachineFunction(MF); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Matt Arsenault | f84e5d9 | 2017-01-31 03:07:46 +0000 | [diff] [blame] | 209 | bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const { |
| 210 | if (TM.Options.NoNaNsFPMath) |
| 211 | return true; |
| 212 | |
| 213 | // TODO: Move into isKnownNeverNaN |
Amara Emerson | d28f0cd4 | 2017-05-01 15:17:51 +0000 | [diff] [blame] | 214 | if (N->getFlags().isDefined()) |
| 215 | return N->getFlags().hasNoNaNs(); |
Matt Arsenault | f84e5d9 | 2017-01-31 03:07:46 +0000 | [diff] [blame] | 216 | |
| 217 | return CurDAG->isKnownNeverNaN(N); |
| 218 | } |
| 219 | |
Matt Arsenault | fe26775 | 2016-07-28 00:32:02 +0000 | [diff] [blame] | 220 | bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N) const { |
| 221 | const SIInstrInfo *TII |
| 222 | = static_cast<const SISubtarget *>(Subtarget)->getInstrInfo(); |
| 223 | |
| 224 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) |
| 225 | return TII->isInlineConstant(C->getAPIntValue()); |
| 226 | |
| 227 | if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) |
| 228 | return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt()); |
| 229 | |
| 230 | return false; |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 233 | /// \brief Determine the register class for \p OpNo |
| 234 | /// \returns The register class of the virtual register that will be used for |
| 235 | /// the given operand number \OpNo or NULL if the register class cannot be |
| 236 | /// determined. |
| 237 | const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N, |
| 238 | unsigned OpNo) const { |
Matt Arsenault | c507cdb | 2016-11-01 23:22:17 +0000 | [diff] [blame] | 239 | if (!N->isMachineOpcode()) { |
| 240 | if (N->getOpcode() == ISD::CopyToReg) { |
| 241 | unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); |
| 242 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 243 | MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo(); |
| 244 | return MRI.getRegClass(Reg); |
| 245 | } |
| 246 | |
| 247 | const SIRegisterInfo *TRI |
| 248 | = static_cast<const SISubtarget *>(Subtarget)->getRegisterInfo(); |
| 249 | return TRI->getPhysRegClass(Reg); |
| 250 | } |
| 251 | |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 252 | return nullptr; |
Matt Arsenault | c507cdb | 2016-11-01 23:22:17 +0000 | [diff] [blame] | 253 | } |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 254 | |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 255 | switch (N->getMachineOpcode()) { |
| 256 | default: { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 257 | const MCInstrDesc &Desc = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 258 | Subtarget->getInstrInfo()->get(N->getMachineOpcode()); |
Alexey Samsonov | 3186eb3 | 2013-08-15 07:11:34 +0000 | [diff] [blame] | 259 | unsigned OpIdx = Desc.getNumDefs() + OpNo; |
| 260 | if (OpIdx >= Desc.getNumOperands()) |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 261 | return nullptr; |
Alexey Samsonov | 3186eb3 | 2013-08-15 07:11:34 +0000 | [diff] [blame] | 262 | int RegClass = Desc.OpInfo[OpIdx].RegClass; |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 263 | if (RegClass == -1) |
| 264 | return nullptr; |
| 265 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 266 | return Subtarget->getRegisterInfo()->getRegClass(RegClass); |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 267 | } |
| 268 | case AMDGPU::REG_SEQUENCE: { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 269 | unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 270 | const TargetRegisterClass *SuperRC = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 271 | Subtarget->getRegisterInfo()->getRegClass(RCID); |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 272 | |
| 273 | SDValue SubRegOp = N->getOperand(OpNo + 1); |
| 274 | unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue(); |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 275 | return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC, |
| 276 | SubRegIdx); |
Tom Stellard | df94dc3 | 2013-08-14 23:24:24 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 281 | SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N) const { |
| 282 | if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS || |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 283 | cast<MemSDNode>(N)->getAddressSpace() != AMDGPUASI.LOCAL_ADDRESS) |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 284 | return N; |
| 285 | |
| 286 | const SITargetLowering& Lowering = |
| 287 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
| 288 | |
| 289 | // Write max value to m0 before each load operation |
| 290 | |
| 291 | SDValue M0 = Lowering.copyToM0(*CurDAG, CurDAG->getEntryNode(), SDLoc(N), |
| 292 | CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32)); |
| 293 | |
| 294 | SDValue Glue = M0.getValue(1); |
| 295 | |
| 296 | SmallVector <SDValue, 8> Ops; |
| 297 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 298 | Ops.push_back(N->getOperand(i)); |
| 299 | } |
| 300 | Ops.push_back(Glue); |
| 301 | CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops); |
| 302 | |
| 303 | return N; |
| 304 | } |
| 305 | |
Matt Arsenault | 61cb6fa | 2015-11-11 00:01:36 +0000 | [diff] [blame] | 306 | static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) { |
Matt Arsenault | f1aebbf | 2015-11-02 23:30:48 +0000 | [diff] [blame] | 307 | switch (NumVectorElts) { |
| 308 | case 1: |
Marek Olsak | 79c0587 | 2016-11-25 17:37:09 +0000 | [diff] [blame] | 309 | return AMDGPU::SReg_32_XM0RegClassID; |
Matt Arsenault | f1aebbf | 2015-11-02 23:30:48 +0000 | [diff] [blame] | 310 | case 2: |
| 311 | return AMDGPU::SReg_64RegClassID; |
| 312 | case 4: |
| 313 | return AMDGPU::SReg_128RegClassID; |
| 314 | case 8: |
| 315 | return AMDGPU::SReg_256RegClassID; |
| 316 | case 16: |
| 317 | return AMDGPU::SReg_512RegClassID; |
| 318 | } |
| 319 | |
| 320 | llvm_unreachable("invalid vector size"); |
| 321 | } |
| 322 | |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 323 | static bool getConstantValue(SDValue N, uint32_t &Out) { |
| 324 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) { |
| 325 | Out = C->getAPIntValue().getZExtValue(); |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) { |
| 330 | Out = C->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | return false; |
| 335 | } |
| 336 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 337 | void AMDGPUDAGToDAGISel::Select(SDNode *N) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 338 | unsigned int Opc = N->getOpcode(); |
| 339 | if (N->isMachineOpcode()) { |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 340 | N->setNodeId(-1); |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 341 | return; // Already selected. |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 342 | } |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 343 | |
Matt Arsenault | a9dbdca | 2016-04-12 14:05:04 +0000 | [diff] [blame] | 344 | if (isa<AtomicSDNode>(N) || |
| 345 | (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC)) |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 346 | N = glueCopyToM0(N); |
| 347 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 348 | switch (Opc) { |
| 349 | default: break; |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 350 | // We are selecting i64 ADD here instead of custom lower it during |
| 351 | // DAG legalization, so we can fold some i64 ADDs used for address |
| 352 | // calculation into the LOAD and STORE instructions. |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 353 | case ISD::ADD: |
Nicolai Haehnle | 67624af | 2016-10-14 10:30:00 +0000 | [diff] [blame] | 354 | case ISD::ADDC: |
| 355 | case ISD::ADDE: |
| 356 | case ISD::SUB: |
| 357 | case ISD::SUBC: |
| 358 | case ISD::SUBE: { |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 359 | if (N->getValueType(0) != MVT::i64 || |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 360 | Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 361 | break; |
| 362 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 363 | SelectADD_SUB_I64(N); |
| 364 | return; |
Tom Stellard | 1f15bff | 2014-02-25 21:36:18 +0000 | [diff] [blame] | 365 | } |
Matt Arsenault | ee3f0ac | 2017-01-30 18:11:38 +0000 | [diff] [blame] | 366 | case ISD::UADDO: |
| 367 | case ISD::USUBO: { |
| 368 | SelectUADDO_USUBO(N); |
| 369 | return; |
| 370 | } |
Tom Stellard | 8485fa0 | 2016-12-07 02:42:15 +0000 | [diff] [blame] | 371 | case AMDGPUISD::FMUL_W_CHAIN: { |
| 372 | SelectFMUL_W_CHAIN(N); |
| 373 | return; |
| 374 | } |
| 375 | case AMDGPUISD::FMA_W_CHAIN: { |
| 376 | SelectFMA_W_CHAIN(N); |
| 377 | return; |
| 378 | } |
| 379 | |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 380 | case ISD::SCALAR_TO_VECTOR: |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 381 | case AMDGPUISD::BUILD_VERTICAL_VECTOR: |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 382 | case ISD::BUILD_VECTOR: { |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 383 | unsigned RegClassID; |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 384 | const AMDGPURegisterInfo *TRI = Subtarget->getRegisterInfo(); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 385 | EVT VT = N->getValueType(0); |
| 386 | unsigned NumVectorElts = VT.getVectorNumElements(); |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 387 | EVT EltVT = VT.getVectorElementType(); |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 388 | |
| 389 | if (VT == MVT::v2i16 || VT == MVT::v2f16) { |
| 390 | if (Opc == ISD::BUILD_VECTOR) { |
| 391 | uint32_t LHSVal, RHSVal; |
| 392 | if (getConstantValue(N->getOperand(0), LHSVal) && |
| 393 | getConstantValue(N->getOperand(1), RHSVal)) { |
| 394 | uint32_t K = LHSVal | (RHSVal << 16); |
| 395 | CurDAG->SelectNodeTo(N, AMDGPU::S_MOV_B32, VT, |
| 396 | CurDAG->getTargetConstant(K, SDLoc(N), MVT::i32)); |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | break; |
| 402 | } |
| 403 | |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 404 | assert(EltVT.bitsEq(MVT::i32)); |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 405 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 406 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
Matt Arsenault | 61cb6fa | 2015-11-11 00:01:36 +0000 | [diff] [blame] | 407 | RegClassID = selectSGPRVectorRegClassID(NumVectorElts); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 408 | } else { |
| 409 | // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG |
| 410 | // that adds a 128 bits reg copy when going through TwoAddressInstructions |
| 411 | // pass. We want to avoid 128 bits copies as much as possible because they |
| 412 | // can't be bundled by our scheduler. |
| 413 | switch(NumVectorElts) { |
| 414 | case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break; |
Tom Stellard | 880a80a | 2014-06-17 16:53:14 +0000 | [diff] [blame] | 415 | case 4: |
| 416 | if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR) |
| 417 | RegClassID = AMDGPU::R600_Reg128VerticalRegClassID; |
| 418 | else |
| 419 | RegClassID = AMDGPU::R600_Reg128RegClassID; |
| 420 | break; |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 421 | default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR"); |
| 422 | } |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 423 | } |
Tom Stellard | 0344cdf | 2013-08-01 15:23:42 +0000 | [diff] [blame] | 424 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 425 | SDLoc DL(N); |
| 426 | SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 427 | |
| 428 | if (NumVectorElts == 1) { |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 429 | CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0), |
| 430 | RegClass); |
| 431 | return; |
Tom Stellard | 0344cdf | 2013-08-01 15:23:42 +0000 | [diff] [blame] | 432 | } |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 433 | |
| 434 | assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not " |
| 435 | "supported yet"); |
| 436 | // 16 = Max Num Vector Elements |
| 437 | // 2 = 2 REG_SEQUENCE operands per element (value, subreg index) |
| 438 | // 1 = Vector Register Class |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 439 | SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1); |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 440 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 441 | RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 442 | bool IsRegSeq = true; |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 443 | unsigned NOps = N->getNumOperands(); |
| 444 | for (unsigned i = 0; i < NOps; i++) { |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 445 | // XXX: Why is this here? |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 446 | if (isa<RegisterSDNode>(N->getOperand(i))) { |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 447 | IsRegSeq = false; |
| 448 | break; |
| 449 | } |
Tom Stellard | 8e5da41 | 2013-08-14 23:24:32 +0000 | [diff] [blame] | 450 | RegSeqArgs[1 + (2 * i)] = N->getOperand(i); |
| 451 | RegSeqArgs[1 + (2 * i) + 1] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 452 | CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, |
| 453 | MVT::i32); |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 454 | } |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 455 | |
| 456 | if (NOps != NumVectorElts) { |
| 457 | // Fill in the missing undef elements if this was a scalar_to_vector. |
| 458 | assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts); |
| 459 | |
| 460 | MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 461 | DL, EltVT); |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 462 | for (unsigned i = NOps; i < NumVectorElts; ++i) { |
| 463 | RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0); |
| 464 | RegSeqArgs[1 + (2 * i) + 1] = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 465 | CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, MVT::i32); |
Matt Arsenault | 064c206 | 2014-06-11 17:40:32 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 469 | if (!IsRegSeq) |
| 470 | break; |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 471 | CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs); |
| 472 | return; |
Vincent Lejeune | 3b6f20e | 2013-03-05 15:04:49 +0000 | [diff] [blame] | 473 | } |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 474 | case ISD::BUILD_PAIR: { |
| 475 | SDValue RC, SubReg0, SubReg1; |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 476 | if (Subtarget->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) { |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 477 | break; |
| 478 | } |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 479 | SDLoc DL(N); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 480 | if (N->getValueType(0) == MVT::i128) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 481 | RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32); |
| 482 | SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32); |
| 483 | SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 484 | } else if (N->getValueType(0) == MVT::i64) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 485 | RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32); |
| 486 | SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); |
| 487 | SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 488 | } else { |
| 489 | llvm_unreachable("Unhandled value type for BUILD_PAIR"); |
| 490 | } |
| 491 | const SDValue Ops[] = { RC, N->getOperand(0), SubReg0, |
| 492 | N->getOperand(1), SubReg1 }; |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 493 | ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, |
| 494 | N->getValueType(0), Ops)); |
| 495 | return; |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 496 | } |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 497 | |
| 498 | case ISD::Constant: |
| 499 | case ISD::ConstantFP: { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 500 | if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS || |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 501 | N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N)) |
| 502 | break; |
| 503 | |
| 504 | uint64_t Imm; |
| 505 | if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N)) |
| 506 | Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 507 | else { |
Tom Stellard | 3cbe014 | 2014-04-07 19:31:13 +0000 | [diff] [blame] | 508 | ConstantSDNode *C = cast<ConstantSDNode>(N); |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 509 | Imm = C->getZExtValue(); |
| 510 | } |
| 511 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 512 | SDLoc DL(N); |
| 513 | SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
| 514 | CurDAG->getConstant(Imm & 0xFFFFFFFF, DL, |
| 515 | MVT::i32)); |
| 516 | SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
| 517 | CurDAG->getConstant(Imm >> 32, DL, MVT::i32)); |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 518 | const SDValue Ops[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 519 | CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), |
| 520 | SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
| 521 | SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32) |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 524 | ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, |
| 525 | N->getValueType(0), Ops)); |
| 526 | return; |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 527 | } |
Matt Arsenault | 4bf43d4 | 2015-09-25 17:27:08 +0000 | [diff] [blame] | 528 | case ISD::LOAD: |
Tom Stellard | 096b8c1 | 2015-02-04 20:49:49 +0000 | [diff] [blame] | 529 | case ISD::STORE: { |
Tom Stellard | 381a94a | 2015-05-12 15:00:49 +0000 | [diff] [blame] | 530 | N = glueCopyToM0(N); |
Tom Stellard | 096b8c1 | 2015-02-04 20:49:49 +0000 | [diff] [blame] | 531 | break; |
| 532 | } |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 533 | |
| 534 | case AMDGPUISD::BFE_I32: |
| 535 | case AMDGPUISD::BFE_U32: { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 536 | if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 537 | break; |
| 538 | |
| 539 | // There is a scalar version available, but unlike the vector version which |
| 540 | // has a separate operand for the offset and width, the scalar version packs |
| 541 | // the width and offset into a single operand. Try to move to the scalar |
| 542 | // version if the offsets are constant, so that we can try to keep extended |
| 543 | // loads of kernel arguments in SGPRs. |
| 544 | |
| 545 | // TODO: Technically we could try to pattern match scalar bitshifts of |
| 546 | // dynamic values, but it's probably not useful. |
| 547 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 548 | if (!Offset) |
| 549 | break; |
| 550 | |
| 551 | ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); |
| 552 | if (!Width) |
| 553 | break; |
| 554 | |
| 555 | bool Signed = Opc == AMDGPUISD::BFE_I32; |
| 556 | |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 557 | uint32_t OffsetVal = Offset->getZExtValue(); |
| 558 | uint32_t WidthVal = Width->getZExtValue(); |
| 559 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 560 | ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32, |
| 561 | SDLoc(N), N->getOperand(0), OffsetVal, WidthVal)); |
| 562 | return; |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 563 | } |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 564 | case AMDGPUISD::DIV_SCALE: { |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 565 | SelectDIV_SCALE(N); |
| 566 | return; |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 567 | } |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 568 | case ISD::CopyToReg: { |
| 569 | const SITargetLowering& Lowering = |
| 570 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
Matt Arsenault | 0d0d6c2 | 2017-04-12 21:58:23 +0000 | [diff] [blame] | 571 | N = Lowering.legalizeTargetIndependentNode(N, *CurDAG); |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 572 | break; |
| 573 | } |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 574 | case ISD::AND: |
| 575 | case ISD::SRL: |
| 576 | case ISD::SRA: |
Matt Arsenault | 7e8de01 | 2016-04-22 22:59:16 +0000 | [diff] [blame] | 577 | case ISD::SIGN_EXTEND_INREG: |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 578 | if (N->getValueType(0) != MVT::i32 || |
| 579 | Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) |
| 580 | break; |
| 581 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 582 | SelectS_BFE(N); |
| 583 | return; |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 584 | case ISD::BRCOND: |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 585 | SelectBRCOND(N); |
| 586 | return; |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 587 | |
| 588 | case AMDGPUISD::ATOMIC_CMP_SWAP: |
| 589 | SelectATOMIC_CMP_SWAP(N); |
| 590 | return; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 591 | } |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 592 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 593 | SelectCode(N); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Jan Vesely | 43b7b5b | 2016-04-07 19:23:11 +0000 | [diff] [blame] | 596 | bool AMDGPUDAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const { |
| 597 | if (!N->readMem()) |
| 598 | return false; |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 599 | if (CbId == -1) |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 600 | return N->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS; |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 601 | |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 602 | return N->getAddressSpace() == AMDGPUASI.CONSTANT_BUFFER_0 + CbId; |
Matt Arsenault | 3f98140 | 2014-09-15 15:41:53 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 605 | bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const { |
| 606 | const BasicBlock *BB = FuncInfo->MBB->getBasicBlock(); |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 607 | const Instruction *Term = BB->getTerminator(); |
| 608 | return Term->getMetadata("amdgpu.uniform") || |
| 609 | Term->getMetadata("structurizecfg.uniform"); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 612 | StringRef AMDGPUDAGToDAGISel::getPassName() const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 613 | return "AMDGPU DAG->DAG Pattern Instruction Selection"; |
| 614 | } |
| 615 | |
Tom Stellard | 41fc785 | 2013-07-23 01:48:42 +0000 | [diff] [blame] | 616 | //===----------------------------------------------------------------------===// |
| 617 | // Complex Patterns |
| 618 | //===----------------------------------------------------------------------===// |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 619 | |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 620 | bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr, |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 621 | SDValue& IntPtr) { |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 622 | if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 623 | IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr), |
| 624 | true); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 625 | return true; |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr, |
| 631 | SDValue& BaseReg, SDValue &Offset) { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 632 | if (!isa<ConstantSDNode>(Addr)) { |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 633 | BaseReg = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 634 | Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true); |
Tom Stellard | 365366f | 2013-01-23 02:09:06 +0000 | [diff] [blame] | 635 | return true; |
| 636 | } |
| 637 | return false; |
| 638 | } |
| 639 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 640 | bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, |
| 641 | SDValue &Offset) { |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 642 | ConstantSDNode *IMMOffset; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 643 | |
| 644 | if (Addr.getOpcode() == ISD::ADD |
| 645 | && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) |
| 646 | && isInt<16>(IMMOffset->getZExtValue())) { |
| 647 | |
| 648 | Base = Addr.getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 649 | Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), |
| 650 | MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 651 | return true; |
| 652 | // If the pointer address is constant, we can move it to the offset field. |
| 653 | } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr)) |
| 654 | && isInt<16>(IMMOffset->getZExtValue())) { |
| 655 | Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 656 | SDLoc(CurDAG->getEntryNode()), |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 657 | AMDGPU::ZERO, MVT::i32); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 658 | Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), |
| 659 | MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 660 | return true; |
| 661 | } |
| 662 | |
| 663 | // Default case, no offset |
| 664 | Base = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 665 | Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 666 | return true; |
| 667 | } |
| 668 | |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 669 | bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, |
| 670 | SDValue &Offset) { |
| 671 | ConstantSDNode *C; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 672 | SDLoc DL(Addr); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 673 | |
| 674 | if ((C = dyn_cast<ConstantSDNode>(Addr))) { |
| 675 | Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 676 | Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); |
Jan Vesely | 06200bd | 2017-01-06 21:00:46 +0000 | [diff] [blame] | 677 | } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) && |
| 678 | (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) { |
| 679 | Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); |
| 680 | Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 681 | } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && |
| 682 | (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { |
| 683 | Base = Addr.getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 684 | Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 685 | } else { |
| 686 | Base = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 687 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | return true; |
| 691 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 692 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 693 | void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) { |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 694 | SDLoc DL(N); |
| 695 | SDValue LHS = N->getOperand(0); |
| 696 | SDValue RHS = N->getOperand(1); |
| 697 | |
Nicolai Haehnle | 67624af | 2016-10-14 10:30:00 +0000 | [diff] [blame] | 698 | unsigned Opcode = N->getOpcode(); |
| 699 | bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE); |
| 700 | bool ProduceCarry = |
| 701 | ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC; |
| 702 | bool IsAdd = |
| 703 | (Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE); |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 704 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 705 | SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); |
| 706 | SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 707 | |
| 708 | SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 709 | DL, MVT::i32, LHS, Sub0); |
| 710 | SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 711 | DL, MVT::i32, LHS, Sub1); |
| 712 | |
| 713 | SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 714 | DL, MVT::i32, RHS, Sub0); |
| 715 | SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
| 716 | DL, MVT::i32, RHS, Sub1); |
| 717 | |
| 718 | SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 719 | |
Tom Stellard | 80942a1 | 2014-09-05 14:07:59 +0000 | [diff] [blame] | 720 | unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32; |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 721 | unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32; |
| 722 | |
Nicolai Haehnle | 67624af | 2016-10-14 10:30:00 +0000 | [diff] [blame] | 723 | SDNode *AddLo; |
| 724 | if (!ConsumeCarry) { |
| 725 | SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) }; |
| 726 | AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args); |
| 727 | } else { |
| 728 | SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) }; |
| 729 | AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args); |
| 730 | } |
| 731 | SDValue AddHiArgs[] = { |
| 732 | SDValue(Hi0, 0), |
| 733 | SDValue(Hi1, 0), |
| 734 | SDValue(AddLo, 1) |
| 735 | }; |
| 736 | SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 737 | |
Nicolai Haehnle | 67624af | 2016-10-14 10:30:00 +0000 | [diff] [blame] | 738 | SDValue RegSequenceArgs[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 739 | CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 740 | SDValue(AddLo,0), |
| 741 | Sub0, |
| 742 | SDValue(AddHi,0), |
| 743 | Sub1, |
| 744 | }; |
Nicolai Haehnle | 67624af | 2016-10-14 10:30:00 +0000 | [diff] [blame] | 745 | SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL, |
| 746 | MVT::i64, RegSequenceArgs); |
| 747 | |
| 748 | if (ProduceCarry) { |
| 749 | // Replace the carry-use |
| 750 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(AddHi, 1)); |
| 751 | } |
| 752 | |
| 753 | // Replace the remaining uses. |
| 754 | CurDAG->ReplaceAllUsesWith(N, RegSequence); |
| 755 | CurDAG->RemoveDeadNode(N); |
Matt Arsenault | 9fa3f93 | 2014-06-23 18:00:34 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Matt Arsenault | ee3f0ac | 2017-01-30 18:11:38 +0000 | [diff] [blame] | 758 | void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) { |
| 759 | // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned |
| 760 | // carry out despite the _i32 name. These were renamed in VI to _U32. |
| 761 | // FIXME: We should probably rename the opcodes here. |
| 762 | unsigned Opc = N->getOpcode() == ISD::UADDO ? |
| 763 | AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64; |
| 764 | |
| 765 | CurDAG->SelectNodeTo(N, Opc, N->getVTList(), |
| 766 | { N->getOperand(0), N->getOperand(1) }); |
| 767 | } |
| 768 | |
Tom Stellard | 8485fa0 | 2016-12-07 02:42:15 +0000 | [diff] [blame] | 769 | void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) { |
| 770 | SDLoc SL(N); |
| 771 | // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod |
| 772 | SDValue Ops[10]; |
| 773 | |
| 774 | SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]); |
| 775 | SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); |
| 776 | SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]); |
| 777 | Ops[8] = N->getOperand(0); |
| 778 | Ops[9] = N->getOperand(4); |
| 779 | |
| 780 | CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops); |
| 781 | } |
| 782 | |
| 783 | void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) { |
| 784 | SDLoc SL(N); |
| 785 | // src0_modifiers, src0, src1_modifiers, src1, clamp, omod |
| 786 | SDValue Ops[8]; |
| 787 | |
| 788 | SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]); |
| 789 | SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); |
| 790 | Ops[6] = N->getOperand(0); |
| 791 | Ops[7] = N->getOperand(3); |
| 792 | |
| 793 | CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops); |
| 794 | } |
| 795 | |
Matt Arsenault | 044f1d1 | 2015-02-14 04:24:28 +0000 | [diff] [blame] | 796 | // We need to handle this here because tablegen doesn't support matching |
| 797 | // instructions with multiple outputs. |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 798 | void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) { |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 799 | SDLoc SL(N); |
| 800 | EVT VT = N->getValueType(0); |
| 801 | |
| 802 | assert(VT == MVT::f32 || VT == MVT::f64); |
| 803 | |
| 804 | unsigned Opc |
| 805 | = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32; |
| 806 | |
Matt Arsenault | 3b99f12 | 2017-01-19 06:04:12 +0000 | [diff] [blame] | 807 | SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) }; |
| 808 | CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); |
Matt Arsenault | f2b0aeb | 2014-06-23 18:28:28 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 811 | bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset, |
| 812 | unsigned OffsetBits) const { |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 813 | if ((OffsetBits == 16 && !isUInt<16>(Offset)) || |
| 814 | (OffsetBits == 8 && !isUInt<8>(Offset))) |
| 815 | return false; |
| 816 | |
Matt Arsenault | 706f930 | 2015-07-06 16:01:58 +0000 | [diff] [blame] | 817 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS || |
| 818 | Subtarget->unsafeDSOffsetFoldingEnabled()) |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 819 | return true; |
| 820 | |
| 821 | // On Southern Islands instruction with a negative base value and an offset |
| 822 | // don't seem to work. |
| 823 | return CurDAG->SignBitIsZero(Base); |
| 824 | } |
| 825 | |
| 826 | bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base, |
| 827 | SDValue &Offset) const { |
Tom Stellard | 92b24f3 | 2016-04-29 14:34:26 +0000 | [diff] [blame] | 828 | SDLoc DL(Addr); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 829 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 830 | SDValue N0 = Addr.getOperand(0); |
| 831 | SDValue N1 = Addr.getOperand(1); |
| 832 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 833 | if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) { |
| 834 | // (add n0, c0) |
| 835 | Base = N0; |
Tom Stellard | 92b24f3 | 2016-04-29 14:34:26 +0000 | [diff] [blame] | 836 | Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 837 | return true; |
| 838 | } |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 839 | } else if (Addr.getOpcode() == ISD::SUB) { |
| 840 | // sub C, x -> add (sub 0, x), C |
| 841 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { |
| 842 | int64_t ByteOffset = C->getSExtValue(); |
| 843 | if (isUInt<16>(ByteOffset)) { |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 844 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 845 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 846 | // XXX - This is kind of hacky. Create a dummy sub node so we can check |
| 847 | // the known bits in isDSOffsetLegal. We need to emit the selected node |
| 848 | // here, so this is thrown away. |
| 849 | SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, |
| 850 | Zero, Addr.getOperand(1)); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 851 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 852 | if (isDSOffsetLegal(Sub, ByteOffset, 16)) { |
| 853 | MachineSDNode *MachineSub |
| 854 | = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, |
| 855 | Zero, Addr.getOperand(1)); |
| 856 | |
| 857 | Base = SDValue(MachineSub, 0); |
Tom Stellard | 26a2ab7 | 2016-06-10 00:01:04 +0000 | [diff] [blame] | 858 | Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16); |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 859 | return true; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { |
| 864 | // If we have a constant address, prefer to put the constant into the |
| 865 | // offset. This can save moves to load the constant address since multiple |
| 866 | // operations can share the zero base address register, and enables merging |
| 867 | // into read2 / write2 instructions. |
| 868 | |
| 869 | SDLoc DL(Addr); |
| 870 | |
Matt Arsenault | e775f5f | 2014-10-14 17:21:19 +0000 | [diff] [blame] | 871 | if (isUInt<16>(CAddr->getZExtValue())) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 872 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | c8d7920 | 2014-10-15 21:08:59 +0000 | [diff] [blame] | 873 | MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 874 | DL, MVT::i32, Zero); |
Tom Stellard | c8d7920 | 2014-10-15 21:08:59 +0000 | [diff] [blame] | 875 | Base = SDValue(MovZero, 0); |
Tom Stellard | 26a2ab7 | 2016-06-10 00:01:04 +0000 | [diff] [blame] | 876 | Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); |
Matt Arsenault | e775f5f | 2014-10-14 17:21:19 +0000 | [diff] [blame] | 877 | return true; |
| 878 | } |
| 879 | } |
| 880 | |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 881 | // default case |
| 882 | Base = Addr; |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 883 | Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16); |
Tom Stellard | 85e8b6d | 2014-08-22 18:49:33 +0000 | [diff] [blame] | 884 | return true; |
| 885 | } |
| 886 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 887 | // TODO: If offset is too big, put low 16-bit into offset. |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 888 | bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base, |
| 889 | SDValue &Offset0, |
| 890 | SDValue &Offset1) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 891 | SDLoc DL(Addr); |
| 892 | |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 893 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 894 | SDValue N0 = Addr.getOperand(0); |
| 895 | SDValue N1 = Addr.getOperand(1); |
| 896 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 897 | unsigned DWordOffset0 = C1->getZExtValue() / 4; |
| 898 | unsigned DWordOffset1 = DWordOffset0 + 1; |
| 899 | // (add n0, c0) |
| 900 | if (isDSOffsetLegal(N0, DWordOffset1, 8)) { |
| 901 | Base = N0; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 902 | Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); |
| 903 | Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 904 | return true; |
| 905 | } |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 906 | } else if (Addr.getOpcode() == ISD::SUB) { |
| 907 | // sub C, x -> add (sub 0, x), C |
| 908 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { |
| 909 | unsigned DWordOffset0 = C->getZExtValue() / 4; |
| 910 | unsigned DWordOffset1 = DWordOffset0 + 1; |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 911 | |
Matt Arsenault | 966a94f | 2015-09-08 19:34:22 +0000 | [diff] [blame] | 912 | if (isUInt<8>(DWordOffset0)) { |
| 913 | SDLoc DL(Addr); |
| 914 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
| 915 | |
| 916 | // XXX - This is kind of hacky. Create a dummy sub node so we can check |
| 917 | // the known bits in isDSOffsetLegal. We need to emit the selected node |
| 918 | // here, so this is thrown away. |
| 919 | SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, |
| 920 | Zero, Addr.getOperand(1)); |
| 921 | |
| 922 | if (isDSOffsetLegal(Sub, DWordOffset1, 8)) { |
| 923 | MachineSDNode *MachineSub |
| 924 | = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, |
| 925 | Zero, Addr.getOperand(1)); |
| 926 | |
| 927 | Base = SDValue(MachineSub, 0); |
| 928 | Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); |
| 929 | Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); |
| 930 | return true; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 935 | unsigned DWordOffset0 = CAddr->getZExtValue() / 4; |
| 936 | unsigned DWordOffset1 = DWordOffset0 + 1; |
| 937 | assert(4 * DWordOffset0 == CAddr->getZExtValue()); |
| 938 | |
| 939 | if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 940 | SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 941 | MachineSDNode *MovZero |
| 942 | = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 943 | DL, MVT::i32, Zero); |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 944 | Base = SDValue(MovZero, 0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 945 | Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); |
| 946 | Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); |
Matt Arsenault | 1a74aff | 2014-10-15 18:06:43 +0000 | [diff] [blame] | 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 951 | // default case |
Matt Arsenault | 0efdd06 | 2016-09-09 22:29:28 +0000 | [diff] [blame] | 952 | |
| 953 | // FIXME: This is broken on SI where we still need to check if the base |
| 954 | // pointer is positive here. |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 955 | Base = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 956 | Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8); |
| 957 | Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8); |
Tom Stellard | f3fc555 | 2014-08-22 18:49:35 +0000 | [diff] [blame] | 958 | return true; |
| 959 | } |
| 960 | |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 961 | static bool isLegalMUBUFImmOffset(unsigned Imm) { |
| 962 | return isUInt<12>(Imm); |
| 963 | } |
| 964 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 965 | static bool isLegalMUBUFImmOffset(const ConstantSDNode *Imm) { |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 966 | return isLegalMUBUFImmOffset(Imm->getZExtValue()); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 969 | bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr, |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 970 | SDValue &VAddr, SDValue &SOffset, |
| 971 | SDValue &Offset, SDValue &Offen, |
| 972 | SDValue &Idxen, SDValue &Addr64, |
| 973 | SDValue &GLC, SDValue &SLC, |
| 974 | SDValue &TFE) const { |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 975 | // Subtarget prefers to use flat instruction |
| 976 | if (Subtarget->useFlatForGlobal()) |
| 977 | return false; |
| 978 | |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 979 | SDLoc DL(Addr); |
| 980 | |
Jan Vesely | 43b7b5b | 2016-04-07 19:23:11 +0000 | [diff] [blame] | 981 | if (!GLC.getNode()) |
| 982 | GLC = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 983 | if (!SLC.getNode()) |
| 984 | SLC = CurDAG->getTargetConstant(0, DL, MVT::i1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 985 | TFE = CurDAG->getTargetConstant(0, DL, MVT::i1); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 986 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 987 | Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 988 | Offen = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 989 | Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 990 | SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 991 | |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 992 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 993 | SDValue N0 = Addr.getOperand(0); |
| 994 | SDValue N1 = Addr.getOperand(1); |
| 995 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 996 | |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 997 | if (N0.getOpcode() == ISD::ADD) { |
| 998 | // (add (add N2, N3), C1) -> addr64 |
| 999 | SDValue N2 = N0.getOperand(0); |
| 1000 | SDValue N3 = N0.getOperand(1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1001 | Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 1002 | Ptr = N2; |
| 1003 | VAddr = N3; |
| 1004 | } else { |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1005 | // (add N0, C1) -> offset |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1006 | VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1007 | Ptr = N0; |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | if (isLegalMUBUFImmOffset(C1)) { |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 1011 | Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | if (isUInt<32>(C1->getZExtValue())) { |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 1016 | // Illegal offset, store it in soffset. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1017 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 1018 | SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1019 | CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)), |
| 1020 | 0); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1021 | return true; |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } |
Tom Stellard | 94b7231 | 2015-02-11 00:34:35 +0000 | [diff] [blame] | 1024 | |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 1025 | if (Addr.getOpcode() == ISD::ADD) { |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1026 | // (add N0, N1) -> addr64 |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 1027 | SDValue N0 = Addr.getOperand(0); |
| 1028 | SDValue N1 = Addr.getOperand(1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1029 | Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1030 | Ptr = N0; |
| 1031 | VAddr = N1; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1032 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1033 | return true; |
Tom Stellard | b02c268 | 2014-06-24 23:33:07 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1036 | // default case -> offset |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1037 | VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1038 | Ptr = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1039 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1040 | |
| 1041 | return true; |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, |
Tom Stellard | c53861a | 2015-02-11 00:34:32 +0000 | [diff] [blame] | 1045 | SDValue &VAddr, SDValue &SOffset, |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 1046 | SDValue &Offset, SDValue &GLC, |
| 1047 | SDValue &SLC, SDValue &TFE) const { |
| 1048 | SDValue Ptr, Offen, Idxen, Addr64; |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1049 | |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 1050 | // addr64 bit was removed for volcanic islands. |
| 1051 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 1052 | return false; |
| 1053 | |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1054 | if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, |
| 1055 | GLC, SLC, TFE)) |
| 1056 | return false; |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1057 | |
| 1058 | ConstantSDNode *C = cast<ConstantSDNode>(Addr64); |
| 1059 | if (C->getSExtValue()) { |
| 1060 | SDLoc DL(Addr); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 1061 | |
| 1062 | const SITargetLowering& Lowering = |
| 1063 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
| 1064 | |
| 1065 | SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 1068 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1072 | bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, |
Tom Stellard | c53861a | 2015-02-11 00:34:32 +0000 | [diff] [blame] | 1073 | SDValue &VAddr, SDValue &SOffset, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 1074 | SDValue &Offset, |
| 1075 | SDValue &SLC) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1076 | SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1); |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 1077 | SDValue GLC, TFE; |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1078 | |
Tom Stellard | 1f9939f | 2015-02-27 14:59:41 +0000 | [diff] [blame] | 1079 | return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE); |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 1082 | static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) { |
| 1083 | auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>(); |
| 1084 | return PSV && PSV->isStack(); |
Matt Arsenault | ac0fc84 | 2016-09-17 16:09:55 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 1087 | std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const { |
| 1088 | const MachineFunction &MF = CurDAG->getMachineFunction(); |
| 1089 | const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
| 1090 | |
| 1091 | if (auto FI = dyn_cast<FrameIndexSDNode>(N)) { |
| 1092 | SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(), |
| 1093 | FI->getValueType(0)); |
| 1094 | |
| 1095 | // If we can resolve this to a frame index access, this is relative to the |
| 1096 | // frame pointer SGPR. |
| 1097 | return std::make_pair(TFI, CurDAG->getRegister(Info->getFrameOffsetReg(), |
| 1098 | MVT::i32)); |
| 1099 | } |
| 1100 | |
| 1101 | // If we don't know this private access is a local stack object, it needs to |
| 1102 | // be relative to the entry point's scratch wave offset register. |
| 1103 | return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(), |
| 1104 | MVT::i32)); |
| 1105 | } |
| 1106 | |
| 1107 | bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Root, |
| 1108 | SDValue Addr, SDValue &Rsrc, |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 1109 | SDValue &VAddr, SDValue &SOffset, |
| 1110 | SDValue &ImmOffset) const { |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1111 | |
| 1112 | SDLoc DL(Addr); |
| 1113 | MachineFunction &MF = CurDAG->getMachineFunction(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 1114 | const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1115 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 1116 | Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1117 | |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 1118 | if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { |
| 1119 | unsigned Imm = CAddr->getZExtValue(); |
| 1120 | assert(!isLegalMUBUFImmOffset(Imm) && |
| 1121 | "should have been selected by other pattern"); |
| 1122 | |
| 1123 | SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32); |
| 1124 | MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, |
| 1125 | DL, MVT::i32, HighBits); |
| 1126 | VAddr = SDValue(MovHighBits, 0); |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 1127 | |
| 1128 | // In a call sequence, stores to the argument stack area are relative to the |
| 1129 | // stack pointer. |
| 1130 | const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Root)->getPointerInfo(); |
| 1131 | unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ? |
| 1132 | Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg(); |
| 1133 | |
| 1134 | SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32); |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 1135 | ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16); |
| 1136 | return true; |
| 1137 | } |
| 1138 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1139 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 1140 | // (add n0, c1) |
| 1141 | |
Tom Stellard | 78655fc | 2015-07-16 19:40:09 +0000 | [diff] [blame] | 1142 | SDValue N0 = Addr.getOperand(0); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1143 | SDValue N1 = Addr.getOperand(1); |
Matt Arsenault | cd09961 | 2016-02-24 04:55:29 +0000 | [diff] [blame] | 1144 | |
Tom Stellard | 78655fc | 2015-07-16 19:40:09 +0000 | [diff] [blame] | 1145 | // Offsets in vaddr must be positive. |
Matt Arsenault | cd09961 | 2016-02-24 04:55:29 +0000 | [diff] [blame] | 1146 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
Matt Arsenault | cb38a6b | 2016-03-21 18:02:18 +0000 | [diff] [blame] | 1147 | if (isLegalMUBUFImmOffset(C1)) { |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 1148 | std::tie(VAddr, SOffset) = foldFrameIndex(N0); |
Matt Arsenault | cd09961 | 2016-02-24 04:55:29 +0000 | [diff] [blame] | 1149 | ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); |
| 1150 | return true; |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1154 | // (node) |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 1155 | std::tie(VAddr, SOffset) = foldFrameIndex(Addr); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1156 | ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
| 1159 | |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 1160 | bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Root, |
| 1161 | SDValue Addr, |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 1162 | SDValue &SRsrc, |
| 1163 | SDValue &SOffset, |
| 1164 | SDValue &Offset) const { |
| 1165 | ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr); |
| 1166 | if (!CAddr || !isLegalMUBUFImmOffset(CAddr)) |
| 1167 | return false; |
| 1168 | |
| 1169 | SDLoc DL(Addr); |
| 1170 | MachineFunction &MF = CurDAG->getMachineFunction(); |
| 1171 | const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
| 1172 | |
| 1173 | SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); |
Matt Arsenault | 156d3ae | 2017-05-17 21:02:58 +0000 | [diff] [blame] | 1174 | |
| 1175 | const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Root)->getPointerInfo(); |
| 1176 | unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ? |
| 1177 | Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg(); |
| 1178 | |
| 1179 | // FIXME: Get from MachinePointerInfo? We should only be using the frame |
| 1180 | // offset if we know this is in a call sequence. |
| 1181 | SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32); |
| 1182 | |
Matt Arsenault | 0774ea2 | 2017-04-24 19:40:59 +0000 | [diff] [blame] | 1183 | Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); |
| 1184 | return true; |
| 1185 | } |
| 1186 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1187 | bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, |
| 1188 | SDValue &SOffset, SDValue &Offset, |
| 1189 | SDValue &GLC, SDValue &SLC, |
| 1190 | SDValue &TFE) const { |
| 1191 | SDValue Ptr, VAddr, Offen, Idxen, Addr64; |
Tom Stellard | 794c8c0 | 2014-12-02 17:05:41 +0000 | [diff] [blame] | 1192 | const SIInstrInfo *TII = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 1193 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1194 | |
Changpeng Fang | b41574a | 2015-12-22 20:55:23 +0000 | [diff] [blame] | 1195 | if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, |
| 1196 | GLC, SLC, TFE)) |
| 1197 | return false; |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1198 | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1199 | if (!cast<ConstantSDNode>(Offen)->getSExtValue() && |
| 1200 | !cast<ConstantSDNode>(Idxen)->getSExtValue() && |
| 1201 | !cast<ConstantSDNode>(Addr64)->getSExtValue()) { |
Tom Stellard | 794c8c0 | 2014-12-02 17:05:41 +0000 | [diff] [blame] | 1202 | uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1203 | APInt::getAllOnesValue(32).getZExtValue(); // Size |
| 1204 | SDLoc DL(Addr); |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 1205 | |
| 1206 | const SITargetLowering& Lowering = |
| 1207 | *static_cast<const SITargetLowering*>(getTargetLowering()); |
| 1208 | |
| 1209 | SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0); |
Tom Stellard | 155bbb7 | 2014-08-11 22:18:17 +0000 | [diff] [blame] | 1210 | return true; |
| 1211 | } |
| 1212 | return false; |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1215 | bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, |
Jan Vesely | 43b7b5b | 2016-04-07 19:23:11 +0000 | [diff] [blame] | 1216 | SDValue &Soffset, SDValue &Offset |
| 1217 | ) const { |
| 1218 | SDValue GLC, SLC, TFE; |
| 1219 | |
| 1220 | return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); |
| 1221 | } |
| 1222 | bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1223 | SDValue &Soffset, SDValue &Offset, |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 1224 | SDValue &SLC) const { |
| 1225 | SDValue GLC, TFE; |
Tom Stellard | 7980fc8 | 2014-09-25 18:30:26 +0000 | [diff] [blame] | 1226 | |
| 1227 | return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); |
| 1228 | } |
| 1229 | |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 1230 | bool AMDGPUDAGToDAGISel::SelectMUBUFConstant(SDValue Constant, |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 1231 | SDValue &SOffset, |
| 1232 | SDValue &ImmOffset) const { |
| 1233 | SDLoc DL(Constant); |
| 1234 | uint32_t Imm = cast<ConstantSDNode>(Constant)->getZExtValue(); |
| 1235 | uint32_t Overflow = 0; |
| 1236 | |
| 1237 | if (Imm >= 4096) { |
| 1238 | if (Imm <= 4095 + 64) { |
| 1239 | // Use an SOffset inline constant for 1..64 |
| 1240 | Overflow = Imm - 4095; |
| 1241 | Imm = 4095; |
| 1242 | } else { |
| 1243 | // Try to keep the same value in SOffset for adjacent loads, so that |
| 1244 | // the corresponding register contents can be re-used. |
| 1245 | // |
| 1246 | // Load values with all low-bits set into SOffset, so that a larger |
| 1247 | // range of values can be covered using s_movk_i32 |
| 1248 | uint32_t High = (Imm + 1) & ~4095; |
| 1249 | uint32_t Low = (Imm + 1) & 4095; |
| 1250 | Imm = Low; |
| 1251 | Overflow = High - 1; |
| 1252 | } |
| 1253 | } |
| 1254 | |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 1255 | // There is a hardware bug in SI and CI which prevents address clamping in |
| 1256 | // MUBUF instructions from working correctly with SOffsets. The immediate |
| 1257 | // offset is unaffected. |
| 1258 | if (Overflow > 0 && |
| 1259 | Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) |
| 1260 | return false; |
| 1261 | |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 1262 | ImmOffset = CurDAG->getTargetConstant(Imm, DL, MVT::i16); |
| 1263 | |
| 1264 | if (Overflow <= 64) |
| 1265 | SOffset = CurDAG->getTargetConstant(Overflow, DL, MVT::i32); |
| 1266 | else |
| 1267 | SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, |
| 1268 | CurDAG->getTargetConstant(Overflow, DL, MVT::i32)), |
| 1269 | 0); |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 1270 | |
| 1271 | return true; |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicOffset(SDValue Offset, |
| 1275 | SDValue &SOffset, |
| 1276 | SDValue &ImmOffset) const { |
| 1277 | SDLoc DL(Offset); |
| 1278 | |
| 1279 | if (!isa<ConstantSDNode>(Offset)) |
| 1280 | return false; |
| 1281 | |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 1282 | return SelectMUBUFConstant(Offset, SOffset, ImmOffset); |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicVOffset(SDValue Offset, |
| 1286 | SDValue &SOffset, |
| 1287 | SDValue &ImmOffset, |
| 1288 | SDValue &VOffset) const { |
| 1289 | SDLoc DL(Offset); |
| 1290 | |
| 1291 | // Don't generate an unnecessary voffset for constant offsets. |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 1292 | if (isa<ConstantSDNode>(Offset)) { |
| 1293 | SDValue Tmp1, Tmp2; |
| 1294 | |
| 1295 | // When necessary, use a voffset in <= CI anyway to work around a hardware |
| 1296 | // bug. |
| 1297 | if (Subtarget->getGeneration() > AMDGPUSubtarget::SEA_ISLANDS || |
| 1298 | SelectMUBUFConstant(Offset, Tmp1, Tmp2)) |
| 1299 | return false; |
| 1300 | } |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 1301 | |
| 1302 | if (CurDAG->isBaseWithConstantOffset(Offset)) { |
| 1303 | SDValue N0 = Offset.getOperand(0); |
| 1304 | SDValue N1 = Offset.getOperand(1); |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 1305 | if (cast<ConstantSDNode>(N1)->getSExtValue() >= 0 && |
| 1306 | SelectMUBUFConstant(N1, SOffset, ImmOffset)) { |
| 1307 | VOffset = N0; |
| 1308 | return true; |
| 1309 | } |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Nicolai Haehnle | a609259 | 2016-06-15 07:13:05 +0000 | [diff] [blame] | 1312 | SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); |
| 1313 | ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); |
| 1314 | VOffset = Offset; |
| 1315 | |
Nicolai Haehnle | 3003ba0 | 2016-03-18 16:24:20 +0000 | [diff] [blame] | 1316 | return true; |
| 1317 | } |
| 1318 | |
Matt Arsenault | db7c6a8 | 2017-06-12 16:53:51 +0000 | [diff] [blame] | 1319 | bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDValue Addr, |
| 1320 | SDValue &VAddr, |
| 1321 | SDValue &Offset, |
| 1322 | SDValue &SLC) const { |
| 1323 | int64_t OffsetVal = 0; |
| 1324 | |
| 1325 | if (Subtarget->hasFlatInstOffsets() && |
| 1326 | CurDAG->isBaseWithConstantOffset(Addr)) { |
| 1327 | SDValue N0 = Addr.getOperand(0); |
| 1328 | SDValue N1 = Addr.getOperand(1); |
| 1329 | uint64_t COffsetVal = cast<ConstantSDNode>(N1)->getZExtValue(); |
| 1330 | if (isUInt<12>(COffsetVal)) { |
| 1331 | Addr = N0; |
| 1332 | OffsetVal = COffsetVal; |
| 1333 | } |
| 1334 | } |
| 1335 | |
Matt Arsenault | 7757c59 | 2016-06-09 23:42:54 +0000 | [diff] [blame] | 1336 | VAddr = Addr; |
Matt Arsenault | db7c6a8 | 2017-06-12 16:53:51 +0000 | [diff] [blame] | 1337 | Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i16); |
Matt Arsenault | 47ccafe | 2017-05-11 17:38:33 +0000 | [diff] [blame] | 1338 | SLC = CurDAG->getTargetConstant(0, SDLoc(), MVT::i1); |
Matt Arsenault | db7c6a8 | 2017-06-12 16:53:51 +0000 | [diff] [blame] | 1339 | |
Matt Arsenault | 7757c59 | 2016-06-09 23:42:54 +0000 | [diff] [blame] | 1340 | return true; |
| 1341 | } |
| 1342 | |
Matt Arsenault | db7c6a8 | 2017-06-12 16:53:51 +0000 | [diff] [blame] | 1343 | bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDValue Addr, |
| 1344 | SDValue &VAddr, |
| 1345 | SDValue &Offset, |
| 1346 | SDValue &SLC) const { |
| 1347 | return SelectFlatOffset(Addr, VAddr, Offset, SLC); |
| 1348 | } |
| 1349 | |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1350 | bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode, |
| 1351 | SDValue &Offset, bool &Imm) const { |
| 1352 | |
| 1353 | // FIXME: Handle non-constant offsets. |
| 1354 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode); |
| 1355 | if (!C) |
| 1356 | return false; |
| 1357 | |
| 1358 | SDLoc SL(ByteOffsetNode); |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1359 | AMDGPUSubtarget::Generation Gen = Subtarget->getGeneration(); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1360 | int64_t ByteOffset = C->getSExtValue(); |
Tom Stellard | 08efb7e | 2017-01-27 18:41:14 +0000 | [diff] [blame] | 1361 | int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1362 | |
Tom Stellard | 08efb7e | 2017-01-27 18:41:14 +0000 | [diff] [blame] | 1363 | if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) { |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1364 | Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); |
| 1365 | Imm = true; |
| 1366 | return true; |
| 1367 | } |
| 1368 | |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1369 | if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset)) |
| 1370 | return false; |
| 1371 | |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1372 | if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) { |
| 1373 | // 32-bit Immediates are supported on Sea Islands. |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1374 | Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); |
| 1375 | } else { |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1376 | SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32); |
| 1377 | Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, |
| 1378 | C32Bit), 0); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1379 | } |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1380 | Imm = false; |
| 1381 | return true; |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase, |
| 1385 | SDValue &Offset, bool &Imm) const { |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1386 | SDLoc SL(Addr); |
| 1387 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 1388 | SDValue N0 = Addr.getOperand(0); |
| 1389 | SDValue N1 = Addr.getOperand(1); |
| 1390 | |
| 1391 | if (SelectSMRDOffset(N1, Offset, Imm)) { |
| 1392 | SBase = N0; |
| 1393 | return true; |
| 1394 | } |
| 1395 | } |
| 1396 | SBase = Addr; |
| 1397 | Offset = CurDAG->getTargetConstant(0, SL, MVT::i32); |
| 1398 | Imm = true; |
| 1399 | return true; |
| 1400 | } |
| 1401 | |
| 1402 | bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase, |
| 1403 | SDValue &Offset) const { |
| 1404 | bool Imm; |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1405 | return SelectSMRD(Addr, SBase, Offset, Imm) && Imm; |
| 1406 | } |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1407 | |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1408 | bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase, |
| 1409 | SDValue &Offset) const { |
| 1410 | |
| 1411 | if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) |
| 1412 | return false; |
| 1413 | |
| 1414 | bool Imm; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1415 | if (!SelectSMRD(Addr, SBase, Offset, Imm)) |
| 1416 | return false; |
| 1417 | |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1418 | return !Imm && isa<ConstantSDNode>(Offset); |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1421 | bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase, |
| 1422 | SDValue &Offset) const { |
| 1423 | bool Imm; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1424 | return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm && |
| 1425 | !isa<ConstantSDNode>(Offset); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr, |
| 1429 | SDValue &Offset) const { |
| 1430 | bool Imm; |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1431 | return SelectSMRDOffset(Addr, Offset, Imm) && Imm; |
| 1432 | } |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1433 | |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1434 | bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr, |
| 1435 | SDValue &Offset) const { |
| 1436 | if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) |
| 1437 | return false; |
| 1438 | |
| 1439 | bool Imm; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1440 | if (!SelectSMRDOffset(Addr, Offset, Imm)) |
| 1441 | return false; |
| 1442 | |
Marek Olsak | 8973a0a | 2017-05-24 14:53:50 +0000 | [diff] [blame] | 1443 | return !Imm && isa<ConstantSDNode>(Offset); |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1446 | bool AMDGPUDAGToDAGISel::SelectSMRDBufferSgpr(SDValue Addr, |
| 1447 | SDValue &Offset) const { |
| 1448 | bool Imm; |
Tom Stellard | 217361c | 2015-08-06 19:28:38 +0000 | [diff] [blame] | 1449 | return SelectSMRDOffset(Addr, Offset, Imm) && !Imm && |
| 1450 | !isa<ConstantSDNode>(Offset); |
Tom Stellard | dee26a2 | 2015-08-06 19:28:30 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Nicolai Haehnle | 7968c34 | 2016-07-12 08:12:16 +0000 | [diff] [blame] | 1453 | bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index, |
| 1454 | SDValue &Base, |
| 1455 | SDValue &Offset) const { |
Matt Arsenault | 1322b6f | 2016-07-09 01:13:56 +0000 | [diff] [blame] | 1456 | SDLoc DL(Index); |
| 1457 | |
| 1458 | if (CurDAG->isBaseWithConstantOffset(Index)) { |
| 1459 | SDValue N0 = Index.getOperand(0); |
| 1460 | SDValue N1 = Index.getOperand(1); |
| 1461 | ConstantSDNode *C1 = cast<ConstantSDNode>(N1); |
| 1462 | |
| 1463 | // (add n0, c0) |
| 1464 | Base = N0; |
| 1465 | Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32); |
| 1466 | return true; |
| 1467 | } |
| 1468 | |
Nicolai Haehnle | 7968c34 | 2016-07-12 08:12:16 +0000 | [diff] [blame] | 1469 | if (isa<ConstantSDNode>(Index)) |
| 1470 | return false; |
Matt Arsenault | 1322b6f | 2016-07-09 01:13:56 +0000 | [diff] [blame] | 1471 | |
| 1472 | Base = Index; |
| 1473 | Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); |
| 1474 | return true; |
| 1475 | } |
| 1476 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1477 | SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL, |
| 1478 | SDValue Val, uint32_t Offset, |
| 1479 | uint32_t Width) { |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1480 | // Transformation function, pack the offset and width of a BFE into |
| 1481 | // the format expected by the S_BFE_I32 / S_BFE_U32. In the second |
| 1482 | // source, bits [5:0] contain the offset and bits [22:16] the width. |
| 1483 | uint32_t PackedVal = Offset | (Width << 16); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1484 | SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32); |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1485 | |
| 1486 | return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst); |
| 1487 | } |
| 1488 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1489 | void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) { |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1490 | // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c) |
| 1491 | // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c) |
| 1492 | // Predicate: 0 < b <= c < 32 |
| 1493 | |
| 1494 | const SDValue &Shl = N->getOperand(0); |
| 1495 | ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1)); |
| 1496 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 1497 | |
| 1498 | if (B && C) { |
| 1499 | uint32_t BVal = B->getZExtValue(); |
| 1500 | uint32_t CVal = C->getZExtValue(); |
| 1501 | |
| 1502 | if (0 < BVal && BVal <= CVal && CVal < 32) { |
| 1503 | bool Signed = N->getOpcode() == ISD::SRA; |
| 1504 | unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32; |
| 1505 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1506 | ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal, |
| 1507 | 32 - CVal)); |
| 1508 | return; |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1511 | SelectCode(N); |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1514 | void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) { |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1515 | switch (N->getOpcode()) { |
| 1516 | case ISD::AND: |
| 1517 | if (N->getOperand(0).getOpcode() == ISD::SRL) { |
| 1518 | // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)" |
| 1519 | // Predicate: isMask(mask) |
| 1520 | const SDValue &Srl = N->getOperand(0); |
| 1521 | ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1)); |
| 1522 | ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 1523 | |
| 1524 | if (Shift && Mask) { |
| 1525 | uint32_t ShiftVal = Shift->getZExtValue(); |
| 1526 | uint32_t MaskVal = Mask->getZExtValue(); |
| 1527 | |
| 1528 | if (isMask_32(MaskVal)) { |
| 1529 | uint32_t WidthVal = countPopulation(MaskVal); |
| 1530 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1531 | ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), |
| 1532 | Srl.getOperand(0), ShiftVal, WidthVal)); |
| 1533 | return; |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | break; |
| 1538 | case ISD::SRL: |
| 1539 | if (N->getOperand(0).getOpcode() == ISD::AND) { |
| 1540 | // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)" |
| 1541 | // Predicate: isMask(mask >> b) |
| 1542 | const SDValue &And = N->getOperand(0); |
| 1543 | ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 1544 | ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1)); |
| 1545 | |
| 1546 | if (Shift && Mask) { |
| 1547 | uint32_t ShiftVal = Shift->getZExtValue(); |
| 1548 | uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal; |
| 1549 | |
| 1550 | if (isMask_32(MaskVal)) { |
| 1551 | uint32_t WidthVal = countPopulation(MaskVal); |
| 1552 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1553 | ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), |
| 1554 | And.getOperand(0), ShiftVal, WidthVal)); |
| 1555 | return; |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1558 | } else if (N->getOperand(0).getOpcode() == ISD::SHL) { |
| 1559 | SelectS_BFEFromShifts(N); |
| 1560 | return; |
| 1561 | } |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1562 | break; |
| 1563 | case ISD::SRA: |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1564 | if (N->getOperand(0).getOpcode() == ISD::SHL) { |
| 1565 | SelectS_BFEFromShifts(N); |
| 1566 | return; |
| 1567 | } |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1568 | break; |
Matt Arsenault | 7e8de01 | 2016-04-22 22:59:16 +0000 | [diff] [blame] | 1569 | |
| 1570 | case ISD::SIGN_EXTEND_INREG: { |
| 1571 | // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8 |
| 1572 | SDValue Src = N->getOperand(0); |
| 1573 | if (Src.getOpcode() != ISD::SRL) |
| 1574 | break; |
| 1575 | |
| 1576 | const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1)); |
| 1577 | if (!Amt) |
| 1578 | break; |
| 1579 | |
| 1580 | unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits(); |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1581 | ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0), |
| 1582 | Amt->getZExtValue(), Width)); |
| 1583 | return; |
Matt Arsenault | 7e8de01 | 2016-04-22 22:59:16 +0000 | [diff] [blame] | 1584 | } |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1587 | SelectCode(N); |
Marek Olsak | 9b72868 | 2015-03-24 13:40:27 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Matt Arsenault | 7b1dc2c | 2016-09-17 02:02:19 +0000 | [diff] [blame] | 1590 | bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const { |
| 1591 | assert(N->getOpcode() == ISD::BRCOND); |
| 1592 | if (!N->hasOneUse()) |
| 1593 | return false; |
| 1594 | |
| 1595 | SDValue Cond = N->getOperand(1); |
| 1596 | if (Cond.getOpcode() == ISD::CopyToReg) |
| 1597 | Cond = Cond.getOperand(2); |
| 1598 | |
| 1599 | if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse()) |
| 1600 | return false; |
| 1601 | |
| 1602 | MVT VT = Cond.getOperand(0).getSimpleValueType(); |
| 1603 | if (VT == MVT::i32) |
| 1604 | return true; |
| 1605 | |
| 1606 | if (VT == MVT::i64) { |
| 1607 | auto ST = static_cast<const SISubtarget *>(Subtarget); |
| 1608 | |
| 1609 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
| 1610 | return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64(); |
| 1611 | } |
| 1612 | |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1616 | void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) { |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1617 | SDValue Cond = N->getOperand(1); |
| 1618 | |
Matt Arsenault | 327188a | 2016-12-15 21:57:11 +0000 | [diff] [blame] | 1619 | if (Cond.isUndef()) { |
| 1620 | CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other, |
| 1621 | N->getOperand(2), N->getOperand(0)); |
| 1622 | return; |
| 1623 | } |
| 1624 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1625 | if (isCBranchSCC(N)) { |
| 1626 | // This brcond will use S_CBRANCH_SCC*, so let tablegen handle it. |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1627 | SelectCode(N); |
| 1628 | return; |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1631 | SDLoc SL(N); |
| 1632 | |
Matt Arsenault | f530e8b | 2016-11-07 19:09:33 +0000 | [diff] [blame] | 1633 | SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, AMDGPU::VCC, Cond); |
Justin Bogner | 95927c0 | 2016-05-12 21:03:32 +0000 | [diff] [blame] | 1634 | CurDAG->SelectNodeTo(N, AMDGPU::S_CBRANCH_VCCNZ, MVT::Other, |
| 1635 | N->getOperand(2), // Basic Block |
Matt Arsenault | f530e8b | 2016-11-07 19:09:33 +0000 | [diff] [blame] | 1636 | VCC.getValue(0)); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 1639 | // This is here because there isn't a way to use the generated sub0_sub1 as the |
| 1640 | // subreg index to EXTRACT_SUBREG in tablegen. |
| 1641 | void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) { |
| 1642 | MemSDNode *Mem = cast<MemSDNode>(N); |
| 1643 | unsigned AS = Mem->getAddressSpace(); |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 1644 | if (AS == AMDGPUASI.FLAT_ADDRESS) { |
Matt Arsenault | 7757c59 | 2016-06-09 23:42:54 +0000 | [diff] [blame] | 1645 | SelectCode(N); |
| 1646 | return; |
| 1647 | } |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 1648 | |
| 1649 | MVT VT = N->getSimpleValueType(0); |
| 1650 | bool Is32 = (VT == MVT::i32); |
| 1651 | SDLoc SL(N); |
| 1652 | |
| 1653 | MachineSDNode *CmpSwap = nullptr; |
| 1654 | if (Subtarget->hasAddr64()) { |
| 1655 | SDValue SRsrc, VAddr, SOffset, Offset, GLC, SLC; |
| 1656 | |
| 1657 | if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) { |
Matt Arsenault | e5456ce | 2017-07-20 21:06:04 +0000 | [diff] [blame^] | 1658 | unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN : |
| 1659 | AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN; |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 1660 | SDValue CmpVal = Mem->getOperand(2); |
| 1661 | |
| 1662 | // XXX - Do we care about glue operands? |
| 1663 | |
| 1664 | SDValue Ops[] = { |
| 1665 | CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain() |
| 1666 | }; |
| 1667 | |
| 1668 | CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | if (!CmpSwap) { |
| 1673 | SDValue SRsrc, SOffset, Offset, SLC; |
| 1674 | if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) { |
Matt Arsenault | e5456ce | 2017-07-20 21:06:04 +0000 | [diff] [blame^] | 1675 | unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN : |
| 1676 | AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN; |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 1677 | |
| 1678 | SDValue CmpVal = Mem->getOperand(2); |
| 1679 | SDValue Ops[] = { |
| 1680 | CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain() |
| 1681 | }; |
| 1682 | |
| 1683 | CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | if (!CmpSwap) { |
| 1688 | SelectCode(N); |
| 1689 | return; |
| 1690 | } |
| 1691 | |
| 1692 | MachineSDNode::mmo_iterator MMOs = MF->allocateMemRefsArray(1); |
| 1693 | *MMOs = Mem->getMemOperand(); |
| 1694 | CmpSwap->setMemRefs(MMOs, MMOs + 1); |
| 1695 | |
| 1696 | unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1; |
| 1697 | SDValue Extract |
| 1698 | = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0)); |
| 1699 | |
| 1700 | ReplaceUses(SDValue(N, 0), Extract); |
| 1701 | ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1)); |
| 1702 | CurDAG->RemoveDeadNode(N); |
| 1703 | } |
| 1704 | |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1705 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src, |
| 1706 | SDValue &SrcMods) const { |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1707 | unsigned Mods = 0; |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1708 | Src = In; |
| 1709 | |
| 1710 | if (Src.getOpcode() == ISD::FNEG) { |
| 1711 | Mods |= SISrcMods::NEG; |
| 1712 | Src = Src.getOperand(0); |
| 1713 | } |
| 1714 | |
| 1715 | if (Src.getOpcode() == ISD::FABS) { |
| 1716 | Mods |= SISrcMods::ABS; |
| 1717 | Src = Src.getOperand(0); |
| 1718 | } |
| 1719 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1720 | SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1721 | return true; |
| 1722 | } |
| 1723 | |
Matt Arsenault | f84e5d9 | 2017-01-31 03:07:46 +0000 | [diff] [blame] | 1724 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, |
| 1725 | SDValue &SrcMods) const { |
| 1726 | SelectVOP3Mods(In, Src, SrcMods); |
| 1727 | return isNoNanSrc(Src); |
| 1728 | } |
| 1729 | |
Matt Arsenault | df58e82 | 2017-04-25 21:17:38 +0000 | [diff] [blame] | 1730 | bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const { |
| 1731 | if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG) |
| 1732 | return false; |
| 1733 | |
| 1734 | Src = In; |
| 1735 | return true; |
Tom Stellard | db5a11f | 2015-07-13 15:47:57 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1738 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src, |
| 1739 | SDValue &SrcMods, SDValue &Clamp, |
| 1740 | SDValue &Omod) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1741 | SDLoc DL(In); |
Matt Arsenault | df58e82 | 2017-04-25 21:17:38 +0000 | [diff] [blame] | 1742 | Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 1743 | Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); |
Tom Stellard | b4a313a | 2014-08-01 00:32:39 +0000 | [diff] [blame] | 1744 | |
| 1745 | return SelectVOP3Mods(In, Src, SrcMods); |
| 1746 | } |
| 1747 | |
Matt Arsenault | 4831ce5 | 2015-01-06 23:00:37 +0000 | [diff] [blame] | 1748 | bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, |
| 1749 | SDValue &SrcMods, |
| 1750 | SDValue &Clamp, |
| 1751 | SDValue &Omod) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1752 | Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); |
Matt Arsenault | 4831ce5 | 2015-01-06 23:00:37 +0000 | [diff] [blame] | 1753 | return SelectVOP3Mods(In, Src, SrcMods); |
| 1754 | } |
| 1755 | |
Dmitry Preobrazhensky | c512d44 | 2017-03-27 15:57:17 +0000 | [diff] [blame] | 1756 | bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src, |
| 1757 | SDValue &Clamp, SDValue &Omod) const { |
| 1758 | Src = In; |
| 1759 | |
| 1760 | SDLoc DL(In); |
Matt Arsenault | df58e82 | 2017-04-25 21:17:38 +0000 | [diff] [blame] | 1761 | Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); |
| 1762 | Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); |
Dmitry Preobrazhensky | c512d44 | 2017-03-27 15:57:17 +0000 | [diff] [blame] | 1763 | |
| 1764 | return true; |
| 1765 | } |
| 1766 | |
Matt Arsenault | 98f2946 | 2017-05-17 20:30:58 +0000 | [diff] [blame] | 1767 | static SDValue stripBitcast(SDValue Val) { |
| 1768 | return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val; |
| 1769 | } |
| 1770 | |
| 1771 | // Figure out if this is really an extract of the high 16-bits of a dword. |
| 1772 | static bool isExtractHiElt(SDValue In, SDValue &Out) { |
| 1773 | In = stripBitcast(In); |
| 1774 | if (In.getOpcode() != ISD::TRUNCATE) |
| 1775 | return false; |
| 1776 | |
| 1777 | SDValue Srl = In.getOperand(0); |
| 1778 | if (Srl.getOpcode() == ISD::SRL) { |
| 1779 | if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) { |
| 1780 | if (ShiftAmt->getZExtValue() == 16) { |
| 1781 | Out = stripBitcast(Srl.getOperand(0)); |
| 1782 | return true; |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | return false; |
| 1788 | } |
| 1789 | |
| 1790 | // Look through operations that obscure just looking at the low 16-bits of the |
| 1791 | // same register. |
| 1792 | static SDValue stripExtractLoElt(SDValue In) { |
| 1793 | if (In.getOpcode() == ISD::TRUNCATE) { |
| 1794 | SDValue Src = In.getOperand(0); |
| 1795 | if (Src.getValueType().getSizeInBits() == 32) |
| 1796 | return stripBitcast(Src); |
| 1797 | } |
| 1798 | |
| 1799 | return In; |
| 1800 | } |
| 1801 | |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 1802 | bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src, |
| 1803 | SDValue &SrcMods) const { |
| 1804 | unsigned Mods = 0; |
| 1805 | Src = In; |
| 1806 | |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 1807 | if (Src.getOpcode() == ISD::FNEG) { |
Matt Arsenault | 786eeea | 2017-05-17 20:00:00 +0000 | [diff] [blame] | 1808 | Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI); |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 1809 | Src = Src.getOperand(0); |
| 1810 | } |
| 1811 | |
Matt Arsenault | 786eeea | 2017-05-17 20:00:00 +0000 | [diff] [blame] | 1812 | if (Src.getOpcode() == ISD::BUILD_VECTOR) { |
| 1813 | unsigned VecMods = Mods; |
| 1814 | |
Matt Arsenault | 98f2946 | 2017-05-17 20:30:58 +0000 | [diff] [blame] | 1815 | SDValue Lo = stripBitcast(Src.getOperand(0)); |
| 1816 | SDValue Hi = stripBitcast(Src.getOperand(1)); |
Matt Arsenault | 786eeea | 2017-05-17 20:00:00 +0000 | [diff] [blame] | 1817 | |
| 1818 | if (Lo.getOpcode() == ISD::FNEG) { |
Matt Arsenault | 98f2946 | 2017-05-17 20:30:58 +0000 | [diff] [blame] | 1819 | Lo = stripBitcast(Lo.getOperand(0)); |
Matt Arsenault | 786eeea | 2017-05-17 20:00:00 +0000 | [diff] [blame] | 1820 | Mods ^= SISrcMods::NEG; |
| 1821 | } |
| 1822 | |
| 1823 | if (Hi.getOpcode() == ISD::FNEG) { |
Matt Arsenault | 98f2946 | 2017-05-17 20:30:58 +0000 | [diff] [blame] | 1824 | Hi = stripBitcast(Hi.getOperand(0)); |
Matt Arsenault | 786eeea | 2017-05-17 20:00:00 +0000 | [diff] [blame] | 1825 | Mods ^= SISrcMods::NEG_HI; |
| 1826 | } |
| 1827 | |
Matt Arsenault | 98f2946 | 2017-05-17 20:30:58 +0000 | [diff] [blame] | 1828 | if (isExtractHiElt(Lo, Lo)) |
| 1829 | Mods |= SISrcMods::OP_SEL_0; |
| 1830 | |
| 1831 | if (isExtractHiElt(Hi, Hi)) |
| 1832 | Mods |= SISrcMods::OP_SEL_1; |
| 1833 | |
| 1834 | Lo = stripExtractLoElt(Lo); |
| 1835 | Hi = stripExtractLoElt(Hi); |
| 1836 | |
Matt Arsenault | 786eeea | 2017-05-17 20:00:00 +0000 | [diff] [blame] | 1837 | if (Lo == Hi && !isInlineImmediate(Lo.getNode())) { |
| 1838 | // Really a scalar input. Just select from the low half of the register to |
| 1839 | // avoid packing. |
| 1840 | |
| 1841 | Src = Lo; |
| 1842 | SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); |
| 1843 | return true; |
| 1844 | } |
| 1845 | |
| 1846 | Mods = VecMods; |
| 1847 | } |
| 1848 | |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 1849 | // Packed instructions do not have abs modifiers. |
Matt Arsenault | eb522e6 | 2017-02-27 22:15:25 +0000 | [diff] [blame] | 1850 | Mods |= SISrcMods::OP_SEL_1; |
| 1851 | |
| 1852 | SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); |
| 1853 | return true; |
| 1854 | } |
| 1855 | |
| 1856 | bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src, |
| 1857 | SDValue &SrcMods, |
| 1858 | SDValue &Clamp) const { |
| 1859 | SDLoc SL(In); |
| 1860 | |
| 1861 | // FIXME: Handle clamp and op_sel |
| 1862 | Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32); |
| 1863 | |
| 1864 | return SelectVOP3PMods(In, Src, SrcMods); |
| 1865 | } |
| 1866 | |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1867 | void AMDGPUDAGToDAGISel::PostprocessISelDAG() { |
Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1868 | const AMDGPUTargetLowering& Lowering = |
Matt Arsenault | 209a7b9 | 2014-04-18 07:40:20 +0000 | [diff] [blame] | 1869 | *static_cast<const AMDGPUTargetLowering*>(getTargetLowering()); |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1870 | bool IsModified = false; |
| 1871 | do { |
| 1872 | IsModified = false; |
| 1873 | // 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] | 1874 | for (SDNode &Node : CurDAG->allnodes()) { |
| 1875 | MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node); |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1876 | if (!MachineNode) |
| 1877 | continue; |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1878 | |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1879 | SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG); |
Pete Cooper | 65c6940 | 2015-07-14 22:10:54 +0000 | [diff] [blame] | 1880 | if (ResNode != &Node) { |
| 1881 | ReplaceUses(&Node, ResNode); |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1882 | IsModified = true; |
| 1883 | } |
Tom Stellard | 2183b70 | 2013-06-03 17:39:46 +0000 | [diff] [blame] | 1884 | } |
Vincent Lejeune | ab3baf8 | 2013-09-12 23:44:44 +0000 | [diff] [blame] | 1885 | CurDAG->RemoveDeadNodes(); |
| 1886 | } while (IsModified); |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1887 | } |