blob: cbbe07a27bf47ca3c6bd834929ec09eff37b411d [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- RISCVInstrInfo.cpp - RISCV Instruction Information ------*- C++ -*-===//
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 RISCV implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RISCVInstrInfo.h"
15#include "RISCV.h"
16#include "RISCVSubtarget.h"
17#include "RISCVTargetMachine.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Bradbury315cd3a2018-01-10 21:05:07 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Alex Bradbury89718422017-10-19 21:37:38 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/TargetRegistry.h"
26
27#define GET_INSTRINFO_CTOR_DTOR
28#include "RISCVGenInstrInfo.inc"
29
30using namespace llvm;
31
Alex Bradburya3376752017-11-08 13:41:21 +000032RISCVInstrInfo::RISCVInstrInfo()
33 : RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP) {}
Alex Bradburycfa62912017-11-08 12:20:01 +000034
35void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
36 MachineBasicBlock::iterator MBBI,
37 const DebugLoc &DL, unsigned DstReg,
38 unsigned SrcReg, bool KillSrc) const {
Alex Bradbury65d6ea52018-03-21 15:11:02 +000039 if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) {
40 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
41 .addReg(SrcReg, getKillRegState(KillSrc))
42 .addImm(0);
43 return;
44 }
Alex Bradburycfa62912017-11-08 12:20:01 +000045
Alex Bradbury21d28fe2018-04-12 05:50:06 +000046 // FPR->FPR copies
47 unsigned Opc;
48 if (RISCV::FPR32RegClass.contains(DstReg, SrcReg))
49 Opc = RISCV::FSGNJ_S;
50 else if (RISCV::FPR64RegClass.contains(DstReg, SrcReg))
51 Opc = RISCV::FSGNJ_D;
52 else
53 llvm_unreachable("Impossible reg-to-reg copy");
Alex Bradbury65d6ea52018-03-21 15:11:02 +000054
Alex Bradbury21d28fe2018-04-12 05:50:06 +000055 BuildMI(MBB, MBBI, DL, get(Opc), DstReg)
56 .addReg(SrcReg, getKillRegState(KillSrc))
57 .addReg(SrcReg, getKillRegState(KillSrc));
Alex Bradburycfa62912017-11-08 12:20:01 +000058}
Alex Bradbury74913e12017-11-08 13:31:40 +000059
60void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator I,
62 unsigned SrcReg, bool IsKill, int FI,
63 const TargetRegisterClass *RC,
64 const TargetRegisterInfo *TRI) const {
65 DebugLoc DL;
66 if (I != MBB.end())
67 DL = I->getDebugLoc();
68
Alex Bradbury65d6ea52018-03-21 15:11:02 +000069 unsigned Opcode;
70
Alex Bradbury87a54d62017-12-07 12:45:05 +000071 if (RISCV::GPRRegClass.hasSubClassEq(RC))
Alex Bradbury65d6ea52018-03-21 15:11:02 +000072 Opcode = RISCV::SW;
73 else if (RISCV::FPR32RegClass.hasSubClassEq(RC))
74 Opcode = RISCV::FSW;
Alex Bradbury0b4175f2018-04-12 05:34:25 +000075 else if (RISCV::FPR64RegClass.hasSubClassEq(RC))
76 Opcode = RISCV::FSD;
Alex Bradbury74913e12017-11-08 13:31:40 +000077 else
78 llvm_unreachable("Can't store this register to stack slot");
Alex Bradbury65d6ea52018-03-21 15:11:02 +000079
80 BuildMI(MBB, I, DL, get(Opcode))
81 .addReg(SrcReg, getKillRegState(IsKill))
82 .addFrameIndex(FI)
83 .addImm(0);
Alex Bradbury74913e12017-11-08 13:31:40 +000084}
85
86void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
87 MachineBasicBlock::iterator I,
88 unsigned DstReg, int FI,
89 const TargetRegisterClass *RC,
90 const TargetRegisterInfo *TRI) const {
91 DebugLoc DL;
92 if (I != MBB.end())
93 DL = I->getDebugLoc();
94
Alex Bradbury65d6ea52018-03-21 15:11:02 +000095 unsigned Opcode;
96
Alex Bradbury87a54d62017-12-07 12:45:05 +000097 if (RISCV::GPRRegClass.hasSubClassEq(RC))
Alex Bradbury65d6ea52018-03-21 15:11:02 +000098 Opcode = RISCV::LW;
99 else if (RISCV::FPR32RegClass.hasSubClassEq(RC))
100 Opcode = RISCV::FLW;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000101 else if (RISCV::FPR64RegClass.hasSubClassEq(RC))
102 Opcode = RISCV::FLD;
Alex Bradbury74913e12017-11-08 13:31:40 +0000103 else
104 llvm_unreachable("Can't load this register from stack slot");
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000105
106 BuildMI(MBB, I, DL, get(Opcode), DstReg).addFrameIndex(FI).addImm(0);
Alex Bradbury74913e12017-11-08 13:31:40 +0000107}
Alex Bradbury9fea4882018-01-10 19:53:46 +0000108
109void RISCVInstrInfo::movImm32(MachineBasicBlock &MBB,
110 MachineBasicBlock::iterator MBBI,
111 const DebugLoc &DL, unsigned DstReg, uint64_t Val,
112 MachineInstr::MIFlag Flag) const {
113 assert(isInt<32>(Val) && "Can only materialize 32-bit constants");
114
Alex Bradbury480b7bc2018-04-17 21:56:40 +0000115 BuildMI(MBB, MBBI, DL, get(RISCV::PseudoLI), DstReg)
116 .addImm(Val)
Alex Bradbury9fea4882018-01-10 19:53:46 +0000117 .setMIFlag(Flag);
118}
Alex Bradburye027c932018-01-10 20:47:00 +0000119
120// The contents of values added to Cond are not examined outside of
121// RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
122// push BranchOpcode, Reg1, Reg2.
123static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target,
124 SmallVectorImpl<MachineOperand> &Cond) {
125 // Block ends with fall-through condbranch.
126 assert(LastInst.getDesc().isConditionalBranch() &&
127 "Unknown conditional branch");
128 Target = LastInst.getOperand(2).getMBB();
129 Cond.push_back(MachineOperand::CreateImm(LastInst.getOpcode()));
130 Cond.push_back(LastInst.getOperand(0));
131 Cond.push_back(LastInst.getOperand(1));
132}
133
134static unsigned getOppositeBranchOpcode(int Opc) {
135 switch (Opc) {
136 default:
137 llvm_unreachable("Unrecognized conditional branch");
138 case RISCV::BEQ:
139 return RISCV::BNE;
140 case RISCV::BNE:
141 return RISCV::BEQ;
142 case RISCV::BLT:
143 return RISCV::BGE;
144 case RISCV::BGE:
145 return RISCV::BLT;
146 case RISCV::BLTU:
147 return RISCV::BGEU;
148 case RISCV::BGEU:
149 return RISCV::BLTU;
150 }
151}
152
153bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
154 MachineBasicBlock *&TBB,
155 MachineBasicBlock *&FBB,
156 SmallVectorImpl<MachineOperand> &Cond,
157 bool AllowModify) const {
158 TBB = FBB = nullptr;
159 Cond.clear();
160
161 // If the block has no terminators, it just falls into the block after it.
162 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
163 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
164 return false;
165
166 // Count the number of terminators and find the first unconditional or
167 // indirect branch.
168 MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end();
169 int NumTerminators = 0;
170 for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J);
171 J++) {
172 NumTerminators++;
173 if (J->getDesc().isUnconditionalBranch() ||
174 J->getDesc().isIndirectBranch()) {
175 FirstUncondOrIndirectBr = J.getReverse();
176 }
177 }
178
179 // If AllowModify is true, we can erase any terminators after
180 // FirstUncondOrIndirectBR.
181 if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) {
182 while (std::next(FirstUncondOrIndirectBr) != MBB.end()) {
183 std::next(FirstUncondOrIndirectBr)->eraseFromParent();
184 NumTerminators--;
185 }
186 I = FirstUncondOrIndirectBr;
187 }
188
189 // We can't handle blocks that end in an indirect branch.
190 if (I->getDesc().isIndirectBranch())
191 return true;
192
193 // We can't handle blocks with more than 2 terminators.
194 if (NumTerminators > 2)
195 return true;
196
197 // Handle a single unconditional branch.
198 if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
199 TBB = I->getOperand(0).getMBB();
200 return false;
201 }
202
203 // Handle a single conditional branch.
204 if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) {
205 parseCondBranch(*I, TBB, Cond);
206 return false;
207 }
208
209 // Handle a conditional branch followed by an unconditional branch.
210 if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
211 I->getDesc().isUnconditionalBranch()) {
212 parseCondBranch(*std::prev(I), TBB, Cond);
213 FBB = I->getOperand(0).getMBB();
214 return false;
215 }
216
217 // Otherwise, we can't handle this.
218 return true;
219}
220
221unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB,
222 int *BytesRemoved) const {
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000223 if (BytesRemoved)
224 *BytesRemoved = 0;
Alex Bradburye027c932018-01-10 20:47:00 +0000225 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
226 if (I == MBB.end())
227 return 0;
228
229 if (!I->getDesc().isUnconditionalBranch() &&
230 !I->getDesc().isConditionalBranch())
231 return 0;
232
233 // Remove the branch.
234 I->eraseFromParent();
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000235 if (BytesRemoved)
236 *BytesRemoved += getInstSizeInBytes(*I);
Alex Bradburye027c932018-01-10 20:47:00 +0000237
238 I = MBB.end();
239
240 if (I == MBB.begin())
241 return 1;
242 --I;
243 if (!I->getDesc().isConditionalBranch())
244 return 1;
245
246 // Remove the branch.
247 I->eraseFromParent();
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000248 if (BytesRemoved)
249 *BytesRemoved += getInstSizeInBytes(*I);
Alex Bradburye027c932018-01-10 20:47:00 +0000250 return 2;
251}
252
253// Inserts a branch into the end of the specific MachineBasicBlock, returning
254// the number of instructions inserted.
255unsigned RISCVInstrInfo::insertBranch(
256 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
257 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000258 if (BytesAdded)
259 *BytesAdded = 0;
Alex Bradburye027c932018-01-10 20:47:00 +0000260
261 // Shouldn't be a fall through.
262 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
263 assert((Cond.size() == 3 || Cond.size() == 0) &&
264 "RISCV branch conditions have two components!");
265
266 // Unconditional branch.
267 if (Cond.empty()) {
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000268 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
269 if (BytesAdded)
270 *BytesAdded += getInstSizeInBytes(MI);
Alex Bradburye027c932018-01-10 20:47:00 +0000271 return 1;
272 }
273
274 // Either a one or two-way conditional branch.
275 unsigned Opc = Cond[0].getImm();
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000276 MachineInstr &CondMI =
277 *BuildMI(&MBB, DL, get(Opc)).add(Cond[1]).add(Cond[2]).addMBB(TBB);
278 if (BytesAdded)
279 *BytesAdded += getInstSizeInBytes(CondMI);
Alex Bradburye027c932018-01-10 20:47:00 +0000280
281 // One-way conditional branch.
282 if (!FBB)
283 return 1;
284
285 // Two-way conditional branch.
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000286 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
287 if (BytesAdded)
288 *BytesAdded += getInstSizeInBytes(MI);
Alex Bradburye027c932018-01-10 20:47:00 +0000289 return 2;
290}
291
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000292unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
293 MachineBasicBlock &DestBB,
294 const DebugLoc &DL,
295 int64_t BrOffset,
296 RegScavenger *RS) const {
297 assert(RS && "RegScavenger required for long branching");
298 assert(MBB.empty() &&
299 "new block should be inserted for expanding unconditional branch");
300 assert(MBB.pred_size() == 1);
301
302 MachineFunction *MF = MBB.getParent();
303 MachineRegisterInfo &MRI = MF->getRegInfo();
304 const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget());
305 const auto &STI = MF->getSubtarget<RISCVSubtarget>();
306
307 if (TM.isPositionIndependent() || STI.is64Bit())
308 report_fatal_error("Unable to insert indirect branch");
309
310 if (!isInt<32>(BrOffset))
311 report_fatal_error(
312 "Branch offsets outside of the signed 32-bit range not supported");
313
314 // FIXME: A virtual register must be used initially, as the register
315 // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
316 // uses the same workaround).
317 unsigned ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
318 auto II = MBB.end();
319
320 MachineInstr &LuiMI = *BuildMI(MBB, II, DL, get(RISCV::LUI), ScratchReg)
321 .addMBB(&DestBB, RISCVII::MO_HI);
322 BuildMI(MBB, II, DL, get(RISCV::PseudoBRIND))
323 .addReg(ScratchReg, RegState::Kill)
324 .addMBB(&DestBB, RISCVII::MO_LO);
325
326 RS->enterBasicBlockEnd(MBB);
327 unsigned Scav = RS->scavengeRegisterBackwards(
328 RISCV::GPRRegClass, MachineBasicBlock::iterator(LuiMI), false, 0);
329 MRI.replaceRegWith(ScratchReg, Scav);
330 MRI.clearVirtRegs();
331 RS->setRegUsed(Scav);
332 return 8;
333}
334
Alex Bradburye027c932018-01-10 20:47:00 +0000335bool RISCVInstrInfo::reverseBranchCondition(
336 SmallVectorImpl<MachineOperand> &Cond) const {
337 assert((Cond.size() == 3) && "Invalid branch condition!");
338 Cond[0].setImm(getOppositeBranchOpcode(Cond[0].getImm()));
339 return false;
340}
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000341
342MachineBasicBlock *
343RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
344 assert(MI.getDesc().isBranch() && "Unexpected opcode!");
345 // The branch target is always the last operand.
346 int NumOp = MI.getNumExplicitOperands();
347 return MI.getOperand(NumOp - 1).getMBB();
348}
349
350bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
351 int64_t BrOffset) const {
352 // Ideally we could determine the supported branch offset from the
353 // RISCVII::FormMask, but this can't be used for Pseudo instructions like
354 // PseudoBR.
355 switch (BranchOp) {
356 default:
357 llvm_unreachable("Unexpected opcode!");
358 case RISCV::BEQ:
359 case RISCV::BNE:
360 case RISCV::BLT:
361 case RISCV::BGE:
362 case RISCV::BLTU:
363 case RISCV::BGEU:
364 return isIntN(13, BrOffset);
365 case RISCV::JAL:
366 case RISCV::PseudoBR:
367 return isIntN(21, BrOffset);
368 }
369}
370
371unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
372 unsigned Opcode = MI.getOpcode();
373
374 switch (Opcode) {
375 default: { return get(Opcode).getSize(); }
376 case TargetOpcode::EH_LABEL:
377 case TargetOpcode::IMPLICIT_DEF:
378 case TargetOpcode::KILL:
379 case TargetOpcode::DBG_VALUE:
380 return 0;
381 case TargetOpcode::INLINEASM: {
382 const MachineFunction &MF = *MI.getParent()->getParent();
383 const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget());
384 return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
385 *TM.getMCAsmInfo());
386 }
387 }
388}