1//===-- SparcInstrInfo.cpp - Sparc Instruction Information ----------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the Sparc implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SparcInstrInfo.h"
14#include "Sparc.h"
15#include "SparcMachineFunctionInfo.h"
16#include "SparcSubtarget.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineMemOperand.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/RegisterScavenging.h"
23#include "llvm/Support/ErrorHandling.h"
24
25using namespace llvm;
26
27#define GET_INSTRINFO_CTOR_DTOR
28#include "SparcGenInstrInfo.inc"
29
30// Pin the vtable to this file.
31void SparcInstrInfo::anchor() {}
32
33SparcInstrInfo::SparcInstrInfo(const SparcSubtarget &ST)
34 : SparcGenInstrInfo(ST, RI, SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP),
35 RI(ST), Subtarget(ST) {}
36
37const TargetRegisterClass *SparcInstrInfo::getInlineAsmMemoryOperandRegClass(
38 InlineAsm::ConstraintCode C) const {
39 return Subtarget.is64Bit() ? &SP::I64RegsRegClass : &SP::IntRegsRegClass;
40}
41
42bool SparcInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
43 const MachineBasicBlock *MBB,
44 const MachineFunction &MF) const {
45 if (MI.getFlag(Flag: MachineInstr::FrameDestroy))
46 return true;
47 switch (MI.getOpcode()) {
48 case SP::SAVErr:
49 case SP::SAVEri:
50 case SP::RESTORErr:
51 case SP::RESTOREri:
52 return true;
53 }
54 return TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF);
55}
56
57/// isLoadFromStackSlot - If the specified machine instruction is a direct
58/// load from a stack slot, return the virtual or physical register number of
59/// the destination along with the FrameIndex of the loaded stack slot. If
60/// not, return 0. This predicate must return 0 if the instruction has
61/// any side effects other than loading from the stack slot.
62Register SparcInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
63 int &FrameIndex,
64 TypeSize &MemBytes) const {
65 switch (MI.getOpcode()) {
66 default:
67 return 0;
68 case SP::LDri:
69 MemBytes = TypeSize::getFixed(ExactSize: 4);
70 break;
71 case SP::LDXri:
72 MemBytes = TypeSize::getFixed(ExactSize: 8);
73 break;
74 case SP::LDFri:
75 MemBytes = TypeSize::getFixed(ExactSize: 4);
76 break;
77 case SP::LDDFri:
78 MemBytes = TypeSize::getFixed(ExactSize: 8);
79 break;
80 case SP::LDQFri:
81 MemBytes = TypeSize::getFixed(ExactSize: 16);
82 break;
83 }
84 if (MI.getOperand(i: 1).isFI() && MI.getOperand(i: 2).isImm() &&
85 MI.getOperand(i: 2).getImm() == 0) {
86 FrameIndex = MI.getOperand(i: 1).getIndex();
87 return MI.getOperand(i: 0).getReg();
88 }
89 return 0;
90}
91
92/// isStoreToStackSlot - If the specified machine instruction is a direct
93/// store to a stack slot, return the virtual or physical register number of
94/// the source reg along with the FrameIndex of the loaded stack slot. If
95/// not, return 0. This predicate must return 0 if the instruction has
96/// any side effects other than storing to the stack slot.
97Register SparcInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
98 int &FrameIndex,
99 TypeSize &MemBytes) const {
100 switch (MI.getOpcode()) {
101 default:
102 return 0;
103 case SP::STri:
104 MemBytes = TypeSize::getFixed(ExactSize: 4);
105 break;
106 case SP::STXri:
107 MemBytes = TypeSize::getFixed(ExactSize: 8);
108 break;
109 case SP::STFri:
110 MemBytes = TypeSize::getFixed(ExactSize: 4);
111 break;
112 case SP::STDFri:
113 MemBytes = TypeSize::getFixed(ExactSize: 8);
114 break;
115 case SP::STQFri:
116 MemBytes = TypeSize::getFixed(ExactSize: 16);
117 break;
118 }
119 if (MI.getOperand(i: 0).isFI() && MI.getOperand(i: 1).isImm() &&
120 MI.getOperand(i: 1).getImm() == 0) {
121 FrameIndex = MI.getOperand(i: 0).getIndex();
122 return MI.getOperand(i: 2).getReg();
123 }
124 return 0;
125}
126
127static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC)
128{
129 switch(CC) {
130 case SPCC::ICC_A: return SPCC::ICC_N;
131 case SPCC::ICC_N: return SPCC::ICC_A;
132 case SPCC::ICC_NE: return SPCC::ICC_E;
133 case SPCC::ICC_E: return SPCC::ICC_NE;
134 case SPCC::ICC_G: return SPCC::ICC_LE;
135 case SPCC::ICC_LE: return SPCC::ICC_G;
136 case SPCC::ICC_GE: return SPCC::ICC_L;
137 case SPCC::ICC_L: return SPCC::ICC_GE;
138 case SPCC::ICC_GU: return SPCC::ICC_LEU;
139 case SPCC::ICC_LEU: return SPCC::ICC_GU;
140 case SPCC::ICC_CC: return SPCC::ICC_CS;
141 case SPCC::ICC_CS: return SPCC::ICC_CC;
142 case SPCC::ICC_POS: return SPCC::ICC_NEG;
143 case SPCC::ICC_NEG: return SPCC::ICC_POS;
144 case SPCC::ICC_VC: return SPCC::ICC_VS;
145 case SPCC::ICC_VS: return SPCC::ICC_VC;
146
147 case SPCC::FCC_A: return SPCC::FCC_N;
148 case SPCC::FCC_N: return SPCC::FCC_A;
149 case SPCC::FCC_U: return SPCC::FCC_O;
150 case SPCC::FCC_O: return SPCC::FCC_U;
151 case SPCC::FCC_G: return SPCC::FCC_ULE;
152 case SPCC::FCC_LE: return SPCC::FCC_UG;
153 case SPCC::FCC_UG: return SPCC::FCC_LE;
154 case SPCC::FCC_ULE: return SPCC::FCC_G;
155 case SPCC::FCC_L: return SPCC::FCC_UGE;
156 case SPCC::FCC_GE: return SPCC::FCC_UL;
157 case SPCC::FCC_UL: return SPCC::FCC_GE;
158 case SPCC::FCC_UGE: return SPCC::FCC_L;
159 case SPCC::FCC_LG: return SPCC::FCC_UE;
160 case SPCC::FCC_UE: return SPCC::FCC_LG;
161 case SPCC::FCC_NE: return SPCC::FCC_E;
162 case SPCC::FCC_E: return SPCC::FCC_NE;
163
164 case SPCC::CPCC_A: return SPCC::CPCC_N;
165 case SPCC::CPCC_N: return SPCC::CPCC_A;
166 case SPCC::CPCC_3: [[fallthrough]];
167 case SPCC::CPCC_2: [[fallthrough]];
168 case SPCC::CPCC_23: [[fallthrough]];
169 case SPCC::CPCC_1: [[fallthrough]];
170 case SPCC::CPCC_13: [[fallthrough]];
171 case SPCC::CPCC_12: [[fallthrough]];
172 case SPCC::CPCC_123: [[fallthrough]];
173 case SPCC::CPCC_0: [[fallthrough]];
174 case SPCC::CPCC_03: [[fallthrough]];
175 case SPCC::CPCC_02: [[fallthrough]];
176 case SPCC::CPCC_023: [[fallthrough]];
177 case SPCC::CPCC_01: [[fallthrough]];
178 case SPCC::CPCC_013: [[fallthrough]];
179 case SPCC::CPCC_012:
180 // "Opposite" code is not meaningful, as we don't know
181 // what the CoProc condition means here. The cond-code will
182 // only be used in inline assembler, so this code should
183 // not be reached in a normal compilation pass.
184 llvm_unreachable("Meaningless inversion of co-processor cond code");
185
186 case SPCC::REG_BEGIN:
187 llvm_unreachable("Use of reserved cond code");
188 case SPCC::REG_Z:
189 return SPCC::REG_NZ;
190 case SPCC::REG_LEZ:
191 return SPCC::REG_GZ;
192 case SPCC::REG_LZ:
193 return SPCC::REG_GEZ;
194 case SPCC::REG_NZ:
195 return SPCC::REG_Z;
196 case SPCC::REG_GZ:
197 return SPCC::REG_LEZ;
198 case SPCC::REG_GEZ:
199 return SPCC::REG_LZ;
200 }
201 llvm_unreachable("Invalid cond code");
202}
203
204static bool isUncondBranchOpcode(int Opc) { return Opc == SP::BA; }
205
206static bool isI32CondBranchOpcode(int Opc) {
207 return Opc == SP::BCOND || Opc == SP::BPICC || Opc == SP::BPICCA ||
208 Opc == SP::BPICCNT || Opc == SP::BPICCANT;
209}
210
211static bool isI64CondBranchOpcode(int Opc) {
212 return Opc == SP::BPXCC || Opc == SP::BPXCCA || Opc == SP::BPXCCNT ||
213 Opc == SP::BPXCCANT;
214}
215
216static bool isRegCondBranchOpcode(int Opc) {
217 return Opc == SP::BPR || Opc == SP::BPRA || Opc == SP::BPRNT ||
218 Opc == SP::BPRANT;
219}
220
221static bool isFCondBranchOpcode(int Opc) {
222 return Opc == SP::FBCOND || Opc == SP::FBCONDA || Opc == SP::FBCOND_V9 ||
223 Opc == SP::FBCONDA_V9;
224}
225
226static bool isCondBranchOpcode(int Opc) {
227 return isI32CondBranchOpcode(Opc) || isI64CondBranchOpcode(Opc) ||
228 isRegCondBranchOpcode(Opc) || isFCondBranchOpcode(Opc);
229}
230
231static bool isIndirectBranchOpcode(int Opc) {
232 return Opc == SP::BINDrr || Opc == SP::BINDri;
233}
234
235static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target,
236 SmallVectorImpl<MachineOperand> &Cond) {
237 unsigned Opc = LastInst->getOpcode();
238 int64_t CC = LastInst->getOperand(i: 1).getImm();
239
240 // Push the branch opcode into Cond too so later in insertBranch
241 // it can use the information to emit the correct SPARC branch opcode.
242 Cond.push_back(Elt: MachineOperand::CreateImm(Val: Opc));
243 Cond.push_back(Elt: MachineOperand::CreateImm(Val: CC));
244
245 // Branch on register contents need another argument to indicate
246 // the register it branches on.
247 if (isRegCondBranchOpcode(Opc))
248 Cond.push_back(Elt: LastInst->getOperand(i: 2));
249
250 Target = LastInst->getOperand(i: 0).getMBB();
251}
252
253MachineBasicBlock *
254SparcInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
255 switch (MI.getOpcode()) {
256 default:
257 llvm_unreachable("unexpected opcode!");
258 case SP::BA:
259 case SP::BCOND:
260 case SP::BCONDA:
261 case SP::FBCOND:
262 case SP::FBCONDA:
263 case SP::BPICC:
264 case SP::BPICCA:
265 case SP::BPICCNT:
266 case SP::BPICCANT:
267 case SP::BPXCC:
268 case SP::BPXCCA:
269 case SP::BPXCCNT:
270 case SP::BPXCCANT:
271 case SP::BPFCC:
272 case SP::BPFCCA:
273 case SP::BPFCCNT:
274 case SP::BPFCCANT:
275 case SP::FBCOND_V9:
276 case SP::FBCONDA_V9:
277 case SP::BPR:
278 case SP::BPRA:
279 case SP::BPRNT:
280 case SP::BPRANT:
281 case SP::CALL_LONGJUMP:
282 return MI.getOperand(i: 0).getMBB();
283 }
284}
285
286bool SparcInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
287 MachineBasicBlock *&TBB,
288 MachineBasicBlock *&FBB,
289 SmallVectorImpl<MachineOperand> &Cond,
290 bool AllowModify) const {
291 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
292 if (I == MBB.end())
293 return false;
294
295 if (!isUnpredicatedTerminator(MI: *I))
296 return false;
297
298 // Get the last instruction in the block.
299 MachineInstr *LastInst = &*I;
300 unsigned LastOpc = LastInst->getOpcode();
301
302 // If there is only one terminator instruction, process it.
303 if (I == MBB.begin() || !isUnpredicatedTerminator(MI: *--I)) {
304 if (isUncondBranchOpcode(Opc: LastOpc)) {
305 TBB = LastInst->getOperand(i: 0).getMBB();
306 return false;
307 }
308 if (isCondBranchOpcode(Opc: LastOpc)) {
309 // Block ends with fall-through condbranch.
310 parseCondBranch(LastInst, Target&: TBB, Cond);
311 return false;
312 }
313 return true; // Can't handle indirect branch.
314 }
315
316 // Get the instruction before it if it is a terminator.
317 MachineInstr *SecondLastInst = &*I;
318 unsigned SecondLastOpc = SecondLastInst->getOpcode();
319
320 // If AllowModify is true and the block ends with two or more unconditional
321 // branches, delete all but the first unconditional branch.
322 if (AllowModify && isUncondBranchOpcode(Opc: LastOpc)) {
323 while (isUncondBranchOpcode(Opc: SecondLastOpc)) {
324 LastInst->eraseFromParent();
325 LastInst = SecondLastInst;
326 LastOpc = LastInst->getOpcode();
327 if (I == MBB.begin() || !isUnpredicatedTerminator(MI: *--I)) {
328 // Return now the only terminator is an unconditional branch.
329 TBB = LastInst->getOperand(i: 0).getMBB();
330 return false;
331 } else {
332 SecondLastInst = &*I;
333 SecondLastOpc = SecondLastInst->getOpcode();
334 }
335 }
336 }
337
338 // If there are three terminators, we don't know what sort of block this is.
339 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(MI: *--I))
340 return true;
341
342 // If the block ends with a B and a Bcc, handle it.
343 if (isCondBranchOpcode(Opc: SecondLastOpc) && isUncondBranchOpcode(Opc: LastOpc)) {
344 parseCondBranch(LastInst: SecondLastInst, Target&: TBB, Cond);
345 FBB = LastInst->getOperand(i: 0).getMBB();
346 return false;
347 }
348
349 // If the block ends with two unconditional branches, handle it. The second
350 // one is not executed.
351 if (isUncondBranchOpcode(Opc: SecondLastOpc) && isUncondBranchOpcode(Opc: LastOpc)) {
352 TBB = SecondLastInst->getOperand(i: 0).getMBB();
353 return false;
354 }
355
356 // ...likewise if it ends with an indirect branch followed by an unconditional
357 // branch.
358 if (isIndirectBranchOpcode(Opc: SecondLastOpc) && isUncondBranchOpcode(Opc: LastOpc)) {
359 I = LastInst;
360 if (AllowModify)
361 I->eraseFromParent();
362 return true;
363 }
364
365 // Otherwise, can't handle this.
366 return true;
367}
368
369unsigned SparcInstrInfo::insertBranch(MachineBasicBlock &MBB,
370 MachineBasicBlock *TBB,
371 MachineBasicBlock *FBB,
372 ArrayRef<MachineOperand> Cond,
373 const DebugLoc &DL,
374 int *BytesAdded) const {
375 assert(TBB && "insertBranch must not be told to insert a fallthrough");
376 assert((Cond.size() <= 3) &&
377 "Sparc branch conditions should have at most three components!");
378
379 if (Cond.empty()) {
380 assert(!FBB && "Unconditional branch with multiple successors!");
381 BuildMI(BB: &MBB, MIMD: DL, MCID: get(Opcode: SP::BA)).addMBB(MBB: TBB);
382 if (BytesAdded)
383 *BytesAdded = 8;
384 return 1;
385 }
386
387 // Conditional branch
388 unsigned Opc = Cond[0].getImm();
389 unsigned CC = Cond[1].getImm();
390 if (isRegCondBranchOpcode(Opc)) {
391 BuildMI(BB: &MBB, MIMD: DL, MCID: get(Opcode: Opc)).addMBB(MBB: TBB).addImm(Val: CC).add(MO: Cond[2]);
392 } else {
393 BuildMI(BB: &MBB, MIMD: DL, MCID: get(Opcode: Opc)).addMBB(MBB: TBB).addImm(Val: CC);
394 }
395
396 if (!FBB) {
397 if (BytesAdded)
398 *BytesAdded = 8;
399 return 1;
400 }
401
402 BuildMI(BB: &MBB, MIMD: DL, MCID: get(Opcode: SP::BA)).addMBB(MBB: FBB);
403 if (BytesAdded)
404 *BytesAdded = 16;
405 return 2;
406}
407
408void SparcInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
409 MachineBasicBlock &NewDestBB,
410 MachineBasicBlock &RestoreBB,
411 const DebugLoc &DL, int64_t BrOffset,
412 RegScavenger *RS) const {
413 assert(RS && "RegScavenger required for long branching");
414 assert(MBB.empty() &&
415 "new block should be inserted for expanding unconditional branch");
416 assert(MBB.pred_size() == 1 && "should only have a single predecessor");
417 assert(RestoreBB.empty() &&
418 "restore block should be inserted for restoring clobbered registers");
419
420 MachineFunction *MF = MBB.getParent();
421 SparcMachineFunctionInfo *SMFI = MF->getInfo<SparcMachineFunctionInfo>();
422
423 if (!isInt<32>(x: BrOffset))
424 report_fatal_error(
425 reason: "Branch offsets outside of the signed 32-bit range not supported");
426 RS->enterBasicBlockEnd(MBB);
427
428 // A SPARC CALL instruction already has signed 32-bit range, so we'll use it
429 // instead of actually emitting an indirect branch.
430 //
431 // Note that using CALLs to do unconditional long-range branch in this manner
432 // should only be done if the target is out of range of a regular BA
433 // instruction, given that CALLS will always clobber %o7 and interfere with
434 // the processor's predictor machinery.
435 bool IsO7Used = RS->isRegUsed(Reg: SP::O7);
436 Register ScratchReg = RS->FindUnusedReg(RC: &SP::IntRegsRegClass);
437 int FI = SMFI->getBranchRelaxationSpillFrameIndex();
438 if (IsO7Used) {
439 if (ScratchReg.isValid()) {
440 BuildMI(BB: &MBB, MIMD: DL, MCID: get(Opcode: SP::ORrr), DestReg: ScratchReg)
441 .addReg(RegNo: SP::G0)
442 .addReg(RegNo: SP::O7, Flags: RegState::Kill);
443 RS->setRegUsed(Reg: ScratchReg);
444 } else {
445 // In case we run out of usable registers we'll spill it into the stack.
446 if (FI == -1)
447 report_fatal_error(reason: "The function size is incorrectly estimated.");
448 const MCRegisterClass &RC =
449 Subtarget.is64Bit() ? SP::I64RegsRegClass : SP::IntRegsRegClass;
450
451 MachineBasicBlock::iterator I = MBB.end();
452 storeRegToStackSlot(MBB, MBBI: I, SrcReg: SP::O7, isKill: true, FrameIndex: FI, RC: &RC, VReg: Register());
453 TRI.eliminateFrameIndex(MI: std::prev(x: I), SPAdj: 0, FIOperandNum: 0, RS);
454 }
455 }
456
457 BuildMI(BB: &MBB, MIMD: DL, MCID: get(Opcode: SP::CALL_LONGJUMP))
458 .addMBB(MBB: IsO7Used ? &RestoreBB : &NewDestBB);
459
460 if (IsO7Used) {
461 if (ScratchReg.isValid()) {
462 BuildMI(BB: &RestoreBB, MIMD: DL, MCID: get(Opcode: SP::ORrr), DestReg: SP::O7)
463 .addReg(RegNo: SP::G0)
464 .addReg(RegNo: ScratchReg, Flags: RegState::Kill);
465 } else {
466 const MCRegisterClass &RC =
467 Subtarget.is64Bit() ? SP::I64RegsRegClass : SP::IntRegsRegClass;
468
469 MachineBasicBlock::iterator I = RestoreBB.begin();
470 loadRegFromStackSlot(MBB&: RestoreBB, MBBI: I, DestReg: SP::O7, FrameIndex: FI, RC: &RC, VReg: Register());
471 TRI.eliminateFrameIndex(MI: std::prev(x: I), SPAdj: 0, FIOperandNum: 1, RS);
472 }
473 }
474}
475
476unsigned SparcInstrInfo::removeBranch(MachineBasicBlock &MBB,
477 int *BytesRemoved) const {
478 MachineBasicBlock::iterator I = MBB.end();
479 unsigned Count = 0;
480 int Removed = 0;
481 while (I != MBB.begin()) {
482 --I;
483
484 if (I->isDebugInstr())
485 continue;
486
487 if (!isCondBranchOpcode(Opc: I->getOpcode()) &&
488 !isUncondBranchOpcode(Opc: I->getOpcode()))
489 break; // Not a branch
490
491 Removed += getInstSizeInBytes(MI: *I);
492 I->eraseFromParent();
493 I = MBB.end();
494 ++Count;
495 }
496
497 if (BytesRemoved)
498 *BytesRemoved = Removed;
499 return Count;
500}
501
502bool SparcInstrInfo::reverseBranchCondition(
503 SmallVectorImpl<MachineOperand> &Cond) const {
504 assert(Cond.size() <= 3);
505 SPCC::CondCodes CC = static_cast<SPCC::CondCodes>(Cond[1].getImm());
506 Cond[1].setImm(GetOppositeBranchCondition(CC));
507 return false;
508}
509
510bool SparcInstrInfo::isBranchOffsetInRange(unsigned BranchOpc,
511 int64_t Offset) const {
512 assert((Offset & 0b11) == 0 && "Malformed branch offset");
513 switch (BranchOpc) {
514 case SP::CALL_LONGJUMP:
515 return isIntN(N: 30, x: Offset >> 2);
516
517 case SP::BA:
518 case SP::BCOND:
519 case SP::BCONDA:
520 case SP::FBCOND:
521 case SP::FBCONDA:
522 return isIntN(N: 22, x: Offset >> 2);
523
524 case SP::BPICC:
525 case SP::BPICCA:
526 case SP::BPICCNT:
527 case SP::BPICCANT:
528 case SP::BPXCC:
529 case SP::BPXCCA:
530 case SP::BPXCCNT:
531 case SP::BPXCCANT:
532 case SP::BPFCC:
533 case SP::BPFCCA:
534 case SP::BPFCCNT:
535 case SP::BPFCCANT:
536 case SP::FBCOND_V9:
537 case SP::FBCONDA_V9:
538 return isIntN(N: Subtarget.getCLOpts().bpcc_offset_bits, x: Offset >> 2);
539
540 case SP::BPR:
541 case SP::BPRA:
542 case SP::BPRNT:
543 case SP::BPRANT:
544 return isIntN(N: Subtarget.getCLOpts().bpr_offset_bits, x: Offset >> 2);
545 }
546
547 llvm_unreachable("Unknown branch instruction!");
548}
549
550void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
551 MachineBasicBlock::iterator I,
552 const DebugLoc &DL, Register DestReg,
553 Register SrcReg, bool KillSrc,
554 bool RenamableDest, bool RenamableSrc) const {
555 unsigned numSubRegs = 0;
556 unsigned movOpc = 0;
557 const unsigned *subRegIdx = nullptr;
558 bool ExtraG0 = false;
559
560 const unsigned DW_SubRegsIdx[] = { SP::sub_even, SP::sub_odd };
561 const unsigned DFP_FP_SubRegsIdx[] = { SP::sub_even, SP::sub_odd };
562 const unsigned QFP_DFP_SubRegsIdx[] = { SP::sub_even64, SP::sub_odd64 };
563 const unsigned QFP_FP_SubRegsIdx[] = { SP::sub_even, SP::sub_odd,
564 SP::sub_odd64_then_sub_even,
565 SP::sub_odd64_then_sub_odd };
566
567 if (SP::IntRegsRegClass.contains(Reg1: DestReg, Reg2: SrcReg))
568 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::ORrr), DestReg).addReg(RegNo: SP::G0)
569 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: KillSrc));
570 else if (SP::IntPairRegClass.contains(Reg1: DestReg, Reg2: SrcReg)) {
571 subRegIdx = DW_SubRegsIdx;
572 numSubRegs = 2;
573 movOpc = SP::ORrr;
574 ExtraG0 = true;
575 } else if (SP::FPRegsRegClass.contains(Reg1: DestReg, Reg2: SrcReg))
576 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::FMOVS), DestReg)
577 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: KillSrc));
578 else if (SP::DFPRegsRegClass.contains(Reg1: DestReg, Reg2: SrcReg)) {
579 if (Subtarget.isV9()) {
580 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::FMOVD), DestReg)
581 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: KillSrc));
582 } else {
583 // Use two FMOVS instructions.
584 subRegIdx = DFP_FP_SubRegsIdx;
585 numSubRegs = 2;
586 movOpc = SP::FMOVS;
587 }
588 } else if (SP::QFPRegsRegClass.contains(Reg1: DestReg, Reg2: SrcReg)) {
589 if (Subtarget.isV9()) {
590 if (Subtarget.hasHardQuad()) {
591 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::FMOVQ), DestReg)
592 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: KillSrc));
593 } else {
594 // Use two FMOVD instructions.
595 subRegIdx = QFP_DFP_SubRegsIdx;
596 numSubRegs = 2;
597 movOpc = SP::FMOVD;
598 }
599 } else {
600 // Use four FMOVS instructions.
601 subRegIdx = QFP_FP_SubRegsIdx;
602 numSubRegs = 4;
603 movOpc = SP::FMOVS;
604 }
605 } else if (SP::ASRRegsRegClass.contains(Reg: DestReg) &&
606 SP::IntRegsRegClass.contains(Reg: SrcReg)) {
607 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::WRASRrr), DestReg)
608 .addReg(RegNo: SP::G0)
609 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: KillSrc));
610 } else if (SP::IntRegsRegClass.contains(Reg: DestReg) &&
611 SP::ASRRegsRegClass.contains(Reg: SrcReg)) {
612 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::RDASR), DestReg)
613 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: KillSrc));
614 } else
615 llvm_unreachable("Impossible reg-to-reg copy");
616
617 if (numSubRegs == 0 || subRegIdx == nullptr || movOpc == 0)
618 return;
619
620 const TargetRegisterInfo *TRI = &getRegisterInfo();
621 MachineInstr *MovMI = nullptr;
622
623 for (unsigned i = 0; i != numSubRegs; ++i) {
624 Register Dst = TRI->getSubReg(Reg: DestReg, Idx: subRegIdx[i]);
625 Register Src = TRI->getSubReg(Reg: SrcReg, Idx: subRegIdx[i]);
626 assert(Dst && Src && "Bad sub-register");
627
628 MachineInstrBuilder MIB = BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: movOpc), DestReg: Dst);
629 if (ExtraG0)
630 MIB.addReg(RegNo: SP::G0);
631 MIB.addReg(RegNo: Src);
632 MovMI = MIB.getInstr();
633 }
634 // Add implicit super-register defs and kills to the last MovMI.
635 MovMI->addRegisterDefined(Reg: DestReg, RegInfo: TRI);
636 if (KillSrc)
637 MovMI->addRegisterKilled(IncomingReg: SrcReg, RegInfo: TRI);
638}
639
640void SparcInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
641 MachineBasicBlock::iterator I,
642 Register SrcReg, bool isKill, int FI,
643 const TargetRegisterClass *RC,
644 Register VReg,
645 MachineInstr::MIFlag Flags) const {
646 DebugLoc DL;
647 if (I != MBB.end()) DL = I->getDebugLoc();
648
649 MachineFunction *MF = MBB.getParent();
650 const MachineFrameInfo &MFI = MF->getFrameInfo();
651 MachineMemOperand *MMO = MF->getMachineMemOperand(
652 PtrInfo: MachinePointerInfo::getFixedStack(MF&: *MF, FI), F: MachineMemOperand::MOStore,
653 Size: MFI.getObjectSize(ObjectIdx: FI), BaseAlignment: MFI.getObjectAlign(ObjectIdx: FI));
654
655 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
656 if (RC == &SP::I64RegsRegClass)
657 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::STXri)).addFrameIndex(Idx: FI).addImm(Val: 0)
658 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: isKill)).addMemOperand(MMO);
659 else if (RC == &SP::IntRegsRegClass)
660 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::STri)).addFrameIndex(Idx: FI).addImm(Val: 0)
661 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: isKill)).addMemOperand(MMO);
662 else if (RC == &SP::IntPairRegClass)
663 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::STDri)).addFrameIndex(Idx: FI).addImm(Val: 0)
664 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: isKill)).addMemOperand(MMO);
665 else if (RC == &SP::FPRegsRegClass)
666 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::STFri)).addFrameIndex(Idx: FI).addImm(Val: 0)
667 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: isKill)).addMemOperand(MMO);
668 else if (SP::DFPRegsRegClass.hasSubClassEq(RC))
669 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::STDFri)).addFrameIndex(Idx: FI).addImm(Val: 0)
670 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: isKill)).addMemOperand(MMO);
671 else if (SP::QFPRegsRegClass.hasSubClassEq(RC))
672 // Use STQFri irrespective of its legality. If STQ is not legal, it will be
673 // lowered into two STDs in eliminateFrameIndex.
674 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::STQFri)).addFrameIndex(Idx: FI).addImm(Val: 0)
675 .addReg(RegNo: SrcReg, Flags: getKillRegState(B: isKill)).addMemOperand(MMO);
676 else
677 llvm_unreachable("Can't store this register to stack slot");
678}
679
680void SparcInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
681 MachineBasicBlock::iterator I,
682 Register DestReg, int FI,
683 const TargetRegisterClass *RC,
684 Register VReg, unsigned SubReg,
685 MachineInstr::MIFlag Flags) const {
686 DebugLoc DL;
687 if (I != MBB.end()) DL = I->getDebugLoc();
688
689 MachineFunction *MF = MBB.getParent();
690 const MachineFrameInfo &MFI = MF->getFrameInfo();
691 MachineMemOperand *MMO = MF->getMachineMemOperand(
692 PtrInfo: MachinePointerInfo::getFixedStack(MF&: *MF, FI), F: MachineMemOperand::MOLoad,
693 Size: MFI.getObjectSize(ObjectIdx: FI), BaseAlignment: MFI.getObjectAlign(ObjectIdx: FI));
694
695 if (RC == &SP::I64RegsRegClass)
696 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::LDXri), DestReg).addFrameIndex(Idx: FI).addImm(Val: 0)
697 .addMemOperand(MMO);
698 else if (RC == &SP::IntRegsRegClass)
699 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::LDri), DestReg).addFrameIndex(Idx: FI).addImm(Val: 0)
700 .addMemOperand(MMO);
701 else if (RC == &SP::IntPairRegClass)
702 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::LDDri), DestReg).addFrameIndex(Idx: FI).addImm(Val: 0)
703 .addMemOperand(MMO);
704 else if (RC == &SP::FPRegsRegClass)
705 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::LDFri), DestReg).addFrameIndex(Idx: FI).addImm(Val: 0)
706 .addMemOperand(MMO);
707 else if (SP::DFPRegsRegClass.hasSubClassEq(RC))
708 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::LDDFri), DestReg).addFrameIndex(Idx: FI).addImm(Val: 0)
709 .addMemOperand(MMO);
710 else if (SP::QFPRegsRegClass.hasSubClassEq(RC))
711 // Use LDQFri irrespective of its legality. If LDQ is not legal, it will be
712 // lowered into two LDDs in eliminateFrameIndex.
713 BuildMI(BB&: MBB, I, MIMD: DL, MCID: get(Opcode: SP::LDQFri), DestReg).addFrameIndex(Idx: FI).addImm(Val: 0)
714 .addMemOperand(MMO);
715 else
716 llvm_unreachable("Can't load this register from stack slot");
717}
718
719Register SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
720 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
721 Register GlobalBaseReg = SparcFI->getGlobalBaseReg();
722 if (GlobalBaseReg)
723 return GlobalBaseReg;
724
725 // Insert the set of GlobalBaseReg into the first MBB of the function
726 MachineBasicBlock &FirstMBB = MF->front();
727 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
728 MachineRegisterInfo &RegInfo = MF->getRegInfo();
729
730 const TargetRegisterClass *PtrRC =
731 Subtarget.is64Bit() ? &SP::I64RegsRegClass : &SP::IntRegsRegClass;
732 GlobalBaseReg = RegInfo.createVirtualRegister(RegClass: PtrRC);
733
734 DebugLoc dl;
735
736 BuildMI(BB&: FirstMBB, I: MBBI, MIMD: dl, MCID: get(Opcode: SP::GETPCX), DestReg: GlobalBaseReg)
737 .setOperandDead(1); // implicit-def $o7
738 SparcFI->setGlobalBaseReg(GlobalBaseReg);
739 return GlobalBaseReg;
740}
741
742bool SparcInstrInfo::needsUnimp(const MachineInstr &MI,
743 unsigned &StructSize) const {
744 if (!MI.isCall())
745 return false;
746
747 unsigned StructSizeOpNum = 0;
748 switch (MI.getOpcode()) {
749 default:
750 llvm_unreachable("Unknown call opcode.");
751 case SP::CALL:
752 StructSizeOpNum = 1;
753 break;
754 case SP::CALLrr:
755 case SP::CALLri:
756 StructSizeOpNum = 2;
757 break;
758 case SP::TLS_CALL:
759 return false;
760 case SP::TAIL_CALLri:
761 case SP::TAIL_CALL:
762 return false;
763 }
764
765 const MachineOperand &MO = MI.getOperand(i: StructSizeOpNum);
766 if (!MO.isImm())
767 return false;
768
769 // A zero-sized return value has nothing for the callee to copy, so GCC emits
770 // no unimp for it and returns to the instruction right after the delay slot.
771 // We replicate this behavior here.
772 StructSize = MO.getImm();
773 return StructSize != 0;
774}
775
776unsigned SparcInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
777 unsigned Opcode = MI.getOpcode();
778
779 if (MI.isInlineAsm()) {
780 const MachineFunction *MF = MI.getParent()->getParent();
781 const char *AsmStr = MI.getOperand(i: 0).getSymbolName();
782 return getInlineAsmLength(Str: AsmStr, MAI: MF->getTarget().getMCAsmInfo());
783 }
784
785 if (Opcode == TargetOpcode::BUNDLE)
786 return getInstBundleSize(MI);
787
788 if (MI.getOpcode() == SP::GETPCX) {
789 const TargetMachine &TM = MI.getParent()->getParent()->getTarget();
790 if (TM.isPositionIndependent())
791 return 16;
792 switch (TM.getCodeModel()) {
793 default:
794 llvm_unreachable("Unsupported absolute code model");
795 case CodeModel::Small:
796 return 8;
797 case CodeModel::Medium:
798 return 16;
799 case CodeModel::Large:
800 return 24;
801 }
802 }
803
804 // If the instruction has a delay slot, be conservative and also include
805 // it for sizing purposes. This is done so that the BranchRelaxation pass
806 // will not mistakenly mark out-of-range branches as in-range.
807 if (MI.hasDelaySlot()) {
808 unsigned StructSize = 0;
809 return get(Opcode).getSize() * (2 + needsUnimp(MI, StructSize));
810 }
811 return get(Opcode).getSize();
812}
813
814bool SparcInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
815 Register &SrcReg2, int64_t &CmpMask,
816 int64_t &CmpValue) const {
817 Register DstReg;
818 switch (MI.getOpcode()) {
819 default:
820 break;
821 case SP::SUBCCri:
822 DstReg = MI.getOperand(i: 0).getReg();
823 SrcReg = MI.getOperand(i: 1).getReg();
824 SrcReg2 = 0;
825 CmpMask = ~0;
826 CmpValue = MI.getOperand(i: 2).getImm();
827 return DstReg == SP::G0 && CmpValue == 0;
828 case SP::SUBCCrr:
829 DstReg = MI.getOperand(i: 0).getReg();
830 SrcReg = MI.getOperand(i: 1).getReg();
831 SrcReg2 = MI.getOperand(i: 2).getReg();
832 CmpMask = ~0;
833 CmpValue = 0;
834 return DstReg == SP::G0 && SrcReg2 == SP::G0;
835 }
836
837 return false;
838}
839
840bool SparcInstrInfo::optimizeCompareInstr(
841 MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask,
842 int64_t CmpValue, const MachineRegisterInfo *MRI) const {
843
844 // Get the unique definition of SrcReg.
845 MachineInstr *MI = MRI->getUniqueVRegDef(Reg: SrcReg);
846 if (!MI)
847 return false;
848
849 // Only optimize if defining and comparing instruction in same block.
850 if (MI->getParent() != CmpInstr.getParent())
851 return false;
852
853 unsigned NewOpcode;
854 switch (MI->getOpcode()) {
855 case SP::ANDNrr:
856 NewOpcode = SP::ANDNCCrr;
857 break;
858 case SP::ANDNri:
859 NewOpcode = SP::ANDNCCri;
860 break;
861 case SP::ANDrr:
862 NewOpcode = SP::ANDCCrr;
863 break;
864 case SP::ANDri:
865 NewOpcode = SP::ANDCCri;
866 break;
867 case SP::ORrr:
868 NewOpcode = SP::ORCCrr;
869 break;
870 case SP::ORri:
871 NewOpcode = SP::ORCCri;
872 break;
873 case SP::ORNCCrr:
874 NewOpcode = SP::ORNCCrr;
875 break;
876 case SP::ORNri:
877 NewOpcode = SP::ORNCCri;
878 break;
879 case SP::XORrr:
880 NewOpcode = SP::XORCCrr;
881 break;
882 case SP::XNORri:
883 NewOpcode = SP::XNORCCri;
884 break;
885 case SP::XNORrr:
886 NewOpcode = SP::XNORCCrr;
887 break;
888 case SP::ADDrr:
889 NewOpcode = SP::ADDCCrr;
890 break;
891 case SP::ADDri:
892 NewOpcode = SP::ADDCCri;
893 break;
894 case SP::SUBrr:
895 NewOpcode = SP::SUBCCrr;
896 break;
897 case SP::SUBri:
898 NewOpcode = SP::SUBCCri;
899 break;
900 default:
901 return false;
902 }
903
904 bool IsICCModified = false;
905 MachineBasicBlock::iterator I = MI;
906 MachineBasicBlock::iterator C = CmpInstr;
907 MachineBasicBlock::iterator E = CmpInstr.getParent()->end();
908 const TargetRegisterInfo *TRI = &getRegisterInfo();
909
910 // If ICC is used or modified between MI and CmpInstr we cannot optimize.
911 while (++I != C) {
912 if (I->modifiesRegister(Reg: SP::ICC, TRI) || I->readsRegister(Reg: SP::ICC, TRI))
913 return false;
914 }
915
916 while (++I != E) {
917 // Only allow conditionals on equality.
918 if (I->readsRegister(Reg: SP::ICC, TRI)) {
919 bool IsICCBranch = I->getOpcode() == SP::BCOND ||
920 I->getOpcode() == SP::BPICC ||
921 I->getOpcode() == SP::BPXCC;
922 bool IsICCMove =
923 I->getOpcode() == SP::MOVICCrr || I->getOpcode() == SP::MOVICCri ||
924 I->getOpcode() == SP::MOVXCCrr || I->getOpcode() == SP::MOVXCCri;
925 bool IsICCConditional = IsICCBranch || IsICCMove;
926 if (!IsICCConditional ||
927 (I->getOperand(i: IsICCBranch ? 1 : 3).getImm() != SPCC::ICC_E &&
928 I->getOperand(i: IsICCBranch ? 1 : 3).getImm() != SPCC::ICC_NE))
929 return false;
930 } else if (I->modifiesRegister(Reg: SP::ICC, TRI)) {
931 IsICCModified = true;
932 break;
933 }
934 }
935
936 if (!IsICCModified) {
937 MachineBasicBlock *MBB = CmpInstr.getParent();
938 if (any_of(Range: MBB->successors(),
939 P: [](MachineBasicBlock *Succ) { return Succ->isLiveIn(Reg: SP::ICC); }))
940 return false;
941 }
942
943 if (MRI->hasOneNonDBGUse(RegNo: SrcReg))
944 MI->getOperand(i: 0).setReg(SP::G0);
945
946 MI->setDesc(get(Opcode: NewOpcode));
947 MI->addRegisterDefined(Reg: SP::ICC);
948 CmpInstr.eraseFromParent();
949
950 return true;
951}
952
953bool SparcInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
954 switch (MI.getOpcode()) {
955 case TargetOpcode::LOAD_STACK_GUARD: {
956 assert(Subtarget.getTargetTriple().isOSLinux() &&
957 "Only Linux target is expected to contain LOAD_STACK_GUARD");
958 // offsetof(tcbhead_t, stack_guard) from sysdeps/sparc/nptl/tls.h in glibc.
959 const int64_t Offset = Subtarget.is64Bit() ? 0x28 : 0x14;
960 MI.setDesc(get(Opcode: Subtarget.is64Bit() ? SP::LDXri : SP::LDri));
961 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
962 .addReg(RegNo: SP::G7)
963 .addImm(Val: Offset);
964 return true;
965 }
966 case SP::V8BAR: {
967 assert(!Subtarget.isV9() &&
968 "V8BAR should not be emitted on V9 processors!");
969
970 // Emit stbar; ldstub [%sp-1], %g0
971 // The sequence acts as a full barrier on V8 systems.
972 MachineBasicBlock &MBB = *MI.getParent();
973 MachineInstr &InstSTBAR =
974 *BuildMI(BB&: MBB, I&: MI, MIMD: MI.getDebugLoc(), MCID: get(Opcode: SP::STBAR));
975 MachineInstr &InstLDSTUB =
976 *BuildMI(BB&: MBB, I&: MI, MIMD: MI.getDebugLoc(), MCID: get(Opcode: SP::LDSTUBri), DestReg: SP::G0)
977 .addReg(RegNo: SP::O6)
978 .addImm(Val: -1);
979 MIBundleBuilder(MBB, InstSTBAR, InstLDSTUB);
980 MBB.erase(I: MI);
981 return true;
982 }
983 }
984 return false;
985}
986