1 //===- NVPTXInstrInfo.cpp - NVPTX Instruction Information -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the NVPTX implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "NVPTXInstrInfo.h"
16 #include "NVPTXTargetMachine.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/IR/Function.h"
25 #define GET_INSTRINFO_CTOR_DTOR
26 #include "NVPTXGenInstrInfo.inc"
28 // Pin the vtable to this file.
29 void NVPTXInstrInfo::anchor() {}
31 NVPTXInstrInfo::NVPTXInstrInfo() : NVPTXGenInstrInfo(), RegInfo() {}
33 void NVPTXInstrInfo::copyPhysReg(
34 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, DebugLoc DL,
35 unsigned DestReg, unsigned SrcReg, bool KillSrc) const {
36 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
37 const TargetRegisterClass *DestRC = MRI.getRegClass(DestReg);
38 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
40 if (DestRC->getSize() != SrcRC->getSize())
41 report_fatal_error("Copy one register into another with a different width");
44 if (DestRC == &NVPTX::Int1RegsRegClass) {
46 } else if (DestRC == &NVPTX::Int16RegsRegClass) {
48 } else if (DestRC == &NVPTX::Int32RegsRegClass) {
49 Op = (SrcRC == &NVPTX::Int32RegsRegClass ? NVPTX::IMOV32rr
50 : NVPTX::BITCONVERT_32_F2I);
51 } else if (DestRC == &NVPTX::Int64RegsRegClass) {
52 Op = (SrcRC == &NVPTX::Int64RegsRegClass ? NVPTX::IMOV64rr
53 : NVPTX::BITCONVERT_64_F2I);
54 } else if (DestRC == &NVPTX::Float32RegsRegClass) {
55 Op = (SrcRC == &NVPTX::Float32RegsRegClass ? NVPTX::FMOV32rr
56 : NVPTX::BITCONVERT_32_I2F);
57 } else if (DestRC == &NVPTX::Float64RegsRegClass) {
58 Op = (SrcRC == &NVPTX::Float64RegsRegClass ? NVPTX::FMOV64rr
59 : NVPTX::BITCONVERT_64_I2F);
61 llvm_unreachable("Bad register copy");
63 BuildMI(MBB, I, DL, get(Op), DestReg)
64 .addReg(SrcReg, getKillRegState(KillSrc));
67 bool NVPTXInstrInfo::isMoveInstr(const MachineInstr &MI, unsigned &SrcReg,
68 unsigned &DestReg) const {
69 // Look for the appropriate part of TSFlags
73 (MI.getDesc().TSFlags & NVPTX::SimpleMoveMask) >> NVPTX::SimpleMoveShift;
74 isMove = (TSFlags == 1);
77 MachineOperand dest = MI.getOperand(0);
78 MachineOperand src = MI.getOperand(1);
79 assert(dest.isReg() && "dest of a movrr is not a reg");
80 assert(src.isReg() && "src of a movrr is not a reg");
82 SrcReg = src.getReg();
83 DestReg = dest.getReg();
90 bool NVPTXInstrInfo::isReadSpecialReg(MachineInstr &MI) const {
91 switch (MI.getOpcode()) {
94 case NVPTX::INT_PTX_SREG_NTID_X:
95 case NVPTX::INT_PTX_SREG_NTID_Y:
96 case NVPTX::INT_PTX_SREG_NTID_Z:
97 case NVPTX::INT_PTX_SREG_TID_X:
98 case NVPTX::INT_PTX_SREG_TID_Y:
99 case NVPTX::INT_PTX_SREG_TID_Z:
100 case NVPTX::INT_PTX_SREG_CTAID_X:
101 case NVPTX::INT_PTX_SREG_CTAID_Y:
102 case NVPTX::INT_PTX_SREG_CTAID_Z:
103 case NVPTX::INT_PTX_SREG_NCTAID_X:
104 case NVPTX::INT_PTX_SREG_NCTAID_Y:
105 case NVPTX::INT_PTX_SREG_NCTAID_Z:
106 case NVPTX::INT_PTX_SREG_WARPSIZE:
111 bool NVPTXInstrInfo::isLoadInstr(const MachineInstr &MI,
112 unsigned &AddrSpace) const {
115 (MI.getDesc().TSFlags & NVPTX::isLoadMask) >> NVPTX::isLoadShift;
116 isLoad = (TSFlags == 1);
118 AddrSpace = getLdStCodeAddrSpace(MI);
122 bool NVPTXInstrInfo::isStoreInstr(const MachineInstr &MI,
123 unsigned &AddrSpace) const {
124 bool isStore = false;
126 (MI.getDesc().TSFlags & NVPTX::isStoreMask) >> NVPTX::isStoreShift;
127 isStore = (TSFlags == 1);
129 AddrSpace = getLdStCodeAddrSpace(MI);
133 bool NVPTXInstrInfo::CanTailMerge(const MachineInstr *MI) const {
134 unsigned addrspace = 0;
135 if (MI->getOpcode() == NVPTX::INT_CUDA_SYNCTHREADS)
137 if (isLoadInstr(*MI, addrspace))
138 if (addrspace == NVPTX::PTXLdStInstCode::SHARED)
140 if (isStoreInstr(*MI, addrspace))
141 if (addrspace == NVPTX::PTXLdStInstCode::SHARED)
146 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
147 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
148 /// implemented for a target). Upon success, this returns false and returns
149 /// with the following information in various cases:
151 /// 1. If this block ends with no branches (it just falls through to its succ)
152 /// just return false, leaving TBB/FBB null.
153 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
154 /// the destination block.
155 /// 3. If this block ends with an conditional branch and it falls through to
156 /// an successor block, it sets TBB to be the branch destination block and a
157 /// list of operands that evaluate the condition. These
158 /// operands can be passed to other TargetInstrInfo methods to create new
160 /// 4. If this block ends with an conditional branch and an unconditional
161 /// block, it returns the 'true' destination in TBB, the 'false' destination
162 /// in FBB, and a list of operands that evaluate the condition. These
163 /// operands can be passed to other TargetInstrInfo methods to create new
166 /// Note that RemoveBranch and InsertBranch must be implemented to support
167 /// cases where this method returns success.
169 bool NVPTXInstrInfo::AnalyzeBranch(
170 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
171 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify) const {
172 // If the block has no terminators, it just falls into the block after it.
173 MachineBasicBlock::iterator I = MBB.end();
174 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
177 // Get the last instruction in the block.
178 MachineInstr *LastInst = I;
180 // If there is only one terminator instruction, process it.
181 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
182 if (LastInst->getOpcode() == NVPTX::GOTO) {
183 TBB = LastInst->getOperand(0).getMBB();
185 } else if (LastInst->getOpcode() == NVPTX::CBranch) {
186 // Block ends with fall-through condbranch.
187 TBB = LastInst->getOperand(1).getMBB();
188 Cond.push_back(LastInst->getOperand(0));
191 // Otherwise, don't know what this is.
195 // Get the instruction before it if it's a terminator.
196 MachineInstr *SecondLastInst = I;
198 // If there are three terminators, we don't know what sort of block this is.
199 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
202 // If the block ends with NVPTX::GOTO and NVPTX:CBranch, handle it.
203 if (SecondLastInst->getOpcode() == NVPTX::CBranch &&
204 LastInst->getOpcode() == NVPTX::GOTO) {
205 TBB = SecondLastInst->getOperand(1).getMBB();
206 Cond.push_back(SecondLastInst->getOperand(0));
207 FBB = LastInst->getOperand(0).getMBB();
211 // If the block ends with two NVPTX:GOTOs, handle it. The second one is not
212 // executed, so remove it.
213 if (SecondLastInst->getOpcode() == NVPTX::GOTO &&
214 LastInst->getOpcode() == NVPTX::GOTO) {
215 TBB = SecondLastInst->getOperand(0).getMBB();
218 I->eraseFromParent();
222 // Otherwise, can't handle this.
226 unsigned NVPTXInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
227 MachineBasicBlock::iterator I = MBB.end();
228 if (I == MBB.begin())
231 if (I->getOpcode() != NVPTX::GOTO && I->getOpcode() != NVPTX::CBranch)
234 // Remove the branch.
235 I->eraseFromParent();
239 if (I == MBB.begin())
242 if (I->getOpcode() != NVPTX::CBranch)
245 // Remove the branch.
246 I->eraseFromParent();
250 unsigned NVPTXInstrInfo::InsertBranch(
251 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
252 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
253 // Shouldn't be a fall through.
254 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
255 assert((Cond.size() == 1 || Cond.size() == 0) &&
256 "NVPTX branch conditions have two components!");
260 if (Cond.empty()) // Unconditional branch
261 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(TBB);
262 else // Conditional branch
263 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg())
268 // Two-way Conditional Branch.
269 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg()).addMBB(TBB);
270 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(FBB);