Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===- NVPTXInstrInfo.cpp - NVPTX Instruction Information -----------------===// |
| 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 | // This file contains the NVPTX implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "NVPTX.h" |
| 15 | #include "NVPTXInstrInfo.h" |
| 16 | #include "NVPTXTargetMachine.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 22 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 25 | #define GET_INSTRINFO_CTOR_DTOR |
| 26 | #include "NVPTXGenInstrInfo.inc" |
| 27 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 28 | // Pin the vtable to this file. |
| 29 | void NVPTXInstrInfo::anchor() {} |
| 30 | |
Eric Christopher | 02389e3 | 2015-02-19 00:08:27 +0000 | [diff] [blame] | 31 | NVPTXInstrInfo::NVPTXInstrInfo() : NVPTXGenInstrInfo(), RegInfo() {} |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 32 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 33 | void NVPTXInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 34 | MachineBasicBlock::iterator I, |
| 35 | const DebugLoc &DL, unsigned DestReg, |
| 36 | unsigned SrcReg, bool KillSrc) const { |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 37 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 38 | const TargetRegisterClass *DestRC = MRI.getRegClass(DestReg); |
| 39 | const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg); |
| 40 | |
Jingyue Wu | ffa09be | 2015-08-01 18:02:12 +0000 | [diff] [blame] | 41 | if (DestRC->getSize() != SrcRC->getSize()) |
| 42 | report_fatal_error("Copy one register into another with a different width"); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 43 | |
Jingyue Wu | ffa09be | 2015-08-01 18:02:12 +0000 | [diff] [blame] | 44 | unsigned Op; |
| 45 | if (DestRC == &NVPTX::Int1RegsRegClass) { |
| 46 | Op = NVPTX::IMOV1rr; |
| 47 | } else if (DestRC == &NVPTX::Int16RegsRegClass) { |
| 48 | Op = NVPTX::IMOV16rr; |
| 49 | } else if (DestRC == &NVPTX::Int32RegsRegClass) { |
| 50 | Op = (SrcRC == &NVPTX::Int32RegsRegClass ? NVPTX::IMOV32rr |
| 51 | : NVPTX::BITCONVERT_32_F2I); |
| 52 | } else if (DestRC == &NVPTX::Int64RegsRegClass) { |
| 53 | Op = (SrcRC == &NVPTX::Int64RegsRegClass ? NVPTX::IMOV64rr |
| 54 | : NVPTX::BITCONVERT_64_F2I); |
Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 55 | } else if (DestRC == &NVPTX::Float16RegsRegClass) { |
| 56 | Op = (SrcRC == &NVPTX::Float16RegsRegClass ? NVPTX::FMOV16rr |
| 57 | : NVPTX::BITCONVERT_16_I2F); |
Jingyue Wu | ffa09be | 2015-08-01 18:02:12 +0000 | [diff] [blame] | 58 | } else if (DestRC == &NVPTX::Float32RegsRegClass) { |
| 59 | Op = (SrcRC == &NVPTX::Float32RegsRegClass ? NVPTX::FMOV32rr |
| 60 | : NVPTX::BITCONVERT_32_I2F); |
| 61 | } else if (DestRC == &NVPTX::Float64RegsRegClass) { |
| 62 | Op = (SrcRC == &NVPTX::Float64RegsRegClass ? NVPTX::FMOV64rr |
| 63 | : NVPTX::BITCONVERT_64_I2F); |
| 64 | } else { |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 65 | llvm_unreachable("Bad register copy"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 66 | } |
Jingyue Wu | ffa09be | 2015-08-01 18:02:12 +0000 | [diff] [blame] | 67 | BuildMI(MBB, I, DL, get(Op), DestReg) |
| 68 | .addReg(SrcReg, getKillRegState(KillSrc)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 71 | bool NVPTXInstrInfo::isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 72 | unsigned &DestReg) const { |
| 73 | // Look for the appropriate part of TSFlags |
| 74 | bool isMove = false; |
| 75 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 76 | unsigned TSFlags = |
| 77 | (MI.getDesc().TSFlags & NVPTX::SimpleMoveMask) >> NVPTX::SimpleMoveShift; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 78 | isMove = (TSFlags == 1); |
| 79 | |
| 80 | if (isMove) { |
| 81 | MachineOperand dest = MI.getOperand(0); |
| 82 | MachineOperand src = MI.getOperand(1); |
| 83 | assert(dest.isReg() && "dest of a movrr is not a reg"); |
| 84 | assert(src.isReg() && "src of a movrr is not a reg"); |
| 85 | |
| 86 | SrcReg = src.getReg(); |
| 87 | DestReg = dest.getReg(); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 94 | bool NVPTXInstrInfo::isLoadInstr(const MachineInstr &MI, |
| 95 | unsigned &AddrSpace) const { |
| 96 | bool isLoad = false; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 97 | unsigned TSFlags = |
| 98 | (MI.getDesc().TSFlags & NVPTX::isLoadMask) >> NVPTX::isLoadShift; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 99 | isLoad = (TSFlags == 1); |
| 100 | if (isLoad) |
| 101 | AddrSpace = getLdStCodeAddrSpace(MI); |
| 102 | return isLoad; |
| 103 | } |
| 104 | |
| 105 | bool NVPTXInstrInfo::isStoreInstr(const MachineInstr &MI, |
| 106 | unsigned &AddrSpace) const { |
| 107 | bool isStore = false; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 108 | unsigned TSFlags = |
| 109 | (MI.getDesc().TSFlags & NVPTX::isStoreMask) >> NVPTX::isStoreShift; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 110 | isStore = (TSFlags == 1); |
| 111 | if (isStore) |
| 112 | AddrSpace = getLdStCodeAddrSpace(MI); |
| 113 | return isStore; |
| 114 | } |
| 115 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 116 | /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning |
| 117 | /// true if it cannot be understood (e.g. it's a switch dispatch or isn't |
| 118 | /// implemented for a target). Upon success, this returns false and returns |
| 119 | /// with the following information in various cases: |
| 120 | /// |
| 121 | /// 1. If this block ends with no branches (it just falls through to its succ) |
| 122 | /// just return false, leaving TBB/FBB null. |
| 123 | /// 2. If this block ends with only an unconditional branch, it sets TBB to be |
| 124 | /// the destination block. |
| 125 | /// 3. If this block ends with an conditional branch and it falls through to |
| 126 | /// an successor block, it sets TBB to be the branch destination block and a |
| 127 | /// list of operands that evaluate the condition. These |
| 128 | /// operands can be passed to other TargetInstrInfo methods to create new |
| 129 | /// branches. |
| 130 | /// 4. If this block ends with an conditional branch and an unconditional |
| 131 | /// block, it returns the 'true' destination in TBB, the 'false' destination |
| 132 | /// in FBB, and a list of operands that evaluate the condition. These |
| 133 | /// operands can be passed to other TargetInstrInfo methods to create new |
| 134 | /// branches. |
| 135 | /// |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 136 | /// Note that removeBranch and insertBranch must be implemented to support |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 137 | /// cases where this method returns success. |
| 138 | /// |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 139 | bool NVPTXInstrInfo::analyzeBranch(MachineBasicBlock &MBB, |
| 140 | MachineBasicBlock *&TBB, |
| 141 | MachineBasicBlock *&FBB, |
| 142 | SmallVectorImpl<MachineOperand> &Cond, |
| 143 | bool AllowModify) const { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 144 | // If the block has no terminators, it just falls into the block after it. |
| 145 | MachineBasicBlock::iterator I = MBB.end(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 146 | if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 147 | return false; |
| 148 | |
| 149 | // Get the last instruction in the block. |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 150 | MachineInstr &LastInst = *I; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 151 | |
| 152 | // If there is only one terminator instruction, process it. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 153 | if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 154 | if (LastInst.getOpcode() == NVPTX::GOTO) { |
| 155 | TBB = LastInst.getOperand(0).getMBB(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 156 | return false; |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 157 | } else if (LastInst.getOpcode() == NVPTX::CBranch) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 158 | // Block ends with fall-through condbranch. |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 159 | TBB = LastInst.getOperand(1).getMBB(); |
| 160 | Cond.push_back(LastInst.getOperand(0)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 161 | return false; |
| 162 | } |
| 163 | // Otherwise, don't know what this is. |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | // Get the instruction before it if it's a terminator. |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 168 | MachineInstr &SecondLastInst = *I; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 169 | |
| 170 | // If there are three terminators, we don't know what sort of block this is. |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 171 | if (I != MBB.begin() && isUnpredicatedTerminator(*--I)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 172 | return true; |
| 173 | |
| 174 | // If the block ends with NVPTX::GOTO and NVPTX:CBranch, handle it. |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 175 | if (SecondLastInst.getOpcode() == NVPTX::CBranch && |
| 176 | LastInst.getOpcode() == NVPTX::GOTO) { |
| 177 | TBB = SecondLastInst.getOperand(1).getMBB(); |
| 178 | Cond.push_back(SecondLastInst.getOperand(0)); |
| 179 | FBB = LastInst.getOperand(0).getMBB(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // If the block ends with two NVPTX:GOTOs, handle it. The second one is not |
| 184 | // executed, so remove it. |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 185 | if (SecondLastInst.getOpcode() == NVPTX::GOTO && |
| 186 | LastInst.getOpcode() == NVPTX::GOTO) { |
| 187 | TBB = SecondLastInst.getOperand(0).getMBB(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 188 | I = LastInst; |
| 189 | if (AllowModify) |
| 190 | I->eraseFromParent(); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // Otherwise, can't handle this. |
| 195 | return true; |
| 196 | } |
| 197 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 198 | unsigned NVPTXInstrInfo::removeBranch(MachineBasicBlock &MBB, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 199 | int *BytesRemoved) const { |
| 200 | assert(!BytesRemoved && "code size not handled"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 201 | MachineBasicBlock::iterator I = MBB.end(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 202 | if (I == MBB.begin()) |
| 203 | return 0; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 204 | --I; |
| 205 | if (I->getOpcode() != NVPTX::GOTO && I->getOpcode() != NVPTX::CBranch) |
| 206 | return 0; |
| 207 | |
| 208 | // Remove the branch. |
| 209 | I->eraseFromParent(); |
| 210 | |
| 211 | I = MBB.end(); |
| 212 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 213 | if (I == MBB.begin()) |
| 214 | return 1; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 215 | --I; |
| 216 | if (I->getOpcode() != NVPTX::CBranch) |
| 217 | return 1; |
| 218 | |
| 219 | // Remove the branch. |
| 220 | I->eraseFromParent(); |
| 221 | return 2; |
| 222 | } |
| 223 | |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 224 | unsigned NVPTXInstrInfo::insertBranch(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 225 | MachineBasicBlock *TBB, |
| 226 | MachineBasicBlock *FBB, |
| 227 | ArrayRef<MachineOperand> Cond, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 228 | const DebugLoc &DL, |
| 229 | int *BytesAdded) const { |
| 230 | assert(!BytesAdded && "code size not handled"); |
| 231 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 232 | // Shouldn't be a fall through. |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 233 | assert(TBB && "insertBranch must not be told to insert a fallthrough"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 234 | assert((Cond.size() == 1 || Cond.size() == 0) && |
| 235 | "NVPTX branch conditions have two components!"); |
| 236 | |
| 237 | // One-way branch. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 238 | if (!FBB) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 239 | if (Cond.empty()) // Unconditional branch |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 240 | BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(TBB); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 241 | else // Conditional branch |
| 242 | BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg()) |
| 243 | .addMBB(TBB); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 244 | return 1; |
| 245 | } |
| 246 | |
| 247 | // Two-way Conditional Branch. |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 248 | BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg()).addMBB(TBB); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 249 | BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(FBB); |
| 250 | return 2; |
| 251 | } |