blob: cff230289e60013854f9ba7217da5b3eeb0318be [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//===- NVPTXInstrInfo.cpp - NVPTX Instruction Information -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Justin Holewinskiae556d32012-05-04 20:18:50 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the NVPTX implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
Justin Holewinskiae556d32012-05-04 20:18:50 +000013#include "NVPTXInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "NVPTX.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000015#include "NVPTXTargetMachine.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/IR/Function.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000021
Justin Holewinskiae556d32012-05-04 20:18:50 +000022using namespace llvm;
23
Chandler Carruthd174b722014-04-22 02:03:14 +000024#define GET_INSTRINFO_CTOR_DTOR
25#include "NVPTXGenInstrInfo.inc"
26
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000027// Pin the vtable to this file.
28void NVPTXInstrInfo::anchor() {}
29
Eric Christopher02389e32015-02-19 00:08:27 +000030NVPTXInstrInfo::NVPTXInstrInfo() : NVPTXGenInstrInfo(), RegInfo() {}
Justin Holewinskiae556d32012-05-04 20:18:50 +000031
Benjamin Kramerbdc49562016-06-12 15:39:02 +000032void NVPTXInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
33 MachineBasicBlock::iterator I,
Matt Arsenaulte6c9a9a2019-11-11 13:54:21 +053034 const DebugLoc &DL, MCRegister DestReg,
35 MCRegister SrcReg, bool KillSrc) const {
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +000036 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
37 const TargetRegisterClass *DestRC = MRI.getRegClass(DestReg);
38 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
39
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +000040 if (RegInfo.getRegSizeInBits(*DestRC) != RegInfo.getRegSizeInBits(*SrcRC))
Jingyue Wuffa09be2015-08-01 18:02:12 +000041 report_fatal_error("Copy one register into another with a different width");
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +000042
Jingyue Wuffa09be2015-08-01 18:02:12 +000043 unsigned Op;
44 if (DestRC == &NVPTX::Int1RegsRegClass) {
45 Op = NVPTX::IMOV1rr;
46 } else if (DestRC == &NVPTX::Int16RegsRegClass) {
47 Op = NVPTX::IMOV16rr;
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);
Artem Belevich64dc9be2017-01-13 20:56:17 +000054 } else if (DestRC == &NVPTX::Float16RegsRegClass) {
55 Op = (SrcRC == &NVPTX::Float16RegsRegClass ? NVPTX::FMOV16rr
56 : NVPTX::BITCONVERT_16_I2F);
Artem Belevich620db1f2017-02-23 22:38:24 +000057 } else if (DestRC == &NVPTX::Float16x2RegsRegClass) {
58 Op = NVPTX::IMOV32rr;
Jingyue Wuffa09be2015-08-01 18:02:12 +000059 } else if (DestRC == &NVPTX::Float32RegsRegClass) {
60 Op = (SrcRC == &NVPTX::Float32RegsRegClass ? NVPTX::FMOV32rr
61 : NVPTX::BITCONVERT_32_I2F);
62 } else if (DestRC == &NVPTX::Float64RegsRegClass) {
63 Op = (SrcRC == &NVPTX::Float64RegsRegClass ? NVPTX::FMOV64rr
64 : NVPTX::BITCONVERT_64_I2F);
65 } else {
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +000066 llvm_unreachable("Bad register copy");
Justin Holewinskiae556d32012-05-04 20:18:50 +000067 }
Jingyue Wuffa09be2015-08-01 18:02:12 +000068 BuildMI(MBB, I, DL, get(Op), DestReg)
69 .addReg(SrcReg, getKillRegState(KillSrc));
Justin Holewinskiae556d32012-05-04 20:18:50 +000070}
71
Justin Holewinskiae556d32012-05-04 20:18:50 +000072/// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
73/// true if it cannot be understood (e.g. it's a switch dispatch or isn't
74/// implemented for a target). Upon success, this returns false and returns
75/// with the following information in various cases:
76///
77/// 1. If this block ends with no branches (it just falls through to its succ)
78/// just return false, leaving TBB/FBB null.
79/// 2. If this block ends with only an unconditional branch, it sets TBB to be
80/// the destination block.
81/// 3. If this block ends with an conditional branch and it falls through to
82/// an successor block, it sets TBB to be the branch destination block and a
83/// list of operands that evaluate the condition. These
84/// operands can be passed to other TargetInstrInfo methods to create new
85/// branches.
86/// 4. If this block ends with an conditional branch and an unconditional
87/// block, it returns the 'true' destination in TBB, the 'false' destination
88/// in FBB, and a list of operands that evaluate the condition. These
89/// operands can be passed to other TargetInstrInfo methods to create new
90/// branches.
91///
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +000092/// Note that removeBranch and insertBranch must be implemented to support
Justin Holewinskiae556d32012-05-04 20:18:50 +000093/// cases where this method returns success.
94///
Jacques Pienaar71c30a12016-07-15 14:41:04 +000095bool NVPTXInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
96 MachineBasicBlock *&TBB,
97 MachineBasicBlock *&FBB,
98 SmallVectorImpl<MachineOperand> &Cond,
99 bool AllowModify) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000100 // If the block has no terminators, it just falls into the block after it.
101 MachineBasicBlock::iterator I = MBB.end();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000102 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000103 return false;
104
105 // Get the last instruction in the block.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000106 MachineInstr &LastInst = *I;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000107
108 // If there is only one terminator instruction, process it.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000109 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000110 if (LastInst.getOpcode() == NVPTX::GOTO) {
111 TBB = LastInst.getOperand(0).getMBB();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000112 return false;
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000113 } else if (LastInst.getOpcode() == NVPTX::CBranch) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000114 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000115 TBB = LastInst.getOperand(1).getMBB();
116 Cond.push_back(LastInst.getOperand(0));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000117 return false;
118 }
119 // Otherwise, don't know what this is.
120 return true;
121 }
122
123 // Get the instruction before it if it's a terminator.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000124 MachineInstr &SecondLastInst = *I;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000125
126 // 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 +0000127 if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000128 return true;
129
130 // If the block ends with NVPTX::GOTO and NVPTX:CBranch, handle it.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000131 if (SecondLastInst.getOpcode() == NVPTX::CBranch &&
132 LastInst.getOpcode() == NVPTX::GOTO) {
133 TBB = SecondLastInst.getOperand(1).getMBB();
134 Cond.push_back(SecondLastInst.getOperand(0));
135 FBB = LastInst.getOperand(0).getMBB();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000136 return false;
137 }
138
139 // If the block ends with two NVPTX:GOTOs, handle it. The second one is not
140 // executed, so remove it.
Duncan P. N. Exon Smith68f499a2016-07-08 21:10:58 +0000141 if (SecondLastInst.getOpcode() == NVPTX::GOTO &&
142 LastInst.getOpcode() == NVPTX::GOTO) {
143 TBB = SecondLastInst.getOperand(0).getMBB();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000144 I = LastInst;
145 if (AllowModify)
146 I->eraseFromParent();
147 return false;
148 }
149
150 // Otherwise, can't handle this.
151 return true;
152}
153
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000154unsigned NVPTXInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000155 int *BytesRemoved) const {
156 assert(!BytesRemoved && "code size not handled");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000157 MachineBasicBlock::iterator I = MBB.end();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000158 if (I == MBB.begin())
159 return 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000160 --I;
161 if (I->getOpcode() != NVPTX::GOTO && I->getOpcode() != NVPTX::CBranch)
162 return 0;
163
164 // Remove the branch.
165 I->eraseFromParent();
166
167 I = MBB.end();
168
Justin Holewinski0497ab12013-03-30 14:29:21 +0000169 if (I == MBB.begin())
170 return 1;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000171 --I;
172 if (I->getOpcode() != NVPTX::CBranch)
173 return 1;
174
175 // Remove the branch.
176 I->eraseFromParent();
177 return 2;
178}
179
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000180unsigned NVPTXInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000181 MachineBasicBlock *TBB,
182 MachineBasicBlock *FBB,
183 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000184 const DebugLoc &DL,
185 int *BytesAdded) const {
186 assert(!BytesAdded && "code size not handled");
187
Justin Holewinskiae556d32012-05-04 20:18:50 +0000188 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000189 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000190 assert((Cond.size() == 1 || Cond.size() == 0) &&
191 "NVPTX branch conditions have two components!");
192
193 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000194 if (!FBB) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000195 if (Cond.empty()) // Unconditional branch
Justin Holewinskiae556d32012-05-04 20:18:50 +0000196 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(TBB);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000197 else // Conditional branch
198 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg())
199 .addMBB(TBB);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000200 return 1;
201 }
202
203 // Two-way Conditional Branch.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000204 BuildMI(&MBB, DL, get(NVPTX::CBranch)).addReg(Cond[0].getReg()).addMBB(TBB);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000205 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(FBB);
206 return 2;
207}