blob: 3454be208420b4c3ed63d23f0d7aab4d50c01b67 [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 Bradbury65d6ea52018-03-21 15:11:02 +000046 if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) {
47 BuildMI(MBB, MBBI, DL, get(RISCV::FSGNJ_S), DstReg)
48 .addReg(SrcReg, getKillRegState(KillSrc))
49 .addReg(SrcReg, getKillRegState(KillSrc));
50 return;
51 }
52
53 llvm_unreachable("Impossible reg-to-reg copy");
Alex Bradburycfa62912017-11-08 12:20:01 +000054}
Alex Bradbury74913e12017-11-08 13:31:40 +000055
56void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
57 MachineBasicBlock::iterator I,
58 unsigned SrcReg, bool IsKill, int FI,
59 const TargetRegisterClass *RC,
60 const TargetRegisterInfo *TRI) const {
61 DebugLoc DL;
62 if (I != MBB.end())
63 DL = I->getDebugLoc();
64
Alex Bradbury65d6ea52018-03-21 15:11:02 +000065 unsigned Opcode;
66
Alex Bradbury87a54d62017-12-07 12:45:05 +000067 if (RISCV::GPRRegClass.hasSubClassEq(RC))
Alex Bradbury65d6ea52018-03-21 15:11:02 +000068 Opcode = RISCV::SW;
69 else if (RISCV::FPR32RegClass.hasSubClassEq(RC))
70 Opcode = RISCV::FSW;
Alex Bradbury0b4175f2018-04-12 05:34:25 +000071 else if (RISCV::FPR64RegClass.hasSubClassEq(RC))
72 Opcode = RISCV::FSD;
Alex Bradbury74913e12017-11-08 13:31:40 +000073 else
74 llvm_unreachable("Can't store this register to stack slot");
Alex Bradbury65d6ea52018-03-21 15:11:02 +000075
76 BuildMI(MBB, I, DL, get(Opcode))
77 .addReg(SrcReg, getKillRegState(IsKill))
78 .addFrameIndex(FI)
79 .addImm(0);
Alex Bradbury74913e12017-11-08 13:31:40 +000080}
81
82void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
83 MachineBasicBlock::iterator I,
84 unsigned DstReg, int FI,
85 const TargetRegisterClass *RC,
86 const TargetRegisterInfo *TRI) const {
87 DebugLoc DL;
88 if (I != MBB.end())
89 DL = I->getDebugLoc();
90
Alex Bradbury65d6ea52018-03-21 15:11:02 +000091 unsigned Opcode;
92
Alex Bradbury87a54d62017-12-07 12:45:05 +000093 if (RISCV::GPRRegClass.hasSubClassEq(RC))
Alex Bradbury65d6ea52018-03-21 15:11:02 +000094 Opcode = RISCV::LW;
95 else if (RISCV::FPR32RegClass.hasSubClassEq(RC))
96 Opcode = RISCV::FLW;
Alex Bradbury0b4175f2018-04-12 05:34:25 +000097 else if (RISCV::FPR64RegClass.hasSubClassEq(RC))
98 Opcode = RISCV::FLD;
Alex Bradbury74913e12017-11-08 13:31:40 +000099 else
100 llvm_unreachable("Can't load this register from stack slot");
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000101
102 BuildMI(MBB, I, DL, get(Opcode), DstReg).addFrameIndex(FI).addImm(0);
Alex Bradbury74913e12017-11-08 13:31:40 +0000103}
Alex Bradbury9fea4882018-01-10 19:53:46 +0000104
105void RISCVInstrInfo::movImm32(MachineBasicBlock &MBB,
106 MachineBasicBlock::iterator MBBI,
107 const DebugLoc &DL, unsigned DstReg, uint64_t Val,
108 MachineInstr::MIFlag Flag) const {
109 assert(isInt<32>(Val) && "Can only materialize 32-bit constants");
110
111 // TODO: If the value can be materialized using only one instruction, only
112 // insert a single instruction.
113
114 uint64_t Hi20 = ((Val + 0x800) >> 12) & 0xfffff;
115 uint64_t Lo12 = SignExtend64<12>(Val);
116 BuildMI(MBB, MBBI, DL, get(RISCV::LUI), DstReg)
117 .addImm(Hi20)
118 .setMIFlag(Flag);
119 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
120 .addReg(DstReg, RegState::Kill)
121 .addImm(Lo12)
122 .setMIFlag(Flag);
123}
Alex Bradburye027c932018-01-10 20:47:00 +0000124
125// The contents of values added to Cond are not examined outside of
126// RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
127// push BranchOpcode, Reg1, Reg2.
128static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target,
129 SmallVectorImpl<MachineOperand> &Cond) {
130 // Block ends with fall-through condbranch.
131 assert(LastInst.getDesc().isConditionalBranch() &&
132 "Unknown conditional branch");
133 Target = LastInst.getOperand(2).getMBB();
134 Cond.push_back(MachineOperand::CreateImm(LastInst.getOpcode()));
135 Cond.push_back(LastInst.getOperand(0));
136 Cond.push_back(LastInst.getOperand(1));
137}
138
139static unsigned getOppositeBranchOpcode(int Opc) {
140 switch (Opc) {
141 default:
142 llvm_unreachable("Unrecognized conditional branch");
143 case RISCV::BEQ:
144 return RISCV::BNE;
145 case RISCV::BNE:
146 return RISCV::BEQ;
147 case RISCV::BLT:
148 return RISCV::BGE;
149 case RISCV::BGE:
150 return RISCV::BLT;
151 case RISCV::BLTU:
152 return RISCV::BGEU;
153 case RISCV::BGEU:
154 return RISCV::BLTU;
155 }
156}
157
158bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
159 MachineBasicBlock *&TBB,
160 MachineBasicBlock *&FBB,
161 SmallVectorImpl<MachineOperand> &Cond,
162 bool AllowModify) const {
163 TBB = FBB = nullptr;
164 Cond.clear();
165
166 // If the block has no terminators, it just falls into the block after it.
167 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
168 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
169 return false;
170
171 // Count the number of terminators and find the first unconditional or
172 // indirect branch.
173 MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end();
174 int NumTerminators = 0;
175 for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J);
176 J++) {
177 NumTerminators++;
178 if (J->getDesc().isUnconditionalBranch() ||
179 J->getDesc().isIndirectBranch()) {
180 FirstUncondOrIndirectBr = J.getReverse();
181 }
182 }
183
184 // If AllowModify is true, we can erase any terminators after
185 // FirstUncondOrIndirectBR.
186 if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) {
187 while (std::next(FirstUncondOrIndirectBr) != MBB.end()) {
188 std::next(FirstUncondOrIndirectBr)->eraseFromParent();
189 NumTerminators--;
190 }
191 I = FirstUncondOrIndirectBr;
192 }
193
194 // We can't handle blocks that end in an indirect branch.
195 if (I->getDesc().isIndirectBranch())
196 return true;
197
198 // We can't handle blocks with more than 2 terminators.
199 if (NumTerminators > 2)
200 return true;
201
202 // Handle a single unconditional branch.
203 if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
204 TBB = I->getOperand(0).getMBB();
205 return false;
206 }
207
208 // Handle a single conditional branch.
209 if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) {
210 parseCondBranch(*I, TBB, Cond);
211 return false;
212 }
213
214 // Handle a conditional branch followed by an unconditional branch.
215 if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
216 I->getDesc().isUnconditionalBranch()) {
217 parseCondBranch(*std::prev(I), TBB, Cond);
218 FBB = I->getOperand(0).getMBB();
219 return false;
220 }
221
222 // Otherwise, we can't handle this.
223 return true;
224}
225
226unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB,
227 int *BytesRemoved) const {
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000228 if (BytesRemoved)
229 *BytesRemoved = 0;
Alex Bradburye027c932018-01-10 20:47:00 +0000230 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
231 if (I == MBB.end())
232 return 0;
233
234 if (!I->getDesc().isUnconditionalBranch() &&
235 !I->getDesc().isConditionalBranch())
236 return 0;
237
238 // Remove the branch.
239 I->eraseFromParent();
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000240 if (BytesRemoved)
241 *BytesRemoved += getInstSizeInBytes(*I);
Alex Bradburye027c932018-01-10 20:47:00 +0000242
243 I = MBB.end();
244
245 if (I == MBB.begin())
246 return 1;
247 --I;
248 if (!I->getDesc().isConditionalBranch())
249 return 1;
250
251 // Remove the branch.
252 I->eraseFromParent();
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000253 if (BytesRemoved)
254 *BytesRemoved += getInstSizeInBytes(*I);
Alex Bradburye027c932018-01-10 20:47:00 +0000255 return 2;
256}
257
258// Inserts a branch into the end of the specific MachineBasicBlock, returning
259// the number of instructions inserted.
260unsigned RISCVInstrInfo::insertBranch(
261 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
262 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000263 if (BytesAdded)
264 *BytesAdded = 0;
Alex Bradburye027c932018-01-10 20:47:00 +0000265
266 // Shouldn't be a fall through.
267 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
268 assert((Cond.size() == 3 || Cond.size() == 0) &&
269 "RISCV branch conditions have two components!");
270
271 // Unconditional branch.
272 if (Cond.empty()) {
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000273 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
274 if (BytesAdded)
275 *BytesAdded += getInstSizeInBytes(MI);
Alex Bradburye027c932018-01-10 20:47:00 +0000276 return 1;
277 }
278
279 // Either a one or two-way conditional branch.
280 unsigned Opc = Cond[0].getImm();
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000281 MachineInstr &CondMI =
282 *BuildMI(&MBB, DL, get(Opc)).add(Cond[1]).add(Cond[2]).addMBB(TBB);
283 if (BytesAdded)
284 *BytesAdded += getInstSizeInBytes(CondMI);
Alex Bradburye027c932018-01-10 20:47:00 +0000285
286 // One-way conditional branch.
287 if (!FBB)
288 return 1;
289
290 // Two-way conditional branch.
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000291 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
292 if (BytesAdded)
293 *BytesAdded += getInstSizeInBytes(MI);
Alex Bradburye027c932018-01-10 20:47:00 +0000294 return 2;
295}
296
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000297unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
298 MachineBasicBlock &DestBB,
299 const DebugLoc &DL,
300 int64_t BrOffset,
301 RegScavenger *RS) const {
302 assert(RS && "RegScavenger required for long branching");
303 assert(MBB.empty() &&
304 "new block should be inserted for expanding unconditional branch");
305 assert(MBB.pred_size() == 1);
306
307 MachineFunction *MF = MBB.getParent();
308 MachineRegisterInfo &MRI = MF->getRegInfo();
309 const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget());
310 const auto &STI = MF->getSubtarget<RISCVSubtarget>();
311
312 if (TM.isPositionIndependent() || STI.is64Bit())
313 report_fatal_error("Unable to insert indirect branch");
314
315 if (!isInt<32>(BrOffset))
316 report_fatal_error(
317 "Branch offsets outside of the signed 32-bit range not supported");
318
319 // FIXME: A virtual register must be used initially, as the register
320 // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
321 // uses the same workaround).
322 unsigned ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
323 auto II = MBB.end();
324
325 MachineInstr &LuiMI = *BuildMI(MBB, II, DL, get(RISCV::LUI), ScratchReg)
326 .addMBB(&DestBB, RISCVII::MO_HI);
327 BuildMI(MBB, II, DL, get(RISCV::PseudoBRIND))
328 .addReg(ScratchReg, RegState::Kill)
329 .addMBB(&DestBB, RISCVII::MO_LO);
330
331 RS->enterBasicBlockEnd(MBB);
332 unsigned Scav = RS->scavengeRegisterBackwards(
333 RISCV::GPRRegClass, MachineBasicBlock::iterator(LuiMI), false, 0);
334 MRI.replaceRegWith(ScratchReg, Scav);
335 MRI.clearVirtRegs();
336 RS->setRegUsed(Scav);
337 return 8;
338}
339
Alex Bradburye027c932018-01-10 20:47:00 +0000340bool RISCVInstrInfo::reverseBranchCondition(
341 SmallVectorImpl<MachineOperand> &Cond) const {
342 assert((Cond.size() == 3) && "Invalid branch condition!");
343 Cond[0].setImm(getOppositeBranchOpcode(Cond[0].getImm()));
344 return false;
345}
Alex Bradbury315cd3a2018-01-10 21:05:07 +0000346
347MachineBasicBlock *
348RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
349 assert(MI.getDesc().isBranch() && "Unexpected opcode!");
350 // The branch target is always the last operand.
351 int NumOp = MI.getNumExplicitOperands();
352 return MI.getOperand(NumOp - 1).getMBB();
353}
354
355bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
356 int64_t BrOffset) const {
357 // Ideally we could determine the supported branch offset from the
358 // RISCVII::FormMask, but this can't be used for Pseudo instructions like
359 // PseudoBR.
360 switch (BranchOp) {
361 default:
362 llvm_unreachable("Unexpected opcode!");
363 case RISCV::BEQ:
364 case RISCV::BNE:
365 case RISCV::BLT:
366 case RISCV::BGE:
367 case RISCV::BLTU:
368 case RISCV::BGEU:
369 return isIntN(13, BrOffset);
370 case RISCV::JAL:
371 case RISCV::PseudoBR:
372 return isIntN(21, BrOffset);
373 }
374}
375
376unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
377 unsigned Opcode = MI.getOpcode();
378
379 switch (Opcode) {
380 default: { return get(Opcode).getSize(); }
381 case TargetOpcode::EH_LABEL:
382 case TargetOpcode::IMPLICIT_DEF:
383 case TargetOpcode::KILL:
384 case TargetOpcode::DBG_VALUE:
385 return 0;
386 case TargetOpcode::INLINEASM: {
387 const MachineFunction &MF = *MI.getParent()->getParent();
388 const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget());
389 return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
390 *TM.getMCAsmInfo());
391 }
392 }
393}