blob: 33a63c26f4e2e1a7e2035feeab5e0a21b0c4268c [file] [log] [blame]
Justin Holewinski49683f32012-05-04 20:18:50 +00001//===- 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"
17#define GET_INSTRINFO_CTOR
18#include "NVPTXGenInstrInfo.inc"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Function.h"
Justin Holewinski49683f32012-05-04 20:18:50 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include <cstdio>
25
Justin Holewinski49683f32012-05-04 20:18:50 +000026using namespace llvm;
27
28// FIXME: Add the subtarget support on this constructor.
29NVPTXInstrInfo::NVPTXInstrInfo(NVPTXTargetMachine &tm)
Justin Holewinski3639ce22013-03-30 14:29:21 +000030 : NVPTXGenInstrInfo(), TM(tm), RegInfo(*this, *TM.getSubtargetImpl()) {}
Justin Holewinski49683f32012-05-04 20:18:50 +000031
Justin Holewinski3639ce22013-03-30 14:29:21 +000032void NVPTXInstrInfo::copyPhysReg(
33 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, DebugLoc DL,
34 unsigned DestReg, unsigned SrcReg, bool KillSrc) const {
Justin Holewinski49683f32012-05-04 20:18:50 +000035 if (NVPTX::Int32RegsRegClass.contains(DestReg) &&
36 NVPTX::Int32RegsRegClass.contains(SrcReg))
37 BuildMI(MBB, I, DL, get(NVPTX::IMOV32rr), DestReg)
Justin Holewinski3639ce22013-03-30 14:29:21 +000038 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinski49683f32012-05-04 20:18:50 +000039 else if (NVPTX::Int8RegsRegClass.contains(DestReg) &&
Justin Holewinski3639ce22013-03-30 14:29:21 +000040 NVPTX::Int8RegsRegClass.contains(SrcReg))
Justin Holewinski49683f32012-05-04 20:18:50 +000041 BuildMI(MBB, I, DL, get(NVPTX::IMOV8rr), DestReg)
Justin Holewinski3639ce22013-03-30 14:29:21 +000042 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinski49683f32012-05-04 20:18:50 +000043 else if (NVPTX::Int1RegsRegClass.contains(DestReg) &&
Justin Holewinski3639ce22013-03-30 14:29:21 +000044 NVPTX::Int1RegsRegClass.contains(SrcReg))
Justin Holewinski49683f32012-05-04 20:18:50 +000045 BuildMI(MBB, I, DL, get(NVPTX::IMOV1rr), DestReg)
Justin Holewinski3639ce22013-03-30 14:29:21 +000046 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinski49683f32012-05-04 20:18:50 +000047 else if (NVPTX::Float32RegsRegClass.contains(DestReg) &&
Justin Holewinski3639ce22013-03-30 14:29:21 +000048 NVPTX::Float32RegsRegClass.contains(SrcReg))
Justin Holewinski49683f32012-05-04 20:18:50 +000049 BuildMI(MBB, I, DL, get(NVPTX::FMOV32rr), DestReg)
Justin Holewinski3639ce22013-03-30 14:29:21 +000050 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinski49683f32012-05-04 20:18:50 +000051 else if (NVPTX::Int16RegsRegClass.contains(DestReg) &&
Justin Holewinski3639ce22013-03-30 14:29:21 +000052 NVPTX::Int16RegsRegClass.contains(SrcReg))
Justin Holewinski49683f32012-05-04 20:18:50 +000053 BuildMI(MBB, I, DL, get(NVPTX::IMOV16rr), DestReg)
Justin Holewinski3639ce22013-03-30 14:29:21 +000054 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinski49683f32012-05-04 20:18:50 +000055 else if (NVPTX::Int64RegsRegClass.contains(DestReg) &&
Justin Holewinski3639ce22013-03-30 14:29:21 +000056 NVPTX::Int64RegsRegClass.contains(SrcReg))
Justin Holewinski49683f32012-05-04 20:18:50 +000057 BuildMI(MBB, I, DL, get(NVPTX::IMOV64rr), DestReg)
Justin Holewinski3639ce22013-03-30 14:29:21 +000058 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinski49683f32012-05-04 20:18:50 +000059 else if (NVPTX::Float64RegsRegClass.contains(DestReg) &&
Justin Holewinski3639ce22013-03-30 14:29:21 +000060 NVPTX::Float64RegsRegClass.contains(SrcReg))
Justin Holewinski49683f32012-05-04 20:18:50 +000061 BuildMI(MBB, I, DL, get(NVPTX::FMOV64rr), DestReg)
Justin Holewinski3639ce22013-03-30 14:29:21 +000062 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinski49683f32012-05-04 20:18:50 +000063 else {
Craig Topper63663612012-05-24 07:02:50 +000064 llvm_unreachable("Don't know how to copy a register");
Justin Holewinski49683f32012-05-04 20:18:50 +000065 }
66}
67
Justin Holewinski3639ce22013-03-30 14:29:21 +000068bool NVPTXInstrInfo::isMoveInstr(const MachineInstr &MI, unsigned &SrcReg,
Justin Holewinski49683f32012-05-04 20:18:50 +000069 unsigned &DestReg) const {
70 // Look for the appropriate part of TSFlags
71 bool isMove = false;
72
Justin Holewinski3639ce22013-03-30 14:29:21 +000073 unsigned TSFlags =
74 (MI.getDesc().TSFlags & NVPTX::SimpleMoveMask) >> NVPTX::SimpleMoveShift;
Justin Holewinski49683f32012-05-04 20:18:50 +000075 isMove = (TSFlags == 1);
76
77 if (isMove) {
78 MachineOperand dest = MI.getOperand(0);
79 MachineOperand src = MI.getOperand(1);
80 assert(dest.isReg() && "dest of a movrr is not a reg");
81 assert(src.isReg() && "src of a movrr is not a reg");
82
83 SrcReg = src.getReg();
84 DestReg = dest.getReg();
85 return true;
86 }
87
88 return false;
89}
90
Justin Holewinski3639ce22013-03-30 14:29:21 +000091bool NVPTXInstrInfo::isReadSpecialReg(MachineInstr &MI) const {
Justin Holewinski49683f32012-05-04 20:18:50 +000092 switch (MI.getOpcode()) {
Justin Holewinski3639ce22013-03-30 14:29:21 +000093 default:
94 return false;
Justin Holewinski49683f32012-05-04 20:18:50 +000095 case NVPTX::INT_PTX_SREG_NTID_X:
96 case NVPTX::INT_PTX_SREG_NTID_Y:
97 case NVPTX::INT_PTX_SREG_NTID_Z:
98 case NVPTX::INT_PTX_SREG_TID_X:
99 case NVPTX::INT_PTX_SREG_TID_Y:
100 case NVPTX::INT_PTX_SREG_TID_Z:
101 case NVPTX::INT_PTX_SREG_CTAID_X:
102 case NVPTX::INT_PTX_SREG_CTAID_Y:
103 case NVPTX::INT_PTX_SREG_CTAID_Z:
104 case NVPTX::INT_PTX_SREG_NCTAID_X:
105 case NVPTX::INT_PTX_SREG_NCTAID_Y:
106 case NVPTX::INT_PTX_SREG_NCTAID_Z:
107 case NVPTX::INT_PTX_SREG_WARPSIZE:
108 return true;
109 }
110}
111
Justin Holewinski49683f32012-05-04 20:18:50 +0000112bool NVPTXInstrInfo::isLoadInstr(const MachineInstr &MI,
113 unsigned &AddrSpace) const {
114 bool isLoad = false;
Justin Holewinski3639ce22013-03-30 14:29:21 +0000115 unsigned TSFlags =
116 (MI.getDesc().TSFlags & NVPTX::isLoadMask) >> NVPTX::isLoadShift;
Justin Holewinski49683f32012-05-04 20:18:50 +0000117 isLoad = (TSFlags == 1);
118 if (isLoad)
119 AddrSpace = getLdStCodeAddrSpace(MI);
120 return isLoad;
121}
122
123bool NVPTXInstrInfo::isStoreInstr(const MachineInstr &MI,
124 unsigned &AddrSpace) const {
125 bool isStore = false;
Justin Holewinski3639ce22013-03-30 14:29:21 +0000126 unsigned TSFlags =
127 (MI.getDesc().TSFlags & NVPTX::isStoreMask) >> NVPTX::isStoreShift;
Justin Holewinski49683f32012-05-04 20:18:50 +0000128 isStore = (TSFlags == 1);
129 if (isStore)
130 AddrSpace = getLdStCodeAddrSpace(MI);
131 return isStore;
132}
133
Justin Holewinski49683f32012-05-04 20:18:50 +0000134bool NVPTXInstrInfo::CanTailMerge(const MachineInstr *MI) const {
135 unsigned addrspace = 0;
136 if (MI->getOpcode() == NVPTX::INT_CUDA_SYNCTHREADS)
137 return false;
138 if (isLoadInstr(*MI, addrspace))
139 if (addrspace == NVPTX::PTXLdStInstCode::SHARED)
140 return false;
141 if (isStoreInstr(*MI, addrspace))
142 if (addrspace == NVPTX::PTXLdStInstCode::SHARED)
143 return false;
144 return true;
145}
146
Justin Holewinski49683f32012-05-04 20:18:50 +0000147/// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
148/// true if it cannot be understood (e.g. it's a switch dispatch or isn't
149/// implemented for a target). Upon success, this returns false and returns
150/// with the following information in various cases:
151///
152/// 1. If this block ends with no branches (it just falls through to its succ)
153/// just return false, leaving TBB/FBB null.
154/// 2. If this block ends with only an unconditional branch, it sets TBB to be
155/// the destination block.
156/// 3. If this block ends with an conditional branch and it falls through to
157/// an successor block, it sets TBB to be the branch destination block and a
158/// list of operands that evaluate the condition. These
159/// operands can be passed to other TargetInstrInfo methods to create new
160/// branches.
161/// 4. If this block ends with an conditional branch and an unconditional
162/// block, it returns the 'true' destination in TBB, the 'false' destination
163/// in FBB, and a list of operands that evaluate the condition. These
164/// operands can be passed to other TargetInstrInfo methods to create new
165/// branches.
166///
167/// Note that RemoveBranch and InsertBranch must be implemented to support
168/// cases where this method returns success.
169///
Justin Holewinski3639ce22013-03-30 14:29:21 +0000170bool NVPTXInstrInfo::AnalyzeBranch(
171 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
172 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify) const {
Justin Holewinski49683f32012-05-04 20:18:50 +0000173 // If the block has no terminators, it just falls into the block after it.
174 MachineBasicBlock::iterator I = MBB.end();
175 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
176 return false;
177
178 // Get the last instruction in the block.
179 MachineInstr *LastInst = I;
180
181 // If there is only one terminator instruction, process it.
182 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
183 if (LastInst->getOpcode() == NVPTX::GOTO) {
184 TBB = LastInst->getOperand(0).getMBB();
185 return false;
186 } else if (LastInst->getOpcode() == NVPTX::CBranch) {
187 // Block ends with fall-through condbranch.
188 TBB = LastInst->getOperand(1).getMBB();
189 Cond.push_back(LastInst->getOperand(0));
190 return false;
191 }
192 // Otherwise, don't know what this is.
193 return true;
194 }
195
196 // Get the instruction before it if it's a terminator.
197 MachineInstr *SecondLastInst = I;
198
199 // If there are three terminators, we don't know what sort of block this is.
Justin Holewinski3639ce22013-03-30 14:29:21 +0000200 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
Justin Holewinski49683f32012-05-04 20:18:50 +0000201 return true;
202
203 // If the block ends with NVPTX::GOTO and NVPTX:CBranch, handle it.
204 if (SecondLastInst->getOpcode() == NVPTX::CBranch &&
205 LastInst->getOpcode() == NVPTX::GOTO) {
Justin Holewinski3639ce22013-03-30 14:29:21 +0000206 TBB = SecondLastInst->getOperand(1).getMBB();
Justin Holewinski49683f32012-05-04 20:18:50 +0000207 Cond.push_back(SecondLastInst->getOperand(0));
208 FBB = LastInst->getOperand(0).getMBB();
209 return false;
210 }
211
212 // If the block ends with two NVPTX:GOTOs, handle it. The second one is not
213 // executed, so remove it.
214 if (SecondLastInst->getOpcode() == NVPTX::GOTO &&
215 LastInst->getOpcode() == NVPTX::GOTO) {
216 TBB = SecondLastInst->getOperand(0).getMBB();
217 I = LastInst;
218 if (AllowModify)
219 I->eraseFromParent();
220 return false;
221 }
222
223 // Otherwise, can't handle this.
224 return true;
225}
226
227unsigned NVPTXInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
228 MachineBasicBlock::iterator I = MBB.end();
Justin Holewinski3639ce22013-03-30 14:29:21 +0000229 if (I == MBB.begin())
230 return 0;
Justin Holewinski49683f32012-05-04 20:18:50 +0000231 --I;
232 if (I->getOpcode() != NVPTX::GOTO && I->getOpcode() != NVPTX::CBranch)
233 return 0;
234
235 // Remove the branch.
236 I->eraseFromParent();
237
238 I = MBB.end();
239
Justin Holewinski3639ce22013-03-30 14:29:21 +0000240 if (I == MBB.begin())
241 return 1;
Justin Holewinski49683f32012-05-04 20:18:50 +0000242 --I;
243 if (I->getOpcode() != NVPTX::CBranch)
244 return 1;
245
246 // Remove the branch.
247 I->eraseFromParent();
248 return 2;
249}
250
Justin Holewinski3639ce22013-03-30 14:29:21 +0000251unsigned NVPTXInstrInfo::InsertBranch(
252 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
253 const SmallVectorImpl<MachineOperand> &Cond, DebugLoc DL) const {
Justin Holewinski49683f32012-05-04 20:18:50 +0000254 // Shouldn't be a fall through.
255 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
256 assert((Cond.size() == 1 || Cond.size() == 0) &&
257 "NVPTX branch conditions have two components!");
258
259 // One-way branch.
260 if (FBB == 0) {
Justin Holewinski3639ce22013-03-30 14:29:21 +0000261 if (Cond.empty()) // Unconditional branch
Justin Holewinski49683f32012-05-04 20:18:50 +0000262 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(TBB);
Justin Holewinski3639ce22013-03-30 14:29:21 +0000263 else // Conditional branch
264 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg())
265 .addMBB(TBB);
Justin Holewinski49683f32012-05-04 20:18:50 +0000266 return 1;
267 }
268
269 // Two-way Conditional Branch.
Justin Holewinski3639ce22013-03-30 14:29:21 +0000270 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg()).addMBB(TBB);
Justin Holewinski49683f32012-05-04 20:18:50 +0000271 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(FBB);
272 return 2;
273}