blob: 0f6c2e53e60ad7f5857c2c99a194193341400843 [file] [log] [blame]
Justin Holewinskiae556d32012-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"
Justin Holewinskiae556d32012-05-04 20:18:50 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "llvm/IR/Function.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000022
Justin Holewinskiae556d32012-05-04 20:18:50 +000023using namespace llvm;
24
Chandler Carruthd174b722014-04-22 02:03:14 +000025#define GET_INSTRINFO_CTOR_DTOR
26#include "NVPTXGenInstrInfo.inc"
27
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000028// Pin the vtable to this file.
29void NVPTXInstrInfo::anchor() {}
30
Eric Christopher02389e32015-02-19 00:08:27 +000031NVPTXInstrInfo::NVPTXInstrInfo() : NVPTXGenInstrInfo(), RegInfo() {}
Justin Holewinskiae556d32012-05-04 20:18:50 +000032
Benjamin Kramerbdc49562016-06-12 15:39:02 +000033void NVPTXInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
34 MachineBasicBlock::iterator I,
35 const DebugLoc &DL, unsigned DestReg,
36 unsigned SrcReg, bool KillSrc) const {
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +000037 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
38 const TargetRegisterClass *DestRC = MRI.getRegClass(DestReg);
39 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
40
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +000041 if (RegInfo.getRegSizeInBits(*DestRC) != RegInfo.getRegSizeInBits(*SrcRC))
Jingyue Wuffa09be2015-08-01 18:02:12 +000042 report_fatal_error("Copy one register into another with a different width");
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +000043
Jingyue Wuffa09be2015-08-01 18:02:12 +000044 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 Belevich64dc9be2017-01-13 20:56:17 +000055 } else if (DestRC == &NVPTX::Float16RegsRegClass) {
56 Op = (SrcRC == &NVPTX::Float16RegsRegClass ? NVPTX::FMOV16rr
57 : NVPTX::BITCONVERT_16_I2F);
Artem Belevich620db1f2017-02-23 22:38:24 +000058 } else if (DestRC == &NVPTX::Float16x2RegsRegClass) {
59 Op = NVPTX::IMOV32rr;
Jingyue Wuffa09be2015-08-01 18:02:12 +000060 } else if (DestRC == &NVPTX::Float32RegsRegClass) {
61 Op = (SrcRC == &NVPTX::Float32RegsRegClass ? NVPTX::FMOV32rr
62 : NVPTX::BITCONVERT_32_I2F);
63 } else if (DestRC == &NVPTX::Float64RegsRegClass) {
64 Op = (SrcRC == &NVPTX::Float64RegsRegClass ? NVPTX::FMOV64rr
65 : NVPTX::BITCONVERT_64_I2F);
66 } else {
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +000067 llvm_unreachable("Bad register copy");
Justin Holewinskiae556d32012-05-04 20:18:50 +000068 }
Jingyue Wuffa09be2015-08-01 18:02:12 +000069 BuildMI(MBB, I, DL, get(Op), DestReg)
70 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinskiae556d32012-05-04 20:18:50 +000071}
72
Justin Holewinski0497ab12013-03-30 14:29:21 +000073bool NVPTXInstrInfo::isMoveInstr(const MachineInstr &MI, unsigned &SrcReg,
Justin Holewinskiae556d32012-05-04 20:18:50 +000074 unsigned &DestReg) const {
75 // Look for the appropriate part of TSFlags
76 bool isMove = false;
77
Justin Holewinski0497ab12013-03-30 14:29:21 +000078 unsigned TSFlags =
79 (MI.getDesc().TSFlags & NVPTX::SimpleMoveMask) >> NVPTX::SimpleMoveShift;
Justin Holewinskiae556d32012-05-04 20:18:50 +000080 isMove = (TSFlags == 1);
81
82 if (isMove) {
83 MachineOperand dest = MI.getOperand(0);
84 MachineOperand src = MI.getOperand(1);
85 assert(dest.isReg() && "dest of a movrr is not a reg");
86 assert(src.isReg() && "src of a movrr is not a reg");
87
88 SrcReg = src.getReg();
89 DestReg = dest.getReg();
90 return true;
91 }
92
93 return false;
94}
95
Justin Holewinskiae556d32012-05-04 20:18:50 +000096bool NVPTXInstrInfo::isLoadInstr(const MachineInstr &MI,
97 unsigned &AddrSpace) const {
98 bool isLoad = false;
Justin Holewinski0497ab12013-03-30 14:29:21 +000099 unsigned TSFlags =
100 (MI.getDesc().TSFlags & NVPTX::isLoadMask) >> NVPTX::isLoadShift;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000101 isLoad = (TSFlags == 1);
102 if (isLoad)
103 AddrSpace = getLdStCodeAddrSpace(MI);
104 return isLoad;
105}
106
107bool NVPTXInstrInfo::isStoreInstr(const MachineInstr &MI,
108 unsigned &AddrSpace) const {
109 bool isStore = false;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000110 unsigned TSFlags =
111 (MI.getDesc().TSFlags & NVPTX::isStoreMask) >> NVPTX::isStoreShift;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000112 isStore = (TSFlags == 1);
113 if (isStore)
114 AddrSpace = getLdStCodeAddrSpace(MI);
115 return isStore;
116}
117
Justin Holewinskiae556d32012-05-04 20:18:50 +0000118/// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
119/// true if it cannot be understood (e.g. it's a switch dispatch or isn't
120/// implemented for a target). Upon success, this returns false and returns
121/// with the following information in various cases:
122///
123/// 1. If this block ends with no branches (it just falls through to its succ)
124/// just return false, leaving TBB/FBB null.
125/// 2. If this block ends with only an unconditional branch, it sets TBB to be
126/// the destination block.
127/// 3. If this block ends with an conditional branch and it falls through to
128/// an successor block, it sets TBB to be the branch destination block and a
129/// list of operands that evaluate the condition. These
130/// operands can be passed to other TargetInstrInfo methods to create new
131/// branches.
132/// 4. If this block ends with an conditional branch and an unconditional
133/// block, it returns the 'true' destination in TBB, the 'false' destination
134/// in FBB, and a list of operands that evaluate the condition. These
135/// operands can be passed to other TargetInstrInfo methods to create new
136/// branches.
137///
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000138/// Note that removeBranch and insertBranch must be implemented to support
Justin Holewinskiae556d32012-05-04 20:18:50 +0000139/// cases where this method returns success.
140///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000141bool NVPTXInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
142 MachineBasicBlock *&TBB,
143 MachineBasicBlock *&FBB,
144 SmallVectorImpl<MachineOperand> &Cond,
145 bool AllowModify) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000146 // If the block has no terminators, it just falls into the block after it.
147 MachineBasicBlock::iterator I = MBB.end();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000148 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000149 return false;
150
151 // Get the last instruction in the block.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000152 MachineInstr &LastInst = *I;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000153
154 // If there is only one terminator instruction, process it.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000155 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000156 if (LastInst.getOpcode() == NVPTX::GOTO) {
157 TBB = LastInst.getOperand(0).getMBB();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000158 return false;
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000159 } else if (LastInst.getOpcode() == NVPTX::CBranch) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000160 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000161 TBB = LastInst.getOperand(1).getMBB();
162 Cond.push_back(LastInst.getOperand(0));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000163 return false;
164 }
165 // Otherwise, don't know what this is.
166 return true;
167 }
168
169 // Get the instruction before it if it's a terminator.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000170 MachineInstr &SecondLastInst = *I;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000171
172 // If there are three terminators, we don't know what sort of block this is.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000173 if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000174 return true;
175
176 // If the block ends with NVPTX::GOTO and NVPTX:CBranch, handle it.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000177 if (SecondLastInst.getOpcode() == NVPTX::CBranch &&
178 LastInst.getOpcode() == NVPTX::GOTO) {
179 TBB = SecondLastInst.getOperand(1).getMBB();
180 Cond.push_back(SecondLastInst.getOperand(0));
181 FBB = LastInst.getOperand(0).getMBB();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000182 return false;
183 }
184
185 // If the block ends with two NVPTX:GOTOs, handle it. The second one is not
186 // executed, so remove it.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000187 if (SecondLastInst.getOpcode() == NVPTX::GOTO &&
188 LastInst.getOpcode() == NVPTX::GOTO) {
189 TBB = SecondLastInst.getOperand(0).getMBB();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000190 I = LastInst;
191 if (AllowModify)
192 I->eraseFromParent();
193 return false;
194 }
195
196 // Otherwise, can't handle this.
197 return true;
198}
199
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000200unsigned NVPTXInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000201 int *BytesRemoved) const {
202 assert(!BytesRemoved && "code size not handled");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000203 MachineBasicBlock::iterator I = MBB.end();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000204 if (I == MBB.begin())
205 return 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000206 --I;
207 if (I->getOpcode() != NVPTX::GOTO && I->getOpcode() != NVPTX::CBranch)
208 return 0;
209
210 // Remove the branch.
211 I->eraseFromParent();
212
213 I = MBB.end();
214
Justin Holewinski0497ab12013-03-30 14:29:21 +0000215 if (I == MBB.begin())
216 return 1;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000217 --I;
218 if (I->getOpcode() != NVPTX::CBranch)
219 return 1;
220
221 // Remove the branch.
222 I->eraseFromParent();
223 return 2;
224}
225
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000226unsigned NVPTXInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000227 MachineBasicBlock *TBB,
228 MachineBasicBlock *FBB,
229 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000230 const DebugLoc &DL,
231 int *BytesAdded) const {
232 assert(!BytesAdded && "code size not handled");
233
Justin Holewinskiae556d32012-05-04 20:18:50 +0000234 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000235 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000236 assert((Cond.size() == 1 || Cond.size() == 0) &&
237 "NVPTX branch conditions have two components!");
238
239 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000240 if (!FBB) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000241 if (Cond.empty()) // Unconditional branch
Justin Holewinskiae556d32012-05-04 20:18:50 +0000242 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(TBB);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000243 else // Conditional branch
244 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg())
245 .addMBB(TBB);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000246 return 1;
247 }
248
249 // Two-way Conditional Branch.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000250 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg()).addMBB(TBB);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000251 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(FBB);
252 return 2;
253}