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);
41 report_fatal_error("Attempted to created cross-class register copy");
43 if (DestRC == &NVPTX::Int32RegsRegClass)
44 BuildMI(MBB, I, DL, get(NVPTX::IMOV32rr), DestReg)
45 .addReg(SrcReg, getKillRegState(KillSrc));
46 else if (DestRC == &NVPTX::Int1RegsRegClass)
47 BuildMI(MBB, I, DL, get(NVPTX::IMOV1rr), DestReg)
48 .addReg(SrcReg, getKillRegState(KillSrc));
49 else if (DestRC == &NVPTX::Float32RegsRegClass)
50 BuildMI(MBB, I, DL, get(NVPTX::FMOV32rr), DestReg)
51 .addReg(SrcReg, getKillRegState(KillSrc));
52 else if (DestRC == &NVPTX::Int16RegsRegClass)
53 BuildMI(MBB, I, DL, get(NVPTX::IMOV16rr), DestReg)
54 .addReg(SrcReg, getKillRegState(KillSrc));
55 else if (DestRC == &NVPTX::Int64RegsRegClass)
56 BuildMI(MBB, I, DL, get(NVPTX::IMOV64rr), DestReg)
57 .addReg(SrcReg, getKillRegState(KillSrc));
58 else if (DestRC == &NVPTX::Float64RegsRegClass)
59 BuildMI(MBB, I, DL, get(NVPTX::FMOV64rr), DestReg)
60 .addReg(SrcReg, getKillRegState(KillSrc));
62 llvm_unreachable("Bad register copy");
66 bool NVPTXInstrInfo::isMoveInstr(const MachineInstr &MI, unsigned &SrcReg,
67 unsigned &DestReg) const {
68 // Look for the appropriate part of TSFlags
72 (MI.getDesc().TSFlags & NVPTX::SimpleMoveMask) >> NVPTX::SimpleMoveShift;
73 isMove = (TSFlags == 1);
76 MachineOperand dest = MI.getOperand(0);
77 MachineOperand src = MI.getOperand(1);
78 assert(dest.isReg() && "dest of a movrr is not a reg");
79 assert(src.isReg() && "src of a movrr is not a reg");
81 SrcReg = src.getReg();
82 DestReg = dest.getReg();
89 bool NVPTXInstrInfo::isReadSpecialReg(MachineInstr &MI) const {
90 switch (MI.getOpcode()) {
93 case NVPTX::INT_PTX_SREG_NTID_X:
94 case NVPTX::INT_PTX_SREG_NTID_Y:
95 case NVPTX::INT_PTX_SREG_NTID_Z:
96 case NVPTX::INT_PTX_SREG_TID_X:
97 case NVPTX::INT_PTX_SREG_TID_Y:
98 case NVPTX::INT_PTX_SREG_TID_Z:
99 case NVPTX::INT_PTX_SREG_CTAID_X:
100 case NVPTX::INT_PTX_SREG_CTAID_Y:
101 case NVPTX::INT_PTX_SREG_CTAID_Z:
102 case NVPTX::INT_PTX_SREG_NCTAID_X:
103 case NVPTX::INT_PTX_SREG_NCTAID_Y:
104 case NVPTX::INT_PTX_SREG_NCTAID_Z:
105 case NVPTX::INT_PTX_SREG_WARPSIZE:
110 bool NVPTXInstrInfo::isLoadInstr(const MachineInstr &MI,
111 unsigned &AddrSpace) const {
114 (MI.getDesc().TSFlags & NVPTX::isLoadMask) >> NVPTX::isLoadShift;
115 isLoad = (TSFlags == 1);
117 AddrSpace = getLdStCodeAddrSpace(MI);
121 bool NVPTXInstrInfo::isStoreInstr(const MachineInstr &MI,
122 unsigned &AddrSpace) const {
123 bool isStore = false;
125 (MI.getDesc().TSFlags & NVPTX::isStoreMask) >> NVPTX::isStoreShift;
126 isStore = (TSFlags == 1);
128 AddrSpace = getLdStCodeAddrSpace(MI);
132 bool NVPTXInstrInfo::CanTailMerge(const MachineInstr *MI) const {
133 unsigned addrspace = 0;
134 if (MI->getOpcode() == NVPTX::INT_CUDA_SYNCTHREADS)
136 if (isLoadInstr(*MI, addrspace))
137 if (addrspace == NVPTX::PTXLdStInstCode::SHARED)
139 if (isStoreInstr(*MI, addrspace))
140 if (addrspace == NVPTX::PTXLdStInstCode::SHARED)
145 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
146 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
147 /// implemented for a target). Upon success, this returns false and returns
148 /// with the following information in various cases:
150 /// 1. If this block ends with no branches (it just falls through to its succ)
151 /// just return false, leaving TBB/FBB null.
152 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
153 /// the destination block.
154 /// 3. If this block ends with an conditional branch and it falls through to
155 /// an successor block, it sets TBB to be the branch destination block and a
156 /// list of operands that evaluate the condition. These
157 /// operands can be passed to other TargetInstrInfo methods to create new
159 /// 4. If this block ends with an conditional branch and an unconditional
160 /// block, it returns the 'true' destination in TBB, the 'false' destination
161 /// in FBB, and a list of operands that evaluate the condition. These
162 /// operands can be passed to other TargetInstrInfo methods to create new
165 /// Note that RemoveBranch and InsertBranch must be implemented to support
166 /// cases where this method returns success.
168 bool NVPTXInstrInfo::AnalyzeBranch(
169 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
170 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify) const {
171 // If the block has no terminators, it just falls into the block after it.
172 MachineBasicBlock::iterator I = MBB.end();
173 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
176 // Get the last instruction in the block.
177 MachineInstr *LastInst = I;
179 // If there is only one terminator instruction, process it.
180 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
181 if (LastInst->getOpcode() == NVPTX::GOTO) {
182 TBB = LastInst->getOperand(0).getMBB();
184 } else if (LastInst->getOpcode() == NVPTX::CBranch) {
185 // Block ends with fall-through condbranch.
186 TBB = LastInst->getOperand(1).getMBB();
187 Cond.push_back(LastInst->getOperand(0));
190 // Otherwise, don't know what this is.
194 // Get the instruction before it if it's a terminator.
195 MachineInstr *SecondLastInst = I;
197 // If there are three terminators, we don't know what sort of block this is.
198 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
201 // If the block ends with NVPTX::GOTO and NVPTX:CBranch, handle it.
202 if (SecondLastInst->getOpcode() == NVPTX::CBranch &&
203 LastInst->getOpcode() == NVPTX::GOTO) {
204 TBB = SecondLastInst->getOperand(1).getMBB();
205 Cond.push_back(SecondLastInst->getOperand(0));
206 FBB = LastInst->getOperand(0).getMBB();
210 // If the block ends with two NVPTX:GOTOs, handle it. The second one is not
211 // executed, so remove it.
212 if (SecondLastInst->getOpcode() == NVPTX::GOTO &&
213 LastInst->getOpcode() == NVPTX::GOTO) {
214 TBB = SecondLastInst->getOperand(0).getMBB();
217 I->eraseFromParent();
221 // Otherwise, can't handle this.
225 unsigned NVPTXInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
226 MachineBasicBlock::iterator I = MBB.end();
227 if (I == MBB.begin())
230 if (I->getOpcode() != NVPTX::GOTO && I->getOpcode() != NVPTX::CBranch)
233 // Remove the branch.
234 I->eraseFromParent();
238 if (I == MBB.begin())
241 if (I->getOpcode() != NVPTX::CBranch)
244 // Remove the branch.
245 I->eraseFromParent();
249 unsigned NVPTXInstrInfo::InsertBranch(
250 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
251 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
252 // Shouldn't be a fall through.
253 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
254 assert((Cond.size() == 1 || Cond.size() == 0) &&
255 "NVPTX branch conditions have two components!");
259 if (Cond.empty()) // Unconditional branch
260 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(TBB);
261 else // Conditional branch
262 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg())
267 // Two-way Conditional Branch.
268 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg()).addMBB(TBB);
269 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(FBB);